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