Fix nodes joining the VPN after tincctl top started.
[tinc] / src / top.c
1 /*
2     top.c -- Show real-time statistics from a running tincd
3     Copyright (C) 2011 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 #include <curses.h>
23
24 #include "control_common.h"
25 #include "list.h"
26 #include "tincctl.h"
27 #include "top.h"
28 #include "xalloc.h"
29
30 typedef struct nodestats_t {
31         char *name;
32         uint64_t in_packets;
33         uint64_t in_bytes;
34         uint64_t out_packets;
35         uint64_t out_bytes;
36         float in_packets_rate;
37         float in_bytes_rate;
38         float out_packets_rate;
39         float out_bytes_rate;
40         bool known;
41 } nodestats_t;
42
43 static const char *const sortname[] = {
44         "name",
45         "in pkts",
46         "in bytes",
47         "out pkts",
48         "out bytes",
49         "tot pkts",
50         "tot bytes",
51 };
52
53 static int sortmode = 0;
54 static bool cumulative = false;
55
56 static list_t node_list;
57 static struct timeval now, prev, diff;
58 static int delay = 1000;
59 static bool running = true;
60 static bool changed = true;
61 static const char *unit = "bytes";
62 static float scale = 1;
63
64 static void update(int fd) {
65         sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
66         gettimeofday(&now, NULL);
67
68         timersub(&now, &prev, &diff);
69         prev = now;
70         float interval = diff.tv_sec + diff.tv_usec * 1e-6;
71
72         char line[4096];
73         char name[4096];
74         int code;
75         int req;
76         uint64_t in_packets;
77         uint64_t in_bytes;
78         uint64_t out_packets;
79         uint64_t out_bytes;
80
81         for(list_node_t *i = node_list.head; i; i = i->next) {
82                 nodestats_t *node = i->data;
83                 node->known = false;
84         }
85
86         while(recvline(fd, line, sizeof line)) {
87                 int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
88
89                 if(n == 2)
90                         break;
91
92                 if(n != 7) {
93                         endwin();
94                         fprintf(stderr, "Error receiving traffic information\n");
95                         exit(1);
96                 }
97
98                 nodestats_t *found = NULL;
99
100                 for(list_node_t *i = node_list.head; i; i = i->next) {
101                         nodestats_t *node = i->data;
102                         int result = strcmp(name, node->name);
103                         if(result > 0) {
104                                 continue;
105                         } if(result == 0) {
106                                 found = node;
107                                 break;
108                         } else {
109                                 found = xmalloc_and_zero(sizeof *found);
110                                 found->name = xstrdup(name);
111                                 fprintf(stderr, "Inserting %s before %s\n", found->name, node->name);
112                                 list_insert_before(&node_list, i, found);
113                                 changed = true;
114                                 break;
115                         }
116                 }
117
118                 if(!found) {
119                         found = xmalloc_and_zero(sizeof *found);
120                         found->name = xstrdup(name);
121                         list_insert_tail(&node_list, found);
122                         changed = true;
123                 }
124
125                 found->known = true;
126                 found->in_packets_rate = (in_packets - found->in_packets) / interval;
127                 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
128                 found->out_packets_rate = (out_packets - found->out_packets) / interval;
129                 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
130                 found->in_packets = in_packets;
131                 found->in_bytes = in_bytes;
132                 found->out_packets = out_packets;
133                 found->out_bytes = out_bytes;
134         }
135 }
136
137 static void redraw(void) {
138         erase();
139
140         mvprintw(0, 0, "Tinc %-16s  Nodes: %4d  Sort: %-8s  %s", netname, node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
141         attrset(A_REVERSE);
142         mvprintw(2, 0, "Node                IN pkts   IN %s   OUT pkts  OUT %s", unit, unit);
143         chgat(-1, A_REVERSE, 0, NULL);
144
145         static nodestats_t **sorted = 0;
146         static int n = 0;
147         if(changed) {
148                 n = 0;
149                 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
150                 for(list_node_t *i = node_list.head; i; i = i->next)
151                         sorted[n++] = i->data;
152                 changed = false;
153         }
154         
155         int cmpfloat(float a, float b) {
156                 if(a < b)
157                         return -1;
158                 else if(a > b)
159                         return 1;
160                 else
161                         return 0;
162         }
163
164         int cmpu64(uint64_t a, uint64_t b) {
165                 if(a < b)
166                         return -1;
167                 else if(a > b)
168                         return 1;
169                 else
170                         return 0;
171         }
172
173         int sortfunc(const void *a, const void *b) {
174                 const nodestats_t *na = *(const nodestats_t **)a;
175                 const nodestats_t *nb = *(const nodestats_t **)b;
176                 switch(sortmode) {
177                         case 1:
178                                 if(cumulative)
179                                         return -cmpu64(na->in_packets, nb->in_packets);
180                                 else
181                                         return -cmpfloat(na->in_packets_rate, nb->in_packets_rate);
182                         case 2:
183                                 if(cumulative)
184                                         return -cmpu64(na->in_bytes, nb->in_bytes);
185                                 else
186                                         return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate);
187                         case 3:
188                                 if(cumulative)
189                                         return -cmpu64(na->out_packets, nb->out_packets);
190                                 else
191                                         return -cmpfloat(na->out_packets_rate, nb->out_packets_rate);
192                         case 4:
193                                 if(cumulative)
194                                         return -cmpu64(na->out_bytes, nb->out_bytes);
195                                 else
196                                         return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate);
197                         case 5:
198                                 if(cumulative)
199                                         return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets);
200                                 else
201                                         return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate);
202                         case 6:
203                                 if(cumulative)
204                                         return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes);
205                                 else
206                                         return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate);
207                         default:
208                                 return strcmp(na->name, nb->name);
209                 }
210         }
211
212         qsort(sorted, n, sizeof *sorted, sortfunc);
213
214         for(int i = 0, row = 3; i < n; i++, row++) {
215                 nodestats_t *node = sorted[i];
216                 if(node->known)
217                         if(node->in_packets_rate || node->out_packets_rate)
218                                 attrset(A_BOLD);
219                         else
220                                 attrset(A_NORMAL);
221                 else
222                         attrset(A_DIM);
223
224                 if(cumulative)
225                         mvprintw(row, 0, "%-16s %'10"PRIu64" %'10.0f %'10"PRIu64" %'10.0f",
226                                         node->name, node->in_packets, node->in_bytes * scale, node->out_packets, node->out_bytes * scale);
227                 else
228                         mvprintw(row, 0, "%-16s %'10.0f %'10.0f %'10.0f %'10.0f",
229                                         node->name, node->in_packets_rate, node->in_bytes_rate * scale, node->out_packets_rate, node->out_bytes_rate * scale);
230         }
231
232         attrset(A_NORMAL);
233         move(1, 0);
234
235         refresh();
236 }
237
238 void top(int fd) {
239         initscr();
240         timeout(delay);
241
242         while(running) {
243                 update(fd);
244                 redraw();
245
246                 switch(getch()) {
247                         case 's': {
248                                 timeout(-1);
249                                 float input = delay * 1e-3;
250                                 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
251                                 scanw("%f", &input);
252                                 if(input < 0.1)
253                                         input = 0.1;
254                                 delay = input * 1e3;
255                                 timeout(delay);
256                                 break;
257                         }
258                         case 'c':
259                                   cumulative = !cumulative;
260                                   break;
261                         case 'n':
262                                   sortmode = 0;
263                                   break;
264                         case 'i':
265                                   sortmode = 2;
266                                   break;
267                         case 'I':
268                                   sortmode = 1;
269                                   break;
270                         case 'o':
271                                   sortmode = 4;
272                                   break;
273                         case 'O':
274                                   sortmode = 3;
275                                   break;
276                         case 't':
277                                   sortmode = 6;
278                                   break;
279                         case 'T':
280                                   sortmode = 5;
281                                   break;
282                         case 'b':
283                                   unit = "bytes";
284                                   scale = 1;
285                                   break;
286                         case 'k':
287                                   unit = "kbyte";
288                                   scale = 1e-3;
289                                   break;
290                         case 'M':
291                                   unit = "Mbyte";
292                                   scale = 1e-6;
293                                   break;
294                         case 'G':
295                                   unit = "Gbyte";
296                                   scale = 1e-9;
297                                   break;
298                         case 'q':
299                         case 27:
300                         case KEY_BREAK:
301                                 running = false;
302                                 break;
303                         default:
304                                 break;
305                 }
306         }
307
308         endwin();
309 }