Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2018 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/stdinc.h"
7 : : #include "spdk/string.h"
8 : : #include "spdk/rpc.h"
9 : : #include "spdk/util.h"
10 : : #include "spdk/bdev_module.h"
11 : : #include "spdk/log.h"
12 : :
13 : : #include "bdev_nvme.h"
14 : : #include "spdk/base64.h"
15 : :
16 : : enum spdk_nvme_rpc_type {
17 : : NVME_ADMIN_CMD = 1,
18 : : NVME_IO_CMD,
19 : : };
20 : :
21 : : struct rpc_bdev_nvme_send_cmd_req {
22 : : char *name;
23 : : int cmd_type;
24 : : int data_direction;
25 : : uint32_t timeout_ms;
26 : : uint32_t data_len;
27 : : uint32_t md_len;
28 : :
29 : : struct spdk_nvme_cmd *cmdbuf;
30 : : char *data;
31 : : char *md;
32 : : };
33 : :
34 : : struct rpc_bdev_nvme_send_cmd_resp {
35 : : char *cpl_text;
36 : : char *data_text;
37 : : char *md_text;
38 : : };
39 : :
40 : : struct rpc_bdev_nvme_send_cmd_ctx {
41 : : struct spdk_jsonrpc_request *jsonrpc_request;
42 : : struct rpc_bdev_nvme_send_cmd_req req;
43 : : struct rpc_bdev_nvme_send_cmd_resp resp;
44 : : struct nvme_ctrlr *nvme_ctrlr;
45 : : struct spdk_io_channel *ctrlr_io_ch;
46 : : };
47 : :
48 : : static void
49 : 6 : free_rpc_bdev_nvme_send_cmd_ctx(struct rpc_bdev_nvme_send_cmd_ctx *ctx)
50 : : {
51 [ - + ]: 6 : assert(ctx != NULL);
52 : :
53 : 6 : free(ctx->req.name);
54 : 6 : free(ctx->req.cmdbuf);
55 : 6 : spdk_free(ctx->req.data);
56 : 6 : spdk_free(ctx->req.md);
57 : 6 : free(ctx->resp.cpl_text);
58 : 6 : free(ctx->resp.data_text);
59 : 6 : free(ctx->resp.md_text);
60 : 6 : free(ctx);
61 : 6 : }
62 : :
63 : : static int
64 : 6 : rpc_bdev_nvme_send_cmd_resp_construct(struct rpc_bdev_nvme_send_cmd_resp *resp,
65 : : struct rpc_bdev_nvme_send_cmd_req *req,
66 : : const struct spdk_nvme_cpl *cpl)
67 : : {
68 : 6 : resp->cpl_text = malloc(spdk_base64_get_encoded_strlen(sizeof(*cpl)) + 1);
69 [ - + ]: 6 : if (!resp->cpl_text) {
70 : 0 : return -ENOMEM;
71 : : }
72 : 6 : spdk_base64_urlsafe_encode(resp->cpl_text, cpl, sizeof(*cpl));
73 : :
74 [ + - ]: 6 : if (req->data_direction == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
75 [ - + ]: 6 : if (req->data_len) {
76 : 0 : resp->data_text = malloc(spdk_base64_get_encoded_strlen(req->data_len) + 1);
77 [ # # ]: 0 : if (!resp->data_text) {
78 : 0 : return -ENOMEM;
79 : : }
80 : 0 : spdk_base64_urlsafe_encode(resp->data_text, req->data, req->data_len);
81 : : }
82 [ - + ]: 6 : if (req->md_len) {
83 : 0 : resp->md_text = malloc(spdk_base64_get_encoded_strlen(req->md_len) + 1);
84 [ # # ]: 0 : if (!resp->md_text) {
85 : 0 : return -ENOMEM;
86 : : }
87 : 0 : spdk_base64_urlsafe_encode(resp->md_text, req->md, req->md_len);
88 : : }
89 : : }
90 : :
91 : 6 : return 0;
92 : : }
93 : :
94 : : static void
95 : 6 : rpc_bdev_nvme_send_cmd_complete(struct rpc_bdev_nvme_send_cmd_ctx *ctx,
96 : : const struct spdk_nvme_cpl *cpl)
97 : : {
98 : 6 : struct spdk_jsonrpc_request *request = ctx->jsonrpc_request;
99 : : struct spdk_json_write_ctx *w;
100 : : int ret;
101 : :
102 : 6 : ret = rpc_bdev_nvme_send_cmd_resp_construct(&ctx->resp, &ctx->req, cpl);
103 [ - + ]: 6 : if (ret) {
104 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
105 : : spdk_strerror(-ret));
106 : 0 : goto out;
107 : : }
108 : :
109 : 6 : w = spdk_jsonrpc_begin_result(request);
110 : 6 : spdk_json_write_object_begin(w);
111 : 6 : spdk_json_write_named_string(w, "cpl", ctx->resp.cpl_text);
112 : :
113 [ - + ]: 6 : if (ctx->resp.data_text) {
114 : 0 : spdk_json_write_named_string(w, "data", ctx->resp.data_text);
115 : : }
116 : :
117 [ - + ]: 6 : if (ctx->resp.md_text) {
118 : 0 : spdk_json_write_named_string(w, "metadata", ctx->resp.md_text);
119 : : }
120 : :
121 : 6 : spdk_json_write_object_end(w);
122 : 6 : spdk_jsonrpc_end_result(request, w);
123 : :
124 : 6 : out:
125 : 6 : free_rpc_bdev_nvme_send_cmd_ctx(ctx);
126 : 6 : return;
127 : : }
128 : :
129 : : static void
130 : 6 : nvme_rpc_bdev_nvme_cb(void *ref, const struct spdk_nvme_cpl *cpl)
131 : : {
132 : 6 : struct rpc_bdev_nvme_send_cmd_ctx *ctx = (struct rpc_bdev_nvme_send_cmd_ctx *)ref;
133 : :
134 [ - + ]: 6 : if (ctx->ctrlr_io_ch) {
135 : 0 : spdk_put_io_channel(ctx->ctrlr_io_ch);
136 : 0 : ctx->ctrlr_io_ch = NULL;
137 : : }
138 : :
139 : 6 : rpc_bdev_nvme_send_cmd_complete(ctx, cpl);
140 : 6 : }
141 : :
142 : : static int
143 : 6 : nvme_rpc_admin_cmd_bdev_nvme(struct rpc_bdev_nvme_send_cmd_ctx *ctx, struct spdk_nvme_cmd *cmd,
144 : : void *buf, uint32_t nbytes, uint32_t timeout_ms)
145 : : {
146 : 6 : struct nvme_ctrlr *_nvme_ctrlr = ctx->nvme_ctrlr;
147 : : int ret;
148 : :
149 : 6 : ret = spdk_nvme_ctrlr_cmd_admin_raw(_nvme_ctrlr->ctrlr, cmd, buf,
150 : : nbytes, nvme_rpc_bdev_nvme_cb, ctx);
151 : :
152 : 6 : return ret;
153 : : }
154 : :
155 : : static int
156 : 0 : nvme_rpc_io_cmd_bdev_nvme(struct rpc_bdev_nvme_send_cmd_ctx *ctx, struct spdk_nvme_cmd *cmd,
157 : : void *buf, uint32_t nbytes, void *md_buf, uint32_t md_len,
158 : : uint32_t timeout_ms)
159 : : {
160 : 0 : struct nvme_ctrlr *_nvme_ctrlr = ctx->nvme_ctrlr;
161 : : struct spdk_nvme_qpair *io_qpair;
162 : : int ret;
163 : :
164 : 0 : ctx->ctrlr_io_ch = spdk_get_io_channel(_nvme_ctrlr);
165 : 0 : io_qpair = bdev_nvme_get_io_qpair(ctx->ctrlr_io_ch);
166 : :
167 : 0 : ret = spdk_nvme_ctrlr_cmd_io_raw_with_md(_nvme_ctrlr->ctrlr, io_qpair,
168 : : cmd, buf, nbytes, md_buf, nvme_rpc_bdev_nvme_cb, ctx);
169 [ # # ]: 0 : if (ret) {
170 : 0 : spdk_put_io_channel(ctx->ctrlr_io_ch);
171 : : }
172 : :
173 : 0 : return ret;
174 : :
175 : : }
176 : :
177 : : static int
178 : 6 : rpc_bdev_nvme_send_cmd_exec(struct rpc_bdev_nvme_send_cmd_ctx *ctx)
179 : : {
180 : 6 : struct rpc_bdev_nvme_send_cmd_req *req = &ctx->req;
181 : 6 : int ret = -EINVAL;
182 : :
183 [ + - - ]: 6 : switch (req->cmd_type) {
184 : 6 : case NVME_ADMIN_CMD:
185 : 6 : ret = nvme_rpc_admin_cmd_bdev_nvme(ctx, req->cmdbuf, req->data,
186 : : req->data_len, req->timeout_ms);
187 : 6 : break;
188 : 0 : case NVME_IO_CMD:
189 : 0 : ret = nvme_rpc_io_cmd_bdev_nvme(ctx, req->cmdbuf, req->data,
190 : 0 : req->data_len, req->md, req->md_len, req->timeout_ms);
191 : 0 : break;
192 : : }
193 : :
194 : 6 : return ret;
195 : : }
196 : :
197 : : static int
198 : 6 : rpc_decode_cmd_type(const struct spdk_json_val *val, void *out)
199 : : {
200 : 6 : int *cmd_type = out;
201 : :
202 [ + - ]: 6 : if (spdk_json_strequal(val, "admin") == true) {
203 : 6 : *cmd_type = NVME_ADMIN_CMD;
204 [ # # ]: 0 : } else if (spdk_json_strequal(val, "io") == true) {
205 : 0 : *cmd_type = NVME_IO_CMD;
206 : : } else {
207 : 0 : SPDK_NOTICELOG("Invalid parameter value: cmd_type\n");
208 : 0 : return -EINVAL;
209 : : }
210 : :
211 : 6 : return 0;
212 : : }
213 : :
214 : : static int
215 : 6 : rpc_decode_data_direction(const struct spdk_json_val *val, void *out)
216 : : {
217 : 6 : int *data_direction = out;
218 : :
219 [ - + ]: 6 : if (spdk_json_strequal(val, "h2c") == true) {
220 : 0 : *data_direction = SPDK_NVME_DATA_HOST_TO_CONTROLLER;
221 [ + - ]: 6 : } else if (spdk_json_strequal(val, "c2h") == true) {
222 : 6 : *data_direction = SPDK_NVME_DATA_CONTROLLER_TO_HOST;
223 : : } else {
224 : 0 : SPDK_NOTICELOG("Invalid parameter value: data_direction\n");
225 : 0 : return -EINVAL;
226 : : }
227 : :
228 : 6 : return 0;
229 : : }
230 : :
231 : : static int
232 : 6 : rpc_decode_cmdbuf(const struct spdk_json_val *val, void *out)
233 : : {
234 : 6 : char *text = NULL;
235 : 3 : size_t text_strlen, raw_len;
236 : 6 : struct spdk_nvme_cmd *cmdbuf, **_cmdbuf = out;
237 : : int rc;
238 : :
239 : 6 : rc = spdk_json_decode_string(val, &text);
240 [ - + ]: 6 : if (rc) {
241 [ # # ]: 0 : return val->type == SPDK_JSON_VAL_STRING ? -ENOMEM : -EINVAL;
242 : : }
243 : :
244 [ - + ]: 6 : text_strlen = strlen(text);
245 : 6 : raw_len = spdk_base64_get_decoded_len(text_strlen);
246 : 6 : cmdbuf = malloc(raw_len);
247 [ - + ]: 6 : if (!cmdbuf) {
248 : 0 : rc = -ENOMEM;
249 : 0 : goto out;
250 : : }
251 : :
252 : 6 : rc = spdk_base64_urlsafe_decode(cmdbuf, &raw_len, text);
253 [ - + ]: 6 : if (rc) {
254 : 0 : free(cmdbuf);
255 : 0 : goto out;
256 : : }
257 [ - + ]: 6 : if (raw_len != sizeof(*cmdbuf)) {
258 : 0 : rc = -EINVAL;
259 : 0 : free(cmdbuf);
260 : 0 : goto out;
261 : : }
262 : :
263 : 6 : *_cmdbuf = cmdbuf;
264 : :
265 : 6 : out:
266 : 6 : free(text);
267 : 6 : return rc;
268 : : }
269 : :
270 : : static int
271 : 0 : rpc_decode_data(const struct spdk_json_val *val, void *out)
272 : : {
273 : 0 : struct rpc_bdev_nvme_send_cmd_req *req = (struct rpc_bdev_nvme_send_cmd_req *)out;
274 : 0 : char *text = NULL;
275 : : size_t text_strlen;
276 : : int rc;
277 : :
278 : 0 : rc = spdk_json_decode_string(val, &text);
279 [ # # ]: 0 : if (rc) {
280 [ # # ]: 0 : return val->type == SPDK_JSON_VAL_STRING ? -ENOMEM : -EINVAL;
281 : : }
282 [ # # ]: 0 : text_strlen = strlen(text);
283 : :
284 [ # # ]: 0 : if (req->data_len) {
285 : : /* data_len is decoded by param "data_len" */
286 [ # # ]: 0 : if (req->data_len != spdk_base64_get_decoded_len(text_strlen)) {
287 : 0 : rc = -EINVAL;
288 : 0 : goto out;
289 : : }
290 : : } else {
291 : 0 : req->data_len = spdk_base64_get_decoded_len(text_strlen);
292 : 0 : req->data = spdk_malloc(req->data_len > 0x1000 ? req->data_len : 0x1000, 0x1000,
293 : : NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
294 [ # # ]: 0 : if (!req->data) {
295 : 0 : rc = -ENOMEM;
296 : 0 : goto out;
297 : : }
298 : : }
299 : :
300 : 0 : rc = spdk_base64_urlsafe_decode(req->data, (size_t *)&req->data_len, text);
301 : :
302 : 0 : out:
303 : 0 : free(text);
304 : 0 : return rc;
305 : : }
306 : :
307 : : static int
308 : 0 : rpc_decode_data_len(const struct spdk_json_val *val, void *out)
309 : : {
310 : 0 : struct rpc_bdev_nvme_send_cmd_req *req = (struct rpc_bdev_nvme_send_cmd_req *)out;
311 : 0 : uint32_t data_len;
312 : : int rc;
313 : :
314 : 0 : rc = spdk_json_decode_uint32(val, &data_len);
315 [ # # ]: 0 : if (rc) {
316 : 0 : return rc;
317 : : }
318 : :
319 [ # # ]: 0 : if (req->data_len) {
320 : : /* data_len is decoded by param "data" */
321 [ # # ]: 0 : if (req->data_len != data_len) {
322 : 0 : rc = -EINVAL;
323 : : }
324 : : } else {
325 : 0 : req->data_len = data_len;
326 : 0 : req->data = spdk_malloc(req->data_len > 0x1000 ? req->data_len : 0x1000, 0x1000,
327 : : NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
328 [ # # ]: 0 : if (!req->data) {
329 : 0 : rc = -ENOMEM;
330 : : }
331 : : }
332 : :
333 : 0 : return rc;
334 : : }
335 : :
336 : : static int
337 : 0 : rpc_decode_metadata(const struct spdk_json_val *val, void *out)
338 : : {
339 : 0 : struct rpc_bdev_nvme_send_cmd_req *req = (struct rpc_bdev_nvme_send_cmd_req *)out;
340 : 0 : char *text = NULL;
341 : : size_t text_strlen;
342 : : int rc;
343 : :
344 : 0 : rc = spdk_json_decode_string(val, &text);
345 [ # # ]: 0 : if (rc) {
346 [ # # ]: 0 : return val->type == SPDK_JSON_VAL_STRING ? -ENOMEM : -EINVAL;
347 : : }
348 [ # # ]: 0 : text_strlen = strlen(text);
349 : :
350 [ # # ]: 0 : if (req->md_len) {
351 : : /* md_len is decoded by param "metadata_len" */
352 [ # # ]: 0 : if (req->md_len != spdk_base64_get_decoded_len(text_strlen)) {
353 : 0 : rc = -EINVAL;
354 : 0 : goto out;
355 : : }
356 : : } else {
357 : 0 : req->md_len = spdk_base64_get_decoded_len(text_strlen);
358 : 0 : req->md = spdk_malloc(req->md_len, 0x1000, NULL,
359 : : SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
360 [ # # ]: 0 : if (!req->md) {
361 : 0 : rc = -ENOMEM;
362 : 0 : goto out;
363 : : }
364 : : }
365 : :
366 : 0 : rc = spdk_base64_urlsafe_decode(req->md, (size_t *)&req->md_len, text);
367 : :
368 : 0 : out:
369 : 0 : free(text);
370 : 0 : return rc;
371 : : }
372 : :
373 : : static int
374 : 0 : rpc_decode_metadata_len(const struct spdk_json_val *val, void *out)
375 : : {
376 : 0 : struct rpc_bdev_nvme_send_cmd_req *req = (struct rpc_bdev_nvme_send_cmd_req *)out;
377 : 0 : uint32_t md_len;
378 : : int rc;
379 : :
380 : 0 : rc = spdk_json_decode_uint32(val, &md_len);
381 [ # # ]: 0 : if (rc) {
382 : 0 : return rc;
383 : : }
384 : :
385 [ # # ]: 0 : if (req->md_len) {
386 : : /* md_len is decoded by param "metadata" */
387 [ # # ]: 0 : if (req->md_len != md_len) {
388 : 0 : rc = -EINVAL;
389 : : }
390 : : } else {
391 : 0 : req->md_len = md_len;
392 : 0 : req->md = spdk_malloc(req->md_len, 0x1000, NULL,
393 : : SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
394 [ # # ]: 0 : if (!req->md) {
395 : 0 : rc = -ENOMEM;
396 : : }
397 : : }
398 : :
399 : 0 : return rc;
400 : : }
401 : :
402 : : static const struct spdk_json_object_decoder rpc_bdev_nvme_send_cmd_req_decoders[] = {
403 : : {"name", offsetof(struct rpc_bdev_nvme_send_cmd_req, name), spdk_json_decode_string},
404 : : {"cmd_type", offsetof(struct rpc_bdev_nvme_send_cmd_req, cmd_type), rpc_decode_cmd_type},
405 : : {"data_direction", offsetof(struct rpc_bdev_nvme_send_cmd_req, data_direction), rpc_decode_data_direction},
406 : : {"cmdbuf", offsetof(struct rpc_bdev_nvme_send_cmd_req, cmdbuf), rpc_decode_cmdbuf},
407 : : {"timeout_ms", offsetof(struct rpc_bdev_nvme_send_cmd_req, timeout_ms), spdk_json_decode_uint32, true},
408 : : {"data_len", 0, rpc_decode_data_len, true},
409 : : {"metadata_len", 0, rpc_decode_metadata_len, true},
410 : : {"data", 0, rpc_decode_data, true},
411 : : {"metadata", 0, rpc_decode_metadata, true},
412 : : };
413 : :
414 : : static void
415 : 6 : rpc_bdev_nvme_send_cmd(struct spdk_jsonrpc_request *request,
416 : : const struct spdk_json_val *params)
417 : : {
418 : : struct rpc_bdev_nvme_send_cmd_ctx *ctx;
419 : : int ret, error_code;
420 : :
421 : 6 : ctx = calloc(1, sizeof(*ctx));
422 [ - + ]: 6 : if (!ctx) {
423 : 0 : SPDK_ERRLOG("Failed at Malloc ctx\n");
424 : 0 : error_code = SPDK_JSONRPC_ERROR_INTERNAL_ERROR;
425 : 0 : ret = -ENOMEM;
426 : 0 : goto invalid;
427 : : }
428 : :
429 [ - + ]: 6 : if (spdk_json_decode_object(params, rpc_bdev_nvme_send_cmd_req_decoders,
430 : : SPDK_COUNTOF(rpc_bdev_nvme_send_cmd_req_decoders),
431 : 6 : &ctx->req)) {
432 : 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
433 : 0 : error_code = SPDK_JSONRPC_ERROR_INVALID_PARAMS;
434 : 0 : ret = -EINVAL;
435 : 0 : goto invalid;
436 : : }
437 : :
438 : 6 : ctx->nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->req.name);
439 [ - + ]: 6 : if (ctx->nvme_ctrlr == NULL) {
440 : 0 : SPDK_ERRLOG("Failed at device lookup\n");
441 : 0 : error_code = SPDK_JSONRPC_ERROR_INVALID_PARAMS;
442 : 0 : ret = -EINVAL;
443 : 0 : goto invalid;
444 : : }
445 : :
446 : 6 : ctx->jsonrpc_request = request;
447 : :
448 : 6 : ret = rpc_bdev_nvme_send_cmd_exec(ctx);
449 [ - + ]: 6 : if (ret < 0) {
450 : 0 : SPDK_NOTICELOG("Failed at rpc_bdev_nvme_send_cmd_exec\n");
451 : 0 : error_code = SPDK_JSONRPC_ERROR_INTERNAL_ERROR;
452 : 0 : goto invalid;
453 : : }
454 : :
455 : 6 : return;
456 : :
457 : 0 : invalid:
458 [ # # ]: 0 : if (ctx != NULL) {
459 : 0 : free_rpc_bdev_nvme_send_cmd_ctx(ctx);
460 : : }
461 : 0 : spdk_jsonrpc_send_error_response(request, error_code, spdk_strerror(-ret));
462 : : }
463 : 2045 : SPDK_RPC_REGISTER("bdev_nvme_send_cmd", rpc_bdev_nvme_send_cmd, SPDK_RPC_RUNTIME)
|