PERFORCE change 166637 for review

Gabor Pali pgj at FreeBSD.org
Mon Jul 27 17:23:43 UTC 2009


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

Change 166637 by pgj at petymeg-current on 2009/07/27 17:22:41

	Simplify calling of statistics routines.

Affected files ...

.. //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/extern.h#13 edit
.. //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/inet.c#28 edit
.. //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/main.c#21 edit

Differences ...

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

@@ -74,8 +74,8 @@
 void	inetpr(void *, int, int);
 void	inetppr(const struct socket_type *);
 void	unixdomainpr(const struct socket_type *);
-void	tcp_stats(void *, const char *);
-void	udp_stats(void *, const char *);
+void	tcp_stats(const struct stat_type *);
+void	udp_stats(const struct stat_type *);
 #ifdef SCTP
 void	sctp_protopr(u_long, const char *, int, int);
 void	sctp_stats(u_long, const char *, int, int);
@@ -84,7 +84,7 @@
 void	icmp_stats(u_long, const char *, int, int);
 void	igmp_stats(u_long, const char *, int, int);
 void	pim_stats(u_long, const char *, int, int);
-void	carp_stats(void *, const char *);
+void	carp_stats(const struct stat_type *);
 void	pfsync_stats(u_long, const char *, int, int);
 #ifdef IPSEC
 void	ipsec_stats(u_long, const char *, int, int);

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

@@ -242,11 +242,9 @@
  * Dump TCP statistics structure.
  */
 void
-tcp_stats(void *kvmd, const char *name)
+tcp_stats(const struct stat_type *sttp)
 {
 	const struct tcp_stat *s;
-	struct stat_type *sttp;
-	int stt_flags, error;
 
 #ifdef INET6
 	if (tcp_done != 0)
@@ -254,35 +252,7 @@
 	else
 		tcp_done = 1;
 #endif
-
-	if (zflag) {
-		netstat_zerostat(stat_TCP);
-		return;
-	}
-
-	stt_flags = 0;
-	if (!live)
-		stt_flags |= NETSTAT_STAT_KVM;
-
-	sttp = netstat_stat_alloc();
-	if (sttp == NULL) {
-		warn("netstat_stat_alloc");
-		return;
-	}
-
-	if (netstat_stat(stat_TCP, sttp, stt_flags, kvmd) < 0) {
-		error = netstat_stat_geterror(sttp);
-		if (error == NETSTAT_ERROR_KVM)
-			warnx("netstat_stat: %s", kvm_geterr(kvmd));
-		else
-			warnx("netstat_stat: %s", netstat_strerror(error));
-		goto out;
-	}
-
 	s = netstat_get_tcpstats(sttp);
-
-	printf ("%s:\n", name);
-
 #define	p(f, m) if (netstat_tcps_get_##f(s) || sflag <= 1) \
     printf(m, netstat_tcps_get_##f(s), plural(netstat_tcps_get_##f(s)))
 #define	p1a(f, m) if (netstat_tcps_get_##f(s) || sflag <= 1) \
@@ -391,9 +361,6 @@
 	p(ecn_ect1, "\t%ju packet%s with ECN ECT(1) bit set\n");
 	p(ecn_shs, "\t%ju successful ECN handshake%s\n");
 	p(ecn_rcwnd, "\t%ju time%s ECN reduced the congestion window\n");
-
-out:
-	netstat_stat_free(sttp);
 #undef p
 #undef p1a
 #undef p2
@@ -405,11 +372,9 @@
  * Dump UDP statistics structure.
  */
 void
-udp_stats(void *kvmd, const char *name)
+udp_stats(const struct stat_type *sttp)
 {
 	const struct udp_stat *s;
-	struct stat_type *sttp;
-	int stt_flags, error;
 
 #ifdef INET6
 	if (udp_done != 0)
@@ -417,34 +382,7 @@
 	else
 		udp_done = 1;
 #endif
-
-	if (zflag) {
-		netstat_zerostat(stat_UDP);
-		return;
-	}
-
-	stt_flags = 0;
-	if (!live)
-		stt_flags |= NETSTAT_STAT_KVM;
-
-	sttp = netstat_stat_alloc();
-	if (sttp == NULL) {
-		warn("netstat_stat_alloc");
-		return;
-	}
-
-	if (netstat_stat(stat_UDP, sttp, stt_flags, kvmd) < 0) {
-		error = netstat_stat_geterror(sttp);
-		if (error == NETSTAT_ERROR_KVM)
-			warnx("netstat_stat: %s", kvm_geterr(kvmd));
-		else
-			warnx("netstat_stat: %s", netstat_strerror(error));
-		goto out;
-	}
-
 	s = netstat_get_udpstats(sttp);
-
-	printf("%s:\n", name);
 #define	p(f, m) if (netstat_udps_get_##f(s) || sflag <= 1) \
     printf(m, netstat_udps_get_##f(s), plural(netstat_udps_get_##f(s)))
 #define	p1a(f, m) if (netstat_udps_get_##f(s) || sflag <= 1) \
@@ -464,8 +402,6 @@
 	/* the next statistic is cumulative in udps_noportbcast */
 	p(filtermcast,
 	    "\t%ju time%s multicast source filter matched\n");
-out:
-	netstat_stat_free(sttp);
 #undef p
 #undef p1a
 }
@@ -474,45 +410,15 @@
  * Dump CARP statistics structure.
  */
 void
-carp_stats(void *kvmd, const char *name)
+carp_stats(const struct stat_type *sttp)
 {
 	const struct carp_stat *s;
-	struct stat_type *sttp;
-	int stt_flags, error;
-
-	if (zflag) {
-		netstat_zerostat(stat_CARP);
-		return;
-	}
 
-	stt_flags = 0;
-	if (!live)
-		stt_flags |= NETSTAT_STAT_KVM;
-
-	sttp = netstat_stat_alloc();
-	if (sttp == NULL) {
-		warn("netstat_stat_alloc");
-		return;
-	}
-
-	if (netstat_stat(stat_CARP, sttp, stt_flags, kvmd) < 0) {
-		error = netstat_stat_geterror(sttp);
-		if (error == NETSTAT_ERROR_KVM)
-			warnx("netstat_stat: %s", kvm_geterr(kvmd));
-		else
-			warnx("netstat_stat: %s", netstat_strerror(error));
-		goto out;
-	}
-
 	s = netstat_get_carpstats(sttp);
-
-	printf("%s:\n", name);
-
 #define	p(f, m) if (netstat_carps_get_##f(s) || sflag <= 1) \
 	printf(m, netstat_carps_get_##f(s), plural(netstat_carps_get_##f(s)))
 #define	p2(f, m) if (netstat_carps_get_##f(s) || sflag <= 1) \
 	printf(m, netstat_carps_get_##f(s))
-
 	p(ipackets, "\t%ju packet%s received (IPv4)\n");
 	p(ipackets6, "\t%ju packet%s received (IPv6)\n");
 	p(badttl, "\t\t%ju packet%s discarded for wrong TTL\n");
@@ -529,8 +435,6 @@
 #if notyet
 	p(ostates, "\t\t%s state update%s sent\n");
 #endif
-out:
-	netstat_stat_free(sttp);
 #undef p
 #undef p2
 }

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

@@ -150,6 +150,8 @@
 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
 	void	(*pr_cblocks)(u_long, const char *, int, int);
 					/* control blocks printing routine */
+	void	(*pr_nstats)(const struct stat_type *);	
+	enum stat pr_stx;
 	void	(*pr_stats)(u_long, const char *, int, int);
 					/* statistics printing routine */
 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
@@ -157,99 +159,99 @@
 	int	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
 	int	pr_protocol;
 } protox[] = {
-	{ 0,		0,		1,	NULL,
+	{ 0,		0,		1,	NULL,	tcp_stats, stat_TCP,
 	  NULL,		NULL,		"tcp",	1,	IPPROTO_TCP },
-	{ 0,		0,		1,	NULL,
+	{ 0,		0,		1,	NULL,	udp_stats, stat_UDP,
 	  NULL,		NULL,		"udp",	1,	IPPROTO_UDP },
 #ifdef SCTP
-	{ -1,		N_SCTPSTAT,	1,	sctp_protopr,
+	{ -1,		N_SCTPSTAT,	1,	sctp_protopr, NULL, stat_MAX,
 	  sctp_stats,	NULL,		"sctp",	1,	IPPROTO_SCTP },
 #endif
-	{ 0,		-1,		1,	NULL,
+	{ 0,		-1,		1,	NULL,	NULL, stat_MAX,
 	  NULL,		NULL,		"divert", 1,	IPPROTO_DIVERT },
-	{ 0,		N_IPSTAT,	1,	NULL,
+	{ 0,		N_IPSTAT,	1,	NULL,	NULL, stat_MAX,
 	  ip_stats,	NULL,		"ip",	1,	IPPROTO_RAW },
-	{ 0,		N_ICMPSTAT,	1,	NULL,
+	{ 0,		N_ICMPSTAT,	1,	NULL,	NULL, stat_MAX,
 	  icmp_stats,	NULL,		"icmp",	1,	IPPROTO_ICMP },
-	{ 0,		N_IGMPSTAT,	1,	NULL,
+	{ 0,		N_IGMPSTAT,	1,	NULL,	NULL, stat_MAX,
 	  igmp_stats,	NULL,		"igmp",	1,	IPPROTO_IGMP },
 #ifdef IPSEC
-	{ -1,		N_IPSECSTAT,	1,	NULL,	/* keep as compat */
+	{ -1,		N_IPSECSTAT,	1,	NULL,	NULL, stat_MAX, /* keep as compat */
 	  ipsec_stats,	NULL,		"ipsec", 0,	0},
-	{ -1,		N_AHSTAT,	1,	NULL,
+	{ -1,		N_AHSTAT,	1,	NULL,	NULL, stat_MAX,
 	  ah_stats,	NULL,		"ah",	0,	0},
-	{ -1,		N_ESPSTAT,	1,	NULL,
+	{ -1,		N_ESPSTAT,	1,	NULL,	NULL, stat_MAX,
 	  esp_stats,	NULL,		"esp",	0,	0},
-	{ -1,		N_IPCOMPSTAT,	1,	NULL,
+	{ -1,		N_IPCOMPSTAT,	1,	NULL,	NULL, stat_MAX,
 	  ipcomp_stats,	NULL,		"ipcomp", 0,	0},
 #endif
-	{ 0,		N_PIMSTAT,	1,	NULL,
+	{ 0,		N_PIMSTAT,	1,	NULL,	NULL, stat_MAX,
 	  pim_stats,	NULL,		"pim",	1,	IPPROTO_PIM },
-	{ -1,		0,		1,	NULL,
+	{ -1,		0,		1,	NULL,	carp_stats, stat_CARP,
 	  NULL,		NULL,		"carp",	1,	0 },
-	{ -1,		N_PFSYNCSTAT,	1,	NULL,
+	{ -1,		N_PFSYNCSTAT,	1,	NULL,	NULL, stat_MAX,
 	  pfsync_stats,	NULL,		"pfsync", 1,	0 },
-	{ -1,		-1,		0,	NULL,
+	{ -1,		-1,		0,	NULL,	NULL, stat_MAX,
 	  NULL,		NULL,		NULL,	0,	0 }
 };
 
 #ifdef INET6
 struct protox ip6protox[] = {
-	{ 0,		0,	    	1,	NULL,
+	{ 0,		0,	    	1,	NULL,	tcp_stats, stat_TCP,
 	  NULL,		NULL,		"tcp",	1,	IPPROTO_TCP },
-	{ 0,		0,		1,	NULL,
+	{ 0,		0,		1,	NULL,	udp_stats, stat_UDP,
 	  NULL,		NULL,		"udp",	1,	IPPROTO_UDP },
-	{ 0,		N_IP6STAT,	1,	NULL,
+	{ 0,		N_IP6STAT,	1,	NULL,	NULL, stat_MAX,
 	  ip6_stats,	ip6_ifstats,	"ip6",	1,	IPPROTO_RAW },
-	{ 0,		N_ICMP6STAT,	1,	NULL,
+	{ 0,		N_ICMP6STAT,	1,	NULL,	NULL, stat_MAX,
 	  icmp6_stats,	icmp6_ifstats,	"icmp6", 1,	IPPROTO_ICMPV6 },
 #ifdef IPSEC
-	{ -1,		N_IPSEC6STAT,	1,	NULL,
+	{ -1,		N_IPSEC6STAT,	1,	NULL,	NULL, stat_MAX,
 	  ipsec_stats,	NULL,		"ipsec6", 0,	0 },
 #endif
 #ifdef notyet
-	{ -1,		N_PIM6STAT,	1,	NULL,
+	{ -1,		N_PIM6STAT,	1,	NULL,	NULL, stat_MAX,
 	  pim6_stats,	NULL,		"pim6",	1,	0 },
 #endif
-	{ -1,		N_RIP6STAT,	1,	NULL,
+	{ -1,		N_RIP6STAT,	1,	NULL,	NULL, stat_MAX,
 	  rip6_stats,	NULL,		"rip6",	1,	0 },
-	{ -1,		-1,		0,	NULL,
+	{ -1,		-1,		0,	NULL,	NULL, stat_MAX,
 	  NULL,		NULL,		NULL,	0,	0 }
 };
 #endif /*INET6*/
 
 #ifdef IPSEC
 struct protox pfkeyprotox[] = {
-	{ -1,		N_PFKEYSTAT,	1,	NULL,
+	{ -1,		N_PFKEYSTAT,	1,	NULL,	NULL, stat_MAX,
 	  pfkey_stats,	NULL,		"pfkey", 0,	0 },
-	{ -1,		-1,		0,	NULL,
+	{ -1,		-1,		0,	NULL,	NULL, stat_MAX,
 	  NULL,		NULL,		NULL,	0,	0 }
 };
 #endif
 
 struct protox atalkprotox[] = {
-	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
+	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr, NULL, stat_MAX,
 	  ddp_stats,	NULL,		"ddp",	0,	0 },
-	{ -1,		-1,		0,	NULL,
+	{ -1,		-1,		0,	NULL,	NULL, stat_MAX,
 	  NULL,		NULL,		NULL,	0,	0 }
 };
 #ifdef NETGRAPH
 struct protox netgraphprotox[] = {
-	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
+	{ N_NGSOCKS,	-1,		1,	netgraphprotopr, NULL, stat_MAX,
 	  NULL,		NULL,		"ctrl",	0,	0 },
-	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
+	{ N_NGSOCKS,	-1,		1,	netgraphprotopr, NULL, stat_MAX,
 	  NULL,		NULL,		"data",	0,	0 },
-	{ -1,		-1,		0,	NULL,
+	{ -1,		-1,		0,	NULL,	NULL, stat_MAX,
 	  NULL,		NULL,		NULL,	0,	0 }
 };
 #endif
 #ifdef IPX
 struct protox ipxprotox[] = {
-	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
+	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr, NULL, stat_MAX,
 	  ipx_stats,	NULL,		"ipx",	0,	0 },
-	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
+	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr, NULL, stat_MAX,
 	  spx_stats,	NULL,		"spx",	0,	0 },
-	{ -1,		-1,		0,	NULL,
+	{ -1,		-1,		0,	NULL,	NULL, stat_MAX,
 	  NULL,		NULL,		0,	0,	0 }
 };
 #endif
@@ -268,6 +270,7 @@
 					 atalkprotox, NULL };
 
 static void connpr(const struct socket_type *);
+static void statpr(void (*)(const struct stat_type *), enum stat, const char *);
 static void printproto(struct protox *, const char *);
 static void usage(void);
 static struct protox *name2protox(const char *);
@@ -649,12 +652,8 @@
 			return;
 		} else {
 			/* XXX: temp. hack */
-			if (strcmp(tp->pr_name, "udp") == 0)
-				tcp_stats(kvmd, tp->pr_name);
-			else if (strcmp(tp->pr_name, "tcp") == 0)
-				udp_stats(kvmd, tp->pr_name);
-			else if (strcmp(tp->pr_name, "carp") == 0)
-				carp_stats(kvmd, tp->pr_name);
+			if (tp->pr_nstats != NULL)
+				statpr(tp->pr_nstats, tp->pr_stx, tp->pr_name);
 			else {
 				pr = tp->pr_stats;
 				if (!pr) {
@@ -719,6 +718,43 @@
 	}
 }
 
+void
+statpr(void (*stats_routine)(const struct stat_type *), enum stat sts,
+    const char *name)
+{
+	struct stat_type *sttp;
+	int stt_flags, error;
+
+	if (zflag) {
+		netstat_zerostat(sts);
+		return;
+	}
+
+	stt_flags = 0;
+	if (!live)
+		stt_flags |= NETSTAT_STAT_KVM;
+
+	sttp = netstat_stat_alloc();
+	if (sttp == NULL) {
+		warn("netstat_stat_alloc");
+		return;
+	}
+
+	if (netstat_stat(sts, sttp, stt_flags, kvmd) < 0) {
+		error = netstat_stat_geterror(sttp);
+		if (error == NETSTAT_ERROR_KVM)
+			warnx("netstat_stat: %s", kvm_geterr(kvmd));
+		else
+			warnx("netstat_stat: %s", netstat_strerror(error));
+		goto out;
+	}
+
+	printf("%s:\n", name);
+	stats_routine(sttp);
+out:
+	netstat_stat_free(sttp);
+}
+
 /*
  * Read kernel memory, return 0 on success.
  */


More information about the p4-projects mailing list