* [PATCH] Virtio network device migration support
@ 2008-03-03 12:59 Dor Laor
2008-03-03 15:27 ` Anthony Liguori
0 siblings, 1 reply; 3+ messages in thread
From: Dor Laor @ 2008-03-03 12:59 UTC (permalink / raw)
To: kvm-devel, Anthony Liguori, Avi Kivity
Virtio network device migration support
It is composed of state saving and dirty bit tracking.
Added dirty bit tracking for the rx packets.
There is no need to add dirty bits for the outgoing
packets since we do not write over guest memory.
As for the descriptor ring (guest memory), I rather
copy the entire ring (3 pages) when saving state than
touching the dirty bits every time in fast path.
Besides that the virtio device, pci bus state and
the network device states are saved.
Signed-off-by: Dor Laor <dor.laor@qumranet.com>
diff --git a/qemu/hw/virtio-net.c b/qemu/hw/virtio-net.c
index eb2a441..612cf6b 100644
--- a/qemu/hw/virtio-net.c
+++ b/qemu/hw/virtio-net.c
@@ -128,6 +128,7 @@ static void virtio_net_receive(void *opaque, const uint8_t *buf, int size)
hdr = (void *)elem.in_sg[0].iov_base;
hdr->flags = 0;
hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
+ cpu_physical_memory_set_dirty((ram_addr_t)elem.in_sg[0].iov_base - (ram_addr_t)phys_ram_base);
/* copy in packet. ugh */
offset = 0;
@@ -136,6 +137,7 @@ static void virtio_net_receive(void *opaque, const uint8_t *buf, int size)
int len = MIN(elem.in_sg[i].iov_len, size - offset);
memcpy(elem.in_sg[i].iov_base, buf + offset, len);
offset += len;
+ cpu_physical_memory_set_dirty((ram_addr_t)elem.in_sg[i].iov_base - (ram_addr_t)phys_ram_base);
i++;
}
@@ -210,6 +212,8 @@ again:
else
fprintf(stderr, "reading network error %d", len);
}
+ cpu_physical_memory_set_dirty((ram_addr_t)elem.in_sg[1].iov_base - (ram_addr_t)phys_ram_base);
+ cpu_physical_memory_set_dirty((ram_addr_t)elem.in_sg[0].iov_base - (ram_addr_t)phys_ram_base);
virtqueue_push(vnet->rx_vq, &elem, sizeof(*hdr) + len);
vnet->do_notify = 1;
}
@@ -281,11 +285,52 @@ static void virtio_net_tx_timer(void *opaque)
virtio_net_flush_tx(n, n->tx_vq);
}
+
+static void virtio_net_save(QEMUFile *f, void *opaque)
+{
+ VirtIONet *n = opaque;
+
+ pci_device_save(&n->vdev.pci_dev, f);
+ qemu_put_buffer(f, n->mac, sizeof n->mac);
+ qemu_put_be32s(f, &n->can_receive);
+
+ virtio_dev_save(f, &n->vdev);
+}
+
+static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
+{
+ VirtIONet *n = opaque;
+ int ret;
+
+ if (version_id > 1) {
+ fprintf(stderr, "%s: not supporting version > 1\n", __FUNCTION__);
+ return -1;
+ }
+
+ if ((ret = pci_device_load(&n->vdev.pci_dev, f)) < 0)
+ return ret;
+
+ qemu_get_buffer(f, n->mac, sizeof n->mac);
+ qemu_get_be32s(f, &n->can_receive);
+
+
+ if ((ret = virtio_dev_load(f, &n->vdev, version_id)) < 0)
+ return ret;
+
+ /* Make sure we kick the tx */
+ qemu_mod_timer(n->tx_timer,
+ qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
+ n->tx_timer_active = 1;
+
+ return 0;
+}
+
void *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
{
VirtIONet *n;
+ const char *info_str = "virtio-net";
- n = (VirtIONet *)virtio_init_pci(bus, "virtio-net", 6900, 0x1000,
+ n = (VirtIONet *)virtio_init_pci(bus, info_str, 6900, 0x1000,
0, VIRTIO_ID_NET,
0x02, 0x00, 0x00,
6, sizeof(VirtIONet));
@@ -308,5 +353,11 @@ void *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
n->tx_timer_active = 0;
+ snprintf(n->vc->info_str, sizeof(n->vc->info_str),
+ "%s macaddr=%02x:%02x:%02x:%02x:%02x:%02x", info_str,
+ nd->macaddr[0], nd->macaddr[1], nd->macaddr[2],
+ nd->macaddr[3], nd->macaddr[4], nd->macaddr[5]);
+ register_savevm(info_str, 1, 1, virtio_net_save, virtio_net_load, n);
+
return &n->vdev;
}
diff --git a/qemu/hw/virtio.c b/qemu/hw/virtio.c
index 634f869..69fe810 100644
--- a/qemu/hw/virtio.c
+++ b/qemu/hw/virtio.c
@@ -180,6 +180,59 @@ void virtio_reset(void *opaque)
}
}
+void virtio_dev_save(QEMUFile *f, VirtIODevice *vdev)
+{
+ int i;
+
+ qemu_put_be32s(f, &vdev->features);
+ qemu_put_be16s(f, &vdev->queue_sel);
+ qemu_put_8s(f, &vdev->status);
+ qemu_put_8s(f, &vdev->isr);
+
+ for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
+ if (!vdev->vq[i].vring.num)
+ continue;
+ qemu_put_be32s(f, &vdev->vq[i].pfn);
+ qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
+ qemu_put_be32s(f, &vdev->vq[i].index);
+
+ /* Save the descriptor ring instead of constantly mark them dirty */
+ qemu_put_buffer(f, (uint8_t*)vdev->vq[i].vring.desc, vdev->vq[i].vring.num * sizeof(VRingDesc));
+ qemu_put_buffer(f, (uint8_t*)vdev->vq[i].vring.avail, TARGET_PAGE_SIZE);
+ qemu_put_buffer(f, (uint8_t*)vdev->vq[i].vring.used, TARGET_PAGE_SIZE);
+ }
+}
+
+int virtio_dev_load(QEMUFile *f, VirtIODevice *vdev, int version_id)
+{
+ int i;
+
+ if (version_id > 1) {
+ fprintf(stderr, "%s: not supporting version > 1\n", __FUNCTION__);
+ return -1;
+ }
+
+ qemu_get_be32s(f, &vdev->features);
+ qemu_get_be16s(f, &vdev->queue_sel);
+ qemu_get_8s(f, &vdev->status);
+ qemu_get_8s(f, &vdev->isr);
+
+ for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
+ if (!vdev->vq[i].vring.num)
+ continue;
+ qemu_get_be32s(f, &vdev->vq[i].pfn);
+ qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
+ qemu_get_be32s(f, &vdev->vq[i].index);
+ virtqueue_init(&vdev->vq[i], phys_ram_base +
+ (vdev->vq[i].pfn << TARGET_PAGE_BITS));
+ qemu_get_buffer(f, (uint8_t*)vdev->vq[i].vring.desc, vdev->vq[i].vring.num * sizeof(VRingDesc));
+ qemu_get_buffer(f, (uint8_t*)vdev->vq[i].vring.avail, TARGET_PAGE_SIZE);
+ qemu_get_buffer(f, (uint8_t*)vdev->vq[i].vring.used, TARGET_PAGE_SIZE);
+ }
+
+ return 0;
+}
+
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
{
VirtIODevice *vdev = to_virtio_device(opaque);
diff --git a/qemu/hw/virtio.h b/qemu/hw/virtio.h
index dee97ba..54632ba 100644
--- a/qemu/hw/virtio.h
+++ b/qemu/hw/virtio.h
@@ -140,4 +140,7 @@ int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem);
void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
+void virtio_dev_save(QEMUFile *f, VirtIODevice *vdev);
+int virtio_dev_load(QEMUFile *f, VirtIODevice *vdev, int version_id);
+
#endif
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] Virtio network device migration support
2008-03-03 12:59 [PATCH] Virtio network device migration support Dor Laor
@ 2008-03-03 15:27 ` Anthony Liguori
2008-03-04 15:32 ` Dor Laor
0 siblings, 1 reply; 3+ messages in thread
From: Anthony Liguori @ 2008-03-03 15:27 UTC (permalink / raw)
To: dor.laor; +Cc: kvm-devel, Avi Kivity
Hi Dor,
Dor Laor wrote:
> void *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
> {
> VirtIONet *n;
> + const char *info_str = "virtio-net";
>
> - n = (VirtIONet *)virtio_init_pci(bus, "virtio-net", 6900, 0x1000,
> + n = (VirtIONet *)virtio_init_pci(bus, info_str, 6900, 0x1000,
> 0, VIRTIO_ID_NET,
> 0x02, 0x00, 0x00,
> 6, sizeof(VirtIONet));
> @@ -308,5 +353,11 @@ void *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
> n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
> n->tx_timer_active = 0;
>
> + snprintf(n->vc->info_str, sizeof(n->vc->info_str),
> + "%s macaddr=%02x:%02x:%02x:%02x:%02x:%02x", info_str,
> + nd->macaddr[0], nd->macaddr[1], nd->macaddr[2],
> + nd->macaddr[3], nd->macaddr[4], nd->macaddr[5]);
> + register_savevm(info_str, 1, 1, virtio_net_save, virtio_net_load, n);
>
I think we need to maintain an instance id and increment it here like we
do for the rest of the network cards.
> return &n->vdev;
> }
> diff --git a/qemu/hw/virtio.c b/qemu/hw/virtio.c
> index 634f869..69fe810 100644
> --- a/qemu/hw/virtio.c
> +++ b/qemu/hw/virtio.c
> @@ -180,6 +180,59 @@ void virtio_reset(void *opaque)
> }
> }
>
> +void virtio_dev_save(QEMUFile *f, VirtIODevice *vdev)
> +{
> + int i;
> +
> + qemu_put_be32s(f, &vdev->features);
> + qemu_put_be16s(f, &vdev->queue_sel);
> + qemu_put_8s(f, &vdev->status);
> + qemu_put_8s(f, &vdev->isr);
> +
> + for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
> + if (!vdev->vq[i].vring.num)
> + continue;
> + qemu_put_be32s(f, &vdev->vq[i].pfn);
> + qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
> + qemu_put_be32s(f, &vdev->vq[i].index);
> +
> + /* Save the descriptor ring instead of constantly mark them dirty */
> + qemu_put_buffer(f, (uint8_t*)vdev->vq[i].vring.desc, vdev->vq[i].vring.num * sizeof(VRingDesc));
> + qemu_put_buffer(f, (uint8_t*)vdev->vq[i].vring.avail, TARGET_PAGE_SIZE);
> + qemu_put_buffer(f, (uint8_t*)vdev->vq[i].vring.used, TARGET_PAGE_SIZE);
>
I think these two need to be sizeof(VRingAvail) * vring.num and
sizeof(VringUsed) * vring.num
Regards,
Anthony Liguori
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Virtio network device migration support
2008-03-03 15:27 ` Anthony Liguori
@ 2008-03-04 15:32 ` Dor Laor
0 siblings, 0 replies; 3+ messages in thread
From: Dor Laor @ 2008-03-04 15:32 UTC (permalink / raw)
To: Anthony Liguori; +Cc: kvm-devel, Avi Kivity
On Mon, 2008-03-03 at 09:27 -0600, Anthony Liguori wrote:
> Hi Dor,
>
> Dor Laor wrote:
> > void *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
> > {
> > VirtIONet *n;
> > + const char *info_str = "virtio-net";
> >
> > - n = (VirtIONet *)virtio_init_pci(bus, "virtio-net", 6900, 0x1000,
> > + n = (VirtIONet *)virtio_init_pci(bus, info_str, 6900, 0x1000,
> > 0, VIRTIO_ID_NET,
> > 0x02, 0x00, 0x00,
> > 6, sizeof(VirtIONet));
> > @@ -308,5 +353,11 @@ void *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
> > n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
> > n->tx_timer_active = 0;
> >
> > + snprintf(n->vc->info_str, sizeof(n->vc->info_str),
> > + "%s macaddr=%02x:%02x:%02x:%02x:%02x:%02x", info_str,
> > + nd->macaddr[0], nd->macaddr[1], nd->macaddr[2],
> > + nd->macaddr[3], nd->macaddr[4], nd->macaddr[5]);
> > + register_savevm(info_str, 1, 1, virtio_net_save, virtio_net_load, n);
> >
>
> I think we need to maintain an instance id and increment it here like we
> do for the rest of the network cards.
>
Sure
> > return &n->vdev;
> > }
> > diff --git a/qemu/hw/virtio.c b/qemu/hw/virtio.c
> > index 634f869..69fe810 100644
> > --- a/qemu/hw/virtio.c
> > +++ b/qemu/hw/virtio.c
> > @@ -180,6 +180,59 @@ void virtio_reset(void *opaque)
> > }
> > }
> >
> > +void virtio_dev_save(QEMUFile *f, VirtIODevice *vdev)
> > +{
> > + int i;
> > +
> > + qemu_put_be32s(f, &vdev->features);
> > + qemu_put_be16s(f, &vdev->queue_sel);
> > + qemu_put_8s(f, &vdev->status);
> > + qemu_put_8s(f, &vdev->isr);
> > +
> > + for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
> > + if (!vdev->vq[i].vring.num)
> > + continue;
> > + qemu_put_be32s(f, &vdev->vq[i].pfn);
> > + qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
> > + qemu_put_be32s(f, &vdev->vq[i].index);
> > +
> > + /* Save the descriptor ring instead of constantly mark them dirty */
> > + qemu_put_buffer(f, (uint8_t*)vdev->vq[i].vring.desc, vdev->vq[i].vring.num * sizeof(VRingDesc));
> > + qemu_put_buffer(f, (uint8_t*)vdev->vq[i].vring.avail, TARGET_PAGE_SIZE);
> > + qemu_put_buffer(f, (uint8_t*)vdev->vq[i].vring.used, TARGET_PAGE_SIZE);
> >
>
> I think these two need to be sizeof(VRingAvail) * vring.num and
> sizeof(VringUsed) * vring.num
>
Right, actually it should be sizeof(VRingAvail) + 2 * vring.num and
sizeof(VRingUsedElem) * vring.num + sizeof(VRingUsed) respectively.
Thanks,
Dor
> Regards,
>
> Anthony Liguori
>
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-03-04 15:32 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-03-03 12:59 [PATCH] Virtio network device migration support Dor Laor
2008-03-03 15:27 ` Anthony Liguori
2008-03-04 15:32 ` Dor Laor
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox