4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <rte_string_fns.h>
41 #include <rte_byteorder.h>
44 #include <rte_common.h>
45 #include <rte_table_acl.h>
47 #include <rte_flow_classify.h>
49 #include "packet_burst_generator.h"
50 #include "test_flow_classify.h"
53 #define FLOW_CLASSIFY_MAX_RULE_NUM 100
54 struct flow_classifier *cls;
56 struct flow_classifier {
57 struct rte_flow_classifier *cls;
58 uint32_t table_id[RTE_FLOW_CLASSIFY_TABLE_MAX];
62 struct flow_classifier_acl {
63 struct flow_classifier cls;
64 } __rte_cache_aligned;
67 * test functions by passing invalid or
68 * non-workable parameters.
71 test_invalid_parameters(void)
73 struct rte_flow_classify_rule *rule;
76 rule = rte_flow_classify_table_entry_add(NULL, 1, NULL, NULL, NULL,
79 printf("Line %i: flow_classifier_table_entry_add", __LINE__);
80 printf(" with NULL param should have failed!\n");
84 ret = rte_flow_classify_table_entry_delete(NULL, 1, NULL);
86 printf("Line %i: rte_flow_classify_table_entry_delete",
88 printf(" with NULL param should have failed!\n");
92 ret = rte_flow_classifier_query(NULL, 1, NULL, 0, NULL, NULL);
94 printf("Line %i: flow_classifier_query", __LINE__);
95 printf(" with NULL param should have failed!\n");
99 rule = rte_flow_classify_table_entry_add(NULL, 1, NULL, NULL, NULL,
102 printf("Line %i: flow_classify_table_entry_add ", __LINE__);
103 printf("with NULL param should have failed!\n");
107 ret = rte_flow_classify_table_entry_delete(NULL, 1, NULL);
109 printf("Line %i: rte_flow_classify_table_entry_delete",
111 printf("with NULL param should have failed!\n");
115 ret = rte_flow_classifier_query(NULL, 1, NULL, 0, NULL, NULL);
117 printf("Line %i: flow_classifier_query", __LINE__);
118 printf(" with NULL param should have failed!\n");
125 test_valid_parameters(void)
127 struct rte_flow_classify_rule *rule;
132 * set up parameters for rte_flow_classify_table_entry_add and
133 * rte_flow_classify_table_entry_delete
138 pattern[0] = eth_item;
139 pattern[1] = ipv4_udp_item_1;
140 pattern[2] = udp_item_1;
141 pattern[3] = end_item;
142 actions[0] = count_action;
143 actions[1] = end_action;
145 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
146 &attr, pattern, actions, &error);
148 printf("Line %i: flow_classify_table_entry_add", __LINE__);
149 printf(" should not have failed!\n");
153 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
155 printf("Line %i: rte_flow_classify_table_entry_delete",
157 printf(" should not have failed!\n");
164 test_invalid_patterns(void)
166 struct rte_flow_classify_rule *rule;
171 * set up parameters for rte_flow_classify_table_entry_add and
172 * rte_flow_classify_table_entry_delete
177 pattern[0] = eth_item_bad;
178 pattern[1] = ipv4_udp_item_1;
179 pattern[2] = udp_item_1;
180 pattern[3] = end_item;
181 actions[0] = count_action;
182 actions[1] = end_action;
184 pattern[0] = eth_item;
185 pattern[1] = ipv4_udp_item_bad;
186 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
187 &attr, pattern, actions, &error);
189 printf("Line %i: flow_classify_table_entry_add", __LINE__);
190 printf(" should have failed!\n");
194 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
196 printf("Line %i: rte_flow_classify_table_entry_delete",
198 printf(" should have failed!\n");
202 pattern[1] = ipv4_udp_item_1;
203 pattern[2] = udp_item_bad;
204 pattern[3] = end_item_bad;
205 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
206 &attr, pattern, actions, &error);
208 printf("Line %i: flow_classify_table_entry_add", __LINE__);
209 printf(" should have failed!\n");
213 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
215 printf("Line %i: rte_flow_classify_table_entry_delete",
217 printf(" should have failed!\n");
224 test_invalid_actions(void)
226 struct rte_flow_classify_rule *rule;
231 * set up parameters for rte_flow_classify_table_entry_add and
232 * rte_flow_classify_table_entry_delete
237 pattern[0] = eth_item;
238 pattern[1] = ipv4_udp_item_1;
239 pattern[2] = udp_item_1;
240 pattern[3] = end_item;
241 actions[0] = count_action_bad;
242 actions[1] = end_action;
244 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
245 &attr, pattern, actions, &error);
247 printf("Line %i: flow_classify_table_entry_add", __LINE__);
248 printf(" should have failed!\n");
252 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
254 printf("Line %i: rte_flow_classify_table_entry_delete",
256 printf(" should have failed!\n");
260 actions[0] = count_action;
261 actions[1] = end_action_bad;
263 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
264 &attr, pattern, actions, &error);
266 printf("Line %i: flow_classify_table_entry_add", __LINE__);
267 printf(" should have failed!\n");
271 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
273 printf("Line %i: rte_flow_classify_table_entry_delete",
275 printf("should have failed!\n");
282 init_ipv4_udp_traffic(struct rte_mempool *mp,
283 struct rte_mbuf **pkts_burst, uint32_t burst_size)
285 struct ether_hdr pkt_eth_hdr;
286 struct ipv4_hdr pkt_ipv4_hdr;
287 struct udp_hdr pkt_udp_hdr;
288 uint32_t src_addr = IPV4_ADDR(2, 2, 2, 3);
289 uint32_t dst_addr = IPV4_ADDR(2, 2, 2, 7);
290 uint16_t src_port = 32;
291 uint16_t dst_port = 33;
294 static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
295 static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
297 printf("Set up IPv4 UDP traffic\n");
298 initialize_eth_header(&pkt_eth_hdr,
299 (struct ether_addr *)src_mac,
300 (struct ether_addr *)dst_mac, ETHER_TYPE_IPv4, 0, 0);
301 pktlen = (uint16_t)(sizeof(struct ether_hdr));
302 printf("ETH pktlen %u\n", pktlen);
304 pktlen = initialize_ipv4_header(&pkt_ipv4_hdr, src_addr, dst_addr,
306 printf("ETH + IPv4 pktlen %u\n", pktlen);
308 pktlen = initialize_udp_header(&pkt_udp_hdr, src_port, dst_port,
310 printf("ETH + IPv4 + UDP pktlen %u\n\n", pktlen);
312 return generate_packet_burst(mp, pkts_burst, &pkt_eth_hdr,
314 &pkt_udp_hdr, burst_size,
315 PACKET_BURST_GEN_PKT_LEN, 1);
319 init_ipv4_tcp_traffic(struct rte_mempool *mp,
320 struct rte_mbuf **pkts_burst, uint32_t burst_size)
322 struct ether_hdr pkt_eth_hdr;
323 struct ipv4_hdr pkt_ipv4_hdr;
324 struct tcp_hdr pkt_tcp_hdr;
325 uint32_t src_addr = IPV4_ADDR(1, 2, 3, 4);
326 uint32_t dst_addr = IPV4_ADDR(5, 6, 7, 8);
327 uint16_t src_port = 16;
328 uint16_t dst_port = 17;
331 static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
332 static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
334 printf("Set up IPv4 TCP traffic\n");
335 initialize_eth_header(&pkt_eth_hdr,
336 (struct ether_addr *)src_mac,
337 (struct ether_addr *)dst_mac, ETHER_TYPE_IPv4, 0, 0);
338 pktlen = (uint16_t)(sizeof(struct ether_hdr));
339 printf("ETH pktlen %u\n", pktlen);
341 pktlen = initialize_ipv4_header_proto(&pkt_ipv4_hdr, src_addr,
342 dst_addr, pktlen, IPPROTO_TCP);
343 printf("ETH + IPv4 pktlen %u\n", pktlen);
345 pktlen = initialize_tcp_header(&pkt_tcp_hdr, src_port, dst_port,
347 printf("ETH + IPv4 + TCP pktlen %u\n\n", pktlen);
349 return generate_packet_burst_proto(mp, pkts_burst, &pkt_eth_hdr,
350 0, &pkt_ipv4_hdr, 1, IPPROTO_TCP,
351 &pkt_tcp_hdr, burst_size,
352 PACKET_BURST_GEN_PKT_LEN, 1);
356 init_ipv4_sctp_traffic(struct rte_mempool *mp,
357 struct rte_mbuf **pkts_burst, uint32_t burst_size)
359 struct ether_hdr pkt_eth_hdr;
360 struct ipv4_hdr pkt_ipv4_hdr;
361 struct sctp_hdr pkt_sctp_hdr;
362 uint32_t src_addr = IPV4_ADDR(11, 12, 13, 14);
363 uint32_t dst_addr = IPV4_ADDR(15, 16, 17, 18);
364 uint16_t src_port = 10;
365 uint16_t dst_port = 11;
368 static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
369 static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
371 printf("Set up IPv4 SCTP traffic\n");
372 initialize_eth_header(&pkt_eth_hdr,
373 (struct ether_addr *)src_mac,
374 (struct ether_addr *)dst_mac, ETHER_TYPE_IPv4, 0, 0);
375 pktlen = (uint16_t)(sizeof(struct ether_hdr));
376 printf("ETH pktlen %u\n", pktlen);
378 pktlen = initialize_ipv4_header_proto(&pkt_ipv4_hdr, src_addr,
379 dst_addr, pktlen, IPPROTO_SCTP);
380 printf("ETH + IPv4 pktlen %u\n", pktlen);
382 pktlen = initialize_sctp_header(&pkt_sctp_hdr, src_port, dst_port,
384 printf("ETH + IPv4 + SCTP pktlen %u\n\n", pktlen);
386 return generate_packet_burst_proto(mp, pkts_burst, &pkt_eth_hdr,
387 0, &pkt_ipv4_hdr, 1, IPPROTO_SCTP,
388 &pkt_sctp_hdr, burst_size,
389 PACKET_BURST_GEN_PKT_LEN, 1);
397 unsigned int lcore_id;
400 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
401 if (rte_lcore_is_enabled(lcore_id) == 0)
404 socketid = rte_lcore_to_socket_id(lcore_id);
405 if (socketid >= NB_SOCKETS) {
407 "Socket %d of lcore %u is out of range %d\n",
408 socketid, lcore_id, NB_SOCKETS);
412 if (mbufpool[socketid] == NULL) {
413 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
415 rte_pktmbuf_pool_create(s, NB_MBUF,
416 MEMPOOL_CACHE_SIZE, 0, MBUF_SIZE,
418 if (mbufpool[socketid]) {
419 printf("Allocated mbuf pool on socket %d\n",
422 printf("Cannot init mbuf pool on socket %d\n",
435 struct rte_flow_error error;
436 struct rte_flow_classify_rule *rule;
441 ret = init_ipv4_udp_traffic(mbufpool[0], bufs, MAX_PKT_BURST);
442 if (ret != MAX_PKT_BURST) {
443 printf("Line %i: init_udp_ipv4_traffic has failed!\n",
448 for (i = 0; i < MAX_PKT_BURST; i++)
449 bufs[i]->packet_type = RTE_PTYPE_L3_IPV4;
452 * set up parameters for rte_flow_classify_table_entry_add and
453 * rte_flow_classify_table_entry_delete
458 pattern[0] = eth_item;
459 pattern[1] = ipv4_udp_item_1;
460 pattern[2] = udp_item_1;
461 pattern[3] = end_item;
462 actions[0] = count_action;
463 actions[1] = end_action;
465 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
466 &attr, pattern, actions, &error);
468 printf("Line %i: flow_classify_table_entry_add", __LINE__);
469 printf(" should not have failed!\n");
473 ret = rte_flow_classifier_query(cls->cls, 0, bufs, MAX_PKT_BURST,
474 rule, &udp_classify_stats);
476 printf("Line %i: flow_classifier_query", __LINE__);
477 printf(" should not have failed!\n");
481 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
483 printf("Line %i: rte_flow_classify_table_entry_delete",
485 printf(" should not have failed!\n");
494 struct rte_flow_classify_rule *rule;
499 ret = init_ipv4_tcp_traffic(mbufpool[0], bufs, MAX_PKT_BURST);
500 if (ret != MAX_PKT_BURST) {
501 printf("Line %i: init_ipv4_tcp_traffic has failed!\n",
506 for (i = 0; i < MAX_PKT_BURST; i++)
507 bufs[i]->packet_type = RTE_PTYPE_L3_IPV4;
510 * set up parameters for rte_flow_classify_table_entry_add and
511 * rte_flow_classify_table_entry_delete
516 pattern[0] = eth_item;
517 pattern[1] = ipv4_tcp_item_1;
518 pattern[2] = tcp_item_1;
519 pattern[3] = end_item;
520 actions[0] = count_action;
521 actions[1] = end_action;
523 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
524 &attr, pattern, actions, &error);
526 printf("Line %i: flow_classify_table_entry_add", __LINE__);
527 printf(" should not have failed!\n");
531 ret = rte_flow_classifier_query(cls->cls, 0, bufs, MAX_PKT_BURST,
532 rule, &tcp_classify_stats);
534 printf("Line %i: flow_classifier_query", __LINE__);
535 printf(" should not have failed!\n");
539 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
541 printf("Line %i: rte_flow_classify_table_entry_delete",
543 printf(" should not have failed!\n");
550 test_query_sctp(void)
552 struct rte_flow_classify_rule *rule;
557 ret = init_ipv4_sctp_traffic(mbufpool[0], bufs, MAX_PKT_BURST);
558 if (ret != MAX_PKT_BURST) {
559 printf("Line %i: init_ipv4_tcp_traffic has failed!\n",
564 for (i = 0; i < MAX_PKT_BURST; i++)
565 bufs[i]->packet_type = RTE_PTYPE_L3_IPV4;
568 * set up parameters rte_flow_classify_table_entry_add and
569 * rte_flow_classify_table_entry_delete
574 pattern[0] = eth_item;
575 pattern[1] = ipv4_sctp_item_1;
576 pattern[2] = sctp_item_1;
577 pattern[3] = end_item;
578 actions[0] = count_action;
579 actions[1] = end_action;
581 rule = rte_flow_classify_table_entry_add(cls->cls, 0, &key_found,
582 &attr, pattern, actions, &error);
584 printf("Line %i: flow_classify_table_entry_add", __LINE__);
585 printf(" should not have failed!\n");
589 ret = rte_flow_classifier_query(cls->cls, 0, bufs, MAX_PKT_BURST,
590 rule, &sctp_classify_stats);
592 printf("Line %i: flow_classifier_query", __LINE__);
593 printf(" should not have failed!\n");
597 ret = rte_flow_classify_table_entry_delete(cls->cls, 0, rule);
599 printf("Line %i: rte_flow_classify_table_entry_delete",
601 printf(" should not have failed!\n");
608 test_flow_classify(void)
610 struct rte_table_acl_params table_acl_params;
611 struct rte_flow_classify_table_params cls_table_params;
612 struct rte_flow_classifier_params cls_params;
617 socket_id = rte_eth_dev_socket_id(0);
619 /* Memory allocation */
620 size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct flow_classifier_acl));
621 cls = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
623 cls_params.name = "flow_classifier";
624 cls_params.socket_id = socket_id;
625 cls_params.type = RTE_FLOW_CLASSIFY_TABLE_TYPE_ACL;
626 cls->cls = rte_flow_classifier_create(&cls_params);
628 /* initialise ACL table params */
629 table_acl_params.n_rule_fields = RTE_DIM(ipv4_defs);
630 table_acl_params.name = "table_acl_ipv4_5tuple";
631 table_acl_params.n_rules = FLOW_CLASSIFY_MAX_RULE_NUM;
632 memcpy(table_acl_params.field_format, ipv4_defs, sizeof(ipv4_defs));
634 /* initialise table create params */
635 cls_table_params.ops = &rte_table_acl_ops,
636 cls_table_params.arg_create = &table_acl_params,
638 ret = rte_flow_classify_table_create(cls->cls, &cls_table_params,
641 printf("Line %i: f_create has failed!\n", __LINE__);
642 rte_flow_classifier_free(cls->cls);
646 printf("Created table_acl for for IPv4 five tuple packets\n");
648 ret = init_mbufpool();
650 printf("Line %i: init_mbufpool has failed!\n", __LINE__);
654 if (test_invalid_parameters() < 0)
656 if (test_valid_parameters() < 0)
658 if (test_invalid_patterns() < 0)
660 if (test_invalid_actions() < 0)
662 if (test_query_udp() < 0)
664 if (test_query_tcp() < 0)
666 if (test_query_sctp() < 0)
672 REGISTER_TEST_COMMAND(flow_classify_autotest, test_flow_classify);