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 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_virtio_detach_controller, remove_virtio_bdev) 135 136 static void 137 rpc_bdev_virtio_scsi_get_devices(struct spdk_jsonrpc_request *request, 138 const struct spdk_json_val *params) 139 { 140 struct spdk_json_write_ctx *w; 141 142 if (params != NULL) { 143 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 144 "bdev_virtio_scsi_get_devices requires no parameters"); 145 return; 146 } 147 148 w = spdk_jsonrpc_begin_result(request); 149 bdev_virtio_scsi_dev_list(w); 150 spdk_jsonrpc_end_result(request, w); 151 } 152 SPDK_RPC_REGISTER("bdev_virtio_scsi_get_devices", 153 rpc_bdev_virtio_scsi_get_devices, SPDK_RPC_RUNTIME) 154 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_virtio_scsi_get_devices, get_virtio_scsi_devs) 155 156 struct rpc_bdev_virtio_attach_controller_ctx { 157 char *name; 158 char *trtype; 159 char *traddr; 160 char *dev_type; 161 uint32_t vq_count; 162 uint32_t vq_size; 163 struct spdk_jsonrpc_request *request; 164 }; 165 166 static const struct spdk_json_object_decoder rpc_bdev_virtio_attach_controller_ctx[] = { 167 {"name", offsetof(struct rpc_bdev_virtio_attach_controller_ctx, name), spdk_json_decode_string }, 168 {"trtype", offsetof(struct rpc_bdev_virtio_attach_controller_ctx, trtype), spdk_json_decode_string }, 169 {"traddr", offsetof(struct rpc_bdev_virtio_attach_controller_ctx, traddr), spdk_json_decode_string }, 170 {"dev_type", offsetof(struct rpc_bdev_virtio_attach_controller_ctx, dev_type), spdk_json_decode_string }, 171 {"vq_count", offsetof(struct rpc_bdev_virtio_attach_controller_ctx, vq_count), spdk_json_decode_uint32, true }, 172 {"vq_size", offsetof(struct rpc_bdev_virtio_attach_controller_ctx, vq_size), spdk_json_decode_uint32, true }, 173 }; 174 175 static void 176 free_rpc_bdev_virtio_attach_controller_ctx(struct rpc_bdev_virtio_attach_controller_ctx *req) 177 { 178 free(req->name); 179 free(req->trtype); 180 free(req->traddr); 181 free(req->dev_type); 182 free(req); 183 } 184 185 static void 186 rpc_create_virtio_dev_cb(void *ctx, int result, struct spdk_bdev **bdevs, size_t cnt) 187 { 188 struct rpc_bdev_virtio_attach_controller_ctx *req = ctx; 189 struct spdk_json_write_ctx *w; 190 size_t i; 191 192 if (result) { 193 spdk_jsonrpc_send_error_response(req->request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 194 spdk_strerror(-result)); 195 free_rpc_bdev_virtio_attach_controller_ctx(req); 196 return; 197 } 198 199 w = spdk_jsonrpc_begin_result(req->request); 200 spdk_json_write_array_begin(w); 201 202 for (i = 0; i < cnt; i++) { 203 spdk_json_write_string(w, spdk_bdev_get_name(bdevs[i])); 204 } 205 206 spdk_json_write_array_end(w); 207 spdk_jsonrpc_end_result(req->request, w); 208 209 free_rpc_bdev_virtio_attach_controller_ctx(ctx); 210 } 211 212 static void 213 rpc_bdev_virtio_attach_controller(struct spdk_jsonrpc_request *request, 214 const struct spdk_json_val *params) 215 { 216 struct rpc_bdev_virtio_attach_controller_ctx *req; 217 struct spdk_bdev *bdev; 218 struct spdk_pci_addr pci_addr; 219 bool pci; 220 int rc; 221 222 req = calloc(1, sizeof(*req)); 223 if (!req) { 224 SPDK_ERRLOG("calloc() failed\n"); 225 spdk_jsonrpc_send_error_response(request, -ENOMEM, spdk_strerror(ENOMEM)); 226 return; 227 } 228 229 if (spdk_json_decode_object(params, rpc_bdev_virtio_attach_controller_ctx, 230 SPDK_COUNTOF(rpc_bdev_virtio_attach_controller_ctx), 231 req)) { 232 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 233 "spdk_json_decode_object failed"); 234 goto cleanup; 235 } 236 237 if (strcmp(req->trtype, "pci") == 0) { 238 if (req->vq_count != 0 || req->vq_size != 0) { 239 SPDK_ERRLOG("VQ count or size is not allowed for PCI transport type\n"); 240 spdk_jsonrpc_send_error_response(request, EINVAL, 241 "vq_count or vq_size is not allowed for PCI transport type."); 242 goto cleanup; 243 } 244 245 if (spdk_pci_addr_parse(&pci_addr, req->traddr) != 0) { 246 SPDK_ERRLOG("Invalid PCI address '%s'\n", req->traddr); 247 spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Invalid PCI address '%s'", req->traddr); 248 goto cleanup; 249 } 250 251 pci = true; 252 } else if (strcmp(req->trtype, "user") == 0) { 253 req->vq_count = req->vq_count == 0 ? SPDK_VIRTIO_USER_DEFAULT_VQ_COUNT : req->vq_count; 254 req->vq_size = req->vq_size == 0 ? SPDK_VIRTIO_USER_DEFAULT_QUEUE_SIZE : req->vq_size; 255 pci = false; 256 } else { 257 SPDK_ERRLOG("Invalid trtype '%s'\n", req->trtype); 258 spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Invalid trtype '%s'", req->trtype); 259 goto cleanup; 260 } 261 262 req->request = request; 263 if (strcmp(req->dev_type, "blk") == 0) { 264 if (pci) { 265 bdev = bdev_virtio_pci_blk_dev_create(req->name, &pci_addr); 266 } else { 267 bdev = bdev_virtio_user_blk_dev_create(req->name, req->traddr, req->vq_count, req->vq_size); 268 } 269 270 /* Virtio blk doesn't use callback so call it manually to send result. */ 271 rc = bdev ? 0 : -EINVAL; 272 rpc_create_virtio_dev_cb(req, rc, &bdev, bdev ? 1 : 0); 273 } else if (strcmp(req->dev_type, "scsi") == 0) { 274 if (pci) { 275 rc = bdev_virtio_pci_scsi_dev_create(req->name, &pci_addr, rpc_create_virtio_dev_cb, req); 276 } else { 277 rc = bdev_virtio_user_scsi_dev_create(req->name, req->traddr, req->vq_count, req->vq_size, 278 rpc_create_virtio_dev_cb, req); 279 } 280 281 if (rc < 0) { 282 /* In case of error callback is not called so do it manually to send result. */ 283 rpc_create_virtio_dev_cb(req, rc, NULL, 0); 284 } 285 } else { 286 SPDK_ERRLOG("Invalid dev_type '%s'\n", req->dev_type); 287 spdk_jsonrpc_send_error_response_fmt(request, EINVAL, "Invalid dev_type '%s'", req->dev_type); 288 goto cleanup; 289 } 290 291 return; 292 293 cleanup: 294 free_rpc_bdev_virtio_attach_controller_ctx(req); 295 } 296 SPDK_RPC_REGISTER("bdev_virtio_attach_controller", 297 rpc_bdev_virtio_attach_controller, SPDK_RPC_RUNTIME); 298 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(bdev_virtio_attach_controller, construct_virtio_dev) 299