1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #include <rte_string_fns.h>
6 #include <rte_hexdump.h>
7 #include "test_table.h"
8 #include "test_table_acl.h"
10 #define IPv4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
11 (((b) & 0xff) << 16) | \
12 (((c) & 0xff) << 8) | \
16 * Rule and trace formats definitions.
36 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
38 .type = RTE_ACL_FIELD_TYPE_BITMASK,
39 .size = sizeof(uint8_t),
40 .field_index = PROTO_FIELD_IPV4,
41 .input_index = PROTO_FIELD_IPV4,
42 .offset = offsetof(struct ipv4_5tuple, proto),
45 .type = RTE_ACL_FIELD_TYPE_MASK,
46 .size = sizeof(uint32_t),
47 .field_index = SRC_FIELD_IPV4,
48 .input_index = SRC_FIELD_IPV4,
49 .offset = offsetof(struct ipv4_5tuple, ip_src),
52 .type = RTE_ACL_FIELD_TYPE_MASK,
53 .size = sizeof(uint32_t),
54 .field_index = DST_FIELD_IPV4,
55 .input_index = DST_FIELD_IPV4,
56 .offset = offsetof(struct ipv4_5tuple, ip_dst),
59 .type = RTE_ACL_FIELD_TYPE_RANGE,
60 .size = sizeof(uint16_t),
61 .field_index = SRCP_FIELD_IPV4,
62 .input_index = SRCP_FIELD_IPV4,
63 .offset = offsetof(struct ipv4_5tuple, port_src),
66 .type = RTE_ACL_FIELD_TYPE_RANGE,
67 .size = sizeof(uint16_t),
68 .field_index = DSTP_FIELD_IPV4,
69 .input_index = SRCP_FIELD_IPV4,
70 .offset = offsetof(struct ipv4_5tuple, port_dst),
74 struct rte_table_acl_rule_add_params table_acl_IPv4_rule;
76 typedef int (*parse_5tuple)(char *text,
77 struct rte_table_acl_rule_add_params *rule);
80 * The order of the fields in the rule string after the initial '@'
85 CB_FLD_SRC_PORT_RANGE,
86 CB_FLD_DST_PORT_RANGE,
92 #define GET_CB_FIELD(in, fd, base, lim, dlm) \
98 val = strtoul((in), &end, (base)); \
99 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
101 (fd) = (typeof(fd)) val; \
109 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
111 uint8_t a, b, c, d, m;
113 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
114 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
115 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
116 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
117 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
119 addr[0] = IPv4(a, b, c, d);
126 parse_port_range(const char *in, uint16_t *port_low, uint16_t *port_high)
130 GET_CB_FIELD(in, a, 0, UINT16_MAX, ':');
131 GET_CB_FIELD(in, b, 0, UINT16_MAX, 0);
140 parse_cb_ipv4_rule(char *str, struct rte_table_acl_rule_add_params *v)
143 char *s, *sp, *in[CB_FLD_NUM];
144 static const char *dlm = " \t\n";
149 if (strchr(str, '@') != str)
155 * Populate the 'in' array with the location of each
156 * field in the string we're parsing
158 for (i = 0; i != DIM(in); i++) {
159 in[i] = strtok_r(s, dlm, &sp);
165 /* Parse x.x.x.x/x */
166 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
167 &v->field_value[SRC_FIELD_IPV4].value.u32,
168 &v->field_value[SRC_FIELD_IPV4].mask_range.u32);
170 RTE_LOG(ERR, PIPELINE, "failed to read src address/mask: %s\n",
171 in[CB_FLD_SRC_ADDR]);
175 printf("V=%u, mask=%u\n", v->field_value[SRC_FIELD_IPV4].value.u32,
176 v->field_value[SRC_FIELD_IPV4].mask_range.u32);
178 /* Parse x.x.x.x/x */
179 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
180 &v->field_value[DST_FIELD_IPV4].value.u32,
181 &v->field_value[DST_FIELD_IPV4].mask_range.u32);
183 RTE_LOG(ERR, PIPELINE, "failed to read dest address/mask: %s\n",
184 in[CB_FLD_DST_ADDR]);
188 printf("V=%u, mask=%u\n", v->field_value[DST_FIELD_IPV4].value.u32,
189 v->field_value[DST_FIELD_IPV4].mask_range.u32);
191 rc = parse_port_range(in[CB_FLD_SRC_PORT_RANGE],
192 &v->field_value[SRCP_FIELD_IPV4].value.u16,
193 &v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
195 RTE_LOG(ERR, PIPELINE, "failed to read source port range: %s\n",
196 in[CB_FLD_SRC_PORT_RANGE]);
200 printf("V=%u, mask=%u\n", v->field_value[SRCP_FIELD_IPV4].value.u16,
201 v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
203 rc = parse_port_range(in[CB_FLD_DST_PORT_RANGE],
204 &v->field_value[DSTP_FIELD_IPV4].value.u16,
205 &v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
207 RTE_LOG(ERR, PIPELINE, "failed to read dest port range: %s\n",
208 in[CB_FLD_DST_PORT_RANGE]);
212 printf("V=%u, mask=%u\n", v->field_value[DSTP_FIELD_IPV4].value.u16,
213 v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
215 GET_CB_FIELD(in[CB_FLD_PROTO],
216 v->field_value[PROTO_FIELD_IPV4].value.u8,
218 GET_CB_FIELD(in[CB_FLD_PROTO],
219 v->field_value[PROTO_FIELD_IPV4].mask_range.u8,
222 printf("V=%u, mask=%u\n",
223 (unsigned int)v->field_value[PROTO_FIELD_IPV4].value.u8,
224 v->field_value[PROTO_FIELD_IPV4].mask_range.u8);
229 parse_cb_ipv4_rule_del(char *str, struct rte_table_acl_rule_delete_params *v)
232 char *s, *sp, *in[CB_FLD_NUM];
233 static const char *dlm = " \t\n";
238 if (strchr(str, '@') != str)
244 * Populate the 'in' array with the location of each
245 * field in the string we're parsing
247 for (i = 0; i != DIM(in); i++) {
248 in[i] = strtok_r(s, dlm, &sp);
254 /* Parse x.x.x.x/x */
255 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
256 &v->field_value[SRC_FIELD_IPV4].value.u32,
257 &v->field_value[SRC_FIELD_IPV4].mask_range.u32);
259 RTE_LOG(ERR, PIPELINE, "failed to read src address/mask: %s\n",
260 in[CB_FLD_SRC_ADDR]);
264 printf("V=%u, mask=%u\n", v->field_value[SRC_FIELD_IPV4].value.u32,
265 v->field_value[SRC_FIELD_IPV4].mask_range.u32);
267 /* Parse x.x.x.x/x */
268 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
269 &v->field_value[DST_FIELD_IPV4].value.u32,
270 &v->field_value[DST_FIELD_IPV4].mask_range.u32);
272 RTE_LOG(ERR, PIPELINE, "failed to read dest address/mask: %s\n",
273 in[CB_FLD_DST_ADDR]);
277 printf("V=%u, mask=%u\n", v->field_value[DST_FIELD_IPV4].value.u32,
278 v->field_value[DST_FIELD_IPV4].mask_range.u32);
280 rc = parse_port_range(in[CB_FLD_SRC_PORT_RANGE],
281 &v->field_value[SRCP_FIELD_IPV4].value.u16,
282 &v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
284 RTE_LOG(ERR, PIPELINE, "failed to read source port range: %s\n",
285 in[CB_FLD_SRC_PORT_RANGE]);
289 printf("V=%u, mask=%u\n", v->field_value[SRCP_FIELD_IPV4].value.u16,
290 v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
292 rc = parse_port_range(in[CB_FLD_DST_PORT_RANGE],
293 &v->field_value[DSTP_FIELD_IPV4].value.u16,
294 &v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
296 RTE_LOG(ERR, PIPELINE, "failed to read dest port range: %s\n",
297 in[CB_FLD_DST_PORT_RANGE]);
301 printf("V=%u, mask=%u\n", v->field_value[DSTP_FIELD_IPV4].value.u16,
302 v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
304 GET_CB_FIELD(in[CB_FLD_PROTO],
305 v->field_value[PROTO_FIELD_IPV4].value.u8,
307 GET_CB_FIELD(in[CB_FLD_PROTO],
308 v->field_value[PROTO_FIELD_IPV4].mask_range.u8,
311 printf("V=%u, mask=%u\n",
312 (unsigned int)v->field_value[PROTO_FIELD_IPV4].value.u8,
313 v->field_value[PROTO_FIELD_IPV4].mask_range.u8);
318 * The format for these rules DO NOT need the port ranges to be
319 * separated by ' : ', just ':'. It's a lot more readable and
322 char lines[][128] = {
323 "@0.0.0.0/0 0.0.0.0/0 0:65535 0:65535 2/0xff", /* Protocol check */
324 "@192.168.3.1/32 0.0.0.0/0 0:65535 0:65535 0/0", /* Src IP checl */
325 "@0.0.0.0/0 10.4.4.1/32 0:65535 0:65535 0/0", /* dst IP check */
326 "@0.0.0.0/0 0.0.0.0/0 105:105 0:65535 0/0", /* src port check */
327 "@0.0.0.0/0 0.0.0.0/0 0:65535 206:206 0/0", /* dst port check */
334 setup_acl_pipeline(void)
338 struct rte_pipeline_params pipeline_params = {
343 struct rte_table_acl_rule_add_params rule_params;
344 struct rte_pipeline_table_acl_rule_delete_params *delete_params;
348 /* Pipeline configuration */
349 p = rte_pipeline_create(&pipeline_params);
351 RTE_LOG(INFO, PIPELINE, "%s: Failed to configure pipeline\n",
356 /* Input port configuration */
357 for (i = 0; i < N_PORTS; i++) {
358 struct rte_port_ring_reader_params port_ring_params = {
362 struct rte_pipeline_port_in_params port_params = {
363 .ops = &rte_port_ring_reader_ops,
364 .arg_create = (void *) &port_ring_params,
366 .burst_size = BURST_SIZE,
369 /* Put in action for some ports */
371 port_params.f_action = port_in_action;
373 ret = rte_pipeline_port_in_create(p, &port_params,
376 rte_panic("Unable to configure input port %d, ret:%d\n",
382 /* output Port configuration */
383 for (i = 0; i < N_PORTS; i++) {
384 struct rte_port_ring_writer_params port_ring_params = {
386 .tx_burst_sz = BURST_SIZE,
389 struct rte_pipeline_port_out_params port_params = {
390 .ops = &rte_port_ring_writer_ops,
391 .arg_create = (void *) &port_ring_params,
397 if (rte_pipeline_port_out_create(p, &port_params,
399 rte_panic("Unable to configure output port %d\n", i);
404 /* Table configuration */
405 for (i = 0; i < N_PORTS; i++) {
406 struct rte_pipeline_table_params table_params;
408 /* Set up defaults for stub */
409 table_params.ops = &rte_table_stub_ops;
410 table_params.arg_create = NULL;
411 table_params.f_action_hit = action_handler_hit;
412 table_params.f_action_miss = NULL;
413 table_params.action_data_size = 0;
415 RTE_LOG(INFO, PIPELINE, "miss_action=%x\n",
416 table_entry_miss_action);
418 printf("RTE_ACL_RULE_SZ(%zu) = %zu\n", DIM(ipv4_defs),
419 RTE_ACL_RULE_SZ(DIM(ipv4_defs)));
421 struct rte_table_acl_params acl_params;
423 acl_params.n_rules = 1 << 5;
424 acl_params.n_rule_fields = DIM(ipv4_defs);
425 snprintf(acl_name, sizeof(acl_name), "ACL%d", i);
426 acl_params.name = acl_name;
427 memcpy(acl_params.field_format, ipv4_defs, sizeof(ipv4_defs));
429 table_params.ops = &rte_table_acl_ops;
430 table_params.arg_create = &acl_params;
432 if (rte_pipeline_table_create(p, &table_params, &table_id[i])) {
433 rte_panic("Unable to configure table %u\n", i);
437 if (connect_miss_action_to_table) {
438 if (rte_pipeline_table_create(p, &table_params,
440 rte_panic("Unable to configure table %u\n", i);
446 for (i = 0; i < N_PORTS; i++) {
447 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
449 rte_panic("Unable to connect input port %u to "
451 port_in_id[i], table_id[i]);
456 /* Add bulk entries to tables */
457 for (i = 0; i < N_PORTS; i++) {
458 struct rte_table_acl_rule_add_params keys[5];
459 struct rte_pipeline_table_entry entries[5];
460 struct rte_table_acl_rule_add_params *key_array[5];
461 struct rte_pipeline_table_entry *table_entries[5];
463 struct rte_pipeline_table_entry *table_entries_ptr[5];
464 struct rte_pipeline_table_entry entries_ptr[5];
466 parser = parse_cb_ipv4_rule;
467 for (n = 0; n < 5; n++) {
468 memset(&keys[n], 0, sizeof(struct rte_table_acl_rule_add_params));
469 key_array[n] = &keys[n];
471 strlcpy(line, lines[n], sizeof(line));
472 printf("PARSING [%s]\n", line);
474 ret = parser(line, &keys[n]);
476 RTE_LOG(ERR, PIPELINE,
477 "line %u: parse_cb_ipv4vlan_rule"
478 " failed, error code: %d (%s)\n",
479 n, ret, strerror(-ret));
483 keys[n].priority = RTE_ACL_MAX_PRIORITY - n - 1;
485 entries[n].action = RTE_PIPELINE_ACTION_PORT;
486 entries[n].port_id = port_out_id[i^1];
487 table_entries[n] = &entries[n];
488 table_entries_ptr[n] = &entries_ptr[n];
491 ret = rte_pipeline_table_entry_add_bulk(p, table_id[i],
492 (void **)key_array, table_entries, 5, key_found, table_entries_ptr);
494 rte_panic("Add entry bulk to table %u failed (%d)\n",
500 /* Delete bulk entries from tables */
501 for (i = 0; i < N_PORTS; i++) {
502 struct rte_table_acl_rule_delete_params keys[5];
503 struct rte_table_acl_rule_delete_params *key_array[5];
504 struct rte_pipeline_table_entry *table_entries[5];
507 memset(table_entries, 0, sizeof(table_entries));
509 for (n = 0; n < 5; n++) {
510 memset(&keys[n], 0, sizeof(struct rte_table_acl_rule_delete_params));
511 key_array[n] = &keys[n];
513 strlcpy(line, lines[n], sizeof(line));
514 printf("PARSING [%s]\n", line);
516 ret = parse_cb_ipv4_rule_del(line, &keys[n]);
518 RTE_LOG(ERR, PIPELINE,
519 "line %u: parse_cb_ipv4vlan_rule"
520 " failed, error code: %d (%s)\n",
521 n, ret, strerror(-ret));
526 ret = rte_pipeline_table_entry_delete_bulk(p, table_id[i],
527 (void **)key_array, 5, key_found, table_entries);
529 rte_panic("Delete bulk entries from table %u failed (%d)\n",
533 printf("Bulk deleted rules.\n");
536 /* Add entries to tables */
537 for (i = 0; i < N_PORTS; i++) {
538 struct rte_pipeline_table_entry table_entry = {
539 .action = RTE_PIPELINE_ACTION_PORT,
540 {.port_id = port_out_id[i^1]},
543 struct rte_pipeline_table_entry *entry_ptr;
545 memset(&rule_params, 0, sizeof(rule_params));
546 parser = parse_cb_ipv4_rule;
548 for (n = 1; n <= 5; n++) {
549 strlcpy(line, lines[n - 1], sizeof(line));
550 printf("PARSING [%s]\n", line);
552 ret = parser(line, &rule_params);
554 RTE_LOG(ERR, PIPELINE,
555 "line %u: parse_cb_ipv4vlan_rule"
556 " failed, error code: %d (%s)\n",
557 n, ret, strerror(-ret));
561 rule_params.priority = RTE_ACL_MAX_PRIORITY - n;
563 ret = rte_pipeline_table_entry_add(p, table_id[i],
565 &table_entry, &key_found, &entry_ptr);
567 rte_panic("Add entry to table %u failed (%d)\n",
573 /* delete a few rules */
574 for (n = 2; n <= 3; n++) {
575 strlcpy(line, lines[n - 1], sizeof(line));
576 printf("PARSING [%s]\n", line);
578 ret = parser(line, &rule_params);
580 RTE_LOG(ERR, PIPELINE, "line %u: parse rule "
581 " failed, error code: %d (%s)\n",
582 n, ret, strerror(-ret));
586 delete_params = (struct
587 rte_pipeline_table_acl_rule_delete_params *)
588 &(rule_params.field_value[0]);
589 ret = rte_pipeline_table_entry_delete(p, table_id[i],
590 delete_params, &key_found, NULL);
592 rte_panic("Add entry to table %u failed (%d)\n",
596 printf("Deleted Rule.\n");
600 /* Try to add duplicates */
601 for (n = 1; n <= 5; n++) {
602 strlcpy(line, lines[n - 1], sizeof(line));
603 printf("PARSING [%s]\n", line);
605 ret = parser(line, &rule_params);
607 RTE_LOG(ERR, PIPELINE, "line %u: parse rule"
608 " failed, error code: %d (%s)\n",
609 n, ret, strerror(-ret));
613 rule_params.priority = RTE_ACL_MAX_PRIORITY - n;
615 ret = rte_pipeline_table_entry_add(p, table_id[i],
617 &table_entry, &key_found, &entry_ptr);
619 rte_panic("Add entry to table %u failed (%d)\n",
626 /* Enable input ports */
627 for (i = 0; i < N_PORTS ; i++)
628 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
629 rte_panic("Unable to enable input port %u\n",
632 /* Check pipeline consistency */
633 if (rte_pipeline_check(p) < 0) {
634 rte_panic("Pipeline consistency check failed\n");
645 test_pipeline_single_filter(int expected_count)
647 int i, j, ret, tx_count;
648 struct ipv4_5tuple five_tuple;
650 /* Allocate a few mbufs and manually insert into the rings. */
651 for (i = 0; i < N_PORTS; i++) {
652 for (j = 0; j < 8; j++) {
653 struct rte_mbuf *mbuf;
655 mbuf = rte_pktmbuf_alloc(pool);
657 /* this will cause test failure after cleanup
658 * of already enqueued mbufs, as the mbuf
659 * counts won't match */
661 memset(rte_pktmbuf_mtod(mbuf, char *), 0x00,
662 sizeof(struct ipv4_5tuple));
664 five_tuple.proto = j;
665 five_tuple.ip_src = rte_bswap32(IPv4(192, 168, j, 1));
666 five_tuple.ip_dst = rte_bswap32(IPv4(10, 4, j, 1));
667 five_tuple.port_src = rte_bswap16(100 + j);
668 five_tuple.port_dst = rte_bswap16(200 + j);
670 memcpy(rte_pktmbuf_mtod(mbuf, char *), &five_tuple,
671 sizeof(struct ipv4_5tuple));
672 RTE_LOG(INFO, PIPELINE, "%s: Enqueue onto ring %d\n",
674 rte_ring_enqueue(rings_rx[i], mbuf);
678 /* Run pipeline once */
679 for (i = 0; i< N_PORTS; i++)
682 rte_pipeline_flush(p);
686 for (i = 0; i < N_PORTS; i++) {
687 void *objs[RING_TX_SIZE];
688 struct rte_mbuf *mbuf;
690 ret = rte_ring_sc_dequeue_burst(rings_tx[i], objs, 10, NULL);
692 printf("Got no objects from ring %d - error code %d\n",
695 printf("Got %d object(s) from ring %d!\n", ret, i);
696 for (j = 0; j < ret; j++) {
698 rte_hexdump(stdout, "mbuf",
699 rte_pktmbuf_mtod(mbuf, char *), 64);
700 rte_pktmbuf_free(mbuf);
706 if (tx_count != expected_count) {
707 RTE_LOG(INFO, PIPELINE,
708 "%s: Unexpected packets for ACL test, "
709 "expected %d, got %d\n",
710 __func__, expected_count, tx_count);
714 rte_pipeline_free(p);
727 override_hit_mask = 0xFF; /* All packets are a hit */
729 setup_acl_pipeline();
730 if (test_pipeline_single_filter(10) < 0)