ZBLOG

19版本dpdk输出数据包ip地址 mac地址 tcp端口 代码

以下代码可以输出收到的数据包的IP地址、MAC地址和TCP端口。

  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <inttypes.h>
  5. #include <rte_eal.h>
  6. #include <rte_ethdev.h>
  7. #include <rte_mbuf.h>
  8. #define RX_RING_SIZE 256
  9. #define NUM_MBUFS 8191
  10. #define MBUF_CACHE_SIZE 250
  11. #define BURST_SIZE 32
  12. static const struct rte_eth_conf port_conf_default = {
  13. .rxmode = {
  14. .max_rx_pkt_len = ETHER_MAX_LEN,
  15. },
  16. };
  17. int main(int argc, char *argv[]) {
  18. int ret;
  19. uint16_t portid;
  20. uint16_t nb_ports;
  21. struct rte_eth_conf port_conf = port_conf_default;
  22. struct rte_mempool *mbuf_pool;
  23. struct rte_eth_rxconf rxq_conf;
  24. struct rte_eth_dev_info dev_info;
  25. uint16_t nb_rxd = RX_RING_SIZE;
  26. uint16_t nb_txd = 0;
  27. uint16_t q;
  28. struct rte_eth_stats stats;
  29. ret = rte_eal_init(argc, argv);
  30. if (ret < 0)
  31. rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
  32. nb_ports = rte_eth_dev_count_avail();
  33. if (nb_ports < 1)
  34. rte_exit(EXIT_FAILURE, "Error: no Ethernet ports found.\n");
  35. portid = 0;
  36. mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS,
  37. MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
  38. if (mbuf_pool == NULL)
  39. rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
  40. printf("Initializing port %"PRIu16"...\n", portid);
  41. ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
  42. if (ret < 0)
  43. rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%"PRIu16"\n",
  44. ret, portid);
  45. ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
  46. if (ret < 0)
  47. rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: err=%d, port=%"PRIu16"\n",
  48. ret, portid);
  49. rte_eth_macaddr_get(portid, &port_conf_default.rx_adv_conf.rss_conf.rss_key);
  50. rxq_conf = dev_info.default_rxconf;
  51. rxq_conf.offloads = port_conf_default.rxmode.offloads;
  52. ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd, rte_eth_dev_socket_id(portid),
  53. &rxq_conf, mbuf_pool);
  54. if (ret < 0)
  55. rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%"PRIu16"\n",
  56. ret, portid);
  57. ret = rte_eth_dev_start(portid);
  58. if (ret < 0)
  59. rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%"PRIu16"\n",
  60. ret, portid);
  61. printf("Port %d started\n", portid);
  62. printf("Accepting packets. Press Ctrl+C to quit.\n");
  63. for (;;) {
  64. struct rte_mbuf *bufs[BURST_SIZE];
  65. const uint16_t nb_rx = rte_eth_rx_burst(portid, 0, bufs, BURST_SIZE);
  66. if (unlikely(nb_rx == 0))
  67. continue;
  68. for (q = 0; q < nb_rx; q++) {
  69. struct rte_mbuf *mbuf = bufs[q];
  70. struct ether_hdr *eth_hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr*);
  71. if (eth_hdr->ether_type == htons(ETHER_TYPE_IPv4)) {
  72. struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
  73. uint32_t src_ip = rte_be_to_cpu_32(ipv4_hdr->src_addr);
  74. uint32_t dst_ip = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
  75. printf("Received packet with src IP address %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
  76. " and dst IP address %"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"\n",
  77. (uint8_t)(src_ip >> 24), (uint8_t)(src_ip >> 16),
  78. (uint8_t)(src_ip >> 8), (uint8_t)src_ip,
  79. (uint8_t)(dst_ip >> 24), (uint8_t)(dst_ip >> 16),
  80. (uint8_t)(dst_ip >> 8), (uint8_t)dst_ip);
  81. } else if (eth_hdr->ether_type == htons(ETHER_TYPE_ARP)) {
  82. // Handle ARP packets
  83. } else if (eth_hdr->ether_type == htons(ETHER_TYPE_VLAN)) {
  84. // Handle VLAN-tagged packets
  85. } else {
  86. printf("Received packet with unknown Ethernet type: %"PRIx16"\n", eth_hdr->ether_type);
  87. }
  88. rte_pktmbuf_free(mbuf);
  89. }
  90. }
  91. return 0;
  92. }

注意,上述代码只处理IPv4数据包。如果需要处理其他类型的数据包,请在if (eth_hdr->ether_type == htons(ETHER_TYPE_IPv4))语句后添加相应的处理代码。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=440

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?