爆款云主机2核4G限时秒杀,88元/年起!
查看详情

活动

天翼云最新优惠活动,涵盖免费试用,产品折扣等,助您降本增效!
热门活动
  • 618智算钜惠季 爆款云主机2核4G限时秒杀,88元/年起!
  • 免费体验DeepSeek,上天翼云息壤 NEW 新老用户均可免费体验2500万Tokens,限时两周
  • 云上钜惠 HOT 爆款云主机全场特惠,更有万元锦鲤券等你来领!
  • 算力套餐 HOT 让算力触手可及
  • 天翼云脑AOne NEW 连接、保护、办公,All-in-One!
  • 中小企业应用上云专场 产品组合下单即享折上9折起,助力企业快速上云
  • 息壤高校钜惠活动 NEW 天翼云息壤杯高校AI大赛,数款产品享受线上订购超值特惠
  • 天翼云电脑专场 HOT 移动办公新选择,爆款4核8G畅享1年3.5折起,快来抢购!
  • 天翼云奖励推广计划 加入成为云推官,推荐新用户注册下单得现金奖励
免费活动
  • 免费试用中心 HOT 多款云产品免费试用,快来开启云上之旅
  • 天翼云用户体验官 NEW 您的洞察,重塑科技边界

智算服务

打造统一的产品能力,实现算网调度、训练推理、技术架构、资源管理一体化智算服务
智算云(DeepSeek专区)
科研助手
  • 算力商城
  • 应用商城
  • 开发机
  • 并行计算
算力互联调度平台
  • 应用市场
  • 算力市场
  • 算力调度推荐
一站式智算服务平台
  • 模型广场
  • 体验中心
  • 服务接入
智算一体机
  • 智算一体机
大模型
  • DeepSeek-R1-昇腾版(671B)
  • DeepSeek-R1-英伟达版(671B)
  • DeepSeek-V3-昇腾版(671B)
  • DeepSeek-R1-Distill-Llama-70B
  • DeepSeek-R1-Distill-Qwen-32B
  • Qwen2-72B-Instruct
  • StableDiffusion-V2.1
  • TeleChat-12B

应用商城

天翼云精选行业优秀合作伙伴及千余款商品,提供一站式云上应用服务
进入甄选商城进入云市场创新解决方案
办公协同
  • WPS云文档
  • 安全邮箱
  • EMM手机管家
  • 智能商业平台
财务管理
  • 工资条
  • 税务风控云
企业应用
  • 翼信息化运维服务
  • 翼视频云归档解决方案
工业能源
  • 智慧工厂_生产流程管理解决方案
  • 智慧工地
建站工具
  • SSL证书
  • 新域名服务
网络工具
  • 翼云加速
灾备迁移
  • 云管家2.0
  • 翼备份
资源管理
  • 全栈混合云敏捷版(软件)
  • 全栈混合云敏捷版(一体机)
行业应用
  • 翼电子教室
  • 翼智慧显示一体化解决方案

合作伙伴

天翼云携手合作伙伴,共创云上生态,合作共赢
天翼云生态合作中心
  • 天翼云生态合作中心
天翼云渠道合作伙伴
  • 天翼云代理渠道合作伙伴
天翼云服务合作伙伴
  • 天翼云集成商交付能力认证
天翼云应用合作伙伴
  • 天翼云云市场合作伙伴
  • 天翼云甄选商城合作伙伴
天翼云技术合作伙伴
  • 天翼云OpenAPI中心
  • 天翼云EasyCoding平台
天翼云培训认证
  • 天翼云学堂
  • 天翼云市场商学院
天翼云合作计划
  • 云汇计划
天翼云东升计划
  • 适配中心
  • 东升计划
  • 适配互认证

开发者

开发者相关功能入口汇聚
技术社区
  • 专栏文章
  • 互动问答
  • 技术视频
资源与工具
  • OpenAPI中心
开放能力
  • EasyCoding敏捷开发平台
培训与认证
  • 天翼云学堂
  • 天翼云认证
魔乐社区
  • 魔乐社区

支持与服务

为您提供全方位支持与服务,全流程技术保障,助您轻松上云,安全无忧
文档与工具
  • 文档中心
  • 新手上云
  • 自助服务
  • OpenAPI中心
定价
  • 价格计算器
  • 定价策略
基础服务
  • 售前咨询
  • 在线支持
  • 在线支持
  • 工单服务
  • 建议与反馈
  • 用户体验官
  • 服务保障
  • 客户公告
  • 会员中心
增值服务
  • 红心服务
  • 首保服务
  • 客户支持计划
  • 专家技术服务
  • 备案管家

了解天翼云

天翼云秉承央企使命,致力于成为数字经济主力军,投身科技强国伟大事业,为用户提供安全、普惠云服务
品牌介绍
  • 关于天翼云
  • 智算云
  • 天翼云4.0
  • 新闻资讯
  • 天翼云APP
基础设施
  • 全球基础设施
  • 信任中心
最佳实践
  • 精选案例
  • 超级探访
  • 云杂志
  • 分析师和白皮书
  • 天翼云·创新直播间
市场活动
  • 2025智能云生态大会
  • 2024智算云生态大会
  • 2023云生态大会
  • 2022云生态大会
  • 天翼云中国行
天翼云
  • 活动
  • 智算服务
  • 产品
  • 解决方案
  • 应用商城
  • 合作伙伴
  • 开发者
  • 支持与服务
  • 了解天翼云
      • 文档
      • 控制中心
      • 备案
      • 管理中心

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

      首页 知识中心 软件开发 文章详情页

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

      2024-12-05 08:49:45 阅读次数:44

      arp,timer,代码,定时器,链表

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

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

      其中的dynamic和static是动态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 1
      
      struct 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_ADD和LL_REMOVE两个宏来进行链表的增加和删除节点。具体如何使用,需要看我们再主函数中的使用过程。

      定时器

      • 为什么需要定时器?

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

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

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

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

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

      这里说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 ethhdr
      	struct 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_REPLY
      
      static uint8_t gDefaultArpMac[RTE_ETHER_ADDR_LEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
      
      #endif
      
      
      
      
      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) {
      
      	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_SEND
      	struct 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");
      		
      	}
      #endif
      
      	if (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 ethhdr
      	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);
      	
      
      	// 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 = 64
      	ip->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 --> mbuf
      
      	const 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_ARP
      
      
      static int ng_encode_arp_pkt(uint8_t *msg, uint16_t opcode, uint8_t *dst_mac, uint32_t sip, uint32_t dip) {
      
      	// 1 ethhdr
      	struct 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_ICMP
      
      
      static 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 ether
      	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_IPV4);
      
      	// 2 ip
      	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(sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_icmp_hdr));
      	ip->packet_id = 0;
      	ip->fragment_offset = 0;
      	ip->time_to_live = 64; // ttl = 64
      	ip->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;
      
      }
      
      
      #endif
      
      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);
      }
      
      
      #if ENABLE_TIMER
      
      static void
      arp_request_timer_cb(__attribute__((unused)) struct rte_timer *tim,
      	   void *arg) {
      
      	struct rte_mempool *mbuf_pool = (struct rte_mempool *)arg;
      
      #if 0
      	struct 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);
      
      #endif
      	
      	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);
      		
      	}
      	
      }
      
      
      #endif
      
      int 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_TIMER
      
      	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);
      
      #endif
      
      	while (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_ARP
      
      			if (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_DEBUG
      						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->hwaddr);
      								
      							printf(" ip: %s \n", inet_ntoa(addr));
      					
      						}
      #endif
      						rte_pktmbuf_free(mbufs[i]);
      					}
      				
      					continue;
      				} 
      			}
      #endif
      
      			if (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));
      
      #endif
      
      				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));
      
      #if ENABLE_SEND
      
      				struct 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);
      				
      #endif
      
      				rte_pktmbuf_free(mbufs[i]);
      			}
      
      #if ENABLE_ICMP
      
      			if (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_TIMER
      
      		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;
      		}
      
      #endif
      
      
      	}
      
      }
      
      版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://lennlouis.blog.csdn.net/article/details/139872960,作者:小狮子安度因,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。

      上一篇:javaScript(六):DOM操作

      下一篇:Linux源码阅读笔记13-进程通信组件上

      相关文章

      2025-05-19 09:04:30

      【Canvas技法】辐射式多道光影的实现

      【Canvas技法】辐射式多道光影的实现

      2025-05-19 09:04:30
      代码 , 函数 , 实现
      2025-05-19 09:04:14

      复杂度的OJ练习

      复杂度的OJ练习

      2025-05-19 09:04:14
      代码 , 复杂度 , 思路 , 数组 , 算法
      2025-05-19 09:04:14

      【牛客网+LeetCode】链表 OJ强训题——高效解法

      【牛客网+LeetCode】链表 OJ强训题——高效解法

      2025-05-19 09:04:14
      数据结构 , 链表
      2025-05-16 09:15:10

      【C/C++算法】蓝桥杯之递归算法(如何编写想出递归写法)

      【C/C++算法】蓝桥杯之递归算法(如何编写想出递归写法)

      2025-05-16 09:15:10
      结点 , 递归 , 遍历 , 链表 , 题目
      2025-05-14 10:33:25

      webpack5基础--13_生产模式介绍

      生产模式是开发完成代码后,我们需要得到代码将来部署上线。

      2025-05-14 10:33:25
      npm , 代码 , 优化 , 指令 , 模式 , 运行
      2025-05-14 10:33:25

      30天拿下Rust之高级类型

      Rust作为一门系统编程语言,以其独特的内存管理方式和强大的类型系统著称。其中,高级类型的应用,为Rust的开发者提供了丰富的编程工具和手段,使得开发者可以更加灵活和高效地进行编程。

      2025-05-14 10:33:25
      Rust , type , 代码 , 函数 , 类型 , 返回
      2025-05-14 10:33:16

      30天拿下Rust之模式与模式匹配

      Rust语言以其强大的类型系统和所有权模型而著称,而模式与模式匹配则是Rust中一种非常强大且灵活的工具,它允许我们在编译时根据数据的结构进行条件分支处理。

      2025-05-14 10:33:16
      match , 代码 , 匹配 , 模式 , 模式匹配 , 绑定
      2025-05-14 10:07:38

      30天拿下Rust之枚举

      Rust中的枚举是一种用户定义的类型,它允许你为一组相关的值赋予友好的名称。在Rust中,枚举是强大的工具,它们不仅仅用于表示几个固定的值,还可以包含函数和方法,使得枚举成员可以有自己的行为。

      2025-05-14 10:07:38
      match , None , Rust , 代码 , 枚举 , 类型
      2025-05-14 10:07:38

      30天拿下Python之函数

      在Python中,函数是一段可以重复使用的代码块,它可以提高代码的可重用性和可维护性,是编程中非常重要的概念。

      2025-05-14 10:07:38
      lambda , 代码 , 传递 , 关键字 , 函数 , 参数 , 定义
      2025-05-14 10:03:13

      AJAX-事件循环(超详细过程)

      JS有一个基于事件循环的并发模型,事件循环负责执行代码、收集和处理事件以及执行队列中的子任务。

      2025-05-14 10:03:13
      代码 , 任务 , 出栈 , 异步 , 执行 , 调用 , 队列
      查看更多
      推荐标签

      作者介绍

      天翼云小翼
      天翼云用户

      文章

      33561

      阅读量

      5252646

      查看更多

      最新文章

      【Canvas技法】辐射式多道光影的实现

      2025-05-19 09:04:30

      复杂度的OJ练习

      2025-05-19 09:04:14

      【C/C++算法】蓝桥杯之递归算法(如何编写想出递归写法)

      2025-05-16 09:15:10

      webpack5基础--13_生产模式介绍

      2025-05-14 10:33:25

      30天拿下Rust之高级类型

      2025-05-14 10:33:25

      30天拿下Rust之模式与模式匹配

      2025-05-14 10:33:16

      查看更多

      热门文章

      C++最普通的定时器功能实现

      2023-03-08 10:27:24

      Python|二进制链表转整数

      2023-02-27 10:24:46

      RxJava/RxAndroid:timer(long delay, TimeUnit unit)

      2023-04-13 09:37:51

      驱动开发:内核使用IO/DPC定时器

      2024-11-20 09:46:40

      驱动开发:内核枚举Registry注册表回调

      2024-07-01 01:32:23

      驱动开发:内核中的自旋锁结构

      2024-11-20 09:46:57

      查看更多

      热门标签

      java Java python 编程开发 代码 开发语言 算法 线程 Python html 数组 C++ 元素 javascript c++
      查看更多

      相关产品

      弹性云主机

      随时自助获取、弹性伸缩的云服务器资源

      天翼云电脑(公众版)

      便捷、安全、高效的云电脑服务

      对象存储

      高品质、低成本的云上存储服务

      云硬盘

      为云上计算资源提供持久性块存储

      查看更多

      随机文章

      【leetcode】位运算 - 汉明距离

      【算法】二分算法——寻找旋转排序数组中的最小值

      DRF—Mixin拓展类、视图集

      代码 测试用例 测试结果 测试结果 两两交换链表中的节点

      Android 关于PopupWindow键盘遮挡的更多解决方案

      Winform画圆以及无边框窗体的移动

      • 7*24小时售后
      • 无忧退款
      • 免费备案
      • 专家服务
      售前咨询热线
      400-810-9889转1
      关注天翼云
      • 旗舰店
      • 天翼云APP
      • 天翼云微信公众号
      服务与支持
      • 备案中心
      • 售前咨询
      • 智能客服
      • 自助服务
      • 工单管理
      • 客户公告
      • 涉诈举报
      账户管理
      • 管理中心
      • 订单管理
      • 余额管理
      • 发票管理
      • 充值汇款
      • 续费管理
      快速入口
      • 天翼云旗舰店
      • 文档中心
      • 最新活动
      • 免费试用
      • 信任中心
      • 天翼云学堂
      云网生态
      • 甄选商城
      • 渠道合作
      • 云市场合作
      了解天翼云
      • 关于天翼云
      • 天翼云APP
      • 服务案例
      • 新闻资讯
      • 联系我们
      热门产品
      • 云电脑
      • 弹性云主机
      • 云电脑政企版
      • 天翼云手机
      • 云数据库
      • 对象存储
      • 云硬盘
      • Web应用防火墙
      • 服务器安全卫士
      • CDN加速
      热门推荐
      • 云服务备份
      • 边缘安全加速平台
      • 全站加速
      • 安全加速
      • 云服务器
      • 云主机
      • 智能边缘云
      • 应用编排服务
      • 微服务引擎
      • 共享流量包
      更多推荐
      • web应用防火墙
      • 密钥管理
      • 等保咨询
      • 安全专区
      • 应用运维管理
      • 云日志服务
      • 文档数据库服务
      • 云搜索服务
      • 数据湖探索
      • 数据仓库服务
      友情链接
      • 中国电信集团
      • 189邮箱
      • 天翼企业云盘
      • 天翼云盘
      ©2025 天翼云科技有限公司版权所有 增值电信业务经营许可证A2.B1.B2-20090001
      公司地址:北京市东城区青龙胡同甲1号、3号2幢2层205-32室
      • 用户协议
      • 隐私政策
      • 个人信息保护
      • 法律声明
      备案 京公网安备11010802043424号 京ICP备 2021034386号