用户态协议栈04-定时arp-table的实现

2024-06-22 08:44

本文主要是介绍用户态协议栈04-定时arp-table的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

之前有写过arp reply的实现,其中有写道,我们的系统内核中会维护一张ARP表,可以通过终端arp -a查看:

其中的dynamicstatic是动态arp的类型,之前的udp实验就是添加了一条静态arp达到了发送的目的。在我们需要发送一个数据包的时候。指定好ip,内核会在arp表中寻找对应的ip,然后向他发送。在用户太协议栈中,DPDK同样需要维护一张arp表,实现类似的功能。

链表实现的ARP表


#ifndef LN_ARP_TABLE
#define LN_ARP_TABLE#include <rte_malloc.h>
#include <stdio.h>#define ARP_ENTRY_STATUS_DYNAMIC 0
#define ARP_ENTRY_STATUS_STATIC 1struct arp_entry {uint32_t ip;uint8_t hwaddr[RTE_ETHER_ADDR_LEN];int type;struct arp_entry* next;struct arp_entry* prev;
};struct arp_table {struct arp_entry* entries;int count;
}#define LL_ADD(item, list) do {		\item->next = NULL;		\item->prev = NULL;		\if(list != NULL) list->prev = NULL;		\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->prev = item->prev;	\if(item == list) list = list->next;		\item->prev = item->next = NULL;		\
}while(0)static struct arp_table arpt = NULL;static struct arp_table* arp_table_instance() {if(arpt == NULL) {arpt = rte_malloc("arp table", sizeof(struct arp_table), 0);if(arpt == NULL) {rte_exit(EXIT_FAILURE, "Error with arp table malloc\n");}memset(arpt, 0, sizeof(struct arp_table));}return arpt;
}static uint8_t* arp_get_dst_macaddr(uint32_t dip) {struct arp_entry* ite;struct arp_table* table = arp_table_instance();for(ite = table->entries; ite != NULL; ite = ite->next) {if(dip == ite->ip) {return ite->hwaddr;}}return NULL;
}#endif

这里采用的是一个单例模式,线程安全等后面再迭代。这里实现的是一个头单项链表,只能头插。arp表一般来说不会太大,所以使用链表就可以了。或者说其实再大胆一点,数组也是可以的。代码中有LL_ADDLL_REMOVE两个宏来进行链表的增加和删除节点。具体如何使用,需要看我们再主函数中的使用过程。

定时器

  • 为什么需要定时器?

    arp协议是一种广播协议,我们需要定时向局域网发起广播,公布自己的arp同时获取别人的arp来更新arp表。

这里是使用的DPDK提供出来的一个定时器组件,我直接抽离出来记录,避免代码繁多看起来眼花缭乱。

#define TIMER_RESOLUTION_CYCLES 120000000000ULL // 10ms * 1000 = 10s * 6 

首先是有一个宏,这个有点像单片机里的知识。根据始终的频率,再一秒钟之内的滴答次数不一样,这里是记录一个抵达次数如果到达这个次数就广播arp。那这个次数是如何计算出来的呢?其实是再例程里面有的:

这里说20000000ULL是对应的10ms的时间间隔,所以可以算出,我们这里的宏是一分钟,广播一次。

rte_timer_subsystem_init();struct rte_timer arp_timer;
rte_timer_init(&arp_timer);uint64_t hz = rte_get_timer_hz();
unsigned lcore_id = rte_lcore_id();
rte_timer_reset(&arp_timer, hz, PERIODICAL, lcore_id, arp_request_timer_cb, mbuf_pool);
  • rte_timer_subsystem_init()函数会初始化相关的数据结构和配置,以便后续使用计时器功能
  • rte_get_timer_hz取得系统计时器的频率
  • rte_lcore_id返回执行当前代码的逻辑核心ID
  • arp_request_timer_cb是自己写的定时器执行的回调函数,执行业务逻辑
static uint64_t prev_tsc = 0, cur_tsc;
uint64_t diff_tsc;cur_tsc = rte_rdtsc();
diff_tsc = cur_tsc - prev_tsc;
if (diff_tsc > TIMER_RESOLUTION_CYCLES) {rte_timer_manage();prev_tsc = cur_tsc;
}
  • rte_timer_manage会检查所有注册的定时器,对超时任务进行处理。

具体实现

arp打包

static int ng_encode_arp_pkt(uint8_t *msg, uint16_t opcode, uint8_t *dst_mac, uint32_t sip, uint32_t dip) {// 1 ethhdrstruct rte_ether_hdr *eth = (struct rte_ether_hdr *)msg;rte_memcpy(eth->s_addr.addr_bytes, gSrcMac, RTE_ETHER_ADDR_LEN);if (!strncmp((const char *)dst_mac, (const char *)gDefaultArpMac, RTE_ETHER_ADDR_LEN)) {uint8_t mac[RTE_ETHER_ADDR_LEN] = {0x0};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);// 2 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;return 0;}static struct rte_mbuf *ng_send_arp(struct rte_mempool *mbuf_pool, uint16_t opcode, uint8_t *dst_mac, uint32_t sip, uint32_t dip) {const unsigned total_length = sizeof(struct rte_ether_hdr) + sizeof(struct rte_arp_hdr);struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool);if (!mbuf) {rte_exit(EXIT_FAILURE, "rte_pktmbuf_alloc\n");}mbuf->pkt_len = total_length;mbuf->data_len = total_length;uint8_t *pkt_data = rte_pktmbuf_mtod(mbuf, uint8_t *);ng_encode_arp_pkt(pkt_data, opcode, dst_mac, sip, dip);return mbuf;
}

这里的两个函数相较于之前的添加了一个opcode参数,因为之前的是默认reply的,这里需要区别是reply还是request,其他没什么变化。

arp广播

static void arp_request_timer_cb(__attribute__((unused)) struct rte_timer *tim, void *arg) {struct rte_mempool *mbuf_pool = (struct rte_mempool *)arg;int i = 0;for (i = 1;i <= 254;i ++) {uint32_t dstip = (gLocalIp & 0x00FFFFFF) | (0xFF000000 & (i << 24));struct in_addr addr;addr.s_addr = dstip;printf("arp ---> src: %s \n", inet_ntoa(addr));struct rte_mbuf *arpbuf = NULL;uint8_t *dstmac = ng_get_dst_macaddr(dstip);if (dstmac == NULL) {arpbuf = ng_send_arp(mbuf_pool, RTE_ARP_OP_REQUEST, gDefaultArpMac, gLocalIp, dstip);} else {arpbuf = ng_send_arp(mbuf_pool, RTE_ARP_OP_REQUEST, dstmac, gLocalIp, dstip);}rte_eth_tx_burst(gDpdkPortId, 0, &arpbuf, 1);rte_pktmbuf_free(arpbuf);}}

这里是对网段中的所有ip发起request请求,我感觉这里有必要梳理一下这个dip的逻辑,毕竟这个代码看着怎么像寄存器[狗头]。假设本地ip是192.168.1.111:因为网络字节序是反的,所以(gLocalIp & 0x00FFFFFF)是把其中的111设为0;(0xFF000000 & (i << 24))从1-255分别将低位置1,这样就形成了目的ip;

完整代码

#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 "arp.h"#define ENABLE_SEND		1
#define ENABLE_ARP		1
#define ENABLE_ICMP		1
#define ENABLE_ARP_REPLY	1#define ENABLE_DEBUG		1#define ENABLE_TIMER		1#define NUM_MBUFS (4096-1)#define BURST_SIZE	32#define TIMER_RESOLUTION_CYCLES 120000000000ULL // 10ms * 1000 = 10s * 6 #if ENABLE_SEND#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, 184);static uint32_t gSrcIp; //
static uint32_t gDstIp;static uint8_t gSrcMac[RTE_ETHER_ADDR_LEN];
static uint8_t gDstMac[RTE_ETHER_ADDR_LEN];static uint16_t gSrcPort;
static uint16_t gDstPort;#endif#if ENABLE_ARP_REPLYstatic uint8_t gDefaultArpMac[RTE_ETHER_ADDR_LEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};#endifint 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) {uint16_t nb_sys_ports= rte_eth_dev_count_avail(); //if (nb_sys_ports == 0) {rte_exit(EXIT_FAILURE, "No Supported eth found\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);if (rte_eth_rx_queue_setup(gDpdkPortId, 0 , 1024, rte_eth_dev_socket_id(gDpdkPortId),NULL, mbuf_pool) < 0) {rte_exit(EXIT_FAILURE, "Could not setup RX queue\n");}#if ENABLE_SENDstruct rte_eth_txconf txq_conf = dev_info.default_txconf;txq_conf.offloads = port_conf.rxmode.offloads;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");}
#endifif (rte_eth_dev_start(gDpdkPortId) < 0 ) {rte_exit(EXIT_FAILURE, "Could not start\n");}}static int ng_encode_udp_pkt(uint8_t *msg, unsigned char *data, uint16_t total_len) {// encode // 1 ethhdrstruct 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);// 2 iphdr struct rte_ipv4_hdr *ip = (struct rte_ipv4_hdr *)(msg + sizeof(struct rte_ether_hdr));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; // ttl = 64ip->next_proto_id = IPPROTO_UDP;ip->src_addr = gSrcIp;ip->dst_addr = gDstIp;ip->hdr_checksum = 0;ip->hdr_checksum = rte_ipv4_cksum(ip);// 3 udphdr struct rte_udp_hdr *udp = (struct rte_udp_hdr *)(msg + sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr));udp->src_port = gSrcPort;udp->dst_port = gDstPort;uint16_t udplen = total_len - sizeof(struct rte_ether_hdr) - sizeof(struct rte_ipv4_hdr);udp->dgram_len = htons(udplen);rte_memcpy((uint8_t*)(udp+1), data, udplen);udp->dgram_cksum = 0;udp->dgram_cksum = rte_ipv4_udptcp_cksum(ip, udp);struct in_addr addr;addr.s_addr = gSrcIp;printf(" --> src: %s:%d, ", inet_ntoa(addr), ntohs(gSrcPort));addr.s_addr = gDstIp;printf("dst: %s:%d\n", inet_ntoa(addr), ntohs(gDstPort));return 0;
}static struct rte_mbuf * ng_send_udp(struct rte_mempool *mbuf_pool, uint8_t *data, uint16_t length) {// mempool --> mbufconst unsigned total_len = length + 42;struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool);if (!mbuf) {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_udp_pkt(pktdata, data, total_len);return mbuf;}#if ENABLE_ARPstatic int ng_encode_arp_pkt(uint8_t *msg, uint16_t opcode, uint8_t *dst_mac, uint32_t sip, uint32_t dip) {// 1 ethhdrstruct rte_ether_hdr *eth = (struct rte_ether_hdr *)msg;rte_memcpy(eth->s_addr.addr_bytes, gSrcMac, RTE_ETHER_ADDR_LEN);if (!strncmp((const char *)dst_mac, (const char *)gDefaultArpMac, RTE_ETHER_ADDR_LEN)) {uint8_t mac[RTE_ETHER_ADDR_LEN] = {0x0};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);// 2 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;return 0;}static struct rte_mbuf *ng_send_arp(struct rte_mempool *mbuf_pool, uint16_t opcode, uint8_t *dst_mac, uint32_t sip, uint32_t dip) {const unsigned total_length = sizeof(struct rte_ether_hdr) + sizeof(struct rte_arp_hdr);struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool);if (!mbuf) {rte_exit(EXIT_FAILURE, "rte_pktmbuf_alloc\n");}mbuf->pkt_len = total_length;mbuf->data_len = total_length;uint8_t *pkt_data = rte_pktmbuf_mtod(mbuf, uint8_t *);ng_encode_arp_pkt(pkt_data, opcode, dst_mac, sip, dip);return mbuf;
}#endif#if ENABLE_ICMPstatic uint16_t ng_checksum(uint16_t *addr, int count) {register long sum = 0;while (count > 1) {sum += *(unsigned short*)addr++;count -= 2;}if (count > 0) {sum += *(unsigned char *)addr;}while (sum >> 16) {sum = (sum & 0xffff) + (sum >> 16);}return ~sum;
}static int ng_encode_icmp_pkt(uint8_t *msg, uint8_t *dst_mac,uint32_t sip, uint32_t dip, uint16_t id, uint16_t seqnb) {// 1 etherstruct 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_IPV4);// 2 ipstruct rte_ipv4_hdr *ip = (struct rte_ipv4_hdr *)(msg + sizeof(struct rte_ether_hdr));ip->version_ihl = 0x45;ip->type_of_service = 0;ip->total_length = htons(sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_icmp_hdr));ip->packet_id = 0;ip->fragment_offset = 0;ip->time_to_live = 64; // ttl = 64ip->next_proto_id = IPPROTO_ICMP;ip->src_addr = sip;ip->dst_addr = dip;ip->hdr_checksum = 0;ip->hdr_checksum = rte_ipv4_cksum(ip);// 3 icmp struct rte_icmp_hdr *icmp = (struct rte_icmp_hdr *)(msg + sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr));icmp->icmp_type = RTE_IP_ICMP_ECHO_REPLY;icmp->icmp_code = 0;icmp->icmp_ident = id;icmp->icmp_seq_nb = seqnb;icmp->icmp_cksum = 0;icmp->icmp_cksum = ng_checksum((uint16_t*)icmp, sizeof(struct rte_icmp_hdr));return 0;
}static struct rte_mbuf *ng_send_icmp(struct rte_mempool *mbuf_pool, uint8_t *dst_mac,uint32_t sip, uint32_t dip, uint16_t id, uint16_t seqnb) {const unsigned total_length = sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_icmp_hdr);struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool);if (!mbuf) {rte_exit(EXIT_FAILURE, "rte_pktmbuf_alloc\n");}mbuf->pkt_len = total_length;mbuf->data_len = total_length;uint8_t *pkt_data = rte_pktmbuf_mtod(mbuf, uint8_t *);ng_encode_icmp_pkt(pkt_data, dst_mac, sip, dip, id, seqnb);return mbuf;}#endifstatic 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);
}#if ENABLE_TIMERstatic void
arp_request_timer_cb(__attribute__((unused)) struct rte_timer *tim,void *arg) {struct rte_mempool *mbuf_pool = (struct rte_mempool *)arg;#if 0struct rte_mbuf *arpbuf = ng_send_arp(mbuf_pool, RTE_ARP_OP_REQUEST, ahdr->arp_data.arp_sha.addr_bytes, ahdr->arp_data.arp_tip, ahdr->arp_data.arp_sip);rte_eth_tx_burst(gDpdkPortId, 0, &arpbuf, 1);rte_pktmbuf_free(arpbuf);#endifint i = 0;for (i = 1;i <= 254;i ++) {uint32_t dstip = (gLocalIp & 0x00FFFFFF) | (0xFF000000 & (i << 24));struct in_addr addr;addr.s_addr = dstip;printf("arp ---> src: %s \n", inet_ntoa(addr));struct rte_mbuf *arpbuf = NULL;uint8_t *dstmac = ng_get_dst_macaddr(dstip);if (dstmac == NULL) {arpbuf = ng_send_arp(mbuf_pool, RTE_ARP_OP_REQUEST, gDefaultArpMac, gLocalIp, dstip);} else {arpbuf = ng_send_arp(mbuf_pool, RTE_ARP_OP_REQUEST, dstmac, gLocalIp, dstip);}rte_eth_tx_burst(gDpdkPortId, 0, &arpbuf, 1);rte_pktmbuf_free(arpbuf);}}#endifint main(int argc, char *argv[]) {if (rte_eal_init(argc, argv) < 0) {rte_exit(EXIT_FAILURE, "Error with EAL init\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);rte_eth_macaddr_get(gDpdkPortId, (struct rte_ether_addr *)gSrcMac);#if ENABLE_TIMERrte_timer_subsystem_init();struct rte_timer arp_timer;rte_timer_init(&arp_timer);uint64_t hz = rte_get_timer_hz();unsigned lcore_id = rte_lcore_id();rte_timer_reset(&arp_timer, hz, PERIODICAL, lcore_id, arp_request_timer_cb, mbuf_pool);#endifwhile (1) {struct rte_mbuf *mbufs[BURST_SIZE];unsigned num_recvd = rte_eth_rx_burst(gDpdkPortId, 0, mbufs, BURST_SIZE);if (num_recvd > BURST_SIZE) {rte_exit(EXIT_FAILURE, "Error receiving from eth\n");}unsigned i = 0;for (i = 0;i < num_recvd;i ++) {struct rte_ether_hdr *ehdr = rte_pktmbuf_mtod(mbufs[i], struct rte_ether_hdr*);#if ENABLE_ARPif (ehdr->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {struct rte_arp_hdr *ahdr = rte_pktmbuf_mtod_offset(mbufs[i], struct rte_arp_hdr *, sizeof(struct rte_ether_hdr));struct in_addr addr;addr.s_addr = ahdr->arp_data.arp_tip;printf("arp ---> src: %s ", inet_ntoa(addr));addr.s_addr = gLocalIp;printf(" local: %s \n", inet_ntoa(addr));if (ahdr->arp_data.arp_tip == gLocalIp) {if (ahdr->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REQUEST)) {printf("arp --> request\n");struct rte_mbuf *arpbuf = 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);rte_eth_tx_burst(gDpdkPortId, 0, &arpbuf, 1);rte_pktmbuf_free(arpbuf);} else if (ahdr->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REPLY)) {printf("arp --> reply\n");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->hwaddr, ahdr->arp_data.arp_sha.addr_bytes, RTE_ETHER_ADDR_LEN);entry->type = 0;LL_ADD(entry, table->entries);table->count ++;}}
#if ENABLE_DEBUGstruct 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->hwaddr);printf(" ip: %s \n", inet_ntoa(addr));}
#endifrte_pktmbuf_free(mbufs[i]);}continue;} }
#endifif (ehdr->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {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);#if ENABLE_SEND //rte_memcpy(gDstMac, ehdr->s_addr.addr_bytes, RTE_ETHER_ADDR_LEN);rte_memcpy(&gSrcIp, &iphdr->dst_addr, sizeof(uint32_t));rte_memcpy(&gDstIp, &iphdr->src_addr, sizeof(uint32_t));rte_memcpy(&gSrcPort, &udphdr->dst_port, sizeof(uint16_t));rte_memcpy(&gDstPort, &udphdr->src_port, sizeof(uint16_t));#endifuint16_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));#if ENABLE_SENDstruct rte_mbuf *txbuf = ng_send_udp(mbuf_pool, (uint8_t *)(udphdr+1), length);rte_eth_tx_burst(gDpdkPortId, 0, &txbuf, 1);rte_pktmbuf_free(txbuf);#endifrte_pktmbuf_free(mbufs[i]);}#if ENABLE_ICMPif (iphdr->next_proto_id == IPPROTO_ICMP) {struct rte_icmp_hdr *icmphdr = (struct rte_icmp_hdr *)(iphdr + 1);struct in_addr addr;addr.s_addr = iphdr->src_addr;printf("icmp ---> src: %s ", inet_ntoa(addr));if (icmphdr->icmp_type == RTE_IP_ICMP_ECHO_REQUEST) {addr.s_addr = iphdr->dst_addr;printf(" local: %s , type : %d\n", inet_ntoa(addr), icmphdr->icmp_type);struct rte_mbuf *txbuf = ng_send_icmp(mbuf_pool, ehdr->s_addr.addr_bytes,iphdr->dst_addr, iphdr->src_addr, icmphdr->icmp_ident, icmphdr->icmp_seq_nb);rte_eth_tx_burst(gDpdkPortId, 0, &txbuf, 1);rte_pktmbuf_free(txbuf);rte_pktmbuf_free(mbufs[i]);}}#endif}#if ENABLE_TIMERstatic uint64_t prev_tsc = 0, cur_tsc;uint64_t diff_tsc;cur_tsc = rte_rdtsc();diff_tsc = cur_tsc - prev_tsc;if (diff_tsc > TIMER_RESOLUTION_CYCLES) {rte_timer_manage();prev_tsc = cur_tsc;}#endif}}

写在最后

  1. 后续对当前代码进行一些架构优化,并且添加别的协议,tcp还是比较麻烦的
  2. 昨天去面试,是一家头部的智能家具公司。虽然说应该是过了,但是回答的比较业余,要把八股文提上日程了哈哈哈。

这篇关于用户态协议栈04-定时arp-table的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1083808

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略 1. 特权模式限制2. 宿主机资源隔离3. 用户和组管理4. 权限提升控制5. SELinux配置 💖The Begin💖点点关注,收藏不迷路💖 Kubernetes的PodSecurityPolicy(PSP)是一个关键的安全特性,它在Pod创建之前实施安全策略,确保P

工厂ERP管理系统实现源码(JAVA)

工厂进销存管理系统是一个集采购管理、仓库管理、生产管理和销售管理于一体的综合解决方案。该系统旨在帮助企业优化流程、提高效率、降低成本,并实时掌握各环节的运营状况。 在采购管理方面,系统能够处理采购订单、供应商管理和采购入库等流程,确保采购过程的透明和高效。仓库管理方面,实现库存的精准管理,包括入库、出库、盘点等操作,确保库存数据的准确性和实时性。 生产管理模块则涵盖了生产计划制定、物料需求计划、

C++——stack、queue的实现及deque的介绍

目录 1.stack与queue的实现 1.1stack的实现  1.2 queue的实现 2.重温vector、list、stack、queue的介绍 2.1 STL标准库中stack和queue的底层结构  3.deque的简单介绍 3.1为什么选择deque作为stack和queue的底层默认容器  3.2 STL中对stack与queue的模拟实现 ①stack模拟实现

基于51单片机的自动转向修复系统的设计与实现

文章目录 前言资料获取设计介绍功能介绍设计清单具体实现截图参考文献设计获取 前言 💗博主介绍:✌全网粉丝10W+,CSDN特邀作者、博客专家、CSDN新星计划导师,一名热衷于单片机技术探索与分享的博主、专注于 精通51/STM32/MSP430/AVR等单片机设计 主要对象是咱们电子相关专业的大学生,希望您们都共创辉煌!✌💗 👇🏻 精彩专栏 推荐订阅👇🏻 单片机