1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
6 * Sample application demonstrating how to do packet I/O in a multi-process
7 * environment. The same code can be run as a primary process and as a
8 * secondary process, just with a different proc-id parameter in each case
9 * (apart from the EAL flag to indicate a secondary process).
11 * Each process will read from the same ports, given by the port-mask
12 * parameter, which should be the same in each case, just using a different
13 * queue per port as determined by the proc-id parameter.
22 #include <sys/queue.h>
27 #include <rte_common.h>
29 #include <rte_memory.h>
30 #include <rte_launch.h>
32 #include <rte_per_lcore.h>
33 #include <rte_lcore.h>
34 #include <rte_branch_prediction.h>
35 #include <rte_debug.h>
36 #include <rte_interrupts.h>
37 #include <rte_ether.h>
38 #include <rte_ethdev.h>
39 #include <rte_mempool.h>
40 #include <rte_memcpy.h>
42 #include <rte_string_fns.h>
43 #include <rte_cycles.h>
45 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
47 #define NB_MBUFS 64*1024 /* use 64k mbufs */
48 #define MBUF_CACHE_SIZE 256
50 #define RX_RING_SIZE 1024
51 #define TX_RING_SIZE 1024
53 #define PARAM_PROC_ID "proc-id"
54 #define PARAM_NUM_PROCS "num-procs"
56 /* for each lcore, record the elements of the ports array to use */
62 /* structure to record the rx and tx packets. Put two per cache line as ports
68 } __rte_aligned(RTE_CACHE_LINE_SIZE / 2);
70 static int proc_id = -1;
71 static unsigned num_procs = 0;
73 static uint16_t ports[RTE_MAX_ETHPORTS];
74 static unsigned num_ports = 0;
76 static struct lcore_ports lcore_ports[RTE_MAX_LCORE];
77 static struct port_stats pstats[RTE_MAX_ETHPORTS];
79 /* prints the usage statement and quits with an error message */
81 smp_usage(const char *prgname, const char *errmsg)
83 printf("\nError: %s\n",errmsg);
84 printf("\n%s [EAL options] -- -p <port mask> "
85 "--"PARAM_NUM_PROCS" <n>"
86 " --"PARAM_PROC_ID" <id>\n"
87 "-p : a hex bitmask indicating what ports are to be used\n"
88 "--num-procs: the number of processes which will be used\n"
89 "--proc-id : the id of the current process (id < num-procs)\n"
96 /* signal handler configured for SIGTERM and SIGINT to print stats on exit */
98 print_stats(int signum)
101 printf("\nExiting on signal %d\n\n", signum);
102 for (i = 0; i < num_ports; i++){
103 const uint8_t p_num = ports[i];
104 printf("Port %u: RX - %u, TX - %u, Drop - %u\n", (unsigned)p_num,
105 pstats[p_num].rx, pstats[p_num].tx, pstats[p_num].drop);
110 /* Parse the argument given in the command line of the application */
112 smp_parse_args(int argc, char **argv)
117 uint16_t i, port_mask = 0;
118 char *prgname = argv[0];
119 static struct option lgopts[] = {
120 {PARAM_NUM_PROCS, 1, 0, 0},
121 {PARAM_PROC_ID, 1, 0, 0},
127 while ((opt = getopt_long(argc, argvopt, "p:", \
128 lgopts, &option_index)) != EOF) {
132 port_mask = strtoull(optarg, NULL, 16);
136 if (strncmp(lgopts[option_index].name, PARAM_NUM_PROCS, 8) == 0)
137 num_procs = atoi(optarg);
138 else if (strncmp(lgopts[option_index].name, PARAM_PROC_ID, 7) == 0)
139 proc_id = atoi(optarg);
143 smp_usage(prgname, "Cannot parse all command-line arguments\n");
148 argv[optind-1] = prgname;
151 smp_usage(prgname, "Invalid or missing proc-id parameter\n");
152 if (rte_eal_process_type() == RTE_PROC_PRIMARY && num_procs == 0)
153 smp_usage(prgname, "Invalid or missing num-procs parameter\n");
155 smp_usage(prgname, "Invalid or missing port mask\n");
157 /* get the port numbers from the port mask */
158 RTE_ETH_FOREACH_DEV(i)
159 if(port_mask & (1 << i))
160 ports[num_ports++] = (uint8_t)i;
163 optind = 1; /* reset getopt lib */
169 * Initialises a given port using global settings and with the rx buffers
170 * coming from the mbuf_pool passed as parameter
173 smp_port_init(uint16_t port, struct rte_mempool *mbuf_pool,
176 struct rte_eth_conf port_conf = {
178 .mq_mode = RTE_ETH_MQ_RX_RSS,
180 .offloads = RTE_ETH_RX_OFFLOAD_CHECKSUM,
185 .rss_hf = RTE_ETH_RSS_IP,
189 .mq_mode = RTE_ETH_MQ_TX_NONE,
192 const uint16_t rx_rings = num_queues, tx_rings = num_queues;
193 struct rte_eth_dev_info info;
194 struct rte_eth_rxconf rxq_conf;
195 struct rte_eth_txconf txq_conf;
198 uint16_t nb_rxd = RX_RING_SIZE;
199 uint16_t nb_txd = TX_RING_SIZE;
202 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
205 if (!rte_eth_dev_is_valid_port(port))
208 printf("# Initialising port %u... ", port);
211 retval = rte_eth_dev_info_get(port, &info);
213 printf("Error during getting device (port %u) info: %s\n",
214 port, strerror(-retval));
218 info.default_rxconf.rx_drop_en = 1;
220 if (info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
221 port_conf.txmode.offloads |=
222 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
224 rss_hf_tmp = port_conf.rx_adv_conf.rss_conf.rss_hf;
225 port_conf.rx_adv_conf.rss_conf.rss_hf &= info.flow_type_rss_offloads;
226 if (port_conf.rx_adv_conf.rss_conf.rss_hf != rss_hf_tmp) {
227 printf("Port %u modified RSS hash function based on hardware support,"
228 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
231 port_conf.rx_adv_conf.rss_conf.rss_hf);
234 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
238 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
242 rxq_conf = info.default_rxconf;
243 rxq_conf.offloads = port_conf.rxmode.offloads;
244 for (q = 0; q < rx_rings; q ++) {
245 retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
246 rte_eth_dev_socket_id(port),
253 txq_conf = info.default_txconf;
254 txq_conf.offloads = port_conf.txmode.offloads;
255 for (q = 0; q < tx_rings; q ++) {
256 retval = rte_eth_tx_queue_setup(port, q, nb_txd,
257 rte_eth_dev_socket_id(port),
263 retval = rte_eth_promiscuous_enable(port);
267 retval = rte_eth_dev_start(port);
274 /* Goes through each of the lcores and calculates what ports should
275 * be used by that core. Fills in the global lcore_ports[] array.
278 assign_ports_to_cores(void)
281 const unsigned int lcores = rte_lcore_count();
282 const unsigned port_pairs = num_ports / 2;
283 const unsigned pairs_per_lcore = port_pairs / lcores;
284 unsigned extra_pairs = port_pairs % lcores;
285 unsigned ports_assigned = 0;
288 RTE_LCORE_FOREACH(i) {
289 lcore_ports[i].start_port = ports_assigned;
290 lcore_ports[i].num_ports = pairs_per_lcore * 2;
291 if (extra_pairs > 0) {
292 lcore_ports[i].num_ports += 2;
295 ports_assigned += lcore_ports[i].num_ports;
299 /* Main function used by the processing threads.
300 * Prints out some configuration details for the thread and then begins
301 * performing packet RX and TX.
304 lcore_main(void *arg __rte_unused)
306 const unsigned id = rte_lcore_id();
307 const unsigned start_port = lcore_ports[id].start_port;
308 const unsigned end_port = start_port + lcore_ports[id].num_ports;
309 const uint16_t q_id = (uint16_t)proc_id;
314 if (start_port == end_port){
315 printf("Lcore %u has nothing to do\n", id);
319 /* build up message in msgbuf before printing to decrease likelihood
320 * of multi-core message interleaving.
322 msgbufpos += snprintf(msgbuf, sizeof(msgbuf) - msgbufpos,
323 "Lcore %u using ports ", id);
324 for (p = start_port; p < end_port; p++){
325 msgbufpos += snprintf(msgbuf + msgbufpos, sizeof(msgbuf) - msgbufpos,
326 "%u ", (unsigned)ports[p]);
328 printf("%s\n", msgbuf);
329 printf("lcore %u using queue %u of each port\n", id, (unsigned)q_id);
331 /* handle packet I/O from the ports, reading and writing to the
332 * queue number corresponding to our process number (not lcore id)
336 struct rte_mbuf *buf[PKT_BURST];
338 for (p = start_port; p < end_port; p++) {
339 const uint8_t src = ports[p];
340 const uint8_t dst = ports[p ^ 1]; /* 0 <-> 1, 2 <-> 3 etc */
341 const uint16_t rx_c = rte_eth_rx_burst(src, q_id, buf, PKT_BURST);
344 pstats[src].rx += rx_c;
346 const uint16_t tx_c = rte_eth_tx_burst(dst, q_id, buf, rx_c);
347 pstats[dst].tx += tx_c;
349 pstats[dst].drop += (rx_c - tx_c);
350 for (i = tx_c; i < rx_c; i++)
351 rte_pktmbuf_free(buf[i]);
357 /* Check the link status of all ports in up to 9s, and print them finally */
359 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
361 #define CHECK_INTERVAL 100 /* 100ms */
362 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
364 uint8_t count, all_ports_up, print_flag = 0;
365 struct rte_eth_link link;
367 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
369 printf("\nChecking link status");
371 for (count = 0; count <= MAX_CHECK_TIME; count++) {
373 for (portid = 0; portid < port_num; portid++) {
374 if ((port_mask & (1 << portid)) == 0)
376 memset(&link, 0, sizeof(link));
377 ret = rte_eth_link_get_nowait(portid, &link);
381 printf("Port %u link get failed: %s\n",
382 portid, rte_strerror(-ret));
385 /* print link status if flag set */
386 if (print_flag == 1) {
387 rte_eth_link_to_str(link_status_text,
388 sizeof(link_status_text), &link);
389 printf("Port %d %s\n", portid,
393 /* clear all_ports_up flag if any link down */
394 if (link.link_status == RTE_ETH_LINK_DOWN) {
399 /* after finally printing all link status, get out */
403 if (all_ports_up == 0) {
406 rte_delay_ms(CHECK_INTERVAL);
409 /* set the print_flag if all ports up or timeout */
410 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
418 * Performs initialisation and then calls the lcore_main on each core
419 * to do the packet-processing work.
422 main(int argc, char **argv)
424 static const char *_SMP_MBUF_POOL = "SMP_MBUF_POOL";
427 enum rte_proc_type_t proc_type;
428 struct rte_mempool *mp;
430 /* set up signal handlers to print stats on exit */
431 signal(SIGINT, print_stats);
432 signal(SIGTERM, print_stats);
434 /* initialise the EAL for all */
435 ret = rte_eal_init(argc, argv);
437 rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
441 /* determine the NIC devices available */
442 if (rte_eth_dev_count_avail() == 0)
443 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
445 /* parse application arguments (those after the EAL ones) */
446 smp_parse_args(argc, argv);
448 proc_type = rte_eal_process_type();
449 mp = (proc_type == RTE_PROC_SECONDARY) ?
450 rte_mempool_lookup(_SMP_MBUF_POOL) :
451 rte_pktmbuf_pool_create(_SMP_MBUF_POOL, NB_MBUFS,
452 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
455 rte_exit(EXIT_FAILURE, "Cannot get memory pool for buffers\n");
457 /* Primary instance initialized. 8< */
459 rte_exit(EXIT_FAILURE, "Application must use an even number of ports\n");
460 for(i = 0; i < num_ports; i++){
461 if(proc_type == RTE_PROC_PRIMARY)
462 if (smp_port_init(ports[i], mp, (uint16_t)num_procs) < 0)
463 rte_exit(EXIT_FAILURE, "Error initialising ports\n");
465 /* >8 End of primary instance initialization. */
467 if (proc_type == RTE_PROC_PRIMARY)
468 check_all_ports_link_status((uint8_t)num_ports, (~0x0));
470 assign_ports_to_cores();
472 RTE_LOG(INFO, APP, "Finished Process Init.\n");
474 rte_eal_mp_remote_launch(lcore_main, NULL, CALL_MAIN);
476 /* clean up the EAL */