Mathieu Poirier <mathieu.poir...@linaro.org> writes:

> Following in the footsteps of what whas done for vhost-user-i2c
> and virtiofsd, introduce a random number generator (RNG) backend
> that communicates with a vhost-user server to retrieve entropy.
> That way another VMM could be using the same vhost-user daemon and
> avoid having to write yet another RNG driver.
>
> Signed-off-by: Mathieu Poirier <mathieu.poir...@linaro.org>
> ---
>  hw/virtio/Kconfig                  |   5 +
>  hw/virtio/meson.build              |   1 +

FWIW there are simple merge failures for the meson and Kconfig parts due
to I2C being merged.

<snip>
> +
> +    rng->vhost_dev.nvqs = 1;
> +    rng->vhost_dev.vqs = g_new0(struct vhost_virtqueue, rng->vhost_dev.nvqs);
> +    if (!rng->vhost_dev.vqs) {
> +        error_setg_errno(errp, -1, "memory allocation failed");
> +        goto vhost_dev_init_failed;
> +    }

g_new0 will abort on memory allocation failure (which is fine for
userspace ;-) so you don't need the check and erro handling exit here.

> +
> +    ret = vhost_dev_init(&rng->vhost_dev, &rng->vhost_user,
> +                         VHOST_BACKEND_TYPE_USER, 0, errp);
> +    if (ret < 0) {
> +        error_setg_errno(errp, -ret, "vhost_dev_init() failed");
> +        goto vhost_dev_init_failed;
> +    }
> +
> +    qemu_chr_fe_set_handlers(&rng->chardev, NULL, NULL, vu_rng_event, NULL,
> +                             dev, NULL, true);
> +
> +    return;
> +
> +vhost_dev_init_failed:
> +    virtio_delete_queue(rng->req_vq);
> +virtio_add_queue_failed:
> +    virtio_cleanup(vdev);
> +    vhost_user_cleanup(&rng->vhost_user);
> +}
> +
> +static void vu_rng_device_unrealize(DeviceState *dev)
> +{
> +    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
> +    VHostUserRNG *rng = VHOST_USER_RNG(dev);
> +
> +    vu_rng_set_status(vdev, 0);
> +
> +    vhost_dev_cleanup(&rng->vhost_dev);
> +    g_free(rng->vhost_dev.vqs);
> +    rng->vhost_dev.vqs = NULL;
> +    virtio_delete_queue(rng->req_vq);
> +    virtio_cleanup(vdev);
> +    vhost_user_cleanup(&rng->vhost_user);
> +}
> +
> +static const VMStateDescription vu_rng_vmstate = {
> +    .name = "vhost-user-rng",
> +    .unmigratable = 1,
> +};
> +
> +static Property vu_rng_properties[] = {
> +    DEFINE_PROP_CHR("chardev", VHostUserRNG, chardev),
> +    DEFINE_PROP_END_OF_LIST(),
> +};
> +
> +static void vu_rng_class_init(ObjectClass *klass, void *data)
> +{
> +    DeviceClass *dc = DEVICE_CLASS(klass);
> +    VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
> +
> +    device_class_set_props(dc, vu_rng_properties);
> +    dc->vmsd = &vu_rng_vmstate;
> +    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
> +
> +    vdc->realize = vu_rng_device_realize;
> +    vdc->unrealize = vu_rng_device_unrealize;
> +    vdc->get_features = vu_rng_get_features;
> +    vdc->set_status = vu_rng_set_status;
> +    vdc->guest_notifier_mask = vu_rng_guest_notifier_mask;
> +    vdc->guest_notifier_pending = vu_rng_guest_notifier_pending;
> +}
> +
> +static const TypeInfo vu_rng_info = {
> +    .name = TYPE_VHOST_USER_RNG,
> +    .parent = TYPE_VIRTIO_DEVICE,
> +    .instance_size = sizeof(VHostUserRNG),
> +    .class_init = vu_rng_class_init,
> +};
> +
> +static void vu_rng_register_types(void)
> +{
> +    type_register_static(&vu_rng_info);
> +}
> +
> +type_init(vu_rng_register_types)
> diff --git a/include/hw/virtio/vhost-user-rng.h 
> b/include/hw/virtio/vhost-user-rng.h
> new file mode 100644
> index 000000000000..071539996d1d
> --- /dev/null
> +++ b/include/hw/virtio/vhost-user-rng.h
> @@ -0,0 +1,33 @@
> +/*
> + * Vhost-user RNG virtio device
> + *
> + * Copyright (c) 2021 Mathieu Poirier <mathieu.poir...@linaro.org>
> + *
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + */
> +
> +#ifndef _QEMU_VHOST_USER_RNG_H
> +#define _QEMU_VHOST_USER_RNG_H
> +
> +#include "hw/virtio/virtio.h"
> +#include "hw/virtio/vhost.h"
> +#include "hw/virtio/vhost-user.h"
> +#include "chardev/char-fe.h"
> +
> +#define TYPE_VHOST_USER_RNG "vhost-user-rng"
> +OBJECT_DECLARE_SIMPLE_TYPE(VHostUserRNG, VHOST_USER_RNG)
> +
> +struct VHostUserRNG {
> +    /*< private >*/
> +    VirtIODevice parent;
> +    CharBackend chardev;
> +    struct vhost_virtqueue *vhost_vq;
> +    struct vhost_dev vhost_dev;
> +    VhostUserState vhost_user;
> +    VirtQueue *req_vq;
> +    bool connected;
> +
> +    /*< public >*/
> +};
> +
> +#endif /* _QEMU_VHOST_USER_RNG_H */

Otherwise:

Reviewed-by: Alex Bennée <alex.ben...@linaro.org>

-- 
Alex Bennée

Reply via email to