From: guy Date: Thu, 24 Nov 2005 19:27:42 +0000 (+0000) Subject: Don't double-count received packets on Linux systems that support the X-Git-Tag: libpcap-1.1.0~563 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/8fe5fce9e47e2520d99c00dacb0eb94134d7aff9 Don't double-count received packets on Linux systems that support the PACKET_STATISTICS getsockopt() argument on PF_PACKET sockets. --- diff --git a/pcap-int.h b/pcap-int.h index 927510e4..49fa3ef5 100644 --- a/pcap-int.h +++ b/pcap-int.h @@ -30,7 +30,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#) $Header: /tcpdump/master/libpcap/pcap-int.h,v 1.75 2005-07-07 06:55:20 guy Exp $ (LBL) + * @(#) $Header: /tcpdump/master/libpcap/pcap-int.h,v 1.76 2005-11-24 19:27:42 guy Exp $ (LBL) */ #ifndef pcap_int_h @@ -88,6 +88,7 @@ struct pcap_md { int ifindex; /* interface index of device we're bound to */ int lo_ifindex; /* interface index of the loopback device */ struct pcap *next; /* list of open promiscuous sock_packet pcaps */ + u_int packets_read; /* count of packets read with recvfrom() */ #endif #ifdef HAVE_DAG_API diff --git a/pcap-linux.c b/pcap-linux.c index c66ed986..219851ad 100644 --- a/pcap-linux.c +++ b/pcap-linux.c @@ -27,7 +27,7 @@ #ifndef lint static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.117 2005-10-08 11:30:26 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.118 2005-11-24 19:27:42 guy Exp $ (LBL)"; #endif /* @@ -704,8 +704,18 @@ pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata) * here, but it's not clear that always incrementing * the count is more expensive than always testing a flag * in memory. + * + * We keep the count in "md.packets_read", and use that for + * "ps_recv" if we can't get the statistics from the kernel. + * We do that because, if we *can* get the statistics from + * the kernel, we use "md.stat.ps_recv" and "md.stat.ps_drop" + * as running counts, as reading the statistics from the + * kernel resets the kernel statistics, and if we directly + * increment "md.stat.ps_recv" here, that means it will + * count packets *twice* on systems where we can get kernel + * statistics - once here, and once in pcap_stats_linux(). */ - handle->md.stat.ps_recv++; + handle->md.packets_read++; /* Call the user supplied callback function */ callback(userdata, &pcap_header, bp); @@ -853,8 +863,14 @@ pcap_stats_linux(pcap_t *handle, struct pcap_stat *stats) * * "ps_recv" doesn't include packets not yet read from * the kernel by libpcap. + * + * We maintain the count of packets processed by libpcap in + * "md.packets_read", for reasons described in the comment + * at the end of pcap_read_packet(). We have no idea how many + * packets were dropped. */ - *stats = handle->md.stat; + stats->ps_recv = handle->md.packets_read; + stats->ps_drop = 0; return 0; }