mbuf: add a copy routine
[dpdk.git] / app / test / test_flow_classify.c
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 rte_ether_hdr) +
43                         offsetof(struct rte_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 rte_ether_hdr) +
53                         offsetof(struct rte_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 rte_ether_hdr) +
63                         offsetof(struct rte_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 rte_ether_hdr) +
76                         sizeof(struct rte_ipv4_hdr) +
77                         offsetof(struct rte_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 rte_ether_hdr) +
86                         sizeof(struct rte_ipv4_hdr) +
87                         offsetof(struct rte_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,
99           RTE_IPV4(2, 2, 2, 3), RTE_IPV4(2, 2, 2, 7)}
100 };
101 static const struct rte_flow_item_ipv4 ipv4_mask_24 = {
102         .hdr = {
103                 .next_proto_id = 0xff,
104                 .src_addr = 0xffffff00,
105                 .dst_addr = 0xffffff00,
106         },
107 };
108 static struct rte_flow_item_udp udp_spec_1 = {
109         { 32, 33, 0, 0 }
110 };
111
112 static struct rte_flow_item  eth_item = { RTE_FLOW_ITEM_TYPE_ETH,
113         0, 0, 0 };
114 static struct rte_flow_item  eth_item_bad = { -1, 0, 0, 0 };
115
116 static struct rte_flow_item  ipv4_udp_item_1 = { RTE_FLOW_ITEM_TYPE_IPV4,
117         &ipv4_udp_spec_1, 0, &ipv4_mask_24};
118 static struct rte_flow_item  ipv4_udp_item_bad = { RTE_FLOW_ITEM_TYPE_IPV4,
119         NULL, 0, NULL};
120
121 static struct rte_flow_item  udp_item_1 = { RTE_FLOW_ITEM_TYPE_UDP,
122         &udp_spec_1, 0, &rte_flow_item_udp_mask};
123 static struct rte_flow_item  udp_item_bad = { RTE_FLOW_ITEM_TYPE_UDP,
124         NULL, 0, NULL};
125
126 static struct rte_flow_item  end_item = { RTE_FLOW_ITEM_TYPE_END,
127         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,
135           RTE_IPV4(1, 2, 3, 4), RTE_IPV4(5, 6, 7, 8)}
136 };
137
138 static struct rte_flow_item_tcp tcp_spec_1 = {
139         { 16, 17, 0, 0, 0, 0, 0, 0, 0}
140 };
141
142 static struct rte_flow_item  ipv4_tcp_item_1 = { RTE_FLOW_ITEM_TYPE_IPV4,
143         &ipv4_tcp_spec_1, 0, &ipv4_mask_24};
144
145 static struct rte_flow_item  tcp_item_1 = { RTE_FLOW_ITEM_TYPE_TCP,
146         &tcp_spec_1, 0, &rte_flow_item_tcp_mask};
147
148 /* test SCTP pattern:
149  * "eth / ipv4 src spec 1.2.3.4 src mask 255.255.255.00 dst spec 5.6.7.8
150  *  dst mask 255.255.255.00 / sctp src is 16 dst is 17/ end"
151  */
152 static struct rte_flow_item_ipv4 ipv4_sctp_spec_1 = {
153         { 0, 0, 0, 0, 0, 0, IPPROTO_SCTP, 0, RTE_IPV4(11, 12, 13, 14),
154         RTE_IPV4(15, 16, 17, 18)}
155 };
156
157 static struct rte_flow_item_sctp sctp_spec_1 = {
158         { 10, 11, 0, 0}
159 };
160
161 static struct rte_flow_item  ipv4_sctp_item_1 = { RTE_FLOW_ITEM_TYPE_IPV4,
162         &ipv4_sctp_spec_1, 0, &ipv4_mask_24};
163
164 static struct rte_flow_item  sctp_item_1 = { RTE_FLOW_ITEM_TYPE_SCTP,
165         &sctp_spec_1, 0, &rte_flow_item_sctp_mask};
166
167
168 /* test actions:
169  * "actions count / end"
170  */
171 static 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 count_action_bad = { -1, 0};
181
182 static struct rte_flow_action end_action = { RTE_FLOW_ACTION_TYPE_END, 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;
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         return 0;
460 }
461
462 static int
463 init_ipv4_udp_traffic(struct rte_mempool *mp,
464              struct rte_mbuf **pkts_burst, uint32_t burst_size)
465 {
466         struct rte_ether_hdr pkt_eth_hdr;
467         struct rte_ipv4_hdr pkt_ipv4_hdr;
468         struct rte_udp_hdr pkt_udp_hdr;
469         uint32_t src_addr = IPV4_ADDR(2, 2, 2, 3);
470         uint32_t dst_addr = IPV4_ADDR(2, 2, 2, 7);
471         uint16_t src_port = 32;
472         uint16_t dst_port = 33;
473         uint16_t pktlen;
474
475         static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
476         static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
477
478         printf("Set up IPv4 UDP traffic\n");
479         initialize_eth_header(&pkt_eth_hdr,
480                 (struct rte_ether_addr *)src_mac,
481                 (struct rte_ether_addr *)dst_mac, RTE_ETHER_TYPE_IPV4, 0, 0);
482         pktlen = (uint16_t)(sizeof(struct rte_ether_hdr));
483         printf("ETH  pktlen %u\n", pktlen);
484
485         pktlen = initialize_ipv4_header(&pkt_ipv4_hdr, src_addr, dst_addr,
486                                         pktlen);
487         printf("ETH + IPv4 pktlen %u\n", pktlen);
488
489         pktlen = initialize_udp_header(&pkt_udp_hdr, src_port, dst_port,
490                                         pktlen);
491         printf("ETH + IPv4 + UDP pktlen %u\n\n", pktlen);
492
493         return generate_packet_burst(mp, pkts_burst, &pkt_eth_hdr,
494                                      0, &pkt_ipv4_hdr, 1,
495                                      &pkt_udp_hdr, burst_size,
496                                      PACKET_BURST_GEN_PKT_LEN, 1);
497 }
498
499 static int
500 init_ipv4_tcp_traffic(struct rte_mempool *mp,
501              struct rte_mbuf **pkts_burst, uint32_t burst_size)
502 {
503         struct rte_ether_hdr pkt_eth_hdr;
504         struct rte_ipv4_hdr pkt_ipv4_hdr;
505         struct rte_tcp_hdr pkt_tcp_hdr;
506         uint32_t src_addr = IPV4_ADDR(1, 2, 3, 4);
507         uint32_t dst_addr = IPV4_ADDR(5, 6, 7, 8);
508         uint16_t src_port = 16;
509         uint16_t dst_port = 17;
510         uint16_t pktlen;
511
512         static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
513         static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
514
515         printf("Set up IPv4 TCP traffic\n");
516         initialize_eth_header(&pkt_eth_hdr,
517                 (struct rte_ether_addr *)src_mac,
518                 (struct rte_ether_addr *)dst_mac, RTE_ETHER_TYPE_IPV4, 0, 0);
519         pktlen = (uint16_t)(sizeof(struct rte_ether_hdr));
520         printf("ETH  pktlen %u\n", pktlen);
521
522         pktlen = initialize_ipv4_header_proto(&pkt_ipv4_hdr, src_addr,
523                                         dst_addr, pktlen, IPPROTO_TCP);
524         printf("ETH + IPv4 pktlen %u\n", pktlen);
525
526         pktlen = initialize_tcp_header(&pkt_tcp_hdr, src_port, dst_port,
527                                         pktlen);
528         printf("ETH + IPv4 + TCP pktlen %u\n\n", pktlen);
529
530         return generate_packet_burst_proto(mp, pkts_burst, &pkt_eth_hdr,
531                                         0, &pkt_ipv4_hdr, 1, IPPROTO_TCP,
532                                         &pkt_tcp_hdr, burst_size,
533                                         PACKET_BURST_GEN_PKT_LEN, 1);
534 }
535
536 static int
537 init_ipv4_sctp_traffic(struct rte_mempool *mp,
538              struct rte_mbuf **pkts_burst, uint32_t burst_size)
539 {
540         struct rte_ether_hdr pkt_eth_hdr;
541         struct rte_ipv4_hdr pkt_ipv4_hdr;
542         struct rte_sctp_hdr pkt_sctp_hdr;
543         uint32_t src_addr = IPV4_ADDR(11, 12, 13, 14);
544         uint32_t dst_addr = IPV4_ADDR(15, 16, 17, 18);
545         uint16_t src_port = 10;
546         uint16_t dst_port = 11;
547         uint16_t pktlen;
548
549         static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
550         static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
551
552         printf("Set up IPv4 SCTP traffic\n");
553         initialize_eth_header(&pkt_eth_hdr,
554                 (struct rte_ether_addr *)src_mac,
555                 (struct rte_ether_addr *)dst_mac, RTE_ETHER_TYPE_IPV4, 0, 0);
556         pktlen = (uint16_t)(sizeof(struct rte_ether_hdr));
557         printf("ETH  pktlen %u\n", pktlen);
558
559         pktlen = initialize_ipv4_header_proto(&pkt_ipv4_hdr, src_addr,
560                                         dst_addr, pktlen, IPPROTO_SCTP);
561         printf("ETH + IPv4 pktlen %u\n", pktlen);
562
563         pktlen = initialize_sctp_header(&pkt_sctp_hdr, src_port, dst_port,
564                                         pktlen);
565         printf("ETH + IPv4 + SCTP pktlen %u\n\n", pktlen);
566
567         return generate_packet_burst_proto(mp, pkts_burst, &pkt_eth_hdr,
568                                         0, &pkt_ipv4_hdr, 1, IPPROTO_SCTP,
569                                         &pkt_sctp_hdr, burst_size,
570                                         PACKET_BURST_GEN_PKT_LEN, 1);
571 }
572
573 static int
574 init_mbufpool(void)
575 {
576         int socketid;
577         int ret = 0;
578         unsigned int lcore_id;
579         char s[64];
580
581         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
582                 if (rte_lcore_is_enabled(lcore_id) == 0)
583                         continue;
584
585                 socketid = rte_lcore_to_socket_id(lcore_id);
586                 if (socketid >= NB_SOCKETS) {
587                         printf(
588                                 "Socket %d of lcore %u is out of range %d\n",
589                                 socketid, lcore_id, NB_SOCKETS);
590                         ret = -1;
591                         break;
592                 }
593                 if (mbufpool[socketid] == NULL) {
594                         snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
595                         mbufpool[socketid] =
596                                 rte_pktmbuf_pool_create(s, NB_MBUF,
597                                         MEMPOOL_CACHE_SIZE, 0, MBUF_SIZE,
598                                         socketid);
599                         if (mbufpool[socketid]) {
600                                 printf("Allocated mbuf pool on socket %d\n",
601                                         socketid);
602                         } else {
603                                 printf("Cannot init mbuf pool on socket %d\n",
604                                         socketid);
605                                 ret = -ENOMEM;
606                                 break;
607                         }
608                 }
609         }
610         return ret;
611 }
612
613 static int
614 test_query_udp(void)
615 {
616         struct rte_flow_error error;
617         struct rte_flow_classify_rule *rule;
618         int ret;
619         int i;
620         int key_found;
621
622         ret = init_ipv4_udp_traffic(mbufpool[0], bufs, MAX_PKT_BURST);
623         if (ret != MAX_PKT_BURST) {
624                 printf("Line %i: init_udp_ipv4_traffic has failed!\n",
625                                 __LINE__);
626                 return -1;
627         }
628
629         for (i = 0; i < MAX_PKT_BURST; i++)
630                 bufs[i]->packet_type = RTE_PTYPE_L3_IPV4;
631
632         /*
633          * set up parameters for rte_flow_classify_validate,
634          * rte_flow_classify_table_entry_add and
635          * rte_flow_classify_table_entry_delete
636          */
637
638         attr.ingress = 1;
639         attr.priority = 1;
640         pattern[0] = eth_item;
641         pattern[1] = ipv4_udp_item_1;
642         pattern[2] = udp_item_1;
643         pattern[3] = end_item;
644         actions[0] = count_action;
645         actions[1] = end_action;
646
647         ret = rte_flow_classify_validate(cls->cls, &attr, pattern,
648                         actions, &error);
649         if (ret) {
650                 printf("Line %i: rte_flow_classify_validate", __LINE__);
651                 printf(" should not have failed!\n");
652                 return -1;
653         }
654
655         rule = rte_flow_classify_table_entry_add(cls->cls, &attr, pattern,
656                         actions, &key_found, &error);
657         if (!rule) {
658                 printf("Line %i: flow_classify_table_entry_add", __LINE__);
659                 printf(" should not have failed!\n");
660                 return -1;
661         }
662
663         ret = rte_flow_classifier_query(cls->cls, bufs, MAX_PKT_BURST,
664                         rule, &udp_classify_stats);
665         if (ret) {
666                 printf("Line %i: flow_classifier_query", __LINE__);
667                 printf(" should not have failed!\n");
668                 return -1;
669         }
670
671         ret = rte_flow_classify_table_entry_delete(cls->cls, rule);
672         if (ret) {
673                 printf("Line %i: rte_flow_classify_table_entry_delete",
674                         __LINE__);
675                 printf(" should not have failed!\n");
676                 return -1;
677         }
678         return 0;
679 }
680
681 static int
682 test_query_tcp(void)
683 {
684         struct rte_flow_classify_rule *rule;
685         int ret;
686         int i;
687         int key_found;
688
689         ret = init_ipv4_tcp_traffic(mbufpool[0], bufs, MAX_PKT_BURST);
690         if (ret != MAX_PKT_BURST) {
691                 printf("Line %i: init_ipv4_tcp_traffic has failed!\n",
692                                 __LINE__);
693                 return -1;
694         }
695
696         for (i = 0; i < MAX_PKT_BURST; i++)
697                 bufs[i]->packet_type = RTE_PTYPE_L3_IPV4;
698
699         /*
700          * set up parameters for rte_flow_classify_validate,
701          * rte_flow_classify_table_entry_add and
702          * rte_flow_classify_table_entry_delete
703          */
704
705         attr.ingress = 1;
706         attr.priority = 1;
707         pattern[0] = eth_item;
708         pattern[1] = ipv4_tcp_item_1;
709         pattern[2] = tcp_item_1;
710         pattern[3] = end_item;
711         actions[0] = count_action;
712         actions[1] = end_action;
713
714         ret = rte_flow_classify_validate(cls->cls, &attr, pattern,
715                         actions, &error);
716         if (ret) {
717                 printf("Line %i: flow_classifier_query", __LINE__);
718                 printf(" should not have failed!\n");
719                 return -1;
720         }
721
722         rule = rte_flow_classify_table_entry_add(cls->cls, &attr, pattern,
723                         actions, &key_found, &error);
724         if (!rule) {
725                 printf("Line %i: flow_classify_table_entry_add", __LINE__);
726                 printf(" should not have failed!\n");
727                 return -1;
728         }
729
730         ret = rte_flow_classifier_query(cls->cls, bufs, MAX_PKT_BURST,
731                         rule, &tcp_classify_stats);
732         if (ret) {
733                 printf("Line %i: flow_classifier_query", __LINE__);
734                 printf(" should not have failed!\n");
735                 return -1;
736         }
737
738         ret = rte_flow_classify_table_entry_delete(cls->cls, rule);
739         if (ret) {
740                 printf("Line %i: rte_flow_classify_table_entry_delete",
741                         __LINE__);
742                 printf(" should not have failed!\n");
743                 return -1;
744         }
745         return 0;
746 }
747
748 static int
749 test_query_sctp(void)
750 {
751         struct rte_flow_classify_rule *rule;
752         int ret;
753         int i;
754         int key_found;
755
756         ret = init_ipv4_sctp_traffic(mbufpool[0], bufs, MAX_PKT_BURST);
757         if (ret != MAX_PKT_BURST) {
758                 printf("Line %i: init_ipv4_tcp_traffic has failed!\n",
759                         __LINE__);
760                 return -1;
761         }
762
763         for (i = 0; i < MAX_PKT_BURST; i++)
764                 bufs[i]->packet_type = RTE_PTYPE_L3_IPV4;
765
766         /*
767          * set up parameters rte_flow_classify_validate,
768          * rte_flow_classify_table_entry_add and
769          * rte_flow_classify_table_entry_delete
770          */
771
772         attr.ingress = 1;
773         attr.priority = 1;
774         pattern[0] = eth_item;
775         pattern[1] = ipv4_sctp_item_1;
776         pattern[2] = sctp_item_1;
777         pattern[3] = end_item;
778         actions[0] = count_action;
779         actions[1] = end_action;
780
781         ret = rte_flow_classify_validate(cls->cls, &attr, pattern,
782                         actions, &error);
783         if (ret) {
784                 printf("Line %i: flow_classifier_query", __LINE__);
785                 printf(" should not have failed!\n");
786                 return -1;
787         }
788
789         rule = rte_flow_classify_table_entry_add(cls->cls, &attr, pattern,
790                         actions, &key_found, &error);
791         if (!rule) {
792                 printf("Line %i: flow_classify_table_entry_add", __LINE__);
793                 printf(" should not have failed!\n");
794                 return -1;
795         }
796
797         ret = rte_flow_classifier_query(cls->cls, bufs, MAX_PKT_BURST,
798                         rule, &sctp_classify_stats);
799         if (ret) {
800                 printf("Line %i: flow_classifier_query", __LINE__);
801                 printf(" should not have failed!\n");
802                 return -1;
803         }
804
805         ret = rte_flow_classify_table_entry_delete(cls->cls, rule);
806         if (ret) {
807                 printf("Line %i: rte_flow_classify_table_entry_delete",
808                         __LINE__);
809                 printf(" should not have failed!\n");
810                 return -1;
811         }
812         return 0;
813 }
814
815 static int
816 test_flow_classify(void)
817 {
818         struct rte_table_acl_params table_acl_params;
819         struct rte_flow_classify_table_params cls_table_params;
820         struct rte_flow_classifier_params cls_params;
821         int ret;
822         uint32_t size;
823
824         /* Memory allocation */
825         size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct flow_classifier_acl));
826         cls = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
827
828         cls_params.name = "flow_classifier";
829         cls_params.socket_id = 0;
830         cls->cls = rte_flow_classifier_create(&cls_params);
831
832         /* initialise ACL table params */
833         table_acl_params.n_rule_fields = RTE_DIM(ipv4_defs);
834         table_acl_params.name = "table_acl_ipv4_5tuple";
835         table_acl_params.n_rules = FLOW_CLASSIFY_MAX_RULE_NUM;
836         memcpy(table_acl_params.field_format, ipv4_defs, sizeof(ipv4_defs));
837
838         /* initialise table create params */
839         cls_table_params.ops = &rte_table_acl_ops;
840         cls_table_params.arg_create = &table_acl_params;
841         cls_table_params.type = RTE_FLOW_CLASSIFY_TABLE_ACL_IP4_5TUPLE;
842
843         ret = rte_flow_classify_table_create(cls->cls, &cls_table_params);
844         if (ret) {
845                 printf("Line %i: f_create has failed!\n", __LINE__);
846                 rte_flow_classifier_free(cls->cls);
847                 rte_free(cls);
848                 return TEST_FAILED;
849         }
850         printf("Created table_acl for for IPv4 five tuple packets\n");
851
852         ret = init_mbufpool();
853         if (ret) {
854                 printf("Line %i: init_mbufpool has failed!\n", __LINE__);
855                 return TEST_FAILED;
856         }
857
858         if (test_invalid_parameters() < 0)
859                 return TEST_FAILED;
860         if (test_valid_parameters() < 0)
861                 return TEST_FAILED;
862         if (test_invalid_patterns() < 0)
863                 return TEST_FAILED;
864         if (test_invalid_actions() < 0)
865                 return TEST_FAILED;
866         if (test_query_udp() < 0)
867                 return TEST_FAILED;
868         if (test_query_tcp() < 0)
869                 return TEST_FAILED;
870         if (test_query_sctp() < 0)
871                 return TEST_FAILED;
872
873         return TEST_SUCCESS;
874 }
875
876 REGISTER_TEST_COMMAND(flow_classify_autotest, test_flow_classify);