1d30ea906Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause
2d30ea906Sjfb8856606 * Copyright(c) 2010-2015 Intel Corporation
3a9643ea8Slogwang */
4a9643ea8Slogwang
5a9643ea8Slogwang #include <stdint.h>
6a9643ea8Slogwang #include <inttypes.h>
7a9643ea8Slogwang #include <rte_eal.h>
8a9643ea8Slogwang #include <rte_ethdev.h>
9a9643ea8Slogwang #include <rte_cycles.h>
10a9643ea8Slogwang #include <rte_lcore.h>
11a9643ea8Slogwang #include <rte_mbuf.h>
12a9643ea8Slogwang
13d30ea906Sjfb8856606 #define RX_RING_SIZE 1024
14d30ea906Sjfb8856606 #define TX_RING_SIZE 1024
15a9643ea8Slogwang
16a9643ea8Slogwang #define NUM_MBUFS 8191
17a9643ea8Slogwang #define MBUF_CACHE_SIZE 250
18a9643ea8Slogwang #define BURST_SIZE 32
19a9643ea8Slogwang
20a9643ea8Slogwang static const struct rte_eth_conf port_conf_default = {
21d30ea906Sjfb8856606 .rxmode = {
224418919fSjohnjiang .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
23d30ea906Sjfb8856606 },
24a9643ea8Slogwang };
25a9643ea8Slogwang
26a9643ea8Slogwang /* basicfwd.c: Basic DPDK skeleton forwarding example. */
27a9643ea8Slogwang
28a9643ea8Slogwang /*
29a9643ea8Slogwang * Initializes a given port using global settings and with the RX buffers
30a9643ea8Slogwang * coming from the mbuf_pool passed as a parameter.
31a9643ea8Slogwang */
32a9643ea8Slogwang static inline int
port_init(uint16_t port,struct rte_mempool * mbuf_pool)332bfe3f2eSlogwang port_init(uint16_t port, struct rte_mempool *mbuf_pool)
34a9643ea8Slogwang {
35a9643ea8Slogwang struct rte_eth_conf port_conf = port_conf_default;
36a9643ea8Slogwang const uint16_t rx_rings = 1, tx_rings = 1;
372bfe3f2eSlogwang uint16_t nb_rxd = RX_RING_SIZE;
382bfe3f2eSlogwang uint16_t nb_txd = TX_RING_SIZE;
39a9643ea8Slogwang int retval;
40a9643ea8Slogwang uint16_t q;
41d30ea906Sjfb8856606 struct rte_eth_dev_info dev_info;
42d30ea906Sjfb8856606 struct rte_eth_txconf txconf;
43a9643ea8Slogwang
44d30ea906Sjfb8856606 if (!rte_eth_dev_is_valid_port(port))
45a9643ea8Slogwang return -1;
46a9643ea8Slogwang
474418919fSjohnjiang retval = rte_eth_dev_info_get(port, &dev_info);
484418919fSjohnjiang if (retval != 0) {
494418919fSjohnjiang printf("Error during getting device (port %u) info: %s\n",
504418919fSjohnjiang port, strerror(-retval));
514418919fSjohnjiang return retval;
524418919fSjohnjiang }
534418919fSjohnjiang
54d30ea906Sjfb8856606 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
55d30ea906Sjfb8856606 port_conf.txmode.offloads |=
56d30ea906Sjfb8856606 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
57d30ea906Sjfb8856606
58a9643ea8Slogwang /* Configure the Ethernet device. */
59a9643ea8Slogwang retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
60a9643ea8Slogwang if (retval != 0)
61a9643ea8Slogwang return retval;
62a9643ea8Slogwang
632bfe3f2eSlogwang retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
642bfe3f2eSlogwang if (retval != 0)
652bfe3f2eSlogwang return retval;
662bfe3f2eSlogwang
67a9643ea8Slogwang /* Allocate and set up 1 RX queue per Ethernet port. */
68a9643ea8Slogwang for (q = 0; q < rx_rings; q++) {
692bfe3f2eSlogwang retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
70a9643ea8Slogwang rte_eth_dev_socket_id(port), NULL, mbuf_pool);
71a9643ea8Slogwang if (retval < 0)
72a9643ea8Slogwang return retval;
73a9643ea8Slogwang }
74a9643ea8Slogwang
75d30ea906Sjfb8856606 txconf = dev_info.default_txconf;
76d30ea906Sjfb8856606 txconf.offloads = port_conf.txmode.offloads;
77a9643ea8Slogwang /* Allocate and set up 1 TX queue per Ethernet port. */
78a9643ea8Slogwang for (q = 0; q < tx_rings; q++) {
792bfe3f2eSlogwang retval = rte_eth_tx_queue_setup(port, q, nb_txd,
80d30ea906Sjfb8856606 rte_eth_dev_socket_id(port), &txconf);
81a9643ea8Slogwang if (retval < 0)
82a9643ea8Slogwang return retval;
83a9643ea8Slogwang }
84a9643ea8Slogwang
85a9643ea8Slogwang /* Start the Ethernet port. */
86a9643ea8Slogwang retval = rte_eth_dev_start(port);
87a9643ea8Slogwang if (retval < 0)
88a9643ea8Slogwang return retval;
89a9643ea8Slogwang
90a9643ea8Slogwang /* Display the port MAC address. */
914418919fSjohnjiang struct rte_ether_addr addr;
924418919fSjohnjiang retval = rte_eth_macaddr_get(port, &addr);
934418919fSjohnjiang if (retval != 0)
944418919fSjohnjiang return retval;
954418919fSjohnjiang
96a9643ea8Slogwang printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
97a9643ea8Slogwang " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
982bfe3f2eSlogwang port,
99a9643ea8Slogwang addr.addr_bytes[0], addr.addr_bytes[1],
100a9643ea8Slogwang addr.addr_bytes[2], addr.addr_bytes[3],
101a9643ea8Slogwang addr.addr_bytes[4], addr.addr_bytes[5]);
102a9643ea8Slogwang
103a9643ea8Slogwang /* Enable RX in promiscuous mode for the Ethernet device. */
1044418919fSjohnjiang retval = rte_eth_promiscuous_enable(port);
1054418919fSjohnjiang if (retval != 0)
1064418919fSjohnjiang return retval;
107a9643ea8Slogwang
108a9643ea8Slogwang return 0;
109a9643ea8Slogwang }
110a9643ea8Slogwang
111a9643ea8Slogwang /*
112a9643ea8Slogwang * The lcore main. This is the main thread that does the work, reading from
113a9643ea8Slogwang * an input port and writing to an output port.
114a9643ea8Slogwang */
115*2d9fd380Sjfb8856606 static __rte_noreturn void
lcore_main(void)116a9643ea8Slogwang lcore_main(void)
117a9643ea8Slogwang {
1182bfe3f2eSlogwang uint16_t port;
119a9643ea8Slogwang
120a9643ea8Slogwang /*
121a9643ea8Slogwang * Check that the port is on the same NUMA node as the polling thread
122a9643ea8Slogwang * for best performance.
123a9643ea8Slogwang */
124d30ea906Sjfb8856606 RTE_ETH_FOREACH_DEV(port)
125a9643ea8Slogwang if (rte_eth_dev_socket_id(port) > 0 &&
126a9643ea8Slogwang rte_eth_dev_socket_id(port) !=
127a9643ea8Slogwang (int)rte_socket_id())
128a9643ea8Slogwang printf("WARNING, port %u is on remote NUMA node to "
129a9643ea8Slogwang "polling thread.\n\tPerformance will "
130a9643ea8Slogwang "not be optimal.\n", port);
131a9643ea8Slogwang
132a9643ea8Slogwang printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
133a9643ea8Slogwang rte_lcore_id());
134a9643ea8Slogwang
135a9643ea8Slogwang /* Run until the application is quit or killed. */
136a9643ea8Slogwang for (;;) {
137a9643ea8Slogwang /*
138a9643ea8Slogwang * Receive packets on a port and forward them on the paired
139a9643ea8Slogwang * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
140a9643ea8Slogwang */
141d30ea906Sjfb8856606 RTE_ETH_FOREACH_DEV(port) {
142a9643ea8Slogwang
143a9643ea8Slogwang /* Get burst of RX packets, from first port of pair. */
144a9643ea8Slogwang struct rte_mbuf *bufs[BURST_SIZE];
145a9643ea8Slogwang const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
146a9643ea8Slogwang bufs, BURST_SIZE);
147a9643ea8Slogwang
148a9643ea8Slogwang if (unlikely(nb_rx == 0))
149a9643ea8Slogwang continue;
150a9643ea8Slogwang
151a9643ea8Slogwang /* Send burst of TX packets, to second port of pair. */
152a9643ea8Slogwang const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
153a9643ea8Slogwang bufs, nb_rx);
154a9643ea8Slogwang
155a9643ea8Slogwang /* Free any unsent packets. */
156a9643ea8Slogwang if (unlikely(nb_tx < nb_rx)) {
157a9643ea8Slogwang uint16_t buf;
158a9643ea8Slogwang for (buf = nb_tx; buf < nb_rx; buf++)
159a9643ea8Slogwang rte_pktmbuf_free(bufs[buf]);
160a9643ea8Slogwang }
161a9643ea8Slogwang }
162a9643ea8Slogwang }
163a9643ea8Slogwang }
164a9643ea8Slogwang
165a9643ea8Slogwang /*
166a9643ea8Slogwang * The main function, which does initialization and calls the per-lcore
167a9643ea8Slogwang * functions.
168a9643ea8Slogwang */
169a9643ea8Slogwang int
main(int argc,char * argv[])170a9643ea8Slogwang main(int argc, char *argv[])
171a9643ea8Slogwang {
172a9643ea8Slogwang struct rte_mempool *mbuf_pool;
173a9643ea8Slogwang unsigned nb_ports;
1742bfe3f2eSlogwang uint16_t portid;
175a9643ea8Slogwang
176a9643ea8Slogwang /* Initialize the Environment Abstraction Layer (EAL). */
177a9643ea8Slogwang int ret = rte_eal_init(argc, argv);
178a9643ea8Slogwang if (ret < 0)
179a9643ea8Slogwang rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
180a9643ea8Slogwang
181a9643ea8Slogwang argc -= ret;
182a9643ea8Slogwang argv += ret;
183a9643ea8Slogwang
184a9643ea8Slogwang /* Check that there is an even number of ports to send/receive on. */
185d30ea906Sjfb8856606 nb_ports = rte_eth_dev_count_avail();
186a9643ea8Slogwang if (nb_ports < 2 || (nb_ports & 1))
187a9643ea8Slogwang rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
188a9643ea8Slogwang
189a9643ea8Slogwang /* Creates a new mempool in memory to hold the mbufs. */
190a9643ea8Slogwang mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
191a9643ea8Slogwang MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
192a9643ea8Slogwang
193a9643ea8Slogwang if (mbuf_pool == NULL)
194a9643ea8Slogwang rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
195a9643ea8Slogwang
196a9643ea8Slogwang /* Initialize all ports. */
197d30ea906Sjfb8856606 RTE_ETH_FOREACH_DEV(portid)
198a9643ea8Slogwang if (port_init(portid, mbuf_pool) != 0)
1992bfe3f2eSlogwang rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu16 "\n",
200a9643ea8Slogwang portid);
201a9643ea8Slogwang
202a9643ea8Slogwang if (rte_lcore_count() > 1)
203a9643ea8Slogwang printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
204a9643ea8Slogwang
205*2d9fd380Sjfb8856606 /* Call lcore_main on the main core only. */
206a9643ea8Slogwang lcore_main();
207a9643ea8Slogwang
208a9643ea8Slogwang return 0;
209a9643ea8Slogwang }
210