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