Fix sending empty SPTPS records.
[tinc] / src / top.c
1 /*
2     top.c -- Show real-time statistics from a running tincd
3     Copyright (C) 2011-2013 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #ifdef HAVE_CURSES
23
24 #include <curses.h>
25
26 #include "control_common.h"
27 #include "list.h"
28 #include "names.h"
29 #include "tincctl.h"
30 #include "top.h"
31 #include "xalloc.h"
32
33 typedef struct nodestats_t {
34         char *name;
35         int i;
36         uint64_t in_packets;
37         uint64_t in_bytes;
38         uint64_t out_packets;
39         uint64_t out_bytes;
40         float in_packets_rate;
41         float in_bytes_rate;
42         float out_packets_rate;
43         float out_bytes_rate;
44         bool known;
45 } nodestats_t;
46
47 static const char *const sortname[] = {
48         "name",
49         "in pkts",
50         "in bytes",
51         "out pkts",
52         "out bytes",
53         "tot pkts",
54         "tot bytes",
55 };
56
57 static int sortmode = 0;
58 static bool cumulative = false;
59
60 static list_t node_list;
61 static struct timeval cur, prev, diff;
62 static int delay = 1000;
63 static bool changed = true;
64 static const char *bunit = "bytes";
65 static float bscale = 1;
66 static const char *punit = "pkts";
67 static float pscale = 1;
68
69 static void update(int fd) {
70         sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
71         gettimeofday(&cur, NULL);
72
73         timersub(&cur, &prev, &diff);
74         prev = cur;
75         float interval = diff.tv_sec + diff.tv_usec * 1e-6;
76
77         char line[4096];
78         char name[4096];
79         int code;
80         int req;
81         uint64_t in_packets;
82         uint64_t in_bytes;
83         uint64_t out_packets;
84         uint64_t out_bytes;
85
86         for list_each(nodestats_t, ns, &node_list)
87                 ns->known = false;
88
89         while(recvline(fd, line, sizeof line)) {
90                 int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
91
92                 if(n == 2)
93                         break;
94
95                 if(n != 7) {
96                         endwin();
97                         fprintf(stderr, "Error receiving traffic information\n");
98                         exit(1);
99                 }
100
101                 nodestats_t *found = NULL;
102
103                 for list_each(nodestats_t, ns, &node_list) {
104                         int result = strcmp(name, ns->name);
105                         if(result > 0) {
106                                 continue;
107                         } if(result == 0) {
108                                 found = ns;
109                                 break;
110                         } else {
111                                 found = xzalloc(sizeof *found);
112                                 found->name = xstrdup(name);
113                                 list_insert_before(&node_list, node, found);
114                                 changed = true;
115                                 break;
116                         }
117                 }
118
119                 if(!found) {
120                         found = xzalloc(sizeof *found);
121                         found->name = xstrdup(name);
122                         list_insert_tail(&node_list, found);
123                         changed = true;
124                 }
125
126                 found->known = true;
127                 found->in_packets_rate = (in_packets - found->in_packets) / interval;
128                 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
129                 found->out_packets_rate = (out_packets - found->out_packets) / interval;
130                 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
131                 found->in_packets = in_packets;
132                 found->in_bytes = in_bytes;
133                 found->out_packets = out_packets;
134                 found->out_bytes = out_bytes;
135         }
136 }
137
138 static int cmpfloat(float a, float b) {
139         if(a < b)
140                 return -1;
141         else if(a > b)
142                 return 1;
143         else
144                 return 0;
145 }
146
147 static int cmpu64(uint64_t a, uint64_t b) {
148         if(a < b)
149                 return -1;
150         else if(a > b)
151                 return 1;
152         else
153                 return 0;
154 }
155
156 static int sortfunc(const void *a, const void *b) {
157         const nodestats_t *na = *(const nodestats_t **)a;
158         const nodestats_t *nb = *(const nodestats_t **)b;
159         switch(sortmode) {
160                 case 1:
161                         if(cumulative)
162                                 return -cmpu64(na->in_packets, nb->in_packets) ?: na->i - nb->i;
163                         else
164                                 return -cmpfloat(na->in_packets_rate, nb->in_packets_rate) ?: na->i - nb->i;
165                 case 2:
166                         if(cumulative)
167                                 return -cmpu64(na->in_bytes, nb->in_bytes) ?: na->i - nb->i;
168                         else
169                                 return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate) ?: na->i - nb->i;
170                 case 3:
171                         if(cumulative)
172                                 return -cmpu64(na->out_packets, nb->out_packets) ?: na->i - nb->i;
173                         else
174                                 return -cmpfloat(na->out_packets_rate, nb->out_packets_rate) ?: na->i - nb->i;
175                 case 4:
176                         if(cumulative)
177                                 return -cmpu64(na->out_bytes, nb->out_bytes) ?: na->i - nb->i;
178                         else
179                                 return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate) ?: na->i - nb->i;
180                 case 5:
181                         if(cumulative)
182                                 return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets) ?: na->i - nb->i;
183                         else
184                                 return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate) ?: na->i - nb->i;
185                 case 6:
186                         if(cumulative)
187                                 return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes) ?: na->i - nb->i;
188                         else
189                                 return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate) ?: na->i - nb->i;
190                 default:
191                         return strcmp(na->name, nb->name) ?: na->i - nb->i;
192         }
193 }
194
195 static void redraw(void) {
196         erase();
197
198         mvprintw(0, 0, "Tinc %-16s  Nodes: %4d  Sort: %-10s  %s", netname ?: "", node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
199         attrset(A_REVERSE);
200         mvprintw(2, 0, "Node                IN %s   IN %s   OUT %s  OUT %s", punit, bunit, punit, bunit);
201         chgat(-1, A_REVERSE, 0, NULL);
202
203         static nodestats_t **sorted = 0;
204         static int n = 0;
205         if(changed) {
206                 n = 0;
207                 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
208                 for list_each(nodestats_t, ns, &node_list)
209                         sorted[n++] = ns;
210                 changed = false;
211         }
212
213         for(int i = 0; i < n; i++)
214                 sorted[i]->i = i;
215
216         qsort(sorted, n, sizeof *sorted, sortfunc);
217
218         for(int i = 0, row = 3; i < n; i++, row++) {
219                 nodestats_t *node = sorted[i];
220                 if(node->known)
221                         if(node->in_packets_rate || node->out_packets_rate)
222                                 attrset(A_BOLD);
223                         else
224                                 attrset(A_NORMAL);
225                 else
226                         attrset(A_DIM);
227
228                 if(cumulative)
229                         mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
230                                         node->name, node->in_packets * pscale, node->in_bytes * bscale, node->out_packets * pscale, node->out_bytes * bscale);
231                 else
232                         mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
233                                         node->name, node->in_packets_rate * pscale, node->in_bytes_rate * bscale, node->out_packets_rate * pscale, node->out_bytes_rate * bscale);
234         }
235
236         attrset(A_NORMAL);
237         move(1, 0);
238
239         refresh();
240 }
241
242 void top(int fd) {
243         initscr();
244         timeout(delay);
245         bool running = true;
246
247         while(running) {
248                 update(fd);
249                 redraw();
250
251                 switch(getch()) {
252                         case 's': {
253                                 timeout(-1);
254                                 float input = delay * 1e-3;
255                                 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
256                                 scanw("%f", &input);
257                                 if(input < 0.1)
258                                         input = 0.1;
259                                 delay = input * 1e3;
260                                 timeout(delay);
261                                 break;
262                         }
263                         case 'c':
264                                 cumulative = !cumulative;
265                                 break;
266                         case 'n':
267                                 sortmode = 0;
268                                 break;
269                         case 'i':
270                                 sortmode = 2;
271                                 break;
272                         case 'I':
273                                 sortmode = 1;
274                                 break;
275                         case 'o':
276                                 sortmode = 4;
277                                 break;
278                         case 'O':
279                                 sortmode = 3;
280                                 break;
281                         case 't':
282                                 sortmode = 6;
283                                 break;
284                         case 'T':
285                                 sortmode = 5;
286                                 break;
287                         case 'b':
288                                 bunit = "bytes";
289                                 bscale = 1;
290                                 punit = "pkts";
291                                 pscale = 1;
292                                 break;
293                         case 'k':
294                                 bunit = "kbyte";
295                                 bscale = 1e-3;
296                                 punit = "pkts";
297                                 pscale = 1;
298                                 break;
299                         case 'M':
300                                 bunit = "Mbyte";
301                                 bscale = 1e-6;
302                                 punit = "kpkt";
303                                 pscale = 1e-3;
304                                 break;
305                         case 'G':
306                                 bunit = "Gbyte";
307                                 bscale = 1e-9;
308                                 punit = "Mpkt";
309                                 pscale = 1e-6;
310                                 break;
311                         case 'q':
312                         case KEY_BREAK:
313                                 running = false;
314                                 break;
315                         default:
316                                 break;
317                 }
318         }
319
320         endwin();
321 }
322
323 #endif