1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
8 #include <rte_ethdev.h>
9 #include <rte_cycles.h>
10 #include <rte_lcore.h>
15 #define RX_RING_SIZE 128
16 #define TX_RING_SIZE 512
18 #define NUM_MBUFS 8191
19 #define MBUF_CACHE_SIZE 250
22 static const struct rte_eth_conf port_conf_default = {
23 .rxmode = { .max_rx_pkt_len = RTE_ETHER_MAX_LEN }
26 /* l2fwd-cat.c: CAT enabled, 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;
39 uint16_t nb_rxd = RX_RING_SIZE;
40 uint16_t nb_txd = TX_RING_SIZE;
42 if (!rte_eth_dev_is_valid_port(port))
45 /* Configure the Ethernet device. */
46 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
50 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
54 /* Allocate and set up 1 RX queue per Ethernet port. */
55 for (q = 0; q < rx_rings; q++) {
56 retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
57 rte_eth_dev_socket_id(port), NULL, mbuf_pool);
62 /* Allocate and set up 1 TX queue per Ethernet port. */
63 for (q = 0; q < tx_rings; q++) {
64 retval = rte_eth_tx_queue_setup(port, q, nb_txd,
65 rte_eth_dev_socket_id(port), NULL);
70 /* Start the Ethernet port. */
71 retval = rte_eth_dev_start(port);
75 /* Display the port MAC address. */
76 struct rte_ether_addr addr;
77 retval = rte_eth_macaddr_get(port, &addr);
81 printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
82 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
84 addr.addr_bytes[0], addr.addr_bytes[1],
85 addr.addr_bytes[2], addr.addr_bytes[3],
86 addr.addr_bytes[4], addr.addr_bytes[5]);
88 /* Enable RX in promiscuous mode for the Ethernet device. */
89 retval = rte_eth_promiscuous_enable(port);
97 * The lcore main. This is the main thread that does the work, reading from
98 * an input port and writing to an output port.
100 static __attribute__((noreturn)) void
106 * Check that the port is on the same NUMA node as the polling thread
107 * for best performance.
109 RTE_ETH_FOREACH_DEV(port)
110 if (rte_eth_dev_socket_id(port) > 0 &&
111 rte_eth_dev_socket_id(port) !=
112 (int)rte_socket_id())
113 printf("WARNING, port %u is on remote NUMA node to "
114 "polling thread.\n\tPerformance will "
115 "not be optimal.\n", port);
117 printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
120 /* Run until the application is quit or killed. */
123 * Receive packets on a port and forward them on the paired
124 * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
126 RTE_ETH_FOREACH_DEV(port) {
128 /* Get burst of RX packets, from first port of pair. */
129 struct rte_mbuf *bufs[BURST_SIZE];
130 const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
133 if (unlikely(nb_rx == 0))
136 /* Send burst of TX packets, to second port of pair. */
137 const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
140 /* Free any unsent packets. */
141 if (unlikely(nb_tx < nb_rx)) {
143 for (buf = nb_tx; buf < nb_rx; buf++)
144 rte_pktmbuf_free(bufs[buf]);
151 * The main function, which does initialization and calls the per-lcore
155 main(int argc, char *argv[])
157 struct rte_mempool *mbuf_pool;
161 /* Initialize the Environment Abstraction Layer (EAL). */
162 int ret = rte_eal_init(argc, argv);
164 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
170 * Initialize the PQoS library and configure CAT.
171 * Please see l2fwd-cat documentation for more info.
173 ret = cat_init(argc, argv);
175 rte_exit(EXIT_FAILURE, "PQOS: L3CA init failed!\n");
180 /* Check that there is an even number of ports to send/receive on. */
181 nb_ports = rte_eth_dev_count_avail();
182 if (nb_ports < 2 || (nb_ports & 1))
183 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
185 /* Creates a new mempool in memory to hold the mbufs. */
186 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
187 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
189 if (mbuf_pool == NULL)
190 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
192 /* Initialize all ports. */
193 RTE_ETH_FOREACH_DEV(portid)
194 if (port_init(portid, mbuf_pool) != 0)
195 rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu16 "\n",
198 if (rte_lcore_count() > 1)
199 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
201 /* Call lcore_main on the master core only. */