4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #include "spdk/stdinc.h"
36 #include "spdk/string.h"
38 #include "spdk/util.h"
39 #include "spdk_internal/log.h"
41 #include "bdev_virtio.h"
43 #define SPDK_VIRTIO_USER_DEFAULT_VQ_COUNT 1
44 #define SPDK_VIRTIO_USER_DEFAULT_QUEUE_SIZE 512
46 struct rpc_remove_virtio_dev
{
50 static const struct spdk_json_object_decoder rpc_remove_virtio_dev
[] = {
51 {"name", offsetof(struct rpc_remove_virtio_dev
, name
), spdk_json_decode_string
},
55 spdk_rpc_remove_virtio_bdev_cb(void *ctx
, int errnum
)
57 struct spdk_jsonrpc_request
*request
= ctx
;
58 struct spdk_json_write_ctx
*w
;
61 spdk_jsonrpc_send_error_response(request
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
,
62 spdk_strerror(-errnum
));
66 w
= spdk_jsonrpc_begin_result(request
);
71 spdk_json_write_bool(w
, true);
72 spdk_jsonrpc_end_result(request
, w
);
76 spdk_rpc_remove_virtio_bdev(struct spdk_jsonrpc_request
*request
,
77 const struct spdk_json_val
*params
)
79 struct rpc_remove_virtio_dev req
= {NULL
};
82 if (spdk_json_decode_object(params
, rpc_remove_virtio_dev
,
83 SPDK_COUNTOF(rpc_remove_virtio_dev
),
89 rc
= bdev_virtio_blk_dev_remove(req
.name
, spdk_rpc_remove_virtio_bdev_cb
, request
);
91 rc
= bdev_virtio_scsi_dev_remove(req
.name
, spdk_rpc_remove_virtio_bdev_cb
, request
);
103 spdk_jsonrpc_send_error_response(request
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
,
107 SPDK_RPC_REGISTER("remove_virtio_bdev", spdk_rpc_remove_virtio_bdev
, SPDK_RPC_RUNTIME
);
110 spdk_rpc_get_virtio_scsi_devs(struct spdk_jsonrpc_request
*request
,
111 const struct spdk_json_val
*params
)
113 struct spdk_json_write_ctx
*w
;
115 if (params
!= NULL
) {
116 spdk_jsonrpc_send_error_response(request
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
,
117 "get_virtio_scsi_devs requires no parameters");
121 w
= spdk_jsonrpc_begin_result(request
);
126 bdev_virtio_scsi_dev_list(w
);
127 spdk_jsonrpc_end_result(request
, w
);
129 SPDK_RPC_REGISTER("get_virtio_scsi_devs", spdk_rpc_get_virtio_scsi_devs
, SPDK_RPC_RUNTIME
)
131 struct rpc_construct_virtio_dev
{
138 struct spdk_jsonrpc_request
*request
;
141 static const struct spdk_json_object_decoder rpc_construct_virtio_dev
[] = {
142 {"name", offsetof(struct rpc_construct_virtio_dev
, name
), spdk_json_decode_string
},
143 {"trtype", offsetof(struct rpc_construct_virtio_dev
, trtype
), spdk_json_decode_string
},
144 {"traddr", offsetof(struct rpc_construct_virtio_dev
, traddr
), spdk_json_decode_string
},
145 {"dev_type", offsetof(struct rpc_construct_virtio_dev
, dev_type
), spdk_json_decode_string
},
146 {"vq_count", offsetof(struct rpc_construct_virtio_dev
, vq_count
), spdk_json_decode_uint32
, true },
147 {"vq_size", offsetof(struct rpc_construct_virtio_dev
, vq_size
), spdk_json_decode_uint32
, true },
151 free_rpc_construct_virtio_dev(struct rpc_construct_virtio_dev
*req
)
161 spdk_rpc_create_virtio_dev_cb(void *ctx
, int result
, struct spdk_bdev
**bdevs
, size_t cnt
)
163 struct rpc_construct_virtio_dev
*req
= ctx
;
164 struct spdk_json_write_ctx
*w
;
168 spdk_jsonrpc_send_error_response(req
->request
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
,
169 spdk_strerror(-result
));
170 free_rpc_construct_virtio_dev(req
);
174 w
= spdk_jsonrpc_begin_result(req
->request
);
176 spdk_json_write_array_begin(w
);
178 for (i
= 0; i
< cnt
; i
++) {
179 spdk_json_write_string(w
, spdk_bdev_get_name(bdevs
[i
]));
182 spdk_json_write_array_end(w
);
183 spdk_jsonrpc_end_result(req
->request
, w
);
186 free_rpc_construct_virtio_dev(ctx
);
190 spdk_rpc_construct_virtio_dev(struct spdk_jsonrpc_request
*request
,
191 const struct spdk_json_val
*params
)
193 struct rpc_construct_virtio_dev
*req
;
194 struct spdk_bdev
*bdev
;
195 struct spdk_pci_addr pci_addr
;
199 req
= calloc(1, sizeof(*req
));
201 SPDK_ERRLOG("calloc() failed\n");
202 spdk_jsonrpc_send_error_response(request
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
, spdk_strerror(ENOMEM
));
206 if (spdk_json_decode_object(params
, rpc_construct_virtio_dev
,
207 SPDK_COUNTOF(rpc_construct_virtio_dev
),
209 spdk_jsonrpc_send_error_response(request
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
, spdk_strerror(EINVAL
));
213 if (strcmp(req
->trtype
, "pci") == 0) {
214 if (req
->vq_count
!= 0 || req
->vq_size
!= 0) {
215 SPDK_ERRLOG("VQ count or size is not allowed for PCI transport type\n");
216 spdk_jsonrpc_send_error_response(request
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
,
217 "vq_count or vq_size is not allowed for PCI transport type.");
221 if (spdk_pci_addr_parse(&pci_addr
, req
->traddr
) != 0) {
222 SPDK_ERRLOG("Invalid PCI address '%s'\n", req
->traddr
);
223 spdk_jsonrpc_send_error_response_fmt(request
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
,
224 "Invalid PCI address '%s'", req
->traddr
);
229 } else if (strcmp(req
->trtype
, "user") == 0) {
230 req
->vq_count
= req
->vq_count
== 0 ? SPDK_VIRTIO_USER_DEFAULT_VQ_COUNT
: req
->vq_count
;
231 req
->vq_size
= req
->vq_size
== 0 ? SPDK_VIRTIO_USER_DEFAULT_QUEUE_SIZE
: req
->vq_size
;
234 SPDK_ERRLOG("Invalid trtype '%s'\n", req
->trtype
);
235 spdk_jsonrpc_send_error_response_fmt(request
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
,
236 "Invalid trtype '%s'", req
->trtype
);
240 req
->request
= request
;
241 if (strcmp(req
->dev_type
, "blk") == 0) {
243 bdev
= bdev_virtio_pci_blk_dev_create(req
->name
, &pci_addr
);
245 bdev
= bdev_virtio_user_blk_dev_create(req
->name
, req
->traddr
, req
->vq_count
, req
->vq_size
);
248 /* Virtio blk doesn't use callback so call it manually to send result. */
249 rc
= bdev
? 0 : -EINVAL
;
250 spdk_rpc_create_virtio_dev_cb(req
, rc
, &bdev
, bdev
? 1 : 0);
251 } else if (strcmp(req
->dev_type
, "scsi") == 0) {
253 rc
= bdev_virtio_pci_scsi_dev_create(req
->name
, &pci_addr
, spdk_rpc_create_virtio_dev_cb
, req
);
255 rc
= bdev_virtio_user_scsi_dev_create(req
->name
, req
->traddr
, req
->vq_count
, req
->vq_size
,
256 spdk_rpc_create_virtio_dev_cb
, req
);
260 /* In case of error callback is not called so do it manually to send result. */
261 spdk_rpc_create_virtio_dev_cb(req
, rc
, NULL
, 0);
264 SPDK_ERRLOG("Invalid dev_type '%s'\n", req
->dev_type
);
265 spdk_jsonrpc_send_error_response_fmt(request
, SPDK_JSONRPC_ERROR_INVALID_PARAMS
,
266 "Invalid dev_type '%s'", req
->dev_type
);
272 free_rpc_construct_virtio_dev(req
);
274 SPDK_RPC_REGISTER("construct_virtio_dev", spdk_rpc_construct_virtio_dev
, SPDK_RPC_RUNTIME
);