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 (rte_eth_dev_is_valid_port(port))
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 	uint16_t port;
263 	int ret;
264 	int i = 0;
265 
266 	ret = rte_flow_classify_table_entry_delete(cls_app->cls,
267 			rules[7]);
268 	if (ret)
269 		printf("table_entry_delete failed [7] %d\n\n", ret);
270 	else
271 		printf("table_entry_delete succeeded [7]\n\n");
272 
273 	/*
274 	 * Check that the port is on the same NUMA node as the polling thread
275 	 * for best performance.
276 	 */
277 	RTE_ETH_FOREACH_DEV(port)
278 		if (rte_eth_dev_socket_id(port) > 0 &&
279 			rte_eth_dev_socket_id(port) != (int)rte_socket_id()) {
280 			printf("\n\n");
281 			printf("WARNING: port %u is on remote NUMA node\n",
282 			       port);
283 			printf("to polling thread.\n");
284 			printf("Performance will not be optimal.\n");
285 		}
286 	printf("\nCore %u forwarding packets. ", rte_lcore_id());
287 	printf("[Ctrl+C to quit]\n");
288 
289 	/* Run until the application is quit or killed. */
290 	for (;;) {
291 		/*
292 		 * Receive packets on a port, classify them and forward them
293 		 * on the paired port.
294 		 * The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
295 		 */
296 		RTE_ETH_FOREACH_DEV(port) {
297 			/* Get burst of RX packets, from first port of pair. */
298 			struct rte_mbuf *bufs[BURST_SIZE];
299 			const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
300 					bufs, BURST_SIZE);
301 
302 			if (unlikely(nb_rx == 0))
303 				continue;
304 
305 			for (i = 0; i < MAX_NUM_CLASSIFY; i++) {
306 				if (rules[i]) {
307 					ret = rte_flow_classifier_query(
308 						cls_app->cls,
309 						bufs, nb_rx, rules[i],
310 						&classify_stats);
311 					if (ret)
312 						printf(
313 							"rule [%d] query failed ret [%d]\n\n",
314 							i, ret);
315 					else {
316 						printf(
317 						"rule[%d] count=%"PRIu64"\n",
318 						i, ntuple_stats.counter1);
319 
320 						printf("proto = %d\n",
321 						ntuple_stats.ipv4_5tuple.proto);
322 					}
323 				}
324 			}
325 
326 			/* Send burst of TX packets, to second port of pair. */
327 			const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
328 					bufs, nb_rx);
329 
330 			/* Free any unsent packets. */
331 			if (unlikely(nb_tx < nb_rx)) {
332 				uint16_t buf;
333 
334 				for (buf = nb_tx; buf < nb_rx; buf++)
335 					rte_pktmbuf_free(bufs[buf]);
336 			}
337 		}
338 	}
339 }
340 
341 /*
342  * Parse IPv4 5 tuple rules file, ipv4_rules_file.txt.
343  * Expected format:
344  * <src_ipv4_addr>'/'<masklen> <space> \
345  * <dst_ipv4_addr>'/'<masklen> <space> \
346  * <src_port> <space> ":" <src_port_mask> <space> \
347  * <dst_port> <space> ":" <dst_port_mask> <space> \
348  * <proto>'/'<proto_mask> <space> \
349  * <priority>
350  */
351 
352 static int
353 get_cb_field(char **in, uint32_t *fd, int base, unsigned long lim,
354 		char dlm)
355 {
356 	unsigned long val;
357 	char *end;
358 
359 	errno = 0;
360 	val = strtoul(*in, &end, base);
361 	if (errno != 0 || end[0] != dlm || val > lim)
362 		return -EINVAL;
363 	*fd = (uint32_t)val;
364 	*in = end + 1;
365 	return 0;
366 }
367 
368 static int
369 parse_ipv4_net(char *in, uint32_t *addr, uint32_t *mask_len)
370 {
371 	uint32_t a, b, c, d, m;
372 
373 	if (get_cb_field(&in, &a, 0, UINT8_MAX, '.'))
374 		return -EINVAL;
375 	if (get_cb_field(&in, &b, 0, UINT8_MAX, '.'))
376 		return -EINVAL;
377 	if (get_cb_field(&in, &c, 0, UINT8_MAX, '.'))
378 		return -EINVAL;
379 	if (get_cb_field(&in, &d, 0, UINT8_MAX, '/'))
380 		return -EINVAL;
381 	if (get_cb_field(&in, &m, 0, sizeof(uint32_t) * CHAR_BIT, 0))
382 		return -EINVAL;
383 
384 	addr[0] = IPv4(a, b, c, d);
385 	mask_len[0] = m;
386 	return 0;
387 }
388 
389 static int
390 parse_ipv4_5tuple_rule(char *str, struct rte_eth_ntuple_filter *ntuple_filter)
391 {
392 	int i, ret;
393 	char *s, *sp, *in[CB_FLD_NUM];
394 	static const char *dlm = " \t\n";
395 	int dim = CB_FLD_NUM;
396 	uint32_t temp;
397 
398 	s = str;
399 	for (i = 0; i != dim; i++, s = NULL) {
400 		in[i] = strtok_r(s, dlm, &sp);
401 		if (in[i] == NULL)
402 			return -EINVAL;
403 	}
404 
405 	ret = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
406 			&ntuple_filter->src_ip,
407 			&ntuple_filter->src_ip_mask);
408 	if (ret != 0) {
409 		flow_classify_log("failed to read source address/mask: %s\n",
410 			in[CB_FLD_SRC_ADDR]);
411 		return ret;
412 	}
413 
414 	ret = parse_ipv4_net(in[CB_FLD_DST_ADDR],
415 			&ntuple_filter->dst_ip,
416 			&ntuple_filter->dst_ip_mask);
417 	if (ret != 0) {
418 		flow_classify_log("failed to read source address/mask: %s\n",
419 			in[CB_FLD_DST_ADDR]);
420 		return ret;
421 	}
422 
423 	if (get_cb_field(&in[CB_FLD_SRC_PORT], &temp, 0, UINT16_MAX, 0))
424 		return -EINVAL;
425 	ntuple_filter->src_port = (uint16_t)temp;
426 
427 	if (strncmp(in[CB_FLD_SRC_PORT_DLM], cb_port_delim,
428 			sizeof(cb_port_delim)) != 0)
429 		return -EINVAL;
430 
431 	if (get_cb_field(&in[CB_FLD_SRC_PORT_MASK], &temp, 0, UINT16_MAX, 0))
432 		return -EINVAL;
433 	ntuple_filter->src_port_mask = (uint16_t)temp;
434 
435 	if (get_cb_field(&in[CB_FLD_DST_PORT], &temp, 0, UINT16_MAX, 0))
436 		return -EINVAL;
437 	ntuple_filter->dst_port = (uint16_t)temp;
438 
439 	if (strncmp(in[CB_FLD_DST_PORT_DLM], cb_port_delim,
440 			sizeof(cb_port_delim)) != 0)
441 		return -EINVAL;
442 
443 	if (get_cb_field(&in[CB_FLD_DST_PORT_MASK], &temp, 0, UINT16_MAX, 0))
444 		return -EINVAL;
445 	ntuple_filter->dst_port_mask = (uint16_t)temp;
446 
447 	if (get_cb_field(&in[CB_FLD_PROTO], &temp, 0, UINT8_MAX, '/'))
448 		return -EINVAL;
449 	ntuple_filter->proto = (uint8_t)temp;
450 
451 	if (get_cb_field(&in[CB_FLD_PROTO], &temp, 0, UINT8_MAX, 0))
452 		return -EINVAL;
453 	ntuple_filter->proto_mask = (uint8_t)temp;
454 
455 	if (get_cb_field(&in[CB_FLD_PRIORITY], &temp, 0, UINT16_MAX, 0))
456 		return -EINVAL;
457 	ntuple_filter->priority = (uint16_t)temp;
458 	if (ntuple_filter->priority > FLOW_CLASSIFY_MAX_PRIORITY)
459 		ret = -EINVAL;
460 
461 	return ret;
462 }
463 
464 /* Bypass comment and empty lines */
465 static inline int
466 is_bypass_line(char *buff)
467 {
468 	int i = 0;
469 
470 	/* comment line */
471 	if (buff[0] == COMMENT_LEAD_CHAR)
472 		return 1;
473 	/* empty line */
474 	while (buff[i] != '\0') {
475 		if (!isspace(buff[i]))
476 			return 0;
477 		i++;
478 	}
479 	return 1;
480 }
481 
482 static uint32_t
483 convert_depth_to_bitmask(uint32_t depth_val)
484 {
485 	uint32_t bitmask = 0;
486 	int i, j;
487 
488 	for (i = depth_val, j = 0; i > 0; i--, j++)
489 		bitmask |= (1 << (31 - j));
490 	return bitmask;
491 }
492 
493 static int
494 add_classify_rule(struct rte_eth_ntuple_filter *ntuple_filter,
495 		struct flow_classifier *cls_app)
496 {
497 	int ret = -1;
498 	int key_found;
499 	struct rte_flow_error error;
500 	struct rte_flow_item_ipv4 ipv4_spec;
501 	struct rte_flow_item_ipv4 ipv4_mask;
502 	struct rte_flow_item ipv4_udp_item;
503 	struct rte_flow_item ipv4_tcp_item;
504 	struct rte_flow_item ipv4_sctp_item;
505 	struct rte_flow_item_udp udp_spec;
506 	struct rte_flow_item_udp udp_mask;
507 	struct rte_flow_item udp_item;
508 	struct rte_flow_item_tcp tcp_spec;
509 	struct rte_flow_item_tcp tcp_mask;
510 	struct rte_flow_item tcp_item;
511 	struct rte_flow_item_sctp sctp_spec;
512 	struct rte_flow_item_sctp sctp_mask;
513 	struct rte_flow_item sctp_item;
514 	struct rte_flow_item pattern_ipv4_5tuple[4];
515 	struct rte_flow_classify_rule *rule;
516 	uint8_t ipv4_proto;
517 
518 	if (num_classify_rules >= MAX_NUM_CLASSIFY) {
519 		printf(
520 			"\nINFO:  classify rule capacity %d reached\n",
521 			num_classify_rules);
522 		return ret;
523 	}
524 
525 	/* set up parameters for validate and add */
526 	memset(&ipv4_spec, 0, sizeof(ipv4_spec));
527 	ipv4_spec.hdr.next_proto_id = ntuple_filter->proto;
528 	ipv4_spec.hdr.src_addr = ntuple_filter->src_ip;
529 	ipv4_spec.hdr.dst_addr = ntuple_filter->dst_ip;
530 	ipv4_proto = ipv4_spec.hdr.next_proto_id;
531 
532 	memset(&ipv4_mask, 0, sizeof(ipv4_mask));
533 	ipv4_mask.hdr.next_proto_id = ntuple_filter->proto_mask;
534 	ipv4_mask.hdr.src_addr = ntuple_filter->src_ip_mask;
535 	ipv4_mask.hdr.src_addr =
536 		convert_depth_to_bitmask(ipv4_mask.hdr.src_addr);
537 	ipv4_mask.hdr.dst_addr = ntuple_filter->dst_ip_mask;
538 	ipv4_mask.hdr.dst_addr =
539 		convert_depth_to_bitmask(ipv4_mask.hdr.dst_addr);
540 
541 	switch (ipv4_proto) {
542 	case IPPROTO_UDP:
543 		ipv4_udp_item.type = RTE_FLOW_ITEM_TYPE_IPV4;
544 		ipv4_udp_item.spec = &ipv4_spec;
545 		ipv4_udp_item.mask = &ipv4_mask;
546 		ipv4_udp_item.last = NULL;
547 
548 		udp_spec.hdr.src_port = ntuple_filter->src_port;
549 		udp_spec.hdr.dst_port = ntuple_filter->dst_port;
550 		udp_spec.hdr.dgram_len = 0;
551 		udp_spec.hdr.dgram_cksum = 0;
552 
553 		udp_mask.hdr.src_port = ntuple_filter->src_port_mask;
554 		udp_mask.hdr.dst_port = ntuple_filter->dst_port_mask;
555 		udp_mask.hdr.dgram_len = 0;
556 		udp_mask.hdr.dgram_cksum = 0;
557 
558 		udp_item.type = RTE_FLOW_ITEM_TYPE_UDP;
559 		udp_item.spec = &udp_spec;
560 		udp_item.mask = &udp_mask;
561 		udp_item.last = NULL;
562 
563 		attr.priority = ntuple_filter->priority;
564 		pattern_ipv4_5tuple[1] = ipv4_udp_item;
565 		pattern_ipv4_5tuple[2] = udp_item;
566 		break;
567 	case IPPROTO_TCP:
568 		ipv4_tcp_item.type = RTE_FLOW_ITEM_TYPE_IPV4;
569 		ipv4_tcp_item.spec = &ipv4_spec;
570 		ipv4_tcp_item.mask = &ipv4_mask;
571 		ipv4_tcp_item.last = NULL;
572 
573 		memset(&tcp_spec, 0, sizeof(tcp_spec));
574 		tcp_spec.hdr.src_port = ntuple_filter->src_port;
575 		tcp_spec.hdr.dst_port = ntuple_filter->dst_port;
576 
577 		memset(&tcp_mask, 0, sizeof(tcp_mask));
578 		tcp_mask.hdr.src_port = ntuple_filter->src_port_mask;
579 		tcp_mask.hdr.dst_port = ntuple_filter->dst_port_mask;
580 
581 		tcp_item.type = RTE_FLOW_ITEM_TYPE_TCP;
582 		tcp_item.spec = &tcp_spec;
583 		tcp_item.mask = &tcp_mask;
584 		tcp_item.last = NULL;
585 
586 		attr.priority = ntuple_filter->priority;
587 		pattern_ipv4_5tuple[1] = ipv4_tcp_item;
588 		pattern_ipv4_5tuple[2] = tcp_item;
589 		break;
590 	case IPPROTO_SCTP:
591 		ipv4_sctp_item.type = RTE_FLOW_ITEM_TYPE_IPV4;
592 		ipv4_sctp_item.spec = &ipv4_spec;
593 		ipv4_sctp_item.mask = &ipv4_mask;
594 		ipv4_sctp_item.last = NULL;
595 
596 		sctp_spec.hdr.src_port = ntuple_filter->src_port;
597 		sctp_spec.hdr.dst_port = ntuple_filter->dst_port;
598 		sctp_spec.hdr.cksum = 0;
599 		sctp_spec.hdr.tag = 0;
600 
601 		sctp_mask.hdr.src_port = ntuple_filter->src_port_mask;
602 		sctp_mask.hdr.dst_port = ntuple_filter->dst_port_mask;
603 		sctp_mask.hdr.cksum = 0;
604 		sctp_mask.hdr.tag = 0;
605 
606 		sctp_item.type = RTE_FLOW_ITEM_TYPE_SCTP;
607 		sctp_item.spec = &sctp_spec;
608 		sctp_item.mask = &sctp_mask;
609 		sctp_item.last = NULL;
610 
611 		attr.priority = ntuple_filter->priority;
612 		pattern_ipv4_5tuple[1] = ipv4_sctp_item;
613 		pattern_ipv4_5tuple[2] = sctp_item;
614 		break;
615 	default:
616 		return ret;
617 	}
618 
619 	attr.ingress = 1;
620 	pattern_ipv4_5tuple[0] = eth_item;
621 	pattern_ipv4_5tuple[3] = end_item;
622 	actions[0] = count_action;
623 	actions[1] = end_action;
624 
625 	/* Validate and add rule */
626 	ret = rte_flow_classify_validate(cls_app->cls, &attr,
627 			pattern_ipv4_5tuple, actions, &error);
628 	if (ret) {
629 		printf("table entry validate failed ipv4_proto = %u\n",
630 			ipv4_proto);
631 		return ret;
632 	}
633 
634 	rule = rte_flow_classify_table_entry_add(
635 			cls_app->cls, &attr, pattern_ipv4_5tuple,
636 			actions, &key_found, &error);
637 	if (rule == NULL) {
638 		printf("table entry add failed ipv4_proto = %u\n",
639 			ipv4_proto);
640 		ret = -1;
641 		return ret;
642 	}
643 
644 	rules[num_classify_rules] = rule;
645 	num_classify_rules++;
646 	return 0;
647 }
648 
649 static int
650 add_rules(const char *rule_path, struct flow_classifier *cls_app)
651 {
652 	FILE *fh;
653 	char buff[LINE_MAX];
654 	unsigned int i = 0;
655 	unsigned int total_num = 0;
656 	struct rte_eth_ntuple_filter ntuple_filter;
657 	int ret;
658 
659 	fh = fopen(rule_path, "rb");
660 	if (fh == NULL)
661 		rte_exit(EXIT_FAILURE, "%s: fopen %s failed\n", __func__,
662 			rule_path);
663 
664 	ret = fseek(fh, 0, SEEK_SET);
665 	if (ret)
666 		rte_exit(EXIT_FAILURE, "%s: fseek %d failed\n", __func__,
667 			ret);
668 
669 	i = 0;
670 	while (fgets(buff, LINE_MAX, fh) != NULL) {
671 		i++;
672 
673 		if (is_bypass_line(buff))
674 			continue;
675 
676 		if (total_num >= FLOW_CLASSIFY_MAX_RULE_NUM - 1) {
677 			printf("\nINFO: classify rule capacity %d reached\n",
678 				total_num);
679 			break;
680 		}
681 
682 		if (parse_ipv4_5tuple_rule(buff, &ntuple_filter) != 0)
683 			rte_exit(EXIT_FAILURE,
684 				"%s Line %u: parse rules error\n",
685 				rule_path, i);
686 
687 		if (add_classify_rule(&ntuple_filter, cls_app) != 0)
688 			rte_exit(EXIT_FAILURE, "add rule error\n");
689 
690 		total_num++;
691 	}
692 
693 	fclose(fh);
694 	return 0;
695 }
696 
697 /* display usage */
698 static void
699 print_usage(const char *prgname)
700 {
701 	printf("%s usage:\n", prgname);
702 	printf("[EAL options] --  --"OPTION_RULE_IPV4"=FILE: ");
703 	printf("specify the ipv4 rules file.\n");
704 	printf("Each rule occupies one line in the file.\n");
705 }
706 
707 /* Parse the argument given in the command line of the application */
708 static int
709 parse_args(int argc, char **argv)
710 {
711 	int opt, ret;
712 	char **argvopt;
713 	int option_index;
714 	char *prgname = argv[0];
715 	static struct option lgopts[] = {
716 		{OPTION_RULE_IPV4, 1, 0, 0},
717 		{NULL, 0, 0, 0}
718 	};
719 
720 	argvopt = argv;
721 
722 	while ((opt = getopt_long(argc, argvopt, "",
723 				lgopts, &option_index)) != EOF) {
724 
725 		switch (opt) {
726 		/* long options */
727 		case 0:
728 			if (!strncmp(lgopts[option_index].name,
729 					OPTION_RULE_IPV4,
730 					sizeof(OPTION_RULE_IPV4)))
731 				parm_config.rule_ipv4_name = optarg;
732 			break;
733 		default:
734 			print_usage(prgname);
735 			return -1;
736 		}
737 	}
738 
739 	if (optind >= 0)
740 		argv[optind-1] = prgname;
741 
742 	ret = optind-1;
743 	optind = 1; /* reset getopt lib */
744 	return ret;
745 }
746 
747 /*
748  * The main function, which does initialization and calls the lcore_main
749  * function.
750  */
751 int
752 main(int argc, char *argv[])
753 {
754 	struct rte_mempool *mbuf_pool;
755 	uint16_t nb_ports;
756 	uint16_t portid;
757 	int ret;
758 	int socket_id;
759 	struct rte_table_acl_params table_acl_params;
760 	struct rte_flow_classify_table_params cls_table_params;
761 	struct flow_classifier *cls_app;
762 	struct rte_flow_classifier_params cls_params;
763 	uint32_t size;
764 
765 	/* Initialize the Environment Abstraction Layer (EAL). */
766 	ret = rte_eal_init(argc, argv);
767 	if (ret < 0)
768 		rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
769 
770 	argc -= ret;
771 	argv += ret;
772 
773 	/* parse application arguments (after the EAL ones) */
774 	ret = parse_args(argc, argv);
775 	if (ret < 0)
776 		rte_exit(EXIT_FAILURE, "Invalid flow_classify parameters\n");
777 
778 	/* Check that there is an even number of ports to send/receive on. */
779 	nb_ports = rte_eth_dev_count_avail();
780 	if (nb_ports < 2 || (nb_ports & 1))
781 		rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
782 
783 	/* Creates a new mempool in memory to hold the mbufs. */
784 	mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
785 		MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
786 
787 	if (mbuf_pool == NULL)
788 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
789 
790 	/* Initialize all ports. */
791 	RTE_ETH_FOREACH_DEV(portid)
792 		if (port_init(portid, mbuf_pool) != 0)
793 			rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
794 					portid);
795 
796 	if (rte_lcore_count() > 1)
797 		printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
798 
799 	socket_id = rte_eth_dev_socket_id(0);
800 
801 	/* Memory allocation */
802 	size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct flow_classifier_acl));
803 	cls_app = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
804 	if (cls_app == NULL)
805 		rte_exit(EXIT_FAILURE, "Cannot allocate classifier memory\n");
806 
807 	cls_params.name = "flow_classifier";
808 	cls_params.socket_id = socket_id;
809 
810 	cls_app->cls = rte_flow_classifier_create(&cls_params);
811 	if (cls_app->cls == NULL) {
812 		rte_free(cls_app);
813 		rte_exit(EXIT_FAILURE, "Cannot create classifier\n");
814 	}
815 
816 	/* initialise ACL table params */
817 	table_acl_params.name = "table_acl_ipv4_5tuple";
818 	table_acl_params.n_rules = FLOW_CLASSIFY_MAX_RULE_NUM;
819 	table_acl_params.n_rule_fields = RTE_DIM(ipv4_defs);
820 	memcpy(table_acl_params.field_format, ipv4_defs, sizeof(ipv4_defs));
821 
822 	/* initialise table create params */
823 	cls_table_params.ops = &rte_table_acl_ops;
824 	cls_table_params.arg_create = &table_acl_params;
825 	cls_table_params.type = RTE_FLOW_CLASSIFY_TABLE_ACL_IP4_5TUPLE;
826 
827 	ret = rte_flow_classify_table_create(cls_app->cls, &cls_table_params);
828 	if (ret) {
829 		rte_flow_classifier_free(cls_app->cls);
830 		rte_free(cls_app);
831 		rte_exit(EXIT_FAILURE, "Failed to create classifier table\n");
832 	}
833 
834 	/* read file of IPv4 5 tuple rules and initialize parameters
835 	 * for rte_flow_classify_validate and rte_flow_classify_table_entry_add
836 	 * API's.
837 	 */
838 	if (add_rules(parm_config.rule_ipv4_name, cls_app)) {
839 		rte_flow_classifier_free(cls_app->cls);
840 		rte_free(cls_app);
841 		rte_exit(EXIT_FAILURE, "Failed to add rules\n");
842 	}
843 
844 	/* Call lcore_main on the master core only. */
845 	lcore_main(cls_app);
846 
847 	return 0;
848 }
849