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_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_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 enum spdk_nvmf_tgt_discovery_filter *_filter = (enum spdk_nvmf_tgt_discovery_filter *)out; 70 enum spdk_nvmf_tgt_discovery_filter 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 const struct spdk_json_object_decoder nvmf_rpc_subsystem_tgt_conf_decoder[] = { 163 {"admin_cmd_passthru", offsetof(struct spdk_nvmf_tgt_conf, admin_passthru), decode_admin_passthru, true}, 164 {"poll_groups_mask", 0, nvmf_decode_poll_groups_mask, true}, 165 {"discovery_filter", offsetof(struct spdk_nvmf_tgt_conf, discovery_filter), decode_discovery_filter, true} 166 }; 167 168 static void 169 rpc_nvmf_set_config(struct spdk_jsonrpc_request *request, 170 const struct spdk_json_val *params) 171 { 172 struct spdk_nvmf_tgt_conf conf; 173 174 memcpy(&conf, &g_spdk_nvmf_tgt_conf, sizeof(conf)); 175 176 if (params != NULL) { 177 if (spdk_json_decode_object(params, nvmf_rpc_subsystem_tgt_conf_decoder, 178 SPDK_COUNTOF(nvmf_rpc_subsystem_tgt_conf_decoder), &conf)) { 179 SPDK_ERRLOG("spdk_json_decode_object() failed\n"); 180 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 181 "Invalid parameters"); 182 return; 183 } 184 } 185 186 memcpy(&g_spdk_nvmf_tgt_conf, &conf, sizeof(conf)); 187 188 spdk_jsonrpc_send_bool_response(request, true); 189 } 190 SPDK_RPC_REGISTER("nvmf_set_config", rpc_nvmf_set_config, SPDK_RPC_STARTUP) 191 192 struct nvmf_rpc_set_crdt { 193 uint16_t crdt1; 194 uint16_t crdt2; 195 uint16_t crdt3; 196 }; 197 198 static const struct spdk_json_object_decoder rpc_set_crdt_opts_decoders[] = { 199 {"crdt1", offsetof(struct nvmf_rpc_set_crdt, crdt1), spdk_json_decode_uint16, true}, 200 {"crdt2", offsetof(struct nvmf_rpc_set_crdt, crdt2), spdk_json_decode_uint16, true}, 201 {"crdt3", offsetof(struct nvmf_rpc_set_crdt, crdt3), spdk_json_decode_uint16, true}, 202 }; 203 204 static void 205 rpc_nvmf_set_crdt(struct spdk_jsonrpc_request *request, 206 const struct spdk_json_val *params) 207 { 208 struct nvmf_rpc_set_crdt rpc_set_crdt; 209 210 rpc_set_crdt.crdt1 = 0; 211 rpc_set_crdt.crdt2 = 0; 212 rpc_set_crdt.crdt3 = 0; 213 214 if (params != NULL) { 215 if (spdk_json_decode_object(params, rpc_set_crdt_opts_decoders, 216 SPDK_COUNTOF(rpc_set_crdt_opts_decoders), &rpc_set_crdt)) { 217 SPDK_ERRLOG("spdk_json_decode_object() failed\n"); 218 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 219 "Invalid parameters"); 220 return; 221 } 222 } 223 224 g_spdk_nvmf_tgt_crdt[0] = rpc_set_crdt.crdt1; 225 g_spdk_nvmf_tgt_crdt[1] = rpc_set_crdt.crdt2; 226 g_spdk_nvmf_tgt_crdt[2] = rpc_set_crdt.crdt3; 227 228 spdk_jsonrpc_send_bool_response(request, true); 229 } 230 SPDK_RPC_REGISTER("nvmf_set_crdt", rpc_nvmf_set_crdt, SPDK_RPC_STARTUP) 231