LCOV - code coverage report
Current view: top level - lib/security - rte_security.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 93 185 50.3 %
Date: 2026-07-01 18:02:41 Functions: 14 26 53.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 105 212 49.5 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright 2017 NXP.
       3                 :            :  * Copyright(c) 2017 Intel Corporation.
       4                 :            :  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
       5                 :            :  */
       6                 :            : 
       7                 :            : #include <stdalign.h>
       8                 :            : #include <ctype.h>
       9                 :            : #include <stdlib.h>
      10                 :            : #include <errno.h>
      11                 :            : #include <limits.h>
      12                 :            : 
      13                 :            : #include <eal_export.h>
      14                 :            : #include <rte_cryptodev.h>
      15                 :            : #include <dev_driver.h>
      16                 :            : #include <rte_telemetry.h>
      17                 :            : #include "rte_security.h"
      18                 :            : #include "rte_security_driver.h"
      19                 :            : 
      20                 :            : /* Macro to check for invalid pointers */
      21                 :            : #define RTE_PTR_OR_ERR_RET(ptr, retval) do {    \
      22                 :            :         if ((ptr) == NULL)                      \
      23                 :            :                 return retval;                  \
      24                 :            : } while (0)
      25                 :            : 
      26                 :            : /* Macro to check for invalid pointers chains */
      27                 :            : #define RTE_PTR_CHAIN3_OR_ERR_RET(p1, p2, p3, retval, last_retval) do { \
      28                 :            :         RTE_PTR_OR_ERR_RET(p1, retval);                                 \
      29                 :            :         RTE_PTR_OR_ERR_RET(p1->p2, retval);                          \
      30                 :            :         RTE_PTR_OR_ERR_RET(p1->p2->p3, last_retval);                      \
      31                 :            : } while (0)
      32                 :            : 
      33                 :            : #define RTE_SECURITY_DYNFIELD_NAME "rte_security_dynfield_metadata"
      34                 :            : #define RTE_SECURITY_OOP_DYNFIELD_NAME "rte_security_oop_dynfield_metadata"
      35                 :            : 
      36                 :            : RTE_EXPORT_SYMBOL(rte_security_dynfield_offset)
      37                 :            : int rte_security_dynfield_offset = -1;
      38                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_security_oop_dynfield_offset, 23.11)
      39                 :            : int rte_security_oop_dynfield_offset = -1;
      40                 :            : 
      41                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_security_dynfield_register)
      42                 :            : int
      43                 :          0 : rte_security_dynfield_register(void)
      44                 :            : {
      45                 :            :         static const struct rte_mbuf_dynfield dynfield_desc = {
      46                 :            :                 .name = RTE_SECURITY_DYNFIELD_NAME,
      47                 :            :                 .size = sizeof(rte_security_dynfield_t),
      48                 :            :                 .align = alignof(rte_security_dynfield_t),
      49                 :            :         };
      50                 :          0 :         rte_security_dynfield_offset =
      51                 :          0 :                 rte_mbuf_dynfield_register(&dynfield_desc);
      52                 :          0 :         return rte_security_dynfield_offset;
      53                 :            : }
      54                 :            : 
      55                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_security_oop_dynfield_register)
      56                 :            : int
      57                 :          0 : rte_security_oop_dynfield_register(void)
      58                 :            : {
      59                 :            :         static const struct rte_mbuf_dynfield dynfield_desc = {
      60                 :            :                 .name = RTE_SECURITY_OOP_DYNFIELD_NAME,
      61                 :            :                 .size = sizeof(rte_security_oop_dynfield_t),
      62                 :            :                 .align = alignof(rte_security_oop_dynfield_t),
      63                 :            :         };
      64                 :            : 
      65                 :          0 :         rte_security_oop_dynfield_offset =
      66                 :          0 :                 rte_mbuf_dynfield_register(&dynfield_desc);
      67                 :          0 :         return rte_security_oop_dynfield_offset;
      68                 :            : }
      69                 :            : 
      70                 :            : RTE_EXPORT_SYMBOL(rte_security_session_create)
      71                 :            : void *
      72                 :         59 : rte_security_session_create(void *ctx,
      73                 :            :                             struct rte_security_session_conf *conf,
      74                 :            :                             struct rte_mempool *mp)
      75                 :            : {
      76                 :         59 :         struct rte_security_session *sess = NULL;
      77                 :            :         struct rte_security_ctx *instance = ctx;
      78                 :            :         uint32_t sess_priv_size;
      79                 :            : 
      80   [ +  +  +  +  :         59 :         RTE_PTR_CHAIN3_OR_ERR_RET(instance, ops, session_create, NULL, NULL);
                   +  + ]
      81         [ +  + ]:         56 :         RTE_PTR_OR_ERR_RET(conf, NULL);
      82         [ +  + ]:         55 :         RTE_PTR_OR_ERR_RET(mp, NULL);
      83                 :            : 
      84                 :         54 :         sess_priv_size = instance->ops->session_get_size(instance->device);
      85         [ +  - ]:         54 :         if (mp->elt_size < (sizeof(struct rte_security_session) + sess_priv_size))
      86                 :            :                 return NULL;
      87                 :            : 
      88         [ -  + ]:          1 :         if (rte_mempool_get(mp, (void **)&sess))
      89                 :            :                 return NULL;
      90                 :            : 
      91                 :            :         /* Clear session priv data */
      92                 :         53 :         memset(sess->driver_priv_data, 0, sess_priv_size);
      93                 :            : 
      94                 :         53 :         sess->driver_priv_data_iova = rte_mempool_virt2iova(sess) +
      95                 :            :                         offsetof(struct rte_security_session, driver_priv_data);
      96         [ +  + ]:         53 :         if (instance->ops->session_create(instance->device, conf, sess)) {
      97         [ -  + ]:          1 :                 rte_mempool_put(mp, (void *)sess);
      98                 :          1 :                 return NULL;
      99                 :            :         }
     100                 :         52 :         instance->sess_cnt++;
     101                 :            : 
     102                 :         52 :         return (void *)sess;
     103                 :            : }
     104                 :            : 
     105                 :            : RTE_EXPORT_SYMBOL(rte_security_session_update)
     106                 :            : int
     107                 :          7 : rte_security_session_update(void *ctx, void *sess, struct rte_security_session_conf *conf)
     108                 :            : {
     109                 :            :         struct rte_security_ctx *instance = ctx;
     110                 :            : 
     111   [ +  +  +  +  :          7 :         RTE_PTR_CHAIN3_OR_ERR_RET(instance, ops, session_update, -EINVAL,
                   +  + ]
     112                 :            :                         -ENOTSUP);
     113         [ +  + ]:          4 :         RTE_PTR_OR_ERR_RET(sess, -EINVAL);
     114         [ +  + ]:          3 :         RTE_PTR_OR_ERR_RET(conf, -EINVAL);
     115                 :            : 
     116                 :          2 :         return instance->ops->session_update(instance->device, sess, conf);
     117                 :            : }
     118                 :            : 
     119                 :            : RTE_EXPORT_SYMBOL(rte_security_session_get_size)
     120                 :            : unsigned int
     121                 :          6 : rte_security_session_get_size(void *ctx)
     122                 :            : {
     123                 :            :         struct rte_security_ctx *instance = ctx;
     124                 :            : 
     125   [ +  +  +  +  :          6 :         RTE_PTR_CHAIN3_OR_ERR_RET(instance, ops, session_get_size, 0, 0);
                   +  + ]
     126                 :            : 
     127                 :          2 :         return (sizeof(struct rte_security_session) +
     128                 :          2 :                         instance->ops->session_get_size(instance->device));
     129                 :            : }
     130                 :            : 
     131                 :            : RTE_EXPORT_SYMBOL(rte_security_session_stats_get)
     132                 :            : int
     133                 :          6 : rte_security_session_stats_get(void *ctx, void *sess, struct rte_security_stats *stats)
     134                 :            : {
     135                 :            :         struct rte_security_ctx *instance = ctx;
     136                 :            : 
     137   [ +  +  +  +  :          6 :         RTE_PTR_CHAIN3_OR_ERR_RET(instance, ops, session_stats_get, -EINVAL,
                   +  + ]
     138                 :            :                         -ENOTSUP);
     139                 :            :         /* Parameter sess can be NULL in case of getting global statistics. */
     140         [ +  + ]:          3 :         RTE_PTR_OR_ERR_RET(stats, -EINVAL);
     141                 :            : 
     142                 :          2 :         return instance->ops->session_stats_get(instance->device, sess, stats);
     143                 :            : }
     144                 :            : 
     145                 :            : RTE_EXPORT_SYMBOL(rte_security_session_destroy)
     146                 :            : int
     147                 :         57 : rte_security_session_destroy(void *ctx, void *sess)
     148                 :            : {
     149                 :            :         struct rte_security_ctx *instance = ctx;
     150                 :            :         int ret;
     151                 :            : 
     152   [ +  +  +  +  :         57 :         RTE_PTR_CHAIN3_OR_ERR_RET(instance, ops, session_destroy, -EINVAL,
                   +  + ]
     153                 :            :                         -ENOTSUP);
     154         [ +  + ]:         54 :         RTE_PTR_OR_ERR_RET(sess, -EINVAL);
     155                 :            : 
     156                 :         53 :         ret = instance->ops->session_destroy(instance->device, sess);
     157         [ +  + ]:         53 :         if (ret != 0)
     158                 :            :                 return ret;
     159                 :            : 
     160                 :         52 :         rte_mempool_put(rte_mempool_from_obj(sess), (void *)sess);
     161                 :            : 
     162         [ +  - ]:         52 :         if (instance->sess_cnt)
     163                 :         52 :                 instance->sess_cnt--;
     164                 :            : 
     165                 :            :         return 0;
     166                 :            : }
     167                 :            : 
     168                 :            : RTE_EXPORT_SYMBOL(rte_security_macsec_sc_create)
     169                 :            : int
     170                 :          0 : rte_security_macsec_sc_create(void *ctx, struct rte_security_macsec_sc *conf)
     171                 :            : {
     172                 :            :         struct rte_security_ctx *instance = ctx;
     173                 :            :         int sc_id;
     174                 :            : 
     175   [ #  #  #  #  :          0 :         RTE_PTR_CHAIN3_OR_ERR_RET(instance, ops, macsec_sc_create, -EINVAL, -ENOTSUP);
                   #  # ]
     176         [ #  # ]:          0 :         RTE_PTR_OR_ERR_RET(conf, -EINVAL);
     177                 :            : 
     178                 :          0 :         sc_id = instance->ops->macsec_sc_create(instance->device, conf);
     179         [ #  # ]:          0 :         if (sc_id >= 0)
     180                 :          0 :                 instance->macsec_sc_cnt++;
     181                 :            : 
     182                 :            :         return sc_id;
     183                 :            : }
     184                 :            : 
     185                 :            : RTE_EXPORT_SYMBOL(rte_security_macsec_sa_create)
     186                 :            : int
     187                 :          0 : rte_security_macsec_sa_create(void *ctx, struct rte_security_macsec_sa *conf)
     188                 :            : {
     189                 :            :         struct rte_security_ctx *instance = ctx;
     190                 :            :         int sa_id;
     191                 :            : 
     192   [ #  #  #  #  :          0 :         RTE_PTR_CHAIN3_OR_ERR_RET(instance, ops, macsec_sa_create, -EINVAL, -ENOTSUP);
                   #  # ]
     193         [ #  # ]:          0 :         RTE_PTR_OR_ERR_RET(conf, -EINVAL);
     194                 :            : 
     195                 :          0 :         sa_id = instance->ops->macsec_sa_create(instance->device, conf);
     196         [ #  # ]:          0 :         if (sa_id >= 0)
     197                 :          0 :                 instance->macsec_sa_cnt++;
     198                 :            : 
     199                 :            :         return sa_id;
     200                 :            : }
     201                 :            : 
     202                 :            : RTE_EXPORT_SYMBOL(rte_security_macsec_sc_destroy)
     203                 :            : int
     204                 :          0 : rte_security_macsec_sc_destroy(void *ctx, uint16_t sc_id,
     205                 :            :                                enum rte_security_macsec_direction dir)
     206                 :            : {
     207                 :            :         struct rte_security_ctx *instance = ctx;
     208                 :            :         int ret;
     209                 :            : 
     210   [ #  #  #  #  :          0 :         RTE_PTR_CHAIN3_OR_ERR_RET(instance, ops, macsec_sc_destroy, -EINVAL, -ENOTSUP);
                   #  # ]
     211                 :            : 
     212                 :          0 :         ret = instance->ops->macsec_sc_destroy(instance->device, sc_id, dir);
     213         [ #  # ]:          0 :         if (ret != 0)
     214                 :            :                 return ret;
     215                 :            : 
     216         [ #  # ]:          0 :         if (instance->macsec_sc_cnt)
     217                 :          0 :                 instance->macsec_sc_cnt--;
     218                 :            : 
     219                 :            :         return 0;
     220                 :            : }
     221                 :            : 
     222                 :            : RTE_EXPORT_SYMBOL(rte_security_macsec_sa_destroy)
     223                 :            : int
     224                 :          0 : rte_security_macsec_sa_destroy(void *ctx, uint16_t sa_id,
     225                 :            :                                enum rte_security_macsec_direction dir)
     226                 :            : {
     227                 :            :         struct rte_security_ctx *instance = ctx;
     228                 :            :         int ret;
     229                 :            : 
     230   [ #  #  #  #  :          0 :         RTE_PTR_CHAIN3_OR_ERR_RET(instance, ops, macsec_sa_destroy, -EINVAL, -ENOTSUP);
                   #  # ]
     231                 :            : 
     232                 :          0 :         ret = instance->ops->macsec_sa_destroy(instance->device, sa_id, dir);
     233         [ #  # ]:          0 :         if (ret != 0)
     234                 :            :                 return ret;
     235                 :            : 
     236         [ #  # ]:          0 :         if (instance->macsec_sa_cnt)
     237                 :          0 :                 instance->macsec_sa_cnt--;
     238                 :            : 
     239                 :            :         return 0;
     240                 :            : }
     241                 :            : 
     242                 :            : RTE_EXPORT_SYMBOL(rte_security_macsec_sc_stats_get)
     243                 :            : int
     244                 :          0 : rte_security_macsec_sc_stats_get(void *ctx, uint16_t sc_id,
     245                 :            :                                  enum rte_security_macsec_direction dir,
     246                 :            :                                  struct rte_security_macsec_sc_stats *stats)
     247                 :            : {
     248                 :            :         struct rte_security_ctx *instance = ctx;
     249                 :            : 
     250   [ #  #  #  #  :          0 :         RTE_PTR_CHAIN3_OR_ERR_RET(instance, ops, macsec_sc_stats_get, -EINVAL, -ENOTSUP);
                   #  # ]
     251         [ #  # ]:          0 :         RTE_PTR_OR_ERR_RET(stats, -EINVAL);
     252                 :            : 
     253                 :          0 :         return instance->ops->macsec_sc_stats_get(instance->device, sc_id, dir, stats);
     254                 :            : }
     255                 :            : 
     256                 :            : RTE_EXPORT_SYMBOL(rte_security_macsec_sa_stats_get)
     257                 :            : int
     258                 :          0 : rte_security_macsec_sa_stats_get(void *ctx, uint16_t sa_id,
     259                 :            :                                  enum rte_security_macsec_direction dir,
     260                 :            :                                  struct rte_security_macsec_sa_stats *stats)
     261                 :            : {
     262                 :            :         struct rte_security_ctx *instance = ctx;
     263                 :            : 
     264   [ #  #  #  #  :          0 :         RTE_PTR_CHAIN3_OR_ERR_RET(instance, ops, macsec_sa_stats_get, -EINVAL, -ENOTSUP);
                   #  # ]
     265         [ #  # ]:          0 :         RTE_PTR_OR_ERR_RET(stats, -EINVAL);
     266                 :            : 
     267                 :          0 :         return instance->ops->macsec_sa_stats_get(instance->device, sa_id, dir, stats);
     268                 :            : }
     269                 :            : 
     270                 :            : RTE_EXPORT_SYMBOL(__rte_security_set_pkt_metadata)
     271                 :            : int
     272                 :          3 : __rte_security_set_pkt_metadata(void *ctx, void *sess, struct rte_mbuf *m, void *params)
     273                 :            : {
     274                 :            :         struct rte_security_ctx *instance = ctx;
     275                 :            : #ifdef RTE_DEBUG
     276                 :            :         RTE_PTR_OR_ERR_RET(sess, -EINVAL);
     277                 :            :         RTE_PTR_OR_ERR_RET(instance, -EINVAL);
     278                 :            :         RTE_PTR_OR_ERR_RET(instance->ops, -EINVAL);
     279                 :            : #endif
     280         [ +  + ]:          3 :         if (instance->ops->set_pkt_metadata == NULL)
     281                 :            :                 return -ENOTSUP;
     282                 :          2 :         return instance->ops->set_pkt_metadata(instance->device, sess, m, params);
     283                 :            : }
     284                 :            : 
     285                 :            : RTE_EXPORT_SYMBOL(rte_security_capabilities_get)
     286                 :            : const struct rte_security_capability *
     287                 :          5 : rte_security_capabilities_get(void *ctx)
     288                 :            : {
     289                 :            :         struct rte_security_ctx *instance = ctx;
     290                 :            : 
     291   [ +  +  +  +  :          5 :         RTE_PTR_CHAIN3_OR_ERR_RET(instance, ops, capabilities_get, NULL, NULL);
                   +  + ]
     292                 :            : 
     293                 :          2 :         return instance->ops->capabilities_get(instance->device);
     294                 :            : }
     295                 :            : 
     296                 :            : RTE_EXPORT_SYMBOL(rte_security_capability_get)
     297                 :            : const struct rte_security_capability *
     298                 :         16 : rte_security_capability_get(void *ctx, struct rte_security_capability_idx *idx)
     299                 :            : {
     300                 :            :         const struct rte_security_capability *capabilities;
     301                 :            :         const struct rte_security_capability *capability;
     302                 :            :         struct rte_security_ctx *instance = ctx;
     303                 :            :         uint16_t i = 0;
     304                 :            : 
     305   [ +  +  +  +  :         16 :         RTE_PTR_CHAIN3_OR_ERR_RET(instance, ops, capabilities_get, NULL, NULL);
                   +  + ]
     306         [ +  + ]:         13 :         RTE_PTR_OR_ERR_RET(idx, NULL);
     307                 :            : 
     308                 :         12 :         capabilities = instance->ops->capabilities_get(instance->device);
     309                 :            : 
     310         [ +  + ]:         12 :         if (capabilities == NULL)
     311                 :            :                 return NULL;
     312                 :            : 
     313                 :         23 :         while ((capability = &capabilities[i++])->action
     314         [ +  + ]:         23 :                         != RTE_SECURITY_ACTION_TYPE_NONE) {
     315         [ +  + ]:         15 :                 if (capability->action == idx->action &&
     316         [ +  + ]:         10 :                                 capability->protocol == idx->protocol) {
     317                 :            :                         if (idx->protocol == RTE_SECURITY_PROTOCOL_IPSEC) {
     318                 :          4 :                                 if (capability->ipsec.proto ==
     319         [ +  + ]:          4 :                                                 idx->ipsec.proto &&
     320                 :          3 :                                         capability->ipsec.mode ==
     321         [ +  + ]:          3 :                                                         idx->ipsec.mode &&
     322                 :          2 :                                         capability->ipsec.direction ==
     323         [ +  + ]:          2 :                                                         idx->ipsec.direction)
     324                 :          1 :                                         return capability;
     325                 :            :                         } else if (idx->protocol == RTE_SECURITY_PROTOCOL_PDCP) {
     326                 :          2 :                                 if (capability->pdcp.domain ==
     327         [ +  + ]:          2 :                                                         idx->pdcp.domain)
     328                 :          1 :                                         return capability;
     329                 :            :                         } else if (idx->protocol ==
     330                 :            :                                                 RTE_SECURITY_PROTOCOL_DOCSIS) {
     331                 :          2 :                                 if (capability->docsis.direction ==
     332         [ +  + ]:          2 :                                                         idx->docsis.direction)
     333                 :          1 :                                         return capability;
     334                 :            :                         } else if (idx->protocol ==
     335                 :            :                                                 RTE_SECURITY_PROTOCOL_MACSEC) {
     336         [ #  # ]:          0 :                                 if (idx->macsec.alg == capability->macsec.alg)
     337                 :          0 :                                         return capability;
     338                 :            :                         } else if (idx->protocol == RTE_SECURITY_PROTOCOL_TLS_RECORD) {
     339         [ #  # ]:          0 :                                 if (capability->tls_record.ver == idx->tls_record.ver &&
     340         [ #  # ]:          0 :                                     capability->tls_record.type == idx->tls_record.type)
     341                 :          0 :                                         return capability;
     342                 :            :                         }
     343                 :            :                 }
     344                 :            :         }
     345                 :            : 
     346                 :            :         return NULL;
     347                 :            : }
     348                 :            : 
     349                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_security_rx_inject_configure, 23.11)
     350                 :            : int
     351                 :          0 : rte_security_rx_inject_configure(void *ctx, uint16_t port_id, bool enable)
     352                 :            : {
     353                 :            :         struct rte_security_ctx *instance = ctx;
     354                 :            : 
     355         [ #  # ]:          0 :         RTE_PTR_OR_ERR_RET(instance, -EINVAL);
     356         [ #  # ]:          0 :         RTE_PTR_OR_ERR_RET(instance->ops, -ENOTSUP);
     357         [ #  # ]:          0 :         RTE_PTR_OR_ERR_RET(instance->ops->rx_inject_configure, -ENOTSUP);
     358                 :            : 
     359                 :          0 :         return instance->ops->rx_inject_configure(instance->device, port_id, enable);
     360                 :            : }
     361                 :            : 
     362                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_security_inb_pkt_rx_inject, 23.11)
     363                 :            : uint16_t
     364                 :          0 : rte_security_inb_pkt_rx_inject(void *ctx, struct rte_mbuf **pkts, void **sess,
     365                 :            :                                uint16_t nb_pkts)
     366                 :            : {
     367                 :            :         struct rte_security_ctx *instance = ctx;
     368                 :            : 
     369                 :          0 :         return instance->ops->inb_pkt_rx_inject(instance->device, pkts,
     370                 :            :                                                 (struct rte_security_session **)sess, nb_pkts);
     371                 :            : }
     372                 :            : 
     373                 :            : static int
     374                 :          3 : security_handle_cryptodev_list(const char *cmd __rte_unused,
     375                 :            :                                const char *params __rte_unused,
     376                 :            :                                struct rte_tel_data *d)
     377                 :            : {
     378                 :            :         int dev_id;
     379                 :            : 
     380         [ +  - ]:          3 :         if (rte_cryptodev_count() < 1)
     381                 :            :                 return -1;
     382                 :            : 
     383                 :          3 :         rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
     384         [ +  + ]:        195 :         for (dev_id = 0; dev_id < RTE_CRYPTO_MAX_DEVS; dev_id++)
     385   [ +  +  -  + ]:        195 :                 if (rte_cryptodev_is_valid_dev(dev_id) &&
     386                 :          3 :                     rte_cryptodev_get_sec_ctx(dev_id))
     387                 :          0 :                         rte_tel_data_add_array_int(d, dev_id);
     388                 :            : 
     389                 :            :         return 0;
     390                 :            : }
     391                 :            : 
     392                 :            : #define CRYPTO_CAPS_SZ                                             \
     393                 :            :         (RTE_ALIGN_CEIL(sizeof(struct rte_cryptodev_capabilities), \
     394                 :            :                         sizeof(uint64_t)) /     sizeof(uint64_t))
     395                 :            : 
     396                 :            : static int
     397                 :          0 : crypto_caps_array(struct rte_tel_data *d,
     398                 :            :                   const struct rte_cryptodev_capabilities *capabilities)
     399                 :            : {
     400                 :            :         const struct rte_cryptodev_capabilities *dev_caps;
     401                 :            :         uint64_t caps_val[CRYPTO_CAPS_SZ];
     402                 :            :         unsigned int i = 0, j;
     403                 :            : 
     404                 :          0 :         rte_tel_data_start_array(d, RTE_TEL_UINT_VAL);
     405                 :            : 
     406         [ #  # ]:          0 :         while ((dev_caps = &capabilities[i++])->op !=
     407                 :            :            RTE_CRYPTO_OP_TYPE_UNDEFINED) {
     408                 :            :                 memset(&caps_val, 0, CRYPTO_CAPS_SZ * sizeof(caps_val[0]));
     409                 :            :                 rte_memcpy(caps_val, dev_caps, sizeof(capabilities[0]));
     410         [ #  # ]:          0 :                 for (j = 0; j < CRYPTO_CAPS_SZ; j++)
     411                 :          0 :                         rte_tel_data_add_array_uint(d, caps_val[j]);
     412                 :            :         }
     413                 :            : 
     414                 :          0 :         return (i - 1);
     415                 :            : }
     416                 :            : 
     417                 :            : #define SEC_CAPS_SZ                                             \
     418                 :            :         (RTE_ALIGN_CEIL(sizeof(struct rte_security_capability), \
     419                 :            :                         sizeof(uint64_t)) /     sizeof(uint64_t))
     420                 :            : 
     421                 :            : static int
     422                 :          0 : sec_caps_array(struct rte_tel_data *d,
     423                 :            :                const struct rte_security_capability *capabilities)
     424                 :            : {
     425                 :            :         const struct rte_security_capability *dev_caps;
     426                 :            :         uint64_t caps_val[SEC_CAPS_SZ];
     427                 :            :         unsigned int i = 0, j;
     428                 :            : 
     429                 :          0 :         rte_tel_data_start_array(d, RTE_TEL_UINT_VAL);
     430                 :            : 
     431         [ #  # ]:          0 :         while ((dev_caps = &capabilities[i++])->action !=
     432                 :            :            RTE_SECURITY_ACTION_TYPE_NONE) {
     433                 :            :                 memset(&caps_val, 0, SEC_CAPS_SZ * sizeof(caps_val[0]));
     434                 :            :                 rte_memcpy(caps_val, dev_caps, sizeof(capabilities[0]));
     435         [ #  # ]:          0 :                 for (j = 0; j < SEC_CAPS_SZ; j++)
     436                 :          0 :                         rte_tel_data_add_array_uint(d, caps_val[j]);
     437                 :            :         }
     438                 :            : 
     439                 :          0 :         return i - 1;
     440                 :            : }
     441                 :            : 
     442                 :            : static const struct rte_security_capability *
     443                 :            : security_capability_by_index(const struct rte_security_capability *capabilities,
     444                 :            :                              int index)
     445                 :            : {
     446                 :            :         const struct rte_security_capability *dev_caps = NULL;
     447                 :            :         int i = 0;
     448                 :            : 
     449         [ #  # ]:          0 :         while ((dev_caps = &capabilities[i])->action !=
     450                 :            :            RTE_SECURITY_ACTION_TYPE_NONE) {
     451         [ #  # ]:          0 :                 if (i == index)
     452                 :            :                         return dev_caps;
     453                 :            : 
     454                 :          0 :                 ++i;
     455                 :            :         }
     456                 :            : 
     457                 :            :         return NULL;
     458                 :            : }
     459                 :            : 
     460                 :            : static int
     461                 :          1 : security_capabilities_from_dev_id(int dev_id, const void **caps)
     462                 :            : {
     463                 :            :         const struct rte_security_capability *capabilities;
     464                 :            :         void *sec_ctx;
     465                 :            : 
     466         [ +  - ]:          1 :         if (rte_cryptodev_is_valid_dev(dev_id) == 0)
     467                 :            :                 return -EINVAL;
     468                 :            : 
     469                 :          1 :         sec_ctx = rte_cryptodev_get_sec_ctx(dev_id);
     470         [ -  + ]:          1 :         RTE_PTR_OR_ERR_RET(sec_ctx, -EINVAL);
     471                 :            : 
     472                 :          0 :         capabilities = rte_security_capabilities_get(sec_ctx);
     473         [ #  # ]:          0 :         RTE_PTR_OR_ERR_RET(capabilities, -EINVAL);
     474                 :            : 
     475                 :          0 :         *caps = capabilities;
     476                 :          0 :         return 0;
     477                 :            : }
     478                 :            : 
     479                 :            : /* Parse an unsigned integer parameter, returning the value or -EINVAL.
     480                 :            :  * 'max' must be <= INT_MAX.
     481                 :            :  */
     482                 :            : static int
     483                 :          6 : telemetry_parse_uint(const char *str, char **end, unsigned long max)
     484                 :            : {
     485                 :            :         unsigned long val;
     486                 :            : 
     487   [ +  +  +  + ]:          6 :         if (str == NULL || !isdigit((unsigned char)*str))
     488                 :            :                 return -EINVAL;
     489                 :            : 
     490                 :          2 :         errno = 0;
     491                 :          2 :         val = strtoul(str, end, 0);
     492   [ +  -  +  - ]:          2 :         if (errno != 0 || val > max)
     493                 :            :                 return -EINVAL;
     494                 :            : 
     495                 :          2 :         return (int)val;
     496                 :            : }
     497                 :            : 
     498                 :            : static int
     499                 :          3 : security_handle_cryptodev_sec_caps(const char *cmd __rte_unused, const char *params,
     500                 :            :                                    struct rte_tel_data *d)
     501                 :            : {
     502                 :            :         const struct rte_security_capability *capabilities;
     503                 :            :         struct rte_tel_data *sec_caps;
     504                 :            :         char *end_param;
     505                 :            :         int sec_caps_n;
     506                 :            :         int dev_id;
     507                 :            :         int rc;
     508                 :            : 
     509                 :          3 :         dev_id = telemetry_parse_uint(params, &end_param, RTE_CRYPTO_MAX_DEVS - 1);
     510   [ +  +  +  - ]:          3 :         if (dev_id < 0 || *end_param != '\0')
     511                 :            :                 return -EINVAL;
     512                 :            : 
     513                 :          1 :         rc = security_capabilities_from_dev_id(dev_id, (void *)&capabilities);
     514         [ -  + ]:          1 :         if (rc < 0)
     515                 :            :                 return rc;
     516                 :            : 
     517                 :          0 :         sec_caps = rte_tel_data_alloc();
     518         [ #  # ]:          0 :         RTE_PTR_OR_ERR_RET(sec_caps, -ENOMEM);
     519                 :            : 
     520                 :          0 :         rte_tel_data_start_dict(d);
     521                 :          0 :         sec_caps_n = sec_caps_array(sec_caps, capabilities);
     522                 :          0 :         rte_tel_data_add_dict_container(d, "sec_caps", sec_caps, 0);
     523                 :          0 :         rte_tel_data_add_dict_int(d, "sec_caps_n", sec_caps_n);
     524                 :            : 
     525                 :          0 :         return 0;
     526                 :            : }
     527                 :            : 
     528                 :            : static int
     529                 :          3 : security_handle_cryptodev_crypto_caps(const char *cmd __rte_unused, const char *params,
     530                 :            :                                       struct rte_tel_data *d)
     531                 :            : {
     532                 :            :         const struct rte_security_capability *capabilities;
     533                 :            :         struct rte_tel_data *crypto_caps;
     534                 :            :         int dev_id, capa_id;
     535                 :            :         int crypto_caps_n;
     536                 :            :         char *end_param;
     537                 :            :         int rc;
     538                 :            : 
     539                 :          3 :         dev_id = telemetry_parse_uint(params, &end_param, RTE_CRYPTO_MAX_DEVS - 1);
     540   [ +  +  -  + ]:          3 :         if (dev_id < 0 || *end_param != ',')
     541                 :            :                 return -EINVAL;
     542                 :            : 
     543                 :          0 :         capa_id = telemetry_parse_uint(end_param + 1, &end_param, INT_MAX);
     544   [ #  #  #  # ]:          0 :         if (capa_id < 0 || *end_param != '\0')
     545                 :            :                 return -EINVAL;
     546                 :            : 
     547                 :          0 :         rc = security_capabilities_from_dev_id(dev_id, (void *)&capabilities);
     548         [ #  # ]:          0 :         if (rc < 0)
     549                 :            :                 return rc;
     550                 :            : 
     551                 :          0 :         capabilities = security_capability_by_index(capabilities, capa_id);
     552         [ #  # ]:          0 :         RTE_PTR_OR_ERR_RET(capabilities, -EINVAL);
     553                 :            : 
     554                 :          0 :         crypto_caps = rte_tel_data_alloc();
     555         [ #  # ]:          0 :         RTE_PTR_OR_ERR_RET(crypto_caps, -ENOMEM);
     556                 :            : 
     557                 :          0 :         rte_tel_data_start_dict(d);
     558                 :          0 :         crypto_caps_n = crypto_caps_array(crypto_caps, capabilities->crypto_capabilities);
     559                 :            : 
     560                 :          0 :         rte_tel_data_add_dict_container(d, "crypto_caps", crypto_caps, 0);
     561                 :          0 :         rte_tel_data_add_dict_int(d, "crypto_caps_n", crypto_caps_n);
     562                 :            : 
     563                 :          0 :         return 0;
     564                 :            : }
     565                 :            : 
     566                 :        301 : RTE_INIT(security_init_telemetry)
     567                 :            : {
     568                 :        301 :         rte_telemetry_register_cmd("/security/cryptodev/list",
     569                 :            :                 security_handle_cryptodev_list,
     570                 :            :                 "Returns list of available crypto devices by IDs. No parameters.");
     571                 :            : 
     572                 :        301 :         rte_telemetry_register_cmd("/security/cryptodev/sec_caps",
     573                 :            :                 security_handle_cryptodev_sec_caps,
     574                 :            :                 "Returns security capabilities for a cryptodev. Parameters: int dev_id");
     575                 :            : 
     576                 :        301 :         rte_telemetry_register_cmd("/security/cryptodev/crypto_caps",
     577                 :            :                 security_handle_cryptodev_crypto_caps,
     578                 :            :                 "Returns crypto capabilities for a security capability. Parameters: int dev_id, sec_cap_id");
     579                 :        301 : }

Generated by: LCOV version 1.14