GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/netstat/mbuf.c Lines: 0 61 0.0 %
Date: 2016-12-06 Branches: 0 36 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: mbuf.c,v 1.37 2016/04/04 16:26:00 sthen 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/socket.h>
34
#include <sys/queue.h>
35
#include <sys/mbuf.h>
36
#include <sys/protosw.h>
37
#include <sys/pool.h>
38
#include <sys/sysctl.h>
39
#include <net/if.h>
40
41
#include <errno.h>
42
#include <limits.h>
43
#include <stdio.h>
44
#include <string.h>
45
#include <unistd.h>
46
#include "netstat.h"
47
48
#define	YES	1
49
typedef int bool;
50
51
struct	mbstat mbstat;
52
struct kinfo_pool mbpool, mclpools[MCLPOOLS];
53
int	mclp;
54
char	*mclnames[] = {
55
	"mcl2k", "mcl4k", "mcl8k", "mcl9k", "mcl12k", "mcl16k", "mcl64k"
56
};
57
char	**mclnamep = mclnames;
58
59
static struct mbtypes {
60
	int	mt_type;
61
	char	*mt_name;
62
} mbtypes[] = {
63
	{ MT_DATA,	"data" },
64
	{ MT_OOBDATA,	"oob data" },
65
	{ MT_CONTROL,	"ancillary data" },
66
	{ MT_HEADER,	"packet headers" },
67
	{ MT_FTABLE,	"fragment reassembly queue headers" },	/* XXX */
68
	{ MT_SONAME,	"socket names and addresses" },
69
	{ MT_SOOPTS,	"socket options" },
70
	{ 0, 0 }
71
};
72
73
int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
74
bool seen[256];			/* "have we seen this type yet?" */
75
76
/*
77
 * Print mbuf statistics.
78
 */
79
void
80
mbpr(void)
81
{
82
	unsigned long totmem, totused, totmbufs;
83
	int totpct;
84
	int i, mib[4], npools;
85
	struct kinfo_pool pool;
86
	struct mbtypes *mp;
87
	size_t size;
88
89
	if (nmbtypes != 256) {
90
		fprintf(stderr,
91
		    "%s: unexpected change to mbstat; check source\n",
92
		    __progname);
93
		return;
94
	}
95
96
	mib[0] = CTL_KERN;
97
	mib[1] = KERN_MBSTAT;
98
	size = sizeof(mbstat);
99
100
	if (sysctl(mib, 2, &mbstat, &size, NULL, 0) < 0) {
101
		printf("Can't retrieve mbuf statistics from the kernel: %s\n",
102
		    strerror(errno));
103
		return;
104
	}
105
106
	mib[0] = CTL_KERN;
107
	mib[1] = KERN_POOL;
108
	mib[2] = KERN_POOL_NPOOLS;
109
	size = sizeof(npools);
110
111
	if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
112
		printf("Can't figure out number of pools in kernel: %s\n",
113
		    strerror(errno));
114
		return;
115
	}
116
117
	for (i = 1; npools; i++) {
118
		char name[32];
119
120
		mib[0] = CTL_KERN;
121
		mib[1] = KERN_POOL;
122
		mib[2] = KERN_POOL_POOL;
123
		mib[3] = i;
124
		size = sizeof(pool);
125
		if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
126
			if (errno == ENOENT)
127
				continue;
128
			printf("error getting pool: %s\n",
129
			    strerror(errno));
130
			return;
131
		}
132
		npools--;
133
		mib[2] = KERN_POOL_NAME;
134
		size = sizeof(name);
135
		if (sysctl(mib, 4, &name, &size, NULL, 0) < 0) {
136
			printf("error getting pool name: %s\n",
137
			    strerror(errno));
138
			return;
139
		}
140
141
		if (!strncmp(name, "mbufpl", strlen("mbufpl")))
142
			bcopy(&pool, &mbpool, sizeof(pool));
143
		else if (mclp < sizeof(mclpools) / sizeof(mclpools[0]) &&
144
		    !strncmp(name, *mclnamep, strlen(*mclnamep))) {
145
			bcopy(&pool, &mclpools[mclp++],
146
			    sizeof(pool));
147
			mclnamep++;
148
		}
149
	}
150
151
	totmbufs = 0;
152
	for (mp = mbtypes; mp->mt_name; mp++)
153
		totmbufs += (unsigned int)mbstat.m_mtypes[mp->mt_type];
154
	printf("%lu mbuf%s in use:\n", totmbufs, plural(totmbufs));
155
	for (mp = mbtypes; mp->mt_name; mp++)
156
		if (mbstat.m_mtypes[mp->mt_type]) {
157
			seen[mp->mt_type] = YES;
158
			printf("\t%u mbuf%s allocated to %s\n",
159
			    mbstat.m_mtypes[mp->mt_type],
160
			    plural(mbstat.m_mtypes[mp->mt_type]),
161
			    mp->mt_name);
162
		}
163
	seen[MT_FREE] = YES;
164
	for (i = 0; i < nmbtypes; i++)
165
		if (!seen[i] && mbstat.m_mtypes[i]) {
166
			printf("\t%u mbuf%s allocated to <mbuf type %d>\n",
167
			    mbstat.m_mtypes[i],
168
			    plural(mbstat.m_mtypes[i]), i);
169
		}
170
	totmem = (mbpool.pr_npages * mbpool.pr_pgsize);
171
	totused = mbpool.pr_nout * mbpool.pr_size;
172
	for (i = 0; i < mclp; i++) {
173
		printf("%u/%lu/%lu mbuf %d byte clusters in use"
174
		    " (current/peak/max)\n",
175
		    mclpools[i].pr_nout,
176
		    (unsigned long)
177
			(mclpools[i].pr_hiwat * mclpools[i].pr_itemsperpage),
178
		    (unsigned long)
179
			(mclpools[i].pr_maxpages * mclpools[i].pr_itemsperpage),
180
		    mclpools[i].pr_size);
181
		totmem += (mclpools[i].pr_npages * mclpools[i].pr_pgsize);
182
		totused += mclpools[i].pr_nout * mclpools[i].pr_size;
183
	}
184
185
	totpct = (totmem == 0) ? 0 : (totused/(totmem / 100));
186
	printf("%lu 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
}