libqos: Use explicit QTestState for ahci operations
[qemu.git] / block / vxhs.c
blob75cc6c8672c00f885623db246134fe46bbe4c6ba
1 /*
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.
9 */
11 #include "qemu/osdep.h"
12 #include <qnio/qnio_api.h>
13 #include <sys/param.h>
14 #include "block/block_int.h"
15 #include "qapi/qmp/qerror.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi/qmp/qstring.h"
18 #include "trace.h"
19 #include "qemu/uri.h"
20 #include "qapi/error.h"
21 #include "qemu/uuid.h"
22 #include "crypto/tlscredsx509.h"
24 #define VXHS_OPT_FILENAME "filename"
25 #define VXHS_OPT_VDISK_ID "vdisk-id"
26 #define VXHS_OPT_SERVER "server"
27 #define VXHS_OPT_HOST "host"
28 #define VXHS_OPT_PORT "port"
30 /* Only accessed under QEMU global mutex */
31 static uint32_t vxhs_ref;
33 typedef enum {
34 VDISK_AIO_READ,
35 VDISK_AIO_WRITE,
36 } VDISKAIOCmd;
39 * HyperScale AIO callbacks structure
41 typedef struct VXHSAIOCB {
42 BlockAIOCB common;
43 int err;
44 } VXHSAIOCB;
46 typedef struct VXHSvDiskHostsInfo {
47 void *dev_handle; /* Device handle */
48 char *host; /* Host name or IP */
49 int port; /* Host's port number */
50 } VXHSvDiskHostsInfo;
53 * Structure per vDisk maintained for state
55 typedef struct BDRVVXHSState {
56 VXHSvDiskHostsInfo vdisk_hostinfo; /* Per host info */
57 char *vdisk_guid;
58 char *tlscredsid; /* tlscredsid */
59 } BDRVVXHSState;
61 static void vxhs_complete_aio_bh(void *opaque)
63 VXHSAIOCB *acb = opaque;
64 BlockCompletionFunc *cb = acb->common.cb;
65 void *cb_opaque = acb->common.opaque;
66 int ret = 0;
68 if (acb->err != 0) {
69 trace_vxhs_complete_aio(acb, acb->err);
70 ret = (-EIO);
73 qemu_aio_unref(acb);
74 cb(cb_opaque, ret);
78 * Called from a libqnio thread
80 static void vxhs_iio_callback(void *ctx, uint32_t opcode, uint32_t error)
82 VXHSAIOCB *acb = NULL;
84 switch (opcode) {
85 case IRP_READ_REQUEST:
86 case IRP_WRITE_REQUEST:
89 * ctx is VXHSAIOCB*
90 * ctx is NULL if error is QNIOERROR_CHANNEL_HUP
92 if (ctx) {
93 acb = ctx;
94 } else {
95 trace_vxhs_iio_callback(error);
96 goto out;
99 if (error) {
100 if (!acb->err) {
101 acb->err = error;
103 trace_vxhs_iio_callback(error);
106 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
107 vxhs_complete_aio_bh, acb);
108 break;
110 default:
111 if (error == QNIOERROR_HUP) {
113 * Channel failed, spontaneous notification,
114 * not in response to I/O
116 trace_vxhs_iio_callback_chnfail(error, errno);
117 } else {
118 trace_vxhs_iio_callback_unknwn(opcode, error);
120 break;
122 out:
123 return;
126 static QemuOptsList runtime_opts = {
127 .name = "vxhs",
128 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
129 .desc = {
131 .name = VXHS_OPT_FILENAME,
132 .type = QEMU_OPT_STRING,
133 .help = "URI to the Veritas HyperScale image",
136 .name = VXHS_OPT_VDISK_ID,
137 .type = QEMU_OPT_STRING,
138 .help = "UUID of the VxHS vdisk",
141 .name = "tls-creds",
142 .type = QEMU_OPT_STRING,
143 .help = "ID of the TLS/SSL credentials to use",
145 { /* end of list */ }
149 static QemuOptsList runtime_tcp_opts = {
150 .name = "vxhs_tcp",
151 .head = QTAILQ_HEAD_INITIALIZER(runtime_tcp_opts.head),
152 .desc = {
154 .name = VXHS_OPT_HOST,
155 .type = QEMU_OPT_STRING,
156 .help = "host address (ipv4 addresses)",
159 .name = VXHS_OPT_PORT,
160 .type = QEMU_OPT_NUMBER,
161 .help = "port number on which VxHSD is listening (default 9999)",
162 .def_value_str = "9999"
164 { /* end of list */ }
169 * Parse incoming URI and populate *options with the host
170 * and device information
172 static int vxhs_parse_uri(const char *filename, QDict *options)
174 URI *uri = NULL;
175 char *port;
176 int ret = 0;
178 trace_vxhs_parse_uri_filename(filename);
179 uri = uri_parse(filename);
180 if (!uri || !uri->server || !uri->path) {
181 uri_free(uri);
182 return -EINVAL;
185 qdict_put_str(options, VXHS_OPT_SERVER ".host", uri->server);
187 if (uri->port) {
188 port = g_strdup_printf("%d", uri->port);
189 qdict_put_str(options, VXHS_OPT_SERVER ".port", port);
190 g_free(port);
193 qdict_put_str(options, "vdisk-id", uri->path);
195 trace_vxhs_parse_uri_hostinfo(uri->server, uri->port);
196 uri_free(uri);
198 return ret;
201 static void vxhs_parse_filename(const char *filename, QDict *options,
202 Error **errp)
204 if (qdict_haskey(options, "vdisk-id") || qdict_haskey(options, "server")) {
205 error_setg(errp, "vdisk-id/server and a file name may not be specified "
206 "at the same time");
207 return;
210 if (strstr(filename, "://")) {
211 int ret = vxhs_parse_uri(filename, options);
212 if (ret < 0) {
213 error_setg(errp, "Invalid URI. URI should be of the form "
214 " vxhs://<host_ip>:<port>/<vdisk-id>");
219 static int vxhs_init_and_ref(void)
221 if (vxhs_ref++ == 0) {
222 if (iio_init(QNIO_VERSION, vxhs_iio_callback)) {
223 return -ENODEV;
226 return 0;
229 static void vxhs_unref(void)
231 if (--vxhs_ref == 0) {
232 iio_fini();
236 static void vxhs_get_tls_creds(const char *id, char **cacert,
237 char **key, char **cert, Error **errp)
239 Object *obj;
240 QCryptoTLSCreds *creds;
241 QCryptoTLSCredsX509 *creds_x509;
243 obj = object_resolve_path_component(
244 object_get_objects_root(), id);
246 if (!obj) {
247 error_setg(errp, "No TLS credentials with id '%s'",
248 id);
249 return;
252 creds_x509 = (QCryptoTLSCredsX509 *)
253 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS_X509);
255 if (!creds_x509) {
256 error_setg(errp, "Object with id '%s' is not TLS credentials",
257 id);
258 return;
261 creds = &creds_x509->parent_obj;
263 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
264 error_setg(errp,
265 "Expecting TLS credentials with a client endpoint");
266 return;
270 * Get the cacert, client_cert and client_key file names.
272 if (!creds->dir) {
273 error_setg(errp, "TLS object missing 'dir' property value");
274 return;
277 *cacert = g_strdup_printf("%s/%s", creds->dir,
278 QCRYPTO_TLS_CREDS_X509_CA_CERT);
279 *cert = g_strdup_printf("%s/%s", creds->dir,
280 QCRYPTO_TLS_CREDS_X509_CLIENT_CERT);
281 *key = g_strdup_printf("%s/%s", creds->dir,
282 QCRYPTO_TLS_CREDS_X509_CLIENT_KEY);
285 static int vxhs_open(BlockDriverState *bs, QDict *options,
286 int bdrv_flags, Error **errp)
288 BDRVVXHSState *s = bs->opaque;
289 void *dev_handlep;
290 QDict *backing_options = NULL;
291 QemuOpts *opts = NULL;
292 QemuOpts *tcp_opts = NULL;
293 char *of_vsa_addr = NULL;
294 Error *local_err = NULL;
295 const char *vdisk_id_opt;
296 const char *server_host_opt;
297 int ret = 0;
298 char *cacert = NULL;
299 char *client_key = NULL;
300 char *client_cert = NULL;
302 ret = vxhs_init_and_ref();
303 if (ret < 0) {
304 ret = -EINVAL;
305 goto out;
308 /* Create opts info from runtime_opts and runtime_tcp_opts list */
309 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
310 tcp_opts = qemu_opts_create(&runtime_tcp_opts, NULL, 0, &error_abort);
312 qemu_opts_absorb_qdict(opts, options, &local_err);
313 if (local_err) {
314 ret = -EINVAL;
315 goto out;
318 /* vdisk-id is the disk UUID */
319 vdisk_id_opt = qemu_opt_get(opts, VXHS_OPT_VDISK_ID);
320 if (!vdisk_id_opt) {
321 error_setg(&local_err, QERR_MISSING_PARAMETER, VXHS_OPT_VDISK_ID);
322 ret = -EINVAL;
323 goto out;
326 /* vdisk-id may contain a leading '/' */
327 if (strlen(vdisk_id_opt) > UUID_FMT_LEN + 1) {
328 error_setg(&local_err, "vdisk-id cannot be more than %d characters",
329 UUID_FMT_LEN);
330 ret = -EINVAL;
331 goto out;
334 s->vdisk_guid = g_strdup(vdisk_id_opt);
335 trace_vxhs_open_vdiskid(vdisk_id_opt);
337 /* get the 'server.' arguments */
338 qdict_extract_subqdict(options, &backing_options, VXHS_OPT_SERVER".");
340 qemu_opts_absorb_qdict(tcp_opts, backing_options, &local_err);
341 if (local_err != NULL) {
342 ret = -EINVAL;
343 goto out;
346 server_host_opt = qemu_opt_get(tcp_opts, VXHS_OPT_HOST);
347 if (!server_host_opt) {
348 error_setg(&local_err, QERR_MISSING_PARAMETER,
349 VXHS_OPT_SERVER"."VXHS_OPT_HOST);
350 ret = -EINVAL;
351 goto out;
354 if (strlen(server_host_opt) > MAXHOSTNAMELEN) {
355 error_setg(&local_err, "server.host cannot be more than %d characters",
356 MAXHOSTNAMELEN);
357 ret = -EINVAL;
358 goto out;
361 /* check if we got tls-creds via the --object argument */
362 s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
363 if (s->tlscredsid) {
364 vxhs_get_tls_creds(s->tlscredsid, &cacert, &client_key,
365 &client_cert, &local_err);
366 if (local_err != NULL) {
367 ret = -EINVAL;
368 goto out;
370 trace_vxhs_get_creds(cacert, client_key, client_cert);
373 s->vdisk_hostinfo.host = g_strdup(server_host_opt);
374 s->vdisk_hostinfo.port = g_ascii_strtoll(qemu_opt_get(tcp_opts,
375 VXHS_OPT_PORT),
376 NULL, 0);
378 trace_vxhs_open_hostinfo(s->vdisk_hostinfo.host,
379 s->vdisk_hostinfo.port);
381 of_vsa_addr = g_strdup_printf("of://%s:%d",
382 s->vdisk_hostinfo.host,
383 s->vdisk_hostinfo.port);
386 * Open qnio channel to storage agent if not opened before
388 dev_handlep = iio_open(of_vsa_addr, s->vdisk_guid, 0,
389 cacert, client_key, client_cert);
390 if (dev_handlep == NULL) {
391 trace_vxhs_open_iio_open(of_vsa_addr);
392 ret = -ENODEV;
393 goto out;
395 s->vdisk_hostinfo.dev_handle = dev_handlep;
397 out:
398 g_free(of_vsa_addr);
399 QDECREF(backing_options);
400 qemu_opts_del(tcp_opts);
401 qemu_opts_del(opts);
402 g_free(cacert);
403 g_free(client_key);
404 g_free(client_cert);
406 if (ret < 0) {
407 vxhs_unref();
408 error_propagate(errp, local_err);
409 g_free(s->vdisk_hostinfo.host);
410 g_free(s->vdisk_guid);
411 g_free(s->tlscredsid);
412 s->vdisk_guid = NULL;
415 return ret;
418 static const AIOCBInfo vxhs_aiocb_info = {
419 .aiocb_size = sizeof(VXHSAIOCB)
423 * This allocates QEMU-VXHS callback for each IO
424 * and is passed to QNIO. When QNIO completes the work,
425 * it will be passed back through the callback.
427 static BlockAIOCB *vxhs_aio_rw(BlockDriverState *bs, int64_t sector_num,
428 QEMUIOVector *qiov, int nb_sectors,
429 BlockCompletionFunc *cb, void *opaque,
430 VDISKAIOCmd iodir)
432 VXHSAIOCB *acb = NULL;
433 BDRVVXHSState *s = bs->opaque;
434 size_t size;
435 uint64_t offset;
436 int iio_flags = 0;
437 int ret = 0;
438 void *dev_handle = s->vdisk_hostinfo.dev_handle;
440 offset = sector_num * BDRV_SECTOR_SIZE;
441 size = nb_sectors * BDRV_SECTOR_SIZE;
442 acb = qemu_aio_get(&vxhs_aiocb_info, bs, cb, opaque);
445 * Initialize VXHSAIOCB.
447 acb->err = 0;
449 iio_flags = IIO_FLAG_ASYNC;
451 switch (iodir) {
452 case VDISK_AIO_WRITE:
453 ret = iio_writev(dev_handle, acb, qiov->iov, qiov->niov,
454 offset, (uint64_t)size, iio_flags);
455 break;
456 case VDISK_AIO_READ:
457 ret = iio_readv(dev_handle, acb, qiov->iov, qiov->niov,
458 offset, (uint64_t)size, iio_flags);
459 break;
460 default:
461 trace_vxhs_aio_rw_invalid(iodir);
462 goto errout;
465 if (ret != 0) {
466 trace_vxhs_aio_rw_ioerr(s->vdisk_guid, iodir, size, offset,
467 acb, ret, errno);
468 goto errout;
470 return &acb->common;
472 errout:
473 qemu_aio_unref(acb);
474 return NULL;
477 static BlockAIOCB *vxhs_aio_readv(BlockDriverState *bs,
478 int64_t sector_num, QEMUIOVector *qiov,
479 int nb_sectors,
480 BlockCompletionFunc *cb, void *opaque)
482 return vxhs_aio_rw(bs, sector_num, qiov, nb_sectors, cb,
483 opaque, VDISK_AIO_READ);
486 static BlockAIOCB *vxhs_aio_writev(BlockDriverState *bs,
487 int64_t sector_num, QEMUIOVector *qiov,
488 int nb_sectors,
489 BlockCompletionFunc *cb, void *opaque)
491 return vxhs_aio_rw(bs, sector_num, qiov, nb_sectors,
492 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;
505 * Close vDisk device
507 if (s->vdisk_hostinfo.dev_handle) {
508 iio_close(s->vdisk_hostinfo.dev_handle);
509 s->vdisk_hostinfo.dev_handle = NULL;
512 vxhs_unref();
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;
527 int ret = 0;
528 void *dev_handle = s->vdisk_hostinfo.dev_handle;
530 ret = iio_ioctl(dev_handle, IOR_VDISK_STAT, &vdisk_size, 0);
531 if (ret < 0) {
532 trace_vxhs_get_vdisk_stat_err(s->vdisk_guid, ret, errno);
533 return -EIO;
536 trace_vxhs_get_vdisk_stat(s->vdisk_guid, vdisk_size);
537 return 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
543 * to guest.
545 static int64_t vxhs_getlength(BlockDriverState *bs)
547 BDRVVXHSState *s = bs->opaque;
548 int64_t vdisk_size;
550 vdisk_size = vxhs_get_vdisk_stat(s);
551 if (vdisk_size < 0) {
552 return -EIO;
555 return vdisk_size;
558 static BlockDriver bdrv_vxhs = {
559 .format_name = "vxhs",
560 .protocol_name = "vxhs",
561 .instance_size = sizeof(BDRVVXHSState),
562 .bdrv_file_open = vxhs_open,
563 .bdrv_parse_filename = vxhs_parse_filename,
564 .bdrv_close = vxhs_close,
565 .bdrv_getlength = vxhs_getlength,
566 .bdrv_aio_readv = vxhs_aio_readv,
567 .bdrv_aio_writev = vxhs_aio_writev,
570 static void bdrv_vxhs_init(void)
572 bdrv_register(&bdrv_vxhs);
575 block_init(bdrv_vxhs_init);