sock: add socket management functions
[netsniff-ng.git] / ifpps.c
blob2d0a5b086d525f44cd3a6092f4e8f3c8eadc51c2
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009 - 2013 Daniel Borkmann.
4 * Copyright 2013 Tobias Klauser
5 * Subject to the GPL, version 2.
6 */
8 #include <stdio.h>
9 #include <string.h>
10 #include <curses.h>
11 #include <getopt.h>
12 #include <ctype.h>
13 #include <sys/socket.h>
14 #include <sys/fsuid.h>
15 #include <signal.h>
16 #include <stdint.h>
17 #include <stdlib.h>
18 #include <time.h>
20 #include "die.h"
21 #include "dev.h"
22 #include "xmalloc.h"
23 #include "xutils.h"
24 #include "ioops.h"
25 #include "promisc.h"
26 #include "cpus.h"
27 #include "built_in.h"
29 struct wifi_stat {
30 uint32_t bitrate;
31 int16_t link_qual, link_qual_max;
32 int signal_level /*, noise_level*/;
35 struct ifstat {
36 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
37 long long unsigned int rx_fifo, rx_frame, rx_multi;
38 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
39 long long unsigned int tx_fifo, tx_colls, tx_carrier;
40 uint64_t mem_free, mem_total;
41 uint32_t irq_nr, procs_run, procs_iow, cswitch, forks;
42 struct wifi_stat wifi;
44 * Pointer members need to be last in order for stats_zero() to work
45 * properly.
47 long long unsigned int *irqs, *irqs_srx, *irqs_stx;
48 uint64_t *cpu_user, *cpu_sys, *cpu_nice, *cpu_idle, *cpu_iow;
51 struct cpu_hit {
52 unsigned int idx;
53 uint64_t hit;
54 long long unsigned int irqs_rel, irqs_abs;
57 static volatile sig_atomic_t sigint = 0;
58 static struct ifstat stats_old, stats_new, stats_delta;
59 static struct cpu_hit *cpu_hits;
60 static int stats_loop = 0;
61 static WINDOW *stats_screen = NULL;
63 static const char *short_options = "d:t:n:vhclp";
64 static const struct option long_options[] = {
65 {"dev", required_argument, NULL, 'd'},
66 {"interval", required_argument, NULL, 't'},
67 {"num-cpus", required_argument, NULL, 'n'},
68 {"promisc", no_argument, NULL, 'p'},
69 {"csv", no_argument, NULL, 'c'},
70 {"loop", no_argument, NULL, 'l'},
71 {"version", no_argument, NULL, 'v'},
72 {"help", no_argument, NULL, 'h'},
73 {NULL, 0, NULL, 0}
76 static void signal_handler(int number)
78 switch (number) {
79 case SIGINT:
80 sigint = 1;
81 break;
82 case SIGHUP:
83 default:
84 break;
88 static inline int iswireless(const struct ifstat *stats)
90 return stats->wifi.bitrate > 0;
93 static void __noreturn help(void)
95 printf("\nifpps %s, top-like kernel networking and system statistics\n",
96 VERSION_STRING);
97 puts("http://www.netsniff-ng.org\n\n"
98 "Usage: ifpps [options] || ifpps <netdev>\n"
99 "Options:\n"
100 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
101 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
102 " -n|--num-cpus <num> Number of top hitter CPUs to display\n"
103 " in ncurses mode (default 10)\n"
104 " -p|--promisc Promiscuous mode\n"
105 " -c|--csv Output to terminal as Gnuplot-ready data\n"
106 " -l|--loop Continuous CSV output\n"
107 " -v|--version Print version and exit\n"
108 " -h|--help Print this help and exit\n\n"
109 "Examples:\n"
110 " ifpps eth0\n"
111 " ifpps -pd eth0\n"
112 " ifpps -lpcd wlan0 > plot.dat\n\n"
113 "Note:\n"
114 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
115 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
116 "Please report bugs to <bugs@netsniff-ng.org>\n"
117 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
118 "Swiss federal institute of technology (ETH Zurich)\n"
119 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
120 "License: GNU GPL version 2.0\n"
121 "This is free software: you are free to change and redistribute it.\n"
122 "There is NO WARRANTY, to the extent permitted by law.\n");
123 die();
126 static void __noreturn version(void)
128 printf("\nifpps %s, top-like kernel networking and system statistics\n",
129 VERSION_LONG);
130 puts("http://www.netsniff-ng.org\n\n"
131 "Please report bugs to <bugs@netsniff-ng.org>\n"
132 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
133 "Swiss federal institute of technology (ETH Zurich)\n"
134 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
135 "License: GNU GPL version 2.0\n"
136 "This is free software: you are free to change and redistribute it.\n"
137 "There is NO WARRANTY, to the extent permitted by law.\n");
138 die();
141 static inline int padding_from_num(int n)
143 int i = 0;
144 do i++;
145 while ((n /= 10) > 0);
146 return i;
149 #define STATS_ALLOC1(member) \
150 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
152 static void stats_alloc(struct ifstat *stats, int cpus)
154 STATS_ALLOC1(irqs);
155 STATS_ALLOC1(irqs_srx);
156 STATS_ALLOC1(irqs_stx);
158 STATS_ALLOC1(cpu_user);
159 STATS_ALLOC1(cpu_sys);
160 STATS_ALLOC1(cpu_nice);
161 STATS_ALLOC1(cpu_idle);
162 STATS_ALLOC1(cpu_iow);
165 #define STATS_ZERO1(member) \
166 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
168 static void stats_zero(struct ifstat *stats, int cpus)
170 /* Only clear the non-pointer members */
171 memset(stats, 0, offsetof(struct ifstat, irqs));
173 STATS_ZERO1(irqs);
174 STATS_ZERO1(irqs_srx);
175 STATS_ZERO1(irqs_stx);
177 STATS_ZERO1(cpu_user);
178 STATS_ZERO1(cpu_sys);
179 STATS_ZERO1(cpu_nice);
180 STATS_ZERO1(cpu_idle);
181 STATS_ZERO1(cpu_iow);
184 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
186 int ret = -EINVAL;
187 char buff[256];
188 FILE *fp;
190 fp = fopen("/proc/net/dev", "r");
191 if (!fp)
192 panic("Cannot open /proc/net/dev!\n");
194 if (fgets(buff, sizeof(buff), fp)) { ; }
195 if (fgets(buff, sizeof(buff), fp)) { ; }
197 memset(buff, 0, sizeof(buff));
199 while (fgets(buff, sizeof(buff), fp) != NULL) {
200 buff[sizeof(buff) -1] = 0;
202 if (strstr(buff, ifname) == NULL)
203 continue;
205 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
206 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
207 &stats->rx_bytes, &stats->rx_packets,
208 &stats->rx_errors, &stats->rx_drops,
209 &stats->rx_fifo, &stats->rx_frame,
210 &stats->rx_multi, &stats->tx_bytes,
211 &stats->tx_packets, &stats->tx_errors,
212 &stats->tx_drops, &stats->tx_fifo,
213 &stats->tx_colls, &stats->tx_carrier) == 14) {
214 ret = 0;
215 break;
218 memset(buff, 0, sizeof(buff));
221 fclose(fp);
222 return ret;
225 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
227 int ret = -EINVAL, i, cpus, try = 0;
228 char *ptr, *buff;
229 bool seen = false;
230 size_t buff_len;
231 struct ethtool_drvinfo drvinf;
232 FILE *fp;
234 fp = fopen("/proc/interrupts", "r");
235 if (!fp)
236 panic("Cannot open /proc/interrupts!\n");
238 cpus = get_number_cpus();
239 buff_len = cpus * 128;
240 buff = xmalloc(buff_len);
241 retry:
242 fseek(fp, 0, SEEK_SET);
243 memset(buff, 0, buff_len);
245 while (fgets(buff, buff_len, fp) != NULL) {
246 buff[buff_len - 1] = 0;
247 ptr = buff;
249 if (strstr(buff, ifname) == NULL)
250 continue;
252 /* XXX: remove this one here */
253 stats->irq_nr = strtol(ptr, &ptr, 10);
254 bug_on(stats->irq_nr == 0);
256 if (ptr)
257 ptr++;
258 for (i = 0; i < cpus && ptr; ++i) {
259 if (seen)
260 stats->irqs[i] += strtol(ptr, &ptr, 10);
261 else
262 stats->irqs[i] = strtol(ptr, &ptr, 10);
263 if (i == cpus - 1) {
264 ret = 0;
265 seen = true;
269 memset(buff, 0, buff_len);
272 if (ret == -EINVAL && try == 0) {
273 memset(&drvinf, 0, sizeof(drvinf));
274 if (ethtool_drvinf(ifname, &drvinf) < 0)
275 goto done;
277 ifname = drvinf.driver;
278 try++;
280 goto retry;
282 done:
283 xfree(buff);
284 fclose(fp);
285 return ret;
288 static int stats_proc_softirqs(struct ifstat *stats)
290 int i, cpus;
291 char *ptr, *buff;
292 size_t buff_len;
293 FILE *fp;
294 enum {
295 softirqs_net_rx,
296 softirqs_net_tx,
297 } net_type;
299 fp = fopen("/proc/softirqs", "r");
300 if (!fp)
301 panic("Cannot open /proc/softirqs!\n");
303 cpus = get_number_cpus();
304 buff_len = cpus * 128;
305 buff = xmalloc(buff_len);
307 memset(buff, 0, buff_len);
309 while (fgets(buff, buff_len, fp) != NULL) {
310 buff[buff_len - 1] = 0;
312 if ((ptr = strstr(buff, "NET_TX:")))
313 net_type = softirqs_net_tx;
314 else if ((ptr = strstr(buff, "NET_RX:")))
315 net_type = softirqs_net_rx;
316 else
317 continue;
319 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
320 switch (net_type) {
321 case softirqs_net_tx:
322 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
323 break;
324 case softirqs_net_rx:
325 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
326 break;
330 memset(buff, 0, buff_len);
333 xfree(buff);
334 fclose(fp);
335 return 0;
338 static int stats_proc_memory(struct ifstat *stats)
340 char *ptr, buff[256];
341 FILE *fp;
343 fp = fopen("/proc/meminfo", "r");
344 if (!fp)
345 panic("Cannot open /proc/meminfo!\n");
347 memset(buff, 0, sizeof(buff));
349 while (fgets(buff, sizeof(buff), fp) != NULL) {
350 buff[sizeof(buff) - 1] = 0;
352 if ((ptr = strstr(buff, "MemTotal:"))) {
353 ptr += strlen("MemTotal:");
354 stats->mem_total = strtoul(ptr, &ptr, 10);
355 } else if ((ptr = strstr(buff, "MemFree:"))) {
356 ptr += strlen("MemFree:");
357 stats->mem_free = strtoul(ptr, &ptr, 10);
360 memset(buff, 0, sizeof(buff));
363 fclose(fp);
364 return 0;
367 static int stats_proc_system(struct ifstat *stats)
369 int cpu, cpus;
370 char *ptr, buff[256];
371 FILE *fp;
373 fp = fopen("/proc/stat", "r");
374 if (!fp)
375 panic("Cannot open /proc/stat!\n");
377 cpus = get_number_cpus();
379 memset(buff, 0, sizeof(buff));
381 while (fgets(buff, sizeof(buff), fp) != NULL) {
382 buff[sizeof(buff) - 1] = 0;
384 if ((ptr = strstr(buff, "cpu"))) {
385 ptr += strlen("cpu");
386 if (isblank(*ptr))
387 goto next;
389 cpu = strtol(ptr, &ptr, 10);
390 bug_on(cpu > cpus);
392 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
393 &stats->cpu_user[cpu],
394 &stats->cpu_nice[cpu],
395 &stats->cpu_sys[cpu],
396 &stats->cpu_idle[cpu],
397 &stats->cpu_iow[cpu]) != 5)
398 goto next;
399 } else if ((ptr = strstr(buff, "ctxt"))) {
400 ptr += strlen("ctxt");
401 stats->cswitch = strtoul(ptr, &ptr, 10);
402 } else if ((ptr = strstr(buff, "processes"))) {
403 ptr += strlen("processes");
404 stats->forks = strtoul(ptr, &ptr, 10);
405 } else if ((ptr = strstr(buff, "procs_running"))) {
406 ptr += strlen("procs_running");
407 stats->procs_run = strtoul(ptr, &ptr, 10);
408 } else if ((ptr = strstr(buff, "procs_blocked"))) {
409 ptr += strlen("procs_blocked");
410 stats->procs_iow = strtoul(ptr, &ptr, 10);
412 next:
413 memset(buff, 0, sizeof(buff));
416 fclose(fp);
417 return 0;
420 static int adjust_dbm_level(int in_dbm, int dbm_val)
422 if (!in_dbm)
423 return dbm_val;
425 return dbm_val - 0x100;
428 static int stats_wireless(const char *ifname, struct ifstat *stats)
430 int ret;
431 struct iw_statistics ws;
433 ret = wireless_sigqual(ifname, &ws);
434 if (ret != 0) {
435 stats->wifi.bitrate = 0;
436 return -EINVAL;
439 stats->wifi.bitrate = wireless_bitrate(ifname);
441 stats->wifi.signal_level =
442 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
444 stats->wifi.link_qual = ws.qual.qual;
445 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
447 return ret;
450 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
451 #define DIFF(member) do { \
452 if (sizeof(diff->member) != sizeof(new->member) || \
453 sizeof(diff->member) != sizeof(old->member)) \
454 bug(); \
455 bug_on((new->member - old->member) > (new->member)); \
456 DIFF1(member); \
457 } while (0)
459 static void stats_diff(struct ifstat *old, struct ifstat *new,
460 struct ifstat *diff)
462 int cpus, i;
464 DIFF(rx_bytes);
465 DIFF(rx_packets);
466 DIFF(rx_drops);
467 DIFF(rx_errors);
468 DIFF(rx_fifo);
469 DIFF(rx_frame);
470 DIFF(rx_multi);
472 DIFF(tx_bytes);
473 DIFF(tx_bytes);
474 DIFF(tx_packets);
475 DIFF(tx_drops);
476 DIFF(tx_errors);
477 DIFF(tx_fifo);
478 DIFF(tx_colls);
479 DIFF(tx_carrier);
481 DIFF1(procs_run);
482 DIFF1(procs_iow);
484 DIFF1(wifi.signal_level);
485 DIFF1(wifi.link_qual);
487 DIFF1(cswitch);
488 DIFF1(forks);
490 cpus = get_number_cpus();
492 for (i = 0; i < cpus; ++i) {
493 DIFF(irqs[i]);
494 DIFF(irqs_srx[i]);
495 DIFF(irqs_stx[i]);
497 DIFF1(cpu_user[i]);
498 DIFF1(cpu_nice[i]);
499 DIFF1(cpu_sys[i]);
500 DIFF1(cpu_idle[i]);
501 DIFF1(cpu_iow[i]);
505 static void stats_fetch(const char *ifname, struct ifstat *stats)
507 if (stats_proc_net_dev(ifname, stats) < 0)
508 panic("Cannot fetch device stats!\n");
509 if (stats_proc_softirqs(stats) < 0)
510 panic("Cannot fetch software interrupts!\n");
511 if (stats_proc_memory(stats) < 0)
512 panic("Cannot fetch memory stats!\n");
513 if (stats_proc_system(stats) < 0)
514 panic("Cannot fetch system stats!\n");
516 stats_proc_interrupts((char *) ifname, stats);
518 stats_wireless(ifname, stats);
521 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
523 int cpus = get_number_cpus();
525 stats_zero(&stats_old, cpus);
526 stats_zero(&stats_new, cpus);
527 stats_zero(&stats_delta, cpus);
529 stats_fetch(ifname, &stats_old);
530 usleep(ms_interval * 1000);
531 stats_fetch(ifname, &stats_new);
533 stats_diff(&stats_old, &stats_new, &stats_delta);
536 static int cmp_hits(const void *p1, const void *p2)
538 const struct cpu_hit *h1 = p1, *h2 = p2;
541 * We want the hits sorted in descending order, thus reverse the return
542 * values.
544 if (h1->hit == h2->hit)
545 return 0;
546 else if (h1->hit < h2->hit)
547 return 1;
548 else
549 return -1;
552 static int cmp_irqs_rel(const void *p1, const void *p2)
554 const struct cpu_hit *h1 = p1, *h2 = p2;
557 * We want the hits sorted in descending order, thus reverse the return
558 * values.
560 if (h1->irqs_rel == h2->irqs_rel)
561 return 0;
562 else if (h1->irqs_rel < h2->irqs_rel)
563 return 1;
564 else
565 return -1;
568 static int cmp_irqs_abs(const void *p1, const void *p2)
570 const struct cpu_hit *h1 = p1, *h2 = p2;
573 * We want the hits sorted in descending order, thus reverse the return
574 * values.
576 if (h1->irqs_abs == h2->irqs_abs)
577 return 0;
578 else if (h1->irqs_abs < h2->irqs_abs)
579 return 1;
580 else
581 return -1;
584 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
585 int top_cpus)
587 int i;
589 for (i = 0; i < top_cpus; ++i) {
590 cpu_hits[i].idx = i;
591 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
592 cpu_hits[i].irqs_rel = rel->irqs[i];
593 cpu_hits[i].irqs_abs = abs->irqs[i];
597 static void screen_init(WINDOW **screen)
599 (*screen) = initscr();
601 raw();
602 noecho();
603 cbreak();
604 nodelay((*screen), TRUE);
606 keypad(stdscr, TRUE);
608 refresh();
609 wrefresh((*screen));
612 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
613 uint64_t ms_interval, unsigned int top_cpus)
615 size_t len = 0;
616 char buff[64];
617 struct ethtool_drvinfo drvinf;
618 u32 rate = device_bitrate(ifname);
619 int link = ethtool_link(ifname);
621 memset(&drvinf, 0, sizeof(drvinf));
622 ethtool_drvinf(ifname, &drvinf);
624 memset(buff, 0, sizeof(buff));
625 if (rate)
626 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
627 if (link >= 0)
628 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
629 link == 0 ? "no" : "yes");
631 mvwprintw(screen, (*voff)++, 2,
632 "Kernel net/sys statistics for %s (%s%s), t=%lums, cpus=%u/%u"
633 " ",
634 ifname, drvinf.driver, buff, ms_interval, top_cpus, get_number_cpus());
637 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
638 int *voff)
640 attron(A_REVERSE);
642 mvwprintw(screen, (*voff)++, 0,
643 " rx: %16.3llf MiB/t "
644 "%10llu pkts/t "
645 "%10llu drops/t "
646 "%10llu errors/t ",
647 ((long double) rel->rx_bytes) / (1LLU << 20),
648 rel->rx_packets, rel->rx_drops, rel->rx_errors);
650 mvwprintw(screen, (*voff)++, 0,
651 " tx: %16.3llf MiB/t "
652 "%10llu pkts/t "
653 "%10llu drops/t "
654 "%10llu errors/t ",
655 ((long double) rel->tx_bytes) / (1LLU << 20),
656 rel->tx_packets, rel->tx_drops, rel->tx_errors);
658 attroff(A_REVERSE);
661 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
662 int *voff)
664 mvwprintw(screen, (*voff)++, 2,
665 "rx: %16.3llf MiB "
666 "%10llu pkts "
667 "%10llu drops "
668 "%10llu errors",
669 ((long double) abs->rx_bytes) / (1LLU << 20),
670 abs->rx_packets, abs->rx_drops, abs->rx_errors);
672 mvwprintw(screen, (*voff)++, 2,
673 "tx: %16.3llf MiB "
674 "%10llu pkts "
675 "%10llu drops "
676 "%10llu errors",
677 ((long double) abs->tx_bytes) / (1LLU << 20),
678 abs->tx_packets, abs->tx_drops, abs->tx_errors);
681 static void screen_sys_mem(WINDOW *screen, const struct ifstat *rel,
682 const struct ifstat *abs, int *voff)
684 mvwprintw(screen, (*voff)++, 2,
685 "sys: %14u cs/t "
686 "%10.1lf%% mem "
687 "%13u running "
688 "%10u iowait",
689 rel->cswitch,
690 (100.0 * (abs->mem_total - abs->mem_free)) / abs->mem_total,
691 abs->procs_run, abs->procs_iow);
694 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
695 int top_cpus, int *voff)
697 int i;
698 uint64_t all;
699 int max_padd = padding_from_num(get_number_cpus());
701 for (i = 0; i < top_cpus; ++i) {
702 unsigned int idx = cpu_hits[i].idx;
704 all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
705 rel->cpu_idle[idx] + rel->cpu_iow[idx];
707 mvwprintw(screen, (*voff)++, 2,
708 "cpu%*d: %13.1lf%% usr/t "
709 "%9.1lf%% sys/t "
710 "%10.1lf%% idl/t "
711 "%11.1lf%% iow/t ", max_padd, idx,
712 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
713 100.0 * rel->cpu_sys[idx] / all,
714 100.0 * rel->cpu_idle[idx] / all,
715 100.0 * rel->cpu_iow[idx] / all);
719 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
720 int top_cpus, int *voff)
722 int i;
723 int max_padd = padding_from_num(get_number_cpus());
725 for (i = 0; i < top_cpus; ++i) {
726 unsigned int idx = cpu_hits[i].idx;
728 mvwprintw(screen, (*voff)++, 2,
729 "cpu%*d: %14llu irqs/t "
730 "%15llu sirq rx/t "
731 "%15llu sirq tx/t ", max_padd, idx,
732 rel->irqs[idx],
733 rel->irqs_srx[idx],
734 rel->irqs_stx[idx]);
738 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
739 int top_cpus, int *voff)
741 int i;
742 int max_padd = padding_from_num(get_number_cpus());
744 for (i = 0; i < top_cpus; ++i) {
745 unsigned int idx = cpu_hits[i].idx;
747 mvwprintw(screen, (*voff)++, 2,
748 "cpu%*d: %14llu irqs", max_padd, idx,
749 abs->irqs[idx]);
753 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
754 const struct ifstat *abs, int *voff)
756 if (iswireless(abs)) {
757 mvwprintw(screen, (*voff)++, 2,
758 "linkqual: %7d/%d (%d/t) ",
759 abs->wifi.link_qual,
760 abs->wifi.link_qual_max,
761 rel->wifi.link_qual);
763 mvwprintw(screen, (*voff)++, 2,
764 "signal: %8d dBm (%d dBm/t) ",
765 abs->wifi.signal_level,
766 rel->wifi.signal_level);
770 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
771 const struct ifstat *abs, int *first, uint64_t ms_interval,
772 unsigned int top_cpus)
774 int cpus, top, voff = 1, cvoff = 2;
776 curs_set(0);
778 cpus = get_number_cpus();
779 top = min(cpus, top_cpus);
781 stats_top(rel, abs, cpus);
783 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
785 screen_header(screen, ifname, &voff, ms_interval, top_cpus);
787 voff++;
788 screen_net_dev_rel(screen, rel, &voff);
790 voff++;
791 screen_net_dev_abs(screen, abs, &voff);
793 voff++;
794 screen_sys_mem(screen, rel, abs, &voff);
796 voff++;
797 screen_percpu_states(screen, rel, top, &voff);
799 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
801 voff++;
802 screen_percpu_irqs_rel(screen, rel, top, &voff);
804 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
806 voff++;
807 screen_percpu_irqs_abs(screen, abs, top, &voff);
809 voff++;
810 screen_wireless(screen, rel, abs, &voff);
812 if (*first) {
813 mvwprintw(screen, cvoff, 2, "Collecting data ...");
814 *first = 0;
815 } else {
816 mvwprintw(screen, cvoff, 2, " ");
819 wrefresh(screen);
820 refresh();
823 static void screen_end(void)
825 endwin();
828 static int screen_main(const char *ifname, uint64_t ms_interval,
829 unsigned int top_cpus)
831 int first = 1, key;
833 screen_init(&stats_screen);
835 while (!sigint) {
836 key = getch();
837 if (key == 'q' || key == 0x1b || key == KEY_F(10))
838 break;
840 screen_update(stats_screen, ifname, &stats_delta, &stats_new,
841 &first, ms_interval, top_cpus);
843 stats_sample_generic(ifname, ms_interval);
846 screen_end();
848 return 0;
851 static void term_csv(const char *ifname, const struct ifstat *rel,
852 const struct ifstat *abs, uint64_t ms_interval)
854 int cpus, i;
856 printf("%ld ", time(0));
858 printf("%llu ", rel->rx_bytes);
859 printf("%llu ", rel->rx_packets);
860 printf("%llu ", rel->rx_drops);
861 printf("%llu ", rel->rx_errors);
863 printf("%llu ", abs->rx_bytes);
864 printf("%llu ", abs->rx_packets);
865 printf("%llu ", abs->rx_drops);
866 printf("%llu ", abs->rx_errors);
868 printf("%llu ", rel->tx_bytes);
869 printf("%llu ", rel->tx_packets);
870 printf("%llu ", rel->tx_drops);
871 printf("%llu ", rel->tx_errors);
873 printf("%llu ", abs->tx_bytes);
874 printf("%llu ", abs->tx_packets);
875 printf("%llu ", abs->tx_drops);
876 printf("%llu ", abs->tx_errors);
878 printf("%u ", rel->cswitch);
879 printf("%lu ", abs->mem_free);
880 printf("%lu ", abs->mem_total - abs->mem_free);
881 printf("%lu ", abs->mem_total);
882 printf("%u ", abs->procs_run);
883 printf("%u ", abs->procs_iow);
885 cpus = get_number_cpus();
887 for (i = 0; i < cpus; ++i) {
888 printf("%lu ", rel->cpu_user[i]);
889 printf("%lu ", rel->cpu_nice[i]);
890 printf("%lu ", rel->cpu_sys[i]);
891 printf("%lu ", rel->cpu_idle[i]);
892 printf("%lu ", rel->cpu_iow[i]);
894 printf("%llu ", rel->irqs[i]);
895 printf("%llu ", abs->irqs[i]);
897 printf("%llu ", rel->irqs_srx[i]);
898 printf("%llu ", abs->irqs_srx[i]);
900 printf("%llu ", rel->irqs_stx[i]);
901 printf("%llu ", abs->irqs_stx[i]);
904 if (iswireless(abs)) {
905 printf("%u ", rel->wifi.link_qual);
906 printf("%u ", abs->wifi.link_qual);
907 printf("%u ", abs->wifi.link_qual_max);
909 printf("%d ", rel->wifi.signal_level);
910 printf("%d ", abs->wifi.signal_level);
913 puts("");
914 fflush(stdout);
917 static void term_csv_header(const char *ifname, const struct ifstat *abs,
918 uint64_t ms_interval)
920 int cpus, i, j = 1;
922 printf("# gnuplot dump (#col:description)\n");
923 printf("# networking interface: %s\n", ifname);
924 printf("# sampling interval (t): %lu ms\n", ms_interval);
925 printf("# %d:unixtime ", j++);
927 printf("%d:rx-bytes-per-t ", j++);
928 printf("%d:rx-pkts-per-t ", j++);
929 printf("%d:rx-drops-per-t ", j++);
930 printf("%d:rx-errors-per-t ", j++);
932 printf("%d:rx-bytes ", j++);
933 printf("%d:rx-pkts ", j++);
934 printf("%d:rx-drops ", j++);
935 printf("%d:rx-errors ", j++);
937 printf("%d:tx-bytes-per-t ", j++);
938 printf("%d:tx-pkts-per-t ", j++);
939 printf("%d:tx-drops-per-t ", j++);
940 printf("%d:tx-errors-per-t ", j++);
942 printf("%d:tx-bytes ", j++);
943 printf("%d:tx-pkts ", j++);
944 printf("%d:tx-drops ", j++);
945 printf("%d:tx-errors ", j++);
947 printf("%d:context-switches-per-t ", j++);
948 printf("%d:mem-free ", j++);
949 printf("%d:mem-used ", j++);
950 printf("%d:mem-total ", j++);
951 printf("%d:procs-in-run ", j++);
952 printf("%d:procs-in-iow ", j++);
954 cpus = get_number_cpus();
956 for (i = 0, j = 22; i < cpus; ++i) {
957 printf("%d:cpu%i-usr-per-t ", j++, i);
958 printf("%d:cpu%i-nice-per-t ", j++, i);
959 printf("%d:cpu%i-sys-per-t ", j++, i);
960 printf("%d:cpu%i-idle-per-t ", j++, i);
961 printf("%d:cpu%i-iow-per-t ", j++, i);
963 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
964 printf("%d:cpu%i-net-irqs ", j++, i);
966 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
967 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
968 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
969 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
972 if (iswireless(abs)) {
973 printf("%d:wifi-link-qual-per-t ", j++);
974 printf("%d:wifi-link-qual ", j++);
975 printf("%d:wifi-link-qual-max ", j++);
977 printf("%d:wifi-signal-dbm-per-t ", j++);
978 printf("%d:wifi-signal-dbm ", j++);
981 puts("");
982 printf("# data:\n");
983 fflush(stdout);
986 static int term_main(const char *ifname, uint64_t ms_interval,
987 unsigned int top_cpus __maybe_unused)
989 int first = 1;
991 do {
992 stats_sample_generic(ifname, ms_interval);
994 if (first) {
995 first = 0;
996 term_csv_header(ifname, &stats_new, ms_interval);
999 term_csv(ifname, &stats_delta, &stats_new, ms_interval);
1000 } while (stats_loop && !sigint);
1002 return 0;
1005 int main(int argc, char **argv)
1007 short ifflags = 0;
1008 int c, opt_index, ret, cpus, promisc = 0;
1009 unsigned int top_cpus = 10;
1010 uint64_t interval = 1000;
1011 char *ifname = NULL;
1012 int (*func_main)(const char *ifname, uint64_t ms_interval,
1013 unsigned int top_cpus) = screen_main;
1015 setfsuid(getuid());
1016 setfsgid(getgid());
1018 while ((c = getopt_long(argc, argv, short_options, long_options,
1019 &opt_index)) != EOF) {
1020 switch (c) {
1021 case 'h':
1022 help();
1023 break;
1024 case 'v':
1025 version();
1026 break;
1027 case 'd':
1028 ifname = xstrndup(optarg, IFNAMSIZ);
1029 break;
1030 case 't':
1031 interval = strtoul(optarg, NULL, 10);
1032 break;
1033 case 'n':
1034 top_cpus = strtoul(optarg, NULL, 10);
1035 break;
1036 case 'l':
1037 stats_loop = 1;
1038 break;
1039 case 'p':
1040 promisc = 1;
1041 break;
1042 case 'c':
1043 func_main = term_main;
1044 break;
1045 case '?':
1046 switch (optopt) {
1047 case 'd':
1048 case 't':
1049 panic("Option -%c requires an argument!\n",
1050 optopt);
1051 default:
1052 if (isprint(optopt))
1053 printf("Unknown option character `0x%X\'!\n", optopt);
1054 die();
1056 default:
1057 break;
1061 if (argc == 1)
1062 help();
1064 if (argc == 2)
1065 ifname = xstrndup(argv[1], IFNAMSIZ);
1066 if (ifname == NULL)
1067 panic("No networking device given!\n");
1069 if (!strncmp("lo", ifname, IFNAMSIZ))
1070 panic("lo is not supported!\n");
1071 if (device_mtu(ifname) == 0)
1072 panic("This is no networking device!\n");
1074 register_signal(SIGINT, signal_handler);
1075 register_signal(SIGHUP, signal_handler);
1077 cpus = get_number_cpus();
1078 top_cpus = min(top_cpus, cpus);
1080 stats_alloc(&stats_old, cpus);
1081 stats_alloc(&stats_new, cpus);
1082 stats_alloc(&stats_delta, cpus);
1084 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1086 if (promisc)
1087 ifflags = enter_promiscuous_mode(ifname);
1088 ret = func_main(ifname, interval, top_cpus);
1089 if (promisc)
1090 leave_promiscuous_mode(ifname, ifflags);
1092 xfree(ifname);
1093 return ret;