Skip to contentSkip to conversation

virtio: stabilize migration format

RFC In review Anthony Liguori 6 participants 15 messages Dec 10, 2012 – Dec 14, 2012

lore
Share & tools
lore
#0 lore raw

cover virtio: stabilize migration format

This series replaces:

    qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));

With code that properly saves out each element of the structure using a well defined endian format. Migration is broken today from big endian to little endian hosts.

There's no way to fix this problem without bumping the migration version number and that's exactly what we do here. By bumping the migration version number, we do break new->old migration but that's unavoidable right now.

In order to support old->new, we assume that all incoming data is in little endian. The final patch adds a check to the load routines to fail old->new on big endian hosts where this may not have been true.

I've not tested this thoroughly enough to apply yet but wanted to share.

---
 hw/virtio-blk.c        |   22 +++++---
 hw/virtio-serial-bus.c |   19 +++----
 hw/virtio.c            |  123 +++++++++++++++++++++++++++++++++++++++++++++++++
 hw/virtio.h            |    4 +
 qemu-file.h            |    7 ++
 savevm.c               |   45 +++++++++++++++++
 6 files changed, 201 insertions(+), 19 deletions(-)
#1 lore raw

PATCH 1/4 savevm: introduce little endian variants of savevm routines

---
 qemu-file.h |  7 +++++++
 savevm.c    | 45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
qemu-file.h +7 -0
index d64bdbb..ac5286c 100644
@@ -94,6 +94,9 @@ static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v)
 void qemu_put_be16(QEMUFile *f, unsigned int v);
 void qemu_put_be32(QEMUFile *f, unsigned int v);
 void qemu_put_be64(QEMUFile *f, uint64_t v);
+void qemu_put_le16(QEMUFile *f, unsigned int v);
+void qemu_put_le32(QEMUFile *f, unsigned int v);
+void qemu_put_le64(QEMUFile *f, uint64_t v);
 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size);
 int qemu_get_byte(QEMUFile *f);
 
@@ -108,6 +111,10 @@ unsigned int qemu_get_be16(QEMUFile *f);
 unsigned int qemu_get_be32(QEMUFile *f);
 uint64_t qemu_get_be64(QEMUFile *f);
 
+unsigned int qemu_get_le16(QEMUFile *f);
+unsigned int qemu_get_le32(QEMUFile *f);
+uint64_t qemu_get_le64(QEMUFile *f);
+
 int qemu_file_rate_limit(QEMUFile *f);
 int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate);
 int64_t qemu_file_get_rate_limit(QEMUFile *f);
savevm.c +45 -0

57 lines not on this page · raw message

Signature · 1 line
1.8.0
#2 lore raw

Re: [Qemu-devel] [PATCH 1/4] savevm: introduce little endian variants of savevm routines

Quoted 11 lines · Anthony Liguori
---
 qemu-file.h |  7 +++++++
 savevm.c    | 45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
qemu-file.h +0 -0
index d64bdbb..ac5286c 100644

qemu-file.h static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v) · line 99

Quoted 6 lines
qemu-file.h +3 -0
@@ -94,6 +94,9 @@ static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v)
 void qemu_put_be16(QEMUFile *f, unsigned int v);
 void qemu_put_be32(QEMUFile *f, unsigned int v);
 void qemu_put_be64(QEMUFile *f, uint64_t v);
+void qemu_put_le16(QEMUFile *f, unsigned int v);
+void qemu_put_le32(QEMUFile *f, unsigned int v);
+void qemu_put_le64(QEMUFile *f, uint64_t v);

Do we want to add a comment here somewhere that says the le versions are for backcompat with a specific thing and the be ones are the ones to use in new code (well, new code not using vmstate)?

-- PMM

#3 lore raw

Re: [Qemu-devel] [PATCH 1/4] savevm: introduce little endian variants of savevm routines

Quoted 7 lines · Peter Maydell
Quoted 6 lines · Anthony Liguori
---
 qemu-file.h |  7 +++++++
 savevm.c    | 45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
Quoted 15 lines

qemu-file.h static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v) · line 99

Quoted 11 lines
qemu-file.h +3 -0
index d64bdbb..ac5286c 100644
@@ -94,6 +94,9 @@ static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v)
 void qemu_put_be16(QEMUFile *f, unsigned int v);
 void qemu_put_be32(QEMUFile *f, unsigned int v);
 void qemu_put_be64(QEMUFile *f, uint64_t v);
+void qemu_put_le16(QEMUFile *f, unsigned int v);
+void qemu_put_le32(QEMUFile *f, unsigned int v);
+void qemu_put_le64(QEMUFile *f, uint64_t v);

Do we want to add a comment here somewhere that says the le versions are for backcompat with a specific thing and the be ones are the ones to use in new code (well, new code not using vmstate)?

Yeah, that's a good idea. I've unfortunately found a couple more cases of this (writing native endian to the wire).

Regards,

Anthony Liguori

Quoted 2 lines

-- PMM

#4 lore raw

PATCH 2/4 virtio: add wrapper for saving/restoring virtqueue elements

In reply to Anthony Liguori

Putting raw structures on the wire is bad news. Add a wrapper and use it.

Note that in virtio-serial-bus, we were mapping both the in and out vectors as writable. This is a bug that is fixed by this change. I checked the revision history, it has been there since the code was first added and does not appear to be intentional.

---
 hw/virtio-blk.c        |  9 ++-------
 hw/virtio-serial-bus.c | 10 ++--------
 hw/virtio.c            | 13 +++++++++++++
 hw/virtio.h            |  4 ++++
 4 files changed, 21 insertions(+), 15 deletions(-)
hw/virtio-blk.c +2 -7
index e25cc96..7ab174f 100644
@@ -555,7 +555,7 @@ static void virtio_blk_save(QEMUFile *f, void *opaque)
     
     while (req) {
         qemu_put_sbyte(f, 1);
-        qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
+        virtio_put_virt_queue_element(f, &req->elem);
         req = req->next;
     }
     qemu_put_sbyte(f, 0);
@@ -576,14 +576,9 @@ static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
 
     while (qemu_get_sbyte(f)) {
         VirtIOBlockReq *req = virtio_blk_alloc_request(s);
-        qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
+        virtio_get_virt_queue_element(f, &req->elem);
         req->next = s->rq;
         s->rq = req;
-
-        virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
-            req->elem.in_num, 1);
-        virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
-            req->elem.out_num, 0);
     }
 
     return 0;
hw/virtio-serial-bus.c +2 -8
index 155da58..aa1ded0 100644
@@ -629,8 +629,7 @@ static void virtio_serial_save(QEMUFile *f, void *opaque)
             qemu_put_be32s(f, &port->iov_idx);
             qemu_put_be64s(f, &port->iov_offset);
 
-            qemu_put_buffer(f, (unsigned char *)&port->elem,
-                            sizeof(port->elem));
+            virtio_put_virt_queue_element(f, &port->elem);
         }
     }
 }
@@ -731,12 +730,7 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
                 qemu_get_be32s(f, &port->iov_idx);
                 qemu_get_be64s(f, &port->iov_offset);
 
-                qemu_get_buffer(f, (unsigned char *)&port->elem,
-                                sizeof(port->elem));
-                virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
-                                 port->elem.in_num, 1);
-                virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
-                                 port->elem.out_num, 1);
+                virtio_get_virt_queue_element(f, &port->elem);
 
                 /*
                  *  Port was throttled on source machine.  Let's
hw/virtio.c +13 -0
index f40a8c5..8eb8f69 100644
@@ -875,6 +875,19 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f)
     return 0;
 }
 
+void virtio_put_virt_queue_element(QEMUFile *f, const VirtQueueElement *elem)
+{
+    qemu_put_buffer(f, (unsigned char*)elem, sizeof(*elem));
+}
+
+void virtio_get_virt_queue_element(QEMUFile *f, VirtQueueElement *elem)
+{
+    qemu_get_buffer(f, (unsigned char *)elem, sizeof(*elem));
+
+    virtqueue_map_sg(elem->in_sg, elem->in_addr, elem->in_num, 1);
+    virtqueue_map_sg(elem->out_sg, elem->out_addr, elem->out_num, 0);
+}
+
 void virtio_cleanup(VirtIODevice *vdev)
 {
     qemu_del_vm_change_state_handler(vdev->vmstate);
hw/virtio.h +4 -0
index 7c17f7b..4af8239 100644
@@ -159,6 +159,10 @@ void virtio_save(VirtIODevice *vdev, QEMUFile *f);
 
 int virtio_load(VirtIODevice *vdev, QEMUFile *f);
 
+void virtio_put_virt_queue_element(QEMUFile *f, const VirtQueueElement *elem);
+
+void virtio_get_virt_queue_element(QEMUFile *f, VirtQueueElement *elem);
+
 void virtio_cleanup(VirtIODevice *vdev);
 
 void virtio_notify_config(VirtIODevice *vdev);
Signature · 1 line
1.8.0
#5 lore raw

Re: [Qemu-devel] [PATCH 2/4] virtio: add wrapper for saving/restoring virtqueue elements

Il 10/12/2012 15:29, Anthony Liguori ha scritto:

Quoted 7 lines

Putting raw structures on the wire is bad news. Add a wrapper and use it.

Note that in virtio-serial-bus, we were mapping both the in and out vectors as writable. This is a bug that is fixed by this change. I checked the revision history, it has been there since the code was first added and does not appear to be intentional.

Quoted 7 lines
---
 hw/virtio-blk.c        |  9 ++-------
 hw/virtio-serial-bus.c | 10 ++--------
 hw/virtio.c            | 13 +++++++++++++
 hw/virtio.h            |  4 ++++
 4 files changed, 21 insertions(+), 15 deletions(-)

virtio-scsi is missing; see virtio_scsi_load_request and virtio_scsi_save_request.

Paolo

Quoted 97 lines
hw/virtio-blk.c +2 -7
index e25cc96..7ab174f 100644
@@ -555,7 +555,7 @@ static void virtio_blk_save(QEMUFile *f, void *opaque)
 
     while (req) {
         qemu_put_sbyte(f, 1);
-        qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
+        virtio_put_virt_queue_element(f, &req->elem);
         req = req->next;
     }
     qemu_put_sbyte(f, 0);
@@ -576,14 +576,9 @@ static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
 
     while (qemu_get_sbyte(f)) {
         VirtIOBlockReq *req = virtio_blk_alloc_request(s);
-        qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
+        virtio_get_virt_queue_element(f, &req->elem);
         req->next = s->rq;
         s->rq = req;
-
-        virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
-            req->elem.in_num, 1);
-        virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
-            req->elem.out_num, 0);
     }
 
     return 0;
hw/virtio-serial-bus.c +2 -8
index 155da58..aa1ded0 100644
@@ -629,8 +629,7 @@ static void virtio_serial_save(QEMUFile *f, void *opaque)
             qemu_put_be32s(f, &port->iov_idx);
             qemu_put_be64s(f, &port->iov_offset);
 
-            qemu_put_buffer(f, (unsigned char *)&port->elem,
-                            sizeof(port->elem));
+            virtio_put_virt_queue_element(f, &port->elem);
         }
     }
 }
@@ -731,12 +730,7 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
                 qemu_get_be32s(f, &port->iov_idx);
                 qemu_get_be64s(f, &port->iov_offset);
 
-                qemu_get_buffer(f, (unsigned char *)&port->elem,
-                                sizeof(port->elem));
-                virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
-                                 port->elem.in_num, 1);
-                virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
-                                 port->elem.out_num, 1);
+                virtio_get_virt_queue_element(f, &port->elem);
 
                 /*
                  *  Port was throttled on source machine.  Let's
hw/virtio.c +13 -0
index f40a8c5..8eb8f69 100644
@@ -875,6 +875,19 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f)
     return 0;
 }
 
+void virtio_put_virt_queue_element(QEMUFile *f, const VirtQueueElement *elem)
+{
+    qemu_put_buffer(f, (unsigned char*)elem, sizeof(*elem));
+}
+
+void virtio_get_virt_queue_element(QEMUFile *f, VirtQueueElement *elem)
+{
+    qemu_get_buffer(f, (unsigned char *)elem, sizeof(*elem));
+
+    virtqueue_map_sg(elem->in_sg, elem->in_addr, elem->in_num, 1);
+    virtqueue_map_sg(elem->out_sg, elem->out_addr, elem->out_num, 0);
+}
+
 void virtio_cleanup(VirtIODevice *vdev)
 {
     qemu_del_vm_change_state_handler(vdev->vmstate);
hw/virtio.h +4 -0
index 7c17f7b..4af8239 100644
@@ -159,6 +159,10 @@ void virtio_save(VirtIODevice *vdev, QEMUFile *f);
 
 int virtio_load(VirtIODevice *vdev, QEMUFile *f);
 
+void virtio_put_virt_queue_element(QEMUFile *f, const VirtQueueElement *elem);
+
+void virtio_get_virt_queue_element(QEMUFile *f, VirtQueueElement *elem);
+
 void virtio_cleanup(VirtIODevice *vdev);
 
 void virtio_notify_config(VirtIODevice *vdev);
#6 lore raw

Re: [Qemu-devel] [PATCH 2/4] virtio: add wrapper for saving/restoring virtqueue elements

Quoted 16 lines · Paolo Bonzini

Il 10/12/2012 15:29, Anthony Liguori ha scritto:

Quoted 7 lines

Putting raw structures on the wire is bad news. Add a wrapper and use it.

Note that in virtio-serial-bus, we were mapping both the in and out vectors as writable. This is a bug that is fixed by this change. I checked the revision history, it has been there since the code was first added and does not appear to be intentional.

Quoted 7 lines
---
 hw/virtio-blk.c        |  9 ++-------
 hw/virtio-serial-bus.c | 10 ++--------
 hw/virtio.c            | 13 +++++++++++++
 hw/virtio.h            |  4 ++++
 4 files changed, 21 insertions(+), 15 deletions(-)
Quoted 2 lines

virtio-scsi is missing; see virtio_scsi_load_request and virtio_scsi_save_request.

Indeed, thanks.

Regards,

Anthony Liguori

Quoted 100 lines

Paolo

Quoted 97 lines
hw/virtio-blk.c +2 -7
index e25cc96..7ab174f 100644
@@ -555,7 +555,7 @@ static void virtio_blk_save(QEMUFile *f, void *opaque)
 
     while (req) {
         qemu_put_sbyte(f, 1);
-        qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
+        virtio_put_virt_queue_element(f, &req->elem);
         req = req->next;
     }
     qemu_put_sbyte(f, 0);
@@ -576,14 +576,9 @@ static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
 
     while (qemu_get_sbyte(f)) {
         VirtIOBlockReq *req = virtio_blk_alloc_request(s);
-        qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
+        virtio_get_virt_queue_element(f, &req->elem);
         req->next = s->rq;
         s->rq = req;
-
-        virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
-            req->elem.in_num, 1);
-        virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
-            req->elem.out_num, 0);
     }
 
     return 0;
hw/virtio-serial-bus.c +2 -8
index 155da58..aa1ded0 100644
@@ -629,8 +629,7 @@ static void virtio_serial_save(QEMUFile *f, void *opaque)
             qemu_put_be32s(f, &port->iov_idx);
             qemu_put_be64s(f, &port->iov_offset);
 
-            qemu_put_buffer(f, (unsigned char *)&port->elem,
-                            sizeof(port->elem));
+            virtio_put_virt_queue_element(f, &port->elem);
         }
     }
 }
@@ -731,12 +730,7 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
                 qemu_get_be32s(f, &port->iov_idx);
                 qemu_get_be64s(f, &port->iov_offset);
 
-                qemu_get_buffer(f, (unsigned char *)&port->elem,
-                                sizeof(port->elem));
-                virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
-                                 port->elem.in_num, 1);
-                virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
-                                 port->elem.out_num, 1);
+                virtio_get_virt_queue_element(f, &port->elem);
 
                 /*
                  *  Port was throttled on source machine.  Let's
hw/virtio.c +13 -0
index f40a8c5..8eb8f69 100644
@@ -875,6 +875,19 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f)
     return 0;
 }
 
+void virtio_put_virt_queue_element(QEMUFile *f, const VirtQueueElement *elem)
+{
+    qemu_put_buffer(f, (unsigned char*)elem, sizeof(*elem));
+}
+
+void virtio_get_virt_queue_element(QEMUFile *f, VirtQueueElement *elem)
+{
+    qemu_get_buffer(f, (unsigned char *)elem, sizeof(*elem));
+
+    virtqueue_map_sg(elem->in_sg, elem->in_addr, elem->in_num, 1);
+    virtqueue_map_sg(elem->out_sg, elem->out_addr, elem->out_num, 0);
+}
+
 void virtio_cleanup(VirtIODevice *vdev)
 {
     qemu_del_vm_change_state_handler(vdev->vmstate);
hw/virtio.h +4 -0
index 7c17f7b..4af8239 100644
@@ -159,6 +159,10 @@ void virtio_save(VirtIODevice *vdev, QEMUFile *f);
 
 int virtio_load(VirtIODevice *vdev, QEMUFile *f);
 
+void virtio_put_virt_queue_element(QEMUFile *f, const VirtQueueElement *elem);
+
+void virtio_get_virt_queue_element(QEMUFile *f, VirtQueueElement *elem);
+
 void virtio_cleanup(VirtIODevice *vdev);
 
 void virtio_notify_config(VirtIODevice *vdev);
#7 lore raw

PATCH 3/4 virtio: modify savevm to have a stable wire format

In reply to Anthony Liguori

We were memcpy()'ing a structure to the wire :-/ Since savevm really only works on x86 today, lets just declare that this element is sent over the wire as a little endian value in order to fix the bitness.

Unfortunately, we also send raw pointers and size_t which are going to be different values on a 32-bit vs. 64-bit QEMU so we need to also deal with that case.

A lot of values that should have been previously ignored are now sent as 0 and ignored on the receive side too.

---
 hw/virtio.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 112 insertions(+), 2 deletions(-)
hw/virtio.c +112 -2
index 8eb8f69..0108d62 100644
@@ -875,14 +875,124 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f)
     return 0;
 }
 
+/* We used to memcpy the structure to the wire so that's the reason for all of
+ * this ugliness */
+
+#if HOST_LONG_BITS == 32
+static uint32 virtio_get_long(QEMUFile *f)
+{
+    return qemu_get_le32(f);
+}
+
+static void virtio_put_long(QEMUFile *f, uint32_t value)
+{
+    qemu_put_le32(f, value);
+}
+#elif HOST_LONG_BITS == 64
+static uint64 virtio_get_long(QEMUFile *f)
+{
+    return qemu_get_le64(f);
+}
+
+static void virtio_put_long(QEMUFile *f, uint64_t value)
+{
+    qemu_put_le64(f, value);
+}
+#else
+#error Invalid HOST_LONG_BITS
+#endif
+
 void virtio_put_virt_queue_element(QEMUFile *f, const VirtQueueElement *elem)
 {
-    qemu_put_buffer(f, (unsigned char*)elem, sizeof(*elem));
+    int i;
+
+    qemu_put_le32(f, elem->index);
+    qemu_put_le32(f, elem->out_num);
+    qemu_put_le32(f, elem->in_num);
+
+    qemu_put_le32(f, 0); /* padding for structure */
+
+    for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) {
+        if (i < elem->in_num) {
+            qemu_put_le64(f, elem->in_addr[i]);
+        } else {
+            qemu_put_le64(f, 0);
+        }
+    }
+
+    for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) {
+        if (i < elem->out_num) {
+            qemu_put_le64(f, elem->out_addr[i]);
+        } else {
+            qemu_put_le64(f, 0);
+        }
+    }
+
+    for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) {
+        virtio_put_long(f, 0);
+        if (i < elem->in_num) {
+            virtio_put_long(f, elem->in_sg[i].iov_len);
+        } else {
+            virtio_put_long(f, 0);
+        }
+    }
+
+    for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) {
+        virtio_put_long(f, 0);
+        if (i < elem->out_num) {
+            virtio_put_long(f, elem->out_sg[i].iov_len);
+        } else {
+            virtio_put_long(f, 0);
+        }
+    }
 }
 
 void virtio_get_virt_queue_element(QEMUFile *f, VirtQueueElement *elem)
 {
-    qemu_get_buffer(f, (unsigned char *)elem, sizeof(*elem));
+    int i;
+
+    elem->index = qemu_get_le32(f);
+    elem->out_num = qemu_get_le32(f);
+    elem->in_num = qemu_get_le32(f);
+
+    assert(elem->out_num <= VIRTQUEUE_MAX_SIZE &&
+           elem->in_num <= VIRTQUEUE_MAX_SIZE);
+
+    qemu_get_le32(f); /* padding for structure */
+
+    for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) {
+        if (i < elem->in_num) {
+            elem->in_addr[i] = qemu_get_le64(f);
+        } else {
+            qemu_get_le64(f);
+        }
+    }
+
+    for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) {
+        if (i < elem->out_num) {
+            elem->out_addr[i] = qemu_get_le64(f);
+        } else {
+            qemu_get_le64(f);
+        }
+    }
+
+    for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) {
+        virtio_get_long(f);
+        if (i < elem->in_num) {
+            elem->in_sg[i].iov_len = virtio_get_long(f);
+        } else {
+            virtio_get_long(f);
+        }
+    }
+
+    for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) {
+        virtio_get_long(f);
+        if (i < elem->out_num) {
+            elem->out_sg[i].iov_len = virtio_get_long(f);
+        } else {
+            virtio_get_long(f);
+        }
+    }
 
     virtqueue_map_sg(elem->in_sg, elem->in_addr, elem->in_num, 1);
     virtqueue_map_sg(elem->out_sg, elem->out_addr, elem->out_num, 0);
Signature · 1 line
1.8.0
#8 lore raw

Re: [Qemu-devel] [PATCH 3/4] virtio: modify savevm to have a stable wire format

Quoted 10 lines · Anthony Liguori

We were memcpy()'ing a structure to the wire :-/ Since savevm really only works on x86 today, lets just declare that this element is sent over the wire as a little endian value in order to fix the bitness.

Unfortunately, we also send raw pointers and size_t which are going to be different values on a 32-bit vs. 64-bit QEMU so we need to also deal with that case.

A lot of values that should have been previously ignored are now sent as 0 and ignored on the receive side too.

Don't we want to transition to vmstate anyway? Can we just do that, and relegate the existing slightly broken code, to legacy?

Cheers,
Rusty.

#9 lore raw

Re: [Qemu-devel] [PATCH 3/4] virtio: modify savevm to have a stable wire format

Quoted 9 lines · Rusty Russell
Quoted 8 lines · Anthony Liguori

We were memcpy()'ing a structure to the wire :-/ Since savevm really only works on x86 today, lets just declare that this element is sent over the wire as a little endian value in order to fix the bitness.

Unfortunately, we also send raw pointers and size_t which are going to be different values on a 32-bit vs. 64-bit QEMU so we need to also deal with that case.

Quoted 5 lines
Quoted 2 lines

A lot of values that should have been previously ignored are now sent as 0 and ignored on the receive side too.

Don't we want to transition to vmstate anyway? Can we just do that, and relegate the existing slightly broken code, to legacy?

What worries me is if someone changes VirtQueueElement, then all the sudden migration breaks. By transitioning to what I've sent, we at least have a better documented protocol that isn't prone to subtle breakage anymore. Plus, we resolve the endian issue before it becomes a bigger problem when David actually gets live migration working reliably on PPC...

I'm certainly in favor of cleaning up the savevm format and probably leaving the existing load/save functions as-is for legacy purposes. I'll leave that as an exercise for someone else though :-)

Regards,

Anthony Liguori

Quoted 3 lines

Cheers,
Rusty.

#10 lore raw

Re: [Qemu-devel] [PATCH 3/4] virtio: modify savevm to have a stable wire format

Quoted 17 lines · Anthony Liguori
Quoted 9 lines · Rusty Russell
Quoted 8 lines · Anthony Liguori

We were memcpy()'ing a structure to the wire :-/ Since savevm really only works on x86 today, lets just declare that this element is sent over the wire as a little endian value in order to fix the bitness.

Unfortunately, we also send raw pointers and size_t which are going to be different values on a 32-bit vs. 64-bit QEMU so we need to also deal with that case.

Quoted 5 lines
Quoted 2 lines

A lot of values that should have been previously ignored are now sent as 0 and ignored on the receive side too.

Don't we want to transition to vmstate anyway? Can we just do that, and relegate the existing slightly broken code, to legacy?

Quoted 6 lines

What worries me is if someone changes VirtQueueElement, then all the sudden migration breaks. By transitioning to what I've sent, we at least have a better documented protocol that isn't prone to subtle breakage anymore. Plus, we resolve the endian issue before it becomes a bigger problem when David actually gets live migration working reliably on PPC...

My transition was to copy that structure to VirtQueueSavedElement, and it's only used for loading old versions.

With the new code we only need the head from that structure.

Quoted 3 lines

I'm certainly in favor of cleaning up the savevm format and probably leaving the existing load/save functions as-is for legacy purposes. I'll leave that as an exercise for someone else though :-)

What is the rule about new versions? Can we introduce a new save version at any time, or only at major qemu version changes?

Thanks,
Rusty.

#11 lore raw

Re: [Qemu-devel] [PATCH 3/4] virtio: modify savevm to have a stable wire format

Il 14/12/2012 01:57, Rusty Russell ha scritto:

Quoted 1 line

With the new code we only need the head from that structure.

We also need to do again all validation of the elements if we fetch it back from the data. Sometimes the parsed data is saved elsewhere (e.g. in a SCSIRequest struct that is serialized by the SCSI subsystem) and that data may be inconsistent with whatever you read from guest memory.
It's a can of worms.

Quoted 6 lines
Quoted 3 lines

I'm certainly in favor of cleaning up the savevm format and probably leaving the existing load/save functions as-is for legacy purposes. I'll leave that as an exercise for someone else though :-)

What is the rule about new versions? Can we introduce a new save version at any time, or only at major qemu version changes?

Any time, but we provide a backwards-compatible loader for older versions.

Paolo

#12 lore raw

Re: [Qemu-devel] [PATCH 3/4] virtio: modify savevm to have a stable wire format

Quoted 13 lines · Paolo Bonzini

Il 14/12/2012 01:57, Rusty Russell ha scritto:

Quoted 1 line

With the new code we only need the head from that structure.

We also need to do again all validation of the elements if we fetch it back from the data. Sometimes the parsed data is saved elsewhere (e.g. in a SCSIRequest struct that is serialized by the SCSI subsystem) and that data may be inconsistent with whatever you read from guest memory.
It's a can of worms.

Quoted 4 lines
Quoted 3 lines

I'm certainly in favor of cleaning up the savevm format and probably leaving the existing load/save functions as-is for legacy purposes. I'll leave that as an exercise for someone else though :-)

Quoted 5 lines
Quoted 2 lines

What is the rule about new versions? Can we introduce a new save version at any time, or only at major qemu version changes?

Any time, but we provide a backwards-compatible loader for older versions.

Well.. if we can avoid bumping the version, we should. Bumping the version breaks migration from new -> old so it's preferrable that we don't do it unless it's absolutely required.

Regards,

Anthony Liguori

Quoted 2 lines

Paolo

#13 lore raw

PATCH 4/4 virtio: bump migration version number

In reply to Anthony Liguori

And disable migration on big endian hosts from older versions where endianness of the device state was ambiguous on the wire.

---
 hw/virtio-blk.c        | 13 +++++++++++--
 hw/virtio-serial-bus.c |  9 +++++++--
 2 files changed, 18 insertions(+), 4 deletions(-)
hw/virtio-blk.c +11 -2
index 7ab174f..837a709 100644
@@ -566,8 +566,17 @@ static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
     VirtIOBlock *s = opaque;
     int ret;
 
-    if (version_id != 2)
+#ifdef HOST_WORDS_BIGENDIAN
+    /* Because of the use of native endianness, we can't reliably handle
+     * migration below this version on big endian hosts. */
+    if (version < 3) {
         return -EINVAL;
+    }
+#endif
+
+    if (version_id < 2) {
+        return -EINVAL;
+    }
 
     ret = virtio_load(&s->vdev, f);
     if (ret) {
@@ -633,7 +642,7 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
 
     qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
     s->qdev = dev;
-    register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
+    register_savevm(dev, "virtio-blk", virtio_blk_id++, 3,
                     virtio_blk_save, virtio_blk_load, s);
     bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
     bdrv_set_buffer_alignment(s->bs, s->conf->logical_block_size);
hw/virtio-serial-bus.c +7 -2
index aa1ded0..6aa3b85 100644
@@ -665,9 +665,14 @@ static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
     unsigned int i;
     int ret;
 
-    if (version_id > 3) {
+#ifdef HOST_WORDS_BIGENDIAN
+    /* Because of the use of native endianness, we can't reliably handle
+     * migration below this version on big endian hosts. */
+
+    if (version_id < 4) {
         return -EINVAL;
     }
+#endif
 
     /* The virtio device */
     ret = virtio_load(&s->vdev, f);
@@ -987,7 +992,7 @@ VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
      * Register for the savevm section with the virtio-console name
      * to preserve backward compat
      */
-    register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
+    register_savevm(dev, "virtio-console", -1, 4, virtio_serial_save,
                     virtio_serial_load, vser);
 
     vser->post_load.timer = qemu_new_timer_ns(vm_clock,
Signature · 1 line
1.8.0
#14 lore raw
In reply to Anthony Liguori
Quoted 8 lines · Anthony Liguori

This series replaces:

    qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));

With code that properly saves out each element of the structure using a well defined endian format. Migration is broken today from big endian to little endian hosts.

Quoted 7 lines

There's no way to fix this problem without bumping the migration version number and that's exactly what we do here. By bumping the migration version number, we do break new->old migration but that's unavoidable right now.

In order to support old->new, we assume that all incoming data is in little endian. The final patch adds a check to the load routines to fail old->new on big endian hosts where this may not have been true.

Is there a way to detect the endianness of the source host - by peaking at a known multibyte value in the incoming stream?

That way we could even support cross-endian migration.

Not sure if this much magic makes sense since cross-endian migration is probably used rarely.

Stefan