Date: Fri, 24 Jul 2009 03:53:04 GMT From: Gabor Pali <pgj@FreeBSD.org> To: Perforce Change Reviews <perforce@FreeBSD.org> Subject: PERFORCE change 166487 for review Message-ID: <200907240353.n6O3r4uF035057@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=166487 Change 166487 by pgj@petymeg-current on 2009/07/24 03:52:18 Modify netstat(1) to use libnetstat(3) for displaying bpf(4) statistics. Since libnetstat(3) includes kvm(3) support, -M flag also works for -B now :) Affected files ... .. //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/bpf.c#3 edit .. //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/extern.h#9 edit .. //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/main.c#16 edit Differences ... ==== //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/bpf.c#3 (text+ko) ==== @@ -1,5 +1,6 @@ /*- * Copyright (c) 2005 Christian S.J. Peron + * Copyright (c) 2009 Gabor Pali * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,108 +29,85 @@ __FBSDID("$FreeBSD: src/usr.bin/netstat/bpf.c,v 1.11 2008/03/24 13:50:39 csjp Exp $"); #include <sys/types.h> -#include <sys/protosw.h> -#include <sys/socket.h> -#include <sys/socketvar.h> -#include <sys/sysctl.h> -#include <sys/param.h> -#include <sys/user.h> - -#include <net/if.h> -#include <net/if_var.h> -#include <net/bpf.h> -#include <net/bpfdesc.h> -#include <arpa/inet.h> #include <err.h> -#include <errno.h> -#include <stdint.h> +#include <kvm.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> -#include <unistd.h> - +#include <netstat.h> #include "extern.h" -/* print bpf stats */ - -static char * -bpf_pidname(pid_t pid) -{ - struct kinfo_proc newkp; - int error, mib[4]; - size_t size; - - mib[0] = CTL_KERN; - mib[1] = KERN_PROC; - mib[2] = KERN_PROC_PID; - mib[3] = pid; - size = sizeof(newkp); - error = sysctl(mib, 4, &newkp, &size, NULL, 0); - if (error < 0) { - warn("kern.proc.pid failed"); - return (strdup("??????")); - } - return (strdup(newkp.ki_comm)); -} - static void -bpf_flags(struct xbpf_d *bd, char *flagbuf) +bpf_flags(const struct bpf_type *btp, char *flagbuf) { + int flags; - *flagbuf++ = bd->bd_promisc ? 'p' : '-'; - *flagbuf++ = bd->bd_immediate ? 'i' : '-'; - *flagbuf++ = bd->bd_hdrcmplt ? '-' : 'f'; - *flagbuf++ = (bd->bd_direction == BPF_D_IN) ? '-' : - ((bd->bd_direction == BPF_D_OUT) ? 'o' : 's'); - *flagbuf++ = bd->bd_feedback ? 'b' : '-'; - *flagbuf++ = bd->bd_async ? 'a' : '-'; - *flagbuf++ = bd->bd_locked ? 'l' : '-'; + flags = netstat_bpt_get_flags(btp); + *flagbuf++ = flags & NETSTAT_BPF_PROMISC ? 'p' : '-'; + *flagbuf++ = flags & NETSTAT_BPF_IMMEDIATE ? 'i' : '-'; + *flagbuf++ = flags & NETSTAT_BPF_HDRCMPLT ? '-' : 'f'; + *flagbuf++ = (netstat_bpt_get_direction(btp) == bpfdir_In) ? '-' : + ((netstat_bpt_get_direction(btp) == bpfdir_Out) ? 'o' : 's'); + *flagbuf++ = flags & NETSTAT_BPF_FEEDBACK ? 'b' : '-'; + *flagbuf++ = flags & NETSTAT_BPF_ASYNC ? 'a' : '-'; + *flagbuf++ = flags & NETSTAT_BPF_LOCKED ? 'l' : '-'; *flagbuf++ = '\0'; } +/* print bpf stats */ void -bpf_stats(char *ifname) +bpf_stats(char *ifname, void *kvmd) { - struct xbpf_d *d, *bd; - char *pname, flagbuf[12]; - size_t size; + struct bpf_type_list *bptlp; + struct bpf_type_iterator *bptip = NULL; + + const struct bpf_type *bptp; + + int bpt_flags, error; + char flagbuf[12]; + kvm_t *kvm; + + kvm = (kvm_t *)kvmd; + bpt_flags = 0; + if (kvmd != NULL) + bpt_flags |= NETSTAT_BPF_KVM; - if (sysctlbyname("net.bpf.stats", NULL, &size, - NULL, 0) < 0) { - warn("net.bpf.stats"); + bptlp = netstat_bptl_alloc(); + if (bptlp == NULL) { + warn("netstat_btl_alloc()"); return; } - if (size == 0) - return; - bd = malloc(size); - if (bd == NULL) { - warn("malloc failed"); - return; + + if (netstat_bpf(ifname, bptlp, bpt_flags, kvm)) { + error = netstat_bptl_geterror(bptlp); + if (error == NETSTAT_ERROR_KVM) { + warnx("netstat_bpf: %s", kvm_geterr(kvm)); + } else { + warnx("netstat_bpf: %s", netstat_strerror(error)); + } + goto out; } - if (sysctlbyname("net.bpf.stats", bd, &size, - NULL, 0) < 0) { - warn("net.bpf.stats"); - free(bd); - return; + + if (netstat_bpti_alloc(bptlp, &bptip) < 0) { + warn("netstat_bti_alloc()"); + goto out; } + (void) printf("%5s %6s %7s %9s %9s %9s %5s %5s %s\n", "Pid", "Netif", "Flags", "Recv", "Drop", "Match", "Sblen", "Hblen", "Command"); - for (d = &bd[0]; d < &bd[size / sizeof(*d)]; d++) { - if (d->bd_structsize != sizeof(*d)) { - warnx("bpf_stats_extended: version mismatch"); - return; - } - if (ifname && strcmp(ifname, d->bd_ifname) != 0) - continue; - bpf_flags(d, flagbuf); - pname = bpf_pidname(d->bd_pid); - (void) printf("%5d %6s %7s %9ju %9ju %9ju %5d %5d %s\n", - d->bd_pid, d->bd_ifname, flagbuf, - d->bd_rcount, d->bd_dcount, d->bd_fcount, - d->bd_slen, d->bd_hlen, pname); - free(pname); + for (bptp = netstat_bpti_first(bptip); bptp != NULL; + bptp = netstat_bpti_next(bptip)) { + bpf_flags(bptp, flagbuf); + (void) printf("%5d %6s %7s %9ju %9ju %9ju %5ju %5ju %s\n", + netstat_bpt_get_pid(bptp), netstat_bpt_get_ifname(bptp), + flagbuf, netstat_bpt_get_recv(bptp), + netstat_bpt_get_drop(bptp), netstat_bpt_get_match(bptp), + netstat_bpt_get_slen(bptp), netstat_bpt_get_hlen(bptp), + netstat_bpt_get_pidname(bptp)); } - free(bd); +out: + if (bptip != NULL) + netstat_bpti_free(bptip); + netstat_bptl_free(bptlp); } ==== //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/extern.h#9 (text+ko) ==== @@ -167,4 +167,4 @@ void mroutepr(u_long, u_long, u_long); void mrt_stats(u_long); -void bpf_stats(char *); +void bpf_stats(char *, void *); ==== //depot/projects/soc2009/pgj_libstat/src/usr.bin/netstat/main.c#16 (text+ko) ==== @@ -473,8 +473,8 @@ if (Bflag) { if (!live) - usage(); - bpf_stats(interface); + kread(0, NULL, 0); + bpf_stats(interface, kvmd); exit(0); } if (mflag) {
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200907240353.n6O3r4uF035057>