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