xref: /dpdk/examples/rxtx_callbacks/main.c (revision 117eaa70584b73eebf6f648cf3ee6f2ab03264a0)
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 static unsigned nb_ports;
28 
29 static struct {
30 	uint64_t total_cycles;
31 	uint64_t total_pkts;
32 } latency_numbers;
33 
34 
35 static uint16_t
36 add_timestamps(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
37 		struct rte_mbuf **pkts, uint16_t nb_pkts,
38 		uint16_t max_pkts __rte_unused, void *_ __rte_unused)
39 {
40 	unsigned i;
41 	uint64_t now = rte_rdtsc();
42 
43 	for (i = 0; i < nb_pkts; i++)
44 		pkts[i]->udata64 = now;
45 	return nb_pkts;
46 }
47 
48 static uint16_t
49 calc_latency(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
50 		struct rte_mbuf **pkts, uint16_t nb_pkts, void *_ __rte_unused)
51 {
52 	uint64_t cycles = 0;
53 	uint64_t now = rte_rdtsc();
54 	unsigned i;
55 
56 	for (i = 0; i < nb_pkts; i++)
57 		cycles += now - pkts[i]->udata64;
58 	latency_numbers.total_cycles += cycles;
59 	latency_numbers.total_pkts += nb_pkts;
60 
61 	if (latency_numbers.total_pkts > (100 * 1000 * 1000ULL)) {
62 		printf("Latency = %"PRIu64" cycles\n",
63 		latency_numbers.total_cycles / latency_numbers.total_pkts);
64 		latency_numbers.total_cycles = latency_numbers.total_pkts = 0;
65 	}
66 	return nb_pkts;
67 }
68 
69 /*
70  * Initialises a given port using global settings and with the rx buffers
71  * coming from the mbuf_pool passed as parameter
72  */
73 static inline int
74 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
75 {
76 	struct rte_eth_conf port_conf = port_conf_default;
77 	const uint16_t rx_rings = 1, tx_rings = 1;
78 	uint16_t nb_rxd = RX_RING_SIZE;
79 	uint16_t nb_txd = TX_RING_SIZE;
80 	int retval;
81 	uint16_t q;
82 	struct rte_eth_dev_info dev_info;
83 	struct rte_eth_txconf txconf;
84 
85 	if (port >= rte_eth_dev_count())
86 		return -1;
87 
88 	rte_eth_dev_info_get(port, &dev_info);
89 	if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
90 		port_conf.txmode.offloads |=
91 			DEV_TX_OFFLOAD_MBUF_FAST_FREE;
92 
93 	retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
94 	if (retval != 0)
95 		return retval;
96 
97 	retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
98 	if (retval != 0)
99 		return retval;
100 
101 	for (q = 0; q < rx_rings; q++) {
102 		retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
103 				rte_eth_dev_socket_id(port), NULL, mbuf_pool);
104 		if (retval < 0)
105 			return retval;
106 	}
107 
108 	txconf = dev_info.default_txconf;
109 	txconf.txq_flags = ETH_TXQ_FLAGS_IGNORE;
110 	txconf.offloads = port_conf.txmode.offloads;
111 	for (q = 0; q < tx_rings; q++) {
112 		retval = rte_eth_tx_queue_setup(port, q, nb_txd,
113 				rte_eth_dev_socket_id(port), &txconf);
114 		if (retval < 0)
115 			return retval;
116 	}
117 
118 	retval  = rte_eth_dev_start(port);
119 	if (retval < 0)
120 		return retval;
121 
122 	struct ether_addr addr;
123 
124 	rte_eth_macaddr_get(port, &addr);
125 	printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
126 			" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
127 			(unsigned)port,
128 			addr.addr_bytes[0], addr.addr_bytes[1],
129 			addr.addr_bytes[2], addr.addr_bytes[3],
130 			addr.addr_bytes[4], addr.addr_bytes[5]);
131 
132 	rte_eth_promiscuous_enable(port);
133 	rte_eth_add_rx_callback(port, 0, add_timestamps, NULL);
134 	rte_eth_add_tx_callback(port, 0, calc_latency, NULL);
135 
136 	return 0;
137 }
138 
139 /*
140  * Main thread that does the work, reading from INPUT_PORT
141  * and writing to OUTPUT_PORT
142  */
143 static  __attribute__((noreturn)) void
144 lcore_main(void)
145 {
146 	uint16_t port;
147 
148 	for (port = 0; port < nb_ports; port++)
149 		if (rte_eth_dev_socket_id(port) > 0 &&
150 				rte_eth_dev_socket_id(port) !=
151 						(int)rte_socket_id())
152 			printf("WARNING, port %u is on remote NUMA node to "
153 					"polling thread.\n\tPerformance will "
154 					"not be optimal.\n", port);
155 
156 	printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
157 			rte_lcore_id());
158 	for (;;) {
159 		for (port = 0; port < nb_ports; port++) {
160 			struct rte_mbuf *bufs[BURST_SIZE];
161 			const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
162 					bufs, BURST_SIZE);
163 			if (unlikely(nb_rx == 0))
164 				continue;
165 			const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
166 					bufs, nb_rx);
167 			if (unlikely(nb_tx < nb_rx)) {
168 				uint16_t buf;
169 
170 				for (buf = nb_tx; buf < nb_rx; buf++)
171 					rte_pktmbuf_free(bufs[buf]);
172 			}
173 		}
174 	}
175 }
176 
177 /* Main function, does initialisation and calls the per-lcore functions */
178 int
179 main(int argc, char *argv[])
180 {
181 	struct rte_mempool *mbuf_pool;
182 	uint16_t portid;
183 
184 	/* init EAL */
185 	int ret = rte_eal_init(argc, argv);
186 
187 	if (ret < 0)
188 		rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
189 	argc -= ret;
190 	argv += ret;
191 
192 	nb_ports = rte_eth_dev_count();
193 	if (nb_ports < 2 || (nb_ports & 1))
194 		rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
195 
196 	mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
197 		NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
198 		RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
199 	if (mbuf_pool == NULL)
200 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
201 
202 	/* initialize all ports */
203 	for (portid = 0; portid < nb_ports; portid++)
204 		if (port_init(portid, mbuf_pool) != 0)
205 			rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8"\n",
206 					portid);
207 
208 	if (rte_lcore_count() > 1)
209 		printf("\nWARNING: Too much enabled lcores - "
210 			"App uses only 1 lcore\n");
211 
212 	/* call lcore_main on master core only */
213 	lcore_main();
214 	return 0;
215 }
216