Advertisement
Guest User

Untitled

a guest
Oct 4th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.49 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <signal.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <locale.h>
  9.  
  10. #include <rte_memzone.h>
  11. #include <rte_memcpy.h>
  12. #include <rte_ethdev.h>
  13. #include <rte_mempool.h>
  14. #include <rte_atomic.h>
  15.  
  16. #define RTE_LOGTYPE_APP         RTE_LOGTYPE_USER1
  17.  
  18. #define PKT_SIZE 60
  19. #define MBUF_SIZE (PKT_SIZE + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
  20.  
  21. #define PRINT_INTERVAL 1
  22.  
  23. #define BURST_SIZE 16
  24.  
  25. #define CALC_RX_STATS
  26.  
  27. #define RUN_TIME 30
  28.  
  29. /* function prototypes */
  30. void send_receive_loop(void);
  31. void init(void);
  32. void crtl_c_handler(int s);
  33. void ALARMhandler(int sig);
  34. inline int send_packets(struct rte_mbuf **packets, uint8_t port);
  35. void print_stats(void);
  36.  
  37. int send_loop(void * arg);
  38. int receive_loop(void * arg);
  39.  
  40. unsigned int counter = 0;
  41. volatile sig_atomic_t stop;
  42.  
  43. rte_atomic32_t rx_pkts = RTE_ATOMIC32_INIT(0);
  44.  
  45. struct rte_mempool *packets_pool = NULL;
  46.  
  47. uint8_t portid1 = 0;
  48. uint8_t portid2 = 1;
  49.  
  50. struct rte_eth_dev_info dev_info;
  51. /* TODO: verify this setup */
  52. static const struct rte_eth_conf port_conf = {
  53.     .rxmode = {
  54.         .split_hdr_size = 0,
  55.         .header_split = 0,
  56.         .hw_ip_checksum = 0,
  57.         .hw_vlan_filter = 0,
  58.         .jumbo_frame = 0,
  59.         .hw_strip_crc = 0,
  60.     },
  61.     .txmode = {
  62.         .mq_mode = ETH_MQ_TX_NONE,
  63.     },
  64. };
  65.  
  66. static uint16_t nb_rxd = 128;
  67. static uint16_t nb_txd = 512;
  68.  
  69. struct fargs
  70. {
  71.     uint8_t portid;
  72. };
  73.  
  74. int main(int argc, char *argv[])
  75. {
  76.     setlocale(LC_NUMERIC, "en_US.utf-8");
  77.  
  78.     int retval = 0;
  79.  
  80.     if ((retval = rte_eal_init(argc, argv)) < 0)
  81.         return -1;
  82.  
  83.     argc -= retval;
  84.     argv +=  retval;
  85.  
  86.  
  87.     init();
  88.  
  89.     RTE_LOG(INFO, APP, "Finished Process Init.\n");
  90.  
  91.     signal(SIGINT,crtl_c_handler);
  92.     signal(SIGALRM, ALARMhandler);
  93.     alarm(PRINT_INTERVAL);
  94.  
  95.     struct fargs f0;
  96.     f0.portid = 0;
  97.  
  98.     struct fargs f1;
  99.     f1.portid = 1;
  100.  
  101.     retval = rte_eal_remote_launch(receive_loop, &f0, 1);
  102.     if (retval) {
  103.         RTE_LOG(ERR, APP, "error launching remote function...\n");
  104.         return -1;
  105.     }
  106.  
  107.     retval = rte_eal_remote_launch(receive_loop, &f1, 2);
  108.     if (retval) {
  109.         RTE_LOG(ERR, APP, "error launching remote function...\n");
  110.         return -1;
  111.     }
  112.  
  113.     retval = rte_eal_remote_launch(send_loop, &f0, 3);
  114.     if (retval) {
  115.         RTE_LOG(ERR, APP, "error launching remote function...\n");
  116.         return -1;
  117.     }
  118.  
  119.     retval = rte_eal_remote_launch(send_loop, &f1, 4);
  120.     if (retval) {
  121.         RTE_LOG(ERR, APP, "error launching remote function...\n");
  122.         return -1;
  123.     }
  124.  
  125.     printf("press enter to continue....\n");
  126.     getchar();
  127.  
  128.     retval = rte_eal_wait_lcore(1);
  129.     if (retval) {
  130.         RTE_LOG(ERR, APP, "error waiting for core...");
  131.         return -1;
  132.     }
  133.  
  134.     retval = rte_eal_wait_lcore(2);
  135.     if (retval) {
  136.         RTE_LOG(ERR, APP, "error waiting for core...");
  137.         return -1;
  138.     }
  139.  
  140.     retval = rte_eal_wait_lcore(3);
  141.     if (retval) {
  142.         RTE_LOG(ERR, APP, "error waiting for core...");
  143.         return -1;
  144.     }
  145.  
  146.     retval = rte_eal_wait_lcore(4);
  147.     if (retval) {
  148.         RTE_LOG(ERR, APP, "error waiting for core...");
  149.         return -1;
  150.     }
  151.  
  152.     RTE_LOG(INFO, APP, "Done\n");
  153.     return 0;
  154. }
  155.  
  156. void init(void)
  157. {
  158.     int ret;
  159.  
  160.     packets_pool = rte_pktmbuf_pool_create("packets", 128*1024, 512,
  161.         0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
  162.     if (packets_pool == NULL) {
  163.         rte_exit(EXIT_FAILURE, "Cannot find memory pool\n");
  164.     }
  165.  
  166.     rte_eth_dev_info_get(portid1, &dev_info);
  167.  
  168.     ret = rte_eth_dev_configure(portid1, 1, 1, &port_conf);
  169.     if(ret < 0)
  170.         rte_exit(EXIT_FAILURE, "Cannot configure device\n");
  171.  
  172.     ret = rte_eth_tx_queue_setup(portid1, 0, nb_txd, SOCKET_ID_ANY, NULL);
  173.     if(ret < 0)
  174.         rte_exit(EXIT_FAILURE, "Cannot configure device tx queue\n");
  175.  
  176.     ret = rte_eth_rx_queue_setup(portid1, 0, nb_rxd, SOCKET_ID_ANY, NULL, packets_pool);
  177.     if(ret < 0)
  178.         rte_exit(EXIT_FAILURE, "Cannot configure device rx queue\n");
  179.  
  180.     ret = rte_eth_dev_start(portid1);
  181.     if (ret < 0)
  182.         rte_exit(EXIT_FAILURE, "Cannot start device\n");
  183.  
  184.     rte_eth_promiscuous_enable(portid1);
  185.  
  186.     rte_eth_dev_info_get(portid2, &dev_info);
  187.  
  188.     ret = rte_eth_dev_configure(portid2, 1, 1, &port_conf);
  189.     if(ret < 0)
  190.         rte_exit(EXIT_FAILURE, "Cannot configure device\n");
  191.  
  192.     ret = rte_eth_tx_queue_setup(portid2, 0, nb_txd, SOCKET_ID_ANY, NULL);
  193.     if(ret < 0)
  194.         rte_exit(EXIT_FAILURE, "Cannot configure device tx queue\n");
  195.  
  196.     ret = rte_eth_rx_queue_setup(portid2, 0, nb_rxd, SOCKET_ID_ANY, NULL, packets_pool);
  197.     if(ret < 0)
  198.         rte_exit(EXIT_FAILURE, "Cannot configure device rx queue\n");
  199.  
  200.     ret = rte_eth_dev_start(portid2);
  201.     if (ret < 0)
  202.         rte_exit(EXIT_FAILURE, "Cannot start device\n");
  203.  
  204.     rte_eth_promiscuous_enable(portid2);
  205. }
  206.  
  207. /* send packets */
  208. inline int send_packets(struct rte_mbuf ** packets, uint8_t port)
  209. {
  210.     int i = 0;
  211.     int ntosend = BURST_SIZE;
  212.     do {
  213.         i += rte_eth_tx_burst(port, 0, &packets[i], ntosend - i);
  214.         if(unlikely(stop))
  215.             break;
  216.     } while(unlikely(i < ntosend));
  217.  
  218.     return BURST_SIZE;
  219. }
  220.  
  221. int send_loop(void *arg)
  222. {
  223.     struct fargs *args = (struct fargs *) arg;
  224.     struct rte_mbuf * packets_array[BURST_SIZE] = {0};
  225.     char pkt[PKT_SIZE] = {0};
  226.     int i;
  227.  
  228.     //Initializate packet contents
  229.     for (i = 0; i < PKT_SIZE; i++)
  230.         pkt[i] = 0xCC;
  231.  
  232.     while (likely(!stop)) {
  233.         int n;
  234.         /* get BURST_SIZE free slots */
  235.         do {
  236.             n = rte_mempool_get_bulk(packets_pool, (void **) packets_array, BURST_SIZE);
  237.         } while(n != 0 && !stop);
  238.  
  239.         //Copy data to the buffers
  240.         for (i = 0; i < BURST_SIZE; i++) {
  241.             rte_mbuf_refcnt_set(packets_array[i], 1);
  242.  
  243.             rte_memcpy(rte_pktmbuf_mtod(packets_array[i], void *), pkt, PKT_SIZE);
  244.             packets_array[i]->next = NULL;
  245.             packets_array[i]->pkt_len = PKT_SIZE;
  246.             packets_array[i]->data_len = PKT_SIZE;
  247.         }
  248.         send_packets(packets_array, args->portid);
  249.     }
  250.  
  251.     return 0;
  252. }
  253.  
  254. int receive_loop(void *arg)
  255. {
  256.     struct fargs *args = (struct fargs *) arg;
  257.     int nreceived;
  258.  
  259.     struct rte_mbuf * packets_array[BURST_SIZE] = {0};
  260.     int i;
  261.     while (likely(!stop)) {
  262.         nreceived = rte_eth_rx_burst(args->portid, 0, packets_array, BURST_SIZE);
  263.         rte_atomic32_add(&rx_pkts, nreceived);
  264.         for (i = 0; i < nreceived; i++)
  265.             rte_pktmbuf_free(packets_array[i]);
  266.     }
  267.     return 0;
  268. }
  269.  
  270. void crtl_c_handler(int s)
  271. {
  272.     (void) s; /* Avoid compile warning */
  273.     printf("Requesting stop.\n");
  274.     stop = 1;
  275. }
  276.  
  277. void print_stats(void)
  278. {
  279.     printf("RX Packets:\t%'" PRIu32 "\n", rx_pkts.cnt);
  280.     rte_atomic32_set(&rx_pkts, 0);
  281.  
  282.     printf("\n");
  283. }
  284.  
  285. void ALARMhandler(int sig)
  286. {
  287.     (void) sig;
  288.  
  289.     signal(SIGALRM, SIG_IGN);          /* ignore this signal       */
  290.  
  291.     print_stats();
  292.     signal(SIGALRM, ALARMhandler);     /* reinstall the handler    */
  293.     alarm(PRINT_INTERVAL);
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement