From nobody Fri Mar 29 00:13:45 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1634072559; cv=none; d=zohomail.com; s=zohoarc; b=iWVubfdpf3EI9BTRc+qrdHrioCyoDlIAi9pEZkTM/h+9jCs+xEFn2YPYnTmeD8uGyNmUdFF4Dujs05GjXwiU3LXcDPMCcRokQec1BWh/21jrRFNrSsrHmIph9ZaivjWwHHOY4qP1tcOaMu0PQ0eLoaBekDuzVGduTGmiw0WAIyo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1634072559; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=vhj6xazBaJToXUVOSdB9AuUCAs0ejcKbfrLA1IUKsDg=; b=Giw2JWjQRbXXonIVBcp52f9fD7XvJ1RnCNnYgOTx2M7wJPpuvGQGvIPCBct04GzsZD6lYJIscI12vVFZ3KsTV7ZdhiwDQpMDKhBeVohAvN5d7MzD7rYG8FtU6ZYn5Drc85vkzG4szqjyjVK8RfRdWFcUTXW3z3KDw5dAs6lXbsc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1634072559265491.0307545444475; Tue, 12 Oct 2021 14:02:39 -0700 (PDT) Received: from localhost ([::1]:49684 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1maOuy-0008Ub-W9 for importer@patchew.org; Tue, 12 Oct 2021 17:02:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44268) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maOrh-0005uT-2i for qemu-devel@nongnu.org; Tue, 12 Oct 2021 16:59:13 -0400 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]:39476) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1maOrf-0005Rx-0r for qemu-devel@nongnu.org; Tue, 12 Oct 2021 16:59:12 -0400 Received: by mail-pg1-x52b.google.com with SMTP id g184so264047pgc.6 for ; Tue, 12 Oct 2021 13:59:10 -0700 (PDT) Received: from p14s.cg.shawcable.net (S0106889e681aac74.cg.shawcable.net. [68.147.0.187]) by smtp.gmail.com with ESMTPSA id e24sm11695087pfn.8.2021.10.12.13.59.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Oct 2021 13:59:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vhj6xazBaJToXUVOSdB9AuUCAs0ejcKbfrLA1IUKsDg=; b=z4fxZcRJiDGmmiNlESoVCR5m8tBgBaqPtUySU9QhEvgc8Xkp2MWG7ezPgTexj0CtOE 8yPrz26yhGUfy/NE3u1p5kwc7r4cfBlBGnOR7D+iA5wUudnGsBHC6t6mASLoNgFEwedl 6GIFSWbfn215suFT1XXnXdZoqjLrHEz7x3kImqRD+vPW5BZ4SY7rulqM18urz7IemGZQ +BwgLXXJnkMUqmsUZu1khFMvzUfSovNowOxWGOINrpeeDAIybU5fNpQ9EXV2nm4k5h7z VMKdd7z7lLrBEYnT93dAl3EIJRxsmlyrtWuwodn2kA8WOlVOlo7QwTKhil8c7vrydz/h 0Kng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vhj6xazBaJToXUVOSdB9AuUCAs0ejcKbfrLA1IUKsDg=; b=xUNa7vENx0YJ5Z5m0aRYfKdcUPvFv/OQ+Lbfstuh14ioE8j1KutnvIkZB+APD2ZLG4 pa+k0MaplH5kJt1/+vc0tzAp/45CHrA2vfr1JUMgnREP7RsaXIu+JtlFXoIvlNRoM4RI EPHLvGn9aE9QUky4Ma3JXh9Aw7NUHKhhL3VKirJclWAd2FV664FKFqihDlP0I6xSYsG8 cWjFVyiqOwlVsLGNEU1T9QnK2s6ZZgcloq6ddi18EEaCeVoXGsqLLQQ30FhmLamKBHZk uYpqhllpbOvx6HMPLu5EgLDF23FbBSeNwvjrZ9hipQ1pPXJvOeiyhRhOmFe8U4Fxptud rYIg== X-Gm-Message-State: AOAM531nR4i4BnVnRTJocXmdXTyfpp9nG9uwIHNST3y4nkDMkVQRqQGH rsZ4TcnS75UFNpLQmioxhVQdNg== X-Google-Smtp-Source: ABdhPJwSpoi1jqReB7K8vygGhX++fgvhMOa8AhTYSLhiM5KLZK9FDk53e9SObOr0J6TkpDhm6uuumw== X-Received: by 2002:a63:fd06:: with SMTP id d6mr24784719pgh.466.1634072349659; Tue, 12 Oct 2021 13:59:09 -0700 (PDT) From: Mathieu Poirier To: mst@redhat.com Subject: [PATCH v5 1/3] vhost-user-rng: Add vhost-user-rng implementation Date: Tue, 12 Oct 2021 14:59:02 -0600 Message-Id: <20211012205904.4106769-2-mathieu.poirier@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211012205904.4106769-1-mathieu.poirier@linaro.org> References: <20211012205904.4106769-1-mathieu.poirier@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::52b; envelope-from=mathieu.poirier@linaro.org; helo=mail-pg1-x52b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1634072559676100001 Introduce a random number generator (RNG) backend that communicates with a vhost-user server to retrieve entropy. That way other VMM that comply with the vhost user protocl can use the same vhost-user daemon without having to write yet another RNG driver. Reviewed-by: Alex Benn=C3=A9e Signed-off-by: Mathieu Poirier --- hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 1 + hw/virtio/vhost-user-rng.c | 289 +++++++++++++++++++++++++++++ include/hw/virtio/vhost-user-rng.h | 33 ++++ 4 files changed, 328 insertions(+) create mode 100644 hw/virtio/vhost-user-rng.c create mode 100644 include/hw/virtio/vhost-user-rng.h diff --git a/hw/virtio/Kconfig b/hw/virtio/Kconfig index 35ab45e2095c..c144d42f9bd0 100644 --- a/hw/virtio/Kconfig +++ b/hw/virtio/Kconfig @@ -63,3 +63,8 @@ config VHOST_USER_I2C bool default y depends on VIRTIO && VHOST_USER + +config VHOST_USER_RNG + bool + default y + depends on VIRTIO && VHOST_USER diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index bc352a600911..ae6b2cde1068 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -27,6 +27,7 @@ virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files= ('virtio-iommu.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2= c.c')) virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_I2C'], if_tru= e: files('vhost-user-i2c-pci.c')) +virtio_ss.add(when: 'CONFIG_VHOST_USER_RNG', if_true: files('vhost-user-rn= g.c')) =20 virtio_pci_ss =3D ss.source_set() virtio_pci_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-= pci.c')) diff --git a/hw/virtio/vhost-user-rng.c b/hw/virtio/vhost-user-rng.c new file mode 100644 index 000000000000..209ee5bf9acd --- /dev/null +++ b/hw/virtio/vhost-user-rng.c @@ -0,0 +1,289 @@ +/* + * Vhost-user RNG virtio device + * + * Copyright (c) 2021 Mathieu Poirier + * + * Implementation seriously tailored on vhost-user-i2c.c + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "hw/qdev-properties.h" +#include "hw/virtio/virtio-bus.h" +#include "hw/virtio/vhost-user-rng.h" +#include "qemu/error-report.h" +#include "standard-headers/linux/virtio_ids.h" + +static void vu_rng_start(VirtIODevice *vdev) +{ + VHostUserRNG *rng =3D VHOST_USER_RNG(vdev); + BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); + VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); + int ret; + int i; + + if (!k->set_guest_notifiers) { + error_report("binding does not support guest notifiers"); + return; + } + + ret =3D vhost_dev_enable_notifiers(&rng->vhost_dev, vdev); + if (ret < 0) { + error_report("Error enabling host notifiers: %d", -ret); + return; + } + + ret =3D k->set_guest_notifiers(qbus->parent, rng->vhost_dev.nvqs, true= ); + if (ret < 0) { + error_report("Error binding guest notifier: %d", -ret); + goto err_host_notifiers; + } + + rng->vhost_dev.acked_features =3D vdev->guest_features; + ret =3D vhost_dev_start(&rng->vhost_dev, vdev); + if (ret < 0) { + error_report("Error starting vhost-user-rng: %d", -ret); + goto err_guest_notifiers; + } + + /* + * guest_notifier_mask/pending not used yet, so just unmask + * everything here. virtio-pci will do the right thing by + * enabling/disabling irqfd. + */ + for (i =3D 0; i < rng->vhost_dev.nvqs; i++) { + vhost_virtqueue_mask(&rng->vhost_dev, vdev, i, false); + } + + return; + +err_guest_notifiers: + k->set_guest_notifiers(qbus->parent, rng->vhost_dev.nvqs, false); +err_host_notifiers: + vhost_dev_disable_notifiers(&rng->vhost_dev, vdev); +} + +static void vu_rng_stop(VirtIODevice *vdev) +{ + VHostUserRNG *rng =3D VHOST_USER_RNG(vdev); + BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); + VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); + int ret; + + if (!k->set_guest_notifiers) { + return; + } + + vhost_dev_stop(&rng->vhost_dev, vdev); + + ret =3D k->set_guest_notifiers(qbus->parent, rng->vhost_dev.nvqs, fals= e); + if (ret < 0) { + error_report("vhost guest notifier cleanup failed: %d", ret); + return; + } + + vhost_dev_disable_notifiers(&rng->vhost_dev, vdev); +} + +static void vu_rng_set_status(VirtIODevice *vdev, uint8_t status) +{ + VHostUserRNG *rng =3D VHOST_USER_RNG(vdev); + bool should_start =3D status & VIRTIO_CONFIG_S_DRIVER_OK; + + if (!vdev->vm_running) { + should_start =3D false; + } + + if (rng->vhost_dev.started =3D=3D should_start) { + return; + } + + if (should_start) { + vu_rng_start(vdev); + } else { + vu_rng_stop(vdev); + } +} + +static uint64_t vu_rng_get_features(VirtIODevice *vdev, + uint64_t requested_features, Error **e= rrp) +{ + /* No feature bits used yet */ + return requested_features; +} + +static void vu_rng_handle_output(VirtIODevice *vdev, VirtQueue *vq) +{ + /* + * Not normally called; it's the daemon that handles the queue; + * however virtio's cleanup path can call this. + */ +} + +static void vu_rng_guest_notifier_mask(VirtIODevice *vdev, int idx, bool m= ask) +{ + VHostUserRNG *rng =3D VHOST_USER_RNG(vdev); + + vhost_virtqueue_mask(&rng->vhost_dev, vdev, idx, mask); +} + +static bool vu_rng_guest_notifier_pending(VirtIODevice *vdev, int idx) +{ + VHostUserRNG *rng =3D VHOST_USER_RNG(vdev); + + return vhost_virtqueue_pending(&rng->vhost_dev, idx); +} + +static void vu_rng_connect(DeviceState *dev) +{ + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + VHostUserRNG *rng =3D VHOST_USER_RNG(vdev); + + if (rng->connected) { + return; + } + + rng->connected =3D true; + + /* restore vhost state */ + if (virtio_device_started(vdev, vdev->status)) { + vu_rng_start(vdev); + } +} + +static void vu_rng_disconnect(DeviceState *dev) +{ + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + VHostUserRNG *rng =3D VHOST_USER_RNG(vdev); + + if (!rng->connected) { + return; + } + + rng->connected =3D false; + + if (rng->vhost_dev.started) { + vu_rng_stop(vdev); + } +} + +static void vu_rng_event(void *opaque, QEMUChrEvent event) +{ + DeviceState *dev =3D opaque; + + switch (event) { + case CHR_EVENT_OPENED: + vu_rng_connect(dev); + break; + case CHR_EVENT_CLOSED: + vu_rng_disconnect(dev); + break; + case CHR_EVENT_BREAK: + case CHR_EVENT_MUX_IN: + case CHR_EVENT_MUX_OUT: + /* Ignore */ + break; + } +} + +static void vu_rng_device_realize(DeviceState *dev, Error **errp) +{ + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + VHostUserRNG *rng =3D VHOST_USER_RNG(dev); + int ret; + + if (!rng->chardev.chr) { + error_setg(errp, "missing chardev"); + return; + } + + if (!vhost_user_init(&rng->vhost_user, &rng->chardev, errp)) { + return; + } + + virtio_init(vdev, "vhost-user-rng", VIRTIO_ID_RNG, 0); + + rng->req_vq =3D virtio_add_queue(vdev, 4, vu_rng_handle_output); + if (!rng->req_vq) { + error_setg_errno(errp, -1, "virtio_add_queue() failed"); + goto virtio_add_queue_failed; + } + + rng->vhost_dev.nvqs =3D 1; + rng->vhost_dev.vqs =3D g_new0(struct vhost_virtqueue, rng->vhost_dev.n= vqs); + ret =3D 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 =3D VIRTIO_DEVICE(dev); + VHostUserRNG *rng =3D 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 =3D NULL; + virtio_delete_queue(rng->req_vq); + virtio_cleanup(vdev); + vhost_user_cleanup(&rng->vhost_user); +} + +static const VMStateDescription vu_rng_vmstate =3D { + .name =3D "vhost-user-rng", + .unmigratable =3D 1, +}; + +static Property vu_rng_properties[] =3D { + DEFINE_PROP_CHR("chardev", VHostUserRNG, chardev), + DEFINE_PROP_END_OF_LIST(), +}; + +static void vu_rng_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + VirtioDeviceClass *vdc =3D VIRTIO_DEVICE_CLASS(klass); + + device_class_set_props(dc, vu_rng_properties); + dc->vmsd =3D &vu_rng_vmstate; + set_bit(DEVICE_CATEGORY_INPUT, dc->categories); + + vdc->realize =3D vu_rng_device_realize; + vdc->unrealize =3D vu_rng_device_unrealize; + vdc->get_features =3D vu_rng_get_features; + vdc->set_status =3D vu_rng_set_status; + vdc->guest_notifier_mask =3D vu_rng_guest_notifier_mask; + vdc->guest_notifier_pending =3D vu_rng_guest_notifier_pending; +} + +static const TypeInfo vu_rng_info =3D { + .name =3D TYPE_VHOST_USER_RNG, + .parent =3D TYPE_VIRTIO_DEVICE, + .instance_size =3D sizeof(VHostUserRNG), + .class_init =3D 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-u= ser-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 + * + * 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 */ --=20 2.25.1 From nobody Fri Mar 29 00:13:45 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1634072668; cv=none; d=zohomail.com; s=zohoarc; b=RBaG0OTQ5CEw/yDnS9q0ERXerlmBBLpqpdZ/bBE6CTsrwKGLh039GnyhKHYZGTz16Ik+6xFQfeJYdVtF9Pisq72gwyb1b4IYyuj+UG2PVAOjlZ7dM5np7TLLah9GC0mgUYt1h+iPvjDGGZcEqUXSWRjP9FHIIqFRUS6D4ahV6jE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1634072668; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ATiigdiyl6eSZmUol7/b9Qr5eElVhu1VmJG7FVbfZqg=; b=AIPNk6OFRlE03pIMfHzSvbfTKoH9Rs1eW1HOeoKlP7aXl97vSSuaG0N/M3vukGcS4HOoEOTw0N2+SQgjHVZoFjCyKrHJuHfnhkRcn4nw7ZkvaAo10hJVmDWRqiHQE0OBDwqDpUfTqw7Y2r1n9+kPs4NPE4P/i93ZlvdyD7r56Us= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1634072668200134.50003159479036; Tue, 12 Oct 2021 14:04:28 -0700 (PDT) Received: from localhost ([::1]:53808 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1maOwl-0003Kx-8p for importer@patchew.org; Tue, 12 Oct 2021 17:04:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44280) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maOri-0005w8-Iz for qemu-devel@nongnu.org; Tue, 12 Oct 2021 16:59:14 -0400 Received: from mail-pg1-x530.google.com ([2607:f8b0:4864:20::530]:34386) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1maOrg-0005ZF-Ut for qemu-devel@nongnu.org; Tue, 12 Oct 2021 16:59:14 -0400 Received: by mail-pg1-x530.google.com with SMTP id 133so284996pgb.1 for ; Tue, 12 Oct 2021 13:59:12 -0700 (PDT) Received: from p14s.cg.shawcable.net (S0106889e681aac74.cg.shawcable.net. [68.147.0.187]) by smtp.gmail.com with ESMTPSA id e24sm11695087pfn.8.2021.10.12.13.59.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Oct 2021 13:59:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ATiigdiyl6eSZmUol7/b9Qr5eElVhu1VmJG7FVbfZqg=; b=XQfYPvEOCV3YdS1ENJoEXO0r1wYzcUtn02COLuUYSsN66QX57k9g9dTd60hfdG2pre 1WqC3aJLiKlwAlFTOtsSNYP1FuhWv3u9fuO84VToWMb7CNa3ukKIIa9kEg95R63VfKPP s5BUcg9GBwF15MuTyO1SHZgBYwLsY4JiH9IxypxAEYvmP+2NKL0KgDpzBkFs+XECWNN/ A3SRTc5LXpVhmv5j7Y0Ufgw6jD0JbcycKM20LiKVQuwZ0AahktluYHg0Ka95jaIeK1q0 hvNzc3yts+7EWC0/iUFpWr9BmGSyMSbuxn3DtjtSgGILm7G3K8wDRgWpQkrlC8eCKsQh WxTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ATiigdiyl6eSZmUol7/b9Qr5eElVhu1VmJG7FVbfZqg=; b=VGq9dgd0NnSuwEhLK+XRQuTRKr/NRekdwWt7dzcY8EQ9VZDg9wLxVbWHTQ13DudOmC 7iBwf9T18bsO1xzHKrFv7Zqll++u8PnE7VEIT9RduTiYWvqk+HQ3xp6Ms7/w+NTR7DV6 e8gEEju38hqKeMTVZ+tOcfr1EmF8xJ/vbj5ftsZhctsFVNZkqSxClFra5NknJzVa7y6c aH1XezJkPpHQ0/jP4syxUCcSGZUkgt/Ac/4z1TMBSXkm+BYTse5YxpUm70syfP7UprKj nbilmpIfSTEaQlGGaEMa1pvTc1FbnCKuuRjwDP64E7o2Bkxt1zltq1EPDkvoR4UR3zVX 1Gkw== X-Gm-Message-State: AOAM532Miqv4ptaZ7mOBTdQLy/XeX/zwZwRws9MwassXBmVcMdHV4PE9 +J4T8MJEdSRvGGPx9gTv64u1MQ== X-Google-Smtp-Source: ABdhPJz1jcZQ0SvudDWBOc8T0GnCG797ydBrzfuAhPfZhvgMnEzP303JmufIAmbIgl1EMQQiFjy1RA== X-Received: by 2002:a05:6a00:1488:b0:44d:25b2:f80b with SMTP id v8-20020a056a00148800b0044d25b2f80bmr10903806pfu.20.1634072351521; Tue, 12 Oct 2021 13:59:11 -0700 (PDT) From: Mathieu Poirier To: mst@redhat.com Subject: [PATCH v5 2/3] vhost-user-rng-pci: Add vhost-user-rng-pci implementation Date: Tue, 12 Oct 2021 14:59:03 -0600 Message-Id: <20211012205904.4106769-3-mathieu.poirier@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211012205904.4106769-1-mathieu.poirier@linaro.org> References: <20211012205904.4106769-1-mathieu.poirier@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::530; envelope-from=mathieu.poirier@linaro.org; helo=mail-pg1-x530.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1634072669754100001 This patch provides a PCI bus interface to the vhost-user-rng backend. Reviewed-by: Alex Benn=C3=A9e Signed-off-by: Mathieu Poirier --- hw/virtio/meson.build | 1 + hw/virtio/vhost-user-rng-pci.c | 79 ++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 hw/virtio/vhost-user-rng-pci.c diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index ae6b2cde1068..521f7d64a86a 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -28,6 +28,7 @@ virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('= virtio-mem.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2= c.c')) virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_I2C'], if_tru= e: files('vhost-user-i2c-pci.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER_RNG', if_true: files('vhost-user-rn= g.c')) +virtio_ss.add(when: ['CONFIG_VHOST_USER_RNG', 'CONFIG_VIRTIO_PCI'], if_tru= e: files('vhost-user-rng-pci.c')) =20 virtio_pci_ss =3D ss.source_set() virtio_pci_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-= pci.c')) diff --git a/hw/virtio/vhost-user-rng-pci.c b/hw/virtio/vhost-user-rng-pci.c new file mode 100644 index 000000000000..c83dc8681385 --- /dev/null +++ b/hw/virtio/vhost-user-rng-pci.c @@ -0,0 +1,79 @@ +/* + * Vhost-user RNG virtio device PCI glue + * + * Copyright (c) 2021 Mathieu Poirier + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "hw/qdev-properties.h" +#include "hw/virtio/vhost-user-rng.h" +#include "virtio-pci.h" + +struct VHostUserRNGPCI { + VirtIOPCIProxy parent_obj; + VHostUserRNG vdev; +}; + +typedef struct VHostUserRNGPCI VHostUserRNGPCI; + +#define TYPE_VHOST_USER_RNG_PCI "vhost-user-rng-pci-base" + +DECLARE_INSTANCE_CHECKER(VHostUserRNGPCI, VHOST_USER_RNG_PCI, + TYPE_VHOST_USER_RNG_PCI) + +static Property vhost_user_rng_pci_properties[] =3D { + DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, + DEV_NVECTORS_UNSPECIFIED), + DEFINE_PROP_END_OF_LIST(), +}; + +static void vhost_user_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **e= rrp) +{ + VHostUserRNGPCI *dev =3D VHOST_USER_RNG_PCI(vpci_dev); + DeviceState *vdev =3D DEVICE(&dev->vdev); + + if (vpci_dev->nvectors =3D=3D DEV_NVECTORS_UNSPECIFIED) { + vpci_dev->nvectors =3D 1; + } + + qdev_realize(vdev, BUS(&vpci_dev->bus), errp); +} + +static void vhost_user_rng_pci_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + VirtioPCIClass *k =3D VIRTIO_PCI_CLASS(klass); + PCIDeviceClass *pcidev_k =3D PCI_DEVICE_CLASS(klass); + k->realize =3D vhost_user_rng_pci_realize; + set_bit(DEVICE_CATEGORY_INPUT, dc->categories); + device_class_set_props(dc, vhost_user_rng_pci_properties); + pcidev_k->vendor_id =3D PCI_VENDOR_ID_REDHAT_QUMRANET; + pcidev_k->device_id =3D 0; /* Set by virtio-pci based on virtio id */ + pcidev_k->revision =3D 0x00; + pcidev_k->class_id =3D PCI_CLASS_OTHERS; +} + +static void vhost_user_rng_pci_instance_init(Object *obj) +{ + VHostUserRNGPCI *dev =3D VHOST_USER_RNG_PCI(obj); + + virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), + TYPE_VHOST_USER_RNG); +} + +static const VirtioPCIDeviceTypeInfo vhost_user_rng_pci_info =3D { + .base_name =3D TYPE_VHOST_USER_RNG_PCI, + .non_transitional_name =3D "vhost-user-rng-pci", + .instance_size =3D sizeof(VHostUserRNGPCI), + .instance_init =3D vhost_user_rng_pci_instance_init, + .class_init =3D vhost_user_rng_pci_class_init, +}; + +static void vhost_user_rng_pci_register(void) +{ + virtio_pci_types_register(&vhost_user_rng_pci_info); +} + +type_init(vhost_user_rng_pci_register); --=20 2.25.1 From nobody Fri Mar 29 00:13:45 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1634072741; cv=none; d=zohomail.com; s=zohoarc; b=iCc3H07E47eAp7oAANLKL7eFRuMUukoW2KSAs1pyTsq5kezZZ/kQIIiwrAycWCCbUoiXzHu3CcyDIJGKGHTKmt0K7XE6oTmtrB+sTfaK5cU7DfeBh7iqlLqBYqKBLJzBqfec1PMx6yrAvRbQLeWeW8z7nDqSvLr3LEP0U1gzVdE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1634072741; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=CpNsM5XY4rcID/wtOi7sQNeAxSi3CpYv6QrAcnaW0PY=; b=ajy9d7ghukhqXJql/d1YEJ73c01nHNJT6r4IqSnCYsJ99fBxAWMbfxuRWVfCG6xp++GtB0d/a/mQGEX3il2ZQ3mwbSl7YGmdups6XYnLOJZkMzQ0QjjMObTRgMOXQM7p4zUMIu7fwCWACzopaZq0oxx2N2Z57PmHUqQ4NLmRPeI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1634072741786779.5704134115928; Tue, 12 Oct 2021 14:05:41 -0700 (PDT) Received: from localhost ([::1]:56654 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1maOxw-0005Jx-SJ for importer@patchew.org; Tue, 12 Oct 2021 17:05:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44294) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maOrn-00060P-E8 for qemu-devel@nongnu.org; Tue, 12 Oct 2021 16:59:19 -0400 Received: from mail-pj1-x102c.google.com ([2607:f8b0:4864:20::102c]:46868) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1maOrl-0005b1-6q for qemu-devel@nongnu.org; Tue, 12 Oct 2021 16:59:19 -0400 Received: by mail-pj1-x102c.google.com with SMTP id pi19-20020a17090b1e5300b0019fdd3557d3so588657pjb.5 for ; Tue, 12 Oct 2021 13:59:15 -0700 (PDT) Received: from p14s.cg.shawcable.net (S0106889e681aac74.cg.shawcable.net. [68.147.0.187]) by smtp.gmail.com with ESMTPSA id e24sm11695087pfn.8.2021.10.12.13.59.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Oct 2021 13:59:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CpNsM5XY4rcID/wtOi7sQNeAxSi3CpYv6QrAcnaW0PY=; b=DYOct+Q82N0NimPcQyb6gNFGkh5UNQXH7Pbk8X+hURmXLyeLCZvYgsWVARPe7vgK8u 3mRSo5sMXkNGmg7dekjw8VWjwUdULZykUPqy9gngU663QamtndRvrma+jdL4ikNh6ePj ixpRc/JemUW241LvW2x/B2kg/XfbGpRZ67Npr2TqXi+8W/p7Z7czQc/SNUlg7DTn8Rnh 7y27WdzU3zHH7aN4mna7v7h2A26L/8WXr5qQTKj6r1g7hbrowFO/F0mdqsXDEKalB/5U a1E3PWk0XUwzhbUKPoJRX73yN08mVv9SBtfzS6Bhd2GfBMDhg661YlzKwxzqluOB2X87 3mlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CpNsM5XY4rcID/wtOi7sQNeAxSi3CpYv6QrAcnaW0PY=; b=CJxAyADiWkSo14nQRbcKACD0SvDWrXN6hBGbkRB+2elCUi8IhQkgBCK42SW0tS5Bof wknBkFmGfEMmz1pfbdCp59ohCZnFrc6Fv2VIw2LF5OCGG9bygukFn6M306yMxTabfESK n6HBcFGyIBZ7mvhkokLwKEpZlhXeeD/nsoNPLYte0SHqii4GoZANjWDt3eYv4euJzI8T n8Wt4ZDlrNnuYiQX70ZEjbYBuX832BA3ZUayF7RDcUT3hmtcfA/Yp1uR9+GNWFR9+4Jb 3PRLFnabkDDYj4LpbF0DqprlTCObdsukPQJAyhO8jginmns3tcIjd9dJoLVi4e4pLL6y qh6Q== X-Gm-Message-State: AOAM533/kr3yaqsQX7TpjmjbYoYssk897sKpo4xLG7qyfoOrcBOBrVZS OWzTJor7+pr3LI6qiGv81lTxDw== X-Google-Smtp-Source: ABdhPJyuG5k9ZK3PwT+MOO1Zyboyv2IUrihCgtUk377C7UlOZCj4bIT33xWC0sFy9lY+efT6kIi9vA== X-Received: by 2002:a17:90a:a41:: with SMTP id o59mr8697118pjo.243.1634072354424; Tue, 12 Oct 2021 13:59:14 -0700 (PDT) From: Mathieu Poirier To: mst@redhat.com Subject: [PATCH v5 3/3] docs: Add documentation for vhost based RNG implementation Date: Tue, 12 Oct 2021 14:59:04 -0600 Message-Id: <20211012205904.4106769-4-mathieu.poirier@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211012205904.4106769-1-mathieu.poirier@linaro.org> References: <20211012205904.4106769-1-mathieu.poirier@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102c; envelope-from=mathieu.poirier@linaro.org; helo=mail-pj1-x102c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1634072743414100001 Content-Type: text/plain; charset="utf-8" Add description and example for the vhost-user based RNG implementation. Signed-off-by: Mathieu Poirier --- docs/system/device-emulation.rst | 1 + docs/system/devices/vhost-user-rng.rst | 39 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 docs/system/devices/vhost-user-rng.rst diff --git a/docs/system/device-emulation.rst b/docs/system/device-emulatio= n.rst index 7afcfd8064ae..19944f526cec 100644 --- a/docs/system/device-emulation.rst +++ b/docs/system/device-emulation.rst @@ -88,3 +88,4 @@ Emulated Devices devices/usb.rst devices/vhost-user.rst devices/virtio-pmem.rst + devices/vhost-user-rng.rst diff --git a/docs/system/devices/vhost-user-rng.rst b/docs/system/devices/v= host-user-rng.rst new file mode 100644 index 000000000000..a145d4105c1a --- /dev/null +++ b/docs/system/devices/vhost-user-rng.rst @@ -0,0 +1,39 @@ +QEMU vhost-user-rng - RNG emulation +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Background +---------- + +What follows builds on the material presented in vhost-user.rst - it should +be reviewed before moving forward with the content in this file. + +Description +----------- + +The vhost-user-rng device implementation was designed to work with a random +number generator daemon such as the one found in the vhost-device crate of +the rust-vmm project available on github [1]. + +[1]. https://github.com/rust-vmm/vhost-device + +Examples +-------- + +The daemon should be started first: + +:: + + host# vhost-device-rng --socket-path=3Drng.sock -c 1 -m 512 -p 1000 + +The QEMU invocation needs to create a chardev socket the device can +use to communicate as well as share the guests memory over a memfd. + +:: + + host# qemu-system \ + -chardev socket,path=3D$(PATH)/rng.sock,id=3Drng0 \ + -device vhost-user-rng-pci,chardev=3Drng0 \ + -m 4096 \ + -object memory-backend-file,id=3Dmem,size=3D4G,mem-path=3D/dev/shm,s= hare=3Don \ + -numa node,memdev=3Dmem \ + ... --=20 2.25.1