xref: /spdk/module/bdev/uring/bdev_uring_rpc.c (revision b30d57cdad6d2bc75cc1e4e2ebbcebcb0d98dcfa)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "bdev_uring.h"
35 #include "spdk/rpc.h"
36 #include "spdk/util.h"
37 #include "spdk/string.h"
38 #include "spdk/log.h"
39 
40 /* Structure to hold the parameters for this RPC method. */
41 struct rpc_create_uring {
42 	char *name;
43 	char *filename;
44 	uint32_t block_size;
45 };
46 
47 /* Free the allocated memory resource after the RPC handling. */
48 static void
49 free_rpc_create_uring(struct rpc_create_uring *r)
50 {
51 	free(r->name);
52 	free(r->filename);
53 }
54 
55 /* Structure to decode the input parameters for this RPC method. */
56 static const struct spdk_json_object_decoder rpc_create_uring_decoders[] = {
57 	{"name", offsetof(struct rpc_create_uring, name), spdk_json_decode_string},
58 	{"filename", offsetof(struct rpc_create_uring, filename), spdk_json_decode_string},
59 	{"block_size", offsetof(struct rpc_create_uring, block_size), spdk_json_decode_uint32, true},
60 };
61 
62 /* Decode the parameters for this RPC method and properly create the uring
63  * device. Error status returned in the failed cases.
64  */
65 static void
66 rpc_bdev_uring_create(struct spdk_jsonrpc_request *request,
67 		      const struct spdk_json_val *params)
68 {
69 	struct rpc_create_uring req = {};
70 	struct spdk_json_write_ctx *w;
71 	struct spdk_bdev *bdev;
72 
73 	if (spdk_json_decode_object(params, rpc_create_uring_decoders,
74 				    SPDK_COUNTOF(rpc_create_uring_decoders),
75 				    &req)) {
76 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
77 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
78 						 "spdk_json_decode_object failed");
79 		goto cleanup;
80 	}
81 
82 	bdev = create_uring_bdev(req.name, req.filename, req.block_size);
83 	if (!bdev) {
84 		SPDK_ERRLOG("Unable to create URING bdev from file %s\n", req.filename);
85 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
86 						 "Unable to create URING bdev.");
87 		goto cleanup;
88 	}
89 
90 	w = spdk_jsonrpc_begin_result(request);
91 	spdk_json_write_string(w, req.name);
92 	spdk_jsonrpc_end_result(request, w);
93 
94 cleanup:
95 	free_rpc_create_uring(&req);
96 }
97 SPDK_RPC_REGISTER("bdev_uring_create", rpc_bdev_uring_create, SPDK_RPC_RUNTIME)
98 
99 struct rpc_delete_uring {
100 	char *name;
101 };
102 
103 static void
104 free_rpc_delete_uring(struct rpc_delete_uring *req)
105 {
106 	free(req->name);
107 }
108 
109 static const struct spdk_json_object_decoder rpc_delete_uring_decoders[] = {
110 	{"name", offsetof(struct rpc_delete_uring, name), spdk_json_decode_string},
111 };
112 
113 static void
114 _rpc_bdev_uring_delete_cb(void *cb_arg, int bdeverrno)
115 {
116 	struct spdk_jsonrpc_request *request = cb_arg;
117 
118 	spdk_jsonrpc_send_bool_response(request, bdeverrno == 0);
119 
120 }
121 
122 static void
123 rpc_bdev_uring_delete(struct spdk_jsonrpc_request *request,
124 		      const struct spdk_json_val *params)
125 {
126 	struct rpc_delete_uring req = {NULL};
127 	struct spdk_bdev *bdev;
128 
129 	if (spdk_json_decode_object(params, rpc_delete_uring_decoders,
130 				    SPDK_COUNTOF(rpc_delete_uring_decoders),
131 				    &req)) {
132 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
133 						 "spdk_json_decode_object failed");
134 		goto cleanup;
135 	}
136 
137 	bdev = spdk_bdev_get_by_name(req.name);
138 	if (bdev == NULL) {
139 		spdk_jsonrpc_send_error_response(request, -ENODEV, spdk_strerror(ENODEV));
140 		goto cleanup;
141 	}
142 
143 	delete_uring_bdev(bdev, _rpc_bdev_uring_delete_cb, request);
144 
145 cleanup:
146 	free_rpc_delete_uring(&req);
147 }
148 SPDK_RPC_REGISTER("bdev_uring_delete", rpc_bdev_uring_delete, SPDK_RPC_RUNTIME)
149