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>
67107e471SBruce Richardson #include <inttypes.h>
77107e471SBruce Richardson #include <rte_eal.h>
87107e471SBruce Richardson #include <rte_ethdev.h>
97107e471SBruce Richardson #include <rte_cycles.h>
107107e471SBruce Richardson #include <rte_lcore.h>
117107e471SBruce Richardson #include <rte_mbuf.h>
127107e471SBruce Richardson
13867a6c66SKevin Laatz #define RX_RING_SIZE 1024
14867a6c66SKevin Laatz #define TX_RING_SIZE 1024
157107e471SBruce Richardson
167107e471SBruce Richardson #define NUM_MBUFS 8191
177107e471SBruce Richardson #define MBUF_CACHE_SIZE 250
187107e471SBruce Richardson #define BURST_SIZE 32
197107e471SBruce Richardson
200f91352cSJohn McNamara /* basicfwd.c: Basic DPDK skeleton forwarding example. */
210f91352cSJohn McNamara
227107e471SBruce Richardson /*
230f91352cSJohn McNamara * Initializes a given port using global settings and with the RX buffers
240f91352cSJohn McNamara * coming from the mbuf_pool passed as a parameter.
257107e471SBruce Richardson */
269a212dc0SConor Fogarty
279a212dc0SConor Fogarty /* Main functional part of port initialization. 8< */
287107e471SBruce Richardson static inline int
port_init(uint16_t port,struct rte_mempool * mbuf_pool)29f8244c63SZhiyong Yang port_init(uint16_t port, struct rte_mempool *mbuf_pool)
307107e471SBruce Richardson {
311bb4a528SFerruh Yigit struct rte_eth_conf port_conf;
327107e471SBruce Richardson const uint16_t rx_rings = 1, tx_rings = 1;
3360efb44fSRoman Zhukov uint16_t nb_rxd = RX_RING_SIZE;
3460efb44fSRoman Zhukov uint16_t nb_txd = TX_RING_SIZE;
357107e471SBruce Richardson int retval;
367107e471SBruce Richardson uint16_t q;
375d98dd08SShahaf Shuler struct rte_eth_dev_info dev_info;
385d98dd08SShahaf Shuler struct rte_eth_txconf txconf;
397107e471SBruce Richardson
40a9dbe180SThomas Monjalon if (!rte_eth_dev_is_valid_port(port))
417107e471SBruce Richardson return -1;
427107e471SBruce Richardson
431bb4a528SFerruh Yigit memset(&port_conf, 0, sizeof(struct rte_eth_conf));
441bb4a528SFerruh Yigit
45089e5ed7SIvan Ilchenko retval = rte_eth_dev_info_get(port, &dev_info);
46089e5ed7SIvan Ilchenko if (retval != 0) {
47089e5ed7SIvan Ilchenko printf("Error during getting device (port %u) info: %s\n",
48089e5ed7SIvan Ilchenko port, strerror(-retval));
49089e5ed7SIvan Ilchenko return retval;
50089e5ed7SIvan Ilchenko }
51089e5ed7SIvan Ilchenko
52295968d1SFerruh Yigit if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
535d98dd08SShahaf Shuler port_conf.txmode.offloads |=
54295968d1SFerruh Yigit RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
555d98dd08SShahaf Shuler
560f91352cSJohn McNamara /* Configure the Ethernet device. */
577107e471SBruce Richardson retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
587107e471SBruce Richardson if (retval != 0)
597107e471SBruce Richardson return retval;
607107e471SBruce Richardson
6160efb44fSRoman Zhukov retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
6260efb44fSRoman Zhukov if (retval != 0)
6360efb44fSRoman Zhukov return retval;
6460efb44fSRoman Zhukov
650f91352cSJohn McNamara /* Allocate and set up 1 RX queue per Ethernet port. */
667107e471SBruce Richardson for (q = 0; q < rx_rings; q++) {
6760efb44fSRoman Zhukov retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
687107e471SBruce Richardson rte_eth_dev_socket_id(port), NULL, mbuf_pool);
697107e471SBruce Richardson if (retval < 0)
707107e471SBruce Richardson return retval;
717107e471SBruce Richardson }
727107e471SBruce Richardson
735d98dd08SShahaf Shuler txconf = dev_info.default_txconf;
745d98dd08SShahaf Shuler txconf.offloads = port_conf.txmode.offloads;
750f91352cSJohn McNamara /* Allocate and set up 1 TX queue per Ethernet port. */
767107e471SBruce Richardson for (q = 0; q < tx_rings; q++) {
7760efb44fSRoman Zhukov retval = rte_eth_tx_queue_setup(port, q, nb_txd,
785d98dd08SShahaf Shuler rte_eth_dev_socket_id(port), &txconf);
797107e471SBruce Richardson if (retval < 0)
807107e471SBruce Richardson return retval;
817107e471SBruce Richardson }
827107e471SBruce Richardson
839a212dc0SConor Fogarty /* Starting Ethernet port. 8< */
847107e471SBruce Richardson retval = rte_eth_dev_start(port);
859a212dc0SConor Fogarty /* >8 End of starting of ethernet port. */
867107e471SBruce Richardson if (retval < 0)
877107e471SBruce Richardson return retval;
887107e471SBruce Richardson
890f91352cSJohn McNamara /* Display the port MAC address. */
906d13ea8eSOlivier Matz struct rte_ether_addr addr;
9170febdcfSIgor Romanov retval = rte_eth_macaddr_get(port, &addr);
9270febdcfSIgor Romanov if (retval != 0)
9370febdcfSIgor Romanov return retval;
9470febdcfSIgor Romanov
957107e471SBruce Richardson printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
967107e471SBruce Richardson " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
97a7db3afcSAman Deep Singh port, RTE_ETHER_ADDR_BYTES(&addr));
987107e471SBruce Richardson
990f91352cSJohn McNamara /* Enable RX in promiscuous mode for the Ethernet device. */
100f430bbceSIvan Ilchenko retval = rte_eth_promiscuous_enable(port);
1019a212dc0SConor Fogarty /* End of setting RX port in promiscuous mode. */
102f430bbceSIvan Ilchenko if (retval != 0)
103f430bbceSIvan Ilchenko return retval;
1047107e471SBruce Richardson
1057107e471SBruce Richardson return 0;
1067107e471SBruce Richardson }
1079a212dc0SConor Fogarty /* >8 End of main functional part of port initialization. */
1087107e471SBruce Richardson
1097107e471SBruce Richardson /*
1100f91352cSJohn McNamara * The lcore main. This is the main thread that does the work, reading from
1110f91352cSJohn McNamara * an input port and writing to an output port.
1127107e471SBruce Richardson */
1139a212dc0SConor Fogarty
1149a212dc0SConor Fogarty /* Basic forwarding application lcore. 8< */
115ddcd7640SThomas Monjalon static __rte_noreturn void
lcore_main(void)1167107e471SBruce Richardson lcore_main(void)
1177107e471SBruce Richardson {
118f8244c63SZhiyong Yang uint16_t port;
1190f91352cSJohn McNamara
1200f91352cSJohn McNamara /*
1210f91352cSJohn McNamara * Check that the port is on the same NUMA node as the polling thread
1220f91352cSJohn McNamara * for best performance.
1230f91352cSJohn McNamara */
1248728ccf3SThomas Monjalon RTE_ETH_FOREACH_DEV(port)
1255ffa60cdSMin Hu (Connor) if (rte_eth_dev_socket_id(port) >= 0 &&
1267107e471SBruce Richardson rte_eth_dev_socket_id(port) !=
1277107e471SBruce Richardson (int)rte_socket_id())
1287107e471SBruce Richardson printf("WARNING, port %u is on remote NUMA node to "
1297107e471SBruce Richardson "polling thread.\n\tPerformance will "
1307107e471SBruce Richardson "not be optimal.\n", port);
1317107e471SBruce Richardson
1327107e471SBruce Richardson printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
1337107e471SBruce Richardson rte_lcore_id());
1340f91352cSJohn McNamara
1359a212dc0SConor Fogarty /* Main work of application loop. 8< */
1367107e471SBruce Richardson for (;;) {
1370f91352cSJohn McNamara /*
1380f91352cSJohn McNamara * Receive packets on a port and forward them on the paired
1390f91352cSJohn McNamara * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
1400f91352cSJohn McNamara */
1418728ccf3SThomas Monjalon RTE_ETH_FOREACH_DEV(port) {
1420f91352cSJohn McNamara
1430f91352cSJohn McNamara /* Get burst of RX packets, from first port of pair. */
1447107e471SBruce Richardson struct rte_mbuf *bufs[BURST_SIZE];
1457107e471SBruce Richardson const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
1467107e471SBruce Richardson bufs, BURST_SIZE);
1470f91352cSJohn McNamara
1487107e471SBruce Richardson if (unlikely(nb_rx == 0))
1497107e471SBruce Richardson continue;
1500f91352cSJohn McNamara
1510f91352cSJohn McNamara /* Send burst of TX packets, to second port of pair. */
1527107e471SBruce Richardson const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
1537107e471SBruce Richardson bufs, nb_rx);
1540f91352cSJohn McNamara
1550f91352cSJohn McNamara /* Free any unsent packets. */
1567107e471SBruce Richardson if (unlikely(nb_tx < nb_rx)) {
1577107e471SBruce Richardson uint16_t buf;
1587107e471SBruce Richardson for (buf = nb_tx; buf < nb_rx; buf++)
1597107e471SBruce Richardson rte_pktmbuf_free(bufs[buf]);
1607107e471SBruce Richardson }
1617107e471SBruce Richardson }
1627107e471SBruce Richardson }
1639a212dc0SConor Fogarty /* >8 End of loop. */
1647107e471SBruce Richardson }
1659a212dc0SConor Fogarty /* >8 End Basic forwarding application lcore. */
1667107e471SBruce Richardson
1670f91352cSJohn McNamara /*
1680f91352cSJohn McNamara * The main function, which does initialization and calls the per-lcore
1690f91352cSJohn McNamara * functions.
1700f91352cSJohn McNamara */
1717107e471SBruce Richardson int
main(int argc,char * argv[])17298a16481SDavid Marchand main(int argc, char *argv[])
1737107e471SBruce Richardson {
1747107e471SBruce Richardson struct rte_mempool *mbuf_pool;
1757107e471SBruce Richardson unsigned nb_ports;
176f8244c63SZhiyong Yang uint16_t portid;
1777107e471SBruce Richardson
1789a212dc0SConor Fogarty /* Initializion the Environment Abstraction Layer (EAL). 8< */
1797107e471SBruce Richardson int ret = rte_eal_init(argc, argv);
1807107e471SBruce Richardson if (ret < 0)
1817107e471SBruce Richardson rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
182*7be78d02SJosh Soref /* >8 End of initialization the Environment Abstraction Layer (EAL). */
1830f91352cSJohn McNamara
1847107e471SBruce Richardson argc -= ret;
1857107e471SBruce Richardson argv += ret;
1867107e471SBruce Richardson
1870f91352cSJohn McNamara /* Check that there is an even number of ports to send/receive on. */
188d9a42a69SThomas Monjalon nb_ports = rte_eth_dev_count_avail();
1897107e471SBruce Richardson if (nb_ports < 2 || (nb_ports & 1))
1907107e471SBruce Richardson rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
1917107e471SBruce Richardson
1920f91352cSJohn McNamara /* Creates a new mempool in memory to hold the mbufs. */
1939a212dc0SConor Fogarty
1949a212dc0SConor Fogarty /* Allocates mempool to hold the mbufs. 8< */
195ea0c20eaSOlivier Matz mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
196824cb29cSKonstantin Ananyev MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1979a212dc0SConor Fogarty /* >8 End of allocating mempool to hold mbuf. */
1980f91352cSJohn McNamara
1997107e471SBruce Richardson if (mbuf_pool == NULL)
2007107e471SBruce Richardson rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2017107e471SBruce Richardson
2029a212dc0SConor Fogarty /* Initializing all ports. 8< */
2038728ccf3SThomas Monjalon RTE_ETH_FOREACH_DEV(portid)
2047107e471SBruce Richardson if (port_init(portid, mbuf_pool) != 0)
205f8244c63SZhiyong Yang rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu16 "\n",
2067107e471SBruce Richardson portid);
2079a212dc0SConor Fogarty /* >8 End of initializing all ports. */
2087107e471SBruce Richardson
2097107e471SBruce Richardson if (rte_lcore_count() > 1)
2100f91352cSJohn McNamara printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
2117107e471SBruce Richardson
2129a212dc0SConor Fogarty /* Call lcore_main on the main core only. Called on single lcore. 8< */
2137107e471SBruce Richardson lcore_main();
2149a212dc0SConor Fogarty /* >8 End of called on single lcore. */
2150f91352cSJohn McNamara
21610aa3757SChengchang Tang /* clean up the EAL */
21710aa3757SChengchang Tang rte_eal_cleanup();
21810aa3757SChengchang Tang
2197107e471SBruce Richardson return 0;
2207107e471SBruce Richardson }
221