1、当前不知道对端mac地址发送arp请求时:

1)以太网帧中目的mac是ff:ff:ff:ff:ff:ff

2)arp报文中的目的mac是00:00:00:00:00:00

dpdk 实现arptable_#include

2、arp探测报文,定时发送arp请求,判断arp是否更新

该报文是单播,且以太网帧中目的mac和arp中的目的mac都是arp表中记录的mac

以下代码以链表实现了一个arptable,启动一个定时器往外发送arp请求,将获取到的ip->mac映射存到arptable中。

#ifndef __NG_ARP_H__
#define __NG_ARP_H__

#include <rte_malloc.h>

#define ARY_ENTRY_STATUS_DYNAMIC 0
#define ARY_ENTRY_STATUS_STATIC 1

//链表头插法
#define LL_ADD(item, list) do {		\
	item->prev = NULL;				\
	item->next = list;				\
	if (list != NULL) list->prev = item; \
	list = item;					\
}while(0)

//链表删除节点
#define LL_REMOVE(item, list) do } { \
	if (item->prev != NULL) item->prev->next = item->next;	\
	if (item->next != NULL) item->next->pre = item->pre;	\
	if (list == item) list = item->next;	\
	item->prev = item->next = NULL;			\
}while(0)


struct arp_entry {
	uint32_t ip;	//网络字节序
	uint8_t hwadder[RTE_ETHER_ADDR_LEN];
	uint8_t type;

	struct arp_entry *next;
	struct arp_entry *prev;
};

struct arp_table {
	struct arp_entry *entries;
	int count;
};

static struct arp_table *arpt = NULL;
//该函数参数中不加void,会告警 function declaration isn’t a prototype(该函数声明不是原型)
static struct arp_table *arp_table_instance(void) {
	if (arpt == NULL) {
		arpt = rte_malloc("arp_table", sizeof(struct arp_table), 0);
		if (arpt == NULL) {
			rte_exit(EXIT_FAILURE, "rte_malloc arp table failed");
		}
		memset(arpt, 0, sizeof(struct arp_table));
	}

	return arpt;
}

// 根据ip查找mac
static uint8_t* ng_get_dst_macaddr(uint32_t dip) {
	struct arp_entry *iter = NULL;
	struct arp_table *table = arp_table_instance();

	for (iter = table->entries; iter != NULL; iter = iter->next) {
		if (dip == iter->ip) {
			return iter->hwadder;
		}
	}
	return NULL;
}

#endif
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_mbuf.h>
#include <rte_malloc.h>
#include <rte_timer.h>
#include <stdio.h>
#include <arpa/inet.h>
#include "arptable.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))
#define TIMER_RESOLUTION_CYCLES 20000000000ULL //10ms * 1000


static uint32_t gLocalIp = MAKE_IPV4_ADDR(192, 168, 1, 5);//当前ip,网络字节序

static uint8_t gSrcMac[RTE_ETHER_ADDR_LEN];

static uint8_t gDefaultArpMac[RTE_ETHER_ADDR_LEN] = {0x0};


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_arp_pkt(uint8_t *msg, uint16_t opcode, 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);
	if (memcpy(gDefaultArpMac, dst_mac, RTE_ETHER_ADDR_LEN) == 0) {

		uint8_t mac[RTE_ETHER_ADDR_LEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
		rte_memcpy(eth->d_addr.addr_bytes, mac, RTE_ETHER_ADDR_LEN);
	} else {
		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(opcode);
	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, uint16_t opcode, 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, opcode, dst_mac, sip, dip);

	rte_eth_tx_burst(gDpdkPortId, 0, &mbuf, 1);
	rte_pktmbuf_free(mbuf);
}

// tim是定时器, arg是传给定时器的参数
static void
arp_request_timer_cb(__attribute__((unused)) struct rte_timer *tim,
	  __attribute__((unused)) void *arg)
{
	puts("arp_request_timer_cb");
	struct rte_mempool *mbuf_pool = (struct rte_mempool *)arg;

	int i = 0;
	for(i = 0; i < 255; i++) {
		uint32_t dstip = (gLocalIp & 0x00FFFFFF) | (0xFF000000 & (i << 24));
		uint8_t *dstmac = ng_get_dst_macaddr(dstip);
			if (dstmac == NULL) {
				// arphdr中目的mac是FF:FF:FF:FF:FF:FF,以太网中目的mac是00:00:00:00:00:00
				ng_send_arp(mbuf_pool, RTE_ARP_OP_REQUEST, gDefaultArpMac, 
							gLocalIp, dstip);
			} else {
				ng_send_arp(mbuf_pool, RTE_ARP_OP_REQUEST, dstmac, 
							gLocalIp, dstip);
			}
		}
	
}

static void
print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
{
	char buf[RTE_ETHER_ADDR_FMT_SIZE];
	rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
	printf("%s%s", name, buf);
}


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);

	static struct rte_timer arp_timer;
	uint64_t prev_tsc = 0, cur_tsc, diff_tsc;

	/* init RTE timer library */
	rte_timer_subsystem_init();
	
	/* init timer structures */
	rte_timer_init(&arp_timer);
	
	/* load timer0, every second, on master lcore, reloaded automatically */
	uint64_t hz = rte_get_timer_hz();
	unsigned lcore_id = rte_lcore_id();
	//PERIODICAL表示重复触发,SINGLE是单次触发
	rte_timer_reset(&arp_timer, hz, PERIODICAL, lcore_id, arp_request_timer_cb, mbuf_pool);
	
	
	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) {
					if (ahdr->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REQUEST)) {
						//处理arp请求报文
						puts("arp request");
						ng_send_arp(mbuf_pool, RTE_ARP_OP_REPLY, ahdr->arp_data.arp_sha.addr_bytes, 
							ahdr->arp_data.arp_tip, ahdr->arp_data.arp_sip);
					} else if (ahdr->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REPLY)) {
						//处理arp响应
						puts("arp reply");
						struct arp_table *table = arp_table_instance();
						uint8_t *hwaddr = ng_get_dst_macaddr(ahdr->arp_data.arp_sip);
						if (hwaddr == NULL) {
							struct arp_entry *entry = rte_malloc("arp entry", sizeof(struct arp_entry), 0);
							//如何内存分配失败,进程不退出,后续会重试
							if (entry) {
								memset(entry, 0, sizeof(struct arp_entry));
								entry->ip = ahdr->arp_data.arp_sip;
								rte_memcpy(entry->hwadder, ahdr->arp_data.arp_sha.addr_bytes, RTE_ETHER_ADDR_LEN);
								entry->type = ARY_ENTRY_STATUS_DYNAMIC;
								LL_ADD(entry, table->entries);
								table->count++;
							}
						}
						struct arp_entry *iter;
						for (iter = table->entries; iter != NULL; iter = iter->next) {
					
							struct in_addr addr;
							addr.s_addr = iter->ip;

							print_ethaddr("arp table --> mac: ", (struct rte_ether_addr *)iter->hwadder);
							printf(" ip: %s \n", inet_ntoa(addr));
					
						}
					}
				}
			}
			rte_pktmbuf_free(mbufs[i]);
		}
		cur_tsc = rte_rdtsc();
		diff_tsc = cur_tsc - prev_tsc;
		if (diff_tsc > TIMER_RESOLUTION_CYCLES) {
			rte_timer_manage();
			prev_tsc = cur_tsc;
		}
		
	}
	
	return 0;
}