dpdk 响应icmp请求(Echo or Echo Reply Message)

news2024/9/8 23:35:45

注:对于"Echo or Echo Reply Message"类型的icmp报文,响应报文的Identiy和Sequence Number的值与请求报文的这两个字段的值要相同。

Identifier(标识符)字段通常由发送方设置,并被用于将ICMP请求与相应的回复相关联。当发送ICMP请求时,发送方会将一个特定的值放入Identifier字段中,在收到对应的回复时可以通过该值进行匹配。

Sequence Number(序列号)字段用于按顺序编号ICMP请求或回复报文。它起到了帮助接收方正确排序和处理报文的作用。

#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 uint8_t gSrcMac[RTE_ETHER_ADDR_LEN];

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 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 void ng_encode_icmp_pkt(uint8_t *msg, uint8_t *dst_mac, uint32_t sip, uint32_t dip, 
																	uint16_t id, uint16_t seqnb) {

	//设置以太网头
	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);

	//设置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(sizeof(struct rte_icmp_hdr) + sizeof(struct rte_ipv4_hdr));
	ip->packet_id = 0;
	ip->fragment_offset = 0;
	ip->time_to_live = 64;
	ip->next_proto_id = IPPROTO_ICMP;
	ip->src_addr = sip;
	ip->dst_addr = dip;
	//计算ip头部校验和时,先把该字段置为0(ip校验和只包括头部)
	ip->hdr_checksum = 0;
	ip->hdr_checksum = rte_ipv4_cksum(ip);

	//设置icmp报文
	struct rte_icmp_hdr *icmp = (struct rte_icmp_hdr *)(ip + 1);
	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));
	
}

static void 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_len = sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_icmp_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;

	//用于将数据包缓冲区(packet buffer)转换为指定类型的数据指针,也就是mbuf存储数据包的首地址
	uint8_t *pktdata = rte_pktmbuf_mtod(mbuf, uint8_t*);
	ng_encode_icmp_pkt(pktdata, dst_mac, sip, dip, id, seqnb);

	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_ICMP) {
				struct rte_icmp_hdr *icmphdr = (struct rte_icmp_hdr *)(iphdr + 1);
				if (icmphdr->icmp_type == RTE_IP_ICMP_ECHO_REQUEST) {
					ng_send_icmp(mbuf_pool, ehdr->s_addr.addr_bytes, iphdr->dst_addr, iphdr->src_addr, 
						icmphdr->icmp_ident, icmphdr->icmp_seq_nb);
				}
				
			}
			rte_pktmbuf_free(mbufs[i]);
		}
		
	}
	
	return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1957867.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Lombok注解之@SneakyThrows作用

Lombok注解之SneakyThrows作用 读法 [ˈsniːki] [θroʊz] 悄悄的 抛出顾名思义&#xff0c;它能够自动偷摸的为咱们的代码生成一个try…catch块&#xff0c;并把异常向上抛出来。 使用 SneakyThrows的使用范围&#xff1a; 只能作用在方法和构造函数之上。从源码就可以…

【C++】选择结构-多条件if语句

多条件if语句格式为 if(第一个条件) else if(若第一个条件未满足&#xff0c;执行此条件) {第二个条件满足执行此操作} else if(若第二个条件未满足&#xff0c;执行此条件) {第三个条件满足执行此操作} ...... else{若所有条件都不满足执行此操作} 下面是一个实例 #inc…

app逆向实战:某咨询6.0.4.4版本signature等参数抓包与破解

本篇博客旨在记录学习过程&#xff0c;不可用于商用等其它途径 入口 密码登录接口 抓包 根据抓包结果得知动态参数是client_timestamp&#xff0c;keyword&#xff0c;client_session&#xff0c;sig&#xff0c;sigTime&#xff0c;cursor 初步观察得出以下结论&#xff…

负债了,打死也别干的六件事!逾期了,六句谎言千万别信!

负债这事儿&#xff0c;真是一言难尽&#xff0c;稍不留神&#xff0c;就可能让情况雪上加霜。今儿咱们聊聊&#xff0c;负债后那几件打死也别干的几件事&#xff0c;尤其是针对还没有逾期的朋友们&#xff0c;免得后悔莫及。 首先&#xff0c;千万别动歪脑筋&#xff0c;拿公款…

深入剖析:GaussDB与MySQL在COUNT查询中的并行化技术

引言 数据库查询性能优化是数据库管理和开发中的一个重要议题。在处理大数据量的COUNT查询时&#xff0c;传统的单线程处理方式可能无法满足现代应用的性能需求。GaussDB(for MySQL)和MySQL作为流行的数据库系统&#xff0c;它们在并行查询优化方面有着各自的策略和技术。本文…

【C++】C++的类型的转换

目录 C语言中的类型转换 C中的类型转换 C强制类型转换 static_cast reinterpret_cast const_cast dynamic_cast C语言中的类型转换 C语言中又两种类型转换&#xff1a;&#xff08;强制&#xff09;显示类型转换和隐式类型转换。 &#xff08;强制&#xff09;显示类型…

多线程与并发思想

问题分析 设计并发程序的目的就是为了使程序运行得更快&#xff08;时间就是金钱、生命&#xff09;&#xff0c;提高软件的性能。并发程序之所以能快&#xff0c;就在于这个“并”字&#xff0c;因为程序能并发(单核)或并行(多核、多CPU)执行&#xff0c;当然能快。这就好比工…

修改linux服务器上的mariadb/mysql数据库的密码

文章目录 一、查看数据库的状态二、修改密码 可能我们在最初安装数据库时没有设置密码或者已经设置了但是又想修改另一个密码&#xff0c;可以这样操作来修改我们的密码。 以数据库 mariadb 为例。 一、查看数据库的状态 使用命令 systemctl is-active mariadb 查看当前数据库…

链表篇-02.从尾到头打印链表(反转链表)

解题思路&#xff1a; 链表从尾到头打印链表, 我的思路是 用三个指针,第一个指针(pre)指向指向头节点的前一个位置&#xff0c;第二个指针(cur)指向头节点&#xff0c; 然后依次往后执行&#xff0c;第三个指针用于临时记录第二个指针的下一个位置。 代码详情: import java.…

Linux云计算 |【第二阶段】AUTOMATION-DAY5

主要内容&#xff1a; YAML语法格式&#xff0c;层级关系、Ansible Playbook文件及语法格式、Ansible变量&#xff08;定义变量方法、优先级顺序、setup和debug查看变量&#xff09; 补充&#xff1a;Ansible ad-hoc 可以通过命令行形式远程管理其他主机&#xff0c;适合执行一…

python学习笔记——字符串

一、创建字符串 1.我们可以使用引号( 或 " )来创建字符串。创建字符串很简单&#xff0c;只要为变量分配一个值即可。 var1 Hello World! var2 "Runoob" 二、访问字符串中的值 1.Python 访问子字符串&#xff0c;可以使用方括号 [] 来截取字符串。…

RocketMQ的Admin Tool工具

文档&#xff1a;https://github.com/apache/rocketmq/blob/develop/docs/cn/operation.md写的很全面&#xff0c;我写了一半就偷懒了&#xff0c;地址放这里。 命令大全&#xff1a;https://github.com/apache/rocketmq/blob/develop/docs/cn/operation.md 1. 删除讨厌的告警…

VUE3——003、VUE 项目中的文件结构(index.html、main.ts、App.vue)

虽然是号称是小白初学&#xff0c;但本文只是对 VUE 小白&#xff0c;其它的基功还是有一丢丢的&#xff0c;不太懂的同学去看看一下详解&#xff0c;我这里记述的是自己的理解和观点。见谅&#xff01; index.html&#xff1a;入口文件&#xff08;以创建 vue3 项目的默认文件…

springboot+vue+mybatis线上选课系统+PPT+论文+讲解+售后

在如今社会上&#xff0c;关于信息上面的处理&#xff0c;没有任何一个企业或者个人会忽视&#xff0c;如何让信息急速传递&#xff0c;并且归档储存查询&#xff0c;采用之前的纸张记录模式已经不符合当前使用要求了。所以&#xff0c;对学生选课信息管理的提升&#xff0c;也…

顺序表的代码实现

顺序表的代码实现 1.认识什么是顺序表1.1顺序表的优缺点 2.实现顺序表代码准备3.顺序表的代码实现3.1 顺序表结构体的定义3.2 顺序表的初始化3.3 顺序表的销毁3.4 顺序表的输出打印3.5顺序表的扩容3.6 顺序表的头部插入(头插)3.7 顺序表的头部删除(头删)3.8 顺序表的尾部插入(尾…

会话存储、本地存储,路由导航守卫、web会话跟踪、JWT生成token、axios请求拦截、响应拦截

1、会话存储、本地存储 前端浏览器中存储用户信息&#xff0c;会话存储、本地存储、cookie 会话存储&#xff08;sessionStorage&#xff09;&#xff1a;会话期间存储&#xff0c;关闭浏览器后&#xff0c;数据就会销毁 sessionStorage.setItem("account",resp.d…

Unity Apple Vision Pro 开发:如何把 PolySpatial 和 Play To Device 的版本从 1.2.3 升级为 1.3.1

XR 开发社区&#xff1a; SpatialXR社区&#xff1a;完整课程、项目下载、项目孵化宣发、答疑、投融资、专属圈子 &#x1f4d5;教程说明 本教程将介绍如何把 Unity 的 PolySpatial 和 Play To Device 版本从 1.2.3 升级为 1.3.1。 &#x1f4d5;Play To Device 软件升级 ht…

科学设计程序员面试内容,破解“八股文”之弊

“八股文”在实际工作中是助力、阻力还是空谈&#xff1f; 作为现在各类大中小企业面试程序员时的必问内容&#xff0c;“八股文”似乎是很重要的存在。但“八股文”是否能在实际工作中发挥它“敲门砖”应有的作用呢&#xff1f;有IT人士不禁发出疑问&#xff1a;程序员面试考…

数据结构之线性表(顺序表的实现)

目录 一、线性表的原理 二、线性表的实现&#xff08;顺序表&#xff09; 1.定义顺序表 2.初始化顺序表 3.判断顺序表是否为空 4.获取顺序表的长度 5.向顺序表中插入元素 6.删除指定位置的元素 7.遍历顺序表 8.得到指定位置的元素 三、打印测试功能 1.测试 2.结果…