4 * Copyright(c) 2010-2014 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.
36 #include <rte_hexdump.h>
37 #include "test_table.h"
38 #include "test_table_acl.h"
40 #define IPv4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
41 (((b) & 0xff) << 16) | \
42 (((c) & 0xff) << 8) | \
45 static const char cb_port_delim[] = ":";
48 * Rule and trace formats definitions.
68 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
70 .type = RTE_ACL_FIELD_TYPE_BITMASK,
71 .size = sizeof(uint8_t),
72 .field_index = PROTO_FIELD_IPV4,
73 .input_index = PROTO_FIELD_IPV4,
74 .offset = offsetof(struct ipv4_5tuple, proto),
77 .type = RTE_ACL_FIELD_TYPE_MASK,
78 .size = sizeof(uint32_t),
79 .field_index = SRC_FIELD_IPV4,
80 .input_index = SRC_FIELD_IPV4,
81 .offset = offsetof(struct ipv4_5tuple, ip_src),
84 .type = RTE_ACL_FIELD_TYPE_MASK,
85 .size = sizeof(uint32_t),
86 .field_index = DST_FIELD_IPV4,
87 .input_index = DST_FIELD_IPV4,
88 .offset = offsetof(struct ipv4_5tuple, ip_dst),
91 .type = RTE_ACL_FIELD_TYPE_RANGE,
92 .size = sizeof(uint16_t),
93 .field_index = SRCP_FIELD_IPV4,
94 .input_index = SRCP_FIELD_IPV4,
95 .offset = offsetof(struct ipv4_5tuple, port_src),
98 .type = RTE_ACL_FIELD_TYPE_RANGE,
99 .size = sizeof(uint16_t),
100 .field_index = DSTP_FIELD_IPV4,
101 .input_index = SRCP_FIELD_IPV4,
102 .offset = offsetof(struct ipv4_5tuple, port_dst),
106 struct rte_table_acl_rule_add_params table_acl_IPv4_rule;
108 typedef int (*parse_5tuple)(char *text,
109 struct rte_table_acl_rule_add_params *rule);
112 * The order of the fields in the rule string after the initial '@'
117 CB_FLD_SRC_PORT_RANGE,
118 CB_FLD_DST_PORT_RANGE,
124 #define GET_CB_FIELD(in, fd, base, lim, dlm) \
130 val = strtoul((in), &end, (base)); \
131 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
133 (fd) = (typeof(fd)) val; \
141 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
143 uint8_t a, b, c, d, m;
145 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
146 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
147 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
148 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
149 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
151 addr[0] = IPv4(a, b, c, d);
158 parse_port_range(const char *in, uint16_t *port_low, uint16_t *port_high)
162 GET_CB_FIELD(in, a, 0, UINT16_MAX, ':');
163 GET_CB_FIELD(in, b, 0, UINT16_MAX, 0);
172 parse_cb_ipv4_rule(char *str, struct rte_table_acl_rule_add_params *v)
175 char *s, *sp, *in[CB_FLD_NUM];
176 static const char *dlm = " \t\n";
181 if (strchr(str, '@') != str)
187 * Populate the 'in' array with the location of each
188 * field in the string we're parsing
190 for (i = 0; i != DIM(in); i++) {
191 in[i] = strtok_r(s, dlm, &sp);
197 /* Parse x.x.x.x/x */
198 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
199 &v->field_value[SRC_FIELD_IPV4].value.u32,
200 &v->field_value[SRC_FIELD_IPV4].mask_range.u32);
202 RTE_LOG(ERR, PIPELINE, "failed to read src address/mask: %s\n",
203 in[CB_FLD_SRC_ADDR]);
207 printf("V=%u, mask=%u\n", v->field_value[SRC_FIELD_IPV4].value.u32,
208 v->field_value[SRC_FIELD_IPV4].mask_range.u32);
210 /* Parse x.x.x.x/x */
211 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
212 &v->field_value[DST_FIELD_IPV4].value.u32,
213 &v->field_value[DST_FIELD_IPV4].mask_range.u32);
215 RTE_LOG(ERR, PIPELINE, "failed to read dest address/mask: %s\n",
216 in[CB_FLD_DST_ADDR]);
220 printf("V=%u, mask=%u\n", v->field_value[DST_FIELD_IPV4].value.u32,
221 v->field_value[DST_FIELD_IPV4].mask_range.u32);
223 rc = parse_port_range(in[CB_FLD_SRC_PORT_RANGE],
224 &v->field_value[SRCP_FIELD_IPV4].value.u16,
225 &v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
227 RTE_LOG(ERR, PIPELINE, "failed to read source port range: %s\n",
228 in[CB_FLD_SRC_PORT_RANGE]);
232 printf("V=%u, mask=%u\n", v->field_value[SRCP_FIELD_IPV4].value.u16,
233 v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
235 rc = parse_port_range(in[CB_FLD_DST_PORT_RANGE],
236 &v->field_value[DSTP_FIELD_IPV4].value.u16,
237 &v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
239 RTE_LOG(ERR, PIPELINE, "failed to read dest port range: %s\n",
240 in[CB_FLD_DST_PORT_RANGE]);
244 printf("V=%u, mask=%u\n", v->field_value[DSTP_FIELD_IPV4].value.u16,
245 v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
247 GET_CB_FIELD(in[CB_FLD_PROTO],
248 v->field_value[PROTO_FIELD_IPV4].value.u8,
250 GET_CB_FIELD(in[CB_FLD_PROTO],
251 v->field_value[PROTO_FIELD_IPV4].mask_range.u8,
254 printf("V=%u, mask=%u\n",
255 (unsigned int)v->field_value[PROTO_FIELD_IPV4].value.u8,
256 v->field_value[PROTO_FIELD_IPV4].mask_range.u8);
262 * The format for these rules DO NOT need the port ranges to be
263 * separated by ' : ', just ':'. It's a lot more readable and
266 char lines[][128] = {
267 "@0.0.0.0/0 0.0.0.0/0 0:65535 0:65535 2/0xff", /* Protocol check */
268 "@192.168.3.1/32 0.0.0.0/0 0:65535 0:65535 0/0", /* Src IP checl */
269 "@0.0.0.0/0 10.4.4.1/32 0:65535 0:65535 0/0", /* dst IP check */
270 "@0.0.0.0/0 0.0.0.0/0 105:105 0:65535 0/0", /* src port check */
271 "@0.0.0.0/0 0.0.0.0/0 0:65535 206:206 0/0", /* dst port check */
278 setup_acl_pipeline(void)
282 struct rte_pipeline_params pipeline_params = {
287 struct rte_table_acl_rule_add_params rule_params;
288 struct rte_pipeline_table_acl_rule_delete_params *delete_params;
292 /* Pipeline configuration */
293 p = rte_pipeline_create(&pipeline_params);
295 RTE_LOG(INFO, PIPELINE, "%s: Failed to configure pipeline\n",
300 /* Input port configuration */
301 for (i = 0; i < N_PORTS; i++) {
302 struct rte_port_ring_reader_params port_ring_params = {
306 struct rte_pipeline_port_in_params port_params = {
307 .ops = &rte_port_ring_reader_ops,
308 .arg_create = (void *) &port_ring_params,
310 .burst_size = BURST_SIZE,
313 /* Put in action for some ports */
315 port_params.f_action = port_in_action;
317 ret = rte_pipeline_port_in_create(p, &port_params,
320 rte_panic("Unable to configure input port %d, ret:%d\n",
326 /* output Port configuration */
327 for (i = 0; i < N_PORTS; i++) {
328 struct rte_port_ring_writer_params port_ring_params = {
330 .tx_burst_sz = BURST_SIZE,
333 struct rte_pipeline_port_out_params port_params = {
334 .ops = &rte_port_ring_writer_ops,
335 .arg_create = (void *) &port_ring_params,
341 if (rte_pipeline_port_out_create(p, &port_params,
343 rte_panic("Unable to configure output port %d\n", i);
348 /* Table configuration */
349 for (i = 0; i < N_PORTS; i++) {
350 struct rte_pipeline_table_params table_params;
352 /* Set up defaults for stub */
353 table_params.ops = &rte_table_stub_ops;
354 table_params.arg_create = NULL;
355 table_params.f_action_hit = action_handler_hit;
356 table_params.f_action_miss = NULL;
357 table_params.action_data_size = 0;
359 RTE_LOG(INFO, PIPELINE, "miss_action=%x\n",
360 table_entry_miss_action);
362 printf("RTE_ACL_RULE_SZ(%zu) = %zu\n", DIM(ipv4_defs),
363 RTE_ACL_RULE_SZ(DIM(ipv4_defs)));
365 struct rte_table_acl_params acl_params;
367 acl_params.n_rules = 1 << 5;
368 acl_params.n_rule_fields = DIM(ipv4_defs);
369 rte_snprintf(acl_name, sizeof(acl_name), "ACL%d", i);
370 acl_params.name = acl_name;
371 memcpy(acl_params.field_format, ipv4_defs, sizeof(ipv4_defs));
373 table_params.ops = &rte_table_acl_ops;
374 table_params.arg_create = &acl_params;
376 if (rte_pipeline_table_create(p, &table_params, &table_id[i])) {
377 rte_panic("Unable to configure table %u\n", i);
381 if (connect_miss_action_to_table) {
382 if (rte_pipeline_table_create(p, &table_params,
384 rte_panic("Unable to configure table %u\n", i);
390 for (i = 0; i < N_PORTS; i++) {
391 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
393 rte_panic("Unable to connect input port %u to "
395 port_in_id[i], table_id[i]);
400 /* Add entries to tables */
401 for (i = 0; i < N_PORTS; i++) {
402 struct rte_pipeline_table_entry table_entry = {
403 .action = RTE_PIPELINE_ACTION_PORT,
404 {.port_id = port_out_id[i^1]},
407 struct rte_pipeline_table_entry *entry_ptr;
409 memset(&rule_params, 0, sizeof(rule_params));
410 parser = parse_cb_ipv4_rule;
412 for (n = 1; n <= 5; n++) {
413 rte_snprintf(line, sizeof(line), "%s", lines[n-1]);
414 printf("PARSING [%s]\n", line);
416 ret = parser(line, &rule_params);
418 RTE_LOG(ERR, PIPELINE,
419 "line %u: parse_cb_ipv4vlan_rule"
420 " failed, error code: %d (%s)\n",
421 n, ret, strerror(-ret));
425 rule_params.priority = RTE_ACL_MAX_PRIORITY - n;
427 ret = rte_pipeline_table_entry_add(p, table_id[i],
429 &table_entry, &key_found, &entry_ptr);
431 rte_panic("Add entry to table %u failed (%d)\n",
437 /* delete a few rules */
438 for (n = 2; n <= 3; n++) {
439 rte_snprintf(line, sizeof(line), "%s", lines[n-1]);
440 printf("PARSING [%s]\n", line);
442 ret = parser(line, &rule_params);
444 RTE_LOG(ERR, PIPELINE, "line %u: parse rule "
445 " failed, error code: %d (%s)\n",
446 n, ret, strerror(-ret));
450 delete_params = (struct
451 rte_pipeline_table_acl_rule_delete_params *)
452 &(rule_params.field_value[0]);
453 ret = rte_pipeline_table_entry_delete(p, table_id[i],
454 delete_params, &key_found, NULL);
456 rte_panic("Add entry to table %u failed (%d)\n",
460 printf("Deleted Rule.\n");
464 /* Try to add duplicates */
465 for (n = 1; n <= 5; n++) {
466 rte_snprintf(line, sizeof(line), "%s", lines[n-1]);
467 printf("PARSING [%s]\n", line);
469 ret = parser(line, &rule_params);
471 RTE_LOG(ERR, PIPELINE, "line %u: parse rule"
472 " failed, error code: %d (%s)\n",
473 n, ret, strerror(-ret));
477 rule_params.priority = RTE_ACL_MAX_PRIORITY - n;
479 ret = rte_pipeline_table_entry_add(p, table_id[i],
481 &table_entry, &key_found, &entry_ptr);
483 rte_panic("Add entry to table %u failed (%d)\n",
490 /* Enable input ports */
491 for (i = 0; i < N_PORTS ; i++)
492 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
493 rte_panic("Unable to enable input port %u\n",
496 /* Check pipeline consistency */
497 if (rte_pipeline_check(p) < 0) {
498 rte_panic("Pipeline consistency check failed\n");
509 test_pipeline_single_filter(int expected_count)
511 int i, j, ret, tx_count;
512 struct ipv4_5tuple five_tuple;
514 /* Allocate a few mbufs and manually insert into the rings. */
515 for (i = 0; i < N_PORTS; i++) {
516 for (j = 0; j < 8; j++) {
517 struct rte_mbuf *mbuf;
519 mbuf = rte_pktmbuf_alloc(pool);
520 memset(mbuf->pkt.data, 0x00,
521 sizeof(struct ipv4_5tuple));
523 five_tuple.proto = j;
524 five_tuple.ip_src = rte_bswap32(IPv4(192, 168, j, 1));
525 five_tuple.ip_dst = rte_bswap32(IPv4(10, 4, j, 1));
526 five_tuple.port_src = rte_bswap16(100 + j);
527 five_tuple.port_dst = rte_bswap16(200 + j);
529 memcpy(mbuf->pkt.data, &five_tuple,
530 sizeof(struct ipv4_5tuple));
531 RTE_LOG(INFO, PIPELINE, "%s: Enqueue onto ring %d\n",
533 rte_ring_enqueue(rings_rx[i], mbuf);
537 /* Run pipeline once */
540 rte_pipeline_flush(p);
544 for (i = 0; i < N_PORTS; i++) {
545 void *objs[RING_TX_SIZE];
546 struct rte_mbuf *mbuf;
548 ret = rte_ring_sc_dequeue_burst(rings_tx[i], objs, 10);
550 printf("Got no objects from ring %d - error code %d\n",
553 printf("Got %d object(s) from ring %d!\n", ret, i);
554 for (j = 0; j < ret; j++) {
555 mbuf = (struct rte_mbuf *)objs[j];
556 rte_hexdump(stdout, "mbuf", mbuf->pkt.data, 64);
557 rte_pktmbuf_free(mbuf);
563 if (tx_count != expected_count) {
564 RTE_LOG(INFO, PIPELINE,
565 "%s: Unexpected packets for ACL test, "
566 "expected %d, got %d\n",
567 __func__, expected_count, tx_count);
571 rte_pipeline_free(p);
584 override_hit_mask = 0xFF; /* All packets are a hit */
586 setup_acl_pipeline();
587 if (test_pipeline_single_filter(10) < 0)