Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause
2 : * Copyright (C) 2018 Intel Corporation.
3 : * Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
4 : * All rights reserved.
5 : */
6 :
7 : #include "vbdev_passthru.h"
8 : #include "spdk/rpc.h"
9 : #include "spdk/util.h"
10 : #include "spdk/string.h"
11 : #include "spdk/log.h"
12 :
13 : /* Structure to hold the parameters for this RPC method. */
14 : struct rpc_bdev_passthru_create {
15 : char *base_bdev_name;
16 : char *name;
17 : struct spdk_uuid uuid;
18 : };
19 :
20 : /* Free the allocated memory resource after the RPC handling. */
21 : static void
22 0 : free_rpc_bdev_passthru_create(struct rpc_bdev_passthru_create *r)
23 : {
24 0 : free(r->base_bdev_name);
25 0 : free(r->name);
26 0 : }
27 :
28 : /* Structure to decode the input parameters for this RPC method. */
29 : static const struct spdk_json_object_decoder rpc_bdev_passthru_create_decoders[] = {
30 : {"base_bdev_name", offsetof(struct rpc_bdev_passthru_create, base_bdev_name), spdk_json_decode_string},
31 : {"name", offsetof(struct rpc_bdev_passthru_create, name), spdk_json_decode_string},
32 : {"uuid", offsetof(struct rpc_bdev_passthru_create, uuid), spdk_json_decode_uuid, true},
33 : };
34 :
35 : /* Decode the parameters for this RPC method and properly construct the passthru
36 : * device. Error status returned in the failed cases.
37 : */
38 : static void
39 0 : rpc_bdev_passthru_create(struct spdk_jsonrpc_request *request,
40 : const struct spdk_json_val *params)
41 : {
42 0 : struct rpc_bdev_passthru_create req = {NULL};
43 : struct spdk_json_write_ctx *w;
44 : int rc;
45 :
46 0 : if (spdk_json_decode_object(params, rpc_bdev_passthru_create_decoders,
47 : SPDK_COUNTOF(rpc_bdev_passthru_create_decoders),
48 : &req)) {
49 0 : SPDK_DEBUGLOG(vbdev_passthru, "spdk_json_decode_object failed\n");
50 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
51 : "spdk_json_decode_object failed");
52 0 : goto cleanup;
53 : }
54 :
55 0 : rc = bdev_passthru_create_disk(req.base_bdev_name, req.name, &req.uuid);
56 0 : if (rc != 0) {
57 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
58 0 : goto cleanup;
59 : }
60 :
61 0 : w = spdk_jsonrpc_begin_result(request);
62 0 : spdk_json_write_string(w, req.name);
63 0 : spdk_jsonrpc_end_result(request, w);
64 :
65 0 : cleanup:
66 0 : free_rpc_bdev_passthru_create(&req);
67 0 : }
68 0 : SPDK_RPC_REGISTER("bdev_passthru_create", rpc_bdev_passthru_create, SPDK_RPC_RUNTIME)
69 :
70 : struct rpc_bdev_passthru_delete {
71 : char *name;
72 : };
73 :
74 : static void
75 0 : free_rpc_bdev_passthru_delete(struct rpc_bdev_passthru_delete *req)
76 : {
77 0 : free(req->name);
78 0 : }
79 :
80 : static const struct spdk_json_object_decoder rpc_bdev_passthru_delete_decoders[] = {
81 : {"name", offsetof(struct rpc_bdev_passthru_delete, name), spdk_json_decode_string},
82 : };
83 :
84 : static void
85 0 : rpc_bdev_passthru_delete_cb(void *cb_arg, int bdeverrno)
86 : {
87 0 : struct spdk_jsonrpc_request *request = cb_arg;
88 :
89 0 : if (bdeverrno == 0) {
90 0 : spdk_jsonrpc_send_bool_response(request, true);
91 : } else {
92 0 : spdk_jsonrpc_send_error_response(request, bdeverrno, spdk_strerror(-bdeverrno));
93 : }
94 0 : }
95 :
96 : static void
97 0 : rpc_bdev_passthru_delete(struct spdk_jsonrpc_request *request,
98 : const struct spdk_json_val *params)
99 : {
100 0 : struct rpc_bdev_passthru_delete req = {NULL};
101 :
102 0 : if (spdk_json_decode_object(params, rpc_bdev_passthru_delete_decoders,
103 : SPDK_COUNTOF(rpc_bdev_passthru_delete_decoders),
104 : &req)) {
105 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
106 : "spdk_json_decode_object failed");
107 0 : goto cleanup;
108 : }
109 :
110 0 : bdev_passthru_delete_disk(req.name, rpc_bdev_passthru_delete_cb, request);
111 :
112 0 : cleanup:
113 0 : free_rpc_bdev_passthru_delete(&req);
114 0 : }
115 0 : SPDK_RPC_REGISTER("bdev_passthru_delete", rpc_bdev_passthru_delete, SPDK_RPC_RUNTIME)
|