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