ae08261befd7de6c86cf68b3e95d36241c529922
[dpdk.git] / examples / skeleton / basicfwd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 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 #define RX_RING_SIZE 1024
14 #define TX_RING_SIZE 1024
15
16 #define NUM_MBUFS 8191
17 #define MBUF_CACHE_SIZE 250
18 #define BURST_SIZE 32
19
20 /* Configuration of ethernet ports. 8<  */
21 static const struct rte_eth_conf port_conf_default = {
22         .rxmode = {
23                 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
24         },
25 };
26 /* >8 End of configuration of ethernet ports. */
27
28 /* basicfwd.c: Basic DPDK skeleton forwarding example. */
29
30 /*
31  * Initializes a given port using global settings and with the RX buffers
32  * coming from the mbuf_pool passed as a parameter.
33  */
34
35 /* Main functional part of port initialization. 8< */
36 static inline int
37 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
38 {
39         struct rte_eth_conf port_conf = port_conf_default;
40         const uint16_t rx_rings = 1, tx_rings = 1;
41         uint16_t nb_rxd = RX_RING_SIZE;
42         uint16_t nb_txd = TX_RING_SIZE;
43         int retval;
44         uint16_t q;
45         struct rte_eth_dev_info dev_info;
46         struct rte_eth_txconf txconf;
47
48         if (!rte_eth_dev_is_valid_port(port))
49                 return -1;
50
51         retval = rte_eth_dev_info_get(port, &dev_info);
52         if (retval != 0) {
53                 printf("Error during getting device (port %u) info: %s\n",
54                                 port, strerror(-retval));
55                 return retval;
56         }
57
58         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
59                 port_conf.txmode.offloads |=
60                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
61
62         /* Configure the Ethernet device. */
63         retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
64         if (retval != 0)
65                 return retval;
66
67         retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
68         if (retval != 0)
69                 return retval;
70
71         /* Allocate and set up 1 RX queue per Ethernet port. */
72         for (q = 0; q < rx_rings; q++) {
73                 retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
74                                 rte_eth_dev_socket_id(port), NULL, mbuf_pool);
75                 if (retval < 0)
76                         return retval;
77         }
78
79         txconf = dev_info.default_txconf;
80         txconf.offloads = port_conf.txmode.offloads;
81         /* Allocate and set up 1 TX queue per Ethernet port. */
82         for (q = 0; q < tx_rings; q++) {
83                 retval = rte_eth_tx_queue_setup(port, q, nb_txd,
84                                 rte_eth_dev_socket_id(port), &txconf);
85                 if (retval < 0)
86                         return retval;
87         }
88
89         /* Starting Ethernet port. 8< */
90         retval = rte_eth_dev_start(port);
91         /* >8 End of starting of ethernet port. */
92         if (retval < 0)
93                 return retval;
94
95         /* Display the port MAC address. */
96         struct rte_ether_addr addr;
97         retval = rte_eth_macaddr_get(port, &addr);
98         if (retval != 0)
99                 return retval;
100
101         printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
102                            " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
103                         port,
104                         addr.addr_bytes[0], addr.addr_bytes[1],
105                         addr.addr_bytes[2], addr.addr_bytes[3],
106                         addr.addr_bytes[4], addr.addr_bytes[5]);
107
108         /* Enable RX in promiscuous mode for the Ethernet device. */
109         retval = rte_eth_promiscuous_enable(port);
110         /* End of setting RX port in promiscuous mode. */
111         if (retval != 0)
112                 return retval;
113
114         return 0;
115 }
116 /* >8 End of main functional part of port initialization. */
117
118 /*
119  * The lcore main. This is the main thread that does the work, reading from
120  * an input port and writing to an output port.
121  */
122
123  /* Basic forwarding application lcore. 8< */
124 static __rte_noreturn void
125 lcore_main(void)
126 {
127         uint16_t port;
128
129         /*
130          * Check that the port is on the same NUMA node as the polling thread
131          * for best performance.
132          */
133         RTE_ETH_FOREACH_DEV(port)
134                 if (rte_eth_dev_socket_id(port) >= 0 &&
135                                 rte_eth_dev_socket_id(port) !=
136                                                 (int)rte_socket_id())
137                         printf("WARNING, port %u is on remote NUMA node to "
138                                         "polling thread.\n\tPerformance will "
139                                         "not be optimal.\n", port);
140
141         printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
142                         rte_lcore_id());
143
144         /* Main work of application loop. 8< */
145         for (;;) {
146                 /*
147                  * Receive packets on a port and forward them on the paired
148                  * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
149                  */
150                 RTE_ETH_FOREACH_DEV(port) {
151
152                         /* Get burst of RX packets, from first port of pair. */
153                         struct rte_mbuf *bufs[BURST_SIZE];
154                         const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
155                                         bufs, BURST_SIZE);
156
157                         if (unlikely(nb_rx == 0))
158                                 continue;
159
160                         /* Send burst of TX packets, to second port of pair. */
161                         const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
162                                         bufs, nb_rx);
163
164                         /* Free any unsent packets. */
165                         if (unlikely(nb_tx < nb_rx)) {
166                                 uint16_t buf;
167                                 for (buf = nb_tx; buf < nb_rx; buf++)
168                                         rte_pktmbuf_free(bufs[buf]);
169                         }
170                 }
171         }
172         /* >8 End of loop. */
173 }
174 /* >8 End Basic forwarding application lcore. */
175
176 /*
177  * The main function, which does initialization and calls the per-lcore
178  * functions.
179  */
180 int
181 main(int argc, char *argv[])
182 {
183         struct rte_mempool *mbuf_pool;
184         unsigned nb_ports;
185         uint16_t portid;
186
187         /* Initializion the Environment Abstraction Layer (EAL). 8< */
188         int ret = rte_eal_init(argc, argv);
189         if (ret < 0)
190                 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
191         /* >8 End of initializion the Environment Abstraction Layer (EAL). */
192
193         argc -= ret;
194         argv += ret;
195
196         /* Check that there is an even number of ports to send/receive on. */
197         nb_ports = rte_eth_dev_count_avail();
198         if (nb_ports < 2 || (nb_ports & 1))
199                 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
200
201         /* Creates a new mempool in memory to hold the mbufs. */
202
203         /* Allocates mempool to hold the mbufs. 8< */
204         mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
205                 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
206         /* >8 End of allocating mempool to hold mbuf. */
207
208         if (mbuf_pool == NULL)
209                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
210
211         /* Initializing all ports. 8< */
212         RTE_ETH_FOREACH_DEV(portid)
213                 if (port_init(portid, mbuf_pool) != 0)
214                         rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu16 "\n",
215                                         portid);
216         /* >8 End of initializing all ports. */
217
218         if (rte_lcore_count() > 1)
219                 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
220
221         /* Call lcore_main on the main core only. Called on single lcore. 8< */
222         lcore_main();
223         /* >8 End of called on single lcore. */
224
225         /* clean up the EAL */
226         rte_eal_cleanup();
227
228         return 0;
229 }