socsvn commit: r286933 - soc2015/btw/head/usr.bin/netstat
btw at FreeBSD.org
btw at FreeBSD.org
Thu Jun 11 01:28:03 UTC 2015
Author: btw
Date: Thu Jun 11 01:28:01 2015
New Revision: 286933
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=286933
Log:
Extend netstat(1) to report the statistics of ifring.
Modified:
soc2015/btw/head/usr.bin/netstat/if.c
soc2015/btw/head/usr.bin/netstat/main.c
soc2015/btw/head/usr.bin/netstat/netstat.h
Modified: soc2015/btw/head/usr.bin/netstat/if.c
==============================================================================
--- soc2015/btw/head/usr.bin/netstat/if.c Thu Jun 11 00:24:33 2015 (r286932)
+++ soc2015/btw/head/usr.bin/netstat/if.c Thu Jun 11 01:28:01 2015 (r286933)
@@ -46,6 +46,7 @@
#include <net/if.h>
#include <net/if_dl.h>
+#include <net/if_mib.h>
#include <net/if_types.h>
#include <net/ethernet.h>
#include <netinet/in.h>
@@ -680,3 +681,87 @@
/* NOTREACHED */
}
+
+void
+ring_stats(char *name)
+{
+ static int mib[] = { CTL_NET,
+ PF_LINK,
+ NETLINK_GENERIC,
+ IFMIB_IFDATA,
+ 0/* index */,
+ IFDATA_RINGINFO };
+ size_t datalen = 0;
+ struct ifmibdata *data;
+ struct if_ring_data *ifrd;
+ struct xifrstat *ifrs;
+ int nrings, ncpus;
+ int ri, cpu, i;
+
+ if ((mib[4] = if_nametoindex(name)) == 0)
+ xo_err(EX_DATAERR, "if_nametoindex(%s)", name);
+
+ if (sysctl(mib, 6, NULL, &datalen, NULL, 0) < 0)
+ xo_err(EX_OSERR, "sysctl: net.link.generic.ifdata");
+
+ if ((data = malloc(datalen)) == NULL)
+ xo_err(EX_OSERR, "malloc %lu bytes", (u_long)datalen);
+
+ if (sysctl(mib, 6, data, &datalen, NULL, 0) < 0)
+ xo_err(EX_OSERR, "sysctl: net.link.generic.ifdata");
+
+ ifrd = &data->ifmd_ring_data;
+ ifrs = ifrd->ifrd_stats;
+
+ nrings = ifrd->ifrd_nrings;
+ ncpus = ifrd->ifrd_ncpus;
+
+ xo_open_instance("interface");
+ for (i = 27; i > 0; i--)
+ xo_emit("-");
+ xo_emit(" {tk:name/%s} ", name);
+ for (i = 28 - (int)strlen(name); i > 0; i--)
+ xo_emit("-");
+ xo_emit("\n");
+
+ xo_open_list("ring");
+ for (ri = 0; ri < nrings; ri++) {
+ xo_open_instance("ring");
+ xo_emit("\nring{tk:ring-id/%d}:\n", ri);
+
+ xo_emit("{T:/%-8s} ", "");
+ for (cpu = 0; cpu < ncpus; cpu++) {
+ char string[32];
+ snprintf(string, sizeof(string), "cpu%d", cpu);
+ xo_emit("{T:/%12s}", string);
+ }
+ xo_emit("\n");
+
+#define DUMP_LAYER(layer) \
+do { \
+ xo_open_instance("layer"); \
+ xo_emit("{tk:name/%-8s}:", #layer); \
+ for (cpu = 0; cpu < ncpus; cpu++) { \
+ char format[32]; \
+ snprintf(format, sizeof(format), "{tk:cpu%d/%%12lu}", cpu);\
+ xo_emit(format, ifrs[ri * ncpus + cpu].ifrs_##layer); \
+ } \
+ xo_emit("\n"); \
+ xo_close_instance("layer"); \
+} while (0)
+ xo_open_list("layer");
+ DUMP_LAYER(ifinput);
+ DUMP_LAYER(netisr);
+ DUMP_LAYER(ether);
+ DUMP_LAYER(ip);
+ DUMP_LAYER(ip6);
+ DUMP_LAYER(tcp);
+ DUMP_LAYER(udp);
+ xo_close_list("layer");
+ xo_close_instance("ring");
+ }
+ xo_close_list("ring");
+ xo_close_instance("interface");
+
+ free(data);
+}
Modified: soc2015/btw/head/usr.bin/netstat/main.c
==============================================================================
--- soc2015/btw/head/usr.bin/netstat/main.c Thu Jun 11 00:24:33 2015 (r286932)
+++ soc2015/btw/head/usr.bin/netstat/main.c Thu Jun 11 01:28:01 2015 (r286933)
@@ -545,7 +545,10 @@
#endif
if (iflag && !sflag) {
xo_open_container("statistics");
- intpr(interval, NULL, af);
+ if (Rflag)
+ intpr(interval, ring_stats, af);
+ else
+ intpr(interval, NULL, af);
xo_close_container("statistics");
xo_finish();
exit(0);
Modified: soc2015/btw/head/usr.bin/netstat/netstat.h
==============================================================================
--- soc2015/btw/head/usr.bin/netstat/netstat.h Thu Jun 11 00:24:33 2015 (r286932)
+++ soc2015/btw/head/usr.bin/netstat/netstat.h Thu Jun 11 01:28:01 2015 (r286933)
@@ -157,3 +157,4 @@
void mroutepr(void);
void mrt_stats(void);
void bpf_stats(char *);
+void ring_stats(char *);
More information about the svn-soc-all
mailing list