1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 */
4
5 /*
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).
10 *
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.
14 */
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <stdarg.h>
21 #include <errno.h>
22 #include <sys/queue.h>
23 #include <getopt.h>
24 #include <signal.h>
25 #include <inttypes.h>
26
27 #include <rte_common.h>
28 #include <rte_log.h>
29 #include <rte_memory.h>
30 #include <rte_launch.h>
31 #include <rte_eal.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>
41 #include <rte_mbuf.h>
42 #include <rte_string_fns.h>
43 #include <rte_cycles.h>
44
45 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
46
47 #define NB_MBUFS 64*1024 /* use 64k mbufs */
48 #define MBUF_CACHE_SIZE 256
49 #define PKT_BURST 32
50 #define RX_RING_SIZE 1024
51 #define TX_RING_SIZE 1024
52
53 #define PARAM_PROC_ID "proc-id"
54 #define PARAM_NUM_PROCS "num-procs"
55
56 /* for each lcore, record the elements of the ports array to use */
57 struct lcore_ports{
58 unsigned start_port;
59 unsigned num_ports;
60 };
61
62 /* structure to record the rx and tx packets. Put two per cache line as ports
63 * used in pairs */
64 struct __rte_aligned(RTE_CACHE_LINE_SIZE / 2) port_stats{
65 unsigned rx;
66 unsigned tx;
67 unsigned drop;
68 };
69
70 static int proc_id = -1;
71 static unsigned num_procs = 0;
72
73 static uint16_t ports[RTE_MAX_ETHPORTS];
74 static unsigned num_ports = 0;
75
76 static struct lcore_ports lcore_ports[RTE_MAX_LCORE];
77 static struct port_stats pstats[RTE_MAX_ETHPORTS];
78
79 /* prints the usage statement and quits with an error message */
80 static void
smp_usage(const char * prgname,const char * errmsg)81 smp_usage(const char *prgname, const char *errmsg)
82 {
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"
90 "\n",
91 prgname);
92 exit(1);
93 }
94
95
96 /* signal handler configured for SIGTERM and SIGINT to print stats on exit */
97 static void
print_stats(int signum)98 print_stats(int signum)
99 {
100 unsigned i;
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);
106 }
107 exit(0);
108 }
109
110 /* Parse the argument given in the command line of the application */
111 static int
smp_parse_args(int argc,char ** argv)112 smp_parse_args(int argc, char **argv)
113 {
114 int opt, ret;
115 char **argvopt;
116 int option_index;
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},
122 {NULL, 0, 0, 0}
123 };
124
125 argvopt = argv;
126
127 while ((opt = getopt_long(argc, argvopt, "p:", \
128 lgopts, &option_index)) != EOF) {
129
130 switch (opt) {
131 case 'p':
132 port_mask = strtoull(optarg, NULL, 16);
133 break;
134 /* long options */
135 case 0:
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);
140 break;
141
142 default:
143 smp_usage(prgname, "Cannot parse all command-line arguments\n");
144 }
145 }
146
147 if (optind >= 0)
148 argv[optind-1] = prgname;
149
150 if (proc_id < 0)
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");
154 if (port_mask == 0)
155 smp_usage(prgname, "Invalid or missing port mask\n");
156
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;
161
162 ret = optind-1;
163 optind = 1; /* reset getopt lib */
164
165 return ret;
166 }
167
168 /*
169 * Initialises a given port using global settings and with the rx buffers
170 * coming from the mbuf_pool passed as parameter
171 */
172 static inline int
smp_port_init(uint16_t port,struct rte_mempool * mbuf_pool,uint16_t num_queues)173 smp_port_init(uint16_t port, struct rte_mempool *mbuf_pool,
174 uint16_t num_queues)
175 {
176 struct rte_eth_conf port_conf = {
177 .rxmode = {
178 .mq_mode = RTE_ETH_MQ_RX_RSS,
179 .offloads = RTE_ETH_RX_OFFLOAD_CHECKSUM,
180 },
181 .rx_adv_conf = {
182 .rss_conf = {
183 .rss_key = NULL,
184 .rss_hf = RTE_ETH_RSS_IP,
185 },
186 },
187 .txmode = {
188 .mq_mode = RTE_ETH_MQ_TX_NONE,
189 }
190 };
191 const uint16_t rx_rings = num_queues, tx_rings = num_queues;
192 struct rte_eth_dev_info info;
193 struct rte_eth_rxconf rxq_conf;
194 struct rte_eth_txconf txq_conf;
195 int retval;
196 uint16_t q;
197 uint16_t nb_rxd = RX_RING_SIZE;
198 uint16_t nb_txd = TX_RING_SIZE;
199 uint64_t rss_hf_tmp;
200
201 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
202 return 0;
203
204 if (!rte_eth_dev_is_valid_port(port))
205 return -1;
206
207 printf("# Initialising port %u... ", port);
208 fflush(stdout);
209
210 retval = rte_eth_dev_info_get(port, &info);
211 if (retval != 0) {
212 printf("Error during getting device (port %u) info: %s\n",
213 port, strerror(-retval));
214 return retval;
215 }
216
217 info.default_rxconf.rx_drop_en = 1;
218
219 if (info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
220 port_conf.txmode.offloads |=
221 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
222
223 rss_hf_tmp = port_conf.rx_adv_conf.rss_conf.rss_hf;
224 port_conf.rx_adv_conf.rss_conf.rss_hf &= info.flow_type_rss_offloads;
225 if (port_conf.rx_adv_conf.rss_conf.rss_hf != rss_hf_tmp) {
226 printf("Port %u modified RSS hash function based on hardware support,"
227 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
228 port,
229 rss_hf_tmp,
230 port_conf.rx_adv_conf.rss_conf.rss_hf);
231 }
232
233 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
234 if (retval == -EINVAL) {
235 printf("Port %u configuration failed. Re-attempting with HW checksum disabled.\n",
236 port);
237 port_conf.rxmode.offloads &= ~(RTE_ETH_RX_OFFLOAD_CHECKSUM);
238 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
239 }
240
241 if (retval == -ENOTSUP) {
242 printf("Port %u configuration failed. Re-attempting with HW RSS disabled.\n",
243 port);
244 port_conf.rxmode.mq_mode &= ~(RTE_ETH_MQ_RX_RSS);
245 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
246 }
247
248 if (retval < 0)
249 return retval;
250
251 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
252 if (retval < 0)
253 return retval;
254
255 rxq_conf = info.default_rxconf;
256 rxq_conf.offloads = port_conf.rxmode.offloads;
257 for (q = 0; q < rx_rings; q ++) {
258 retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
259 rte_eth_dev_socket_id(port),
260 &rxq_conf,
261 mbuf_pool);
262 if (retval < 0)
263 return retval;
264 }
265
266 txq_conf = info.default_txconf;
267 txq_conf.offloads = port_conf.txmode.offloads;
268 for (q = 0; q < tx_rings; q ++) {
269 retval = rte_eth_tx_queue_setup(port, q, nb_txd,
270 rte_eth_dev_socket_id(port),
271 &txq_conf);
272 if (retval < 0)
273 return retval;
274 }
275
276 retval = rte_eth_promiscuous_enable(port);
277 if (retval != 0)
278 return retval;
279
280 retval = rte_eth_dev_start(port);
281 if (retval < 0)
282 return retval;
283
284 return 0;
285 }
286
287 /* Goes through each of the lcores and calculates what ports should
288 * be used by that core. Fills in the global lcore_ports[] array.
289 */
290 static void
assign_ports_to_cores(void)291 assign_ports_to_cores(void)
292 {
293
294 const unsigned int lcores = rte_lcore_count();
295 const unsigned port_pairs = num_ports / 2;
296 const unsigned pairs_per_lcore = port_pairs / lcores;
297 unsigned extra_pairs = port_pairs % lcores;
298 unsigned ports_assigned = 0;
299 unsigned i;
300
301 RTE_LCORE_FOREACH(i) {
302 lcore_ports[i].start_port = ports_assigned;
303 lcore_ports[i].num_ports = pairs_per_lcore * 2;
304 if (extra_pairs > 0) {
305 lcore_ports[i].num_ports += 2;
306 extra_pairs--;
307 }
308 ports_assigned += lcore_ports[i].num_ports;
309 }
310 }
311
312 /* Main function used by the processing threads.
313 * Prints out some configuration details for the thread and then begins
314 * performing packet RX and TX.
315 */
316 static int
lcore_main(void * arg __rte_unused)317 lcore_main(void *arg __rte_unused)
318 {
319 const unsigned id = rte_lcore_id();
320 const unsigned start_port = lcore_ports[id].start_port;
321 const unsigned end_port = start_port + lcore_ports[id].num_ports;
322 const uint16_t q_id = (uint16_t)proc_id;
323 unsigned p, i;
324 char msgbuf[256];
325 int msgbufpos = 0;
326
327 if (start_port == end_port){
328 printf("Lcore %u has nothing to do\n", id);
329 return 0;
330 }
331
332 /* build up message in msgbuf before printing to decrease likelihood
333 * of multi-core message interleaving.
334 */
335 msgbufpos += snprintf(msgbuf, sizeof(msgbuf) - msgbufpos,
336 "Lcore %u using ports ", id);
337 for (p = start_port; p < end_port; p++){
338 msgbufpos += snprintf(msgbuf + msgbufpos, sizeof(msgbuf) - msgbufpos,
339 "%u ", (unsigned)ports[p]);
340 }
341 printf("%s\n", msgbuf);
342 printf("lcore %u using queue %u of each port\n", id, (unsigned)q_id);
343
344 /* handle packet I/O from the ports, reading and writing to the
345 * queue number corresponding to our process number (not lcore id)
346 */
347
348 for (;;) {
349 struct rte_mbuf *buf[PKT_BURST];
350
351 for (p = start_port; p < end_port; p++) {
352 const uint8_t src = ports[p];
353 const uint8_t dst = ports[p ^ 1]; /* 0 <-> 1, 2 <-> 3 etc */
354 const uint16_t rx_c = rte_eth_rx_burst(src, q_id, buf, PKT_BURST);
355 if (rx_c == 0)
356 continue;
357 pstats[src].rx += rx_c;
358
359 const uint16_t tx_c = rte_eth_tx_burst(dst, q_id, buf, rx_c);
360 pstats[dst].tx += tx_c;
361 if (tx_c != rx_c) {
362 pstats[dst].drop += (rx_c - tx_c);
363 for (i = tx_c; i < rx_c; i++)
364 rte_pktmbuf_free(buf[i]);
365 }
366 }
367 }
368 }
369
370 /* Check the link status of all ports in up to 9s, and print them finally */
371 static void
check_all_ports_link_status(uint16_t port_num,uint32_t port_mask)372 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
373 {
374 #define CHECK_INTERVAL 100 /* 100ms */
375 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
376 uint16_t portid;
377 uint8_t count, all_ports_up, print_flag = 0;
378 struct rte_eth_link link;
379 int ret;
380 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
381
382 printf("\nChecking link status");
383 fflush(stdout);
384 for (count = 0; count <= MAX_CHECK_TIME; count++) {
385 all_ports_up = 1;
386 for (portid = 0; portid < port_num; portid++) {
387 if ((port_mask & (1 << portid)) == 0)
388 continue;
389 memset(&link, 0, sizeof(link));
390 ret = rte_eth_link_get_nowait(portid, &link);
391 if (ret < 0) {
392 all_ports_up = 0;
393 if (print_flag == 1)
394 printf("Port %u link get failed: %s\n",
395 portid, rte_strerror(-ret));
396 continue;
397 }
398 /* print link status if flag set */
399 if (print_flag == 1) {
400 rte_eth_link_to_str(link_status_text,
401 sizeof(link_status_text), &link);
402 printf("Port %d %s\n", portid,
403 link_status_text);
404 continue;
405 }
406 /* clear all_ports_up flag if any link down */
407 if (link.link_status == RTE_ETH_LINK_DOWN) {
408 all_ports_up = 0;
409 break;
410 }
411 }
412 /* after finally printing all link status, get out */
413 if (print_flag == 1)
414 break;
415
416 if (all_ports_up == 0) {
417 printf(".");
418 fflush(stdout);
419 rte_delay_ms(CHECK_INTERVAL);
420 }
421
422 /* set the print_flag if all ports up or timeout */
423 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
424 print_flag = 1;
425 printf("done\n");
426 }
427 }
428 }
429
430 /* Main function.
431 * Performs initialisation and then calls the lcore_main on each core
432 * to do the packet-processing work.
433 */
434 int
main(int argc,char ** argv)435 main(int argc, char **argv)
436 {
437 static const char *_SMP_MBUF_POOL = "SMP_MBUF_POOL";
438 int ret;
439 unsigned i;
440 enum rte_proc_type_t proc_type;
441 struct rte_mempool *mp;
442
443 /* set up signal handlers to print stats on exit */
444 signal(SIGINT, print_stats);
445 signal(SIGTERM, print_stats);
446
447 /* initialise the EAL for all */
448 ret = rte_eal_init(argc, argv);
449 if (ret < 0)
450 rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
451 argc -= ret;
452 argv += ret;
453
454 /* determine the NIC devices available */
455 if (rte_eth_dev_count_avail() == 0)
456 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
457
458 /* parse application arguments (those after the EAL ones) */
459 smp_parse_args(argc, argv);
460
461 proc_type = rte_eal_process_type();
462 mp = (proc_type == RTE_PROC_SECONDARY) ?
463 rte_mempool_lookup(_SMP_MBUF_POOL) :
464 rte_pktmbuf_pool_create(_SMP_MBUF_POOL, NB_MBUFS,
465 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
466 rte_socket_id());
467 if (mp == NULL)
468 rte_exit(EXIT_FAILURE, "Cannot get memory pool for buffers\n");
469
470 /* Primary instance initialized. 8< */
471 if (num_ports & 1)
472 rte_exit(EXIT_FAILURE, "Application must use an even number of ports\n");
473 for(i = 0; i < num_ports; i++){
474 if(proc_type == RTE_PROC_PRIMARY)
475 if (smp_port_init(ports[i], mp, (uint16_t)num_procs) < 0)
476 rte_exit(EXIT_FAILURE, "Error initialising ports\n");
477 }
478 /* >8 End of primary instance initialization. */
479
480 if (proc_type == RTE_PROC_PRIMARY)
481 check_all_ports_link_status((uint8_t)num_ports, (~0x0));
482
483 assign_ports_to_cores();
484
485 RTE_LOG(INFO, APP, "Finished Process Init.\n");
486
487 rte_eal_mp_remote_launch(lcore_main, NULL, CALL_MAIN);
488
489 /* clean up the EAL */
490 rte_eal_cleanup();
491
492 return 0;
493 }
494