Skip to contentSkip to conversation

Add virtio block device sanboot support

PATCH In review Stefan Hajnoczi 2 participants 2 messages Jan 5, 2010 – Jan 6, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add virtio block device sanboot support

This patch adds virtio block device support alongside the existing iSCSI, ATA-over-Ethernet, and ramdisk block devices. The virtio block device provides storage in virtualized environments.

Using this patch, a gPXE option ROM can boot a QEMU/KVM virtual machine directly from a virtio block device.

Here is an example QEMU invocation:
qemu -drive if=virtio,file=debian.qcow2 -option-rom gpxe/src/bin/1af41001.rom

SANBOOT_PROTO_VIRTIO_BLK must be defined in config/general.h to enable this feature. The gPXE option ROM must be built for PCI ID 1af4:1001.

The sanboot gPXE command is used with the virtio_blk: root path scheme. The virtio block device instance is identified by its PCI bus, device, and function (there could be multiple virtio block devices).

gPXE> sanboot virtio_blk:PCI00:04.0

Perhaps the first available device should be chosen if virtio_blk: is given without PCI bus, device, and function. I am open to suggestions on how virtio block device option ROMs should work.

I have successfully booted Debian testing i386 and FreeDOS 0.84-pre2 under QEMU 0.11.0.

Note that QEMU/KVM can boot from a virtio block device using a separate non-virtio interface (-drive if=virtio,boot=on,[...]). This gPXE patch provides a native virtio block implementation.

While developing this feature, I noticed that the AoE sanboot code leaves a pointer to a stack value around after it returns. A commit to fix this is included.

Stefan Hajnoczi (2):
[sanboot] Prevent leaking a stack reference for "keep-san" AoE [virtio] Add virtio block device sanboot support

 src/arch/i386/interface/pcbios/aoeboot.c        |   54 +++--
 src/arch/i386/interface/pcbios/virtio_blkboot.c |   70 ++++++
 src/config/config.c                             |    3 +
 src/config/general.h                            |    1 +
 src/drivers/block/virtio-blk.c                  |  271 +++++++++++++++++++++++
 src/drivers/block/virtio-blk.h                  |   30 +++
 src/include/gpxe/errfile.h                      |    2 +
 src/include/gpxe/virtblk.h                      |   37 +++
 8 files changed, 447 insertions(+), 21 deletions(-)
 create mode 100644 src/arch/i386/interface/pcbios/virtio_blkboot.c
 create mode 100644 src/drivers/block/virtio-blk.c
 create mode 100644 src/drivers/block/virtio-blk.h
 create mode 100644 src/include/gpxe/virtblk.h
Quoted 1 line

From 0926649bece8e06dfdb19e4f819795da6ee364dd Mon Sep 17 00:00:00 2001

From: Stefan Hajnoczi <[email protected]>
Date: Tue, 5 Jan 2010 08:05:32 +0000
Subject: [PATCH 1/2] [sanboot] Prevent leaking a stack reference for "keep-san" AoE

When the "keep-san" option is used, the function is exited without unregistering the stack allocated int13h drive. To prevent a dangling pointer to the stack, these structs should be heap allocated.

---
 src/arch/i386/interface/pcbios/aoeboot.c |   54 ++++++++++++++++++-----------
 1 files changed, 33 insertions(+), 21 deletions(-)
(quoted diff) +0 -0

b/src/arch/i386/interface/pcbios/aoeboot.c
index 8446c15..2670b15 100644

src/arch/i386/interface/pcbios/aoeboot.c +33 -21

94 lines not on this page · raw message

Signature · 1 line
1.6.5
Quoted 1 line

From 3dbc385d29cbdff520ed0694934c01a6808ff1dc Mon Sep 17 00:00:00 2001

From: Stefan Hajnoczi <[email protected]>
Date: Tue, 5 Jan 2010 17:39:18 +0000
Subject: [PATCH 2/2] [virtio] Add virtio block device sanboot support

This patch adds virtio block device support alongside the existing iSCSI, ATA-over-Ethernet, and ramdisk block devices. A gPXE option ROM can boot a QEMU/KVM virtual machine directly from a virtio block device:

# Ensure SANBOOT_PROTO_VIRTIO_BLK is defined in config/defaults.h make bin/1af41001.rom

The sanboot gPXE command is used with the virtio_blk: root path scheme. The virtio block device instance is identified by its PCI bus, device, and function (there could be multiple virtio block devices).

    sanboot virtio_blk:PCI00:04.0

Successfully boots Debian testing i386 and FreeDOS 0.84-pre2 under QEMU.

---
 src/arch/i386/interface/pcbios/virtio_blkboot.c |   70 ++++++
 src/config/config.c                             |    3 +
 src/config/general.h                            |    1 +
 src/drivers/block/virtio-blk.c                  |  271 +++++++++++++++++++++++
 src/drivers/block/virtio-blk.h                  |   30 +++
 src/include/gpxe/errfile.h                      |    2 +
 src/include/gpxe/virtblk.h                      |   37 +++
 7 files changed, 414 insertions(+), 0 deletions(-)
 create mode 100644 src/arch/i386/interface/pcbios/virtio_blkboot.c
 create mode 100644 src/drivers/block/virtio-blk.c
 create mode 100644 src/drivers/block/virtio-blk.h
 create mode 100644 src/include/gpxe/virtblk.h
(quoted diff) +0 -0

b/src/arch/i386/interface/pcbios/virtio_blkboot.c
new file mode 100644
index 0000000..3c2dd47

src/arch/i386/interface/pcbios/virtio_blkboot.c new file +70 -0

70 lines not on this page · raw message

src/config/config.c +3 -0
index 3c43dfb..bb0ff67 100644
@@ -130,6 +130,9 @@ REQUIRE_OBJECT ( aoeboot );
 #ifdef SANBOOT_PROTO_IB_SRP
 REQUIRE_OBJECT ( ib_srpboot );
 #endif
+#ifdef SANBOOT_PROTO_VIRTIO_BLK
+REQUIRE_OBJECT ( virtio_blkboot );
+#endif
 
 /*
  * Drag in all requested resolvers
src/config/general.h +1 -0
index ee07dfc..a1dec39 100644
@@ -63,6 +63,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 //#undef	SANBOOT_PROTO_ISCSI	/* iSCSI protocol */
 //#undef	SANBOOT_PROTO_AOE	/* AoE protocol */
 //#undef	SANBOOT_PROTO_IB_SRP	/* Infiniband SCSI RDMA protocol */
+//#undef	SANBOOT_PROTO_VIRTIO_BLK /* Virtio block device */
 
 /*
  * Name resolution modules
src/drivers/block/virtio-blk.c new file +180 -0

180 lines not on this page · raw message

uint64_t block __unused,
+ unsigned long count __unused, userptr_t buffer __unused ) {
+ struct virtblk *virtblk =
+ container_of ( blockdev, struct virtblk, blockdev );
+ return virtblk_command ( virtblk, VIRTIO_BLK_T_OUT, block,
+ count, buffer );
+}
+
+static struct block_device_operations virtblk_operations = {
+ .read = virtblk_read,
+ .write = virtblk_write,
+};
+
+/**
+ * Probe PCI device
+ *
+ * @v pci PCI device
+ * @v id PCI ID
+ * @ret rc Return status code
+ */
+static int virtblk_probe ( struct pci_device *pci,
+ const struct pci_device_id *id __unused ) {
+ unsigned long ioaddr = pci->ioaddr;
+
+ /* Initialize driver state */
+ struct virtblk *virtblk = zalloc ( sizeof *virtblk );
+ if ( ! virtblk )
+ return -ENOMEM;
+ INIT_LIST_HEAD ( &virtblk->virtblk_devices );
+ virtblk->blockdev.op = &virtblk_operations;
+ virtblk->pdev = pci;
+ pci->priv = virtblk;
+
+ /* Prepare the device */
+ adjust_pci_device ( pci );
+ vp_reset ( ioaddr );
+
+ /* Indicate that the driver is starting */
+ vp_set_status ( ioaddr, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+ VIRTIO_CONFIG_S_DRIVER );
+
+ /* Grab the I/O requests virtqueue */
+ if ( vp_find_vq ( ioaddr, 0, &virtblk->virtqueue ) < 0 ) {
+ free ( virtblk );
+ return -ENOENT;
+ }
+
+ /* Feature negotiation */
+ u32 features = vp_get_features ( ioaddr );
+ vp_set_features ( ioaddr, 0 ); /* no features required */
+
+ /* Find out the drive capacity */
+ virtblk->blockdev.blksize = 512; /* the default */
+ vp_get ( ioaddr, 0, &virtblk->blockdev.blocks,
+ sizeof virtblk->blockdev.blocks );
+
+ /* Indicate that the driver is ready */
+ vp_set_status ( ioaddr, VIRTIO_CONFIG_S_ACKNOWLEDGE |
+ VIRTIO_CONFIG_S_DRIVER |
+ VIRTIO_CONFIG_S_DRIVER_OK );
+
+ DBGC ( virtblk, "VIRTBLK %p virtio_blk:%s ioaddr=0x%lx irq=%d
features=0x%x capacity=%lld\n",
+ virtblk, pci->dev.name, ioaddr, pci->irq,
+ features, virtblk->blockdev.blocks );
+
+ register_virtblk ( virtblk );
+ return 0;
+}
+
+/**
+ * Remove PCI device
+ *
+ * @v pci PCI device
+ */
+static void virtblk_remove ( struct pci_device *pci ) {
+ struct virtblk *virtblk = pci->priv;
+
+ unregister_virtblk ( virtblk );
+ vp_reset ( pci->ioaddr );
+ free ( virtblk );
+}
+
+static struct pci_device_id virtblk_ids[] = {
+ PCI_ROM(0x1af4, 0x1001, "virtio-blk", "Virtio Block Device", 0),
+};
+
+struct pci_driver virtblk_driver __pci_driver = {

+  .ids = virtblk_ids,
+  .id_count = ( sizeof ( virtblk_ids ) / sizeof ( virtblk_ids[0] ) ),
+  .probe = virtblk_probe,
+  .remove = virtblk_remove,

+};

src/drivers/block/virtio-blk.h new file +30 -0
new file mode 100644
index 0000000..3a34868
@@ -0,0 +1,30 @@
+#ifndef _VIRTIO_BLK_H
+#define _VIRTIO_BLK_H
+/* This header is BSD licensed so anyone can use the definitions to implement
+ * compatible drivers/servers. */
+
+FILE_LICENCE ( BSD2 );
+
+/*
+ * Command types
+ */
+
+/* These two define direction. */
+#define VIRTIO_BLK_T_IN		0
+#define VIRTIO_BLK_T_OUT	1
+
+/* This is the first element of the read scatter-gather list. */
+struct virtio_blk_outhdr {
+	/* VIRTIO_BLK_T* */
+	u32 type;
+	/* io priority. */
+	u32 ioprio;
+	/* Sector (ie. 512 byte offset) */
+	u64 sector;
+};
+
+/* And this is the final byte of the write scatter-gather list. */
+#define VIRTIO_BLK_S_OK		0
+#define VIRTIO_BLK_S_IOERR	1
+#define VIRTIO_BLK_S_UNSUPP	2
+#endif /* _VIRTIO_BLK_H */
src/include/gpxe/errfile.h +2 -0
index 5231c14..092d7eb 100644
@@ -122,6 +122,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #define ERRFILE_linda		     ( ERRFILE_DRIVER | 0x00730000 )
 #define ERRFILE_ata		     ( ERRFILE_DRIVER | 0x00740000 )
 #define ERRFILE_srp		     ( ERRFILE_DRIVER | 0x00750000 )
+#define ERRFILE_virtio_blk	     ( ERRFILE_DRIVER | 0x00760000 )
 
 #define ERRFILE_aoe			( ERRFILE_NET | 0x00000000 )
 #define ERRFILE_arp			( ERRFILE_NET | 0x00010000 )
@@ -191,6 +192,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #define ERRFILE_x509		      ( ERRFILE_OTHER | 0x00160000 )
 #define ERRFILE_login_ui	      ( ERRFILE_OTHER | 0x00170000 )
 #define ERRFILE_ib_srpboot	      ( ERRFILE_OTHER | 0x00180000 )
+#define ERRFILE_virtio_blkboot	      ( ERRFILE_OTHER | 0x00190000 )
 
 /** @} */
 
src/include/gpxe/virtblk.h new file +37 -0
new file mode 100644
index 0000000..781d433
@@ -0,0 +1,37 @@
+#ifndef _GPXE_VIRTBLK_H
+#define _GPXE_VIRTBLK_H
+
+/** @file
+ *
+ * Virtio block device
+ *
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+#include <gpxe/list.h>
+#include <gpxe/pci.h>
+#include <gpxe/virtio-ring.h>
+#include <gpxe/virtio-pci.h>
+#include <gpxe/blockdev.h>
+
+/**
+ * Virtio block device
+ */
+struct virtblk {
+	/** List of virtio-blk devices */
+	struct list_head virtblk_devices;
+
+	/** Underlying PCI device */
+	struct pci_device *pdev;
+
+	/** Outgoing requests virtqueue */
+	struct vring_virtqueue virtqueue;
+
+	/** Block device */
+	struct block_device blockdev;
+};
+
+extern struct virtblk *find_virtblk ( const char *device_name );
+
+#endif /* _GPXE_VIRTBLK_H */
Signature · 1 line
1.6.5

From 0926649bece8e06dfdb19e4f819795da6ee364dd Mon Sep 17 00:00:00 2001
From: Stefan Hajnoczi <[email protected]>
Date: Tue, 5 Jan 2010 08:05:32 +0000
Subject: [PATCH 1/2] [sanboot] Prevent leaking a stack reference for "keep-san" AoE

When the "keep-san" option is used, the function is exited without unregistering the stack allocated int13h drive. To prevent a dangling pointer to the stack, these structs should be heap allocated.

---
 src/arch/i386/interface/pcbios/aoeboot.c |   54 ++++++++++++++++++-----------
 1 files changed, 33 insertions(+), 21 deletions(-)
src/arch/i386/interface/pcbios/aoeboot.c +33 -21

94 lines not on this page · raw message

Signature · 35 lines
1.6.5


From 3dbc385d29cbdff520ed0694934c01a6808ff1dc Mon Sep 17 00:00:00 2001
From: Stefan Hajnoczi <[email protected]>
Date: Tue, 5 Jan 2010 17:39:18 +0000
Subject: [PATCH 2/2] [virtio] Add virtio block device sanboot support

This patch adds virtio block device support alongside the existing
iSCSI, ATA-over-Ethernet, and ramdisk block devices.  A gPXE option ROM
can boot a QEMU/KVM virtual machine directly from a virtio block device:

    # Ensure SANBOOT_PROTO_VIRTIO_BLK is defined in config/defaults.h
    make bin/1af41001.rom

The sanboot gPXE command is used with the virtio_blk: root path scheme.
The virtio block device instance is identified by its PCI bus, device,
and function (there could be multiple virtio block devices).

    sanboot virtio_blk:PCI00:04.0

Successfully boots Debian testing i386 and FreeDOS 0.84-pre2 under QEMU.
---
 src/arch/i386/interface/pcbios/virtio_blkboot.c |   70 ++++++
 src/config/config.c                             |    3 +
 src/config/general.h                            |    1 +
 src/drivers/block/virtio-blk.c                  |  271 +++++++++++++++++++++++
 src/drivers/block/virtio-blk.h                  |   30 +++
 src/include/gpxe/errfile.h                      |    2 +
 src/include/gpxe/virtblk.h                      |   37 +++
 7 files changed, 414 insertions(+), 0 deletions(-)
 create mode 100644 src/arch/i386/interface/pcbios/virtio_blkboot.c
 create mode 100644 src/drivers/block/virtio-blk.c
 create mode 100644 src/drivers/block/virtio-blk.h
 create mode 100644 src/include/gpxe/virtblk.h
src/arch/i386/interface/pcbios/virtio_blkboot.c new file +70 -0

70 lines not on this page · raw message

src/config/config.c +3 -0
index 3c43dfb..bb0ff67 100644
@@ -130,6 +130,9 @@ REQUIRE_OBJECT ( aoeboot );
 #ifdef SANBOOT_PROTO_IB_SRP
 REQUIRE_OBJECT ( ib_srpboot );
 #endif
+#ifdef SANBOOT_PROTO_VIRTIO_BLK
+REQUIRE_OBJECT ( virtio_blkboot );
+#endif
 
 /*
  * Drag in all requested resolvers
src/config/general.h +1 -0
index ee07dfc..a1dec39 100644
@@ -63,6 +63,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 //#undef	SANBOOT_PROTO_ISCSI	/* iSCSI protocol */
 //#undef	SANBOOT_PROTO_AOE	/* AoE protocol */
 //#undef	SANBOOT_PROTO_IB_SRP	/* Infiniband SCSI RDMA protocol */
+//#undef	SANBOOT_PROTO_VIRTIO_BLK /* Virtio block device */
 
 /*
  * Name resolution modules
src/drivers/block/virtio-blk.c new file +271 -0

271 lines not on this page · raw message

src/drivers/block/virtio-blk.h new file +30 -0
new file mode 100644
index 0000000..3a34868
@@ -0,0 +1,30 @@
+#ifndef _VIRTIO_BLK_H
+#define _VIRTIO_BLK_H
+/* This header is BSD licensed so anyone can use the definitions to implement
+ * compatible drivers/servers. */
+
+FILE_LICENCE ( BSD2 );
+
+/*
+ * Command types
+ */
+
+/* These two define direction. */
+#define VIRTIO_BLK_T_IN		0
+#define VIRTIO_BLK_T_OUT	1
+
+/* This is the first element of the read scatter-gather list. */
+struct virtio_blk_outhdr {
+	/* VIRTIO_BLK_T* */
+	u32 type;
+	/* io priority. */
+	u32 ioprio;
+	/* Sector (ie. 512 byte offset) */
+	u64 sector;
+};
+
+/* And this is the final byte of the write scatter-gather list. */
+#define VIRTIO_BLK_S_OK		0
+#define VIRTIO_BLK_S_IOERR	1
+#define VIRTIO_BLK_S_UNSUPP	2
+#endif /* _VIRTIO_BLK_H */
src/include/gpxe/errfile.h +2 -0
index 5231c14..092d7eb 100644
@@ -122,6 +122,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #define ERRFILE_linda		     ( ERRFILE_DRIVER | 0x00730000 )
 #define ERRFILE_ata		     ( ERRFILE_DRIVER | 0x00740000 )
 #define ERRFILE_srp		     ( ERRFILE_DRIVER | 0x00750000 )
+#define ERRFILE_virtio_blk	     ( ERRFILE_DRIVER | 0x00760000 )
 
 #define ERRFILE_aoe			( ERRFILE_NET | 0x00000000 )
 #define ERRFILE_arp			( ERRFILE_NET | 0x00010000 )
@@ -191,6 +192,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #define ERRFILE_x509		      ( ERRFILE_OTHER | 0x00160000 )
 #define ERRFILE_login_ui	      ( ERRFILE_OTHER | 0x00170000 )
 #define ERRFILE_ib_srpboot	      ( ERRFILE_OTHER | 0x00180000 )
+#define ERRFILE_virtio_blkboot	      ( ERRFILE_OTHER | 0x00190000 )
 
 /** @} */
 
src/include/gpxe/virtblk.h new file +37 -0
new file mode 100644
index 0000000..781d433
@@ -0,0 +1,37 @@
+#ifndef _GPXE_VIRTBLK_H
+#define _GPXE_VIRTBLK_H
+
+/** @file
+ *
+ * Virtio block device
+ *
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+#include <gpxe/list.h>
+#include <gpxe/pci.h>
+#include <gpxe/virtio-ring.h>
+#include <gpxe/virtio-pci.h>
+#include <gpxe/blockdev.h>
+
+/**
+ * Virtio block device
+ */
+struct virtblk {
+	/** List of virtio-blk devices */
+	struct list_head virtblk_devices;
+
+	/** Underlying PCI device */
+	struct pci_device *pdev;
+
+	/** Outgoing requests virtqueue */
+	struct vring_virtqueue virtqueue;
+
+	/** Block device */
+	struct block_device blockdev;
+};
+
+extern struct virtblk *find_virtblk ( const char *device_name );
+
+#endif /* _GPXE_VIRTBLK_H */
Signature · 1 line
1.6.5
#1 lore raw
Quoted 6 lines · Stefan Hajnoczi

This patch adds virtio block device support alongside the existing iSCSI, ATA-over-Ethernet, and ramdisk block devices. The virtio block device provides storage in virtualized environments.

Using this patch, a gPXE option ROM can boot a QEMU/KVM virtual machine directly from a virtio block device.

Great.

Quoted 6 lines

Here is an example QEMU invocation:
qemu -drive if=virtio,file=debian.qcow2 -option-rom gpxe/src/bin/1af41001.rom

Perhaps the first available device should be chosen if virtio_blk: is given without PCI bus, device, and function. I am open to suggestions on how virtio block device option ROMs should work.

Clear answer: as PCI ROM.

Try this (requires qemu 0.12):

qemu -drive if=none,id=boot,file=debian.qcow2 \
     -device virtio-blk-pci,drive=boot,romfile=gpxe/src/bin/1af41001.rom

You should end up with a virtio-blk device with a pci rom bar holding the option rom. seabios should load and run the rom. The disk should appear in the boot menu (enabled by '-boot menu=on').

cheers,
Gerd