Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause
2 : * Copyright (C) 2017 Intel Corporation. All rights reserved.
3 : * All rights reserved.
4 : */
5 :
6 : #include "spdk/stdinc.h"
7 :
8 : #include "spdk/log.h"
9 : #include "spdk/rpc.h"
10 : #include "spdk/util.h"
11 : #include "spdk/string.h"
12 : #include "spdk/env.h"
13 : #include "spdk/scsi.h"
14 : #include "spdk/vhost.h"
15 : #include "vhost_internal.h"
16 : #include "spdk/bdev.h"
17 :
18 : struct rpc_vhost_scsi_ctrlr {
19 : char *ctrlr;
20 : char *cpumask;
21 : bool delay;
22 : };
23 :
24 : static void
25 0 : free_rpc_vhost_scsi_ctrlr(struct rpc_vhost_scsi_ctrlr *req)
26 : {
27 0 : free(req->ctrlr);
28 0 : free(req->cpumask);
29 0 : }
30 :
31 : static const struct spdk_json_object_decoder rpc_vhost_create_scsi_ctrlr[] = {
32 : {"ctrlr", offsetof(struct rpc_vhost_scsi_ctrlr, ctrlr), spdk_json_decode_string },
33 : {"cpumask", offsetof(struct rpc_vhost_scsi_ctrlr, cpumask), spdk_json_decode_string, true},
34 : {"delay", offsetof(struct rpc_vhost_scsi_ctrlr, delay), spdk_json_decode_bool, true},
35 : };
36 :
37 : static void
38 0 : rpc_vhost_create_scsi_controller(struct spdk_jsonrpc_request *request,
39 : const struct spdk_json_val *params)
40 : {
41 0 : struct rpc_vhost_scsi_ctrlr req = {0};
42 : int rc;
43 :
44 0 : if (spdk_json_decode_object(params, rpc_vhost_create_scsi_ctrlr,
45 : SPDK_COUNTOF(rpc_vhost_create_scsi_ctrlr),
46 : &req)) {
47 0 : SPDK_DEBUGLOG(vhost_rpc, "spdk_json_decode_object failed\n");
48 0 : rc = -EINVAL;
49 0 : goto invalid;
50 : }
51 :
52 0 : if (req.delay) {
53 0 : rc = spdk_vhost_scsi_dev_construct_no_start(req.ctrlr, req.cpumask);
54 : } else {
55 0 : rc = spdk_vhost_scsi_dev_construct(req.ctrlr, req.cpumask);
56 : }
57 0 : if (rc < 0) {
58 0 : goto invalid;
59 : }
60 :
61 0 : free_rpc_vhost_scsi_ctrlr(&req);
62 :
63 0 : spdk_jsonrpc_send_bool_response(request, true);
64 0 : return;
65 :
66 0 : invalid:
67 0 : free_rpc_vhost_scsi_ctrlr(&req);
68 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
69 : spdk_strerror(-rc));
70 : }
71 0 : SPDK_RPC_REGISTER("vhost_create_scsi_controller", rpc_vhost_create_scsi_controller,
72 : SPDK_RPC_RUNTIME)
73 :
74 : struct rpc_start_vhost_scsi_ctrlr {
75 : char *ctrlr;
76 : };
77 :
78 : static void
79 0 : free_rpc_start_vhost_scsi_ctrlr(struct rpc_start_vhost_scsi_ctrlr *req)
80 : {
81 0 : free(req->ctrlr);
82 0 : }
83 :
84 : static const struct spdk_json_object_decoder rpc_start_vhost_scsi_ctrlr_decoder[] = {
85 : {"ctrlr", offsetof(struct rpc_start_vhost_scsi_ctrlr, ctrlr), spdk_json_decode_string },
86 : };
87 :
88 : static void
89 0 : rpc_vhost_start_scsi_controller(struct spdk_jsonrpc_request *request,
90 : const struct spdk_json_val *params)
91 : {
92 0 : struct rpc_start_vhost_scsi_ctrlr req = {0};
93 : int rc;
94 :
95 0 : if (spdk_json_decode_object(params, rpc_start_vhost_scsi_ctrlr_decoder,
96 : SPDK_COUNTOF(rpc_start_vhost_scsi_ctrlr_decoder),
97 : &req)) {
98 0 : SPDK_DEBUGLOG(vhost_rpc, "spdk_json_decode_object failed\n");
99 0 : rc = -EINVAL;
100 0 : goto invalid;
101 : }
102 :
103 0 : rc = vhost_scsi_controller_start(req.ctrlr);
104 0 : if (rc < 0) {
105 0 : goto invalid;
106 : }
107 :
108 0 : free_rpc_start_vhost_scsi_ctrlr(&req);
109 :
110 0 : spdk_jsonrpc_send_bool_response(request, true);
111 0 : return;
112 :
113 0 : invalid:
114 0 : free_rpc_start_vhost_scsi_ctrlr(&req);
115 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
116 : spdk_strerror(-rc));
117 : }
118 0 : SPDK_RPC_REGISTER("vhost_start_scsi_controller", rpc_vhost_start_scsi_controller,
119 : SPDK_RPC_RUNTIME)
120 :
121 : struct rpc_vhost_scsi_ctrlr_add_target {
122 : char *ctrlr;
123 : int32_t scsi_target_num;
124 : char *bdev_name;
125 : };
126 :
127 : static void
128 0 : free_rpc_vhost_scsi_ctrlr_add_target(struct rpc_vhost_scsi_ctrlr_add_target *req)
129 : {
130 0 : free(req->ctrlr);
131 0 : free(req->bdev_name);
132 0 : }
133 :
134 : static const struct spdk_json_object_decoder rpc_vhost_scsi_ctrlr_add_target[] = {
135 : {"ctrlr", offsetof(struct rpc_vhost_scsi_ctrlr_add_target, ctrlr), spdk_json_decode_string },
136 : {"scsi_target_num", offsetof(struct rpc_vhost_scsi_ctrlr_add_target, scsi_target_num), spdk_json_decode_int32},
137 : {"bdev_name", offsetof(struct rpc_vhost_scsi_ctrlr_add_target, bdev_name), spdk_json_decode_string },
138 : };
139 :
140 : static void
141 0 : rpc_vhost_scsi_controller_add_target(struct spdk_jsonrpc_request *request,
142 : const struct spdk_json_val *params)
143 : {
144 0 : struct rpc_vhost_scsi_ctrlr_add_target req = {0};
145 : struct spdk_json_write_ctx *w;
146 : struct spdk_vhost_dev *vdev;
147 : int rc;
148 :
149 0 : if (spdk_json_decode_object(params, rpc_vhost_scsi_ctrlr_add_target,
150 : SPDK_COUNTOF(rpc_vhost_scsi_ctrlr_add_target),
151 : &req)) {
152 0 : SPDK_DEBUGLOG(vhost_rpc, "spdk_json_decode_object failed\n");
153 0 : rc = -EINVAL;
154 0 : goto invalid;
155 : }
156 :
157 0 : spdk_vhost_lock();
158 0 : vdev = spdk_vhost_dev_find(req.ctrlr);
159 0 : if (vdev == NULL) {
160 0 : spdk_vhost_unlock();
161 0 : rc = -ENODEV;
162 0 : goto invalid;
163 : }
164 :
165 0 : rc = spdk_vhost_scsi_dev_add_tgt(vdev, req.scsi_target_num, req.bdev_name);
166 0 : spdk_vhost_unlock();
167 0 : if (rc < 0) {
168 0 : goto invalid;
169 : }
170 :
171 0 : free_rpc_vhost_scsi_ctrlr_add_target(&req);
172 :
173 0 : w = spdk_jsonrpc_begin_result(request);
174 0 : spdk_json_write_int32(w, rc);
175 0 : spdk_jsonrpc_end_result(request, w);
176 0 : return;
177 :
178 0 : invalid:
179 0 : free_rpc_vhost_scsi_ctrlr_add_target(&req);
180 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
181 : spdk_strerror(-rc));
182 : }
183 0 : SPDK_RPC_REGISTER("vhost_scsi_controller_add_target", rpc_vhost_scsi_controller_add_target,
184 : SPDK_RPC_RUNTIME)
185 :
186 : struct rpc_remove_vhost_scsi_ctrlr_target {
187 : char *ctrlr;
188 : uint32_t scsi_target_num;
189 : };
190 :
191 : static void
192 0 : free_rpc_remove_vhost_scsi_ctrlr_target(struct rpc_remove_vhost_scsi_ctrlr_target *req)
193 : {
194 0 : free(req->ctrlr);
195 0 : }
196 :
197 : static const struct spdk_json_object_decoder rpc_vhost_remove_target[] = {
198 : {"ctrlr", offsetof(struct rpc_remove_vhost_scsi_ctrlr_target, ctrlr), spdk_json_decode_string },
199 : {"scsi_target_num", offsetof(struct rpc_remove_vhost_scsi_ctrlr_target, scsi_target_num), spdk_json_decode_uint32},
200 : };
201 :
202 : static int
203 0 : rpc_vhost_scsi_controller_remove_target_finish_cb(struct spdk_vhost_dev *vdev, void *arg)
204 : {
205 0 : struct spdk_jsonrpc_request *request = arg;
206 :
207 0 : spdk_jsonrpc_send_bool_response(request, true);
208 0 : return 0;
209 : }
210 :
211 : static void
212 0 : rpc_vhost_scsi_controller_remove_target(struct spdk_jsonrpc_request *request,
213 : const struct spdk_json_val *params)
214 : {
215 0 : struct rpc_remove_vhost_scsi_ctrlr_target req = {0};
216 : struct spdk_vhost_dev *vdev;
217 : int rc;
218 :
219 0 : if (spdk_json_decode_object(params, rpc_vhost_remove_target,
220 : SPDK_COUNTOF(rpc_vhost_remove_target),
221 : &req)) {
222 0 : SPDK_DEBUGLOG(vhost_rpc, "spdk_json_decode_object failed\n");
223 0 : rc = -EINVAL;
224 0 : goto invalid;
225 : }
226 :
227 0 : spdk_vhost_lock();
228 0 : vdev = spdk_vhost_dev_find(req.ctrlr);
229 0 : if (vdev == NULL) {
230 0 : spdk_vhost_unlock();
231 0 : rc = -ENODEV;
232 0 : goto invalid;
233 : }
234 :
235 0 : rc = spdk_vhost_scsi_dev_remove_tgt(vdev, req.scsi_target_num,
236 : rpc_vhost_scsi_controller_remove_target_finish_cb,
237 : request);
238 0 : spdk_vhost_unlock();
239 0 : if (rc < 0) {
240 0 : goto invalid;
241 : }
242 :
243 0 : free_rpc_remove_vhost_scsi_ctrlr_target(&req);
244 0 : return;
245 :
246 0 : invalid:
247 0 : free_rpc_remove_vhost_scsi_ctrlr_target(&req);
248 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
249 : spdk_strerror(-rc));
250 : }
251 :
252 0 : SPDK_RPC_REGISTER("vhost_scsi_controller_remove_target",
253 : rpc_vhost_scsi_controller_remove_target, SPDK_RPC_RUNTIME)
254 :
255 : struct rpc_vhost_blk_ctrlr {
256 : char *ctrlr;
257 : char *dev_name;
258 : char *cpumask;
259 : char *transport;
260 : };
261 :
262 : static const struct spdk_json_object_decoder rpc_construct_vhost_blk_ctrlr[] = {
263 : {"ctrlr", offsetof(struct rpc_vhost_blk_ctrlr, ctrlr), spdk_json_decode_string },
264 : {"dev_name", offsetof(struct rpc_vhost_blk_ctrlr, dev_name), spdk_json_decode_string },
265 : {"cpumask", offsetof(struct rpc_vhost_blk_ctrlr, cpumask), spdk_json_decode_string, true},
266 : {"transport", offsetof(struct rpc_vhost_blk_ctrlr, transport), spdk_json_decode_string, true},
267 : };
268 :
269 : static void
270 0 : free_rpc_vhost_blk_ctrlr(struct rpc_vhost_blk_ctrlr *req)
271 : {
272 0 : free(req->ctrlr);
273 0 : free(req->dev_name);
274 0 : free(req->cpumask);
275 0 : free(req->transport);
276 0 : }
277 :
278 : static void
279 0 : rpc_vhost_create_blk_controller(struct spdk_jsonrpc_request *request,
280 : const struct spdk_json_val *params)
281 : {
282 0 : struct rpc_vhost_blk_ctrlr req = {0};
283 : int rc;
284 :
285 0 : if (spdk_json_decode_object_relaxed(params, rpc_construct_vhost_blk_ctrlr,
286 : SPDK_COUNTOF(rpc_construct_vhost_blk_ctrlr),
287 : &req)) {
288 0 : SPDK_DEBUGLOG(vhost_rpc, "spdk_json_decode_object failed\n");
289 0 : rc = -EINVAL;
290 0 : goto invalid;
291 : }
292 :
293 0 : rc = spdk_vhost_blk_construct(req.ctrlr, req.cpumask, req.dev_name, req.transport, params);
294 0 : if (rc < 0) {
295 0 : goto invalid;
296 : }
297 :
298 0 : free_rpc_vhost_blk_ctrlr(&req);
299 :
300 0 : spdk_jsonrpc_send_bool_response(request, true);
301 0 : return;
302 :
303 0 : invalid:
304 0 : free_rpc_vhost_blk_ctrlr(&req);
305 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
306 : spdk_strerror(-rc));
307 :
308 : }
309 0 : SPDK_RPC_REGISTER("vhost_create_blk_controller", rpc_vhost_create_blk_controller,
310 : SPDK_RPC_RUNTIME)
311 :
312 : struct rpc_delete_vhost_ctrlr {
313 : char *ctrlr;
314 : };
315 :
316 : static const struct spdk_json_object_decoder rpc_delete_vhost_ctrlr_decoder[] = {
317 : {"ctrlr", offsetof(struct rpc_delete_vhost_ctrlr, ctrlr), spdk_json_decode_string },
318 : };
319 :
320 : static void
321 0 : free_rpc_delete_vhost_ctrlr(struct rpc_delete_vhost_ctrlr *req)
322 : {
323 0 : free(req->ctrlr);
324 0 : }
325 :
326 : struct vhost_delete_ctrlr_context {
327 : struct spdk_jsonrpc_request *request;
328 : const struct spdk_json_val *params;
329 : };
330 :
331 : static void _rpc_vhost_delete_controller(void *arg);
332 :
333 : static void
334 0 : rpc_vhost_delete_controller(struct spdk_jsonrpc_request *request,
335 : const struct spdk_json_val *params)
336 : {
337 0 : struct rpc_delete_vhost_ctrlr req = {0};
338 : struct spdk_vhost_dev *vdev;
339 : int rc;
340 :
341 0 : if (spdk_json_decode_object(params, rpc_delete_vhost_ctrlr_decoder,
342 : SPDK_COUNTOF(rpc_delete_vhost_ctrlr_decoder), &req)) {
343 0 : SPDK_DEBUGLOG(vhost_rpc, "spdk_json_decode_object failed\n");
344 0 : rc = -EINVAL;
345 0 : goto invalid;
346 : }
347 :
348 0 : spdk_vhost_lock();
349 0 : vdev = spdk_vhost_dev_find(req.ctrlr);
350 0 : if (vdev == NULL) {
351 0 : spdk_vhost_unlock();
352 0 : rc = -ENODEV;
353 0 : goto invalid;
354 : }
355 0 : spdk_vhost_unlock();
356 :
357 0 : rc = spdk_vhost_dev_remove(vdev);
358 0 : if (rc < 0) {
359 0 : if (rc == -EBUSY) {
360 : struct vhost_delete_ctrlr_context *ctx;
361 :
362 0 : ctx = calloc(1, sizeof(*ctx));
363 0 : if (ctx == NULL) {
364 0 : SPDK_ERRLOG("Failed to allocate memory for vhost_delete_ctrlr context\n");
365 0 : rc = -ENOMEM;
366 0 : goto invalid;
367 : }
368 0 : ctx->request = request;
369 0 : ctx->params = params;
370 0 : spdk_thread_send_msg(spdk_get_thread(), _rpc_vhost_delete_controller, ctx);
371 0 : free_rpc_delete_vhost_ctrlr(&req);
372 0 : return;
373 : }
374 0 : goto invalid;
375 : }
376 :
377 0 : free_rpc_delete_vhost_ctrlr(&req);
378 :
379 0 : spdk_jsonrpc_send_bool_response(request, true);
380 0 : return;
381 :
382 0 : invalid:
383 0 : free_rpc_delete_vhost_ctrlr(&req);
384 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
385 : spdk_strerror(-rc));
386 :
387 : }
388 0 : SPDK_RPC_REGISTER("vhost_delete_controller", rpc_vhost_delete_controller, SPDK_RPC_RUNTIME)
389 :
390 0 : static void _rpc_vhost_delete_controller(void *arg)
391 : {
392 0 : struct vhost_delete_ctrlr_context *ctx = arg;
393 :
394 0 : rpc_vhost_delete_controller(ctx->request, ctx->params);
395 0 : free(ctx);
396 0 : }
397 :
398 : struct rpc_get_vhost_ctrlrs {
399 : char *name;
400 : };
401 :
402 : static void
403 0 : _rpc_get_vhost_controller(struct spdk_json_write_ctx *w, struct spdk_vhost_dev *vdev)
404 : {
405 0 : uint32_t delay_base_us, iops_threshold;
406 :
407 0 : spdk_vhost_get_coalescing(vdev, &delay_base_us, &iops_threshold);
408 :
409 0 : spdk_json_write_object_begin(w);
410 :
411 0 : spdk_json_write_named_string(w, "ctrlr", spdk_vhost_dev_get_name(vdev));
412 0 : spdk_json_write_named_string_fmt(w, "cpumask", "0x%s",
413 : spdk_cpuset_fmt(spdk_thread_get_cpumask(vdev->thread)));
414 0 : spdk_json_write_named_uint32(w, "delay_base_us", delay_base_us);
415 0 : spdk_json_write_named_uint32(w, "iops_threshold", iops_threshold);
416 0 : spdk_json_write_named_string(w, "socket", vdev->path);
417 0 : spdk_json_write_named_array_begin(w, "sessions");
418 0 : vhost_session_info_json(vdev, w);
419 0 : spdk_json_write_array_end(w);
420 :
421 0 : spdk_json_write_named_object_begin(w, "backend_specific");
422 0 : vhost_dump_info_json(vdev, w);
423 0 : spdk_json_write_object_end(w);
424 :
425 0 : spdk_json_write_object_end(w);
426 0 : }
427 :
428 : static const struct spdk_json_object_decoder rpc_get_vhost_ctrlrs_decoders[] = {
429 : {"name", offsetof(struct rpc_get_vhost_ctrlrs, name), spdk_json_decode_string, true},
430 : };
431 :
432 : static void
433 0 : free_rpc_get_vhost_ctrlrs(struct rpc_get_vhost_ctrlrs *req)
434 : {
435 0 : free(req->name);
436 0 : }
437 :
438 : static void
439 0 : rpc_vhost_get_controllers(struct spdk_jsonrpc_request *request,
440 : const struct spdk_json_val *params)
441 : {
442 0 : struct rpc_get_vhost_ctrlrs req = {0};
443 : struct spdk_json_write_ctx *w;
444 : struct spdk_vhost_dev *vdev;
445 : int rc;
446 :
447 0 : if (params && spdk_json_decode_object(params, rpc_get_vhost_ctrlrs_decoders,
448 : SPDK_COUNTOF(rpc_get_vhost_ctrlrs_decoders), &req)) {
449 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
450 0 : rc = -EINVAL;
451 0 : goto invalid;
452 : }
453 :
454 0 : spdk_vhost_lock();
455 0 : if (req.name != NULL) {
456 0 : vdev = spdk_vhost_dev_find(req.name);
457 0 : if (vdev == NULL) {
458 0 : spdk_vhost_unlock();
459 0 : rc = -ENODEV;
460 0 : goto invalid;
461 : }
462 :
463 0 : free_rpc_get_vhost_ctrlrs(&req);
464 :
465 0 : w = spdk_jsonrpc_begin_result(request);
466 0 : spdk_json_write_array_begin(w);
467 :
468 0 : _rpc_get_vhost_controller(w, vdev);
469 0 : spdk_vhost_unlock();
470 :
471 0 : spdk_json_write_array_end(w);
472 0 : spdk_jsonrpc_end_result(request, w);
473 0 : return;
474 : }
475 :
476 0 : free_rpc_get_vhost_ctrlrs(&req);
477 :
478 0 : w = spdk_jsonrpc_begin_result(request);
479 0 : spdk_json_write_array_begin(w);
480 :
481 0 : vdev = spdk_vhost_dev_next(NULL);
482 0 : while (vdev != NULL) {
483 0 : _rpc_get_vhost_controller(w, vdev);
484 0 : vdev = spdk_vhost_dev_next(vdev);
485 : }
486 0 : spdk_vhost_unlock();
487 :
488 0 : spdk_json_write_array_end(w);
489 0 : spdk_jsonrpc_end_result(request, w);
490 0 : return;
491 :
492 0 : invalid:
493 0 : free_rpc_get_vhost_ctrlrs(&req);
494 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
495 : spdk_strerror(-rc));
496 : }
497 0 : SPDK_RPC_REGISTER("vhost_get_controllers", rpc_vhost_get_controllers, SPDK_RPC_RUNTIME)
498 :
499 :
500 : struct rpc_vhost_ctrlr_coalescing {
501 : char *ctrlr;
502 : uint32_t delay_base_us;
503 : uint32_t iops_threshold;
504 : };
505 :
506 : static const struct spdk_json_object_decoder rpc_set_vhost_ctrlr_coalescing[] = {
507 : {"ctrlr", offsetof(struct rpc_vhost_ctrlr_coalescing, ctrlr), spdk_json_decode_string },
508 : {"delay_base_us", offsetof(struct rpc_vhost_ctrlr_coalescing, delay_base_us), spdk_json_decode_uint32},
509 : {"iops_threshold", offsetof(struct rpc_vhost_ctrlr_coalescing, iops_threshold), spdk_json_decode_uint32},
510 : };
511 :
512 : static void
513 0 : free_rpc_set_vhost_controllers_event_coalescing(struct rpc_vhost_ctrlr_coalescing *req)
514 : {
515 0 : free(req->ctrlr);
516 0 : }
517 :
518 : static void
519 0 : rpc_vhost_controller_set_coalescing(struct spdk_jsonrpc_request *request,
520 : const struct spdk_json_val *params)
521 : {
522 0 : struct rpc_vhost_ctrlr_coalescing req = {0};
523 : struct spdk_vhost_dev *vdev;
524 : int rc;
525 :
526 0 : if (spdk_json_decode_object(params, rpc_set_vhost_ctrlr_coalescing,
527 : SPDK_COUNTOF(rpc_set_vhost_ctrlr_coalescing), &req)) {
528 0 : SPDK_DEBUGLOG(vhost_rpc, "spdk_json_decode_object failed\n");
529 0 : rc = -EINVAL;
530 0 : goto invalid;
531 : }
532 :
533 0 : spdk_vhost_lock();
534 0 : vdev = spdk_vhost_dev_find(req.ctrlr);
535 0 : if (vdev == NULL) {
536 0 : spdk_vhost_unlock();
537 0 : rc = -ENODEV;
538 0 : goto invalid;
539 : }
540 :
541 0 : rc = spdk_vhost_set_coalescing(vdev, req.delay_base_us, req.iops_threshold);
542 0 : spdk_vhost_unlock();
543 0 : if (rc) {
544 0 : goto invalid;
545 : }
546 :
547 0 : free_rpc_set_vhost_controllers_event_coalescing(&req);
548 :
549 0 : spdk_jsonrpc_send_bool_response(request, true);
550 0 : return;
551 :
552 0 : invalid:
553 0 : free_rpc_set_vhost_controllers_event_coalescing(&req);
554 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
555 : spdk_strerror(-rc));
556 : }
557 0 : SPDK_RPC_REGISTER("vhost_controller_set_coalescing", rpc_vhost_controller_set_coalescing,
558 : SPDK_RPC_RUNTIME)
559 :
560 : struct rpc_get_transport {
561 : char *name;
562 : };
563 :
564 : static const struct spdk_json_object_decoder rpc_get_transport_decoders[] = {
565 : {"name", offsetof(struct rpc_get_transport, name), spdk_json_decode_string, true},
566 : };
567 :
568 : static void
569 0 : rpc_virtio_blk_get_transports(struct spdk_jsonrpc_request *request,
570 : const struct spdk_json_val *params)
571 : {
572 0 : struct rpc_get_transport req = { 0 };
573 : struct spdk_json_write_ctx *w;
574 0 : struct spdk_virtio_blk_transport *transport = NULL;
575 :
576 0 : if (params) {
577 0 : if (spdk_json_decode_object(params, rpc_get_transport_decoders,
578 : SPDK_COUNTOF(rpc_get_transport_decoders),
579 : &req)) {
580 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
581 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
582 0 : return;
583 : }
584 : }
585 :
586 0 : if (req.name) {
587 0 : transport = virtio_blk_tgt_get_transport(req.name);
588 0 : if (transport == NULL) {
589 0 : SPDK_ERRLOG("transport '%s' does not exist\n", req.name);
590 0 : spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
591 0 : free(req.name);
592 0 : return;
593 : }
594 : }
595 :
596 0 : w = spdk_jsonrpc_begin_result(request);
597 0 : spdk_json_write_array_begin(w);
598 :
599 0 : if (transport) {
600 0 : virtio_blk_transport_dump_opts(transport, w);
601 : } else {
602 0 : for (transport = virtio_blk_transport_get_first(); transport != NULL;
603 0 : transport = virtio_blk_transport_get_next(transport)) {
604 0 : virtio_blk_transport_dump_opts(transport, w);
605 : }
606 : }
607 :
608 0 : spdk_json_write_array_end(w);
609 0 : spdk_jsonrpc_end_result(request, w);
610 0 : free(req.name);
611 : }
612 0 : SPDK_RPC_REGISTER("virtio_blk_get_transports", rpc_virtio_blk_get_transports, SPDK_RPC_RUNTIME)
613 :
614 : struct rpc_virtio_blk_create_transport {
615 : char *name;
616 : };
617 :
618 : static const struct spdk_json_object_decoder rpc_create_virtio_blk_transport[] = {
619 : {"name", offsetof(struct rpc_virtio_blk_create_transport, name), spdk_json_decode_string},
620 : };
621 :
622 : static void
623 0 : free_rpc_virtio_blk_create_transport(struct rpc_virtio_blk_create_transport *req)
624 : {
625 0 : free(req->name);
626 0 : }
627 :
628 : static void
629 0 : rpc_virtio_blk_create_transport(struct spdk_jsonrpc_request *request,
630 : const struct spdk_json_val *params)
631 : {
632 0 : struct rpc_virtio_blk_create_transport req = {0};
633 : int rc;
634 :
635 0 : if (spdk_json_decode_object_relaxed(params, rpc_create_virtio_blk_transport,
636 : SPDK_COUNTOF(rpc_create_virtio_blk_transport), &req)) {
637 0 : SPDK_DEBUGLOG(vhost_rpc, "spdk_json_decode_object failed\n");
638 0 : rc = -EINVAL;
639 0 : goto invalid;
640 : }
641 :
642 0 : spdk_vhost_lock();
643 0 : rc = virtio_blk_transport_create(req.name, params);
644 0 : spdk_vhost_unlock();
645 0 : if (rc != 0) {
646 0 : goto invalid;
647 : }
648 :
649 0 : free_rpc_virtio_blk_create_transport(&req);
650 0 : spdk_jsonrpc_send_bool_response(request, true);
651 0 : return;
652 :
653 0 : invalid:
654 0 : free_rpc_virtio_blk_create_transport(&req);
655 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
656 : }
657 0 : SPDK_RPC_REGISTER("virtio_blk_create_transport", rpc_virtio_blk_create_transport,
658 : SPDK_RPC_RUNTIME)
659 :
660 0 : SPDK_LOG_REGISTER_COMPONENT(vhost_rpc)
|