2 * QEMU Block driver for Veritas HyperScale (VxHS)
4 * Copyright (c) 2017 Veritas Technologies LLC.
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
11 #include "qemu/osdep.h"
12 #include <qnio/qnio_api.h>
13 #include <sys/param.h>
14 #include "block/block_int.h"
15 #include "block/qdict.h"
16 #include "qapi/qmp/qerror.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qapi/qmp/qstring.h"
21 #include "qapi/error.h"
22 #include "qemu/uuid.h"
23 #include "crypto/tlscredsx509.h"
25 #define VXHS_OPT_FILENAME "filename"
26 #define VXHS_OPT_VDISK_ID "vdisk-id"
27 #define VXHS_OPT_SERVER "server"
28 #define VXHS_OPT_HOST "host"
29 #define VXHS_OPT_PORT "port"
31 /* Only accessed under QEMU global mutex */
32 static uint32_t vxhs_ref;
40 * HyperScale AIO callbacks structure
42 typedef struct VXHSAIOCB {
47 typedef struct VXHSvDiskHostsInfo {
48 void *dev_handle; /* Device handle */
49 char *host; /* Host name or IP */
50 int port; /* Host's port number */
54 * Structure per vDisk maintained for state
56 typedef struct BDRVVXHSState {
57 VXHSvDiskHostsInfo vdisk_hostinfo; /* Per host info */
59 char *tlscredsid; /* tlscredsid */
62 static void vxhs_complete_aio_bh(void *opaque)
64 VXHSAIOCB *acb = opaque;
65 BlockCompletionFunc *cb = acb->common.cb;
66 void *cb_opaque = acb->common.opaque;
70 trace_vxhs_complete_aio(acb, acb->err);
79 * Called from a libqnio thread
81 static void vxhs_iio_callback(void *ctx, uint32_t opcode, uint32_t error)
83 VXHSAIOCB *acb = NULL;
86 case IRP_READ_REQUEST:
87 case IRP_WRITE_REQUEST:
91 * ctx is NULL if error is QNIOERROR_CHANNEL_HUP
96 trace_vxhs_iio_callback(error);
104 trace_vxhs_iio_callback(error);
107 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
108 vxhs_complete_aio_bh, acb);
112 if (error == QNIOERROR_HUP) {
114 * Channel failed, spontaneous notification,
115 * not in response to I/O
117 trace_vxhs_iio_callback_chnfail(error, errno);
119 trace_vxhs_iio_callback_unknwn(opcode, error);
127 static QemuOptsList runtime_opts = {
129 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
132 .name = VXHS_OPT_FILENAME,
133 .type = QEMU_OPT_STRING,
134 .help = "URI to the Veritas HyperScale image",
137 .name = VXHS_OPT_VDISK_ID,
138 .type = QEMU_OPT_STRING,
139 .help = "UUID of the VxHS vdisk",
143 .type = QEMU_OPT_STRING,
144 .help = "ID of the TLS/SSL credentials to use",
146 { /* end of list */ }
150 static QemuOptsList runtime_tcp_opts = {
152 .head = QTAILQ_HEAD_INITIALIZER(runtime_tcp_opts.head),
155 .name = VXHS_OPT_HOST,
156 .type = QEMU_OPT_STRING,
157 .help = "host address (ipv4 addresses)",
160 .name = VXHS_OPT_PORT,
161 .type = QEMU_OPT_NUMBER,
162 .help = "port number on which VxHSD is listening (default 9999)",
163 .def_value_str = "9999"
165 { /* end of list */ }
170 * Parse incoming URI and populate *options with the host
171 * and device information
173 static int vxhs_parse_uri(const char *filename, QDict *options)
179 trace_vxhs_parse_uri_filename(filename);
180 uri = uri_parse(filename);
181 if (!uri || !uri->server || !uri->path) {
186 qdict_put_str(options, VXHS_OPT_SERVER ".host", uri->server);
189 port = g_strdup_printf("%d", uri->port);
190 qdict_put_str(options, VXHS_OPT_SERVER ".port", port);
194 qdict_put_str(options, "vdisk-id", uri->path);
196 trace_vxhs_parse_uri_hostinfo(uri->server, uri->port);
202 static void vxhs_parse_filename(const char *filename, QDict *options,
205 if (qdict_haskey(options, "vdisk-id") || qdict_haskey(options, "server")) {
206 error_setg(errp, "vdisk-id/server and a file name may not be specified "
211 if (strstr(filename, "://")) {
212 int ret = vxhs_parse_uri(filename, options);
214 error_setg(errp, "Invalid URI. URI should be of the form "
215 " vxhs://<host_ip>:<port>/<vdisk-id>");
220 static void vxhs_refresh_limits(BlockDriverState *bs, Error **errp)
222 /* XXX Does VXHS support AIO on less than 512-byte alignment? */
223 bs->bl.request_alignment = 512;
226 static int vxhs_init_and_ref(void)
228 if (vxhs_ref++ == 0) {
229 if (iio_init(QNIO_VERSION, vxhs_iio_callback)) {
236 static void vxhs_unref(void)
238 if (--vxhs_ref == 0) {
243 static void vxhs_get_tls_creds(const char *id, char **cacert,
244 char **key, char **cert, Error **errp)
247 QCryptoTLSCreds *creds;
248 QCryptoTLSCredsX509 *creds_x509;
250 obj = object_resolve_path_component(
251 object_get_objects_root(), id);
254 error_setg(errp, "No TLS credentials with id '%s'",
259 creds_x509 = (QCryptoTLSCredsX509 *)
260 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS_X509);
263 error_setg(errp, "Object with id '%s' is not TLS credentials",
268 creds = &creds_x509->parent_obj;
270 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
272 "Expecting TLS credentials with a client endpoint");
277 * Get the cacert, client_cert and client_key file names.
280 error_setg(errp, "TLS object missing 'dir' property value");
284 *cacert = g_strdup_printf("%s/%s", creds->dir,
285 QCRYPTO_TLS_CREDS_X509_CA_CERT);
286 *cert = g_strdup_printf("%s/%s", creds->dir,
287 QCRYPTO_TLS_CREDS_X509_CLIENT_CERT);
288 *key = g_strdup_printf("%s/%s", creds->dir,
289 QCRYPTO_TLS_CREDS_X509_CLIENT_KEY);
292 static int vxhs_open(BlockDriverState *bs, QDict *options,
293 int bdrv_flags, Error **errp)
295 BDRVVXHSState *s = bs->opaque;
297 QDict *backing_options = NULL;
298 QemuOpts *opts = NULL;
299 QemuOpts *tcp_opts = NULL;
300 char *of_vsa_addr = NULL;
301 Error *local_err = NULL;
302 const char *vdisk_id_opt;
303 const char *server_host_opt;
306 char *client_key = NULL;
307 char *client_cert = NULL;
309 ret = vxhs_init_and_ref();
315 /* Create opts info from runtime_opts and runtime_tcp_opts list */
316 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
317 tcp_opts = qemu_opts_create(&runtime_tcp_opts, NULL, 0, &error_abort);
319 qemu_opts_absorb_qdict(opts, options, &local_err);
325 /* vdisk-id is the disk UUID */
326 vdisk_id_opt = qemu_opt_get(opts, VXHS_OPT_VDISK_ID);
328 error_setg(&local_err, QERR_MISSING_PARAMETER, VXHS_OPT_VDISK_ID);
333 /* vdisk-id may contain a leading '/' */
334 if (strlen(vdisk_id_opt) > UUID_FMT_LEN + 1) {
335 error_setg(&local_err, "vdisk-id cannot be more than %d characters",
341 s->vdisk_guid = g_strdup(vdisk_id_opt);
342 trace_vxhs_open_vdiskid(vdisk_id_opt);
344 /* get the 'server.' arguments */
345 qdict_extract_subqdict(options, &backing_options, VXHS_OPT_SERVER".");
347 qemu_opts_absorb_qdict(tcp_opts, backing_options, &local_err);
348 if (local_err != NULL) {
353 server_host_opt = qemu_opt_get(tcp_opts, VXHS_OPT_HOST);
354 if (!server_host_opt) {
355 error_setg(&local_err, QERR_MISSING_PARAMETER,
356 VXHS_OPT_SERVER"."VXHS_OPT_HOST);
361 if (strlen(server_host_opt) > MAXHOSTNAMELEN) {
362 error_setg(&local_err, "server.host cannot be more than %d characters",
368 /* check if we got tls-creds via the --object argument */
369 s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
371 vxhs_get_tls_creds(s->tlscredsid, &cacert, &client_key,
372 &client_cert, &local_err);
373 if (local_err != NULL) {
377 trace_vxhs_get_creds(cacert, client_key, client_cert);
380 s->vdisk_hostinfo.host = g_strdup(server_host_opt);
381 s->vdisk_hostinfo.port = g_ascii_strtoll(qemu_opt_get(tcp_opts,
385 trace_vxhs_open_hostinfo(s->vdisk_hostinfo.host,
386 s->vdisk_hostinfo.port);
388 of_vsa_addr = g_strdup_printf("of://%s:%d",
389 s->vdisk_hostinfo.host,
390 s->vdisk_hostinfo.port);
393 * Open qnio channel to storage agent if not opened before
395 dev_handlep = iio_open(of_vsa_addr, s->vdisk_guid, 0,
396 cacert, client_key, client_cert);
397 if (dev_handlep == NULL) {
398 trace_vxhs_open_iio_open(of_vsa_addr);
402 s->vdisk_hostinfo.dev_handle = dev_handlep;
406 qobject_unref(backing_options);
407 qemu_opts_del(tcp_opts);
415 error_propagate(errp, local_err);
416 g_free(s->vdisk_hostinfo.host);
417 g_free(s->vdisk_guid);
418 g_free(s->tlscredsid);
419 s->vdisk_guid = NULL;
425 static const AIOCBInfo vxhs_aiocb_info = {
426 .aiocb_size = sizeof(VXHSAIOCB)
430 * This allocates QEMU-VXHS callback for each IO
431 * and is passed to QNIO. When QNIO completes the work,
432 * it will be passed back through the callback.
434 static BlockAIOCB *vxhs_aio_rw(BlockDriverState *bs, uint64_t offset,
435 QEMUIOVector *qiov, uint64_t size,
436 BlockCompletionFunc *cb, void *opaque,
439 VXHSAIOCB *acb = NULL;
440 BDRVVXHSState *s = bs->opaque;
443 void *dev_handle = s->vdisk_hostinfo.dev_handle;
445 acb = qemu_aio_get(&vxhs_aiocb_info, bs, cb, opaque);
448 * Initialize VXHSAIOCB.
452 iio_flags = IIO_FLAG_ASYNC;
455 case VDISK_AIO_WRITE:
456 ret = iio_writev(dev_handle, acb, qiov->iov, qiov->niov,
457 offset, size, iio_flags);
460 ret = iio_readv(dev_handle, acb, qiov->iov, qiov->niov,
461 offset, size, iio_flags);
464 trace_vxhs_aio_rw_invalid(iodir);
469 trace_vxhs_aio_rw_ioerr(s->vdisk_guid, iodir, size, offset,
480 static BlockAIOCB *vxhs_aio_preadv(BlockDriverState *bs,
481 uint64_t offset, uint64_t bytes,
482 QEMUIOVector *qiov, int flags,
483 BlockCompletionFunc *cb, void *opaque)
485 return vxhs_aio_rw(bs, offset, qiov, bytes, cb, opaque, VDISK_AIO_READ);
488 static BlockAIOCB *vxhs_aio_pwritev(BlockDriverState *bs,
489 uint64_t offset, uint64_t bytes,
490 QEMUIOVector *qiov, int flags,
491 BlockCompletionFunc *cb, void *opaque)
493 return vxhs_aio_rw(bs, offset, qiov, bytes, cb, opaque, VDISK_AIO_WRITE);
496 static void vxhs_close(BlockDriverState *bs)
498 BDRVVXHSState *s = bs->opaque;
500 trace_vxhs_close(s->vdisk_guid);
502 g_free(s->vdisk_guid);
503 s->vdisk_guid = NULL;
508 if (s->vdisk_hostinfo.dev_handle) {
509 iio_close(s->vdisk_hostinfo.dev_handle);
510 s->vdisk_hostinfo.dev_handle = NULL;
516 * Free the dynamically allocated host string etc
518 g_free(s->vdisk_hostinfo.host);
519 g_free(s->tlscredsid);
520 s->tlscredsid = NULL;
521 s->vdisk_hostinfo.host = NULL;
522 s->vdisk_hostinfo.port = 0;
525 static int64_t vxhs_get_vdisk_stat(BDRVVXHSState *s)
527 int64_t vdisk_size = -1;
529 void *dev_handle = s->vdisk_hostinfo.dev_handle;
531 ret = iio_ioctl(dev_handle, IOR_VDISK_STAT, &vdisk_size, 0);
533 trace_vxhs_get_vdisk_stat_err(s->vdisk_guid, ret, errno);
537 trace_vxhs_get_vdisk_stat(s->vdisk_guid, vdisk_size);
542 * Returns the size of vDisk in bytes. This is required
543 * by QEMU block upper block layer so that it is visible
546 static int64_t vxhs_getlength(BlockDriverState *bs)
548 BDRVVXHSState *s = bs->opaque;
551 vdisk_size = vxhs_get_vdisk_stat(s);
552 if (vdisk_size < 0) {
559 static BlockDriver bdrv_vxhs = {
560 .format_name = "vxhs",
561 .protocol_name = "vxhs",
562 .instance_size = sizeof(BDRVVXHSState),
563 .bdrv_file_open = vxhs_open,
564 .bdrv_parse_filename = vxhs_parse_filename,
565 .bdrv_refresh_limits = vxhs_refresh_limits,
566 .bdrv_close = vxhs_close,
567 .bdrv_getlength = vxhs_getlength,
568 .bdrv_aio_preadv = vxhs_aio_preadv,
569 .bdrv_aio_pwritev = vxhs_aio_pwritev,
572 static void bdrv_vxhs_init(void)
574 bdrv_register(&bdrv_vxhs);
577 block_init(bdrv_vxhs_init);