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>
50 #include <rte_memzone.h>
51 #include <rte_tailq.h>
53 #include <rte_per_lcore.h>
54 #include <rte_launch.h>
55 #include <rte_atomic.h>
56 #include <rte_cycles.h>
57 #include <rte_prefetch.h>
58 #include <rte_lcore.h>
59 #include <rte_per_lcore.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_interrupts.h>
63 #include <rte_random.h>
64 #include <rte_debug.h>
65 #include <rte_ether.h>
66 #include <rte_ethdev.h>
68 #include <rte_mempool.h>
70 #include <rte_string_fns.h>
77 static struct rte_eth_conf port_conf = {
79 .mq_mode = ETH_MQ_RX_RSS,
81 .header_split = 0, /**< Header Split disabled */
82 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
83 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
84 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
85 .hw_strip_crc = 0, /**< CRC stripped by hardware */
94 .mq_mode = ETH_MQ_TX_NONE,
99 app_assign_worker_ids(void)
101 uint32_t lcore, worker_id;
103 /* Assign ID for each worker */
105 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
106 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore].worker;
108 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
112 lp_worker->worker_id = worker_id;
118 app_init_mbuf_pools(void)
120 unsigned socket, lcore;
122 /* Init the buffer pools */
123 for (socket = 0; socket < APP_MAX_SOCKETS; socket ++) {
125 if (app_is_socket_used(socket) == 0) {
129 snprintf(name, sizeof(name), "mbuf_pool_%u", socket);
130 printf("Creating the mbuf pool for socket %u ...\n", socket);
131 app.pools[socket] = rte_mempool_create(
133 APP_DEFAULT_MEMPOOL_BUFFERS,
134 APP_DEFAULT_MBUF_SIZE,
135 APP_DEFAULT_MEMPOOL_CACHE_SIZE,
136 sizeof(struct rte_pktmbuf_pool_private),
137 rte_pktmbuf_pool_init, NULL,
138 rte_pktmbuf_init, NULL,
141 if (app.pools[socket] == NULL) {
142 rte_panic("Cannot create mbuf pool on socket %u\n", socket);
146 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
147 if (app.lcore_params[lcore].type == e_APP_LCORE_DISABLED) {
151 socket = rte_lcore_to_socket_id(lcore);
152 app.lcore_params[lcore].pool = app.pools[socket];
157 app_init_lpm_tables(void)
159 unsigned socket, lcore;
161 /* Init the LPM tables */
162 for (socket = 0; socket < APP_MAX_SOCKETS; socket ++) {
166 if (app_is_socket_used(socket) == 0) {
170 snprintf(name, sizeof(name), "lpm_table_%u", socket);
171 printf("Creating the LPM table for socket %u ...\n", socket);
172 app.lpm_tables[socket] = rte_lpm_create(
177 if (app.lpm_tables[socket] == NULL) {
178 rte_panic("Unable to create LPM table on socket %u\n", socket);
181 for (rule = 0; rule < app.n_lpm_rules; rule ++) {
184 ret = rte_lpm_add(app.lpm_tables[socket],
185 app.lpm_rules[rule].ip,
186 app.lpm_rules[rule].depth,
187 app.lpm_rules[rule].if_out);
190 rte_panic("Unable to add entry %u (%x/%u => %u) to the LPM table on socket %u (%d)\n",
192 (unsigned) app.lpm_rules[rule].ip,
193 (unsigned) app.lpm_rules[rule].depth,
194 (unsigned) app.lpm_rules[rule].if_out,
202 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
203 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
207 socket = rte_lcore_to_socket_id(lcore);
208 app.lcore_params[lcore].worker.lpm_table = app.lpm_tables[socket];
213 app_init_rings_rx(void)
217 /* Initialize the rings for the RX side */
218 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
219 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
220 unsigned socket_io, lcore_worker;
222 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
223 (lp_io->rx.n_nic_queues == 0)) {
227 socket_io = rte_lcore_to_socket_id(lcore);
229 for (lcore_worker = 0; lcore_worker < APP_MAX_LCORES; lcore_worker ++) {
231 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore_worker].worker;
232 struct rte_ring *ring = NULL;
234 if (app.lcore_params[lcore_worker].type != e_APP_LCORE_WORKER) {
238 printf("Creating ring to connect I/O lcore %u (socket %u) with worker lcore %u ...\n",
242 snprintf(name, sizeof(name), "app_ring_rx_s%u_io%u_w%u",
246 ring = rte_ring_create(
250 RING_F_SP_ENQ | RING_F_SC_DEQ);
252 rte_panic("Cannot create ring to connect I/O core %u with worker core %u\n",
257 lp_io->rx.rings[lp_io->rx.n_rings] = ring;
258 lp_io->rx.n_rings ++;
260 lp_worker->rings_in[lp_worker->n_rings_in] = ring;
261 lp_worker->n_rings_in ++;
265 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
266 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
268 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
269 (lp_io->rx.n_nic_queues == 0)) {
273 if (lp_io->rx.n_rings != app_get_lcores_worker()) {
274 rte_panic("Algorithmic error (I/O RX rings)\n");
278 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
279 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore].worker;
281 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
285 if (lp_worker->n_rings_in != app_get_lcores_io_rx()) {
286 rte_panic("Algorithmic error (worker input rings)\n");
292 app_init_rings_tx(void)
296 /* Initialize the rings for the TX side */
297 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
298 struct app_lcore_params_worker *lp_worker = &app.lcore_params[lcore].worker;
301 if (app.lcore_params[lcore].type != e_APP_LCORE_WORKER) {
305 for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
307 struct app_lcore_params_io *lp_io = NULL;
308 struct rte_ring *ring;
309 uint32_t socket_io, lcore_io;
311 if (app.nic_tx_port_mask[port] == 0) {
315 if (app_get_lcore_for_nic_tx((uint8_t) port, &lcore_io) < 0) {
316 rte_panic("Algorithmic error (no I/O core to handle TX of port %u)\n",
320 lp_io = &app.lcore_params[lcore_io].io;
321 socket_io = rte_lcore_to_socket_id(lcore_io);
323 printf("Creating ring to connect worker lcore %u with TX port %u (through I/O lcore %u) (socket %u) ...\n",
324 lcore, port, (unsigned)lcore_io, (unsigned)socket_io);
325 snprintf(name, sizeof(name), "app_ring_tx_s%u_w%u_p%u", socket_io, lcore, port);
326 ring = rte_ring_create(
330 RING_F_SP_ENQ | RING_F_SC_DEQ);
332 rte_panic("Cannot create ring to connect worker core %u with TX port %u\n",
337 lp_worker->rings_out[port] = ring;
338 lp_io->tx.rings[port][lp_worker->worker_id] = ring;
342 for (lcore = 0; lcore < APP_MAX_LCORES; lcore ++) {
343 struct app_lcore_params_io *lp_io = &app.lcore_params[lcore].io;
346 if ((app.lcore_params[lcore].type != e_APP_LCORE_IO) ||
347 (lp_io->tx.n_nic_ports == 0)) {
351 for (i = 0; i < lp_io->tx.n_nic_ports; i ++){
354 port = lp_io->tx.nic_ports[i];
355 for (j = 0; j < app_get_lcores_worker(); j ++) {
356 if (lp_io->tx.rings[port][j] == NULL) {
357 rte_panic("Algorithmic error (I/O TX rings)\n");
364 /* Check the link status of all ports in up to 9s, and print them finally */
366 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
368 #define CHECK_INTERVAL 100 /* 100ms */
369 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
370 uint8_t portid, count, all_ports_up, print_flag = 0;
371 struct rte_eth_link link;
372 uint32_t n_rx_queues, n_tx_queues;
374 printf("\nChecking link status");
376 for (count = 0; count <= MAX_CHECK_TIME; count++) {
378 for (portid = 0; portid < port_num; portid++) {
379 if ((port_mask & (1 << portid)) == 0)
381 n_rx_queues = app_get_nic_rx_queues_per_port(portid);
382 n_tx_queues = app.nic_tx_port_mask[portid];
383 if ((n_rx_queues == 0) && (n_tx_queues == 0))
385 memset(&link, 0, sizeof(link));
386 rte_eth_link_get_nowait(portid, &link);
387 /* print link status if flag set */
388 if (print_flag == 1) {
389 if (link.link_status)
390 printf("Port %d Link Up - speed %u "
391 "Mbps - %s\n", (uint8_t)portid,
392 (unsigned)link.link_speed,
393 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
394 ("full-duplex") : ("half-duplex\n"));
396 printf("Port %d Link Down\n",
400 /* clear all_ports_up flag if any link down */
401 if (link.link_status == 0) {
406 /* after finally printing all link status, get out */
410 if (all_ports_up == 0) {
413 rte_delay_ms(CHECK_INTERVAL);
416 /* set the print_flag if all ports up or timeout */
417 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
431 uint32_t n_rx_queues, n_tx_queues;
433 /* Init NIC ports and queues, then start the ports */
434 for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
435 struct rte_mempool *pool;
437 n_rx_queues = app_get_nic_rx_queues_per_port(port);
438 n_tx_queues = app.nic_tx_port_mask[port];
440 if ((n_rx_queues == 0) && (n_tx_queues == 0)) {
445 printf("Initializing NIC port %u ...\n", (unsigned) port);
446 ret = rte_eth_dev_configure(
448 (uint8_t) n_rx_queues,
449 (uint8_t) n_tx_queues,
452 rte_panic("Cannot init NIC port %u (%d)\n", (unsigned) port, ret);
454 rte_eth_promiscuous_enable(port);
457 for (queue = 0; queue < APP_MAX_RX_QUEUES_PER_NIC_PORT; queue ++) {
458 if (app.nic_rx_queue_mask[port][queue] == 0) {
462 app_get_lcore_for_nic_rx(port, queue, &lcore);
463 socket = rte_lcore_to_socket_id(lcore);
464 pool = app.lcore_params[lcore].pool;
466 printf("Initializing NIC port %u RX queue %u ...\n",
469 ret = rte_eth_rx_queue_setup(
472 (uint16_t) app.nic_rx_ring_size,
477 rte_panic("Cannot init RX queue %u for port %u (%d)\n",
485 if (app.nic_tx_port_mask[port] == 1) {
486 app_get_lcore_for_nic_tx(port, &lcore);
487 socket = rte_lcore_to_socket_id(lcore);
488 printf("Initializing NIC port %u TX queue 0 ...\n",
490 ret = rte_eth_tx_queue_setup(
493 (uint16_t) app.nic_tx_ring_size,
497 rte_panic("Cannot init TX queue 0 for port %d (%d)\n",
504 ret = rte_eth_dev_start(port);
506 rte_panic("Cannot start port %d (%d)\n", port, ret);
510 check_all_ports_link_status(APP_MAX_NIC_PORTS, (~0x0));
516 app_assign_worker_ids();
517 app_init_mbuf_pools();
518 app_init_lpm_tables();
523 printf("Initialization completed.\n");