Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 8 Jul 2005 03:55:45 GMT
From:      soc-anders <soc-anders@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 79741 for review
Message-ID:  <200507080355.j683tjwB018786@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
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 <string.h>
 #include <unistd.h>
 
+#define IFCLEANUP
+
+#ifdef IFCLEANUP
+#include <net/if_mib.h>
+#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;
 	}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200507080355.j683tjwB018786>