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