xref: /dragonfly/usr.bin/netstat/mbuf.c (revision b40e316c)
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)mbuf.c	8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.bin/netstat/mbuf.c,v 1.17.2.3 2001/08/10 09:07:09 ru Exp $
35  * $DragonFly: src/usr.bin/netstat/mbuf.c,v 1.2 2003/06/17 04:29:30 dillon Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/mbuf.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
43 
44 #include <err.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include "netstat.h"
48 
49 #define	YES	1
50 typedef int bool;
51 
52 static struct mbtypenames {
53 	int	mt_type;
54 	char	*mt_name;
55 } mbtypenames[] = {
56 	{ MT_DATA,	"data" },
57 	{ MT_OOBDATA,	"oob data" },
58 	{ MT_CONTROL,	"ancillary data" },
59 	{ MT_HEADER,	"packet headers" },
60 #ifdef MT_SOCKET
61 	{ MT_SOCKET,	"socket structures" },			/* XXX */
62 #endif
63 #ifdef MT_PCB
64 	{ MT_PCB,	"protocol control blocks" },		/* XXX */
65 #endif
66 #ifdef MT_RTABLE
67 	{ MT_RTABLE,	"routing table entries" },		/* XXX */
68 #endif
69 #ifdef MT_HTABLE
70 	{ MT_HTABLE,	"IMP host table entries" },		/* XXX */
71 #endif
72 #ifdef MT_ATABLE
73 	{ MT_ATABLE,	"address resolution tables" },
74 #endif
75 	{ MT_FTABLE,	"fragment reassembly queue headers" },	/* XXX */
76 	{ MT_SONAME,	"socket names and addresses" },
77 #ifdef MT_SOOPTS
78 	{ MT_SOOPTS,	"socket options" },
79 #endif
80 #ifdef MT_RIGHTS
81 	{ MT_RIGHTS,	"access rights" },
82 #endif
83 #ifdef MT_IFADDR
84 	{ MT_IFADDR,	"interface addresses" },		/* XXX */
85 #endif
86 	{ 0, 0 }
87 };
88 
89 /*
90  * Print mbuf statistics.
91  */
92 void
93 mbpr(u_long mbaddr, u_long mbtaddr, u_long nmbcaddr, u_long nmbufaddr)
94 {
95 	u_long totmem, totpossible, totmbufs;
96 	register int i;
97 	struct mbstat mbstat;
98 	struct mbtypenames *mp;
99 	int name[3], nmbclusters, nmbufs, nmbtypes;
100 	size_t nmbclen, nmbuflen, mbstatlen, mbtypeslen;
101 	u_long *mbtypes;
102 	bool *seen;	/* "have we seen this type yet?" */
103 
104 	mbtypes = NULL;
105 	seen = NULL;
106 
107 	/*
108 	 * XXX
109 	 * We can't kread() mbtypeslen from a core image so we'll
110 	 * bogusly assume it's the same as in the running kernel.
111 	 */
112 	if (sysctlbyname("kern.ipc.mbtypes", NULL, &mbtypeslen, NULL, 0) < 0) {
113 		warn("sysctl: retrieving mbtypes length");
114 		goto err;
115 	}
116 	if ((mbtypes = malloc(mbtypeslen)) == NULL) {
117 		warn("malloc: %lu bytes for mbtypes", (u_long)mbtypeslen);
118 		goto err;
119 	}
120 
121 	nmbtypes = mbtypeslen / sizeof(*mbtypes);
122 	if ((seen = calloc(nmbtypes, sizeof(*seen))) == NULL) {
123 		warn("calloc");
124 		goto err;
125 	}
126 
127 	if (mbaddr) {
128 		if (kread(mbaddr, (char *)&mbstat, sizeof mbstat))
129 			goto err;
130 		if (kread(mbtaddr, (char *)mbtypes, mbtypeslen))
131 			goto err;
132 		if (kread(nmbcaddr, (char *)&nmbclusters, sizeof(int)))
133 			goto err;
134 		if (kread(nmbufaddr, (char *)&nmbufs, sizeof(int)))
135 			goto err;
136 	} else {
137 		name[0] = CTL_KERN;
138 		name[1] = KERN_IPC;
139 		name[2] = KIPC_MBSTAT;
140 		mbstatlen = sizeof mbstat;
141 		if (sysctl(name, 3, &mbstat, &mbstatlen, 0, 0) < 0) {
142 			warn("sysctl: retrieving mbstat");
143 			goto err;
144 		}
145 
146 		if (sysctlbyname("kern.ipc.mbtypes", mbtypes, &mbtypeslen, NULL,
147 		    0) < 0) {
148 			warn("sysctl: retrieving mbtypes");
149 			goto err;
150 		}
151 
152 		name[2] = KIPC_NMBCLUSTERS;
153 		nmbclen = sizeof(int);
154 		if (sysctl(name, 3, &nmbclusters, &nmbclen, 0, 0) < 0) {
155 			warn("sysctl: retrieving nmbclusters");
156 			goto err;
157 		}
158 
159 		nmbuflen = sizeof(int);
160 		if (sysctlbyname("kern.ipc.nmbufs", &nmbufs, &nmbuflen, 0, 0) < 0) {
161 			warn("sysctl: retrieving nmbufs");
162 			goto err;
163 		}
164 	}
165 
166 #undef MSIZE
167 #define MSIZE		(mbstat.m_msize)
168 #undef MCLBYTES
169 #define	MCLBYTES	(mbstat.m_mclbytes)
170 
171 	totmbufs = 0;
172 	for (mp = mbtypenames; mp->mt_name; mp++)
173 		totmbufs += mbtypes[mp->mt_type];
174 	printf("%lu/%lu/%u mbufs in use (current/peak/max):\n", totmbufs,
175 	    mbstat.m_mbufs, nmbufs);
176 	for (mp = mbtypenames; mp->mt_name; mp++)
177 		if (mbtypes[mp->mt_type]) {
178 			seen[mp->mt_type] = YES;
179 			printf("\t%lu mbufs allocated to %s\n",
180 			    mbtypes[mp->mt_type], mp->mt_name);
181 		}
182 	seen[MT_FREE] = YES;
183 	for (i = 0; i < nmbtypes; i++)
184 		if (!seen[i] && mbtypes[i]) {
185 			printf("\t%lu mbufs allocated to <mbuf type %d>\n",
186 			    mbtypes[i], i);
187 		}
188 	printf("%lu/%lu/%u mbuf clusters in use (current/peak/max)\n",
189 		mbstat.m_clusters - mbstat.m_clfree, mbstat.m_clusters,
190 		nmbclusters);
191 	totmem = mbstat.m_mbufs * MSIZE + mbstat.m_clusters * MCLBYTES;
192 	totpossible = nmbclusters * MCLBYTES + MSIZE * nmbufs;
193 	printf("%lu Kbytes allocated to network (%lu%% of mb_map in use)\n",
194 		totmem / 1024, (totmem * 100) / totpossible);
195 	printf("%lu requests for memory denied\n", mbstat.m_drops);
196 	printf("%lu requests for memory delayed\n", mbstat.m_wait);
197 	printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
198 
199 err:
200 	if (mbtypes != NULL)
201 		free(mbtypes);
202 	if (seen != NULL)
203 		free(seen);
204 }
205