]> Git Repo - qemu.git/blob - block/vxhs.c
vxhs: Switch to byte-based callbacks
[qemu.git] / block / vxhs.c
1 /*
2  * QEMU Block driver for Veritas HyperScale (VxHS)
3  *
4  * Copyright (c) 2017 Veritas Technologies LLC.
5  *
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.
8  *
9  */
10
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"
23
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"
29
30 /* Only accessed under QEMU global mutex */
31 static uint32_t vxhs_ref;
32
33 typedef enum {
34     VDISK_AIO_READ,
35     VDISK_AIO_WRITE,
36 } VDISKAIOCmd;
37
38 /*
39  * HyperScale AIO callbacks structure
40  */
41 typedef struct VXHSAIOCB {
42     BlockAIOCB common;
43     int err;
44 } VXHSAIOCB;
45
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;
51
52 /*
53  * Structure per vDisk maintained for state
54  */
55 typedef struct BDRVVXHSState {
56     VXHSvDiskHostsInfo vdisk_hostinfo; /* Per host info */
57     char *vdisk_guid;
58     char *tlscredsid; /* tlscredsid */
59 } BDRVVXHSState;
60
61 static void vxhs_complete_aio_bh(void *opaque)
62 {
63     VXHSAIOCB *acb = opaque;
64     BlockCompletionFunc *cb = acb->common.cb;
65     void *cb_opaque = acb->common.opaque;
66     int ret = 0;
67
68     if (acb->err != 0) {
69         trace_vxhs_complete_aio(acb, acb->err);
70         ret = (-EIO);
71     }
72
73     qemu_aio_unref(acb);
74     cb(cb_opaque, ret);
75 }
76
77 /*
78  * Called from a libqnio thread
79  */
80 static void vxhs_iio_callback(void *ctx, uint32_t opcode, uint32_t error)
81 {
82     VXHSAIOCB *acb = NULL;
83
84     switch (opcode) {
85     case IRP_READ_REQUEST:
86     case IRP_WRITE_REQUEST:
87
88         /*
89          * ctx is VXHSAIOCB*
90          * ctx is NULL if error is QNIOERROR_CHANNEL_HUP
91          */
92         if (ctx) {
93             acb = ctx;
94         } else {
95             trace_vxhs_iio_callback(error);
96             goto out;
97         }
98
99         if (error) {
100             if (!acb->err) {
101                 acb->err = error;
102             }
103             trace_vxhs_iio_callback(error);
104         }
105
106         aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
107                                 vxhs_complete_aio_bh, acb);
108         break;
109
110     default:
111         if (error == QNIOERROR_HUP) {
112             /*
113              * Channel failed, spontaneous notification,
114              * not in response to I/O
115              */
116             trace_vxhs_iio_callback_chnfail(error, errno);
117         } else {
118             trace_vxhs_iio_callback_unknwn(opcode, error);
119         }
120         break;
121     }
122 out:
123     return;
124 }
125
126 static QemuOptsList runtime_opts = {
127     .name = "vxhs",
128     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
129     .desc = {
130         {
131             .name = VXHS_OPT_FILENAME,
132             .type = QEMU_OPT_STRING,
133             .help = "URI to the Veritas HyperScale image",
134         },
135         {
136             .name = VXHS_OPT_VDISK_ID,
137             .type = QEMU_OPT_STRING,
138             .help = "UUID of the VxHS vdisk",
139         },
140         {
141             .name = "tls-creds",
142             .type = QEMU_OPT_STRING,
143             .help = "ID of the TLS/SSL credentials to use",
144         },
145         { /* end of list */ }
146     },
147 };
148
149 static QemuOptsList runtime_tcp_opts = {
150     .name = "vxhs_tcp",
151     .head = QTAILQ_HEAD_INITIALIZER(runtime_tcp_opts.head),
152     .desc = {
153         {
154             .name = VXHS_OPT_HOST,
155             .type = QEMU_OPT_STRING,
156             .help = "host address (ipv4 addresses)",
157         },
158         {
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"
163         },
164         { /* end of list */ }
165     },
166 };
167
168 /*
169  * Parse incoming URI and populate *options with the host
170  * and device information
171  */
172 static int vxhs_parse_uri(const char *filename, QDict *options)
173 {
174     URI *uri = NULL;
175     char *port;
176     int ret = 0;
177
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;
183     }
184
185     qdict_put_str(options, VXHS_OPT_SERVER ".host", uri->server);
186
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);
191     }
192
193     qdict_put_str(options, "vdisk-id", uri->path);
194
195     trace_vxhs_parse_uri_hostinfo(uri->server, uri->port);
196     uri_free(uri);
197
198     return ret;
199 }
200
201 static void vxhs_parse_filename(const char *filename, QDict *options,
202                                 Error **errp)
203 {
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;
208     }
209
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>");
215         }
216     }
217 }
218
219 static void vxhs_refresh_limits(BlockDriverState *bs, Error **errp)
220 {
221     /* XXX Does VXHS support AIO on less than 512-byte alignment? */
222     bs->bl.request_alignment = 512;
223 }
224
225 static int vxhs_init_and_ref(void)
226 {
227     if (vxhs_ref++ == 0) {
228         if (iio_init(QNIO_VERSION, vxhs_iio_callback)) {
229             return -ENODEV;
230         }
231     }
232     return 0;
233 }
234
235 static void vxhs_unref(void)
236 {
237     if (--vxhs_ref == 0) {
238         iio_fini();
239     }
240 }
241
242 static void vxhs_get_tls_creds(const char *id, char **cacert,
243                                char **key, char **cert, Error **errp)
244 {
245     Object *obj;
246     QCryptoTLSCreds *creds;
247     QCryptoTLSCredsX509 *creds_x509;
248
249     obj = object_resolve_path_component(
250         object_get_objects_root(), id);
251
252     if (!obj) {
253         error_setg(errp, "No TLS credentials with id '%s'",
254                    id);
255         return;
256     }
257
258     creds_x509 = (QCryptoTLSCredsX509 *)
259         object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS_X509);
260
261     if (!creds_x509) {
262         error_setg(errp, "Object with id '%s' is not TLS credentials",
263                    id);
264         return;
265     }
266
267     creds = &creds_x509->parent_obj;
268
269     if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
270         error_setg(errp,
271                    "Expecting TLS credentials with a client endpoint");
272         return;
273     }
274
275     /*
276      * Get the cacert, client_cert and client_key file names.
277      */
278     if (!creds->dir) {
279         error_setg(errp, "TLS object missing 'dir' property value");
280         return;
281     }
282
283     *cacert = g_strdup_printf("%s/%s", creds->dir,
284                               QCRYPTO_TLS_CREDS_X509_CA_CERT);
285     *cert = g_strdup_printf("%s/%s", creds->dir,
286                             QCRYPTO_TLS_CREDS_X509_CLIENT_CERT);
287     *key = g_strdup_printf("%s/%s", creds->dir,
288                            QCRYPTO_TLS_CREDS_X509_CLIENT_KEY);
289 }
290
291 static int vxhs_open(BlockDriverState *bs, QDict *options,
292                      int bdrv_flags, Error **errp)
293 {
294     BDRVVXHSState *s = bs->opaque;
295     void *dev_handlep;
296     QDict *backing_options = NULL;
297     QemuOpts *opts = NULL;
298     QemuOpts *tcp_opts = NULL;
299     char *of_vsa_addr = NULL;
300     Error *local_err = NULL;
301     const char *vdisk_id_opt;
302     const char *server_host_opt;
303     int ret = 0;
304     char *cacert = NULL;
305     char *client_key = NULL;
306     char *client_cert = NULL;
307
308     ret = vxhs_init_and_ref();
309     if (ret < 0) {
310         ret = -EINVAL;
311         goto out;
312     }
313
314     /* Create opts info from runtime_opts and runtime_tcp_opts list */
315     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
316     tcp_opts = qemu_opts_create(&runtime_tcp_opts, NULL, 0, &error_abort);
317
318     qemu_opts_absorb_qdict(opts, options, &local_err);
319     if (local_err) {
320         ret = -EINVAL;
321         goto out;
322     }
323
324     /* vdisk-id is the disk UUID */
325     vdisk_id_opt = qemu_opt_get(opts, VXHS_OPT_VDISK_ID);
326     if (!vdisk_id_opt) {
327         error_setg(&local_err, QERR_MISSING_PARAMETER, VXHS_OPT_VDISK_ID);
328         ret = -EINVAL;
329         goto out;
330     }
331
332     /* vdisk-id may contain a leading '/' */
333     if (strlen(vdisk_id_opt) > UUID_FMT_LEN + 1) {
334         error_setg(&local_err, "vdisk-id cannot be more than %d characters",
335                    UUID_FMT_LEN);
336         ret = -EINVAL;
337         goto out;
338     }
339
340     s->vdisk_guid = g_strdup(vdisk_id_opt);
341     trace_vxhs_open_vdiskid(vdisk_id_opt);
342
343     /* get the 'server.' arguments */
344     qdict_extract_subqdict(options, &backing_options, VXHS_OPT_SERVER".");
345
346     qemu_opts_absorb_qdict(tcp_opts, backing_options, &local_err);
347     if (local_err != NULL) {
348         ret = -EINVAL;
349         goto out;
350     }
351
352     server_host_opt = qemu_opt_get(tcp_opts, VXHS_OPT_HOST);
353     if (!server_host_opt) {
354         error_setg(&local_err, QERR_MISSING_PARAMETER,
355                    VXHS_OPT_SERVER"."VXHS_OPT_HOST);
356         ret = -EINVAL;
357         goto out;
358     }
359
360     if (strlen(server_host_opt) > MAXHOSTNAMELEN) {
361         error_setg(&local_err, "server.host cannot be more than %d characters",
362                    MAXHOSTNAMELEN);
363         ret = -EINVAL;
364         goto out;
365     }
366
367     /* check if we got tls-creds via the --object argument */
368     s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
369     if (s->tlscredsid) {
370         vxhs_get_tls_creds(s->tlscredsid, &cacert, &client_key,
371                            &client_cert, &local_err);
372         if (local_err != NULL) {
373             ret = -EINVAL;
374             goto out;
375         }
376         trace_vxhs_get_creds(cacert, client_key, client_cert);
377     }
378
379     s->vdisk_hostinfo.host = g_strdup(server_host_opt);
380     s->vdisk_hostinfo.port = g_ascii_strtoll(qemu_opt_get(tcp_opts,
381                                                           VXHS_OPT_PORT),
382                                                           NULL, 0);
383
384     trace_vxhs_open_hostinfo(s->vdisk_hostinfo.host,
385                              s->vdisk_hostinfo.port);
386
387     of_vsa_addr = g_strdup_printf("of://%s:%d",
388                                   s->vdisk_hostinfo.host,
389                                   s->vdisk_hostinfo.port);
390
391     /*
392      * Open qnio channel to storage agent if not opened before
393      */
394     dev_handlep = iio_open(of_vsa_addr, s->vdisk_guid, 0,
395                            cacert, client_key, client_cert);
396     if (dev_handlep == NULL) {
397         trace_vxhs_open_iio_open(of_vsa_addr);
398         ret = -ENODEV;
399         goto out;
400     }
401     s->vdisk_hostinfo.dev_handle = dev_handlep;
402
403 out:
404     g_free(of_vsa_addr);
405     qobject_unref(backing_options);
406     qemu_opts_del(tcp_opts);
407     qemu_opts_del(opts);
408     g_free(cacert);
409     g_free(client_key);
410     g_free(client_cert);
411
412     if (ret < 0) {
413         vxhs_unref();
414         error_propagate(errp, local_err);
415         g_free(s->vdisk_hostinfo.host);
416         g_free(s->vdisk_guid);
417         g_free(s->tlscredsid);
418         s->vdisk_guid = NULL;
419     }
420
421     return ret;
422 }
423
424 static const AIOCBInfo vxhs_aiocb_info = {
425     .aiocb_size = sizeof(VXHSAIOCB)
426 };
427
428 /*
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.
432  */
433 static BlockAIOCB *vxhs_aio_rw(BlockDriverState *bs, uint64_t offset,
434                                QEMUIOVector *qiov, uint64_t size,
435                                BlockCompletionFunc *cb, void *opaque,
436                                VDISKAIOCmd iodir)
437 {
438     VXHSAIOCB *acb = NULL;
439     BDRVVXHSState *s = bs->opaque;
440     int iio_flags = 0;
441     int ret = 0;
442     void *dev_handle = s->vdisk_hostinfo.dev_handle;
443
444     acb = qemu_aio_get(&vxhs_aiocb_info, bs, cb, opaque);
445
446     /*
447      * Initialize VXHSAIOCB.
448      */
449     acb->err = 0;
450
451     iio_flags = IIO_FLAG_ASYNC;
452
453     switch (iodir) {
454     case VDISK_AIO_WRITE:
455             ret = iio_writev(dev_handle, acb, qiov->iov, qiov->niov,
456                              offset, size, iio_flags);
457             break;
458     case VDISK_AIO_READ:
459             ret = iio_readv(dev_handle, acb, qiov->iov, qiov->niov,
460                             offset, size, iio_flags);
461             break;
462     default:
463             trace_vxhs_aio_rw_invalid(iodir);
464             goto errout;
465     }
466
467     if (ret != 0) {
468         trace_vxhs_aio_rw_ioerr(s->vdisk_guid, iodir, size, offset,
469                                 acb, ret, errno);
470         goto errout;
471     }
472     return &acb->common;
473
474 errout:
475     qemu_aio_unref(acb);
476     return NULL;
477 }
478
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)
483 {
484     return vxhs_aio_rw(bs, offset, qiov, bytes, cb, opaque, VDISK_AIO_READ);
485 }
486
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)
491 {
492     return vxhs_aio_rw(bs, offset, qiov, bytes, cb, opaque, VDISK_AIO_WRITE);
493 }
494
495 static void vxhs_close(BlockDriverState *bs)
496 {
497     BDRVVXHSState *s = bs->opaque;
498
499     trace_vxhs_close(s->vdisk_guid);
500
501     g_free(s->vdisk_guid);
502     s->vdisk_guid = NULL;
503
504     /*
505      * Close vDisk device
506      */
507     if (s->vdisk_hostinfo.dev_handle) {
508         iio_close(s->vdisk_hostinfo.dev_handle);
509         s->vdisk_hostinfo.dev_handle = NULL;
510     }
511
512     vxhs_unref();
513
514     /*
515      * Free the dynamically allocated host string etc
516      */
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;
522 }
523
524 static int64_t vxhs_get_vdisk_stat(BDRVVXHSState *s)
525 {
526     int64_t vdisk_size = -1;
527     int ret = 0;
528     void *dev_handle = s->vdisk_hostinfo.dev_handle;
529
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;
534     }
535
536     trace_vxhs_get_vdisk_stat(s->vdisk_guid, vdisk_size);
537     return vdisk_size;
538 }
539
540 /*
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.
544  */
545 static int64_t vxhs_getlength(BlockDriverState *bs)
546 {
547     BDRVVXHSState *s = bs->opaque;
548     int64_t vdisk_size;
549
550     vdisk_size = vxhs_get_vdisk_stat(s);
551     if (vdisk_size < 0) {
552         return -EIO;
553     }
554
555     return vdisk_size;
556 }
557
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_refresh_limits          = vxhs_refresh_limits,
565     .bdrv_close                   = vxhs_close,
566     .bdrv_getlength               = vxhs_getlength,
567     .bdrv_aio_preadv              = vxhs_aio_preadv,
568     .bdrv_aio_pwritev             = vxhs_aio_pwritev,
569 };
570
571 static void bdrv_vxhs_init(void)
572 {
573     bdrv_register(&bdrv_vxhs);
574 }
575
576 block_init(bdrv_vxhs_init);
This page took 0.057613 seconds and 4 git commands to generate.