Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <signal.h>
- #include <time.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <locale.h>
- #include <rte_memzone.h>
- #include <rte_memcpy.h>
- #include <rte_ethdev.h>
- #include <rte_mempool.h>
- #include <rte_atomic.h>
- #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
- #define PKT_SIZE 60
- #define MBUF_SIZE (PKT_SIZE + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
- #define PRINT_INTERVAL 1
- #define BURST_SIZE 16
- #define CALC_RX_STATS
- #define RUN_TIME 30
- /* function prototypes */
- void send_receive_loop(void);
- void init(void);
- void crtl_c_handler(int s);
- void ALARMhandler(int sig);
- inline int send_packets(struct rte_mbuf **packets, uint8_t port);
- void print_stats(void);
- int send_loop(void * arg);
- int receive_loop(void * arg);
- unsigned int counter = 0;
- volatile sig_atomic_t stop;
- rte_atomic32_t rx_pkts = RTE_ATOMIC32_INIT(0);
- struct rte_mempool *packets_pool = NULL;
- uint8_t portid1 = 0;
- uint8_t portid2 = 1;
- struct rte_eth_dev_info dev_info;
- /* TODO: verify this setup */
- static const struct rte_eth_conf port_conf = {
- .rxmode = {
- .split_hdr_size = 0,
- .header_split = 0,
- .hw_ip_checksum = 0,
- .hw_vlan_filter = 0,
- .jumbo_frame = 0,
- .hw_strip_crc = 0,
- },
- .txmode = {
- .mq_mode = ETH_MQ_TX_NONE,
- },
- };
- static uint16_t nb_rxd = 128;
- static uint16_t nb_txd = 512;
- struct fargs
- {
- uint8_t portid;
- };
- int main(int argc, char *argv[])
- {
- setlocale(LC_NUMERIC, "en_US.utf-8");
- int retval = 0;
- if ((retval = rte_eal_init(argc, argv)) < 0)
- return -1;
- argc -= retval;
- argv += retval;
- init();
- RTE_LOG(INFO, APP, "Finished Process Init.\n");
- signal(SIGINT,crtl_c_handler);
- signal(SIGALRM, ALARMhandler);
- alarm(PRINT_INTERVAL);
- struct fargs f0;
- f0.portid = 0;
- struct fargs f1;
- f1.portid = 1;
- retval = rte_eal_remote_launch(receive_loop, &f0, 1);
- if (retval) {
- RTE_LOG(ERR, APP, "error launching remote function...\n");
- return -1;
- }
- retval = rte_eal_remote_launch(receive_loop, &f1, 2);
- if (retval) {
- RTE_LOG(ERR, APP, "error launching remote function...\n");
- return -1;
- }
- retval = rte_eal_remote_launch(send_loop, &f0, 3);
- if (retval) {
- RTE_LOG(ERR, APP, "error launching remote function...\n");
- return -1;
- }
- retval = rte_eal_remote_launch(send_loop, &f1, 4);
- if (retval) {
- RTE_LOG(ERR, APP, "error launching remote function...\n");
- return -1;
- }
- printf("press enter to continue....\n");
- getchar();
- retval = rte_eal_wait_lcore(1);
- if (retval) {
- RTE_LOG(ERR, APP, "error waiting for core...");
- return -1;
- }
- retval = rte_eal_wait_lcore(2);
- if (retval) {
- RTE_LOG(ERR, APP, "error waiting for core...");
- return -1;
- }
- retval = rte_eal_wait_lcore(3);
- if (retval) {
- RTE_LOG(ERR, APP, "error waiting for core...");
- return -1;
- }
- retval = rte_eal_wait_lcore(4);
- if (retval) {
- RTE_LOG(ERR, APP, "error waiting for core...");
- return -1;
- }
- RTE_LOG(INFO, APP, "Done\n");
- return 0;
- }
- void init(void)
- {
- int ret;
- packets_pool = rte_pktmbuf_pool_create("packets", 128*1024, 512,
- 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
- if (packets_pool == NULL) {
- rte_exit(EXIT_FAILURE, "Cannot find memory pool\n");
- }
- rte_eth_dev_info_get(portid1, &dev_info);
- ret = rte_eth_dev_configure(portid1, 1, 1, &port_conf);
- if(ret < 0)
- rte_exit(EXIT_FAILURE, "Cannot configure device\n");
- ret = rte_eth_tx_queue_setup(portid1, 0, nb_txd, SOCKET_ID_ANY, NULL);
- if(ret < 0)
- rte_exit(EXIT_FAILURE, "Cannot configure device tx queue\n");
- ret = rte_eth_rx_queue_setup(portid1, 0, nb_rxd, SOCKET_ID_ANY, NULL, packets_pool);
- if(ret < 0)
- rte_exit(EXIT_FAILURE, "Cannot configure device rx queue\n");
- ret = rte_eth_dev_start(portid1);
- if (ret < 0)
- rte_exit(EXIT_FAILURE, "Cannot start device\n");
- rte_eth_promiscuous_enable(portid1);
- rte_eth_dev_info_get(portid2, &dev_info);
- ret = rte_eth_dev_configure(portid2, 1, 1, &port_conf);
- if(ret < 0)
- rte_exit(EXIT_FAILURE, "Cannot configure device\n");
- ret = rte_eth_tx_queue_setup(portid2, 0, nb_txd, SOCKET_ID_ANY, NULL);
- if(ret < 0)
- rte_exit(EXIT_FAILURE, "Cannot configure device tx queue\n");
- ret = rte_eth_rx_queue_setup(portid2, 0, nb_rxd, SOCKET_ID_ANY, NULL, packets_pool);
- if(ret < 0)
- rte_exit(EXIT_FAILURE, "Cannot configure device rx queue\n");
- ret = rte_eth_dev_start(portid2);
- if (ret < 0)
- rte_exit(EXIT_FAILURE, "Cannot start device\n");
- rte_eth_promiscuous_enable(portid2);
- }
- /* send packets */
- inline int send_packets(struct rte_mbuf ** packets, uint8_t port)
- {
- int i = 0;
- int ntosend = BURST_SIZE;
- do {
- i += rte_eth_tx_burst(port, 0, &packets[i], ntosend - i);
- if(unlikely(stop))
- break;
- } while(unlikely(i < ntosend));
- return BURST_SIZE;
- }
- int send_loop(void *arg)
- {
- struct fargs *args = (struct fargs *) arg;
- struct rte_mbuf * packets_array[BURST_SIZE] = {0};
- char pkt[PKT_SIZE] = {0};
- int i;
- //Initializate packet contents
- for (i = 0; i < PKT_SIZE; i++)
- pkt[i] = 0xCC;
- while (likely(!stop)) {
- int n;
- /* get BURST_SIZE free slots */
- do {
- n = rte_mempool_get_bulk(packets_pool, (void **) packets_array, BURST_SIZE);
- } while(n != 0 && !stop);
- //Copy data to the buffers
- for (i = 0; i < BURST_SIZE; i++) {
- rte_mbuf_refcnt_set(packets_array[i], 1);
- rte_memcpy(rte_pktmbuf_mtod(packets_array[i], void *), pkt, PKT_SIZE);
- packets_array[i]->next = NULL;
- packets_array[i]->pkt_len = PKT_SIZE;
- packets_array[i]->data_len = PKT_SIZE;
- }
- send_packets(packets_array, args->portid);
- }
- return 0;
- }
- int receive_loop(void *arg)
- {
- struct fargs *args = (struct fargs *) arg;
- int nreceived;
- struct rte_mbuf * packets_array[BURST_SIZE] = {0};
- int i;
- while (likely(!stop)) {
- nreceived = rte_eth_rx_burst(args->portid, 0, packets_array, BURST_SIZE);
- rte_atomic32_add(&rx_pkts, nreceived);
- for (i = 0; i < nreceived; i++)
- rte_pktmbuf_free(packets_array[i]);
- }
- return 0;
- }
- void crtl_c_handler(int s)
- {
- (void) s; /* Avoid compile warning */
- printf("Requesting stop.\n");
- stop = 1;
- }
- void print_stats(void)
- {
- printf("RX Packets:\t%'" PRIu32 "\n", rx_pkts.cnt);
- rte_atomic32_set(&rx_pkts, 0);
- printf("\n");
- }
- void ALARMhandler(int sig)
- {
- (void) sig;
- signal(SIGALRM, SIG_IGN); /* ignore this signal */
- print_stats();
- signal(SIGALRM, ALARMhandler); /* reinstall the handler */
- alarm(PRINT_INTERVAL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement