Skip to contentSkip to conversation

virtio_balloon: header update for virtio 1

PATCH In review Michael S. Tsirkin 2 participants 9 messages Apr 12, 2015 – Apr 13, 2015 virtio core

lore
Share & tools
lore

Michael S. Tsirkin

author maintainer
#0 lore raw

PATCH 1/2 virtio_balloon: header update for virtio 1

add modern header.
This patch is for virtio 1.0 branch, doesn't
apply to master.

---
 include/hw/virtio/virtio-balloon.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/hw/virtio/virtio-balloon.h +6 -0
index f863bfe..79eca67 100644
@@ -56,6 +56,12 @@ typedef struct VirtIOBalloonStat {
     uint64_t val;
 } QEMU_PACKED VirtIOBalloonStat;
 
+typedef struct virtio_balloon_stat_modern {
+       uint16_t tag;
+       uint8_t reserved[6];
+       uint64_t val;
+} VirtIOBalloonStatModern;
+
 typedef struct VirtIOBalloon {
     VirtIODevice parent_obj;
     VirtQueue *ivq, *dvq, *svq;
Signature · 1 line
MST
author maintainer
#1 lore raw

PATCH 2/2 virtio-balloon: virtio 1 support

Virtio 1.0 doesn't include a modern balloon device. At some point we'll likely define an incompatible interface with a different ID and different semantics. But for now, it's not a big effort to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently, as well as transports that don't allow mixing virtio 0 and virtio 1 devices. And balloon is an easy device to test, so it's also useful for people to test virtio core handling of transitional devices.

The only interface issue is with the stats buffer, which has misaligned fields. We could leave it as is, but this sets a bad precedent that others might copy by mistake.

---
 hw/virtio/virtio-balloon.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
hw/virtio/virtio-balloon.c +23 -8
index d2d7c3e..568a008 100644
@@ -239,7 +239,8 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
 {
     VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
     VirtQueueElement *elem = &s->stats_vq_elem;
-    VirtIOBalloonStat stat;
+    VirtIOBalloonStat legacy_stat;
+    VirtIOBalloonStatModern modern_stat;
     size_t offset = 0;
     qemu_timeval tv;
 
@@ -253,14 +254,28 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
      */
     reset_stats(s);
 
-    while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
-           == sizeof(stat)) {
-        uint16_t tag = virtio_tswap16(vdev, stat.tag);
-        uint64_t val = virtio_tswap64(vdev, stat.val);
+    if (virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
+        while (iov_to_buf(elem->out_sg, elem->out_num, offset,
+                          &modern_stat, sizeof(modern_stat))
+               == sizeof(modern_stat)) {
+            uint16_t tag = le16_to_cpu(modern_stat.tag);
+            uint64_t val = le64_to_cpu(modern_stat.val);
 
-        offset += sizeof(stat);
-        if (tag < VIRTIO_BALLOON_S_NR)
-            s->stats[tag] = val;
+            offset += sizeof(modern_stat);
+            if (tag < VIRTIO_BALLOON_S_NR)
+                s->stats[tag] = val;
+        }
+    } else {
+        while (iov_to_buf(elem->out_sg, elem->out_num, offset,
+                          &legacy_stat, sizeof(legacy_stat))
+               == sizeof(legacy_stat)) {
+            uint16_t tag = virtio_tswap16(vdev, legacy_stat.tag);
+            uint64_t val = virtio_tswap64(vdev, legacy_stat.val);
+
+            offset += sizeof(legacy_stat);
+            if (tag < VIRTIO_BALLOON_S_NR)
+                s->stats[tag] = val;
+        }
     }
     s->stats_vq_offset = offset;
 
Signature · 1 line
MST
author maintainer
#2 lore raw

PATCH 2/2 virtio-balloon: virtio 1 support

In reply to Michael S. Tsirkin

Virtio 1.0 doesn't include a modern balloon device. At some point we'll likely define an incompatible interface with a different ID and different semantics. But for now, it's not a big effort to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently, as well as transports that don't allow mixing virtio 0 and virtio 1 devices. And balloon is an easy device to test, so it's also useful for people to test virtio core handling of transitional devices.

The only interface issue is with the stats buffer, which has misaligned fields. We could leave it as is, but this sets a bad precedent that others might copy by mistake.

---
 hw/virtio/virtio-balloon.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
hw/virtio/virtio-balloon.c +23 -8
index d2d7c3e..568a008 100644
@@ -239,7 +239,8 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
 {
     VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
     VirtQueueElement *elem = &s->stats_vq_elem;
-    VirtIOBalloonStat stat;
+    VirtIOBalloonStat legacy_stat;
+    VirtIOBalloonStatModern modern_stat;
     size_t offset = 0;
     qemu_timeval tv;
 
@@ -253,14 +254,28 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
      */
     reset_stats(s);
 
-    while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
-           == sizeof(stat)) {
-        uint16_t tag = virtio_tswap16(vdev, stat.tag);
-        uint64_t val = virtio_tswap64(vdev, stat.val);
+    if (virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
+        while (iov_to_buf(elem->out_sg, elem->out_num, offset,
+                          &modern_stat, sizeof(modern_stat))
+               == sizeof(modern_stat)) {
+            uint16_t tag = le16_to_cpu(modern_stat.tag);
+            uint64_t val = le64_to_cpu(modern_stat.val);
 
-        offset += sizeof(stat);
-        if (tag < VIRTIO_BALLOON_S_NR)
-            s->stats[tag] = val;
+            offset += sizeof(modern_stat);
+            if (tag < VIRTIO_BALLOON_S_NR)
+                s->stats[tag] = val;
+        }
+    } else {
+        while (iov_to_buf(elem->out_sg, elem->out_num, offset,
+                          &legacy_stat, sizeof(legacy_stat))
+               == sizeof(legacy_stat)) {
+            uint16_t tag = virtio_tswap16(vdev, legacy_stat.tag);
+            uint64_t val = virtio_tswap64(vdev, legacy_stat.val);
+
+            offset += sizeof(legacy_stat);
+            if (tag < VIRTIO_BALLOON_S_NR)
+                s->stats[tag] = val;
+        }
     }
     s->stats_vq_offset = offset;
 
Signature · 1 line
MST
#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] virtio-balloon: virtio 1 support

Quoted 58 lines · On Sun, 12 Apr 2015 17:00:48 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

Virtio 1.0 doesn't include a modern balloon device. At some point we'll likely define an incompatible interface with a different ID and different semantics. But for now, it's not a big effort to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently, as well as transports that don't allow mixing virtio 0 and virtio 1 devices. And balloon is an easy device to test, so it's also useful for people to test virtio core handling of transitional devices.

The only interface issue is with the stats buffer, which has misaligned fields. We could leave it as is, but this sets a bad precedent that others might copy by mistake.

---
 hw/virtio/virtio-balloon.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
hw/virtio/virtio-balloon.c +16 -8
index d2d7c3e..568a008 100644
@@ -239,7 +239,8 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
 {
     VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
     VirtQueueElement *elem = &s->stats_vq_elem;
-    VirtIOBalloonStat stat;
+    VirtIOBalloonStat legacy_stat;
+    VirtIOBalloonStatModern modern_stat;
     size_t offset = 0;
     qemu_timeval tv;
 
@@ -253,14 +254,28 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
      */
     reset_stats(s);
 
-    while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
-           == sizeof(stat)) {
-        uint16_t tag = virtio_tswap16(vdev, stat.tag);
-        uint64_t val = virtio_tswap64(vdev, stat.val);
+    if (virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
+        while (iov_to_buf(elem->out_sg, elem->out_num, offset,
+                          &modern_stat, sizeof(modern_stat))
+               == sizeof(modern_stat)) {
+            uint16_t tag = le16_to_cpu(modern_stat.tag);
+            uint64_t val = le64_to_cpu(modern_stat.val);
 
-        offset += sizeof(stat);
-        if (tag < VIRTIO_BALLOON_S_NR)
-            s->stats[tag] = val;
+            offset += sizeof(modern_stat);
+            if (tag < VIRTIO_BALLOON_S_NR)
+                s->stats[tag] = val;
+        }
+    } else {
+        while (iov_to_buf(elem->out_sg, elem->out_num, offset,
+                          &legacy_stat, sizeof(legacy_stat))
+               == sizeof(legacy_stat)) {

hw/virtio/virtio-balloon.c static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq) · line 281

Quoted 10 lines
hw/virtio/virtio-balloon.c +7 -0
+            uint16_t tag = virtio_tswap16(vdev, legacy_stat.tag);
+            uint64_t val = virtio_tswap64(vdev, legacy_stat.val);
+
+            offset += sizeof(legacy_stat);
+            if (tag < VIRTIO_BALLOON_S_NR)
+                s->stats[tag] = val;
+        }
     }
     s->stats_vq_offset = offset;
 

I'm still not really convinced that changing the stat structure is an improvement. Without that, you wouldn't need the above change at all, would you?

Also, doesn't get_features need to be modified as well so that VERSION_1 is advertised?

author maintainer
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] virtio-balloon: virtio 1 support

Quoted 66 lines · Cornelia Huck
Quoted 63 lines · On Sun, 12 Apr 2015 17:00:48 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

Virtio 1.0 doesn't include a modern balloon device. At some point we'll likely define an incompatible interface with a different ID and different semantics. But for now, it's not a big effort to support a transitional balloon device: this has the advantage of supporting existing drivers, transparently, as well as transports that don't allow mixing virtio 0 and virtio 1 devices. And balloon is an easy device to test, so it's also useful for people to test virtio core handling of transitional devices.

The only interface issue is with the stats buffer, which has misaligned fields. We could leave it as is, but this sets a bad precedent that others might copy by mistake.

---
 hw/virtio/virtio-balloon.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
hw/virtio/virtio-balloon.c +21 -8
index d2d7c3e..568a008 100644
@@ -239,7 +239,8 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
 {
     VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
     VirtQueueElement *elem = &s->stats_vq_elem;
-    VirtIOBalloonStat stat;
+    VirtIOBalloonStat legacy_stat;
+    VirtIOBalloonStatModern modern_stat;
     size_t offset = 0;
     qemu_timeval tv;
 
@@ -253,14 +254,28 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
      */
     reset_stats(s);
 
-    while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
-           == sizeof(stat)) {
-        uint16_t tag = virtio_tswap16(vdev, stat.tag);
-        uint64_t val = virtio_tswap64(vdev, stat.val);
+    if (virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
+        while (iov_to_buf(elem->out_sg, elem->out_num, offset,
+                          &modern_stat, sizeof(modern_stat))
+               == sizeof(modern_stat)) {
+            uint16_t tag = le16_to_cpu(modern_stat.tag);
+            uint64_t val = le64_to_cpu(modern_stat.val);
 
-        offset += sizeof(stat);
-        if (tag < VIRTIO_BALLOON_S_NR)
-            s->stats[tag] = val;
+            offset += sizeof(modern_stat);
+            if (tag < VIRTIO_BALLOON_S_NR)
+                s->stats[tag] = val;
+        }
+    } else {
+        while (iov_to_buf(elem->out_sg, elem->out_num, offset,
+                          &legacy_stat, sizeof(legacy_stat))
+               == sizeof(legacy_stat)) {
+            uint16_t tag = virtio_tswap16(vdev, legacy_stat.tag);
+            uint64_t val = virtio_tswap64(vdev, legacy_stat.val);
+
+            offset += sizeof(legacy_stat);
+            if (tag < VIRTIO_BALLOON_S_NR)
Quoted 8 lines

hw/virtio/virtio-balloon.c static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq) · line 281

Quoted 5 lines
hw/virtio/virtio-balloon.c +2 -0
+                s->stats[tag] = val;
+        }
     }
     s->stats_vq_offset = offset;
 

I'm still not really convinced that changing the stat structure is an improvement.

The point is to avoid setting bad precedent for virtio 1 devices. It does have a bit of cost for transitional devices.

Quoted 2 lines

Without that, you wouldn't need the above change at all, would you?

I think so, yes. BTW I suspect the stats code is broken for cross-endian platforms: it should do LE unconditinally, should it not?

Quoted 2 lines

Also, doesn't get_features need to be modified as well so that VERSION_1 is advertised?

virtio_pci_device_plugged seems to set it ATM. I'll re-test to confirm.

Signature · 1 line
MST
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] virtio-balloon: virtio 1 support

Quoted 6 lines · On Mon, 13 Apr 2015 13:26:31 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 2 lines · Cornelia Huck

Also, doesn't get_features need to be modified as well so that VERSION_1 is advertised?

virtio_pci_device_plugged seems to set it ATM. I'll re-test to confirm.

Indeed, pci sets it, but ccw does not. And virtio-net, for example, sets it explicitly as well. We need to unify this :)

author maintainer
#6 lore raw

Re: [Qemu-devel] [PATCH 2/2] virtio-balloon: virtio 1 support

Quoted 5 lines · Cornelia Huck
Quoted 1 line · On Mon, 13 Apr 2015 13:26:31 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

On Mon, Apr 13, 2015 at 10:02:58AM +0200, Cornelia Huck wrote:

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Also, doesn't get_features need to be modified as well so that VERSION_1 is advertised?

virtio_pci_device_plugged seems to set it ATM. I'll re-test to confirm.

Indeed, pci sets it, but ccw does not. And virtio-net, for example, sets it explicitly as well. We need to unify this :)

I'm inclined to set it in transport and black-list in specific devices.

Signature · 1 line
MST
#7 lore raw

Re: [Qemu-devel] [PATCH 2/2] virtio-balloon: virtio 1 support

Quoted 9 lines · On Mon, 13 Apr 2015 13:49:42 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 8 lines · Cornelia Huck
Quoted 5 lines · On Mon, 13 Apr 2015 13:26:31 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 2 lines · Cornelia Huck

Also, doesn't get_features need to be modified as well so that VERSION_1 is advertised?

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

virtio_pci_device_plugged seems to set it ATM. I'll re-test to confirm.

Indeed, pci sets it, but ccw does not. And virtio-net, for example, sets it explicitly as well. We need to unify this :)

I'm inclined to set it in transport and black-list in specific devices.

Agreed, let's set it in the transports.

For ccw, I'd probably only want to offer it once the driver has negotiated a revision >= 1, so something like the following (untested):

hw/s390x/virtio-ccw.c +3 -0
index 8b6b2ab..4d8cc24 100644
@@ -703,6 +703,9 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
         dev->host_features =
             virtio_bus_get_vdev_features_rev(&dev->bus, dev->host_features,
                                              dev->revision >= 1 ? 1 : 0);
+        if (dev->revision >= 1) {
+            virtio_add_feature(&dev->host_features, VIRTIO_F_VERSION_1);
+        }
         break;
     default:
         ret = -ENOSYS;
#8 lore raw

Re: [Qemu-devel] [PATCH 2/2] virtio-balloon: virtio 1 support

In reply to Michael S. Tsirkin
Quoted 3 lines · On Mon, 13 Apr 2015 13:26:31 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

BTW I suspect the stats code is broken for cross-endian platforms: it should do LE unconditinally, should it not?

Stats are guest-endian for legacy, so no. Only the config space is always LE.