二、DPDK之发送UDP报文

昨天写了UDP接收的代码,没有实现发送的代码。今天补全一下,我始终觉得学习这件事,必不可少的是动手实操,所以我都是尽量自己写代码,哪怕是抄一遍也会收获颇多,学习的时候写注释更能加深印象。
既然是发送UDP,那么还是将UDP格式回顾一下:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version|  IHL  |Type of Service|          Total Length         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|         Identification        |Flags|      Fragment Offset    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Time to Live |    Protocol   |        Header Checksum        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                         Source IP Address                     |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                      Destination IP Address                   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options                    |    Padding    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

附上代码以做记录`

#include<rte_eal.h>
#include<rte_ethdev.h>
#include<rte_mbuf.h>
#include<stdio.h>
#include<arpa/inet.h>

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


int gDpdkPortId = 0;//eth0
#if ENABLE_SEND
	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(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;
}

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 *);
			//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(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]);
			}
		}
	}
	  return 0;
}


结果如下,因为只是掌握流程,所以报文数据固定为“send day 2 by zxk” ,后面就是编译运行后,通过抓包验证是否成功了
在这里插入图片描述
OK,今天的UDP发送就结束了!!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

写一封情书

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值