xref: /spdk/module/bdev/virtio/bdev_virtio_rpc.c (revision 7506a7aa53d239f533af3bc768f0d2af55e735fe)
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 "spdk/stdinc.h"
35 
36 #include "spdk/string.h"
37 #include "spdk/rpc.h"
38 #include "spdk/util.h"
39 #include "spdk/log.h"
40 #include "spdk/thread.h"
41 
42 #include "bdev_virtio.h"
43 
44 #define SPDK_VIRTIO_USER_DEFAULT_VQ_COUNT		1
45 #define SPDK_VIRTIO_USER_DEFAULT_QUEUE_SIZE		512
46 
47 struct rpc_bdev_virtio_blk_hotplug {
48 	bool enabled;
49 	uint64_t period_us;
50 };
51 
52 static const struct spdk_json_object_decoder rpc_bdev_virtio_blk_hotplug_decoders[] = {
53 	{"enable", offsetof(struct rpc_bdev_virtio_blk_hotplug, enabled), spdk_json_decode_bool, false},
54 	{"period_us", offsetof(struct rpc_bdev_virtio_blk_hotplug, period_us), spdk_json_decode_uint64, true},
55 };
56 
57 static void
58 rpc_bdev_virtio_blk_set_hotplug(struct spdk_jsonrpc_request *request,
59 				const struct spdk_json_val *params)
60 {
61 	struct rpc_bdev_virtio_blk_hotplug req = {false, 0};
62 	int rc;
63 
64 	if (spdk_json_decode_object(params, rpc_bdev_virtio_blk_hotplug_decoders,
65 				    SPDK_COUNTOF(rpc_bdev_virtio_blk_hotplug_decoders), &req)) {
66 		SPDK_ERRLOG("spdk_json_decode_object failed\n");
67 		rc = -EINVAL;
68 		goto invalid;
69 	}
70 
71 	rc = bdev_virtio_pci_blk_set_hotplug(req.enabled, req.period_us);
72 	if (rc) {
73 		goto invalid;
74 	}
75 
76 	spdk_jsonrpc_send_bool_response(request, true);
77 	return;
78 invalid:
79 	spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, spdk_strerror(-rc));
80 }
81 SPDK_RPC_REGISTER("bdev_virtio_blk_set_hotplug", rpc_bdev_virtio_blk_set_hotplug, SPDK_RPC_RUNTIME)
82 
83 struct rpc_remove_virtio_dev {
84 	char *name;
85 };
86 
87 static const struct spdk_json_object_decoder rpc_remove_virtio_dev[] = {
88 	{"name", offsetof(struct rpc_remove_virtio_dev, name), spdk_json_decode_string },
89 };
90 
91 static void
92 rpc_bdev_virtio_detach_controller_cb(void *ctx, int errnum)
93 {
94 	struct spdk_jsonrpc_request *request = ctx;
95 
96 	if (errnum != 0) {
97 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
98 						 spdk_strerror(-errnum));
99 		return;
100 	}
101 
102 	spdk_jsonrpc_send_bool_response(request, true);
103 }
104 
105 static void
106 rpc_bdev_virtio_detach_controller(struct spdk_jsonrpc_request *request,
107 				  const struct spdk_json_val *params)
108 {
109 	struct rpc_remove_virtio_dev req = {NULL};
110 	int rc = 0;
111 
112 	if (spdk_json_decode_object(params, rpc_remove_virtio_dev,
113 				    SPDK_COUNTOF(rpc_remove_virtio_dev),
114 				    &req)) {
115 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
116 						 "spdk_json_decode_object failed");
117 		goto cleanup;
118 	}
119 
120 	rc = bdev_virtio_blk_dev_remove(req.name, rpc_bdev_virtio_detach_controller_cb, request);
121 	if (rc == -ENODEV) {
122 		rc = bdev_virtio_scsi_dev_remove(req.name, rpc_bdev_virtio_detach_controller_cb, request);
123 	}
124 
125 	if (rc != 0) {
126 		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
127 	}
128 
129 cleanup:
130 	free(req.name);
131 }
132 SPDK_RPC_REGISTER("bdev_virtio_detach_controller",
133 		  rpc_bdev_virtio_detach_controller, SPDK_RPC_RUNTIME)
134 
135 static void
136 rpc_bdev_virtio_scsi_get_devices(struct spdk_jsonrpc_request *request,
137 				 const struct spdk_json_val *params)
138 {
139 	struct spdk_json_write_ctx *w;
140 
141 	if (params != NULL) {
142 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
143 						 "bdev_virtio_scsi_get_devices requires no parameters");
144 		return;
145 	}
146 
147 	w = spdk_jsonrpc_begin_result(request);
148 	bdev_virtio_scsi_dev_list(w);
149 	spdk_jsonrpc_end_result(request, w);
150 }
151 SPDK_RPC_REGISTER("bdev_virtio_scsi_get_devices",
152 		  rpc_bdev_virtio_scsi_get_devices, SPDK_RPC_RUNTIME)
153 
154 struct rpc_bdev_virtio_attach_controller_ctx {
155 	char *name;
156 	char *trtype;
157 	char *traddr;
158 	char *dev_type;
159 	uint32_t vq_count;
160 	uint32_t vq_size;
161 	struct spdk_jsonrpc_request *request;
162 };
163 
164 static const struct spdk_json_object_decoder rpc_bdev_virtio_attach_controller_ctx[] = {
165 	{"name", offsetof(struct rpc_bdev_virtio_attach_controller_ctx, name), spdk_json_decode_string },
166 	{"trtype", offsetof(struct rpc_bdev_virtio_attach_controller_ctx, trtype), spdk_json_decode_string },
167 	{"traddr", offsetof(struct rpc_bdev_virtio_attach_controller_ctx, traddr), spdk_json_decode_string },
168 	{"dev_type", offsetof(struct rpc_bdev_virtio_attach_controller_ctx, dev_type), spdk_json_decode_string },
169 	{"vq_count", offsetof(struct rpc_bdev_virtio_attach_controller_ctx, vq_count), spdk_json_decode_uint32, true },
170 	{"vq_size", offsetof(struct rpc_bdev_virtio_attach_controller_ctx, vq_size), spdk_json_decode_uint32, true },
171 };
172 
173 static void
174 free_rpc_bdev_virtio_attach_controller_ctx(struct rpc_bdev_virtio_attach_controller_ctx *req)
175 {
176 	free(req->name);
177 	free(req->trtype);
178 	free(req->traddr);
179 	free(req->dev_type);
180 	free(req);
181 }
182 
183 static void
184 rpc_create_virtio_dev_cb(void *ctx, int result, struct spdk_bdev **bdevs, size_t cnt)
185 {
186 	struct rpc_bdev_virtio_attach_controller_ctx *req = ctx;
187 	struct spdk_json_write_ctx *w;
188 	size_t i;
189 
190 	if (result) {
191 		spdk_jsonrpc_send_error_response(req->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
192 						 spdk_strerror(-result));
193 		free_rpc_bdev_virtio_attach_controller_ctx(req);
194 		return;
195 	}
196 
197 	w = spdk_jsonrpc_begin_result(req->request);
198 	spdk_json_write_array_begin(w);
199 
200 	for (i = 0; i < cnt; i++) {
201 		spdk_json_write_string(w, spdk_bdev_get_name(bdevs[i]));
202 	}
203 
204 	spdk_json_write_array_end(w);
205 	spdk_jsonrpc_end_result(req->request, w);
206 
207 	free_rpc_bdev_virtio_attach_controller_ctx(ctx);
208 }
209 
210 static void
211 rpc_bdev_virtio_attach_controller(struct spdk_jsonrpc_request *request,
212 				  const struct spdk_json_val *params)
213 {
214 	struct rpc_bdev_virtio_attach_controller_ctx *req;
215 	struct spdk_bdev *bdev;
216 	struct spdk_pci_addr pci_addr;
217 	bool pci;
218 	int rc;
219 
220 	req = calloc(1, sizeof(*req));
221 	if (!req) {
222 		SPDK_ERRLOG("calloc() failed\n");
223 		spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM));
224 		return;
225 	}
226 
227 	if (spdk_json_decode_object(params, rpc_bdev_virtio_attach_controller_ctx,
228 				    SPDK_COUNTOF(rpc_bdev_virtio_attach_controller_ctx),
229 				    req)) {
230 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
231 						 "spdk_json_decode_object failed");
232 		goto cleanup;
233 	}
234 
235 	if (strcmp(req->trtype, "pci") == 0) {
236 		if (req->vq_count != 0 || req->vq_size != 0) {
237 			SPDK_ERRLOG("VQ count or size is not allowed for PCI transport type\n");
238 			spdk_jsonrpc_send_error_response(request, EINVAL,
239 							 "vq_count or vq_size is not allowed for PCI transport type.");
240 			goto cleanup;
241 		}
242 
243 		if (spdk_pci_addr_parse(&pci_addr, req->traddr) != 0) {
244 			SPDK_ERRLOG("Invalid PCI address '%s'\n", req->traddr);
245 			spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Invalid PCI address '%s'", req->traddr);
246 			goto cleanup;
247 		}
248 
249 		pci = true;
250 	} else if (strcmp(req->trtype, "user") == 0) {
251 		req->vq_count = req->vq_count == 0 ? SPDK_VIRTIO_USER_DEFAULT_VQ_COUNT : req->vq_count;
252 		req->vq_size = req->vq_size == 0 ? SPDK_VIRTIO_USER_DEFAULT_QUEUE_SIZE : req->vq_size;
253 		pci = false;
254 	} else {
255 		SPDK_ERRLOG("Invalid trtype '%s'\n", req->trtype);
256 		spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Invalid trtype '%s'", req->trtype);
257 		goto cleanup;
258 	}
259 
260 	req->request = request;
261 	if (strcmp(req->dev_type, "blk") == 0) {
262 		if (pci) {
263 			bdev = bdev_virtio_pci_blk_dev_create(req->name, &pci_addr);
264 		} else {
265 			bdev = bdev_virtio_user_blk_dev_create(req->name, req->traddr, req->vq_count, req->vq_size);
266 		}
267 
268 		/* Virtio blk doesn't use callback so call it manually to send result. */
269 		rc = bdev ? 0 : -EINVAL;
270 		rpc_create_virtio_dev_cb(req, rc, &bdev, bdev ? 1 : 0);
271 	} else if (strcmp(req->dev_type, "scsi") == 0) {
272 		if (pci) {
273 			rc = bdev_virtio_pci_scsi_dev_create(req->name, &pci_addr, rpc_create_virtio_dev_cb, req);
274 		} else {
275 			rc = bdev_virtio_user_scsi_dev_create(req->name, req->traddr, req->vq_count, req->vq_size,
276 							      rpc_create_virtio_dev_cb, req);
277 		}
278 
279 		if (rc < 0) {
280 			/* In case of error callback is not called so do it manually to send result. */
281 			rpc_create_virtio_dev_cb(req, rc, NULL, 0);
282 		}
283 	} else {
284 		SPDK_ERRLOG("Invalid dev_type '%s'\n", req->dev_type);
285 		spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Invalid dev_type '%s'", req->dev_type);
286 		goto cleanup;
287 	}
288 
289 	return;
290 
291 cleanup:
292 	free_rpc_bdev_virtio_attach_controller_ctx(req);
293 }
294 SPDK_RPC_REGISTER("bdev_virtio_attach_controller",
295 		  rpc_bdev_virtio_attach_controller, SPDK_RPC_RUNTIME);
296