From owner-svn-soc-all@FreeBSD.ORG Thu Jun 11 01:28:03 2015 Return-Path: Delivered-To: svn-soc-all@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 211DA7D2 for ; Thu, 11 Jun 2015 01:28:03 +0000 (UTC) (envelope-from btw@FreeBSD.org) Received: from socsvn.freebsd.org (socsvn.freebsd.org [IPv6:2001:1900:2254:206a::50:2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 0F66A11AC for ; Thu, 11 Jun 2015 01:28:03 +0000 (UTC) (envelope-from btw@FreeBSD.org) Received: from socsvn.freebsd.org ([127.0.1.124]) by socsvn.freebsd.org (8.14.9/8.14.9) with ESMTP id t5B1S2qu080194 for ; Thu, 11 Jun 2015 01:28:02 GMT (envelope-from btw@FreeBSD.org) Received: (from www@localhost) by socsvn.freebsd.org (8.14.9/8.14.9/Submit) id t5B1S1Fp080169 for svn-soc-all@FreeBSD.org; Thu, 11 Jun 2015 01:28:01 GMT (envelope-from btw@FreeBSD.org) Date: Thu, 11 Jun 2015 01:28:01 GMT Message-Id: <201506110128.t5B1S1Fp080169@socsvn.freebsd.org> X-Authentication-Warning: socsvn.freebsd.org: www set sender to btw@FreeBSD.org using -f From: btw@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r286933 - soc2015/btw/head/usr.bin/netstat MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-soc-all@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: SVN commit messages for the entire Summer of Code repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Jun 2015 01:28:03 -0000 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 #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); +} 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 *);