xref: /netbsd-src/usr.bin/netstat/mbuf.c (revision a536ee5124e62c9a0051a252f7833dc8f50f44c9)
1 /*	$NetBSD: mbuf.c,v 1.31 2012/03/20 20:34:58 matt Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "from: @(#)mbuf.c	8.1 (Berkeley) 6/6/93";
36 #else
37 __RCSID("$NetBSD: mbuf.c,v 1.31 2012/03/20 20:34:58 matt Exp $");
38 #endif
39 #endif /* not lint */
40 
41 #define	__POOL_EXPOSE
42 
43 #include <sys/param.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/mbuf.h>
47 #include <sys/pool.h>
48 #include <sys/sysctl.h>
49 
50 #include <stdio.h>
51 #include <kvm.h>
52 #include <stdlib.h>
53 #include <limits.h>
54 #include <errno.h>
55 #include <err.h>
56 #include <stdbool.h>
57 #include "netstat.h"
58 #include "prog_ops.h"
59 
60 #define	YES	1
61 
62 struct	mbstat mbstat;
63 struct pool mbpool, mclpool;
64 struct pool_allocator mbpa, mclpa;
65 
66 static struct mbtypes {
67 	int		mt_type;
68 	const char	*mt_name;
69 } mbtypes[] = {
70 	{ MT_DATA,	"data" },
71 	{ MT_OOBDATA,	"oob data" },
72 	{ MT_CONTROL,	"ancillary data" },
73 	{ MT_HEADER,	"packet headers" },
74 	{ MT_FTABLE,	"fragment reassembly queue headers" },	/* XXX */
75 	{ MT_SONAME,	"socket names and addresses" },
76 	{ MT_SOOPTS,	"socket options" },
77 	{ 0, 0 }
78 };
79 
80 const int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
81 bool seen[256];			/* "have we seen this type yet?" */
82 
83 int mbstats_ctl[] = { CTL_KERN, KERN_MBUF, MBUF_STATS };
84 int mowners_ctl[] = { CTL_KERN, KERN_MBUF, MBUF_MOWNERS };
85 
86 /*
87  * Print mbuf statistics.
88  */
89 void
90 mbpr(u_long mbaddr, u_long msizeaddr, u_long mclbaddr, u_long mbpooladdr,
91 	u_long mclpooladdr)
92 {
93 	u_long totmem, totused, totpct;
94 	u_int totmbufs;
95 	int i, lines;
96 	struct mbtypes *mp;
97 	size_t len;
98 	void *data;
99 	struct mowner_user *mo;
100 	int mclbytes, msize;
101 
102 	if (nmbtypes != 256) {
103 		fprintf(stderr,
104 		    "%s: unexpected change to mbstat; check source\n",
105 		        getprogname());
106 		return;
107 	}
108 
109 	if (use_sysctl) {
110 		size_t mbstatlen = sizeof(mbstat);
111 		if (prog_sysctl(mbstats_ctl,
112 			    sizeof(mbstats_ctl) / sizeof(mbstats_ctl[0]),
113 			    &mbstat, &mbstatlen, NULL, 0) < 0) {
114 			warn("mbstat: sysctl failed");
115 			return;
116 		}
117 		goto printit;
118 	}
119 
120 	if (mbaddr == 0) {
121 		fprintf(stderr, "%s: mbstat: symbol not in namelist\n",
122 		    getprogname());
123 		return;
124 	}
125 /*XXX*/
126 	if (msizeaddr != 0)
127 		kread(msizeaddr, (char *)&msize, sizeof (msize));
128 	else
129 		msize = MSIZE;
130 	if (mclbaddr != 0)
131 		kread(mclbaddr, (char *)&mclbytes, sizeof (mclbytes));
132 	else
133 		mclbytes = MCLBYTES;
134 /*XXX*/
135 
136 	if (kread(mbaddr, (char *)&mbstat, sizeof (mbstat)))
137 		return;
138 
139 	if (kread(mbpooladdr, (char *)&mbpool, sizeof (mbpool)))
140 		return;
141 
142 	if (kread(mclpooladdr, (char *)&mclpool, sizeof (mclpool)))
143 		return;
144 
145 	mbpooladdr = (u_long) mbpool.pr_alloc;
146 	mclpooladdr = (u_long) mclpool.pr_alloc;
147 
148 	if (kread(mbpooladdr, (char *)&mbpa, sizeof (mbpa)))
149 		return;
150 
151 	if (kread(mclpooladdr, (char *)&mclpa, sizeof (mclpa)))
152 		return;
153 
154     printit:
155 	totmbufs = 0;
156 	for (mp = mbtypes; mp->mt_name; mp++)
157 		totmbufs += mbstat.m_mtypes[mp->mt_type];
158 	printf("%u mbufs in use:\n", totmbufs);
159 	for (mp = mbtypes; mp->mt_name; mp++)
160 		if (mbstat.m_mtypes[mp->mt_type]) {
161 			seen[mp->mt_type] = YES;
162 			printf("\t%u mbufs allocated to %s\n",
163 			    mbstat.m_mtypes[mp->mt_type], mp->mt_name);
164 		}
165 	seen[MT_FREE] = YES;
166 	for (i = 0; i < nmbtypes; i++)
167 		if (!seen[i] && mbstat.m_mtypes[i]) {
168 			printf("\t%u mbufs allocated to <mbuf type %d>\n",
169 			    mbstat.m_mtypes[i], i);
170 		}
171 
172 	if (use_sysctl)		/* XXX */
173 		goto dump_drain;
174 
175 	printf("%lu/%lu mapped pages in use\n",
176 	       (u_long)(mclpool.pr_nget - mclpool.pr_nput),
177 	       ((u_long)mclpool.pr_npages * mclpool.pr_itemsperpage));
178 	totmem = (mbpool.pr_npages << mbpa.pa_pageshift) +
179 	    (mclpool.pr_npages << mclpa.pa_pageshift);
180 	totused = (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size +
181 	    (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
182 	if (totmem == 0)
183 		totpct = 0;
184 	else if (totused < (ULONG_MAX/100))
185 		totpct = (totused * 100)/totmem;
186 	else {
187 		u_long totmem1 = totmem/100;
188 		u_long totused1 = totused/100;
189 		totpct = (totused1 * 100)/totmem1;
190 	}
191 
192 	printf("%lu Kbytes allocated to network (%lu%% in use)\n",
193 	    totmem / 1024, totpct);
194 
195 dump_drain:
196 	printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
197 
198  	if (sflag < 2)
199 		return;
200 
201 	if (!use_sysctl)
202 		return;
203 
204 	if (prog_sysctl(mowners_ctl,
205 	    sizeof(mowners_ctl)/sizeof(mowners_ctl[0]),
206 	    NULL, &len, NULL, 0) < 0) {
207 		if (errno == ENOENT)
208 			return;
209 		warn("mowners: sysctl test");
210 		return;
211 	}
212 	len += 10 * sizeof(*mo);		/* add some slop */
213 	data = malloc(len);
214 	if (data == NULL) {
215 		warn("malloc(%lu)", (u_long)len);
216 		return;
217 	}
218 
219 	if (prog_sysctl(mowners_ctl,
220 	    sizeof(mowners_ctl)/sizeof(mowners_ctl[0]),
221 	    data, &len, NULL, 0) < 0) {
222 		warn("mowners: sysctl get");
223 		free(data);
224 		return;
225 	}
226 
227 	for (mo = (void *) data, lines = 0; len >= sizeof(*mo);
228 	    len -= sizeof(*mo), mo++) {
229 		char buf[32];
230 		if (vflag == 1 &&
231 		    mo->mo_counter[MOWNER_COUNTER_CLAIMS] == 0 &&
232 		    mo->mo_counter[MOWNER_COUNTER_EXT_CLAIMS] == 0 &&
233 		    mo->mo_counter[MOWNER_COUNTER_CLUSTER_CLAIMS] == 0)
234 			continue;
235 		if (vflag == 0 &&
236 		    mo->mo_counter[MOWNER_COUNTER_CLAIMS] ==
237 		    mo->mo_counter[MOWNER_COUNTER_RELEASES] &&
238 		    mo->mo_counter[MOWNER_COUNTER_EXT_CLAIMS] ==
239 		    mo->mo_counter[MOWNER_COUNTER_EXT_RELEASES] &&
240 		    mo->mo_counter[MOWNER_COUNTER_CLUSTER_CLAIMS] ==
241 		    mo->mo_counter[MOWNER_COUNTER_CLUSTER_RELEASES])
242 			continue;
243 		snprintf(buf, sizeof(buf), "%16s %-13s",
244 		    mo->mo_name, mo->mo_descr);
245 		if ((lines % 24) == 0 || lines > 24) {
246 			printf("%30s %-8s %10s %10s %10s\n",
247 			    "", "", "small", "ext", "cluster");
248 			lines = 1;
249 		}
250 		printf("%30s %-8s %10lu %10lu %10lu\n",
251 		    buf, "inuse",
252 		    mo->mo_counter[MOWNER_COUNTER_CLAIMS] -
253 		    mo->mo_counter[MOWNER_COUNTER_RELEASES],
254 		    mo->mo_counter[MOWNER_COUNTER_EXT_CLAIMS] -
255 		    mo->mo_counter[MOWNER_COUNTER_EXT_RELEASES],
256 		    mo->mo_counter[MOWNER_COUNTER_CLUSTER_CLAIMS] -
257 		    mo->mo_counter[MOWNER_COUNTER_CLUSTER_RELEASES]);
258 		lines++;
259 		if (vflag) {
260 			printf("%30s %-8s %10lu %10lu %10lu\n",
261 			    "", "claims",
262 			    mo->mo_counter[MOWNER_COUNTER_CLAIMS],
263 			    mo->mo_counter[MOWNER_COUNTER_EXT_CLAIMS],
264 			    mo->mo_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]);
265 			printf("%30s %-8s %10lu %10lu %10lu\n",
266 			    "", "releases",
267 			    mo->mo_counter[MOWNER_COUNTER_RELEASES],
268 			    mo->mo_counter[MOWNER_COUNTER_EXT_RELEASES],
269 			    mo->mo_counter[MOWNER_COUNTER_CLUSTER_RELEASES]);
270 			lines += 2;
271 		}
272 	}
273 	free(data);
274 }
275