Index: usr.bin/netstat/if.c =================================================================== --- usr.bin/netstat/if.c +++ usr.bin/netstat/if.c @@ -46,6 +46,7 @@ #include #include +#include #include #include #include @@ -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); +} Index: usr.bin/netstat/main.c =================================================================== --- usr.bin/netstat/main.c +++ usr.bin/netstat/main.c @@ -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); Index: usr.bin/netstat/netstat.h =================================================================== --- usr.bin/netstat/netstat.h +++ usr.bin/netstat/netstat.h @@ -157,3 +157,4 @@ void mroutepr(void); void mrt_stats(void); void bpf_stats(char *); +void ring_stats(char *);