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