mbuf.c revision 1.28
1/* $OpenBSD: mbuf.c,v 1.28 2008/12/04 06:00:47 deraadt Exp $ */ 2/* $NetBSD: mbuf.c,v 1.9 1996/05/07 02:55:03 thorpej Exp $ */ 3 4/* 5 * Copyright (c) 1983, 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#include <sys/param.h> 34#include <sys/protosw.h> 35#include <sys/socket.h> 36#include <sys/mbuf.h> 37#include <sys/pool.h> 38#include <sys/sysctl.h> 39#include <net/if.h> 40 41#include <errno.h> 42#include <kvm.h> 43#include <limits.h> 44#include <stdio.h> 45#include <string.h> 46#include <unistd.h> 47#include "netstat.h" 48 49#define YES 1 50typedef int bool; 51 52struct mbstat mbstat; 53struct pool mbpool, mclpools[MCLPOOLS]; 54int mclp; 55char *mclnames[] = { 56 "mcl2k", "mcl4k", "mcl8k", "mcl9k", "mcl12k", "mcl16k", "mcl64k" 57}; 58char **mclnamep = mclnames; 59 60extern kvm_t *kvmd; 61 62static struct mbtypes { 63 int mt_type; 64 char *mt_name; 65} mbtypes[] = { 66 { MT_DATA, "data" }, 67 { MT_OOBDATA, "oob data" }, 68 { MT_CONTROL, "ancillary data" }, 69 { MT_HEADER, "packet headers" }, 70 { MT_FTABLE, "fragment reassembly queue headers" }, /* XXX */ 71 { MT_SONAME, "socket names and addresses" }, 72 { MT_SOOPTS, "socket options" }, 73 { 0, 0 } 74}; 75 76int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short); 77bool seen[256]; /* "have we seen this type yet?" */ 78 79/* 80 * Print mbuf statistics. 81 */ 82void 83mbpr(void) 84{ 85 int totmem, totused, totmbufs, totpct; 86 int i, mib[4], npools; 87 struct pool pool; 88 struct mbtypes *mp; 89 size_t size; 90 int page_size = getpagesize(); 91 92 if (nmbtypes != 256) { 93 fprintf(stderr, 94 "%s: unexpected change to mbstat; check source\n", 95 __progname); 96 return; 97 } 98 99 mib[0] = CTL_KERN; 100 mib[1] = KERN_MBSTAT; 101 size = sizeof(mbstat); 102 103 if (sysctl(mib, 2, &mbstat, &size, NULL, 0) < 0) { 104 printf("Can't retrieve mbuf statistics from the kernel: %s\n", 105 strerror(errno)); 106 return; 107 } 108 109 mib[0] = CTL_KERN; 110 mib[1] = KERN_POOL; 111 mib[2] = KERN_POOL_NPOOLS; 112 size = sizeof(npools); 113 114 if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) { 115 printf("Can't figure out number of pools in kernel: %s\n", 116 strerror(errno)); 117 return; 118 } 119 120 for (i = 1; npools; i++) { 121 char name[32]; 122 123 mib[0] = CTL_KERN; 124 mib[1] = KERN_POOL; 125 mib[2] = KERN_POOL_POOL; 126 mib[3] = i; 127 size = sizeof(struct pool); 128 if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) { 129 if (errno == ENOENT) 130 continue; 131 printf("error getting pool: %s\n", 132 strerror(errno)); 133 return; 134 } 135 npools--; 136 mib[2] = KERN_POOL_NAME; 137 size = sizeof(name); 138 if (sysctl(mib, 4, &name, &size, NULL, 0) < 0) { 139 printf("error getting pool name: %s\n", 140 strerror(errno)); 141 return; 142 } 143 144 if (!strncmp(name, "mbpl", strlen("mbpl"))) 145 bcopy(&pool, &mbpool, sizeof(struct pool)); 146 else if (mclp < nitems(mclpools) && 147 !strncmp(name, *mclnamep, strlen(*mclnamep))) { 148 bcopy(&pool, &mclpools[mclp++], 149 sizeof(struct pool)); 150 mclnamep++; 151 } 152 } 153 154 totmbufs = 0; 155 for (mp = mbtypes; mp->mt_name; mp++) 156 totmbufs += mbstat.m_mtypes[mp->mt_type]; 157 printf("%u mbuf%s in use:\n", totmbufs, plural(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 mbuf%s allocated to %s\n", 162 mbstat.m_mtypes[mp->mt_type], 163 plural((int)mbstat.m_mtypes[mp->mt_type]), 164 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 mbuf%s allocated to <mbuf type %d>\n", 170 mbstat.m_mtypes[i], 171 plural((int)mbstat.m_mtypes[i]), i); 172 } 173 totmem = (mbpool.pr_npages * page_size); 174 totused = mbpool.pr_nout * mbpool.pr_size; 175 for (i = 0; i < mclp; i++) { 176 printf("%lu/%lu/%lu mbuf %d byte clusters in use (current/peak/max)\n", 177 (u_long)(mclpools[i].pr_nout), 178 (u_long)(mclpools[i].pr_hiwat * mclpools[i].pr_itemsperpage), 179 (u_long)(mclpools[i].pr_maxpages * mclpools[i].pr_itemsperpage), 180 mclpools[i].pr_size); 181 totmem += (mclpools[i].pr_npages * page_size); 182 totused += mclpools[i].pr_nout * mclpools[i].pr_size; 183 } 184 185 totpct = (totmem == 0)? 0 : ((totused * 100)/totmem); 186 printf("%u Kbytes allocated to network (%d%% in use)\n", 187 totmem / 1024, totpct); 188 printf("%lu requests for memory denied\n", mbstat.m_drops); 189 printf("%lu requests for memory delayed\n", mbstat.m_wait); 190 printf("%lu calls to protocol drain routines\n", mbstat.m_drain); 191} 192