2 * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/types.h>
31 #include <sys/protosw.h>
32 #include <sys/socket.h>
33 #include <sys/socketvar.h>
34 #include <sys/sysctl.h>
35 #include <sys/param.h>
40 #include <net/bpfdesc.h>
41 #include <arpa/inet.h>
58 bpf_pidname(pid_t pid)
60 struct kinfo_proc newkp;
66 mib[2] = KERN_PROC_PID;
69 error = sysctl(mib, 4, &newkp, &size, NULL, 0);
71 xo_warn("kern.proc.pid failed");
72 return (strdup("??????"));
74 return (strdup(newkp.ki_comm));
78 bpf_flags(struct xbpf_d *bd, char *flagbuf)
81 *flagbuf++ = bd->bd_promisc ? 'p' : '-';
82 *flagbuf++ = bd->bd_immediate ? 'i' : '-';
83 *flagbuf++ = bd->bd_hdrcmplt ? '-' : 'f';
84 *flagbuf++ = (bd->bd_direction == BPF_D_IN) ? '-' :
85 ((bd->bd_direction == BPF_D_OUT) ? 'o' : 's');
86 *flagbuf++ = bd->bd_feedback ? 'b' : '-';
87 *flagbuf++ = bd->bd_async ? 'a' : '-';
88 *flagbuf++ = bd->bd_locked ? 'l' : '-';
92 xo_emit("{e:promiscuous/}");
94 xo_emit("{e:immediate/}");
96 xo_emit("{e:header-complete/}");
97 xo_emit("{e:direction}", (bd->bd_direction == BPF_D_IN) ? "input" :
98 (bd->bd_direction == BPF_D_OUT) ? "output" : "bidirectional");
100 xo_emit("{e:feedback/}");
102 xo_emit("{e:async/}");
104 xo_emit("{e:locked/}");
108 bpf_stats(char *ifname)
110 struct xbpf_d *d, *bd, zerostat;
111 char *pname, flagbuf[12];
115 bzero(&zerostat, sizeof(zerostat));
116 if (sysctlbyname("net.bpf.stats", NULL, NULL,
117 &zerostat, sizeof(zerostat)) < 0)
118 xo_warn("failed to zero bpf counters");
121 if (sysctlbyname("net.bpf.stats", NULL, &size,
123 xo_warn("net.bpf.stats");
130 xo_warn("malloc failed");
133 if (sysctlbyname("net.bpf.stats", bd, &size,
135 xo_warn("net.bpf.stats");
139 xo_emit("{T:/%5s} {T:/%6s} {T:/%7s} {T:/%9s} {T:/%9s} {T:/%9s} "
140 "{T:/%5s} {T:/%5s} {T:/%s}\n",
141 "Pid", "Netif", "Flags", "Recv", "Drop", "Match",
142 "Sblen", "Hblen", "Command");
143 xo_open_container("bpf-statistics");
144 xo_open_list("bpf-entry");
145 for (d = &bd[0]; d < &bd[size / sizeof(*d)]; d++) {
146 if (d->bd_structsize != sizeof(*d)) {
147 xo_warnx("bpf_stats_extended: version mismatch");
150 if (ifname && strcmp(ifname, d->bd_ifname) != 0)
152 xo_open_instance("bpf-entry");
153 pname = bpf_pidname(d->bd_pid);
154 xo_emit("{k:pid/%5d} {k:interface-name/%6s} ",
155 d->bd_pid, d->bd_ifname);
156 bpf_flags(d, flagbuf);
157 xo_emit("{d:flags/%7s} {:received-packets/%9ju} "
158 "{:dropped-packets/%9ju} {:filter-packets/%9ju} "
159 "{:store-buffer-length/%5d} {:hold-buffer-length/%5d} "
161 flagbuf, (uintmax_t)d->bd_rcount, (uintmax_t)d->bd_dcount,
162 (uintmax_t)d->bd_fcount, d->bd_slen, d->bd_hlen, pname);
164 xo_close_instance("bpf-entry");
166 xo_close_list("bpf-entry");
167 xo_close_container("bpf-statistics");