xref: /freebsd/usr.bin/netstat/mbuf.c (revision 13ae2e2d75f0f570babd3048b09fb223156fcced)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1983, 1988, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
359b50d902SRodney W. Grimes static char sccsid[] = "@(#)mbuf.c	8.1 (Berkeley) 6/6/93";
369b50d902SRodney W. Grimes #endif /* not lint */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #include <sys/param.h>
3913ae2e2dSGarrett Wollman #include <sys/mbuf.h>
409b50d902SRodney W. Grimes #include <sys/protosw.h>
419b50d902SRodney W. Grimes #include <sys/socket.h>
4213ae2e2dSGarrett Wollman #include <sys/sysctl.h>
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #include <stdio.h>
459b50d902SRodney W. Grimes #include "netstat.h"
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #define	YES	1
489b50d902SRodney W. Grimes typedef int bool;
499b50d902SRodney W. Grimes 
509b50d902SRodney W. Grimes struct	mbstat mbstat;
519b50d902SRodney W. Grimes 
529b50d902SRodney W. Grimes static struct mbtypes {
539b50d902SRodney W. Grimes 	int	mt_type;
549b50d902SRodney W. Grimes 	char	*mt_name;
559b50d902SRodney W. Grimes } mbtypes[] = {
569b50d902SRodney W. Grimes 	{ MT_DATA,	"data" },
579b50d902SRodney W. Grimes 	{ MT_OOBDATA,	"oob data" },
589b50d902SRodney W. Grimes 	{ MT_CONTROL,	"ancillary data" },
599b50d902SRodney W. Grimes 	{ MT_HEADER,	"packet headers" },
609b50d902SRodney W. Grimes 	{ MT_SOCKET,	"socket structures" },			/* XXX */
619b50d902SRodney W. Grimes 	{ MT_PCB,	"protocol control blocks" },		/* XXX */
629b50d902SRodney W. Grimes 	{ MT_RTABLE,	"routing table entries" },		/* XXX */
639b50d902SRodney W. Grimes 	{ MT_HTABLE,	"IMP host table entries" },		/* XXX */
649b50d902SRodney W. Grimes 	{ MT_ATABLE,	"address resolution tables" },
659b50d902SRodney W. Grimes 	{ MT_FTABLE,	"fragment reassembly queue headers" },	/* XXX */
669b50d902SRodney W. Grimes 	{ MT_SONAME,	"socket names and addresses" },
679b50d902SRodney W. Grimes 	{ MT_SOOPTS,	"socket options" },
689b50d902SRodney W. Grimes 	{ MT_RIGHTS,	"access rights" },
699b50d902SRodney W. Grimes 	{ MT_IFADDR,	"interface addresses" },		/* XXX */
709b50d902SRodney W. Grimes 	{ 0, 0 }
719b50d902SRodney W. Grimes };
729b50d902SRodney W. Grimes 
739b50d902SRodney W. Grimes int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
749b50d902SRodney W. Grimes bool seen[256];			/* "have we seen this type yet?" */
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes /*
779b50d902SRodney W. Grimes  * Print mbuf statistics.
789b50d902SRodney W. Grimes  */
799b50d902SRodney W. Grimes void
809b50d902SRodney W. Grimes mbpr(mbaddr)
819b50d902SRodney W. Grimes 	u_long mbaddr;
829b50d902SRodney W. Grimes {
839b50d902SRodney W. Grimes 	register int totmem, totfree, totmbufs;
849b50d902SRodney W. Grimes 	register int i;
859b50d902SRodney W. Grimes 	register struct mbtypes *mp;
8613ae2e2dSGarrett Wollman 	int name[3];
8713ae2e2dSGarrett Wollman 	size_t mbstatlen;
8813ae2e2dSGarrett Wollman 
8913ae2e2dSGarrett Wollman 	name[0] = CTL_KERN;
9013ae2e2dSGarrett Wollman 	name[1] = KERN_IPC;
9113ae2e2dSGarrett Wollman 	name[2] = KIPC_MBSTAT;
9213ae2e2dSGarrett Wollman 	mbstatlen = sizeof mbstat;
9313ae2e2dSGarrett Wollman 
9413ae2e2dSGarrett Wollman 	if (sysctl(name, 3, &mbstat, &mbstatlen, 0, 0) < 0) {
9513ae2e2dSGarrett Wollman 		warn("sysctl: retrieving mbstat");
9613ae2e2dSGarrett Wollman 		return;
9713ae2e2dSGarrett Wollman 	}
9813ae2e2dSGarrett Wollman #undef MSIZE
9913ae2e2dSGarrett Wollman #define MSIZE		(mbstat.m_msize)
10013ae2e2dSGarrett Wollman #undef MCLBYTES
10113ae2e2dSGarrett Wollman #define	MCLBYTES	(mbstat.m_mclbytes)
1029b50d902SRodney W. Grimes 
1039b50d902SRodney W. Grimes 	if (nmbtypes != 256) {
10451e7d42cSGarrett Wollman 		warnx("unexpected change to mbstat; check source");
1059b50d902SRodney W. Grimes 		return;
1069b50d902SRodney W. Grimes 	}
10713ae2e2dSGarrett Wollman 
1089b50d902SRodney W. Grimes 	totmbufs = 0;
1099b50d902SRodney W. Grimes 	for (mp = mbtypes; mp->mt_name; mp++)
1109b50d902SRodney W. Grimes 		totmbufs += mbstat.m_mtypes[mp->mt_type];
1112fde9bd6SGarrett Wollman 	printf("%u/%u mbufs in use:\n", totmbufs, mbstat.m_mbufs);
1129b50d902SRodney W. Grimes 	for (mp = mbtypes; mp->mt_name; mp++)
1139b50d902SRodney W. Grimes 		if (mbstat.m_mtypes[mp->mt_type]) {
1149b50d902SRodney W. Grimes 			seen[mp->mt_type] = YES;
1159b50d902SRodney W. Grimes 			printf("\t%u mbufs allocated to %s\n",
1169b50d902SRodney W. Grimes 			    mbstat.m_mtypes[mp->mt_type], mp->mt_name);
1179b50d902SRodney W. Grimes 		}
1189b50d902SRodney W. Grimes 	seen[MT_FREE] = YES;
1199b50d902SRodney W. Grimes 	for (i = 0; i < nmbtypes; i++)
1209b50d902SRodney W. Grimes 		if (!seen[i] && mbstat.m_mtypes[i]) {
1219b50d902SRodney W. Grimes 			printf("\t%u mbufs allocated to <mbuf type %d>\n",
1229b50d902SRodney W. Grimes 			    mbstat.m_mtypes[i], i);
1239b50d902SRodney W. Grimes 		}
1247d56c0eeSAlexander Langer 	printf("%lu/%lu mbuf clusters in use\n",
1259b50d902SRodney W. Grimes 		mbstat.m_clusters - mbstat.m_clfree, mbstat.m_clusters);
1262fde9bd6SGarrett Wollman 	totmem = mbstat.m_mbufs * MSIZE + mbstat.m_clusters * MCLBYTES;
1272fde9bd6SGarrett Wollman 	totfree = mbstat.m_clfree * MCLBYTES +
1282fde9bd6SGarrett Wollman 		MSIZE * (mbstat.m_mbufs - totmbufs);
1299b50d902SRodney W. Grimes 	printf("%u Kbytes allocated to network (%d%% in use)\n",
1309b50d902SRodney W. Grimes 		totmem / 1024, (totmem - totfree) * 100 / totmem);
1317d56c0eeSAlexander Langer 	printf("%lu requests for memory denied\n", mbstat.m_drops);
1327d56c0eeSAlexander Langer 	printf("%lu requests for memory delayed\n", mbstat.m_wait);
1337d56c0eeSAlexander Langer 	printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
1349b50d902SRodney W. Grimes }
135