Home | History | Annotate | Line # | Download | only in netstat
      1 /*	$NetBSD: mbuf.c,v 1.36 2024/02/24 09:53:26 mlelstv 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.36 2024/02/24 09:53:26 mlelstv 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 "netstat.h"
     57 #include "prog_ops.h"
     58 
     59 #define	YES	1
     60 
     61 struct	mbstat mbstat;
     62 struct pool mbpool, mclpool;
     63 struct pool_allocator mbpa, mclpa;
     64 
     65 static struct mbtypes {
     66 	int		mt_type;
     67 	const char	*mt_name;
     68 } mbtypes[] = {
     69 	{ MT_DATA,	"data" },
     70 	{ MT_OOBDATA,	"oob data" },
     71 	{ MT_CONTROL,	"ancillary data" },
     72 	{ MT_HEADER,	"packet headers" },
     73 	{ MT_FTABLE,	"fragment reassembly queue headers" },	/* XXX */
     74 	{ MT_SONAME,	"socket names and addresses" },
     75 	{ MT_SOOPTS,	"socket options" },
     76 	{ 0, 0 }
     77 };
     78 
     79 const int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
     80 bool seen[256];			/* "have we seen this type yet?" */
     81 
     82 int mbstats_ctl[] = { CTL_KERN, KERN_MBUF, MBUF_STATS };
     83 int mowners_ctl[] = { CTL_KERN, KERN_MBUF, MBUF_MOWNERS };
     84 
     85 /*
     86  * Print mbuf statistics.
     87  */
     88 void
     89 mbpr(u_long mbaddr, u_long msizeaddr, u_long mclbaddr, u_long mbpooladdr,
     90 	u_long mclpooladdr)
     91 {
     92 	u_long totmem, totused, totpct;
     93 	u_int totmbufs;
     94 	int i, lines;
     95 	struct mbtypes *mp;
     96 	size_t len;
     97 	void *data;
     98 	struct mowner_user *mo;
     99 	int mclbytes, msize;
    100 
    101 	if (nmbtypes != 256) {
    102 		fprintf(stderr,
    103 		    "%s: unexpected change to mbstat; check source\n",
    104 		    getprogname());
    105 		return;
    106 	}
    107 
    108 	if (use_sysctl) {
    109 		size_t mbstatlen = sizeof(mbstat);
    110 		if (prog_sysctl(mbstats_ctl,
    111 			    sizeof(mbstats_ctl) / sizeof(mbstats_ctl[0]),
    112 			    &mbstat, &mbstatlen, NULL, 0) < 0) {
    113 			warn("mbstat: sysctl failed");
    114 			return;
    115 		}
    116 		goto printit;
    117 	}
    118 
    119 	if (mbaddr == 0) {
    120 		fprintf(stderr, "%s: mbstat: symbol not in namelist\n",
    121 		    getprogname());
    122 		return;
    123 	}
    124 /*XXX*/
    125 	if (msizeaddr != 0)
    126 		kread(msizeaddr, (char *)&msize, sizeof (msize));
    127 	else
    128 		msize = MSIZE;
    129 	if (mclbaddr != 0)
    130 		kread(mclbaddr, (char *)&mclbytes, sizeof (mclbytes));
    131 	else
    132 		mclbytes = MCLBYTES;
    133 /*XXX*/
    134 
    135 	if (kread(mbaddr, (char *)&mbstat, sizeof (mbstat)))
    136 		return;
    137 
    138 	if (kread(mbpooladdr, (char *)&mbpool, sizeof (mbpool)))
    139 		return;
    140 
    141 	if (kread(mclpooladdr, (char *)&mclpool, sizeof (mclpool)))
    142 		return;
    143 
    144 	mbpooladdr = (u_long) mbpool.pr_alloc;
    145 	mclpooladdr = (u_long) mclpool.pr_alloc;
    146 
    147 	if (kread(mbpooladdr, (char *)&mbpa, sizeof (mbpa)))
    148 		return;
    149 
    150 	if (kread(mclpooladdr, (char *)&mclpa, sizeof (mclpa)))
    151 		return;
    152 
    153     printit:
    154 	totmbufs = 0;
    155 	for (mp = mbtypes; mp->mt_name; mp++)
    156 		totmbufs += mbstat.m_mtypes[mp->mt_type];
    157 	printf("%u mbufs in use:\n", totmbufs);
    158 	for (mp = mbtypes; mp->mt_name; mp++)
    159 		if (mbstat.m_mtypes[mp->mt_type]) {
    160 			seen[mp->mt_type] = YES;
    161 			printf("\t%u mbufs allocated to %s\n",
    162 			    mbstat.m_mtypes[mp->mt_type], mp->mt_name);
    163 		}
    164 	seen[MT_FREE] = YES;
    165 	for (i = 0; i < nmbtypes; i++)
    166 		if (!seen[i] && mbstat.m_mtypes[i]) {
    167 			printf("\t%u mbufs allocated to <mbuf type %d>\n",
    168 			    mbstat.m_mtypes[i], i);
    169 		}
    170 
    171 	if (use_sysctl)		/* XXX */
    172 		goto dump_drain;
    173 
    174 	printf("%lu/%lu mapped pages in use\n",
    175 	    (u_long)(mclpool.pr_nget - mclpool.pr_nput),
    176 	    ((u_long)mclpool.pr_npages * mclpool.pr_itemsperpage));
    177 	totmem = (mbpool.pr_npages << mbpa.pa_pageshift) +
    178 	    (mclpool.pr_npages << mclpa.pa_pageshift);
    179 	totused = (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size +
    180 	    (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
    181 	if (totmem == 0)
    182 		totpct = 0;
    183 	else if (totused < (ULONG_MAX / 100))
    184 		totpct = (totused * 100)/totmem;
    185 	else {
    186 		u_long totmem1 = totmem / 100;
    187 		u_long totused1 = totused / 100;
    188 		totpct = (totused1 * 100) / totmem1;
    189 	}
    190 
    191 	printf("%lu Kbytes allocated to network (%lu%% in use)\n",
    192 	    totmem / 1024, totpct);
    193 
    194 dump_drain:
    195 	printf("%lu calls to protocol drain routines\n", mbstat.m_drain);
    196 
    197 	if (sflag < 2)
    198 		return;
    199 
    200 	if (!use_sysctl)
    201 		return;
    202 
    203 	if (prog_sysctl(mowners_ctl,
    204 	    sizeof(mowners_ctl)/sizeof(mowners_ctl[0]),
    205 	    NULL, &len, NULL, 0) < 0) {
    206 		if (errno == ENOENT)
    207 			return;
    208 		warn("mowners: sysctl test");
    209 		return;
    210 	}
    211 	len += 10 * sizeof(*mo);		/* add some slop */
    212 	data = malloc(len);
    213 	if (data == NULL) {
    214 		warn("malloc(%lu)", (u_long)len);
    215 		return;
    216 	}
    217 
    218 	if (prog_sysctl(mowners_ctl,
    219 	    sizeof(mowners_ctl)/sizeof(mowners_ctl[0]),
    220 	    data, &len, NULL, 0) < 0) {
    221 		warn("mowners: sysctl get");
    222 		free(data);
    223 		return;
    224 	}
    225 
    226 	for (mo = (void *) data, lines = 0; len >= sizeof(*mo);
    227 	    len -= sizeof(*mo), mo++) {
    228 		char buf[34];
    229 		if (vflag == 1 &&
    230 		    mo->mo_counter[MOWNER_COUNTER_CLAIMS] == 0 &&
    231 		    mo->mo_counter[MOWNER_COUNTER_EXT_CLAIMS] == 0 &&
    232 		    mo->mo_counter[MOWNER_COUNTER_CLUSTER_CLAIMS] == 0)
    233 			continue;
    234 		if (vflag == 0 &&
    235 		    mo->mo_counter[MOWNER_COUNTER_CLAIMS] ==
    236 		    mo->mo_counter[MOWNER_COUNTER_RELEASES] &&
    237 		    mo->mo_counter[MOWNER_COUNTER_EXT_CLAIMS] ==
    238 		    mo->mo_counter[MOWNER_COUNTER_EXT_RELEASES] &&
    239 		    mo->mo_counter[MOWNER_COUNTER_CLUSTER_CLAIMS] ==
    240 		    mo->mo_counter[MOWNER_COUNTER_CLUSTER_RELEASES])
    241 			continue;
    242 		snprintf(buf, sizeof(buf), "%16.16s %-13.16s",
    243 		    mo->mo_name, mo->mo_descr);
    244 		if ((lines % 24) == 0 || lines > 24) {
    245 			printf("%30s %-8s %10s %10s %10s\n",
    246 			    "", "", "small", "ext", "cluster");
    247 			lines = 1;
    248 		}
    249 		printf("%30s %-8s %10lu %10lu %10lu\n",
    250 		    buf, "inuse",
    251 		    mo->mo_counter[MOWNER_COUNTER_CLAIMS] -
    252 		    mo->mo_counter[MOWNER_COUNTER_RELEASES],
    253 		    mo->mo_counter[MOWNER_COUNTER_EXT_CLAIMS] -
    254 		    mo->mo_counter[MOWNER_COUNTER_EXT_RELEASES],
    255 		    mo->mo_counter[MOWNER_COUNTER_CLUSTER_CLAIMS] -
    256 		    mo->mo_counter[MOWNER_COUNTER_CLUSTER_RELEASES]);
    257 		lines++;
    258 		if (vflag) {
    259 			printf("%30s %-8s %10lu %10lu %10lu\n",
    260 			    "", "claims",
    261 			    mo->mo_counter[MOWNER_COUNTER_CLAIMS],
    262 			    mo->mo_counter[MOWNER_COUNTER_EXT_CLAIMS],
    263 			    mo->mo_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]);
    264 			printf("%30s %-8s %10lu %10lu %10lu\n",
    265 			    "", "releases",
    266 			    mo->mo_counter[MOWNER_COUNTER_RELEASES],
    267 			    mo->mo_counter[MOWNER_COUNTER_EXT_RELEASES],
    268 			    mo->mo_counter[MOWNER_COUNTER_CLUSTER_RELEASES]);
    269 			lines += 2;
    270 		}
    271 	}
    272 	free(data);
    273 }
    274