kvm tools: Add initial virtio-scsi support
PATCH 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 $lun2) 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
--- tools/kvm/Makefile | 1 + tools/kvm/builtin-run.c | 25 +++ tools/kvm/disk/core.c | 14 ++ tools/kvm/include/kvm/disk-image.h | 4 + tools/kvm/include/kvm/virtio-pci-dev.h | 1 + tools/kvm/include/kvm/virtio-scsi.h | 11 ++ tools/kvm/include/linux/compiler.h | 1 + tools/kvm/virtio/blk.c | 2 + tools/kvm/virtio/scsi.c | 332 +++++++++++++++++++++++++++++++++ 9 files changed, 391 insertions(+) create mode 100644 tools/kvm/include/kvm/virtio-scsi.h create mode 100644 tools/kvm/virtio/scsi.c
tools/kvm/Makefile +1 -0
@@ -55,6 +55,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
@@ -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) {
@@ -1184,6 +1198,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);
@@ -1332,6 +1353,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
@@ -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 +4 -0
@@ -41,6 +41,8 @@ struct disk_image_operations {struct disk_image_params {const char *filename; +const char *wwpn; +const char *tpgt;bool readonly;bool direct; };
@@ -57,6 +59,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
@@ -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 +11 -0
@@ -0,0 +1,11 @@ +#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); + +#endif /* KVM__SCSI_VIRTIO_H */
tools/kvm/include/linux/compiler.h +1 -0
@@ -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
@@ -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 +332 -0
332 lines not on this page · raw message
Signature · 1 line
1.7.11.2
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 driverwe can implement virito-scsi by simply having vhost-scsi to handle the SCSI command.
Howto use:
1) Setup thetcm_vhosttarget 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:0Signed-off-by Asias He <[email protected]>Cc Nicholas A. Bellinger <[email protected]>Cc Stefan Hajnoczi <[email protected]>Cc Paolo Bonzini <[email protected]>
I've included some comments below but overall, looks good to me. Sasha?
tools/kvm/include/kvm/disk-image.h struct disk_image_operations { · line 45
Quoted 11 lines
---
tools/kvm/include/kvm/disk-image.h +2
-0@@ -41,6 +41,8 @@ struct disk_image_operations {struct disk_image_params {const char *filename; +const char *wwpn; +const char *tpgt;
Maybe it's just me but "wwpn" and "tpgt" really could use a comment explaining what they are...
Quoted 49 lines
tools/kvm/include/kvm/disk-image.h +2
-0bool readonly;bool direct; };@@ -57,6 +59,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/virtio/blk.c +2
-0@@ -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 +18
-0@@ -0,0 +1,332 @@ +#include "kvm/virtio-scsi.h" +#include "kvm/virtio-pci-dev.h" +#include "kvm/disk-image.h" +#include "kvm/kvm.h" +#include "kvm/pci.h" +#include "kvm/ioeventfd.h" +#include "kvm/guest_compat.h" +#include "kvm/virtio-pci.h" +#include "kvm/virtio.h" + +#include <linux/kernel.h> +#include <linux/virtio_scsi.h> +#include <linux/vhost.h> + +/*----------------------------------------------------*/ +/* TODO: Remove this when tcm_vhost goes upstream */ +#define TRANSPORT_IQN_LEN 224 +#define VHOST_SCSI_ABI_VERSION 0
tools/kvm/virtio/scsi.c line 28
Quoted 10 lines
tools/kvm/virtio/scsi.c new file +10
-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) +/*----------------------------------------------------*/
You might as well move the above block into a small helper header file.
Quoted 14 lines
tools/kvm/virtio/scsi.c new file +14
-0+ + +#define VIRTIO_SCSI_QUEUE_SIZE 128 +#define NUM_VIRT_QUEUES 3 + +static LIST_HEAD (sdevs); +static int compat_id =-1 ; + +struct scsi_dev { +struct virt_queue vqs[NUM_VIRT_QUEUES ]; +struct virtio_scsi_config scsi_config; +struct vhost_scsi_target target; +u32 features; +int vhost_fd;
tools/kvm/virtio/scsi.c line 52
Quoted 10 lines
tools/kvm/virtio/scsi.c new file +10
-0+struct virtio_device vdev; +struct list_head list; +struct kvm *kvm; +}; + +static void set_config (struct kvm *kvm,void *dev,u8 data,u32 offset) +{ +struct scsi_dev *sdev = dev; + + ((u8 *)(&sdev->scsi_config))[offset] = data;
Can you introduce a helper function for this, please?
tools/kvm/virtio/scsi.c line 59
Quoted 7 lines
tools/kvm/virtio/scsi.c new file +7
-0+} + +static u8 get_config (struct kvm *kvm,void *dev,u32 offset) +{ +struct scsi_dev *sdev = dev; + +return ((u8 *)(&sdev->scsi_config))[offset];
Ditto.
Quoted 2 lines
tools/kvm/virtio/scsi.c new file +2
-0+} +
Quoted 50 lines · Pekka Enberg
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 driverwe can implement virito-scsi by simply having vhost-scsi to handle the SCSI command.
Howto use:
1) Setup thetcm_vhosttarget 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 $lunQuoted 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:0Signed-off-by Asias He <[email protected]>Cc Nicholas A. Bellinger <[email protected]>Cc Stefan Hajnoczi <[email protected]>Cc Paolo Bonzini <[email protected]>I've included some comments below but overall, looks good to me. Sasha?
Quoted 7 lines
---
tools/kvm/include/kvm/disk-image.h +0
-0@@ -41,6 +41,8 @@ struct disk_image_operations {
Quoted 7 lines
tools/kvm/include/kvm/disk-image.h struct disk_image_operations { · line 45
Quoted 4 lines
tools/kvm/include/kvm/disk-image.h +2
-0struct disk_image_params {const char *filename; +const char *wwpn; +const char *tpgt;Maybe it's just me but "wwpn" and "tpgt" really could use a comment explaining what they are...
OK. They are terminology from linux target.
wwpn == world wide port number
tpgt == target port group tag
Nicholas, is this right?
Quoted 53 lines
Quoted 53 lines
tools/kvm/include/kvm/disk-image.h +2
-0bool readonly;bool direct; };@@ -57,6 +59,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/virtio/blk.c +2
-0@@ -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 +22
-0@@ -0,0 +1,332 @@ +#include "kvm/virtio-scsi.h" +#include "kvm/virtio-pci-dev.h" +#include "kvm/disk-image.h" +#include "kvm/kvm.h" +#include "kvm/pci.h" +#include "kvm/ioeventfd.h" +#include "kvm/guest_compat.h" +#include "kvm/virtio-pci.h" +#include "kvm/virtio.h" + +#include <linux/kernel.h> +#include <linux/virtio_scsi.h> +#include <linux/vhost.h> + +/*----------------------------------------------------*/ +/* 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;
Quoted 8 lines
tools/kvm/virtio/scsi.c line 28
Quoted 6 lines
tools/kvm/virtio/scsi.c new file +6
-0+}; +/* 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) +/*----------------------------------------------------*/ You might as well move the above block into a small helper header file.
How about include/kvm/virtio-scsi.h ?
Quoted 18 lines
Quoted 18 lines
tools/kvm/virtio/scsi.c new file +18
-0+ + +#define VIRTIO_SCSI_QUEUE_SIZE 128 +#define NUM_VIRT_QUEUES 3 + +static LIST_HEAD (sdevs); +static int compat_id =-1 ; + +struct scsi_dev { +struct virt_queue vqs[NUM_VIRT_QUEUES ]; +struct virtio_scsi_config scsi_config; +struct vhost_scsi_target target; +u32 features; +int vhost_fd; +struct virtio_device vdev; +struct list_head list; +struct kvm *kvm; +};
Quoted 8 lines
tools/kvm/virtio/scsi.c line 52
Quoted 6 lines
tools/kvm/virtio/scsi.c new file +6
-0+ +static void set_config (struct kvm *kvm,void *dev,u8 data,u32 offset) +{ +struct scsi_dev *sdev = dev; + + ((u8 *)(&sdev->scsi_config))[offset] = data;Can you introduce a helper function for this, please?
We have this similar code in other devices' get_config, get_config as well. Let's clean them up after this patch? Also, I have plans to clean up the vhost code.
Quoted 17 lines
tools/kvm/virtio/scsi.c line 59
Quoted 8 lines
tools/kvm/virtio/scsi.c new file +7
-0+} + +static u8 get_config (struct kvm *kvm,void *dev,u32 offset) +{ +struct scsi_dev *sdev = dev; + +return ((u8 *)(&sdev->scsi_config))[offset];Ditto.
tools/kvm/virtio/scsi.c line 61
Quoted 2 lines
tools/kvm/virtio/scsi.c new file +2
-0+} +--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 1 line
Asias He
Quoted 59 lines · Asias He
Quoted 50 lines · Pekka Enberg
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 driverwe can implement virito-scsi by simply having vhost-scsi to handle the SCSI command.
Howto use:
1) Setup thetcm_vhosttarget 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 $lunQuoted 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:0Signed-off-by Asias He <[email protected]>Cc Nicholas A. Bellinger <[email protected]>Cc Stefan Hajnoczi <[email protected]>Cc Paolo Bonzini <[email protected]>I've included some comments below but overall, looks good to me. Sasha?
Quoted 7 lines
---
tools/kvm/include/kvm/disk-image.h +0
-0@@ -41,6 +41,8 @@ struct disk_image_operations {Quoted 7 lines
tools/kvm/include/kvm/disk-image.h struct disk_image_operations { · line 45
Quoted 4 lines
tools/kvm/include/kvm/disk-image.h +2
-0struct disk_image_params {const char *filename; +const char *wwpn; +const char *tpgt;Maybe it's just me but "wwpn" and "tpgt" really could use a comment explaining what they are...
Quoted 4 lines
OK. They are terminology from linux target.
wwpn == world wide port number
tpgt == target port group tag
Target Portal Group Tag
TPGT is iSCSI-specific terminology but other transports like SAS or FCP have similar concepts of naming target ports. I think this terminology is used by the LIO target regardless of the fabric.
The SCSI Architecture Model standard says the target port name is the "name (see 3.1.70) of a SCSI target port that is world wide unique within the SCSI transport protocol of the SCSI domain of that SCSI target port".
Quoted 3 lines · Stefan Hajnoczi
Quoted 2 lines · Asias He
Quoted 1 line · Pekka Enberg
On Thu, Aug 9, 2012 at 3:51 AM, Asias He <[email protected]> wrote:
<SNIP>
Quoted 22 lines
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
---
tools/kvm/include/kvm/disk-image.h +0
-0@@ -41,6 +41,8 @@ struct disk_image_operations {Quoted 7 lines
tools/kvm/include/kvm/disk-image.h struct disk_image_operations { · line 45
Quoted 4 lines
tools/kvm/include/kvm/disk-image.h +2
-0struct disk_image_params {const char *filename; +const char *wwpn; +const char *tpgt;Maybe it's just me but "wwpn" and "tpgt" really could use a comment explaining what they are...
Quoted 4 lines
OK. They are terminology from linux target.
wwpn == world wide port number
tpgt == target port group tagTarget Portal Group Tag
Quoted 9 lines
TPGT is iSCSI-specific terminology but other transports like SAS or FCP have similar concepts of naming target ports. I think this terminology is used by the LIO target regardless of the fabric.
The SCSI Architecture Model standard says the target port name is the "name (see 3.1.70) of a SCSI target port that is world wide unique within the SCSI transport protocol of the SCSI domain of that SCSI target port".
The idea of a TargetPortalGroupTag originally came from iSCSI to do logical partitioning of a single TargetName (IQN or EUI) instance with different LUN/auth/network portal configurations. For iSCSI fabrics, the SCSI target WWPN takes the form of a symbolic string as:
$TARGETNAME+t+0x$TPGT
In generic target_core_fabric_configfs.c code, all SCSI target endpoints are attached to se_portal_group->tpg_group residing individually at /sys/kernel/config/target/$FABRIC/$FABRIC_WWN/$TPGT/. It's parent configfs group is at se_wwn->wwn_group, who's higher level parent is the top level fabric group registered by the fabric driver with TCM.
So the $FABRIC_WWN/$TPGT/[luns,acls] groups hang off this primary config_group, which means that currently non iSCSI target based fabric drivers are required to individually enforce TPGT=1 usage in their own code when mapping SCSI target WWPNs to underlying storage HW WWPNs, or a fabric using software based WWPNs (as iSCSI does).
Also, from the user level of rtsadmin/targetcli we do hide the notion of TPGTs for all non iscsi fabrics, however using rtslib directly does still require following the underlying $FABRIC/$FABRIC_WWN/$TPGT/ object layout.
--nab