xref: /dpdk/app/test/test_flow_classify.c (revision e59b75ff)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4 
5 #include <string.h>
6 #include <errno.h>
7 
8 #include "test.h"
9 
10 #include <rte_string_fns.h>
11 #include <rte_mbuf.h>
12 #include <rte_byteorder.h>
13 #include <rte_ip.h>
14 #include <rte_acl.h>
15 #include <rte_common.h>
16 #include <rte_table_acl.h>
17 #include <rte_flow.h>
18 #include <rte_flow_classify.h>
19 
20 #include "packet_burst_generator.h"
21 #include "test_flow_classify.h"
22 
23 
24 #define FLOW_CLASSIFY_MAX_RULE_NUM 100
25 #define MAX_PKT_BURST              32
26 #define NB_SOCKETS                 1
27 #define MEMPOOL_CACHE_SIZE         256
28 #define MBUF_SIZE                  512
29 #define NB_MBUF                    512
30 
31 /* test UDP, TCP and SCTP packets */
32 static struct rte_mempool *mbufpool[NB_SOCKETS];
33 static struct rte_mbuf *bufs[MAX_PKT_BURST];
34 
35 static struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
36 	/* first input field - always one byte long. */
37 	{
38 		.type = RTE_ACL_FIELD_TYPE_BITMASK,
39 		.size = sizeof(uint8_t),
40 		.field_index = PROTO_FIELD_IPV4,
41 		.input_index = PROTO_INPUT_IPV4,
42 		.offset = sizeof(struct ether_hdr) +
43 			offsetof(struct ipv4_hdr, next_proto_id),
44 	},
45 	/* next input field (IPv4 source address) - 4 consecutive bytes. */
46 	{
47 		/* rte_flow uses a bit mask for IPv4 addresses */
48 		.type = RTE_ACL_FIELD_TYPE_BITMASK,
49 		.size = sizeof(uint32_t),
50 		.field_index = SRC_FIELD_IPV4,
51 		.input_index = SRC_INPUT_IPV4,
52 		.offset = sizeof(struct ether_hdr) +
53 			offsetof(struct ipv4_hdr, src_addr),
54 	},
55 	/* next input field (IPv4 destination address) - 4 consecutive bytes. */
56 	{
57 		/* rte_flow uses a bit mask for IPv4 addresses */
58 		.type = RTE_ACL_FIELD_TYPE_BITMASK,
59 		.size = sizeof(uint32_t),
60 		.field_index = DST_FIELD_IPV4,
61 		.input_index = DST_INPUT_IPV4,
62 		.offset = sizeof(struct ether_hdr) +
63 			offsetof(struct ipv4_hdr, dst_addr),
64 	},
65 	/*
66 	 * Next 2 fields (src & dst ports) form 4 consecutive bytes.
67 	 * They share the same input index.
68 	 */
69 	{
70 		/* rte_flow uses a bit mask for protocol ports */
71 		.type = RTE_ACL_FIELD_TYPE_BITMASK,
72 		.size = sizeof(uint16_t),
73 		.field_index = SRCP_FIELD_IPV4,
74 		.input_index = SRCP_DESTP_INPUT_IPV4,
75 		.offset = sizeof(struct ether_hdr) +
76 			sizeof(struct ipv4_hdr) +
77 			offsetof(struct tcp_hdr, src_port),
78 	},
79 	{
80 		/* rte_flow uses a bit mask for protocol ports */
81 		.type = RTE_ACL_FIELD_TYPE_BITMASK,
82 		.size = sizeof(uint16_t),
83 		.field_index = DSTP_FIELD_IPV4,
84 		.input_index = SRCP_DESTP_INPUT_IPV4,
85 		.offset = sizeof(struct ether_hdr) +
86 			sizeof(struct ipv4_hdr) +
87 			offsetof(struct tcp_hdr, dst_port),
88 	},
89 };
90 
91 /* parameters for rte_flow_classify_validate and rte_flow_classify_create */
92 
93 /* test UDP pattern:
94  * "eth / ipv4 src spec 2.2.2.3 src mask 255.255.255.00 dst spec 2.2.2.7
95  *  dst mask 255.255.255.00 / udp src is 32 dst is 33 / end"
96  */
97 static struct rte_flow_item_ipv4 ipv4_udp_spec_1 = {
98 	{ 0, 0, 0, 0, 0, 0, IPPROTO_UDP, 0, IPv4(2, 2, 2, 3), IPv4(2, 2, 2, 7)}
99 };
100 static const struct rte_flow_item_ipv4 ipv4_mask_24 = {
101 	.hdr = {
102 		.next_proto_id = 0xff,
103 		.src_addr = 0xffffff00,
104 		.dst_addr = 0xffffff00,
105 	},
106 };
107 static struct rte_flow_item_udp udp_spec_1 = {
108 	{ 32, 33, 0, 0 }
109 };
110 
111 static struct rte_flow_item  eth_item = { RTE_FLOW_ITEM_TYPE_ETH,
112 	0, 0, 0 };
113 static struct rte_flow_item  eth_item_bad = { -1, 0, 0, 0 };
114 
115 static struct rte_flow_item  ipv4_udp_item_1 = { RTE_FLOW_ITEM_TYPE_IPV4,
116 	&ipv4_udp_spec_1, 0, &ipv4_mask_24};
117 static struct rte_flow_item  ipv4_udp_item_bad = { RTE_FLOW_ITEM_TYPE_IPV4,
118 	NULL, 0, NULL};
119 
120 static struct rte_flow_item  udp_item_1 = { RTE_FLOW_ITEM_TYPE_UDP,
121 	&udp_spec_1, 0, &rte_flow_item_udp_mask};
122 static struct rte_flow_item  udp_item_bad = { RTE_FLOW_ITEM_TYPE_UDP,
123 	NULL, 0, NULL};
124 
125 static struct rte_flow_item  end_item = { RTE_FLOW_ITEM_TYPE_END,
126 	0, 0, 0 };
127 static struct rte_flow_item  end_item_bad = { -1, 0, 0, 0 };
128 
129 /* test TCP pattern:
130  * "eth / ipv4 src spec 1.2.3.4 src mask 255.255.255.00 dst spec 5.6.7.8
131  *  dst mask 255.255.255.00 / tcp src is 16 dst is 17 / end"
132  */
133 static struct rte_flow_item_ipv4 ipv4_tcp_spec_1 = {
134 	{ 0, 0, 0, 0, 0, 0, IPPROTO_TCP, 0, IPv4(1, 2, 3, 4), IPv4(5, 6, 7, 8)}
135 };
136 
137 static struct rte_flow_item_tcp tcp_spec_1 = {
138 	{ 16, 17, 0, 0, 0, 0, 0, 0, 0}
139 };
140 
141 static struct rte_flow_item  ipv4_tcp_item_1 = { RTE_FLOW_ITEM_TYPE_IPV4,
142 	&ipv4_tcp_spec_1, 0, &ipv4_mask_24};
143 
144 static struct rte_flow_item  tcp_item_1 = { RTE_FLOW_ITEM_TYPE_TCP,
145 	&tcp_spec_1, 0, &rte_flow_item_tcp_mask};
146 
147 /* test SCTP pattern:
148  * "eth / ipv4 src spec 1.2.3.4 src mask 255.255.255.00 dst spec 5.6.7.8
149  *  dst mask 255.255.255.00 / sctp src is 16 dst is 17/ end"
150  */
151 static struct rte_flow_item_ipv4 ipv4_sctp_spec_1 = {
152 	{ 0, 0, 0, 0, 0, 0, IPPROTO_SCTP, 0, IPv4(11, 12, 13, 14),
153 	IPv4(15, 16, 17, 18)}
154 };
155 
156 static struct rte_flow_item_sctp sctp_spec_1 = {
157 	{ 10, 11, 0, 0}
158 };
159 
160 static struct rte_flow_item  ipv4_sctp_item_1 = { RTE_FLOW_ITEM_TYPE_IPV4,
161 	&ipv4_sctp_spec_1, 0, &ipv4_mask_24};
162 
163 static struct rte_flow_item  sctp_item_1 = { RTE_FLOW_ITEM_TYPE_SCTP,
164 	&sctp_spec_1, 0, &rte_flow_item_sctp_mask};
165 
166 
167 /* test actions:
168  * "actions count / end"
169  */
170 static struct rte_flow_query_count count = {
171 	.reset = 1,
172 	.hits_set = 1,
173 	.bytes_set = 1,
174 	.hits = 0,
175 	.bytes = 0,
176 };
177 static struct rte_flow_action count_action = { RTE_FLOW_ACTION_TYPE_COUNT,
178 	&count};
179 static struct rte_flow_action count_action_bad = { -1, 0};
180 
181 static struct rte_flow_action end_action = { RTE_FLOW_ACTION_TYPE_END, 0};
182 static struct rte_flow_action end_action_bad =	{ -1, 0};
183 
184 static struct rte_flow_action actions[2];
185 
186 /* test attributes */
187 static struct rte_flow_attr attr;
188 
189 /* test error */
190 static struct rte_flow_error error;
191 
192 /* test pattern */
193 static struct rte_flow_item  pattern[4];
194 
195 /* flow classify data for UDP burst */
196 static struct rte_flow_classify_ipv4_5tuple_stats udp_ntuple_stats;
197 static struct rte_flow_classify_stats udp_classify_stats = {
198 		.stats = (void *)&udp_ntuple_stats
199 };
200 
201 /* flow classify data for TCP burst */
202 static struct rte_flow_classify_ipv4_5tuple_stats tcp_ntuple_stats;
203 static struct rte_flow_classify_stats tcp_classify_stats = {
204 		.stats = (void *)&tcp_ntuple_stats
205 };
206 
207 /* flow classify data for SCTP burst */
208 static struct rte_flow_classify_ipv4_5tuple_stats sctp_ntuple_stats;
209 static struct rte_flow_classify_stats sctp_classify_stats = {
210 		.stats = (void *)&sctp_ntuple_stats
211 };
212 
213 struct flow_classifier_acl *cls;
214 
215 struct flow_classifier_acl {
216 	struct rte_flow_classifier *cls;
217 } __rte_cache_aligned;
218 
219 /*
220  * test functions by passing invalid or
221  * non-workable parameters.
222  */
223 static int
224 test_invalid_parameters(void)
225 {
226 	struct rte_flow_classify_rule *rule;
227 	int ret;
228 
229 	ret = rte_flow_classify_validate(NULL, NULL, NULL, NULL, NULL);
230 	if (!ret) {
231 		printf("Line %i: rte_flow_classify_validate",
232 			__LINE__);
233 		printf(" with NULL param should have failed!\n");
234 		return -1;
235 	}
236 
237 	rule = rte_flow_classify_table_entry_add(NULL, NULL, NULL, NULL,
238 			NULL, NULL);
239 	if (rule) {
240 		printf("Line %i: flow_classifier_table_entry_add", __LINE__);
241 		printf(" with NULL param should have failed!\n");
242 		return -1;
243 	}
244 
245 	ret = rte_flow_classify_table_entry_delete(NULL, NULL);
246 	if (!ret) {
247 		printf("Line %i: rte_flow_classify_table_entry_delete",
248 			__LINE__);
249 		printf(" with NULL param should have failed!\n");
250 		return -1;
251 	}
252 
253 	ret = rte_flow_classifier_query(NULL, NULL, 0, NULL, NULL);
254 	if (!ret) {
255 		printf("Line %i: flow_classifier_query", __LINE__);
256 		printf(" with NULL param should have failed!\n");
257 		return -1;
258 	}
259 
260 	rule = rte_flow_classify_table_entry_add(NULL, NULL, NULL, NULL,
261 		NULL, &error);
262 	if (rule) {
263 		printf("Line %i: flow_classify_table_entry_add ", __LINE__);
264 		printf("with NULL param should have failed!\n");
265 		return -1;
266 	}
267 
268 	ret = rte_flow_classify_table_entry_delete(NULL, NULL);
269 	if (!ret) {
270 		printf("Line %i: rte_flow_classify_table_entry_delete",
271 			__LINE__);
272 		printf("with NULL param should have failed!\n");
273 		return -1;
274 	}
275 
276 	ret = rte_flow_classifier_query(NULL, NULL, 0, NULL, NULL);
277 	if (!ret) {
278 		printf("Line %i: flow_classifier_query", __LINE__);
279 		printf(" with NULL param should have failed!\n");
280 		return -1;
281 	}
282 	return 0;
283 }
284 
285 static int
286 test_valid_parameters(void)
287 {
288 	struct rte_flow_classify_rule *rule;
289 	int ret;
290 	int key_found;
291 
292 	/*
293 	 * set up parameters for rte_flow_classify_validate,
294 	 * rte_flow_classify_table_entry_add and
295 	 * rte_flow_classify_table_entry_delete
296 	 */
297 
298 	attr.ingress = 1;
299 	attr.priority = 1;
300 	pattern[0] = eth_item;
301 	pattern[1] = ipv4_udp_item_1;
302 	pattern[2] = udp_item_1;
303 	pattern[3] = end_item;
304 	actions[0] = count_action;
305 	actions[1] = end_action;
306 
307 	ret = rte_flow_classify_validate(cls->cls, &attr, pattern,
308 			actions, &error);
309 	if (ret) {
310 		printf("Line %i: rte_flow_classify_validate",
311 			__LINE__);
312 		printf(" should not have failed!\n");
313 		return -1;
314 	}
315 	rule = rte_flow_classify_table_entry_add(cls->cls, &attr, pattern,
316 			actions, &key_found, &error);
317 
318 	if (!rule) {
319 		printf("Line %i: flow_classify_table_entry_add", __LINE__);
320 		printf(" should not have failed!\n");
321 		return -1;
322 	}
323 
324 	ret = rte_flow_classify_table_entry_delete(cls->cls, rule);
325 	if (ret) {
326 		printf("Line %i: rte_flow_classify_table_entry_delete",
327 			__LINE__);
328 		printf(" should not have failed!\n");
329 		return -1;
330 	}
331 	return 0;
332 }
333 
334 static int
335 test_invalid_patterns(void)
336 {
337 	struct rte_flow_classify_rule *rule;
338 	int ret;
339 	int key_found;
340 
341 	/*
342 	 * set up parameters for rte_flow_classify_validate,
343 	 * rte_flow_classify_table_entry_add and
344 	 * rte_flow_classify_table_entry_delete
345 	 */
346 
347 	attr.ingress = 1;
348 	attr.priority = 1;
349 	pattern[0] = eth_item_bad;
350 	pattern[1] = ipv4_udp_item_1;
351 	pattern[2] = udp_item_1;
352 	pattern[3] = end_item;
353 	actions[0] = count_action;
354 	actions[1] = end_action;
355 
356 	pattern[0] = eth_item;
357 	pattern[1] = ipv4_udp_item_bad;
358 
359 	ret = rte_flow_classify_validate(cls->cls, &attr, pattern,
360 			actions, &error);
361 	if (!ret) {
362 		printf("Line %i: rte_flow_classify_validate", __LINE__);
363 		printf(" should have failed!\n");
364 		return -1;
365 	}
366 
367 	rule = rte_flow_classify_table_entry_add(cls->cls, &attr, pattern,
368 			actions, &key_found, &error);
369 	if (rule) {
370 		printf("Line %i: flow_classify_table_entry_add", __LINE__);
371 		printf(" should have failed!\n");
372 		return -1;
373 	}
374 
375 	ret = rte_flow_classify_table_entry_delete(cls->cls, rule);
376 	if (!ret) {
377 		printf("Line %i: rte_flow_classify_table_entry_delete",
378 			__LINE__);
379 		printf(" should have failed!\n");
380 		return -1;
381 	}
382 
383 	pattern[1] = ipv4_udp_item_1;
384 	pattern[2] = udp_item_bad;
385 	pattern[3] = end_item_bad;
386 
387 	ret = rte_flow_classify_validate(cls->cls, &attr, pattern,
388 			actions, &error);
389 	if (!ret) {
390 		printf("Line %i: rte_flow_classify_validate", __LINE__);
391 		printf(" should have failed!\n");
392 		return -1;
393 	}
394 
395 	rule = rte_flow_classify_table_entry_add(cls->cls, &attr, pattern,
396 			actions, &key_found, &error);
397 	if (rule) {
398 		printf("Line %i: flow_classify_table_entry_add", __LINE__);
399 		printf(" should have failed!\n");
400 		return -1;
401 	}
402 
403 	ret = rte_flow_classify_table_entry_delete(cls->cls, rule);
404 	if (!ret) {
405 		printf("Line %i: rte_flow_classify_table_entry_delete",
406 			__LINE__);
407 		printf(" should have failed!\n");
408 		return -1;
409 	}
410 	return 0;
411 }
412 
413 static int
414 test_invalid_actions(void)
415 {
416 	struct rte_flow_classify_rule *rule;
417 	int ret;
418 	int key_found;
419 
420 	/*
421 	 * set up parameters for rte_flow_classify_validate,
422 	 * rte_flow_classify_table_entry_add and
423 	 * rte_flow_classify_table_entry_delete
424 	 */
425 
426 	attr.ingress = 1;
427 	attr.priority = 1;
428 	pattern[0] = eth_item;
429 	pattern[1] = ipv4_udp_item_1;
430 	pattern[2] = udp_item_1;
431 	pattern[3] = end_item;
432 	actions[0] = count_action_bad;
433 	actions[1] = end_action;
434 
435 	ret = rte_flow_classify_validate(cls->cls, &attr, pattern,
436 			actions, &error);
437 	if (!ret) {
438 		printf("Line %i: rte_flow_classify_validate", __LINE__);
439 		printf(" should have failed!\n");
440 		return -1;
441 	}
442 
443 	rule = rte_flow_classify_table_entry_add(cls->cls, &attr, pattern,
444 			actions, &key_found, &error);
445 	if (rule) {
446 		printf("Line %i: flow_classify_table_entry_add", __LINE__);
447 		printf(" should have failed!\n");
448 		return -1;
449 	}
450 
451 	ret = rte_flow_classify_table_entry_delete(cls->cls, rule);
452 	if (!ret) {
453 		printf("Line %i: rte_flow_classify_table_entry_delete",
454 			__LINE__);
455 		printf(" should have failed!\n");
456 		return -1;
457 	}
458 
459 	actions[0] = count_action;
460 	actions[1] = end_action_bad;
461 
462 	ret = rte_flow_classify_validate(cls->cls, &attr, pattern,
463 			actions, &error);
464 	if (!ret) {
465 		printf("Line %i: rte_flow_classify_validate", __LINE__);
466 		printf(" should have failed!\n");
467 		return -1;
468 	}
469 
470 	rule = rte_flow_classify_table_entry_add(cls->cls, &attr, pattern,
471 			actions, &key_found, &error);
472 	if (rule) {
473 		printf("Line %i: flow_classify_table_entry_add", __LINE__);
474 		printf(" should have failed!\n");
475 		return -1;
476 	}
477 
478 	ret = rte_flow_classify_table_entry_delete(cls->cls, rule);
479 	if (!ret) {
480 		printf("Line %i: rte_flow_classify_table_entry_delete",
481 			__LINE__);
482 		printf("should have failed!\n");
483 		return -1;
484 	}
485 	return 0;
486 }
487 
488 static int
489 init_ipv4_udp_traffic(struct rte_mempool *mp,
490 	     struct rte_mbuf **pkts_burst, uint32_t burst_size)
491 {
492 	struct ether_hdr pkt_eth_hdr;
493 	struct ipv4_hdr pkt_ipv4_hdr;
494 	struct udp_hdr pkt_udp_hdr;
495 	uint32_t src_addr = IPV4_ADDR(2, 2, 2, 3);
496 	uint32_t dst_addr = IPV4_ADDR(2, 2, 2, 7);
497 	uint16_t src_port = 32;
498 	uint16_t dst_port = 33;
499 	uint16_t pktlen;
500 
501 	static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
502 	static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
503 
504 	printf("Set up IPv4 UDP traffic\n");
505 	initialize_eth_header(&pkt_eth_hdr,
506 		(struct ether_addr *)src_mac,
507 		(struct ether_addr *)dst_mac, ETHER_TYPE_IPv4, 0, 0);
508 	pktlen = (uint16_t)(sizeof(struct ether_hdr));
509 	printf("ETH  pktlen %u\n", pktlen);
510 
511 	pktlen = initialize_ipv4_header(&pkt_ipv4_hdr, src_addr, dst_addr,
512 					pktlen);
513 	printf("ETH + IPv4 pktlen %u\n", pktlen);
514 
515 	pktlen = initialize_udp_header(&pkt_udp_hdr, src_port, dst_port,
516 					pktlen);
517 	printf("ETH + IPv4 + UDP pktlen %u\n\n", pktlen);
518 
519 	return generate_packet_burst(mp, pkts_burst, &pkt_eth_hdr,
520 				     0, &pkt_ipv4_hdr, 1,
521 				     &pkt_udp_hdr, burst_size,
522 				     PACKET_BURST_GEN_PKT_LEN, 1);
523 }
524 
525 static int
526 init_ipv4_tcp_traffic(struct rte_mempool *mp,
527 	     struct rte_mbuf **pkts_burst, uint32_t burst_size)
528 {
529 	struct ether_hdr pkt_eth_hdr;
530 	struct ipv4_hdr pkt_ipv4_hdr;
531 	struct tcp_hdr pkt_tcp_hdr;
532 	uint32_t src_addr = IPV4_ADDR(1, 2, 3, 4);
533 	uint32_t dst_addr = IPV4_ADDR(5, 6, 7, 8);
534 	uint16_t src_port = 16;
535 	uint16_t dst_port = 17;
536 	uint16_t pktlen;
537 
538 	static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
539 	static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
540 
541 	printf("Set up IPv4 TCP traffic\n");
542 	initialize_eth_header(&pkt_eth_hdr,
543 		(struct ether_addr *)src_mac,
544 		(struct ether_addr *)dst_mac, ETHER_TYPE_IPv4, 0, 0);
545 	pktlen = (uint16_t)(sizeof(struct ether_hdr));
546 	printf("ETH  pktlen %u\n", pktlen);
547 
548 	pktlen = initialize_ipv4_header_proto(&pkt_ipv4_hdr, src_addr,
549 					dst_addr, pktlen, IPPROTO_TCP);
550 	printf("ETH + IPv4 pktlen %u\n", pktlen);
551 
552 	pktlen = initialize_tcp_header(&pkt_tcp_hdr, src_port, dst_port,
553 					pktlen);
554 	printf("ETH + IPv4 + TCP pktlen %u\n\n", pktlen);
555 
556 	return generate_packet_burst_proto(mp, pkts_burst, &pkt_eth_hdr,
557 					0, &pkt_ipv4_hdr, 1, IPPROTO_TCP,
558 					&pkt_tcp_hdr, burst_size,
559 					PACKET_BURST_GEN_PKT_LEN, 1);
560 }
561 
562 static int
563 init_ipv4_sctp_traffic(struct rte_mempool *mp,
564 	     struct rte_mbuf **pkts_burst, uint32_t burst_size)
565 {
566 	struct ether_hdr pkt_eth_hdr;
567 	struct ipv4_hdr pkt_ipv4_hdr;
568 	struct sctp_hdr pkt_sctp_hdr;
569 	uint32_t src_addr = IPV4_ADDR(11, 12, 13, 14);
570 	uint32_t dst_addr = IPV4_ADDR(15, 16, 17, 18);
571 	uint16_t src_port = 10;
572 	uint16_t dst_port = 11;
573 	uint16_t pktlen;
574 
575 	static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
576 	static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
577 
578 	printf("Set up IPv4 SCTP traffic\n");
579 	initialize_eth_header(&pkt_eth_hdr,
580 		(struct ether_addr *)src_mac,
581 		(struct ether_addr *)dst_mac, ETHER_TYPE_IPv4, 0, 0);
582 	pktlen = (uint16_t)(sizeof(struct ether_hdr));
583 	printf("ETH  pktlen %u\n", pktlen);
584 
585 	pktlen = initialize_ipv4_header_proto(&pkt_ipv4_hdr, src_addr,
586 					dst_addr, pktlen, IPPROTO_SCTP);
587 	printf("ETH + IPv4 pktlen %u\n", pktlen);
588 
589 	pktlen = initialize_sctp_header(&pkt_sctp_hdr, src_port, dst_port,
590 					pktlen);
591 	printf("ETH + IPv4 + SCTP pktlen %u\n\n", pktlen);
592 
593 	return generate_packet_burst_proto(mp, pkts_burst, &pkt_eth_hdr,
594 					0, &pkt_ipv4_hdr, 1, IPPROTO_SCTP,
595 					&pkt_sctp_hdr, burst_size,
596 					PACKET_BURST_GEN_PKT_LEN, 1);
597 }
598 
599 static int
600 init_mbufpool(void)
601 {
602 	int socketid;
603 	int ret = 0;
604 	unsigned int lcore_id;
605 	char s[64];
606 
607 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
608 		if (rte_lcore_is_enabled(lcore_id) == 0)
609 			continue;
610 
611 		socketid = rte_lcore_to_socket_id(lcore_id);
612 		if (socketid >= NB_SOCKETS) {
613 			printf(
614 				"Socket %d of lcore %u is out of range %d\n",
615 				socketid, lcore_id, NB_SOCKETS);
616 			ret = -1;
617 			break;
618 		}
619 		if (mbufpool[socketid] == NULL) {
620 			snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
621 			mbufpool[socketid] =
622 				rte_pktmbuf_pool_create(s, NB_MBUF,
623 					MEMPOOL_CACHE_SIZE, 0, MBUF_SIZE,
624 					socketid);
625 			if (mbufpool[socketid]) {
626 				printf("Allocated mbuf pool on socket %d\n",
627 					socketid);
628 			} else {
629 				printf("Cannot init mbuf pool on socket %d\n",
630 					socketid);
631 				ret = -ENOMEM;
632 				break;
633 			}
634 		}
635 	}
636 	return ret;
637 }
638 
639 static int
640 test_query_udp(void)
641 {
642 	struct rte_flow_error error;
643 	struct rte_flow_classify_rule *rule;
644 	int ret;
645 	int i;
646 	int key_found;
647 
648 	ret = init_ipv4_udp_traffic(mbufpool[0], bufs, MAX_PKT_BURST);
649 	if (ret != MAX_PKT_BURST) {
650 		printf("Line %i: init_udp_ipv4_traffic has failed!\n",
651 				__LINE__);
652 		return -1;
653 	}
654 
655 	for (i = 0; i < MAX_PKT_BURST; i++)
656 		bufs[i]->packet_type = RTE_PTYPE_L3_IPV4;
657 
658 	/*
659 	 * set up parameters for rte_flow_classify_validate,
660 	 * rte_flow_classify_table_entry_add and
661 	 * rte_flow_classify_table_entry_delete
662 	 */
663 
664 	attr.ingress = 1;
665 	attr.priority = 1;
666 	pattern[0] = eth_item;
667 	pattern[1] = ipv4_udp_item_1;
668 	pattern[2] = udp_item_1;
669 	pattern[3] = end_item;
670 	actions[0] = count_action;
671 	actions[1] = end_action;
672 
673 	ret = rte_flow_classify_validate(cls->cls, &attr, pattern,
674 			actions, &error);
675 	if (ret) {
676 		printf("Line %i: rte_flow_classify_validate", __LINE__);
677 		printf(" should not have failed!\n");
678 		return -1;
679 	}
680 
681 	rule = rte_flow_classify_table_entry_add(cls->cls, &attr, pattern,
682 			actions, &key_found, &error);
683 	if (!rule) {
684 		printf("Line %i: flow_classify_table_entry_add", __LINE__);
685 		printf(" should not have failed!\n");
686 		return -1;
687 	}
688 
689 	ret = rte_flow_classifier_query(cls->cls, bufs, MAX_PKT_BURST,
690 			rule, &udp_classify_stats);
691 	if (ret) {
692 		printf("Line %i: flow_classifier_query", __LINE__);
693 		printf(" should not have failed!\n");
694 		return -1;
695 	}
696 
697 	ret = rte_flow_classify_table_entry_delete(cls->cls, rule);
698 	if (ret) {
699 		printf("Line %i: rte_flow_classify_table_entry_delete",
700 			__LINE__);
701 		printf(" should not have failed!\n");
702 		return -1;
703 	}
704 	return 0;
705 }
706 
707 static int
708 test_query_tcp(void)
709 {
710 	struct rte_flow_classify_rule *rule;
711 	int ret;
712 	int i;
713 	int key_found;
714 
715 	ret = init_ipv4_tcp_traffic(mbufpool[0], bufs, MAX_PKT_BURST);
716 	if (ret != MAX_PKT_BURST) {
717 		printf("Line %i: init_ipv4_tcp_traffic has failed!\n",
718 				__LINE__);
719 		return -1;
720 	}
721 
722 	for (i = 0; i < MAX_PKT_BURST; i++)
723 		bufs[i]->packet_type = RTE_PTYPE_L3_IPV4;
724 
725 	/*
726 	 * set up parameters for rte_flow_classify_validate,
727 	 * rte_flow_classify_table_entry_add and
728 	 * rte_flow_classify_table_entry_delete
729 	 */
730 
731 	attr.ingress = 1;
732 	attr.priority = 1;
733 	pattern[0] = eth_item;
734 	pattern[1] = ipv4_tcp_item_1;
735 	pattern[2] = tcp_item_1;
736 	pattern[3] = end_item;
737 	actions[0] = count_action;
738 	actions[1] = end_action;
739 
740 	ret = rte_flow_classify_validate(cls->cls, &attr, pattern,
741 			actions, &error);
742 	if (ret) {
743 		printf("Line %i: flow_classifier_query", __LINE__);
744 		printf(" should not have failed!\n");
745 		return -1;
746 	}
747 
748 	rule = rte_flow_classify_table_entry_add(cls->cls, &attr, pattern,
749 			actions, &key_found, &error);
750 	if (!rule) {
751 		printf("Line %i: flow_classify_table_entry_add", __LINE__);
752 		printf(" should not have failed!\n");
753 		return -1;
754 	}
755 
756 	ret = rte_flow_classifier_query(cls->cls, bufs, MAX_PKT_BURST,
757 			rule, &tcp_classify_stats);
758 	if (ret) {
759 		printf("Line %i: flow_classifier_query", __LINE__);
760 		printf(" should not have failed!\n");
761 		return -1;
762 	}
763 
764 	ret = rte_flow_classify_table_entry_delete(cls->cls, rule);
765 	if (ret) {
766 		printf("Line %i: rte_flow_classify_table_entry_delete",
767 			__LINE__);
768 		printf(" should not have failed!\n");
769 		return -1;
770 	}
771 	return 0;
772 }
773 
774 static int
775 test_query_sctp(void)
776 {
777 	struct rte_flow_classify_rule *rule;
778 	int ret;
779 	int i;
780 	int key_found;
781 
782 	ret = init_ipv4_sctp_traffic(mbufpool[0], bufs, MAX_PKT_BURST);
783 	if (ret != MAX_PKT_BURST) {
784 		printf("Line %i: init_ipv4_tcp_traffic has failed!\n",
785 			__LINE__);
786 		return -1;
787 	}
788 
789 	for (i = 0; i < MAX_PKT_BURST; i++)
790 		bufs[i]->packet_type = RTE_PTYPE_L3_IPV4;
791 
792 	/*
793 	 * set up parameters rte_flow_classify_validate,
794 	 * rte_flow_classify_table_entry_add and
795 	 * rte_flow_classify_table_entry_delete
796 	 */
797 
798 	attr.ingress = 1;
799 	attr.priority = 1;
800 	pattern[0] = eth_item;
801 	pattern[1] = ipv4_sctp_item_1;
802 	pattern[2] = sctp_item_1;
803 	pattern[3] = end_item;
804 	actions[0] = count_action;
805 	actions[1] = end_action;
806 
807 	ret = rte_flow_classify_validate(cls->cls, &attr, pattern,
808 			actions, &error);
809 	if (ret) {
810 		printf("Line %i: flow_classifier_query", __LINE__);
811 		printf(" should not have failed!\n");
812 		return -1;
813 	}
814 
815 	rule = rte_flow_classify_table_entry_add(cls->cls, &attr, pattern,
816 			actions, &key_found, &error);
817 	if (!rule) {
818 		printf("Line %i: flow_classify_table_entry_add", __LINE__);
819 		printf(" should not have failed!\n");
820 		return -1;
821 	}
822 
823 	ret = rte_flow_classifier_query(cls->cls, bufs, MAX_PKT_BURST,
824 			rule, &sctp_classify_stats);
825 	if (ret) {
826 		printf("Line %i: flow_classifier_query", __LINE__);
827 		printf(" should not have failed!\n");
828 		return -1;
829 	}
830 
831 	ret = rte_flow_classify_table_entry_delete(cls->cls, rule);
832 	if (ret) {
833 		printf("Line %i: rte_flow_classify_table_entry_delete",
834 			__LINE__);
835 		printf(" should not have failed!\n");
836 		return -1;
837 	}
838 	return 0;
839 }
840 
841 static int
842 test_flow_classify(void)
843 {
844 	struct rte_table_acl_params table_acl_params;
845 	struct rte_flow_classify_table_params cls_table_params;
846 	struct rte_flow_classifier_params cls_params;
847 	int ret;
848 	uint32_t size;
849 
850 	/* Memory allocation */
851 	size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct flow_classifier_acl));
852 	cls = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
853 
854 	cls_params.name = "flow_classifier";
855 	cls_params.socket_id = 0;
856 	cls->cls = rte_flow_classifier_create(&cls_params);
857 
858 	/* initialise ACL table params */
859 	table_acl_params.n_rule_fields = RTE_DIM(ipv4_defs);
860 	table_acl_params.name = "table_acl_ipv4_5tuple";
861 	table_acl_params.n_rules = FLOW_CLASSIFY_MAX_RULE_NUM;
862 	memcpy(table_acl_params.field_format, ipv4_defs, sizeof(ipv4_defs));
863 
864 	/* initialise table create params */
865 	cls_table_params.ops = &rte_table_acl_ops;
866 	cls_table_params.arg_create = &table_acl_params;
867 	cls_table_params.type = RTE_FLOW_CLASSIFY_TABLE_ACL_IP4_5TUPLE;
868 
869 	ret = rte_flow_classify_table_create(cls->cls, &cls_table_params);
870 	if (ret) {
871 		printf("Line %i: f_create has failed!\n", __LINE__);
872 		rte_flow_classifier_free(cls->cls);
873 		rte_free(cls);
874 		return TEST_FAILED;
875 	}
876 	printf("Created table_acl for for IPv4 five tuple packets\n");
877 
878 	ret = init_mbufpool();
879 	if (ret) {
880 		printf("Line %i: init_mbufpool has failed!\n", __LINE__);
881 		return TEST_FAILED;
882 	}
883 
884 	if (test_invalid_parameters() < 0)
885 		return TEST_FAILED;
886 	if (test_valid_parameters() < 0)
887 		return TEST_FAILED;
888 	if (test_invalid_patterns() < 0)
889 		return TEST_FAILED;
890 	if (test_invalid_actions() < 0)
891 		return TEST_FAILED;
892 	if (test_query_udp() < 0)
893 		return TEST_FAILED;
894 	if (test_query_tcp() < 0)
895 		return TEST_FAILED;
896 	if (test_query_sctp() < 0)
897 		return TEST_FAILED;
898 
899 	return TEST_SUCCESS;
900 }
901 
902 REGISTER_TEST_COMMAND(flow_classify_autotest, test_flow_classify);
903