1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
10 #include <rte_string_fns.h>
12 #include <rte_byteorder.h>
15 #include <rte_common.h>
16 #include <rte_table_acl.h>
18 #include <rte_flow_classify.h>
20 #include "packet_burst_generator.h"
21 #include "test_flow_classify.h"
24 #define FLOW_CLASSIFY_MAX_RULE_NUM 100
25 struct flow_classifier *cls;
27 struct flow_classifier {
28 struct rte_flow_classifier *cls;
29 uint32_t table_id[RTE_FLOW_CLASSIFY_TABLE_MAX];
33 struct flow_classifier_acl {
34 struct flow_classifier cls;
35 } __rte_cache_aligned;
38 * test functions by passing invalid or
39 * non-workable parameters.
42 test_invalid_parameters(void)
44 struct rte_flow_classify_rule *rule;
47 rule = rte_flow_classify_table_entry_add(NULL, 1, NULL, NULL, NULL,
50 printf("Line %i: flow_classifier_table_entry_add", __LINE__);
51 printf(" with NULL param should have failed!\n");
55 ret = rte_flow_classify_table_entry_delete(NULL, 1, NULL);
57 printf("Line %i: rte_flow_classify_table_entry_delete",
59 printf(" with NULL param should have failed!\n");
63 ret = rte_flow_classifier_query(NULL, 1, NULL, 0, NULL, NULL);
65 printf("Line %i: flow_classifier_query", __LINE__);
66 printf(" with NULL param should have failed!\n");
70 rule = rte_flow_classify_table_entry_add(NULL, 1, NULL, NULL, NULL,
73 printf("Line %i: flow_classify_table_entry_add ", __LINE__);
74 printf("with NULL param should have failed!\n");
78 ret = rte_flow_classify_table_entry_delete(NULL, 1, NULL);
80 printf("Line %i: rte_flow_classify_table_entry_delete",
82 printf("with NULL param should have failed!\n");
86 ret = rte_flow_classifier_query(NULL, 1, NULL, 0, NULL, NULL);
88 printf("Line %i: flow_classifier_query", __LINE__);
89 printf(" with NULL param should have failed!\n");
96 test_valid_parameters(void)
98 struct rte_flow_classify_rule *rule;
103 * set up parameters for rte_flow_classify_table_entry_add and
104 * rte_flow_classify_table_entry_delete
109 pattern[0] = eth_item;
110 pattern[1] = ipv4_udp_item_1;
111 pattern[2] = udp_item_1;
112 pattern[3] = end_item;
113 actions[0] = count_action;
114 actions[1] = end_action;
116 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
117 &attr, pattern, actions, &error);
119 printf("Line %i: flow_classify_table_entry_add", __LINE__);
120 printf(" should not have failed!\n");
124 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
126 printf("Line %i: rte_flow_classify_table_entry_delete",
128 printf(" should not have failed!\n");
135 test_invalid_patterns(void)
137 struct rte_flow_classify_rule *rule;
142 * set up parameters for rte_flow_classify_table_entry_add and
143 * rte_flow_classify_table_entry_delete
148 pattern[0] = eth_item_bad;
149 pattern[1] = ipv4_udp_item_1;
150 pattern[2] = udp_item_1;
151 pattern[3] = end_item;
152 actions[0] = count_action;
153 actions[1] = end_action;
155 pattern[0] = eth_item;
156 pattern[1] = ipv4_udp_item_bad;
157 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
158 &attr, pattern, actions, &error);
160 printf("Line %i: flow_classify_table_entry_add", __LINE__);
161 printf(" should have failed!\n");
165 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
167 printf("Line %i: rte_flow_classify_table_entry_delete",
169 printf(" should have failed!\n");
173 pattern[1] = ipv4_udp_item_1;
174 pattern[2] = udp_item_bad;
175 pattern[3] = end_item_bad;
176 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
177 &attr, pattern, actions, &error);
179 printf("Line %i: flow_classify_table_entry_add", __LINE__);
180 printf(" should have failed!\n");
184 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
186 printf("Line %i: rte_flow_classify_table_entry_delete",
188 printf(" should have failed!\n");
195 test_invalid_actions(void)
197 struct rte_flow_classify_rule *rule;
202 * set up parameters for rte_flow_classify_table_entry_add and
203 * rte_flow_classify_table_entry_delete
208 pattern[0] = eth_item;
209 pattern[1] = ipv4_udp_item_1;
210 pattern[2] = udp_item_1;
211 pattern[3] = end_item;
212 actions[0] = count_action_bad;
213 actions[1] = end_action;
215 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
216 &attr, pattern, actions, &error);
218 printf("Line %i: flow_classify_table_entry_add", __LINE__);
219 printf(" should have failed!\n");
223 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
225 printf("Line %i: rte_flow_classify_table_entry_delete",
227 printf(" should have failed!\n");
231 actions[0] = count_action;
232 actions[1] = end_action_bad;
234 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
235 &attr, pattern, actions, &error);
237 printf("Line %i: flow_classify_table_entry_add", __LINE__);
238 printf(" should have failed!\n");
242 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
244 printf("Line %i: rte_flow_classify_table_entry_delete",
246 printf("should have failed!\n");
253 init_ipv4_udp_traffic(struct rte_mempool *mp,
254 struct rte_mbuf **pkts_burst, uint32_t burst_size)
256 struct ether_hdr pkt_eth_hdr;
257 struct ipv4_hdr pkt_ipv4_hdr;
258 struct udp_hdr pkt_udp_hdr;
259 uint32_t src_addr = IPV4_ADDR(2, 2, 2, 3);
260 uint32_t dst_addr = IPV4_ADDR(2, 2, 2, 7);
261 uint16_t src_port = 32;
262 uint16_t dst_port = 33;
265 static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
266 static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
268 printf("Set up IPv4 UDP traffic\n");
269 initialize_eth_header(&pkt_eth_hdr,
270 (struct ether_addr *)src_mac,
271 (struct ether_addr *)dst_mac, ETHER_TYPE_IPv4, 0, 0);
272 pktlen = (uint16_t)(sizeof(struct ether_hdr));
273 printf("ETH pktlen %u\n", pktlen);
275 pktlen = initialize_ipv4_header(&pkt_ipv4_hdr, src_addr, dst_addr,
277 printf("ETH + IPv4 pktlen %u\n", pktlen);
279 pktlen = initialize_udp_header(&pkt_udp_hdr, src_port, dst_port,
281 printf("ETH + IPv4 + UDP pktlen %u\n\n", pktlen);
283 return generate_packet_burst(mp, pkts_burst, &pkt_eth_hdr,
285 &pkt_udp_hdr, burst_size,
286 PACKET_BURST_GEN_PKT_LEN, 1);
290 init_ipv4_tcp_traffic(struct rte_mempool *mp,
291 struct rte_mbuf **pkts_burst, uint32_t burst_size)
293 struct ether_hdr pkt_eth_hdr;
294 struct ipv4_hdr pkt_ipv4_hdr;
295 struct tcp_hdr pkt_tcp_hdr;
296 uint32_t src_addr = IPV4_ADDR(1, 2, 3, 4);
297 uint32_t dst_addr = IPV4_ADDR(5, 6, 7, 8);
298 uint16_t src_port = 16;
299 uint16_t dst_port = 17;
302 static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
303 static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
305 printf("Set up IPv4 TCP traffic\n");
306 initialize_eth_header(&pkt_eth_hdr,
307 (struct ether_addr *)src_mac,
308 (struct ether_addr *)dst_mac, ETHER_TYPE_IPv4, 0, 0);
309 pktlen = (uint16_t)(sizeof(struct ether_hdr));
310 printf("ETH pktlen %u\n", pktlen);
312 pktlen = initialize_ipv4_header_proto(&pkt_ipv4_hdr, src_addr,
313 dst_addr, pktlen, IPPROTO_TCP);
314 printf("ETH + IPv4 pktlen %u\n", pktlen);
316 pktlen = initialize_tcp_header(&pkt_tcp_hdr, src_port, dst_port,
318 printf("ETH + IPv4 + TCP pktlen %u\n\n", pktlen);
320 return generate_packet_burst_proto(mp, pkts_burst, &pkt_eth_hdr,
321 0, &pkt_ipv4_hdr, 1, IPPROTO_TCP,
322 &pkt_tcp_hdr, burst_size,
323 PACKET_BURST_GEN_PKT_LEN, 1);
327 init_ipv4_sctp_traffic(struct rte_mempool *mp,
328 struct rte_mbuf **pkts_burst, uint32_t burst_size)
330 struct ether_hdr pkt_eth_hdr;
331 struct ipv4_hdr pkt_ipv4_hdr;
332 struct sctp_hdr pkt_sctp_hdr;
333 uint32_t src_addr = IPV4_ADDR(11, 12, 13, 14);
334 uint32_t dst_addr = IPV4_ADDR(15, 16, 17, 18);
335 uint16_t src_port = 10;
336 uint16_t dst_port = 11;
339 static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
340 static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
342 printf("Set up IPv4 SCTP traffic\n");
343 initialize_eth_header(&pkt_eth_hdr,
344 (struct ether_addr *)src_mac,
345 (struct ether_addr *)dst_mac, ETHER_TYPE_IPv4, 0, 0);
346 pktlen = (uint16_t)(sizeof(struct ether_hdr));
347 printf("ETH pktlen %u\n", pktlen);
349 pktlen = initialize_ipv4_header_proto(&pkt_ipv4_hdr, src_addr,
350 dst_addr, pktlen, IPPROTO_SCTP);
351 printf("ETH + IPv4 pktlen %u\n", pktlen);
353 pktlen = initialize_sctp_header(&pkt_sctp_hdr, src_port, dst_port,
355 printf("ETH + IPv4 + SCTP pktlen %u\n\n", pktlen);
357 return generate_packet_burst_proto(mp, pkts_burst, &pkt_eth_hdr,
358 0, &pkt_ipv4_hdr, 1, IPPROTO_SCTP,
359 &pkt_sctp_hdr, burst_size,
360 PACKET_BURST_GEN_PKT_LEN, 1);
368 unsigned int lcore_id;
371 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
372 if (rte_lcore_is_enabled(lcore_id) == 0)
375 socketid = rte_lcore_to_socket_id(lcore_id);
376 if (socketid >= NB_SOCKETS) {
378 "Socket %d of lcore %u is out of range %d\n",
379 socketid, lcore_id, NB_SOCKETS);
383 if (mbufpool[socketid] == NULL) {
384 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
386 rte_pktmbuf_pool_create(s, NB_MBUF,
387 MEMPOOL_CACHE_SIZE, 0, MBUF_SIZE,
389 if (mbufpool[socketid]) {
390 printf("Allocated mbuf pool on socket %d\n",
393 printf("Cannot init mbuf pool on socket %d\n",
406 struct rte_flow_error error;
407 struct rte_flow_classify_rule *rule;
412 ret = init_ipv4_udp_traffic(mbufpool[0], bufs, MAX_PKT_BURST);
413 if (ret != MAX_PKT_BURST) {
414 printf("Line %i: init_udp_ipv4_traffic has failed!\n",
419 for (i = 0; i < MAX_PKT_BURST; i++)
420 bufs[i]->packet_type = RTE_PTYPE_L3_IPV4;
423 * set up parameters for rte_flow_classify_table_entry_add and
424 * rte_flow_classify_table_entry_delete
429 pattern[0] = eth_item;
430 pattern[1] = ipv4_udp_item_1;
431 pattern[2] = udp_item_1;
432 pattern[3] = end_item;
433 actions[0] = count_action;
434 actions[1] = end_action;
436 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
437 &attr, pattern, actions, &error);
439 printf("Line %i: flow_classify_table_entry_add", __LINE__);
440 printf(" should not have failed!\n");
444 ret = rte_flow_classifier_query(cls->cls, 0, bufs, MAX_PKT_BURST,
445 rule, &udp_classify_stats);
447 printf("Line %i: flow_classifier_query", __LINE__);
448 printf(" should not have failed!\n");
452 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
454 printf("Line %i: rte_flow_classify_table_entry_delete",
456 printf(" should not have failed!\n");
465 struct rte_flow_classify_rule *rule;
470 ret = init_ipv4_tcp_traffic(mbufpool[0], bufs, MAX_PKT_BURST);
471 if (ret != MAX_PKT_BURST) {
472 printf("Line %i: init_ipv4_tcp_traffic has failed!\n",
477 for (i = 0; i < MAX_PKT_BURST; i++)
478 bufs[i]->packet_type = RTE_PTYPE_L3_IPV4;
481 * set up parameters for rte_flow_classify_table_entry_add and
482 * rte_flow_classify_table_entry_delete
487 pattern[0] = eth_item;
488 pattern[1] = ipv4_tcp_item_1;
489 pattern[2] = tcp_item_1;
490 pattern[3] = end_item;
491 actions[0] = count_action;
492 actions[1] = end_action;
494 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
495 &attr, pattern, actions, &error);
497 printf("Line %i: flow_classify_table_entry_add", __LINE__);
498 printf(" should not have failed!\n");
502 ret = rte_flow_classifier_query(cls->cls, 0, bufs, MAX_PKT_BURST,
503 rule, &tcp_classify_stats);
505 printf("Line %i: flow_classifier_query", __LINE__);
506 printf(" should not have failed!\n");
510 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
512 printf("Line %i: rte_flow_classify_table_entry_delete",
514 printf(" should not have failed!\n");
521 test_query_sctp(void)
523 struct rte_flow_classify_rule *rule;
528 ret = init_ipv4_sctp_traffic(mbufpool[0], bufs, MAX_PKT_BURST);
529 if (ret != MAX_PKT_BURST) {
530 printf("Line %i: init_ipv4_tcp_traffic has failed!\n",
535 for (i = 0; i < MAX_PKT_BURST; i++)
536 bufs[i]->packet_type = RTE_PTYPE_L3_IPV4;
539 * set up parameters rte_flow_classify_table_entry_add and
540 * rte_flow_classify_table_entry_delete
545 pattern[0] = eth_item;
546 pattern[1] = ipv4_sctp_item_1;
547 pattern[2] = sctp_item_1;
548 pattern[3] = end_item;
549 actions[0] = count_action;
550 actions[1] = end_action;
552 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
553 &attr, pattern, actions, &error);
555 printf("Line %i: flow_classify_table_entry_add", __LINE__);
556 printf(" should not have failed!\n");
560 ret = rte_flow_classifier_query(cls->cls, 0, bufs, MAX_PKT_BURST,
561 rule, &sctp_classify_stats);
563 printf("Line %i: flow_classifier_query", __LINE__);
564 printf(" should not have failed!\n");
568 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
570 printf("Line %i: rte_flow_classify_table_entry_delete",
572 printf(" should not have failed!\n");
579 test_flow_classify(void)
581 struct rte_table_acl_params table_acl_params;
582 struct rte_flow_classify_table_params cls_table_params;
583 struct rte_flow_classifier_params cls_params;
588 socket_id = rte_eth_dev_socket_id(0);
590 /* Memory allocation */
591 size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct flow_classifier_acl));
592 cls = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
594 cls_params.name = "flow_classifier";
595 cls_params.socket_id = socket_id;
596 cls_params.type = RTE_FLOW_CLASSIFY_TABLE_TYPE_ACL;
597 cls->cls = rte_flow_classifier_create(&cls_params);
599 /* initialise ACL table params */
600 table_acl_params.n_rule_fields = RTE_DIM(ipv4_defs);
601 table_acl_params.name = "table_acl_ipv4_5tuple";
602 table_acl_params.n_rules = FLOW_CLASSIFY_MAX_RULE_NUM;
603 memcpy(table_acl_params.field_format, ipv4_defs, sizeof(ipv4_defs));
605 /* initialise table create params */
606 cls_table_params.ops = &rte_table_acl_ops,
607 cls_table_params.arg_create = &table_acl_params,
609 ret = rte_flow_classify_table_create(cls->cls, &cls_table_params,
612 printf("Line %i: f_create has failed!\n", __LINE__);
613 rte_flow_classifier_free(cls->cls);
617 printf("Created table_acl for for IPv4 five tuple packets\n");
619 ret = init_mbufpool();
621 printf("Line %i: init_mbufpool has failed!\n", __LINE__);
625 if (test_invalid_parameters() < 0)
627 if (test_valid_parameters() < 0)
629 if (test_invalid_patterns() < 0)
631 if (test_invalid_actions() < 0)
633 if (test_query_udp() < 0)
635 if (test_query_tcp() < 0)
637 if (test_query_sctp() < 0)
643 REGISTER_TEST_COMMAND(flow_classify_autotest, test_flow_classify);