Skip to contentSkip to conversation

kvm tools: Add initial virtio-scsi support

PATCH Applied Asias He 3 participants 5 messages Aug 17, 2012 – Aug 22, 2012 KVM

lore
Share & tools
lore

Asias He

author
#0 lore raw

PATCH v2 1/1 kvm tools: Add initial virtio-scsi support

This patch brings virito-scsi support to kvm tool.

With the introduce of tcm_vhost (vhost-scsi)

tcm_vhost: Initial merge for vhost level target fabric driver

we can implement virito-scsi by simply having vhost-scsi to handle the SCSI command.

Howto use:
1) Setup the tcm_vhost target through /sys/kernel/config

[Stefan Hajnoczi, Thanks for the script to setup tcm_vhost]

** Setup wwpn and tpgt

   $ wwpn="naa.0"
   $ tpgt=/sys/kernel/config/target/vhost/$wwpn/tpgt_0
   $ nexus=$tpgt/nexus
   $ mkdir -p $tpgt
   $ echo -n $wwpn > $nexus
   ** Setup lun using /dev/ram
   $ n=0
   $ lun=$tpgt/lun/lun_${n}
   $ data=/sys/kernel/config/target/core/iblock_0/data_${n}
   $ ram=/dev/ram${n}
   $ mkdir -p $lun
   $ mkdir -p $data
   $ echo -n udev_path=${ram} > $data/control
   $ echo -n 1 > $data/enable
   $ ln -s $data $lun

2) Run kvm tool with the new disk option '-d scsi:$wwpn:$tpgt', e.g

   $ lkvm run -k /boot/bzImage -d ~/img/sid.img -d scsi:naa.0:0
Cc Nicholas A. Bellinger <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 tools/kvm/Makefile                     |   1 +
 tools/kvm/builtin-run.c                |  25 +++
 tools/kvm/disk/core.c                  |  14 ++
 tools/kvm/include/kvm/disk-image.h     |   8 +
 tools/kvm/include/kvm/virtio-pci-dev.h |   1 +
 tools/kvm/include/kvm/virtio-scsi.h    |  26 +++
 tools/kvm/include/linux/compiler.h     |   1 +
 tools/kvm/virtio/blk.c                 |   2 +
 tools/kvm/virtio/scsi.c                | 315 +++++++++++++++++++++++++++++++++
 9 files changed, 393 insertions(+)
 create mode 100644 tools/kvm/include/kvm/virtio-scsi.h
 create mode 100644 tools/kvm/virtio/scsi.c
tools/kvm/Makefile +1 -0
index 924b191..2bf931b 100644
@@ -63,6 +63,7 @@ OBJS	+= mmio.o
 OBJS	+= pci.o
 OBJS	+= term.o
 OBJS	+= virtio/blk.o
+OBJS	+= virtio/scsi.o
 OBJS	+= virtio/console.o
 OBJS	+= virtio/core.o
 OBJS	+= virtio/net.o
tools/kvm/builtin-run.c +25 -0
index 40e147e..befce9e 100644
@@ -8,6 +8,7 @@
 #include "kvm/framebuffer.h"
 #include "kvm/disk-image.h"
 #include "kvm/threadpool.h"
+#include "kvm/virtio-scsi.h"
 #include "kvm/virtio-blk.h"
 #include "kvm/virtio-net.h"
 #include "kvm/virtio-rng.h"
@@ -171,6 +172,19 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset)
 
 	disk_image[image_count].filename = arg;
 	cur = arg;
+
+	if (strncmp(arg, "scsi:", 5) == 0) {
+		sep = strstr(arg, ":");
+		if (sep)
+			disk_image[image_count].wwpn = sep + 1;
+		sep = strstr(sep + 1, ":");
+		if (sep) {
+			*sep = 0;
+			disk_image[image_count].tpgt = sep + 1;
+		}
+		cur = sep + 1;
+	}
+
 	do {
 		sep = strstr(cur, ",");
 		if (sep) {
@@ -1185,6 +1199,13 @@ static int kvm_cmd_run_init(int argc, const char **argv)
 		goto fail;
 	}
 
+	r = virtio_scsi_init(kvm);
+	if (r < 0) {
+		pr_err("virtio_scsi_init() failed with error %d\n", r);
+		goto fail;
+	}
+
+
 	if (active_console == CONSOLE_VIRTIO)
 		virtio_console__init(kvm);
 
@@ -1333,6 +1354,10 @@ static void kvm_cmd_run_exit(int guest_ret)
 
 	fb__stop();
 
+	r = virtio_scsi_exit(kvm);
+	if (r < 0)
+		pr_warning("virtio_scsi_exit() failed with error %d\n", r);
+
 	r = virtio_blk__exit(kvm);
 	if (r < 0)
 		pr_warning("virtio_blk__exit() failed with error %d\n", r);
tools/kvm/disk/core.c +14 -0
index 621c940..f7e2c7f 100644
@@ -122,6 +122,8 @@ struct disk_image **disk_image__open_all(struct disk_image_params *params, int c
 {
 	struct disk_image **disks;
 	const char *filename;
+	const char *wwpn;
+	const char *tpgt;
 	bool readonly;
 	bool direct;
 	void *err;
@@ -140,6 +142,18 @@ struct disk_image **disk_image__open_all(struct disk_image_params *params, int c
 		filename = params[i].filename;
 		readonly = params[i].readonly;
 		direct = params[i].direct;
+		wwpn = params[i].wwpn;
+		tpgt = params[i].tpgt;
+
+		if (wwpn) {
+			disks[i] = malloc(sizeof(struct disk_image));
+			if (!disks[i])
+				return ERR_PTR(-ENOMEM);
+			disks[i]->wwpn = wwpn;
+			disks[i]->tpgt = tpgt;
+			continue;
+		}
+
 		if (!filename)
 			continue;
 
tools/kvm/include/kvm/disk-image.h +8 -0
index 7ae17f8..83fc725 100644
@@ -41,6 +41,12 @@ struct disk_image_operations {
 
 struct disk_image_params {
 	const char *filename;
+	/*
+	 * wwpn == World Wide Port Number
+	 * tpgt == Target Portal Group Tag
+	 */
+	const char *wwpn;
+	const char *tpgt;
 	bool readonly;
 	bool direct;
 };
@@ -57,6 +63,8 @@ struct disk_image {
 #ifdef CONFIG_HAS_AIO
 	io_context_t			ctx;
 #endif
+	const char			*wwpn;
+	const char			*tpgt;
 };
 
 struct disk_image *disk_image__open(const char *filename, bool readonly, bool direct);
tools/kvm/include/kvm/virtio-pci-dev.h +1 -0
index 7ceb125..48ae018 100644
@@ -13,6 +13,7 @@
 #define PCI_DEVICE_ID_VIRTIO_CONSOLE		0x1003
 #define PCI_DEVICE_ID_VIRTIO_RNG		0x1004
 #define PCI_DEVICE_ID_VIRTIO_BLN		0x1005
+#define PCI_DEVICE_ID_VIRTIO_SCSI		0x1008
 #define PCI_DEVICE_ID_VIRTIO_9P			0x1009
 #define PCI_DEVICE_ID_VESA			0x2000
 #define PCI_DEVICE_ID_PCI_SHMEM			0x0001
tools/kvm/include/kvm/virtio-scsi.h new file +26 -0
new file mode 100644
index 0000000..a780d7e
@@ -0,0 +1,26 @@
+#ifndef KVM__SCSI_VIRTIO_H
+#define KVM__SCSI_VIRTIO_H
+
+#include "kvm/disk-image.h"
+
+struct kvm;
+
+int virtio_scsi_init(struct kvm *kvm);
+int virtio_scsi_exit(struct kvm *kvm);
+
+/*----------------------------------------------------*/
+/* TODO: Remove this when tcm_vhost goes upstream */
+#define TRANSPORT_IQN_LEN		224
+#define VHOST_SCSI_ABI_VERSION		0
+struct vhost_scsi_target {
+	int abi_version;
+	unsigned char vhost_wwpn[TRANSPORT_IQN_LEN];
+	unsigned short vhost_tpgt;
+};
+/* VHOST_SCSI specific defines */
+#define VHOST_SCSI_SET_ENDPOINT _IOW(VHOST_VIRTIO, 0x40, struct vhost_scsi_target)
+#define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target)
+#define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, struct vhost_scsi_target)
+/*----------------------------------------------------*/
+
+#endif /* KVM__SCSI_VIRTIO_H */
tools/kvm/include/linux/compiler.h +1 -0
index b9c5346..898420b 100644
@@ -11,6 +11,7 @@
 #endif
 
 #define __used		__attribute__((__unused__))
+#define __packed	__attribute__((packed))
 #define __iomem
 #define __force
 #define __must_check
tools/kvm/virtio/blk.c +2 -0
index 1fb969f..740442a 100644
@@ -290,6 +290,8 @@ int virtio_blk__init(struct kvm *kvm)
 	int i, r = 0;
 
 	for (i = 0; i < kvm->nr_disks; i++) {
+		if (kvm->disks[i]->wwpn)
+			continue;
 		r = virtio_blk__init_one(kvm, kvm->disks[i]);
 		if (r < 0)
 			goto cleanup;
tools/kvm/virtio/scsi.c new file +315 -0

315 lines not on this page · raw message

Signature · 1 line
1.7.11.2
#1 lore raw
Quoted 32 lines · Asias He

This patch brings virito-scsi support to kvm tool.

With the introduce of tcm_vhost (vhost-scsi)

tcm_vhost: Initial merge for vhost level target fabric driver

we can implement virito-scsi by simply having vhost-scsi to handle the SCSI command.

Howto use:
1) Setup the tcm_vhost target through /sys/kernel/config

[Stefan Hajnoczi, Thanks for the script to setup tcm_vhost]

** Setup wwpn and tpgt

   $ wwpn="naa.0"
   $ tpgt=/sys/kernel/config/target/vhost/$wwpn/tpgt_0
   $ nexus=$tpgt/nexus
   $ mkdir -p $tpgt
   $ echo -n $wwpn > $nexus
   ** Setup lun using /dev/ram
   $ n=0
   $ lun=$tpgt/lun/lun_${n}
   $ data=/sys/kernel/config/target/core/iblock_0/data_${n}
   $ ram=/dev/ram${n}
   $ mkdir -p $lun
   $ mkdir -p $data
   $ echo -n udev_path=${ram} > $data/control
   $ echo -n 1 > $data/enable
   $ ln -s $data $lun
Quoted 7 lines

2) Run kvm tool with the new disk option '-d scsi:$wwpn:$tpgt', e.g

   $ lkvm run -k /boot/bzImage -d ~/img/sid.img -d scsi:naa.0:0
Cc Nicholas A. Bellinger <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Paolo Bonzini <[email protected]>

Looks good to me. I'll sit on this for a little while if the virtio-scsi folks want to comment on the code.

Pekka

Applied
#2 lore raw
Quoted 2 lines · Pekka Enberg

Looks good to me. I'll sit on this for a little while if the virtio-scsi folks want to comment on the code.

Applied, thanks Asias!

Reviewed-by on v2 1/1
#3 lore raw
In reply to Asias He
Quoted 8 lines · Asias He

This patch brings virito-scsi support to kvm tool.

With the introduce of tcm_vhost (vhost-scsi)

tcm_vhost: Initial merge for vhost level target fabric driver

we can implement virito-scsi by simply having vhost-scsi to handle the SCSI command.

Excited to see vhost-scsi support within kvm-tool btw. :)

Just one comment below wrt syncing up with v3.6-rc3 code..

Quoted 21 lines
tools/kvm/include/kvm/virtio-scsi.h new file +15 -0
new file mode 100644
index 0000000..a780d7e
@@ -0,0 +1,26 @@
+#ifndef KVM__SCSI_VIRTIO_H
+#define KVM__SCSI_VIRTIO_H
+
+#include "kvm/disk-image.h"
+
+struct kvm;
+
+int virtio_scsi_init(struct kvm *kvm);
+int virtio_scsi_exit(struct kvm *kvm);
+
+/*----------------------------------------------------*/
+/* TODO: Remove this when tcm_vhost goes upstream */
+#define TRANSPORT_IQN_LEN		224
+#define VHOST_SCSI_ABI_VERSION		0
+struct vhost_scsi_target {

tools/kvm/include/kvm/virtio-scsi.h line 25

Quoted 10 lines
tools/kvm/include/kvm/virtio-scsi.h new file +10 -0
+	int abi_version;
+	unsigned char vhost_wwpn[TRANSPORT_IQN_LEN];
+	unsigned short vhost_tpgt;
+};
+/* VHOST_SCSI specific defines */
+#define VHOST_SCSI_SET_ENDPOINT _IOW(VHOST_VIRTIO, 0x40, struct vhost_scsi_target)
+#define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target)
+#define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, struct vhost_scsi_target)
+/*----------------------------------------------------*/
+

Just a heads up that these have changed slightly based upon MST's feedback over the last weeks..

The vhost-scsi RFC-v3 for QEMU 1.3 + v3.6-rc3 PULL request for target rc-fixes have gone out today containing a slightly modified version of these definitions, so you'll want to go ahead and update to follow what's now in target-pending/master, which for vhost-scsi IOCTL ops are:

struct vhost_scsi_target {
    int abi_version;
    char vhost_wwpn[TRANSPORT_IQN_LEN];
    unsigned short vhost_tpgt;
    unsigned short reserved;
};

and update the GET_ABI_VERSION to return 'int':

#define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, int)

Aside from this minor bit, I think the patch looks good wrt to upstream tcm_vhost kernel code. Feel free to add my:

Nice work Asias!

author
#4 lore raw
Quoted 61 lines · Nicholas A. Bellinger
Quoted 8 lines · Asias He

This patch brings virito-scsi support to kvm tool.

With the introduce of tcm_vhost (vhost-scsi)

tcm_vhost: Initial merge for vhost level target fabric driver

we can implement virito-scsi by simply having vhost-scsi to handle the SCSI command.

Excited to see vhost-scsi support within kvm-tool btw. :)

Just one comment below wrt syncing up with v3.6-rc3 code..

Quoted 21 lines
tools/kvm/include/kvm/virtio-scsi.h new file +15 -0
new file mode 100644
index 0000000..a780d7e
@@ -0,0 +1,26 @@
+#ifndef KVM__SCSI_VIRTIO_H
+#define KVM__SCSI_VIRTIO_H
+
+#include "kvm/disk-image.h"
+
+struct kvm;
+
+int virtio_scsi_init(struct kvm *kvm);
+int virtio_scsi_exit(struct kvm *kvm);
+
+/*----------------------------------------------------*/
+/* TODO: Remove this when tcm_vhost goes upstream */
+#define TRANSPORT_IQN_LEN            224
+#define VHOST_SCSI_ABI_VERSION               0
+struct vhost_scsi_target {

tools/kvm/include/kvm/virtio-scsi.h line 25

Quoted 10 lines
tools/kvm/include/kvm/virtio-scsi.h new file +10 -0
+     int abi_version;
+     unsigned char vhost_wwpn[TRANSPORT_IQN_LEN];
+     unsigned short vhost_tpgt;
+};
+/* VHOST_SCSI specific defines */
+#define VHOST_SCSI_SET_ENDPOINT _IOW(VHOST_VIRTIO, 0x40, struct vhost_scsi_target)
+#define VHOST_SCSI_CLEAR_ENDPOINT _IOW(VHOST_VIRTIO, 0x41, struct vhost_scsi_target)
+#define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, struct vhost_scsi_target)
+/*----------------------------------------------------*/
+

Just a heads up that these have changed slightly based upon MST's feedback over the last weeks..

The vhost-scsi RFC-v3 for QEMU 1.3 + v3.6-rc3 PULL request for target rc-fixes have gone out today containing a slightly modified version of these definitions, so you'll want to go ahead and update to follow what's now in target-pending/master, which for vhost-scsi IOCTL ops are:

struct vhost_scsi_target {
    int abi_version;
    char vhost_wwpn[TRANSPORT_IQN_LEN];
    unsigned short vhost_tpgt;
    unsigned short reserved;
};
Quoted 3 lines

and update the GET_ABI_VERSION to return 'int':

#define VHOST_SCSI_GET_ABI_VERSION _IOW(VHOST_VIRTIO, 0x42, int)

OK. Will sync these bits to kvm-tool.

Quoted 6 lines

Aside from this minor bit, I think the patch looks good wrt to upstream tcm_vhost kernel code. Feel free to add my:

Nice work Asias!

Cheers, thanks for reviewing the kvm tool bits.

Signature · 1 line
Asias He