1 /* $NetBSD: bpf.c,v 1.11 2012/12/14 08:15:44 msaitoh Exp $ */
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/sysctl.h>
44 #include <net/bpfdesc.h>
52 struct bpf_stat bpf_s
;
53 size_t len
= sizeof(bpf_s
);
56 if (sysctlbyname("net.bpf.stats", &bpf_s
, &len
, NULL
, 0) == -1)
57 err(1, "net.bpf.stats");
60 printf("\t%" PRIu64
" total packets received\n",
62 printf("\t%" PRIu64
" total packets captured\n",
64 printf("\t%" PRIu64
" total packets dropped\n",
67 warnx("BPF stats not available via KVM.");
72 bpf_dump(const char *bpfif
)
74 struct bpf_d_ext
*dpe
;
77 int name
[CTL_MAXNAME
], rc
;
83 /* adapted from sockstat.c by Andrew Brown */
86 if (sysctlnametomib("net.bpf.peers", &name
[0], &sz
) == -1)
87 err(1, "sysctlnametomib: net.bpf.peers");
90 name
[namelen
++] = sizeof(*dpe
);
91 name
[namelen
++] = INT_MAX
;
96 rc
= prog_sysctl(&name
[0], namelen
,
98 if (rc
== -1 && errno
!= ENOMEM
)
99 err(1, "sysctl: net.bpf.peers");
100 if (rc
== -1 && v
!= NULL
) {
114 puts("Active BPF peers\nPID\tInt\tRecv Drop Capt" \
115 " Flags Bufsize Comm");
117 #define BPFEXT(entry) dpe->entry
119 for (i
= 0; i
< (sz
/ sizeof(*dpe
)); i
++, dpe
++) {
121 strncmp(BPFEXT(bde_ifname
), bpfif
, IFNAMSIZ
))
124 printf("%-7d ", BPFEXT(bde_pid
));
126 (BPFEXT(bde_ifname
)[0] == '\0') ? "-" :
129 printf("%-8" PRIu64
" %-8" PRIu64
" %-8" PRIu64
" ",
130 BPFEXT(bde_rcount
), BPFEXT(bde_dcount
),
133 switch (BPFEXT(bde_state
)) {
148 printf("%c", BPFEXT(bde_promisc
) ? 'P' : '-');
149 printf("%c", BPFEXT(bde_immediate
) ? 'R' : '-');
150 printf("%c", BPFEXT(bde_seesent
) ? 'S' : '-');
151 printf("%c", BPFEXT(bde_hdrcmplt
) ? 'H' : '-');
152 printf(" %-8d ", BPFEXT(bde_bufsize
));
156 name
[namelen
++] = CTL_KERN
;
157 name
[namelen
++] = KERN_PROC2
;
158 name
[namelen
++] = KERN_PROC_PID
;
159 name
[namelen
++] = BPFEXT(bde_pid
);
160 name
[namelen
++] = szproc
;
163 if (prog_sysctl(&name
[0], namelen
, &p
, &szproc
,
167 printf("%s\n", p
.p_comm
);
173 errx(1, "bpf_dump not implemented using kvm");