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.
38 #include <sys/types.h>
40 #include <sys/queue.h>
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
48 #include <rte_memory.h>
49 #include <rte_memcpy.h>
51 #include <rte_launch.h>
52 #include <rte_atomic.h>
53 #include <rte_cycles.h>
54 #include <rte_prefetch.h>
55 #include <rte_lcore.h>
56 #include <rte_per_lcore.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_interrupts.h>
60 #include <rte_random.h>
61 #include <rte_debug.h>
62 #include <rte_ether.h>
63 #include <rte_ethdev.h>
65 #include <rte_mempool.h>
67 #include <rte_string_fns.h>
74 static struct rte_eth_conf port_conf = {
76 .mq_mode = ETH_MQ_RX_RSS,
78 .header_split = 0, /**< Header Split disabled */
79 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
80 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
81 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
82 .hw_strip_crc = 1, /**< CRC stripped by hardware */
91 .mq_mode = ETH_MQ_TX_NONE,
96 app_assign_worker_ids(void)
98 uint32_t lcore, worker_id;
100 /* Assign ID for each worker */
102 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
103 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore].worker;
105 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
109 lp_worker->worker_id = worker_id;
115 app_init_mbuf_pools(void)
117 unsigned socket, lcore;
119 /* Init the buffer pools */
120 for (socket = 0; socket < APP_MAX_SOCKETS; socket ++) {
122 if (app_is_socket_used(socket) == 0) {
126 snprintf(name, sizeof(name), "mbuf_pool_%u", socket);
127 printf("Creating the mbuf pool for socket %u ...\n", socket);
128 app.pools[socket] = rte_pktmbuf_pool_create(
129 name, APP_DEFAULT_MEMPOOL_BUFFERS,
130 APP_DEFAULT_MEMPOOL_CACHE_SIZE,
131 0, APP_DEFAULT_MBUF_DATA_SIZE, socket);
132 if (app.pools[socket] == NULL) {
133 rte_panic("Cannot create mbuf pool on socket %u\n", socket);
137 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
138 if (app.lcore_params[lcore].type == e_APP_LCORE_DISABLED) {
142 socket = rte_lcore_to_socket_id(lcore);
143 app.lcore_params[lcore].pool = app.pools[socket];
148 app_init_lpm_tables(void)
150 unsigned socket, lcore;
152 /* Init the LPM tables */
153 for (socket = 0; socket < APP_MAX_SOCKETS; socket ++) {
157 if (app_is_socket_used(socket) == 0) {
161 struct rte_lpm_config lpm_config;
163 lpm_config.max_rules = APP_MAX_LPM_RULES;
164 lpm_config.number_tbl8s = 256;
165 lpm_config.flags = 0;
166 snprintf(name, sizeof(name), "lpm_table_%u", socket);
167 printf("Creating the LPM table for socket %u ...\n", socket);
168 app.lpm_tables[socket] = rte_lpm_create(
172 if (app.lpm_tables[socket] == NULL) {
173 rte_panic("Unable to create LPM table on socket %u\n", socket);
176 for (rule = 0; rule < app.n_lpm_rules; rule ++) {
179 ret = rte_lpm_add(app.lpm_tables[socket],
180 app.lpm_rules[rule].ip,
181 app.lpm_rules[rule].depth,
182 app.lpm_rules[rule].if_out);
185 rte_panic("Unable to add entry %u (%x/%u => %u) to the LPM table on socket %u (%d)\n",
187 (unsigned) app.lpm_rules[rule].ip,
188 (unsigned) app.lpm_rules[rule].depth,
189 (unsigned) app.lpm_rules[rule].if_out,
197 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
198 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
202 socket = rte_lcore_to_socket_id(lcore);
203 app.lcore_params[lcore].worker.lpm_table = app.lpm_tables[socket];
208 app_init_rings_rx(void)
212 /* Initialize the rings for the RX side */
213 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
214 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
215 unsigned socket_io, lcore_worker;
217 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
218 (lp_io->rx.n_nic_queues == 0)) {
222 socket_io = rte_lcore_to_socket_id(lcore);
224 for (lcore_worker = 0; lcore_worker < APP_MAX_LCORES; lcore_worker ++) {
226 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore_worker].worker;
227 struct rte_ring *ring = NULL;
229 if (app.lcore_params[lcore_worker].type != e_APP_LCORE_WORKER) {
233 printf("Creating ring to connect I/O lcore %u (socket %u) with worker lcore %u ...\n",
237 snprintf(name, sizeof(name), "app_ring_rx_s%u_io%u_w%u",
241 ring = rte_ring_create(
245 RING_F_SP_ENQ | RING_F_SC_DEQ);
247 rte_panic("Cannot create ring to connect I/O core %u with worker core %u\n",
252 lp_io->rx.rings[lp_io->rx.n_rings] = ring;
253 lp_io->rx.n_rings ++;
255 lp_worker->rings_in[lp_worker->n_rings_in] = ring;
256 lp_worker->n_rings_in ++;
260 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
261 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
263 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
264 (lp_io->rx.n_nic_queues == 0)) {
268 if (lp_io->rx.n_rings != app_get_lcores_worker()) {
269 rte_panic("Algorithmic error (I/O RX rings)\n");
273 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
274 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore].worker;
276 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
280 if (lp_worker->n_rings_in != app_get_lcores_io_rx()) {
281 rte_panic("Algorithmic error (worker input rings)\n");
287 app_init_rings_tx(void)
291 /* Initialize the rings for the TX side */
292 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
293 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore].worker;
296 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
300 for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
302 struct app_lcore_params_io *lp_io = NULL;
303 struct rte_ring *ring;
304 uint32_t socket_io, lcore_io;
306 if (app.nic_tx_port_mask[port] == 0) {
310 if (app_get_lcore_for_nic_tx(port, &lcore_io) < 0) {
311 rte_panic("Algorithmic error (no I/O core to handle TX of port %u)\n",
315 lp_io = &app.lcore_params[lcore_io].io;
316 socket_io = rte_lcore_to_socket_id(lcore_io);
318 printf("Creating ring to connect worker lcore %u with TX port %u (through I/O lcore %u) (socket %u) ...\n",
319 lcore, port, (unsigned)lcore_io, (unsigned)socket_io);
320 snprintf(name, sizeof(name), "app_ring_tx_s%u_w%u_p%u", socket_io, lcore, port);
321 ring = rte_ring_create(
325 RING_F_SP_ENQ | RING_F_SC_DEQ);
327 rte_panic("Cannot create ring to connect worker core %u with TX port %u\n",
332 lp_worker->rings_out[port] = ring;
333 lp_io->tx.rings[port][lp_worker->worker_id] = ring;
337 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
338 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
341 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
342 (lp_io->tx.n_nic_ports == 0)) {
346 for (i = 0; i < lp_io->tx.n_nic_ports; i ++){
349 port = lp_io->tx.nic_ports[i];
350 for (j = 0; j < app_get_lcores_worker(); j ++) {
351 if (lp_io->tx.rings[port][j] == NULL) {
352 rte_panic("Algorithmic error (I/O TX rings)\n");
359 /* Check the link status of all ports in up to 9s, and print them finally */
361 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
363 #define CHECK_INTERVAL 100 /* 100ms */
364 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
366 uint8_t count, all_ports_up, print_flag = 0;
367 struct rte_eth_link link;
368 uint32_t n_rx_queues, n_tx_queues;
370 printf("\nChecking link status");
372 for (count = 0; count <= MAX_CHECK_TIME; count++) {
374 for (portid = 0; portid < port_num; portid++) {
375 if ((port_mask & (1 << portid)) == 0)
377 n_rx_queues = app_get_nic_rx_queues_per_port(portid);
378 n_tx_queues = app.nic_tx_port_mask[portid];
379 if ((n_rx_queues == 0) && (n_tx_queues == 0))
381 memset(&link, 0, sizeof(link));
382 rte_eth_link_get_nowait(portid, &link);
383 /* print link status if flag set */
384 if (print_flag == 1) {
385 if (link.link_status)
387 "Port%d Link Up - speed %uMbps - %s\n",
388 portid, link.link_speed,
389 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
390 ("full-duplex") : ("half-duplex\n"));
392 printf("Port %d Link Down\n", portid);
395 /* clear all_ports_up flag if any link down */
396 if (link.link_status == ETH_LINK_DOWN) {
401 /* after finally printing all link status, get out */
405 if (all_ports_up == 0) {
408 rte_delay_ms(CHECK_INTERVAL);
411 /* set the print_flag if all ports up or timeout */
412 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
427 uint32_t n_rx_queues, n_tx_queues;
429 /* Init NIC ports and queues, then start the ports */
430 for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
431 struct rte_mempool *pool;
432 uint16_t nic_rx_ring_size;
433 uint16_t nic_tx_ring_size;
435 n_rx_queues = app_get_nic_rx_queues_per_port(port);
436 n_tx_queues = app.nic_tx_port_mask[port];
438 if ((n_rx_queues == 0) && (n_tx_queues == 0)) {
443 printf("Initializing NIC port %u ...\n", port);
444 ret = rte_eth_dev_configure(
446 (uint8_t) n_rx_queues,
447 (uint8_t) n_tx_queues,
450 rte_panic("Cannot init NIC port %u (%d)\n", port, ret);
452 rte_eth_promiscuous_enable(port);
454 nic_rx_ring_size = app.nic_rx_ring_size;
455 nic_tx_ring_size = app.nic_tx_ring_size;
456 ret = rte_eth_dev_adjust_nb_rx_tx_desc(
457 port, &nic_rx_ring_size, &nic_tx_ring_size);
459 rte_panic("Cannot adjust number of descriptors for port %u (%d)\n",
462 app.nic_rx_ring_size = nic_rx_ring_size;
463 app.nic_tx_ring_size = nic_tx_ring_size;
466 for (queue = 0; queue < APP_MAX_RX_QUEUES_PER_NIC_PORT; queue ++) {
467 if (app.nic_rx_queue_mask[port][queue] == 0) {
471 app_get_lcore_for_nic_rx(port, queue, &lcore);
472 socket = rte_lcore_to_socket_id(lcore);
473 pool = app.lcore_params[lcore].pool;
475 printf("Initializing NIC port %u RX queue %u ...\n",
477 ret = rte_eth_rx_queue_setup(
480 (uint16_t) app.nic_rx_ring_size,
485 rte_panic("Cannot init RX queue %u for port %u (%d)\n",
491 if (app.nic_tx_port_mask[port] == 1) {
492 app_get_lcore_for_nic_tx(port, &lcore);
493 socket = rte_lcore_to_socket_id(lcore);
494 printf("Initializing NIC port %u TX queue 0 ...\n",
496 ret = rte_eth_tx_queue_setup(
499 (uint16_t) app.nic_tx_ring_size,
503 rte_panic("Cannot init TX queue 0 for port %d (%d)\n",
510 ret = rte_eth_dev_start(port);
512 rte_panic("Cannot start port %d (%d)\n", port, ret);
516 check_all_ports_link_status(APP_MAX_NIC_PORTS, (~0x0));
522 app_assign_worker_ids();
523 app_init_mbuf_pools();
524 app_init_lpm_tables();
529 printf("Initialization completed.\n");