2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 Christian S.J. Peron
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/protosw.h>
34 #include <sys/socket.h>
35 #include <sys/socketvar.h>
36 #include <sys/sysctl.h>
37 #include <sys/param.h>
42 #include <net/bpfdesc.h>
43 #include <arpa/inet.h>
60 bpf_pidname(pid_t pid)
62 struct kinfo_proc newkp;
68 mib[2] = KERN_PROC_PID;
71 error = sysctl(mib, 4, &newkp, &size, NULL, 0);
73 xo_warn("kern.proc.pid failed");
74 return (strdup("??????"));
76 return (strdup(newkp.ki_comm));
80 bpf_flags(struct xbpf_d *bd, char *flagbuf)
83 *flagbuf++ = bd->bd_promisc ? 'p' : '-';
84 *flagbuf++ = bd->bd_immediate ? 'i' : '-';
85 *flagbuf++ = bd->bd_hdrcmplt ? '-' : 'f';
86 *flagbuf++ = (bd->bd_direction == BPF_D_IN) ? '-' :
87 ((bd->bd_direction == BPF_D_OUT) ? 'o' : 's');
88 *flagbuf++ = bd->bd_feedback ? 'b' : '-';
89 *flagbuf++ = bd->bd_async ? 'a' : '-';
90 *flagbuf++ = bd->bd_locked ? 'l' : '-';
94 xo_emit("{e:promiscuous/}");
96 xo_emit("{e:immediate/}");
98 xo_emit("{e:header-complete/}");
99 xo_emit("{e:direction}", (bd->bd_direction == BPF_D_IN) ? "input" :
100 (bd->bd_direction == BPF_D_OUT) ? "output" : "bidirectional");
102 xo_emit("{e:feedback/}");
104 xo_emit("{e:async/}");
106 xo_emit("{e:locked/}");
110 bpf_stats(char *ifname)
112 struct xbpf_d *d, *bd, zerostat;
113 char *pname, flagbuf[12];
117 bzero(&zerostat, sizeof(zerostat));
118 if (sysctlbyname("net.bpf.stats", NULL, NULL,
119 &zerostat, sizeof(zerostat)) < 0)
120 xo_warn("failed to zero bpf counters");
123 if (sysctlbyname("net.bpf.stats", NULL, &size,
125 xo_warn("net.bpf.stats");
132 xo_warn("malloc failed");
135 if (sysctlbyname("net.bpf.stats", bd, &size,
137 xo_warn("net.bpf.stats");
141 xo_emit("{T:/%5s} {T:/%6s} {T:/%7s} {T:/%9s} {T:/%9s} {T:/%9s} "
142 "{T:/%5s} {T:/%5s} {T:/%s}\n",
143 "Pid", "Netif", "Flags", "Recv", "Drop", "Match",
144 "Sblen", "Hblen", "Command");
145 xo_open_container("bpf-statistics");
146 xo_open_list("bpf-entry");
147 for (d = &bd[0]; d < &bd[size / sizeof(*d)]; d++) {
148 if (d->bd_structsize != sizeof(*d)) {
149 xo_warnx("bpf_stats_extended: version mismatch");
152 if (ifname && strcmp(ifname, d->bd_ifname) != 0)
154 xo_open_instance("bpf-entry");
155 pname = bpf_pidname(d->bd_pid);
156 xo_emit("{k:pid/%5d} {k:interface-name/%6s} ",
157 d->bd_pid, d->bd_ifname);
158 bpf_flags(d, flagbuf);
159 xo_emit("{d:flags/%7s} {:received-packets/%9ju} "
160 "{:dropped-packets/%9ju} {:filter-packets/%9ju} "
161 "{:store-buffer-length/%5d} {:hold-buffer-length/%5d} "
163 flagbuf, (uintmax_t)d->bd_rcount, (uintmax_t)d->bd_dcount,
164 (uintmax_t)d->bd_fcount, d->bd_slen, d->bd_hlen, pname);
166 xo_close_instance("bpf-entry");
168 xo_close_list("bpf-entry");
169 xo_close_container("bpf-statistics");