From owner-p4-projects@FreeBSD.ORG Fri Jul 8 03:55:46 2005 Return-Path: X-Original-To: p4-projects@freebsd.org Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 12E1016A420; Fri, 8 Jul 2005 03:55:46 +0000 (GMT) X-Original-To: perforce@freebsd.org Delivered-To: perforce@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id C73E816A41C for ; Fri, 8 Jul 2005 03:55:45 +0000 (GMT) (envelope-from soc-anders@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8BED643D49 for ; Fri, 8 Jul 2005 03:55:45 +0000 (GMT) (envelope-from soc-anders@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.13.1/8.13.1) with ESMTP id j683tjxt018789 for ; Fri, 8 Jul 2005 03:55:45 GMT (envelope-from soc-anders@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.1/8.13.1/Submit) id j683tjwB018786 for perforce@freebsd.org; Fri, 8 Jul 2005 03:55:45 GMT (envelope-from soc-anders@freebsd.org) Date: Fri, 8 Jul 2005 03:55:45 GMT Message-Id: <200507080355.j683tjwB018786@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to soc-anders@freebsd.org using -f From: soc-anders To: Perforce Change Reviews Cc: Subject: PERFORCE change 79741 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Jul 2005 03:55:46 -0000 http://perforce.freebsd.org/chv.cgi?CH=79741 Change 79741 by soc-anders@soc-anders_gimli on 2005/07/08 03:54:44 Removed kvm calls from sidewaysintpr() and replaced with ifmib. Affected files ... .. //depot/projects/soc2005/ifcleanup/src/src/usr.bin/netstat/if.c#2 edit Differences ... ==== //depot/projects/soc2005/ifcleanup/src/src/usr.bin/netstat/if.c#2 (text+ko) ==== @@ -64,6 +64,12 @@ #include #include +#define IFCLEANUP + +#ifdef IFCLEANUP +#include +#endif + #include "netstat.h" #define YES 1 @@ -176,14 +182,15 @@ short network_layer; short link_layer; - if (ifnetaddr == 0) { - printf("ifnet: symbol not defined\n"); + if (_interval) { + sidewaysintpr((unsigned)_interval, ifnetaddr); return; } - if (_interval) { - sidewaysintpr((unsigned)_interval, ifnetaddr); + else if (ifnetaddr == 0) { + printf("ifnet: symbol not defined\n"); return; } + if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead)) return; ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead); @@ -494,7 +501,7 @@ u_long ift_op; /* output packets */ u_long ift_oe; /* output errors */ u_long ift_co; /* collisions */ - u_int ift_dr; /* drops */ + u_long ift_dr; /* drops */ u_long ift_ib; /* input bytes */ u_long ift_ob; /* output bytes */ }; @@ -508,20 +515,29 @@ * First line printed at top of screen is always cumulative. * XXX - should be rewritten to use ifmib(4). */ + static void sidewaysintpr(unsigned interval1, u_long off) { - struct ifnet ifnet; - u_long firstifnet; - struct ifnethead ifnethead; struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting; int line; int oldmask, first; - u_long interesting_off; + int mib[6], ifcnt = 0, interesting_row; + int i; + size_t len; + struct ifmibdata ifmib; + + mib[0] = CTL_NET; + mib[1] = PF_LINK; + mib[2] = NETLINK_GENERIC; + mib[3] = IFMIB_SYSTEM; + mib[4] = IFMIB_IFCOUNT; - if (kread(off, (char *)&ifnethead, sizeof ifnethead)) - return; - firstifnet = (u_long)TAILQ_FIRST(&ifnethead); + len = sizeof(ifcnt); + if (sysctl(mib, 5, (void *)&ifcnt, &len, (void *)0, 0) < 0) { + printf("sysctl\n"); + exit(1); + } if ((iftot = malloc(sizeof(struct iftot))) == NULL) { printf("malloc failed\n"); @@ -530,16 +546,23 @@ memset(iftot, 0, sizeof(struct iftot)); interesting = NULL; - interesting_off = 0; - for (off = firstifnet, ip = iftot; off;) { + mib[3] = IFMIB_IFDATA; + mib[5] = IFDATA_GENERAL; + len = sizeof(ifmib); + for (i = 1; i <= ifcnt; i++) { + char name[IFNAMSIZ]; - if (kread(off, (char *)&ifnet, sizeof ifnet)) - break; - strlcpy(name, ifnet.if_xname, sizeof(name)); + mib[4] = i; + if (sysctl(mib, 6, (void *)&ifmib, &len, (void *)0, 0) < 0) { + printf("sysctl\n"); + exit(1); + } + strlcpy(name, ifmib.ifmd_name, sizeof(name)); + if (interface && strcmp(name, interface) == 0) { interesting = ip; - interesting_off = off; + interesting_row = i; } snprintf(ip->ift_name, sizeof(ip->ift_name), "(%s)", name);; if ((ipn = malloc(sizeof(struct iftot))) == NULL) { @@ -549,8 +572,8 @@ memset(ipn, 0, sizeof(struct iftot)); SLIST_NEXT(ip, chain) = ipn; ip = ipn; - off = (u_long)TAILQ_NEXT(&ifnet, if_link); } + if ((total = malloc(sizeof(struct iftot))) == NULL) { printf("malloc failed\n"); exit(1); @@ -566,6 +589,7 @@ signalled = NO; (void)alarm(interval1); first = 1; + banner: printf("%17s %14s %16s", "input", interesting ? interesting->ift_name : "(Total)", "output"); @@ -580,30 +604,32 @@ loop: if (interesting != NULL) { ip = interesting; - if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) { + mib[4] = interesting_row; + if (sysctl(mib, 6, (void *)&ifmib, &len, (void *)0, 0) < 0) { printf("???\n"); exit(1); }; + if (!first) { printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu", - ifnet.if_ipackets - ip->ift_ip, - ifnet.if_ierrors - ip->ift_ie, - ifnet.if_ibytes - ip->ift_ib, - ifnet.if_opackets - ip->ift_op, - ifnet.if_oerrors - ip->ift_oe, - ifnet.if_obytes - ip->ift_ob, - ifnet.if_collisions - ip->ift_co); + ifmib.ifmd_data.ifi_ipackets - ip->ift_ip, + ifmib.ifmd_data.ifi_ierrors - ip->ift_ie, + ifmib.ifmd_data.ifi_ibytes - ip->ift_ib, + ifmib.ifmd_data.ifi_opackets - ip->ift_op, + ifmib.ifmd_data.ifi_oerrors - ip->ift_oe, + ifmib.ifmd_data.ifi_obytes - ip->ift_ob, + ifmib.ifmd_data.ifi_collisions - ip->ift_co); if (dflag) - printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr); + printf(" %5lu", ifmib.ifmd_data.ifi_iqdrops - ip->ift_dr); } - ip->ift_ip = ifnet.if_ipackets; - ip->ift_ie = ifnet.if_ierrors; - ip->ift_ib = ifnet.if_ibytes; - ip->ift_op = ifnet.if_opackets; - ip->ift_oe = ifnet.if_oerrors; - ip->ift_ob = ifnet.if_obytes; - ip->ift_co = ifnet.if_collisions; - ip->ift_dr = ifnet.if_snd.ifq_drops; + ip->ift_ip = ifmib.ifmd_data.ifi_ipackets; + ip->ift_ie = ifmib.ifmd_data.ifi_ierrors; + ip->ift_ib = ifmib.ifmd_data.ifi_ibytes; + ip->ift_op = ifmib.ifmd_data.ifi_opackets; + ip->ift_oe = ifmib.ifmd_data.ifi_oerrors; + ip->ift_ob = ifmib.ifmd_data.ifi_obytes; + ip->ift_co = ifmib.ifmd_data.ifi_collisions; + ip->ift_dr = ifmib.ifmd_data.ifi_iqdrops; } else { sum->ift_ip = 0; sum->ift_ie = 0; @@ -613,22 +639,22 @@ sum->ift_ob = 0; sum->ift_co = 0; sum->ift_dr = 0; - for (off = firstifnet, ip = iftot; - off && SLIST_NEXT(ip, chain) != NULL; - ip = SLIST_NEXT(ip, chain)) { - if (kread(off, (char *)&ifnet, sizeof ifnet)) { - off = 0; - continue; + + for (i = 1; i <= ifcnt; i++) { + mib[4] = i; + if (sysctl(mib, 6, &ifmib, &len, (void *)0, 0) < 0) { + printf("sysctl\n"); + exit(1); } - sum->ift_ip += ifnet.if_ipackets; - sum->ift_ie += ifnet.if_ierrors; - sum->ift_ib += ifnet.if_ibytes; - sum->ift_op += ifnet.if_opackets; - sum->ift_oe += ifnet.if_oerrors; - sum->ift_ob += ifnet.if_obytes; - sum->ift_co += ifnet.if_collisions; - sum->ift_dr += ifnet.if_snd.ifq_drops; - off = (u_long)TAILQ_NEXT(&ifnet, if_link); + + sum->ift_ip += ifmib.ifmd_data.ifi_ipackets; + sum->ift_ie += ifmib.ifmd_data.ifi_ierrors; + sum->ift_ib += ifmib.ifmd_data.ifi_ibytes; + sum->ift_op += ifmib.ifmd_data.ifi_opackets; + sum->ift_oe += ifmib.ifmd_data.ifi_oerrors; + sum->ift_ob += ifmib.ifmd_data.ifi_obytes; + sum->ift_co += ifmib.ifmd_data.ifi_collisions; + sum->ift_dr += ifmib.ifmd_data.ifi_iqdrops; } if (!first) { printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu", @@ -640,7 +666,7 @@ sum->ift_ob - total->ift_ob, sum->ift_co - total->ift_co); if (dflag) - printf(" %5u", sum->ift_dr - total->ift_dr); + printf(" %5lu", sum->ift_dr - total->ift_dr); } *total = *sum; }