我们正在尝试使用tcpreplay通过10 GbE连接重播pcap文件(smallFlows.pcap),并捕获所有数据包,记录源和目标端口/IP地址。然而,存在显著的数据包丢失。在3 Gbps的速度下,我们丢失了大约15%的发送数据包。即使在1 Gbps的速度下,我们也会损失7%。我们的嗅探器程序是使用netmap-libpcap用C编写的,是sniffx.C.的修改版本
我们在测试时删除了所有打印语句。我们尝试更改快照长度和缓冲区大小,但这只是略微提高了数据包丢失率。我们还将发送器和接收器上的CPU核心设置为性能模式,以最大限度地提高时钟速度(接收器上约为2.67 GHz),但这没有影响。根据top的说法,CPU使用率相当低,大约为15%。
接收器采用英特尔酷睿i7处理器。发送方运行Ubuntu 12.04.3 LTS(linux内核3.8.13),接收方运行Ubuntu 12.04(linux内核3.2.0-23-generic)。
我们能做些什么来确保接收到所有的数据包?
以下是主要功能:
int main(int argc, char **argv)
{
char *dev = NULL; /* capture device name */
char errbuf[PCAP_ERRBUF_SIZE]; /* error buffer */
pcap_t *handle; /* packet capture handle */
char filter_exp[] = "ip"; /* filter expression [3] */
bpf_u_int32 mask; /* subnet mask */
bpf_u_int32 net; /* ip */
int num_packets = 10; /* number of packets to capture */
print_app_banner();
printf(pcap_lib_version());
/* check for capture device name on command-line */
if (argc == 2) {
dev = argv[1];
}
else if (argc > 2) {
fprintf(stderr, "error: unrecognized command-line optionsnn");
print_app_usage();
exit(EXIT_FAILURE);
}
else {
/* find a capture device if not specified on command-line */
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
fprintf(stderr, "Couldn't find default device: %sn",
errbuf);
exit(EXIT_FAILURE);
}
}
/* get network number and mask associated with capture device */
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Couldn't get netmask for device %s: %sn",
dev, errbuf);
net = 0;
mask = 0;
}
/* print capture info */
printf("Device: %sn", dev);
printf("Number of packets: %dn", num_packets);
printf("Filter expression: %sn", filter_exp);
/* open capture device */
//handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
handle = pcap_create(dev, errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %sn", dev, errbuf);
exit(EXIT_FAILURE);
}
pcap_set_snaplen(handle, 1518);
pcap_set_promisc(handle, 1);
pcap_set_timeout(handle, 1000);
pcap_set_buffer_size(handle, 20971520);
pcap_activate(handle);
/* make sure we're capturing on an Ethernet device [2] */
if (pcap_datalink(handle) != DLT_EN10MB) {
fprintf(stderr, "%s is not an Ethernetn", dev);
exit(EXIT_FAILURE);
}
/* now we can set our callback function */
pcap_loop(handle, 0/*num_packets*/, got_packet, NULL);
/* cleanup */
pcap_close(handle);
printf("nCapture complete.n");
return 0;
}
以下是pcap_loop()调用的数据包处理程序代码:
/*
* dissect packet
*/
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
static int count = 1; /* packet counter */
/* declare pointers to packet headers */
const struct sniff_ethernet *ethernet; /* The ethernet header [1] */
const struct sniff_ip *ip; /* The IP header */
const struct sniff_tcp *tcp; /* The TCP header */
const char *payload; /* Packet payload */
int size_ip;
int size_tcp;
int size_payload;
//printf("nPacket number %d:n", count);
count++;
//if(count >= 2852200)
printf("count: %dn", count);
/* define ethernet header */
ethernet = (struct sniff_ethernet*)(packet);
/* define/compute ip header offset */
ip = (struct sniff_ip*)(packet + SIZE_ETHERNET);
size_ip = IP_HL(ip)*4;
if (size_ip < 20) {
//printf(" * Invalid IP header length: %u bytesn", size_ip);
return;
}
/* define/compute tcp header offset */
tcp = (struct sniff_tcp*)(packet + SIZE_ETHERNET + size_ip);
size_tcp = TH_OFF(tcp)*4;
/* compute tcp payload (segment) size */
size_payload = ntohs(ip->ip_len) - (size_ip + size_tcp);
return;
}
谢谢你的帮助。
CPU使用情况如何?它是单个核心的15%还是所有核心的15%?如果它是所有核心的15%,而你有8个核心,那么它实际上超过了单个核心的100%。因此,这可以解释为什么单线程应用程序无法捕获所有数据包。
如果您无法使用pcap库接收所有数据包,那么除了尝试使用另一种数据包接收机制之外,别无选择。Linux有PF_PACKET套接字,这可能对您的情况有所帮助。根据这个答案:libpcap还是PF_PACKET。。。libpcap应该优先于PF_PACKET,因为libpcap更具可移植性,并且在内部使用PF_PACKET的内存映射机制,这很难使用。
根据答案,libpcap使用PF_PACKET的内存映射机制。您可以尝试在非内存映射模式下手动使用PF_PACKET,这样您的数据包访问机制就会有所不同。如果内存映射模式中有错误,可能会导致数据包丢失。
你试过用tcpdump记录数据包捕获吗?Tcpdump在内部使用libpcap,因此,如果Tcpdump能够捕获所有数据包,而您的软件却无法捕获,那么它就证明错误存在于您的软件中,并且它不是libpcap的固有限制。