4 * Copyright(c) 2016 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.
37 #include <sys/types.h>
38 #include <netinet/in.h>
39 #include <netinet/ip6.h>
47 #define MAX_ACL_RULE_NUM 1024
64 #define IP6_ADDR_SIZE 16
66 struct rte_acl_field_def ip6_defs[IP6_NUM] = {
68 .type = RTE_ACL_FIELD_TYPE_BITMASK,
69 .size = sizeof(uint8_t),
70 .field_index = IP6_PROTO,
71 .input_index = IP6_PROTO,
75 .type = RTE_ACL_FIELD_TYPE_MASK,
77 .field_index = IP6_SRC0,
78 .input_index = IP6_SRC0,
82 .type = RTE_ACL_FIELD_TYPE_MASK,
84 .field_index = IP6_SRC1,
85 .input_index = IP6_SRC1,
89 .type = RTE_ACL_FIELD_TYPE_MASK,
91 .field_index = IP6_SRC2,
92 .input_index = IP6_SRC2,
96 .type = RTE_ACL_FIELD_TYPE_MASK,
98 .field_index = IP6_SRC3,
99 .input_index = IP6_SRC3,
103 .type = RTE_ACL_FIELD_TYPE_MASK,
105 .field_index = IP6_DST0,
106 .input_index = IP6_DST0,
110 .type = RTE_ACL_FIELD_TYPE_MASK,
112 .field_index = IP6_DST1,
113 .input_index = IP6_DST1,
117 .type = RTE_ACL_FIELD_TYPE_MASK,
119 .field_index = IP6_DST2,
120 .input_index = IP6_DST2,
124 .type = RTE_ACL_FIELD_TYPE_MASK,
126 .field_index = IP6_DST3,
127 .input_index = IP6_DST3,
131 .type = RTE_ACL_FIELD_TYPE_RANGE,
132 .size = sizeof(uint16_t),
133 .field_index = IP6_SRCP,
134 .input_index = IP6_SRCP,
138 .type = RTE_ACL_FIELD_TYPE_RANGE,
139 .size = sizeof(uint16_t),
140 .field_index = IP6_DSTP,
141 .input_index = IP6_SRCP,
146 RTE_ACL_RULE_DEF(acl6_rules, RTE_DIM(ip6_defs));
148 struct acl6_rules acl6_rules_out[MAX_ACL_RULE_NUM];
149 uint32_t nb_acl6_rules_out;
151 struct acl6_rules acl6_rules_in[MAX_ACL_RULE_NUM];
152 uint32_t nb_acl6_rules_in;
155 parse_sp6_tokens(char **tokens, uint32_t n_tokens,
156 struct parse_status *status)
158 struct acl6_rules *rule_ipv6 = NULL;
160 uint32_t *ri = NULL; /* rule index */
161 uint32_t ti = 0; /* token index */
164 uint32_t protect_p = 0;
165 uint32_t bypass_p = 0;
166 uint32_t discard_p = 0;
170 uint32_t proto_p = 0;
171 uint32_t sport_p = 0;
172 uint32_t dport_p = 0;
174 if (strcmp(tokens[1], "in") == 0) {
175 ri = &nb_acl6_rules_in;
177 APP_CHECK(*ri <= MAX_ACL_RULE_NUM - 1, status, "too "
178 "many sp rules, abort insertion\n");
179 if (status->status < 0)
182 rule_ipv6 = &acl6_rules_in[*ri];
184 } else if (strcmp(tokens[1], "out") == 0) {
185 ri = &nb_acl6_rules_out;
187 APP_CHECK(*ri <= MAX_ACL_RULE_NUM - 1, status, "too "
188 "many sp rules, abort insertion\n");
189 if (status->status < 0)
192 rule_ipv6 = &acl6_rules_out[*ri];
195 APP_CHECK(0, status, "unrecognized input \"%s\", expect"
196 " \"in\" or \"out\"\n", tokens[ti]);
200 rule_ipv6->data.category_mask = 1;
203 for (ti = 2; ti < n_tokens; ti++) {
204 if (strcmp(tokens[ti], "esp") == 0) {
205 /* currently do nothing */
206 APP_CHECK_PRESENCE(esp_p, tokens[ti], status);
207 if (status->status < 0)
213 if (strcmp(tokens[ti], "protect") == 0) {
214 APP_CHECK_PRESENCE(protect_p, tokens[ti], status);
215 if (status->status < 0)
217 APP_CHECK(bypass_p == 0, status, "conflict item "
218 "between \"%s\" and \"%s\"", tokens[ti],
220 if (status->status < 0)
222 APP_CHECK(discard_p == 0, status, "conflict item "
223 "between \"%s\" and \"%s\"", tokens[ti],
225 if (status->status < 0)
227 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
228 if (status->status < 0)
230 APP_CHECK_TOKEN_IS_NUM(tokens, ti, status);
231 if (status->status < 0)
234 rule_ipv6->data.userdata =
235 PROTECT(atoi(tokens[ti]));
241 if (strcmp(tokens[ti], "bypass") == 0) {
242 APP_CHECK_PRESENCE(bypass_p, tokens[ti], status);
243 if (status->status < 0)
245 APP_CHECK(protect_p == 0, status, "conflict item "
246 "between \"%s\" and \"%s\"", tokens[ti],
248 if (status->status < 0)
250 APP_CHECK(discard_p == 0, status, "conflict item "
251 "between \"%s\" and \"%s\"", tokens[ti],
253 if (status->status < 0)
256 rule_ipv6->data.userdata = BYPASS;
262 if (strcmp(tokens[ti], "discard") == 0) {
263 APP_CHECK_PRESENCE(discard_p, tokens[ti], status);
264 if (status->status < 0)
266 APP_CHECK(protect_p == 0, status, "conflict item "
267 "between \"%s\" and \"%s\"", tokens[ti],
269 if (status->status < 0)
271 APP_CHECK(bypass_p == 0, status, "conflict item "
272 "between \"%s\" and \"%s\"", tokens[ti],
274 if (status->status < 0)
277 rule_ipv6->data.userdata = DISCARD;
283 if (strcmp(tokens[ti], "pri") == 0) {
284 APP_CHECK_PRESENCE(pri_p, tokens[ti], status);
285 if (status->status < 0)
287 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
288 if (status->status < 0)
290 APP_CHECK_TOKEN_IS_NUM(tokens, ti, status);
291 if (status->status < 0)
294 rule_ipv6->data.priority = atoi(tokens[ti]);
300 if (strcmp(tokens[ti], "src") == 0) {
304 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
305 if (status->status < 0)
307 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
308 if (status->status < 0)
311 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
312 &depth) == 0, status, "unrecognized "
313 "input \"%s\", expect valid ipv6 "
315 if (status->status < 0)
318 rule_ipv6->field[1].value.u32 =
319 (uint32_t)ip.s6_addr[0] << 24 |
320 (uint32_t)ip.s6_addr[1] << 16 |
321 (uint32_t)ip.s6_addr[2] << 8 |
322 (uint32_t)ip.s6_addr[3];
323 rule_ipv6->field[1].mask_range.u32 =
324 (depth > 32) ? 32 : depth;
325 depth = (depth > 32) ? (depth - 32) : 0;
326 rule_ipv6->field[2].value.u32 =
327 (uint32_t)ip.s6_addr[4] << 24 |
328 (uint32_t)ip.s6_addr[5] << 16 |
329 (uint32_t)ip.s6_addr[6] << 8 |
330 (uint32_t)ip.s6_addr[7];
331 rule_ipv6->field[2].mask_range.u32 =
332 (depth > 32) ? 32 : depth;
333 depth = (depth > 32) ? (depth - 32) : 0;
334 rule_ipv6->field[3].value.u32 =
335 (uint32_t)ip.s6_addr[8] << 24 |
336 (uint32_t)ip.s6_addr[9] << 16 |
337 (uint32_t)ip.s6_addr[10] << 8 |
338 (uint32_t)ip.s6_addr[11];
339 rule_ipv6->field[3].mask_range.u32 =
340 (depth > 32) ? 32 : depth;
341 depth = (depth > 32) ? (depth - 32) : 0;
342 rule_ipv6->field[4].value.u32 =
343 (uint32_t)ip.s6_addr[12] << 24 |
344 (uint32_t)ip.s6_addr[13] << 16 |
345 (uint32_t)ip.s6_addr[14] << 8 |
346 (uint32_t)ip.s6_addr[15];
347 rule_ipv6->field[4].mask_range.u32 =
348 (depth > 32) ? 32 : depth;
354 if (strcmp(tokens[ti], "dst") == 0) {
358 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
359 if (status->status < 0)
361 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
362 if (status->status < 0)
365 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
366 &depth) == 0, status, "unrecognized "
367 "input \"%s\", expect valid ipv6 "
369 if (status->status < 0)
372 rule_ipv6->field[5].value.u32 =
373 (uint32_t)ip.s6_addr[0] << 24 |
374 (uint32_t)ip.s6_addr[1] << 16 |
375 (uint32_t)ip.s6_addr[2] << 8 |
376 (uint32_t)ip.s6_addr[3];
377 rule_ipv6->field[5].mask_range.u32 =
378 (depth > 32) ? 32 : depth;
379 depth = (depth > 32) ? (depth - 32) : 0;
380 rule_ipv6->field[6].value.u32 =
381 (uint32_t)ip.s6_addr[4] << 24 |
382 (uint32_t)ip.s6_addr[5] << 16 |
383 (uint32_t)ip.s6_addr[6] << 8 |
384 (uint32_t)ip.s6_addr[7];
385 rule_ipv6->field[6].mask_range.u32 =
386 (depth > 32) ? 32 : depth;
387 depth = (depth > 32) ? (depth - 32) : 0;
388 rule_ipv6->field[7].value.u32 =
389 (uint32_t)ip.s6_addr[8] << 24 |
390 (uint32_t)ip.s6_addr[9] << 16 |
391 (uint32_t)ip.s6_addr[10] << 8 |
392 (uint32_t)ip.s6_addr[11];
393 rule_ipv6->field[7].mask_range.u32 =
394 (depth > 32) ? 32 : depth;
395 depth = (depth > 32) ? (depth - 32) : 0;
396 rule_ipv6->field[8].value.u32 =
397 (uint32_t)ip.s6_addr[12] << 24 |
398 (uint32_t)ip.s6_addr[13] << 16 |
399 (uint32_t)ip.s6_addr[14] << 8 |
400 (uint32_t)ip.s6_addr[15];
401 rule_ipv6->field[8].mask_range.u32 =
402 (depth > 32) ? 32 : depth;
408 if (strcmp(tokens[ti], "proto") == 0) {
411 APP_CHECK_PRESENCE(proto_p, tokens[ti], status);
412 if (status->status < 0)
414 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
415 if (status->status < 0)
418 APP_CHECK(parse_range(tokens[ti], &low, &high)
419 == 0, status, "unrecognized input \"%s\""
420 ", expect \"from:to\"", tokens[ti]);
421 if (status->status < 0)
423 APP_CHECK(low <= 0xff, status, "proto low "
425 if (status->status < 0)
427 APP_CHECK(high <= 0xff, status, "proto high "
429 if (status->status < 0)
432 rule_ipv6->field[0].value.u8 = (uint8_t)low;
433 rule_ipv6->field[0].mask_range.u8 = (uint8_t)high;
439 if (strcmp(tokens[ti], "sport") == 0) {
440 uint16_t port_low, port_high;
442 APP_CHECK_PRESENCE(sport_p, tokens[ti], status);
443 if (status->status < 0)
445 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
446 if (status->status < 0)
449 APP_CHECK(parse_range(tokens[ti], &port_low,
450 &port_high) == 0, status, "unrecognized "
451 "input \"%s\", expect \"port_from:"
452 "port_to\"", tokens[ti]);
453 if (status->status < 0)
456 rule_ipv6->field[9].value.u16 = port_low;
457 rule_ipv6->field[9].mask_range.u16 = port_high;
463 if (strcmp(tokens[ti], "dport") == 0) {
464 uint16_t port_low, port_high;
466 APP_CHECK_PRESENCE(dport_p, tokens[ti], status);
467 if (status->status < 0)
469 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
470 if (status->status < 0)
473 APP_CHECK(parse_range(tokens[ti], &port_low,
474 &port_high) == 0, status, "unrecognized "
475 "input \"%s\", expect \"port_from:"
476 "port_to\"", tokens[ti]);
477 if (status->status < 0)
480 rule_ipv6->field[10].value.u16 = port_low;
481 rule_ipv6->field[10].mask_range.u16 = port_high;
487 /* unrecognizeable input */
488 APP_CHECK(0, status, "unrecognized input \"%s\"",
493 /* check if argument(s) are missing */
494 APP_CHECK(esp_p == 1, status, "missing argument \"esp\"");
495 if (status->status < 0)
498 APP_CHECK(protect_p | bypass_p | discard_p, status, "missing "
499 "argument \"protect\", \"bypass\", or \"discard\"");
500 if (status->status < 0)
507 print_one_ip6_rule(const struct acl6_rules *rule, int32_t extra)
511 uint32_t_to_char(rule->field[IP6_SRC0].value.u32,
513 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
514 uint32_t_to_char(rule->field[IP6_SRC1].value.u32,
516 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
517 uint32_t_to_char(rule->field[IP6_SRC2].value.u32,
519 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
520 uint32_t_to_char(rule->field[IP6_SRC3].value.u32,
522 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
523 rule->field[IP6_SRC0].mask_range.u32
524 + rule->field[IP6_SRC1].mask_range.u32
525 + rule->field[IP6_SRC2].mask_range.u32
526 + rule->field[IP6_SRC3].mask_range.u32);
528 uint32_t_to_char(rule->field[IP6_DST0].value.u32,
530 printf("%.2x%.2x:%.2x%.2x", a, b, c, d);
531 uint32_t_to_char(rule->field[IP6_DST1].value.u32,
533 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
534 uint32_t_to_char(rule->field[IP6_DST2].value.u32,
536 printf(":%.2x%.2x:%.2x%.2x", a, b, c, d);
537 uint32_t_to_char(rule->field[IP6_DST3].value.u32,
539 printf(":%.2x%.2x:%.2x%.2x/%u ", a, b, c, d,
540 rule->field[IP6_DST0].mask_range.u32
541 + rule->field[IP6_DST1].mask_range.u32
542 + rule->field[IP6_DST2].mask_range.u32
543 + rule->field[IP6_DST3].mask_range.u32);
545 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
546 rule->field[IP6_SRCP].value.u16,
547 rule->field[IP6_SRCP].mask_range.u16,
548 rule->field[IP6_DSTP].value.u16,
549 rule->field[IP6_DSTP].mask_range.u16,
550 rule->field[IP6_PROTO].value.u8,
551 rule->field[IP6_PROTO].mask_range.u8);
553 printf("0x%x-0x%x-0x%x ",
554 rule->data.category_mask,
556 rule->data.userdata);
560 dump_ip6_rules(const struct acl6_rules *rule, int32_t num, int32_t extra)
564 for (i = 0; i < num; i++, rule++) {
565 printf("\t%d:", i + 1);
566 print_one_ip6_rule(rule, extra);
571 static struct rte_acl_ctx *
572 acl6_init(const char *name, int32_t socketid, const struct acl6_rules *rules,
576 struct rte_acl_param acl_param;
577 struct rte_acl_config acl_build_param;
578 struct rte_acl_ctx *ctx;
580 printf("Creating SP context with %u max rules\n", MAX_ACL_RULE_NUM);
582 memset(&acl_param, 0, sizeof(acl_param));
584 /* Create ACL contexts */
585 snprintf(s, sizeof(s), "%s_%d", name, socketid);
587 printf("IPv4 %s entries [%u]:\n", s, rules_nb);
588 dump_ip6_rules(rules, rules_nb, 1);
591 acl_param.socket_id = socketid;
592 acl_param.rule_size = RTE_ACL_RULE_SZ(RTE_DIM(ip6_defs));
593 acl_param.max_rule_num = MAX_ACL_RULE_NUM;
595 ctx = rte_acl_create(&acl_param);
597 rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
599 if (rte_acl_add_rules(ctx, (const struct rte_acl_rule *)rules,
601 rte_exit(EXIT_FAILURE, "add rules failed\n");
604 memset(&acl_build_param, 0, sizeof(acl_build_param));
606 acl_build_param.num_categories = DEFAULT_MAX_CATEGORIES;
607 acl_build_param.num_fields = RTE_DIM(ip6_defs);
608 memcpy(&acl_build_param.defs, ip6_defs, sizeof(ip6_defs));
610 if (rte_acl_build(ctx, &acl_build_param) != 0)
611 rte_exit(EXIT_FAILURE, "Failed to build ACL trie\n");
619 sp6_init(struct socket_ctx *ctx, int32_t socket_id)
624 rte_exit(EXIT_FAILURE, "NULL context.\n");
626 if (ctx->sp_ip6_in != NULL)
627 rte_exit(EXIT_FAILURE, "Inbound IPv6 SP DB for socket %u "
628 "already initialized\n", socket_id);
630 if (ctx->sp_ip6_out != NULL)
631 rte_exit(EXIT_FAILURE, "Outbound IPv6 SP DB for socket %u "
632 "already initialized\n", socket_id);
634 if (nb_acl6_rules_in > 0) {
636 ctx->sp_ip6_in = (struct sp_ctx *)acl6_init(name,
637 socket_id, acl6_rules_in, nb_acl6_rules_in);
639 RTE_LOG(WARNING, IPSEC, "No IPv6 SP Inbound rule "
642 if (nb_acl6_rules_out > 0) {
644 ctx->sp_ip6_out = (struct sp_ctx *)acl6_init(name,
645 socket_id, acl6_rules_out, nb_acl6_rules_out);
647 RTE_LOG(WARNING, IPSEC, "No IPv6 SP Outbound rule "