背景:dpdk接管网卡后,默认不响应arp信息,使用工具往网卡发送信息后,由于dpdk不响应arp请求,所以发送方无法根据ip找到mac地址,导致数据发送失败。
解决:该程序判断当前报文是针对本机的arp请求,则发送arp响应报文,使得发送方可以自动添加上对应的arp信息。
arp报文格式
当前arp中没有192.168.1.5 00-0c-29-f2-de-8d这条信息
使用udp往192.168.1.5发送数据成功
当前arp中新增一条192.168.1.5 00-0c-29-f2-de-8d,发送方自动添加arp信息成功
注:如果把代码中if (ahdr->arp_data.arp_tip == gLocalIp) 语句注释了,则会造成arp攻击,导致局域网内大量机器的arp信息中,ip对应的mac地址是错误的
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_mbuf.h>
#include <stdio.h>
#include <arpa/inet.h>
#define NUM_MBUFS (4096-1) // 内存池中 mbuf 的数量
#define BURST_SIZE 32
#define MAKE_IPV4_ADDR(a, b, c, d) (a + (b<<8) + (c<<16) + (d<<24))
static uint32_t gLocalIp = MAKE_IPV4_ADDR(192, 168, 1, 5);//当前ip,网络字节序
static uint32_t gSrcIp, gDstIp;
static uint8_t gSrcMac[RTE_ETHER_ADDR_LEN], gDstMac[RTE_ETHER_ADDR_LEN];
static uint16_t gSrcPort, gDstPort;
int gDpdkPortId = 0;
static const struct rte_eth_conf port_conf_default = {
.rxmode = {.max_rx_pkt_len = RTE_ETHER_MAX_LEN}
};
static void ng_init_port(struct rte_mempool *mbuf_pool) {
//dpdk绑定的网卡数量
uint16_t nb_sys_ports = rte_eth_dev_count_avail();
if (nb_sys_ports == 0) {
rte_exit(EXIT_FAILURE, "not support eth\n");
}
struct rte_eth_dev_info dev_info;
/*获取以太网设备的配置和状态信息。它通常用于初始化网络设备、
*配置网络设备或者获取网络设备的状态信息。
*这里的端口号和网卡是一一对应的
*/
rte_eth_dev_info_get(gDpdkPortId, &dev_info);
const int num_rx_queues = 1; //接收队列个数
const int num_tx_queues = 1; //发送队列个数
struct rte_eth_conf port_conf = port_conf_default;
rte_eth_dev_configure(gDpdkPortId, num_rx_queues, num_tx_queues, &port_conf);
// 0是0号接收队列
// 128是队列长度
if (rte_eth_rx_queue_setup(gDpdkPortId, 0, 128, rte_eth_dev_socket_id(gDpdkPortId), NULL, mbuf_pool) < 0) {
rte_exit(EXIT_FAILURE, "Could not setup RX queue\n");
}
struct rte_eth_txconf txq_conf = dev_info.default_txconf;
//offloads 成员是一个 64 位无符号整数,每个比特位表示不同的接收功能选项
txq_conf.offloads = port_conf.rxmode.offloads;
/* 0是0号发送队列
* 1024是队列长度
* 发送队列长度设置太小运行时会报错:Invalid value for nb_tx_desc(=128), should be: <= 4096, >= 512, and a product of 1
*/
if (rte_eth_tx_queue_setup(gDpdkPortId, 0, 1024, rte_eth_dev_socket_id(gDpdkPortId), &txq_conf) < 0) {
rte_exit(EXIT_FAILURE, "Could not setup TX queue\n");
}
if (rte_eth_dev_start(gDpdkPortId) < 0) {
rte_exit(EXIT_FAILURE, "Could not start\n");
}
}
static void ng_encode_udp_pkt(uint8_t *msg, unsigned char *data, uint16_t total_len) {
//设置以太网头
struct rte_ether_hdr *eth = (struct rte_ether_hdr *)msg;
rte_memcpy(eth->s_addr.addr_bytes, gSrcMac, RTE_ETHER_ADDR_LEN);
rte_memcpy(eth->d_addr.addr_bytes, gDstMac, RTE_ETHER_ADDR_LEN);
eth->ether_type = htons(RTE_ETHER_TYPE_IPV4);
//设置ipv4头
struct rte_ipv4_hdr *ip = (struct rte_ipv4_hdr *)(eth + 1);
ip->version_ihl = 0x45;
ip->type_of_service = 0;
ip->total_length = htons(total_len - sizeof(struct rte_ether_hdr));
ip->packet_id = 0;
ip->fragment_offset = 0;
ip->time_to_live = 64;
//dpdk中没有UDP类型的定义,使用内核的协议类型
ip->next_proto_id = IPPROTO_UDP;
ip->src_addr = gSrcIp;
ip->dst_addr = gDstIp;
//计算ip头部校验和时,先把该字段置为0(ip校验和只包括头部)
ip->hdr_checksum = 0;
ip->hdr_checksum = rte_ipv4_cksum(ip);
//设置udp头
struct rte_udp_hdr *udp = (struct rte_udp_hdr *)(ip + 1);
udp->src_port = gSrcPort;
udp->dst_port = gDstPort;
uint16_t udp_len = total_len - sizeof(struct rte_ether_hdr) - sizeof(struct rte_ipv4_hdr);
udp->dgram_len = htons(udp_len);
memcpy((uint8_t *)(udp + 1), data, udp_len - sizeof(struct rte_udp_hdr));
//计算udp校验和,udp校验位包括负载数据
udp->dgram_cksum = 0;
udp->dgram_cksum = rte_ipv4_udptcp_cksum(ip, udp);
}
static void ng_send_udp(struct rte_mempool *mbuf_pool, unsigned char *data, uint16_t length) {
const unsigned total_len = sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr) + length;
//从内存中申请一个mbuf
struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool);
if (mbuf == NULL) {
rte_exit(EXIT_FAILURE, "rte_pktmbuf_alloc\n");
}
mbuf->pkt_len = total_len;
mbuf->data_len = total_len;
//用于将数据包缓冲区(packet buffer)转换为指定类型的数据指针,也就是mbuf存储数据包的首地址
uint8_t *pktdata = rte_pktmbuf_mtod(mbuf, uint8_t*);
ng_encode_udp_pkt(pktdata, data, total_len);
rte_eth_tx_burst(gDpdkPortId, 0, &mbuf, 1);
rte_pktmbuf_free(mbuf);
}
static void ng_encode_arp_pkt(uint8_t *msg, uint8_t *dst_mac, uint32_t sip, uint32_t dip) {
//设置以太网头
struct rte_ether_hdr *eth = (struct rte_ether_hdr *)msg;
rte_memcpy(eth->s_addr.addr_bytes, &gSrcMac, RTE_ETHER_ADDR_LEN);
rte_memcpy(eth->d_addr.addr_bytes, dst_mac, RTE_ETHER_ADDR_LEN);
eth->ether_type = htons(RTE_ETHER_TYPE_ARP);
//设置arp报文
struct rte_arp_hdr *arp = (struct rte_arp_hdr *)(eth + 1);
arp->arp_hardware = htons(1);
arp->arp_protocol = htons(RTE_ETHER_TYPE_IPV4);
arp->arp_hlen = RTE_ETHER_ADDR_LEN;
arp->arp_plen = sizeof(uint32_t);
arp->arp_opcode = htons(RTE_ARP_OP_REPLY);
rte_memcpy(arp->arp_data.arp_sha.addr_bytes, &gSrcMac, RTE_ETHER_ADDR_LEN);
rte_memcpy(arp->arp_data.arp_tha.addr_bytes, dst_mac, RTE_ETHER_ADDR_LEN);
arp->arp_data.arp_sip = sip;
arp->arp_data.arp_tip= dip;
}
static void ng_send_arp(struct rte_mempool *mbuf_pool, uint8_t *dst_mac,
uint32_t sip, uint32_t dip) {
const unsigned total_len = sizeof(struct rte_ether_hdr) + sizeof(struct rte_arp_hdr);
//从内存中申请一个mbuf
struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool);
if (mbuf == NULL) {
rte_exit(EXIT_FAILURE, "rte_pktmbuf_alloc\n");
}
mbuf->pkt_len = total_len;
mbuf->data_len = total_len;
uint8_t *pktdata = rte_pktmbuf_mtod(mbuf, uint8_t*);
ng_encode_arp_pkt(pktdata, dst_mac, sip, dip);
rte_eth_tx_burst(gDpdkPortId, 0, &mbuf, 1);
rte_pktmbuf_free(mbuf);
}
int main(int argc, char *argv[]) {
/*dpdk初始化资源
*用于初始化 Environment Abstraction Layer (EAL)。EAL 是 DPDK 的一个核心组件,
*负责抽象和管理硬件和操作系统依赖性,使得上层应用可以在不同的硬件和操作系统上
*以统一的方式运行。
*/
if (rte_eal_init(argc, argv) < 0) {
rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
}
//内存池,接收的数据存在该内存池中
struct rte_mempool *mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NUM_MBUFS,
0, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
if (mbuf_pool == NULL) {
rte_exit(EXIT_FAILURE, "Could not create mbuf pool\n");
}
ng_init_port(mbuf_pool);
//获取dpdk绑定的网卡源mac
rte_eth_macaddr_get(gDpdkPortId, (struct rte_ether_addr *)gSrcMac);
while(1) {
struct rte_mbuf *mbufs[BURST_SIZE] = {0};
unsigned num_recvd = rte_eth_rx_burst(gDpdkPortId, 0, mbufs, BURST_SIZE);
if (num_recvd > BURST_SIZE) {
rte_exit(EXIT_FAILURE, "Error receive from eth\n");
}
unsigned int i = 0;
for (i = 0; i < num_recvd; i++) {
struct rte_ether_hdr *ehdr = rte_pktmbuf_mtod(mbufs[i], struct rte_ether_hdr*);
//arp是广播
if (ehdr->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {
struct rte_arp_hdr *ahdr = (struct rte_arp_hdr *)(ehdr + 1);
if (ahdr->arp_data.arp_tip == gLocalIp) {
ng_send_arp(mbuf_pool, ahdr->arp_data.arp_sha.addr_bytes,
ahdr->arp_data.arp_tip, ahdr->arp_data.arp_sip);
}
rte_pktmbuf_free(mbufs[i]);
continue;
}
if (ehdr->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
rte_pktmbuf_free(mbufs[i]);
continue;
}
struct rte_ipv4_hdr *iphdr = rte_pktmbuf_mtod_offset(mbufs[i], struct rte_ipv4_hdr*, sizeof(struct rte_ether_hdr));
if (iphdr->next_proto_id == IPPROTO_UDP) {
struct rte_udp_hdr *udphdr = (struct rte_udp_hdr *)(iphdr + 1);
//两个字节以上的变量是需要大小端转换
uint16_t length = ntohs(udphdr->dgram_len);
*((char*)udphdr + length) = '\0';
struct in_addr addr;
addr.s_addr = iphdr->src_addr;
printf("src: %s:%d, ", inet_ntoa(addr), ntohs(udphdr->src_port));
addr.s_addr = iphdr->dst_addr;
printf("dst: %s:%d, %s\n", inet_ntoa(addr), ntohs(udphdr->dst_port), (char *)(udphdr+1));
//构造发送数据包
rte_memcpy(gDstMac, ehdr->s_addr.addr_bytes, RTE_ETHER_ADDR_LEN);
gSrcIp = iphdr->dst_addr;
gDstIp = iphdr->src_addr;
gSrcPort = udphdr->dst_port;
gDstPort = udphdr->src_port;
//发送udp报文
ng_send_udp(mbuf_pool, (unsigned char *)(udphdr+1), length);
}
rte_pktmbuf_free(mbufs[i]);
}
}
return 0;
}