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