1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #ifndef RTE_EXEC_ENV_WINDOWS
8 #include <rte_string_fns.h>
9 #include <rte_hexdump.h>
10 #include "test_table.h"
11 #include "test_table_acl.h"
14 * Rule and trace formats definitions.
34 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
36 .type = RTE_ACL_FIELD_TYPE_BITMASK,
37 .size = sizeof(uint8_t),
38 .field_index = PROTO_FIELD_IPV4,
39 .input_index = PROTO_FIELD_IPV4,
40 .offset = offsetof(struct ipv4_5tuple, proto),
43 .type = RTE_ACL_FIELD_TYPE_MASK,
44 .size = sizeof(uint32_t),
45 .field_index = SRC_FIELD_IPV4,
46 .input_index = SRC_FIELD_IPV4,
47 .offset = offsetof(struct ipv4_5tuple, ip_src),
50 .type = RTE_ACL_FIELD_TYPE_MASK,
51 .size = sizeof(uint32_t),
52 .field_index = DST_FIELD_IPV4,
53 .input_index = DST_FIELD_IPV4,
54 .offset = offsetof(struct ipv4_5tuple, ip_dst),
57 .type = RTE_ACL_FIELD_TYPE_RANGE,
58 .size = sizeof(uint16_t),
59 .field_index = SRCP_FIELD_IPV4,
60 .input_index = SRCP_FIELD_IPV4,
61 .offset = offsetof(struct ipv4_5tuple, port_src),
64 .type = RTE_ACL_FIELD_TYPE_RANGE,
65 .size = sizeof(uint16_t),
66 .field_index = DSTP_FIELD_IPV4,
67 .input_index = SRCP_FIELD_IPV4,
68 .offset = offsetof(struct ipv4_5tuple, port_dst),
72 struct rte_table_acl_rule_add_params table_acl_IPv4_rule;
74 typedef int (*parse_5tuple)(char *text,
75 struct rte_table_acl_rule_add_params *rule);
78 * The order of the fields in the rule string after the initial '@'
83 CB_FLD_SRC_PORT_RANGE,
84 CB_FLD_DST_PORT_RANGE,
90 #define GET_CB_FIELD(in, fd, base, lim, dlm) \
96 val = strtoul((in), &end, (base)); \
97 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
99 (fd) = (typeof(fd)) val; \
107 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
109 uint8_t a, b, c, d, m;
111 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
112 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
113 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
114 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
115 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
117 addr[0] = RTE_IPV4(a, b, c, d);
124 parse_port_range(const char *in, uint16_t *port_low, uint16_t *port_high)
128 GET_CB_FIELD(in, a, 0, UINT16_MAX, ':');
129 GET_CB_FIELD(in, b, 0, UINT16_MAX, 0);
138 parse_cb_ipv4_rule(char *str, struct rte_table_acl_rule_add_params *v)
141 char *s, *sp, *in[CB_FLD_NUM];
142 static const char *dlm = " \t\n";
147 if (strchr(str, '@') != str)
153 * Populate the 'in' array with the location of each
154 * field in the string we're parsing
156 for (i = 0; i != DIM(in); i++) {
157 in[i] = strtok_r(s, dlm, &sp);
163 /* Parse x.x.x.x/x */
164 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
165 &v->field_value[SRC_FIELD_IPV4].value.u32,
166 &v->field_value[SRC_FIELD_IPV4].mask_range.u32);
168 RTE_LOG(ERR, PIPELINE, "failed to read src address/mask: %s\n",
169 in[CB_FLD_SRC_ADDR]);
173 printf("V=%u, mask=%u\n", v->field_value[SRC_FIELD_IPV4].value.u32,
174 v->field_value[SRC_FIELD_IPV4].mask_range.u32);
176 /* Parse x.x.x.x/x */
177 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
178 &v->field_value[DST_FIELD_IPV4].value.u32,
179 &v->field_value[DST_FIELD_IPV4].mask_range.u32);
181 RTE_LOG(ERR, PIPELINE, "failed to read dest address/mask: %s\n",
182 in[CB_FLD_DST_ADDR]);
186 printf("V=%u, mask=%u\n", v->field_value[DST_FIELD_IPV4].value.u32,
187 v->field_value[DST_FIELD_IPV4].mask_range.u32);
189 rc = parse_port_range(in[CB_FLD_SRC_PORT_RANGE],
190 &v->field_value[SRCP_FIELD_IPV4].value.u16,
191 &v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
193 RTE_LOG(ERR, PIPELINE, "failed to read source port range: %s\n",
194 in[CB_FLD_SRC_PORT_RANGE]);
198 printf("V=%u, mask=%u\n", v->field_value[SRCP_FIELD_IPV4].value.u16,
199 v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
201 rc = parse_port_range(in[CB_FLD_DST_PORT_RANGE],
202 &v->field_value[DSTP_FIELD_IPV4].value.u16,
203 &v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
205 RTE_LOG(ERR, PIPELINE, "failed to read dest port range: %s\n",
206 in[CB_FLD_DST_PORT_RANGE]);
210 printf("V=%u, mask=%u\n", v->field_value[DSTP_FIELD_IPV4].value.u16,
211 v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
213 GET_CB_FIELD(in[CB_FLD_PROTO],
214 v->field_value[PROTO_FIELD_IPV4].value.u8,
216 GET_CB_FIELD(in[CB_FLD_PROTO],
217 v->field_value[PROTO_FIELD_IPV4].mask_range.u8,
220 printf("V=%u, mask=%u\n",
221 (unsigned int)v->field_value[PROTO_FIELD_IPV4].value.u8,
222 v->field_value[PROTO_FIELD_IPV4].mask_range.u8);
227 parse_cb_ipv4_rule_del(char *str, struct rte_table_acl_rule_delete_params *v)
230 char *s, *sp, *in[CB_FLD_NUM];
231 static const char *dlm = " \t\n";
236 if (strchr(str, '@') != str)
242 * Populate the 'in' array with the location of each
243 * field in the string we're parsing
245 for (i = 0; i != DIM(in); i++) {
246 in[i] = strtok_r(s, dlm, &sp);
252 /* Parse x.x.x.x/x */
253 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
254 &v->field_value[SRC_FIELD_IPV4].value.u32,
255 &v->field_value[SRC_FIELD_IPV4].mask_range.u32);
257 RTE_LOG(ERR, PIPELINE, "failed to read src address/mask: %s\n",
258 in[CB_FLD_SRC_ADDR]);
262 printf("V=%u, mask=%u\n", v->field_value[SRC_FIELD_IPV4].value.u32,
263 v->field_value[SRC_FIELD_IPV4].mask_range.u32);
265 /* Parse x.x.x.x/x */
266 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
267 &v->field_value[DST_FIELD_IPV4].value.u32,
268 &v->field_value[DST_FIELD_IPV4].mask_range.u32);
270 RTE_LOG(ERR, PIPELINE, "failed to read dest address/mask: %s\n",
271 in[CB_FLD_DST_ADDR]);
275 printf("V=%u, mask=%u\n", v->field_value[DST_FIELD_IPV4].value.u32,
276 v->field_value[DST_FIELD_IPV4].mask_range.u32);
278 rc = parse_port_range(in[CB_FLD_SRC_PORT_RANGE],
279 &v->field_value[SRCP_FIELD_IPV4].value.u16,
280 &v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
282 RTE_LOG(ERR, PIPELINE, "failed to read source port range: %s\n",
283 in[CB_FLD_SRC_PORT_RANGE]);
287 printf("V=%u, mask=%u\n", v->field_value[SRCP_FIELD_IPV4].value.u16,
288 v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
290 rc = parse_port_range(in[CB_FLD_DST_PORT_RANGE],
291 &v->field_value[DSTP_FIELD_IPV4].value.u16,
292 &v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
294 RTE_LOG(ERR, PIPELINE, "failed to read dest port range: %s\n",
295 in[CB_FLD_DST_PORT_RANGE]);
299 printf("V=%u, mask=%u\n", v->field_value[DSTP_FIELD_IPV4].value.u16,
300 v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
302 GET_CB_FIELD(in[CB_FLD_PROTO],
303 v->field_value[PROTO_FIELD_IPV4].value.u8,
305 GET_CB_FIELD(in[CB_FLD_PROTO],
306 v->field_value[PROTO_FIELD_IPV4].mask_range.u8,
309 printf("V=%u, mask=%u\n",
310 (unsigned int)v->field_value[PROTO_FIELD_IPV4].value.u8,
311 v->field_value[PROTO_FIELD_IPV4].mask_range.u8);
316 * The format for these rules DO NOT need the port ranges to be
317 * separated by ' : ', just ':'. It's a lot more readable and
320 char lines[][128] = {
321 "@0.0.0.0/0 0.0.0.0/0 0:65535 0:65535 2/0xff", /* Protocol check */
322 "@192.168.3.1/32 0.0.0.0/0 0:65535 0:65535 0/0", /* Src IP checl */
323 "@0.0.0.0/0 10.4.4.1/32 0:65535 0:65535 0/0", /* dst IP check */
324 "@0.0.0.0/0 0.0.0.0/0 105:105 0:65535 0/0", /* src port check */
325 "@0.0.0.0/0 0.0.0.0/0 0:65535 206:206 0/0", /* dst port check */
332 setup_acl_pipeline(void)
336 struct rte_pipeline_params pipeline_params = {
341 struct rte_table_acl_rule_add_params rule_params;
342 struct rte_pipeline_table_acl_rule_delete_params *delete_params;
346 /* Pipeline configuration */
347 p = rte_pipeline_create(&pipeline_params);
349 RTE_LOG(INFO, PIPELINE, "%s: Failed to configure pipeline\n",
354 /* Input port configuration */
355 for (i = 0; i < N_PORTS; i++) {
356 struct rte_port_ring_reader_params port_ring_params = {
360 struct rte_pipeline_port_in_params port_params = {
361 .ops = &rte_port_ring_reader_ops,
362 .arg_create = (void *) &port_ring_params,
364 .burst_size = BURST_SIZE,
367 /* Put in action for some ports */
369 port_params.f_action = port_in_action;
371 ret = rte_pipeline_port_in_create(p, &port_params,
374 rte_panic("Unable to configure input port %d, ret:%d\n",
380 /* output Port configuration */
381 for (i = 0; i < N_PORTS; i++) {
382 struct rte_port_ring_writer_params port_ring_params = {
384 .tx_burst_sz = BURST_SIZE,
387 struct rte_pipeline_port_out_params port_params = {
388 .ops = &rte_port_ring_writer_ops,
389 .arg_create = (void *) &port_ring_params,
395 if (rte_pipeline_port_out_create(p, &port_params,
397 rte_panic("Unable to configure output port %d\n", i);
402 /* Table configuration */
403 for (i = 0; i < N_PORTS; i++) {
404 struct rte_pipeline_table_params table_params;
406 /* Set up defaults for stub */
407 table_params.ops = &rte_table_stub_ops;
408 table_params.arg_create = NULL;
409 table_params.f_action_hit = action_handler_hit;
410 table_params.f_action_miss = NULL;
411 table_params.action_data_size = 0;
413 RTE_LOG(INFO, PIPELINE, "miss_action=%x\n",
414 table_entry_miss_action);
416 printf("RTE_ACL_RULE_SZ(%zu) = %zu\n", DIM(ipv4_defs),
417 RTE_ACL_RULE_SZ(DIM(ipv4_defs)));
419 struct rte_table_acl_params acl_params;
421 acl_params.n_rules = 1 << 5;
422 acl_params.n_rule_fields = DIM(ipv4_defs);
423 snprintf(acl_name, sizeof(acl_name), "ACL%d", i);
424 acl_params.name = acl_name;
425 memcpy(acl_params.field_format, ipv4_defs, sizeof(ipv4_defs));
427 table_params.ops = &rte_table_acl_ops;
428 table_params.arg_create = &acl_params;
430 if (rte_pipeline_table_create(p, &table_params, &table_id[i])) {
431 rte_panic("Unable to configure table %u\n", i);
435 if (connect_miss_action_to_table) {
436 if (rte_pipeline_table_create(p, &table_params,
438 rte_panic("Unable to configure table %u\n", i);
444 for (i = 0; i < N_PORTS; i++) {
445 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
447 rte_panic("Unable to connect input port %u to "
449 port_in_id[i], table_id[i]);
454 /* Add bulk entries to tables */
455 for (i = 0; i < N_PORTS; i++) {
456 struct rte_table_acl_rule_add_params keys[5];
457 struct rte_pipeline_table_entry entries[5];
458 struct rte_table_acl_rule_add_params *key_array[5];
459 struct rte_pipeline_table_entry *table_entries[5];
461 struct rte_pipeline_table_entry *table_entries_ptr[5];
462 struct rte_pipeline_table_entry entries_ptr[5];
464 parser = parse_cb_ipv4_rule;
465 for (n = 0; n < 5; n++) {
466 memset(&keys[n], 0, sizeof(struct rte_table_acl_rule_add_params));
467 key_array[n] = &keys[n];
469 strlcpy(line, lines[n], sizeof(line));
470 printf("PARSING [%s]\n", line);
472 ret = parser(line, &keys[n]);
474 RTE_LOG(ERR, PIPELINE,
475 "line %u: parse_cb_ipv4vlan_rule"
476 " failed, error code: %d (%s)\n",
477 n, ret, strerror(-ret));
481 keys[n].priority = RTE_ACL_MAX_PRIORITY - n - 1;
483 entries[n].action = RTE_PIPELINE_ACTION_PORT;
484 entries[n].port_id = port_out_id[i^1];
485 table_entries[n] = &entries[n];
486 table_entries_ptr[n] = &entries_ptr[n];
489 ret = rte_pipeline_table_entry_add_bulk(p, table_id[i],
490 (void **)key_array, table_entries, 5, key_found, table_entries_ptr);
492 rte_panic("Add entry bulk to table %u failed (%d)\n",
498 /* Delete bulk entries from tables */
499 for (i = 0; i < N_PORTS; i++) {
500 struct rte_table_acl_rule_delete_params keys[5];
501 struct rte_table_acl_rule_delete_params *key_array[5];
502 struct rte_pipeline_table_entry *table_entries[5];
505 memset(table_entries, 0, sizeof(table_entries));
507 for (n = 0; n < 5; n++) {
508 memset(&keys[n], 0, sizeof(struct rte_table_acl_rule_delete_params));
509 key_array[n] = &keys[n];
511 strlcpy(line, lines[n], sizeof(line));
512 printf("PARSING [%s]\n", line);
514 ret = parse_cb_ipv4_rule_del(line, &keys[n]);
516 RTE_LOG(ERR, PIPELINE,
517 "line %u: parse_cb_ipv4vlan_rule"
518 " failed, error code: %d (%s)\n",
519 n, ret, strerror(-ret));
524 ret = rte_pipeline_table_entry_delete_bulk(p, table_id[i],
525 (void **)key_array, 5, key_found, table_entries);
527 rte_panic("Delete bulk entries from table %u failed (%d)\n",
531 printf("Bulk deleted rules.\n");
534 /* Add entries to tables */
535 for (i = 0; i < N_PORTS; i++) {
536 struct rte_pipeline_table_entry table_entry = {
537 .action = RTE_PIPELINE_ACTION_PORT,
538 {.port_id = port_out_id[i^1]},
541 struct rte_pipeline_table_entry *entry_ptr;
543 memset(&rule_params, 0, sizeof(rule_params));
544 parser = parse_cb_ipv4_rule;
546 for (n = 1; n <= 5; n++) {
547 strlcpy(line, lines[n - 1], sizeof(line));
548 printf("PARSING [%s]\n", line);
550 ret = parser(line, &rule_params);
552 RTE_LOG(ERR, PIPELINE,
553 "line %u: parse_cb_ipv4vlan_rule"
554 " failed, error code: %d (%s)\n",
555 n, ret, strerror(-ret));
559 rule_params.priority = RTE_ACL_MAX_PRIORITY - n;
561 ret = rte_pipeline_table_entry_add(p, table_id[i],
563 &table_entry, &key_found, &entry_ptr);
565 rte_panic("Add entry to table %u failed (%d)\n",
571 /* delete a few rules */
572 for (n = 2; n <= 3; n++) {
573 strlcpy(line, lines[n - 1], sizeof(line));
574 printf("PARSING [%s]\n", line);
576 ret = parser(line, &rule_params);
578 RTE_LOG(ERR, PIPELINE, "line %u: parse rule "
579 " failed, error code: %d (%s)\n",
580 n, ret, strerror(-ret));
584 delete_params = (struct
585 rte_pipeline_table_acl_rule_delete_params *)
586 &(rule_params.field_value[0]);
587 ret = rte_pipeline_table_entry_delete(p, table_id[i],
588 delete_params, &key_found, NULL);
590 rte_panic("Add entry to table %u failed (%d)\n",
594 printf("Deleted Rule.\n");
598 /* Try to add duplicates */
599 for (n = 1; n <= 5; n++) {
600 strlcpy(line, lines[n - 1], sizeof(line));
601 printf("PARSING [%s]\n", line);
603 ret = parser(line, &rule_params);
605 RTE_LOG(ERR, PIPELINE, "line %u: parse rule"
606 " failed, error code: %d (%s)\n",
607 n, ret, strerror(-ret));
611 rule_params.priority = RTE_ACL_MAX_PRIORITY - n;
613 ret = rte_pipeline_table_entry_add(p, table_id[i],
615 &table_entry, &key_found, &entry_ptr);
617 rte_panic("Add entry to table %u failed (%d)\n",
624 /* Enable input ports */
625 for (i = 0; i < N_PORTS ; i++)
626 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
627 rte_panic("Unable to enable input port %u\n",
630 /* Check pipeline consistency */
631 if (rte_pipeline_check(p) < 0) {
632 rte_panic("Pipeline consistency check failed\n");
643 test_pipeline_single_filter(int expected_count)
645 int i, j, ret, tx_count;
646 struct ipv4_5tuple five_tuple;
648 /* Allocate a few mbufs and manually insert into the rings. */
649 for (i = 0; i < N_PORTS; i++) {
650 for (j = 0; j < 8; j++) {
651 struct rte_mbuf *mbuf;
653 mbuf = rte_pktmbuf_alloc(pool);
655 /* this will cause test failure after cleanup
656 * of already enqueued mbufs, as the mbuf
657 * counts won't match */
659 memset(rte_pktmbuf_mtod(mbuf, char *), 0x00,
660 sizeof(struct ipv4_5tuple));
662 five_tuple.proto = j;
663 five_tuple.ip_src = rte_bswap32(RTE_IPV4(192, 168, j, 1));
664 five_tuple.ip_dst = rte_bswap32(RTE_IPV4(10, 4, j, 1));
665 five_tuple.port_src = rte_bswap16(100 + j);
666 five_tuple.port_dst = rte_bswap16(200 + j);
668 memcpy(rte_pktmbuf_mtod(mbuf, char *), &five_tuple,
669 sizeof(struct ipv4_5tuple));
670 RTE_LOG(INFO, PIPELINE, "%s: Enqueue onto ring %d\n",
672 rte_ring_enqueue(rings_rx[i], mbuf);
676 /* Run pipeline once */
677 for (i = 0; i< N_PORTS; i++)
680 rte_pipeline_flush(p);
684 for (i = 0; i < N_PORTS; i++) {
685 void *objs[RING_TX_SIZE];
686 struct rte_mbuf *mbuf;
688 ret = rte_ring_sc_dequeue_burst(rings_tx[i], objs, 10, NULL);
690 printf("Got no objects from ring %d - error code %d\n",
693 printf("Got %d object(s) from ring %d!\n", ret, i);
694 for (j = 0; j < ret; j++) {
696 rte_hexdump(stdout, "mbuf",
697 rte_pktmbuf_mtod(mbuf, char *), 64);
698 rte_pktmbuf_free(mbuf);
704 if (tx_count != expected_count) {
705 RTE_LOG(INFO, PIPELINE,
706 "%s: Unexpected packets for ACL test, "
707 "expected %d, got %d\n",
708 __func__, expected_count, tx_count);
712 rte_pipeline_free(p);
725 override_hit_mask = 0xFF; /* All packets are a hit */
727 setup_acl_pipeline();
728 if (test_pipeline_single_filter(10) < 0)
734 #endif /* !RTE_EXEC_ENV_WINDOWS */