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 : 37 : rpc_nvmf_set_max_subsystems(struct spdk_jsonrpc_request *request,
19 : : const struct spdk_json_val *params)
20 : : {
21 : 37 : uint32_t max_subsystems = 0;
22 : :
23 [ - + ]: 37 : 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 [ + - ]: 37 : if (params != NULL) {
31 [ - + ]: 37 : 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 : 37 : g_spdk_nvmf_tgt_conf.opts.max_subsystems = max_subsystems;
41 : :
42 : 37 : spdk_jsonrpc_send_bool_response(request, true);
43 : : }
44 : 774 : 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 : 39 : decode_admin_passthru(const struct spdk_json_val *val, void *out)
53 : : {
54 : 39 : struct spdk_nvmf_admin_passthru_conf *req = (struct spdk_nvmf_admin_passthru_conf *)out;
55 : :
56 [ - + ]: 39 : 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 : 39 : return 0;
64 : : }
65 : :
66 : : static int
67 : 38 : decode_discovery_filter(const struct spdk_json_val *val, void *out)
68 : : {
69 : 38 : uint32_t *_filter = out;
70 : 38 : uint32_t filter = SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY;
71 : 38 : char *tokens = spdk_json_strdup(val);
72 : : char *tok;
73 : 38 : int rc = -EINVAL;
74 : 38 : bool all_specified = false;
75 : :
76 [ - + ]: 38 : if (!tokens) {
77 : 0 : return -ENOMEM;
78 : : }
79 : :
80 : 38 : tok = strtok(tokens, ",");
81 [ + + ]: 76 : while (tok) {
82 [ + + + + ]: 38 : if (strncmp(tok, "match_any", 9) == 0) {
83 [ - + ]: 37 : if (filter != SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY) {
84 : 0 : goto out;
85 : : }
86 : 37 : filter = SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY;
87 : 37 : all_specified = true;
88 : : } else {
89 [ - + ]: 1 : if (all_specified) {
90 : 0 : goto out;
91 : : }
92 [ - + - + ]: 1 : if (strncmp(tok, "transport", 9) == 0) {
93 : 0 : filter |= SPDK_NVMF_TGT_DISCOVERY_MATCH_TRANSPORT_TYPE;
94 [ - + + - ]: 1 : } else if (strncmp(tok, "address", 7) == 0) {
95 : 1 : 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 : 38 : tok = strtok(NULL, ",");
105 : : }
106 : :
107 : 38 : rc = 0;
108 : 38 : *_filter = filter;
109 : :
110 : 38 : out:
111 : 38 : free(tokens);
112 : :
113 : 38 : 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 : 8 : decode_digest(const struct spdk_json_val *val, void *out)
164 : : {
165 : 8 : uint32_t *flags = out;
166 : 8 : char *digest = NULL;
167 : : int rc;
168 : :
169 : 8 : rc = spdk_json_decode_string(val, &digest);
170 [ - + ]: 8 : if (rc != 0) {
171 : 0 : return rc;
172 : : }
173 : :
174 : 8 : rc = spdk_nvme_dhchap_get_digest_id(digest);
175 [ + - ]: 8 : if (rc >= 0) {
176 [ - + ]: 8 : *flags |= SPDK_BIT(rc);
177 : 8 : rc = 0;
178 : : }
179 : 8 : free(digest);
180 : :
181 : 8 : return rc;
182 : : }
183 : :
184 : : static int
185 : 4 : decode_digest_array(const struct spdk_json_val *val, void *out)
186 : : {
187 : 4 : uint32_t *flags = out;
188 : 0 : size_t count;
189 : :
190 : 4 : *flags = 0;
191 : :
192 : 4 : return spdk_json_decode_array(val, decode_digest, out, 32, &count, 0);
193 : : }
194 : :
195 : : static int
196 : 8 : decode_dhgroup(const struct spdk_json_val *val, void *out)
197 : : {
198 : 8 : uint32_t *flags = out;
199 : 8 : char *dhgroup = NULL;
200 : : int rc;
201 : :
202 : 8 : rc = spdk_json_decode_string(val, &dhgroup);
203 [ - + ]: 8 : if (rc != 0) {
204 : 0 : return rc;
205 : : }
206 : :
207 : 8 : rc = spdk_nvme_dhchap_get_dhgroup_id(dhgroup);
208 [ + - ]: 8 : if (rc >= 0) {
209 [ - + ]: 8 : *flags |= SPDK_BIT(rc);
210 : 8 : rc = 0;
211 : : }
212 : 8 : free(dhgroup);
213 : :
214 : 8 : return rc;
215 : : }
216 : :
217 : : static int
218 : 4 : decode_dhgroup_array(const struct spdk_json_val *val, void *out)
219 : : {
220 : 4 : uint32_t *flags = out;
221 : 0 : size_t count;
222 : :
223 : 4 : *flags = 0;
224 : :
225 : 4 : 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 : 44 : 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 : 44 : memcpy(&conf, &g_spdk_nvmf_tgt_conf, sizeof(conf));
243 : :
244 [ + - ]: 44 : if (params != NULL) {
245 [ - + ]: 44 : 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 : 44 : memcpy(&g_spdk_nvmf_tgt_conf, &conf, sizeof(conf));
255 : :
256 : 44 : spdk_jsonrpc_send_bool_response(request, true);
257 : : }
258 : 774 : 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 : 37 : 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 : 37 : rpc_set_crdt.crdt1 = 0;
279 : 37 : rpc_set_crdt.crdt2 = 0;
280 : 37 : rpc_set_crdt.crdt3 = 0;
281 : :
282 [ + - ]: 37 : if (params != NULL) {
283 [ - + ]: 37 : 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 : 37 : g_spdk_nvmf_tgt_conf.opts.crdt[0] = rpc_set_crdt.crdt1;
293 : 37 : g_spdk_nvmf_tgt_conf.opts.crdt[1] = rpc_set_crdt.crdt2;
294 : 37 : g_spdk_nvmf_tgt_conf.opts.crdt[2] = rpc_set_crdt.crdt3;
295 : :
296 : 37 : spdk_jsonrpc_send_bool_response(request, true);
297 : : }
298 : 774 : SPDK_RPC_REGISTER("nvmf_set_crdt", rpc_nvmf_set_crdt, SPDK_RPC_STARTUP)
|