From owner-p4-projects@FreeBSD.ORG Fri Jul 24 03:53:05 2009 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id D0F5B1065670; Fri, 24 Jul 2009 03:53:04 +0000 (UTC) Delivered-To: perforce@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 90FA2106564A for ; Fri, 24 Jul 2009 03:53:04 +0000 (UTC) (envelope-from pgj@FreeBSD.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id 7DD498FC17 for ; Fri, 24 Jul 2009 03:53:04 +0000 (UTC) (envelope-from pgj@FreeBSD.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.3/8.14.3) with ESMTP id n6O3r4i2035059 for ; Fri, 24 Jul 2009 03:53:04 GMT (envelope-from pgj@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.3/8.14.3/Submit) id n6O3r4uF035057 for perforce@freebsd.org; Fri, 24 Jul 2009 03:53:04 GMT (envelope-from pgj@FreeBSD.org) Date: Fri, 24 Jul 2009 03:53:04 GMT Message-Id: <200907240353.n6O3r4uF035057@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to pgj@FreeBSD.org using -f From: Gabor Pali To: Perforce Change Reviews Cc: Subject: PERFORCE change 166487 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 24 Jul 2009 03:53:05 -0000 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 -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include #include -#include -#include +#include #include #include -#include -#include - +#include #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) {