c++ - 如何得知 libpcap 的环形缓冲区是否已满

标签 c++ c networking pcap libpcap

我在项目中使用libpcap并使用函数pcap_set_buffer_size()来设置初始值。当环形缓冲区已满时,libpacp 统计数据显示大量数据包被丢弃。

如何获取有关环形缓冲区可用空间的信息?

最佳答案

来自 man pcap :

a size that's too small could mean that, if too many packets are being captured and the snapshot length doesn't limit the amount of data that's buffered, packets could be dropped if the buffer fills up before the application can read packets from it, while a size that's too large could use more non- pageable operating system memory than is necessary to prevent packets from being dropped

缓冲区的理想大小取决于用例。

您可以通过使用二分法来找到这个理想的尺寸:

  • 首先设置一个巨大但系统支持的大小
  • 使用 pcap_stats() 获取丢弃的数据包数量
  • 再次编译并运行。
  • 请注意是否丢失了数据包。


  • 然后设置为当前尺寸

  • 使用 pcap_stats() 获取丢弃的数据包数量
  • 再次编译并运行。
  • 请注意是否丢失了数据包。


  • 然后将大小设置为(大尺寸 + 当前尺寸)/2

  • 使用 pcap_stats() 获取丢弃的数据包数量
  • 再次编译并运行。
  • 请注意是否丢失了数据包。


  • ...

请注意,您可以通过使用 pcap_set_snaplen() 设置快照长度来减少缓冲区使用量

If, when capturing, you capture the entire contents of the packet, that requires more CPU time to copy the packet to your application, more disk and possibly network bandwidth to write the packet data to a file, and more disk space to save the packet. If you don't need the entire contents of the packet - for example, if you are only interested in the TCP headers of packets - you can set the "snapshot length" for the capture to an appropriate value. If the snapshot length is set to snaplen, and snaplen is less than the size of a packet that is captured, only the first snaplen bytes of that packet will be captured and provided as packet data

您可以使用shotpcap_snapshot()获取当前的snaplen

关于c++ - 如何得知 libpcap 的环形缓冲区是否已满,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57285445/

相关文章:

我可以根据平台包含不同的文件吗?

Android 应用程序确认互联网设施是否可用?

networking - 套接字性能飙升

c++ - 加载深度图产生错误的值

c# - 奇怪的 DllImport 行为(仅在 Unity3d 中)

c++ - 使用 old_pointer + offset 的 new_pointer 设置 shared_ptr

与 memcpy 连接

c++ - 停止 winapi 线程时的错误行为

c - 如何防止缓冲区溢出/数组溢出?

python - 使用 BPF 过滤器时 python 脚本的 CPU 使用率