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) | \
46 * Rule and trace formats definitions.
66 struct rte_acl_field_def ipv4_defs[NUM_FIELDS_IPV4] = {
68 .type = RTE_ACL_FIELD_TYPE_BITMASK,
69 .size = sizeof(uint8_t),
70 .field_index = PROTO_FIELD_IPV4,
71 .input_index = PROTO_FIELD_IPV4,
72 .offset = offsetof(struct ipv4_5tuple, proto),
75 .type = RTE_ACL_FIELD_TYPE_MASK,
76 .size = sizeof(uint32_t),
77 .field_index = SRC_FIELD_IPV4,
78 .input_index = SRC_FIELD_IPV4,
79 .offset = offsetof(struct ipv4_5tuple, ip_src),
82 .type = RTE_ACL_FIELD_TYPE_MASK,
83 .size = sizeof(uint32_t),
84 .field_index = DST_FIELD_IPV4,
85 .input_index = DST_FIELD_IPV4,
86 .offset = offsetof(struct ipv4_5tuple, ip_dst),
89 .type = RTE_ACL_FIELD_TYPE_RANGE,
90 .size = sizeof(uint16_t),
91 .field_index = SRCP_FIELD_IPV4,
92 .input_index = SRCP_FIELD_IPV4,
93 .offset = offsetof(struct ipv4_5tuple, port_src),
96 .type = RTE_ACL_FIELD_TYPE_RANGE,
97 .size = sizeof(uint16_t),
98 .field_index = DSTP_FIELD_IPV4,
99 .input_index = SRCP_FIELD_IPV4,
100 .offset = offsetof(struct ipv4_5tuple, port_dst),
104 struct rte_table_acl_rule_add_params table_acl_IPv4_rule;
106 typedef int (*parse_5tuple)(char *text,
107 struct rte_table_acl_rule_add_params *rule);
110 * The order of the fields in the rule string after the initial '@'
115 CB_FLD_SRC_PORT_RANGE,
116 CB_FLD_DST_PORT_RANGE,
122 #define GET_CB_FIELD(in, fd, base, lim, dlm) \
128 val = strtoul((in), &end, (base)); \
129 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
131 (fd) = (typeof(fd)) val; \
139 parse_ipv4_net(const char *in, uint32_t *addr, uint32_t *mask_len)
141 uint8_t a, b, c, d, m;
143 GET_CB_FIELD(in, a, 0, UINT8_MAX, '.');
144 GET_CB_FIELD(in, b, 0, UINT8_MAX, '.');
145 GET_CB_FIELD(in, c, 0, UINT8_MAX, '.');
146 GET_CB_FIELD(in, d, 0, UINT8_MAX, '/');
147 GET_CB_FIELD(in, m, 0, sizeof(uint32_t) * CHAR_BIT, 0);
149 addr[0] = IPv4(a, b, c, d);
156 parse_port_range(const char *in, uint16_t *port_low, uint16_t *port_high)
160 GET_CB_FIELD(in, a, 0, UINT16_MAX, ':');
161 GET_CB_FIELD(in, b, 0, UINT16_MAX, 0);
170 parse_cb_ipv4_rule(char *str, struct rte_table_acl_rule_add_params *v)
173 char *s, *sp, *in[CB_FLD_NUM];
174 static const char *dlm = " \t\n";
179 if (strchr(str, '@') != str)
185 * Populate the 'in' array with the location of each
186 * field in the string we're parsing
188 for (i = 0; i != DIM(in); i++) {
189 in[i] = strtok_r(s, dlm, &sp);
195 /* Parse x.x.x.x/x */
196 rc = parse_ipv4_net(in[CB_FLD_SRC_ADDR],
197 &v->field_value[SRC_FIELD_IPV4].value.u32,
198 &v->field_value[SRC_FIELD_IPV4].mask_range.u32);
200 RTE_LOG(ERR, PIPELINE, "failed to read src address/mask: %s\n",
201 in[CB_FLD_SRC_ADDR]);
205 printf("V=%u, mask=%u\n", v->field_value[SRC_FIELD_IPV4].value.u32,
206 v->field_value[SRC_FIELD_IPV4].mask_range.u32);
208 /* Parse x.x.x.x/x */
209 rc = parse_ipv4_net(in[CB_FLD_DST_ADDR],
210 &v->field_value[DST_FIELD_IPV4].value.u32,
211 &v->field_value[DST_FIELD_IPV4].mask_range.u32);
213 RTE_LOG(ERR, PIPELINE, "failed to read dest address/mask: %s\n",
214 in[CB_FLD_DST_ADDR]);
218 printf("V=%u, mask=%u\n", v->field_value[DST_FIELD_IPV4].value.u32,
219 v->field_value[DST_FIELD_IPV4].mask_range.u32);
221 rc = parse_port_range(in[CB_FLD_SRC_PORT_RANGE],
222 &v->field_value[SRCP_FIELD_IPV4].value.u16,
223 &v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
225 RTE_LOG(ERR, PIPELINE, "failed to read source port range: %s\n",
226 in[CB_FLD_SRC_PORT_RANGE]);
230 printf("V=%u, mask=%u\n", v->field_value[SRCP_FIELD_IPV4].value.u16,
231 v->field_value[SRCP_FIELD_IPV4].mask_range.u16);
233 rc = parse_port_range(in[CB_FLD_DST_PORT_RANGE],
234 &v->field_value[DSTP_FIELD_IPV4].value.u16,
235 &v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
237 RTE_LOG(ERR, PIPELINE, "failed to read dest port range: %s\n",
238 in[CB_FLD_DST_PORT_RANGE]);
242 printf("V=%u, mask=%u\n", v->field_value[DSTP_FIELD_IPV4].value.u16,
243 v->field_value[DSTP_FIELD_IPV4].mask_range.u16);
245 GET_CB_FIELD(in[CB_FLD_PROTO],
246 v->field_value[PROTO_FIELD_IPV4].value.u8,
248 GET_CB_FIELD(in[CB_FLD_PROTO],
249 v->field_value[PROTO_FIELD_IPV4].mask_range.u8,
252 printf("V=%u, mask=%u\n",
253 (unsigned int)v->field_value[PROTO_FIELD_IPV4].value.u8,
254 v->field_value[PROTO_FIELD_IPV4].mask_range.u8);
260 * The format for these rules DO NOT need the port ranges to be
261 * separated by ' : ', just ':'. It's a lot more readable and
264 char lines[][128] = {
265 "@0.0.0.0/0 0.0.0.0/0 0:65535 0:65535 2/0xff", /* Protocol check */
266 "@192.168.3.1/32 0.0.0.0/0 0:65535 0:65535 0/0", /* Src IP checl */
267 "@0.0.0.0/0 10.4.4.1/32 0:65535 0:65535 0/0", /* dst IP check */
268 "@0.0.0.0/0 0.0.0.0/0 105:105 0:65535 0/0", /* src port check */
269 "@0.0.0.0/0 0.0.0.0/0 0:65535 206:206 0/0", /* dst port check */
276 setup_acl_pipeline(void)
280 struct rte_pipeline_params pipeline_params = {
285 struct rte_table_acl_rule_add_params rule_params;
286 struct rte_pipeline_table_acl_rule_delete_params *delete_params;
290 /* Pipeline configuration */
291 p = rte_pipeline_create(&pipeline_params);
293 RTE_LOG(INFO, PIPELINE, "%s: Failed to configure pipeline\n",
298 /* Input port configuration */
299 for (i = 0; i < N_PORTS; i++) {
300 struct rte_port_ring_reader_params port_ring_params = {
304 struct rte_pipeline_port_in_params port_params = {
305 .ops = &rte_port_ring_reader_ops,
306 .arg_create = (void *) &port_ring_params,
308 .burst_size = BURST_SIZE,
311 /* Put in action for some ports */
313 port_params.f_action = port_in_action;
315 ret = rte_pipeline_port_in_create(p, &port_params,
318 rte_panic("Unable to configure input port %d, ret:%d\n",
324 /* output Port configuration */
325 for (i = 0; i < N_PORTS; i++) {
326 struct rte_port_ring_writer_params port_ring_params = {
328 .tx_burst_sz = BURST_SIZE,
331 struct rte_pipeline_port_out_params port_params = {
332 .ops = &rte_port_ring_writer_ops,
333 .arg_create = (void *) &port_ring_params,
339 if (rte_pipeline_port_out_create(p, &port_params,
341 rte_panic("Unable to configure output port %d\n", i);
346 /* Table configuration */
347 for (i = 0; i < N_PORTS; i++) {
348 struct rte_pipeline_table_params table_params;
350 /* Set up defaults for stub */
351 table_params.ops = &rte_table_stub_ops;
352 table_params.arg_create = NULL;
353 table_params.f_action_hit = action_handler_hit;
354 table_params.f_action_miss = NULL;
355 table_params.action_data_size = 0;
357 RTE_LOG(INFO, PIPELINE, "miss_action=%x\n",
358 table_entry_miss_action);
360 printf("RTE_ACL_RULE_SZ(%zu) = %zu\n", DIM(ipv4_defs),
361 RTE_ACL_RULE_SZ(DIM(ipv4_defs)));
363 struct rte_table_acl_params acl_params;
365 acl_params.n_rules = 1 << 5;
366 acl_params.n_rule_fields = DIM(ipv4_defs);
367 snprintf(acl_name, sizeof(acl_name), "ACL%d", i);
368 acl_params.name = acl_name;
369 memcpy(acl_params.field_format, ipv4_defs, sizeof(ipv4_defs));
371 table_params.ops = &rte_table_acl_ops;
372 table_params.arg_create = &acl_params;
374 if (rte_pipeline_table_create(p, &table_params, &table_id[i])) {
375 rte_panic("Unable to configure table %u\n", i);
379 if (connect_miss_action_to_table) {
380 if (rte_pipeline_table_create(p, &table_params,
382 rte_panic("Unable to configure table %u\n", i);
388 for (i = 0; i < N_PORTS; i++) {
389 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
391 rte_panic("Unable to connect input port %u to "
393 port_in_id[i], table_id[i]);
398 /* Add entries to tables */
399 for (i = 0; i < N_PORTS; i++) {
400 struct rte_pipeline_table_entry table_entry = {
401 .action = RTE_PIPELINE_ACTION_PORT,
402 {.port_id = port_out_id[i^1]},
405 struct rte_pipeline_table_entry *entry_ptr;
407 memset(&rule_params, 0, sizeof(rule_params));
408 parser = parse_cb_ipv4_rule;
410 for (n = 1; n <= 5; n++) {
411 snprintf(line, sizeof(line), "%s", lines[n-1]);
412 printf("PARSING [%s]\n", line);
414 ret = parser(line, &rule_params);
416 RTE_LOG(ERR, PIPELINE,
417 "line %u: parse_cb_ipv4vlan_rule"
418 " failed, error code: %d (%s)\n",
419 n, ret, strerror(-ret));
423 rule_params.priority = RTE_ACL_MAX_PRIORITY - n;
425 ret = rte_pipeline_table_entry_add(p, table_id[i],
427 &table_entry, &key_found, &entry_ptr);
429 rte_panic("Add entry to table %u failed (%d)\n",
435 /* delete a few rules */
436 for (n = 2; n <= 3; n++) {
437 snprintf(line, sizeof(line), "%s", lines[n-1]);
438 printf("PARSING [%s]\n", line);
440 ret = parser(line, &rule_params);
442 RTE_LOG(ERR, PIPELINE, "line %u: parse rule "
443 " failed, error code: %d (%s)\n",
444 n, ret, strerror(-ret));
448 delete_params = (struct
449 rte_pipeline_table_acl_rule_delete_params *)
450 &(rule_params.field_value[0]);
451 ret = rte_pipeline_table_entry_delete(p, table_id[i],
452 delete_params, &key_found, NULL);
454 rte_panic("Add entry to table %u failed (%d)\n",
458 printf("Deleted Rule.\n");
462 /* Try to add duplicates */
463 for (n = 1; n <= 5; n++) {
464 snprintf(line, sizeof(line), "%s", lines[n-1]);
465 printf("PARSING [%s]\n", line);
467 ret = parser(line, &rule_params);
469 RTE_LOG(ERR, PIPELINE, "line %u: parse rule"
470 " failed, error code: %d (%s)\n",
471 n, ret, strerror(-ret));
475 rule_params.priority = RTE_ACL_MAX_PRIORITY - n;
477 ret = rte_pipeline_table_entry_add(p, table_id[i],
479 &table_entry, &key_found, &entry_ptr);
481 rte_panic("Add entry to table %u failed (%d)\n",
488 /* Enable input ports */
489 for (i = 0; i < N_PORTS ; i++)
490 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
491 rte_panic("Unable to enable input port %u\n",
494 /* Check pipeline consistency */
495 if (rte_pipeline_check(p) < 0) {
496 rte_panic("Pipeline consistency check failed\n");
507 test_pipeline_single_filter(int expected_count)
509 int i, j, ret, tx_count;
510 struct ipv4_5tuple five_tuple;
512 /* Allocate a few mbufs and manually insert into the rings. */
513 for (i = 0; i < N_PORTS; i++) {
514 for (j = 0; j < 8; j++) {
515 struct rte_mbuf *mbuf;
517 mbuf = rte_pktmbuf_alloc(pool);
518 memset(mbuf->pkt.data, 0x00,
519 sizeof(struct ipv4_5tuple));
521 five_tuple.proto = j;
522 five_tuple.ip_src = rte_bswap32(IPv4(192, 168, j, 1));
523 five_tuple.ip_dst = rte_bswap32(IPv4(10, 4, j, 1));
524 five_tuple.port_src = rte_bswap16(100 + j);
525 five_tuple.port_dst = rte_bswap16(200 + j);
527 memcpy(mbuf->pkt.data, &five_tuple,
528 sizeof(struct ipv4_5tuple));
529 RTE_LOG(INFO, PIPELINE, "%s: Enqueue onto ring %d\n",
531 rte_ring_enqueue(rings_rx[i], mbuf);
535 /* Run pipeline once */
538 rte_pipeline_flush(p);
542 for (i = 0; i < N_PORTS; i++) {
543 void *objs[RING_TX_SIZE];
544 struct rte_mbuf *mbuf;
546 ret = rte_ring_sc_dequeue_burst(rings_tx[i], objs, 10);
548 printf("Got no objects from ring %d - error code %d\n",
551 printf("Got %d object(s) from ring %d!\n", ret, i);
552 for (j = 0; j < ret; j++) {
553 mbuf = (struct rte_mbuf *)objs[j];
554 rte_hexdump(stdout, "mbuf", mbuf->pkt.data, 64);
555 rte_pktmbuf_free(mbuf);
561 if (tx_count != expected_count) {
562 RTE_LOG(INFO, PIPELINE,
563 "%s: Unexpected packets for ACL test, "
564 "expected %d, got %d\n",
565 __func__, expected_count, tx_count);
569 rte_pipeline_free(p);
582 override_hit_mask = 0xFF; /* All packets are a hit */
584 setup_acl_pipeline();
585 if (test_pipeline_single_filter(10) < 0)