4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Sample application demostrating how to do packet I/O in a multi-process
36 * environment. The same code can be run as a primary process and as a
37 * secondary process, just with a different proc-id parameter in each case
38 * (apart from the EAL flag to indicate a secondary process).
40 * Each process will read from the same ports, given by the port-mask
41 * parameter, which should be the same in each case, just using a different
42 * queue per port as determined by the proc-id parameter.
51 #include <sys/queue.h>
56 #include <rte_common.h>
58 #include <rte_memory.h>
59 #include <rte_memzone.h>
60 #include <rte_launch.h>
62 #include <rte_per_lcore.h>
63 #include <rte_lcore.h>
64 #include <rte_debug.h>
65 #include <rte_atomic.h>
66 #include <rte_branch_prediction.h>
67 #include <rte_debug.h>
68 #include <rte_interrupts.h>
70 #include <rte_ether.h>
71 #include <rte_ethdev.h>
72 #include <rte_mempool.h>
73 #include <rte_memcpy.h>
75 #include <rte_string_fns.h>
76 #include <rte_cycles.h>
78 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
80 #define NB_MBUFS 64*1024 /* use 64k mbufs */
81 #define MBUF_CACHE_SIZE 256
83 #define RX_RING_SIZE 128
84 #define TX_RING_SIZE 512
86 #define PARAM_PROC_ID "proc-id"
87 #define PARAM_NUM_PROCS "num-procs"
89 /* for each lcore, record the elements of the ports array to use */
95 /* structure to record the rx and tx packets. Put two per cache line as ports
101 } __attribute__((aligned(RTE_CACHE_LINE_SIZE / 2)));
103 static int proc_id = -1;
104 static unsigned num_procs = 0;
106 static uint8_t ports[RTE_MAX_ETHPORTS];
107 static unsigned num_ports = 0;
109 static struct lcore_ports lcore_ports[RTE_MAX_LCORE];
110 static struct port_stats pstats[RTE_MAX_ETHPORTS];
112 /* prints the usage statement and quits with an error message */
114 smp_usage(const char *prgname, const char *errmsg)
116 printf("\nError: %s\n",errmsg);
117 printf("\n%s [EAL options] -- -p <port mask> "
118 "--"PARAM_NUM_PROCS" <n>"
119 " --"PARAM_PROC_ID" <id>\n"
120 "-p : a hex bitmask indicating what ports are to be used\n"
121 "--num-procs: the number of processes which will be used\n"
122 "--proc-id : the id of the current process (id < num-procs)\n"
129 /* signal handler configured for SIGTERM and SIGINT to print stats on exit */
131 print_stats(int signum)
134 printf("\nExiting on signal %d\n\n", signum);
135 for (i = 0; i < num_ports; i++){
136 const uint8_t p_num = ports[i];
137 printf("Port %u: RX - %u, TX - %u, Drop - %u\n", (unsigned)p_num,
138 pstats[p_num].rx, pstats[p_num].tx, pstats[p_num].drop);
143 /* Parse the argument given in the command line of the application */
145 smp_parse_args(int argc, char **argv)
150 unsigned i, port_mask = 0;
151 char *prgname = argv[0];
152 static struct option lgopts[] = {
153 {PARAM_NUM_PROCS, 1, 0, 0},
154 {PARAM_PROC_ID, 1, 0, 0},
160 while ((opt = getopt_long(argc, argvopt, "p:", \
161 lgopts, &option_index)) != EOF) {
165 port_mask = strtoull(optarg, NULL, 16);
169 if (strncmp(lgopts[option_index].name, PARAM_NUM_PROCS, 8) == 0)
170 num_procs = atoi(optarg);
171 else if (strncmp(lgopts[option_index].name, PARAM_PROC_ID, 7) == 0)
172 proc_id = atoi(optarg);
176 smp_usage(prgname, "Cannot parse all command-line arguments\n");
181 argv[optind-1] = prgname;
184 smp_usage(prgname, "Invalid or missing proc-id parameter\n");
185 if (rte_eal_process_type() == RTE_PROC_PRIMARY && num_procs == 0)
186 smp_usage(prgname, "Invalid or missing num-procs parameter\n");
188 smp_usage(prgname, "Invalid or missing port mask\n");
190 /* get the port numbers from the port mask */
191 for(i = 0; i < rte_eth_dev_count(); i++)
192 if(port_mask & (1 << i))
193 ports[num_ports++] = (uint8_t)i;
196 optind = 0; /* reset getopt lib */
202 * Initialises a given port using global settings and with the rx buffers
203 * coming from the mbuf_pool passed as parameter
206 smp_port_init(uint8_t port, struct rte_mempool *mbuf_pool, uint16_t num_queues)
208 struct rte_eth_conf port_conf = {
210 .mq_mode = ETH_MQ_RX_RSS,
212 .header_split = 0, /**< Header Split disabled */
213 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
214 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
215 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
216 .hw_strip_crc = 0, /**< CRC stripped by hardware */
221 .rss_hf = ETH_RSS_IP,
225 .mq_mode = ETH_MQ_TX_NONE,
228 const uint16_t rx_rings = num_queues, tx_rings = num_queues;
229 struct rte_eth_dev_info info;
233 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
236 if (port >= rte_eth_dev_count())
239 printf("# Initialising port %u... ", (unsigned)port);
242 rte_eth_dev_info_get(port, &info);
243 info.default_rxconf.rx_drop_en = 1;
245 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
249 for (q = 0; q < rx_rings; q ++) {
250 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
251 rte_eth_dev_socket_id(port),
252 &info.default_rxconf,
258 for (q = 0; q < tx_rings; q ++) {
259 retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
260 rte_eth_dev_socket_id(port),
266 rte_eth_promiscuous_enable(port);
268 retval = rte_eth_dev_start(port);
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.
279 assign_ports_to_cores(void)
282 const unsigned lcores = rte_eal_get_configuration()->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;
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;
296 ports_assigned += lcore_ports[i].num_ports;
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.
305 lcore_main(void *arg __rte_unused)
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;
315 if (start_port == end_port){
316 printf("Lcore %u has nothing to do\n", id);
320 /* build up message in msgbuf before printing to decrease likelihood
321 * of multi-core message interleaving.
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]);
329 printf("%s\n", msgbuf);
330 printf("lcore %u using queue %u of each port\n", id, (unsigned)q_id);
332 /* handle packet I/O from the ports, reading and writing to the
333 * queue number corresponding to our process number (not lcore id)
337 struct rte_mbuf *buf[PKT_BURST];
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);
345 pstats[src].rx += rx_c;
347 const uint16_t tx_c = rte_eth_tx_burst(dst, q_id, buf, rx_c);
348 pstats[dst].tx += tx_c;
350 pstats[dst].drop += (rx_c - tx_c);
351 for (i = tx_c; i < rx_c; i++)
352 rte_pktmbuf_free(buf[i]);
358 /* Check the link status of all ports in up to 9s, and print them finally */
360 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
362 #define CHECK_INTERVAL 100 /* 100ms */
363 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
364 uint8_t portid, count, all_ports_up, print_flag = 0;
365 struct rte_eth_link link;
367 printf("\nChecking link status");
369 for (count = 0; count <= MAX_CHECK_TIME; count++) {
371 for (portid = 0; portid < port_num; portid++) {
372 if ((port_mask & (1 << portid)) == 0)
374 memset(&link, 0, sizeof(link));
375 rte_eth_link_get_nowait(portid, &link);
376 /* print link status if flag set */
377 if (print_flag == 1) {
378 if (link.link_status)
379 printf("Port %d Link Up - speed %u "
380 "Mbps - %s\n", (uint8_t)portid,
381 (unsigned)link.link_speed,
382 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
383 ("full-duplex") : ("half-duplex\n"));
385 printf("Port %d Link Down\n",
389 /* clear all_ports_up flag if any link down */
390 if (link.link_status == ETH_LINK_DOWN) {
395 /* after finally printing all link status, get out */
399 if (all_ports_up == 0) {
402 rte_delay_ms(CHECK_INTERVAL);
405 /* set the print_flag if all ports up or timeout */
406 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
414 * Performs initialisation and then calls the lcore_main on each core
415 * to do the packet-processing work.
418 main(int argc, char **argv)
420 static const char *_SMP_MBUF_POOL = "SMP_MBUF_POOL";
423 enum rte_proc_type_t proc_type;
424 struct rte_mempool *mp;
426 /* set up signal handlers to print stats on exit */
427 signal(SIGINT, print_stats);
428 signal(SIGTERM, print_stats);
430 /* initialise the EAL for all */
431 ret = rte_eal_init(argc, argv);
433 rte_exit(EXIT_FAILURE, "Cannot init EAL\n");
437 /* determine the NIC devices available */
438 if (rte_eth_dev_count() == 0)
439 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
441 /* parse application arguments (those after the EAL ones) */
442 smp_parse_args(argc, argv);
444 proc_type = rte_eal_process_type();
445 mp = (proc_type == RTE_PROC_SECONDARY) ?
446 rte_mempool_lookup(_SMP_MBUF_POOL) :
447 rte_pktmbuf_pool_create(_SMP_MBUF_POOL, NB_MBUFS,
448 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
451 rte_exit(EXIT_FAILURE, "Cannot get memory pool for buffers\n");
454 rte_exit(EXIT_FAILURE, "Application must use an even number of ports\n");
455 for(i = 0; i < num_ports; i++){
456 if(proc_type == RTE_PROC_PRIMARY)
457 if (smp_port_init(ports[i], mp, (uint16_t)num_procs) < 0)
458 rte_exit(EXIT_FAILURE, "Error initialising ports\n");
461 if (proc_type == RTE_PROC_PRIMARY)
462 check_all_ports_link_status((uint8_t)num_ports, (~0x0));
464 assign_ports_to_cores();
466 RTE_LOG(INFO, APP, "Finished Process Init.\n");
468 rte_eal_mp_remote_launch(lcore_main, NULL, CALL_MASTER);