• 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 修改一下即可。

    1. //使用win10 socket 客户端发送”hello dpdk“,收到相同的回包
    2. ubuntu:~/share/dpdk/dpdk-stable-19.08.2/examples/mysend/build$ sudo ./udpsend
    3. EAL: Detected 8 lcore(s)
    4. EAL: Detected 1 NUMA nodes
    5. EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
    6. EAL: Selected IOVA mode 'PA'
    7. EAL: Probing VFIO support...
    8. EAL: VFIO support initialized
    9. EAL: PCI device 0000:02:01.0 on NUMA socket -1
    10. EAL: Invalid NUMA socket, default to 0
    11. EAL: probe driver: 8086:100f net_e1000_em
    12. EAL: PCI device 0000:02:06.0 on NUMA socket -1
    13. EAL: Invalid NUMA socket, default to 0
    14. EAL: probe driver: 8086:100f net_e1000_em
    15. EAL: PCI device 0000:03:00.0 on NUMA socket -1
    16. EAL: Invalid NUMA socket, default to 0
    17. EAL: probe driver: 15ad:7b0 net_vmxnet3
    18. EAL: PCI device 0000:0b:00.0 on NUMA socket -1
    19. EAL: Invalid NUMA socket, default to 0
    20. EAL: probe driver: 15ad:7b0 net_vmxnet3
    21. src: 192.168.1.20:10000, dst: 192.168.1.30:60000, hello dpdk
    22. --> src: 192.168.1.30:60000, dst: 192.168.1.20:10000

    2. DPDK API 学习

    2.1. rte_pktmbuf_pool_create()

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

    2.2. rte_eth_dev_count_avail()

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

    2.3. rte_eth_dev_info_get()

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

    2.4. rte_eth_dev_configure()

    1. #include <rte_ethdev.h>
    2. 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);
    3. //作用:用于配置 DPDK 网卡设备的接收和发送队列的数量以及端口的配置信息。
    4. //参数:
    5. // port_id: 要配置的网卡设备的端口编号
    6. // nb_rx_queues: 接收队列的数量
    7. // nb_tx_queues: 发送队列的数量
    8. // dev_conf: 一个指向 rte_eth_conf 结构体的指针,包含了要应用的端口配置信息。

    2.5. rte_eth_rx_queue_setup()

    1. #include <rte_ethdev.h>
    2. 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);
    3. //作用:用于设置 DPDK 网卡设备的接收队列。
    4. //参数:
    5. // port_id: 要配置的网卡设备的端口编号
    6. // tx_queue_id: 接收队列的编号
    7. // nb_tx_desc : 发送队列的数量,描述符是用于存储待发送数据包的数据结构,这个参数决定了发送队列的大小,即可以同时存储待发送的数据包的最大数量。
    8. // socket_id : 一发送队列的 NUMA 节点编号,用于指定内存分配的位置。
    9. // rx_conf : 一个指向 rte_eth_txconf 结构体的指针,包含了接收队列的配置信息。
    10. // mb_pool : 一个指向 rte_mempool 结构体的指针,用于指定接收队列接收数据包时的内存管理。

    2.6. rte_eth_tx_queue_setup()

    1. #include <rte_ethdev.h>
    2. 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);
    3. //作用:用于设置 DPDK 网卡设备的发送队列。
    4. //参数:
    5. // port_id: 要配置的网卡设备的端口编号
    6. // tx_queue_id: 接收队列的编号
    7. // nb_tx_desc : 发送队列的数量,描述符是用于存储待发送数据包的数据结构,这个参数决定了发送队列的大小,即可以同时存储待发送的数据包的最大数量。
    8. // socket_id : 一发送队列的 NUMA 节点编号,用于指定内存分配的位置。
    9. // tx_conf : 一个指向 rte_eth_txconf 结构体的指针,包含了发送队列的配置信息。

    2.7. rte_eth_dev_start()

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

    2.8. rte_eth_macaddr_get()

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

    2.9. rte_eth_rx_burst()

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

    2.10. rte_pktmbuf_mtod()

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

    2.11. rte_pktmbuf_mtod_offset()

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

    2.12. rte_eth_tx_burst()

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

    3. DPDK UDP 通信源码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #define ENABLE_SEND 1
    7. #define ENABLE_ARP 1
    8. #define NUM_MBUFS (4096-1)
    9. #define BURST_SIZE 32
    10. #if ENABLE_SEND
    11. static uint32_t gSrcIp; //
    12. static uint32_t gDstIp;
    13. static uint8_t gSrcMac[RTE_ETHER_ADDR_LEN];
    14. static uint8_t gDstMac[RTE_ETHER_ADDR_LEN];
    15. static uint16_t gSrcPort;
    16. static uint16_t gDstPort;
    17. #endif
    18. int gDpdkPortId = 0;
    19. static const struct rte_eth_conf port_conf_default = {
    20. .rxmode = {.max_rx_pkt_len = RTE_ETHER_MAX_LEN }
    21. };
    22. static void ng_init_port(struct rte_mempool *mbuf_pool) {
    23. // 获取绑定dpdk网卡的个数
    24. uint16_t nb_sys_ports= rte_eth_dev_count_avail(); //
    25. if (nb_sys_ports == 0) {
    26. rte_exit(EXIT_FAILURE, "No Supported eth found\n");
    27. }
    28. //获取指定端口的设备信息,包括设备的能力、特性
    29. struct rte_eth_dev_info dev_info;
    30. rte_eth_dev_info_get(gDpdkPortId, &dev_info);
    31. const int num_rx_queues = 1;
    32. const int num_tx_queues = 1;
    33. struct rte_eth_conf port_conf = port_conf_default;
    34. //用于配置指定端口的接收队列和发送队列的数量,以及端口的配置信息。
    35. rte_eth_dev_configure(gDpdkPortId, num_rx_queues, num_tx_queues, &port_conf);
    36. //用于设置指定端口的接收队列的参数。
    37. if (rte_eth_rx_queue_setup(gDpdkPortId, 0 , 1024, rte_eth_dev_socket_id(gDpdkPortId),NULL, mbuf_pool) < 0) {
    38. rte_exit(EXIT_FAILURE, "Could not setup RX queue\n");
    39. }
    40. struct rte_eth_txconf txq_conf = dev_info.default_txconf;
    41. txq_conf.offloads = port_conf.rxmode.offloads;
    42. //用于设置指定端口的发送队列的参数。
    43. if (rte_eth_tx_queue_setup(gDpdkPortId, 0 , 1024, rte_eth_dev_socket_id(gDpdkPortId), &txq_conf) < 0) {
    44. rte_exit(EXIT_FAILURE, "Could not setup TX queue\n");
    45. }
    46. //用于启动指定端口的数据包收发功能,使得网卡端口能够开始收发数据。
    47. if (rte_eth_dev_start(gDpdkPortId) < 0 ) {
    48. rte_exit(EXIT_FAILURE, "Could not start\n");
    49. }
    50. }
    51. static int ng_encode_udp_pkt(uint8_t *msg, unsigned char *data, uint16_t total_len) {
    52. // 1 ethhdr
    53. struct rte_ether_hdr *eth = (struct rte_ether_hdr *)msg;
    54. rte_memcpy(eth->s_addr.addr_bytes, gSrcMac, RTE_ETHER_ADDR_LEN);
    55. rte_memcpy(eth->d_addr.addr_bytes, gDstMac, RTE_ETHER_ADDR_LEN);
    56. eth->ether_type = htons(RTE_ETHER_TYPE_IPV4);
    57. // 2 iphdr
    58. struct rte_ipv4_hdr *ip = (struct rte_ipv4_hdr *)(msg + sizeof(struct rte_ether_hdr));
    59. ip->version_ihl = 0x45;
    60. ip->type_of_service = 0;
    61. ip->total_length = htons(total_len - sizeof(struct rte_ether_hdr));
    62. ip->packet_id = 0;
    63. ip->fragment_offset = 0;
    64. ip->time_to_live = 64; // ttl = 64
    65. ip->next_proto_id = IPPROTO_UDP;
    66. ip->src_addr = gSrcIp;
    67. ip->dst_addr = gDstIp;
    68. ip->hdr_checksum = 0;
    69. ip->hdr_checksum = rte_ipv4_cksum(ip);
    70. // 3 udphdr
    71. struct rte_udp_hdr *udp = (struct rte_udp_hdr *)(msg + sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr));
    72. udp->src_port = gSrcPort;
    73. udp->dst_port = gDstPort;
    74. uint16_t udplen = total_len - sizeof(struct rte_ether_hdr) - sizeof(struct rte_ipv4_hdr);
    75. udp->dgram_len = htons(udplen);
    76. rte_memcpy((uint8_t*)(udp+1), data, udplen);
    77. udp->dgram_cksum = 0;
    78. udp->dgram_cksum = rte_ipv4_udptcp_cksum(ip, udp);
    79. struct in_addr addr;
    80. addr.s_addr = gSrcIp;
    81. printf(" --> src: %s:%d, ", inet_ntoa(addr), ntohs(gSrcPort));
    82. addr.s_addr = gDstIp;
    83. printf("dst: %s:%d\n", inet_ntoa(addr), ntohs(gDstPort));
    84. return 0;
    85. }
    86. static struct rte_mbuf * ng_send(struct rte_mempool *mbuf_pool, uint8_t *data, uint16_t length) {
    87. // mempool --> mbuf
    88. const unsigned total_len = length + 42;
    89. struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool);
    90. if (!mbuf) {
    91. rte_exit(EXIT_FAILURE, "rte_pktmbuf_alloc\n");
    92. }
    93. mbuf->pkt_len = total_len;
    94. mbuf->data_len = total_len;
    95. uint8_t *pktdata = rte_pktmbuf_mtod(mbuf, uint8_t*);
    96. ng_encode_udp_pkt(pktdata, data, total_len);
    97. return mbuf;
    98. }
    99. int main(int argc, char *argv[]) {
    100. //初始化 EAL 环境
    101. if (rte_eal_init(argc, argv) < 0) {
    102. rte_exit(EXIT_FAILURE, "Error with EAL init\n");
    103. }
    104. //创建 mbuf 内存池
    105. struct rte_mempool *mbuf_pool = rte_pktmbuf_pool_create("mbuf pool", NUM_MBUFS, 0, 0,
    106. RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
    107. if (mbuf_pool == NULL) {
    108. rte_exit(EXIT_FAILURE, "Could not create mbuf pool\n");
    109. }
    110. ng_init_port(mbuf_pool);
    111. //获取指定接口的 MAC 地址
    112. rte_eth_macaddr_get(gDpdkPortId, (struct rte_ether_addr *)gSrcMac);
    113. while (1) {
    114. struct rte_mbuf *mbufs[BURST_SIZE];
    115. //接收 rx 队列中的数据
    116. unsigned num_recvd = rte_eth_rx_burst(gDpdkPortId, 0, mbufs, BURST_SIZE);
    117. if (num_recvd > BURST_SIZE) {
    118. rte_exit(EXIT_FAILURE, "Error receiving from eth\n");
    119. }
    120. unsigned i = 0;
    121. for (i = 0;i < num_recvd;i ++) {
    122. //将一个 MBUF 中的数据缓冲区转换为相应的数据类型的指针
    123. struct rte_ether_hdr *ehdr = rte_pktmbuf_mtod(mbufs[i], struct rte_ether_hdr*);
    124. if (ehdr->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
    125. continue;
    126. }
    127. struct rte_ipv4_hdr *iphdr = rte_pktmbuf_mtod_offset(mbufs[i], struct rte_ipv4_hdr *,
    128. sizeof(struct rte_ether_hdr));
    129. if (iphdr->next_proto_id == IPPROTO_UDP) {
    130. struct rte_udp_hdr *udphdr = (struct rte_udp_hdr *)(iphdr + 1);
    131. rte_memcpy(gDstMac, ehdr->s_addr.addr_bytes, RTE_ETHER_ADDR_LEN);
    132. rte_memcpy(&gSrcIp, &iphdr->dst_addr, sizeof(uint32_t));
    133. rte_memcpy(&gDstIp, &iphdr->src_addr, sizeof(uint32_t));
    134. rte_memcpy(&gSrcPort, &udphdr->dst_port, sizeof(uint16_t));
    135. rte_memcpy(&gDstPort, &udphdr->src_port, sizeof(uint16_t));
    136. uint16_t length = ntohs(udphdr->dgram_len);
    137. *((char*)udphdr + length) = '\0';
    138. struct in_addr addr;
    139. addr.s_addr = iphdr->src_addr;
    140. printf("src: %s:%d, ", inet_ntoa(addr), ntohs(udphdr->src_port));
    141. addr.s_addr = iphdr->dst_addr;
    142. printf("dst: %s:%d, %s\n", inet_ntoa(addr), ntohs(udphdr->dst_port), (char *)(udphdr+1));
    143. struct rte_mbuf *txbuf = ng_send(mbuf_pool, (uint8_t *)(udphdr+1), length);
    144. rte_eth_tx_burst(gDpdkPortId, 0, &txbuf, 1);
    145. rte_pktmbuf_free(txbuf);
    146. rte_pktmbuf_free(mbufs[i]);
    147. }
    148. }
    149. }
    150. }
  • 相关阅读:
    python切片选取行和列
    element -ui table表格内容无限滚动 使用插件vue-seamless-scroll
    Java之线程详解(三)——多线程常用API、七种状态、优先级、Lock锁
    用R语言模拟混合制排队随机服务排队系统
    最强总结!18个机器学习核心算法模型!!
    高可用技术
    ubuntu静态ip地址设置
    Linux内存管理(十):unflatten_device_tree 详解
    技术面经总结
    dayjs 笔记
  • 原文地址:https://blog.csdn.net/A152419/article/details/138168953