xref: /dpdk/examples/qos_meter/main.c (revision d737e95480f4893f47b28acd7ac935d919456682)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <stdio.h>
35 #include <getopt.h>
36 
37 #include <rte_common.h>
38 #include <rte_eal.h>
39 #include <rte_mempool.h>
40 #include <rte_ethdev.h>
41 #include <rte_cycles.h>
42 #include <rte_mbuf.h>
43 #include <rte_meter.h>
44 
45 /*
46  * Traffic metering configuration
47  *
48  */
49 #define APP_MODE_FWD                    0
50 #define APP_MODE_SRTCM_COLOR_BLIND      1
51 #define APP_MODE_SRTCM_COLOR_AWARE      2
52 #define APP_MODE_TRTCM_COLOR_BLIND      3
53 #define APP_MODE_TRTCM_COLOR_AWARE      4
54 
55 #define APP_MODE	APP_MODE_SRTCM_COLOR_BLIND
56 
57 
58 #include "main.h"
59 
60 
61 #define APP_PKT_FLOW_POS                33
62 #define APP_PKT_COLOR_POS               5
63 
64 
65 #if APP_PKT_FLOW_POS > 64 || APP_PKT_COLOR_POS > 64
66 #error Byte offset needs to be less than 64
67 #endif
68 
69 /*
70  * Buffer pool configuration
71  *
72  ***/
73 #define MBUF_SIZE           (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
74 #define NB_MBUF             8192
75 #define MEMPOOL_CACHE_SIZE  256
76 
77 static struct rte_mempool *pool = NULL;
78 
79 /*
80  * NIC configuration
81  *
82  ***/
83 static struct rte_eth_conf port_conf = {
84 	.rxmode = {
85 		.mq_mode	= ETH_MQ_RX_RSS,
86 		.max_rx_pkt_len = ETHER_MAX_LEN,
87 		.split_hdr_size = 0,
88 		.header_split   = 0,
89 		.hw_ip_checksum = 1,
90 		.hw_vlan_filter = 0,
91 		.jumbo_frame    = 0,
92 		.hw_strip_crc   = 0,
93 	},
94 	.rx_adv_conf = {
95 		.rss_conf = {
96 			.rss_key = NULL,
97 			.rss_hf = ETH_RSS_IP,
98 		},
99 	},
100 	.txmode = {
101 		.mq_mode = ETH_DCB_NONE,
102 	},
103 };
104 
105 #define NIC_RX_QUEUE_DESC               128
106 #define NIC_TX_QUEUE_DESC               512
107 
108 #define NIC_RX_QUEUE                    0
109 #define NIC_TX_QUEUE                    0
110 
111 /*
112  * Packet RX/TX
113  *
114  ***/
115 #define PKT_RX_BURST_MAX                32
116 #define PKT_TX_BURST_MAX                32
117 #define TIME_TX_DRAIN                   200000ULL
118 
119 static uint8_t port_rx;
120 static uint8_t port_tx;
121 static struct rte_mbuf *pkts_rx[PKT_RX_BURST_MAX];
122 static struct rte_mbuf *pkts_tx[PKT_TX_BURST_MAX];
123 static uint16_t pkts_tx_len = 0;
124 
125 
126 struct rte_meter_srtcm_params app_srtcm_params[] = {
127 	{.cir = 1000000 * 46,  .cbs = 2048, .ebs = 2048},
128 };
129 
130 struct rte_meter_trtcm_params app_trtcm_params[] = {
131 	{.cir = 1000000 * 46,  .pir = 1500000 * 46,  .cbs = 2048, .pbs = 2048},
132 };
133 
134 #define APP_FLOWS_MAX  256
135 
136 FLOW_METER app_flows[APP_FLOWS_MAX];
137 
138 static void
139 app_configure_flow_table(void)
140 {
141 	uint32_t i, j;
142 
143 	for (i = 0, j = 0; i < APP_FLOWS_MAX; i ++, j = (j + 1) % RTE_DIM(PARAMS)){
144 		FUNC_CONFIG(&app_flows[i], &PARAMS[j]);
145 	}
146 }
147 
148 static inline void
149 app_set_pkt_color(uint8_t *pkt_data, enum policer_action color)
150 {
151 	pkt_data[APP_PKT_COLOR_POS] = (uint8_t)color;
152 }
153 
154 static inline int
155 app_pkt_handle(struct rte_mbuf *pkt, uint64_t time)
156 {
157 	uint8_t input_color, output_color;
158 	uint8_t *pkt_data = rte_pktmbuf_mtod(pkt, uint8_t *);
159 	uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct ether_hdr);
160 	uint8_t flow_id = (uint8_t)(pkt_data[APP_PKT_FLOW_POS] & (APP_FLOWS_MAX - 1));
161 	input_color = pkt_data[APP_PKT_COLOR_POS];
162 	enum policer_action action;
163 
164 	/* color input is not used for blind modes */
165 	output_color = (uint8_t) FUNC_METER(&app_flows[flow_id], time, pkt_len,
166 		(enum rte_meter_color) input_color);
167 
168 	/* Apply policing and set the output color */
169 	action = policer_table[input_color][output_color];
170 	app_set_pkt_color(pkt_data, action);
171 
172 	return action;
173 }
174 
175 
176 static __attribute__((noreturn)) int
177 main_loop(__attribute__((unused)) void *dummy)
178 {
179 	uint64_t current_time, last_time = rte_rdtsc();
180 	uint32_t lcore_id = rte_lcore_id();
181 
182 	printf("Core %u: port RX = %d, port TX = %d\n", lcore_id, port_rx, port_tx);
183 
184 	while (1) {
185 		uint64_t time_diff;
186 		int i, nb_rx;
187 
188 		/* Mechanism to avoid stale packets in the output buffer */
189 		current_time = rte_rdtsc();
190 		time_diff = current_time - last_time;
191 		if (unlikely(time_diff > TIME_TX_DRAIN)) {
192 			int ret;
193 
194 			if (pkts_tx_len == 0) {
195 				last_time = current_time;
196 
197 				continue;
198 			}
199 
200 			/* Write packet burst to NIC TX */
201 			ret = rte_eth_tx_burst(port_tx, NIC_TX_QUEUE, pkts_tx, pkts_tx_len);
202 
203 			/* Free buffers for any packets not written successfully */
204 			if (unlikely(ret < pkts_tx_len)) {
205 				for ( ; ret < pkts_tx_len; ret ++) {
206 					rte_pktmbuf_free(pkts_tx[ret]);
207 				}
208 			}
209 
210 			/* Empty the output buffer */
211 			pkts_tx_len = 0;
212 
213 			last_time = current_time;
214 		}
215 
216 		/* Read packet burst from NIC RX */
217 		nb_rx = rte_eth_rx_burst(port_rx, NIC_RX_QUEUE, pkts_rx, PKT_RX_BURST_MAX);
218 
219 		/* Handle packets */
220 		for (i = 0; i < nb_rx; i ++) {
221 			struct rte_mbuf *pkt = pkts_rx[i];
222 
223 			/* Handle current packet */
224 			if (app_pkt_handle(pkt, current_time) == DROP)
225 				rte_pktmbuf_free(pkt);
226 			else {
227 				pkts_tx[pkts_tx_len] = pkt;
228 				pkts_tx_len ++;
229 			}
230 
231 			/* Write packets from output buffer to NIC TX when full burst is available */
232 			if (unlikely(pkts_tx_len == PKT_TX_BURST_MAX)) {
233 				/* Write packet burst to NIC TX */
234 				int ret = rte_eth_tx_burst(port_tx, NIC_TX_QUEUE, pkts_tx, PKT_TX_BURST_MAX);
235 
236 				/* Free buffers for any packets not written successfully */
237 				if (unlikely(ret < PKT_TX_BURST_MAX)) {
238 					for ( ; ret < PKT_TX_BURST_MAX; ret ++) {
239 						rte_pktmbuf_free(pkts_tx[ret]);
240 					}
241 				}
242 
243 				/* Empty the output buffer */
244 				pkts_tx_len = 0;
245 			}
246 		}
247 	}
248 }
249 
250 static void
251 print_usage(const char *prgname)
252 {
253 	printf ("%s [EAL options] -- -p PORTMASK\n"
254 		"  -p PORTMASK: hexadecimal bitmask of ports to configure\n",
255 		prgname);
256 }
257 
258 static int
259 parse_portmask(const char *portmask)
260 {
261 	char *end = NULL;
262 	unsigned long pm;
263 
264 	/* parse hexadecimal string */
265 	pm = strtoul(portmask, &end, 16);
266 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
267 		return -1;
268 
269 	if (pm == 0)
270 		return -1;
271 
272 	return pm;
273 }
274 
275 /* Parse the argument given in the command line of the application */
276 static int
277 parse_args(int argc, char **argv)
278 {
279 	int opt;
280 	char **argvopt;
281 	int option_index;
282 	char *prgname = argv[0];
283 	static struct option lgopts[] = {
284 		{NULL, 0, 0, 0}
285 	};
286 	uint64_t port_mask, i, mask;
287 
288 	argvopt = argv;
289 
290 	while ((opt = getopt_long(argc, argvopt, "p:", lgopts, &option_index)) != EOF) {
291 		switch (opt) {
292 		case 'p':
293 			port_mask = parse_portmask(optarg);
294 			if (port_mask == 0) {
295 				printf("invalid port mask (null port mask)\n");
296 				print_usage(prgname);
297 				return -1;
298 			}
299 
300 			for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
301 				if (mask & port_mask){
302 					port_rx = i;
303 					port_mask &= ~ mask;
304 					break;
305 				}
306 			}
307 
308 			for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
309 				if (mask & port_mask){
310 					port_tx = i;
311 					port_mask &= ~ mask;
312 					break;
313 				}
314 			}
315 
316 			if (port_mask != 0) {
317 				printf("invalid port mask (more than 2 ports)\n");
318 				print_usage(prgname);
319 				return -1;
320 			}
321 			break;
322 
323 		default:
324 			print_usage(prgname);
325 			return -1;
326 		}
327 	}
328 
329 	if (optind <= 1) {
330 		print_usage(prgname);
331 		return -1;
332 	}
333 
334 	argv[optind-1] = prgname;
335 
336 	optind = 0; /* reset getopt lib */
337 	return 0;
338 }
339 
340 int
341 main(int argc, char **argv)
342 {
343 	uint32_t lcore_id;
344 	int ret;
345 
346 	/* EAL init */
347 	ret = rte_eal_init(argc, argv);
348 	if (ret < 0)
349 		rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
350 	argc -= ret;
351 	argv += ret;
352 	if (rte_lcore_count() != 1) {
353 		rte_exit(EXIT_FAILURE, "This application does not accept more than one core. "
354 		"Please adjust the \"-c COREMASK\" parameter accordingly.\n");
355 	}
356 
357 	/* Application non-EAL arguments parse */
358 	ret = parse_args(argc, argv);
359 	if (ret < 0)
360 		rte_exit(EXIT_FAILURE, "Invalid input arguments\n");
361 
362 	/* Buffer pool init */
363 	pool = rte_mempool_create("pool", NB_MBUF, MBUF_SIZE, MEMPOOL_CACHE_SIZE,
364 		sizeof(struct rte_pktmbuf_pool_private), rte_pktmbuf_pool_init, NULL,
365 		rte_pktmbuf_init, NULL, rte_socket_id(), 0);
366 	if (pool == NULL)
367 		rte_exit(EXIT_FAILURE, "Buffer pool creation error\n");
368 
369 	/* NIC init */
370 	ret = rte_eth_dev_configure(port_rx, 1, 1, &port_conf);
371 	if (ret < 0)
372 		rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_rx, ret);
373 
374 	ret = rte_eth_rx_queue_setup(port_rx, NIC_RX_QUEUE, NIC_RX_QUEUE_DESC,
375 				rte_eth_dev_socket_id(port_rx),
376 				NULL, pool);
377 	if (ret < 0)
378 		rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_rx, ret);
379 
380 	ret = rte_eth_tx_queue_setup(port_rx, NIC_TX_QUEUE, NIC_TX_QUEUE_DESC,
381 				rte_eth_dev_socket_id(port_rx),
382 				NULL);
383 	if (ret < 0)
384 	rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_rx, ret);
385 
386 	ret = rte_eth_dev_configure(port_tx, 1, 1, &port_conf);
387 	if (ret < 0)
388 		rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_tx, ret);
389 
390 	ret = rte_eth_rx_queue_setup(port_tx, NIC_RX_QUEUE, NIC_RX_QUEUE_DESC,
391 				rte_eth_dev_socket_id(port_tx),
392 				NULL, pool);
393 	if (ret < 0)
394 		rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_tx, ret);
395 
396 	ret = rte_eth_tx_queue_setup(port_tx, NIC_TX_QUEUE, NIC_TX_QUEUE_DESC,
397 				rte_eth_dev_socket_id(port_tx),
398 				NULL);
399 	if (ret < 0)
400 		rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_tx, ret);
401 
402 	ret = rte_eth_dev_start(port_rx);
403 	if (ret < 0)
404 		rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_rx, ret);
405 
406 	ret = rte_eth_dev_start(port_tx);
407 	if (ret < 0)
408 		rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_tx, ret);
409 
410 	rte_eth_promiscuous_enable(port_rx);
411 
412 	rte_eth_promiscuous_enable(port_tx);
413 
414 	/* App configuration */
415 	app_configure_flow_table();
416 
417 	/* Launch per-lcore init on every lcore */
418 	rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
419 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
420 		if (rte_eal_wait_lcore(lcore_id) < 0)
421 			return -1;
422 	}
423 
424 	return 0;
425 }
426