LCOV - code coverage report
Current view: top level - drivers/crypto/cnxk - cn20k_ipsec.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 133 0.0 %
Date: 2025-08-01 17:49:26 Functions: 0 6 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 52 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(C) 2025 Marvell.
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <cryptodev_pmd.h>
       6                 :            : #include <rte_esp.h>
       7                 :            : #include <rte_ip.h>
       8                 :            : #include <rte_malloc.h>
       9                 :            : #include <rte_security.h>
      10                 :            : #include <rte_security_driver.h>
      11                 :            : #include <rte_udp.h>
      12                 :            : 
      13                 :            : #include "cn20k_cryptodev_ops.h"
      14                 :            : #include "cn20k_cryptodev_sec.h"
      15                 :            : #include "cn20k_ipsec.h"
      16                 :            : #include "cnxk_cryptodev.h"
      17                 :            : #include "cnxk_cryptodev_ops.h"
      18                 :            : #include "cnxk_ipsec.h"
      19                 :            : #include "cnxk_security.h"
      20                 :            : 
      21                 :            : #include "roc_api.h"
      22                 :            : 
      23                 :            : static int
      24                 :          0 : cn20k_ipsec_outb_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
      25                 :            :                            struct rte_security_ipsec_xform *ipsec_xfrm,
      26                 :            :                            struct rte_crypto_sym_xform *crypto_xfrm,
      27                 :            :                            struct cn20k_sec_session *sec_sess)
      28                 :            : {
      29                 :            :         union roc_ow_ipsec_outb_param1 param1;
      30                 :            :         struct roc_ow_ipsec_outb_sa *sa_dptr;
      31                 :            :         struct cnxk_ipsec_outb_rlens rlens;
      32                 :            :         struct cn20k_ipsec_sa *sa;
      33                 :            :         union cpt_inst_w4 inst_w4;
      34                 :            :         void *out_sa;
      35                 :            :         int ret = 0;
      36                 :            : 
      37                 :            :         sa = &sec_sess->sa;
      38                 :          0 :         out_sa = &sa->out_sa;
      39                 :            : 
      40                 :            :         /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
      41                 :          0 :         sa_dptr = plt_zmalloc(sizeof(struct roc_ow_ipsec_outb_sa), 8);
      42         [ #  # ]:          0 :         if (sa_dptr == NULL) {
      43                 :          0 :                 plt_err("Could not allocate memory for SA dptr");
      44                 :          0 :                 return -ENOMEM;
      45                 :            :         }
      46                 :            : 
      47                 :            :         /* Translate security parameters to SA */
      48                 :          0 :         ret = cnxk_ow_ipsec_outb_sa_fill(sa_dptr, ipsec_xfrm, crypto_xfrm);
      49         [ #  # ]:          0 :         if (ret) {
      50                 :          0 :                 plt_err("Could not fill outbound session parameters");
      51                 :          0 :                 goto sa_dptr_free;
      52                 :            :         }
      53                 :            : 
      54                 :          0 :         sec_sess->inst.w7 = cnxk_cpt_sec_inst_w7_get(roc_cpt, out_sa);
      55                 :            : 
      56                 :            : #ifdef LA_IPSEC_DEBUG
      57                 :            :         /* Use IV from application in debug mode */
      58                 :            :         if (ipsec_xfrm->options.iv_gen_disable == 1) {
      59                 :            :                 sa_dptr->w2.s.iv_src = ROC_IE_OW_SA_IV_SRC_FROM_SA;
      60                 :            :                 if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
      61                 :            :                         sec_sess->iv_offset = crypto_xfrm->aead.iv.offset;
      62                 :            :                         sec_sess->iv_length = crypto_xfrm->aead.iv.length;
      63                 :            :                 } else if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
      64                 :            :                         sec_sess->iv_offset = crypto_xfrm->cipher.iv.offset;
      65                 :            :                         sec_sess->iv_length = crypto_xfrm->cipher.iv.length;
      66                 :            :                 } else {
      67                 :            :                         sec_sess->iv_offset = crypto_xfrm->auth.iv.offset;
      68                 :            :                         sec_sess->iv_length = crypto_xfrm->auth.iv.length;
      69                 :            :                 }
      70                 :            :         }
      71                 :            : #else
      72         [ #  # ]:          0 :         if (ipsec_xfrm->options.iv_gen_disable != 0) {
      73                 :          0 :                 plt_err("Application provided IV not supported");
      74                 :            :                 ret = -ENOTSUP;
      75                 :          0 :                 goto sa_dptr_free;
      76                 :            :         }
      77                 :            : #endif
      78                 :            : 
      79                 :          0 :         sec_sess->ipsec.is_outbound = 1;
      80                 :            : 
      81                 :            :         /* Get Rlen calculation data */
      82                 :          0 :         ret = cnxk_ipsec_outb_rlens_get(&rlens, ipsec_xfrm, crypto_xfrm);
      83         [ #  # ]:          0 :         if (ret)
      84                 :          0 :                 goto sa_dptr_free;
      85                 :            : 
      86                 :          0 :         sec_sess->max_extended_len = rlens.max_extended_len;
      87                 :            : 
      88                 :            :         /* pre-populate CPT INST word 4 */
      89                 :          0 :         inst_w4.u64 = 0;
      90                 :          0 :         inst_w4.s.opcode_major = ROC_IE_OW_MAJOR_OP_PROCESS_OUTBOUND_IPSEC | ROC_IE_OW_INPLACE_BIT;
      91                 :            : 
      92                 :          0 :         param1.u16 = 0;
      93                 :            : 
      94                 :          0 :         param1.s.ttl_or_hop_limit = ipsec_xfrm->options.dec_ttl;
      95                 :            : 
      96                 :            :         /* Disable IP checksum computation by default */
      97                 :          0 :         param1.s.ip_csum_disable = ROC_IE_OW_SA_INNER_PKT_IP_CSUM_DISABLE;
      98                 :            : 
      99         [ #  # ]:          0 :         if (ipsec_xfrm->options.ip_csum_enable)
     100                 :          0 :                 param1.s.ip_csum_disable = ROC_IE_OW_SA_INNER_PKT_IP_CSUM_ENABLE;
     101                 :            : 
     102                 :            :         /* Disable L4 checksum computation by default */
     103                 :          0 :         param1.s.l4_csum_disable = ROC_IE_OW_SA_INNER_PKT_L4_CSUM_DISABLE;
     104                 :            : 
     105         [ #  # ]:          0 :         if (ipsec_xfrm->options.l4_csum_enable)
     106                 :          0 :                 param1.s.l4_csum_disable = ROC_IE_OW_SA_INNER_PKT_L4_CSUM_ENABLE;
     107                 :            : 
     108                 :          0 :         inst_w4.s.param1 = param1.u16;
     109                 :            : 
     110                 :          0 :         sec_sess->inst.w4 = inst_w4.u64;
     111                 :            : 
     112         [ #  # ]:          0 :         if (ipsec_xfrm->options.stats == 1) {
     113                 :            :                 /* Enable mib counters */
     114                 :          0 :                 sa_dptr->w0.s.count_mib_bytes = 1;
     115                 :          0 :                 sa_dptr->w0.s.count_mib_pkts = 1;
     116                 :          0 :                 sa_dptr->w0.s.count_glb_pkts = 1;
     117                 :          0 :                 sa_dptr->w0.s.count_glb_octets = 1;
     118                 :            :         }
     119                 :            : 
     120                 :            :         memset(out_sa, 0, sizeof(struct roc_ow_ipsec_outb_sa));
     121                 :            : 
     122                 :            :         /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
     123                 :            :         memcpy(out_sa, sa_dptr, 8);
     124                 :            : 
     125                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
     126                 :            : 
     127                 :            :         /* Write session using microcode opcode */
     128                 :          0 :         ret = roc_cpt_ctx_write(lf, sa_dptr, out_sa, sizeof(struct roc_ow_ipsec_outb_sa));
     129         [ #  # ]:          0 :         if (ret) {
     130                 :          0 :                 plt_err("Could not write outbound session to hardware");
     131                 :          0 :                 goto sa_dptr_free;
     132                 :            :         }
     133                 :            : 
     134                 :            :         /* Trigger CTX flush so that data is written back to DRAM */
     135                 :          0 :         ret = roc_cpt_lf_ctx_flush(lf, out_sa, false);
     136         [ #  # ]:          0 :         if (ret == -EFAULT) {
     137                 :          0 :                 plt_err("Could not flush outbound session");
     138                 :          0 :                 goto sa_dptr_free;
     139                 :            :         }
     140                 :            : 
     141                 :          0 :         sec_sess->proto = RTE_SECURITY_PROTOCOL_IPSEC;
     142                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
     143                 :            : 
     144                 :          0 : sa_dptr_free:
     145                 :          0 :         plt_free(sa_dptr);
     146                 :            : 
     147                 :          0 :         return ret;
     148                 :            : }
     149                 :            : 
     150                 :            : static int
     151                 :          0 : cn20k_ipsec_inb_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
     152                 :            :                           struct rte_security_ipsec_xform *ipsec_xfrm,
     153                 :            :                           struct rte_crypto_sym_xform *crypto_xfrm,
     154                 :            :                           struct cn20k_sec_session *sec_sess)
     155                 :            : {
     156                 :            :         union roc_ow_ipsec_inb_param1 param1;
     157                 :            :         struct roc_ow_ipsec_inb_sa *sa_dptr;
     158                 :            :         struct cn20k_ipsec_sa *sa;
     159                 :            :         union cpt_inst_w4 inst_w4;
     160                 :            :         void *in_sa;
     161                 :            :         int ret = 0;
     162                 :            : 
     163                 :            :         sa = &sec_sess->sa;
     164                 :          0 :         in_sa = &sa->in_sa;
     165                 :            : 
     166                 :            :         /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
     167                 :          0 :         sa_dptr = plt_zmalloc(sizeof(struct roc_ow_ipsec_inb_sa), 8);
     168         [ #  # ]:          0 :         if (sa_dptr == NULL) {
     169                 :          0 :                 plt_err("Could not allocate memory for SA dptr");
     170                 :          0 :                 return -ENOMEM;
     171                 :            :         }
     172                 :            : 
     173                 :            :         /* Translate security parameters to SA */
     174                 :          0 :         ret = cnxk_ow_ipsec_inb_sa_fill(sa_dptr, ipsec_xfrm, crypto_xfrm);
     175         [ #  # ]:          0 :         if (ret) {
     176                 :          0 :                 plt_err("Could not fill inbound session parameters");
     177                 :          0 :                 goto sa_dptr_free;
     178                 :            :         }
     179                 :            : 
     180         [ #  # ]:          0 :         sec_sess->ipsec.is_outbound = 0;
     181                 :          0 :         sec_sess->inst.w7 = cnxk_cpt_sec_inst_w7_get(roc_cpt, in_sa);
     182                 :            : 
     183                 :            :         /* Save index/SPI in cookie, requirement for Rx Inject */
     184                 :          0 :         sa_dptr->w1.s.cookie = 0xFFFFFFFF;
     185                 :            : 
     186                 :            :         /* pre-populate CPT INST word 4 */
     187                 :          0 :         inst_w4.u64 = 0;
     188                 :          0 :         inst_w4.s.opcode_major = ROC_IE_OW_MAJOR_OP_PROCESS_INBOUND_IPSEC | ROC_IE_OW_INPLACE_BIT;
     189                 :            : 
     190                 :          0 :         param1.u16 = 0;
     191                 :            : 
     192                 :            :         /* Disable IP checksum verification by default */
     193                 :          0 :         param1.s.ip_csum_disable = ROC_IE_OW_SA_INNER_PKT_IP_CSUM_DISABLE;
     194                 :            : 
     195                 :            :         /* Set the ip chksum flag in mbuf before enqueue.
     196                 :            :          * Reset the flag in post process in case of errors
     197                 :            :          */
     198         [ #  # ]:          0 :         if (ipsec_xfrm->options.ip_csum_enable) {
     199                 :          0 :                 param1.s.ip_csum_disable = ROC_IE_OW_SA_INNER_PKT_IP_CSUM_ENABLE;
     200                 :          0 :                 sec_sess->ipsec.ip_csum = RTE_MBUF_F_RX_IP_CKSUM_GOOD;
     201                 :            :         }
     202                 :            : 
     203                 :            :         /* Disable L4 checksum verification by default */
     204                 :          0 :         param1.s.l4_csum_disable = ROC_IE_OW_SA_INNER_PKT_L4_CSUM_DISABLE;
     205                 :            : 
     206         [ #  # ]:          0 :         if (ipsec_xfrm->options.l4_csum_enable)
     207                 :          0 :                 param1.s.l4_csum_disable = ROC_IE_OW_SA_INNER_PKT_L4_CSUM_ENABLE;
     208                 :            : 
     209                 :          0 :         param1.s.esp_trailer_disable = 1;
     210                 :            : 
     211                 :          0 :         inst_w4.s.param1 = param1.u16;
     212                 :            : 
     213                 :          0 :         sec_sess->inst.w4 = inst_w4.u64;
     214                 :            : 
     215         [ #  # ]:          0 :         if (ipsec_xfrm->options.stats == 1) {
     216                 :            :                 /* Enable mib counters */
     217                 :          0 :                 sa_dptr->w0.s.count_mib_bytes = 1;
     218                 :          0 :                 sa_dptr->w0.s.count_mib_pkts = 1;
     219                 :          0 :                 sa_dptr->w0.s.count_glb_pkts = 1;
     220                 :          0 :                 sa_dptr->w0.s.count_glb_octets = 1;
     221                 :            :         }
     222                 :            : 
     223                 :            :         memset(in_sa, 0, sizeof(struct roc_ow_ipsec_inb_sa));
     224                 :            : 
     225                 :            :         /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
     226                 :            :         memcpy(in_sa, sa_dptr, 8);
     227                 :            : 
     228                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
     229                 :            : 
     230                 :            :         /* Write session using microcode opcode */
     231                 :          0 :         ret = roc_cpt_ctx_write(lf, sa_dptr, in_sa, sizeof(struct roc_ow_ipsec_inb_sa));
     232         [ #  # ]:          0 :         if (ret) {
     233                 :          0 :                 plt_err("Could not write inbound session to hardware");
     234                 :          0 :                 goto sa_dptr_free;
     235                 :            :         }
     236                 :            : 
     237                 :            :         /* Trigger CTX flush so that data is written back to DRAM */
     238                 :          0 :         ret = roc_cpt_lf_ctx_flush(lf, in_sa, true);
     239         [ #  # ]:          0 :         if (ret == -EFAULT) {
     240                 :          0 :                 plt_err("Could not flush inbound session");
     241                 :          0 :                 goto sa_dptr_free;
     242                 :            :         }
     243                 :            : 
     244                 :          0 :         sec_sess->proto = RTE_SECURITY_PROTOCOL_IPSEC;
     245                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
     246                 :            : 
     247                 :          0 : sa_dptr_free:
     248                 :          0 :         plt_free(sa_dptr);
     249                 :            : 
     250                 :          0 :         return ret;
     251                 :            : }
     252                 :            : 
     253                 :            : int
     254                 :          0 : cn20k_ipsec_session_create(struct cnxk_cpt_vf *vf, struct cnxk_cpt_qp *qp,
     255                 :            :                            struct rte_security_ipsec_xform *ipsec_xfrm,
     256                 :            :                            struct rte_crypto_sym_xform *crypto_xfrm,
     257                 :            :                            struct rte_security_session *sess)
     258                 :            : {
     259                 :            :         struct roc_cpt *roc_cpt;
     260                 :            :         int ret;
     261                 :            : 
     262                 :          0 :         ret = cnxk_ipsec_xform_verify(ipsec_xfrm, crypto_xfrm);
     263         [ #  # ]:          0 :         if (ret)
     264                 :            :                 return ret;
     265                 :            : 
     266                 :          0 :         roc_cpt = &vf->cpt;
     267                 :            : 
     268         [ #  # ]:          0 :         if (ipsec_xfrm->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
     269                 :          0 :                 return cn20k_ipsec_inb_sa_create(roc_cpt, &qp->lf, ipsec_xfrm, crypto_xfrm,
     270                 :            :                                                  (struct cn20k_sec_session *)sess);
     271                 :            :         else
     272                 :          0 :                 return cn20k_ipsec_outb_sa_create(roc_cpt, &qp->lf, ipsec_xfrm, crypto_xfrm,
     273                 :            :                                                   (struct cn20k_sec_session *)sess);
     274                 :            : }
     275                 :            : 
     276                 :            : int
     277                 :          0 : cn20k_sec_ipsec_session_destroy(struct cnxk_cpt_qp *qp, struct cn20k_sec_session *sess)
     278                 :            : {
     279                 :            :         union roc_ow_ipsec_sa_word2 *w2;
     280                 :            :         struct cn20k_ipsec_sa *sa;
     281                 :            :         struct roc_cpt_lf *lf;
     282                 :            :         void *sa_dptr = NULL;
     283                 :            :         int ret;
     284                 :            : 
     285                 :          0 :         lf = &qp->lf;
     286                 :            : 
     287                 :            :         sa = &sess->sa;
     288                 :            : 
     289                 :            :         /* Trigger CTX flush to write dirty data back to DRAM */
     290                 :          0 :         roc_cpt_lf_ctx_flush(lf, &sa->in_sa, false);
     291                 :            : 
     292                 :            :         ret = -1;
     293                 :            : 
     294         [ #  # ]:          0 :         if (sess->ipsec.is_outbound) {
     295                 :          0 :                 sa_dptr = plt_zmalloc(sizeof(struct roc_ow_ipsec_outb_sa), 8);
     296         [ #  # ]:          0 :                 if (sa_dptr != NULL) {
     297                 :          0 :                         roc_ow_ipsec_outb_sa_init(sa_dptr);
     298                 :            : 
     299                 :          0 :                         ret = roc_cpt_ctx_write(lf, sa_dptr, &sa->out_sa,
     300                 :            :                                                 sizeof(struct roc_ow_ipsec_outb_sa));
     301                 :            :                 }
     302                 :            :         } else {
     303                 :          0 :                 sa_dptr = plt_zmalloc(sizeof(struct roc_ow_ipsec_inb_sa), 8);
     304         [ #  # ]:          0 :                 if (sa_dptr != NULL) {
     305                 :          0 :                         roc_ow_ipsec_inb_sa_init(sa_dptr);
     306                 :            : 
     307                 :          0 :                         ret = roc_cpt_ctx_write(lf, sa_dptr, &sa->in_sa,
     308                 :            :                                                 sizeof(struct roc_ow_ipsec_inb_sa));
     309                 :            :                 }
     310                 :            :         }
     311                 :            : 
     312                 :          0 :         plt_free(sa_dptr);
     313                 :            : 
     314         [ #  # ]:          0 :         if (ret) {
     315                 :            :                 /* MC write_ctx failed. Attempt reload of CTX */
     316                 :            : 
     317                 :            :                 /* Wait for 1 ms so that flush is complete */
     318                 :            :                 rte_delay_ms(1);
     319                 :            : 
     320                 :            :                 w2 = (union roc_ow_ipsec_sa_word2 *)&sa->in_sa.w2;
     321                 :          0 :                 w2->s.valid = 0;
     322                 :            : 
     323                 :            :                 rte_atomic_thread_fence(rte_memory_order_seq_cst);
     324                 :            : 
     325                 :            :                 /* Trigger CTX reload to fetch new data from DRAM */
     326                 :          0 :                 roc_cpt_lf_ctx_reload(lf, &sa->in_sa);
     327                 :            :         }
     328                 :            : 
     329                 :          0 :         return 0;
     330                 :            : }
     331                 :            : 
     332                 :            : int
     333                 :          0 : cn20k_ipsec_stats_get(struct cnxk_cpt_qp *qp, struct cn20k_sec_session *sess,
     334                 :            :                       struct rte_security_stats *stats)
     335                 :            : {
     336                 :            :         struct roc_ow_ipsec_outb_sa *out_sa;
     337                 :            :         struct roc_ow_ipsec_inb_sa *in_sa;
     338                 :            :         struct cn20k_ipsec_sa *sa;
     339                 :            : 
     340                 :          0 :         stats->protocol = RTE_SECURITY_PROTOCOL_IPSEC;
     341                 :            :         sa = &sess->sa;
     342                 :            : 
     343         [ #  # ]:          0 :         if (sess->ipsec.is_outbound) {
     344                 :          0 :                 out_sa = &sa->out_sa;
     345                 :          0 :                 roc_cpt_lf_ctx_flush(&qp->lf, out_sa, false);
     346                 :          0 :                 stats->ipsec.opackets = out_sa->ctx.mib_pkts;
     347                 :          0 :                 stats->ipsec.obytes = out_sa->ctx.mib_octs;
     348                 :            :         } else {
     349                 :          0 :                 in_sa = &sa->in_sa;
     350                 :          0 :                 roc_cpt_lf_ctx_flush(&qp->lf, in_sa, false);
     351                 :          0 :                 stats->ipsec.ipackets = in_sa->ctx.mib_pkts;
     352                 :          0 :                 stats->ipsec.ibytes = in_sa->ctx.mib_octs;
     353                 :            :         }
     354                 :            : 
     355                 :          0 :         return 0;
     356                 :            : }
     357                 :            : 
     358                 :            : int
     359                 :          0 : cn20k_ipsec_session_update(struct cnxk_cpt_vf *vf, struct cnxk_cpt_qp *qp,
     360                 :            :                            struct cn20k_sec_session *sess, struct rte_security_session_conf *conf)
     361                 :            : {
     362                 :            :         struct roc_cpt *roc_cpt;
     363                 :            :         int ret;
     364                 :            : 
     365         [ #  # ]:          0 :         if (conf->ipsec.direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
     366                 :            :                 return -ENOTSUP;
     367                 :            : 
     368                 :          0 :         ret = cnxk_ipsec_xform_verify(&conf->ipsec, conf->crypto_xform);
     369         [ #  # ]:          0 :         if (ret)
     370                 :            :                 return ret;
     371                 :            : 
     372                 :          0 :         roc_cpt = &vf->cpt;
     373                 :            : 
     374                 :          0 :         return cn20k_ipsec_outb_sa_create(roc_cpt, &qp->lf, &conf->ipsec, conf->crypto_xform,
     375                 :            :                                           (struct cn20k_sec_session *)sess);
     376                 :            : 
     377                 :            :         return 0;
     378                 :            : }

Generated by: LCOV version 1.14