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