From owner-svn-src-all@freebsd.org Fri Sep 11 04:37:05 2015 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 19482A01524; Fri, 11 Sep 2015 04:37:05 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (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 06FE21EDB; Fri, 11 Sep 2015 04:37:05 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.70]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id t8B4b4Bn002232; Fri, 11 Sep 2015 04:37:04 GMT (envelope-from markj@FreeBSD.org) Received: (from markj@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id t8B4b2O3002215; Fri, 11 Sep 2015 04:37:02 GMT (envelope-from markj@FreeBSD.org) Message-Id: <201509110437.t8B4b2O3002215@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: markj set sender to markj@FreeBSD.org using -f From: Mark Johnston Date: Fri, 11 Sep 2015 04:37:02 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r287649 - head/usr.bin/netstat X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Sep 2015 04:37:05 -0000 Author: markj Date: Fri Sep 11 04:37:01 2015 New Revision: 287649 URL: https://svnweb.freebsd.org/changeset/base/287649 Log: Use a common subroutine to fetch and zero protocol stats instead of duplicating roughly similar code for each protocol. MFC after: 2 weeks Modified: head/usr.bin/netstat/flowtable.c head/usr.bin/netstat/if.c head/usr.bin/netstat/inet.c head/usr.bin/netstat/inet6.c head/usr.bin/netstat/main.c head/usr.bin/netstat/mbuf.c head/usr.bin/netstat/mroute.c head/usr.bin/netstat/mroute6.c head/usr.bin/netstat/netstat.h head/usr.bin/netstat/sctp.c Modified: head/usr.bin/netstat/flowtable.c ============================================================================== --- head/usr.bin/netstat/flowtable.c Fri Sep 11 04:20:04 2015 (r287648) +++ head/usr.bin/netstat/flowtable.c Fri Sep 11 04:37:01 2015 (r287649) @@ -28,13 +28,15 @@ #include __FBSDID("$FreeBSD$"); + #include -#include + #include -#include + #include #include #include + #include "netstat.h" /* @@ -68,17 +70,18 @@ void flowtable_stats(void) { struct flowtable_stat stat; - size_t len = sizeof(stat); if (!live) return; - if (sysctlbyname("net.flowtable.ip4.stat", &stat, &len, NULL, 0) == 0) { + if (fetch_stats("net.flowtable.ip4.stat", 0, &stat, + sizeof(stat), NULL) == 0) { printf("flowtable for IPv4:\n"); print_stats(&stat); } - if (sysctlbyname("net.flowtable.ip6.stat", &stat, &len, NULL, 0) == 0) { + if (fetch_stats("net.flowtable.ip6.stat", 0, &stat, + sizeof(stat), NULL) == 0) { printf("flowtable for IPv6:\n"); print_stats(&stat); } Modified: head/usr.bin/netstat/if.c ============================================================================== --- head/usr.bin/netstat/if.c Fri Sep 11 04:20:04 2015 (r287648) +++ head/usr.bin/netstat/if.c Fri Sep 11 04:37:01 2015 (r287649) @@ -41,7 +41,6 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include #include #include @@ -134,20 +133,11 @@ pfsync_acts_stats(const char *list, cons void pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { - struct pfsyncstats pfsyncstat, zerostat; - size_t len = sizeof(struct pfsyncstats); + struct pfsyncstats pfsyncstat; - if (live) { - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.pfsync.stats", &pfsyncstat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - if (errno != ENOENT) - warn("sysctl: net.pfsync.stats"); - return; - } - } else - kread(off, &pfsyncstat, len); + if (fetch_stats("net.pfsync.stats", off, &pfsyncstat, + sizeof(pfsyncstat), kread) != 0) + return; xo_emit("{T:/%s}:\n", name); xo_open_container(name); Modified: head/usr.bin/netstat/inet.c ============================================================================== --- head/usr.bin/netstat/inet.c Fri Sep 11 04:20:04 2015 (r287648) +++ head/usr.bin/netstat/inet.c Fri Sep 11 04:37:01 2015 (r287649) @@ -627,8 +627,7 @@ protopr(u_long off, const char *name, in void tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { - struct tcpstat tcpstat, zerostat; - size_t len = sizeof tcpstat; + struct tcpstat tcpstat; #ifdef INET6 if (tcp_done != 0) @@ -637,16 +636,9 @@ tcp_stats(u_long off, const char *name, tcp_done = 1; #endif - if (live) { - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - xo_warn("sysctl: net.inet.tcp.stats"); - return; - } - } else - kread_counters(off, &tcpstat, len); + if (fetch_stats("net.inet.tcp.stats", off, &tcpstat, + sizeof(tcpstat), kread_counters) != 0) + return; xo_open_container("tcp"); xo_emit("{T:/%s}:\n", name); @@ -860,8 +852,7 @@ tcp_stats(u_long off, const char *name, void udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { - struct udpstat udpstat, zerostat; - size_t len = sizeof udpstat; + struct udpstat udpstat; uint64_t delivered; #ifdef INET6 @@ -871,16 +862,9 @@ udp_stats(u_long off, const char *name, udp_done = 1; #endif - if (live) { - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.inet.udp.stats", &udpstat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - xo_warn("sysctl: net.inet.udp.stats"); - return; - } - } else - kread_counters(off, &udpstat, len); + if (fetch_stats("net.inet.udp.stats", off, &udpstat, + sizeof(udpstat), kread_counters) != 0) + return; xo_open_container("udp"); xo_emit("{T:/%s}:\n", name); @@ -933,23 +917,11 @@ udp_stats(u_long off, const char *name, void carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { - struct carpstats carpstat, zerostat; - size_t len = sizeof(struct carpstats); + struct carpstats carpstat; - if (live) { - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.inet.carp.stats", &carpstat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - if (errno != ENOENT) - xo_warn("sysctl: net.inet.carp.stats"); - return; - } - } else { - if (off == 0) - return; - kread_counters(off, &carpstat, len); - } + if (fetch_stats("net.inet.carp.stats", off, &carpstat, + sizeof(carpstat), kread_counters) != 0) + return; xo_open_container(name); xo_emit("{T:/%s}:\n", name); @@ -1000,19 +972,11 @@ carp_stats(u_long off, const char *name, void ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { - struct ipstat ipstat, zerostat; - size_t len = sizeof ipstat; + struct ipstat ipstat; - if (live) { - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.inet.ip.stats", &ipstat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - xo_warn("sysctl: net.inet.ip.stats"); - return; - } - } else - kread_counters(off, &ipstat, len); + if (fetch_stats("net.inet.ip.stats", off, &ipstat, + sizeof(ipstat), kread_counters) != 0) + return; xo_open_container(name); xo_emit("{T:/%s}:\n", name); @@ -1093,19 +1057,11 @@ ip_stats(u_long off, const char *name, i void arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { - struct arpstat arpstat, zerostat; - size_t len = sizeof(arpstat); + struct arpstat arpstat; - if (live) { - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - xo_warn("sysctl: net.link.ether.arp.stats"); - return; - } - } else - kread_counters(off, &arpstat, len); + if (fetch_stats("net.link.ether.arp.stats", off, &arpstat, + sizeof(arpstat), kread_counters) != 0) + return; xo_open_container(name); xo_emit("{T:/%s}:\n", name); @@ -1186,21 +1142,13 @@ static const char *icmpnames[ICMP_MAXTYP void icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { - struct icmpstat icmpstat, zerostat; - int i, first; + struct icmpstat icmpstat; size_t len; + int i, first; - len = sizeof icmpstat; - if (live) { - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - xo_warn("sysctl: net.inet.icmp.stats"); - return; - } - } else - kread_counters(off, &icmpstat, len); + if (fetch_stats("net.inet.icmp.stats", off, &icmpstat, + sizeof(icmpstat), kread_counters) != 0) + return; xo_open_container(name); xo_emit("{T:/%s}:\n", name); @@ -1300,22 +1248,11 @@ icmp_stats(u_long off, const char *name, void igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { - struct igmpstat igmpstat, zerostat; - size_t len; + struct igmpstat igmpstat; - len = sizeof(igmpstat); - if (live) { - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - xo_warn("sysctl: net.inet.igmp.stats"); - return; - } - } else { - len = sizeof(igmpstat); - kread(off, &igmpstat, len); - } + if (fetch_stats("net.inet.igmp.stats", 0, &igmpstat, + sizeof(igmpstat), kread) != 0) + return; if (igmpstat.igps_version != IGPS_VERSION_3) { xo_warnx("%s: version mismatch (%d != %d)", __func__, @@ -1380,23 +1317,11 @@ void pim_stats(u_long off __unused, const char *name, int af1 __unused, int proto __unused) { - struct pimstat pimstat, zerostat; - size_t len = sizeof pimstat; + struct pimstat pimstat; - if (live) { - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.inet.pim.stats", &pimstat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - if (errno != ENOENT) - xo_warn("sysctl: net.inet.pim.stats"); - return; - } - } else { - if (off == 0) - return; - kread_counters(off, &pimstat, len); - } + if (fetch_stats("net.inet.pim.stats", off, &pimstat, + sizeof(pimstat), kread_counters) != 0) + return; xo_open_container(name); xo_emit("{T:/%s}:\n", name); Modified: head/usr.bin/netstat/inet6.c ============================================================================== --- head/usr.bin/netstat/inet6.c Fri Sep 11 04:20:04 2015 (r287648) +++ head/usr.bin/netstat/inet6.c Fri Sep 11 04:37:01 2015 (r287649) @@ -44,7 +44,6 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include #include #include @@ -359,23 +358,13 @@ static const char *srcrule_str[] = { void ip6_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { - struct ip6stat ip6stat, zerostat; + struct ip6stat ip6stat; int first, i; - size_t len; - len = sizeof ip6stat; - if (live) { - memset(&ip6stat, 0, len); - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.inet6.ip6.stats", &ip6stat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - if (errno != ENOENT) - xo_warn("sysctl: net.inet6.ip6.stats"); - return; - } - } else - kread_counters(off, &ip6stat, len); + if (fetch_stats("net.inet6.ip6.stats", off, &ip6stat, + sizeof(ip6stat), kread_counters) != 0) + return; + xo_open_container(name); xo_emit("{T:/%s}:\n", name); @@ -956,23 +945,12 @@ static const char *icmp6names[] = { void icmp6_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { - struct icmp6stat icmp6stat, zerostat; + struct icmp6stat icmp6stat; int i, first; - size_t len; - len = sizeof icmp6stat; - if (live) { - memset(&icmp6stat, 0, len); - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.inet6.icmp6.stats", &icmp6stat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - if (errno != ENOENT) - xo_warn("sysctl: net.inet6.icmp6.stats"); - return; - } - } else - kread_counters(off, &icmp6stat, len); + if (fetch_stats("net.inet6.icmp6.stats", off, &icmp6stat, + sizeof(icmp6stat), kread_counters) != 0) + return; xo_emit("{T:/%s}:\n", name); xo_open_container(name); @@ -1196,23 +1174,11 @@ end: void pim6_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { - struct pim6stat pim6stat, zerostat; - size_t len = sizeof pim6stat; + struct pim6stat pim6stat; - if (live) { - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.inet6.pim.stats", &pim6stat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - if (errno != ENOENT) - xo_warn("sysctl: net.inet6.pim.stats"); - return; - } - } else { - if (off == 0) - return; - kread(off, &pim6stat, len); - } + if (fetch_stats("net.inet6.pim.stats", off, &pim6stat, + sizeof(pim6stat), kread) != 0) + return; xo_emit("{T:/%s}:\n", name); xo_open_container(name); @@ -1244,22 +1210,12 @@ pim6_stats(u_long off, const char *name, void rip6_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { - struct rip6stat rip6stat, zerostat; + struct rip6stat rip6stat; u_quad_t delivered; - size_t len; - len = sizeof(rip6stat); - if (live) { - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.inet6.ip6.rip6stats", &rip6stat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - if (errno != ENOENT) - xo_warn("sysctl: net.inet6.ip6.rip6stats"); - return; - } - } else - kread_counters(off, &rip6stat, len); + if (fetch_stats("net.inet6.ip6.rip6stats", off, &rip6stat, + sizeof(rip6stat), kread_counters) != 0) + return; xo_emit("{T:/%s}:\n", name); xo_open_container(name); Modified: head/usr.bin/netstat/main.c ============================================================================== --- head/usr.bin/netstat/main.c Fri Sep 11 04:20:04 2015 (r287648) +++ head/usr.bin/netstat/main.c Fri Sep 11 04:37:01 2015 (r287649) @@ -47,6 +47,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include @@ -548,6 +549,29 @@ main(int argc, char *argv[]) exit(0); } +int +fetch_stats(const char *sysctlname, u_long off, void *stats, size_t len, + int (*kreadfn)(u_long, void *, size_t)) +{ + int error; + + if (live) { + memset(stats, 0, len); + if (zflag) + error = sysctlbyname(sysctlname, NULL, NULL, stats, + len); + else + error = sysctlbyname(sysctlname, stats, &len, NULL, 0); + if (error == -1 && errno != ENOENT) + xo_warn("sysctl %s", sysctlname); + } else { + if (off == 0) + return (1); + error = kreadfn(off, stats, len); + } + return (error); +} + /* * Print out protocol statistics or control blocks (per sflag). * If the interface was not specifically requested, and the symbol Modified: head/usr.bin/netstat/mbuf.c ============================================================================== --- head/usr.bin/netstat/mbuf.c Fri Sep 11 04:20:04 2015 (r287648) +++ head/usr.bin/netstat/mbuf.c Fri Sep 11 04:37:01 2015 (r287649) @@ -310,27 +310,22 @@ mbpr(void *kvmd, u_long mbaddr) jumbop_failures, jumbo9_failures, jumbo16_failures, jumbop_size / 1024); - if (live) { - mlen = sizeof(nsfbufs); - if (!sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL, - 0) && - !sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused, - &mlen, NULL, 0) && - !sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak, - &mlen, NULL, 0)) - xo_emit("{:nsfbufs-current/%d}/{:nsfbufs-peak/%d}/" - "{:nsfbufs/%d} " - "{N:sfbufs in use (current\\/peak\\/max)}\n", - nsfbufsused, nsfbufspeak, nsfbufs); - mlen = sizeof(sfstat); - if (sysctlbyname("kern.ipc.sfstat", &sfstat, &mlen, NULL, 0)) { - xo_warn("kern.ipc.sfstat"); - goto out; - } - } else { - if (kread_counters(mbaddr, (char *)&sfstat, sizeof sfstat) != 0) - goto out; - } + mlen = sizeof(nsfbufs); + if (live && + sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL, 0) == 0 && + sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused, &mlen, + NULL, 0) == 0 && + sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak, &mlen, + NULL, 0) == 0) + xo_emit("{:nsfbufs-current/%d}/{:nsfbufs-peak/%d}/" + "{:nsfbufs/%d} " + "{N:sfbufs in use (current\\/peak\\/max)}\n", + nsfbufsused, nsfbufspeak, nsfbufs); + + if (fetch_stats("kern.ipc.sfstat", mbaddr, &sfstat, sizeof(sfstat), + kread_counters) != 0) + goto out; + xo_emit("{:sfbufs-alloc-failed/%ju} {N:requests for sfbufs denied}\n", (uintmax_t)sfstat.sf_allocfail); xo_emit("{:sfbufs-alloc-wait/%ju} {N:requests for sfbufs delayed}\n", Modified: head/usr.bin/netstat/mroute.c ============================================================================== --- head/usr.bin/netstat/mroute.c Fri Sep 11 04:20:04 2015 (r287648) +++ head/usr.bin/netstat/mroute.c Fri Sep 11 04:37:01 2015 (r287649) @@ -400,7 +400,6 @@ mrt_stats() { struct mrtstat mrtstat; u_long mstaddr; - size_t len = sizeof(mrtstat); mstaddr = nl[N_MRTSTAT].n_value; @@ -409,14 +408,9 @@ mrt_stats() return; } - if (live) { - if (sysctlbyname("net.inet.ip.mrtstat", &mrtstat, &len, NULL, - 0) < 0) { - xo_warn("sysctl: net.inet.ip.mrtstat failed."); - return; - } - } else - kread_counters(mstaddr, &mrtstat, len); + if (fetch_stats("net.inet.ip.mrtstat", mstaddr, &mrtstat, + sizeof(mrtstat), kread_counters) != 0) + return; xo_emit("{T:IPv4 multicast forwarding}:\n"); Modified: head/usr.bin/netstat/mroute6.c ============================================================================== --- head/usr.bin/netstat/mroute6.c Fri Sep 11 04:20:04 2015 (r287648) +++ head/usr.bin/netstat/mroute6.c Fri Sep 11 04:37:01 2015 (r287649) @@ -231,13 +231,10 @@ void mrt6_stats() { struct mrt6stat mrtstat; - size_t len = sizeof mrtstat; - if (sysctlbyname("net.inet6.ip6.mrt6stat", &mrtstat, &len, NULL, 0) < - 0) { - xo_warn("sysctl: net.inet6.ip6.mrt6stat"); + if (fetch_stats("net.inet6.ip6.mrt6stat", 0, &mrtstat, + sizeof(mrtstat), kread_counters) != 0) return; - } xo_open_container("multicast-statistics"); xo_emit("{T:IPv6 multicast forwarding}:\n"); Modified: head/usr.bin/netstat/netstat.h ============================================================================== --- head/usr.bin/netstat/netstat.h Fri Sep 11 04:20:04 2015 (r287648) +++ head/usr.bin/netstat/netstat.h Fri Sep 11 04:37:01 2015 (r287649) @@ -63,6 +63,8 @@ extern int unit; /* unit number for abov extern int live; /* true if we are examining a live system */ +int fetch_stats(const char *sysctlname, u_long addr, void *stats, + size_t len, int (*kreadfn)(u_long, void *, size_t)); int kread(u_long addr, void *buf, size_t size); uint64_t kread_counter(u_long addr); int kread_counters(u_long addr, void *buf, size_t size); Modified: head/usr.bin/netstat/sctp.c ============================================================================== --- head/usr.bin/netstat/sctp.c Fri Sep 11 04:20:04 2015 (r287648) +++ head/usr.bin/netstat/sctp.c Fri Sep 11 04:37:01 2015 (r287649) @@ -658,20 +658,11 @@ sctp_statesprint(uint32_t state) void sctp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { - struct sctpstat sctpstat, zerostat; - size_t len = sizeof(sctpstat); + struct sctpstat sctpstat; - if (live) { - if (zflag) - memset(&zerostat, 0, len); - if (sysctlbyname("net.inet.sctp.stats", &sctpstat, &len, - zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { - if (errno != ENOENT) - xo_warn("sysctl: net.inet.sctp.stats"); - return; - } - } else - kread(off, &sctpstat, len); + if (fetch_stats("net.inet.sctp.stats", off, &sctpstat, + sizeof(sctpstat), kread) != 0) + return; xo_open_container(name); xo_emit("{T:/%s}:\n", name);