1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
8 #include <rte_ethdev.h>
9 #include <rte_cycles.h>
10 #include <rte_lcore.h>
13 #define RX_RING_SIZE 1024
14 #define TX_RING_SIZE 1024
16 #define NUM_MBUFS 8191
17 #define MBUF_CACHE_SIZE 250
20 static const struct rte_eth_conf port_conf_default
= {
22 .max_rx_pkt_len
= ETHER_MAX_LEN
,
26 /* basicfwd.c: Basic DPDK skeleton forwarding example. */
29 * Initializes a given port using global settings and with the RX buffers
30 * coming from the mbuf_pool passed as a parameter.
33 port_init(uint16_t port
, struct rte_mempool
*mbuf_pool
)
35 struct rte_eth_conf port_conf
= port_conf_default
;
36 const uint16_t rx_rings
= 1, tx_rings
= 1;
37 uint16_t nb_rxd
= RX_RING_SIZE
;
38 uint16_t nb_txd
= TX_RING_SIZE
;
41 struct rte_eth_dev_info dev_info
;
42 struct rte_eth_txconf txconf
;
44 if (!rte_eth_dev_is_valid_port(port
))
47 rte_eth_dev_info_get(port
, &dev_info
);
48 if (dev_info
.tx_offload_capa
& DEV_TX_OFFLOAD_MBUF_FAST_FREE
)
49 port_conf
.txmode
.offloads
|=
50 DEV_TX_OFFLOAD_MBUF_FAST_FREE
;
52 /* Configure the Ethernet device. */
53 retval
= rte_eth_dev_configure(port
, rx_rings
, tx_rings
, &port_conf
);
57 retval
= rte_eth_dev_adjust_nb_rx_tx_desc(port
, &nb_rxd
, &nb_txd
);
61 /* Allocate and set up 1 RX queue per Ethernet port. */
62 for (q
= 0; q
< rx_rings
; q
++) {
63 retval
= rte_eth_rx_queue_setup(port
, q
, nb_rxd
,
64 rte_eth_dev_socket_id(port
), NULL
, mbuf_pool
);
69 txconf
= dev_info
.default_txconf
;
70 txconf
.offloads
= port_conf
.txmode
.offloads
;
71 /* Allocate and set up 1 TX queue per Ethernet port. */
72 for (q
= 0; q
< tx_rings
; q
++) {
73 retval
= rte_eth_tx_queue_setup(port
, q
, nb_txd
,
74 rte_eth_dev_socket_id(port
), &txconf
);
79 /* Start the Ethernet port. */
80 retval
= rte_eth_dev_start(port
);
84 /* Display the port MAC address. */
85 struct ether_addr addr
;
86 rte_eth_macaddr_get(port
, &addr
);
87 printf("Port %u MAC: %02" PRIx8
" %02" PRIx8
" %02" PRIx8
88 " %02" PRIx8
" %02" PRIx8
" %02" PRIx8
"\n",
90 addr
.addr_bytes
[0], addr
.addr_bytes
[1],
91 addr
.addr_bytes
[2], addr
.addr_bytes
[3],
92 addr
.addr_bytes
[4], addr
.addr_bytes
[5]);
94 /* Enable RX in promiscuous mode for the Ethernet device. */
95 rte_eth_promiscuous_enable(port
);
101 * The lcore main. This is the main thread that does the work, reading from
102 * an input port and writing to an output port.
104 static __attribute__((noreturn
)) void
110 * Check that the port is on the same NUMA node as the polling thread
111 * for best performance.
113 RTE_ETH_FOREACH_DEV(port
)
114 if (rte_eth_dev_socket_id(port
) > 0 &&
115 rte_eth_dev_socket_id(port
) !=
116 (int)rte_socket_id())
117 printf("WARNING, port %u is on remote NUMA node to "
118 "polling thread.\n\tPerformance will "
119 "not be optimal.\n", port
);
121 printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
124 /* Run until the application is quit or killed. */
127 * Receive packets on a port and forward them on the paired
128 * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
130 RTE_ETH_FOREACH_DEV(port
) {
132 /* Get burst of RX packets, from first port of pair. */
133 struct rte_mbuf
*bufs
[BURST_SIZE
];
134 const uint16_t nb_rx
= rte_eth_rx_burst(port
, 0,
137 if (unlikely(nb_rx
== 0))
140 /* Send burst of TX packets, to second port of pair. */
141 const uint16_t nb_tx
= rte_eth_tx_burst(port
^ 1, 0,
144 /* Free any unsent packets. */
145 if (unlikely(nb_tx
< nb_rx
)) {
147 for (buf
= nb_tx
; buf
< nb_rx
; buf
++)
148 rte_pktmbuf_free(bufs
[buf
]);
155 * The main function, which does initialization and calls the per-lcore
159 main(int argc
, char *argv
[])
161 struct rte_mempool
*mbuf_pool
;
165 /* Initialize the Environment Abstraction Layer (EAL). */
166 int ret
= rte_eal_init(argc
, argv
);
168 rte_exit(EXIT_FAILURE
, "Error with EAL initialization\n");
173 /* Check that there is an even number of ports to send/receive on. */
174 nb_ports
= rte_eth_dev_count_avail();
175 if (nb_ports
< 2 || (nb_ports
& 1))
176 rte_exit(EXIT_FAILURE
, "Error: number of ports must be even\n");
178 /* Creates a new mempool in memory to hold the mbufs. */
179 mbuf_pool
= rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS
* nb_ports
,
180 MBUF_CACHE_SIZE
, 0, RTE_MBUF_DEFAULT_BUF_SIZE
, rte_socket_id());
182 if (mbuf_pool
== NULL
)
183 rte_exit(EXIT_FAILURE
, "Cannot create mbuf pool\n");
185 /* Initialize all ports. */
186 RTE_ETH_FOREACH_DEV(portid
)
187 if (port_init(portid
, mbuf_pool
) != 0)
188 rte_exit(EXIT_FAILURE
, "Cannot init port %"PRIu16
"\n",
191 if (rte_lcore_count() > 1)
192 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
194 /* Call lcore_main on the master core only. */