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