DPDK之ICMP reply实现

昨天给自己挖了一个坑0.0 昨天在验证ARP发送程序时,当时为了看到PC 的arp记录被改变,专门给写了个错误的mac 00:11:22:33:44:55,结果今调试ICMP时,老是收不到包。。自作孽啊DPDK之ICMP reply实现_第1张图片
还是简单介绍下ICMP协议报文格式
ICMP是TCP/IP协议族的一个子协议,用于在IP主机、路由器之间传递控制消息。控制消息是指网络通不通、主机是否可达、路由是否可用等网络本身的消息。
  ICMP协议通过IP协议发送的,IP协议是一种无连接的,不可靠的数据包协议,属于网络层协议。
  ICMP报文是在IP数据报内被传输的。在实际传输中的数据包结构:20字节IP首部 + 8字节ICMP首部+ 1472字节<数据大小>38字节。
  ICMP报文格式:IP首部(20字节)+8位类型+8位代码+16位校验和+(不同的类型和代码,格式也有所不同)。下面即为ICMP报文格式:
  DPDK之ICMP reply实现_第2张图片

上代码,也是在之前的代码的基础上增加,感觉多实现几个协议后就不想加注释了。。。

#include
#include
#include
#include
#include

#define DEBUG_LEVEL 0

#define NUM_MBUFS (4096-1)
#define BURST_SIZE	32
#define ENABLE_SEND		1
#define ENABLE_ARP		1
#define ENABLE_ICMP		1


int gDpdkPortId = 0;//eth0
#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, 101, 83);
	static uint32_t gSrcIp;
	static uint32_t gDstIp;
	static uint16_t gSrcPort;
	static uint16_t gDstPort;
	static uint8_t gSrcMac[RTE_ETHER_ADDR_LEN];
	static uint8_t gDstMac[RTE_ETHER_ADDR_LEN];
#endif


static const struct rte_eth_conf port_conf_default = {
.rxmode = {.max_rx_pkt_len = RTE_ETHER_MAX_LEN} //RTE_ETHER_MAX_LEN 以太网数据中长度,一般为1518
};

static void ng_init_port(struct rte_mempool *mbuf_pool)
{
	//查询系统中可用的以太网设备数量,比如eth0,eth1等
	uint16_t nb_sys_ports = rte_eth_dev_count_avail();
	if(nb_sys_ports == 0)
	{
		rte_exit(EXIT_FAILURE,"no eth dev is availble\n");
	}
	struct rte_eth_dev_info dev_info;
	//查询以太网接口属性,此处的id = 0,代表查询eth0
	rte_eth_dev_info_get(gDpdkPortId,&dev_info);

	const int num_rx_queues = 1;//设置接受队列大小,通常每个队列与一个独立CPU关联
	const int num_tx_queues = 1;
	struct rte_eth_conf port_conf = port_conf_default;
	//配置eth0相关属性,用于后面接收发送数据包
	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,"can not setup TX queue\n");
	}
#endif
	//启动指定的网卡,使其能够接收和发送数据包
	//初始化指定的以太网设备,配置接收队列和设备属性,并启动该网卡,以便进行数据包的收发和处理操作
	if(rte_eth_dev_start(gDpdkPortId) < 0)
	{
		rte_exit(EXIT_FAILURE,"can not start\n");
	}
}

static int ng_encode_udp_pkt(uint8_t *msg,uint8_t *data,uint16_t total_len)
{
	//构造以太网头部(Ethernet Header),并将源MAC地址、目的MAC地址以及以太网类型(Ethernet Type)进行填充
	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头部(IPv4 Header)
	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;//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);

	//构造UDP头部(UDP Header)
	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);

    const char *source_str = "send day 2 by zxk";
    strcpy((char *)data, source_str);
	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(" zxk_send--> src: %s:%d, ", inet_ntoa(addr), ntohs(gSrcPort));
	addr.s_addr = gDstIp;
	printf("zxk_send 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)
{
	// 42是以太网头部(14字节)+ IPv4头部(20字节)+ UDP头部(8字节)
	const unsigned total_len = length + 42;

	// 使用rte_pktmbuf_alloc函数从指定的内存池中分配一个rte_mbuf结构
	struct rte_mbuf  *mbuf = rte_pktmbuf_alloc(mbuf_pool);
	if(!mbuf)
	{
		rte_exit(EXIT_FAILURE,"rte_pktmbuf_alloc fail\n");
	}
	// 设置rte_mbuf的数据包长度和实际数据长度
	mbuf->pkt_len = total_len;
	mbuf->data_len = total_len;

	// 获取rte_mbuf的数据指针
	uint8_t * pktdata = rte_pktmbuf_mtod(mbuf,uint8_t *);
	
	// 使用ng_encode_udp_pkt函数对rte_mbuf进行填充
	ng_encode_udp_pkt(pktdata,data,total_len);

	return mbuf;
}
#if ENABLE_ARP
/*
这段代码是一个函数 ng_encode_arp_pkt,用于构造 ARP 数据包的头部和数据部分。

msg: 指向数据包缓冲区的指针,用于存储构造的 ARP 数据包。
dst_mac: 目标主机的 MAC 地址,用于填充 ARP 数据包的目标 MAC 地址字段。
sip: 源 IP 地址,用于填充 ARP 数据包的源 IP 地址字段。
dip: 目标 IP 地址,用于填充 ARP 数据包的目标 IP 地址字段。

*/
static int ng_encode_arp_pkt(uint8_t *msg,uint8_t *dst_mac,uint32_t sip,uint32_t dip)	
{
	//构造以太网头部(Ethernet Header),并将源MAC地址、目的MAC地址以及以太网类型(Ethernet Type)进行填充
	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(Address Resolution Protocol)数据包的头部
	struct rte_arp_hdr *arp = (struct rte_arp_hdr *)(eth+1);
	arp->arp_hardware = htons(1);//1:以太网
	arp->arp_protocol = htons(RTE_ETHER_TYPE_IPV4);
	arp->arp_hlen = RTE_ETHER_ADDR_LEN;//设置 ARP 数据包的硬件地址长度字段。在以太网中,MAC 地址长度为 6 字节
	arp->arp_plen = sizeof(uint32_t);//设置 ARP 数据包的协议地址长度字段。在 IPv4 中,IP 地址长度为 4 字节
	arp->arp_opcode = htons(2); //设置 ARP 数据包的操作码字段。这里的值 2 表示 ARP Reply(响应)

#if DEBUG_LEVEL
	const char* mac_address = "00:11:22:33:44:55";
	sscanf(mac_address, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx",
			   &gSrcMac[0], &gSrcMac[1], &gSrcMac[2], &gSrcMac[3], &gSrcMac[4], &gSrcMac[5]);
	rte_memcpy(arp->arp_data.arp_sha.addr_bytes,gSrcMac,RTE_ETHER_ADDR_LEN);
#else
	rte_memcpy(arp->arp_data.arp_sha.addr_bytes,gSrcMac,RTE_ETHER_ADDR_LEN);
#endif
	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,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 fail\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,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 header
	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 header
	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;
	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);

	//ICMP header
	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 fail\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

int main(int argc,char *argv[])
{
	//初始化EAL环境
	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");
	}

	//mbuf_pool 是一个预先创建好的内存池,它将被用于接收队列来存储数据包的缓冲区
	ng_init_port(mbuf_pool);

	rte_eth_macaddr_get(gDpdkPortId, (struct rte_ether_addr *)gSrcMac);
	while(1)
	{
		struct rte_mbuf *mbufs[BURST_SIZE];
		//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++)
		{
			//rte_ether_hdr是DPDK 中用于表示以太网数据包头部的结构体
			//rte_pktmbuf_mtod用于将数据包缓冲区中的数据指针转换为特定类型的指针,以方便对数据包头部进行解析
			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("zxk arp ---> src: %s ", inet_ntoa(addr));

				addr.s_addr = gLocalIp;
				printf("zxk local: %s \n", inet_ntoa(addr));

				//只处理ip地址是本机的arp数据包
				if(ahdr->arp_data.arp_tip == gLocalIp)
				{
					struct rte_mbuf *arpbuf = ng_send_arp(mbuf_pool,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);

						rte_pktmbuf_free(mbufs[i]);
				}
				continue;
			}
#endif

			//rte_cpu_to_be_16用于将 16 位的数据从主机字节序(CPU 字节序)转换为网络字节序(大端字节序)
			if(ehdr->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
			{
				continue;
			}
			//rte_pktmbuf_mtod_offset来获取数据包缓冲区中 IPv4 头部的指针
			//将数据包偏移以太网数据包头部大小后,就是IPV4头部信息,再转换为struct rte_ipv4_hdr *
			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)
			{
				//(iphdr + 1) +1指的是偏移rte_ipv4_hdr(iphdr类型)大小
				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\n",inet_ntoa(addr),ntohs(udphdr->src_port));

				addr.s_addr = iphdr->dst_addr;
				printf("dst: %s:%d data:%s\n",inet_ntoa(addr),ntohs(udphdr->dst_port),(char *)(udphdr+1));
#if ENABLE_SEND
				//ng_send构建好的数据包,udphdr + 1 表示数据包的有效负载
				struct rte_mbuf * txbuf = ng_send_udp(mbuf_pool,(uint8_t *)(udphdr+1),length);
				/*
				第一个参数是要发送的以太网端口的 ID,这里是 gDpdkPortId。
				第二个参数是发送队列的 ID,这里是 0,表示发送到队列 0。
				第三个参数是一个指向 rte_mbuf 指针数组的指针,用于指定要发送的数据包。
				最后一个参数是要发送的数据包的数量,这里是 1,因为只发送了一个数据包。
				*/
				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("zxk_icmp ---> src: %s ", inet_ntoa(addr));

			if (icmphdr->icmp_type == RTE_IP_ICMP_ECHO_REQUEST) 
			{
				addr.s_addr = iphdr->dst_addr;
				printf("zxk 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
		}
	}
	  return 0;
}



OK ,ICMP reply实现完成!!明天再冲!

你可能感兴趣的:(DPDK,网络)