1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
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).
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_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>
43 #include <rte_string_fns.h>
44 #include <rte_cycles.h>
46 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
48 #define NB_MBUFS 64*1024 /* use 64k mbufs */
49 #define MBUF_CACHE_SIZE 256
51 #define RX_RING_SIZE 128
52 #define TX_RING_SIZE 512
54 #define PARAM_PROC_ID "proc-id"
55 #define PARAM_NUM_PROCS "num-procs"
57 /* for each lcore, record the elements of the ports array to use */
63 /* structure to record the rx and tx packets. Put two per cache line as ports
69 } __attribute__((aligned(RTE_CACHE_LINE_SIZE / 2)));
71 static int proc_id = -1;
72 static unsigned num_procs = 0;
74 static uint16_t ports[RTE_MAX_ETHPORTS];
75 static unsigned num_ports = 0;
77 static struct lcore_ports lcore_ports[RTE_MAX_LCORE];
78 static struct port_stats pstats[RTE_MAX_ETHPORTS];
80 /* prints the usage statement and quits with an error message */
82 smp_usage(const char *prgname, const char *errmsg)
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"
97 /* signal handler configured for SIGTERM and SIGINT to print stats on exit */
99 print_stats(int signum)
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);
111 /* Parse the argument given in the command line of the application */
113 smp_parse_args(int argc, char **argv)
118 unsigned 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},
128 while ((opt = getopt_long(argc, argvopt, "p:", \
129 lgopts, &option_index)) != EOF) {
133 port_mask = strtoull(optarg, NULL, 16);
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);
144 smp_usage(prgname, "Cannot parse all command-line arguments\n");
149 argv[optind-1] = prgname;
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");
156 smp_usage(prgname, "Invalid or missing port mask\n");
158 /* get the port numbers from the port mask */
159 for(i = 0; i < rte_eth_dev_count(); i++)
160 if(port_mask & (1 << i))
161 ports[num_ports++] = (uint8_t)i;
164 optind = 1; /* reset getopt lib */
170 * Initialises a given port using global settings and with the rx buffers
171 * coming from the mbuf_pool passed as parameter
174 smp_port_init(uint16_t port, struct rte_mempool *mbuf_pool,
177 struct rte_eth_conf port_conf = {
179 .mq_mode = ETH_MQ_RX_RSS,
181 .header_split = 0, /**< Header Split disabled */
182 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
183 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
184 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
185 .hw_strip_crc = 1, /**< CRC stripped by hardware */
190 .rss_hf = ETH_RSS_IP,
194 .mq_mode = ETH_MQ_TX_NONE,
197 const uint16_t rx_rings = num_queues, tx_rings = num_queues;
198 struct rte_eth_dev_info info;
201 uint16_t nb_rxd = RX_RING_SIZE;
202 uint16_t nb_txd = TX_RING_SIZE;
204 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
207 if (port >= rte_eth_dev_count())
210 printf("# Initialising port %u... ", port);
213 rte_eth_dev_info_get(port, &info);
214 info.default_rxconf.rx_drop_en = 1;
216 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
220 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
224 for (q = 0; q < rx_rings; q ++) {
225 retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
226 rte_eth_dev_socket_id(port),
227 &info.default_rxconf,
233 for (q = 0; q < tx_rings; q ++) {
234 retval = rte_eth_tx_queue_setup(port, q, nb_txd,
235 rte_eth_dev_socket_id(port),
241 rte_eth_promiscuous_enable(port);
243 retval = rte_eth_dev_start(port);
250 /* Goes through each of the lcores and calculates what ports should
251 * be used by that core. Fills in the global lcore_ports[] array.
254 assign_ports_to_cores(void)
257 const unsigned lcores = rte_eal_get_configuration()->lcore_count;
258 const unsigned port_pairs = num_ports / 2;
259 const unsigned pairs_per_lcore = port_pairs / lcores;
260 unsigned extra_pairs = port_pairs % lcores;
261 unsigned ports_assigned = 0;
264 RTE_LCORE_FOREACH(i) {
265 lcore_ports[i].start_port = ports_assigned;
266 lcore_ports[i].num_ports = pairs_per_lcore * 2;
267 if (extra_pairs > 0) {
268 lcore_ports[i].num_ports += 2;
271 ports_assigned += lcore_ports[i].num_ports;
275 /* Main function used by the processing threads.
276 * Prints out some configuration details for the thread and then begins
277 * performing packet RX and TX.
280 lcore_main(void *arg __rte_unused)
282 const unsigned id = rte_lcore_id();
283 const unsigned start_port = lcore_ports[id].start_port;
284 const unsigned end_port = start_port + lcore_ports[id].num_ports;
285 const uint16_t q_id = (uint16_t)proc_id;
290 if (start_port == end_port){
291 printf("Lcore %u has nothing to do\n", id);
295 /* build up message in msgbuf before printing to decrease likelihood
296 * of multi-core message interleaving.
298 msgbufpos += snprintf(msgbuf, sizeof(msgbuf) - msgbufpos,
299 "Lcore %u using ports ", id);
300 for (p = start_port; p < end_port; p++){
301 msgbufpos += snprintf(msgbuf + msgbufpos, sizeof(msgbuf) - msgbufpos,
302 "%u ", (unsigned)ports[p]);
304 printf("%s\n", msgbuf);
305 printf("lcore %u using queue %u of each port\n", id, (unsigned)q_id);
307 /* handle packet I/O from the ports, reading and writing to the
308 * queue number corresponding to our process number (not lcore id)
312 struct rte_mbuf *buf[PKT_BURST];
314 for (p = start_port; p < end_port; p++) {
315 const uint8_t src = ports[p];
316 const uint8_t dst = ports[p ^ 1]; /* 0 <-> 1, 2 <-> 3 etc */
317 const uint16_t rx_c = rte_eth_rx_burst(src, q_id, buf, PKT_BURST);
320 pstats[src].rx += rx_c;
322 const uint16_t tx_c = rte_eth_tx_burst(dst, q_id, buf, rx_c);
323 pstats[dst].tx += tx_c;
325 pstats[dst].drop += (rx_c - tx_c);
326 for (i = tx_c; i < rx_c; i++)
327 rte_pktmbuf_free(buf[i]);
333 /* Check the link status of all ports in up to 9s, and print them finally */
335 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
337 #define CHECK_INTERVAL 100 /* 100ms */
338 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
340 uint8_t count, all_ports_up, print_flag = 0;
341 struct rte_eth_link link;
343 printf("\nChecking link status");
345 for (count = 0; count <= MAX_CHECK_TIME; count++) {
347 for (portid = 0; portid < port_num; portid++) {
348 if ((port_mask & (1 << portid)) == 0)
350 memset(&link, 0, sizeof(link));
351 rte_eth_link_get_nowait(portid, &link);
352 /* print link status if flag set */
353 if (print_flag == 1) {
354 if (link.link_status)
356 "Port%d Link Up. Speed %u Mbps - %s\n",
357 portid, link.link_speed,
358 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
359 ("full-duplex") : ("half-duplex\n"));
361 printf("Port %d Link Down\n", portid);
364 /* clear all_ports_up flag if any link down */
365 if (link.link_status == ETH_LINK_DOWN) {
370 /* after finally printing all link status, get out */
374 if (all_ports_up == 0) {
377 rte_delay_ms(CHECK_INTERVAL);
380 /* set the print_flag if all ports up or timeout */
381 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
389 * Performs initialisation and then calls the lcore_main on each core
390 * to do the packet-processing work.
393 main(int argc, char **argv)
395 static const char *_SMP_MBUF_POOL = "SMP_MBUF_POOL";
398 enum rte_proc_type_t proc_type;
399 struct rte_mempool *mp;
401 /* set up signal handlers to print stats on exit */
402 signal(SIGINT, print_stats);
403 signal(SIGTERM, print_stats);
405 /* initialise the EAL for all */
406 ret = rte_eal_init(argc, argv);
408 rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
412 /* determine the NIC devices available */
413 if (rte_eth_dev_count() == 0)
414 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
416 /* parse application arguments (those after the EAL ones) */
417 smp_parse_args(argc, argv);
419 proc_type = rte_eal_process_type();
420 mp = (proc_type == RTE_PROC_SECONDARY) ?
421 rte_mempool_lookup(_SMP_MBUF_POOL) :
422 rte_pktmbuf_pool_create(_SMP_MBUF_POOL, NB_MBUFS,
423 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
426 rte_exit(EXIT_FAILURE, "Cannot get memory pool for buffers\n");
429 rte_exit(EXIT_FAILURE, "Application must use an even number of ports\n");
430 for(i = 0; i < num_ports; i++){
431 if(proc_type == RTE_PROC_PRIMARY)
432 if (smp_port_init(ports[i], mp, (uint16_t)num_procs) < 0)
433 rte_exit(EXIT_FAILURE, "Error initialising ports\n");
436 if (proc_type == RTE_PROC_PRIMARY)
437 check_all_ports_link_status((uint8_t)num_ports, (~0x0));
439 assign_ports_to_cores();
441 RTE_LOG(INFO, APP, "Finished Process Init.\n");
443 rte_eal_mp_remote_launch(lcore_main, NULL, CALL_MASTER);