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