GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/netstat/mbuf.c Lines: 0 81 0.0 %
Date: 2017-11-07 Branches: 0 39 0.0 %

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