ARTICLE · 1279505
[程序员] 有些信息就是需要看源码才能知道
[程序员] 有些信息就是需要看源码才能知道这是人类比不过AI的一个原因。一个人哪有那么多精力看完所有的代码?但是AI可以。 最近在看libpcap/tcpdump的源代码,发现tcpdump在verbose的状态下也会把内核在interface-level丢包的数据打印出来,但是,是在有丢包的情况下发生的时候才会打印,如果丢包数为0,就没有必要输出了。 而tcpdump的使用手册没有提这么一个信息。
https://www.tcpdump.org/manpages/tcpdump.1.html
通篇搜drop关键字,只有本身用来抓取的socket的丢包才会被,在最后停止tcpdump的时候打印出来。
packets ``dropped by kernel'' (this is the number of packets that were dropped, due to a lack of buffer space, by the packet capture mechanism in the OS on whichtcpdumpis running, if the OS reports that information to applications; if not, it will be reported as 0).
https://www.tcpdump.org/manpages/tcpdump.1.html
是在libpcap的代码里记的一个数:
do {read_ret = recv(handle->fd, handle->buffer, handle->bufsize, 0);if (handle->break_loop) {handle->break_loop = 0;return PCAP_ERROR_BREAK;}if (read_ret == -1 && errno == ENOBUFS)handlep->packets_nobufs++;} while ((read_ret == -1) && (errno == EINTR || errno == ENOBUFS));
另外一个抓取的interface上的丢包,如果是0,不打印,如果大于0才打印:
if (stats.ps_ifdrop != 0) {if (!verbose)fputs(", ", stderr);elseputc('\n', stderr);(void)fprintf(stderr, "%u packet%s dropped by interface\n",stats.ps_ifdrop, PLURAL_SUFFIX(stats.ps_ifdrop));} elseputc('\n', stderr);
而这个ifdrop的具体要看libpcap的代码:
staticlonglongintlinux_if_drops(constchar * if_name){long long int missed = linux_get_stat(if_name, "rx_missed_errors");long long int fifo = linux_get_stat(if_name, "rx_fifo_errors");return missed + fifo;}....if_dropped = handlep->sysfs_dropped;handlep->sysfs_dropped = linux_if_drops(handlep->device);handlep->stat.ps_ifdrop += (u_int)(handlep->sysfs_dropped - if_dropped);....
而丢包是通过读取interface相关的sys文件来获取,分别是:
rx_missed_errors和rx_fifo_errors。














































































