用于在内核空间中捕获数据包的缓冲区大小?

Ano*_*non 17 networking kernel buffer tcpdump

查看 的手册页tcpdump,如果缓冲区已满,内核似乎可以丢弃数据包。我想知道是否:

  1. 该大小是可配置的和/或
  2. 我在哪里可以看到我的发行版的大小?

从手册页(为了便于参考):

“被内核丢弃”的数据包(这是由于缺少缓冲区空间而被运行 tcpdump 的操作系统中的数据包捕获机制丢弃的数据包数量,如果操作系统向应用程序报告该信息;如果不是,则报告为0)。

Pet*_*zel 27

Tcpdump 具有-B设置捕获缓冲区大小的选项。然后该值通过函数传递给 libpcap(tcpdump 用于执行实际数据包捕获的库)pcap_set_buffer_size()。Tcpdump 联机帮助页没有指定使用 -B 指定缓冲区大小的单位,但从源代码来看,它似乎是 KiB。

手册页pcap_set_buffer_size()没有指定默认缓冲区大小(如果未调用此函数时使用),但同样,从libpcap 源代码来看,这似乎是 2 MiB,至少在 linux 上(但很可能取决于系统)。

关于数据包的缓冲和丢弃,您还应该注意相应地设置 snaplen( -s) 参数。man tcpdump

-s     Snarf  snaplen bytes of data from each packet rather than the
default of 65535 bytes.  Packets truncated because of a limited snapshot
are indicated in the output with ``[|proto]'', where proto is the name of
the protocol level at which the truncation has occurred. Note that  taking
larger  snapshots both increases the amount of time it  takes  to
process packets and, effectively, decreases the amount of packet buffering.
This may cause packets to be lost. You should limit snaplen to the
smallest number that will capture the protocol information you're
interested in. Setting snaplen to 0 sets it to the default of 65535, for
back-wards compatibility with recent older versions of tcpdump.
Run Code Online (Sandbox Code Playgroud)

这意味着在固定缓冲区大小的情况下,您可以通过减小 snaplen 大小来增加适合缓冲区(因此不会被丢弃)的数据包数量。

  • 我知道这是一个旧线程(我喜欢这个答案),但是当您在 GitHub 上引用源时,请指向当前提交(因为 master 分支可以更改),例如:https://github.com/mcr/ tcpdump/blob/2c0a8eb4eb535eb1863e18497cd7dff54cc0d97e/tcpdump.c#L673。 (2认同)