LCOV - code coverage report
Current view: top level - spdk/module/event/subsystems/nvmf - nvmf_rpc.c (source / functions) Hit Total Coverage
Test: Combined Lines: 81 136 59.6 %
Date: 2024-07-15 17:17:46 Functions: 12 14 85.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 21 64 32.8 %

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

Generated by: LCOV version 1.14