1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4 
5 #include <stdint.h>
6 #include <inttypes.h>
7 #include <getopt.h>
8 
9 #include <rte_eal.h>
10 #include <rte_ethdev.h>
11 #include <rte_cycles.h>
12 #include <rte_lcore.h>
13 #include <rte_mbuf.h>
14 #include <rte_flow.h>
15 #include <rte_flow_classify.h>
16 #include <rte_table_acl.h>
17 
18 #define RX_RING_SIZE 1024
19 #define TX_RING_SIZE 1024
20 
21 #define NUM_MBUFS 8191
22 #define MBUF_CACHE_SIZE 250
23 #define BURST_SIZE 32
24 
25 #define MAX_NUM_CLASSIFY 30
26 #define FLOW_CLASSIFY_MAX_RULE_NUM 91
27 #define FLOW_CLASSIFY_MAX_PRIORITY 8
28 #define FLOW_CLASSIFIER_NAME_SIZE 64
29 
30 #define COMMENT_LEAD_CHAR	('#')
31 #define OPTION_RULE_IPV4	"rule_ipv4"
32 #define RTE_LOGTYPE_FLOW_CLASSIFY	RTE_LOGTYPE_USER3
33 #define flow_classify_log(format, ...) \
34 		RTE_LOG(ERR, FLOW_CLASSIFY, format, ##__VA_ARGS__)
35 
36 #define uint32_t_to_char(ip, a, b, c, d) do {\
37 		*a = (unsigned char)(ip >> 24 & 0xff);\
38 		*b = (unsigned char)(ip >> 16 & 0xff);\
39 		*c = (unsigned char)(ip >> 8 & 0xff);\
40 		*d = (unsigned char)(ip & 0xff);\
41 	} while (0)
42 
43 enum {
44 	CB_FLD_SRC_ADDR,
45 	CB_FLD_DST_ADDR,
46 	CB_FLD_SRC_PORT,
47 	CB_FLD_SRC_PORT_DLM,
48 	CB_FLD_SRC_PORT_MASK,
49 	CB_FLD_DST_PORT,
50 	CB_FLD_DST_PORT_DLM,
51 	CB_FLD_DST_PORT_MASK,
52 	CB_FLD_PROTO,
53 	CB_FLD_PRIORITY,
54 	CB_FLD_NUM,
55 };
56 
57 static struct{
58 	const char *rule_ipv4_name;
59 } parm_config;
60 const char cb_port_delim[] = ":";
61 
62 /* Ethernet ports configured with default settings using struct. 8< */
63 static const struct rte_eth_conf port_conf_default = {
64 	.rxmode = {
65 		.max_rx_pkt_len = RTE_ETHER_MAX_LEN,
66 	},
67 };
68 /* >8 End of configuration of Ethernet ports. */
69 
70 /* Creation of flow classifier object. 8< */
71 struct flow_classifier {
72 	struct rte_flow_classifier *cls;
73 };
74 
75 struct flow_classifier_acl {
76 	struct flow_classifier cls;
77 } __rte_cache_aligned;
78 /* >8 End of creation of flow classifier object. */
79 
80 /*  Creation of ACL table during initialization of application. 8< */
81 
82 /* ACL field definitions for IPv4 5 tuple rule */
83 enum {
84 	PROTO_FIELD_IPV4,
85 	SRC_FIELD_IPV4,
86 	DST_FIELD_IPV4,
87 	SRCP_FIELD_IPV4,
88 	DSTP_FIELD_IPV4,
89 	NUM_FIELDS_IPV4
90 };
91 
92 enum {
93 	PROTO_INPUT_IPV4,
94 	SRC_INPUT_IPV4,
95 	DST_INPUT_IPV4,
96 	SRCP_DESTP_INPUT_IPV4
97 };
98 
99 static struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
100 	/* first input field - always one byte long. */
101 	{
102 		.type = RTE_ACL_FIELD_TYPE_BITMASK,
103 		.size = sizeof(uint8_t),
104 		.field_index = PROTO_FIELD_IPV4,
105 		.input_index = PROTO_INPUT_IPV4,
106 		.offset = sizeof(struct rte_ether_hdr) +
107 			offsetof(struct rte_ipv4_hdr, next_proto_id),
108 	},
109 	/* next input field (IPv4 source address) - 4 consecutive bytes. */
110 	{
111 		/* rte_flow uses a bit mask for IPv4 addresses */
112 		.type = RTE_ACL_FIELD_TYPE_BITMASK,
113 		.size = sizeof(uint32_t),
114 		.field_index = SRC_FIELD_IPV4,
115 		.input_index = SRC_INPUT_IPV4,
116 		.offset = sizeof(struct rte_ether_hdr) +
117 			offsetof(struct rte_ipv4_hdr, src_addr),
118 	},
119 	/* next input field (IPv4 destination address) - 4 consecutive bytes. */
120 	{
121 		/* rte_flow uses a bit mask for IPv4 addresses */
122 		.type = RTE_ACL_FIELD_TYPE_BITMASK,
123 		.size = sizeof(uint32_t),
124 		.field_index = DST_FIELD_IPV4,
125 		.input_index = DST_INPUT_IPV4,
126 		.offset = sizeof(struct rte_ether_hdr) +
127 			offsetof(struct rte_ipv4_hdr, dst_addr),
128 	},
129 	/*
130 	 * Next 2 fields (src & dst ports) form 4 consecutive bytes.
131 	 * They share the same input index.
132 	 */
133 	{
134 		/* rte_flow uses a bit mask for protocol ports */
135 		.type = RTE_ACL_FIELD_TYPE_BITMASK,
136 		.size = sizeof(uint16_t),
137 		.field_index = SRCP_FIELD_IPV4,
138 		.input_index = SRCP_DESTP_INPUT_IPV4,
139 		.offset = sizeof(struct rte_ether_hdr) +
140 			sizeof(struct rte_ipv4_hdr) +
141 			offsetof(struct rte_tcp_hdr, src_port),
142 	},
143 	{
144 		/* rte_flow uses a bit mask for protocol ports */
145 		.type = RTE_ACL_FIELD_TYPE_BITMASK,
146 		.size = sizeof(uint16_t),
147 		.field_index = DSTP_FIELD_IPV4,
148 		.input_index = SRCP_DESTP_INPUT_IPV4,
149 		.offset = sizeof(struct rte_ether_hdr) +
150 			sizeof(struct rte_ipv4_hdr) +
151 			offsetof(struct rte_tcp_hdr, dst_port),
152 	},
153 };
154 /* >8 End of creation of ACL table. */
155 
156 /* Flow classify data. 8< */
157 static int num_classify_rules;
158 static struct rte_flow_classify_rule *rules[MAX_NUM_CLASSIFY];
159 static struct rte_flow_classify_ipv4_5tuple_stats ntuple_stats;
160 static struct rte_flow_classify_stats classify_stats = {
161 		.stats = (void **)&ntuple_stats
162 };
163 /* >8 End of flow classify data. */
164 
165 /* parameters for rte_flow_classify_validate and
166  * rte_flow_classify_table_entry_add functions
167  */
168 
169 static struct rte_flow_item  eth_item = { RTE_FLOW_ITEM_TYPE_ETH,
170 	0, 0, 0 };
171 static struct rte_flow_item  end_item = { RTE_FLOW_ITEM_TYPE_END,
172 	0, 0, 0 };
173 
174 /* sample actions:
175  * "actions count / end"
176  */
177 struct rte_flow_query_count count = {
178 	.reset = 1,
179 	.hits_set = 1,
180 	.bytes_set = 1,
181 	.hits = 0,
182 	.bytes = 0,
183 };
184 static struct rte_flow_action count_action = { RTE_FLOW_ACTION_TYPE_COUNT,
185 	&count};
186 static struct rte_flow_action end_action = { RTE_FLOW_ACTION_TYPE_END, 0};
187 static struct rte_flow_action actions[2];
188 
189 /* sample attributes */
190 static struct rte_flow_attr attr;
191 
192 /* flow_classify.c: * Based on DPDK skeleton forwarding example. */
193 
194 /*
195  * Initializes a given port using global settings and with the RX buffers
196  * coming from the mbuf_pool passed as a parameter.
197  */
198 
199 /* Initializing port using global settings. 8< */
200 static inline int
201 port_init(uint8_t port, struct rte_mempool *mbuf_pool)
202 {
203 	struct rte_eth_conf port_conf = port_conf_default;
204 	struct rte_ether_addr addr;
205 	const uint16_t rx_rings = 1, tx_rings = 1;
206 	int retval;
207 	uint16_t q;
208 	struct rte_eth_dev_info dev_info;
209 	struct rte_eth_txconf txconf;
210 
211 	if (!rte_eth_dev_is_valid_port(port))
212 		return -1;
213 
214 	retval = rte_eth_dev_info_get(port, &dev_info);
215 	if (retval != 0) {
216 		printf("Error during getting device (port %u) info: %s\n",
217 				port, strerror(-retval));
218 		return retval;
219 	}
220 
221 	if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
222 		port_conf.txmode.offloads |=
223 			DEV_TX_OFFLOAD_MBUF_FAST_FREE;
224 
225 	/* Configure the Ethernet device. */
226 	retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
227 	if (retval != 0)
228 		return retval;
229 
230 	/* Allocate and set up 1 RX queue per Ethernet port. */
231 	for (q = 0; q < rx_rings; q++) {
232 		retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
233 				rte_eth_dev_socket_id(port), NULL, mbuf_pool);
234 		if (retval < 0)
235 			return retval;
236 	}
237 
238 	txconf = dev_info.default_txconf;
239 	txconf.offloads = port_conf.txmode.offloads;
240 	/* Allocate and set up 1 TX queue per Ethernet port. */
241 	for (q = 0; q < tx_rings; q++) {
242 		retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
243 				rte_eth_dev_socket_id(port), &txconf);
244 		if (retval < 0)
245 			return retval;
246 	}
247 
248 	/* Start the Ethernet port. 8< */
249 	retval = rte_eth_dev_start(port);
250 	/* >8 End of starting the Ethernet port. */
251 	if (retval < 0)
252 		return retval;
253 
254 	/* Display the port MAC address. */
255 	retval = rte_eth_macaddr_get(port, &addr);
256 	if (retval != 0)
257 		return retval;
258 
259 	printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
260 			   " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
261 			port,
262 			addr.addr_bytes[0], addr.addr_bytes[1],
263 			addr.addr_bytes[2], addr.addr_bytes[3],
264 			addr.addr_bytes[4], addr.addr_bytes[5]);
265 
266 	/* Enable RX in promiscuous mode for the Ethernet device. */
267 	retval = rte_eth_promiscuous_enable(port);
268 	if (retval != 0)
269 		return retval;
270 
271 	return 0;
272 }
273 /* >8 End of initializing a given port. */
274 
275 /*
276  * The lcore main. This is the main thread that does the work, reading from
277  * an input port classifying the packets and writing to an output port.
278  */
279 
280 /* Classifying the packets. 8< */
281 static __rte_noreturn void
282 lcore_main(struct flow_classifier *cls_app)
283 {
284 	uint16_t port;
285 	int ret;
286 	int i = 0;
287 
288 	ret = rte_flow_classify_table_entry_delete(cls_app->cls,
289 			rules[7]);
290 	if (ret)
291 		printf("table_entry_delete failed [7] %d\n\n", ret);
292 	else
293 		printf("table_entry_delete succeeded [7]\n\n");
294 
295 	/*
296 	 * Check that the port is on the same NUMA node as the polling thread
297 	 * for best performance.
298 	 */
299 	RTE_ETH_FOREACH_DEV(port)
300 		if (rte_eth_dev_socket_id(port) >= 0 &&
301 			rte_eth_dev_socket_id(port) != (int)rte_socket_id()) {
302 			printf("\n\n");
303 			printf("WARNING: port %u is on remote NUMA node\n",
304 			       port);
305 			printf("to polling thread.\n");
306 			printf("Performance will not be optimal.\n");
307 		}
308 	printf("\nCore %u forwarding packets. ", rte_lcore_id());
309 	printf("[Ctrl+C to quit]\n");
310 
311 	/* Run until the application is quit or killed. 8< */
312 	for (;;) {
313 		/*
314 		 * Receive packets on a port, classify them and forward them
315 		 * on the paired port.
316 		 * The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
317 		 */
318 		RTE_ETH_FOREACH_DEV(port) {
319 			/* Get burst of RX packets, from first port of pair. */
320 			struct rte_mbuf *bufs[BURST_SIZE];
321 			const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
322 					bufs, BURST_SIZE);
323 
324 			if (unlikely(nb_rx == 0))
325 				continue;
326 
327 			for (i = 0; i < MAX_NUM_CLASSIFY; i++) {
328 				if (rules[i]) {
329 					ret = rte_flow_classifier_query(
330 						cls_app->cls,
331 						bufs, nb_rx, rules[i],
332 						&classify_stats);
333 					if (ret)
334 						printf(
335 							"rule [%d] query failed ret [%d]\n\n",
336 							i, ret);
337 					else {
338 						printf(
339 						"rule[%d] count=%"PRIu64"\n",
340 						i, ntuple_stats.counter1);
341 
342 						printf("proto = %d\n",
343 						ntuple_stats.ipv4_5tuple.proto);
344 					}
345 				}
346 			}
347 
348 			/* Send burst of TX packets, to second port of pair. */
349 			const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
350 					bufs, nb_rx);
351 
352 			/* Free any unsent packets. */
353 			if (unlikely(nb_tx < nb_rx)) {
354 				uint16_t buf;
355 
356 				for (buf = nb_tx; buf < nb_rx; buf++)
357 					rte_pktmbuf_free(bufs[buf]);
358 			}
359 		}
360 	}
361 	/* >8 End of main loop. */
362 }
363 /* >8 End of lcore main. */
364 
365 /*
366  * Parse IPv4 5 tuple rules file, ipv4_rules_file.txt.
367  * Expected format:
368  * <src_ipv4_addr>'/'<masklen> <space> \
369  * <dst_ipv4_addr>'/'<masklen> <space> \
370  * <src_port> <space> ":" <src_port_mask> <space> \
371  * <dst_port> <space> ":" <dst_port_mask> <space> \
372  * <proto>'/'<proto_mask> <space> \
373  * <priority>
374  */
375 
376 static int
377 get_cb_field(char **in, uint32_t *fd, int base, unsigned long lim,
378 		char dlm)
379 {
380 	unsigned long val;
381 	char *end;
382 
383 	errno = 0;
384 	val = strtoul(*in, &end, base);
385 	if (errno != 0 || end[0] != dlm || val > lim)
386 		return -EINVAL;
387 	*fd = (uint32_t)val;
388 	*in = end + 1;
389 	return 0;
390 }
391 
392 static int
393 parse_ipv4_net(char *in, uint32_t *addr, uint32_t *mask_len)
394 {
395 	uint32_t a, b, c, d, m;
396 
397 	if (get_cb_field(&in, &a, 0, UINT8_MAX, '.'))
398 		return -EINVAL;
399 	if (get_cb_field(&in, &b, 0, UINT8_MAX, '.'))
400 		return -EINVAL;
401 	if (get_cb_field(&in, &c, 0, UINT8_MAX, '.'))
402 		return -EINVAL;
403 	if (get_cb_field(&in, &d, 0, UINT8_MAX, '/'))
404 		return -EINVAL;
405 	if (get_cb_field(&in, &m, 0, sizeof(uint32_t) * CHAR_BIT, 0))
406 		return -EINVAL;
407 
408 	addr[0] = RTE_IPV4(a, b, c, d);
409 	mask_len[0] = m;
410 	return 0;
411 }
412 
413 static int
414 parse_ipv4_5tuple_rule(char *str, struct rte_eth_ntuple_filter *ntuple_filter)
415 {
416 	int i, ret;
417 	char *s, *sp, *in[CB_FLD_NUM];
418 	static const char *dlm = " \t\n";
419 	int dim = CB_FLD_NUM;
420 	uint32_t temp;
421 
422 	s = str;
423 	for (i = 0; i != dim; i++, s = NULL) {
424 		in[i] = strtok_r(s, dlm, &sp);
425 		if (in[i] == NULL)
426 			return -EINVAL;
427 	}
428 
429 	ret = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
430 			&ntuple_filter->src_ip,
431 			&ntuple_filter->src_ip_mask);
432 	if (ret != 0) {
433 		flow_classify_log("failed to read source address/mask: %s\n",
434 			in[CB_FLD_SRC_ADDR]);
435 		return ret;
436 	}
437 
438 	ret = parse_ipv4_net(in[CB_FLD_DST_ADDR],
439 			&ntuple_filter->dst_ip,
440 			&ntuple_filter->dst_ip_mask);
441 	if (ret != 0) {
442 		flow_classify_log("failed to read source address/mask: %s\n",
443 			in[CB_FLD_DST_ADDR]);
444 		return ret;
445 	}
446 
447 	if (get_cb_field(&in[CB_FLD_SRC_PORT], &temp, 0, UINT16_MAX, 0))
448 		return -EINVAL;
449 	ntuple_filter->src_port = (uint16_t)temp;
450 
451 	if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
452 			sizeof(cb_port_delim)) != 0)
453 		return -EINVAL;
454 
455 	if (get_cb_field(&in[CB_FLD_SRC_PORT_MASK], &temp, 0, UINT16_MAX, 0))
456 		return -EINVAL;
457 	ntuple_filter->src_port_mask = (uint16_t)temp;
458 
459 	if (get_cb_field(&in[CB_FLD_DST_PORT], &temp, 0, UINT16_MAX, 0))
460 		return -EINVAL;
461 	ntuple_filter->dst_port = (uint16_t)temp;
462 
463 	if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
464 			sizeof(cb_port_delim)) != 0)
465 		return -EINVAL;
466 
467 	if (get_cb_field(&in[CB_FLD_DST_PORT_MASK], &temp, 0, UINT16_MAX, 0))
468 		return -EINVAL;
469 	ntuple_filter->dst_port_mask = (uint16_t)temp;
470 
471 	if (get_cb_field(&in[CB_FLD_PROTO], &temp, 0, UINT8_MAX, '/'))
472 		return -EINVAL;
473 	ntuple_filter->proto = (uint8_t)temp;
474 
475 	if (get_cb_field(&in[CB_FLD_PROTO], &temp, 0, UINT8_MAX, 0))
476 		return -EINVAL;
477 	ntuple_filter->proto_mask = (uint8_t)temp;
478 
479 	if (get_cb_field(&in[CB_FLD_PRIORITY], &temp, 0, UINT16_MAX, 0))
480 		return -EINVAL;
481 	ntuple_filter->priority = (uint16_t)temp;
482 	if (ntuple_filter->priority > FLOW_CLASSIFY_MAX_PRIORITY)
483 		ret = -EINVAL;
484 
485 	return ret;
486 }
487 
488 /* Bypass comment and empty lines */
489 static inline int
490 is_bypass_line(char *buff)
491 {
492 	int i = 0;
493 
494 	/* comment line */
495 	if (buff[0] == COMMENT_LEAD_CHAR)
496 		return 1;
497 	/* empty line */
498 	while (buff[i] != '\0') {
499 		if (!isspace(buff[i]))
500 			return 0;
501 		i++;
502 	}
503 	return 1;
504 }
505 
506 static uint32_t
507 convert_depth_to_bitmask(uint32_t depth_val)
508 {
509 	uint32_t bitmask = 0;
510 	int i, j;
511 
512 	for (i = depth_val, j = 0; i > 0; i--, j++)
513 		bitmask |= (1 << (31 - j));
514 	return bitmask;
515 }
516 
517 static int
518 add_classify_rule(struct rte_eth_ntuple_filter *ntuple_filter,
519 		struct flow_classifier *cls_app)
520 {
521 	int ret = -1;
522 	int key_found;
523 	struct rte_flow_error error;
524 	struct rte_flow_item_ipv4 ipv4_spec;
525 	struct rte_flow_item_ipv4 ipv4_mask;
526 	struct rte_flow_item ipv4_udp_item;
527 	struct rte_flow_item ipv4_tcp_item;
528 	struct rte_flow_item ipv4_sctp_item;
529 	struct rte_flow_item_udp udp_spec;
530 	struct rte_flow_item_udp udp_mask;
531 	struct rte_flow_item udp_item;
532 	struct rte_flow_item_tcp tcp_spec;
533 	struct rte_flow_item_tcp tcp_mask;
534 	struct rte_flow_item tcp_item;
535 	struct rte_flow_item_sctp sctp_spec;
536 	struct rte_flow_item_sctp sctp_mask;
537 	struct rte_flow_item sctp_item;
538 	struct rte_flow_item pattern_ipv4_5tuple[4];
539 	struct rte_flow_classify_rule *rule;
540 	uint8_t ipv4_proto;
541 
542 	if (num_classify_rules >= MAX_NUM_CLASSIFY) {
543 		printf(
544 			"\nINFO:  classify rule capacity %d reached\n",
545 			num_classify_rules);
546 		return ret;
547 	}
548 
549 	/* set up parameters for validate and add */
550 	memset(&ipv4_spec, 0, sizeof(ipv4_spec));
551 	ipv4_spec.hdr.next_proto_id = ntuple_filter->proto;
552 	ipv4_spec.hdr.src_addr = ntuple_filter->src_ip;
553 	ipv4_spec.hdr.dst_addr = ntuple_filter->dst_ip;
554 	ipv4_proto = ipv4_spec.hdr.next_proto_id;
555 
556 	memset(&ipv4_mask, 0, sizeof(ipv4_mask));
557 	ipv4_mask.hdr.next_proto_id = ntuple_filter->proto_mask;
558 	ipv4_mask.hdr.src_addr = ntuple_filter->src_ip_mask;
559 	ipv4_mask.hdr.src_addr =
560 		convert_depth_to_bitmask(ipv4_mask.hdr.src_addr);
561 	ipv4_mask.hdr.dst_addr = ntuple_filter->dst_ip_mask;
562 	ipv4_mask.hdr.dst_addr =
563 		convert_depth_to_bitmask(ipv4_mask.hdr.dst_addr);
564 
565 	switch (ipv4_proto) {
566 	case IPPROTO_UDP:
567 		ipv4_udp_item.type = RTE_FLOW_ITEM_TYPE_IPV4;
568 		ipv4_udp_item.spec = &ipv4_spec;
569 		ipv4_udp_item.mask = &ipv4_mask;
570 		ipv4_udp_item.last = NULL;
571 
572 		udp_spec.hdr.src_port = ntuple_filter->src_port;
573 		udp_spec.hdr.dst_port = ntuple_filter->dst_port;
574 		udp_spec.hdr.dgram_len = 0;
575 		udp_spec.hdr.dgram_cksum = 0;
576 
577 		udp_mask.hdr.src_port = ntuple_filter->src_port_mask;
578 		udp_mask.hdr.dst_port = ntuple_filter->dst_port_mask;
579 		udp_mask.hdr.dgram_len = 0;
580 		udp_mask.hdr.dgram_cksum = 0;
581 
582 		udp_item.type = RTE_FLOW_ITEM_TYPE_UDP;
583 		udp_item.spec = &udp_spec;
584 		udp_item.mask = &udp_mask;
585 		udp_item.last = NULL;
586 
587 		attr.priority = ntuple_filter->priority;
588 		pattern_ipv4_5tuple[1] = ipv4_udp_item;
589 		pattern_ipv4_5tuple[2] = udp_item;
590 		break;
591 	case IPPROTO_TCP:
592 		ipv4_tcp_item.type = RTE_FLOW_ITEM_TYPE_IPV4;
593 		ipv4_tcp_item.spec = &ipv4_spec;
594 		ipv4_tcp_item.mask = &ipv4_mask;
595 		ipv4_tcp_item.last = NULL;
596 
597 		memset(&tcp_spec, 0, sizeof(tcp_spec));
598 		tcp_spec.hdr.src_port = ntuple_filter->src_port;
599 		tcp_spec.hdr.dst_port = ntuple_filter->dst_port;
600 
601 		memset(&tcp_mask, 0, sizeof(tcp_mask));
602 		tcp_mask.hdr.src_port = ntuple_filter->src_port_mask;
603 		tcp_mask.hdr.dst_port = ntuple_filter->dst_port_mask;
604 
605 		tcp_item.type = RTE_FLOW_ITEM_TYPE_TCP;
606 		tcp_item.spec = &tcp_spec;
607 		tcp_item.mask = &tcp_mask;
608 		tcp_item.last = NULL;
609 
610 		attr.priority = ntuple_filter->priority;
611 		pattern_ipv4_5tuple[1] = ipv4_tcp_item;
612 		pattern_ipv4_5tuple[2] = tcp_item;
613 		break;
614 	case IPPROTO_SCTP:
615 		ipv4_sctp_item.type = RTE_FLOW_ITEM_TYPE_IPV4;
616 		ipv4_sctp_item.spec = &ipv4_spec;
617 		ipv4_sctp_item.mask = &ipv4_mask;
618 		ipv4_sctp_item.last = NULL;
619 
620 		sctp_spec.hdr.src_port = ntuple_filter->src_port;
621 		sctp_spec.hdr.dst_port = ntuple_filter->dst_port;
622 		sctp_spec.hdr.cksum = 0;
623 		sctp_spec.hdr.tag = 0;
624 
625 		sctp_mask.hdr.src_port = ntuple_filter->src_port_mask;
626 		sctp_mask.hdr.dst_port = ntuple_filter->dst_port_mask;
627 		sctp_mask.hdr.cksum = 0;
628 		sctp_mask.hdr.tag = 0;
629 
630 		sctp_item.type = RTE_FLOW_ITEM_TYPE_SCTP;
631 		sctp_item.spec = &sctp_spec;
632 		sctp_item.mask = &sctp_mask;
633 		sctp_item.last = NULL;
634 
635 		attr.priority = ntuple_filter->priority;
636 		pattern_ipv4_5tuple[1] = ipv4_sctp_item;
637 		pattern_ipv4_5tuple[2] = sctp_item;
638 		break;
639 	default:
640 		return ret;
641 	}
642 
643 	attr.ingress = 1;
644 	pattern_ipv4_5tuple[0] = eth_item;
645 	pattern_ipv4_5tuple[3] = end_item;
646 	actions[0] = count_action;
647 	actions[1] = end_action;
648 
649 	/* Validate and add rule */
650 	ret = rte_flow_classify_validate(cls_app->cls, &attr,
651 			pattern_ipv4_5tuple, actions, &error);
652 	if (ret) {
653 		printf("table entry validate failed ipv4_proto = %u\n",
654 			ipv4_proto);
655 		return ret;
656 	}
657 
658 	rule = rte_flow_classify_table_entry_add(
659 			cls_app->cls, &attr, pattern_ipv4_5tuple,
660 			actions, &key_found, &error);
661 	if (rule == NULL) {
662 		printf("table entry add failed ipv4_proto = %u\n",
663 			ipv4_proto);
664 		ret = -1;
665 		return ret;
666 	}
667 
668 	rules[num_classify_rules] = rule;
669 	num_classify_rules++;
670 	return 0;
671 }
672 
673 /* Reads file and calls the add_classify_rule function. 8< */
674 static int
675 add_rules(const char *rule_path, struct flow_classifier *cls_app)
676 {
677 	FILE *fh;
678 	char buff[LINE_MAX];
679 	unsigned int i = 0;
680 	unsigned int total_num = 0;
681 	struct rte_eth_ntuple_filter ntuple_filter;
682 	int ret;
683 
684 	fh = fopen(rule_path, "rb");
685 	if (fh == NULL)
686 		rte_exit(EXIT_FAILURE, "%s: fopen %s failed\n", __func__,
687 			rule_path);
688 
689 	ret = fseek(fh, 0, SEEK_SET);
690 	if (ret)
691 		rte_exit(EXIT_FAILURE, "%s: fseek %d failed\n", __func__,
692 			ret);
693 
694 	i = 0;
695 	while (fgets(buff, LINE_MAX, fh) != NULL) {
696 		i++;
697 
698 		if (is_bypass_line(buff))
699 			continue;
700 
701 		if (total_num >= FLOW_CLASSIFY_MAX_RULE_NUM - 1) {
702 			printf("\nINFO: classify rule capacity %d reached\n",
703 				total_num);
704 			break;
705 		}
706 
707 		if (parse_ipv4_5tuple_rule(buff, &ntuple_filter) != 0)
708 			rte_exit(EXIT_FAILURE,
709 				"%s Line %u: parse rules error\n",
710 				rule_path, i);
711 
712 		if (add_classify_rule(&ntuple_filter, cls_app) != 0)
713 			rte_exit(EXIT_FAILURE, "add rule error\n");
714 
715 		total_num++;
716 	}
717 
718 	fclose(fh);
719 	return 0;
720 }
721 /* >8 End of add_rules. */
722 
723 /* display usage */
724 static void
725 print_usage(const char *prgname)
726 {
727 	printf("%s usage:\n", prgname);
728 	printf("[EAL options] --  --"OPTION_RULE_IPV4"=FILE: ");
729 	printf("specify the ipv4 rules file.\n");
730 	printf("Each rule occupies one line in the file.\n");
731 }
732 
733 /* Parse the argument given in the command line of the application */
734 static int
735 parse_args(int argc, char **argv)
736 {
737 	int opt, ret;
738 	char **argvopt;
739 	int option_index;
740 	char *prgname = argv[0];
741 	static struct option lgopts[] = {
742 		{OPTION_RULE_IPV4, 1, 0, 0},
743 		{NULL, 0, 0, 0}
744 	};
745 
746 	argvopt = argv;
747 
748 	while ((opt = getopt_long(argc, argvopt, "",
749 				lgopts, &option_index)) != EOF) {
750 
751 		switch (opt) {
752 		/* long options */
753 		case 0:
754 			if (!strncmp(lgopts[option_index].name,
755 					OPTION_RULE_IPV4,
756 					sizeof(OPTION_RULE_IPV4)))
757 				parm_config.rule_ipv4_name = optarg;
758 			break;
759 		default:
760 			print_usage(prgname);
761 			return -1;
762 		}
763 	}
764 
765 	if (optind >= 0)
766 		argv[optind-1] = prgname;
767 
768 	ret = optind-1;
769 	optind = 1; /* reset getopt lib */
770 	return ret;
771 }
772 
773 /*
774  * The main function, which does initialization and calls the lcore_main
775  * function.
776  */
777 int
778 main(int argc, char *argv[])
779 {
780 	struct rte_mempool *mbuf_pool;
781 	uint16_t nb_ports;
782 	uint16_t portid;
783 	int ret;
784 	int socket_id;
785 	struct rte_table_acl_params table_acl_params;
786 	struct rte_flow_classify_table_params cls_table_params;
787 	struct flow_classifier *cls_app;
788 	struct rte_flow_classifier_params cls_params;
789 	uint32_t size;
790 
791 	/* Initialize the Environment Abstraction Layer (EAL). 8< */
792 	ret = rte_eal_init(argc, argv);
793 	if (ret < 0)
794 		rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
795 	/* >8 End of initialization of EAL. */
796 
797 	argc -= ret;
798 	argv += ret;
799 
800 	/* Parse application arguments (after the EAL ones). 8< */
801 	ret = parse_args(argc, argv);
802 	if (ret < 0)
803 		rte_exit(EXIT_FAILURE, "Invalid flow_classify parameters\n");
804 	/* >8 End of parse application arguments. */
805 
806 	/* Check that there is an even number of ports to send/receive on. */
807 	nb_ports = rte_eth_dev_count_avail();
808 	if (nb_ports < 2 || (nb_ports & 1))
809 		rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
810 
811 	/* Creates a new mempool in memory to hold the mbufs. 8< */
812 	mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
813 		MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
814 	/* >8 End of creation of new mempool in memory. */
815 
816 	if (mbuf_pool == NULL)
817 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
818 
819 	/* Initialize all ports. 8< */
820 	RTE_ETH_FOREACH_DEV(portid)
821 		if (port_init(portid, mbuf_pool) != 0)
822 			rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
823 					portid);
824 	/* >8 End of initialization of all ports. */
825 
826 	if (rte_lcore_count() > 1)
827 		printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
828 
829 	socket_id = rte_eth_dev_socket_id(0);
830 
831 	/* Memory allocation. 8< */
832 	size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct flow_classifier_acl));
833 	cls_app = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
834 	if (cls_app == NULL)
835 		rte_exit(EXIT_FAILURE, "Cannot allocate classifier memory\n");
836 
837 	cls_params.name = "flow_classifier";
838 	cls_params.socket_id = socket_id;
839 
840 	cls_app->cls = rte_flow_classifier_create(&cls_params);
841 	if (cls_app->cls == NULL) {
842 		rte_free(cls_app);
843 		rte_exit(EXIT_FAILURE, "Cannot create classifier\n");
844 	}
845 
846 	/* initialise ACL table params */
847 	table_acl_params.name = "table_acl_ipv4_5tuple";
848 	table_acl_params.n_rules = FLOW_CLASSIFY_MAX_RULE_NUM;
849 	table_acl_params.n_rule_fields = RTE_DIM(ipv4_defs);
850 	memcpy(table_acl_params.field_format, ipv4_defs, sizeof(ipv4_defs));
851 
852 	/* initialise table create params */
853 	cls_table_params.ops = &rte_table_acl_ops;
854 	cls_table_params.arg_create = &table_acl_params;
855 	cls_table_params.type = RTE_FLOW_CLASSIFY_TABLE_ACL_IP4_5TUPLE;
856 
857 	ret = rte_flow_classify_table_create(cls_app->cls, &cls_table_params);
858 	if (ret) {
859 		rte_flow_classifier_free(cls_app->cls);
860 		rte_free(cls_app);
861 		rte_exit(EXIT_FAILURE, "Failed to create classifier table\n");
862 	}
863 	/* >8 End of initialization of table create params. */
864 
865 	/* read file of IPv4 5 tuple rules and initialize parameters
866 	 * for rte_flow_classify_validate and rte_flow_classify_table_entry_add
867 	 * API's.
868 	 */
869 
870 	/* Read file of IPv4 tuple rules. 8< */
871 	if (add_rules(parm_config.rule_ipv4_name, cls_app)) {
872 		rte_flow_classifier_free(cls_app->cls);
873 		rte_free(cls_app);
874 		rte_exit(EXIT_FAILURE, "Failed to add rules\n");
875 	}
876 	/* >8 End of reading file of IPv4 5 tuple rules. */
877 
878 	/* Call lcore_main on the main core only. */
879 	lcore_main(cls_app);
880 
881 	/* clean up the EAL */
882 	rte_eal_cleanup();
883 
884 	return 0;
885 }
886