DPDK UDP通信

1. 编译运行程序

环境配置:

win10 运行 socket 客户端工具 + Linux DPDK 运行 UDP 程序

注意事项:

  1. DPDK 跳过内核协议栈,所以 ARP 协议也不支持,需要手动在 win10 上配置静态 arp 地址,保证数据包发到网卡。
  • netsh i i show in : 查看与 Linux 主机通信的网卡 Idx 编号。
  • netsh -c i i add neighbors Idx IP MAC
  • 示例:netsh -c i i add neighbors 23 192.168.1.30 00-0c-29-7d-80-e1

编译:

从官方提供的 demo 中拷贝个 Makefile 修改一下即可。

//使用win10 socket 客户端发送”hello dpdk“,收到相同的回包

ubuntu:~/share/dpdk/dpdk-stable-19.08.2/examples/mysend/build$ sudo ./udpsend
EAL: Detected 8 lcore(s)
EAL: Detected 1 NUMA nodes
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'PA'
EAL: Probing VFIO support...
EAL: VFIO support initialized
EAL: PCI device 0000:02:01.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:06.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:03:00.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 15ad:7b0 net_vmxnet3
EAL: PCI device 0000:0b:00.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 15ad:7b0 net_vmxnet3
src: 192.168.1.20:10000, dst: 192.168.1.30:60000, hello dpdk
 --> src: 192.168.1.30:60000, dst: 192.168.1.20:10000

2. DPDK API 学习

2.1. rte_pktmbuf_pool_create()

#include <rte_mempool.h>
struct rte_mempool *rte_pktmbuf_pool_create(const char *name, unsigned n, unsigned cache_size, uint16_t priv_size, 
                                            uint16_t data_room_size, int socket_id);
//作用:创建一个 DPDK 内存池,以供存储数据包缓冲区(MBUFs)
//参数:
// name: 内存池名称
// n   : 内存池中MBUF的数量。
// cache_size : 每个核心的缓存MBUF的数量。DPDK 会为每个核心创建一个缓存,用于存储从内存池中获取的MBUF,以提高性能。
// priv_size  : MBUF私有数据的大小。
// data_room_size: MBUF中数据缓冲区的大小。这个参数决定了每个MBUF中数据缓冲区的大小,即可以存储数据的最大长度。
// socket_id  : 指定内存池分配的NUMA节点。

2.2. rte_eth_dev_count_avail()

#include <rte_ethdev.h>
uint16_t rte_eth_dev_count_avail(void);
//作用:函数用于获取系统中可用的 DPDK 网卡端口的数量。

2.3. rte_eth_dev_info_get()

#include <rte_ethdev.h>
void rte_eth_dev_info_get(uint16_t port_id, struct rte_eth_dev_info *dev_info);
//作用:用于获取指定网卡设备的信息。
//参数:
// port_id:  指定网卡设备的端口编号。
// dev_info:  一个指向 rte_eth_dev_info 结构体的指针,用于存储获取到的网卡设备信息。

2.4. rte_eth_dev_configure()

#include <rte_ethdev.h>
int rte_eth_dev_configure(uint16_t port_id, uint16_t nb_rx_queues, uint16_t nb_tx_queues, const struct rte_eth_conf *dev_conf);
//作用:用于配置 DPDK 网卡设备的接收和发送队列的数量以及端口的配置信息。
//参数:
// port_id: 要配置的网卡设备的端口编号
// nb_rx_queues: 接收队列的数量
// nb_tx_queues: 发送队列的数量
// dev_conf: 一个指向 rte_eth_conf 结构体的指针,包含了要应用的端口配置信息。

2.5. rte_eth_rx_queue_setup()

#include <rte_ethdev.h>
int rte_eth_rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id, uint16_t nb_rx_desc, unsigned int socket_id, const struct rte_eth_rxconf *rx_conf, struct rte_mempool *mb_pool);
//作用:用于设置 DPDK 网卡设备的接收队列。
//参数:
// port_id: 要配置的网卡设备的端口编号
// tx_queue_id:  接收队列的编号
// nb_tx_desc :  发送队列的数量,描述符是用于存储待发送数据包的数据结构,这个参数决定了发送队列的大小,即可以同时存储待发送的数据包的最大数量。
// socket_id  :  一发送队列的 NUMA 节点编号,用于指定内存分配的位置。
// rx_conf    :  一个指向 rte_eth_txconf 结构体的指针,包含了接收队列的配置信息。
// mb_pool    :  一个指向 rte_mempool 结构体的指针,用于指定接收队列接收数据包时的内存管理。

2.6. rte_eth_tx_queue_setup()

#include <rte_ethdev.h>
int rte_eth_tx_queue_setup(uint16_t port_id, uint16_t nb_rx_queues, uint16_t nb_tx_queues, const struct rte_eth_conf *dev_conf);
//作用:用于设置 DPDK 网卡设备的发送队列。
//参数:
// port_id: 要配置的网卡设备的端口编号
// tx_queue_id:  接收队列的编号
// nb_tx_desc :  发送队列的数量,描述符是用于存储待发送数据包的数据结构,这个参数决定了发送队列的大小,即可以同时存储待发送的数据包的最大数量。
// socket_id  :  一发送队列的 NUMA 节点编号,用于指定内存分配的位置。
// tx_conf    :  一个指向 rte_eth_txconf 结构体的指针,包含了发送队列的配置信息。

2.7. rte_eth_dev_start()

#include <rte_ethdev.h>
int rte_eth_dev_start(uint16_t port_id);
//作用:用于启动指定端口的数据包收发功能。
//参数:
//port_id:要启动的网卡设备的端口编号。

2.8. rte_eth_macaddr_get()

#include <rte_ethdev.h>
void rte_eth_macaddr_get(uint16_t port_id, struct rte_ether_addr *addr);
//作用:用于获取指定端口的 MAC 地址。
//参数:
//port_id:网卡设备的端口编号。
//addr   :一个指向 rte_ether_addr 结构体的指针,用于存储获取到的 MAC 地址。

2.9. rte_eth_rx_burst()

#include <rte_ethdev.h>
uint16_t rte_eth_rx_burst(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **rx_pkts, const uint16_t nb_pkts);
//作用:用于从指定端口的接收队列中接收数据包。
//参数:
//port_id :网卡设备的端口编号。
//queue_id:要从哪个接收队列中接收数据包。
//rx_pkts :用于存储接收到的数据包的指针数组。
//nb_pkts :指定要接收的最大数据包数量。
//返回值:
//返回实际接收到的数据包的数量。

2.10. rte_pktmbuf_mtod()

#include <rte_mbuf.h>
void *rte_pktmbuf_mtod(const struct rte_mbuf *m, void *);
//作用:作用是将一个 MBUF 中的数据缓冲区转换为相应的数据类型的指针。
//参数:
// m:指向 MBUF 的指针,表示待转换的 MBUF。
// void *:表示要转换的数据类型的指针。这个参数指定了将 MBUF 中的数据缓冲区转换为何种类型的指针。

2.11. rte_pktmbuf_mtod_offset()

#include <rte_mbuf.h>
void *rte_pktmbuf_mtod_offset(const struct rte_mbuf *m, uint16_t offset, uint16_t data_len);
//作用:用于将一个 MBUF 中的数据缓冲区偏移量转换为相应的数据类型的指针。
//参数:
// m:指向 MBUF 的指针。
// offset  :数据缓冲区的偏移量。这是指相对于 MBUF 数据字段起始位置的偏移量。
// data_len:数据长度。这个参数用于检查偏移量是否超出了有效数据范围,如果超出了则会触发异常处理。

2.12. rte_eth_tx_burst()

#include <rte_ethdev.h>
uint16_t rte_eth_tx_burst(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
//作用:用于向指定的端口发送一组数据包。
//参数:
//port_id :表示目标端口的端口编号。
//queue_id:表示目标端口的发送队列编号。
//tx_pkts :表示待发送的数据包数组的指针。
//nb_pkts :指定要发送的最大数据包数量。
//返回值:
//返回实际接收到的数据包的数量。

3. DPDK UDP 通信源码

#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_mbuf.h>

#include <stdio.h>
#include <arpa/inet.h>

#define ENABLE_SEND		1
#define ENABLE_ARP		1

#define NUM_MBUFS (4096-1)

#define BURST_SIZE	32

#if ENABLE_SEND

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

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, "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");
	}
	
	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");
	}

    //用于启动指定端口的数据包收发功能,使得网卡端口能够开始收发数据。
	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) {

	// 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(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;
}

int main(int argc, char *argv[]) {

    //初始化 EAL 环境
	if (rte_eal_init(argc, argv) < 0) {
		rte_exit(EXIT_FAILURE, "Error with EAL init\n");
	}

    //创建 mbuf 内存池
	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);

    //获取指定接口的 MAC 地址
	rte_eth_macaddr_get(gDpdkPortId, (struct rte_ether_addr *)gSrcMac);

	while (1) {

		struct rte_mbuf *mbufs[BURST_SIZE];
        //接收 rx 队列中的数据 
		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 ++) {
            //将一个 MBUF 中的数据缓冲区转换为相应的数据类型的指针
			struct rte_ether_hdr *ehdr = rte_pktmbuf_mtod(mbufs[i], struct rte_ether_hdr*);
			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);

				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));

				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));

				struct rte_mbuf *txbuf = ng_send(mbuf_pool, (uint8_t *)(udphdr+1), length);
				rte_eth_tx_burst(gDpdkPortId, 0, &txbuf, 1);
				rte_pktmbuf_free(txbuf);

				rte_pktmbuf_free(mbufs[i]);
			}
			
		}

	}

}
  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值