LCOV - code coverage report
Current view: top level - drivers/regex/mlx5 - mlx5_rxp.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 139 0.0 %
Date: 2025-12-01 19:08:10 Functions: 0 8 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 75 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright 2020 Mellanox Technologies, Ltd
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <rte_log.h>
       6                 :            : #include <rte_errno.h>
       7                 :            : #include <rte_malloc.h>
       8                 :            : #include <rte_regexdev.h>
       9                 :            : #include <rte_regexdev_core.h>
      10                 :            : #include <rte_regexdev_driver.h>
      11                 :            : #include <sys/mman.h>
      12                 :            : 
      13                 :            : #include <mlx5_glue.h>
      14                 :            : #include <mlx5_devx_cmds.h>
      15                 :            : #include <mlx5_prm.h>
      16                 :            : #include <mlx5_common_os.h>
      17                 :            : 
      18                 :            : #include "mlx5_regex.h"
      19                 :            : #include "mlx5_regex_utils.h"
      20                 :            : #include "mlx5_rxp.h"
      21                 :            : 
      22                 :            : #define MLX5_REGEX_MAX_MATCHES MLX5_RXP_MAX_MATCHES
      23                 :            : #define MLX5_REGEX_MAX_PAYLOAD_SIZE MLX5_RXP_MAX_JOB_LENGTH
      24                 :            : #define MLX5_REGEX_MAX_RULES_PER_GROUP UINT32_MAX
      25                 :            : #define MLX5_REGEX_MAX_GROUPS MLX5_RXP_MAX_SUBSETS
      26                 :            : 
      27                 :            : const uint64_t combined_rof_tag = 0xff52544424a52475;
      28                 :            : 
      29                 :            : /* Private Declarations */
      30                 :            : static int
      31                 :            : rxp_create_mkey(struct mlx5_regex_priv *priv, void *ptr, size_t size,
      32                 :            :         uint32_t access, struct mlx5_regex_mkey *mkey);
      33                 :            : static inline void
      34                 :            : rxp_destroy_mkey(struct mlx5_regex_mkey *mkey);
      35                 :            : 
      36                 :            : int
      37                 :          0 : mlx5_regex_info_get(struct rte_regexdev *dev __rte_unused,
      38                 :            :                     struct rte_regexdev_info *info)
      39                 :            : {
      40                 :          0 :         info->max_matches = MLX5_REGEX_MAX_MATCHES;
      41                 :          0 :         info->max_payload_size = MLX5_REGEX_MAX_PAYLOAD_SIZE;
      42                 :          0 :         info->max_rules_per_group = MLX5_REGEX_MAX_RULES_PER_GROUP;
      43                 :          0 :         info->max_groups = MLX5_REGEX_MAX_GROUPS;
      44                 :          0 :         info->regexdev_capa = RTE_REGEXDEV_SUPP_PCRE_GREEDY_F |
      45                 :            :                               RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F;
      46                 :          0 :         info->rule_flags = 0;
      47                 :          0 :         info->max_queue_pairs = UINT16_MAX;
      48                 :          0 :         info->max_segs = mlx5_regexdev_max_segs_get();
      49                 :          0 :         return 0;
      50                 :            : }
      51                 :            : 
      52                 :            : static int
      53                 :          0 : rxp_create_mkey(struct mlx5_regex_priv *priv, void *ptr, size_t size,
      54                 :            :         uint32_t access, struct mlx5_regex_mkey *mkey)
      55                 :            : {
      56                 :            :         struct mlx5_devx_mkey_attr mkey_attr;
      57                 :            : 
      58                 :            :         /* Register the memory. */
      59                 :          0 :         mkey->umem = mlx5_glue->devx_umem_reg(priv->cdev->ctx, ptr, size, access);
      60         [ #  # ]:          0 :         if (!mkey->umem) {
      61                 :          0 :                 DRV_LOG(ERR, "Failed to register memory!");
      62                 :          0 :                 return -ENODEV;
      63                 :            :         }
      64                 :            :         /* Create mkey */
      65                 :          0 :         mkey_attr = (struct mlx5_devx_mkey_attr) {
      66                 :          0 :                 .addr = (uintptr_t)ptr,
      67                 :            :                 .size = (uint32_t)size,
      68                 :            :                 .umem_id = mlx5_os_get_umem_id(mkey->umem),
      69                 :            :                 .pg_access = 1,
      70                 :            :                 .umr_en = 0,
      71                 :            :         };
      72                 :            : #ifdef HAVE_IBV_FLOW_DV_SUPPORT
      73                 :          0 :         mkey_attr.pd = priv->cdev->pdn;
      74                 :            : #endif
      75                 :          0 :         mkey->mkey = mlx5_devx_cmd_mkey_create(priv->cdev->ctx, &mkey_attr);
      76         [ #  # ]:          0 :         if (!mkey->mkey) {
      77                 :          0 :                 DRV_LOG(ERR, "Failed to create direct mkey!");
      78                 :          0 :                 return -ENODEV;
      79                 :            :         }
      80                 :            :         return 0;
      81                 :            : }
      82                 :            : 
      83                 :            : static inline void
      84                 :          0 : rxp_destroy_mkey(struct mlx5_regex_mkey *mkey)
      85                 :            : {
      86         [ #  # ]:          0 :         if (mkey->mkey)
      87                 :          0 :                 claim_zero(mlx5_devx_cmd_destroy(mkey->mkey));
      88         [ #  # ]:          0 :         if (mkey->umem)
      89                 :          0 :                 claim_zero(mlx5_glue->devx_umem_dereg(mkey->umem));
      90                 :          0 : }
      91                 :            : 
      92                 :            : int
      93                 :          0 : mlx5_regex_get_rxp_vers(uint32_t regexp_version, uint32_t *target_rxp_vers)
      94                 :            : {
      95                 :            :         int ret = 0;
      96      [ #  #  # ]:          0 :         switch (regexp_version) {
      97                 :          0 :         case MLX5_RXP_BF2_IDENTIFIER:
      98                 :          0 :                 *target_rxp_vers = MLX5_RXP_BF2_ROF_VERSION_STRING;
      99                 :          0 :                 break;
     100                 :          0 :         case MLX5_RXP_BF3_IDENTIFIER:
     101                 :          0 :                 *target_rxp_vers = MLX5_RXP_BF3_ROF_VERSION_STRING;
     102                 :          0 :                 break;
     103                 :          0 :         default:
     104                 :          0 :                 DRV_LOG(ERR, "Unsupported rxp version: %u", regexp_version);
     105                 :            :                 ret = -EINVAL;
     106                 :          0 :                 break;
     107                 :            :         }
     108                 :          0 :         return ret;
     109                 :            : }
     110                 :            : 
     111                 :            : int
     112                 :          0 : mlx5_regex_check_rof_version(uint32_t combined_rof_vers)
     113                 :            : {
     114                 :            :         int ret = 0;
     115                 :            :         /* Check if combined rof version is supported */
     116         [ #  # ]:          0 :         switch (combined_rof_vers) {
     117                 :            :         case 1:
     118                 :            :                 break;
     119                 :          0 :         default:
     120                 :          0 :                 DRV_LOG(ERR, "Unsupported combined rof version: %u",
     121                 :            :                 combined_rof_vers);
     122                 :            :                 ret = -EINVAL;
     123                 :          0 :                 break;
     124                 :            :         }
     125                 :          0 :         return ret;
     126                 :            : }
     127                 :            : 
     128                 :            : int
     129                 :          0 : mlx5_regex_parse_rules_db(struct mlx5_regex_priv *priv,
     130                 :            :                         const char **rules_db, uint32_t *rules_db_len)
     131                 :            : {
     132                 :            :         int i = 0;
     133                 :            :         uint32_t j = 0;
     134                 :            :         int ret = 0;
     135                 :            :         bool combined_rof = true;
     136                 :          0 :         const char *rof_ptr = *rules_db;
     137                 :            :         uint32_t combined_rof_vers = 0;
     138                 :            :         uint32_t num_rof_blocks = 0;
     139                 :            :         uint32_t rxpc_vers = 0;
     140                 :          0 :         uint32_t target_rxp_vers = 0;
     141                 :            :         uint32_t byte_count = 0;
     142                 :            :         uint32_t rof_bytes_read = 0;
     143                 :            :         bool rof_binary_found = false;
     144                 :          0 :         struct mlx5_hca_attr *attr = &priv->cdev->config.hca_attr;
     145                 :            : 
     146                 :            :         /* Need minimum of 8 bytes to process single or combined rof */
     147         [ #  # ]:          0 :         if (*rules_db_len < 8)
     148                 :            :                 return -EINVAL;
     149                 :            : 
     150         [ #  # ]:          0 :         for (i = 0; i < 8; i++) {
     151                 :          0 :                 if ((char) *rof_ptr !=
     152         [ #  # ]:          0 :                         (char)((combined_rof_tag >> (i * 8)) & 0xFF)) {
     153                 :            :                         combined_rof = false;
     154                 :            :                         break;
     155                 :            :                 }
     156                 :          0 :                 rof_ptr++;
     157                 :            :         }
     158                 :            :         rof_bytes_read += 8;
     159                 :            : 
     160         [ #  # ]:          0 :         if (combined_rof == true) {
     161                 :            :                 /* Need at least 24 bytes of header info: 16 byte combined */
     162                 :            :                 /* rof header and 8 byte binary rof blob header.           */
     163         [ #  # ]:          0 :                 if (*rules_db_len < 24)
     164                 :            :                         return -EINVAL;
     165                 :            : 
     166                 :            :                 /* Read the combined rof version and number of rof blocks */
     167         [ #  # ]:          0 :                 for (i = 0; i < 4; i++) {
     168                 :          0 :                         combined_rof_vers |= *rof_ptr << (i * 8);
     169                 :          0 :                         rof_ptr++;
     170                 :            :                 }
     171                 :            : 
     172                 :            :                 rof_bytes_read += 4;
     173                 :          0 :                 ret = mlx5_regex_check_rof_version(combined_rof_vers);
     174         [ #  # ]:          0 :                 if (ret < 0)
     175                 :            :                         return ret;
     176                 :            : 
     177         [ #  # ]:          0 :                 for (i = 0; i < 4; i++) {
     178                 :          0 :                         num_rof_blocks |= *rof_ptr << (i * 8);
     179                 :          0 :                         rof_ptr++;
     180                 :            :                 }
     181                 :            :                 rof_bytes_read += 4;
     182                 :            : 
     183         [ #  # ]:          0 :                 if (num_rof_blocks == 0)
     184                 :            :                         return -EINVAL;
     185                 :            : 
     186                 :            :                 /* Get the version of rxp we need the rof for */
     187                 :          0 :                 ret = mlx5_regex_get_rxp_vers(attr->regexp_version, &target_rxp_vers);
     188         [ #  # ]:          0 :                 if (ret < 0)
     189                 :            :                         return ret;
     190                 :            : 
     191                 :            :                 /* Try to find the rof binary blob for this version of rxp */
     192         [ #  # ]:          0 :                 for (j = 0; j < num_rof_blocks; j++) {
     193                 :            :                         rxpc_vers = 0;
     194                 :            :                         byte_count = 0;
     195         [ #  # ]:          0 :                         for (i = 0; i < 4; i++) {
     196                 :          0 :                                 rxpc_vers |= (*rof_ptr & 0xFF) << (i * 8);
     197                 :          0 :                                 rof_ptr++;
     198                 :            :                         }
     199         [ #  # ]:          0 :                         for (i = 0; i < 4; i++) {
     200                 :          0 :                                 byte_count |= (*rof_ptr & 0xFF) << (i * 8);
     201                 :          0 :                                 rof_ptr++;
     202                 :            :                         }
     203                 :          0 :                         rof_bytes_read += 8;
     204                 :            : 
     205         [ #  # ]:          0 :                         if (rxpc_vers == target_rxp_vers) {
     206                 :            :                                 /* Found corresponding binary rof entry */
     207         [ #  # ]:          0 :                                 if (rof_bytes_read + byte_count <= (*rules_db_len))
     208                 :            :                                         rof_binary_found = true;
     209                 :            :                                 else
     210                 :          0 :                                         DRV_LOG(ERR, "Compatible rof file found - invalid length!");
     211                 :            :                                 break;
     212                 :            :                         }
     213                 :            :                                 /* Move on to next rof blob */
     214         [ #  # ]:          0 :                         if (rof_bytes_read + byte_count + 8 < (*rules_db_len)) {
     215                 :          0 :                                 rof_ptr += byte_count;
     216                 :            :                                 rof_bytes_read += byte_count;
     217                 :            :                         } else {
     218                 :            :                                 /* Cannot parse any more of combined rof file */
     219                 :            :                                 break;
     220                 :            :                         }
     221                 :            :                 }
     222         [ #  # ]:          0 :                 if (rof_binary_found == true) {
     223                 :          0 :                         *rules_db = rof_ptr;
     224                 :          0 :                         *rules_db_len = byte_count;
     225                 :            :                 } else {
     226                 :          0 :                         DRV_LOG(ERR, "Compatible rof file not found!");
     227                 :          0 :                         return -EINVAL;
     228                 :            :                 }
     229                 :            :         }
     230                 :            :         return 0;
     231                 :            : }
     232                 :            : 
     233                 :            : int
     234                 :          0 : mlx5_regex_rules_db_import(struct rte_regexdev *dev,
     235                 :            :                      const char *rule_db, uint32_t rule_db_len)
     236                 :            : {
     237                 :          0 :         struct mlx5_regex_priv *priv = dev->data->dev_private;
     238                 :            :         struct mlx5_regex_mkey mkey;
     239                 :            :         uint32_t id;
     240                 :            :         int ret;
     241                 :            :         void *ptr;
     242                 :            : 
     243         [ #  # ]:          0 :         if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED) {
     244                 :          0 :                 DRV_LOG(ERR, "RXP programming mode not set!");
     245                 :          0 :                 return -1;
     246                 :            :         }
     247         [ #  # ]:          0 :         if (rule_db == NULL) {
     248                 :          0 :                 DRV_LOG(ERR, "Database empty!");
     249                 :          0 :                 return -ENODEV;
     250                 :            :         }
     251         [ #  # ]:          0 :         if (rule_db_len == 0)
     252                 :            :                 return -EINVAL;
     253                 :            : 
     254                 :          0 :         ret = mlx5_regex_parse_rules_db(priv, &rule_db, &rule_db_len);
     255         [ #  # ]:          0 :         if (ret < 0)
     256                 :            :                 return ret;
     257                 :            : 
     258                 :            :         /* copy rules - rules have to be 4KB aligned. */
     259                 :          0 :         ptr = rte_malloc("", rule_db_len, 1 << 12);
     260         [ #  # ]:          0 :         if (!ptr) {
     261                 :          0 :                 DRV_LOG(ERR, "Failed to allocate rules file memory.");
     262                 :          0 :                 return -ENOMEM;
     263                 :            :         }
     264         [ #  # ]:          0 :         rte_memcpy(ptr, rule_db, rule_db_len);
     265                 :            :         /* Register umem and create rof mkey. */
     266                 :          0 :         ret = rxp_create_mkey(priv, ptr, rule_db_len, /*access=*/7, &mkey);
     267         [ #  # ]:          0 :         if (ret < 0)
     268                 :            :                 return ret;
     269                 :            : 
     270         [ #  # ]:          0 :         for (id = 0; id < priv->nb_engines; id++) {
     271                 :          0 :                 ret = mlx5_devx_regex_rules_program(priv->cdev->ctx, id,
     272                 :          0 :                         mkey.mkey->id, rule_db_len, (uintptr_t)ptr);
     273         [ #  # ]:          0 :                 if (ret < 0) {
     274                 :          0 :                         DRV_LOG(ERR, "Failed to program rxp rules.");
     275                 :            :                         ret = -ENODEV;
     276                 :          0 :                         break;
     277                 :            :                 }
     278                 :            :                 ret = 0;
     279                 :            :         }
     280                 :          0 :         rxp_destroy_mkey(&mkey);
     281                 :          0 :         rte_free(ptr);
     282                 :          0 :         return ret;
     283                 :            : }
     284                 :            : 
     285                 :            : int
     286                 :          0 : mlx5_regex_configure(struct rte_regexdev *dev,
     287                 :            :                      const struct rte_regexdev_config *cfg)
     288                 :            : {
     289                 :          0 :         struct mlx5_regex_priv *priv = dev->data->dev_private;
     290                 :            :         int ret;
     291                 :            : 
     292         [ #  # ]:          0 :         if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED)
     293                 :            :                 return -1;
     294         [ #  # ]:          0 :         if (cfg->nb_max_matches != MLX5_REGEX_MAX_MATCHES) {
     295                 :          0 :                 DRV_LOG(ERR, "nb_max_matches is not configurable.");
     296                 :          0 :                 rte_errno = EINVAL;
     297                 :          0 :                 return -rte_errno;
     298                 :            :         }
     299                 :          0 :         priv->nb_queues = cfg->nb_queue_pairs;
     300                 :          0 :         dev->data->dev_conf.nb_queue_pairs = priv->nb_queues;
     301                 :          0 :         priv->qps = rte_zmalloc(NULL, sizeof(struct mlx5_regex_qp) *
     302                 :          0 :                                 priv->nb_queues, 0);
     303         [ #  # ]:          0 :         if (!priv->qps) {
     304                 :          0 :                 DRV_LOG(ERR, "can't allocate qps memory");
     305                 :          0 :                 rte_errno = ENOMEM;
     306                 :          0 :                 return -rte_errno;
     307                 :            :         }
     308                 :          0 :         priv->nb_max_matches = cfg->nb_max_matches;
     309         [ #  # ]:          0 :         if (cfg->rule_db != NULL) {
     310                 :          0 :                 ret = mlx5_regex_rules_db_import(dev, cfg->rule_db,
     311                 :          0 :                                                  cfg->rule_db_len);
     312         [ #  # ]:          0 :                 if (ret < 0) {
     313                 :          0 :                         DRV_LOG(ERR, "Failed to program rxp rules.");
     314                 :          0 :                         rte_errno = ENODEV;
     315                 :          0 :                         goto configure_error;
     316                 :            :                 }
     317                 :            :         } else
     318                 :          0 :                 DRV_LOG(DEBUG, "Regex config without rules programming!");
     319                 :            :         return 0;
     320                 :            : configure_error:
     321                 :          0 :         rte_free(priv->qps);
     322                 :          0 :         return -rte_errno;
     323                 :            : }

Generated by: LCOV version 1.14