docs: install: Fix a minor typo
[netsniff-ng.git] / ifpps.c
blob6f50bede7c0b8e64b96feb90a68fa0badd1c4f20
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 <sys/types.h>
16 #include <sys/utsname.h>
17 #include <signal.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <time.h>
21 #include <dirent.h>
23 #include "die.h"
24 #include "dev.h"
25 #include "sig.h"
26 #include "str.h"
27 #include "link.h"
28 #include "xmalloc.h"
29 #include "ioops.h"
30 #include "cpus.h"
31 #include "config.h"
32 #include "built_in.h"
33 #include "screen.h"
35 struct wifi_stat {
36 uint32_t bitrate;
37 int16_t link_qual, link_qual_max;
38 int signal_level /*, noise_level*/;
41 struct ifstat {
42 long long unsigned int rx_bytes, rx_packets, rx_drops, rx_errors;
43 long long unsigned int rx_fifo, rx_frame, rx_multi;
44 long long unsigned int tx_bytes, tx_packets, tx_drops, tx_errors;
45 long long unsigned int tx_fifo, tx_colls, tx_carrier;
46 uint64_t mem_free, mem_total, mem_active, mem_inactive;
47 uint64_t swap_total, swap_free, swap_cached;
48 uint32_t procs_total, procs_run, procs_iow, cswitch;
49 struct wifi_stat wifi;
51 * Pointer members need to be last in order for stats_zero() to work
52 * properly.
54 long long unsigned int *irqs, *irqs_srx, *irqs_stx;
55 uint64_t *cpu_user, *cpu_sys, *cpu_nice, *cpu_idle, *cpu_iow;
58 struct cpu_hit {
59 unsigned int idx;
60 uint64_t hit;
61 long long unsigned int irqs_rel, irqs_abs;
64 struct avg_stat {
65 uint64_t cpu_user, cpu_sys, cpu_nice, cpu_idle, cpu_iow;
66 long double irqs_abs, irqs_rel, irqs_srx_rel, irqs_stx_rel;
69 static volatile sig_atomic_t sigint = 0;
70 static struct ifstat stats_old, stats_new, stats_delta;
71 static struct cpu_hit *cpu_hits;
72 static struct avg_stat stats_avg;
73 static int stats_loop = 0;
74 static int show_median = 0, show_percentage = 0;
75 static WINDOW *stats_screen = NULL;
76 static struct utsname uts;
78 static const char *short_options = "d:n:t:clmopPWvh";
79 static const struct option long_options[] = {
80 {"dev", required_argument, NULL, 'd'},
81 {"num-cpus", required_argument, NULL, 'n'},
82 {"interval", required_argument, NULL, 't'},
83 {"csv", no_argument, NULL, 'c'},
84 {"loop", no_argument, NULL, 'l'},
85 {"median", no_argument, NULL, 'm'},
86 {"omit-header", no_argument, NULL, 'o'},
87 {"promisc", no_argument, NULL, 'p'},
88 {"percentage", no_argument, NULL, 'P'},
89 {"no-warn", no_argument, NULL, 'W'},
90 {"version", no_argument, NULL, 'v'},
91 {"help", no_argument, NULL, 'h'},
92 {NULL, 0, NULL, 0}
95 static void signal_handler(int number)
97 switch (number) {
98 case SIGINT:
99 case SIGQUIT:
100 case SIGTERM:
101 sigint = 1;
102 break;
103 case SIGHUP:
104 default:
105 break;
109 static inline int iswireless(const struct ifstat *stats)
111 return stats->wifi.bitrate > 0;
114 static void __noreturn help(void)
116 printf("\nifpps %s, top-like kernel networking and system statistics\n",
117 VERSION_STRING);
118 puts("http://www.netsniff-ng.org\n\n"
119 "Usage: ifpps [options] || ifpps <netdev>\n"
120 "Options:\n"
121 " -d|--dev <netdev> Device to fetch statistics for e.g., eth0\n"
122 " -n|--num-cpus <num> Number of top hitter CPUs in ncurses mode (def: 5)\n"
123 " -t|--interval <time> Refresh time in ms (default 1000 ms)\n"
124 " -c|--csv Output to terminal as Gnuplot-ready data\n"
125 " -l|--loop Continuous CSV output\n"
126 " -m|--median Display median values\n"
127 " -o|--omit-header Do not print the CSV header\n"
128 " -p|--promisc Promiscuous mode\n"
129 " -P|--percentage Show percentage of theoretical line rate\n"
130 " -W|--no-warn Suppress warnings\n"
131 " -v|--version Print version and exit\n"
132 " -h|--help Print this help and exit\n\n"
133 "Examples:\n"
134 " ifpps eth0\n"
135 " ifpps -pd eth0\n"
136 " ifpps -lpcd wlan0 > plot.dat\n\n"
137 "Note:\n"
138 " On 10G cards, RX/TX statistics are usually accumulated each > 1sec.\n"
139 " Thus, in those situations, it's good to use a -t of 10sec.\n\n"
140 "Please report bugs to <bugs@netsniff-ng.org>\n"
141 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
142 "Swiss federal institute of technology (ETH Zurich)\n"
143 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
144 "License: GNU GPL version 2.0\n"
145 "This is free software: you are free to change and redistribute it.\n"
146 "There is NO WARRANTY, to the extent permitted by law.\n");
147 die();
150 static void __noreturn version(void)
152 printf("\nifpps %s, Git id: %s\n", VERSION_LONG, GITVERSION);
153 puts("top-like kernel networking and system statistics\n"
154 "http://www.netsniff-ng.org\n\n"
155 "Please report bugs to <bugs@netsniff-ng.org>\n"
156 "Copyright (C) 2009-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>\n"
157 "Swiss federal institute of technology (ETH Zurich)\n"
158 "Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>\n"
159 "License: GNU GPL version 2.0\n"
160 "This is free software: you are free to change and redistribute it.\n"
161 "There is NO WARRANTY, to the extent permitted by law.\n");
162 die();
165 static inline int padding_from_num(int n)
167 int i = 0;
168 do {
169 i++;
170 } while ((n /= 10) > 0);
171 return i;
174 #define STATS_ALLOC1(member) \
175 do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
177 static void stats_alloc(struct ifstat *stats, unsigned int cpus)
179 STATS_ALLOC1(irqs);
180 STATS_ALLOC1(irqs_srx);
181 STATS_ALLOC1(irqs_stx);
183 STATS_ALLOC1(cpu_user);
184 STATS_ALLOC1(cpu_sys);
185 STATS_ALLOC1(cpu_nice);
186 STATS_ALLOC1(cpu_idle);
187 STATS_ALLOC1(cpu_iow);
190 #define STATS_ZERO1(member) \
191 do { memset(stats->member, 0, cpus * sizeof(*(stats->member))); } while (0)
193 static void stats_zero(struct ifstat *stats, unsigned int cpus)
195 /* Only clear the non-pointer members */
196 memset(stats, 0, offsetof(struct ifstat, irqs));
198 STATS_ZERO1(irqs);
199 STATS_ZERO1(irqs_srx);
200 STATS_ZERO1(irqs_stx);
202 STATS_ZERO1(cpu_user);
203 STATS_ZERO1(cpu_sys);
204 STATS_ZERO1(cpu_nice);
205 STATS_ZERO1(cpu_idle);
206 STATS_ZERO1(cpu_iow);
209 #define STATS_RELEASE(member) \
210 do { xfree(stats->member); } while (0)
212 static void stats_release(struct ifstat *stats)
214 STATS_RELEASE(irqs);
215 STATS_RELEASE(irqs_srx);
216 STATS_RELEASE(irqs_stx);
218 STATS_RELEASE(cpu_user);
219 STATS_RELEASE(cpu_sys);
220 STATS_RELEASE(cpu_nice);
221 STATS_RELEASE(cpu_idle);
222 STATS_RELEASE(cpu_iow);
225 static int stats_proc_net_dev(const char *ifname, struct ifstat *stats)
227 int ret = -EINVAL;
228 char buff[256];
229 FILE *fp;
231 fp = fopen("/proc/net/dev", "r");
232 if (!fp)
233 panic("Cannot open /proc/net/dev!\n");
235 if (fgets(buff, sizeof(buff), fp)) { ; }
236 if (fgets(buff, sizeof(buff), fp)) { ; }
238 memset(buff, 0, sizeof(buff));
240 while (fgets(buff, sizeof(buff), fp) != NULL) {
241 buff[sizeof(buff) -1] = 0;
243 if (strstr(buff, ifname) == NULL)
244 continue;
246 if (sscanf(buff, "%*[a-z0-9 .-]:%llu%llu%llu%llu%llu%llu"
247 "%llu%*u%llu%llu%llu%llu%llu%llu%llu",
248 &stats->rx_bytes, &stats->rx_packets,
249 &stats->rx_errors, &stats->rx_drops,
250 &stats->rx_fifo, &stats->rx_frame,
251 &stats->rx_multi, &stats->tx_bytes,
252 &stats->tx_packets, &stats->tx_errors,
253 &stats->tx_drops, &stats->tx_fifo,
254 &stats->tx_colls, &stats->tx_carrier) == 14) {
255 ret = 0;
256 break;
259 memset(buff, 0, sizeof(buff));
262 fclose(fp);
263 return ret;
266 static int stats_proc_interrupts(char *ifname, struct ifstat *stats)
268 int ret = -EINVAL, try = 0;
269 unsigned int i, cpus;
270 char *ptr, *buff;
271 bool seen = false;
272 size_t buff_len;
273 struct ethtool_drvinfo drvinf;
274 FILE *fp;
276 cpus = get_number_cpus();
277 buff_len = cpus * 128;
278 buff = xmalloc(buff_len);
280 fp = fopen("/proc/interrupts", "r");
281 if (!fp)
282 panic("Cannot open /proc/interrupts!\n");
283 retry:
284 fseek(fp, 0, SEEK_SET);
285 memset(buff, 0, buff_len);
287 while (fgets(buff, buff_len, fp) != NULL) {
288 buff[buff_len - 1] = 0;
290 if (strstr(buff, ifname) == NULL)
291 continue;
293 ptr = strchr(buff, ':');
294 if (!ptr)
295 continue;
296 ptr++;
298 for (i = 0; i < cpus && ptr; ++i) {
299 if (seen)
300 stats->irqs[i] += strtol(ptr, &ptr, 10);
301 else
302 stats->irqs[i] = strtol(ptr, &ptr, 10);
303 if (i == cpus - 1) {
304 ret = 0;
305 seen = true;
309 memset(buff, 0, buff_len);
312 if (ret == -EINVAL && try == 0) {
313 memset(&drvinf, 0, sizeof(drvinf));
314 if (ethtool_drvinf(ifname, &drvinf) < 0)
315 goto done;
317 ifname = drvinf.driver;
318 try++;
320 goto retry;
322 done:
323 xfree(buff);
324 fclose(fp);
325 return ret;
328 static int stats_proc_softirqs(struct ifstat *stats)
330 unsigned int i, cpus;
331 char *ptr, *buff;
332 size_t buff_len;
333 FILE *fp;
334 enum {
335 softirqs_net_rx,
336 softirqs_net_tx,
337 } net_type;
339 fp = fopen("/proc/softirqs", "r");
340 if (!fp)
341 panic("Cannot open /proc/softirqs!\n");
343 cpus = get_number_cpus();
344 buff_len = cpus * 128;
345 buff = xmalloc(buff_len);
347 memset(buff, 0, buff_len);
349 while (fgets(buff, buff_len, fp) != NULL) {
350 buff[buff_len - 1] = 0;
352 if ((ptr = strstr(buff, "NET_TX:")))
353 net_type = softirqs_net_tx;
354 else if ((ptr = strstr(buff, "NET_RX:")))
355 net_type = softirqs_net_rx;
356 else
357 continue;
359 for (ptr += strlen("NET_xX:"), i = 0; i < cpus; ++i) {
360 switch (net_type) {
361 case softirqs_net_tx:
362 stats->irqs_stx[i] = strtol(ptr, &ptr, 10);
363 break;
364 case softirqs_net_rx:
365 stats->irqs_srx[i] = strtol(ptr, &ptr, 10);
366 break;
370 memset(buff, 0, buff_len);
373 xfree(buff);
374 fclose(fp);
375 return 0;
378 static int stats_proc_memory(struct ifstat *stats)
380 char *ptr, buff[256];
381 FILE *fp;
383 fp = fopen("/proc/meminfo", "r");
384 if (!fp)
385 panic("Cannot open /proc/meminfo!\n");
387 memset(buff, 0, sizeof(buff));
389 while (fgets(buff, sizeof(buff), fp) != NULL) {
390 buff[sizeof(buff) - 1] = 0;
392 if ((ptr = strstr(buff, "MemTotal:"))) {
393 ptr += strlen("MemTotal:");
394 stats->mem_total = strtoul(ptr, &ptr, 10);
395 } else if ((ptr = strstr(buff, "MemFree:"))) {
396 ptr += strlen("MemFree:");
397 stats->mem_free = strtoul(ptr, &ptr, 10);
398 } else if ((ptr = strstr(buff, "Active:"))) {
399 ptr += strlen("Active:");
400 stats->mem_active = strtoul(ptr, &ptr, 10);
401 } else if ((ptr = strstr(buff, "Inactive:"))) {
402 ptr += strlen("Inactive:");
403 stats->mem_inactive = strtoul(ptr, &ptr, 10);
404 } else if ((ptr = strstr(buff, "SwapTotal:"))) {
405 ptr += strlen("SwapTotal:");
406 stats->swap_total = strtoul(ptr, &ptr, 10);
407 } else if ((ptr = strstr(buff, "SwapFree:"))) {
408 ptr += strlen("SwapFree:");
409 stats->swap_free = strtoul(ptr, &ptr, 10);
410 } else if ((ptr = strstr(buff, "SwapCached:"))) {
411 ptr += strlen("SwapCached:");
412 stats->swap_cached = strtoul(ptr, &ptr, 10);
415 memset(buff, 0, sizeof(buff));
418 fclose(fp);
419 return 0;
422 static int stats_proc_system(struct ifstat *stats)
424 unsigned int cpu, cpus;
425 char *ptr, buff[256];
426 FILE *fp;
428 fp = fopen("/proc/stat", "r");
429 if (!fp)
430 panic("Cannot open /proc/stat!\n");
432 cpus = get_number_cpus();
434 memset(buff, 0, sizeof(buff));
436 while (fgets(buff, sizeof(buff), fp) != NULL) {
437 buff[sizeof(buff) - 1] = 0;
439 if ((ptr = strstr(buff, "cpu"))) {
440 ptr += strlen("cpu");
441 if (isblank(*ptr))
442 goto next;
444 cpu = strtol(ptr, &ptr, 10);
445 bug_on(cpu > cpus);
447 if (sscanf(ptr, "%lu%lu%lu%lu%lu",
448 &stats->cpu_user[cpu],
449 &stats->cpu_nice[cpu],
450 &stats->cpu_sys[cpu],
451 &stats->cpu_idle[cpu],
452 &stats->cpu_iow[cpu]) != 5)
453 goto next;
454 } else if ((ptr = strstr(buff, "ctxt"))) {
455 ptr += strlen("ctxt");
456 stats->cswitch = strtoul(ptr, &ptr, 10);
457 } else if ((ptr = strstr(buff, "procs_running"))) {
458 ptr += strlen("procs_running");
459 stats->procs_run = strtoul(ptr, &ptr, 10);
460 } else if ((ptr = strstr(buff, "procs_blocked"))) {
461 ptr += strlen("procs_blocked");
462 stats->procs_iow = strtoul(ptr, &ptr, 10);
464 next:
465 memset(buff, 0, sizeof(buff));
468 fclose(fp);
469 return 0;
472 static int stats_proc_procs(struct ifstat *stats)
474 DIR *dir;
475 struct dirent *e;
477 dir = opendir("/proc");
478 if (!dir)
479 panic("Cannot open /proc\n");
481 stats->procs_total = 0;
483 while ((e = readdir(dir)) != NULL) {
484 const char *name = e->d_name;
485 char *end;
486 unsigned int pid = strtoul(name, &end, 10);
488 /* not a number */
489 if (pid == 0 && end == name)
490 continue;
492 stats->procs_total++;
495 closedir(dir);
497 return 0;
500 static int adjust_dbm_level(int in_dbm, int dbm_val)
502 if (!in_dbm)
503 return dbm_val;
505 return dbm_val - 0x100;
508 static int stats_wireless(const char *ifname, struct ifstat *stats)
510 int ret;
511 struct iw_statistics ws;
513 ret = wireless_sigqual(ifname, &ws);
514 if (ret != 0) {
515 stats->wifi.bitrate = 0;
516 return -EINVAL;
519 stats->wifi.bitrate = wireless_bitrate(ifname);
521 stats->wifi.signal_level =
522 adjust_dbm_level(ws.qual.updated & IW_QUAL_DBM, ws.qual.level);
524 stats->wifi.link_qual = ws.qual.qual;
525 stats->wifi.link_qual_max = wireless_rangemax_sigqual(ifname);
527 return ret;
530 #define DIFF1(member) do { diff->member = new->member - old->member; } while (0)
531 #define DIFF(member) do { \
532 if (sizeof(diff->member) != sizeof(new->member) || \
533 sizeof(diff->member) != sizeof(old->member)) \
534 bug(); \
535 if ((new->member - old->member) > (new->member)) { \
536 diff->member = 0; \
537 } else { \
538 DIFF1(member); \
540 } while (0)
542 static void stats_diff(struct ifstat *old, struct ifstat *new,
543 struct ifstat *diff)
545 unsigned int cpus, i;
547 DIFF(rx_bytes);
548 DIFF(rx_packets);
549 DIFF(rx_drops);
550 DIFF(rx_errors);
551 DIFF(rx_fifo);
552 DIFF(rx_frame);
553 DIFF(rx_multi);
555 DIFF(tx_bytes);
556 DIFF(tx_packets);
557 DIFF(tx_drops);
558 DIFF(tx_errors);
559 DIFF(tx_fifo);
560 DIFF(tx_colls);
561 DIFF(tx_carrier);
563 DIFF1(wifi.signal_level);
564 DIFF1(wifi.link_qual);
566 DIFF1(cswitch);
568 cpus = get_number_cpus();
570 for (i = 0; i < cpus; ++i) {
571 DIFF(irqs[i]);
572 DIFF(irqs_srx[i]);
573 DIFF(irqs_stx[i]);
575 DIFF1(cpu_user[i]);
576 DIFF1(cpu_nice[i]);
577 DIFF1(cpu_sys[i]);
578 DIFF1(cpu_idle[i]);
579 DIFF1(cpu_iow[i]);
583 static void stats_fetch(const char *ifname, struct ifstat *stats)
585 if (stats_proc_net_dev(ifname, stats) < 0)
586 panic("Cannot fetch device stats!\n");
587 if (stats_proc_softirqs(stats) < 0)
588 panic("Cannot fetch software interrupts!\n");
589 if (stats_proc_memory(stats) < 0)
590 panic("Cannot fetch memory stats!\n");
591 if (stats_proc_system(stats) < 0)
592 panic("Cannot fetch system stats!\n");
593 if (stats_proc_procs(stats) < 0)
594 panic("Cannot fetch process stats!\n");
596 stats_proc_interrupts((char *) ifname, stats);
598 stats_wireless(ifname, stats);
601 static void stats_sample_generic(const char *ifname, uint64_t ms_interval)
603 unsigned int cpus = get_number_cpus();
605 stats_zero(&stats_old, cpus);
606 stats_zero(&stats_new, cpus);
607 stats_zero(&stats_delta, cpus);
609 stats_fetch(ifname, &stats_old);
610 usleep(ms_interval * 1000);
611 stats_fetch(ifname, &stats_new);
613 stats_diff(&stats_old, &stats_new, &stats_delta);
616 static int cmp_hits(const void *p1, const void *p2)
618 const struct cpu_hit *h1 = p1, *h2 = p2;
621 * We want the hits sorted in descending order, thus reverse the return
622 * values.
624 if (h1->hit == h2->hit)
625 return 0;
626 else if (h1->hit < h2->hit)
627 return 1;
628 else
629 return -1;
632 static int cmp_irqs_rel(const void *p1, const void *p2)
634 const struct cpu_hit *h1 = p1, *h2 = p2;
637 * We want the hits sorted in descending order, thus reverse the return
638 * values.
640 if (h1->irqs_rel == h2->irqs_rel)
641 return 0;
642 else if (h1->irqs_rel < h2->irqs_rel)
643 return 1;
644 else
645 return -1;
648 static int cmp_irqs_abs(const void *p1, const void *p2)
650 const struct cpu_hit *h1 = p1, *h2 = p2;
653 * We want the hits sorted in descending order, thus reverse the return
654 * values.
656 if (h1->irqs_abs == h2->irqs_abs)
657 return 0;
658 else if (h1->irqs_abs < h2->irqs_abs)
659 return 1;
660 else
661 return -1;
664 static void stats_top(const struct ifstat *rel, const struct ifstat *abs,
665 unsigned int cpus)
667 unsigned int i;
669 memset(&stats_avg, 0, sizeof(stats_avg));
671 for (i = 0; i < cpus; ++i) {
672 cpu_hits[i].idx = i;
673 cpu_hits[i].hit = rel->cpu_user[i] + rel->cpu_nice[i] + rel->cpu_sys[i];
674 cpu_hits[i].irqs_rel = rel->irqs[i];
675 cpu_hits[i].irqs_abs = abs->irqs[i];
677 stats_avg.cpu_user += rel->cpu_user[i];
678 stats_avg.cpu_sys += rel->cpu_sys[i];
679 stats_avg.cpu_nice += rel->cpu_nice[i];
680 stats_avg.cpu_idle += rel->cpu_idle[i];
681 stats_avg.cpu_iow += rel->cpu_iow[i];
683 stats_avg.irqs_abs += abs->irqs[i];
684 stats_avg.irqs_rel += rel->irqs[i];
685 stats_avg.irqs_srx_rel += rel->irqs_srx[i];
686 stats_avg.irqs_stx_rel += rel->irqs_stx[i];
689 stats_avg.cpu_user /= cpus;
690 stats_avg.cpu_sys /= cpus;
691 stats_avg.cpu_nice /= cpus;
692 stats_avg.cpu_idle /= cpus;
693 stats_avg.cpu_iow /= cpus;
694 stats_avg.irqs_abs /= cpus;
695 stats_avg.irqs_rel /= cpus;
696 stats_avg.irqs_srx_rel /= cpus;
697 stats_avg.irqs_stx_rel /= cpus;
700 static void screen_header(WINDOW *screen, const char *ifname, int *voff,
701 u32 rate, uint64_t ms_interval, unsigned int top_cpus)
703 size_t len = 0;
704 char buff[64], machine[64];
705 struct ethtool_drvinfo drvinf;
706 int link = ethtool_link(ifname);
707 unsigned int cpus = get_number_cpus();
709 memset(&drvinf, 0, sizeof(drvinf));
710 ethtool_drvinf(ifname, &drvinf);
712 memset(buff, 0, sizeof(buff));
713 memset(machine, 0, sizeof(machine));
715 if (rate)
716 len += snprintf(buff + len, sizeof(buff) - len, " %uMbit/s", rate);
717 if (link >= 0)
718 len += snprintf(buff + len, sizeof(buff) - len, " link:%s",
719 link == 0 ? "no" : "yes");
721 if (!strstr(uts.release, uts.machine))
722 slprintf(machine, sizeof(machine), " %s,", uts.machine);
724 mvwprintw(screen, (*voff)++, 2,
725 "%s,%s %s (%s%s), t=%lums, cpus=%u%s/%u"
726 " ", uts.release, machine,
727 ifname, drvinf.driver, buff, ms_interval, top_cpus,
728 top_cpus > 0 && top_cpus < cpus ? "+1" : "", cpus);
731 static void screen_net_dev_rel(WINDOW *screen, const struct ifstat *rel,
732 int *voff)
734 attron(A_REVERSE);
736 mvwprintw(screen, (*voff)++, 0,
737 " rx: %16.3llf MiB/t "
738 "%10llu pkts/t "
739 "%10llu drops/t "
740 "%10llu errors/t ",
741 ((long double) rel->rx_bytes) / (1LLU << 20),
742 rel->rx_packets, rel->rx_drops, rel->rx_errors);
744 mvwprintw(screen, (*voff)++, 0,
745 " tx: %16.3llf MiB/t "
746 "%10llu pkts/t "
747 "%10llu drops/t "
748 "%10llu errors/t ",
749 ((long double) rel->tx_bytes) / (1LLU << 20),
750 rel->tx_packets, rel->tx_drops, rel->tx_errors);
752 attroff(A_REVERSE);
755 static void screen_net_dev_percentage(WINDOW *screen, const struct ifstat *rel,
756 int *voff, u32 rate)
758 mvwprintw(screen, (*voff)++, 0,
759 " rx: %15.2llf%% of line rate "
760 " ",
761 rate ? ((((long double) rel->rx_bytes) / 125000) / rate) * 100.0 : 0.0);
763 mvwprintw(screen, (*voff)++, 0,
764 " tx: %15.2llf%% of line rate "
765 " ",
766 rate ? ((((long double) rel->tx_bytes) / 125000) / rate) * 100.0 : 0.0);
769 static void screen_net_dev_abs(WINDOW *screen, const struct ifstat *abs,
770 int *voff)
772 mvwprintw(screen, (*voff)++, 2,
773 "rx: %16.3llf MiB "
774 "%10llu pkts "
775 "%10llu drops "
776 "%10llu errors",
777 ((long double) abs->rx_bytes) / (1LLU << 20),
778 abs->rx_packets, abs->rx_drops, abs->rx_errors);
780 mvwprintw(screen, (*voff)++, 2,
781 "tx: %16.3llf MiB "
782 "%10llu pkts "
783 "%10llu drops "
784 "%10llu errors",
785 ((long double) abs->tx_bytes) / (1LLU << 20),
786 abs->tx_packets, abs->tx_drops, abs->tx_errors);
789 static void screen_sys(WINDOW *screen, const struct ifstat *rel,
790 const struct ifstat *abs, int *voff)
792 mvwprintw(screen, (*voff)++, 2,
793 "sys: %14u cs/t "
794 "%11u procs "
795 "%11u running "
796 "%10u iowait",
797 rel->cswitch, abs->procs_total, abs->procs_run, abs->procs_iow);
800 static void screen_mem_swap(WINDOW *screen, const struct ifstat *abs, int *voff)
802 mvwprintw(screen, (*voff)++, 2,
803 "mem: %13uM total "
804 "%9uM used "
805 "%11uM active "
806 "%10uM inactive",
807 abs->mem_total / 1024,
808 (abs->mem_total - abs->mem_free) / 1024,
809 abs->mem_active / 1024,
810 abs->mem_inactive / 1024);
812 mvwprintw(screen, (*voff)++, 2,
813 "swap: %12uM total "
814 "%9uM used "
815 "%11uM cached",
816 abs->swap_total / 1024,
817 (abs->swap_total - abs->swap_free) / 1024,
818 abs->swap_cached / 1024);
821 static void screen_percpu_states_one(WINDOW *screen, const struct ifstat *rel,
822 int *voff, unsigned int idx, char *tag)
824 int max_padd = padding_from_num(get_number_cpus());
825 uint64_t all = rel->cpu_user[idx] + rel->cpu_nice[idx] + rel->cpu_sys[idx] +
826 rel->cpu_idle[idx] + rel->cpu_iow[idx];
828 mvwprintw(screen, (*voff)++, 2,
829 "cpu%*d %s: %11.1lf%% usr/t "
830 "%9.1lf%% sys/t "
831 "%10.1lf%% idl/t "
832 "%11.1lf%% iow/t",
833 max_padd, idx, tag,
834 100.0 * (rel->cpu_user[idx] + rel->cpu_nice[idx]) / all,
835 100.0 * rel->cpu_sys[idx] / all,
836 100.0 * rel->cpu_idle[idx] / all,
837 100.0 * rel->cpu_iow[idx] / all);
840 #define MEDIAN_EVEN(member) do { \
841 m_##member = (rel->member[i] + rel->member[j]) / 2.0; \
842 } while (0)
844 #define MEDIAN_ODD(member) do { \
845 m_##member = rel->member[i]; \
846 } while (0)
848 static void screen_percpu_states(WINDOW *screen, const struct ifstat *rel,
849 const struct avg_stat *avg,
850 unsigned int top_cpus, int *voff)
852 unsigned int i;
853 unsigned int cpus = get_number_cpus();
854 int max_padd = padding_from_num(cpus);
855 uint64_t all;
857 if (top_cpus == 0)
858 return;
860 /* Display top hitter */
861 screen_percpu_states_one(screen, rel, voff, cpu_hits[0].idx, "+");
863 /* Make sure we don't display the min. hitter twice */
864 if (top_cpus == cpus)
865 top_cpus--;
867 for (i = 1; i < top_cpus; ++i)
868 screen_percpu_states_one(screen, rel, voff, cpu_hits[i].idx, "|");
870 /* Display minimum hitter */
871 if (cpus != 1)
872 screen_percpu_states_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
874 all = avg->cpu_user + avg->cpu_sys + avg->cpu_nice + avg->cpu_idle + avg->cpu_iow;
875 mvwprintw(screen, (*voff)++, 2,
876 "avg:%*s%14.1lf%% "
877 "%9.1lf%% "
878 "%10.1lf%% "
879 "%11.1lf%%", max_padd, "",
880 100.0 * (avg->cpu_user + avg->cpu_nice) / all,
881 100.0 * avg->cpu_sys / all,
882 100.0 * avg->cpu_idle /all,
883 100.0 * avg->cpu_iow / all);
885 if (show_median) {
886 long double m_cpu_user, m_cpu_nice, m_cpu_sys, m_cpu_idle, m_cpu_iow;
887 long double m_all;
889 i = cpu_hits[cpus / 2].idx;
890 if (cpus % 2 == 0) {
891 /* take the mean of the 2 middle entries */
892 int j = cpu_hits[(cpus / 2) - 1].idx;
894 MEDIAN_EVEN(cpu_user);
895 MEDIAN_EVEN(cpu_nice);
896 MEDIAN_EVEN(cpu_sys);
897 MEDIAN_EVEN(cpu_idle);
898 MEDIAN_EVEN(cpu_iow);
899 } else {
900 /* take the middle entry as is */
901 MEDIAN_ODD(cpu_user);
902 MEDIAN_ODD(cpu_nice);
903 MEDIAN_ODD(cpu_sys);
904 MEDIAN_ODD(cpu_idle);
905 MEDIAN_ODD(cpu_iow);
908 m_all = m_cpu_user + m_cpu_sys + m_cpu_nice + m_cpu_idle + m_cpu_iow;
909 mvwprintw(screen, (*voff)++, 2,
910 "med:%*s%14.1Lf%% "
911 "%9.1Lf%% "
912 "%10.1Lf%% "
913 "%11.1Lf%%", max_padd, "",
914 100.0 * (m_cpu_user + m_cpu_nice) / m_all,
915 100.0 * m_cpu_sys / m_all,
916 100.0 * m_cpu_idle /m_all,
917 100.0 * m_cpu_iow / m_all);
921 static void screen_percpu_irqs_rel_one(WINDOW *screen, const struct ifstat *rel,
922 int *voff, unsigned int idx, char *tag)
924 int max_padd = padding_from_num(get_number_cpus());
926 mvwprintw(screen, (*voff)++, 2,
927 "cpu%*d %s: %12llu irqs/t "
928 "%17llu sirq rx/t "
929 "%17llu sirq tx/t",
930 max_padd, idx, tag,
931 rel->irqs[idx],
932 rel->irqs_srx[idx],
933 rel->irqs_stx[idx]);
936 static void screen_percpu_irqs_rel(WINDOW *screen, const struct ifstat *rel,
937 const struct avg_stat *avg,
938 unsigned int top_cpus, int *voff)
940 unsigned int i;
941 unsigned int cpus = get_number_cpus();
942 int max_padd = padding_from_num(cpus);
944 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[0].idx, "+");
946 if (top_cpus == cpus)
947 top_cpus--;
949 for (i = 1; i < top_cpus; ++i)
950 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[i].idx, "|");
952 if (cpus != 1)
953 screen_percpu_irqs_rel_one(screen, rel, voff, cpu_hits[cpus - 1].idx, "-");
955 mvwprintw(screen, (*voff)++, 2,
956 "avg:%*s%17.1Lf "
957 "%17.1Lf "
958 "%17.1Lf", max_padd, "",
959 avg->irqs_rel, avg->irqs_srx_rel, avg->irqs_stx_rel);
961 if (show_median) {
962 long double m_irqs, m_irqs_srx, m_irqs_stx;
964 i = cpu_hits[cpus / 2].idx;
965 if (cpus % 2 == 0) {
966 /* take the mean of the 2 middle entries */
967 int j = cpu_hits[(cpus / 2) - 1].idx;
969 MEDIAN_EVEN(irqs);
970 MEDIAN_EVEN(irqs_srx);
971 MEDIAN_EVEN(irqs_stx);
972 } else {
973 /* take the middle entry as is */
974 MEDIAN_ODD(irqs);
975 MEDIAN_ODD(irqs_srx);
976 MEDIAN_ODD(irqs_stx);
979 mvwprintw(screen, (*voff)++, 2,
980 "med:%*s%17.1Lf "
981 "%17.1Lf "
982 "%17.1Lf", max_padd, "",
983 m_irqs, m_irqs_srx, m_irqs_stx);
987 static void screen_percpu_irqs_abs_one(WINDOW *screen, const struct ifstat *abs,
988 int *voff, unsigned int idx, char *tag)
990 int max_padd = padding_from_num(get_number_cpus());
992 mvwprintw(screen, (*voff)++, 2,
993 "cpu%*d %s: %12llu irqs",
994 max_padd, idx, tag, abs->irqs[idx]);
997 static void screen_percpu_irqs_abs(WINDOW *screen, const struct ifstat *abs,
998 const struct avg_stat *avg,
999 unsigned int top_cpus, int *voff)
1001 unsigned int i;
1002 unsigned int cpus = get_number_cpus();
1003 int max_padd = padding_from_num(cpus);
1005 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[0].idx, "+");
1007 if (top_cpus == cpus)
1008 top_cpus--;
1010 for (i = 1; i < top_cpus; ++i)
1011 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[i].idx, "|");
1013 if (cpus != 1)
1014 screen_percpu_irqs_abs_one(screen, abs, voff, cpu_hits[cpus - 1].idx, "-");
1016 mvwprintw(screen, (*voff)++, 2,
1017 "avg:%*s%17.1Lf", max_padd, "", avg->irqs_abs);
1019 if (show_median) {
1020 long double m_irqs;
1022 i = cpu_hits[cpus / 2].idx;
1023 if (cpus % 2 == 0) {
1024 /* take the mean of the 2 middle entries */
1025 int j = cpu_hits[(cpus / 2) - 1].idx;
1027 m_irqs = (abs->irqs[i] + abs->irqs[j]) / 2;
1028 } else {
1029 /* take the middle entry as is */
1030 m_irqs = abs->irqs[i];
1033 mvwprintw(screen, (*voff)++, 2,
1034 "med:%*s%17.1Lf", max_padd, "", m_irqs);
1038 static void screen_wireless(WINDOW *screen, const struct ifstat *rel,
1039 const struct ifstat *abs, int *voff)
1041 if (iswireless(abs)) {
1042 mvwprintw(screen, (*voff)++, 2,
1043 "linkqual: %7d/%d (%d/t) ",
1044 abs->wifi.link_qual,
1045 abs->wifi.link_qual_max,
1046 rel->wifi.link_qual);
1048 mvwprintw(screen, (*voff)++, 2,
1049 "signal: %8d dBm (%d dBm/t) ",
1050 abs->wifi.signal_level,
1051 rel->wifi.signal_level);
1055 static void screen_update(WINDOW *screen, const char *ifname, const struct ifstat *rel,
1056 const struct ifstat *abs, const struct avg_stat *avg,
1057 int *first, uint64_t ms_interval, unsigned int top_cpus,
1058 bool need_info)
1060 unsigned int cpus, top;
1061 int voff = 1, cvoff = 2;
1062 u32 rate = device_bitrate(ifname);
1064 curs_set(0);
1066 cpus = get_number_cpus();
1067 top = min(cpus, top_cpus);
1069 stats_top(rel, abs, cpus);
1071 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_hits);
1073 screen_header(screen, ifname, &voff, rate, ms_interval, top_cpus);
1075 voff++;
1076 screen_net_dev_rel(screen, rel, &voff);
1078 if (show_percentage) {
1079 voff++;
1080 screen_net_dev_percentage(screen, rel, &voff, rate);
1083 voff++;
1084 screen_net_dev_abs(screen, abs, &voff);
1086 voff++;
1087 screen_sys(screen, rel, abs, &voff);
1089 voff++;
1090 screen_mem_swap(screen, abs, &voff);
1092 voff++;
1093 screen_percpu_states(screen, rel, avg, top, &voff);
1095 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_rel);
1097 voff++;
1098 screen_percpu_irqs_rel(screen, rel, avg, top, &voff);
1100 qsort(cpu_hits, cpus, sizeof(*cpu_hits), cmp_irqs_abs);
1102 voff++;
1103 screen_percpu_irqs_abs(screen, abs, avg, top, &voff);
1105 voff++;
1106 screen_wireless(screen, rel, abs, &voff);
1108 if (*first) {
1109 mvwprintw(screen, cvoff, 2, "Collecting data ...");
1110 *first = 0;
1111 } else {
1112 if (need_info)
1113 mvwprintw(screen, cvoff, 2, "(consider to increase "
1114 "your sampling interval, e.g. -t %d)",
1115 rate > SPEED_1000 ? 10000 : 1000);
1116 else
1117 mvwprintw(screen, cvoff, 2, " "
1118 " ");
1121 wrefresh(screen);
1122 refresh();
1125 static int screen_main(const char *ifname, uint64_t ms_interval,
1126 unsigned int top_cpus, bool suppress_warnings,
1127 bool omit_header __maybe_unused)
1129 int first = 1, key;
1130 u32 rate = device_bitrate(ifname);
1131 bool need_info = false;
1133 stats_screen = screen_init(true);
1135 if (((rate > SPEED_1000 && ms_interval <= 1000) ||
1136 (rate = SPEED_1000 && ms_interval < 1000)) &&
1137 !suppress_warnings)
1138 need_info = true;
1140 while (!sigint) {
1141 key = getch();
1142 if (key == 'q' || key == 0x1b || key == KEY_F(10))
1143 break;
1145 screen_update(stats_screen, ifname, &stats_delta, &stats_new, &stats_avg,
1146 &first, ms_interval, top_cpus, need_info);
1148 stats_sample_generic(ifname, ms_interval);
1151 screen_end();
1153 return 0;
1156 static void term_csv(const struct ifstat *rel, const struct ifstat *abs)
1158 unsigned int cpus, i;
1160 printf("%ld ", time(NULL));
1162 printf("%llu ", rel->rx_bytes);
1163 printf("%llu ", rel->rx_packets);
1164 printf("%llu ", rel->rx_drops);
1165 printf("%llu ", rel->rx_errors);
1167 printf("%llu ", abs->rx_bytes);
1168 printf("%llu ", abs->rx_packets);
1169 printf("%llu ", abs->rx_drops);
1170 printf("%llu ", abs->rx_errors);
1172 printf("%llu ", rel->tx_bytes);
1173 printf("%llu ", rel->tx_packets);
1174 printf("%llu ", rel->tx_drops);
1175 printf("%llu ", rel->tx_errors);
1177 printf("%llu ", abs->tx_bytes);
1178 printf("%llu ", abs->tx_packets);
1179 printf("%llu ", abs->tx_drops);
1180 printf("%llu ", abs->tx_errors);
1182 printf("%u ", rel->cswitch);
1183 printf("%lu ", abs->mem_free);
1184 printf("%lu ", abs->mem_total - abs->mem_free);
1185 printf("%lu ", abs->mem_total);
1186 printf("%lu ", abs->swap_free);
1187 printf("%lu ", abs->swap_total - abs->swap_free);
1188 printf("%lu ", abs->swap_total);
1189 printf("%u ", abs->procs_total);
1190 printf("%u ", abs->procs_run);
1191 printf("%u ", abs->procs_iow);
1193 cpus = get_number_cpus();
1195 for (i = 0; i < cpus; ++i) {
1196 printf("%lu ", rel->cpu_user[i]);
1197 printf("%lu ", rel->cpu_nice[i]);
1198 printf("%lu ", rel->cpu_sys[i]);
1199 printf("%lu ", rel->cpu_idle[i]);
1200 printf("%lu ", rel->cpu_iow[i]);
1202 printf("%llu ", rel->irqs[i]);
1203 printf("%llu ", abs->irqs[i]);
1205 printf("%llu ", rel->irqs_srx[i]);
1206 printf("%llu ", abs->irqs_srx[i]);
1208 printf("%llu ", rel->irqs_stx[i]);
1209 printf("%llu ", abs->irqs_stx[i]);
1212 if (iswireless(abs)) {
1213 printf("%u ", rel->wifi.link_qual);
1214 printf("%u ", abs->wifi.link_qual);
1215 printf("%u ", abs->wifi.link_qual_max);
1217 printf("%d ", rel->wifi.signal_level);
1218 printf("%d ", abs->wifi.signal_level);
1221 puts("");
1222 fflush(stdout);
1225 static void term_csv_header(const char *ifname, const struct ifstat *abs,
1226 uint64_t ms_interval)
1228 unsigned int cpus, i, j = 1;
1230 printf("# gnuplot dump (#col:description)\n");
1231 printf("# networking interface: %s\n", ifname);
1232 printf("# sampling interval (t): %lu ms\n", ms_interval);
1233 printf("# %d:unixtime ", j++);
1235 printf("%d:rx-bytes-per-t ", j++);
1236 printf("%d:rx-pkts-per-t ", j++);
1237 printf("%d:rx-drops-per-t ", j++);
1238 printf("%d:rx-errors-per-t ", j++);
1240 printf("%d:rx-bytes ", j++);
1241 printf("%d:rx-pkts ", j++);
1242 printf("%d:rx-drops ", j++);
1243 printf("%d:rx-errors ", j++);
1245 printf("%d:tx-bytes-per-t ", j++);
1246 printf("%d:tx-pkts-per-t ", j++);
1247 printf("%d:tx-drops-per-t ", j++);
1248 printf("%d:tx-errors-per-t ", j++);
1250 printf("%d:tx-bytes ", j++);
1251 printf("%d:tx-pkts ", j++);
1252 printf("%d:tx-drops ", j++);
1253 printf("%d:tx-errors ", j++);
1255 printf("%d:context-switches-per-t ", j++);
1256 printf("%d:mem-free ", j++);
1257 printf("%d:mem-used ", j++);
1258 printf("%d:mem-total ", j++);
1259 printf("%d:swap-free ", j++);
1260 printf("%d:swap-used ", j++);
1261 printf("%d:swap-total ", j++);
1262 printf("%d:procs-total ", j++);
1263 printf("%d:procs-in-run ", j++);
1264 printf("%d:procs-in-iow ", j++);
1266 cpus = get_number_cpus();
1268 for (i = 0; i < cpus; ++i) {
1269 printf("%d:cpu%i-usr-per-t ", j++, i);
1270 printf("%d:cpu%i-nice-per-t ", j++, i);
1271 printf("%d:cpu%i-sys-per-t ", j++, i);
1272 printf("%d:cpu%i-idle-per-t ", j++, i);
1273 printf("%d:cpu%i-iow-per-t ", j++, i);
1275 printf("%d:cpu%i-net-irqs-per-t ", j++, i);
1276 printf("%d:cpu%i-net-irqs ", j++, i);
1278 printf("%d:cpu%i-net-rx-soft-irqs-per-t ", j++, i);
1279 printf("%d:cpu%i-net-rx-soft-irqs ", j++, i);
1280 printf("%d:cpu%i-net-tx-soft-irqs-per-t ", j++, i);
1281 printf("%d:cpu%i-net-tx-soft-irqs ", j++, i);
1284 if (iswireless(abs)) {
1285 printf("%d:wifi-link-qual-per-t ", j++);
1286 printf("%d:wifi-link-qual ", j++);
1287 printf("%d:wifi-link-qual-max ", j++);
1289 printf("%d:wifi-signal-dbm-per-t ", j++);
1290 printf("%d:wifi-signal-dbm ", j++);
1293 puts("");
1294 printf("# data:\n");
1295 fflush(stdout);
1298 static int term_main(const char *ifname, uint64_t ms_interval,
1299 unsigned int top_cpus __maybe_unused,
1300 bool suppress_warnings __maybe_unused,
1301 bool omit_header)
1303 do {
1304 stats_sample_generic(ifname, ms_interval);
1306 if (!omit_header) {
1307 omit_header = true;
1308 term_csv_header(ifname, &stats_new, ms_interval);
1311 term_csv(&stats_delta, &stats_new);
1312 } while (stats_loop && !sigint);
1314 return 0;
1317 int main(int argc, char **argv)
1319 short ifflags = 0;
1320 int c, opt_index, ret, promisc = 0;
1321 unsigned int cpus, top_cpus = 5;
1322 uint64_t interval = 1000;
1323 char *ifname = NULL;
1324 bool suppress_warnings = false;
1325 bool omit_header = false;
1326 int (*func_main)(const char *ifname, uint64_t ms_interval,
1327 unsigned int top_cpus, bool suppress_warnings,
1328 bool omit_header);
1330 func_main = screen_main;
1332 setfsuid(getuid());
1333 setfsgid(getgid());
1335 while ((c = getopt_long(argc, argv, short_options, long_options,
1336 &opt_index)) != EOF) {
1337 switch (c) {
1338 case 'h':
1339 help();
1340 break;
1341 case 'v':
1342 version();
1343 break;
1344 case 'W':
1345 suppress_warnings = true;
1346 break;
1347 case 'd':
1348 ifname = xstrndup(optarg, IFNAMSIZ);
1349 break;
1350 case 't':
1351 interval = strtoul(optarg, NULL, 10);
1352 break;
1353 case 'n':
1354 top_cpus = strtoul(optarg, NULL, 10);
1355 if (top_cpus < 1)
1356 panic("Number of top hitter CPUs must be greater than 0");
1357 break;
1358 case 'l':
1359 stats_loop = 1;
1360 break;
1361 case 'p':
1362 promisc = 1;
1363 break;
1364 case 'P':
1365 show_percentage = 1;
1366 break;
1367 case 'm':
1368 show_median = 1;
1369 break;
1370 case 'c':
1371 func_main = term_main;
1372 break;
1373 case 'o':
1374 omit_header = true;
1375 break;
1376 case '?':
1377 switch (optopt) {
1378 case 'd':
1379 case 't':
1380 panic("Option -%c requires an argument!\n",
1381 optopt);
1382 default:
1383 if (isprint(optopt))
1384 printf("Unknown option character `0x%X\'!\n", optopt);
1385 die();
1387 default:
1388 break;
1392 if (argc == 1)
1393 help();
1395 if (argc == 2)
1396 ifname = xstrndup(argv[1], IFNAMSIZ);
1397 if (ifname == NULL)
1398 panic("No networking device given!\n");
1400 if (!strncmp("lo", ifname, IFNAMSIZ))
1401 panic("lo is not supported!\n");
1402 if (device_mtu(ifname) == 0)
1403 panic("This is no networking device!\n");
1405 register_signal(SIGINT, signal_handler);
1406 register_signal(SIGQUIT, signal_handler);
1407 register_signal(SIGSTOP, signal_handler);
1408 register_signal(SIGHUP, signal_handler);
1410 cpus = get_number_cpus();
1411 top_cpus = min(top_cpus, cpus);
1412 if (uname(&uts) < 0)
1413 panic("Cannot execute uname!\n");
1415 stats_alloc(&stats_old, cpus);
1416 stats_alloc(&stats_new, cpus);
1417 stats_alloc(&stats_delta, cpus);
1419 cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
1421 if (promisc)
1422 ifflags = device_enter_promiscuous_mode(ifname);
1423 ret = func_main(ifname, interval, top_cpus, suppress_warnings, omit_header);
1424 if (promisc)
1425 device_leave_promiscuous_mode(ifname, ifflags);
1427 stats_release(&stats_old);
1428 stats_release(&stats_new);
1429 stats_release(&stats_delta);
1431 xfree(ifname);
1432 return ret;