2 * Copyright (c) 2005 Christian S.J. Peron
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/types.h>
29 #include <sys/protosw.h>
30 #include <sys/socket.h>
31 #include <sys/sysctl.h>
32 #include <sys/param.h>
36 #include <net/if_var.h>
37 #include <net/bpfdesc.h>
38 #include <arpa/inet.h>
52 bpf_pidname(pid_t pid)
54 struct kinfo_proc newkp;
60 mib[2] = KERN_PROC_PID;
63 error = sysctl(mib, 4, &newkp, &size, NULL, 0);
65 warn("kern.proc.pid failed");
66 return (strdup("??????"));
68 return (strdup(newkp.ki_comm));
72 bpf_flags(struct xbpf_d *bd, char *flagbuf)
75 *flagbuf++ = bd->bd_promisc ? 'p' : '-';
76 *flagbuf++ = bd->bd_immediate ? 'i' : '-';
77 *flagbuf++ = bd->bd_hdrcmplt ? '-' : 'f';
78 *flagbuf++ = bd->bd_seesent ? 's' : '-';
79 *flagbuf++ = bd->bd_async ? 'a' : '-';
80 *flagbuf++ = bd->bd_locked ? 'l' : '-';
85 bpf_stats(char *interface)
87 struct xbpf_d *d, *bd;
88 char *pname, flagbuf[12];
91 if (sysctlbyname("net.bpf.stats", NULL, &size,
93 warn("net.bpf.stats");
100 warn("malloc failed");
103 if (sysctlbyname("net.bpf.stats", bd, &size,
105 warn("net.bpf.stats");
109 printf("%5s %6s %6s %9s %9s %9s %5s %5s %s\n",
110 "Pid", "Netif", "Flags", "Recv", "Drop", "Match", "Sblen",
112 for (d = &bd[0]; d < &bd[size / sizeof(*d)]; d++) {
113 if (interface && strcmp(interface, d->bd_ifname) != 0)
115 bpf_flags(d, flagbuf);
116 pname = bpf_pidname(d->bd_pid);
117 printf("%5d %6s %6s %9lu %9lu %9lu %5d %5d %s\n",
118 d->bd_pid, d->bd_ifname, flagbuf,
119 d->bd_rcount, d->bd_dcount, d->bd_fcount,
120 d->bd_slen, d->bd_hlen, pname);