LCOV - code coverage report
Current view: top level - spdk/module/event/subsystems/nvmf - nvmf_rpc.c (source / functions) Hit Total Coverage
Test: Combined Lines: 102 156 65.4 %
Date: 2024-12-10 00:36:33 Functions: 12 14 85.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 69 132 52.3 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2016 Intel Corporation. All rights reserved.
       3                 :            :  *   Copyright (c) 2018-2019 Mellanox Technologies LTD. All rights reserved.
       4                 :            :  *   Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include "event_nvmf.h"
       8                 :            : 
       9                 :            : #include "spdk/rpc.h"
      10                 :            : #include "spdk/util.h"
      11                 :            : #include "spdk/cpuset.h"
      12                 :            : 
      13                 :            : static const struct spdk_json_object_decoder nvmf_rpc_subsystem_tgt_opts_decoder[] = {
      14                 :            :         {"max_subsystems", 0, spdk_json_decode_uint32, true}
      15                 :            : };
      16                 :            : 
      17                 :            : static void
      18                 :         43 : rpc_nvmf_set_max_subsystems(struct spdk_jsonrpc_request *request,
      19                 :            :                             const struct spdk_json_val *params)
      20                 :            : {
      21                 :         43 :         uint32_t max_subsystems = 0;
      22                 :            : 
      23   [ +  +  -  + ]:         43 :         if (g_spdk_nvmf_tgt_conf.opts.max_subsystems != 0) {
      24                 :          0 :                 SPDK_ERRLOG("this RPC must not be called more than once.\n");
      25                 :          0 :                 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
      26                 :            :                                                  "Must not call more than once");
      27                 :          0 :                 return;
      28                 :            :         }
      29                 :            : 
      30         [ +  + ]:         43 :         if (params != NULL) {
      31         [ -  + ]:         43 :                 if (spdk_json_decode_object(params, nvmf_rpc_subsystem_tgt_opts_decoder,
      32                 :            :                                             SPDK_COUNTOF(nvmf_rpc_subsystem_tgt_opts_decoder), &max_subsystems)) {
      33                 :          0 :                         SPDK_ERRLOG("spdk_json_decode_object() failed\n");
      34                 :          0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
      35                 :            :                                                          "Invalid parameters");
      36                 :          0 :                         return;
      37                 :            :                 }
      38                 :          3 :         }
      39                 :            : 
      40         [ +  - ]:         43 :         g_spdk_nvmf_tgt_conf.opts.max_subsystems = max_subsystems;
      41                 :            : 
      42                 :         43 :         spdk_jsonrpc_send_bool_response(request, true);
      43                 :          3 : }
      44                 :        913 : SPDK_RPC_REGISTER("nvmf_set_max_subsystems", rpc_nvmf_set_max_subsystems,
      45                 :            :                   SPDK_RPC_STARTUP)
      46                 :            : 
      47                 :            : static const struct spdk_json_object_decoder admin_passthru_decoder[] = {
      48                 :            :         {"identify_ctrlr", offsetof(struct spdk_nvmf_admin_passthru_conf, identify_ctrlr), spdk_json_decode_bool}
      49                 :            : };
      50                 :            : 
      51                 :            : static int
      52                 :         45 : decode_admin_passthru(const struct spdk_json_val *val, void *out)
      53                 :            : {
      54                 :         45 :         struct spdk_nvmf_admin_passthru_conf *req = (struct spdk_nvmf_admin_passthru_conf *)out;
      55                 :            : 
      56   [ -  +  -  + ]:         48 :         if (spdk_json_decode_object(val, admin_passthru_decoder,
      57                 :            :                                     SPDK_COUNTOF(admin_passthru_decoder),
      58                 :          3 :                                     req)) {
      59                 :          0 :                 SPDK_ERRLOG("spdk_json_decode_object failed\n");
      60                 :          0 :                 return -1;
      61                 :            :         }
      62                 :            : 
      63                 :         45 :         return 0;
      64                 :          3 : }
      65                 :            : 
      66                 :            : static int
      67                 :         44 : decode_discovery_filter(const struct spdk_json_val *val, void *out)
      68                 :            : {
      69                 :         44 :         uint32_t *_filter = out;
      70                 :         44 :         uint32_t filter = SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY;
      71                 :         44 :         char *tokens = spdk_json_strdup(val);
      72                 :            :         char *tok;
      73                 :         44 :         char *sp = NULL;
      74                 :         44 :         int rc = -EINVAL;
      75                 :         44 :         bool all_specified = false;
      76                 :            : 
      77         [ +  + ]:         44 :         if (!tokens) {
      78                 :          0 :                 return -ENOMEM;
      79                 :            :         }
      80                 :            : 
      81   [ +  -  +  - ]:         44 :         tok = strtok_r(tokens, ",", &sp);
      82         [ +  + ]:         88 :         while (tok) {
      83   [ +  +  +  +  :         44 :                 if (strncmp(tok, "match_any", 9) == 0) {
                   -  + ]
      84         [ +  + ]:         43 :                         if (filter != SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY) {
      85                 :          0 :                                 goto out;
      86                 :            :                         }
      87                 :         43 :                         filter = SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY;
      88                 :         43 :                         all_specified = true;
      89                 :          3 :                 } else {
      90   [ -  +  #  # ]:          1 :                         if (all_specified) {
      91                 :          0 :                                 goto out;
      92                 :            :                         }
      93   [ -  +  -  +  :          1 :                         if (strncmp(tok, "transport", 9) == 0) {
                   #  # ]
      94                 :          0 :                                 filter |= SPDK_NVMF_TGT_DISCOVERY_MATCH_TRANSPORT_TYPE;
      95   [ -  +  +  -  :          1 :                         } else if (strncmp(tok, "address", 7) == 0) {
                   #  # ]
      96                 :          1 :                                 filter |= SPDK_NVMF_TGT_DISCOVERY_MATCH_TRANSPORT_ADDRESS;
      97   [ #  #  #  #  :          0 :                         } else if (strncmp(tok, "svcid", 5) == 0) {
                   #  # ]
      98                 :          0 :                                 filter |= SPDK_NVMF_TGT_DISCOVERY_MATCH_TRANSPORT_SVCID;
      99                 :          0 :                         } else {
     100                 :          0 :                                 SPDK_ERRLOG("Invalid value %s\n", tok);
     101                 :          0 :                                 goto out;
     102                 :            :                         }
     103                 :            :                 }
     104                 :            : 
     105   [ +  -  +  - ]:         44 :                 tok = strtok_r(NULL, ",", &sp);
     106                 :            :         }
     107                 :            : 
     108                 :         44 :         rc = 0;
     109         [ +  - ]:         44 :         *_filter = filter;
     110                 :            : 
     111                 :         41 : out:
     112                 :         44 :         free(tokens);
     113                 :            : 
     114                 :         44 :         return rc;
     115                 :          3 : }
     116                 :            : 
     117                 :            : static int
     118                 :          0 : nvmf_is_subset_of_env_core_mask(const struct spdk_cpuset *set)
     119                 :            : {
     120                 :          0 :         uint32_t i, tmp_counter = 0;
     121                 :            : 
     122         [ #  # ]:          0 :         SPDK_ENV_FOREACH_CORE(i) {
     123         [ #  # ]:          0 :                 if (spdk_cpuset_get_cpu(set, i)) {
     124                 :          0 :                         ++tmp_counter;
     125                 :          0 :                 }
     126                 :          0 :         }
     127                 :          0 :         return spdk_cpuset_count(set) - tmp_counter;
     128                 :            : }
     129                 :            : 
     130                 :            : static int
     131                 :          0 : nvmf_decode_poll_groups_mask(const struct spdk_json_val *val, void *out)
     132                 :            : {
     133                 :          0 :         char *mask = spdk_json_strdup(val);
     134                 :          0 :         int ret = -1;
     135                 :            : 
     136         [ #  # ]:          0 :         if (mask == NULL) {
     137                 :          0 :                 return -1;
     138                 :            :         }
     139                 :            : 
     140         [ #  # ]:          0 :         if (!(g_poll_groups_mask = spdk_cpuset_alloc())) {
     141                 :          0 :                 SPDK_ERRLOG("Unable to allocate a poll groups mask object in nvmf_decode_poll_groups_mask.\n");
     142                 :          0 :                 free(mask);
     143                 :          0 :                 return -1;
     144                 :            :         }
     145                 :            : 
     146                 :          0 :         ret = spdk_cpuset_parse(g_poll_groups_mask, mask);
     147                 :          0 :         free(mask);
     148         [ #  # ]:          0 :         if (ret == 0) {
     149         [ #  # ]:          0 :                 if (nvmf_is_subset_of_env_core_mask(g_poll_groups_mask) == 0) {
     150                 :          0 :                         return 0;
     151                 :            :                 } else {
     152                 :          0 :                         SPDK_ERRLOG("Poll groups cpumask 0x%s is out of range\n", spdk_cpuset_fmt(g_poll_groups_mask));
     153                 :            :                 }
     154                 :          0 :         } else {
     155                 :          0 :                 SPDK_ERRLOG("Invalid cpumask\n");
     156                 :            :         }
     157                 :            : 
     158                 :          0 :         spdk_cpuset_free(g_poll_groups_mask);
     159                 :          0 :         g_poll_groups_mask = NULL;
     160                 :          0 :         return -1;
     161                 :          0 : }
     162                 :            : 
     163                 :            : static int
     164                 :        137 : decode_digest(const struct spdk_json_val *val, void *out)
     165                 :            : {
     166                 :        137 :         uint32_t *flags = out;
     167                 :        137 :         char *digest = NULL;
     168                 :            :         int rc;
     169                 :            : 
     170                 :        137 :         rc = spdk_json_decode_string(val, &digest);
     171         [ -  + ]:        137 :         if (rc != 0) {
     172                 :          0 :                 return rc;
     173                 :            :         }
     174                 :            : 
     175                 :        137 :         rc = spdk_nvme_dhchap_get_digest_id(digest);
     176         [ +  + ]:        137 :         if (rc >= 0) {
     177   [ +  +  +  - ]:        137 :                 *flags |= SPDK_BIT(rc);
     178                 :        137 :                 rc = 0;
     179                 :          9 :         }
     180                 :        137 :         free(digest);
     181                 :            : 
     182                 :        137 :         return rc;
     183                 :          9 : }
     184                 :            : 
     185                 :            : static int
     186                 :         47 : decode_digest_array(const struct spdk_json_val *val, void *out)
     187                 :            : {
     188                 :         47 :         uint32_t *flags = out;
     189                 :         15 :         size_t count;
     190                 :            : 
     191         [ +  - ]:         47 :         *flags = 0;
     192                 :            : 
     193                 :         47 :         return spdk_json_decode_array(val, decode_digest, out, 32, &count, 0);
     194                 :            : }
     195                 :            : 
     196                 :            : static int
     197                 :        266 : decode_dhgroup(const struct spdk_json_val *val, void *out)
     198                 :            : {
     199                 :        266 :         uint32_t *flags = out;
     200                 :        266 :         char *dhgroup = NULL;
     201                 :            :         int rc;
     202                 :            : 
     203                 :        266 :         rc = spdk_json_decode_string(val, &dhgroup);
     204         [ -  + ]:        266 :         if (rc != 0) {
     205                 :          0 :                 return rc;
     206                 :            :         }
     207                 :            : 
     208                 :        266 :         rc = spdk_nvme_dhchap_get_dhgroup_id(dhgroup);
     209         [ +  + ]:        266 :         if (rc >= 0) {
     210   [ +  +  +  - ]:        266 :                 *flags |= SPDK_BIT(rc);
     211                 :        266 :                 rc = 0;
     212                 :         18 :         }
     213                 :        266 :         free(dhgroup);
     214                 :            : 
     215                 :        266 :         return rc;
     216                 :         18 : }
     217                 :            : 
     218                 :            : static int
     219                 :         47 : decode_dhgroup_array(const struct spdk_json_val *val, void *out)
     220                 :            : {
     221                 :         47 :         uint32_t *flags = out;
     222                 :         15 :         size_t count;
     223                 :            : 
     224         [ +  - ]:         47 :         *flags = 0;
     225                 :            : 
     226                 :         47 :         return spdk_json_decode_array(val, decode_dhgroup, out, 32, &count, 0);
     227                 :            : }
     228                 :            : 
     229                 :            : static const struct spdk_json_object_decoder nvmf_rpc_subsystem_tgt_conf_decoder[] = {
     230                 :            :         {"admin_cmd_passthru", offsetof(struct spdk_nvmf_tgt_conf, admin_passthru), decode_admin_passthru, true},
     231                 :            :         {"poll_groups_mask", 0, nvmf_decode_poll_groups_mask, true},
     232                 :            :         {"discovery_filter", offsetof(struct spdk_nvmf_tgt_conf, opts.discovery_filter), decode_discovery_filter, true},
     233                 :            :         {"dhchap_digests", offsetof(struct spdk_nvmf_tgt_conf, opts.dhchap_digests), decode_digest_array, true},
     234                 :            :         {"dhchap_dhgroups", offsetof(struct spdk_nvmf_tgt_conf, opts.dhchap_dhgroups), decode_dhgroup_array, true},
     235                 :            : };
     236                 :            : 
     237                 :            : static void
     238                 :         50 : rpc_nvmf_set_config(struct spdk_jsonrpc_request *request,
     239                 :            :                     const struct spdk_json_val *params)
     240                 :            : {
     241                 :         15 :         struct spdk_nvmf_tgt_conf conf;
     242                 :            : 
     243   [ +  -  +  - ]:         50 :         memcpy(&conf, &g_spdk_nvmf_tgt_conf, sizeof(conf));
     244                 :            : 
     245         [ +  + ]:         50 :         if (params != NULL) {
     246         [ +  + ]:         50 :                 if (spdk_json_decode_object(params, nvmf_rpc_subsystem_tgt_conf_decoder,
     247                 :            :                                             SPDK_COUNTOF(nvmf_rpc_subsystem_tgt_conf_decoder), &conf)) {
     248                 :          0 :                         SPDK_ERRLOG("spdk_json_decode_object() failed\n");
     249                 :          0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     250                 :            :                                                          "Invalid parameters");
     251                 :          0 :                         return;
     252                 :            :                 }
     253                 :          3 :         }
     254                 :            : 
     255   [ +  -  +  - ]:         50 :         memcpy(&g_spdk_nvmf_tgt_conf, &conf, sizeof(conf));
     256                 :            : 
     257                 :         50 :         spdk_jsonrpc_send_bool_response(request, true);
     258                 :          3 : }
     259                 :        913 : SPDK_RPC_REGISTER("nvmf_set_config", rpc_nvmf_set_config, SPDK_RPC_STARTUP)
     260                 :            : 
     261                 :            : struct nvmf_rpc_set_crdt {
     262                 :            :         uint16_t crdt1;
     263                 :            :         uint16_t crdt2;
     264                 :            :         uint16_t crdt3;
     265                 :            : };
     266                 :            : 
     267                 :            : static const struct spdk_json_object_decoder rpc_set_crdt_opts_decoders[] = {
     268                 :            :         {"crdt1", offsetof(struct nvmf_rpc_set_crdt, crdt1), spdk_json_decode_uint16, true},
     269                 :            :         {"crdt2", offsetof(struct nvmf_rpc_set_crdt, crdt2), spdk_json_decode_uint16, true},
     270                 :            :         {"crdt3", offsetof(struct nvmf_rpc_set_crdt, crdt3), spdk_json_decode_uint16, true},
     271                 :            : };
     272                 :            : 
     273                 :            : static void
     274                 :         43 : rpc_nvmf_set_crdt(struct spdk_jsonrpc_request *request,
     275                 :            :                   const struct spdk_json_val *params)
     276                 :            : {
     277                 :         15 :         struct nvmf_rpc_set_crdt rpc_set_crdt;
     278                 :            : 
     279                 :         43 :         rpc_set_crdt.crdt1 = 0;
     280         [ +  - ]:         43 :         rpc_set_crdt.crdt2 = 0;
     281         [ +  - ]:         43 :         rpc_set_crdt.crdt3 = 0;
     282                 :            : 
     283         [ +  + ]:         43 :         if (params != NULL) {
     284         [ +  + ]:         43 :                 if (spdk_json_decode_object(params, rpc_set_crdt_opts_decoders,
     285                 :            :                                             SPDK_COUNTOF(rpc_set_crdt_opts_decoders), &rpc_set_crdt)) {
     286                 :          0 :                         SPDK_ERRLOG("spdk_json_decode_object() failed\n");
     287                 :          0 :                         spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
     288                 :            :                                                          "Invalid parameters");
     289                 :          0 :                         return;
     290                 :            :                 }
     291                 :          3 :         }
     292                 :            : 
     293   [ +  -  +  -  :         43 :         g_spdk_nvmf_tgt_conf.opts.crdt[0] = rpc_set_crdt.crdt1;
                   +  - ]
     294   [ +  -  +  -  :         43 :         g_spdk_nvmf_tgt_conf.opts.crdt[1] = rpc_set_crdt.crdt2;
             +  -  +  - ]
     295   [ +  -  +  -  :         43 :         g_spdk_nvmf_tgt_conf.opts.crdt[2] = rpc_set_crdt.crdt3;
             +  -  +  - ]
     296                 :            : 
     297                 :         43 :         spdk_jsonrpc_send_bool_response(request, true);
     298                 :          3 : }
     299                 :        913 : SPDK_RPC_REGISTER("nvmf_set_crdt", rpc_nvmf_set_crdt, SPDK_RPC_STARTUP)

Generated by: LCOV version 1.14