本文主要是介绍[总结] 浅谈渐进式优化LinuxPcap抓包历程(部分附代码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 前言
- 历程
- 使用阻塞式pcap_loop
- 使用非阻塞式pcap_dispatch
- 优化1多网卡名创建一个pcap实例
- 优化2使用select异步监听fd
- 总结
前言
最近一直在做pcap抓包,需求很简单就是指定一个或多个bpf语句抓计算机上所有网卡。
不考虑性能开销和资源开销的话,实现方式很多。
历程
使用阻塞式pcap_loop
不考虑性能开销和资源开销的话的实现方式很多。
浅谈最简单的一种:
- 使用单线程阻塞式的pcap_loop,一个线程抓一个网卡和一种bpf语句。
- 优点:实现简单。
- 缺点:资源开销大,线程数 = bpf数量 * 网卡数。
大致实现:
那么如何减少线程数量呢?
那就不得不提pcap_dispatch方法
使用非阻塞式pcap_dispatch
pcap_dispatch函数是非阻塞的,即可以使用循环的方式轮询查找是否捕获到数据包。
思路:
- 根据bpf和网卡创建pcap实例
- 所有pcap都放在一个线程使用轮询方式查找
- 抓包成功会自动调用回调
- 优化:线程数量大幅度减少,
- 缺点:1.线程数 = bpf数量 2.需要创建大量网卡数量的pcap实例
优化1多网卡名创建一个pcap实例
利用pcap创建时device_name=“any”,使之多个网卡对应一个pcap实例
思路:
- 根据bpf和网卡创建pcap实例
- pcap都放在一个线程使用轮询方式查找
- 抓包成功会自动调用回调
- 优化:多个网卡对应一个pcap实例,减少了pcap实例数量
- 缺点:
- 线程数 = bpf数量,pcap实例数=bpf数量,
- pcap_callback回调函数需要特殊解析数据包,使用any设备名,数据包结构会发生变化,需要特殊调整和特别解析。
- 多实例放在一个循环中可能会造成超时,影响后一个pcap实例的延迟处理
优化2使用select异步监听fd
利用pcap_get_select_fd获取socket fd,再使用select函数监听处理
思路:
- 根据不同的bpf和多个网卡创建pcap实例
- bpf数量的pcap都放在一个线程使用轮询方式查找
- 抓包成功会自动调用回调
- 优化:
- 线程数只有1,pcap实例数=bpf数量
- select去除了非响应的pcap实例,减少了pcap实例的超时造成的延迟影响
- 如果使用多个网卡创建多个pcap的方法影响也几乎没有影响 并且callback不需要特殊解析数据包
- 缺点:几乎没有
- 优化:
#include <mutex>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <list>
#include <queue>
#include <string>
#include <sstream>
#include <chrono>#include <sys/time.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netinet/ip.h>
#include <string.h>
#include <dirent.h>#include <netinet/tcp.h>
#include <netinet/ip_icmp.h>
#include <netinet/ether.h>#include <pcap.h>void PcapCallback(u_char* argument, const struct pcap_pkthdr* packet_header, const u_char* packet_content) {// to do something
}pcap_t * create_pcap(const char * dev, const std::string &str_filter_exp, std::mutex *mutex){char errbuf[PCAP_ERRBUF_SIZE]; // 存储错误信息的缓冲区int snaplen = 65535; // 捕获数据包的长度int promisc = 0; // 混杂模式int to_ms = 1000; // 等待捕获的超时时间// 打开捕获设备pcap_t* handle = pcap_create(dev, errbuf);if (handle == nullptr) {printf("Could not create pcap handle: %s\n", errbuf);return nullptr;}struct bpf_program filter;pcap_set_snaplen(handle, snaplen);pcap_set_promisc(handle, promisc);pcap_set_timeout(handle, to_ms);pcap_compile(handle, &filter, str_filter_exp.c_str(), 1, 0);pcap_setfilter(handle, &filter);pcap_set_buffer_size(handle, 20 * 1024 * 1024);// pcap_set_immediate_mode(handle, 1); // 开启immediate模式if (pcap_activate(handle) != 0) {printf("Could not activate pcap handle: %s\n", pcap_geterr(handle));pcap_close(handle);return nullptr;}return handle;
}int main() {char errbuf[PCAP_ERRBUF_SIZE];pcap_if_t* alldevs;pcap_if_t* device;// 获取系统上的所有网络设备if (pcap_findalldevs(&alldevs, errbuf) == -1) {printf("Error finding devices: %s", errbuf);return 1;}std::vector<int> fds;std::vector<pcap *> pds;for (device = alldevs; device != nullptr; device = device->next) {for(pcap_addr_t *a=device->addresses; a!=NULL; a=a->next) {if(a->addr->sa_family == AF_INET){char* sz_ip_addr = inet_ntoa(((struct sockaddr_in*)a->addr)->sin_addr);std::string str_bpf_str = "tcp";pcap_t* pcap = create_pcap(device->name, str_filter_exp, nullptr);int fd = pcap_get_selectable_fd(pcap);fds.push_back(fd);pds.push_back(pcap);}}}// 释放设备列表pcap_freealldevs(alldevs);while (true){fd_set read_fds;FD_ZERO(&read_fds);int max_fd = 0;for (auto &one : fds){if (one > max_fd){max_fd = one;}FD_SET(one, &read_fds);}struct timeval timeout;timeout.tv_sec = 5;timeout.tv_usec = 0;int result = select(max_fd + 1, &read_fds, NULL, NULL, &timeout);if (result == -1){printf("error: select");break;}else if (result > 0){std::vector<std::size_t> pds_indexs;for (std::size_t i = 0; i < fds.size(); i++){if (FD_ISSET(fds[i], &read_fds)){pds_indexs.emplace_back(i);}}for (std::size_t id = 0; id < pds_indexs.size(); id++){std::size_t i = pds_indexs[id];//set_ns(containers[i].pid);int status =pcap_dispatch(pds[i], -1, PcapCallback, nullptr);if (status < 0)break;}}else{printf("Timeout!\n");}}for (auto &one : pds){pcap_close(one);}return 0;
}
总结
优化过程很长,主要考虑是线程数量的优化,以及使用非阻塞函数pcap_dispatch过程中产生pcap实例延迟问题。
记下此文希望大家都能掌握这些技巧。
这篇关于[总结] 浅谈渐进式优化LinuxPcap抓包历程(部分附代码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!