PERFORCE change 166424 for review

Gabor Pali pgj at FreeBSD.org
Wed Jul 22 22:43:10 UTC 2009


http://perforce.freebsd.org/chv.cgi?CH=166424

Change 166424 by pgj at petymeg-current on 2009/07/22 22:42:09

	Modify netstat(1) to use libnetstat(3) for interface statistics,
	and it also includes some further simplifications in the original
	code.

Affected files ...

.. //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/extern.h#8 edit
.. //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/if.c#3 edit
.. //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/main.c#15 edit

Differences ...

==== //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/extern.h#8 (text+ko) ====

@@ -119,7 +119,7 @@
 void	hostpr(u_long, u_long);
 void	impstats(u_long, u_long);
 
-void	intpr(int, u_long, void (*)(char *));
+void	intpr(int, void *, void (*)(char *));
 
 void	pr_rthdr(int);
 void	pr_family(int);

==== //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/if.c#3 (text+ko) ====

@@ -1,6 +1,8 @@
 /*-
  * Copyright (c) 1983, 1988, 1993
- *	The Regents of the University of California.  All rights reserved.
+ *	The Regents of the University of California.
+ * Copyright (c) 2009 Gabor Pali
+ * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -62,7 +64,9 @@
 
 #include <err.h>
 #include <errno.h>
+#include <kvm.h>
 #include <libutil.h>
+#include <netstat.h>
 #include <signal.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -75,13 +79,9 @@
 #define	YES	1
 #define	NO	0
 
-static void sidewaysintpr(int, u_long);
+static void sidewaysintpr(int, void *);
 static void catchalarm(int);
 
-#ifdef INET6
-static char ntop_buf[INET6_ADDRSTRLEN];		/* for inet_ntop() */
-#endif
-
 /*
  * Dump pfsync statistics structure.
  */
@@ -175,50 +175,53 @@
  * Print a description of the network interfaces.
  */
 void
-intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *))
+intpr(int interval1, void *kvm_handle, void (*pfunc)(char *))
 {
-	struct ifnet ifnet;
-	struct ifnethead ifnethead;
-	union {
-		struct ifaddr ifa;
-		struct in_ifaddr in;
-#ifdef INET6
-		struct in6_ifaddr in6;
-#endif
-		struct ipx_ifaddr ipx;
-	} ifaddr;
-	u_long ifaddraddr;
-	u_long ifaddrfound;
-	u_long ifnetfound;
-	u_long opackets;
-	u_long ipackets;
-	u_long obytes;
-	u_long ibytes;
-	u_long omcasts;
-	u_long imcasts;
-	u_long oerrors;
-	u_long ierrors;
-	u_long collisions;
-	short timer;
-	int drops;
-	struct sockaddr *sa = NULL;
-	char name[IFNAMSIZ];
-	short network_layer;
-	short link_layer;
+	int it_flags;
+	kvm_t *kvm;
+
+	struct interface_type_list *itlp;
+	struct interface_type_iterator *itip;
+
+	const struct face_type *fin, *fout;
+	const struct interface_type *itp;
+	const struct intfaddr_type *iatp;
+
+	char if_name[16];
+	int error, i, network_layer, link_layer;
 
-	if (ifnetaddr == 0) {
-		printf("ifnet: symbol not defined\n");
+	if (kvm_handle == NULL)
 		return;
-	}
+
 	if (interval1) {
-		sidewaysintpr(interval1, ifnetaddr);
+		sidewaysintpr(interval1, kvm_handle);
 		return;
 	}
-	if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead) != 0)
+
+	kvm = (kvm_t *)kvm_handle;
+	it_flags = 0;
+	it_flags |= NETSTAT_INTERFACE_ALL;
+	it_flags |= NETSTAT_INTERFACE_KVM;
+
+	itlp = netstat_itl_alloc();
+	if (itlp == NULL) {
+		warn("netstat_itl_alloc()");
 		return;
-	ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
-	if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
-		return;
+	}
+
+	if (netstat_interface(interface, af, itlp, it_flags, kvm) < 0) {
+		error = netstat_itl_geterror(itlp);
+		if (error == NETSTAT_ERROR_KVM) {
+			warnx("netstat_interface: %s", kvm_geterr(kvm));
+		} else
+			warnx("netstat_interface: %s", netstat_strerror(error));
+		goto out;
+	}
+
+	if (netstat_iti_alloc(itlp, &itip) < 0) {
+		warn("netstat_iti_alloc()");
+		goto out;
+	}
 
 	if (!pfunc) {
 		if (Wflag)
@@ -228,10 +231,10 @@
 		printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s",
 		    "Mtu", "Network", "Address", "Ipkts", "Ierrs");
 		if (bflag)
-			printf(" %10.10s","Ibytes");
+			printf(" %10.10s", "Ibytes");
 		printf(" %8.8s %5.5s", "Opkts", "Oerrs");
 		if (bflag)
-			printf(" %10.10s","Obytes");
+			printf(" %10.10s", "Obytes");
 		printf(" %5s", "Coll");
 		if (tflag)
 			printf(" %s", "Time");
@@ -239,278 +242,102 @@
 			printf(" %s", "Drop");
 		putchar('\n');
 	}
-	ifaddraddr = 0;
-	while (ifnetaddr || ifaddraddr) {
-		struct sockaddr_in *sockin;
-#ifdef INET6
-		struct sockaddr_in6 *sockin6;
-#endif
-		char *cp;
-		int n, m;
 
-		network_layer = 0;
-		link_layer = 0;
+	for (itp = netstat_iti_first(itip); itp != NULL;
+	    itp = netstat_iti_next(itip)) {
+		strlcpy(if_name, netstat_it_get_name(itp), sizeof(if_name));
+		if (pfunc) {
+			pfunc(if_name);
+			continue;
+		}
 
-		if (ifaddraddr == 0) {
-			ifnetfound = ifnetaddr;
-			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
-				return;
-			strlcpy(name, ifnet.if_xname, sizeof(name));
-			ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
-			if (interface != 0 && strcmp(name, interface) != 0)
-				continue;
-			cp = index(name, '\0');
+		for (i = 0; i < netstat_it_get_addrcnt(itp); i++) {
+			iatp = netstat_it_get_address(itp, i);
+			
+			network_layer =
+			    (netstat_iat_get_layer(iatp) == layer_Network);
+			link_layer =
+			    (netstat_iat_get_layer(iatp) == layer_Link);
 
-			if (pfunc) {
-				(*pfunc)(name);
-				continue;
+			if ((netstat_it_get_flags(itp) & NETSTAT_IF_UP) == 0) {
+				strcat(if_name, "*");
 			}
-
-			if ((ifnet.if_flags&IFF_UP) == 0)
-				*cp++ = '*';
-			*cp = '\0';
-			ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
+			printf(Wflag ? "%-7.7s" : "%-5.5s", if_name);
+			printf(" %5ju ", netstat_it_get_mtu(itp));
+			printf("%-13.13s ",
+			    netstat_iat_get_network(iatp, numeric_addr));
+			printf("%-17.17s ",
+			    netstat_iat_get_address(iatp, numeric_addr));
+			fin  = netstat_it_get_in(itp);
+			fout = netstat_it_get_out(itp);
+			show_stat("lu", 8,
+			    network_layer ?
+				netstat_iat_get_ipackets(iatp) :
+				netstat_ft_get_packets(fin),
+			    network_layer|link_layer);
+			show_stat("lu", 5, netstat_ft_get_errors(fin),
+			    link_layer);
+			if (bflag)
+				show_stat("lu", 10,
+				    network_layer ?
+					netstat_iat_get_ibytes(iatp) :
+					netstat_ft_get_bytes(fin),
+				    network_layer|link_layer);
+			show_stat("lu", 8,
+			    network_layer ?
+				netstat_iat_get_opackets(iatp) :
+				netstat_ft_get_packets(fout),
+			    network_layer|link_layer);
+			show_stat("lu", 5, netstat_ft_get_errors(fout),
+			    link_layer);
+			if (bflag)
+				show_stat("lu", 10,
+				    network_layer ?
+					netstat_iat_get_obytes(iatp) :
+					netstat_ft_get_bytes(fout),
+				    network_layer|link_layer);
+			show_stat("NRSlu", 5, netstat_it_get_collisions(itp),
+			    link_layer);
+			if (tflag)
+				show_stat("LSd", 4, netstat_it_get_timer(itp),
+				    link_layer);
+			if (dflag)
+				show_stat("LSd", 4, netstat_it_get_drops(itp),
+				    link_layer);
+			putchar('\n');
 		}
-		ifaddrfound = ifaddraddr;
 
-		/*
-		 * Get the interface stats.  These may get
-		 * overriden below on a per-interface basis.
-		 */
-		opackets = ifnet.if_opackets;
-		ipackets = ifnet.if_ipackets;
-		obytes = ifnet.if_obytes;
-		ibytes = ifnet.if_ibytes;
-		omcasts = ifnet.if_omcasts;
-		imcasts = ifnet.if_imcasts;
-		oerrors = ifnet.if_oerrors;
-		ierrors = ifnet.if_ierrors;
-		collisions = ifnet.if_collisions;
-		timer = ifnet.if_timer;
-		drops = ifnet.if_snd.ifq_drops;
+		if (!aflag)
+			continue;
 
-		if (ifaddraddr == 0) {
-			if (Wflag)
-				printf("%-7.7s", name);
-			else
-				printf("%-5.5s", name);
-			printf(" %5lu ", ifnet.if_mtu);
-			printf("%-13.13s ", "none");
-			printf("%-17.17s ", "none");
-		} else {
-			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)
-			    != 0) {
-				ifaddraddr = 0;
-				continue;
-			}
-#define	CP(x) ((char *)(x))
-			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
-				CP(&ifaddr);
-			sa = (struct sockaddr *)cp;
-			if (af != AF_UNSPEC && sa->sa_family != af) {
-				ifaddraddr =
-				    (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
-				continue;
-			}
-			if (Wflag)
-				printf("%-7.7s", name);
-			else
-				printf("%-5.5s", name);
-			printf(" %5lu ", ifnet.if_mtu);
-			switch (sa->sa_family) {
-			case AF_UNSPEC:
-				printf("%-13.13s ", "none");
-				printf("%-15.15s ", "none");
-				break;
-			case AF_INET:
-				sockin = (struct sockaddr_in *)sa;
-#ifdef notdef
-				/* can't use inet_makeaddr because kernel
-				 * keeps nets unshifted.
-				 */
-				in = inet_makeaddr(ifaddr.in.ia_subnet,
-					INADDR_ANY);
-				printf("%-13.13s ", netname(in.s_addr,
-				    ifaddr.in.ia_subnetmask));
-#else
-				printf("%-13.13s ",
-				    netname(htonl(ifaddr.in.ia_subnet),
-				    ifaddr.in.ia_subnetmask));
-#endif
-				printf("%-17.17s ",
-				    routename(sockin->sin_addr.s_addr));
+		for (i = 0; i < netstat_it_get_mcast_addrcnt(itp); i++) {
+			iatp = netstat_it_get_mcast_address(itp, i);
 
-				network_layer = 1;
-				break;
-#ifdef INET6
-			case AF_INET6:
-				sockin6 = (struct sockaddr_in6 *)sa;
-				printf("%-13.13s ",
-				       netname6(&ifaddr.in6.ia_addr,
-						&ifaddr.in6.ia_prefixmask.sin6_addr));
-				printf("%-17.17s ",
-				    inet_ntop(AF_INET6,
-					&sockin6->sin6_addr,
-					ntop_buf, sizeof(ntop_buf)));
-
-				network_layer = 1;
-				break;
-#endif /*INET6*/
-			case AF_IPX:
-				{
-				struct sockaddr_ipx *sipx =
-					(struct sockaddr_ipx *)sa;
-				u_long net;
-				char netnum[10];
-
-				*(union ipx_net *) &net = sipx->sipx_addr.x_net;
-				sprintf(netnum, "%lx", (u_long)ntohl(net));
-				printf("ipx:%-8s  ", netnum);
-/*				printf("ipx:%-8s ", netname(net, 0L)); */
-				printf("%-17s ",
-				    ipx_phost((struct sockaddr *)sipx));
-				}
-
-				network_layer = 1;
-				break;
-
-			case AF_APPLETALK:
-				printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
-				printf("%-11.11s  ",atalk_print(sa,0x0b) );
-				break;
-			case AF_LINK:
-				{
-				struct sockaddr_dl *sdl =
-					(struct sockaddr_dl *)sa;
-				char linknum[10];
-				cp = (char *)LLADDR(sdl);
-				n = sdl->sdl_alen;
-				sprintf(linknum, "<Link#%d>", sdl->sdl_index);
-				m = printf("%-13.13s ", linknum);
-				}
-				goto hexprint;
-			default:
-				m = printf("(%d)", sa->sa_family);
-				for (cp = sa->sa_len + (char *)sa;
-					--cp > sa->sa_data && (*cp == 0);) {}
-				n = cp - sa->sa_data + 1;
-				cp = sa->sa_data;
-			hexprint:
-				while (--n >= 0)
-					m += printf("%02x%c", *cp++ & 0xff,
-						    n > 0 ? ':' : ' ');
-				m = 32 - m;
-				while (m-- > 0)
-					putchar(' ');
-
-				link_layer = 1;
-				break;
+			if (netstat_iat_get_family(iatp) == PF_INET6) {
+				printf("%*s %-19.19s(refs: %d)",
+				    Wflag ? 27 : 25, "",
+				    netstat_iat_get_address(iatp, numeric_addr),
+				    netstat_iat_get_refcount(iatp));
+			} else {
+				printf("%*s %-17.17s", Wflag ? 27 : 25, "",
+				    netstat_iat_get_address(iatp, numeric_addr));
 			}
 
-			/*
-			 * Fixup the statistics for interfaces that
-			 * update stats for their network addresses
-			 */
-			if (network_layer) {
-				opackets = ifaddr.in.ia_ifa.if_opackets;
-				ipackets = ifaddr.in.ia_ifa.if_ipackets;
-				obytes = ifaddr.in.ia_ifa.if_obytes;
-				ibytes = ifaddr.in.ia_ifa.if_ibytes;
+			if (netstat_iat_get_family(iatp) == PF_LINK) {
+				printf(" %8ju", netstat_ft_get_mcasts(fin));
+				printf("%*s", bflag ? 17 : 6, "");
+				printf(" %8ju", netstat_ft_get_mcasts(fout));
 			}
 
-			ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
+			putchar('\n');
 		}
-
-		show_stat("lu", 8, ipackets, link_layer|network_layer);
-		show_stat("lu", 5, ierrors, link_layer);
-		if (bflag)
-			show_stat("lu", 10, ibytes, link_layer|network_layer);
-
-		show_stat("lu", 8, opackets, link_layer|network_layer);
-		show_stat("lu", 5, oerrors, link_layer);
-		if (bflag)
-			show_stat("lu", 10, obytes, link_layer|network_layer);
-
-		show_stat("NRSlu", 5, collisions, link_layer);
-		if (tflag)
-			show_stat("LSd", 4, timer, link_layer);
-		if (dflag)
-			show_stat("LSd", 4, drops, link_layer);
-		putchar('\n');
-
-		if (aflag && ifaddrfound) {
-			/*
-			 * Print family's multicast addresses
-			 */
-			struct ifmultiaddr *multiaddr;
-			struct ifmultiaddr ifma;
-			union {
-				struct sockaddr sa;
-				struct sockaddr_in in;
-#ifdef INET6
-				struct sockaddr_in6 in6;
-#endif /* INET6 */
-				struct sockaddr_dl dl;
-			} msa;
-			const char *fmt;
-
-			TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
-				if (kread((u_long)multiaddr, (char *)&ifma,
-					  sizeof ifma) != 0)
-					break;
-				multiaddr = &ifma;
-				if (kread((u_long)ifma.ifma_addr, (char *)&msa,
-					  sizeof msa) != 0)
-					break;
-				if (msa.sa.sa_family != sa->sa_family)
-					continue;
-
-				fmt = 0;
-				switch (msa.sa.sa_family) {
-				case AF_INET:
-					fmt = routename(msa.in.sin_addr.s_addr);
-					break;
-#ifdef INET6
-				case AF_INET6:
-					printf("%*s %-19.19s(refs: %d)\n",
-					       Wflag ? 27 : 25, "",
-					       inet_ntop(AF_INET6,
-							 &msa.in6.sin6_addr,
-							 ntop_buf,
-							 sizeof(ntop_buf)),
-					       ifma.ifma_refcount);
-					break;
-#endif /* INET6 */
-				case AF_LINK:
-					switch (msa.dl.sdl_type) {
-					case IFT_ETHER:
-					case IFT_FDDI:
-						fmt = ether_ntoa(
-							(struct ether_addr *)
-							LLADDR(&msa.dl));
-						break;
-					}
-					break;
-				}
-				if (fmt) {
-					printf("%*s %-17.17s",
-					    Wflag ? 27 : 25, "", fmt);
-					if (msa.sa.sa_family == AF_LINK) {
-						printf(" %8lu", imcasts);
-						printf("%*s",
-						    bflag ? 17 : 6, "");
-						printf(" %8lu", omcasts);
-					}
-					putchar('\n');
-				}
-			}
-		}
 	}
+out:
+	netstat_itl_free(itlp);
+	netstat_iti_free(itip);
 }
 
 struct	iftot {
-	SLIST_ENTRY(iftot) chain;
-	char	ift_name[IFNAMSIZ];	/* interface name */
 	u_long	ift_ip;			/* input packets */
 	u_long	ift_ie;			/* input errors */
 	u_long	ift_op;			/* output packets */
@@ -528,64 +355,32 @@
  * Repeat display every interval1 seconds, showing statistics
  * collected over that interval.  Assumes that interval1 is non-zero.
  * First line printed at top of screen is always cumulative.
- * XXX - should be rewritten to use ifmib(4).
  */
 static void
-sidewaysintpr(int interval1, u_long off)
+sidewaysintpr(int interval1, void *kvm_handle)
 {
-	struct ifnet ifnet;
-	u_long firstifnet;
-	struct ifnethead ifnethead;
+	kvm_t *kvm;
+	int it_flags;
+	
 	struct itimerval interval_it;
-	struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
-	int line;
+	struct iftot total, sum;
+	int line, error;
 	int oldmask, first;
-	u_long interesting_off;
+	char	ift_name[IFNAMSIZ];
 
-	if (kread(off, (char *)&ifnethead, sizeof ifnethead) != 0)
-		return;
-	firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
+	struct interface_type_list *itlp;
+	struct interface_type_iterator *itip;
 
-	if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
-		printf("malloc failed\n");
-		exit(1);
-	}
-	memset(iftot, 0, sizeof(struct iftot));
+	const struct face_type *fin, *fout;
+	const struct interface_type *itp;
 
-	interesting = NULL;
-	interesting_off = 0;
-	for (off = firstifnet, ip = iftot; off;) {
-		char name[IFNAMSIZ];
+	if (kvm_handle == NULL)
+		return;
 
-		if (kread(off, (char *)&ifnet, sizeof ifnet) != 0)
-			break;
-		strlcpy(name, ifnet.if_xname, sizeof(name));
-		if (interface && strcmp(name, interface) == 0) {
-			interesting = ip;
-			interesting_off = off;
-		}
-		snprintf(ip->ift_name, sizeof(ip->ift_name), "(%s)", name);;
-		if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
-			printf("malloc failed\n");
-			exit(1);
-		}
-		memset(ipn, 0, sizeof(struct iftot));
-		SLIST_NEXT(ip, chain) = ipn;
-		ip = ipn;
-		off = (u_long)TAILQ_NEXT(&ifnet, if_link);
-	}
-	if (interface && interesting == NULL)
-		errx(1, "%s: unknown interface", interface);
-	if ((total = malloc(sizeof(struct iftot))) == NULL) {
-		printf("malloc failed\n");
-		exit(1);
-	}
-	memset(total, 0, sizeof(struct iftot));
-	if ((sum = malloc(sizeof(struct iftot))) == NULL) {
-		printf("malloc failed\n");
-		exit(1);
-	}
-	memset(sum, 0, sizeof(struct iftot));
+	it_flags = 0;
+	it_flags |= NETSTAT_INTERFACE_KVM;
+	it_flags |= NETSTAT_INTERFACE_ALL;
+	kvm = (kvm_t *)kvm_handle;
 
 	(void)signal(SIGALRM, catchalarm);
 	signalled = NO;
@@ -594,9 +389,31 @@
 	interval_it.it_value = interval_it.it_interval;
 	setitimer(ITIMER_REAL, &interval_it, NULL);
 	first = 1;
+
+	itlp = netstat_itl_alloc();
+	if (itlp == NULL) {
+		warn("netstat_itl_alloc()");
+		return;
+	}
+	if (netstat_interface(interface, PF_UNSPEC, itlp, it_flags, kvm) < 0) {
+		error = netstat_itl_geterror(itlp);
+		if (error == NETSTAT_ERROR_KVM) {
+			warnx("netstat_interface: %s", kvm_geterr(kvm));
+		} else
+			warnx("netstat_interface: %s", netstat_strerror(error));
+		netstat_itl_free(itlp);
+		return;
+	}
+	if (interface != NULL && netstat_itl_length(itlp) == 0) {
+		netstat_itl_free(itlp);
+		errx(1, "%s: unknown interface", interface);
+	}
+
+	if (interface != NULL)
+		sprintf(ift_name, "(%s)", interface);
 banner:
 	printf("%17s %14s %16s", "input",
-	    interesting ? interesting->ift_name : "(Total)", "output");
+	    interface ? ift_name : "(Total)", "output");
 	putchar('\n');
 	printf("%10s %5s %10s %10s %5s %10s %5s",
 	    "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
@@ -606,73 +423,53 @@
 	fflush(stdout);
 	line = 0;
 loop:
-	if (interesting != NULL) {
-		ip = interesting;
-		if (kread(interesting_off, (char *)&ifnet, sizeof ifnet) != 0) {
-			printf("???\n");
-			exit(1);
-		};
-		if (!first) {
-			show_stat("lu", 10, ifnet.if_ipackets - ip->ift_ip, 1);
-			show_stat("lu", 5, ifnet.if_ierrors - ip->ift_ie, 1);
-			show_stat("lu", 10, ifnet.if_ibytes - ip->ift_ib, 1);
-			show_stat("lu", 10, ifnet.if_opackets - ip->ift_op, 1);
-			show_stat("lu", 5, ifnet.if_oerrors - ip->ift_oe, 1);
-			show_stat("lu", 10, ifnet.if_obytes - ip->ift_ob, 1);
-			show_stat("NRSlu", 5,
-			    ifnet.if_collisions - ip->ift_co, 1);
-			if (dflag)
-				show_stat("LSu", 5,
-				    ifnet.if_snd.ifq_drops - ip->ift_dr, 1);
-		}
-		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;
-	} else {
-		sum->ift_ip = 0;
-		sum->ift_ie = 0;
-		sum->ift_ib = 0;
-		sum->ift_op = 0;
-		sum->ift_oe = 0;
-		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) != 0) {
-				off = 0;
-				continue;
-			}
-			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);
-		}
-		if (!first) {
-			show_stat("lu", 10, sum->ift_ip - total->ift_ip, 1);
-			show_stat("lu", 5, sum->ift_ie - total->ift_ie, 1);
-			show_stat("lu", 10, sum->ift_ib - total->ift_ib, 1);
-			show_stat("lu", 10, sum->ift_op - total->ift_op, 1);
-			show_stat("lu", 5, sum->ift_oe - total->ift_oe, 1);
-			show_stat("lu", 10, sum->ift_ob - total->ift_ob, 1);
-			show_stat("NRSlu", 5, sum->ift_co - total->ift_co, 1);
-			if (dflag)
-				show_stat("LSu", 5,
-				    sum->ift_dr - total->ift_dr, 1);
-		}
-		*total = *sum;
+	itlp = netstat_itl_alloc();
+	if (itlp == NULL) {
+		warn("netstat_itl_alloc()");
+		return;
+	}
+	if (netstat_interface(interface, PF_UNSPEC, itlp, it_flags, kvm) < 0) {
+		error = netstat_itl_geterror(itlp);
+		if (error == NETSTAT_ERROR_KVM) {
+			warnx("netstat_interface: %s", kvm_geterr(kvm));
+		} else
+			warnx("netstat_interface: %s", netstat_strerror(error));
+		netstat_itl_free(itlp);
+		return;
+	}
+	if (netstat_iti_alloc(itlp, &itip) < 0) {
+		warn("netstat_iti_alloc()");
+		netstat_itl_free(itlp);
+		return;
+	}
+	bzero(&sum, sizeof(struct iftot));
+	for (itp = netstat_iti_first(itip); itp != NULL;
+	    itp = netstat_iti_next(itip)) {
+		fin  = netstat_it_get_in(itp);
+		fout = netstat_it_get_out(itp);
+		sum.ift_ip += netstat_ft_get_packets(fin);
+		sum.ift_ie += netstat_ft_get_errors(fin);
+		sum.ift_ib += netstat_ft_get_bytes(fin);
+		sum.ift_op += netstat_ft_get_packets(fout);
+		sum.ift_oe += netstat_ft_get_errors(fout);
+		sum.ift_ob += netstat_ft_get_bytes(fout);
+		sum.ift_co += netstat_it_get_collisions(itp);
+		sum.ift_dr += netstat_it_get_drops(itp);
+	}
+	if (!first) {
+		show_stat("lu", 10, sum.ift_ip - total.ift_ip, 1);
+		show_stat("lu", 5, sum.ift_ie - total.ift_ie, 1);
+		show_stat("lu", 10, sum.ift_ib - total.ift_ib, 1);
+		show_stat("lu", 10, sum.ift_op - total.ift_op, 1);
+		show_stat("lu", 5, sum.ift_oe - total.ift_oe, 1);
+		show_stat("lu", 10, sum.ift_ob - total.ift_ob, 1);
+		show_stat("NRSlu", 5, sum.ift_co - total.ift_co, 1);
+		if (dflag)
+			show_stat("LSu", 5,
+			    sum.ift_dr - total.ift_dr, 1);
 	}
+	total = sum;
+	netstat_itl_free(itlp);
 	if (!first)
 		putchar('\n');
 	fflush(stdout);

==== //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/main.c#15 (text+ko) ====

@@ -77,77 +77,75 @@
 
 
 static struct nlist nl[] = {
-#define	N_IFNET		0
-	{ .n_name = "_ifnet" },
-#define	N_RTSTAT	1
+#define	N_RTSTAT	0
 	{ .n_name = "_rtstat" },
-#define	N_RTREE		2
+#define	N_RTREE		1
 	{ .n_name = "_rt_tables"},
-#define	N_MRTSTAT	3
+#define	N_MRTSTAT	2
 	{ .n_name = "_mrtstat" },
-#define	N_MFCHASHTBL	4
+#define	N_MFCHASHTBL	3
 	{ .n_name = "_mfchashtbl" },
-#define	N_VIFTABLE	5
+#define	N_VIFTABLE	4
 	{ .n_name = "_viftable" },
-#define	N_IPX		6
+#define	N_IPX		5
 	{ .n_name = "_ipxpcb_list"},
-#define	N_IPXSTAT	7
+#define	N_IPXSTAT	6
 	{ .n_name = "_ipxstat"},
-#define	N_SPXSTAT	8
+#define	N_SPXSTAT	7
 	{ .n_name = "_spx_istat"},
-#define	N_DDPSTAT	9
+#define	N_DDPSTAT	8
 	{ .n_name = "_ddpstat"},
-#define	N_DDPCB		10
+#define	N_DDPCB		9
 	{ .n_name = "_ddpcb"},
-#define	N_NGSOCKS	11
+#define	N_NGSOCKS	10
 	{ .n_name = "_ngsocklist"},
-#define	N_IP6STAT	12
+#define	N_IP6STAT	11
 	{ .n_name = "_ip6stat" },
-#define	N_ICMP6STAT	13
+#define	N_ICMP6STAT	12
 	{ .n_name = "_icmp6stat" },
-#define	N_IPSECSTAT	14
+#define	N_IPSECSTAT	13
 	{ .n_name = "_ipsec4stat" },
-#define	N_IPSEC6STAT	15
+#define	N_IPSEC6STAT	14
 	{ .n_name = "_ipsec6stat" },
-#define	N_PIM6STAT	16
+#define	N_PIM6STAT	15
 	{ .n_name = "_pim6stat" },
-#define	N_MRT6STAT	17
+#define	N_MRT6STAT	16
 	{ .n_name = "_mrt6stat" },
-#define	N_MF6CTABLE	18
+#define	N_MF6CTABLE	17
 	{ .n_name = "_mf6ctable" },
-#define	N_MIF6TABLE	19
+#define	N_MIF6TABLE	18
 	{ .n_name = "_mif6table" },
-#define	N_PFKEYSTAT	20
+#define	N_PFKEYSTAT	19
 	{ .n_name = "_pfkeystat" },
-#define	N_RTTRASH	21
+#define	N_RTTRASH	20
 	{ .n_name = "_rttrash" },
-#define	N_CARPSTAT	22
+#define	N_CARPSTAT	21
 	{ .n_name = "_carpstats" },
-#define	N_PFSYNCSTAT	23
+#define	N_PFSYNCSTAT	22
 	{ .n_name = "_pfsyncstats" },
-#define	N_AHSTAT	24
+#define	N_AHSTAT	23
 	{ .n_name = "_ahstat" },
-#define	N_ESPSTAT	25
+#define	N_ESPSTAT	24
 	{ .n_name = "_espstat" },
-#define	N_IPCOMPSTAT	26
+#define	N_IPCOMPSTAT	25
 	{ .n_name = "_ipcompstat" },
-#define	N_TCPSTAT	27
+#define	N_TCPSTAT	26
 	{ .n_name = "_tcpstat" },
-#define	N_UDPSTAT	28
+#define	N_UDPSTAT	27
 	{ .n_name = "_udpstat" },
-#define	N_IPSTAT	29
+#define	N_IPSTAT	28
 	{ .n_name = "_ipstat" },
-#define	N_ICMPSTAT	30
+#define	N_ICMPSTAT	29
 	{ .n_name = "_icmpstat" },
-#define	N_IGMPSTAT	31
+#define	N_IGMPSTAT	30
 	{ .n_name = "_igmpstat" },
-#define	N_PIMSTAT	32
+#define	N_PIMSTAT	31
 	{ .n_name = "_pimstat" },
-#define	N_RIP6STAT	33
+#define	N_RIP6STAT	32
 	{ .n_name = "_rip6stat" },
-#define	N_SCTPSTAT	34
+#define	N_SCTPSTAT	33
 	{ .n_name = "_sctpstat" },
-#define	N_MFCTABLESIZE	35
+#define	N_MFCTABLESIZE	34
 	{ .n_name = "_mfctablesize" },
 	{ .n_name = NULL },
 };
@@ -501,7 +499,7 @@
 #endif
 	if (iflag && !sflag) {
 		kread(0, NULL, 0);
-		intpr(interval, nl[N_IFNET].n_value, NULL);
+		intpr(interval, kvmd, NULL);
 		exit(0);
 	}
 	if (rflag) {
@@ -640,8 +638,7 @@
 	if (sflag) {
 		if (iflag) {
 			if (tp->pr_istats)
-				intpr(interval, nl[N_IFNET].n_value,
-				      tp->pr_istats);
+				intpr(interval, kvmd, tp->pr_istats);
 			else if (pflag)
 				printf("%s: no per-interface stats routine\n",
 				    tp->pr_name);


More information about the p4-projects mailing list