ZBLOG

DPDK初始化rss的代码

以下是DPDK初始化RSS的示例代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <sys/queue.h>
  7. #include <rte_common.h>
  8. #include <rte_ethdev.h>
  9. #define RX_RING_SIZE 1024
  10. #define TX_RING_SIZE 1024
  11. static const struct rte_eth_conf port_conf_default = {
  12. .rxmode = {
  13. .max_rx_pkt_len = ETHER_MAX_LEN,
  14. .split_hdr_size = 0,
  15. .header_split = 0, /**< Header Split disabled */
  16. .hw_ip_checksum = 1, /**< IP checksum offload enabled */
  17. .hw_vlan_filter = 0, /**< VLAN filtering disabled */
  18. .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
  19. .hw_strip_crc = 1, /**< CRC stripped by hardware */
  20. },
  21. .txmode = {
  22. .mq_mode = ETH_MQ_TX_NONE,
  23. },
  24. };
  25. int main(int argc, char **argv)
  26. {
  27. int ret;
  28. uint16_t nb_ports;
  29. uint16_t portid;
  30. // 初始化EAL环境
  31. ret = rte_eal_init(argc, argv);
  32. if (ret < 0) {
  33. rte_exit(EXIT_FAILURE, "Error: Failed to initialize EAL (%s)\n", strerror(-ret));
  34. }
  35. // 获取可用端口数
  36. nb_ports = rte_eth_dev_count_avail();
  37. if (nb_ports == 0) {
  38. rte_exit(EXIT_FAILURE, "Error: No available network ports\n");
  39. }
  40. // 配置每个端口
  41. for (portid = 0; portid < nb_ports; portid++) {
  42. struct rte_eth_conf port_conf;
  43. struct rte_eth_rxconf rxq_conf;
  44. struct rte_eth_txconf txq_conf;
  45. struct rte_eth_dev_info dev_info;
  46. // 获取端口设备信息
  47. memset(&dev_info, 0, sizeof(dev_info));
  48. rte_eth_dev_info_get(portid, &dev_info);
  49. // 设置rx和tx队列配置
  50. memset(&rxq_conf, 0, sizeof(rxq_conf));
  51. rxq_conf.rx_thresh.pthresh = 8;
  52. rxq_conf.rx_thresh.hthresh = 8;
  53. rxq_conf.rx_thresh.wthresh = 4;
  54. rxq_conf.rx_free_thresh = 32;
  55. memset(&txq_conf, 0, sizeof(txq_conf));
  56. txq_conf.tx_rs_thresh = 32;
  57. txq_conf.tx_free_thresh = 0;
  58. txq_conf.tx_thresh.pthresh = 36;
  59. txq_conf.tx_thresh.hthresh = 0;
  60. txq_conf.tx_thresh.wthresh = 0;
  61. // 初始化port configuration structure with default values
  62. port_conf = port_conf_default;
  63. // 配置rss功能
  64. if (dev_info.reta_size > RTE_ETH_RSS_RETA_SIZE_64) {
  65. uint8_t rss_key[] = {52,102,121,161,177};
  66. uint8_t rss_hash_func =
  67. ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP |
  68. ETH_RSS_SCTP | ETH_RSS_L2_PAYLOAD |
  69. ETH_RSS_IPV6_EX | ETH_RSS_IPV6_TCP_EX |
  70. ETH_RSS_IPV6_UDP_EX | ETH_RSS_IPV6_EX_L2_PAYLOAD |
  71. ETH_RSS_NONFRAG_IPV4_OTHER |
  72. ETH_RSS_NONFRAG_IPV4_UDP |
  73. ETH_RSS_NONFRAG_IPV4_TCP |
  74. ETH_RSS_NONFRAG_IPV6_OTHER |
  75. ETH_RSS_NONFRAG_IPV6_UDP |
  76. ETH_RSS_NONFRAG_IPV6_TCP;
  77. ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
  78. if (ret < 0) {
  79. rte_exit(EXIT_FAILURE, "Error: Failed to configure port %u (%s)\n", portid, strerror(-ret));
  80. }
  81. ret = rte_eth_dev_rss_hash_update(portid, rss_key,
  82. sizeof(rss_key),
  83. rss_hash_func);
  84. if (ret < 0) {
  85. rte_exit(EXIT_FAILURE, "Error: Failed to update RSS hash functions for port %u (%s)\n",
  86. portid, strerror(-ret));
  87. }
  88. uint16_t i;
  89. struct ether_addr eth_addr;
  90. memset(&eth_addr.addr_bytes[0], 0x00, ETHER_ADDR_LEN);
  91. ret = rte_eth_macaddr_get(portid,&eth_addr);
  92. if(ret != 0){
  93. printf("Faild to get MAC address of port :%d \n",portid);
  94. return -1;
  95. }
  96. printf("MAC address of port:%d is %02X:%02X:%02X:%02X:%02X:%02X\n",
  97. portid,
  98. eth_addr.addr_bytes[0],
  99. eth_addr.addr_bytes[1],
  100. eth_addr.addr_bytes[2],
  101. eth_addr.addr_bytes[3],
  102. eth_addr.addr_bytes[4],
  103. eth_addr.addr_bytes[5]);
  104. } else {
  105. ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
  106. if (ret < 0) {
  107. rte_exit(EXIT_FAILURE, "Error: Failed to configure port %u (%s)\n", portid,strerror(-ret));
  108. }
  109. }
  110. // 初始化rx和tx队列
  111. ret = rte_eth_rx_queue_setup(portid, 0, RX_RING_SIZE,
  112. rte_eth_dev_socket_id(portid),
  113. &rxq_conf,
  114. rte_eth_rx_burst);
  115. if (ret < 0) {
  116. rte_exit(EXIT_FAILURE, "Error: Failed to setup rx queue for port %u (%s)\n", portid,strerror(-ret));
  117. }
  118. ret = rte_eth_tx_queue_setup(portid, 0, TX_RING_SIZE,
  119. rte_eth_dev_socket_id(portid),
  120. &txq_conf);
  121. if (ret < 0) {
  122. rte_exit(EXIT_FAILURE, "Error: Failed to setup tx queue for port %u (%s)\n", portid,strerror(-ret));
  123. }
  124. // 启动端口
  125. ret = rte_eth_dev_start(portid);
  126. if (ret < 0) {
  127. rte_exit(EXIT_FAILURE, "Error: Failed to start port %u (%s)\n", portid,strerror(-ret));
  128. }
  129. }
  130. }

此示例演示了如何初始化DPDK以使用RSS功能。在主函数中,我们首先调用rte_eal_init()来初始化EAL环境,然后获取可用端口数,并遍历每个端口以配置其rx和tx队列。

对于支持更大的RETAsize的设备,我们调用rte_eth_dev_rss_hash_update()函数来启用RSS哈希功能,并将所需的哈希键、哈希函数等参数传递给它。否则,我们只是简单地配置端口并启动它。

请注意,在此示例中未显示完整错误检查代码。实际应用程序中,您应该在每个函数调用后检查返回值以确保操作成功完成。

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?