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