13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
23998e2a0SBruce Richardson * Copyright(c) 2010-2015 Intel Corporation
37107e471SBruce Richardson */
47107e471SBruce Richardson
57107e471SBruce Richardson #include <stdint.h>
6*72b452c5SDmitry Kozlyuk #include <stdlib.h>
77107e471SBruce Richardson #include <inttypes.h>
87107e471SBruce Richardson #include <rte_eal.h>
97107e471SBruce Richardson #include <rte_ethdev.h>
107107e471SBruce Richardson #include <rte_cycles.h>
117107e471SBruce Richardson #include <rte_lcore.h>
127107e471SBruce Richardson #include <rte_mbuf.h>
137107e471SBruce Richardson
14867a6c66SKevin Laatz #define RX_RING_SIZE 1024
15867a6c66SKevin Laatz #define TX_RING_SIZE 1024
167107e471SBruce Richardson
177107e471SBruce Richardson #define NUM_MBUFS 8191
187107e471SBruce Richardson #define MBUF_CACHE_SIZE 250
197107e471SBruce Richardson #define BURST_SIZE 32
207107e471SBruce Richardson
210f91352cSJohn McNamara /* basicfwd.c: Basic DPDK skeleton forwarding example. */
220f91352cSJohn McNamara
237107e471SBruce Richardson /*
240f91352cSJohn McNamara * Initializes a given port using global settings and with the RX buffers
250f91352cSJohn McNamara * coming from the mbuf_pool passed as a parameter.
267107e471SBruce Richardson */
279a212dc0SConor Fogarty
289a212dc0SConor Fogarty /* Main functional part of port initialization. 8< */
297107e471SBruce Richardson static inline int
port_init(uint16_t port,struct rte_mempool * mbuf_pool)30f8244c63SZhiyong Yang port_init(uint16_t port, struct rte_mempool *mbuf_pool)
317107e471SBruce Richardson {
321bb4a528SFerruh Yigit struct rte_eth_conf port_conf;
337107e471SBruce Richardson const uint16_t rx_rings = 1, tx_rings = 1;
3460efb44fSRoman Zhukov uint16_t nb_rxd = RX_RING_SIZE;
3560efb44fSRoman Zhukov uint16_t nb_txd = TX_RING_SIZE;
367107e471SBruce Richardson int retval;
377107e471SBruce Richardson uint16_t q;
385d98dd08SShahaf Shuler struct rte_eth_dev_info dev_info;
395d98dd08SShahaf Shuler struct rte_eth_txconf txconf;
407107e471SBruce Richardson
41a9dbe180SThomas Monjalon if (!rte_eth_dev_is_valid_port(port))
427107e471SBruce Richardson return -1;
437107e471SBruce Richardson
441bb4a528SFerruh Yigit memset(&port_conf, 0, sizeof(struct rte_eth_conf));
451bb4a528SFerruh Yigit
46089e5ed7SIvan Ilchenko retval = rte_eth_dev_info_get(port, &dev_info);
47089e5ed7SIvan Ilchenko if (retval != 0) {
48089e5ed7SIvan Ilchenko printf("Error during getting device (port %u) info: %s\n",
49089e5ed7SIvan Ilchenko port, strerror(-retval));
50089e5ed7SIvan Ilchenko return retval;
51089e5ed7SIvan Ilchenko }
52089e5ed7SIvan Ilchenko
53295968d1SFerruh Yigit if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
545d98dd08SShahaf Shuler port_conf.txmode.offloads |=
55295968d1SFerruh Yigit RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
565d98dd08SShahaf Shuler
570f91352cSJohn McNamara /* Configure the Ethernet device. */
587107e471SBruce Richardson retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
597107e471SBruce Richardson if (retval != 0)
607107e471SBruce Richardson return retval;
617107e471SBruce Richardson
6260efb44fSRoman Zhukov retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
6360efb44fSRoman Zhukov if (retval != 0)
6460efb44fSRoman Zhukov return retval;
6560efb44fSRoman Zhukov
660f91352cSJohn McNamara /* Allocate and set up 1 RX queue per Ethernet port. */
677107e471SBruce Richardson for (q = 0; q < rx_rings; q++) {
6860efb44fSRoman Zhukov retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
697107e471SBruce Richardson rte_eth_dev_socket_id(port), NULL, mbuf_pool);
707107e471SBruce Richardson if (retval < 0)
717107e471SBruce Richardson return retval;
727107e471SBruce Richardson }
737107e471SBruce Richardson
745d98dd08SShahaf Shuler txconf = dev_info.default_txconf;
755d98dd08SShahaf Shuler txconf.offloads = port_conf.txmode.offloads;
760f91352cSJohn McNamara /* Allocate and set up 1 TX queue per Ethernet port. */
777107e471SBruce Richardson for (q = 0; q < tx_rings; q++) {
7860efb44fSRoman Zhukov retval = rte_eth_tx_queue_setup(port, q, nb_txd,
795d98dd08SShahaf Shuler rte_eth_dev_socket_id(port), &txconf);
807107e471SBruce Richardson if (retval < 0)
817107e471SBruce Richardson return retval;
827107e471SBruce Richardson }
837107e471SBruce Richardson
849a212dc0SConor Fogarty /* Starting Ethernet port. 8< */
857107e471SBruce Richardson retval = rte_eth_dev_start(port);
869a212dc0SConor Fogarty /* >8 End of starting of ethernet port. */
877107e471SBruce Richardson if (retval < 0)
887107e471SBruce Richardson return retval;
897107e471SBruce Richardson
900f91352cSJohn McNamara /* Display the port MAC address. */
916d13ea8eSOlivier Matz struct rte_ether_addr addr;
9270febdcfSIgor Romanov retval = rte_eth_macaddr_get(port, &addr);
9370febdcfSIgor Romanov if (retval != 0)
9470febdcfSIgor Romanov return retval;
9570febdcfSIgor Romanov
967107e471SBruce Richardson printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
977107e471SBruce Richardson " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
98a7db3afcSAman Deep Singh port, RTE_ETHER_ADDR_BYTES(&addr));
997107e471SBruce Richardson
1000f91352cSJohn McNamara /* Enable RX in promiscuous mode for the Ethernet device. */
101f430bbceSIvan Ilchenko retval = rte_eth_promiscuous_enable(port);
1029a212dc0SConor Fogarty /* End of setting RX port in promiscuous mode. */
103f430bbceSIvan Ilchenko if (retval != 0)
104f430bbceSIvan Ilchenko return retval;
1057107e471SBruce Richardson
1067107e471SBruce Richardson return 0;
1077107e471SBruce Richardson }
1089a212dc0SConor Fogarty /* >8 End of main functional part of port initialization. */
1097107e471SBruce Richardson
1107107e471SBruce Richardson /*
1110f91352cSJohn McNamara * The lcore main. This is the main thread that does the work, reading from
1120f91352cSJohn McNamara * an input port and writing to an output port.
1137107e471SBruce Richardson */
1149a212dc0SConor Fogarty
1159a212dc0SConor Fogarty /* Basic forwarding application lcore. 8< */
116ddcd7640SThomas Monjalon static __rte_noreturn void
lcore_main(void)1177107e471SBruce Richardson lcore_main(void)
1187107e471SBruce Richardson {
119f8244c63SZhiyong Yang uint16_t port;
1200f91352cSJohn McNamara
1210f91352cSJohn McNamara /*
1220f91352cSJohn McNamara * Check that the port is on the same NUMA node as the polling thread
1230f91352cSJohn McNamara * for best performance.
1240f91352cSJohn McNamara */
1258728ccf3SThomas Monjalon RTE_ETH_FOREACH_DEV(port)
1265ffa60cdSMin Hu (Connor) if (rte_eth_dev_socket_id(port) >= 0 &&
1277107e471SBruce Richardson rte_eth_dev_socket_id(port) !=
1287107e471SBruce Richardson (int)rte_socket_id())
1297107e471SBruce Richardson printf("WARNING, port %u is on remote NUMA node to "
1307107e471SBruce Richardson "polling thread.\n\tPerformance will "
1317107e471SBruce Richardson "not be optimal.\n", port);
1327107e471SBruce Richardson
1337107e471SBruce Richardson printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
1347107e471SBruce Richardson rte_lcore_id());
1350f91352cSJohn McNamara
1369a212dc0SConor Fogarty /* Main work of application loop. 8< */
1377107e471SBruce Richardson for (;;) {
1380f91352cSJohn McNamara /*
1390f91352cSJohn McNamara * Receive packets on a port and forward them on the paired
1400f91352cSJohn McNamara * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
1410f91352cSJohn McNamara */
1428728ccf3SThomas Monjalon RTE_ETH_FOREACH_DEV(port) {
1430f91352cSJohn McNamara
1440f91352cSJohn McNamara /* Get burst of RX packets, from first port of pair. */
1457107e471SBruce Richardson struct rte_mbuf *bufs[BURST_SIZE];
1467107e471SBruce Richardson const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
1477107e471SBruce Richardson bufs, BURST_SIZE);
1480f91352cSJohn McNamara
1497107e471SBruce Richardson if (unlikely(nb_rx == 0))
1507107e471SBruce Richardson continue;
1510f91352cSJohn McNamara
1520f91352cSJohn McNamara /* Send burst of TX packets, to second port of pair. */
1537107e471SBruce Richardson const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
1547107e471SBruce Richardson bufs, nb_rx);
1550f91352cSJohn McNamara
1560f91352cSJohn McNamara /* Free any unsent packets. */
1577107e471SBruce Richardson if (unlikely(nb_tx < nb_rx)) {
1587107e471SBruce Richardson uint16_t buf;
1597107e471SBruce Richardson for (buf = nb_tx; buf < nb_rx; buf++)
1607107e471SBruce Richardson rte_pktmbuf_free(bufs[buf]);
1617107e471SBruce Richardson }
1627107e471SBruce Richardson }
1637107e471SBruce Richardson }
1649a212dc0SConor Fogarty /* >8 End of loop. */
1657107e471SBruce Richardson }
1669a212dc0SConor Fogarty /* >8 End Basic forwarding application lcore. */
1677107e471SBruce Richardson
1680f91352cSJohn McNamara /*
1690f91352cSJohn McNamara * The main function, which does initialization and calls the per-lcore
1700f91352cSJohn McNamara * functions.
1710f91352cSJohn McNamara */
1727107e471SBruce Richardson int
main(int argc,char * argv[])17398a16481SDavid Marchand main(int argc, char *argv[])
1747107e471SBruce Richardson {
1757107e471SBruce Richardson struct rte_mempool *mbuf_pool;
1767107e471SBruce Richardson unsigned nb_ports;
177f8244c63SZhiyong Yang uint16_t portid;
1787107e471SBruce Richardson
1799a212dc0SConor Fogarty /* Initializion the Environment Abstraction Layer (EAL). 8< */
1807107e471SBruce Richardson int ret = rte_eal_init(argc, argv);
1817107e471SBruce Richardson if (ret < 0)
1827107e471SBruce Richardson rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
1837be78d02SJosh Soref /* >8 End of initialization the Environment Abstraction Layer (EAL). */
1840f91352cSJohn McNamara
1857107e471SBruce Richardson argc -= ret;
1867107e471SBruce Richardson argv += ret;
1877107e471SBruce Richardson
1880f91352cSJohn McNamara /* Check that there is an even number of ports to send/receive on. */
189d9a42a69SThomas Monjalon nb_ports = rte_eth_dev_count_avail();
1907107e471SBruce Richardson if (nb_ports < 2 || (nb_ports & 1))
1917107e471SBruce Richardson rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
1927107e471SBruce Richardson
1930f91352cSJohn McNamara /* Creates a new mempool in memory to hold the mbufs. */
1949a212dc0SConor Fogarty
1959a212dc0SConor Fogarty /* Allocates mempool to hold the mbufs. 8< */
196ea0c20eaSOlivier Matz mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
197824cb29cSKonstantin Ananyev MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1989a212dc0SConor Fogarty /* >8 End of allocating mempool to hold mbuf. */
1990f91352cSJohn McNamara
2007107e471SBruce Richardson if (mbuf_pool == NULL)
2017107e471SBruce Richardson rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2027107e471SBruce Richardson
2039a212dc0SConor Fogarty /* Initializing all ports. 8< */
2048728ccf3SThomas Monjalon RTE_ETH_FOREACH_DEV(portid)
2057107e471SBruce Richardson if (port_init(portid, mbuf_pool) != 0)
206f8244c63SZhiyong Yang rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu16 "\n",
2077107e471SBruce Richardson portid);
2089a212dc0SConor Fogarty /* >8 End of initializing all ports. */
2097107e471SBruce Richardson
2107107e471SBruce Richardson if (rte_lcore_count() > 1)
2110f91352cSJohn McNamara printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
2127107e471SBruce Richardson
2139a212dc0SConor Fogarty /* Call lcore_main on the main core only. Called on single lcore. 8< */
2147107e471SBruce Richardson lcore_main();
2159a212dc0SConor Fogarty /* >8 End of called on single lcore. */
2160f91352cSJohn McNamara
21710aa3757SChengchang Tang /* clean up the EAL */
21810aa3757SChengchang Tang rte_eal_cleanup();
21910aa3757SChengchang Tang
2207107e471SBruce Richardson return 0;
2217107e471SBruce Richardson }
222