1 /* $NetBSD: mbuf.c,v 1.24 2005/08/04 19:41:28 rpaulo 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.24 2005/08/04 19:41:28 rpaulo 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 58 #define YES 1 59 typedef int bool; 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 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(mbaddr, msizeaddr, mclbaddr, mbpooladdr, mclpooladdr) 90 u_long mbaddr; 91 u_long msizeaddr, mclbaddr; 92 u_long mbpooladdr, mclpooladdr; 93 { 94 u_long totmem, totused, totpct; 95 u_int totmbufs; 96 int i, lines; 97 struct mbtypes *mp; 98 size_t len; 99 void *data; 100 struct mowner *mo; 101 int mclbytes, msize; 102 103 if (nmbtypes != 256) { 104 fprintf(stderr, 105 "%s: unexpected change to mbstat; check source\n", 106 getprogname()); 107 return; 108 } 109 110 if (use_sysctl) { 111 size_t mbstatlen = sizeof(mbstat); 112 if (sysctl(mbstats_ctl, 113 sizeof(mbstats_ctl) / sizeof(mbstats_ctl[0]), 114 &mbstat, &mbstatlen, NULL, 0) < 0) { 115 warn("mbstat: sysctl failed"); 116 return; 117 } 118 goto printit; 119 } 120 121 if (mbaddr == 0) { 122 fprintf(stderr, "%s: mbstat: symbol not in namelist\n", 123 getprogname()); 124 return; 125 } 126 /*XXX*/ 127 if (msizeaddr != 0) 128 kread(msizeaddr, (char *)&msize, sizeof (msize)); 129 else 130 msize = MSIZE; 131 if (mclbaddr != 0) 132 kread(mclbaddr, (char *)&mclbytes, sizeof (mclbytes)); 133 else 134 mclbytes = MCLBYTES; 135 /*XXX*/ 136 137 if (kread(mbaddr, (char *)&mbstat, sizeof (mbstat))) 138 return; 139 140 if (kread(mbpooladdr, (char *)&mbpool, sizeof (mbpool))) 141 return; 142 143 if (kread(mclpooladdr, (char *)&mclpool, sizeof (mclpool))) 144 return; 145 146 mbpooladdr = (u_long) mbpool.pr_alloc; 147 mclpooladdr = (u_long) mclpool.pr_alloc; 148 149 if (kread(mbpooladdr, (char *)&mbpa, sizeof (mbpa))) 150 return; 151 152 if (kread(mclpooladdr, (char *)&mclpa, sizeof (mclpa))) 153 return; 154 155 printit: 156 totmbufs = 0; 157 for (mp = mbtypes; mp->mt_name; mp++) 158 totmbufs += mbstat.m_mtypes[mp->mt_type]; 159 printf("%u mbufs in use:\n", totmbufs); 160 for (mp = mbtypes; mp->mt_name; mp++) 161 if (mbstat.m_mtypes[mp->mt_type]) { 162 seen[mp->mt_type] = YES; 163 printf("\t%u mbufs allocated to %s\n", 164 mbstat.m_mtypes[mp->mt_type], mp->mt_name); 165 } 166 seen[MT_FREE] = YES; 167 for (i = 0; i < nmbtypes; i++) 168 if (!seen[i] && mbstat.m_mtypes[i]) { 169 printf("\t%u mbufs allocated to <mbuf type %d>\n", 170 mbstat.m_mtypes[i], i); 171 } 172 173 if (use_sysctl) /* XXX */ 174 goto dump_drain; 175 176 printf("%lu/%lu mapped pages in use\n", 177 (u_long)(mclpool.pr_nget - mclpool.pr_nput), 178 ((u_long)mclpool.pr_npages * mclpool.pr_itemsperpage)); 179 totmem = (mbpool.pr_npages << mbpa.pa_pageshift) + 180 (mclpool.pr_npages << mclpa.pa_pageshift); 181 totused = (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size + 182 (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size; 183 if (totmem == 0) 184 totpct = 0; 185 else if (totused < (ULONG_MAX/100)) 186 totpct = (totused * 100)/totmem; 187 else { 188 u_long totmem1 = totmem/100; 189 u_long totused1 = totused/100; 190 totpct = (totused1 * 100)/totmem1; 191 } 192 193 printf("%lu Kbytes allocated to network (%lu%% in use)\n", 194 totmem / 1024, totpct); 195 printf("%lu requests for memory denied\n", mbstat.m_drops); 196 printf("%lu requests for memory delayed\n", mbstat.m_wait); 197 198 dump_drain: 199 printf("%lu calls to protocol drain routines\n", mbstat.m_drain); 200 201 if (sflag < 2) 202 return; 203 204 if (!use_sysctl) 205 return; 206 207 if (sysctl(mowners_ctl, sizeof(mowners_ctl)/sizeof(mowners_ctl[0]), 208 NULL, &len, NULL, 0) < 0) { 209 if (errno == ENOENT) 210 return; 211 warn("mowners: sysctl test"); 212 return; 213 } 214 len += 10 * sizeof(mo); /* add some slop */ 215 data = malloc(len); 216 if (data == NULL) { 217 warn("malloc(%lu)", (u_long)len); 218 return; 219 } 220 221 if (sysctl(mowners_ctl, sizeof(mowners_ctl)/sizeof(mowners_ctl[0]), 222 data, &len, NULL, 0) < 0) { 223 warn("mowners: sysctl get"); 224 free(data); 225 return; 226 } 227 228 for (mo = (void *) data, lines = 0; len >= sizeof(*mo); 229 len -= sizeof(*mo), mo++) { 230 char buf[32]; 231 if (vflag == 1 && 232 mo->mo_claims == 0 && 233 mo->mo_ext_claims == 0 && 234 mo->mo_cluster_claims == 0) 235 continue; 236 if (vflag == 0 && 237 mo->mo_claims == mo->mo_releases && 238 mo->mo_ext_claims == mo->mo_ext_releases && 239 mo->mo_cluster_claims == mo->mo_cluster_releases) 240 continue; 241 snprintf(buf, sizeof(buf), "%16s %-13s", 242 mo->mo_name, mo->mo_descr); 243 if ((lines % 24) == 0 || lines > 24) { 244 printf("%30s %-8s %10s %10s %10s\n", 245 "", "", "small", "ext", "cluster"); 246 lines = 1; 247 } 248 printf("%30s %-8s %10lu %10lu %10lu\n", 249 buf, "inuse", 250 mo->mo_claims - mo->mo_releases, 251 mo->mo_ext_claims - mo->mo_ext_releases, 252 mo->mo_cluster_claims - mo->mo_cluster_releases); 253 lines++; 254 if (vflag) { 255 printf("%30s %-8s %10lu %10lu %10lu\n", 256 "", "claims", 257 mo->mo_claims, 258 mo->mo_ext_claims, 259 mo->mo_cluster_claims); 260 printf("%30s %-8s %10lu %10lu %10lu\n", 261 "", "releases", 262 mo->mo_releases, 263 mo->mo_ext_releases, 264 mo->mo_cluster_releases); 265 lines += 2; 266 } 267 } 268 free(data); 269 } 270