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"
20 #include "qemu/module.h"
22 #include "qapi/error.h"
23 #include "qemu/uuid.h"
24 #include "crypto/tlscredsx509.h"
26 #define VXHS_OPT_FILENAME "filename"
27 #define VXHS_OPT_VDISK_ID "vdisk-id"
28 #define VXHS_OPT_SERVER "server"
29 #define VXHS_OPT_HOST "host"
30 #define VXHS_OPT_PORT "port"
32 /* Only accessed under QEMU global mutex */
33 static uint32_t vxhs_ref;
41 * HyperScale AIO callbacks structure
43 typedef struct VXHSAIOCB {
48 typedef struct VXHSvDiskHostsInfo {
49 void *dev_handle; /* Device handle */
50 char *host; /* Host name or IP */
51 int port; /* Host's port number */
55 * Structure per vDisk maintained for state
57 typedef struct BDRVVXHSState {
58 VXHSvDiskHostsInfo vdisk_hostinfo; /* Per host info */
60 char *tlscredsid; /* tlscredsid */
63 static void vxhs_complete_aio_bh(void *opaque)
65 VXHSAIOCB *acb = opaque;
66 BlockCompletionFunc *cb = acb->common.cb;
67 void *cb_opaque = acb->common.opaque;
71 trace_vxhs_complete_aio(acb, acb->err);
80 * Called from a libqnio thread
82 static void vxhs_iio_callback(void *ctx, uint32_t opcode, uint32_t error)
84 VXHSAIOCB *acb = NULL;
87 case IRP_READ_REQUEST:
88 case IRP_WRITE_REQUEST:
92 * ctx is NULL if error is QNIOERROR_CHANNEL_HUP
97 trace_vxhs_iio_callback(error);
105 trace_vxhs_iio_callback(error);
108 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
109 vxhs_complete_aio_bh, acb);
113 if (error == QNIOERROR_HUP) {
115 * Channel failed, spontaneous notification,
116 * not in response to I/O
118 trace_vxhs_iio_callback_chnfail(error, errno);
120 trace_vxhs_iio_callback_unknwn(opcode, error);
128 static QemuOptsList runtime_opts = {
130 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
133 .name = VXHS_OPT_FILENAME,
134 .type = QEMU_OPT_STRING,
135 .help = "URI to the Veritas HyperScale image",
138 .name = VXHS_OPT_VDISK_ID,
139 .type = QEMU_OPT_STRING,
140 .help = "UUID of the VxHS vdisk",
144 .type = QEMU_OPT_STRING,
145 .help = "ID of the TLS/SSL credentials to use",
147 { /* end of list */ }
151 static QemuOptsList runtime_tcp_opts = {
153 .head = QTAILQ_HEAD_INITIALIZER(runtime_tcp_opts.head),
156 .name = VXHS_OPT_HOST,
157 .type = QEMU_OPT_STRING,
158 .help = "host address (ipv4 addresses)",
161 .name = VXHS_OPT_PORT,
162 .type = QEMU_OPT_NUMBER,
163 .help = "port number on which VxHSD is listening (default 9999)",
164 .def_value_str = "9999"
166 { /* end of list */ }
171 * Parse incoming URI and populate *options with the host
172 * and device information
174 static int vxhs_parse_uri(const char *filename, QDict *options)
180 trace_vxhs_parse_uri_filename(filename);
181 uri = uri_parse(filename);
182 if (!uri || !uri->server || !uri->path) {
187 qdict_put_str(options, VXHS_OPT_SERVER ".host", uri->server);
190 port = g_strdup_printf("%d", uri->port);
191 qdict_put_str(options, VXHS_OPT_SERVER ".port", port);
195 qdict_put_str(options, "vdisk-id", uri->path);
197 trace_vxhs_parse_uri_hostinfo(uri->server, uri->port);
203 static void vxhs_parse_filename(const char *filename, QDict *options,
206 if (qdict_haskey(options, "vdisk-id") || qdict_haskey(options, "server")) {
207 error_setg(errp, "vdisk-id/server and a file name may not be specified "
212 if (strstr(filename, "://")) {
213 int ret = vxhs_parse_uri(filename, options);
215 error_setg(errp, "Invalid URI. URI should be of the form "
216 " vxhs://<host_ip>:<port>/<vdisk-id>");
221 static void vxhs_refresh_limits(BlockDriverState *bs, Error **errp)
223 /* XXX Does VXHS support AIO on less than 512-byte alignment? */
224 bs->bl.request_alignment = 512;
227 static int vxhs_init_and_ref(void)
229 if (vxhs_ref++ == 0) {
230 if (iio_init(QNIO_VERSION, vxhs_iio_callback)) {
237 static void vxhs_unref(void)
239 if (--vxhs_ref == 0) {
244 static void vxhs_get_tls_creds(const char *id, char **cacert,
245 char **key, char **cert, Error **errp)
248 QCryptoTLSCreds *creds;
249 QCryptoTLSCredsX509 *creds_x509;
251 obj = object_resolve_path_component(
252 object_get_objects_root(), id);
255 error_setg(errp, "No TLS credentials with id '%s'",
260 creds_x509 = (QCryptoTLSCredsX509 *)
261 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS_X509);
264 error_setg(errp, "Object with id '%s' is not TLS credentials",
269 creds = &creds_x509->parent_obj;
271 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
273 "Expecting TLS credentials with a client endpoint");
278 * Get the cacert, client_cert and client_key file names.
281 error_setg(errp, "TLS object missing 'dir' property value");
285 *cacert = g_strdup_printf("%s/%s", creds->dir,
286 QCRYPTO_TLS_CREDS_X509_CA_CERT);
287 *cert = g_strdup_printf("%s/%s", creds->dir,
288 QCRYPTO_TLS_CREDS_X509_CLIENT_CERT);
289 *key = g_strdup_printf("%s/%s", creds->dir,
290 QCRYPTO_TLS_CREDS_X509_CLIENT_KEY);
293 static int vxhs_open(BlockDriverState *bs, QDict *options,
294 int bdrv_flags, Error **errp)
296 BDRVVXHSState *s = bs->opaque;
298 QDict *backing_options = NULL;
299 QemuOpts *opts = NULL;
300 QemuOpts *tcp_opts = NULL;
301 char *of_vsa_addr = NULL;
302 Error *local_err = NULL;
303 const char *vdisk_id_opt;
304 const char *server_host_opt;
307 char *client_key = NULL;
308 char *client_cert = NULL;
310 ret = vxhs_init_and_ref();
316 /* Create opts info from runtime_opts and runtime_tcp_opts list */
317 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
318 tcp_opts = qemu_opts_create(&runtime_tcp_opts, NULL, 0, &error_abort);
320 qemu_opts_absorb_qdict(opts, options, &local_err);
326 /* vdisk-id is the disk UUID */
327 vdisk_id_opt = qemu_opt_get(opts, VXHS_OPT_VDISK_ID);
329 error_setg(&local_err, QERR_MISSING_PARAMETER, VXHS_OPT_VDISK_ID);
334 /* vdisk-id may contain a leading '/' */
335 if (strlen(vdisk_id_opt) > UUID_FMT_LEN + 1) {
336 error_setg(&local_err, "vdisk-id cannot be more than %d characters",
342 s->vdisk_guid = g_strdup(vdisk_id_opt);
343 trace_vxhs_open_vdiskid(vdisk_id_opt);
345 /* get the 'server.' arguments */
346 qdict_extract_subqdict(options, &backing_options, VXHS_OPT_SERVER".");
348 qemu_opts_absorb_qdict(tcp_opts, backing_options, &local_err);
349 if (local_err != NULL) {
354 server_host_opt = qemu_opt_get(tcp_opts, VXHS_OPT_HOST);
355 if (!server_host_opt) {
356 error_setg(&local_err, QERR_MISSING_PARAMETER,
357 VXHS_OPT_SERVER"."VXHS_OPT_HOST);
362 if (strlen(server_host_opt) > MAXHOSTNAMELEN) {
363 error_setg(&local_err, "server.host cannot be more than %d characters",
369 /* check if we got tls-creds via the --object argument */
370 s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
372 vxhs_get_tls_creds(s->tlscredsid, &cacert, &client_key,
373 &client_cert, &local_err);
374 if (local_err != NULL) {
378 trace_vxhs_get_creds(cacert, client_key, client_cert);
381 s->vdisk_hostinfo.host = g_strdup(server_host_opt);
382 s->vdisk_hostinfo.port = g_ascii_strtoll(qemu_opt_get(tcp_opts,
386 trace_vxhs_open_hostinfo(s->vdisk_hostinfo.host,
387 s->vdisk_hostinfo.port);
389 of_vsa_addr = g_strdup_printf("of://%s:%d",
390 s->vdisk_hostinfo.host,
391 s->vdisk_hostinfo.port);
394 * Open qnio channel to storage agent if not opened before
396 dev_handlep = iio_open(of_vsa_addr, s->vdisk_guid, 0,
397 cacert, client_key, client_cert);
398 if (dev_handlep == NULL) {
399 trace_vxhs_open_iio_open(of_vsa_addr);
403 s->vdisk_hostinfo.dev_handle = dev_handlep;
407 qobject_unref(backing_options);
408 qemu_opts_del(tcp_opts);
416 error_propagate(errp, local_err);
417 g_free(s->vdisk_hostinfo.host);
418 g_free(s->vdisk_guid);
419 g_free(s->tlscredsid);
420 s->vdisk_guid = NULL;
426 static const AIOCBInfo vxhs_aiocb_info = {
427 .aiocb_size = sizeof(VXHSAIOCB)
431 * This allocates QEMU-VXHS callback for each IO
432 * and is passed to QNIO. When QNIO completes the work,
433 * it will be passed back through the callback.
435 static BlockAIOCB *vxhs_aio_rw(BlockDriverState *bs, uint64_t offset,
436 QEMUIOVector *qiov, uint64_t size,
437 BlockCompletionFunc *cb, void *opaque,
440 VXHSAIOCB *acb = NULL;
441 BDRVVXHSState *s = bs->opaque;
444 void *dev_handle = s->vdisk_hostinfo.dev_handle;
446 acb = qemu_aio_get(&vxhs_aiocb_info, bs, cb, opaque);
449 * Initialize VXHSAIOCB.
453 iio_flags = IIO_FLAG_ASYNC;
456 case VDISK_AIO_WRITE:
457 ret = iio_writev(dev_handle, acb, qiov->iov, qiov->niov,
458 offset, size, iio_flags);
461 ret = iio_readv(dev_handle, acb, qiov->iov, qiov->niov,
462 offset, size, iio_flags);
465 trace_vxhs_aio_rw_invalid(iodir);
470 trace_vxhs_aio_rw_ioerr(s->vdisk_guid, iodir, size, offset,
481 static BlockAIOCB *vxhs_aio_preadv(BlockDriverState *bs,
482 uint64_t offset, uint64_t bytes,
483 QEMUIOVector *qiov, int flags,
484 BlockCompletionFunc *cb, void *opaque)
486 return vxhs_aio_rw(bs, offset, qiov, bytes, cb, opaque, VDISK_AIO_READ);
489 static BlockAIOCB *vxhs_aio_pwritev(BlockDriverState *bs,
490 uint64_t offset, uint64_t bytes,
491 QEMUIOVector *qiov, int flags,
492 BlockCompletionFunc *cb, void *opaque)
494 return vxhs_aio_rw(bs, offset, qiov, bytes, cb, opaque, VDISK_AIO_WRITE);
497 static void vxhs_close(BlockDriverState *bs)
499 BDRVVXHSState *s = bs->opaque;
501 trace_vxhs_close(s->vdisk_guid);
503 g_free(s->vdisk_guid);
504 s->vdisk_guid = NULL;
509 if (s->vdisk_hostinfo.dev_handle) {
510 iio_close(s->vdisk_hostinfo.dev_handle);
511 s->vdisk_hostinfo.dev_handle = NULL;
517 * Free the dynamically allocated host string etc
519 g_free(s->vdisk_hostinfo.host);
520 g_free(s->tlscredsid);
521 s->tlscredsid = NULL;
522 s->vdisk_hostinfo.host = NULL;
523 s->vdisk_hostinfo.port = 0;
526 static int64_t vxhs_get_vdisk_stat(BDRVVXHSState *s)
528 int64_t vdisk_size = -1;
530 void *dev_handle = s->vdisk_hostinfo.dev_handle;
532 ret = iio_ioctl(dev_handle, IOR_VDISK_STAT, &vdisk_size, 0);
534 trace_vxhs_get_vdisk_stat_err(s->vdisk_guid, ret, errno);
538 trace_vxhs_get_vdisk_stat(s->vdisk_guid, vdisk_size);
543 * Returns the size of vDisk in bytes. This is required
544 * by QEMU block upper block layer so that it is visible
547 static int64_t vxhs_getlength(BlockDriverState *bs)
549 BDRVVXHSState *s = bs->opaque;
552 vdisk_size = vxhs_get_vdisk_stat(s);
553 if (vdisk_size < 0) {
560 static const char *const vxhs_strong_runtime_opts[] = {
570 static BlockDriver bdrv_vxhs = {
571 .format_name = "vxhs",
572 .protocol_name = "vxhs",
573 .instance_size = sizeof(BDRVVXHSState),
574 .bdrv_file_open = vxhs_open,
575 .bdrv_parse_filename = vxhs_parse_filename,
576 .bdrv_refresh_limits = vxhs_refresh_limits,
577 .bdrv_close = vxhs_close,
578 .bdrv_getlength = vxhs_getlength,
579 .bdrv_aio_preadv = vxhs_aio_preadv,
580 .bdrv_aio_pwritev = vxhs_aio_pwritev,
581 .strong_runtime_opts = vxhs_strong_runtime_opts,
584 static void bdrv_vxhs_init(void)
586 bdrv_register(&bdrv_vxhs);
589 block_init(bdrv_vxhs_init);