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"
25 #include "sysemu/replay.h"
27 #define VXHS_OPT_FILENAME "filename"
28 #define VXHS_OPT_VDISK_ID "vdisk-id"
29 #define VXHS_OPT_SERVER "server"
30 #define VXHS_OPT_HOST "host"
31 #define VXHS_OPT_PORT "port"
33 /* Only accessed under QEMU global mutex */
34 static uint32_t vxhs_ref
;
42 * HyperScale AIO callbacks structure
44 typedef struct VXHSAIOCB
{
49 typedef struct VXHSvDiskHostsInfo
{
50 void *dev_handle
; /* Device handle */
51 char *host
; /* Host name or IP */
52 int port
; /* Host's port number */
56 * Structure per vDisk maintained for state
58 typedef struct BDRVVXHSState
{
59 VXHSvDiskHostsInfo vdisk_hostinfo
; /* Per host info */
61 char *tlscredsid
; /* tlscredsid */
64 static void vxhs_complete_aio_bh(void *opaque
)
66 VXHSAIOCB
*acb
= opaque
;
67 BlockCompletionFunc
*cb
= acb
->common
.cb
;
68 void *cb_opaque
= acb
->common
.opaque
;
72 trace_vxhs_complete_aio(acb
, acb
->err
);
81 * Called from a libqnio thread
83 static void vxhs_iio_callback(void *ctx
, uint32_t opcode
, uint32_t error
)
85 VXHSAIOCB
*acb
= NULL
;
88 case IRP_READ_REQUEST
:
89 case IRP_WRITE_REQUEST
:
93 * ctx is NULL if error is QNIOERROR_CHANNEL_HUP
98 trace_vxhs_iio_callback(error
);
106 trace_vxhs_iio_callback(error
);
109 replay_bh_schedule_oneshot_event(bdrv_get_aio_context(acb
->common
.bs
),
110 vxhs_complete_aio_bh
, acb
);
114 if (error
== QNIOERROR_HUP
) {
116 * Channel failed, spontaneous notification,
117 * not in response to I/O
119 trace_vxhs_iio_callback_chnfail(error
, errno
);
121 trace_vxhs_iio_callback_unknwn(opcode
, error
);
129 static QemuOptsList runtime_opts
= {
131 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
134 .name
= VXHS_OPT_FILENAME
,
135 .type
= QEMU_OPT_STRING
,
136 .help
= "URI to the Veritas HyperScale image",
139 .name
= VXHS_OPT_VDISK_ID
,
140 .type
= QEMU_OPT_STRING
,
141 .help
= "UUID of the VxHS vdisk",
145 .type
= QEMU_OPT_STRING
,
146 .help
= "ID of the TLS/SSL credentials to use",
148 { /* end of list */ }
152 static QemuOptsList runtime_tcp_opts
= {
154 .head
= QTAILQ_HEAD_INITIALIZER(runtime_tcp_opts
.head
),
157 .name
= VXHS_OPT_HOST
,
158 .type
= QEMU_OPT_STRING
,
159 .help
= "host address (ipv4 addresses)",
162 .name
= VXHS_OPT_PORT
,
163 .type
= QEMU_OPT_NUMBER
,
164 .help
= "port number on which VxHSD is listening (default 9999)",
165 .def_value_str
= "9999"
167 { /* end of list */ }
172 * Parse incoming URI and populate *options with the host
173 * and device information
175 static int vxhs_parse_uri(const char *filename
, QDict
*options
)
181 trace_vxhs_parse_uri_filename(filename
);
182 uri
= uri_parse(filename
);
183 if (!uri
|| !uri
->server
|| !uri
->path
) {
188 qdict_put_str(options
, VXHS_OPT_SERVER
".host", uri
->server
);
191 port
= g_strdup_printf("%d", uri
->port
);
192 qdict_put_str(options
, VXHS_OPT_SERVER
".port", port
);
196 qdict_put_str(options
, "vdisk-id", uri
->path
);
198 trace_vxhs_parse_uri_hostinfo(uri
->server
, uri
->port
);
204 static void vxhs_parse_filename(const char *filename
, QDict
*options
,
207 if (qdict_haskey(options
, "vdisk-id") || qdict_haskey(options
, "server")) {
208 error_setg(errp
, "vdisk-id/server and a file name may not be specified "
213 if (strstr(filename
, "://")) {
214 int ret
= vxhs_parse_uri(filename
, options
);
216 error_setg(errp
, "Invalid URI. URI should be of the form "
217 " vxhs://<host_ip>:<port>/<vdisk-id>");
222 static void vxhs_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
224 /* XXX Does VXHS support AIO on less than 512-byte alignment? */
225 bs
->bl
.request_alignment
= 512;
228 static int vxhs_init_and_ref(void)
230 if (vxhs_ref
++ == 0) {
231 if (iio_init(QNIO_VERSION
, vxhs_iio_callback
)) {
238 static void vxhs_unref(void)
240 if (--vxhs_ref
== 0) {
245 static void vxhs_get_tls_creds(const char *id
, char **cacert
,
246 char **key
, char **cert
, Error
**errp
)
249 QCryptoTLSCreds
*creds
;
250 QCryptoTLSCredsX509
*creds_x509
;
252 obj
= object_resolve_path_component(
253 object_get_objects_root(), id
);
256 error_setg(errp
, "No TLS credentials with id '%s'",
261 creds_x509
= (QCryptoTLSCredsX509
*)
262 object_dynamic_cast(obj
, TYPE_QCRYPTO_TLS_CREDS_X509
);
265 error_setg(errp
, "Object with id '%s' is not TLS credentials",
270 creds
= &creds_x509
->parent_obj
;
272 if (creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
274 "Expecting TLS credentials with a client endpoint");
279 * Get the cacert, client_cert and client_key file names.
282 error_setg(errp
, "TLS object missing 'dir' property value");
286 *cacert
= g_strdup_printf("%s/%s", creds
->dir
,
287 QCRYPTO_TLS_CREDS_X509_CA_CERT
);
288 *cert
= g_strdup_printf("%s/%s", creds
->dir
,
289 QCRYPTO_TLS_CREDS_X509_CLIENT_CERT
);
290 *key
= g_strdup_printf("%s/%s", creds
->dir
,
291 QCRYPTO_TLS_CREDS_X509_CLIENT_KEY
);
294 static int vxhs_open(BlockDriverState
*bs
, QDict
*options
,
295 int bdrv_flags
, Error
**errp
)
297 BDRVVXHSState
*s
= bs
->opaque
;
299 QDict
*backing_options
= NULL
;
300 QemuOpts
*opts
= NULL
;
301 QemuOpts
*tcp_opts
= NULL
;
302 char *of_vsa_addr
= NULL
;
303 Error
*local_err
= NULL
;
304 const char *vdisk_id_opt
;
305 const char *server_host_opt
;
308 char *client_key
= NULL
;
309 char *client_cert
= NULL
;
311 ret
= vxhs_init_and_ref();
317 /* Create opts info from runtime_opts and runtime_tcp_opts list */
318 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
319 tcp_opts
= qemu_opts_create(&runtime_tcp_opts
, NULL
, 0, &error_abort
);
321 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
326 /* vdisk-id is the disk UUID */
327 vdisk_id_opt
= qemu_opt_get(opts
, VXHS_OPT_VDISK_ID
);
329 error_setg(errp
, 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(errp
, "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 if (!qemu_opts_absorb_qdict(tcp_opts
, backing_options
, errp
)) {
353 server_host_opt
= qemu_opt_get(tcp_opts
, VXHS_OPT_HOST
);
354 if (!server_host_opt
) {
355 error_setg(errp
, QERR_MISSING_PARAMETER
,
356 VXHS_OPT_SERVER
"."VXHS_OPT_HOST
);
361 if (strlen(server_host_opt
) > MAXHOSTNAMELEN
) {
362 error_setg(errp
, "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 g_free(s
->vdisk_hostinfo
.host
);
416 g_free(s
->vdisk_guid
);
417 g_free(s
->tlscredsid
);
418 s
->vdisk_guid
= NULL
;
424 static const AIOCBInfo vxhs_aiocb_info
= {
425 .aiocb_size
= sizeof(VXHSAIOCB
)
429 * This allocates QEMU-VXHS callback for each IO
430 * and is passed to QNIO. When QNIO completes the work,
431 * it will be passed back through the callback.
433 static BlockAIOCB
*vxhs_aio_rw(BlockDriverState
*bs
, uint64_t offset
,
434 QEMUIOVector
*qiov
, uint64_t size
,
435 BlockCompletionFunc
*cb
, void *opaque
,
438 VXHSAIOCB
*acb
= NULL
;
439 BDRVVXHSState
*s
= bs
->opaque
;
442 void *dev_handle
= s
->vdisk_hostinfo
.dev_handle
;
444 acb
= qemu_aio_get(&vxhs_aiocb_info
, bs
, cb
, opaque
);
447 * Initialize VXHSAIOCB.
451 iio_flags
= IIO_FLAG_ASYNC
;
454 case VDISK_AIO_WRITE
:
455 ret
= iio_writev(dev_handle
, acb
, qiov
->iov
, qiov
->niov
,
456 offset
, size
, iio_flags
);
459 ret
= iio_readv(dev_handle
, acb
, qiov
->iov
, qiov
->niov
,
460 offset
, size
, iio_flags
);
463 trace_vxhs_aio_rw_invalid(iodir
);
468 trace_vxhs_aio_rw_ioerr(s
->vdisk_guid
, iodir
, size
, offset
,
479 static BlockAIOCB
*vxhs_aio_preadv(BlockDriverState
*bs
,
480 uint64_t offset
, uint64_t bytes
,
481 QEMUIOVector
*qiov
, int flags
,
482 BlockCompletionFunc
*cb
, void *opaque
)
484 return vxhs_aio_rw(bs
, offset
, qiov
, bytes
, cb
, opaque
, VDISK_AIO_READ
);
487 static BlockAIOCB
*vxhs_aio_pwritev(BlockDriverState
*bs
,
488 uint64_t offset
, uint64_t bytes
,
489 QEMUIOVector
*qiov
, int flags
,
490 BlockCompletionFunc
*cb
, void *opaque
)
492 return vxhs_aio_rw(bs
, offset
, qiov
, bytes
, cb
, opaque
, VDISK_AIO_WRITE
);
495 static void vxhs_close(BlockDriverState
*bs
)
497 BDRVVXHSState
*s
= bs
->opaque
;
499 trace_vxhs_close(s
->vdisk_guid
);
501 g_free(s
->vdisk_guid
);
502 s
->vdisk_guid
= NULL
;
507 if (s
->vdisk_hostinfo
.dev_handle
) {
508 iio_close(s
->vdisk_hostinfo
.dev_handle
);
509 s
->vdisk_hostinfo
.dev_handle
= NULL
;
515 * Free the dynamically allocated host string etc
517 g_free(s
->vdisk_hostinfo
.host
);
518 g_free(s
->tlscredsid
);
519 s
->tlscredsid
= NULL
;
520 s
->vdisk_hostinfo
.host
= NULL
;
521 s
->vdisk_hostinfo
.port
= 0;
524 static int64_t vxhs_get_vdisk_stat(BDRVVXHSState
*s
)
526 int64_t vdisk_size
= -1;
528 void *dev_handle
= s
->vdisk_hostinfo
.dev_handle
;
530 ret
= iio_ioctl(dev_handle
, IOR_VDISK_STAT
, &vdisk_size
, 0);
532 trace_vxhs_get_vdisk_stat_err(s
->vdisk_guid
, ret
, errno
);
536 trace_vxhs_get_vdisk_stat(s
->vdisk_guid
, vdisk_size
);
541 * Returns the size of vDisk in bytes. This is required
542 * by QEMU block upper block layer so that it is visible
545 static int64_t vxhs_getlength(BlockDriverState
*bs
)
547 BDRVVXHSState
*s
= bs
->opaque
;
550 vdisk_size
= vxhs_get_vdisk_stat(s
);
551 if (vdisk_size
< 0) {
558 static const char *const vxhs_strong_runtime_opts
[] = {
568 static BlockDriver bdrv_vxhs
= {
569 .format_name
= "vxhs",
570 .protocol_name
= "vxhs",
571 .instance_size
= sizeof(BDRVVXHSState
),
572 .bdrv_file_open
= vxhs_open
,
573 .bdrv_parse_filename
= vxhs_parse_filename
,
574 .bdrv_refresh_limits
= vxhs_refresh_limits
,
575 .bdrv_close
= vxhs_close
,
576 .bdrv_getlength
= vxhs_getlength
,
577 .bdrv_aio_preadv
= vxhs_aio_preadv
,
578 .bdrv_aio_pwritev
= vxhs_aio_pwritev
,
579 .strong_runtime_opts
= vxhs_strong_runtime_opts
,
582 static void bdrv_vxhs_init(void)
584 bdrv_register(&bdrv_vxhs
);
587 block_init(bdrv_vxhs_init
);