1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
9 #include <netinet/in.h>
10 #include <netinet/ip.h>
18 #define INIT_ACL_RULE_NUM 128
20 #define IPV4_DST_FROM_SP(acr) \
21 (rte_cpu_to_be_32((acr).field[DST_FIELD_IPV4].value.u32))
23 #define IPV4_SRC_FROM_SP(acr) \
24 (rte_cpu_to_be_32((acr).field[SRC_FIELD_IPV4].value.u32))
26 #define IPV4_DST_MASK_FROM_SP(acr) \
27 ((acr).field[DST_FIELD_IPV4].mask_range.u32)
29 #define IPV4_SRC_MASK_FROM_SP(acr) \
30 ((acr).field[SRC_FIELD_IPV4].mask_range.u32)
33 * Rule and trace formats definitions.
45 * That effectively defines order of IPV4 classifications:
49 * - PORTS (SRC and DST)
59 static struct rte_acl_field_def ip4_defs[NUM_FIELDS_IPV4] = {
61 .type = RTE_ACL_FIELD_TYPE_BITMASK,
62 .size = sizeof(uint8_t),
63 .field_index = PROTO_FIELD_IPV4,
64 .input_index = RTE_ACL_IPV4_PROTO,
68 .type = RTE_ACL_FIELD_TYPE_MASK,
69 .size = sizeof(uint32_t),
70 .field_index = SRC_FIELD_IPV4,
71 .input_index = RTE_ACL_IPV4_SRC,
72 .offset = offsetof(struct ip, ip_src) - offsetof(struct ip, ip_p)
75 .type = RTE_ACL_FIELD_TYPE_MASK,
76 .size = sizeof(uint32_t),
77 .field_index = DST_FIELD_IPV4,
78 .input_index = RTE_ACL_IPV4_DST,
79 .offset = offsetof(struct ip, ip_dst) - offsetof(struct ip, ip_p)
82 .type = RTE_ACL_FIELD_TYPE_RANGE,
83 .size = sizeof(uint16_t),
84 .field_index = SRCP_FIELD_IPV4,
85 .input_index = RTE_ACL_IPV4_PORTS,
86 .offset = sizeof(struct ip) - offsetof(struct ip, ip_p)
89 .type = RTE_ACL_FIELD_TYPE_RANGE,
90 .size = sizeof(uint16_t),
91 .field_index = DSTP_FIELD_IPV4,
92 .input_index = RTE_ACL_IPV4_PORTS,
93 .offset = sizeof(struct ip) - offsetof(struct ip, ip_p) +
98 RTE_ACL_RULE_DEF(acl4_rules, RTE_DIM(ip4_defs));
100 static struct acl4_rules *acl4_rules_out;
101 static uint32_t nb_acl4_rules_out;
102 static uint32_t sp_out_sz;
104 static struct acl4_rules *acl4_rules_in;
105 static uint32_t nb_acl4_rules_in;
106 static uint32_t sp_in_sz;
109 extend_sp_arr(struct acl4_rules **sp_tbl, uint32_t cur_cnt, uint32_t *cur_sz)
111 if (*sp_tbl == NULL) {
112 *sp_tbl = calloc(INIT_ACL_RULE_NUM, sizeof(struct acl4_rules));
115 *cur_sz = INIT_ACL_RULE_NUM;
119 if (cur_cnt >= *cur_sz) {
120 *sp_tbl = realloc(*sp_tbl,
121 *cur_sz * sizeof(struct acl4_rules) * 2);
124 /* clean reallocated extra space */
125 memset(&(*sp_tbl)[*cur_sz], 0,
126 *cur_sz * sizeof(struct acl4_rules));
135 parse_sp4_tokens(char **tokens, uint32_t n_tokens,
136 struct parse_status *status)
138 struct acl4_rules *rule_ipv4 = NULL;
140 uint32_t *ri = NULL; /* rule index */
141 uint32_t ti = 0; /* token index */
145 uint32_t protect_p = 0;
146 uint32_t bypass_p = 0;
147 uint32_t discard_p = 0;
151 uint32_t proto_p = 0;
152 uint32_t sport_p = 0;
153 uint32_t dport_p = 0;
155 if (strcmp(tokens[1], "in") == 0) {
156 ri = &nb_acl4_rules_in;
158 if (extend_sp_arr(&acl4_rules_in, nb_acl4_rules_in,
162 rule_ipv4 = &acl4_rules_in[*ri];
164 } else if (strcmp(tokens[1], "out") == 0) {
165 ri = &nb_acl4_rules_out;
167 if (extend_sp_arr(&acl4_rules_out, nb_acl4_rules_out,
171 rule_ipv4 = &acl4_rules_out[*ri];
173 APP_CHECK(0, status, "unrecognized input \"%s\", expect"
174 " \"in\" or \"out\"\n", tokens[ti]);
178 rule_ipv4->data.category_mask = 1;
180 for (ti = 2; ti < n_tokens; ti++) {
181 if (strcmp(tokens[ti], "esp") == 0) {
182 /* currently do nothing */
183 APP_CHECK_PRESENCE(esp_p, tokens[ti], status);
184 if (status->status < 0)
190 if (strcmp(tokens[ti], "protect") == 0) {
191 APP_CHECK_PRESENCE(protect_p, tokens[ti], status);
192 if (status->status < 0)
194 APP_CHECK(bypass_p == 0, status, "conflict item "
195 "between \"%s\" and \"%s\"", tokens[ti],
197 if (status->status < 0)
199 APP_CHECK(discard_p == 0, status, "conflict item "
200 "between \"%s\" and \"%s\"", tokens[ti],
202 if (status->status < 0)
204 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
205 if (status->status < 0)
207 APP_CHECK_TOKEN_IS_NUM(tokens, ti, status);
208 if (status->status < 0)
211 tv = atoi(tokens[ti]);
212 APP_CHECK(tv != DISCARD && tv != BYPASS, status,
213 "invalid SPI: %s", tokens[ti]);
214 if (status->status < 0)
216 rule_ipv4->data.userdata = tv;
222 if (strcmp(tokens[ti], "bypass") == 0) {
223 APP_CHECK_PRESENCE(bypass_p, tokens[ti], status);
224 if (status->status < 0)
226 APP_CHECK(protect_p == 0, status, "conflict item "
227 "between \"%s\" and \"%s\"", tokens[ti],
229 if (status->status < 0)
231 APP_CHECK(discard_p == 0, status, "conflict item "
232 "between \"%s\" and \"%s\"", tokens[ti],
234 if (status->status < 0)
237 rule_ipv4->data.userdata = BYPASS;
243 if (strcmp(tokens[ti], "discard") == 0) {
244 APP_CHECK_PRESENCE(discard_p, tokens[ti], status);
245 if (status->status < 0)
247 APP_CHECK(protect_p == 0, status, "conflict item "
248 "between \"%s\" and \"%s\"", tokens[ti],
250 if (status->status < 0)
252 APP_CHECK(bypass_p == 0, status, "conflict item "
253 "between \"%s\" and \"%s\"", tokens[ti],
255 if (status->status < 0)
258 rule_ipv4->data.userdata = DISCARD;
264 if (strcmp(tokens[ti], "pri") == 0) {
265 APP_CHECK_PRESENCE(pri_p, tokens[ti], status);
266 if (status->status < 0)
268 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
269 if (status->status < 0)
271 APP_CHECK_TOKEN_IS_NUM(tokens, ti, status);
272 if (status->status < 0)
275 rule_ipv4->data.priority = atoi(tokens[ti]);
281 if (strcmp(tokens[ti], "src") == 0) {
285 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
286 if (status->status < 0)
288 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
289 if (status->status < 0)
292 APP_CHECK(parse_ipv4_addr(tokens[ti], &ip,
293 &depth) == 0, status, "unrecognized "
294 "input \"%s\", expect valid ipv4 addr",
296 if (status->status < 0)
299 rule_ipv4->field[1].value.u32 =
300 rte_bswap32(ip.s_addr);
301 rule_ipv4->field[1].mask_range.u32 =
308 if (strcmp(tokens[ti], "dst") == 0) {
312 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
313 if (status->status < 0)
315 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
316 if (status->status < 0)
318 APP_CHECK(parse_ipv4_addr(tokens[ti], &ip,
319 &depth) == 0, status, "unrecognized "
320 "input \"%s\", expect valid ipv4 addr",
322 if (status->status < 0)
325 rule_ipv4->field[2].value.u32 =
326 rte_bswap32(ip.s_addr);
327 rule_ipv4->field[2].mask_range.u32 =
334 if (strcmp(tokens[ti], "proto") == 0) {
337 APP_CHECK_PRESENCE(proto_p, tokens[ti], status);
338 if (status->status < 0)
340 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
341 if (status->status < 0)
344 APP_CHECK(parse_range(tokens[ti], &low, &high)
345 == 0, status, "unrecognized input \"%s\""
346 ", expect \"from:to\"", tokens[ti]);
347 if (status->status < 0)
349 APP_CHECK(low <= 0xff, status, "proto low "
351 if (status->status < 0)
353 APP_CHECK(high <= 0xff, status, "proto high "
355 if (status->status < 0)
358 rule_ipv4->field[0].value.u8 = (uint8_t)low;
359 rule_ipv4->field[0].mask_range.u8 = (uint8_t)high;
365 if (strcmp(tokens[ti], "sport") == 0) {
366 uint16_t port_low, port_high;
368 APP_CHECK_PRESENCE(sport_p, tokens[ti], status);
369 if (status->status < 0)
371 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
372 if (status->status < 0)
375 APP_CHECK(parse_range(tokens[ti], &port_low,
376 &port_high) == 0, status, "unrecognized "
377 "input \"%s\", expect \"port_from:"
378 "port_to\"", tokens[ti]);
379 if (status->status < 0)
382 rule_ipv4->field[3].value.u16 = port_low;
383 rule_ipv4->field[3].mask_range.u16 = port_high;
389 if (strcmp(tokens[ti], "dport") == 0) {
390 uint16_t port_low, port_high;
392 APP_CHECK_PRESENCE(dport_p, tokens[ti], status);
393 if (status->status < 0)
395 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
396 if (status->status < 0)
399 APP_CHECK(parse_range(tokens[ti], &port_low,
400 &port_high) == 0, status, "unrecognized "
401 "input \"%s\", expect \"port_from:"
402 "port_to\"", tokens[ti]);
403 if (status->status < 0)
406 rule_ipv4->field[4].value.u16 = port_low;
407 rule_ipv4->field[4].mask_range.u16 = port_high;
413 /* unrecognizeable input */
414 APP_CHECK(0, status, "unrecognized input \"%s\"",
419 /* check if argument(s) are missing */
420 APP_CHECK(esp_p == 1, status, "missing argument \"esp\"");
421 if (status->status < 0)
424 APP_CHECK(protect_p | bypass_p | discard_p, status, "missing "
425 "argument \"protect\", \"bypass\", or \"discard\"");
426 if (status->status < 0)
433 print_one_ip4_rule(const struct acl4_rules *rule, int32_t extra)
437 uint32_t_to_char(rule->field[SRC_FIELD_IPV4].value.u32,
439 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
440 rule->field[SRC_FIELD_IPV4].mask_range.u32);
441 uint32_t_to_char(rule->field[DST_FIELD_IPV4].value.u32,
443 printf("%hhu.%hhu.%hhu.%hhu/%u ", a, b, c, d,
444 rule->field[DST_FIELD_IPV4].mask_range.u32);
445 printf("%hu : %hu %hu : %hu 0x%hhx/0x%hhx ",
446 rule->field[SRCP_FIELD_IPV4].value.u16,
447 rule->field[SRCP_FIELD_IPV4].mask_range.u16,
448 rule->field[DSTP_FIELD_IPV4].value.u16,
449 rule->field[DSTP_FIELD_IPV4].mask_range.u16,
450 rule->field[PROTO_FIELD_IPV4].value.u8,
451 rule->field[PROTO_FIELD_IPV4].mask_range.u8);
453 printf("0x%x-0x%x-0x%x ",
454 rule->data.category_mask,
456 rule->data.userdata);
460 dump_ip4_rules(const struct acl4_rules *rule, int32_t num, int32_t extra)
464 for (i = 0; i < num; i++, rule++) {
465 printf("\t%d:", i + 1);
466 print_one_ip4_rule(rule, extra);
471 static struct rte_acl_ctx *
472 acl4_init(const char *name, int32_t socketid, const struct acl4_rules *rules,
476 struct rte_acl_param acl_param;
477 struct rte_acl_config acl_build_param;
478 struct rte_acl_ctx *ctx;
480 printf("Creating SP context with %u rules\n", rules_nb);
482 memset(&acl_param, 0, sizeof(acl_param));
484 /* Create ACL contexts */
485 snprintf(s, sizeof(s), "%s_%d", name, socketid);
487 printf("IPv4 %s entries [%u]:\n", s, rules_nb);
488 dump_ip4_rules(rules, rules_nb, 1);
491 acl_param.socket_id = socketid;
492 acl_param.rule_size = RTE_ACL_RULE_SZ(RTE_DIM(ip4_defs));
493 acl_param.max_rule_num = rules_nb;
495 ctx = rte_acl_create(&acl_param);
497 rte_exit(EXIT_FAILURE, "Failed to create ACL context\n");
499 if (rte_acl_add_rules(ctx, (const struct rte_acl_rule *)rules,
501 rte_exit(EXIT_FAILURE, "add rules failed\n");
504 memset(&acl_build_param, 0, sizeof(acl_build_param));
506 acl_build_param.num_categories = DEFAULT_MAX_CATEGORIES;
507 acl_build_param.num_fields = RTE_DIM(ip4_defs);
508 memcpy(&acl_build_param.defs, ip4_defs, sizeof(ip4_defs));
510 if (rte_acl_build(ctx, &acl_build_param) != 0)
511 rte_exit(EXIT_FAILURE, "Failed to build ACL trie\n");
519 * check that for each rule it's SPI has a correspondent entry in SAD
522 check_spi_value(struct sa_ctx *sa_ctx, int inbound)
524 uint32_t i, num, spi;
526 struct acl4_rules *acr;
530 num = nb_acl4_rules_in;
532 acr = acl4_rules_out;
533 num = nb_acl4_rules_out;
536 for (i = 0; i != num; i++) {
537 spi = acr[i].data.userdata;
538 if (spi != DISCARD && spi != BYPASS) {
539 spi_idx = sa_spi_present(sa_ctx, spi, inbound);
542 "SPI %u is not present in SAD\n",
546 /* Update userdata with spi index */
547 acr[i].data.userdata = spi_idx + 1;
555 sp4_init(struct socket_ctx *ctx, int32_t socket_id)
560 rte_exit(EXIT_FAILURE, "NULL context.\n");
562 if (ctx->sp_ip4_in != NULL)
563 rte_exit(EXIT_FAILURE, "Inbound SP DB for socket %u already "
564 "initialized\n", socket_id);
566 if (ctx->sp_ip4_out != NULL)
567 rte_exit(EXIT_FAILURE, "Outbound SP DB for socket %u already "
568 "initialized\n", socket_id);
570 if (check_spi_value(ctx->sa_in, 1) < 0)
571 rte_exit(EXIT_FAILURE,
572 "Inbound IPv4 SP DB has unmatched in SAD SPIs\n");
574 if (check_spi_value(ctx->sa_out, 0) < 0)
575 rte_exit(EXIT_FAILURE,
576 "Outbound IPv4 SP DB has unmatched in SAD SPIs\n");
578 if (nb_acl4_rules_in > 0) {
580 ctx->sp_ip4_in = (struct sp_ctx *)acl4_init(name,
581 socket_id, acl4_rules_in, nb_acl4_rules_in);
583 RTE_LOG(WARNING, IPSEC, "No IPv4 SP Inbound rule "
586 if (nb_acl4_rules_out > 0) {
588 ctx->sp_ip4_out = (struct sp_ctx *)acl4_init(name,
589 socket_id, acl4_rules_out, nb_acl4_rules_out);
591 RTE_LOG(WARNING, IPSEC, "No IPv4 SP Outbound rule "
596 sp_cmp(const void *p, const void *q)
598 uint32_t spi1 = ((const struct acl4_rules *)p)->data.userdata;
599 uint32_t spi2 = ((const struct acl4_rules *)q)->data.userdata;
601 return (int)(spi1 - spi2);
606 * Search though SP rules for given SPI.
609 sp4_spi_present(uint32_t spi, int inbound, struct ip_addr ip_addr[2],
613 struct acl4_rules *rule;
614 const struct acl4_rules *acr;
615 struct acl4_rules tmpl;
619 num = nb_acl4_rules_in;
621 acr = acl4_rules_out;
622 num = nb_acl4_rules_out;
625 tmpl.data.userdata = spi;
627 rule = bsearch(&tmpl, acr, num, sizeof(struct acl4_rules), sp_cmp);
629 if (NULL != ip_addr && NULL != mask) {
630 ip_addr[0].ip.ip4 = IPV4_SRC_FROM_SP(*rule);
631 ip_addr[1].ip.ip4 = IPV4_DST_FROM_SP(*rule);
632 mask[0] = IPV4_SRC_MASK_FROM_SP(*rule);
633 mask[1] = IPV4_DST_MASK_FROM_SP(*rule);
635 return RTE_PTR_DIFF(rule, acr) / sizeof(struct acl4_rules);
644 qsort(acl4_rules_in, nb_acl4_rules_in, sizeof(struct acl4_rules),
646 qsort(acl4_rules_out, nb_acl4_rules_out, sizeof(struct acl4_rules),