scsi: persistent reservation live migration
cover scsi: persistent reservation live migration
Live migration does not work for SCSI Persistent Reservations acquired on scsi-block devices. This patch series migrates the reservation key and reservation type so that the destination QEMU can take over the persistent reservation with the PREEMPT service action upon live migration.
The approach involves snooping PERSISTENT RESERVE OUT replies and tracking the scsi-block device's current reservation key and reservation type. In most cases this involves no additional SCSI commands. This approach isn't perfect: if another machine modifies the reservation on the physical LUN, then QEMU's state becomes stale. Persistent reservations are inherently cooperative, so this is acceptable as long as real applications don't run into problems.
I am also working on a test suite called pr-tests that runs sg_persist(8) commands across multiple machines in order to exercise various scenarios:
https://gitlab.com/stefanha/pr-tests
Stefan Hajnoczi (4):
scsi: track SCSI reservation state for live migration
scsi: generalize scsi_SG_IO_FROM_DEV() to scsi_SG_IO()
scsi: add error reporting to scsi_SG_IO()
scsi: save/load SCSI reservation state
include/hw/scsi/scsi.h | 15 +- include/scsi/constants.h | 21 +++ hw/core/machine.c | 4 +- hw/scsi/scsi-bus.c | 3 + hw/scsi/scsi-disk.c | 53 +++++++- hw/scsi/scsi-generic.c | 286 +++++++++++++++++++++++++++++++++++++-- hw/scsi/trace-events | 2 + 7 files changed, 369 insertions(+), 15 deletions(-)
Signature · 1 line
2.52.0
RFC 1/4 scsi: track SCSI reservation state for live migration
SCSI Persistent Reservations are stateful and external to the guest. In order to transparently move reservations to the destination host during live migration, it is necessary to track the state built up on the source host before migration. Only then can the destination host ensure an equivalent state is restored upon migration.
Snoop on successful PERSISTENT RESERVE OUT commands and save the reservation key and reservation type. This will allow registered keys and reservations to be migrated.
Also patch PERSISTENT RESERVE IN replies with the REPORT CAPABILITIES service action since features that involve the physical SCSI bus target ports must not be exposed to the guest (it sees a virtual SCSI bus).
Usually this plays out as follows:
1. The guest invokes the REGISTER service action to register a reservation key on its I_T nexus.
2. The guest invokes the RESERVE service action to create a reservation using the previously-registered key.
This commit implements the snooping and stores the reservation key and type (if any) for each LUN. The snooped PR state and the migrate_pr flag to enable PR migration will be used in later commits.
--- include/hw/scsi/scsi.h | 10 +++ include/scsi/constants.h | 21 +++++ hw/scsi/scsi-bus.c | 3 + hw/scsi/scsi-generic.c | 160 +++++++++++++++++++++++++++++++++++++++ hw/scsi/trace-events | 1 + 5 files changed, 195 insertions(+)
include/hw/scsi/scsi.h +10 -0
@@ -57,6 +57,13 @@ struct SCSIRequest {QTAILQ_ENTRY (SCSIRequest) next; }; +/* Per-SCSIDevice Persistent Reservation state */ +typedef struct { +QemuMutex mutex;/* protects all fields (e.g. from multiple IOThreads) */ +uint64_t key;/* 0 if no registered key */ +uint8_t resv_type;/* 0 if no reservation */ +}SCSIPRState ; +#define TYPE_SCSI_DEVICE "scsi-device"OBJECT_DECLARE_TYPE (SCSIDevice, SCSIDeviceClass,SCSI_DEVICE )
@@ -97,6 +104,9 @@ struct SCSIDeviceuint32_t io_timeout;bool needs_vpd_bl_emulation;bool hba_supports_iothread; + +bool migrate_pr; +SCSIPRState pr_state; };extern const VMStateDescription vmstate_scsi_device;
include/scsi/constants.h +21 -0
@@ -319,4 +319,25 @@#define IDENT_DESCR_TGT_DESCR_SIZE 32#define XCOPY_BLK2BLK_SEG_DESC_SIZE 28 +/* +* PERSISTENT RESERVATION IN service action codes +*/ +#define PRI_READ_KEYS 0x00 +#define PRI_READ_RESERVATION 0x01 +#define PRI_REPORT_CAPABILITIES 0x02 +#define PRI_READ_FULL_STATUS 0x03 + +/* +* PERSISTENT RESERVATION OUT service action codes +*/ +#define PRO_REGISTER 0x00 +#define PRO_RESERVE 0x01 +#define PRO_RELEASE 0x02 +#define PRO_CLEAR 0x03 +#define PRO_PREEMPT 0x04 +#define PRO_PREEMPT_AND_ABORT 0x05 +#define PRO_REGISTER_AND_IGNORE_EXISTING_KEY 0x06 +#define PRO_REGISTER_AND_MOVE 0x07 +#define PRO_REPLACE_LOST_RESERVATION 0x08 +#endif
hw/scsi/scsi-bus.c +3 -0
@@ -393,6 +393,7 @@ static void scsi_qdev_realize(DeviceState *qdev, Error **errp) }qemu_mutex_init (&dev->requests_lock); +qemu_mutex_init (&dev->pr_state.mutex);QTAILQ_INIT (&dev->requests);scsi_device_realize (dev, &local_err);if (local_err) {
@@ -417,6 +418,8 @@ static void scsi_qdev_unrealize(DeviceState *qdev)scsi_device_unrealize (dev); +qemu_mutex_destroy (&dev->pr_state.mutex); +blockdev_mark_auto_del (dev->conf.blk); }
hw/scsi/scsi-generic.c +160 -0
178 lines not on this page · raw message
hw/scsi/trace-events +1 -0
@@ -390,3 +390,4 @@ scsi_generic_realize_blocksize(int blocksize) "block size %d" scsi_generic_aio_sgio_command(uint32_t tag, uint8_t cmd, uint32_t timeout) "generic aio sgio: tag=0x%x cmd=0x%x timeout=%u" scsi_generic_ioctl_sgio_command(uint8_t cmd, uint32_t timeout) "generic ioctl sgio: cmd=0x%x timeout=%u" scsi_generic_ioctl_sgio_done(uint8_t cmd, int ret, uint8_t status, uint8_t host_status) "generic ioctl sgio: cmd=0x%x ret=%d status=0x%x host_status=0x%x" +scsi_generic_persistent_reserve_out_reply(uint8_t service_action, uint8_t resv_type, uint64_t old_key, uint64_t new_key) "persistent reserve out reply service_action=%u resv_type=%u old_key=0x%" PRIx64 " new_key=0x%" PRIx64
Signature · 1 line
2.52.0
RFC 2/4 scsi: generalize scsi_SG_IO_FROM_DEV() to scsi_SG_IO()
In reply to Stefan HajnocziAdd a direction argument so that scsi_SG_IO() can be used for SG_DXFER_FROM_DEV and SG_DXFER_TO_DEV transfers.
--- include/hw/scsi/scsi.h | 4 ++-- hw/scsi/scsi-disk.c | 4 ++-- hw/scsi/scsi-generic.c | 18 ++++++++++-------- 3 files changed, 14 insertions(+), 12 deletions(-)
include/hw/scsi/scsi.h +2 -2
@@ -246,8 +246,8 @@ void scsi_device_report_change(SCSIDevice *dev, SCSISense sense);void scsi_device_unit_attention_reported (SCSIDevice *dev);void scsi_generic_read_device_inquiry (SCSIDevice *dev);int scsi_device_get_sense (SCSIDevice *dev,uint8_t *buf,int len,bool fixed); -int scsi_SG_IO_FROM_DEV (BlockBackend *blk,uint8_t *cmd,uint8_t cmd_size, -uint8_t *buf,uint8_t buf_size,uint32_t timeout); +int scsi_SG_IO (BlockBackend *blk,int direction,uint8_t *cmd,uint8_t cmd_size, +uint8_t *buf,uint8_t buf_size,uint32_t timeout);SCSIDevice *scsi_device_find (SCSIBus *bus,int channel,int target,int lun);SCSIDevice *scsi_device_get (SCSIBus *bus,int channel,int target,int lun);
hw/scsi/scsi-disk.c +2 -2
@@ -2748,8 +2748,8 @@ static int get_device_type(SCSIDiskState *s) cmd[0 ] =INQUIRY ; cmd[4 ] =sizeof (buf); - ret =scsi_SG_IO_FROM_DEV (s->qdev.conf.blk, cmd,sizeof (cmd), - buf,sizeof (buf), s->qdev.io_timeout); + ret =scsi_SG_IO (s->qdev.conf.blk,SG_DXFER_FROM_DEV , cmd,sizeof (cmd), + buf,sizeof (buf), s->qdev.io_timeout);if (ret <0 ) {return -1 ; }
hw/scsi/scsi-generic.c +10 -8
@@ -684,8 +684,9 @@ static int read_naa_id(const uint8_t *p, uint64_t *p_wwn)return -EINVAL ; } -int scsi_SG_IO_FROM_DEV (BlockBackend *blk,uint8_t *cmd,uint8_t cmd_size, -uint8_t *buf,uint8_t buf_size,uint32_t timeout) +int scsi_SG_IO (BlockBackend *blk,int direction,uint8_t *cmd, +uint8_t cmd_size,uint8_t *buf,uint8_t buf_size, +uint32_t timeout) {sg_io_hdr_t io_header;uint8_t sensebuf[8 ];
@@ -693,7 +694,7 @@ int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size,memset (&io_header,0 ,sizeof (io_header)); io_header.interface_id ='S' ; - io_header.dxfer_direction =SG_DXFER_FROM_DEV ; + io_header.dxfer_direction = direction; io_header.dxfer_len = buf_size; io_header.dxferp = buf; io_header.cmdp = cmd;
@@ -733,8 +734,8 @@ static void scsi_generic_set_vpd_bl_emulation(SCSIDevice *s) cmd[2 ] =0x00 ; cmd[4 ] =sizeof (buf); - ret =scsi_SG_IO_FROM_DEV (s->conf.blk, cmd,sizeof (cmd), - buf,sizeof (buf), s->io_timeout); + ret =scsi_SG_IO (s->conf.blk,SG_DXFER_FROM_DEV , cmd,sizeof (cmd), + buf,sizeof (buf), s->io_timeout);if (ret <0 ) {/* * Do not assume anything if we can't retrieve the
@@ -769,8 +770,8 @@ static void scsi_generic_read_device_identification(SCSIDevice *s) cmd[2 ] =0x83 ; cmd[4 ] =sizeof (buf); - ret =scsi_SG_IO_FROM_DEV (s->conf.blk, cmd,sizeof (cmd), - buf,sizeof (buf), s->io_timeout); + ret =scsi_SG_IO (s->conf.blk,SG_DXFER_FROM_DEV , cmd,sizeof (cmd), + buf,sizeof (buf), s->io_timeout);if (ret <0 ) {return ; }
@@ -821,7 +822,8 @@ static int get_stream_blocksize(BlockBackend *blk) cmd[0 ] =MODE_SENSE ; cmd[4 ] =sizeof (buf); - ret =scsi_SG_IO_FROM_DEV (blk, cmd,sizeof (cmd), buf,sizeof (buf),6 ); + ret =scsi_SG_IO (blk,SG_DXFER_FROM_DEV , cmd,sizeof (cmd), + buf,sizeof (buf),6 );if (ret <0 ) {return -1 ; }
Signature · 1 line
2.52.0
RFC 3/4 scsi: add error reporting to scsi_SG_IO()
In reply to Stefan HajnocziReport the details of the SG_IO ioctl failure if an Error pointer is provided. This information aids troubleshooting and will be used by the SCSI Persistent Reservations migration code.
--- include/hw/scsi/scsi.h | 2 +- hw/scsi/scsi-disk.c | 2 +- hw/scsi/scsi-generic.c | 33 ++++++++++++++++++++++++++++----- 3 files changed, 30 insertions(+), 7 deletions(-)
include/hw/scsi/scsi.h +1 -1
@@ -247,7 +247,7 @@ void scsi_device_unit_attention_reported(SCSIDevice *dev);void scsi_generic_read_device_inquiry (SCSIDevice *dev);int scsi_device_get_sense (SCSIDevice *dev,uint8_t *buf,int len,bool fixed);int scsi_SG_IO (BlockBackend *blk,int direction,uint8_t *cmd,uint8_t cmd_size, -uint8_t *buf,uint8_t buf_size,uint32_t timeout); +uint8_t *buf,uint8_t buf_size,uint32_t timeout,Error **errp);SCSIDevice *scsi_device_find (SCSIBus *bus,int channel,int target,int lun);SCSIDevice *scsi_device_get (SCSIBus *bus,int channel,int target,int lun);
hw/scsi/scsi-disk.c +1 -1
@@ -2749,7 +2749,7 @@ static int get_device_type(SCSIDiskState *s) cmd[4 ] =sizeof (buf); ret =scsi_SG_IO (s->qdev.conf.blk,SG_DXFER_FROM_DEV , cmd,sizeof (cmd), - buf,sizeof (buf), s->qdev.io_timeout); + buf,sizeof (buf), s->qdev.io_timeout,NULL );if (ret <0 ) {return -1 ; }
hw/scsi/scsi-generic.c +28 -5
@@ -686,10 +686,10 @@ static int read_naa_id(const uint8_t *p, uint64_t *p_wwn)int scsi_SG_IO (BlockBackend *blk,int direction,uint8_t *cmd,uint8_t cmd_size,uint8_t *buf,uint8_t buf_size, -uint32_t timeout) +uint32_t timeout,Error **errp) {sg_io_hdr_t io_header; -uint8_t sensebuf[8 ]; +uint8_t sensebuf[8 ] = {};int ret;memset (&io_header,0 ,sizeof (io_header));
@@ -709,6 +709,29 @@ int scsi_SG_IO(BlockBackend *blk, int direction, uint8_t *cmd, io_header.driver_status || io_header.host_status) {trace_scsi_generic_ioctl_sgio_done (cmd[0 ], ret, io_header.status, io_header.host_status); +if (ret <0 ) { +error_setg_errno (errp, -ret,"SG_IO ioctl failed" ); + }else { + g_autofree char *sensebuf_hex = +g_strdup_printf ("%02x%02x%02x%02x%02x%02x%02x%02x" , + sensebuf[0 ], + sensebuf[1 ], + sensebuf[2 ], + sensebuf[3 ], + sensebuf[4 ], + sensebuf[5 ], + sensebuf[6 ], + sensebuf[7 ]); + +error_setg (errp,"SG_IO SCSI command failed with status=0x%x " +"driver_status=0x%x host_status=0x%x sensebuf=%s " +"sb_len_wr=%u" , + io_header.status, + io_header.driver_status, + io_header.host_status, + sensebuf_hex, + io_header.sb_len_wr); + }return -1 ; }return 0 ;
@@ -735,7 +758,7 @@ static void scsi_generic_set_vpd_bl_emulation(SCSIDevice *s) cmd[4 ] =sizeof (buf); ret =scsi_SG_IO (s->conf.blk,SG_DXFER_FROM_DEV , cmd,sizeof (cmd), - buf,sizeof (buf), s->io_timeout); + buf,sizeof (buf), s->io_timeout,NULL );if (ret <0 ) {/* * Do not assume anything if we can't retrieve the
@@ -771,7 +794,7 @@ static void scsi_generic_read_device_identification(SCSIDevice *s) cmd[4 ] =sizeof (buf); ret =scsi_SG_IO (s->conf.blk,SG_DXFER_FROM_DEV , cmd,sizeof (cmd), - buf,sizeof (buf), s->io_timeout); + buf,sizeof (buf), s->io_timeout,NULL );if (ret <0 ) {return ; }
@@ -823,7 +846,7 @@ static int get_stream_blocksize(BlockBackend *blk) cmd[4 ] =sizeof (buf); ret =scsi_SG_IO (blk,SG_DXFER_FROM_DEV , cmd,sizeof (cmd), - buf,sizeof (buf),6 ); + buf,sizeof (buf),6 ,NULL );if (ret <0 ) {return -1 ; }
Signature · 1 line
2.52.0
RFC 4/4 scsi: save/load SCSI reservation state
In reply to Stefan HajnocziAdd a vmstate subsection to SCSIDiskState so that scsi-block devices can transfer their reservation state during live migration. Upon loading the subsection, the destination QEMU invokes the PERSISTENT RESERVE OUT command's PREEMPT service action to atomically move the reservation from the source I_T nexus to the destination I_T nexus. This results in transparent live migration of SCSI reservations.
This approach is incomplete since SCSI reservations are cooperative and other hosts could interfere. Neither the source QEMU nor the destination QEMU are aware of changes made by other hosts. The assumption is that reservation is not taken over by a third host without cooperation from the source host.
I considered adding the vmstate subsection to SCSIDevice instead of SCSIDiskState, since reservations are part of the SCSI Primary Commands that other devices apart from disks could support. However, due to fragility of migrating reservations, we will probably limit support to scsi-block and maybe scsi-disk in the future. In the end, I think it makes sense to place this within scsi-disk.c.
--- include/hw/scsi/scsi.h | 1 + hw/core/machine.c | 4 +- hw/scsi/scsi-disk.c | 49 ++++++++++++++++++++++++- hw/scsi/scsi-generic.c | 83 ++++++++++++++++++++++++++++++++++++++++++ hw/scsi/trace-events | 1 + 5 files changed, 136 insertions(+), 2 deletions(-)
include/hw/scsi/scsi.h +1 -0
@@ -253,6 +253,7 @@ SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int target, int lun);/* scsi-generic.c. */ extern const SCSIReqOps scsi_generic_req_ops; +bool scsi_generic_pr_state_post_load_errp (SCSIDevice *s,Error **errp);/* scsi-disk.c */ #define SCSI_DISK_QUIRK_MODE_PAGE_APPLE_VENDOR 0
hw/core/machine.c +3 -1
@@ -38,7 +38,9 @@#include "hw/acpi/generic_event_device.h" #include "qemu/audio.h" -GlobalProperty hw_compat_10_2[] = {}; +GlobalProperty hw_compat_10_2[] = { + {"scsi-block" ,"migrate-pr" ,"off" }, +};const size_t hw_compat_10_2_len =G_N_ELEMENTS (hw_compat_10_2);GlobalProperty hw_compat_10_1[] = {
hw/scsi/scsi-disk.c +48 -1
67 lines not on this page · raw message
hw/scsi/scsi-generic.c +83 -0
89 lines not on this page · raw message
hw/scsi/trace-events +1 -0
@@ -391,3 +391,4 @@ scsi_generic_aio_sgio_command(uint32_t tag, uint8_t cmd, uint32_t timeout) "gene scsi_generic_ioctl_sgio_command(uint8_t cmd, uint32_t timeout) "generic ioctl sgio: cmd=0x%x timeout=%u" scsi_generic_ioctl_sgio_done(uint8_t cmd, int ret, uint8_t status, uint8_t host_status) "generic ioctl sgio: cmd=0x%x ret=%d status=0x%x host_status=0x%x" scsi_generic_persistent_reserve_out_reply(uint8_t service_action, uint8_t resv_type, uint64_t old_key, uint64_t new_key) "persistent reserve out reply service_action=%u resv_type=%u old_key=0x%" PRIx64 " new_key=0x%" PRIx64 +scsi_generic_pr_state_post_load_errp(uint64_t key, uint8_t resv_type) "key=0x%" PRIx64 " resv_type=%u"
Signature · 1 line
2.52.0
Quoted 12 lines · Stefan Hajnoczi
Live migration does not work for SCSI Persistent Reservations acquired on scsi-block devices. This patch series migrates the reservation key and reservation type so that the destination QEMU can take over the persistent reservation with the PREEMPT service action upon live migration.
The approach involves snooping PERSISTENT RESERVE OUT replies and tracking the scsi-block device's current reservation key and reservation type. In most cases this involves no additional SCSI commands. This approach isn't perfect: if another machine modifies the reservation on the physical LUN, then QEMU's state becomes stale. Persistent reservations are inherently cooperative, so this is acceptable as long as real applications don't run into problems.
Quoted 3 lines
I am also working on a test suite called pr-tests that runs
sg_persist(8)commands across multiple machines in order to exercise various scenarios:
https://gitlab.com/stefanha/pr-tests
I've also prepared libvirt RFC patches adding support for the feature:
https://lists.libvirt.org/archives/list/[email protected]/thread/KBZDAIQWFILAC4USJY3C3TDPYHI6K5WK/
Quoted 11 lines · Stefan Hajnoczi
Live migration does not work for SCSI Persistent Reservations acquired on scsi-block devices. This patch series migrates the reservation key and reservation type so that the destination QEMU can take over the persistent reservation with the PREEMPT service action upon live migration.
The approach involves snooping PERSISTENT RESERVE OUT replies and tracking the scsi-block device's current reservation key and reservation type. In most cases this involves no additional SCSI commands. This approach isn't perfect: if another machine modifies the reservation on the physical LUN, then QEMU's state becomes stale. Persistent reservations are inherently cooperative, so this is acceptable as long as real applications don't run into problems.
One issue is that this would not transfer reservations done from a previous invocation of the VM. Are you assuming that the restarted VM won't assume to still have the reservation? I think this is fine, but it has to be documented, or maybe QEMU could issue a PR IN command at startup?
Quoted 3 lines
I am also working on a test suite called pr-tests that runs
sg_persist(8)commands across multiple machines in order to exercise various scenarios:
https://gitlab.com/stefanha/pr-tests
Thank you so much for that!
Paolo
Quoted 13 lines · Paolo Bonzini
Quoted 11 lines · Stefan Hajnoczi
Live migration does not work for SCSI Persistent Reservations acquired on scsi-block devices. This patch series migrates the reservation key and reservation type so that the destination QEMU can take over the persistent reservation with the PREEMPT service action upon live migration.
The approach involves snooping PERSISTENT RESERVE OUT replies and tracking the scsi-block device's current reservation key and reservation type. In most cases this involves no additional SCSI commands. This approach isn't perfect: if another machine modifies the reservation on the physical LUN, then QEMU's state becomes stale. Persistent reservations are inherently cooperative, so this is acceptable as long as real applications don't run into problems.
Quoted 4 lines
One issue is that this would not transfer reservations done from a previous invocation of the VM. Are you assuming that the restarted VM won't assume to still have the reservation? I think this is fine, but it has to be documented, or maybe QEMU could issue a PR IN command at startup?
Good point. The reason for this limitation is that I don't see a reliable way to detect reservation keys and reservations that belong to a guest. This is why the patch series uses the snooping approach.
The basic READ KEYS and READ RESERVATION service actions for PERSISTENT RESERVATION IN only report the list of reservation keys that have been registered and the key of the current reservation holder. There is no way of tying that information back to the guest or even the host. It would be necessary to know the guest's unique reservation key, but that can be chosen by the guest at runtime. In addition, keys are not guaranteed to be unique so there is no way to tell whether this host is actually the reservation holder without potentially destructive probing (e.g. attempting a command and seeing if it results in a RESERVATION CONFLICT).
The optional READ FULL STATUS service action improves the situation by identifying the initiator together with the reservation holder's key, but it is not universally available. The hardware I'm testing on doesn't implement this service action.
Therefore the assumption is that the guest configures persistent reservations and if it is shut down, it reclaims the reservation while starting up again. It also means that an administrator cannot configure persistent reservations outside the guest and expect live migration to move those persistent reservations with the guest.
The clean way to handle persistent reservation migration is using Fibre Channel N_Port ID Virtualization (NPIV) so that each guest is a distinct initiator that can migrate to another host without messing with the persistent reservation state. NPIV requires the storage administrator to set up the initiators and their zoning - something that few users go through the trouble of doing.
Having said all this, if someone knows about a better way or I'm wrong about how this works, please let me know!
Stefan