13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
23998e2a0SBruce Richardson * Copyright(c) 2016 Intel Corporation
3f6baccbcSWojciech Andralojc */
4f6baccbcSWojciech Andralojc
5f6baccbcSWojciech Andralojc #include <stdint.h>
6f6baccbcSWojciech Andralojc #include <inttypes.h>
7f6baccbcSWojciech Andralojc #include <rte_eal.h>
8f6baccbcSWojciech Andralojc #include <rte_ethdev.h>
9f6baccbcSWojciech Andralojc #include <rte_cycles.h>
10f6baccbcSWojciech Andralojc #include <rte_lcore.h>
11f6baccbcSWojciech Andralojc #include <rte_mbuf.h>
12f6baccbcSWojciech Andralojc
13f6baccbcSWojciech Andralojc #include "cat.h"
14f6baccbcSWojciech Andralojc
15f6baccbcSWojciech Andralojc #define RX_RING_SIZE 128
16f6baccbcSWojciech Andralojc #define TX_RING_SIZE 512
17f6baccbcSWojciech Andralojc
18f6baccbcSWojciech Andralojc #define NUM_MBUFS 8191
19f6baccbcSWojciech Andralojc #define MBUF_CACHE_SIZE 250
20f6baccbcSWojciech Andralojc #define BURST_SIZE 32
21f6baccbcSWojciech Andralojc
22f6baccbcSWojciech Andralojc /* l2fwd-cat.c: CAT enabled, basic DPDK skeleton forwarding example. */
23f6baccbcSWojciech Andralojc
24f6baccbcSWojciech Andralojc /*
25f6baccbcSWojciech Andralojc * Initializes a given port using global settings and with the RX buffers
26f6baccbcSWojciech Andralojc * coming from the mbuf_pool passed as a parameter.
27f6baccbcSWojciech Andralojc */
28f6baccbcSWojciech Andralojc 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)
30f6baccbcSWojciech Andralojc {
311bb4a528SFerruh Yigit struct rte_eth_conf port_conf;
32f6baccbcSWojciech Andralojc const uint16_t rx_rings = 1, tx_rings = 1;
33f6baccbcSWojciech Andralojc int retval;
34f6baccbcSWojciech Andralojc uint16_t q;
3560efb44fSRoman Zhukov uint16_t nb_rxd = RX_RING_SIZE;
3660efb44fSRoman Zhukov uint16_t nb_txd = TX_RING_SIZE;
37f6baccbcSWojciech Andralojc
38a9dbe180SThomas Monjalon if (!rte_eth_dev_is_valid_port(port))
39f6baccbcSWojciech Andralojc return -1;
40f6baccbcSWojciech Andralojc
411bb4a528SFerruh Yigit memset(&port_conf, 0, sizeof(struct rte_eth_conf));
421bb4a528SFerruh Yigit
43f6baccbcSWojciech Andralojc /* Configure the Ethernet device. */
44f6baccbcSWojciech Andralojc retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
45f6baccbcSWojciech Andralojc if (retval != 0)
46f6baccbcSWojciech Andralojc return retval;
47f6baccbcSWojciech Andralojc
4860efb44fSRoman Zhukov retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
4960efb44fSRoman Zhukov if (retval != 0)
5060efb44fSRoman Zhukov return retval;
5160efb44fSRoman Zhukov
52f6baccbcSWojciech Andralojc /* Allocate and set up 1 RX queue per Ethernet port. */
53f6baccbcSWojciech Andralojc for (q = 0; q < rx_rings; q++) {
5460efb44fSRoman Zhukov retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
55f6baccbcSWojciech Andralojc rte_eth_dev_socket_id(port), NULL, mbuf_pool);
56f6baccbcSWojciech Andralojc if (retval < 0)
57f6baccbcSWojciech Andralojc return retval;
58f6baccbcSWojciech Andralojc }
59f6baccbcSWojciech Andralojc
60f6baccbcSWojciech Andralojc /* Allocate and set up 1 TX queue per Ethernet port. */
61f6baccbcSWojciech Andralojc for (q = 0; q < tx_rings; q++) {
6260efb44fSRoman Zhukov retval = rte_eth_tx_queue_setup(port, q, nb_txd,
63f6baccbcSWojciech Andralojc rte_eth_dev_socket_id(port), NULL);
64f6baccbcSWojciech Andralojc if (retval < 0)
65f6baccbcSWojciech Andralojc return retval;
66f6baccbcSWojciech Andralojc }
67f6baccbcSWojciech Andralojc
68f6baccbcSWojciech Andralojc /* Start the Ethernet port. */
69f6baccbcSWojciech Andralojc retval = rte_eth_dev_start(port);
70f6baccbcSWojciech Andralojc if (retval < 0)
71f6baccbcSWojciech Andralojc return retval;
72f6baccbcSWojciech Andralojc
73f6baccbcSWojciech Andralojc /* Display the port MAC address. */
746d13ea8eSOlivier Matz struct rte_ether_addr addr;
7570febdcfSIgor Romanov retval = rte_eth_macaddr_get(port, &addr);
7670febdcfSIgor Romanov if (retval < 0)
7770febdcfSIgor Romanov return retval;
7870febdcfSIgor Romanov
79f6baccbcSWojciech Andralojc printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
80f6baccbcSWojciech Andralojc " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
81a7db3afcSAman Deep Singh port, RTE_ETHER_ADDR_BYTES(&addr));
82f6baccbcSWojciech Andralojc
83f6baccbcSWojciech Andralojc /* Enable RX in promiscuous mode for the Ethernet device. */
84f430bbceSIvan Ilchenko retval = rte_eth_promiscuous_enable(port);
85f430bbceSIvan Ilchenko if (retval != 0)
86f430bbceSIvan Ilchenko return retval;
87f6baccbcSWojciech Andralojc
88f6baccbcSWojciech Andralojc return 0;
89f6baccbcSWojciech Andralojc }
90f6baccbcSWojciech Andralojc
91f6baccbcSWojciech Andralojc /*
92f6baccbcSWojciech Andralojc * The lcore main. This is the main thread that does the work, reading from
93f6baccbcSWojciech Andralojc * an input port and writing to an output port.
94f6baccbcSWojciech Andralojc */
95ddcd7640SThomas Monjalon static __rte_noreturn void
lcore_main(void)96f6baccbcSWojciech Andralojc lcore_main(void)
97f6baccbcSWojciech Andralojc {
98f8244c63SZhiyong Yang uint16_t port;
99f6baccbcSWojciech Andralojc
100f6baccbcSWojciech Andralojc /*
101f6baccbcSWojciech Andralojc * Check that the port is on the same NUMA node as the polling thread
102f6baccbcSWojciech Andralojc * for best performance.
103f6baccbcSWojciech Andralojc */
1048728ccf3SThomas Monjalon RTE_ETH_FOREACH_DEV(port)
10559a50c6aSMin Hu (Connor) if (rte_eth_dev_socket_id(port) >= 0 &&
106f6baccbcSWojciech Andralojc rte_eth_dev_socket_id(port) !=
107f6baccbcSWojciech Andralojc (int)rte_socket_id())
108f6baccbcSWojciech Andralojc printf("WARNING, port %u is on remote NUMA node to "
109f6baccbcSWojciech Andralojc "polling thread.\n\tPerformance will "
110f6baccbcSWojciech Andralojc "not be optimal.\n", port);
111f6baccbcSWojciech Andralojc
112f6baccbcSWojciech Andralojc printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
113f6baccbcSWojciech Andralojc rte_lcore_id());
114f6baccbcSWojciech Andralojc
115f6baccbcSWojciech Andralojc /* Run until the application is quit or killed. */
116f6baccbcSWojciech Andralojc for (;;) {
117f6baccbcSWojciech Andralojc /*
118f6baccbcSWojciech Andralojc * Receive packets on a port and forward them on the paired
119f6baccbcSWojciech Andralojc * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
120f6baccbcSWojciech Andralojc */
1218728ccf3SThomas Monjalon RTE_ETH_FOREACH_DEV(port) {
122f6baccbcSWojciech Andralojc
123f6baccbcSWojciech Andralojc /* Get burst of RX packets, from first port of pair. */
124f6baccbcSWojciech Andralojc struct rte_mbuf *bufs[BURST_SIZE];
125f6baccbcSWojciech Andralojc const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
126f6baccbcSWojciech Andralojc bufs, BURST_SIZE);
127f6baccbcSWojciech Andralojc
128f6baccbcSWojciech Andralojc if (unlikely(nb_rx == 0))
129f6baccbcSWojciech Andralojc continue;
130f6baccbcSWojciech Andralojc
131f6baccbcSWojciech Andralojc /* Send burst of TX packets, to second port of pair. */
132f6baccbcSWojciech Andralojc const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
133f6baccbcSWojciech Andralojc bufs, nb_rx);
134f6baccbcSWojciech Andralojc
135f6baccbcSWojciech Andralojc /* Free any unsent packets. */
136f6baccbcSWojciech Andralojc if (unlikely(nb_tx < nb_rx)) {
137f6baccbcSWojciech Andralojc uint16_t buf;
138f6baccbcSWojciech Andralojc for (buf = nb_tx; buf < nb_rx; buf++)
139f6baccbcSWojciech Andralojc rte_pktmbuf_free(bufs[buf]);
140f6baccbcSWojciech Andralojc }
141f6baccbcSWojciech Andralojc }
142f6baccbcSWojciech Andralojc }
143f6baccbcSWojciech Andralojc }
144f6baccbcSWojciech Andralojc
145f6baccbcSWojciech Andralojc /*
146f6baccbcSWojciech Andralojc * The main function, which does initialization and calls the per-lcore
147f6baccbcSWojciech Andralojc * functions.
148f6baccbcSWojciech Andralojc */
149f6baccbcSWojciech Andralojc int
main(int argc,char * argv[])150f6baccbcSWojciech Andralojc main(int argc, char *argv[])
151f6baccbcSWojciech Andralojc {
152f6baccbcSWojciech Andralojc struct rte_mempool *mbuf_pool;
153f6baccbcSWojciech Andralojc unsigned nb_ports;
154f8244c63SZhiyong Yang uint16_t portid;
155f6baccbcSWojciech Andralojc
1569a212dc0SConor Fogarty /* Initialize the Environment Abstraction Layer (EAL). 8< */
157f6baccbcSWojciech Andralojc int ret = rte_eal_init(argc, argv);
158f6baccbcSWojciech Andralojc if (ret < 0)
159f6baccbcSWojciech Andralojc rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
160*7be78d02SJosh Soref /* >8 End of initialization the Environment Abstraction Layer (EAL). */
161f6baccbcSWojciech Andralojc
162f6baccbcSWojciech Andralojc argc -= ret;
163f6baccbcSWojciech Andralojc argv += ret;
164f6baccbcSWojciech Andralojc
165f6baccbcSWojciech Andralojc /*
166f6baccbcSWojciech Andralojc * Initialize the PQoS library and configure CAT.
167f6baccbcSWojciech Andralojc * Please see l2fwd-cat documentation for more info.
168f6baccbcSWojciech Andralojc */
1699a212dc0SConor Fogarty
1709a212dc0SConor Fogarty /* Initialize the PQoS. 8< */
171f6baccbcSWojciech Andralojc ret = cat_init(argc, argv);
172f6baccbcSWojciech Andralojc if (ret < 0)
173f6baccbcSWojciech Andralojc rte_exit(EXIT_FAILURE, "PQOS: L3CA init failed!\n");
1749a212dc0SConor Fogarty /* >8 End of initialization of PQoS. */
175f6baccbcSWojciech Andralojc
176f6baccbcSWojciech Andralojc argc -= ret;
177f6baccbcSWojciech Andralojc argv += ret;
178f6baccbcSWojciech Andralojc
179f6baccbcSWojciech Andralojc /* Check that there is an even number of ports to send/receive on. */
180d9a42a69SThomas Monjalon nb_ports = rte_eth_dev_count_avail();
181f6baccbcSWojciech Andralojc if (nb_ports < 2 || (nb_ports & 1))
182f6baccbcSWojciech Andralojc rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
183f6baccbcSWojciech Andralojc
184f6baccbcSWojciech Andralojc /* Creates a new mempool in memory to hold the mbufs. */
185f6baccbcSWojciech Andralojc mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
186f6baccbcSWojciech Andralojc MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
187f6baccbcSWojciech Andralojc
188f6baccbcSWojciech Andralojc if (mbuf_pool == NULL)
189f6baccbcSWojciech Andralojc rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
190f6baccbcSWojciech Andralojc
191f6baccbcSWojciech Andralojc /* Initialize all ports. */
1928728ccf3SThomas Monjalon RTE_ETH_FOREACH_DEV(portid)
193f6baccbcSWojciech Andralojc if (port_init(portid, mbuf_pool) != 0)
194f8244c63SZhiyong Yang rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu16 "\n",
195f6baccbcSWojciech Andralojc portid);
196f6baccbcSWojciech Andralojc
197f6baccbcSWojciech Andralojc if (rte_lcore_count() > 1)
198f6baccbcSWojciech Andralojc printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
199f6baccbcSWojciech Andralojc
200cb056611SStephen Hemminger /* Call lcore_main on the main core only. */
201f6baccbcSWojciech Andralojc lcore_main();
202f6baccbcSWojciech Andralojc
20310aa3757SChengchang Tang /* clean up the EAL */
20410aa3757SChengchang Tang rte_eal_cleanup();
20510aa3757SChengchang Tang
206f6baccbcSWojciech Andralojc return 0;
207f6baccbcSWojciech Andralojc }
208