Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add viornd(4), a driver for the VirtIO entropy source availa...



details:   https://anonhg.NetBSD.org/src/rev/8bee2346eb6e
branches:  trunk
changeset: 803417:8bee2346eb6e
user:      tls <tls%NetBSD.org@localhost>
date:      Sun Oct 26 18:43:18 2014 +0000

description:
Add viornd(4), a driver for the VirtIO entropy source available on
QEMU, KVM, and Google Compute Engine.  From OpenBSD.

diffstat:

 doc/CHANGES                 |    6 +-
 share/man/man4/viornd.4     |   61 ++++++++++
 sys/arch/amd64/conf/GENERIC |    5 +-
 sys/dev/pci/files.pci       |    6 +-
 sys/dev/pci/viornd.c        |  249 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 322 insertions(+), 5 deletions(-)

diffs (truncated from 382 to 300 lines):

diff -r 1b434cb2491f -r 8bee2346eb6e doc/CHANGES
--- a/doc/CHANGES       Sun Oct 26 18:33:43 2014 +0000
+++ b/doc/CHANGES       Sun Oct 26 18:43:18 2014 +0000
@@ -1,4 +1,4 @@
-# LIST OF CHANGES FROM LAST RELEASE:                   <$Revision: 1.2004 $>
+# LIST OF CHANGES FROM LAST RELEASE:                   <$Revision: 1.2005 $>
 #
 #
 # [Note: This file does not mention every change made to the NetBSD source tree.
@@ -87,5 +87,7 @@
                sources (be more power friendly). [tls 20141026]
        rnd(9): Make "skew" source polled so it runs only when there
                is entropy demand. [tls 20141026]
-       rnd(9): Adjust entropy collection from polled sources so it's
+       rnd(9): Adjust entropy collection from polled sources so it's
                processed sooner. [tls 20141026]
+       viornd(4):      Add driver for VirtIO entropy source available on
+                       QEMU, KVM, and Google Compute Engine.  From OpenBSD.
diff -r 1b434cb2491f -r 8bee2346eb6e share/man/man4/viornd.4
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/share/man/man4/viornd.4   Sun Oct 26 18:43:18 2014 +0000
@@ -0,0 +1,61 @@
+.\" $NetBSD: viornd.4,v 1.1 2014/10/26 18:43:18 tls Exp $
+.\"
+.\" Copyright (c) 2014 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Thor Lancelot Simon.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd October 26, 2014
+.Dt VIORND 4
+.Os
+.Sh NAME
+.Nm viornd
+.Nd VirtIO entropy source
+.Sh SYNOPSIS
+.Cd "viornd* at virtio?"
+.Sh DESCRIPTION
+When the system has entropy demand,
+the
+.Nm
+driver, used with a compatible hypervisor such as QEMU, KVM, or Google
+Compute Engine, requests entropy using the VirtIO random number interface
+and feeds it into the system entropy pool.
+.Sh SEE ALSO
+.Xr virtio 4 ,
+.Xr rnd 4
+.Sh HISTORY
+The
+.Nm
+driver appeared in
+.Ox 5.5 .
+.Sh AUTHORS
+The
+.Nm
+driver was written by Stephan Fritsch <sf%fritsch.de@localhost> and reworked to
+request entropy upon demand by Thor Lancelot Simon <tls%NetBSD.ORG@localhost>.
+.Sh CAVEATS
+VirtIO appears to support at least 8 pending entropy requests, but
+.Nm
+currently supports only one pending request at a time.
diff -r 1b434cb2491f -r 8bee2346eb6e sys/arch/amd64/conf/GENERIC
--- a/sys/arch/amd64/conf/GENERIC       Sun Oct 26 18:33:43 2014 +0000
+++ b/sys/arch/amd64/conf/GENERIC       Sun Oct 26 18:43:18 2014 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.400 2014/10/18 16:56:51 uebayasi Exp $
+# $NetBSD: GENERIC,v 1.401 2014/10/26 18:43:18 tls Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@
 
 options        INCLUDE_CONFIG_FILE     # embed config file in kernel binary
 
-#ident                 "GENERIC-$Revision: 1.400 $"
+#ident                 "GENERIC-$Revision: 1.401 $"
 
 maxusers       64              # estimated number of users
 
@@ -1171,6 +1171,7 @@
 viomb* at virtio?                      # Virtio memory balloon device
 ld*    at virtio?                      # Virtio disk device
 vioif* at virtio?                      # Virtio network device
+viornd*        at virtio?                      # Virtio entropy device
 
 
 # Pull in optional local configuration
diff -r 1b434cb2491f -r 8bee2346eb6e sys/dev/pci/files.pci
--- a/sys/dev/pci/files.pci     Sun Oct 26 18:33:43 2014 +0000
+++ b/sys/dev/pci/files.pci     Sun Oct 26 18:43:18 2014 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.pci,v 1.371 2014/03/19 15:26:41 nonaka Exp $
+#      $NetBSD: files.pci,v 1.372 2014/10/26 18:43:18 tls Exp $
 #
 # Config file and device description for machine-independent PCI code.
 # Included by ports that need it.  Requires that the SCSI files be
@@ -1125,6 +1125,10 @@
 attach vioif at virtio
 file   dev/pci/if_vioif.c      vioif
 
+device viornd
+attach viornd at virtio
+file dev/pci/viornd.c          viornd
+
 # Silicon Motion SM712(LynxEM+) frame buffer
 device lynxfb: wsemuldisplaydev, rasops16
 attach lynxfb at pci
diff -r 1b434cb2491f -r 8bee2346eb6e sys/dev/pci/viornd.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/pci/viornd.c      Sun Oct 26 18:43:18 2014 +0000
@@ -0,0 +1,249 @@
+/*     $NetBSD: viornd.c,v 1.1 2014/10/26 18:43:18 tls Exp $ */
+/*     $OpenBSD: viornd.c,v 1.1 2014/01/21 21:14:58 sf Exp $   */
+
+/*
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Thor Lancelot Simon (tls%NetBSD.org@localhost).
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 2014 Stefan Fritsch <sf%sfritsch.de@localhost>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/device.h>
+#include <sys/callout.h>
+#include <sys/rnd.h>
+#include <sys/mutex.h>
+#include <dev/pci/pcidevs.h>
+#include <dev/pci/pcivar.h>
+#include <dev/pci/pcivar.h>
+#include <dev/pci/virtioreg.h>
+#include <dev/pci/virtiovar.h>
+
+#define        VIORND_BUFSIZE                  32
+
+#define VIORND_DEBUG 1
+
+struct viornd_softc {
+       device_t                sc_dev;
+       struct virtio_softc     *sc_virtio;
+
+       kmutex_t                sc_mutex;
+       bool                    sc_active;
+
+       void                    *sc_buf;
+       struct virtqueue        sc_vq;
+       bus_dmamap_t            sc_dmamap;
+       krndsource_t            sc_rndsource;
+};
+
+int    viornd_match(device_t, cfdata_t, void *);
+void   viornd_attach(device_t, device_t, void *);
+int    viornd_vq_done(struct virtqueue *);
+
+CFATTACH_DECL_NEW(viornd, sizeof(struct viornd_softc),
+                 viornd_match, viornd_attach, NULL, NULL);
+
+static void
+viornd_get(size_t bytes, void *priv)
+{
+        struct viornd_softc *sc = priv;
+        struct virtio_softc *vsc = sc->sc_virtio;
+        struct virtqueue *vq = &sc->sc_vq;
+        int slot;
+
+       aprint_normal("%s: asked for %d bytes of entropy\n", __func__,
+                     VIORND_BUFSIZE);
+       mutex_enter(&sc->sc_mutex);
+
+       if (sc->sc_active) {
+               goto out;
+       }
+
+        bus_dmamap_sync(vsc->sc_dmat, sc->sc_dmamap, 0, VIORND_BUFSIZE,
+            BUS_DMASYNC_PREREAD);
+       if (virtio_enqueue_prep(vsc, vq, &slot)) {
+               virtio_enqueue_abort(vsc, vq, slot);
+               goto out;
+       }
+        if (virtio_enqueue_reserve(vsc, vq, slot, 1)) {
+               goto out;
+       }
+        virtio_enqueue(vsc, vq, slot, sc->sc_dmamap, 0);
+        virtio_enqueue_commit(vsc, vq, slot, 1);
+       sc->sc_active = true;
+out:
+       mutex_exit(&sc->sc_mutex);
+}
+
+int
+viornd_match(device_t parent, cfdata_t match, void *aux)
+{
+       struct virtio_softc *va = aux;
+       if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_ENTROPY)
+               return 1;
+       return 0;
+}
+
+void
+viornd_attach( device_t parent, device_t self, void *aux)
+{
+       struct viornd_softc *sc = device_private(self);
+       struct virtio_softc *vsc = device_private(parent);
+       bus_dma_segment_t segs[1];
+       int nsegs;
+       int error;
+
+       vsc->sc_vqs = &sc->sc_vq;
+       vsc->sc_nvqs = 1;
+       vsc->sc_config_change = 0;
+       if (vsc->sc_child != NULL)
+               panic("already attached to something else");
+       vsc->sc_child = self;
+       vsc->sc_ipl = IPL_NET;
+       vsc->sc_intrhand = virtio_vq_intr;
+       sc->sc_virtio = vsc;
+       sc->sc_dev = self;
+
+       (void)virtio_negotiate_features(vsc, 0);
+
+       mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
+
+       error = bus_dmamem_alloc(vsc->sc_dmat, 
+                                VIRTIO_PAGE_SIZE, 0, 0, segs, 1, &nsegs,
+                                BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW);
+       if (error) {
+               aprint_error_dev(sc->sc_dev, "can't alloc dmamem: %d\n",
+                                error);
+               goto alloc_failed;
+       }
+
+       error = bus_dmamem_map(vsc->sc_dmat, segs, nsegs, VIORND_BUFSIZE,
+                              &sc->sc_buf, BUS_DMA_NOWAIT);
+       if (error) {
+               aprint_error_dev(sc->sc_dev, "can't map dmamem: %d\n", error);
+               goto map_failed;
+       }
+
+       error = bus_dmamap_create(vsc->sc_dmat, VIORND_BUFSIZE, 1,



Home | Main Index | Thread Index | Old Index