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