qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Michael Roth <mdroth@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: aliguori@us.ibm.com, mdroth@linux.vnet.ibm.com, quintela@redhat.com
Subject: [Qemu-devel] [PATCH 11/13] virtio-net: convert save/load to visitors
Date: Thu, 27 Oct 2011 13:17:23 -0500	[thread overview]
Message-ID: <1319739445-17629-12-git-send-email-mdroth@linux.vnet.ibm.com> (raw)
In-Reply-To: <1319739445-17629-1-git-send-email-mdroth@linux.vnet.ibm.com>


Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
 hw/virtio-net.c |  160 +++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 115 insertions(+), 45 deletions(-)

diff --git a/hw/virtio-net.c b/hw/virtio-net.c
index 8c2f460..974af5c 100644
--- a/hw/virtio-net.c
+++ b/hw/virtio-net.c
@@ -862,78 +862,138 @@ static void virtio_net_tx_bh(void *opaque)
 static void virtio_net_save(QEMUFile *f, void *opaque)
 {
     VirtIONet *n = opaque;
+    Visitor *v = qemu_file_get_visitor(f);
+    Error *err = NULL;
+    uint8_t *buf;
+    int i;
 
     /* At this point, backend must be stopped, otherwise
      * it might keep writing to memory. */
     assert(!n->vhost_started);
+
+    visit_start_struct(v, NULL, NULL, "virtio_net", 0, &err);
+
     virtio_save(&n->vdev, f);
 
-    qemu_put_buffer(f, n->mac, ETH_ALEN);
-    qemu_put_be32(f, n->tx_waiting);
-    qemu_put_be32(f, n->mergeable_rx_bufs);
-    qemu_put_be16(f, n->status);
-    qemu_put_byte(f, n->promisc);
-    qemu_put_byte(f, n->allmulti);
-    qemu_put_be32(f, n->mac_table.in_use);
-    qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
-    qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
-    qemu_put_be32(f, n->has_vnet_hdr);
-    qemu_put_byte(f, n->mac_table.multi_overflow);
-    qemu_put_byte(f, n->mac_table.uni_overflow);
-    qemu_put_byte(f, n->alluni);
-    qemu_put_byte(f, n->nomulti);
-    qemu_put_byte(f, n->nouni);
-    qemu_put_byte(f, n->nobcast);
-    qemu_put_byte(f, n->has_ufo);
+    visit_start_array(v, NULL, "mac", ETH_ALEN, 1, &err);
+    for (i = 0; i < ETH_ALEN; i++) {
+        visit_type_uint8(v, &n->mac[i], NULL, &err);
+    }
+    visit_end_array(v, &err);
+    visit_type_int32(v, &n->tx_waiting, "tx_waiting", &err);
+    visit_type_int32(v, &n->mergeable_rx_bufs, "mergeable_rx_bufs", &err);
+    visit_type_uint16(v, &n->status, "status", &err);
+    visit_type_uint8(v, &n->promisc, "promisc", &err);
+    visit_type_uint8(v, &n->allmulti, "allmulti", &err);
+    visit_type_int32(v, &n->mac_table.in_use, "mac_table", &err);
+    visit_start_array(v, NULL, "mac_tables.macs",
+                      n->mac_table.in_use * ETH_ALEN, 1, &err);
+    for (i = 0; i < n->mac_table.in_use * ETH_ALEN; i++) {
+        visit_type_uint8(v, &n->mac_table.macs[i], NULL, &err);
+    }
+    visit_end_array(v, &err);
+    visit_start_array(v, NULL, "vlans", MAX_VLAN >> 3, 1, &err);
+    buf = (uint8_t *)n->vlans;
+    for (i = 0; i < MAX_VLAN >> 3; i++) {
+        visit_type_uint8(v, &buf[i], NULL, &err);
+    }
+    visit_end_array(v, &err);
+    visit_type_uint32(v, &n->has_vnet_hdr, "has_vnet_hdr", &err);
+    visit_type_uint8(v, &n->mac_table.multi_overflow,
+                     "mac_table.multi_overflow", &err);
+    visit_type_uint8(v, &n->mac_table.uni_overflow,
+                      "mac_table.uni_overflow", &err);
+    visit_type_uint8(v, &n->alluni, "alluni", &err);
+    visit_type_uint8(v, &n->nomulti, "nomulti", &err);
+    visit_type_uint8(v, &n->nouni, "nouni", &err);
+    visit_type_uint8(v, &n->nobcast, "nobcast", &err);
+    visit_type_uint8(v, &n->has_ufo, "has_ufo", &err);
+
+    visit_end_struct(v, &err);
+    if (err) {
+        error_report("error loading virtio-net state: %s", error_get_pretty(err));
+        error_free(err);
+    }
 }
 
 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
 {
     VirtIONet *n = opaque;
-    int i;
+    Visitor *v = qemu_file_get_visitor(f);
+    Error *err = NULL;
+    uint8_t *buf, tmp, has_ufo;
+    uint32_t has_vnet_hdr;
+    int i, ret = 0;
+
+    if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) {
+        ret = -EINVAL;
+        goto out;
+    }
 
-    if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
-        return -EINVAL;
+    visit_start_struct(v, NULL, NULL, "virtio_net", 0, &err);
 
     virtio_load(&n->vdev, f);
 
-    qemu_get_buffer(f, n->mac, ETH_ALEN);
-    n->tx_waiting = qemu_get_be32(f);
-    n->mergeable_rx_bufs = qemu_get_be32(f);
+    visit_start_array(v, NULL, "mac", ETH_ALEN, 1, &err);
+    for (i = 0; i < ETH_ALEN; i++) {
+        visit_type_uint8(v, &n->mac[i], NULL, &err);
+    }
+    visit_end_array(v, &err);
+    visit_type_int32(v, &n->tx_waiting, "tx_waiting", &err);
+    visit_type_int32(v, &n->mergeable_rx_bufs, "mergeable_rx_bufs", &err);
 
-    if (version_id >= 3)
-        n->status = qemu_get_be16(f);
+    if (version_id >= 3) {
+        visit_type_uint16(v, &n->status, "status", &err);
+    }
 
     if (version_id >= 4) {
         if (version_id < 8) {
-            n->promisc = qemu_get_be32(f);
-            n->allmulti = qemu_get_be32(f);
+            visit_type_uint32(v, (uint32_t *)&n->promisc, "promisc", &err);
+            visit_type_uint32(v, (uint32_t *)&n->allmulti, "allmulti", &err);
         } else {
-            n->promisc = qemu_get_byte(f);
-            n->allmulti = qemu_get_byte(f);
+            visit_type_uint8(v, (uint8_t *)&n->promisc, "promisc", &err);
+            visit_type_uint8(v, (uint8_t *)&n->allmulti, "allmulti", &err);
         }
     }
 
     if (version_id >= 5) {
-        n->mac_table.in_use = qemu_get_be32(f);
+        visit_type_int32(v, &n->mac_table.in_use, "mac_table", &err);
         /* MAC_TABLE_ENTRIES may be different from the saved image */
         if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
-            qemu_get_buffer(f, n->mac_table.macs,
-                            n->mac_table.in_use * ETH_ALEN);
+            visit_start_array(v, NULL, "mac_tables.macs",
+                              n->mac_table.in_use * ETH_ALEN, 1, &err);
+            for (i = 0; i < n->mac_table.in_use * ETH_ALEN; i++) {
+                visit_type_uint8(v, &n->mac_table.macs[i], NULL, &err);
+            }
+            visit_end_array(v, &err);
         } else if (n->mac_table.in_use) {
-            qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
+            /* dummy load */
+            visit_start_array(v, NULL, "mac_tables.macs",
+                              n->mac_table.in_use * ETH_ALEN, 1, &err);
+            for (i = 0; i < n->mac_table.in_use * ETH_ALEN; i++) {
+                visit_type_uint8(v, &tmp, NULL, &err);
+            }
+            visit_end_array(v, &err);
             n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
             n->mac_table.in_use = 0;
         }
     }
  
-    if (version_id >= 6)
-        qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
+    if (version_id >= 6) {
+        visit_start_array(v, NULL, "vlans", MAX_VLAN >> 3, 1, &err);
+        buf = (uint8_t *)n->vlans;
+        for (i = 0; i < MAX_VLAN >> 3; i++) {
+            visit_type_uint8(v, &buf[i], NULL, &err);
+        }
+        visit_end_array(v, &err);
+    }
 
     if (version_id >= 7) {
-        if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
+        visit_type_uint32(v, &has_vnet_hdr, "has_vnet_hdr", &err);
+        if (has_vnet_hdr && !peer_has_vnet_hdr(n)) {
             error_report("virtio-net: saved image requires vnet_hdr=on");
-            return -1;
+            ret = -1;
+            goto out;
         }
 
         if (n->has_vnet_hdr) {
@@ -948,24 +1008,27 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
     }
 
     if (version_id >= 9) {
-        n->mac_table.multi_overflow = qemu_get_byte(f);
-        n->mac_table.uni_overflow = qemu_get_byte(f);
+        visit_type_uint8(v, &n->mac_table.multi_overflow, "mac_table.multi_overflow", &err);
+        visit_type_uint8(v, &n->mac_table.uni_overflow, "mac_table.uni_overflow", &err);
     }
 
     if (version_id >= 10) {
-        n->alluni = qemu_get_byte(f);
-        n->nomulti = qemu_get_byte(f);
-        n->nouni = qemu_get_byte(f);
-        n->nobcast = qemu_get_byte(f);
+        visit_type_uint8(v, &n->alluni, "alluni", &err);
+        visit_type_uint8(v, &n->nomulti, "nomulti", &err);
+        visit_type_uint8(v, &n->nouni, "nouni", &err);
+        visit_type_uint8(v, &n->nobcast, "nobcast", &err);
     }
 
     if (version_id >= 11) {
-        if (qemu_get_byte(f) && !peer_has_ufo(n)) {
+        visit_type_uint8(v, &has_ufo, "has_ufo", &err);
+        if (has_ufo && !peer_has_ufo(n)) {
             error_report("virtio-net: saved image requires TUN_F_UFO support");
             return -1;
         }
     }
 
+    visit_end_struct(v, &err);
+
     /* Find the first multicast entry in the saved MAC filter */
     for (i = 0; i < n->mac_table.in_use; i++) {
         if (n->mac_table.macs[i * ETH_ALEN] & 1) {
@@ -973,7 +1036,14 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
         }
     }
     n->mac_table.first_multi = i;
-    return 0;
+
+    if (err) {
+        error_report("error loading virtio-net state: %s", error_get_pretty(err));
+        ret = -EINVAL;
+    }
+out:
+    error_free(err);
+    return ret;
 }
 
 static void virtio_net_cleanup(VLANClientState *nc)
-- 
1.7.4.1

  parent reply	other threads:[~2011-10-27 18:19 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-27 18:17 [Qemu-devel] [PATCH 00/13] Convert slirp/ivshmem/virtio save/load to Visitors Michael Roth
2011-10-27 18:17 ` [Qemu-devel] [PATCH 01/13] slirp: convert save/load function to visitor interface Michael Roth
2011-10-27 18:17 ` [Qemu-devel] [PATCH 02/13] ivshmem: convert save/load to visitor Michael Roth
2011-10-27 18:17 ` [Qemu-devel] [PATCH 03/13] virtio-pci: convert save/load to visitors Michael Roth
2011-10-27 18:17 ` [Qemu-devel] [PATCH 04/13] msix: convert save/load to visitors (including interfaces) Michael Roth
2011-10-27 18:17 ` [Qemu-devel] [PATCH 05/13] openpic: convert save/load to visitors Michael Roth
2011-10-27 18:17 ` [Qemu-devel] [PATCH 06/13] i440fx: " Michael Roth
2011-10-27 18:17 ` [Qemu-devel] [PATCH 07/13] pci: convert pci_device_(save|load) interfaces to accept Visitors Michael Roth
2011-10-27 18:17 ` [Qemu-devel] [PATCH 08/13] virtio: convert common virtio save/load to visitors Michael Roth
2011-10-27 18:17 ` [Qemu-devel] [PATCH 09/13] virtio-balloon: convert " Michael Roth
2011-10-27 18:17 ` [Qemu-devel] [PATCH 10/13] virtio-blk: " Michael Roth
2011-10-27 18:17 ` Michael Roth [this message]
2011-10-27 18:17 ` [Qemu-devel] [PATCH 12/13] virtio-serial: " Michael Roth
2011-10-27 18:17 ` [Qemu-devel] [PATCH 13/13] virtio: convert virtio_save/virtio_load interfaces to accept Visitors Michael Roth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1319739445-17629-12-git-send-email-mdroth@linux.vnet.ibm.com \
    --to=mdroth@linux.vnet.ibm.com \
    --cc=aliguori@us.ibm.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).