1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2020 Intel Corporation
6 * Security Associations
9 #include <netinet/in.h>
10 #include <netinet/ip.h>
11 #include <netinet/ip6.h>
13 #include <rte_memzone.h>
14 #include <rte_crypto.h>
15 #include <rte_security.h>
16 #include <rte_cryptodev.h>
17 #include <rte_byteorder.h>
18 #include <rte_errno.h>
21 #include <rte_random.h>
22 #include <rte_ethdev.h>
23 #include <rte_malloc.h>
32 #define IP4_FULL_MASK (sizeof(((struct ip_addr *)NULL)->ip.ip4) * CHAR_BIT)
34 #define IP6_FULL_MASK (sizeof(((struct ip_addr *)NULL)->ip.ip6.ip6) * CHAR_BIT)
36 #define MBUF_NO_SEC_OFFLOAD(m) ((m->ol_flags & RTE_MBUF_F_RX_SEC_OFFLOAD) == 0)
38 struct supported_cipher_algo {
40 enum rte_crypto_cipher_algorithm algo;
46 struct supported_auth_algo {
48 enum rte_crypto_auth_algorithm algo;
55 struct supported_aead_algo {
57 enum rte_crypto_aead_algorithm algo;
66 const struct supported_cipher_algo cipher_algos[] = {
69 .algo = RTE_CRYPTO_CIPHER_NULL,
75 .keyword = "aes-128-cbc",
76 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
82 .keyword = "aes-192-cbc",
83 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
89 .keyword = "aes-256-cbc",
90 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
96 .keyword = "aes-128-ctr",
97 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
103 .keyword = "aes-192-ctr",
104 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
110 .keyword = "aes-256-ctr",
111 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
117 .keyword = "3des-cbc",
118 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
124 .keyword = "des-cbc",
125 .algo = RTE_CRYPTO_CIPHER_DES_CBC,
132 const struct supported_auth_algo auth_algos[] = {
135 .algo = RTE_CRYPTO_AUTH_NULL,
141 .keyword = "sha1-hmac",
142 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
147 .keyword = "sha256-hmac",
148 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
153 .keyword = "sha384-hmac",
154 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC,
159 .keyword = "sha512-hmac",
160 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC,
165 .keyword = "aes-gmac",
166 .algo = RTE_CRYPTO_AUTH_AES_GMAC,
172 .keyword = "aes-xcbc-mac-96",
173 .algo = RTE_CRYPTO_AUTH_AES_XCBC_MAC,
179 const struct supported_aead_algo aead_algos[] = {
181 .keyword = "aes-128-gcm",
182 .algo = RTE_CRYPTO_AEAD_AES_GCM,
190 .keyword = "aes-192-gcm",
191 .algo = RTE_CRYPTO_AEAD_AES_GCM,
199 .keyword = "aes-256-gcm",
200 .algo = RTE_CRYPTO_AEAD_AES_GCM,
208 .keyword = "aes-128-ccm",
209 .algo = RTE_CRYPTO_AEAD_AES_CCM,
217 .keyword = "aes-192-ccm",
218 .algo = RTE_CRYPTO_AEAD_AES_CCM,
226 .keyword = "aes-256-ccm",
227 .algo = RTE_CRYPTO_AEAD_AES_CCM,
235 .keyword = "chacha20-poly1305",
236 .algo = RTE_CRYPTO_AEAD_CHACHA20_POLY1305,
245 #define SA_INIT_NB 128
247 static uint32_t nb_crypto_sessions;
248 struct ipsec_sa *sa_out;
250 static uint32_t sa_out_sz;
251 static struct ipsec_sa_cnt sa_out_cnt;
253 struct ipsec_sa *sa_in;
255 static uint32_t sa_in_sz;
256 static struct ipsec_sa_cnt sa_in_cnt;
258 static const struct supported_cipher_algo *
259 find_match_cipher_algo(const char *cipher_keyword)
263 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
264 const struct supported_cipher_algo *algo =
267 if (strcmp(cipher_keyword, algo->keyword) == 0)
274 static const struct supported_auth_algo *
275 find_match_auth_algo(const char *auth_keyword)
279 for (i = 0; i < RTE_DIM(auth_algos); i++) {
280 const struct supported_auth_algo *algo =
283 if (strcmp(auth_keyword, algo->keyword) == 0)
290 static const struct supported_aead_algo *
291 find_match_aead_algo(const char *aead_keyword)
295 for (i = 0; i < RTE_DIM(aead_algos); i++) {
296 const struct supported_aead_algo *algo =
299 if (strcmp(aead_keyword, algo->keyword) == 0)
307 * parse x:x:x:x.... hex number key string into uint8_t *key
309 * > 0: number of bytes parsed
313 parse_key_string(const char *key_str, uint8_t *key)
315 const char *pt_start = key_str, *pt_end = key_str;
316 uint32_t nb_bytes = 0;
318 while (pt_end != NULL) {
319 char sub_str[3] = {0};
321 pt_end = strchr(pt_start, ':');
323 if (pt_end == NULL) {
324 if (strlen(pt_start) > 2)
326 strncpy(sub_str, pt_start, 2);
328 if (pt_end - pt_start > 2)
331 strncpy(sub_str, pt_start, pt_end - pt_start);
332 pt_start = pt_end + 1;
335 key[nb_bytes++] = strtol(sub_str, NULL, 16);
342 extend_sa_arr(struct ipsec_sa **sa_tbl, uint32_t cur_cnt, uint32_t *cur_sz)
344 if (*sa_tbl == NULL) {
345 *sa_tbl = calloc(SA_INIT_NB, sizeof(struct ipsec_sa));
348 *cur_sz = SA_INIT_NB;
352 if (cur_cnt >= *cur_sz) {
353 *sa_tbl = realloc(*sa_tbl,
354 *cur_sz * sizeof(struct ipsec_sa) * 2);
357 /* clean reallocated extra space */
358 memset(&(*sa_tbl)[*cur_sz], 0,
359 *cur_sz * sizeof(struct ipsec_sa));
367 parse_sa_tokens(char **tokens, uint32_t n_tokens,
368 struct parse_status *status)
370 struct ipsec_sa *rule = NULL;
371 struct rte_ipsec_session *ips;
372 uint32_t ti; /*token index*/
373 uint32_t *ri /*rule index*/;
374 struct ipsec_sa_cnt *sa_cnt;
375 uint32_t cipher_algo_p = 0;
376 uint32_t auth_algo_p = 0;
377 uint32_t aead_algo_p = 0;
382 uint32_t portid_p = 0;
383 uint32_t fallback_p = 0;
384 int16_t status_p = 0;
385 uint16_t udp_encap_p = 0;
387 if (strcmp(tokens[0], "in") == 0) {
390 if (extend_sa_arr(&sa_in, nb_sa_in, &sa_in_sz) < 0)
393 rule->direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
396 sa_cnt = &sa_out_cnt;
397 if (extend_sa_arr(&sa_out, nb_sa_out, &sa_out_sz) < 0)
400 rule->direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS;
404 APP_CHECK_TOKEN_IS_NUM(tokens, 1, status);
405 if (status->status < 0)
407 if (atoi(tokens[1]) == INVALID_SPI)
410 rule->spi = atoi(tokens[1]);
411 rule->portid = UINT16_MAX;
412 ips = ipsec_get_primary_session(rule);
414 for (ti = 2; ti < n_tokens; ti++) {
415 if (strcmp(tokens[ti], "mode") == 0) {
416 APP_CHECK_PRESENCE(mode_p, tokens[ti], status);
417 if (status->status < 0)
420 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
421 if (status->status < 0)
424 if (strcmp(tokens[ti], "ipv4-tunnel") == 0) {
426 rule->flags |= IP4_TUNNEL;
427 } else if (strcmp(tokens[ti], "ipv6-tunnel") == 0) {
429 rule->flags |= IP6_TUNNEL;
430 } else if (strcmp(tokens[ti], "transport") == 0) {
433 rule->flags |= TRANSPORT;
435 APP_CHECK(0, status, "unrecognized "
436 "input \"%s\"", tokens[ti]);
444 if (strcmp(tokens[ti], "telemetry") == 0) {
445 rule->flags |= SA_TELEMETRY_ENABLE;
449 if (strcmp(tokens[ti], "cipher_algo") == 0) {
450 const struct supported_cipher_algo *algo;
453 APP_CHECK_PRESENCE(cipher_algo_p, tokens[ti],
455 if (status->status < 0)
458 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
459 if (status->status < 0)
462 algo = find_match_cipher_algo(tokens[ti]);
464 APP_CHECK(algo != NULL, status, "unrecognized "
465 "input \"%s\"", tokens[ti]);
467 if (status->status < 0)
470 rule->cipher_algo = algo->algo;
471 rule->block_size = algo->block_size;
472 rule->iv_len = algo->iv_len;
473 rule->cipher_key_len = algo->key_len;
475 /* for NULL algorithm, no cipher key required */
476 if (rule->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
481 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
482 if (status->status < 0)
485 APP_CHECK(strcmp(tokens[ti], "cipher_key") == 0,
486 status, "unrecognized input \"%s\", "
487 "expect \"cipher_key\"", tokens[ti]);
488 if (status->status < 0)
491 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
492 if (status->status < 0)
495 key_len = parse_key_string(tokens[ti],
497 APP_CHECK(key_len == rule->cipher_key_len, status,
498 "unrecognized input \"%s\"", tokens[ti]);
499 if (status->status < 0)
502 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC ||
503 algo->algo == RTE_CRYPTO_CIPHER_3DES_CBC)
504 rule->salt = (uint32_t)rte_rand();
506 if (algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) {
508 rule->cipher_key_len = key_len;
510 &rule->cipher_key[key_len], 4);
517 if (strcmp(tokens[ti], "auth_algo") == 0) {
518 const struct supported_auth_algo *algo;
521 APP_CHECK_PRESENCE(auth_algo_p, tokens[ti],
523 if (status->status < 0)
526 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
527 if (status->status < 0)
530 algo = find_match_auth_algo(tokens[ti]);
531 APP_CHECK(algo != NULL, status, "unrecognized "
532 "input \"%s\"", tokens[ti]);
534 if (status->status < 0)
537 rule->auth_algo = algo->algo;
538 rule->auth_key_len = algo->key_len;
539 rule->digest_len = algo->digest_len;
541 /* NULL algorithm and combined algos do not
544 if (algo->key_not_req) {
549 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
550 if (status->status < 0)
553 APP_CHECK(strcmp(tokens[ti], "auth_key") == 0,
554 status, "unrecognized input \"%s\", "
555 "expect \"auth_key\"", tokens[ti]);
556 if (status->status < 0)
559 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
560 if (status->status < 0)
563 key_len = parse_key_string(tokens[ti],
565 APP_CHECK(key_len == rule->auth_key_len, status,
566 "unrecognized input \"%s\"", tokens[ti]);
567 if (status->status < 0)
570 if (algo->algo == RTE_CRYPTO_AUTH_AES_GMAC) {
572 rule->auth_key_len = key_len;
573 rule->iv_len = algo->iv_len;
575 &rule->auth_key[key_len], 4);
582 if (strcmp(tokens[ti], "aead_algo") == 0) {
583 const struct supported_aead_algo *algo;
586 APP_CHECK_PRESENCE(aead_algo_p, tokens[ti],
588 if (status->status < 0)
591 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
592 if (status->status < 0)
595 algo = find_match_aead_algo(tokens[ti]);
597 APP_CHECK(algo != NULL, status, "unrecognized "
598 "input \"%s\"", tokens[ti]);
600 if (status->status < 0)
603 rule->aead_algo = algo->algo;
604 rule->cipher_key_len = algo->key_len;
605 rule->digest_len = algo->digest_len;
606 rule->aad_len = algo->aad_len;
607 rule->block_size = algo->block_size;
608 rule->iv_len = algo->iv_len;
610 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
611 if (status->status < 0)
614 APP_CHECK(strcmp(tokens[ti], "aead_key") == 0,
615 status, "unrecognized input \"%s\", "
616 "expect \"aead_key\"", tokens[ti]);
617 if (status->status < 0)
620 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
621 if (status->status < 0)
624 key_len = parse_key_string(tokens[ti],
626 APP_CHECK(key_len == rule->cipher_key_len, status,
627 "unrecognized input \"%s\"", tokens[ti]);
628 if (status->status < 0)
632 rule->cipher_key_len = key_len;
634 &rule->cipher_key[key_len], 4);
640 if (strcmp(tokens[ti], "src") == 0) {
641 APP_CHECK_PRESENCE(src_p, tokens[ti], status);
642 if (status->status < 0)
645 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
646 if (status->status < 0)
649 if (IS_IP4_TUNNEL(rule->flags)) {
652 APP_CHECK(parse_ipv4_addr(tokens[ti],
653 &ip, NULL) == 0, status,
654 "unrecognized input \"%s\", "
655 "expect valid ipv4 addr",
657 if (status->status < 0)
659 rule->src.ip.ip4 = rte_bswap32(
660 (uint32_t)ip.s_addr);
661 } else if (IS_IP6_TUNNEL(rule->flags)) {
664 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
666 "unrecognized input \"%s\", "
667 "expect valid ipv6 addr",
669 if (status->status < 0)
671 memcpy(rule->src.ip.ip6.ip6_b,
673 } else if (IS_TRANSPORT(rule->flags)) {
674 APP_CHECK(0, status, "unrecognized input "
675 "\"%s\"", tokens[ti]);
683 if (strcmp(tokens[ti], "dst") == 0) {
684 APP_CHECK_PRESENCE(dst_p, tokens[ti], status);
685 if (status->status < 0)
688 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
689 if (status->status < 0)
692 if (IS_IP4_TUNNEL(rule->flags)) {
695 APP_CHECK(parse_ipv4_addr(tokens[ti],
696 &ip, NULL) == 0, status,
697 "unrecognized input \"%s\", "
698 "expect valid ipv4 addr",
700 if (status->status < 0)
702 rule->dst.ip.ip4 = rte_bswap32(
703 (uint32_t)ip.s_addr);
704 } else if (IS_IP6_TUNNEL(rule->flags)) {
707 APP_CHECK(parse_ipv6_addr(tokens[ti], &ip,
709 "unrecognized input \"%s\", "
710 "expect valid ipv6 addr",
712 if (status->status < 0)
714 memcpy(rule->dst.ip.ip6.ip6_b, ip.s6_addr, 16);
715 } else if (IS_TRANSPORT(rule->flags)) {
716 APP_CHECK(0, status, "unrecognized "
717 "input \"%s\"", tokens[ti]);
725 if (strcmp(tokens[ti], "type") == 0) {
726 APP_CHECK_PRESENCE(type_p, tokens[ti], status);
727 if (status->status < 0)
730 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
731 if (status->status < 0)
734 if (strcmp(tokens[ti], "inline-crypto-offload") == 0)
736 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO;
737 else if (strcmp(tokens[ti],
738 "inline-protocol-offload") == 0)
740 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL;
741 else if (strcmp(tokens[ti],
742 "lookaside-protocol-offload") == 0)
744 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL;
745 else if (strcmp(tokens[ti], "no-offload") == 0)
746 ips->type = RTE_SECURITY_ACTION_TYPE_NONE;
747 else if (strcmp(tokens[ti], "cpu-crypto") == 0)
748 ips->type = RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO;
750 APP_CHECK(0, status, "Invalid input \"%s\"",
759 if (strcmp(tokens[ti], "port_id") == 0) {
760 APP_CHECK_PRESENCE(portid_p, tokens[ti], status);
761 if (status->status < 0)
763 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
764 if (status->status < 0)
766 if (rule->portid == UINT16_MAX)
767 rule->portid = atoi(tokens[ti]);
768 else if (rule->portid != atoi(tokens[ti])) {
770 "portid %s not matching with already assigned portid %u",
771 tokens[ti], rule->portid);
778 if (strcmp(tokens[ti], "mss") == 0) {
779 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
780 if (status->status < 0)
782 rule->mss = atoi(tokens[ti]);
783 if (status->status < 0)
788 if (strcmp(tokens[ti], "esn") == 0) {
789 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
790 if (status->status < 0)
792 rule->esn = atoll(tokens[ti]);
793 if (status->status < 0)
798 if (strcmp(tokens[ti], "fallback") == 0) {
799 struct rte_ipsec_session *fb;
801 APP_CHECK(app_sa_prm.enable, status, "Fallback session "
802 "not allowed for legacy mode.");
803 if (status->status < 0)
805 APP_CHECK(ips->type ==
806 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO, status,
807 "Fallback session allowed if primary session "
808 "is of type inline-crypto-offload only.");
809 if (status->status < 0)
811 APP_CHECK(rule->direction ==
812 RTE_SECURITY_IPSEC_SA_DIR_INGRESS, status,
813 "Fallback session not allowed for egress "
815 if (status->status < 0)
817 APP_CHECK_PRESENCE(fallback_p, tokens[ti], status);
818 if (status->status < 0)
820 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
821 if (status->status < 0)
823 fb = ipsec_get_fallback_session(rule);
824 if (strcmp(tokens[ti], "lookaside-none") == 0)
825 fb->type = RTE_SECURITY_ACTION_TYPE_NONE;
826 else if (strcmp(tokens[ti], "cpu-crypto") == 0)
827 fb->type = RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO;
829 APP_CHECK(0, status, "unrecognized fallback "
830 "type %s.", tokens[ti]);
834 rule->fallback_sessions = 1;
835 nb_crypto_sessions++;
839 if (strcmp(tokens[ti], "flow-direction") == 0) {
841 case RTE_SECURITY_ACTION_TYPE_NONE:
842 case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO:
844 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
845 if (status->status < 0)
847 if (rule->portid == UINT16_MAX)
848 rule->portid = atoi(tokens[ti]);
849 else if (rule->portid != atoi(tokens[ti])) {
851 "portid %s not matching with already assigned portid %u",
852 tokens[ti], rule->portid);
855 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
856 if (status->status < 0)
858 rule->fdir_qid = atoi(tokens[ti]);
859 /* validating portid and queueid */
860 status_p = check_flow_params(rule->portid,
863 printf("port id %u / queue id %u is "
864 "not valid\n", rule->portid,
868 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
869 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
870 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
873 "flow director not supported for security session type %d",
879 if (strcmp(tokens[ti], "udp-encap") == 0) {
881 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
882 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
883 APP_CHECK_PRESENCE(udp_encap_p, tokens[ti],
885 if (status->status < 0)
889 app_sa_prm.udp_encap = 1;
892 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
895 rule->udp.dport = 4500;
899 "UDP encapsulation not supported for "
900 "security session type %d",
907 /* unrecognizable input */
908 APP_CHECK(0, status, "unrecognized input \"%s\"",
914 APP_CHECK(cipher_algo_p == 0, status,
915 "AEAD used, no need for cipher options");
916 if (status->status < 0)
919 APP_CHECK(auth_algo_p == 0, status,
920 "AEAD used, no need for auth options");
921 if (status->status < 0)
924 APP_CHECK(cipher_algo_p == 1, status, "missing cipher or AEAD options");
925 if (status->status < 0)
928 APP_CHECK(auth_algo_p == 1, status, "missing auth or AEAD options");
929 if (status->status < 0)
933 APP_CHECK(mode_p == 1, status, "missing mode option");
934 if (status->status < 0)
937 if ((ips->type != RTE_SECURITY_ACTION_TYPE_NONE && ips->type !=
938 RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO) && (portid_p == 0))
939 printf("Missing portid option, falling back to non-offload\n");
941 if (!type_p || (!portid_p && ips->type !=
942 RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO)) {
943 ips->type = RTE_SECURITY_ACTION_TYPE_NONE;
946 if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO)
947 wrkr_flags |= INL_CR_F;
948 else if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
949 wrkr_flags |= INL_PR_F;
950 else if (ips->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL)
951 wrkr_flags |= LA_PR_F;
953 wrkr_flags |= LA_ANY_F;
955 nb_crypto_sessions++;
960 print_one_sa_rule(const struct ipsec_sa *sa, int inbound)
964 const struct rte_ipsec_session *ips;
965 const struct rte_ipsec_session *fallback_ips;
967 printf("\tspi_%s(%3u):", inbound?"in":"out", sa->spi);
969 for (i = 0; i < RTE_DIM(cipher_algos); i++) {
970 if (cipher_algos[i].algo == sa->cipher_algo &&
971 cipher_algos[i].key_len == sa->cipher_key_len) {
972 printf("%s ", cipher_algos[i].keyword);
977 for (i = 0; i < RTE_DIM(auth_algos); i++) {
978 if (auth_algos[i].algo == sa->auth_algo) {
979 printf("%s ", auth_algos[i].keyword);
984 for (i = 0; i < RTE_DIM(aead_algos); i++) {
985 if (aead_algos[i].algo == sa->aead_algo &&
986 aead_algos[i].key_len-4 == sa->cipher_key_len) {
987 printf("%s ", aead_algos[i].keyword);
994 printf("UDP encapsulated ");
996 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
998 printf("IP4Tunnel ");
999 uint32_t_to_char(sa->src.ip.ip4, &a, &b, &c, &d);
1000 printf("%hhu.%hhu.%hhu.%hhu ", d, c, b, a);
1001 uint32_t_to_char(sa->dst.ip.ip4, &a, &b, &c, &d);
1002 printf("%hhu.%hhu.%hhu.%hhu", d, c, b, a);
1005 printf("IP6Tunnel ");
1006 for (i = 0; i < 16; i++) {
1007 if (i % 2 && i != 15)
1008 printf("%.2x:", sa->src.ip.ip6.ip6_b[i]);
1010 printf("%.2x", sa->src.ip.ip6.ip6_b[i]);
1013 for (i = 0; i < 16; i++) {
1014 if (i % 2 && i != 15)
1015 printf("%.2x:", sa->dst.ip.ip6.ip6_b[i]);
1017 printf("%.2x", sa->dst.ip.ip6.ip6_b[i]);
1021 printf("Transport ");
1025 ips = &sa->sessions[IPSEC_SESSION_PRIMARY];
1027 switch (ips->type) {
1028 case RTE_SECURITY_ACTION_TYPE_NONE:
1029 printf("no-offload ");
1031 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
1032 printf("inline-crypto-offload ");
1034 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
1035 printf("inline-protocol-offload ");
1037 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
1038 printf("lookaside-protocol-offload ");
1040 case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO:
1041 printf("cpu-crypto-accelerated ");
1045 fallback_ips = &sa->sessions[IPSEC_SESSION_FALLBACK];
1046 if (fallback_ips != NULL && sa->fallback_sessions > 0) {
1047 printf("inline fallback: ");
1048 switch (fallback_ips->type) {
1049 case RTE_SECURITY_ACTION_TYPE_NONE:
1050 printf("lookaside-none");
1052 case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO:
1053 printf("cpu-crypto-accelerated");
1060 if (sa->fdir_flag == 1)
1061 printf("flow-direction port %d queue %d", sa->portid,
1067 static struct sa_ctx *
1068 sa_create(const char *name, int32_t socket_id, uint32_t nb_sa)
1071 struct sa_ctx *sa_ctx;
1073 const struct rte_memzone *mz;
1075 snprintf(s, sizeof(s), "%s_%u", name, socket_id);
1077 /* Create SA context */
1078 printf("Creating SA context with %u maximum entries on socket %d\n",
1081 mz_size = sizeof(struct ipsec_xf) * nb_sa;
1082 mz = rte_memzone_reserve(s, mz_size, socket_id,
1083 RTE_MEMZONE_1GB | RTE_MEMZONE_SIZE_HINT_ONLY);
1085 printf("Failed to allocate SA XFORM memory\n");
1090 sa_ctx = rte_zmalloc(NULL, sizeof(struct sa_ctx) +
1091 sizeof(struct ipsec_sa) * nb_sa, RTE_CACHE_LINE_SIZE);
1093 if (sa_ctx == NULL) {
1094 printf("Failed to allocate SA CTX memory\n");
1096 rte_memzone_free(mz);
1100 sa_ctx->xf = (struct ipsec_xf *)mz->addr;
1101 sa_ctx->nb_sa = nb_sa;
1107 check_eth_dev_caps(uint16_t portid, uint32_t inbound, uint32_t tso)
1109 struct rte_eth_dev_info dev_info;
1112 retval = rte_eth_dev_info_get(portid, &dev_info);
1115 "Error during getting device (port %u) info: %s\n",
1116 portid, strerror(-retval));
1122 if ((dev_info.rx_offload_capa &
1123 RTE_ETH_RX_OFFLOAD_SECURITY) == 0) {
1124 RTE_LOG(WARNING, PORT,
1125 "hardware RX IPSec offload is not supported\n");
1129 } else { /* outbound */
1130 if ((dev_info.tx_offload_capa &
1131 RTE_ETH_TX_OFFLOAD_SECURITY) == 0) {
1132 RTE_LOG(WARNING, PORT,
1133 "hardware TX IPSec offload is not supported\n");
1136 if (tso && (dev_info.tx_offload_capa &
1137 RTE_ETH_TX_OFFLOAD_TCP_TSO) == 0) {
1138 RTE_LOG(WARNING, PORT,
1139 "hardware TCP TSO offload is not supported\n");
1147 * Helper function, tries to determine next_proto for SPI
1148 * by searching though SP rules.
1151 get_spi_proto(uint32_t spi, enum rte_security_ipsec_sa_direction dir,
1152 struct ip_addr ip_addr[2], uint32_t mask[2])
1156 rc4 = sp4_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
1158 rc6 = sp6_spi_present(spi, dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
1164 "%s: SPI %u used simultaneously by "
1165 "IPv4(%d) and IPv6 (%d) SP rules\n",
1166 __func__, spi, rc4, rc6);
1169 return IPPROTO_IPIP;
1170 } else if (rc6 < 0) {
1172 "%s: SPI %u is not used by any SP rule\n",
1176 return IPPROTO_IPV6;
1180 * Helper function for getting source and destination IP addresses
1181 * from SP. Needed for inline crypto transport mode, as addresses are not
1182 * provided in config file for that mode. It checks if SP for current SA exists,
1183 * and based on what type of protocol is returned, it stores appropriate
1184 * addresses got from SP into SA.
1187 sa_add_address_inline_crypto(struct ipsec_sa *sa)
1190 struct ip_addr ip_addr[2];
1193 protocol = get_spi_proto(sa->spi, sa->direction, ip_addr, mask);
1196 else if (protocol == IPPROTO_IPIP) {
1197 sa->flags |= IP4_TRANSPORT;
1198 if (mask[0] == IP4_FULL_MASK &&
1199 mask[1] == IP4_FULL_MASK &&
1200 ip_addr[0].ip.ip4 != 0 &&
1201 ip_addr[1].ip.ip4 != 0) {
1203 sa->src.ip.ip4 = ip_addr[0].ip.ip4;
1204 sa->dst.ip.ip4 = ip_addr[1].ip.ip4;
1207 "%s: No valid address or mask entry in"
1208 " IPv4 SP rule for SPI %u\n",
1212 } else if (protocol == IPPROTO_IPV6) {
1213 sa->flags |= IP6_TRANSPORT;
1214 if (mask[0] == IP6_FULL_MASK &&
1215 mask[1] == IP6_FULL_MASK &&
1216 (ip_addr[0].ip.ip6.ip6[0] != 0 ||
1217 ip_addr[0].ip.ip6.ip6[1] != 0) &&
1218 (ip_addr[1].ip.ip6.ip6[0] != 0 ||
1219 ip_addr[1].ip.ip6.ip6[1] != 0)) {
1221 sa->src.ip.ip6 = ip_addr[0].ip.ip6;
1222 sa->dst.ip.ip6 = ip_addr[1].ip.ip6;
1225 "%s: No valid address or mask entry in"
1226 " IPv6 SP rule for SPI %u\n",
1235 sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1236 uint32_t nb_entries, uint32_t inbound,
1237 struct socket_ctx *skt_ctx,
1238 struct ipsec_ctx *ips_ctx[])
1240 struct ipsec_sa *sa;
1242 uint16_t iv_length, aad_length;
1245 struct rte_ipsec_session *ips;
1247 /* for ESN upper 32 bits of SQN also need to be part of AAD */
1248 aad_length = (app_sa_prm.enable_esn != 0) ? sizeof(uint32_t) : 0;
1250 for (i = 0; i < nb_entries; i++) {
1252 sa = &sa_ctx->sa[idx];
1254 printf("Index %u already in use by SPI %u\n",
1261 rc = ipsec_sad_add(&sa_ctx->sad, sa);
1267 ips = ipsec_get_primary_session(sa);
1269 if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
1270 ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1271 if (check_eth_dev_caps(sa->portid, inbound, sa->mss))
1275 switch (WITHOUT_TRANSPORT_VERSION(sa->flags)) {
1277 sa->src.ip.ip4 = rte_cpu_to_be_32(sa->src.ip.ip4);
1278 sa->dst.ip.ip4 = rte_cpu_to_be_32(sa->dst.ip.ip4);
1282 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1284 sa_add_address_inline_crypto(sa);
1285 if (inline_status < 0)
1286 return inline_status;
1292 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_GCM ||
1293 sa->aead_algo == RTE_CRYPTO_AEAD_AES_CCM ||
1294 sa->aead_algo == RTE_CRYPTO_AEAD_CHACHA20_POLY1305) {
1296 if (sa->aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
1301 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1302 sa_ctx->xf[idx].a.aead.algo = sa->aead_algo;
1303 sa_ctx->xf[idx].a.aead.key.data = sa->cipher_key;
1304 sa_ctx->xf[idx].a.aead.key.length =
1306 sa_ctx->xf[idx].a.aead.op = (inbound == 1) ?
1307 RTE_CRYPTO_AEAD_OP_DECRYPT :
1308 RTE_CRYPTO_AEAD_OP_ENCRYPT;
1309 sa_ctx->xf[idx].a.next = NULL;
1310 sa_ctx->xf[idx].a.aead.iv.offset = IV_OFFSET;
1311 sa_ctx->xf[idx].a.aead.iv.length = iv_length;
1312 sa_ctx->xf[idx].a.aead.aad_length =
1313 sa->aad_len + aad_length;
1314 sa_ctx->xf[idx].a.aead.digest_length =
1317 sa->xforms = &sa_ctx->xf[idx].a;
1319 switch (sa->cipher_algo) {
1320 case RTE_CRYPTO_CIPHER_NULL:
1321 case RTE_CRYPTO_CIPHER_DES_CBC:
1322 case RTE_CRYPTO_CIPHER_3DES_CBC:
1323 case RTE_CRYPTO_CIPHER_AES_CBC:
1324 case RTE_CRYPTO_CIPHER_AES_CTR:
1325 iv_length = sa->iv_len;
1328 RTE_LOG(ERR, IPSEC_ESP,
1329 "unsupported cipher algorithm %u\n",
1334 /* AES_GMAC uses salt like AEAD algorithms */
1335 if (sa->auth_algo == RTE_CRYPTO_AUTH_AES_GMAC)
1339 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1340 sa_ctx->xf[idx].b.cipher.algo = sa->cipher_algo;
1341 sa_ctx->xf[idx].b.cipher.key.data = sa->cipher_key;
1342 sa_ctx->xf[idx].b.cipher.key.length =
1344 sa_ctx->xf[idx].b.cipher.op =
1345 RTE_CRYPTO_CIPHER_OP_DECRYPT;
1346 sa_ctx->xf[idx].b.next = NULL;
1347 sa_ctx->xf[idx].b.cipher.iv.offset = IV_OFFSET;
1348 sa_ctx->xf[idx].b.cipher.iv.length = iv_length;
1350 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1351 sa_ctx->xf[idx].a.auth.algo = sa->auth_algo;
1352 sa_ctx->xf[idx].a.auth.key.data = sa->auth_key;
1353 sa_ctx->xf[idx].a.auth.key.length =
1355 sa_ctx->xf[idx].a.auth.digest_length =
1357 sa_ctx->xf[idx].a.auth.op =
1358 RTE_CRYPTO_AUTH_OP_VERIFY;
1359 sa_ctx->xf[idx].a.auth.iv.offset = IV_OFFSET;
1360 sa_ctx->xf[idx].a.auth.iv.length = iv_length;
1362 } else { /* outbound */
1363 sa_ctx->xf[idx].a.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1364 sa_ctx->xf[idx].a.cipher.algo = sa->cipher_algo;
1365 sa_ctx->xf[idx].a.cipher.key.data = sa->cipher_key;
1366 sa_ctx->xf[idx].a.cipher.key.length =
1368 sa_ctx->xf[idx].a.cipher.op =
1369 RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1370 sa_ctx->xf[idx].a.next = NULL;
1371 sa_ctx->xf[idx].a.cipher.iv.offset = IV_OFFSET;
1372 sa_ctx->xf[idx].a.cipher.iv.length = iv_length;
1374 sa_ctx->xf[idx].b.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1375 sa_ctx->xf[idx].b.auth.algo = sa->auth_algo;
1376 sa_ctx->xf[idx].b.auth.key.data = sa->auth_key;
1377 sa_ctx->xf[idx].b.auth.key.length =
1379 sa_ctx->xf[idx].b.auth.digest_length =
1381 sa_ctx->xf[idx].b.auth.op =
1382 RTE_CRYPTO_AUTH_OP_GENERATE;
1383 sa_ctx->xf[idx].b.auth.iv.offset = IV_OFFSET;
1384 sa_ctx->xf[idx].b.auth.iv.length = iv_length;
1388 if (sa->auth_algo == RTE_CRYPTO_AUTH_AES_GMAC) {
1389 sa->xforms = inbound ?
1390 &sa_ctx->xf[idx].a : &sa_ctx->xf[idx].b;
1391 sa->xforms->next = NULL;
1394 sa_ctx->xf[idx].a.next = &sa_ctx->xf[idx].b;
1395 sa_ctx->xf[idx].b.next = NULL;
1396 sa->xforms = &sa_ctx->xf[idx].a;
1401 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL ||
1403 RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1404 rc = create_inline_session(skt_ctx, sa, ips);
1406 RTE_LOG(ERR, IPSEC_ESP,
1407 "create_inline_session() failed\n");
1411 rc = create_lookaside_session(ips_ctx, skt_ctx, sa, ips);
1413 RTE_LOG(ERR, IPSEC_ESP,
1414 "create_lookaside_session() failed\n");
1419 if (sa->fdir_flag && inbound) {
1420 rc = create_ipsec_esp_flow(sa);
1422 RTE_LOG(ERR, IPSEC_ESP,
1423 "create_ipsec_esp_flow() failed\n");
1425 print_one_sa_rule(sa, inbound);
1432 sa_out_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1433 uint32_t nb_entries, struct socket_ctx *skt_ctx,
1434 struct ipsec_ctx *ips_ctx[])
1436 return sa_add_rules(sa_ctx, entries, nb_entries, 0, skt_ctx, ips_ctx);
1440 sa_in_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
1441 uint32_t nb_entries, struct socket_ctx *skt_ctx,
1442 struct ipsec_ctx *ips_ctx[])
1444 return sa_add_rules(sa_ctx, entries, nb_entries, 1, skt_ctx, ips_ctx);
1448 * helper function, fills parameters that are identical for all SAs
1451 fill_ipsec_app_sa_prm(struct rte_ipsec_sa_prm *prm,
1452 const struct app_sa_prm *app_prm)
1454 memset(prm, 0, sizeof(*prm));
1456 prm->flags = app_prm->flags;
1457 prm->ipsec_xform.options.esn = app_prm->enable_esn;
1458 prm->ipsec_xform.replay_win_sz = app_prm->window_size;
1462 fill_ipsec_sa_prm(struct rte_ipsec_sa_prm *prm, const struct ipsec_sa *ss,
1463 const struct rte_ipv4_hdr *v4, struct rte_ipv6_hdr *v6)
1468 * Try to get SPI next proto by searching that SPI in SPD.
1469 * probably not the optimal way, but there seems nothing
1472 rc = get_spi_proto(ss->spi, ss->direction, NULL, NULL);
1476 fill_ipsec_app_sa_prm(prm, &app_sa_prm);
1477 prm->userdata = (uintptr_t)ss;
1479 /* setup ipsec xform */
1480 prm->ipsec_xform.spi = ss->spi;
1481 prm->ipsec_xform.salt = ss->salt;
1482 prm->ipsec_xform.direction = ss->direction;
1483 prm->ipsec_xform.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP;
1484 prm->ipsec_xform.mode = (IS_TRANSPORT(ss->flags)) ?
1485 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT :
1486 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL;
1487 prm->ipsec_xform.options.udp_encap = ss->udp_encap;
1488 prm->ipsec_xform.options.ecn = 1;
1489 prm->ipsec_xform.options.copy_dscp = 1;
1491 if (IS_IP4_TUNNEL(ss->flags)) {
1492 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV4;
1493 prm->tun.hdr_len = sizeof(*v4);
1494 prm->tun.next_proto = rc;
1496 } else if (IS_IP6_TUNNEL(ss->flags)) {
1497 prm->ipsec_xform.tunnel.type = RTE_SECURITY_IPSEC_TUNNEL_IPV6;
1498 prm->tun.hdr_len = sizeof(*v6);
1499 prm->tun.next_proto = rc;
1502 /* transport mode */
1503 prm->trs.proto = rc;
1506 /* setup crypto section */
1507 prm->crypto_xform = ss->xforms;
1512 fill_ipsec_session(struct rte_ipsec_session *ss, struct rte_ipsec_sa *sa)
1518 rc = rte_ipsec_session_prepare(ss);
1520 memset(ss, 0, sizeof(*ss));
1526 * Initialise related rte_ipsec_sa object.
1529 ipsec_sa_init(struct ipsec_sa *lsa, struct rte_ipsec_sa *sa, uint32_t sa_size)
1532 struct rte_ipsec_sa_prm prm;
1533 struct rte_ipsec_session *ips;
1534 struct rte_ipv4_hdr v4 = {
1535 .version_ihl = IPVERSION << 4 |
1536 sizeof(v4) / RTE_IPV4_IHL_MULTIPLIER,
1537 .time_to_live = IPDEFTTL,
1538 .next_proto_id = IPPROTO_ESP,
1539 .src_addr = lsa->src.ip.ip4,
1540 .dst_addr = lsa->dst.ip.ip4,
1542 struct rte_ipv6_hdr v6 = {
1543 .vtc_flow = htonl(IP6_VERSION << 28),
1544 .proto = IPPROTO_ESP,
1547 if (IS_IP6_TUNNEL(lsa->flags)) {
1548 memcpy(v6.src_addr, lsa->src.ip.ip6.ip6_b, sizeof(v6.src_addr));
1549 memcpy(v6.dst_addr, lsa->dst.ip.ip6.ip6_b, sizeof(v6.dst_addr));
1552 rc = fill_ipsec_sa_prm(&prm, lsa, &v4, &v6);
1554 rc = rte_ipsec_sa_init(sa, &prm, sa_size);
1558 if (lsa->flags & SA_TELEMETRY_ENABLE)
1559 rte_ipsec_telemetry_sa_add(sa);
1561 /* init primary processing session */
1562 ips = ipsec_get_primary_session(lsa);
1563 rc = fill_ipsec_session(ips, sa);
1567 /* init inline fallback processing session */
1568 if (lsa->fallback_sessions == 1)
1569 rc = fill_ipsec_session(ipsec_get_fallback_session(lsa), sa);
1575 * Allocate space and init rte_ipsec_sa structures,
1579 ipsec_satbl_init(struct sa_ctx *ctx, uint32_t nb_ent, int32_t socket)
1584 struct rte_ipsec_sa *sa;
1585 struct ipsec_sa *lsa;
1586 struct rte_ipsec_sa_prm prm;
1588 /* determine SA size */
1590 fill_ipsec_sa_prm(&prm, ctx->sa + idx, NULL, NULL);
1591 sz = rte_ipsec_sa_size(&prm);
1593 RTE_LOG(ERR, IPSEC, "%s(%p, %u, %d): "
1594 "failed to determine SA size, error code: %d\n",
1595 __func__, ctx, nb_ent, socket, sz);
1601 ctx->satbl = rte_zmalloc_socket(NULL, tsz, RTE_CACHE_LINE_SIZE, socket);
1602 if (ctx->satbl == NULL) {
1604 "%s(%p, %u, %d): failed to allocate %zu bytes\n",
1605 __func__, ctx, nb_ent, socket, tsz);
1610 for (i = 0; i != nb_ent && rc == 0; i++) {
1614 sa = (struct rte_ipsec_sa *)((uintptr_t)ctx->satbl + sz * i);
1615 lsa = ctx->sa + idx;
1617 rc = ipsec_sa_init(lsa, sa, sz);
1624 sa_cmp(const void *p, const void *q)
1626 uint32_t spi1 = ((const struct ipsec_sa *)p)->spi;
1627 uint32_t spi2 = ((const struct ipsec_sa *)q)->spi;
1629 return (int)(spi1 - spi2);
1633 * Walk through all SA rules to find an SA with given SPI
1636 sa_spi_present(struct sa_ctx *sa_ctx, uint32_t spi, int inbound)
1639 struct ipsec_sa *sa;
1640 struct ipsec_sa tmpl;
1641 const struct ipsec_sa *sar;
1651 sa = bsearch(&tmpl, sar, num, sizeof(struct ipsec_sa), sa_cmp);
1653 return RTE_PTR_DIFF(sa, sar) / sizeof(struct ipsec_sa);
1659 sa_init(struct socket_ctx *ctx, int32_t socket_id,
1660 struct lcore_conf *lcore_conf)
1665 struct ipsec_ctx *ipsec_ctx[RTE_MAX_LCORE];
1668 rte_exit(EXIT_FAILURE, "NULL context.\n");
1670 if (ctx->sa_in != NULL)
1671 rte_exit(EXIT_FAILURE, "Inbound SA DB for socket %u already "
1672 "initialized\n", socket_id);
1674 if (ctx->sa_out != NULL)
1675 rte_exit(EXIT_FAILURE, "Outbound SA DB for socket %u already "
1676 "initialized\n", socket_id);
1680 ctx->sa_in = sa_create(name, socket_id, nb_sa_in);
1681 if (ctx->sa_in == NULL)
1682 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1683 "context %s in socket %d\n", rte_errno,
1686 rc = ipsec_sad_create(name, &ctx->sa_in->sad, socket_id,
1689 rte_exit(EXIT_FAILURE, "failed to init SAD\n");
1690 RTE_LCORE_FOREACH(lcore_id)
1691 ipsec_ctx[lcore_id] = &lcore_conf[lcore_id].inbound;
1692 sa_in_add_rules(ctx->sa_in, sa_in, nb_sa_in, ctx, ipsec_ctx);
1694 if (app_sa_prm.enable != 0) {
1695 rc = ipsec_satbl_init(ctx->sa_in, nb_sa_in,
1698 rte_exit(EXIT_FAILURE,
1699 "failed to init inbound SAs\n");
1702 RTE_LOG(WARNING, IPSEC, "No SA Inbound rule specified\n");
1704 if (nb_sa_out > 0) {
1706 ctx->sa_out = sa_create(name, socket_id, nb_sa_out);
1707 if (ctx->sa_out == NULL)
1708 rte_exit(EXIT_FAILURE, "Error [%d] creating SA "
1709 "context %s in socket %d\n", rte_errno,
1712 RTE_LCORE_FOREACH(lcore_id)
1713 ipsec_ctx[lcore_id] = &lcore_conf[lcore_id].outbound;
1714 sa_out_add_rules(ctx->sa_out, sa_out, nb_sa_out, ctx, ipsec_ctx);
1716 if (app_sa_prm.enable != 0) {
1717 rc = ipsec_satbl_init(ctx->sa_out, nb_sa_out,
1720 rte_exit(EXIT_FAILURE,
1721 "failed to init outbound SAs\n");
1724 RTE_LOG(WARNING, IPSEC, "No SA Outbound rule "
1729 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx)
1731 struct ipsec_mbuf_metadata *priv;
1732 struct ipsec_sa *sa;
1737 return (sa_ctx->sa[sa_idx].spi == sa->spi);
1739 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
1744 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
1745 void *sa_arr[], uint16_t nb_pkts)
1749 struct ipsec_sa *sa;
1751 sad_lookup(&sa_ctx->sad, pkts, sa_arr, nb_pkts);
1754 * Mark need for inline offload fallback on the LSB of SA pointer.
1755 * Thanks to packet grouping mechanism which ipsec_process is using
1756 * packets marked for fallback processing will form separate group.
1758 * Because it is not safe to use SA pointer it is casted to generic
1759 * pointer to prevent from unintentional use. Use ipsec_mask_saptr
1760 * to get valid struct pointer.
1762 for (i = 0; i < nb_pkts; i++) {
1763 if (sa_arr[i] == NULL)
1766 result_sa = sa = sa_arr[i];
1767 if (MBUF_NO_SEC_OFFLOAD(pkts[i]) &&
1768 sa->fallback_sessions > 0) {
1769 uintptr_t intsa = (uintptr_t)sa;
1770 intsa |= IPSEC_SA_OFFLOAD_FALLBACK_FLAG;
1771 result_sa = (void *)intsa;
1773 sa_arr[i] = result_sa;
1778 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
1779 void *sa[], uint16_t nb_pkts)
1783 for (i = 0; i < nb_pkts; i++)
1784 sa[i] = &sa_ctx->sa[sa_idx[i]];
1788 * Select HW offloads to be used.
1791 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads,
1792 uint64_t *tx_offloads)
1794 struct ipsec_sa *rule;
1796 enum rte_security_session_action_type rule_type;
1797 struct rte_eth_dev_info dev_info;
1803 ret = rte_eth_dev_info_get(port_id, &dev_info);
1805 rte_exit(EXIT_FAILURE,
1806 "Error during getting device (port %u) info: %s\n",
1807 port_id, strerror(-ret));
1809 /* Check for inbound rules that use offloads and use this port */
1810 for (idx_sa = 0; idx_sa < nb_sa_in; idx_sa++) {
1811 rule = &sa_in[idx_sa];
1812 rule_type = ipsec_get_action_type(rule);
1813 if ((rule_type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
1815 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
1816 && rule->portid == port_id)
1817 *rx_offloads |= RTE_ETH_RX_OFFLOAD_SECURITY;
1820 /* Check for outbound rules that use offloads and use this port */
1821 for (idx_sa = 0; idx_sa < nb_sa_out; idx_sa++) {
1822 rule = &sa_out[idx_sa];
1823 rule_type = ipsec_get_action_type(rule);
1824 switch (rule_type) {
1825 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
1826 /* Checksum offload is not needed for inline protocol as
1827 * all processing for Outbound IPSec packets will be
1828 * implicitly taken care and for non-IPSec packets,
1829 * there is no need of IPv4 Checksum offload.
1831 if (rule->portid == port_id) {
1832 *tx_offloads |= RTE_ETH_TX_OFFLOAD_SECURITY;
1834 *tx_offloads |= (RTE_ETH_TX_OFFLOAD_TCP_TSO |
1835 RTE_ETH_TX_OFFLOAD_IPV4_CKSUM);
1838 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
1839 if (rule->portid == port_id) {
1840 *tx_offloads |= RTE_ETH_TX_OFFLOAD_SECURITY;
1843 RTE_ETH_TX_OFFLOAD_TCP_TSO;
1844 *tx_offloads |= RTE_ETH_TX_OFFLOAD_IPV4_CKSUM;
1848 /* Enable IPv4 checksum offload even if one of lookaside
1851 if (dev_info.tx_offload_capa &
1852 RTE_ETH_TX_OFFLOAD_IPV4_CKSUM)
1853 *tx_offloads |= RTE_ETH_TX_OFFLOAD_IPV4_CKSUM;
1863 qsort(sa_in, nb_sa_in, sizeof(struct ipsec_sa), sa_cmp);
1864 qsort(sa_out, nb_sa_out, sizeof(struct ipsec_sa), sa_cmp);
1868 get_nb_crypto_sessions(void)
1870 return nb_crypto_sessions;