Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause
2 : * Copyright (C) 2016 Intel Corporation. All rights reserved.
3 : * Copyright (c) 2019-2021 Mellanox Technologies LTD. All rights reserved.
4 : * Copyright (c) 2022-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : * Copyright (c) 2022 Dell Inc, or its subsidiaries. All rights reserved.
6 : */
7 :
8 : #include "spdk/stdinc.h"
9 :
10 : #include "bdev_nvme.h"
11 :
12 : #include "spdk/config.h"
13 :
14 : #include "spdk/string.h"
15 : #include "spdk/rpc.h"
16 : #include "spdk/util.h"
17 : #include "spdk/env.h"
18 : #include "spdk/nvme.h"
19 : #include "spdk/nvme_spec.h"
20 :
21 : #include "spdk/log.h"
22 : #include "spdk/bdev_module.h"
23 :
24 : static bool g_tls_log = false;
25 :
26 : static int
27 0 : rpc_decode_action_on_timeout(const struct spdk_json_val *val, void *out)
28 : {
29 0 : enum spdk_bdev_timeout_action *action = out;
30 :
31 0 : if (spdk_json_strequal(val, "none") == true) {
32 0 : *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE;
33 0 : } else if (spdk_json_strequal(val, "abort") == true) {
34 0 : *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT;
35 0 : } else if (spdk_json_strequal(val, "reset") == true) {
36 0 : *action = SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET;
37 : } else {
38 0 : SPDK_NOTICELOG("Invalid parameter value: action_on_timeout\n");
39 0 : return -EINVAL;
40 : }
41 :
42 0 : return 0;
43 : }
44 :
45 : static int
46 0 : rpc_decode_digest(const struct spdk_json_val *val, void *out)
47 : {
48 0 : uint32_t *flags = out;
49 0 : char *digest = NULL;
50 : int rc;
51 :
52 0 : rc = spdk_json_decode_string(val, &digest);
53 0 : if (rc != 0) {
54 0 : return rc;
55 : }
56 :
57 0 : rc = spdk_nvme_dhchap_get_digest_id(digest);
58 0 : if (rc >= 0) {
59 0 : *flags |= SPDK_BIT(rc);
60 0 : rc = 0;
61 : }
62 0 : free(digest);
63 :
64 0 : return rc;
65 : }
66 :
67 : static int
68 0 : rpc_decode_digest_array(const struct spdk_json_val *val, void *out)
69 : {
70 0 : uint32_t *flags = out;
71 0 : size_t count;
72 :
73 0 : *flags = 0;
74 :
75 0 : return spdk_json_decode_array(val, rpc_decode_digest, out, 32, &count, 0);
76 : }
77 :
78 : static int
79 0 : rpc_decode_dhgroup(const struct spdk_json_val *val, void *out)
80 : {
81 0 : uint32_t *flags = out;
82 0 : char *dhgroup = NULL;
83 : int rc;
84 :
85 0 : rc = spdk_json_decode_string(val, &dhgroup);
86 0 : if (rc != 0) {
87 0 : return rc;
88 : }
89 :
90 0 : rc = spdk_nvme_dhchap_get_dhgroup_id(dhgroup);
91 0 : if (rc >= 0) {
92 0 : *flags |= SPDK_BIT(rc);
93 0 : rc = 0;
94 : }
95 0 : free(dhgroup);
96 :
97 0 : return rc;
98 : }
99 :
100 : static int
101 0 : rpc_decode_dhgroup_array(const struct spdk_json_val *val, void *out)
102 : {
103 0 : uint32_t *flags = out;
104 0 : size_t count;
105 :
106 0 : *flags = 0;
107 :
108 0 : return spdk_json_decode_array(val, rpc_decode_dhgroup, out, 32, &count, 0);
109 : }
110 :
111 : static const struct spdk_json_object_decoder rpc_bdev_nvme_options_decoders[] = {
112 : {"action_on_timeout", offsetof(struct spdk_bdev_nvme_opts, action_on_timeout), rpc_decode_action_on_timeout, true},
113 : {"timeout_us", offsetof(struct spdk_bdev_nvme_opts, timeout_us), spdk_json_decode_uint64, true},
114 : {"timeout_admin_us", offsetof(struct spdk_bdev_nvme_opts, timeout_admin_us), spdk_json_decode_uint64, true},
115 : {"keep_alive_timeout_ms", offsetof(struct spdk_bdev_nvme_opts, keep_alive_timeout_ms), spdk_json_decode_uint32, true},
116 : {"arbitration_burst", offsetof(struct spdk_bdev_nvme_opts, arbitration_burst), spdk_json_decode_uint32, true},
117 : {"low_priority_weight", offsetof(struct spdk_bdev_nvme_opts, low_priority_weight), spdk_json_decode_uint32, true},
118 : {"medium_priority_weight", offsetof(struct spdk_bdev_nvme_opts, medium_priority_weight), spdk_json_decode_uint32, true},
119 : {"high_priority_weight", offsetof(struct spdk_bdev_nvme_opts, high_priority_weight), spdk_json_decode_uint32, true},
120 : {"nvme_adminq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_adminq_poll_period_us), spdk_json_decode_uint64, true},
121 : {"nvme_ioq_poll_period_us", offsetof(struct spdk_bdev_nvme_opts, nvme_ioq_poll_period_us), spdk_json_decode_uint64, true},
122 : {"io_queue_requests", offsetof(struct spdk_bdev_nvme_opts, io_queue_requests), spdk_json_decode_uint32, true},
123 : {"delay_cmd_submit", offsetof(struct spdk_bdev_nvme_opts, delay_cmd_submit), spdk_json_decode_bool, true},
124 : {"transport_retry_count", offsetof(struct spdk_bdev_nvme_opts, transport_retry_count), spdk_json_decode_uint32, true},
125 : {"bdev_retry_count", offsetof(struct spdk_bdev_nvme_opts, bdev_retry_count), spdk_json_decode_int32, true},
126 : {"transport_ack_timeout", offsetof(struct spdk_bdev_nvme_opts, transport_ack_timeout), spdk_json_decode_uint8, true},
127 : {"ctrlr_loss_timeout_sec", offsetof(struct spdk_bdev_nvme_opts, ctrlr_loss_timeout_sec), spdk_json_decode_int32, true},
128 : {"reconnect_delay_sec", offsetof(struct spdk_bdev_nvme_opts, reconnect_delay_sec), spdk_json_decode_uint32, true},
129 : {"fast_io_fail_timeout_sec", offsetof(struct spdk_bdev_nvme_opts, fast_io_fail_timeout_sec), spdk_json_decode_uint32, true},
130 : {"disable_auto_failback", offsetof(struct spdk_bdev_nvme_opts, disable_auto_failback), spdk_json_decode_bool, true},
131 : {"generate_uuids", offsetof(struct spdk_bdev_nvme_opts, generate_uuids), spdk_json_decode_bool, true},
132 : {"transport_tos", offsetof(struct spdk_bdev_nvme_opts, transport_tos), spdk_json_decode_uint8, true},
133 : {"nvme_error_stat", offsetof(struct spdk_bdev_nvme_opts, nvme_error_stat), spdk_json_decode_bool, true},
134 : {"rdma_srq_size", offsetof(struct spdk_bdev_nvme_opts, rdma_srq_size), spdk_json_decode_uint32, true},
135 : {"io_path_stat", offsetof(struct spdk_bdev_nvme_opts, io_path_stat), spdk_json_decode_bool, true},
136 : {"allow_accel_sequence", offsetof(struct spdk_bdev_nvme_opts, allow_accel_sequence), spdk_json_decode_bool, true},
137 : {"rdma_max_cq_size", offsetof(struct spdk_bdev_nvme_opts, rdma_max_cq_size), spdk_json_decode_uint32, true},
138 : {"rdma_cm_event_timeout_ms", offsetof(struct spdk_bdev_nvme_opts, rdma_cm_event_timeout_ms), spdk_json_decode_uint16, true},
139 : {"dhchap_digests", offsetof(struct spdk_bdev_nvme_opts, dhchap_digests), rpc_decode_digest_array, true},
140 : {"dhchap_dhgroups", offsetof(struct spdk_bdev_nvme_opts, dhchap_dhgroups), rpc_decode_dhgroup_array, true},
141 : };
142 :
143 : static void
144 0 : rpc_bdev_nvme_set_options(struct spdk_jsonrpc_request *request,
145 : const struct spdk_json_val *params)
146 : {
147 0 : struct spdk_bdev_nvme_opts opts;
148 : int rc;
149 :
150 0 : bdev_nvme_get_opts(&opts);
151 0 : if (params && spdk_json_decode_object(params, rpc_bdev_nvme_options_decoders,
152 : SPDK_COUNTOF(rpc_bdev_nvme_options_decoders),
153 : &opts)) {
154 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
155 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
156 : "spdk_json_decode_object failed");
157 0 : return;
158 : }
159 :
160 0 : rc = bdev_nvme_set_opts(&opts);
161 0 : if (rc == -EPERM) {
162 0 : spdk_jsonrpc_send_error_response(request, -EPERM,
163 : "RPC not permitted with nvme controllers already attached");
164 0 : } else if (rc) {
165 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
166 : } else {
167 0 : spdk_jsonrpc_send_bool_response(request, true);
168 : }
169 :
170 0 : return;
171 : }
172 0 : SPDK_RPC_REGISTER("bdev_nvme_set_options", rpc_bdev_nvme_set_options,
173 : SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
174 :
175 : struct rpc_bdev_nvme_hotplug {
176 : bool enabled;
177 : uint64_t period_us;
178 : };
179 :
180 : static const struct spdk_json_object_decoder rpc_bdev_nvme_hotplug_decoders[] = {
181 : {"enable", offsetof(struct rpc_bdev_nvme_hotplug, enabled), spdk_json_decode_bool, false},
182 : {"period_us", offsetof(struct rpc_bdev_nvme_hotplug, period_us), spdk_json_decode_uint64, true},
183 : };
184 :
185 : static void
186 0 : rpc_bdev_nvme_set_hotplug_done(void *ctx)
187 : {
188 0 : struct spdk_jsonrpc_request *request = ctx;
189 :
190 0 : spdk_jsonrpc_send_bool_response(request, true);
191 0 : }
192 :
193 : static void
194 0 : rpc_bdev_nvme_set_hotplug(struct spdk_jsonrpc_request *request,
195 : const struct spdk_json_val *params)
196 : {
197 0 : struct rpc_bdev_nvme_hotplug req = {false, 0};
198 : int rc;
199 :
200 0 : if (spdk_json_decode_object(params, rpc_bdev_nvme_hotplug_decoders,
201 : SPDK_COUNTOF(rpc_bdev_nvme_hotplug_decoders), &req)) {
202 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
203 0 : rc = -EINVAL;
204 0 : goto invalid;
205 : }
206 :
207 0 : rc = bdev_nvme_set_hotplug(req.enabled, req.period_us, rpc_bdev_nvme_set_hotplug_done,
208 : request);
209 0 : if (rc) {
210 0 : goto invalid;
211 : }
212 :
213 0 : return;
214 0 : invalid:
215 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(-rc));
216 : }
217 0 : SPDK_RPC_REGISTER("bdev_nvme_set_hotplug", rpc_bdev_nvme_set_hotplug, SPDK_RPC_RUNTIME)
218 :
219 : enum bdev_nvme_multipath_mode {
220 : BDEV_NVME_MP_MODE_FAILOVER,
221 : BDEV_NVME_MP_MODE_MULTIPATH,
222 : BDEV_NVME_MP_MODE_DISABLE,
223 : };
224 :
225 : struct rpc_bdev_nvme_attach_controller {
226 : char *name;
227 : char *trtype;
228 : char *adrfam;
229 : char *traddr;
230 : char *trsvcid;
231 : char *priority;
232 : char *subnqn;
233 : char *hostnqn;
234 : char *hostaddr;
235 : char *hostsvcid;
236 : char *psk;
237 : char *dhchap_key;
238 : char *dhchap_ctrlr_key;
239 : enum bdev_nvme_multipath_mode multipath;
240 : struct nvme_ctrlr_opts bdev_opts;
241 : struct spdk_nvme_ctrlr_opts drv_opts;
242 : uint32_t max_bdevs;
243 : };
244 :
245 : static void
246 0 : free_rpc_bdev_nvme_attach_controller(struct rpc_bdev_nvme_attach_controller *req)
247 : {
248 0 : free(req->name);
249 0 : free(req->trtype);
250 0 : free(req->adrfam);
251 0 : free(req->traddr);
252 0 : free(req->trsvcid);
253 0 : free(req->priority);
254 0 : free(req->subnqn);
255 0 : free(req->hostnqn);
256 0 : free(req->hostaddr);
257 0 : free(req->hostsvcid);
258 0 : free(req->psk);
259 0 : free(req->dhchap_key);
260 0 : free(req->dhchap_ctrlr_key);
261 0 : spdk_memset_s(req->drv_opts.psk, sizeof(req->drv_opts.psk), 0, sizeof(req->drv_opts.psk));
262 0 : }
263 :
264 : static int
265 0 : bdev_nvme_decode_reftag(const struct spdk_json_val *val, void *out)
266 : {
267 0 : uint32_t *flag = out;
268 0 : bool reftag;
269 : int rc;
270 :
271 0 : rc = spdk_json_decode_bool(val, &reftag);
272 0 : if (rc == 0 && reftag == true) {
273 0 : *flag |= SPDK_NVME_IO_FLAGS_PRCHK_REFTAG;
274 : }
275 :
276 0 : return rc;
277 : }
278 :
279 : static int
280 0 : bdev_nvme_decode_guard(const struct spdk_json_val *val, void *out)
281 : {
282 0 : uint32_t *flag = out;
283 0 : bool guard;
284 : int rc;
285 :
286 0 : rc = spdk_json_decode_bool(val, &guard);
287 0 : if (rc == 0 && guard == true) {
288 0 : *flag |= SPDK_NVME_IO_FLAGS_PRCHK_GUARD;
289 : }
290 :
291 0 : return rc;
292 : }
293 :
294 : static int
295 0 : bdev_nvme_decode_multipath(const struct spdk_json_val *val, void *out)
296 : {
297 0 : enum bdev_nvme_multipath_mode *multipath = out;
298 :
299 0 : if (spdk_json_strequal(val, "failover") == true) {
300 0 : *multipath = BDEV_NVME_MP_MODE_FAILOVER;
301 0 : } else if (spdk_json_strequal(val, "multipath") == true) {
302 0 : *multipath = BDEV_NVME_MP_MODE_MULTIPATH;
303 0 : } else if (spdk_json_strequal(val, "disable") == true) {
304 0 : *multipath = BDEV_NVME_MP_MODE_DISABLE;
305 : } else {
306 0 : SPDK_NOTICELOG("Invalid parameter value: multipath\n");
307 0 : return -EINVAL;
308 : }
309 :
310 0 : return 0;
311 : }
312 :
313 :
314 : static const struct spdk_json_object_decoder rpc_bdev_nvme_attach_controller_decoders[] = {
315 : {"name", offsetof(struct rpc_bdev_nvme_attach_controller, name), spdk_json_decode_string},
316 : {"trtype", offsetof(struct rpc_bdev_nvme_attach_controller, trtype), spdk_json_decode_string},
317 : {"traddr", offsetof(struct rpc_bdev_nvme_attach_controller, traddr), spdk_json_decode_string},
318 :
319 : {"adrfam", offsetof(struct rpc_bdev_nvme_attach_controller, adrfam), spdk_json_decode_string, true},
320 : {"trsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, trsvcid), spdk_json_decode_string, true},
321 : {"priority", offsetof(struct rpc_bdev_nvme_attach_controller, priority), spdk_json_decode_string, true},
322 : {"subnqn", offsetof(struct rpc_bdev_nvme_attach_controller, subnqn), spdk_json_decode_string, true},
323 : {"hostnqn", offsetof(struct rpc_bdev_nvme_attach_controller, hostnqn), spdk_json_decode_string, true},
324 : {"hostaddr", offsetof(struct rpc_bdev_nvme_attach_controller, hostaddr), spdk_json_decode_string, true},
325 : {"hostsvcid", offsetof(struct rpc_bdev_nvme_attach_controller, hostsvcid), spdk_json_decode_string, true},
326 :
327 : {"prchk_reftag", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.prchk_flags), bdev_nvme_decode_reftag, true},
328 : {"prchk_guard", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.prchk_flags), bdev_nvme_decode_guard, true},
329 : {"hdgst", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.header_digest), spdk_json_decode_bool, true},
330 : {"ddgst", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.data_digest), spdk_json_decode_bool, true},
331 : {"fabrics_connect_timeout_us", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.fabrics_connect_timeout_us), spdk_json_decode_uint64, true},
332 : {"multipath", offsetof(struct rpc_bdev_nvme_attach_controller, multipath), bdev_nvme_decode_multipath, true},
333 : {"num_io_queues", offsetof(struct rpc_bdev_nvme_attach_controller, drv_opts.num_io_queues), spdk_json_decode_uint32, true},
334 : {"ctrlr_loss_timeout_sec", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.ctrlr_loss_timeout_sec), spdk_json_decode_int32, true},
335 : {"reconnect_delay_sec", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.reconnect_delay_sec), spdk_json_decode_uint32, true},
336 : {"fast_io_fail_timeout_sec", offsetof(struct rpc_bdev_nvme_attach_controller, bdev_opts.fast_io_fail_timeout_sec), spdk_json_decode_uint32, true},
337 : {"psk", offsetof(struct rpc_bdev_nvme_attach_controller, psk), spdk_json_decode_string, true},
338 : {"max_bdevs", offsetof(struct rpc_bdev_nvme_attach_controller, max_bdevs), spdk_json_decode_uint32, true},
339 : {"dhchap_key", offsetof(struct rpc_bdev_nvme_attach_controller, dhchap_key), spdk_json_decode_string, true},
340 : {"dhchap_ctrlr_key", offsetof(struct rpc_bdev_nvme_attach_controller, dhchap_ctrlr_key), spdk_json_decode_string, true},
341 : };
342 :
343 : #define DEFAULT_MAX_BDEVS_PER_RPC 128
344 :
345 : struct rpc_bdev_nvme_attach_controller_ctx {
346 : struct rpc_bdev_nvme_attach_controller req;
347 : size_t bdev_count;
348 : const char **names;
349 : struct spdk_jsonrpc_request *request;
350 : };
351 :
352 : static void
353 0 : free_rpc_bdev_nvme_attach_controller_ctx(struct rpc_bdev_nvme_attach_controller_ctx *ctx)
354 : {
355 0 : free_rpc_bdev_nvme_attach_controller(&ctx->req);
356 0 : free(ctx->names);
357 0 : free(ctx);
358 0 : }
359 :
360 : static void
361 0 : rpc_bdev_nvme_attach_controller_examined(void *cb_ctx)
362 : {
363 0 : struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx;
364 0 : struct spdk_jsonrpc_request *request = ctx->request;
365 : struct spdk_json_write_ctx *w;
366 : size_t i;
367 :
368 0 : w = spdk_jsonrpc_begin_result(request);
369 0 : spdk_json_write_array_begin(w);
370 0 : for (i = 0; i < ctx->bdev_count; i++) {
371 0 : spdk_json_write_string(w, ctx->names[i]);
372 : }
373 0 : spdk_json_write_array_end(w);
374 0 : spdk_jsonrpc_end_result(request, w);
375 :
376 0 : free_rpc_bdev_nvme_attach_controller_ctx(ctx);
377 0 : }
378 :
379 : static void
380 0 : rpc_bdev_nvme_attach_controller_done(void *cb_ctx, size_t bdev_count, int rc)
381 : {
382 0 : struct rpc_bdev_nvme_attach_controller_ctx *ctx = cb_ctx;
383 0 : struct spdk_jsonrpc_request *request = ctx->request;
384 :
385 0 : if (rc < 0) {
386 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
387 0 : free_rpc_bdev_nvme_attach_controller_ctx(ctx);
388 0 : return;
389 : }
390 :
391 0 : ctx->bdev_count = bdev_count;
392 0 : spdk_bdev_wait_for_examine(rpc_bdev_nvme_attach_controller_examined, ctx);
393 : }
394 :
395 : static void
396 0 : rpc_bdev_nvme_attach_controller(struct spdk_jsonrpc_request *request,
397 : const struct spdk_json_val *params)
398 : {
399 : struct rpc_bdev_nvme_attach_controller_ctx *ctx;
400 0 : struct spdk_nvme_transport_id trid = {};
401 : const struct spdk_nvme_ctrlr_opts *drv_opts;
402 : const struct spdk_nvme_transport_id *ctrlr_trid;
403 0 : struct nvme_ctrlr *ctrlr = NULL;
404 : size_t len, maxlen;
405 0 : bool multipath = false;
406 : int rc;
407 :
408 0 : ctx = calloc(1, sizeof(*ctx));
409 0 : if (!ctx) {
410 0 : spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
411 0 : return;
412 : }
413 :
414 0 : spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.drv_opts, sizeof(ctx->req.drv_opts));
415 0 : bdev_nvme_get_default_ctrlr_opts(&ctx->req.bdev_opts);
416 : /* For now, initialize the multipath parameter to add a failover path. This maintains backward
417 : * compatibility with past behavior. In the future, this behavior will change to "disable". */
418 0 : ctx->req.multipath = BDEV_NVME_MP_MODE_FAILOVER;
419 0 : ctx->req.max_bdevs = DEFAULT_MAX_BDEVS_PER_RPC;
420 :
421 0 : if (spdk_json_decode_object(params, rpc_bdev_nvme_attach_controller_decoders,
422 : SPDK_COUNTOF(rpc_bdev_nvme_attach_controller_decoders),
423 0 : &ctx->req)) {
424 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
425 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
426 : "spdk_json_decode_object failed");
427 0 : goto cleanup;
428 : }
429 :
430 0 : if (ctx->req.max_bdevs == 0) {
431 0 : spdk_jsonrpc_send_error_response(request, -EINVAL, "max_bdevs cannot be zero");
432 0 : goto cleanup;
433 : }
434 :
435 0 : ctx->names = calloc(ctx->req.max_bdevs, sizeof(char *));
436 0 : if (ctx->names == NULL) {
437 0 : spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
438 0 : goto cleanup;
439 : }
440 :
441 : /* Parse trstring */
442 0 : rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype);
443 0 : if (rc < 0) {
444 0 : SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype);
445 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
446 : ctx->req.trtype);
447 0 : goto cleanup;
448 : }
449 :
450 : /* Parse trtype */
451 0 : rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype);
452 0 : assert(rc == 0);
453 :
454 : /* Parse traddr */
455 0 : maxlen = sizeof(trid.traddr);
456 0 : len = strnlen(ctx->req.traddr, maxlen);
457 0 : if (len == maxlen) {
458 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s",
459 : ctx->req.traddr);
460 0 : goto cleanup;
461 : }
462 0 : memcpy(trid.traddr, ctx->req.traddr, len + 1);
463 :
464 : /* Parse adrfam */
465 0 : if (ctx->req.adrfam) {
466 0 : rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam);
467 0 : if (rc < 0) {
468 0 : SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam);
469 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s",
470 : ctx->req.adrfam);
471 0 : goto cleanup;
472 : }
473 : }
474 :
475 : /* Parse trsvcid */
476 0 : if (ctx->req.trsvcid) {
477 0 : maxlen = sizeof(trid.trsvcid);
478 0 : len = strnlen(ctx->req.trsvcid, maxlen);
479 0 : if (len == maxlen) {
480 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s",
481 : ctx->req.trsvcid);
482 0 : goto cleanup;
483 : }
484 0 : memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1);
485 : }
486 :
487 : /* Parse priority for the NVMe-oF transport connection */
488 0 : if (ctx->req.priority) {
489 0 : trid.priority = spdk_strtol(ctx->req.priority, 10);
490 : }
491 :
492 : /* Parse subnqn */
493 0 : if (ctx->req.subnqn) {
494 0 : maxlen = sizeof(trid.subnqn);
495 0 : len = strnlen(ctx->req.subnqn, maxlen);
496 0 : if (len == maxlen) {
497 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s",
498 : ctx->req.subnqn);
499 0 : goto cleanup;
500 : }
501 0 : memcpy(trid.subnqn, ctx->req.subnqn, len + 1);
502 : }
503 :
504 0 : if (ctx->req.hostnqn) {
505 0 : maxlen = sizeof(ctx->req.drv_opts.hostnqn);
506 0 : len = strnlen(ctx->req.hostnqn, maxlen);
507 0 : if (len == maxlen) {
508 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostnqn too long: %s",
509 : ctx->req.hostnqn);
510 0 : goto cleanup;
511 : }
512 0 : memcpy(ctx->req.drv_opts.hostnqn, ctx->req.hostnqn, len + 1);
513 : }
514 :
515 0 : if (ctx->req.psk) {
516 0 : if (!g_tls_log) {
517 0 : SPDK_NOTICELOG("TLS support is considered experimental\n");
518 0 : g_tls_log = true;
519 : }
520 :
521 0 : rc = snprintf(ctx->req.bdev_opts.psk, sizeof(ctx->req.bdev_opts.psk), "%s", ctx->req.psk);
522 0 : if (rc < 0 || (size_t)rc >= sizeof(ctx->req.bdev_opts.psk)) {
523 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Could not store PSK: %s",
524 : ctx->req.psk);
525 0 : goto cleanup;
526 : }
527 : }
528 :
529 0 : if (ctx->req.hostaddr) {
530 0 : maxlen = sizeof(ctx->req.drv_opts.src_addr);
531 0 : len = strnlen(ctx->req.hostaddr, maxlen);
532 0 : if (len == maxlen) {
533 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s",
534 : ctx->req.hostaddr);
535 0 : goto cleanup;
536 : }
537 0 : snprintf(ctx->req.drv_opts.src_addr, maxlen, "%s", ctx->req.hostaddr);
538 : }
539 :
540 0 : if (ctx->req.hostsvcid) {
541 0 : maxlen = sizeof(ctx->req.drv_opts.src_svcid);
542 0 : len = strnlen(ctx->req.hostsvcid, maxlen);
543 0 : if (len == maxlen) {
544 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s",
545 : ctx->req.hostsvcid);
546 0 : goto cleanup;
547 : }
548 0 : snprintf(ctx->req.drv_opts.src_svcid, maxlen, "%s", ctx->req.hostsvcid);
549 : }
550 :
551 0 : ctrlr = nvme_ctrlr_get_by_name(ctx->req.name);
552 :
553 0 : if (ctrlr) {
554 : /* This controller already exists. Check what the user wants to do. */
555 0 : if (ctx->req.multipath == BDEV_NVME_MP_MODE_DISABLE) {
556 : /* The user does not want to do any form of multipathing. */
557 0 : spdk_jsonrpc_send_error_response_fmt(request, -EALREADY,
558 : "A controller named %s already exists and multipath is disabled",
559 : ctx->req.name);
560 0 : goto cleanup;
561 : }
562 :
563 0 : assert(ctx->req.multipath == BDEV_NVME_MP_MODE_FAILOVER ||
564 : ctx->req.multipath == BDEV_NVME_MP_MODE_MULTIPATH);
565 :
566 : /* The user wants to add this as a failover path or add this to create multipath. */
567 0 : drv_opts = spdk_nvme_ctrlr_get_opts(ctrlr->ctrlr);
568 0 : ctrlr_trid = spdk_nvme_ctrlr_get_transport_id(ctrlr->ctrlr);
569 :
570 0 : if (strncmp(trid.traddr, ctrlr_trid->traddr, sizeof(trid.traddr)) == 0 &&
571 0 : strncmp(trid.trsvcid, ctrlr_trid->trsvcid, sizeof(trid.trsvcid)) == 0 &&
572 0 : strncmp(ctx->req.drv_opts.src_addr, drv_opts->src_addr, sizeof(drv_opts->src_addr)) == 0 &&
573 0 : strncmp(ctx->req.drv_opts.src_svcid, drv_opts->src_svcid, sizeof(drv_opts->src_svcid)) == 0) {
574 : /* Exactly same network path can't be added a second time */
575 0 : spdk_jsonrpc_send_error_response_fmt(request, -EALREADY,
576 : "A controller named %s already exists with the specified network path",
577 : ctx->req.name);
578 0 : goto cleanup;
579 : }
580 :
581 0 : if (strncmp(trid.subnqn,
582 0 : ctrlr_trid->subnqn,
583 : SPDK_NVMF_NQN_MAX_LEN) != 0) {
584 : /* Different SUBNQN is not allowed when specifying the same controller name. */
585 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
586 : "A controller named %s already exists, but uses a different subnqn (%s)",
587 0 : ctx->req.name, ctrlr_trid->subnqn);
588 0 : goto cleanup;
589 : }
590 :
591 0 : if (strncmp(ctx->req.drv_opts.hostnqn, drv_opts->hostnqn, SPDK_NVMF_NQN_MAX_LEN) != 0) {
592 : /* Different HOSTNQN is not allowed when specifying the same controller name. */
593 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
594 : "A controller named %s already exists, but uses a different hostnqn (%s)",
595 0 : ctx->req.name, drv_opts->hostnqn);
596 0 : goto cleanup;
597 : }
598 :
599 0 : if (ctx->req.bdev_opts.prchk_flags) {
600 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
601 : "A controller named %s already exists. To add a path, do not specify PI options.",
602 : ctx->req.name);
603 0 : goto cleanup;
604 : }
605 :
606 0 : ctx->req.bdev_opts.prchk_flags = ctrlr->opts.prchk_flags;
607 : }
608 :
609 0 : if (ctx->req.multipath == BDEV_NVME_MP_MODE_MULTIPATH) {
610 0 : multipath = true;
611 : }
612 :
613 0 : if (ctx->req.drv_opts.num_io_queues == 0 || ctx->req.drv_opts.num_io_queues > UINT16_MAX + 1) {
614 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL,
615 : "num_io_queues out of bounds, min: %u max: %u",
616 : 1, UINT16_MAX + 1);
617 0 : goto cleanup;
618 : }
619 :
620 0 : ctx->request = request;
621 : /* Should already be zero due to the calloc(), but set explicitly for clarity. */
622 0 : ctx->req.bdev_opts.from_discovery_service = false;
623 0 : ctx->req.bdev_opts.dhchap_key = ctx->req.dhchap_key;
624 0 : ctx->req.bdev_opts.dhchap_ctrlr_key = ctx->req.dhchap_ctrlr_key;
625 0 : rc = bdev_nvme_create(&trid, ctx->req.name, ctx->names, ctx->req.max_bdevs,
626 : rpc_bdev_nvme_attach_controller_done, ctx, &ctx->req.drv_opts,
627 : &ctx->req.bdev_opts, multipath);
628 0 : if (rc) {
629 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
630 0 : goto cleanup;
631 : }
632 :
633 0 : return;
634 :
635 0 : cleanup:
636 0 : free_rpc_bdev_nvme_attach_controller_ctx(ctx);
637 : }
638 0 : SPDK_RPC_REGISTER("bdev_nvme_attach_controller", rpc_bdev_nvme_attach_controller,
639 : SPDK_RPC_RUNTIME)
640 :
641 : static void
642 0 : rpc_dump_nvme_bdev_controller_info(struct nvme_bdev_ctrlr *nbdev_ctrlr, void *ctx)
643 : {
644 0 : struct spdk_json_write_ctx *w = ctx;
645 : struct nvme_ctrlr *nvme_ctrlr;
646 :
647 0 : spdk_json_write_object_begin(w);
648 0 : spdk_json_write_named_string(w, "name", nbdev_ctrlr->name);
649 :
650 0 : spdk_json_write_named_array_begin(w, "ctrlrs");
651 0 : TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
652 0 : nvme_ctrlr_info_json(w, nvme_ctrlr);
653 : }
654 0 : spdk_json_write_array_end(w);
655 0 : spdk_json_write_object_end(w);
656 0 : }
657 :
658 : struct rpc_bdev_nvme_get_controllers {
659 : char *name;
660 : };
661 :
662 : static void
663 0 : free_rpc_bdev_nvme_get_controllers(struct rpc_bdev_nvme_get_controllers *r)
664 : {
665 0 : free(r->name);
666 0 : }
667 :
668 : static const struct spdk_json_object_decoder rpc_bdev_nvme_get_controllers_decoders[] = {
669 : {"name", offsetof(struct rpc_bdev_nvme_get_controllers, name), spdk_json_decode_string, true},
670 : };
671 :
672 : static void
673 0 : rpc_bdev_nvme_get_controllers(struct spdk_jsonrpc_request *request,
674 : const struct spdk_json_val *params)
675 : {
676 0 : struct rpc_bdev_nvme_get_controllers req = {};
677 : struct spdk_json_write_ctx *w;
678 0 : struct nvme_bdev_ctrlr *nbdev_ctrlr = NULL;
679 :
680 0 : if (params && spdk_json_decode_object(params, rpc_bdev_nvme_get_controllers_decoders,
681 : SPDK_COUNTOF(rpc_bdev_nvme_get_controllers_decoders),
682 : &req)) {
683 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
684 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
685 : "spdk_json_decode_object failed");
686 0 : goto cleanup;
687 : }
688 :
689 0 : if (req.name) {
690 0 : nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(req.name);
691 0 : if (nbdev_ctrlr == NULL) {
692 0 : SPDK_ERRLOG("ctrlr '%s' does not exist\n", req.name);
693 0 : spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Controller %s does not exist", req.name);
694 0 : goto cleanup;
695 : }
696 : }
697 :
698 0 : w = spdk_jsonrpc_begin_result(request);
699 0 : spdk_json_write_array_begin(w);
700 :
701 0 : if (nbdev_ctrlr != NULL) {
702 0 : rpc_dump_nvme_bdev_controller_info(nbdev_ctrlr, w);
703 : } else {
704 0 : nvme_bdev_ctrlr_for_each(rpc_dump_nvme_bdev_controller_info, w);
705 : }
706 :
707 0 : spdk_json_write_array_end(w);
708 :
709 0 : spdk_jsonrpc_end_result(request, w);
710 :
711 0 : cleanup:
712 0 : free_rpc_bdev_nvme_get_controllers(&req);
713 0 : }
714 0 : SPDK_RPC_REGISTER("bdev_nvme_get_controllers", rpc_bdev_nvme_get_controllers, SPDK_RPC_RUNTIME)
715 :
716 : struct rpc_bdev_nvme_detach_controller {
717 : char *name;
718 : char *trtype;
719 : char *adrfam;
720 : char *traddr;
721 : char *trsvcid;
722 : char *subnqn;
723 : char *hostaddr;
724 : char *hostsvcid;
725 : };
726 :
727 : static void
728 0 : free_rpc_bdev_nvme_detach_controller(struct rpc_bdev_nvme_detach_controller *req)
729 : {
730 0 : free(req->name);
731 0 : free(req->trtype);
732 0 : free(req->adrfam);
733 0 : free(req->traddr);
734 0 : free(req->trsvcid);
735 0 : free(req->subnqn);
736 0 : free(req->hostaddr);
737 0 : free(req->hostsvcid);
738 0 : }
739 :
740 : static const struct spdk_json_object_decoder rpc_bdev_nvme_detach_controller_decoders[] = {
741 : {"name", offsetof(struct rpc_bdev_nvme_detach_controller, name), spdk_json_decode_string},
742 : {"trtype", offsetof(struct rpc_bdev_nvme_detach_controller, trtype), spdk_json_decode_string, true},
743 : {"traddr", offsetof(struct rpc_bdev_nvme_detach_controller, traddr), spdk_json_decode_string, true},
744 : {"adrfam", offsetof(struct rpc_bdev_nvme_detach_controller, adrfam), spdk_json_decode_string, true},
745 : {"trsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, trsvcid), spdk_json_decode_string, true},
746 : {"subnqn", offsetof(struct rpc_bdev_nvme_detach_controller, subnqn), spdk_json_decode_string, true},
747 : {"hostaddr", offsetof(struct rpc_bdev_nvme_detach_controller, hostaddr), spdk_json_decode_string, true},
748 : {"hostsvcid", offsetof(struct rpc_bdev_nvme_detach_controller, hostsvcid), spdk_json_decode_string, true},
749 : };
750 :
751 : static void
752 0 : rpc_bdev_nvme_detach_controller_done(void *arg, int rc)
753 : {
754 0 : struct spdk_jsonrpc_request *request = arg;
755 :
756 0 : if (rc == 0) {
757 0 : spdk_jsonrpc_send_bool_response(request, true);
758 : } else {
759 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
760 : }
761 0 : }
762 :
763 : static void
764 0 : rpc_bdev_nvme_detach_controller(struct spdk_jsonrpc_request *request,
765 : const struct spdk_json_val *params)
766 : {
767 0 : struct rpc_bdev_nvme_detach_controller req = {NULL};
768 0 : struct nvme_path_id path = {};
769 : size_t len, maxlen;
770 0 : int rc = 0;
771 :
772 0 : if (spdk_json_decode_object(params, rpc_bdev_nvme_detach_controller_decoders,
773 : SPDK_COUNTOF(rpc_bdev_nvme_detach_controller_decoders),
774 : &req)) {
775 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
776 : "spdk_json_decode_object failed");
777 0 : goto cleanup;
778 : }
779 :
780 0 : if (req.trtype != NULL) {
781 0 : rc = spdk_nvme_transport_id_populate_trstring(&path.trid, req.trtype);
782 0 : if (rc < 0) {
783 0 : SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype);
784 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
785 : req.trtype);
786 0 : goto cleanup;
787 : }
788 :
789 0 : rc = spdk_nvme_transport_id_parse_trtype(&path.trid.trtype, req.trtype);
790 0 : if (rc < 0) {
791 0 : SPDK_ERRLOG("Failed to parse trtype: %s\n", req.trtype);
792 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
793 : req.trtype);
794 0 : goto cleanup;
795 : }
796 : }
797 :
798 0 : if (req.traddr != NULL) {
799 0 : maxlen = sizeof(path.trid.traddr);
800 0 : len = strnlen(req.traddr, maxlen);
801 0 : if (len == maxlen) {
802 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s",
803 : req.traddr);
804 0 : goto cleanup;
805 : }
806 0 : memcpy(path.trid.traddr, req.traddr, len + 1);
807 : }
808 :
809 0 : if (req.adrfam != NULL) {
810 0 : rc = spdk_nvme_transport_id_parse_adrfam(&path.trid.adrfam, req.adrfam);
811 0 : if (rc < 0) {
812 0 : SPDK_ERRLOG("Failed to parse adrfam: %s\n", req.adrfam);
813 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s",
814 : req.adrfam);
815 0 : goto cleanup;
816 : }
817 : }
818 :
819 0 : if (req.trsvcid != NULL) {
820 0 : maxlen = sizeof(path.trid.trsvcid);
821 0 : len = strnlen(req.trsvcid, maxlen);
822 0 : if (len == maxlen) {
823 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s",
824 : req.trsvcid);
825 0 : goto cleanup;
826 : }
827 0 : memcpy(path.trid.trsvcid, req.trsvcid, len + 1);
828 : }
829 :
830 : /* Parse subnqn */
831 0 : if (req.subnqn != NULL) {
832 0 : maxlen = sizeof(path.trid.subnqn);
833 0 : len = strnlen(req.subnqn, maxlen);
834 0 : if (len == maxlen) {
835 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "subnqn too long: %s",
836 : req.subnqn);
837 0 : goto cleanup;
838 : }
839 0 : memcpy(path.trid.subnqn, req.subnqn, len + 1);
840 : }
841 :
842 0 : if (req.hostaddr) {
843 0 : maxlen = sizeof(path.hostid.hostaddr);
844 0 : len = strnlen(req.hostaddr, maxlen);
845 0 : if (len == maxlen) {
846 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostaddr too long: %s",
847 : req.hostaddr);
848 0 : goto cleanup;
849 : }
850 0 : snprintf(path.hostid.hostaddr, maxlen, "%s", req.hostaddr);
851 : }
852 :
853 0 : if (req.hostsvcid) {
854 0 : maxlen = sizeof(path.hostid.hostsvcid);
855 0 : len = strnlen(req.hostsvcid, maxlen);
856 0 : if (len == maxlen) {
857 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "hostsvcid too long: %s",
858 : req.hostsvcid);
859 0 : goto cleanup;
860 : }
861 0 : snprintf(path.hostid.hostsvcid, maxlen, "%s", req.hostsvcid);
862 : }
863 :
864 0 : rc = bdev_nvme_delete(req.name, &path, rpc_bdev_nvme_detach_controller_done, request);
865 :
866 0 : if (rc != 0) {
867 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
868 : }
869 :
870 0 : cleanup:
871 0 : free_rpc_bdev_nvme_detach_controller(&req);
872 0 : }
873 0 : SPDK_RPC_REGISTER("bdev_nvme_detach_controller", rpc_bdev_nvme_detach_controller,
874 : SPDK_RPC_RUNTIME)
875 :
876 : struct rpc_apply_firmware {
877 : char *filename;
878 : char *bdev_name;
879 : };
880 :
881 : static void
882 0 : free_rpc_apply_firmware(struct rpc_apply_firmware *req)
883 : {
884 0 : free(req->filename);
885 0 : free(req->bdev_name);
886 0 : }
887 :
888 : static const struct spdk_json_object_decoder rpc_apply_firmware_decoders[] = {
889 : {"filename", offsetof(struct rpc_apply_firmware, filename), spdk_json_decode_string},
890 : {"bdev_name", offsetof(struct rpc_apply_firmware, bdev_name), spdk_json_decode_string},
891 : };
892 :
893 : struct firmware_update_info {
894 : void *fw_image;
895 : void *p;
896 : unsigned int size;
897 : unsigned int size_remaining;
898 : unsigned int offset;
899 : unsigned int transfer;
900 : bool success;
901 :
902 : struct spdk_bdev_desc *desc;
903 : struct spdk_io_channel *ch;
904 : struct spdk_thread *orig_thread;
905 : struct spdk_jsonrpc_request *request;
906 : struct spdk_nvme_ctrlr *ctrlr;
907 : struct rpc_apply_firmware req;
908 : };
909 :
910 : static void
911 0 : apply_firmware_cleanup(struct firmware_update_info *firm_ctx)
912 : {
913 0 : assert(firm_ctx != NULL);
914 0 : assert(firm_ctx->orig_thread == spdk_get_thread());
915 :
916 0 : if (firm_ctx->fw_image) {
917 0 : spdk_free(firm_ctx->fw_image);
918 : }
919 :
920 0 : free_rpc_apply_firmware(&firm_ctx->req);
921 :
922 0 : if (firm_ctx->ch) {
923 0 : spdk_put_io_channel(firm_ctx->ch);
924 : }
925 :
926 0 : if (firm_ctx->desc) {
927 0 : spdk_bdev_close(firm_ctx->desc);
928 : }
929 :
930 0 : free(firm_ctx);
931 0 : }
932 :
933 : static void
934 0 : _apply_firmware_complete_reset(void *ctx)
935 : {
936 : struct spdk_json_write_ctx *w;
937 0 : struct firmware_update_info *firm_ctx = ctx;
938 :
939 0 : assert(firm_ctx->orig_thread == spdk_get_thread());
940 :
941 0 : if (!firm_ctx->success) {
942 0 : spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
943 : "firmware commit failed.");
944 0 : apply_firmware_cleanup(firm_ctx);
945 0 : return;
946 : }
947 :
948 0 : if (spdk_nvme_ctrlr_reset(firm_ctx->ctrlr) != 0) {
949 0 : spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
950 : "Controller reset failed.");
951 0 : apply_firmware_cleanup(firm_ctx);
952 0 : return;
953 : }
954 :
955 0 : w = spdk_jsonrpc_begin_result(firm_ctx->request);
956 0 : spdk_json_write_string(w, "firmware commit succeeded. Controller reset in progress.");
957 0 : spdk_jsonrpc_end_result(firm_ctx->request, w);
958 0 : apply_firmware_cleanup(firm_ctx);
959 : }
960 :
961 : static void
962 0 : apply_firmware_complete_reset(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
963 : {
964 0 : struct firmware_update_info *firm_ctx = cb_arg;
965 :
966 0 : spdk_bdev_free_io(bdev_io);
967 :
968 0 : firm_ctx->success = success;
969 :
970 0 : spdk_thread_exec_msg(firm_ctx->orig_thread, _apply_firmware_complete_reset, firm_ctx);
971 0 : }
972 :
973 : static void apply_firmware_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg);
974 :
975 : static void
976 0 : _apply_firmware_complete(void *ctx)
977 : {
978 0 : struct spdk_nvme_cmd cmd = {};
979 0 : struct spdk_nvme_fw_commit fw_commit;
980 0 : int slot = 0;
981 : int rc;
982 0 : struct firmware_update_info *firm_ctx = ctx;
983 0 : enum spdk_nvme_fw_commit_action commit_action = SPDK_NVME_FW_COMMIT_REPLACE_AND_ENABLE_IMG;
984 :
985 0 : assert(firm_ctx->orig_thread == spdk_get_thread());
986 :
987 0 : if (!firm_ctx->success) {
988 0 : spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
989 : "firmware download failed .");
990 0 : apply_firmware_cleanup(firm_ctx);
991 0 : return;
992 : }
993 :
994 0 : firm_ctx->p += firm_ctx->transfer;
995 0 : firm_ctx->offset += firm_ctx->transfer;
996 0 : firm_ctx->size_remaining -= firm_ctx->transfer;
997 :
998 0 : switch (firm_ctx->size_remaining) {
999 0 : case 0:
1000 : /* firmware download completed. Commit firmware */
1001 0 : memset(&fw_commit, 0, sizeof(struct spdk_nvme_fw_commit));
1002 0 : fw_commit.fs = slot;
1003 0 : fw_commit.ca = commit_action;
1004 :
1005 0 : cmd.opc = SPDK_NVME_OPC_FIRMWARE_COMMIT;
1006 0 : memcpy(&cmd.cdw10, &fw_commit, sizeof(uint32_t));
1007 0 : rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, NULL, 0,
1008 : apply_firmware_complete_reset, firm_ctx);
1009 0 : if (rc) {
1010 0 : spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1011 : "firmware commit failed.");
1012 0 : apply_firmware_cleanup(firm_ctx);
1013 0 : return;
1014 : }
1015 0 : break;
1016 0 : default:
1017 0 : firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096);
1018 0 : cmd.opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
1019 :
1020 0 : cmd.cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer);
1021 0 : cmd.cdw11 = firm_ctx->offset >> 2;
1022 0 : rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, firm_ctx->p,
1023 0 : firm_ctx->transfer, apply_firmware_complete, firm_ctx);
1024 0 : if (rc) {
1025 0 : spdk_jsonrpc_send_error_response(firm_ctx->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1026 : "firmware download failed.");
1027 0 : apply_firmware_cleanup(firm_ctx);
1028 0 : return;
1029 : }
1030 0 : break;
1031 : }
1032 : }
1033 :
1034 : static void
1035 0 : apply_firmware_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
1036 : {
1037 0 : struct firmware_update_info *firm_ctx = cb_arg;
1038 :
1039 0 : spdk_bdev_free_io(bdev_io);
1040 :
1041 0 : firm_ctx->success = success;
1042 :
1043 0 : spdk_thread_exec_msg(firm_ctx->orig_thread, _apply_firmware_complete, firm_ctx);
1044 0 : }
1045 :
1046 : static void
1047 0 : apply_firmware_open_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx)
1048 : {
1049 0 : }
1050 :
1051 : static void
1052 0 : rpc_bdev_nvme_apply_firmware(struct spdk_jsonrpc_request *request,
1053 : const struct spdk_json_val *params)
1054 : {
1055 : int rc;
1056 0 : int fd = -1;
1057 0 : struct stat fw_stat;
1058 : struct spdk_bdev *bdev;
1059 0 : struct spdk_nvme_cmd cmd = {};
1060 : struct firmware_update_info *firm_ctx;
1061 :
1062 0 : firm_ctx = calloc(1, sizeof(struct firmware_update_info));
1063 0 : if (!firm_ctx) {
1064 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1065 : "Memory allocation error.");
1066 0 : return;
1067 : }
1068 0 : firm_ctx->fw_image = NULL;
1069 0 : firm_ctx->request = request;
1070 0 : firm_ctx->orig_thread = spdk_get_thread();
1071 :
1072 0 : if (spdk_json_decode_object(params, rpc_apply_firmware_decoders,
1073 0 : SPDK_COUNTOF(rpc_apply_firmware_decoders), &firm_ctx->req)) {
1074 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1075 : "spdk_json_decode_object failed.");
1076 0 : goto err;
1077 : }
1078 :
1079 0 : if (spdk_bdev_open_ext(firm_ctx->req.bdev_name, true, apply_firmware_open_cb, NULL,
1080 : &firm_ctx->desc) != 0) {
1081 0 : spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1082 : "bdev %s could not be opened",
1083 : firm_ctx->req.bdev_name);
1084 0 : goto err;
1085 : }
1086 0 : bdev = spdk_bdev_desc_get_bdev(firm_ctx->desc);
1087 :
1088 0 : if ((firm_ctx->ctrlr = bdev_nvme_get_ctrlr(bdev)) == NULL) {
1089 0 : spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1090 : "Controller information for %s were not found.",
1091 : firm_ctx->req.bdev_name);
1092 0 : goto err;
1093 : }
1094 :
1095 0 : firm_ctx->ch = spdk_bdev_get_io_channel(firm_ctx->desc);
1096 0 : if (!firm_ctx->ch) {
1097 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1098 : "No channels were found.");
1099 0 : goto err;
1100 : }
1101 :
1102 0 : fd = open(firm_ctx->req.filename, O_RDONLY);
1103 0 : if (fd < 0) {
1104 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1105 : "open file failed.");
1106 0 : goto err;
1107 : }
1108 :
1109 0 : rc = fstat(fd, &fw_stat);
1110 0 : if (rc < 0) {
1111 0 : close(fd);
1112 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1113 : "fstat failed.");
1114 0 : goto err;
1115 : }
1116 :
1117 0 : firm_ctx->size = fw_stat.st_size;
1118 0 : if (fw_stat.st_size % 4) {
1119 0 : close(fd);
1120 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1121 : "Firmware image size is not multiple of 4.");
1122 0 : goto err;
1123 : }
1124 :
1125 0 : firm_ctx->fw_image = spdk_zmalloc(firm_ctx->size, 4096, NULL,
1126 : SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
1127 0 : if (!firm_ctx->fw_image) {
1128 0 : close(fd);
1129 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1130 : "Memory allocation error.");
1131 0 : goto err;
1132 : }
1133 0 : firm_ctx->p = firm_ctx->fw_image;
1134 :
1135 0 : if (read(fd, firm_ctx->p, firm_ctx->size) != ((ssize_t)(firm_ctx->size))) {
1136 0 : close(fd);
1137 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1138 : "Read firmware image failed!");
1139 0 : goto err;
1140 : }
1141 0 : close(fd);
1142 :
1143 0 : firm_ctx->offset = 0;
1144 0 : firm_ctx->size_remaining = firm_ctx->size;
1145 0 : firm_ctx->transfer = spdk_min(firm_ctx->size_remaining, 4096);
1146 :
1147 0 : cmd.opc = SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD;
1148 0 : cmd.cdw10 = spdk_nvme_bytes_to_numd(firm_ctx->transfer);
1149 0 : cmd.cdw11 = firm_ctx->offset >> 2;
1150 :
1151 0 : rc = spdk_bdev_nvme_admin_passthru(firm_ctx->desc, firm_ctx->ch, &cmd, firm_ctx->p,
1152 0 : firm_ctx->transfer, apply_firmware_complete, firm_ctx);
1153 0 : if (rc == 0) {
1154 : /* normal return here. */
1155 0 : return;
1156 : }
1157 :
1158 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1159 : "Read firmware image failed!");
1160 0 : err:
1161 0 : apply_firmware_cleanup(firm_ctx);
1162 : }
1163 0 : SPDK_RPC_REGISTER("bdev_nvme_apply_firmware", rpc_bdev_nvme_apply_firmware, SPDK_RPC_RUNTIME)
1164 :
1165 : struct rpc_bdev_nvme_transport_stat_ctx {
1166 : struct spdk_jsonrpc_request *request;
1167 : struct spdk_json_write_ctx *w;
1168 : };
1169 :
1170 : static void
1171 0 : rpc_bdev_nvme_rdma_stats(struct spdk_json_write_ctx *w,
1172 : struct spdk_nvme_transport_poll_group_stat *stat)
1173 : {
1174 : struct spdk_nvme_rdma_device_stat *device_stats;
1175 : uint32_t i;
1176 :
1177 0 : spdk_json_write_named_array_begin(w, "devices");
1178 :
1179 0 : for (i = 0; i < stat->rdma.num_devices; i++) {
1180 0 : device_stats = &stat->rdma.device_stats[i];
1181 0 : spdk_json_write_object_begin(w);
1182 0 : spdk_json_write_named_string(w, "dev_name", device_stats->name);
1183 0 : spdk_json_write_named_uint64(w, "polls", device_stats->polls);
1184 0 : spdk_json_write_named_uint64(w, "idle_polls", device_stats->idle_polls);
1185 0 : spdk_json_write_named_uint64(w, "completions", device_stats->completions);
1186 0 : spdk_json_write_named_uint64(w, "queued_requests", device_stats->queued_requests);
1187 0 : spdk_json_write_named_uint64(w, "total_send_wrs", device_stats->total_send_wrs);
1188 0 : spdk_json_write_named_uint64(w, "send_doorbell_updates", device_stats->send_doorbell_updates);
1189 0 : spdk_json_write_named_uint64(w, "total_recv_wrs", device_stats->total_recv_wrs);
1190 0 : spdk_json_write_named_uint64(w, "recv_doorbell_updates", device_stats->recv_doorbell_updates);
1191 0 : spdk_json_write_object_end(w);
1192 : }
1193 0 : spdk_json_write_array_end(w);
1194 0 : }
1195 :
1196 : static void
1197 0 : rpc_bdev_nvme_pcie_stats(struct spdk_json_write_ctx *w,
1198 : struct spdk_nvme_transport_poll_group_stat *stat)
1199 : {
1200 0 : spdk_json_write_named_uint64(w, "polls", stat->pcie.polls);
1201 0 : spdk_json_write_named_uint64(w, "idle_polls", stat->pcie.idle_polls);
1202 0 : spdk_json_write_named_uint64(w, "completions", stat->pcie.completions);
1203 0 : spdk_json_write_named_uint64(w, "cq_mmio_doorbell_updates", stat->pcie.cq_mmio_doorbell_updates);
1204 0 : spdk_json_write_named_uint64(w, "cq_shadow_doorbell_updates",
1205 : stat->pcie.cq_shadow_doorbell_updates);
1206 0 : spdk_json_write_named_uint64(w, "queued_requests", stat->pcie.queued_requests);
1207 0 : spdk_json_write_named_uint64(w, "submitted_requests", stat->pcie.submitted_requests);
1208 0 : spdk_json_write_named_uint64(w, "sq_mmio_doorbell_updates", stat->pcie.sq_mmio_doorbell_updates);
1209 0 : spdk_json_write_named_uint64(w, "sq_shadow_doorbell_updates",
1210 : stat->pcie.sq_shadow_doorbell_updates);
1211 0 : }
1212 :
1213 : static void
1214 0 : rpc_bdev_nvme_tcp_stats(struct spdk_json_write_ctx *w,
1215 : struct spdk_nvme_transport_poll_group_stat *stat)
1216 : {
1217 0 : spdk_json_write_named_uint64(w, "polls", stat->tcp.polls);
1218 0 : spdk_json_write_named_uint64(w, "idle_polls", stat->tcp.idle_polls);
1219 0 : spdk_json_write_named_uint64(w, "socket_completions", stat->tcp.socket_completions);
1220 0 : spdk_json_write_named_uint64(w, "nvme_completions", stat->tcp.nvme_completions);
1221 0 : spdk_json_write_named_uint64(w, "queued_requests", stat->tcp.queued_requests);
1222 0 : spdk_json_write_named_uint64(w, "submitted_requests", stat->tcp.submitted_requests);
1223 0 : }
1224 :
1225 : static void
1226 0 : rpc_bdev_nvme_stats_per_channel(struct spdk_io_channel_iter *i)
1227 : {
1228 : struct rpc_bdev_nvme_transport_stat_ctx *ctx;
1229 : struct spdk_io_channel *ch;
1230 : struct nvme_poll_group *group;
1231 0 : struct spdk_nvme_poll_group_stat *stat;
1232 : struct spdk_nvme_transport_poll_group_stat *tr_stat;
1233 : uint32_t j;
1234 : int rc;
1235 :
1236 0 : ctx = spdk_io_channel_iter_get_ctx(i);
1237 0 : ch = spdk_io_channel_iter_get_channel(i);
1238 0 : group = spdk_io_channel_get_ctx(ch);
1239 :
1240 0 : rc = spdk_nvme_poll_group_get_stats(group->group, &stat);
1241 0 : if (rc) {
1242 0 : spdk_for_each_channel_continue(i, rc);
1243 0 : return;
1244 : }
1245 :
1246 0 : spdk_json_write_object_begin(ctx->w);
1247 0 : spdk_json_write_named_string(ctx->w, "thread", spdk_thread_get_name(spdk_get_thread()));
1248 0 : spdk_json_write_named_array_begin(ctx->w, "transports");
1249 :
1250 0 : for (j = 0; j < stat->num_transports; j++) {
1251 0 : tr_stat = stat->transport_stat[j];
1252 0 : spdk_json_write_object_begin(ctx->w);
1253 0 : spdk_json_write_named_string(ctx->w, "trname", spdk_nvme_transport_id_trtype_str(tr_stat->trtype));
1254 :
1255 0 : switch (stat->transport_stat[j]->trtype) {
1256 0 : case SPDK_NVME_TRANSPORT_RDMA:
1257 0 : rpc_bdev_nvme_rdma_stats(ctx->w, tr_stat);
1258 0 : break;
1259 0 : case SPDK_NVME_TRANSPORT_PCIE:
1260 : case SPDK_NVME_TRANSPORT_VFIOUSER:
1261 0 : rpc_bdev_nvme_pcie_stats(ctx->w, tr_stat);
1262 0 : break;
1263 0 : case SPDK_NVME_TRANSPORT_TCP:
1264 0 : rpc_bdev_nvme_tcp_stats(ctx->w, tr_stat);
1265 0 : break;
1266 0 : default:
1267 0 : SPDK_WARNLOG("Can't handle trtype %d %s\n", tr_stat->trtype,
1268 : spdk_nvme_transport_id_trtype_str(tr_stat->trtype));
1269 : }
1270 0 : spdk_json_write_object_end(ctx->w);
1271 : }
1272 : /* transports array */
1273 0 : spdk_json_write_array_end(ctx->w);
1274 0 : spdk_json_write_object_end(ctx->w);
1275 :
1276 0 : spdk_nvme_poll_group_free_stats(group->group, stat);
1277 0 : spdk_for_each_channel_continue(i, 0);
1278 : }
1279 :
1280 : static void
1281 0 : rpc_bdev_nvme_stats_done(struct spdk_io_channel_iter *i, int status)
1282 : {
1283 0 : struct rpc_bdev_nvme_transport_stat_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1284 :
1285 0 : spdk_json_write_array_end(ctx->w);
1286 0 : spdk_json_write_object_end(ctx->w);
1287 0 : spdk_jsonrpc_end_result(ctx->request, ctx->w);
1288 0 : free(ctx);
1289 0 : }
1290 :
1291 : static void
1292 0 : rpc_bdev_nvme_get_transport_statistics(struct spdk_jsonrpc_request *request,
1293 : const struct spdk_json_val *params)
1294 : {
1295 : struct rpc_bdev_nvme_transport_stat_ctx *ctx;
1296 :
1297 0 : if (params) {
1298 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
1299 : "'bdev_nvme_get_transport_statistics' requires no arguments");
1300 0 : return;
1301 : }
1302 :
1303 0 : ctx = calloc(1, sizeof(*ctx));
1304 0 : if (!ctx) {
1305 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1306 : "Memory allocation error");
1307 0 : return;
1308 : }
1309 0 : ctx->request = request;
1310 0 : ctx->w = spdk_jsonrpc_begin_result(ctx->request);
1311 0 : spdk_json_write_object_begin(ctx->w);
1312 0 : spdk_json_write_named_array_begin(ctx->w, "poll_groups");
1313 :
1314 0 : spdk_for_each_channel(&g_nvme_bdev_ctrlrs,
1315 : rpc_bdev_nvme_stats_per_channel,
1316 : ctx,
1317 : rpc_bdev_nvme_stats_done);
1318 : }
1319 0 : SPDK_RPC_REGISTER("bdev_nvme_get_transport_statistics", rpc_bdev_nvme_get_transport_statistics,
1320 : SPDK_RPC_RUNTIME)
1321 :
1322 : struct rpc_bdev_nvme_controller_op_req {
1323 : char *name;
1324 : uint16_t cntlid;
1325 : };
1326 :
1327 : static void
1328 0 : free_rpc_bdev_nvme_controller_op_req(struct rpc_bdev_nvme_controller_op_req *r)
1329 : {
1330 0 : free(r->name);
1331 0 : }
1332 :
1333 : static const struct spdk_json_object_decoder rpc_bdev_nvme_controller_op_req_decoders[] = {
1334 : {"name", offsetof(struct rpc_bdev_nvme_controller_op_req, name), spdk_json_decode_string},
1335 : {"cntlid", offsetof(struct rpc_bdev_nvme_controller_op_req, cntlid), spdk_json_decode_uint16, true},
1336 : };
1337 :
1338 : static void
1339 0 : rpc_bdev_nvme_controller_op_cb(void *cb_arg, int rc)
1340 : {
1341 0 : struct spdk_jsonrpc_request *request = cb_arg;
1342 :
1343 0 : if (rc == 0) {
1344 0 : spdk_jsonrpc_send_bool_response(request, true);
1345 : } else {
1346 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
1347 : }
1348 0 : }
1349 :
1350 : static void
1351 0 : rpc_bdev_nvme_controller_op(struct spdk_jsonrpc_request *request,
1352 : const struct spdk_json_val *params,
1353 : enum nvme_ctrlr_op op)
1354 : {
1355 0 : struct rpc_bdev_nvme_controller_op_req req = {NULL};
1356 : struct nvme_bdev_ctrlr *nbdev_ctrlr;
1357 : struct nvme_ctrlr *nvme_ctrlr;
1358 :
1359 0 : if (spdk_json_decode_object(params, rpc_bdev_nvme_controller_op_req_decoders,
1360 : SPDK_COUNTOF(rpc_bdev_nvme_controller_op_req_decoders),
1361 : &req)) {
1362 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
1363 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(EINVAL));
1364 0 : goto exit;
1365 : }
1366 :
1367 0 : nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(req.name);
1368 0 : if (nbdev_ctrlr == NULL) {
1369 0 : SPDK_ERRLOG("Failed at NVMe bdev controller lookup\n");
1370 0 : spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
1371 0 : goto exit;
1372 : }
1373 :
1374 0 : if (req.cntlid == 0) {
1375 0 : nvme_bdev_ctrlr_op_rpc(nbdev_ctrlr, op, rpc_bdev_nvme_controller_op_cb, request);
1376 : } else {
1377 0 : nvme_ctrlr = nvme_bdev_ctrlr_get_ctrlr_by_id(nbdev_ctrlr, req.cntlid);
1378 0 : if (nvme_ctrlr == NULL) {
1379 0 : SPDK_ERRLOG("Failed at NVMe controller lookup\n");
1380 0 : spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
1381 0 : goto exit;
1382 : }
1383 0 : nvme_ctrlr_op_rpc(nvme_ctrlr, op, rpc_bdev_nvme_controller_op_cb, request);
1384 : }
1385 :
1386 0 : exit:
1387 0 : free_rpc_bdev_nvme_controller_op_req(&req);
1388 0 : }
1389 :
1390 : static void
1391 0 : rpc_bdev_nvme_reset_controller(struct spdk_jsonrpc_request *request,
1392 : const struct spdk_json_val *params)
1393 : {
1394 0 : rpc_bdev_nvme_controller_op(request, params, NVME_CTRLR_OP_RESET);
1395 0 : }
1396 0 : SPDK_RPC_REGISTER("bdev_nvme_reset_controller", rpc_bdev_nvme_reset_controller, SPDK_RPC_RUNTIME)
1397 :
1398 : static void
1399 0 : rpc_bdev_nvme_enable_controller(struct spdk_jsonrpc_request *request,
1400 : const struct spdk_json_val *params)
1401 : {
1402 0 : rpc_bdev_nvme_controller_op(request, params, NVME_CTRLR_OP_ENABLE);
1403 0 : }
1404 0 : SPDK_RPC_REGISTER("bdev_nvme_enable_controller", rpc_bdev_nvme_enable_controller, SPDK_RPC_RUNTIME)
1405 :
1406 : static void
1407 0 : rpc_bdev_nvme_disable_controller(struct spdk_jsonrpc_request *request,
1408 : const struct spdk_json_val *params)
1409 : {
1410 0 : rpc_bdev_nvme_controller_op(request, params, NVME_CTRLR_OP_DISABLE);
1411 0 : }
1412 0 : SPDK_RPC_REGISTER("bdev_nvme_disable_controller", rpc_bdev_nvme_disable_controller,
1413 : SPDK_RPC_RUNTIME)
1414 :
1415 : struct rpc_get_controller_health_info {
1416 : char *name;
1417 : };
1418 :
1419 : struct spdk_nvme_health_info_context {
1420 : struct spdk_jsonrpc_request *request;
1421 : struct spdk_nvme_ctrlr *ctrlr;
1422 : struct spdk_nvme_health_information_page health_page;
1423 : };
1424 :
1425 : static void
1426 0 : free_rpc_get_controller_health_info(struct rpc_get_controller_health_info *r)
1427 : {
1428 0 : free(r->name);
1429 0 : }
1430 :
1431 : static const struct spdk_json_object_decoder rpc_get_controller_health_info_decoders[] = {
1432 : {"name", offsetof(struct rpc_get_controller_health_info, name), spdk_json_decode_string, true},
1433 : };
1434 :
1435 : static void
1436 0 : nvme_health_info_cleanup(struct spdk_nvme_health_info_context *context, bool response)
1437 : {
1438 0 : if (response == true) {
1439 0 : spdk_jsonrpc_send_error_response(context->request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1440 : "Internal error.");
1441 : }
1442 :
1443 0 : free(context);
1444 0 : }
1445 :
1446 : static void
1447 0 : get_health_log_page_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl)
1448 : {
1449 : int i;
1450 0 : char buf[128];
1451 0 : struct spdk_nvme_health_info_context *context = cb_arg;
1452 0 : struct spdk_jsonrpc_request *request = context->request;
1453 : struct spdk_json_write_ctx *w;
1454 0 : struct spdk_nvme_ctrlr *ctrlr = context->ctrlr;
1455 0 : const struct spdk_nvme_transport_id *trid = NULL;
1456 0 : const struct spdk_nvme_ctrlr_data *cdata = NULL;
1457 0 : struct spdk_nvme_health_information_page *health_page = NULL;
1458 :
1459 0 : if (spdk_nvme_cpl_is_error(cpl)) {
1460 0 : nvme_health_info_cleanup(context, true);
1461 0 : SPDK_ERRLOG("get log page failed\n");
1462 0 : return;
1463 : }
1464 :
1465 0 : if (ctrlr == NULL) {
1466 0 : nvme_health_info_cleanup(context, true);
1467 0 : SPDK_ERRLOG("ctrlr is NULL\n");
1468 0 : return;
1469 : } else {
1470 0 : trid = spdk_nvme_ctrlr_get_transport_id(ctrlr);
1471 0 : cdata = spdk_nvme_ctrlr_get_data(ctrlr);
1472 0 : health_page = &(context->health_page);
1473 : }
1474 :
1475 0 : w = spdk_jsonrpc_begin_result(request);
1476 :
1477 0 : spdk_json_write_object_begin(w);
1478 0 : snprintf(buf, sizeof(cdata->mn) + 1, "%s", cdata->mn);
1479 0 : spdk_str_trim(buf);
1480 0 : spdk_json_write_named_string(w, "model_number", buf);
1481 0 : snprintf(buf, sizeof(cdata->sn) + 1, "%s", cdata->sn);
1482 0 : spdk_str_trim(buf);
1483 0 : spdk_json_write_named_string(w, "serial_number", buf);
1484 0 : snprintf(buf, sizeof(cdata->fr) + 1, "%s", cdata->fr);
1485 0 : spdk_str_trim(buf);
1486 0 : spdk_json_write_named_string(w, "firmware_revision", buf);
1487 0 : spdk_json_write_named_string(w, "traddr", trid->traddr);
1488 0 : spdk_json_write_named_uint64(w, "critical_warning", health_page->critical_warning.raw);
1489 0 : spdk_json_write_named_uint64(w, "temperature_celsius", health_page->temperature - 273);
1490 0 : spdk_json_write_named_uint64(w, "available_spare_percentage", health_page->available_spare);
1491 0 : spdk_json_write_named_uint64(w, "available_spare_threshold_percentage",
1492 0 : health_page->available_spare_threshold);
1493 0 : spdk_json_write_named_uint64(w, "percentage_used", health_page->percentage_used);
1494 0 : spdk_json_write_named_uint128(w, "data_units_read",
1495 : health_page->data_units_read[0], health_page->data_units_read[1]);
1496 0 : spdk_json_write_named_uint128(w, "data_units_written",
1497 : health_page->data_units_written[0], health_page->data_units_written[1]);
1498 0 : spdk_json_write_named_uint128(w, "host_read_commands",
1499 : health_page->host_read_commands[0], health_page->host_read_commands[1]);
1500 0 : spdk_json_write_named_uint128(w, "host_write_commands",
1501 : health_page->host_write_commands[0], health_page->host_write_commands[1]);
1502 0 : spdk_json_write_named_uint128(w, "controller_busy_time",
1503 : health_page->controller_busy_time[0], health_page->controller_busy_time[1]);
1504 0 : spdk_json_write_named_uint128(w, "power_cycles",
1505 : health_page->power_cycles[0], health_page->power_cycles[1]);
1506 0 : spdk_json_write_named_uint128(w, "power_on_hours",
1507 : health_page->power_on_hours[0], health_page->power_on_hours[1]);
1508 0 : spdk_json_write_named_uint128(w, "unsafe_shutdowns",
1509 : health_page->unsafe_shutdowns[0], health_page->unsafe_shutdowns[1]);
1510 0 : spdk_json_write_named_uint128(w, "media_errors",
1511 : health_page->media_errors[0], health_page->media_errors[1]);
1512 0 : spdk_json_write_named_uint128(w, "num_err_log_entries",
1513 : health_page->num_error_info_log_entries[0], health_page->num_error_info_log_entries[1]);
1514 0 : spdk_json_write_named_uint64(w, "warning_temperature_time_minutes", health_page->warning_temp_time);
1515 0 : spdk_json_write_named_uint64(w, "critical_composite_temperature_time_minutes",
1516 0 : health_page->critical_temp_time);
1517 0 : for (i = 0; i < 8; i++) {
1518 0 : if (health_page->temp_sensor[i] != 0) {
1519 0 : spdk_json_write_named_uint64(w, "temperature_sensor_celsius", health_page->temp_sensor[i] - 273);
1520 : }
1521 : }
1522 0 : spdk_json_write_object_end(w);
1523 :
1524 0 : spdk_jsonrpc_end_result(request, w);
1525 0 : nvme_health_info_cleanup(context, false);
1526 : }
1527 :
1528 : static void
1529 0 : get_health_log_page(struct spdk_nvme_health_info_context *context)
1530 : {
1531 0 : struct spdk_nvme_ctrlr *ctrlr = context->ctrlr;
1532 :
1533 0 : if (spdk_nvme_ctrlr_cmd_get_log_page(ctrlr, SPDK_NVME_LOG_HEALTH_INFORMATION,
1534 : SPDK_NVME_GLOBAL_NS_TAG,
1535 0 : &(context->health_page), sizeof(context->health_page), 0,
1536 : get_health_log_page_completion, context)) {
1537 0 : nvme_health_info_cleanup(context, true);
1538 0 : SPDK_ERRLOG("spdk_nvme_ctrlr_cmd_get_log_page() failed\n");
1539 : }
1540 0 : }
1541 :
1542 : static void
1543 0 : get_temperature_threshold_feature_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl)
1544 : {
1545 0 : struct spdk_nvme_health_info_context *context = cb_arg;
1546 :
1547 0 : if (spdk_nvme_cpl_is_error(cpl)) {
1548 0 : nvme_health_info_cleanup(context, true);
1549 0 : SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed in completion\n");
1550 : } else {
1551 0 : get_health_log_page(context);
1552 : }
1553 0 : }
1554 :
1555 : static int
1556 0 : get_temperature_threshold_feature(struct spdk_nvme_health_info_context *context)
1557 : {
1558 0 : struct spdk_nvme_cmd cmd = {};
1559 :
1560 0 : cmd.opc = SPDK_NVME_OPC_GET_FEATURES;
1561 0 : cmd.cdw10 = SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD;
1562 :
1563 0 : return spdk_nvme_ctrlr_cmd_admin_raw(context->ctrlr, &cmd, NULL, 0,
1564 : get_temperature_threshold_feature_completion, context);
1565 : }
1566 :
1567 : static void
1568 0 : get_controller_health_info(struct spdk_jsonrpc_request *request, struct spdk_nvme_ctrlr *ctrlr)
1569 : {
1570 : struct spdk_nvme_health_info_context *context;
1571 :
1572 0 : context = calloc(1, sizeof(struct spdk_nvme_health_info_context));
1573 0 : if (!context) {
1574 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1575 : "Memory allocation error.");
1576 0 : return;
1577 : }
1578 :
1579 0 : context->request = request;
1580 0 : context->ctrlr = ctrlr;
1581 :
1582 0 : if (get_temperature_threshold_feature(context)) {
1583 0 : nvme_health_info_cleanup(context, true);
1584 0 : SPDK_ERRLOG("feature SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD failed to submit\n");
1585 : }
1586 :
1587 0 : return;
1588 : }
1589 :
1590 : static void
1591 0 : rpc_bdev_nvme_get_controller_health_info(struct spdk_jsonrpc_request *request,
1592 : const struct spdk_json_val *params)
1593 : {
1594 0 : struct rpc_get_controller_health_info req = {};
1595 0 : struct nvme_ctrlr *nvme_ctrlr = NULL;
1596 :
1597 0 : if (!params) {
1598 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1599 : "Missing device name");
1600 :
1601 0 : return;
1602 : }
1603 0 : if (spdk_json_decode_object(params, rpc_get_controller_health_info_decoders,
1604 : SPDK_COUNTOF(rpc_get_controller_health_info_decoders), &req)) {
1605 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
1606 0 : free_rpc_get_controller_health_info(&req);
1607 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1608 : "Invalid parameters");
1609 :
1610 0 : return;
1611 : }
1612 :
1613 0 : nvme_ctrlr = nvme_ctrlr_get_by_name(req.name);
1614 :
1615 0 : if (!nvme_ctrlr) {
1616 0 : SPDK_ERRLOG("nvme ctrlr name '%s' does not exist\n", req.name);
1617 0 : free_rpc_get_controller_health_info(&req);
1618 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1619 : "Device not found");
1620 0 : return;
1621 : }
1622 :
1623 0 : get_controller_health_info(request, nvme_ctrlr->ctrlr);
1624 0 : free_rpc_get_controller_health_info(&req);
1625 :
1626 0 : return;
1627 : }
1628 0 : SPDK_RPC_REGISTER("bdev_nvme_get_controller_health_info",
1629 : rpc_bdev_nvme_get_controller_health_info, SPDK_RPC_RUNTIME)
1630 :
1631 : struct rpc_bdev_nvme_start_discovery {
1632 : char *name;
1633 : char *trtype;
1634 : char *adrfam;
1635 : char *traddr;
1636 : char *trsvcid;
1637 : char *hostnqn;
1638 : bool wait_for_attach;
1639 : uint64_t attach_timeout_ms;
1640 : struct spdk_nvme_ctrlr_opts opts;
1641 : struct nvme_ctrlr_opts bdev_opts;
1642 : };
1643 :
1644 : static void
1645 0 : free_rpc_bdev_nvme_start_discovery(struct rpc_bdev_nvme_start_discovery *req)
1646 : {
1647 0 : free(req->name);
1648 0 : free(req->trtype);
1649 0 : free(req->adrfam);
1650 0 : free(req->traddr);
1651 0 : free(req->trsvcid);
1652 0 : free(req->hostnqn);
1653 0 : }
1654 :
1655 : static const struct spdk_json_object_decoder rpc_bdev_nvme_start_discovery_decoders[] = {
1656 : {"name", offsetof(struct rpc_bdev_nvme_start_discovery, name), spdk_json_decode_string},
1657 : {"trtype", offsetof(struct rpc_bdev_nvme_start_discovery, trtype), spdk_json_decode_string},
1658 : {"traddr", offsetof(struct rpc_bdev_nvme_start_discovery, traddr), spdk_json_decode_string},
1659 : {"adrfam", offsetof(struct rpc_bdev_nvme_start_discovery, adrfam), spdk_json_decode_string, true},
1660 : {"trsvcid", offsetof(struct rpc_bdev_nvme_start_discovery, trsvcid), spdk_json_decode_string, true},
1661 : {"hostnqn", offsetof(struct rpc_bdev_nvme_start_discovery, hostnqn), spdk_json_decode_string, true},
1662 : {"wait_for_attach", offsetof(struct rpc_bdev_nvme_start_discovery, wait_for_attach), spdk_json_decode_bool, true},
1663 : {"attach_timeout_ms", offsetof(struct rpc_bdev_nvme_start_discovery, attach_timeout_ms), spdk_json_decode_uint64, true},
1664 : {"ctrlr_loss_timeout_sec", offsetof(struct rpc_bdev_nvme_start_discovery, bdev_opts.ctrlr_loss_timeout_sec), spdk_json_decode_int32, true},
1665 : {"reconnect_delay_sec", offsetof(struct rpc_bdev_nvme_start_discovery, bdev_opts.reconnect_delay_sec), spdk_json_decode_uint32, true},
1666 : {"fast_io_fail_timeout_sec", offsetof(struct rpc_bdev_nvme_start_discovery, bdev_opts.fast_io_fail_timeout_sec), spdk_json_decode_uint32, true},
1667 : };
1668 :
1669 : struct rpc_bdev_nvme_start_discovery_ctx {
1670 : struct rpc_bdev_nvme_start_discovery req;
1671 : struct spdk_jsonrpc_request *request;
1672 : };
1673 :
1674 : static void
1675 0 : rpc_bdev_nvme_start_discovery_done(void *ctx, int status)
1676 : {
1677 0 : struct spdk_jsonrpc_request *request = ctx;
1678 :
1679 0 : if (status != 0) {
1680 0 : spdk_jsonrpc_send_error_response(request, status, spdk_strerror(-status));
1681 : } else {
1682 0 : spdk_jsonrpc_send_bool_response(request, true);
1683 : }
1684 0 : }
1685 :
1686 : static void
1687 0 : rpc_bdev_nvme_start_discovery(struct spdk_jsonrpc_request *request,
1688 : const struct spdk_json_val *params)
1689 : {
1690 : struct rpc_bdev_nvme_start_discovery_ctx *ctx;
1691 0 : struct spdk_nvme_transport_id trid = {};
1692 : size_t len, maxlen;
1693 : int rc;
1694 : spdk_bdev_nvme_start_discovery_fn cb_fn;
1695 : void *cb_ctx;
1696 :
1697 0 : ctx = calloc(1, sizeof(*ctx));
1698 0 : if (!ctx) {
1699 0 : spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
1700 0 : return;
1701 : }
1702 :
1703 0 : spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.opts, sizeof(ctx->req.opts));
1704 :
1705 0 : if (spdk_json_decode_object(params, rpc_bdev_nvme_start_discovery_decoders,
1706 : SPDK_COUNTOF(rpc_bdev_nvme_start_discovery_decoders),
1707 0 : &ctx->req)) {
1708 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
1709 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1710 : "spdk_json_decode_object failed");
1711 0 : goto cleanup;
1712 : }
1713 :
1714 : /* Parse trstring */
1715 0 : rc = spdk_nvme_transport_id_populate_trstring(&trid, ctx->req.trtype);
1716 0 : if (rc < 0) {
1717 0 : SPDK_ERRLOG("Failed to parse trtype: %s\n", ctx->req.trtype);
1718 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse trtype: %s",
1719 : ctx->req.trtype);
1720 0 : goto cleanup;
1721 : }
1722 :
1723 : /* Parse trtype */
1724 0 : rc = spdk_nvme_transport_id_parse_trtype(&trid.trtype, ctx->req.trtype);
1725 0 : assert(rc == 0);
1726 :
1727 : /* Parse traddr */
1728 0 : maxlen = sizeof(trid.traddr);
1729 0 : len = strnlen(ctx->req.traddr, maxlen);
1730 0 : if (len == maxlen) {
1731 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "traddr too long: %s",
1732 : ctx->req.traddr);
1733 0 : goto cleanup;
1734 : }
1735 0 : memcpy(trid.traddr, ctx->req.traddr, len + 1);
1736 :
1737 : /* Parse adrfam */
1738 0 : if (ctx->req.adrfam) {
1739 0 : rc = spdk_nvme_transport_id_parse_adrfam(&trid.adrfam, ctx->req.adrfam);
1740 0 : if (rc < 0) {
1741 0 : SPDK_ERRLOG("Failed to parse adrfam: %s\n", ctx->req.adrfam);
1742 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "Failed to parse adrfam: %s",
1743 : ctx->req.adrfam);
1744 0 : goto cleanup;
1745 : }
1746 : }
1747 :
1748 : /* Parse trsvcid */
1749 0 : if (ctx->req.trsvcid) {
1750 0 : maxlen = sizeof(trid.trsvcid);
1751 0 : len = strnlen(ctx->req.trsvcid, maxlen);
1752 0 : if (len == maxlen) {
1753 0 : spdk_jsonrpc_send_error_response_fmt(request, -EINVAL, "trsvcid too long: %s",
1754 : ctx->req.trsvcid);
1755 0 : goto cleanup;
1756 : }
1757 0 : memcpy(trid.trsvcid, ctx->req.trsvcid, len + 1);
1758 : }
1759 :
1760 0 : if (ctx->req.hostnqn) {
1761 0 : snprintf(ctx->req.opts.hostnqn, sizeof(ctx->req.opts.hostnqn), "%s",
1762 : ctx->req.hostnqn);
1763 : }
1764 :
1765 0 : if (ctx->req.attach_timeout_ms != 0) {
1766 0 : ctx->req.wait_for_attach = true;
1767 : }
1768 :
1769 0 : ctx->request = request;
1770 0 : cb_fn = ctx->req.wait_for_attach ? rpc_bdev_nvme_start_discovery_done : NULL;
1771 0 : cb_ctx = ctx->req.wait_for_attach ? request : NULL;
1772 0 : rc = bdev_nvme_start_discovery(&trid, ctx->req.name, &ctx->req.opts, &ctx->req.bdev_opts,
1773 : ctx->req.attach_timeout_ms, false, cb_fn, cb_ctx);
1774 0 : if (rc) {
1775 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
1776 0 : } else if (!ctx->req.wait_for_attach) {
1777 0 : rpc_bdev_nvme_start_discovery_done(request, 0);
1778 : }
1779 :
1780 0 : cleanup:
1781 0 : free_rpc_bdev_nvme_start_discovery(&ctx->req);
1782 0 : free(ctx);
1783 : }
1784 0 : SPDK_RPC_REGISTER("bdev_nvme_start_discovery", rpc_bdev_nvme_start_discovery,
1785 : SPDK_RPC_RUNTIME)
1786 :
1787 : struct rpc_bdev_nvme_stop_discovery {
1788 : char *name;
1789 : };
1790 :
1791 : static const struct spdk_json_object_decoder rpc_bdev_nvme_stop_discovery_decoders[] = {
1792 : {"name", offsetof(struct rpc_bdev_nvme_stop_discovery, name), spdk_json_decode_string},
1793 : };
1794 :
1795 : struct rpc_bdev_nvme_stop_discovery_ctx {
1796 : struct rpc_bdev_nvme_stop_discovery req;
1797 : struct spdk_jsonrpc_request *request;
1798 : };
1799 :
1800 : static void
1801 0 : rpc_bdev_nvme_stop_discovery_done(void *cb_ctx)
1802 : {
1803 0 : struct rpc_bdev_nvme_stop_discovery_ctx *ctx = cb_ctx;
1804 :
1805 0 : spdk_jsonrpc_send_bool_response(ctx->request, true);
1806 0 : free(ctx->req.name);
1807 0 : free(ctx);
1808 0 : }
1809 :
1810 : static void
1811 0 : rpc_bdev_nvme_stop_discovery(struct spdk_jsonrpc_request *request,
1812 : const struct spdk_json_val *params)
1813 : {
1814 : struct rpc_bdev_nvme_stop_discovery_ctx *ctx;
1815 : int rc;
1816 :
1817 0 : ctx = calloc(1, sizeof(*ctx));
1818 0 : if (!ctx) {
1819 0 : spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
1820 0 : return;
1821 : }
1822 :
1823 0 : if (spdk_json_decode_object(params, rpc_bdev_nvme_stop_discovery_decoders,
1824 : SPDK_COUNTOF(rpc_bdev_nvme_stop_discovery_decoders),
1825 0 : &ctx->req)) {
1826 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
1827 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1828 : "spdk_json_decode_object failed");
1829 0 : goto cleanup;
1830 : }
1831 :
1832 0 : ctx->request = request;
1833 0 : rc = bdev_nvme_stop_discovery(ctx->req.name, rpc_bdev_nvme_stop_discovery_done, ctx);
1834 0 : if (rc) {
1835 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
1836 0 : goto cleanup;
1837 : }
1838 :
1839 0 : return;
1840 :
1841 0 : cleanup:
1842 0 : free(ctx->req.name);
1843 0 : free(ctx);
1844 : }
1845 0 : SPDK_RPC_REGISTER("bdev_nvme_stop_discovery", rpc_bdev_nvme_stop_discovery,
1846 : SPDK_RPC_RUNTIME)
1847 :
1848 : static void
1849 0 : rpc_bdev_nvme_get_discovery_info(struct spdk_jsonrpc_request *request,
1850 : const struct spdk_json_val *params)
1851 : {
1852 : struct spdk_json_write_ctx *w;
1853 :
1854 0 : w = spdk_jsonrpc_begin_result(request);
1855 0 : bdev_nvme_get_discovery_info(w);
1856 0 : spdk_jsonrpc_end_result(request, w);
1857 0 : }
1858 0 : SPDK_RPC_REGISTER("bdev_nvme_get_discovery_info", rpc_bdev_nvme_get_discovery_info,
1859 : SPDK_RPC_RUNTIME)
1860 :
1861 : enum error_injection_cmd_type {
1862 : NVME_ADMIN_CMD = 1,
1863 : NVME_IO_CMD,
1864 : };
1865 :
1866 : struct rpc_add_error_injection {
1867 : char *name;
1868 : enum error_injection_cmd_type cmd_type;
1869 : uint8_t opc;
1870 : bool do_not_submit;
1871 : uint64_t timeout_in_us;
1872 : uint32_t err_count;
1873 : uint8_t sct;
1874 : uint8_t sc;
1875 : };
1876 :
1877 : static void
1878 0 : free_rpc_add_error_injection(struct rpc_add_error_injection *req)
1879 : {
1880 0 : free(req->name);
1881 0 : }
1882 :
1883 : static int
1884 0 : rpc_error_injection_decode_cmd_type(const struct spdk_json_val *val, void *out)
1885 : {
1886 0 : int *cmd_type = out;
1887 :
1888 0 : if (spdk_json_strequal(val, "admin")) {
1889 0 : *cmd_type = NVME_ADMIN_CMD;
1890 0 : } else if (spdk_json_strequal(val, "io")) {
1891 0 : *cmd_type = NVME_IO_CMD;
1892 : } else {
1893 0 : SPDK_ERRLOG("Invalid parameter value: cmd_type\n");
1894 0 : return -EINVAL;
1895 : }
1896 :
1897 0 : return 0;
1898 : }
1899 :
1900 : static const struct spdk_json_object_decoder rpc_add_error_injection_decoders[] = {
1901 : { "name", offsetof(struct rpc_add_error_injection, name), spdk_json_decode_string },
1902 : { "cmd_type", offsetof(struct rpc_add_error_injection, cmd_type), rpc_error_injection_decode_cmd_type },
1903 : { "opc", offsetof(struct rpc_add_error_injection, opc), spdk_json_decode_uint8 },
1904 : { "do_not_submit", offsetof(struct rpc_add_error_injection, do_not_submit), spdk_json_decode_bool, true },
1905 : { "timeout_in_us", offsetof(struct rpc_add_error_injection, timeout_in_us), spdk_json_decode_uint64, true },
1906 : { "err_count", offsetof(struct rpc_add_error_injection, err_count), spdk_json_decode_uint32, true },
1907 : { "sct", offsetof(struct rpc_add_error_injection, sct), spdk_json_decode_uint8, true},
1908 : { "sc", offsetof(struct rpc_add_error_injection, sc), spdk_json_decode_uint8, true},
1909 : };
1910 :
1911 : struct rpc_add_error_injection_ctx {
1912 : struct spdk_jsonrpc_request *request;
1913 : struct rpc_add_error_injection rpc;
1914 : };
1915 :
1916 : static void
1917 0 : rpc_add_error_injection_done(struct spdk_io_channel_iter *i, int status)
1918 : {
1919 0 : struct rpc_add_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1920 :
1921 0 : if (status) {
1922 0 : spdk_jsonrpc_send_error_response(ctx->request, status,
1923 : "Failed to add the error injection.");
1924 : } else {
1925 0 : spdk_jsonrpc_send_bool_response(ctx->request, true);
1926 : }
1927 :
1928 0 : free_rpc_add_error_injection(&ctx->rpc);
1929 0 : free(ctx);
1930 0 : }
1931 :
1932 : static void
1933 0 : rpc_add_error_injection_per_channel(struct spdk_io_channel_iter *i)
1934 : {
1935 0 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
1936 0 : struct rpc_add_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
1937 0 : struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch);
1938 0 : struct spdk_nvme_qpair *qpair = ctrlr_ch->qpair->qpair;
1939 0 : struct spdk_nvme_ctrlr *ctrlr = ctrlr_ch->qpair->ctrlr->ctrlr;
1940 0 : int rc = 0;
1941 :
1942 0 : if (qpair != NULL) {
1943 0 : rc = spdk_nvme_qpair_add_cmd_error_injection(ctrlr, qpair, ctx->rpc.opc,
1944 0 : ctx->rpc.do_not_submit, ctx->rpc.timeout_in_us, ctx->rpc.err_count,
1945 0 : ctx->rpc.sct, ctx->rpc.sc);
1946 : }
1947 :
1948 0 : spdk_for_each_channel_continue(i, rc);
1949 0 : }
1950 :
1951 : static void
1952 0 : rpc_bdev_nvme_add_error_injection(
1953 : struct spdk_jsonrpc_request *request,
1954 : const struct spdk_json_val *params)
1955 : {
1956 : struct rpc_add_error_injection_ctx *ctx;
1957 : struct nvme_ctrlr *nvme_ctrlr;
1958 : int rc;
1959 :
1960 0 : ctx = calloc(1, sizeof(*ctx));
1961 0 : if (!ctx) {
1962 0 : spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
1963 0 : return;
1964 : }
1965 0 : ctx->rpc.err_count = 1;
1966 0 : ctx->request = request;
1967 :
1968 0 : if (spdk_json_decode_object(params,
1969 : rpc_add_error_injection_decoders,
1970 : SPDK_COUNTOF(rpc_add_error_injection_decoders),
1971 0 : &ctx->rpc)) {
1972 0 : spdk_jsonrpc_send_error_response(request, -EINVAL,
1973 : "Failed to parse the request");
1974 0 : goto cleanup;
1975 : }
1976 :
1977 0 : nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->rpc.name);
1978 0 : if (nvme_ctrlr == NULL) {
1979 0 : SPDK_ERRLOG("No controller with specified name was found.\n");
1980 0 : spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
1981 0 : goto cleanup;
1982 : }
1983 :
1984 0 : if (ctx->rpc.cmd_type == NVME_IO_CMD) {
1985 0 : spdk_for_each_channel(nvme_ctrlr,
1986 : rpc_add_error_injection_per_channel,
1987 : ctx,
1988 : rpc_add_error_injection_done);
1989 :
1990 0 : return;
1991 : } else {
1992 0 : rc = spdk_nvme_qpair_add_cmd_error_injection(nvme_ctrlr->ctrlr, NULL, ctx->rpc.opc,
1993 0 : ctx->rpc.do_not_submit, ctx->rpc.timeout_in_us, ctx->rpc.err_count,
1994 0 : ctx->rpc.sct, ctx->rpc.sc);
1995 0 : if (rc) {
1996 0 : spdk_jsonrpc_send_error_response(request, -rc,
1997 : "Failed to add the error injection");
1998 : } else {
1999 0 : spdk_jsonrpc_send_bool_response(ctx->request, true);
2000 : }
2001 : }
2002 :
2003 0 : cleanup:
2004 0 : free_rpc_add_error_injection(&ctx->rpc);
2005 0 : free(ctx);
2006 : }
2007 0 : SPDK_RPC_REGISTER("bdev_nvme_add_error_injection", rpc_bdev_nvme_add_error_injection,
2008 : SPDK_RPC_RUNTIME)
2009 :
2010 : struct rpc_remove_error_injection {
2011 : char *name;
2012 : enum error_injection_cmd_type cmd_type;
2013 : uint8_t opc;
2014 : };
2015 :
2016 : static void
2017 0 : free_rpc_remove_error_injection(struct rpc_remove_error_injection *req)
2018 : {
2019 0 : free(req->name);
2020 0 : }
2021 :
2022 : static const struct spdk_json_object_decoder rpc_remove_error_injection_decoders[] = {
2023 : { "name", offsetof(struct rpc_remove_error_injection, name), spdk_json_decode_string },
2024 : { "cmd_type", offsetof(struct rpc_remove_error_injection, cmd_type), rpc_error_injection_decode_cmd_type },
2025 : { "opc", offsetof(struct rpc_remove_error_injection, opc), spdk_json_decode_uint8 },
2026 : };
2027 :
2028 : struct rpc_remove_error_injection_ctx {
2029 : struct spdk_jsonrpc_request *request;
2030 : struct rpc_remove_error_injection rpc;
2031 : };
2032 :
2033 : static void
2034 0 : rpc_remove_error_injection_done(struct spdk_io_channel_iter *i, int status)
2035 : {
2036 0 : struct rpc_remove_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
2037 :
2038 0 : if (status) {
2039 0 : spdk_jsonrpc_send_error_response(ctx->request, status,
2040 : "Failed to remove the error injection.");
2041 : } else {
2042 0 : spdk_jsonrpc_send_bool_response(ctx->request, true);
2043 : }
2044 :
2045 0 : free_rpc_remove_error_injection(&ctx->rpc);
2046 0 : free(ctx);
2047 0 : }
2048 :
2049 : static void
2050 0 : rpc_remove_error_injection_per_channel(struct spdk_io_channel_iter *i)
2051 : {
2052 0 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
2053 0 : struct rpc_remove_error_injection_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
2054 0 : struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch);
2055 0 : struct spdk_nvme_qpair *qpair = ctrlr_ch->qpair->qpair;
2056 0 : struct spdk_nvme_ctrlr *ctrlr = ctrlr_ch->qpair->ctrlr->ctrlr;
2057 :
2058 0 : if (qpair != NULL) {
2059 0 : spdk_nvme_qpair_remove_cmd_error_injection(ctrlr, qpair, ctx->rpc.opc);
2060 : }
2061 :
2062 0 : spdk_for_each_channel_continue(i, 0);
2063 0 : }
2064 :
2065 : static void
2066 0 : rpc_bdev_nvme_remove_error_injection(struct spdk_jsonrpc_request *request,
2067 : const struct spdk_json_val *params)
2068 : {
2069 : struct rpc_remove_error_injection_ctx *ctx;
2070 : struct nvme_ctrlr *nvme_ctrlr;
2071 :
2072 0 : ctx = calloc(1, sizeof(*ctx));
2073 0 : if (!ctx) {
2074 0 : spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
2075 0 : return;
2076 : }
2077 0 : ctx->request = request;
2078 :
2079 0 : if (spdk_json_decode_object(params,
2080 : rpc_remove_error_injection_decoders,
2081 : SPDK_COUNTOF(rpc_remove_error_injection_decoders),
2082 0 : &ctx->rpc)) {
2083 0 : spdk_jsonrpc_send_error_response(request, -EINVAL,
2084 : "Failed to parse the request");
2085 0 : goto cleanup;
2086 : }
2087 :
2088 0 : nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->rpc.name);
2089 0 : if (nvme_ctrlr == NULL) {
2090 0 : SPDK_ERRLOG("No controller with specified name was found.\n");
2091 0 : spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
2092 0 : goto cleanup;
2093 : }
2094 :
2095 0 : if (ctx->rpc.cmd_type == NVME_IO_CMD) {
2096 0 : spdk_for_each_channel(nvme_ctrlr,
2097 : rpc_remove_error_injection_per_channel,
2098 : ctx,
2099 : rpc_remove_error_injection_done);
2100 0 : return;
2101 : } else {
2102 0 : spdk_nvme_qpair_remove_cmd_error_injection(nvme_ctrlr->ctrlr, NULL, ctx->rpc.opc);
2103 0 : spdk_jsonrpc_send_bool_response(ctx->request, true);
2104 : }
2105 :
2106 0 : cleanup:
2107 0 : free_rpc_remove_error_injection(&ctx->rpc);
2108 0 : free(ctx);
2109 : }
2110 0 : SPDK_RPC_REGISTER("bdev_nvme_remove_error_injection", rpc_bdev_nvme_remove_error_injection,
2111 : SPDK_RPC_RUNTIME)
2112 :
2113 : struct rpc_get_io_paths {
2114 : char *name;
2115 : };
2116 :
2117 : static void
2118 0 : free_rpc_get_io_paths(struct rpc_get_io_paths *r)
2119 : {
2120 0 : free(r->name);
2121 0 : }
2122 :
2123 : static const struct spdk_json_object_decoder rpc_get_io_paths_decoders[] = {
2124 : {"name", offsetof(struct rpc_get_io_paths, name), spdk_json_decode_string, true},
2125 : };
2126 :
2127 : struct rpc_get_io_paths_ctx {
2128 : struct rpc_get_io_paths req;
2129 : struct spdk_jsonrpc_request *request;
2130 : struct spdk_json_write_ctx *w;
2131 : };
2132 :
2133 : static void
2134 0 : rpc_bdev_nvme_get_io_paths_done(struct spdk_io_channel_iter *i, int status)
2135 : {
2136 0 : struct rpc_get_io_paths_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
2137 :
2138 0 : spdk_json_write_array_end(ctx->w);
2139 :
2140 0 : spdk_json_write_object_end(ctx->w);
2141 :
2142 0 : spdk_jsonrpc_end_result(ctx->request, ctx->w);
2143 :
2144 0 : free_rpc_get_io_paths(&ctx->req);
2145 0 : free(ctx);
2146 0 : }
2147 :
2148 : static void
2149 0 : _rpc_bdev_nvme_get_io_paths(struct spdk_io_channel_iter *i)
2150 : {
2151 0 : struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
2152 0 : struct nvme_poll_group *group = spdk_io_channel_get_ctx(_ch);
2153 0 : struct rpc_get_io_paths_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
2154 : struct nvme_qpair *qpair;
2155 : struct nvme_io_path *io_path;
2156 : struct nvme_bdev *nbdev;
2157 :
2158 0 : spdk_json_write_object_begin(ctx->w);
2159 :
2160 0 : spdk_json_write_named_string(ctx->w, "thread", spdk_thread_get_name(spdk_get_thread()));
2161 :
2162 0 : spdk_json_write_named_array_begin(ctx->w, "io_paths");
2163 :
2164 0 : TAILQ_FOREACH(qpair, &group->qpair_list, tailq) {
2165 0 : TAILQ_FOREACH(io_path, &qpair->io_path_list, tailq) {
2166 0 : nbdev = io_path->nvme_ns->bdev;
2167 :
2168 0 : if (ctx->req.name != NULL &&
2169 0 : strcmp(ctx->req.name, nbdev->disk.name) != 0) {
2170 0 : continue;
2171 : }
2172 :
2173 0 : nvme_io_path_info_json(ctx->w, io_path);
2174 : }
2175 : }
2176 :
2177 0 : spdk_json_write_array_end(ctx->w);
2178 :
2179 0 : spdk_json_write_object_end(ctx->w);
2180 :
2181 0 : spdk_for_each_channel_continue(i, 0);
2182 0 : }
2183 :
2184 : static void
2185 0 : rpc_bdev_nvme_get_io_paths(struct spdk_jsonrpc_request *request,
2186 : const struct spdk_json_val *params)
2187 : {
2188 : struct rpc_get_io_paths_ctx *ctx;
2189 :
2190 0 : ctx = calloc(1, sizeof(*ctx));
2191 0 : if (ctx == NULL) {
2192 0 : spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
2193 0 : return;
2194 : }
2195 :
2196 0 : if (params != NULL &&
2197 0 : spdk_json_decode_object(params, rpc_get_io_paths_decoders,
2198 : SPDK_COUNTOF(rpc_get_io_paths_decoders),
2199 0 : &ctx->req)) {
2200 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
2201 : "bdev_nvme_get_io_paths requires no parameters");
2202 :
2203 0 : free_rpc_get_io_paths(&ctx->req);
2204 0 : free(ctx);
2205 0 : return;
2206 : }
2207 :
2208 0 : ctx->request = request;
2209 0 : ctx->w = spdk_jsonrpc_begin_result(request);
2210 :
2211 0 : spdk_json_write_object_begin(ctx->w);
2212 :
2213 0 : spdk_json_write_named_array_begin(ctx->w, "poll_groups");
2214 :
2215 0 : spdk_for_each_channel(&g_nvme_bdev_ctrlrs,
2216 : _rpc_bdev_nvme_get_io_paths,
2217 : ctx,
2218 : rpc_bdev_nvme_get_io_paths_done);
2219 : }
2220 0 : SPDK_RPC_REGISTER("bdev_nvme_get_io_paths", rpc_bdev_nvme_get_io_paths, SPDK_RPC_RUNTIME)
2221 :
2222 : struct rpc_bdev_nvme_set_preferred_path {
2223 : char *name;
2224 : uint16_t cntlid;
2225 : };
2226 :
2227 : static void
2228 0 : free_rpc_bdev_nvme_set_preferred_path(struct rpc_bdev_nvme_set_preferred_path *req)
2229 : {
2230 0 : free(req->name);
2231 0 : }
2232 :
2233 : static const struct spdk_json_object_decoder rpc_bdev_nvme_set_preferred_path_decoders[] = {
2234 : {"name", offsetof(struct rpc_bdev_nvme_set_preferred_path, name), spdk_json_decode_string},
2235 : {"cntlid", offsetof(struct rpc_bdev_nvme_set_preferred_path, cntlid), spdk_json_decode_uint16},
2236 : };
2237 :
2238 : struct rpc_bdev_nvme_set_preferred_path_ctx {
2239 : struct rpc_bdev_nvme_set_preferred_path req;
2240 : struct spdk_jsonrpc_request *request;
2241 : };
2242 :
2243 : static void
2244 0 : rpc_bdev_nvme_set_preferred_path_done(void *cb_arg, int rc)
2245 : {
2246 0 : struct rpc_bdev_nvme_set_preferred_path_ctx *ctx = cb_arg;
2247 :
2248 0 : if (rc == 0) {
2249 0 : spdk_jsonrpc_send_bool_response(ctx->request, true);
2250 : } else {
2251 0 : spdk_jsonrpc_send_error_response(ctx->request, rc, spdk_strerror(-rc));
2252 : }
2253 :
2254 0 : free_rpc_bdev_nvme_set_preferred_path(&ctx->req);
2255 0 : free(ctx);
2256 0 : }
2257 :
2258 : static void
2259 0 : rpc_bdev_nvme_set_preferred_path(struct spdk_jsonrpc_request *request,
2260 : const struct spdk_json_val *params)
2261 : {
2262 : struct rpc_bdev_nvme_set_preferred_path_ctx *ctx;
2263 :
2264 0 : ctx = calloc(1, sizeof(*ctx));
2265 0 : if (ctx == NULL) {
2266 0 : spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
2267 0 : return;
2268 : }
2269 :
2270 0 : if (spdk_json_decode_object(params, rpc_bdev_nvme_set_preferred_path_decoders,
2271 : SPDK_COUNTOF(rpc_bdev_nvme_set_preferred_path_decoders),
2272 0 : &ctx->req)) {
2273 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
2274 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
2275 : "spdk_json_decode_object failed");
2276 0 : goto cleanup;
2277 : }
2278 :
2279 0 : ctx->request = request;
2280 :
2281 0 : bdev_nvme_set_preferred_path(ctx->req.name, ctx->req.cntlid,
2282 : rpc_bdev_nvme_set_preferred_path_done, ctx);
2283 0 : return;
2284 :
2285 0 : cleanup:
2286 0 : free_rpc_bdev_nvme_set_preferred_path(&ctx->req);
2287 0 : free(ctx);
2288 : }
2289 0 : SPDK_RPC_REGISTER("bdev_nvme_set_preferred_path", rpc_bdev_nvme_set_preferred_path,
2290 : SPDK_RPC_RUNTIME)
2291 :
2292 : struct rpc_set_multipath_policy {
2293 : char *name;
2294 : enum bdev_nvme_multipath_policy policy;
2295 : enum bdev_nvme_multipath_selector selector;
2296 : uint32_t rr_min_io;
2297 : };
2298 :
2299 : static void
2300 0 : free_rpc_set_multipath_policy(struct rpc_set_multipath_policy *req)
2301 : {
2302 0 : free(req->name);
2303 0 : }
2304 :
2305 : static int
2306 0 : rpc_decode_mp_policy(const struct spdk_json_val *val, void *out)
2307 : {
2308 0 : enum bdev_nvme_multipath_policy *policy = out;
2309 :
2310 0 : if (spdk_json_strequal(val, "active_passive") == true) {
2311 0 : *policy = BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE;
2312 0 : } else if (spdk_json_strequal(val, "active_active") == true) {
2313 0 : *policy = BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE;
2314 : } else {
2315 0 : SPDK_NOTICELOG("Invalid parameter value: policy\n");
2316 0 : return -EINVAL;
2317 : }
2318 :
2319 0 : return 0;
2320 : }
2321 :
2322 : static int
2323 0 : rpc_decode_mp_selector(const struct spdk_json_val *val, void *out)
2324 : {
2325 0 : enum bdev_nvme_multipath_selector *selector = out;
2326 :
2327 0 : if (spdk_json_strequal(val, "round_robin") == true) {
2328 0 : *selector = BDEV_NVME_MP_SELECTOR_ROUND_ROBIN;
2329 0 : } else if (spdk_json_strequal(val, "queue_depth") == true) {
2330 0 : *selector = BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH;
2331 : } else {
2332 0 : SPDK_NOTICELOG("Invalid parameter value: selector\n");
2333 0 : return -EINVAL;
2334 : }
2335 :
2336 0 : return 0;
2337 : }
2338 :
2339 : static const struct spdk_json_object_decoder rpc_set_multipath_policy_decoders[] = {
2340 : {"name", offsetof(struct rpc_set_multipath_policy, name), spdk_json_decode_string},
2341 : {"policy", offsetof(struct rpc_set_multipath_policy, policy), rpc_decode_mp_policy},
2342 : {"selector", offsetof(struct rpc_set_multipath_policy, selector), rpc_decode_mp_selector, true},
2343 : {"rr_min_io", offsetof(struct rpc_set_multipath_policy, rr_min_io), spdk_json_decode_uint32, true},
2344 : };
2345 :
2346 : struct rpc_set_multipath_policy_ctx {
2347 : struct rpc_set_multipath_policy req;
2348 : struct spdk_jsonrpc_request *request;
2349 : };
2350 :
2351 : static void
2352 0 : rpc_bdev_nvme_set_multipath_policy_done(void *cb_arg, int rc)
2353 : {
2354 0 : struct rpc_set_multipath_policy_ctx *ctx = cb_arg;
2355 :
2356 0 : if (rc == 0) {
2357 0 : spdk_jsonrpc_send_bool_response(ctx->request, true);
2358 : } else {
2359 0 : spdk_jsonrpc_send_error_response(ctx->request, rc, spdk_strerror(-rc));
2360 : }
2361 :
2362 0 : free_rpc_set_multipath_policy(&ctx->req);
2363 0 : free(ctx);
2364 0 : }
2365 :
2366 : static void
2367 0 : rpc_bdev_nvme_set_multipath_policy(struct spdk_jsonrpc_request *request,
2368 : const struct spdk_json_val *params)
2369 : {
2370 : struct rpc_set_multipath_policy_ctx *ctx;
2371 :
2372 0 : ctx = calloc(1, sizeof(*ctx));
2373 0 : if (ctx == NULL) {
2374 0 : spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
2375 0 : return;
2376 : }
2377 :
2378 0 : ctx->req.rr_min_io = UINT32_MAX;
2379 0 : ctx->req.selector = UINT32_MAX;
2380 :
2381 0 : if (spdk_json_decode_object(params, rpc_set_multipath_policy_decoders,
2382 : SPDK_COUNTOF(rpc_set_multipath_policy_decoders),
2383 0 : &ctx->req)) {
2384 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
2385 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
2386 : "spdk_json_decode_object failed");
2387 0 : goto cleanup;
2388 : }
2389 :
2390 0 : ctx->request = request;
2391 0 : if (ctx->req.selector == UINT32_MAX) {
2392 0 : if (ctx->req.policy == BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE) {
2393 0 : ctx->req.selector = BDEV_NVME_MP_SELECTOR_ROUND_ROBIN;
2394 : } else {
2395 0 : ctx->req.selector = 0;
2396 : }
2397 : }
2398 :
2399 0 : if (ctx->req.policy != BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE && ctx->req.selector > 0) {
2400 0 : SPDK_ERRLOG("selector only works in active_active mode\n");
2401 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
2402 : "spdk_json_decode_object failed");
2403 0 : goto cleanup;
2404 : }
2405 :
2406 0 : bdev_nvme_set_multipath_policy(ctx->req.name, ctx->req.policy, ctx->req.selector,
2407 : ctx->req.rr_min_io,
2408 : rpc_bdev_nvme_set_multipath_policy_done, ctx);
2409 0 : return;
2410 :
2411 0 : cleanup:
2412 0 : free_rpc_set_multipath_policy(&ctx->req);
2413 0 : free(ctx);
2414 : }
2415 0 : SPDK_RPC_REGISTER("bdev_nvme_set_multipath_policy", rpc_bdev_nvme_set_multipath_policy,
2416 : SPDK_RPC_RUNTIME)
2417 :
2418 : struct rpc_bdev_nvme_start_mdns_discovery {
2419 : char *name;
2420 : char *svcname;
2421 : char *hostnqn;
2422 : struct spdk_nvme_ctrlr_opts opts;
2423 : struct nvme_ctrlr_opts bdev_opts;
2424 : };
2425 :
2426 : static void
2427 0 : free_rpc_bdev_nvme_start_mdns_discovery(struct rpc_bdev_nvme_start_mdns_discovery *req)
2428 : {
2429 0 : free(req->name);
2430 0 : free(req->svcname);
2431 0 : free(req->hostnqn);
2432 0 : }
2433 :
2434 : static const struct spdk_json_object_decoder rpc_bdev_nvme_start_mdns_discovery_decoders[] = {
2435 : {"name", offsetof(struct rpc_bdev_nvme_start_mdns_discovery, name), spdk_json_decode_string},
2436 : {"svcname", offsetof(struct rpc_bdev_nvme_start_mdns_discovery, svcname), spdk_json_decode_string},
2437 : {"hostnqn", offsetof(struct rpc_bdev_nvme_start_mdns_discovery, hostnqn), spdk_json_decode_string, true},
2438 : };
2439 :
2440 : struct rpc_bdev_nvme_start_mdns_discovery_ctx {
2441 : struct rpc_bdev_nvme_start_mdns_discovery req;
2442 : struct spdk_jsonrpc_request *request;
2443 : };
2444 :
2445 : static void
2446 0 : rpc_bdev_nvme_start_mdns_discovery(struct spdk_jsonrpc_request *request,
2447 : const struct spdk_json_val *params)
2448 : {
2449 : struct rpc_bdev_nvme_start_mdns_discovery_ctx *ctx;
2450 : int rc;
2451 :
2452 0 : ctx = calloc(1, sizeof(*ctx));
2453 0 : if (!ctx) {
2454 0 : spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
2455 0 : return;
2456 : }
2457 :
2458 0 : spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->req.opts, sizeof(ctx->req.opts));
2459 :
2460 0 : if (spdk_json_decode_object(params, rpc_bdev_nvme_start_mdns_discovery_decoders,
2461 : SPDK_COUNTOF(rpc_bdev_nvme_start_mdns_discovery_decoders),
2462 0 : &ctx->req)) {
2463 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
2464 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
2465 : "spdk_json_decode_object failed");
2466 0 : goto cleanup;
2467 : }
2468 :
2469 0 : if (ctx->req.hostnqn) {
2470 0 : snprintf(ctx->req.opts.hostnqn, sizeof(ctx->req.opts.hostnqn), "%s",
2471 : ctx->req.hostnqn);
2472 : }
2473 0 : ctx->request = request;
2474 0 : rc = bdev_nvme_start_mdns_discovery(ctx->req.name, ctx->req.svcname, &ctx->req.opts,
2475 : &ctx->req.bdev_opts);
2476 0 : if (rc) {
2477 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
2478 : } else {
2479 0 : spdk_jsonrpc_send_bool_response(request, true);
2480 : }
2481 :
2482 0 : cleanup:
2483 0 : free_rpc_bdev_nvme_start_mdns_discovery(&ctx->req);
2484 0 : free(ctx);
2485 : }
2486 0 : SPDK_RPC_REGISTER("bdev_nvme_start_mdns_discovery", rpc_bdev_nvme_start_mdns_discovery,
2487 : SPDK_RPC_RUNTIME)
2488 :
2489 : struct rpc_bdev_nvme_stop_mdns_discovery {
2490 : char *name;
2491 : };
2492 :
2493 : static const struct spdk_json_object_decoder rpc_bdev_nvme_stop_mdns_discovery_decoders[] = {
2494 : {"name", offsetof(struct rpc_bdev_nvme_stop_mdns_discovery, name), spdk_json_decode_string},
2495 : };
2496 :
2497 : struct rpc_bdev_nvme_stop_mdns_discovery_ctx {
2498 : struct rpc_bdev_nvme_stop_mdns_discovery req;
2499 : struct spdk_jsonrpc_request *request;
2500 : };
2501 :
2502 : static void
2503 0 : rpc_bdev_nvme_stop_mdns_discovery(struct spdk_jsonrpc_request *request,
2504 : const struct spdk_json_val *params)
2505 : {
2506 : struct rpc_bdev_nvme_stop_mdns_discovery_ctx *ctx;
2507 : int rc;
2508 :
2509 0 : ctx = calloc(1, sizeof(*ctx));
2510 0 : if (!ctx) {
2511 0 : spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
2512 0 : return;
2513 : }
2514 :
2515 0 : if (spdk_json_decode_object(params, rpc_bdev_nvme_stop_mdns_discovery_decoders,
2516 : SPDK_COUNTOF(rpc_bdev_nvme_stop_mdns_discovery_decoders),
2517 0 : &ctx->req)) {
2518 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
2519 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
2520 : "spdk_json_decode_object failed");
2521 0 : goto cleanup;
2522 : }
2523 :
2524 0 : ctx->request = request;
2525 0 : rc = bdev_nvme_stop_mdns_discovery(ctx->req.name);
2526 :
2527 0 : if (rc) {
2528 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
2529 0 : goto cleanup;
2530 : }
2531 0 : spdk_jsonrpc_send_bool_response(ctx->request, true);
2532 :
2533 0 : cleanup:
2534 0 : free(ctx->req.name);
2535 0 : free(ctx);
2536 : }
2537 0 : SPDK_RPC_REGISTER("bdev_nvme_stop_mdns_discovery", rpc_bdev_nvme_stop_mdns_discovery,
2538 : SPDK_RPC_RUNTIME)
2539 :
2540 : static void
2541 0 : rpc_bdev_nvme_get_mdns_discovery_info(struct spdk_jsonrpc_request *request,
2542 : const struct spdk_json_val *params)
2543 : {
2544 0 : bdev_nvme_get_mdns_discovery_info(request);
2545 0 : }
2546 :
2547 0 : SPDK_RPC_REGISTER("bdev_nvme_get_mdns_discovery_info", rpc_bdev_nvme_get_mdns_discovery_info,
2548 : SPDK_RPC_RUNTIME)
2549 :
2550 : struct rpc_get_path_stat {
2551 : char *name;
2552 : };
2553 :
2554 : struct path_stat {
2555 : struct spdk_bdev_io_stat stat;
2556 : struct spdk_nvme_transport_id trid;
2557 : struct nvme_ns *ns;
2558 : };
2559 :
2560 : struct rpc_bdev_nvme_path_stat_ctx {
2561 : struct spdk_jsonrpc_request *request;
2562 : struct path_stat *path_stat;
2563 : uint32_t num_paths;
2564 : struct spdk_bdev_desc *desc;
2565 : };
2566 :
2567 : static void
2568 0 : free_rpc_get_path_stat(struct rpc_get_path_stat *req)
2569 : {
2570 0 : free(req->name);
2571 0 : }
2572 :
2573 : static const struct spdk_json_object_decoder rpc_get_path_stat_decoders[] = {
2574 : {"name", offsetof(struct rpc_get_path_stat, name), spdk_json_decode_string},
2575 : };
2576 :
2577 : static void
2578 0 : dummy_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *ctx)
2579 : {
2580 0 : }
2581 :
2582 : static void
2583 0 : rpc_bdev_nvme_path_stat_per_channel(struct spdk_io_channel_iter *i)
2584 : {
2585 0 : struct rpc_bdev_nvme_path_stat_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
2586 0 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
2587 0 : struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(ch);
2588 : struct nvme_io_path *io_path;
2589 : struct path_stat *path_stat;
2590 : uint32_t j;
2591 :
2592 0 : assert(ctx->num_paths != 0);
2593 :
2594 0 : for (j = 0; j < ctx->num_paths; j++) {
2595 0 : path_stat = &ctx->path_stat[j];
2596 :
2597 0 : STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
2598 0 : if (path_stat->ns == io_path->nvme_ns) {
2599 0 : assert(io_path->stat != NULL);
2600 0 : spdk_bdev_add_io_stat(&path_stat->stat, io_path->stat);
2601 : }
2602 : }
2603 : }
2604 :
2605 0 : spdk_for_each_channel_continue(i, 0);
2606 0 : }
2607 :
2608 : static void
2609 0 : rpc_bdev_nvme_path_stat_done(struct spdk_io_channel_iter *i, int status)
2610 : {
2611 0 : struct rpc_bdev_nvme_path_stat_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
2612 0 : struct nvme_bdev *nbdev = spdk_io_channel_iter_get_io_device(i);
2613 : struct spdk_json_write_ctx *w;
2614 : struct path_stat *path_stat;
2615 : uint32_t j;
2616 :
2617 0 : assert(ctx->num_paths != 0);
2618 :
2619 0 : w = spdk_jsonrpc_begin_result(ctx->request);
2620 0 : spdk_json_write_object_begin(w);
2621 0 : spdk_json_write_named_string(w, "name", nbdev->disk.name);
2622 0 : spdk_json_write_named_array_begin(w, "stats");
2623 :
2624 0 : for (j = 0; j < ctx->num_paths; j++) {
2625 0 : path_stat = &ctx->path_stat[j];
2626 0 : spdk_json_write_object_begin(w);
2627 :
2628 0 : spdk_json_write_named_object_begin(w, "trid");
2629 0 : nvme_bdev_dump_trid_json(&path_stat->trid, w);
2630 0 : spdk_json_write_object_end(w);
2631 :
2632 0 : spdk_json_write_named_object_begin(w, "stat");
2633 0 : spdk_bdev_dump_io_stat_json(&path_stat->stat, w);
2634 0 : spdk_json_write_object_end(w);
2635 :
2636 0 : spdk_json_write_object_end(w);
2637 : }
2638 :
2639 0 : spdk_json_write_array_end(w);
2640 0 : spdk_json_write_object_end(w);
2641 0 : spdk_jsonrpc_end_result(ctx->request, w);
2642 :
2643 0 : spdk_bdev_close(ctx->desc);
2644 0 : free(ctx->path_stat);
2645 0 : free(ctx);
2646 0 : }
2647 :
2648 : static void
2649 0 : rpc_bdev_nvme_get_path_iostat(struct spdk_jsonrpc_request *request,
2650 : const struct spdk_json_val *params)
2651 : {
2652 0 : struct rpc_get_path_stat req = {};
2653 0 : struct spdk_bdev_desc *desc = NULL;
2654 : struct spdk_bdev *bdev;
2655 : struct nvme_bdev *nbdev;
2656 : struct nvme_ns *nvme_ns;
2657 : struct path_stat *path_stat;
2658 : struct rpc_bdev_nvme_path_stat_ctx *ctx;
2659 0 : struct spdk_bdev_nvme_opts opts;
2660 0 : uint32_t num_paths = 0, i = 0;
2661 : int rc;
2662 :
2663 0 : bdev_nvme_get_opts(&opts);
2664 0 : if (!opts.io_path_stat) {
2665 0 : SPDK_ERRLOG("RPC not enabled if enable_io_path_stat is false\n");
2666 0 : spdk_jsonrpc_send_error_response(request, -EPERM,
2667 : "RPC not enabled if enable_io_path_stat is false");
2668 0 : return;
2669 : }
2670 :
2671 0 : if (spdk_json_decode_object(params, rpc_get_path_stat_decoders,
2672 : SPDK_COUNTOF(rpc_get_path_stat_decoders),
2673 : &req)) {
2674 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
2675 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
2676 : "spdk_json_decode_object failed");
2677 0 : free_rpc_get_path_stat(&req);
2678 0 : return;
2679 : }
2680 :
2681 0 : rc = spdk_bdev_open_ext(req.name, false, dummy_bdev_event_cb, NULL, &desc);
2682 0 : if (rc != 0) {
2683 0 : SPDK_ERRLOG("Failed to open bdev '%s': %d\n", req.name, rc);
2684 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
2685 0 : free_rpc_get_path_stat(&req);
2686 0 : return;
2687 : }
2688 :
2689 0 : free_rpc_get_path_stat(&req);
2690 :
2691 0 : ctx = calloc(1, sizeof(struct rpc_bdev_nvme_path_stat_ctx));
2692 0 : if (ctx == NULL) {
2693 0 : spdk_bdev_close(desc);
2694 0 : SPDK_ERRLOG("Failed to allocate rpc_bdev_nvme_path_stat_ctx struct\n");
2695 0 : spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
2696 0 : return;
2697 : }
2698 :
2699 0 : bdev = spdk_bdev_desc_get_bdev(desc);
2700 0 : nbdev = bdev->ctxt;
2701 :
2702 0 : pthread_mutex_lock(&nbdev->mutex);
2703 0 : if (nbdev->ref == 0) {
2704 0 : rc = -ENOENT;
2705 0 : goto err;
2706 : }
2707 :
2708 0 : num_paths = nbdev->ref;
2709 0 : path_stat = calloc(num_paths, sizeof(struct path_stat));
2710 0 : if (path_stat == NULL) {
2711 0 : rc = -ENOMEM;
2712 0 : SPDK_ERRLOG("Failed to allocate memory for path_stat.\n");
2713 0 : goto err;
2714 : }
2715 :
2716 : /* store the history stat */
2717 0 : TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
2718 0 : assert(i < num_paths);
2719 0 : path_stat[i].ns = nvme_ns;
2720 0 : path_stat[i].trid = nvme_ns->ctrlr->active_path_id->trid;
2721 :
2722 0 : assert(nvme_ns->stat != NULL);
2723 0 : memcpy(&path_stat[i].stat, nvme_ns->stat, sizeof(struct spdk_bdev_io_stat));
2724 0 : i++;
2725 : }
2726 0 : pthread_mutex_unlock(&nbdev->mutex);
2727 :
2728 0 : ctx->request = request;
2729 0 : ctx->desc = desc;
2730 0 : ctx->path_stat = path_stat;
2731 0 : ctx->num_paths = num_paths;
2732 :
2733 0 : spdk_for_each_channel(nbdev,
2734 : rpc_bdev_nvme_path_stat_per_channel,
2735 : ctx,
2736 : rpc_bdev_nvme_path_stat_done);
2737 0 : return;
2738 :
2739 0 : err:
2740 0 : pthread_mutex_unlock(&nbdev->mutex);
2741 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
2742 0 : spdk_bdev_close(desc);
2743 0 : free(ctx);
2744 : }
2745 0 : SPDK_RPC_REGISTER("bdev_nvme_get_path_iostat", rpc_bdev_nvme_get_path_iostat,
2746 : SPDK_RPC_RUNTIME)
|