为什么 tcpdump 的默认捕获大小为 262144?

为什么 tcpdump 的默认捕获大小为 262144?

我想知道为什么 tcpdump 将魔术数字262144作为默认快照长度?

--snapshot-length=snaplen 从每个数据包中截取 snaplen 字节的数据,而不是默认的 262144 字节。由于快照有限而被截断的数据包在输出中用“[|proto]”表示,其中 proto 是发生截断的协议级别的名称。请注意,拍摄较大的快照会增加处理数据包所需的时间,并且实际上会减少数据包缓冲量。这可能会导致数据包丢失。还请注意,拍摄较小的快照将丢弃传输层上方协议的数据,从而丢失可能很重要的信息。例如,NFS 和 AFS 请求和回复非常大,如果选择的快照长度太短,则许多详细信息将不可用。如果您需要将快照大小减小到默认值以下,则应将 snaplen 限制为可捕获您感兴趣的协议信息的最小数字。将 snaplen 设置为 0 会将其设置为默认值 262144,以便与最近的旧版本的 tcpdump 保持向后兼容。

参考:手册页

答案1

https://github.com/the-tcpdump-group/tcpdump/blob/tcpdump-4.9/netdissect.h

/*
 * Maximum snapshot length.  This should be enough to capture the full
 * packet on most network interfaces.
 *
 *
 * Somewhat arbitrary, but chosen to be:
 *
 *    1) big enough for maximum-size Linux loopback packets (65549)
 *       and some USB packets captured with USBPcap:
 *
 *           http://desowin.org/usbpcap/
 *
 *       (> 131072, < 262144)
 *
 * and
 *
 *    2) small enough not to cause attempts to allocate huge amounts of
 *       memory; some applications might use the snapshot length in a
 *       savefile header to control the size of the buffer they allocate,
 *       so a size of, say, 2^31-1 might not work well.
 *
 * XXX - does it need to be bigger still?
 */
#define MAXIMUM_SNAPLEN 262144

没什么大不了的。Linux 环回不受硬件帧的限制,因此最大大小相当大,为 64k。其他数据包可能更大,因此最多可达 2 的几个幂,即 256k。

相关内容