xref: /dpdk/examples/qos_meter/main.c (revision 169a9da64a859e1782e49886e7214982304a580f)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <getopt.h>
7 
8 #include <rte_common.h>
9 #include <rte_eal.h>
10 #include <rte_malloc.h>
11 #include <rte_mempool.h>
12 #include <rte_ethdev.h>
13 #include <rte_cycles.h>
14 #include <rte_mbuf.h>
15 #include <rte_meter.h>
16 
17 /*
18  * Traffic metering configuration
19  *
20  */
21 #define APP_MODE_FWD                    0
22 #define APP_MODE_SRTCM_COLOR_BLIND      1
23 #define APP_MODE_SRTCM_COLOR_AWARE      2
24 #define APP_MODE_TRTCM_COLOR_BLIND      3
25 #define APP_MODE_TRTCM_COLOR_AWARE      4
26 
27 #define APP_MODE	APP_MODE_SRTCM_COLOR_BLIND
28 
29 
30 #include "main.h"
31 
32 
33 #define APP_PKT_FLOW_POS                33
34 #define APP_PKT_COLOR_POS               5
35 
36 
37 #if APP_PKT_FLOW_POS > 64 || APP_PKT_COLOR_POS > 64
38 #error Byte offset needs to be less than 64
39 #endif
40 
41 /*
42  * Buffer pool configuration
43  *
44  ***/
45 #define NB_MBUF             8192
46 #define MEMPOOL_CACHE_SIZE  256
47 
48 static struct rte_mempool *pool = NULL;
49 
50 /*
51  * NIC configuration
52  *
53  ***/
54 static struct rte_eth_conf port_conf = {
55 	.rxmode = {
56 		.mq_mode	= ETH_MQ_RX_RSS,
57 		.max_rx_pkt_len = ETHER_MAX_LEN,
58 		.split_hdr_size = 0,
59 		.ignore_offload_bitfield = 1,
60 		.offloads = (DEV_RX_OFFLOAD_CHECKSUM |
61 			     DEV_RX_OFFLOAD_CRC_STRIP),
62 	},
63 	.rx_adv_conf = {
64 		.rss_conf = {
65 			.rss_key = NULL,
66 			.rss_hf = ETH_RSS_IP,
67 		},
68 	},
69 	.txmode = {
70 		.mq_mode = ETH_DCB_NONE,
71 	},
72 };
73 
74 #define NIC_RX_QUEUE_DESC               1024
75 #define NIC_TX_QUEUE_DESC               1024
76 
77 #define NIC_RX_QUEUE                    0
78 #define NIC_TX_QUEUE                    0
79 
80 /*
81  * Packet RX/TX
82  *
83  ***/
84 #define PKT_RX_BURST_MAX                32
85 #define PKT_TX_BURST_MAX                32
86 #define TIME_TX_DRAIN                   200000ULL
87 
88 static uint16_t port_rx;
89 static uint16_t port_tx;
90 static struct rte_mbuf *pkts_rx[PKT_RX_BURST_MAX];
91 struct rte_eth_dev_tx_buffer *tx_buffer;
92 
93 struct rte_meter_srtcm_params app_srtcm_params = {
94 	.cir = 1000000 * 46,
95 	.cbs = 2048,
96 	.ebs = 2048
97 };
98 
99 struct rte_meter_srtcm_profile app_srtcm_profile;
100 
101 struct rte_meter_trtcm_params app_trtcm_params = {
102 	.cir = 1000000 * 46,
103 	.pir = 1500000 * 46,
104 	.cbs = 2048,
105 	.pbs = 2048
106 };
107 
108 struct rte_meter_trtcm_profile app_trtcm_profile;
109 
110 #define APP_FLOWS_MAX  256
111 
112 FLOW_METER app_flows[APP_FLOWS_MAX];
113 
114 static int
115 app_configure_flow_table(void)
116 {
117 	uint32_t i;
118 	int ret;
119 
120 	ret = rte_meter_srtcm_profile_config(&app_srtcm_profile,
121 		&app_srtcm_params);
122 	if (ret)
123 		return ret;
124 
125 	ret = rte_meter_trtcm_profile_config(&app_trtcm_profile,
126 		&app_trtcm_params);
127 	if (ret)
128 		return ret;
129 
130 	for (i = 0; i < APP_FLOWS_MAX; i++) {
131 		ret = FUNC_CONFIG(&app_flows[i], &PROFILE);
132 		if (ret)
133 			return ret;
134 	}
135 
136 	return 0;
137 }
138 
139 static inline void
140 app_set_pkt_color(uint8_t *pkt_data, enum policer_action color)
141 {
142 	pkt_data[APP_PKT_COLOR_POS] = (uint8_t)color;
143 }
144 
145 static inline int
146 app_pkt_handle(struct rte_mbuf *pkt, uint64_t time)
147 {
148 	uint8_t input_color, output_color;
149 	uint8_t *pkt_data = rte_pktmbuf_mtod(pkt, uint8_t *);
150 	uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct ether_hdr);
151 	uint8_t flow_id = (uint8_t)(pkt_data[APP_PKT_FLOW_POS] & (APP_FLOWS_MAX - 1));
152 	input_color = pkt_data[APP_PKT_COLOR_POS];
153 	enum policer_action action;
154 
155 	/* color input is not used for blind modes */
156 	output_color = (uint8_t) FUNC_METER(&app_flows[flow_id],
157 		&PROFILE,
158 		time,
159 		pkt_len,
160 		(enum rte_meter_color) input_color);
161 
162 	/* Apply policing and set the output color */
163 	action = policer_table[input_color][output_color];
164 	app_set_pkt_color(pkt_data, action);
165 
166 	return action;
167 }
168 
169 
170 static __attribute__((noreturn)) int
171 main_loop(__attribute__((unused)) void *dummy)
172 {
173 	uint64_t current_time, last_time = rte_rdtsc();
174 	uint32_t lcore_id = rte_lcore_id();
175 
176 	printf("Core %u: port RX = %d, port TX = %d\n", lcore_id, port_rx, port_tx);
177 
178 	while (1) {
179 		uint64_t time_diff;
180 		int i, nb_rx;
181 
182 		/* Mechanism to avoid stale packets in the output buffer */
183 		current_time = rte_rdtsc();
184 		time_diff = current_time - last_time;
185 		if (unlikely(time_diff > TIME_TX_DRAIN)) {
186 			/* Flush tx buffer */
187 			rte_eth_tx_buffer_flush(port_tx, NIC_TX_QUEUE, tx_buffer);
188 			last_time = current_time;
189 		}
190 
191 		/* Read packet burst from NIC RX */
192 		nb_rx = rte_eth_rx_burst(port_rx, NIC_RX_QUEUE, pkts_rx, PKT_RX_BURST_MAX);
193 
194 		/* Handle packets */
195 		for (i = 0; i < nb_rx; i ++) {
196 			struct rte_mbuf *pkt = pkts_rx[i];
197 
198 			/* Handle current packet */
199 			if (app_pkt_handle(pkt, current_time) == DROP)
200 				rte_pktmbuf_free(pkt);
201 			else
202 				rte_eth_tx_buffer(port_tx, NIC_TX_QUEUE, tx_buffer, pkt);
203 		}
204 	}
205 }
206 
207 static void
208 print_usage(const char *prgname)
209 {
210 	printf ("%s [EAL options] -- -p PORTMASK\n"
211 		"  -p PORTMASK: hexadecimal bitmask of ports to configure\n",
212 		prgname);
213 }
214 
215 static int
216 parse_portmask(const char *portmask)
217 {
218 	char *end = NULL;
219 	unsigned long pm;
220 
221 	/* parse hexadecimal string */
222 	pm = strtoul(portmask, &end, 16);
223 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
224 		return -1;
225 
226 	if (pm == 0)
227 		return -1;
228 
229 	return pm;
230 }
231 
232 /* Parse the argument given in the command line of the application */
233 static int
234 parse_args(int argc, char **argv)
235 {
236 	int opt;
237 	char **argvopt;
238 	int option_index;
239 	char *prgname = argv[0];
240 	static struct option lgopts[] = {
241 		{NULL, 0, 0, 0}
242 	};
243 	uint64_t port_mask, i, mask;
244 
245 	argvopt = argv;
246 
247 	while ((opt = getopt_long(argc, argvopt, "p:", lgopts, &option_index)) != EOF) {
248 		switch (opt) {
249 		case 'p':
250 			port_mask = parse_portmask(optarg);
251 			if (port_mask == 0) {
252 				printf("invalid port mask (null port mask)\n");
253 				print_usage(prgname);
254 				return -1;
255 			}
256 
257 			for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
258 				if (mask & port_mask){
259 					port_rx = i;
260 					port_mask &= ~ mask;
261 					break;
262 				}
263 			}
264 
265 			for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
266 				if (mask & port_mask){
267 					port_tx = i;
268 					port_mask &= ~ mask;
269 					break;
270 				}
271 			}
272 
273 			if (port_mask != 0) {
274 				printf("invalid port mask (more than 2 ports)\n");
275 				print_usage(prgname);
276 				return -1;
277 			}
278 			break;
279 
280 		default:
281 			print_usage(prgname);
282 			return -1;
283 		}
284 	}
285 
286 	if (optind <= 1) {
287 		print_usage(prgname);
288 		return -1;
289 	}
290 
291 	argv[optind-1] = prgname;
292 
293 	optind = 1; /* reset getopt lib */
294 	return 0;
295 }
296 
297 int
298 main(int argc, char **argv)
299 {
300 	uint32_t lcore_id;
301 	uint16_t nb_rxd = NIC_RX_QUEUE_DESC;
302 	uint16_t nb_txd = NIC_TX_QUEUE_DESC;
303 	struct rte_eth_conf conf;
304 	struct rte_eth_rxconf rxq_conf;
305 	struct rte_eth_txconf txq_conf;
306 	struct rte_eth_dev_info dev_info;
307 	int ret;
308 
309 	/* EAL init */
310 	ret = rte_eal_init(argc, argv);
311 	if (ret < 0)
312 		rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
313 	argc -= ret;
314 	argv += ret;
315 	if (rte_lcore_count() != 1) {
316 		rte_exit(EXIT_FAILURE, "This application does not accept more than one core. "
317 		"Please adjust the \"-c COREMASK\" parameter accordingly.\n");
318 	}
319 
320 	/* Application non-EAL arguments parse */
321 	ret = parse_args(argc, argv);
322 	if (ret < 0)
323 		rte_exit(EXIT_FAILURE, "Invalid input arguments\n");
324 
325 	/* Buffer pool init */
326 	pool = rte_pktmbuf_pool_create("pool", NB_MBUF, MEMPOOL_CACHE_SIZE,
327 		0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
328 	if (pool == NULL)
329 		rte_exit(EXIT_FAILURE, "Buffer pool creation error\n");
330 
331 	/* NIC init */
332 	conf = port_conf;
333 	rte_eth_dev_info_get(port_rx, &dev_info);
334 	if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
335 		conf.txmode.offloads |= DEV_TX_OFFLOAD_MBUF_FAST_FREE;
336 	ret = rte_eth_dev_configure(port_rx, 1, 1, &conf);
337 	if (ret < 0)
338 		rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_rx, ret);
339 
340 	ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_rx, &nb_rxd, &nb_txd);
341 	if (ret < 0)
342 		rte_exit(EXIT_FAILURE, "Port %d adjust number of descriptors error (%d)\n",
343 				port_rx, ret);
344 
345 	rxq_conf = dev_info.default_rxconf;
346 	rxq_conf.offloads = conf.rxmode.offloads;
347 	ret = rte_eth_rx_queue_setup(port_rx, NIC_RX_QUEUE, nb_rxd,
348 				rte_eth_dev_socket_id(port_rx),
349 				&rxq_conf, pool);
350 	if (ret < 0)
351 		rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_rx, ret);
352 
353 	txq_conf = dev_info.default_txconf;
354 	txq_conf.txq_flags = ETH_TXQ_FLAGS_IGNORE;
355 	txq_conf.offloads = conf.txmode.offloads;
356 	ret = rte_eth_tx_queue_setup(port_rx, NIC_TX_QUEUE, nb_txd,
357 				rte_eth_dev_socket_id(port_rx),
358 				&txq_conf);
359 	if (ret < 0)
360 	rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_rx, ret);
361 
362 	conf = port_conf;
363 	rte_eth_dev_info_get(port_tx, &dev_info);
364 	if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
365 		conf.txmode.offloads |= DEV_TX_OFFLOAD_MBUF_FAST_FREE;
366 	ret = rte_eth_dev_configure(port_tx, 1, 1, &conf);
367 	if (ret < 0)
368 		rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_tx, ret);
369 
370 	nb_rxd = NIC_RX_QUEUE_DESC;
371 	nb_txd = NIC_TX_QUEUE_DESC;
372 	ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_tx, &nb_rxd, &nb_txd);
373 	if (ret < 0)
374 		rte_exit(EXIT_FAILURE, "Port %d adjust number of descriptors error (%d)\n",
375 				port_tx, ret);
376 
377 	rxq_conf = dev_info.default_rxconf;
378 	rxq_conf.offloads = conf.rxmode.offloads;
379 	ret = rte_eth_rx_queue_setup(port_tx, NIC_RX_QUEUE, nb_rxd,
380 				rte_eth_dev_socket_id(port_tx),
381 				NULL, pool);
382 	if (ret < 0)
383 		rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_tx, ret);
384 
385 	txq_conf = dev_info.default_txconf;
386 	txq_conf.txq_flags = ETH_TXQ_FLAGS_IGNORE;
387 	txq_conf.offloads = conf.txmode.offloads;
388 	ret = rte_eth_tx_queue_setup(port_tx, NIC_TX_QUEUE, nb_txd,
389 				rte_eth_dev_socket_id(port_tx),
390 				NULL);
391 	if (ret < 0)
392 		rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_tx, ret);
393 
394 	tx_buffer = rte_zmalloc_socket("tx_buffer",
395 			RTE_ETH_TX_BUFFER_SIZE(PKT_TX_BURST_MAX), 0,
396 			rte_eth_dev_socket_id(port_tx));
397 	if (tx_buffer == NULL)
398 		rte_exit(EXIT_FAILURE, "Port %d TX buffer allocation error\n",
399 				port_tx);
400 
401 	rte_eth_tx_buffer_init(tx_buffer, PKT_TX_BURST_MAX);
402 
403 	ret = rte_eth_dev_start(port_rx);
404 	if (ret < 0)
405 		rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_rx, ret);
406 
407 	ret = rte_eth_dev_start(port_tx);
408 	if (ret < 0)
409 		rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_tx, ret);
410 
411 	rte_eth_promiscuous_enable(port_rx);
412 
413 	rte_eth_promiscuous_enable(port_tx);
414 
415 	/* App configuration */
416 	ret = app_configure_flow_table();
417 	if (ret < 0)
418 		rte_exit(EXIT_FAILURE, "Invalid configure flow table\n");
419 
420 	/* Launch per-lcore init on every lcore */
421 	rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
422 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
423 		if (rte_eal_wait_lcore(lcore_id) < 0)
424 			return -1;
425 	}
426 
427 	return 0;
428 }
429