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