Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2022 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/bdev.h"
7 : : #include "spdk/log.h"
8 : : #include "spdk/rpc.h"
9 : : #include "spdk/env.h"
10 : : #include "spdk/string.h"
11 : : #include "spdk/util.h"
12 : : #include "spdk/thread.h"
13 : : #include "spdk/config.h"
14 : :
15 : : #include "vfu_virtio_internal.h"
16 : :
17 : : struct rpc_delete_vfu_endpoint {
18 : : char *name;
19 : : };
20 : :
21 : : static const struct spdk_json_object_decoder rpc_delete_vfu_endpoint_decode[] = {
22 : : {"name", offsetof(struct rpc_delete_vfu_endpoint, name), spdk_json_decode_string }
23 : : };
24 : :
25 : : static void
26 : 0 : free_rpc_delete_vfu_endpoint(struct rpc_delete_vfu_endpoint *req)
27 : : {
28 : 0 : free(req->name);
29 : 0 : }
30 : :
31 : : static void
32 : 0 : rpc_vfu_virtio_delete_endpoint(struct spdk_jsonrpc_request *request,
33 : : const struct spdk_json_val *params)
34 : : {
35 : 0 : struct rpc_delete_vfu_endpoint req = {0};
36 : : int rc;
37 : :
38 [ # # ]: 0 : if (spdk_json_decode_object(params, rpc_delete_vfu_endpoint_decode,
39 : : SPDK_COUNTOF(rpc_delete_vfu_endpoint_decode),
40 : : &req)) {
41 : 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
42 : 0 : rc = -EINVAL;
43 : 0 : goto invalid;
44 : : }
45 : :
46 : 0 : rc = spdk_vfu_delete_endpoint(req.name);
47 [ # # ]: 0 : if (rc < 0) {
48 : 0 : goto invalid;
49 : : }
50 : 0 : free_rpc_delete_vfu_endpoint(&req);
51 : :
52 : 0 : spdk_jsonrpc_send_bool_response(request, true);
53 : 0 : return;
54 : :
55 : 0 : invalid:
56 : 0 : free_rpc_delete_vfu_endpoint(&req);
57 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
58 : : spdk_strerror(-rc));
59 : : }
60 : 156 : SPDK_RPC_REGISTER("vfu_virtio_delete_endpoint", rpc_vfu_virtio_delete_endpoint,
61 : : SPDK_RPC_RUNTIME)
62 : :
63 : : struct rpc_vfu_virtio_create_blk {
64 : : char *name;
65 : : char *bdev_name;
66 : : char *cpumask;
67 : : uint16_t num_queues;
68 : : uint16_t qsize;
69 : : bool packed_ring;
70 : : };
71 : :
72 : : static const struct spdk_json_object_decoder rpc_construct_vfu_virtio_create_blk[] = {
73 : : {"name", offsetof(struct rpc_vfu_virtio_create_blk, name), spdk_json_decode_string },
74 : : {"bdev_name", offsetof(struct rpc_vfu_virtio_create_blk, bdev_name), spdk_json_decode_string },
75 : : {"cpumask", offsetof(struct rpc_vfu_virtio_create_blk, cpumask), spdk_json_decode_string, true},
76 : : {"num_queues", offsetof(struct rpc_vfu_virtio_create_blk, num_queues), spdk_json_decode_uint16, true },
77 : : {"qsize", offsetof(struct rpc_vfu_virtio_create_blk, qsize), spdk_json_decode_uint16, true },
78 : : {"packed_ring", offsetof(struct rpc_vfu_virtio_create_blk, packed_ring), spdk_json_decode_bool, true},
79 : : };
80 : :
81 : : static void
82 : 0 : free_rpc_vfu_virtio_create_blk(struct rpc_vfu_virtio_create_blk *req)
83 : : {
84 : 0 : free(req->name);
85 : 0 : free(req->bdev_name);
86 : 0 : free(req->cpumask);
87 : 0 : }
88 : :
89 : : static void
90 : 0 : rpc_vfu_virtio_create_blk_endpoint(struct spdk_jsonrpc_request *request,
91 : : const struct spdk_json_val *params)
92 : : {
93 : 0 : struct rpc_vfu_virtio_create_blk req = {0};
94 : : int rc;
95 : :
96 [ # # ]: 0 : if (spdk_json_decode_object(params, rpc_construct_vfu_virtio_create_blk,
97 : : SPDK_COUNTOF(rpc_construct_vfu_virtio_create_blk),
98 : : &req)) {
99 : 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
100 : 0 : rc = -EINVAL;
101 : 0 : goto invalid;
102 : : }
103 : :
104 : 0 : rc = spdk_vfu_create_endpoint(req.name, req.cpumask, "virtio_blk");
105 [ # # ]: 0 : if (rc) {
106 : 0 : SPDK_ERRLOG("Failed to create virtio_blk endpoint\n");
107 : 0 : goto invalid;
108 : : }
109 : :
110 : 0 : rc = vfu_virtio_blk_add_bdev(req.name, req.bdev_name, req.num_queues, req.qsize,
111 [ # # ]: 0 : req.packed_ring);
112 [ # # ]: 0 : if (rc < 0) {
113 : 0 : spdk_vfu_delete_endpoint(req.name);
114 : 0 : goto invalid;
115 : : }
116 : 0 : free_rpc_vfu_virtio_create_blk(&req);
117 : :
118 : 0 : spdk_jsonrpc_send_bool_response(request, true);
119 : 0 : return;
120 : :
121 : 0 : invalid:
122 : 0 : free_rpc_vfu_virtio_create_blk(&req);
123 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
124 : : spdk_strerror(-rc));
125 : : }
126 : 156 : SPDK_RPC_REGISTER("vfu_virtio_create_blk_endpoint", rpc_vfu_virtio_create_blk_endpoint,
127 : : SPDK_RPC_RUNTIME)
128 : :
129 : : struct rpc_vfu_virtio_scsi {
130 : : char *name;
131 : : uint8_t scsi_target_num;
132 : : char *bdev_name;
133 : : };
134 : :
135 : : static const struct spdk_json_object_decoder rpc_construct_vfu_virtio_scsi[] = {
136 : : {"name", offsetof(struct rpc_vfu_virtio_scsi, name), spdk_json_decode_string },
137 : : {"scsi_target_num", offsetof(struct rpc_vfu_virtio_scsi, scsi_target_num), spdk_json_decode_uint8 },
138 : : {"bdev_name", offsetof(struct rpc_vfu_virtio_scsi, bdev_name), spdk_json_decode_string },
139 : : };
140 : :
141 : : static void
142 : 0 : free_rpc_vfu_virtio_scsi(struct rpc_vfu_virtio_scsi *req)
143 : : {
144 : 0 : free(req->name);
145 : 0 : free(req->bdev_name);
146 : 0 : }
147 : :
148 : : static void
149 : 0 : rpc_vfu_virtio_scsi_add_target(struct spdk_jsonrpc_request *request,
150 : : const struct spdk_json_val *params)
151 : : {
152 : 0 : struct rpc_vfu_virtio_scsi req = {0};
153 : : int rc;
154 : :
155 [ # # ]: 0 : if (spdk_json_decode_object(params, rpc_construct_vfu_virtio_scsi,
156 : : SPDK_COUNTOF(rpc_construct_vfu_virtio_scsi),
157 : : &req)) {
158 : 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
159 : 0 : rc = -EINVAL;
160 : 0 : goto invalid;
161 : : }
162 : :
163 : 0 : rc = vfu_virtio_scsi_add_target(req.name, req.scsi_target_num, req.bdev_name);;
164 [ # # ]: 0 : if (rc < 0) {
165 : 0 : goto invalid;
166 : : }
167 : :
168 : 0 : free_rpc_vfu_virtio_scsi(&req);
169 : 0 : spdk_jsonrpc_send_bool_response(request, true);
170 : 0 : return;
171 : :
172 : 0 : invalid:
173 : 0 : free_rpc_vfu_virtio_scsi(&req);
174 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
175 : : spdk_strerror(-rc));
176 : : }
177 : 156 : SPDK_RPC_REGISTER("vfu_virtio_scsi_add_target", rpc_vfu_virtio_scsi_add_target,
178 : : SPDK_RPC_RUNTIME)
179 : :
180 : : struct rpc_vfu_virtio_scsi_remove {
181 : : char *name;
182 : : uint8_t scsi_target_num;
183 : : };
184 : :
185 : : static const struct spdk_json_object_decoder rpc_remove_vfu_virtio_scsi_target[] = {
186 : : {"name", offsetof(struct rpc_vfu_virtio_scsi_remove, name), spdk_json_decode_string },
187 : : {"scsi_target_num", offsetof(struct rpc_vfu_virtio_scsi_remove, scsi_target_num), spdk_json_decode_uint8 },
188 : : };
189 : :
190 : : static void
191 : 0 : free_rpc_vfu_virtio_scsi_remove(struct rpc_vfu_virtio_scsi_remove *req)
192 : : {
193 : 0 : free(req->name);
194 : 0 : }
195 : :
196 : : static void
197 : 0 : rpc_vfu_virtio_scsi_remove_target(struct spdk_jsonrpc_request *request,
198 : : const struct spdk_json_val *params)
199 : : {
200 : 0 : struct rpc_vfu_virtio_scsi_remove req = {0};
201 : : int rc;
202 : :
203 [ # # ]: 0 : if (spdk_json_decode_object(params, rpc_remove_vfu_virtio_scsi_target,
204 : : SPDK_COUNTOF(rpc_remove_vfu_virtio_scsi_target),
205 : : &req)) {
206 : 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
207 : 0 : rc = -EINVAL;
208 : 0 : goto invalid;
209 : : }
210 : :
211 : 0 : rc = vfu_virtio_scsi_remove_target(req.name, req.scsi_target_num);
212 [ # # ]: 0 : if (rc < 0) {
213 : 0 : goto invalid;
214 : : }
215 : :
216 : 0 : free_rpc_vfu_virtio_scsi_remove(&req);
217 : 0 : spdk_jsonrpc_send_bool_response(request, true);
218 : 0 : return;
219 : :
220 : 0 : invalid:
221 : 0 : free_rpc_vfu_virtio_scsi_remove(&req);
222 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
223 : : spdk_strerror(-rc));
224 : : }
225 : 156 : SPDK_RPC_REGISTER("vfu_virtio_scsi_remove_target", rpc_vfu_virtio_scsi_remove_target,
226 : : SPDK_RPC_RUNTIME)
227 : :
228 : : struct rpc_vfu_virtio_create_scsi {
229 : : char *name;
230 : : char *cpumask;
231 : : uint16_t num_io_queues;
232 : : uint16_t qsize;
233 : : bool packed_ring;
234 : : };
235 : :
236 : : static const struct spdk_json_object_decoder rpc_construct_vfu_virtio_create_scsi[] = {
237 : : {"name", offsetof(struct rpc_vfu_virtio_create_scsi, name), spdk_json_decode_string },
238 : : {"cpumask", offsetof(struct rpc_vfu_virtio_create_scsi, cpumask), spdk_json_decode_string, true},
239 : : {"num_io_queues", offsetof(struct rpc_vfu_virtio_create_scsi, num_io_queues), spdk_json_decode_uint16, true },
240 : : {"qsize", offsetof(struct rpc_vfu_virtio_create_scsi, qsize), spdk_json_decode_uint16, true },
241 : : {"packed_ring", offsetof(struct rpc_vfu_virtio_create_scsi, packed_ring), spdk_json_decode_bool, true},
242 : : };
243 : :
244 : : static void
245 : 0 : free_rpc_vfu_virtio_create_scsi(struct rpc_vfu_virtio_create_scsi *req)
246 : : {
247 : 0 : free(req->name);
248 : 0 : free(req->cpumask);
249 : 0 : }
250 : :
251 : : static void
252 : 0 : rpc_vfu_virtio_create_scsi_endpoint(struct spdk_jsonrpc_request *request,
253 : : const struct spdk_json_val *params)
254 : : {
255 : 0 : struct rpc_vfu_virtio_create_scsi req = {0};
256 : : int rc;
257 : :
258 [ # # ]: 0 : if (spdk_json_decode_object(params, rpc_construct_vfu_virtio_create_scsi,
259 : : SPDK_COUNTOF(rpc_construct_vfu_virtio_create_scsi),
260 : : &req)) {
261 : 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
262 : 0 : rc = -EINVAL;
263 : 0 : goto invalid;
264 : : }
265 : :
266 : 0 : rc = spdk_vfu_create_endpoint(req.name, req.cpumask, "virtio_scsi");
267 [ # # ]: 0 : if (rc) {
268 : 0 : SPDK_ERRLOG("Failed to create virtio_blk endpoint\n");
269 : 0 : goto invalid;
270 : : }
271 : :
272 [ # # ]: 0 : rc = vfu_virtio_scsi_set_options(req.name, req.num_io_queues, req.qsize, req.packed_ring);
273 [ # # ]: 0 : if (rc < 0) {
274 : 0 : spdk_vfu_delete_endpoint(req.name);
275 : 0 : goto invalid;
276 : : }
277 : 0 : free_rpc_vfu_virtio_create_scsi(&req);
278 : :
279 : 0 : spdk_jsonrpc_send_bool_response(request, true);
280 : 0 : return;
281 : :
282 : 0 : invalid:
283 : 0 : free_rpc_vfu_virtio_create_scsi(&req);
284 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
285 : : spdk_strerror(-rc));
286 : : }
287 : 156 : SPDK_RPC_REGISTER("vfu_virtio_create_scsi_endpoint", rpc_vfu_virtio_create_scsi_endpoint,
288 : : SPDK_RPC_RUNTIME)
289 : :
290 : : #ifdef SPDK_CONFIG_FSDEV
291 : : struct rpc_vfu_virtio_create_fs {
292 : : char *name;
293 : : char *fsdev_name;
294 : : char *tag;
295 : : char *cpumask;
296 : : uint16_t num_queues;
297 : : uint16_t qsize;
298 : : bool packed_ring;
299 : : struct spdk_jsonrpc_request *request;
300 : : };
301 : :
302 : : static const struct spdk_json_object_decoder rpc_construct_vfu_virtio_create_fs[] = {
303 : : {"name", offsetof(struct rpc_vfu_virtio_create_fs, name), spdk_json_decode_string },
304 : : {"fsdev_name", offsetof(struct rpc_vfu_virtio_create_fs, fsdev_name), spdk_json_decode_string },
305 : : {"tag", offsetof(struct rpc_vfu_virtio_create_fs, tag), spdk_json_decode_string },
306 : : {"cpumask", offsetof(struct rpc_vfu_virtio_create_fs, cpumask), spdk_json_decode_string, true},
307 : : {"num_queues", offsetof(struct rpc_vfu_virtio_create_fs, num_queues), spdk_json_decode_uint16, true },
308 : : {"qsize", offsetof(struct rpc_vfu_virtio_create_fs, qsize), spdk_json_decode_uint16, true },
309 : : {"packed_ring", offsetof(struct rpc_vfu_virtio_create_fs, packed_ring), spdk_json_decode_bool, true},
310 : : };
311 : :
312 : : static void
313 : 0 : free_rpc_vfu_virtio_create_fs(struct rpc_vfu_virtio_create_fs *req)
314 : : {
315 [ # # ]: 0 : if (req) {
316 : 0 : free(req->name);
317 : 0 : free(req->fsdev_name);
318 : 0 : free(req->tag);
319 : 0 : free(req->cpumask);
320 : 0 : free(req);
321 : : }
322 : 0 : }
323 : :
324 : : static void
325 : 0 : rpc_vfu_virtio_create_fs_endpoint_cpl(void *cb_arg, int status)
326 : : {
327 : 0 : struct rpc_vfu_virtio_create_fs *req = cb_arg;
328 : :
329 : 0 : spdk_jsonrpc_send_bool_response(req->request, true);
330 : :
331 : 0 : free_rpc_vfu_virtio_create_fs(req);
332 : 0 : }
333 : :
334 : : static void
335 : 0 : rpc_vfu_virtio_create_fs_endpoint(struct spdk_jsonrpc_request *request,
336 : : const struct spdk_json_val *params)
337 : : {
338 : : struct rpc_vfu_virtio_create_fs *req;
339 : : int rc;
340 : :
341 : 0 : req = calloc(1, sizeof(*req));
342 [ # # ]: 0 : if (!req) {
343 : 0 : SPDK_ERRLOG("cannot allocate req\n");
344 : 0 : rc = -ENOMEM;
345 : 0 : goto invalid;
346 : : }
347 : :
348 [ # # ]: 0 : if (spdk_json_decode_object(params, rpc_construct_vfu_virtio_create_fs,
349 : : SPDK_COUNTOF(rpc_construct_vfu_virtio_create_fs),
350 : : req)) {
351 : 0 : SPDK_ERRLOG("spdk_json_decode_object failed\n");
352 : 0 : rc = -EINVAL;
353 : 0 : goto invalid;
354 : : }
355 : :
356 : 0 : rc = spdk_vfu_create_endpoint(req->name, req->cpumask, "virtio_fs");
357 [ # # ]: 0 : if (rc) {
358 : 0 : SPDK_ERRLOG("Failed to create virtio_blk endpoint\n");
359 : 0 : goto invalid;
360 : : }
361 : :
362 : 0 : req->request = request;
363 : :
364 : 0 : rc = vfu_virtio_fs_add_fsdev(req->name, req->fsdev_name, req->tag, req->num_queues, req->qsize,
365 [ # # ]: 0 : req->packed_ring, rpc_vfu_virtio_create_fs_endpoint_cpl, req);
366 [ # # ]: 0 : if (rc < 0) {
367 : 0 : spdk_vfu_delete_endpoint(req->name);
368 : 0 : goto invalid;
369 : : }
370 : :
371 : 0 : return;
372 : :
373 : 0 : invalid:
374 : 0 : free_rpc_vfu_virtio_create_fs(req);
375 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
376 : : spdk_strerror(-rc));
377 : : }
378 : 156 : SPDK_RPC_REGISTER("vfu_virtio_create_fs_endpoint", rpc_vfu_virtio_create_fs_endpoint,
379 : : SPDK_RPC_RUNTIME)
380 : : #endif
|