我正在开发一个程序,它使用原始套接字(AF_PACKET,SOCK_RAW)嗅探网络数据包,并以某种方式处理它们。
我不确定我的程序是否运行得足够快,是否成功捕获套接字上的所有数据包。我担心这个套接字的接收缓冲区有时会被填满(由于业务突发),一些数据包会被丢弃。
如何知道数据包是否由于套接字的接收缓冲区中缺少空间而被丢弃?
我试过运行ss -f link -nlp
。
这将输出当前存储在该套接字stored缓冲区中的字节数,但我无法判断是否有任何数据包被丢弃。
我使用的是Ubuntu14.04.2LTS(GNU/Linux3.13.0-52-泛型x86_64)。
发布于 2015-06-10 14:37:53
从netstat,ethtool输出可以看到丢弃的数据包。对于UDP数据包掉话,检查'netstat -us'
的输出。数据包也被丢弃在网卡层本身,这可以通过'ethtool -S <device_name>'
看到。
实例产出:
$ netstat -us
IcmpMsg:
InType3: 44
InType8: 5
InType13: 1
InType17: 3
InType37: 1
OutType0: 5
OutType3: 599
OutType8: 4
OutType14: 1
Udp:
86942 packets received
209 packets to unknown port received.
**0 packet receive errors** <== This indicates packets dropped due to socket buffer full
213901 packets sent
UdpLite:
IpExt:
InOctets: 38683476091
OutOctets: 959938111
尝试使用更大的缓冲区空间(SO_RCVBUF),并通过sysctl控件net.core.rmem_max最大限度地增加系统宽度。
在网卡层,您也可以尝试增加环形缓冲区来处理突发流量(通过ethtool -g
检查设置)。
发布于 2019-12-11 23:24:03
您可以使用PACKET_STATISTICS套接字选项检索程序中接收到的数据包总数和丢弃数据包的数量,如包(7)手册中所述。
#include <linux/if_packet.h>
#include <sys/socket.h>
#include <sys/types.h>
...
struct tpacket_stats lStats = {};
socklen_t lStatsLength = sizeof( lStats );
if ( getsockopt( mRawSocket, SOL_PACKET, PACKET_STATISTICS, &lStats, &lStatsLength ) == 0 )
{
printf( "Total Packets: %u\nDropped Packets: %u\n", lStats.tp_packets, lStats.tp_drops );
}
else
{
perror( "Failed to get network receive statistics" );
}
https://unix.stackexchange.com/questions/208441
复制相似问题