1 /* $NetBSD: bpf.c,v 1.8 2008/04/28 20:24:14 martin 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>
51 struct bpf_stat bpf_s
;
52 size_t len
= sizeof(bpf_s
);
55 if (sysctlbyname("net.bpf.stats", &bpf_s
, &len
, NULL
, 0) == -1)
56 err(1, "net.bpf.stats");
59 printf("\t%" PRIu64
" total packets received\n",
61 printf("\t%" PRIu64
" total packets captured\n",
63 printf("\t%" PRIu64
" total packets dropped\n",
66 warnx("BPF stats not available via KVM.");
71 bpf_dump(const char *bpfif
)
73 struct bpf_d_ext
*dpe
;
76 int name
[CTL_MAXNAME
], rc
;
82 /* adapted from sockstat.c by Andrew Brown */
85 if (sysctlnametomib("net.bpf.peers", &name
[0], &sz
) == -1)
86 err(1, "sysctlnametomib: net.bpf.peers");
89 name
[namelen
++] = sizeof(*dpe
);
90 name
[namelen
++] = INT_MAX
;
95 rc
= sysctl(&name
[0], namelen
, v
, &sz
, NULL
, 0);
96 if (rc
== -1 && errno
!= ENOMEM
)
97 err(1, "sysctl: net.bpf.peers");
98 if (rc
== -1 && v
!= NULL
) {
112 puts("Active BPF peers\nPID\tInt\tRecv Drop Capt" \
113 " Flags Bufsize Comm");
115 #define BPFEXT(entry) dpe->entry
117 for (i
= 0; i
< (sz
/ sizeof(*dpe
)); i
++, dpe
++) {
119 strncmp(BPFEXT(bde_ifname
), bpfif
, IFNAMSIZ
))
122 printf("%-7d ", BPFEXT(bde_pid
));
124 (BPFEXT(bde_ifname
)[0] == '\0') ? "-" :
127 printf("%-8" PRIu64
" %-8" PRIu64
" %-8" PRIu64
" ",
128 BPFEXT(bde_rcount
), BPFEXT(bde_dcount
),
131 switch (BPFEXT(bde_state
)) {
146 printf("%c", BPFEXT(bde_promisc
) ? 'P' : '-');
147 printf("%c", BPFEXT(bde_immediate
) ? 'R' : '-');
148 printf("%c", BPFEXT(bde_seesent
) ? 'S' : '-');
149 printf("%c", BPFEXT(bde_hdrcmplt
) ? 'H' : '-');
150 printf(" %-8d ", BPFEXT(bde_bufsize
));
154 name
[namelen
++] = CTL_KERN
;
155 name
[namelen
++] = KERN_PROC2
;
156 name
[namelen
++] = KERN_PROC_PID
;
157 name
[namelen
++] = BPFEXT(bde_pid
);
158 name
[namelen
++] = szproc
;
161 if (sysctl(&name
[0], namelen
, &p
, &szproc
,
165 printf("%s\n", p
.p_comm
);
170 errx(1, "bpf_dump not implemented using kvm");