Patchwork [16/41] virtio: Add num_pci_queues field

login
register
mail settings
Submitter Juan Quintela
Date Dec. 2, 2009, 12:04 p.m.
Message ID <39c4db726d98ccfa1b1ed0b85caef2ee60f83971.1259754427.git.quintela@redhat.com>
Download mbox | patch
Permalink /patch/40021/
State New
Headers show

Comments

Juan Quintela - Dec. 2, 2009, 12:04 p.m.
Signed-off-by: Juan Quintela <quintela@redhat.com>
---
 hw/virtio.c |   32 +++++++++++++++++++-------------
 hw/virtio.h |    2 ++
 2 files changed, 21 insertions(+), 13 deletions(-)
Michael S. Tsirkin - Dec. 2, 2009, 2:46 p.m.
On Wed, Dec 02, 2009 at 01:04:14PM +0100, Juan Quintela wrote:
> 
> Signed-off-by: Juan Quintela <quintela@redhat.com>
> ---
>  hw/virtio.c |   32 +++++++++++++++++++-------------
>  hw/virtio.h |    2 ++
>  2 files changed, 21 insertions(+), 13 deletions(-)
> 
> diff --git a/hw/virtio.c b/hw/virtio.c
> index b565bf9..f549543 100644
> --- a/hw/virtio.c
> +++ b/hw/virtio.c
> @@ -615,10 +615,24 @@ void virtio_notify_config(VirtIODevice *vdev)
>      virtio_notify_vector(vdev, vdev->config_vector);
>  }
> 
> +static void virtio_pre_save(void *opaque)
> +{
> +    VirtIODevice *vdev = opaque;
> +    int i;
> +
> +    for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
> +        if (vdev->vq[i].vring.num == 0)
> +            break;
> +    }
> +    vdev->num_pci_queues = i;
> +}
> +
>  void virtio_save(VirtIODevice *vdev, QEMUFile *f)
>  {
>      int i;
> 
> +    virtio_pre_save(vdev);
> +
>      if (vdev->type == VIRTIO_PCI)
>          vmstate_save_state(f, &vmstate_virtio_pci_config, vdev->binding_opaque);
> 
> @@ -629,17 +643,9 @@ void virtio_save(VirtIODevice *vdev, QEMUFile *f)
>      qemu_put_be32(f, vdev->config_len);
>      qemu_put_buffer(f, vdev->config, vdev->config_len);
> 
> -    for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
> -        if (vdev->vq[i].vring.num == 0)
> -            break;
> -    }
> -
> -    qemu_put_be32(f, i);
> -
> -    for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
> -        if (vdev->vq[i].vring.num == 0)
> -            break;
> +    qemu_put_sbe32s(f, &vdev->num_pci_queues);
> 
> +    for (i = 0; i < vdev->num_pci_queues; i++) {
>          qemu_put_be32(f, vdev->vq[i].vring.num);
>          qemu_put_be64(f, vdev->vq[i].pa);
>          qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
> @@ -652,7 +658,7 @@ void virtio_save(VirtIODevice *vdev, QEMUFile *f)
> 
>  int virtio_load(VirtIODevice *vdev, QEMUFile *f)
>  {
> -    int num, i, ret;
> +    int i, ret;
> 
>      if (vdev->type == VIRTIO_PCI) {
>          ret = vmstate_load_state(f, &vmstate_virtio_pci_config, vdev->binding_opaque,
> @@ -668,9 +674,9 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f)
>      vdev->config_len = qemu_get_be32(f);
>      qemu_get_buffer(f, vdev->config, vdev->config_len);
> 
> -    num = qemu_get_be32(f);
> +    qemu_get_sbe32s(f, &vdev->num_pci_queues);
> 
> -    for (i = 0; i < num; i++) {
> +    for (i = 0; i < vdev->num_pci_queues; i++) {
>          vdev->vq[i].vring.num = qemu_get_be32(f);
>          vdev->vq[i].pa = qemu_get_be64(f);
>          qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
> diff --git a/hw/virtio.h b/hw/virtio.h
> index 91a6c10..d849f44 100644
> --- a/hw/virtio.h
> +++ b/hw/virtio.h
> @@ -111,6 +111,8 @@ struct VirtIODevice
>      const VirtIOBindings *binding;
>      void *binding_opaque;
>      uint16_t device_id;
> +    /* fields used only by vmstate */
> +    int32_t num_pci_queues;
>  };
> 
>  VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,

Hmm.
Sticking pci specific fields here looks wrong.
virtio pci proxy has nvectors which you could use.

> -- 
> 1.6.5.2

Patch

diff --git a/hw/virtio.c b/hw/virtio.c
index b565bf9..f549543 100644
--- a/hw/virtio.c
+++ b/hw/virtio.c
@@ -615,10 +615,24 @@  void virtio_notify_config(VirtIODevice *vdev)
     virtio_notify_vector(vdev, vdev->config_vector);
 }

+static void virtio_pre_save(void *opaque)
+{
+    VirtIODevice *vdev = opaque;
+    int i;
+
+    for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
+        if (vdev->vq[i].vring.num == 0)
+            break;
+    }
+    vdev->num_pci_queues = i;
+}
+
 void virtio_save(VirtIODevice *vdev, QEMUFile *f)
 {
     int i;

+    virtio_pre_save(vdev);
+
     if (vdev->type == VIRTIO_PCI)
         vmstate_save_state(f, &vmstate_virtio_pci_config, vdev->binding_opaque);

@@ -629,17 +643,9 @@  void virtio_save(VirtIODevice *vdev, QEMUFile *f)
     qemu_put_be32(f, vdev->config_len);
     qemu_put_buffer(f, vdev->config, vdev->config_len);

-    for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
-        if (vdev->vq[i].vring.num == 0)
-            break;
-    }
-
-    qemu_put_be32(f, i);
-
-    for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
-        if (vdev->vq[i].vring.num == 0)
-            break;
+    qemu_put_sbe32s(f, &vdev->num_pci_queues);

+    for (i = 0; i < vdev->num_pci_queues; i++) {
         qemu_put_be32(f, vdev->vq[i].vring.num);
         qemu_put_be64(f, vdev->vq[i].pa);
         qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
@@ -652,7 +658,7 @@  void virtio_save(VirtIODevice *vdev, QEMUFile *f)

 int virtio_load(VirtIODevice *vdev, QEMUFile *f)
 {
-    int num, i, ret;
+    int i, ret;

     if (vdev->type == VIRTIO_PCI) {
         ret = vmstate_load_state(f, &vmstate_virtio_pci_config, vdev->binding_opaque,
@@ -668,9 +674,9 @@  int virtio_load(VirtIODevice *vdev, QEMUFile *f)
     vdev->config_len = qemu_get_be32(f);
     qemu_get_buffer(f, vdev->config, vdev->config_len);

-    num = qemu_get_be32(f);
+    qemu_get_sbe32s(f, &vdev->num_pci_queues);

-    for (i = 0; i < num; i++) {
+    for (i = 0; i < vdev->num_pci_queues; i++) {
         vdev->vq[i].vring.num = qemu_get_be32(f);
         vdev->vq[i].pa = qemu_get_be64(f);
         qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
diff --git a/hw/virtio.h b/hw/virtio.h
index 91a6c10..d849f44 100644
--- a/hw/virtio.h
+++ b/hw/virtio.h
@@ -111,6 +111,8 @@  struct VirtIODevice
     const VirtIOBindings *binding;
     void *binding_opaque;
     uint16_t device_id;
+    /* fields used only by vmstate */
+    int32_t num_pci_queues;
 };

 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,