Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 21 Jul 2014 00:21:57 +0000 (UTC)
From:      John Baldwin <jhb@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r268933 - in stable/10: share/man/man4 sys/amd64/conf sys/conf sys/dev/virtio/random sys/i386/conf sys/modules/virtio sys/modules/virtio/random sys/sys usr.sbin/bhyve
Message-ID:  <201407210021.s6L0Lv1W072248@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jhb
Date: Mon Jul 21 00:21:56 2014
New Revision: 268933
URL: http://svnweb.freebsd.org/changeset/base/268933

Log:
  MFC 260847,264055,264867:
  - Add a very simple virtio_random(4) driver for FreeBSD guests to harvest
    entropy from hypervisors.
  - Add support to bhyve for the virtio RNG entropy-source device to provide
    entry to bhyve guests.

Added:
  stable/10/share/man/man4/virtio_random.4
     - copied unchanged from r260847, head/share/man/man4/virtio_random.4
  stable/10/sys/dev/virtio/random/
     - copied from r260847, head/sys/dev/virtio/random/
  stable/10/sys/modules/virtio/random/
     - copied from r260847, head/sys/modules/virtio/random/
  stable/10/usr.sbin/bhyve/pci_virtio_rnd.c
     - copied unchanged from r264055, head/usr.sbin/bhyve/pci_virtio_rnd.c
Modified:
  stable/10/share/man/man4/Makefile
  stable/10/sys/amd64/conf/NOTES
  stable/10/sys/conf/files.amd64
  stable/10/sys/conf/files.i386
  stable/10/sys/dev/virtio/random/virtio_random.c
  stable/10/sys/i386/conf/NOTES
  stable/10/sys/modules/virtio/Makefile
  stable/10/sys/sys/random.h
  stable/10/usr.sbin/bhyve/Makefile
  stable/10/usr.sbin/bhyve/bhyve.8
  stable/10/usr.sbin/bhyve/virtio.h
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/share/man/man4/Makefile
==============================================================================
--- stable/10/share/man/man4/Makefile	Sun Jul 20 22:54:03 2014	(r268932)
+++ stable/10/share/man/man4/Makefile	Mon Jul 21 00:21:56 2014	(r268933)
@@ -555,6 +555,7 @@ MAN=	aac.4 \
 	${_virtio.4} \
 	${_virtio_balloon.4} \
 	${_virtio_blk.4} \
+	${_virtio_random.4} \
 	${_virtio_scsi.4} \
 	vkbd.4 \
 	vlan.4 \
@@ -804,6 +805,7 @@ _nxge.4=	nxge.4
 _virtio.4=	virtio.4
 _virtio_balloon.4=virtio_balloon.4
 _virtio_blk.4=	virtio_blk.4
+_virtio_random.4= virtio_random.4
 _virtio_scsi.4= virtio_scsi.4
 _vmx.4=		vmx.4
 _vtnet.4=	vtnet.4

Copied: stable/10/share/man/man4/virtio_random.4 (from r260847, head/share/man/man4/virtio_random.4)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/10/share/man/man4/virtio_random.4	Mon Jul 21 00:21:56 2014	(r268933, copy of r260847, head/share/man/man4/virtio_random.4)
@@ -0,0 +1,61 @@
+.\" Copyright (c) 2013 Bryan Venteicher
+.\" All rights reserved.
+.\"
+.\" 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 AUTHOR 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 AUTHOR 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.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd December 28, 2013
+.Dt VIRTIO_RANDOM 4
+.Os
+.Sh NAME
+.Nm virtio_random
+.Nd VirtIO Entropy driver
+.Sh SYNOPSIS
+To compile this driver into the kernel,
+place the following lines in your
+kernel configuration file:
+.Bd -ragged -offset indent
+.Cd "device virtio_random"
+.Ed
+.Pp
+Alternatively, to load the driver as a
+module at boot time, place the following line in
+.Xr loader.conf 5 :
+.Bd -literal -offset indent
+virtio_random_load="YES"
+.Ed
+.Sh DESCRIPTION
+The
+.Nm
+device driver provides support for VirtIO entropy devices.
+.Pp
+The entropy device supplies high-quality randomness from the
+hypervisor to the guest.
+.Sh SEE ALSO
+.Xr random 4
+.Xr virtio 4
+.Sh HISTORY
+The
+.Nm
+driver was written by
+.An Bryan Venteicher Aq bryanv@FreeBSD.org .

Modified: stable/10/sys/amd64/conf/NOTES
==============================================================================
--- stable/10/sys/amd64/conf/NOTES	Sun Jul 20 22:54:03 2014	(r268932)
+++ stable/10/sys/amd64/conf/NOTES	Mon Jul 21 00:21:56 2014	(r268933)
@@ -472,6 +472,7 @@ device		vtnet		# VirtIO Ethernet device
 device		virtio_blk	# VirtIO Block device
 device		virtio_scsi	# VirtIO SCSI device
 device		virtio_balloon	# VirtIO Memory Balloon device
+device		virtio_random	# VirtIO Entropy device
 
 device 		hyperv		# HyperV drivers
 

Modified: stable/10/sys/conf/files.amd64
==============================================================================
--- stable/10/sys/conf/files.amd64	Sun Jul 20 22:54:03 2014	(r268932)
+++ stable/10/sys/conf/files.amd64	Mon Jul 21 00:21:56 2014	(r268933)
@@ -448,6 +448,7 @@ dev/virtio/network/if_vtnet.c		optional	
 dev/virtio/block/virtio_blk.c		optional	virtio_blk
 dev/virtio/balloon/virtio_balloon.c	optional	virtio_balloon
 dev/virtio/scsi/virtio_scsi.c		optional	virtio_scsi
+dev/virtio/random/virtio_random.c	optional	virtio_random
 isa/syscons_isa.c		optional	sc
 isa/vga_isa.c			optional	vga
 kern/imgact_binmisc.c		optional	imagact_binmisc

Modified: stable/10/sys/conf/files.i386
==============================================================================
--- stable/10/sys/conf/files.i386	Sun Jul 20 22:54:03 2014	(r268932)
+++ stable/10/sys/conf/files.i386	Mon Jul 21 00:21:56 2014	(r268933)
@@ -413,6 +413,7 @@ dev/virtio/network/if_vtnet.c		optional	
 dev/virtio/block/virtio_blk.c		optional	virtio_blk
 dev/virtio/balloon/virtio_balloon.c	optional	virtio_balloon
 dev/virtio/scsi/virtio_scsi.c		optional	virtio_scsi
+dev/virtio/random/virtio_random.c	optional	virtio_random
 i386/acpica/acpi_machdep.c	optional acpi
 acpi_wakecode.o			optional acpi				\
 	dependency	"$S/i386/acpica/acpi_wakecode.S assym.s"	\

Modified: stable/10/sys/dev/virtio/random/virtio_random.c
==============================================================================
--- head/sys/dev/virtio/random/virtio_random.c	Sat Jan 18 06:14:38 2014	(r260847)
+++ stable/10/sys/dev/virtio/random/virtio_random.c	Mon Jul 21 00:21:56 2014	(r268933)
@@ -156,7 +156,7 @@ vtrnd_detach(device_t dev)
 
 	sc = device_get_softc(dev);
 
-	callout_stop(&sc->vtrnd_callout);
+	callout_drain(&sc->vtrnd_callout);
 
 	return (0);
 }

Modified: stable/10/sys/i386/conf/NOTES
==============================================================================
--- stable/10/sys/i386/conf/NOTES	Sun Jul 20 22:54:03 2014	(r268932)
+++ stable/10/sys/i386/conf/NOTES	Mon Jul 21 00:21:56 2014	(r268933)
@@ -800,6 +800,7 @@ device		vtnet		# VirtIO Ethernet device
 device		virtio_blk	# VirtIO Block device
 device		virtio_scsi	# VirtIO SCSI device
 device		virtio_balloon	# VirtIO Memory Balloon device
+device		virtio_random	# VirtIO Entropy device
 
 device 		hyperv		# HyperV drivers
 

Modified: stable/10/sys/modules/virtio/Makefile
==============================================================================
--- stable/10/sys/modules/virtio/Makefile	Sun Jul 20 22:54:03 2014	(r268932)
+++ stable/10/sys/modules/virtio/Makefile	Mon Jul 21 00:21:56 2014	(r268933)
@@ -23,6 +23,6 @@
 # SUCH DAMAGE.
 #
 
-SUBDIR=	virtio pci network block balloon scsi
+SUBDIR=	virtio pci network block balloon scsi random
 
 .include <bsd.subdir.mk>

Modified: stable/10/sys/sys/random.h
==============================================================================
--- stable/10/sys/sys/random.h	Sun Jul 20 22:54:03 2014	(r268932)
+++ stable/10/sys/sys/random.h	Mon Jul 21 00:21:56 2014	(r268933)
@@ -56,6 +56,7 @@ enum esource {
 	RANDOM_PURE_RDRAND,
 	RANDOM_PURE_NEHEMIAH,
 	RANDOM_PURE_RNDTEST,
+	RANDOM_PURE_VIRTIO,
 	ENTROPYSOURCE
 };
 void random_harvest(void *, u_int, u_int, enum esource);

Modified: stable/10/usr.sbin/bhyve/Makefile
==============================================================================
--- stable/10/usr.sbin/bhyve/Makefile	Sun Jul 20 22:54:03 2014	(r268932)
+++ stable/10/usr.sbin/bhyve/Makefile	Mon Jul 21 00:21:56 2014	(r268933)
@@ -26,6 +26,7 @@ SRCS=	\
 	pci_passthru.c		\
 	pci_virtio_block.c	\
 	pci_virtio_net.c	\
+	pci_virtio_rnd.c	\
 	pci_uart.c		\
 	pm.c			\
 	pmtmr.c			\

Modified: stable/10/usr.sbin/bhyve/bhyve.8
==============================================================================
--- stable/10/usr.sbin/bhyve/bhyve.8	Sun Jul 20 22:54:03 2014	(r268932)
+++ stable/10/usr.sbin/bhyve/bhyve.8	Mon Jul 21 00:21:56 2014	(r268933)
@@ -24,7 +24,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd January 27, 2014
+.Dd April 2, 2014
 .Dt BHYVE 8
 .Os
 .Sh NAME
@@ -126,6 +126,8 @@ PCI pass-through device.
 Virtio network interface.
 .It Li virtio-blk
 Virtio block storage interface.
+.It Li virtio-rnd
+Virtio RNG interface.
 .It Li ahci-cd
 AHCI controller attached to an ATAPI CD/DVD.
 .It Li ahci-hd

Copied: stable/10/usr.sbin/bhyve/pci_virtio_rnd.c (from r264055, head/usr.sbin/bhyve/pci_virtio_rnd.c)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/10/usr.sbin/bhyve/pci_virtio_rnd.c	Mon Jul 21 00:21:56 2014	(r268933, copy of r264055, head/usr.sbin/bhyve/pci_virtio_rnd.c)
@@ -0,0 +1,189 @@
+/*-
+ * Copyright (c) 2014 Nahanni Systems Inc.
+ * All rights reserved.
+ *
+ * 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
+ *    in this position and unchanged.
+ * 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 AUTHOR 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 AUTHOR 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.
+ */
+
+/*
+ * virtio entropy device emulation.
+ * Randomness is sourced from /dev/random which does not block
+ * once it has been seeded at bootup.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/linker_set.h>
+#include <sys/uio.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <assert.h>
+#include <pthread.h>
+
+#include "bhyverun.h"
+#include "pci_emul.h"
+#include "virtio.h"
+
+#define VTRND_RINGSZ	64
+
+
+static int pci_vtrnd_debug;
+#define DPRINTF(params) if (pci_vtrnd_debug) printf params
+#define WPRINTF(params) printf params
+
+/*
+ * Per-device softc
+ */
+struct pci_vtrnd_softc {
+	struct virtio_softc vrsc_vs;
+	struct vqueue_info  vrsc_vq;
+	pthread_mutex_t     vrsc_mtx;
+	uint64_t            vrsc_cfg;
+	int                 vrsc_fd;
+};
+
+static void pci_vtrnd_reset(void *);
+static void pci_vtrnd_notify(void *, struct vqueue_info *);
+
+static struct virtio_consts vtrnd_vi_consts = {
+	"vtrnd",		/* our name */
+	1,			/* we support 1 virtqueue */
+	0,			/* config reg size */
+	pci_vtrnd_reset,	/* reset */
+	pci_vtrnd_notify,	/* device-wide qnotify */
+	NULL,			/* read virtio config */
+	NULL,			/* write virtio config */
+	0,			/* our capabilities */
+};
+
+
+static void
+pci_vtrnd_reset(void *vsc)
+{
+	struct pci_vtrnd_softc *sc;
+
+	sc = vsc;
+
+	DPRINTF(("vtrnd: device reset requested !\n"));
+	vi_reset_dev(&sc->vrsc_vs);
+}
+
+
+static void
+pci_vtrnd_notify(void *vsc, struct vqueue_info *vq)
+{
+	struct iovec iov;
+	struct pci_vtrnd_softc *sc;
+	int len;
+
+	sc = vsc;
+
+	vq_startchains(vq);
+
+	if (sc->vrsc_fd < 0) {
+		vq_endchains(vq, 0);
+		return;
+	}
+
+	while (vq_has_descs(vq)) {
+		vq_getchain(vq, &iov, 1, NULL);
+
+		len = read(sc->vrsc_fd, iov.iov_base, iov.iov_len);
+
+		DPRINTF(("vtrnd: vtrnd_notify(): %d\r\n", len));
+
+		/* Catastrophe if unable to read from /dev/random */
+		assert(len > 0);
+
+		/*
+		 * Release this chain and handle more
+		 */
+		vq_relchain(vq, len);
+	}
+	vq_endchains(vq, 1);	/* Generate interrupt if appropriate. */
+}
+
+
+static int
+pci_vtrnd_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
+{
+	struct pci_vtrnd_softc *sc;
+	int fd;
+	int len;
+	uint8_t v;
+
+	/*
+	 * Should always be able to open /dev/random.
+	 */
+	fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
+
+	assert(fd >= 0);
+
+	/*
+	 * Check that device is seeded and non-blocking.
+	 */
+	len = read(fd, &v, sizeof(v));
+	if (len <= 0) {
+		WPRINTF(("vtrnd: /dev/random not ready, read(): %d", len));
+		return (1);
+	}
+
+	sc = malloc(sizeof(struct pci_vtrnd_softc));
+	memset(sc, 0, sizeof(struct pci_vtrnd_softc));
+
+	vi_softc_linkup(&sc->vrsc_vs, &vtrnd_vi_consts, sc, pi, &sc->vrsc_vq);
+	sc->vrsc_vs.vs_mtx = &sc->vrsc_mtx;
+
+	sc->vrsc_vq.vq_qsize = VTRND_RINGSZ;
+
+	/* keep /dev/random opened while emulating */
+	sc->vrsc_fd = fd;
+
+	/* initialize config space */
+	pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_RANDOM);
+	pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
+	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_CRYPTO);
+	pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_ENTROPY);
+
+	if (vi_intr_init(&sc->vrsc_vs, 1, fbsdrun_virtio_msix()))
+		return (1);
+	vi_set_io_bar(&sc->vrsc_vs, 0);
+
+	return (0);
+}
+
+
+struct pci_devemu pci_de_vrnd = {
+	.pe_emu =	"virtio-rnd",
+	.pe_init =	pci_vtrnd_init,
+	.pe_barwrite =	vi_pci_write,
+	.pe_barread =	vi_pci_read
+};
+PCI_EMUL_SET(pci_de_vrnd);

Modified: stable/10/usr.sbin/bhyve/virtio.h
==============================================================================
--- stable/10/usr.sbin/bhyve/virtio.h	Sun Jul 20 22:54:03 2014	(r268932)
+++ stable/10/usr.sbin/bhyve/virtio.h	Mon Jul 21 00:21:56 2014	(r268933)
@@ -209,6 +209,7 @@ struct vring_used {
 #define	VIRTIO_VENDOR		0x1AF4
 #define	VIRTIO_DEV_NET		0x1000
 #define	VIRTIO_DEV_BLOCK	0x1001
+#define	VIRTIO_DEV_RANDOM	0x1002
 
 /*
  * PCI config space constants.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201407210021.s6L0Lv1W072248>