Date: Fri, 24 Jul 2009 03:45:56 GMT From: Gabor Pali <pgj@FreeBSD.org> To: Perforce Change Reviews <perforce@FreeBSD.org> Subject: PERFORCE change 166485 for review Message-ID: <200907240345.n6O3ju0S034444@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=166485 Change 166485 by pgj@petymeg-current on 2009/07/24 03:45:11 Add net.bpf.sstats, a streamed version of net.bpf.stats (as a read-only variable). This will be used by libnetstat(3) for live monitoring. Affected files ... .. //depot/projects/soc2009/pgj_libstat/src/sys/net/bpf.c#3 edit .. //depot/projects/soc2009/pgj_libstat/src/sys/net/bpfdesc.h#2 edit Differences ... ==== //depot/projects/soc2009/pgj_libstat/src/sys/net/bpf.c#3 (text+ko) ==== @@ -48,6 +48,7 @@ #include <sys/fcntl.h> #include <sys/malloc.h> #include <sys/mbuf.h> +#include <sys/sbuf.h> #include <sys/time.h> #include <sys/priv.h> #include <sys/proc.h> @@ -119,6 +120,7 @@ static int filt_bpfread(struct knote *, long); static void bpf_drvinit(void *); static int bpf_stats_sysctl(SYSCTL_HANDLER_ARGS); +static int bpf_sstats_sysctl(SYSCTL_HANDLER_ARGS); SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl"); int bpf_maxinsns = BPF_MAXINSNS; @@ -129,6 +131,9 @@ &bpf_zerocopy_enable, 0, "Enable new zero-copy BPF buffer sessions"); SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_MPSAFE | CTLFLAG_RW, bpf_stats_sysctl, "bpf statistics portal"); +SYSCTL_PROC(_net_bpf, OID_AUTO, sstats, CTLFLAG_RD | CTLTYPE_STRUCT, + 0, 0, bpf_sstats_sysctl, "s,struct bpfd_data", + "bpf statistics portal (streamed)"); static d_open_t bpfopen; static d_read_t bpfread; @@ -2108,6 +2113,87 @@ return (error); } +static int +bpf_sstats_sysctl(SYSCTL_HANDLER_ARGS) +{ + int error, i, buflen; + char *buffer; + struct bpf_if *bp; + struct bpf_d *bd, **bd_list; + + struct bpfd_stream bps; + struct bpfd_data bpd; + struct sbuf sbuf; + + error = priv_check(req->td, PRIV_NET_BPF); + if (error != 0) + return (error); + + bzero(&bps, sizeof(bps)); + bps.bps_version = BPFD_STREAM_VERSION; + bps.bps_count = bpf_bpfd_cnt; + + bd_list = malloc(bps.bps_count * sizeof(*bd_list), M_TEMP, M_WAITOK); + if (bd_list == NULL) + return (ENOMEM); + + i = 0; + mtx_lock(&bpf_mtx); + LIST_FOREACH(bp, &bpf_iflist, bif_next) { + BPFIF_LOCK(bp); + LIST_FOREACH(bd, &bp->bif_dlist, bd_next) { + BPFD_LOCK(bd); + bd_list[i++] = bd; + BPFD_UNLOCK(bd); + } + BPFIF_UNLOCK(bp); + } + mtx_unlock(&bpf_mtx); + bps.bps_count = i; + + buflen = sizeof(bps) + bps.bps_count * sizeof(bpd) + 1; + buffer = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO); + sbuf_new(&sbuf, buffer, buflen, SBUF_FIXEDLEN); + + if (sbuf_bcat(&sbuf, &bps, sizeof(bps)) < 0) { + error = ENOMEM; + goto out; + } + + for (i = 0; i < bps.bps_count; i++) { + bd = bd_list[i]; + bzero(&bpd, sizeof(bpd)); + BPFD_LOCK(bd); + bpd.bpd_promisc = bd->bd_promisc; + bpd.bpd_immediate = bd->bd_immediate; + bpd.bpd_hdrcmplt = bd->bd_hdrcmplt; + bpd.bpd_direction = bd->bd_direction; + bpd.bpd_feedback = bd->bd_feedback; + bpd.bpd_async = bd->bd_async; + bpd.bpd_locked = bd->bd_locked; + bpd.bpd_slen = bd->bd_slen; + bpd.bpd_hlen = bd->bd_hlen; + bpd.bpd_rcount = bd->bd_rcount; + bpd.bpd_dcount = bd->bd_dcount; + bpd.bpd_fcount = bd->bd_fcount; + bpd.bpd_pid = bd->bd_pid; + bcopy(bd->bd_bif->bif_ifp->if_xname, bpd.bpd_ifname, + sizeof(bpd.bpd_ifname)); + BPFD_UNLOCK(bd); + if (sbuf_bcat(&sbuf, &bpd, sizeof(bpd)) < 0) { + error = ENOMEM; + goto out; + } + } + + sbuf_finish(&sbuf); + error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf)); +out: + free(bd_list, M_TEMP); + free(buffer, M_TEMP); + return (error); +} + SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL); #else /* !DEV_BPF && !NETGRAPH_BPF */ ==== //depot/projects/soc2009/pgj_libstat/src/sys/net/bpfdesc.h#2 (text+ko) ==== @@ -147,4 +147,34 @@ #define BPFIF_LOCK(bif) mtx_lock(&(bif)->bif_mtx) #define BPFIF_UNLOCK(bif) mtx_unlock(&(bif)->bif_mtx) +/* + * Structures for exporting BPF statistics for user-space monitoring tools. + */ + +#define BPFD_STREAM_VERSION 0x00000001 + +struct bpfd_stream { + u_int32_t bps_version; + u_int64_t bps_count; + u_int8_t _bps_pad[4]; +}; + +struct bpfd_data { + u_int8_t bpd_promisc; + u_int8_t bpd_immediate; + u_int8_t bpd_hdrcmplt; + u_int8_t bpd_direction; + u_int8_t bpd_feedback; + u_int8_t bpd_async; + u_int8_t bpd_locked; + u_int32_t bpd_slen; + u_int32_t bpd_hlen; + u_int64_t bpd_rcount; + u_int64_t bpd_dcount; + u_int64_t bpd_fcount; + u_int32_t bpd_pid; + char bpd_ifname[16]; /* IFNAMSIZ */ + u_int8_t _bpd_pad[5]; +}; + #endif
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200907240345.n6O3ju0S034444>