Skip to contentSkip to conversation

virtio-net: announce self by guest

PATCH In review Jason Wang 2 participants 9 messages May 15, 2014 – May 20, 2014

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 virtio-net: announce self by guest

It's hard to track all mac addresses and their configurations (e.g vlan or ipv6) in qemu. Without those informations, it's impossible to build proper garp packet after migration. The only possible solution to this is let guest (who knew all configurations) to do this.

So, this patch introduces a new readonly config status bit of virtio-net, VIRTIO_NET_S_ANNOUNCE which is used to notify guest to announce presence of its link through config update interrupt.When guest has done the announcement, it should ack the notification through VIRTIO_NET_CTRL_ANNOUNCE_ACK cmd. This feature is negotiated by a new feature bit VIRTIO_NET_F_ANNOUNCE (which has already been supported by Linux guest).

During load, a counter of announcing rounds were set so that the after the vm is running it can trigger rounds of config interrupts to notify the guest to build and send the correct garps.

Tested with ping to guest with vlan during migration. Without the patch, lots of the packets were lost after migration. With the patch, could not notice packet loss after migration.

Reference:

RFC v2: https://lists.gnu.org/archive/html/qemu-devel/2014-04/msg01750.html
RFC v1: https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg02648.html
V7:     https://lists.gnu.org/archive/html/qemu-devel/2013-03/msg01127.html

Changes from RFC v2:
- use QEMU_CLOCK_VIRTUAL instead of QEMU_CLOCK_REALTIME
- compat self announce for 2.0 machine type

Changes from RFC v1:
- clean VIRTIO_NET_S_ANNOUNCE bit during reset
- free announce timer during clean
- make announce work for non-vhost case

Changes from V7:
- Instead of introducing a global method for each kind of nic, this version limits the changes to virtio-net itself.

---
 hw/net/virtio-net.c            |   48 ++++++++++++++++++++++++++++++++++++++++
 include/hw/i386/pc.h           |    5 ++++
 include/hw/virtio/virtio-net.h |   16 +++++++++++++
 3 files changed, 69 insertions(+), 0 deletions(-)
hw/net/virtio-net.c +48 -0
index 940a7cf..98d59e9 100644
@@ -25,6 +25,7 @@
 #include "monitor/monitor.h"
 
 #define VIRTIO_NET_VM_VERSION    11
+#define VIRTIO_NET_ANNOUNCE_ROUNDS    3
 
 #define MAC_TABLE_ENTRIES    64
 #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */
@@ -99,6 +100,25 @@ static bool virtio_net_started(VirtIONet *n, uint8_t status)
         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
 }
 
+static void virtio_net_announce_timer(void *opaque)
+{
+    VirtIONet *n = opaque;
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    if (n->announce &&
+        virtio_net_started(n, vdev->status) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_GUEST_ANNOUNCE) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_CTRL_VQ)) {
+
+        n->announce--;
+        n->status |= VIRTIO_NET_S_ANNOUNCE;
+        virtio_notify_config(vdev);
+    } else {
+        timer_del(n->announce_timer);
+        n->announce = 0;
+    }
+}
+
 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -147,6 +167,8 @@ static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
 
     virtio_net_vhost_status(n, status);
 
+    virtio_net_announce_timer(n);
+
     for (i = 0; i < n->max_queues; i++) {
         q = &n->vqs[i];
 
@@ -322,6 +344,9 @@ static void virtio_net_reset(VirtIODevice *vdev)
     n->nobcast = 0;
     /* multiqueue is disabled by default */
     n->curr_queues = 1;
+    timer_del(n->announce_timer);
+    n->announce = 0;
+    n->status &= ~VIRTIO_NET_S_ANNOUNCE;
 
     /* Flush any MAC and VLAN filter table state */
     n->mac_table.in_use = 0;
@@ -731,6 +756,22 @@ static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
     return VIRTIO_NET_OK;
 }
 
+static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
+                                      struct iovec *iov, unsigned int iov_cnt)
+{
+    if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK) {
+        n->status &= ~VIRTIO_NET_S_ANNOUNCE;
+        if (n->announce) {
+            timer_mod(n->announce_timer,
+                      qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 50 +
+                      (VIRTIO_NET_ANNOUNCE_ROUNDS - n->announce - 1) * 100);
+        }
+        return VIRTIO_NET_OK;
+    } else {
+        return VIRTIO_NET_ERR;
+    }
+}
+
 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
                                 struct iovec *iov, unsigned int iov_cnt)
 {
@@ -794,6 +835,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
+        } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
+            status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
@@ -1451,6 +1494,7 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
         qemu_get_subqueue(n->nic, i)->link_down = link_down;
     }
 
+    n->announce = VIRTIO_NET_ANNOUNCE_ROUNDS;
     return 0;
 }
 
@@ -1562,6 +1606,8 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
     n->status = VIRTIO_NET_S_LINK_UP;
+    n->announce_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                     virtio_net_announce_timer, n);
 
     if (n->netclient_type) {
         /*
@@ -1642,6 +1688,8 @@ static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
         }
     }
 
+    timer_del(n->announce_timer);
+    timer_free(n->announce_timer);
     g_free(n->vqs);
     qemu_del_nic(n->nic);
     virtio_cleanup(vdev);
include/hw/i386/pc.h +5 -0
index 32a7687..f93b427 100644
@@ -271,6 +271,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
             .driver   = "apic",\
             .property = "version",\
             .value    = stringify(0x11),\
+        },\
+        {\
+            .driver   = "virtio-net-pci",\
+            .property = "guest_announce",\
+            .value    = "off",\
         }
 
 #define PC_COMPAT_1_7 \
include/hw/virtio/virtio-net.h +16 -0
index 4b32440..ca1a9c5 100644
@@ -49,12 +49,14 @@
 #define VIRTIO_NET_F_CTRL_RX    18      /* Control channel RX mode support */
 #define VIRTIO_NET_F_CTRL_VLAN  19      /* Control channel VLAN filtering */
 #define VIRTIO_NET_F_CTRL_RX_EXTRA 20   /* Extra RX mode control support */
+#define VIRTIO_NET_F_GUEST_ANNOUNCE 21  /* Guest can announce itself */
 #define VIRTIO_NET_F_MQ         22      /* Device supports Receive Flow
                                          * Steering */
 
 #define VIRTIO_NET_F_CTRL_MAC_ADDR   23 /* Set MAC address */
 
 #define VIRTIO_NET_S_LINK_UP    1       /* Link is up */
+#define VIRTIO_NET_S_ANNOUNCE   2       /* Announcement is needed */
 
 #define TX_TIMER_INTERVAL 150000 /* 150 us */
 
@@ -193,6 +195,8 @@ typedef struct VirtIONet {
     char *netclient_name;
     char *netclient_type;
     uint64_t curr_guest_offloads;
+    QEMUTimer *announce_timer;
+    int announce;
 } VirtIONet;
 
 #define VIRTIO_NET_CTRL_MAC    1
@@ -213,6 +217,17 @@ typedef struct VirtIONet {
  #define VIRTIO_NET_CTRL_VLAN_DEL             1
 
 /*
+ * Control link announce acknowledgement
+ *
+ * The command VIRTIO_NET_CTRL_ANNOUNCE_ACK is used to indicate that
+ * driver has recevied the notification and device would clear the
+ * VIRTIO_NET_S_ANNOUNCE bit in the status filed after it received
+ * this command.
+ */
+#define VIRTIO_NET_CTRL_ANNOUNCE       3
+ #define VIRTIO_NET_CTRL_ANNOUNCE_ACK         0
+
+/*
  * Control Multiqueue
  *
  * The command VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
@@ -251,6 +266,7 @@ struct virtio_net_ctrl_mq {
         DEFINE_PROP_BIT("guest_tso6", _state, _field, VIRTIO_NET_F_GUEST_TSO6, true), \
         DEFINE_PROP_BIT("guest_ecn", _state, _field, VIRTIO_NET_F_GUEST_ECN, true), \
         DEFINE_PROP_BIT("guest_ufo", _state, _field, VIRTIO_NET_F_GUEST_UFO, true), \
+        DEFINE_PROP_BIT("guest_announce", _state, _field, VIRTIO_NET_F_GUEST_ANNOUNCE, true), \
         DEFINE_PROP_BIT("host_tso4", _state, _field, VIRTIO_NET_F_HOST_TSO4, true), \
         DEFINE_PROP_BIT("host_tso6", _state, _field, VIRTIO_NET_F_HOST_TSO6, true), \
         DEFINE_PROP_BIT("host_ecn", _state, _field, VIRTIO_NET_F_HOST_ECN, true), \
Signature · 1 line
1.7.1
#1 lore raw

Thanks, looks good.
Some minor comments below,

Quoted 2 lines · Jason Wang

It's hard to track all mac addresses and their configurations (e.g vlan or ipv6) in qemu. Without those informations, it's impossible to

s/those informations/this information/

Quoted 2 lines

build proper garp packet after migration. The only possible solution to this is let guest (who knew all configurations) to do this.

s/knew/knows/

Quoted 10 lines

So, this patch introduces a new readonly config status bit of virtio-net, VIRTIO_NET_S_ANNOUNCE which is used to notify guest to announce presence of its link through config update interrupt.When guest has done the announcement, it should ack the notification through VIRTIO_NET_CTRL_ANNOUNCE_ACK cmd. This feature is negotiated by a new feature bit VIRTIO_NET_F_ANNOUNCE (which has already been supported by Linux guest).

During load, a counter of announcing rounds were set so that the after

s/were/is/
s/the after/after/

Quoted 6 lines

the vm is running it can trigger rounds of config interrupts to notify the guest to build and send the correct garps.

Tested with ping to guest with vlan during migration. Without the patch, lots of the packets were lost after migration. With the patch, could not notice packet loss after migration.

below changelog should go after ---, until the ack list.

Quoted 33 lines

Reference:

RFC v2: https://lists.gnu.org/archive/html/qemu-devel/2014-04/msg01750.html
RFC v1: https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg02648.html
V7:     https://lists.gnu.org/archive/html/qemu-devel/2013-03/msg01127.html

Changes from RFC v2:
- use QEMU_CLOCK_VIRTUAL instead of QEMU_CLOCK_REALTIME
- compat self announce for 2.0 machine type

Changes from RFC v1:
- clean VIRTIO_NET_S_ANNOUNCE bit during reset
- free announce timer during clean
- make announce work for non-vhost case

Changes from V7:
- Instead of introducing a global method for each kind of nic, this version limits the changes to virtio-net itself.

---
 hw/net/virtio-net.c            |   48 ++++++++++++++++++++++++++++++++++++++++
 include/hw/i386/pc.h           |    5 ++++
 include/hw/virtio/virtio-net.h |   16 +++++++++++++
 3 files changed, 69 insertions(+), 0 deletions(-)
hw/net/virtio-net.c +0 -0
index 940a7cf..98d59e9 100644

hw/net/virtio-net.c line 31

Quoted 7 lines
hw/net/virtio-net.c +1 -0
@@ -25,6 +25,7 @@
 #include "monitor/monitor.h"
 
 #define VIRTIO_NET_VM_VERSION    11
+#define VIRTIO_NET_ANNOUNCE_ROUNDS    3
 
 #define MAC_TABLE_ENTRIES    64
 #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */

I would make it 5 to be consistent with SELF_ANNOUNCE_ROUNDS in savevm.c

in fact, let's move SELF_ANNOUNCE_ROUNDS to include/migration/vmstate.h and reuse it.

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 108

Quoted 10 lines
hw/net/virtio-net.c +6 -0
@@ -99,6 +100,25 @@ static bool virtio_net_started(VirtIONet *n, uint8_t status)
         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
 }
 
+static void virtio_net_announce_timer(void *opaque)
+{
+    VirtIONet *n = opaque;
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    if (n->announce &&

I would make it > 0 here, just in case it becomes negative as a result of some bug.

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 117

Quoted 9 lines
hw/net/virtio-net.c +9 -0
+        virtio_net_started(n, vdev->status) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_GUEST_ANNOUNCE) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_CTRL_VQ)) {
+
+        n->announce--;
+        n->status |= VIRTIO_NET_S_ANNOUNCE;
+        virtio_notify_config(vdev);
+    } else {
+        timer_del(n->announce_timer);

why do this here?

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 118

Quoted 1 line
hw/net/virtio-net.c +1 -0
+        n->announce = 0;

why is this here?

hw/net/virtio-net.c static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) · line 171

Quoted 12 lines
hw/net/virtio-net.c +5 -0
+    }
+}
+
 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -147,6 +167,8 @@ static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
 
     virtio_net_vhost_status(n, status);
 
+    virtio_net_announce_timer(n);
+

why do this here? why not right after we set announce counter?

Quoted 14 lines
hw/net/virtio-net.c +3 -0
     for (i = 0; i < n->max_queues; i++) {
         q = &n->vqs[i];
 
@@ -322,6 +344,9 @@ static void virtio_net_reset(VirtIODevice *vdev)
     n->nobcast = 0;
     /* multiqueue is disabled by default */
     n->curr_queues = 1;
+    timer_del(n->announce_timer);
+    n->announce = 0;
+    n->status &= ~VIRTIO_NET_S_ANNOUNCE;
 
     /* Flush any MAC and VLAN filter table state */
     n->mac_table.in_use = 0;

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 764

Quoted 9 lines
hw/net/virtio-net.c +6 -0
@@ -731,6 +756,22 @@ static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
     return VIRTIO_NET_OK;
 }
 
+static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
+                                      struct iovec *iov, unsigned int iov_cnt)
+{
+    if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK) {
+        n->status &= ~VIRTIO_NET_S_ANNOUNCE;
+        if (n->announce) {

I would make it > 0 here, just in case it becomes negative as a result of some bug.

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 767

Quoted 3 lines
hw/net/virtio-net.c +3 -0
+            timer_mod(n->announce_timer,
+                      qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 50 +
+                      (VIRTIO_NET_ANNOUNCE_ROUNDS - n->announce - 1) * 100);

savevm.c has this code, and a comment:

        /* delay 50ms, 150ms, 250ms, ... */
	50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100

how about an API in include/migration/vmstate.h

static inline
int64_t self_announce_delay(int round)

{
	assert(round < SELF_ANNOUNCE_ROUNDS && round > 0);
        /* delay 50ms, 150ms, 250ms, ... */
	return 50 + (SELF_ANNOUNCE_ROUNDS - round - 1) * 100;
}

or something to this end.

Quoted 20 lines
hw/net/virtio-net.c +9 -0
+        }
+        return VIRTIO_NET_OK;
+    } else {
+        return VIRTIO_NET_ERR;
+    }
+}
+
 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
                                 struct iovec *iov, unsigned int iov_cnt)
 {
@@ -794,6 +835,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
+        } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
+            status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {

hw/net/virtio-net.c static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) · line 1497

Quoted 4 lines
hw/net/virtio-net.c +1 -0
@@ -1451,6 +1494,7 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
         qemu_get_subqueue(n->nic, i)->link_down = link_down;
     }
 
+    n->announce = VIRTIO_NET_ANNOUNCE_ROUNDS;

Well if virtio_net_handle_announce runs now it will start timer in the past, right?
This seems wrong.

Quoted 57 lines
hw/net/virtio-net.c +4 -0
     return 0;
 }
 
@@ -1562,6 +1606,8 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
     n->status = VIRTIO_NET_S_LINK_UP;
+    n->announce_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                     virtio_net_announce_timer, n);
 
     if (n->netclient_type) {
         /*
@@ -1642,6 +1688,8 @@ static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
         }
     }
 
+    timer_del(n->announce_timer);
+    timer_free(n->announce_timer);
     g_free(n->vqs);
     qemu_del_nic(n->nic);
     virtio_cleanup(vdev);
include/hw/i386/pc.h +5 -0
index 32a7687..f93b427 100644
@@ -271,6 +271,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
             .driver   = "apic",\
             .property = "version",\
             .value    = stringify(0x11),\
+        },\
+        {\
+            .driver   = "virtio-net-pci",\
+            .property = "guest_announce",\
+            .value    = "off",\
         }
 
 #define PC_COMPAT_1_7 \
include/hw/virtio/virtio-net.h +2 -0
index 4b32440..ca1a9c5 100644
@@ -49,12 +49,14 @@
 #define VIRTIO_NET_F_CTRL_RX    18      /* Control channel RX mode support */
 #define VIRTIO_NET_F_CTRL_VLAN  19      /* Control channel VLAN filtering */
 #define VIRTIO_NET_F_CTRL_RX_EXTRA 20   /* Extra RX mode control support */
+#define VIRTIO_NET_F_GUEST_ANNOUNCE 21  /* Guest can announce itself */
 #define VIRTIO_NET_F_MQ         22      /* Device supports Receive Flow
                                          * Steering */
 
 #define VIRTIO_NET_F_CTRL_MAC_ADDR   23 /* Set MAC address */
 
 #define VIRTIO_NET_S_LINK_UP    1       /* Link is up */
+#define VIRTIO_NET_S_ANNOUNCE   2       /* Announcement is needed */
 
 #define TX_TIMER_INTERVAL 150000 /* 150 us */
 

include/hw/virtio/virtio-net.h typedef struct VirtIONet { · line 199

Quoted 5 lines
include/hw/virtio/virtio-net.h +2 -0
@@ -193,6 +195,8 @@ typedef struct VirtIONet {
     char *netclient_name;
     char *netclient_type;
     uint64_t curr_guest_offloads;
+    QEMUTimer *announce_timer;
+    int announce;

rename announce_counter pls.

include/hw/virtio/virtio-net.h typedef struct VirtIONet { · line 221

Quoted 9 lines
include/hw/virtio/virtio-net.h +2 -0
 } VirtIONet;
 
 #define VIRTIO_NET_CTRL_MAC    1
@@ -213,6 +217,17 @@ typedef struct VirtIONet {
  #define VIRTIO_NET_CTRL_VLAN_DEL             1
 
 /*
+ * Control link announce acknowledgement
+ *

bunch of typos and vagueness below

include/hw/virtio/virtio-net.h typedef struct VirtIONet { · line 225

Quoted 4 lines
include/hw/virtio/virtio-net.h +4 -0
+ * The command VIRTIO_NET_CTRL_ANNOUNCE_ACK is used to indicate that
+ * driver has recevied the notification and device would clear the
+ * VIRTIO_NET_S_ANNOUNCE bit in the status filed after it received
+ * this command.

fixed version:

VIRTIO_NET_S_ANNOUNCE bit in the status field requests link announcement from guest driver. The driver is notified by config space change interrupt. The command VIRTIO_NET_CTRL_ANNOUNCE_ACK is used to indicate that the driver has received the notification. It makes the device clear the bit VIRTIO_NET_S_ANNOUNCE in the status field.

Quoted 18 lines
include/hw/virtio/virtio-net.h +6 -0
+ */
+#define VIRTIO_NET_CTRL_ANNOUNCE       3
+ #define VIRTIO_NET_CTRL_ANNOUNCE_ACK         0
+
+/*
  * Control Multiqueue
  *
  * The command VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
@@ -251,6 +266,7 @@ struct virtio_net_ctrl_mq {
         DEFINE_PROP_BIT("guest_tso6", _state, _field, VIRTIO_NET_F_GUEST_TSO6, true), \
         DEFINE_PROP_BIT("guest_ecn", _state, _field, VIRTIO_NET_F_GUEST_ECN, true), \
         DEFINE_PROP_BIT("guest_ufo", _state, _field, VIRTIO_NET_F_GUEST_UFO, true), \
+        DEFINE_PROP_BIT("guest_announce", _state, _field, VIRTIO_NET_F_GUEST_ANNOUNCE, true), \
         DEFINE_PROP_BIT("host_tso4", _state, _field, VIRTIO_NET_F_HOST_TSO4, true), \
         DEFINE_PROP_BIT("host_tso6", _state, _field, VIRTIO_NET_F_HOST_TSO6, true), \
         DEFINE_PROP_BIT("host_ecn", _state, _field, VIRTIO_NET_F_HOST_ECN, true), \
Signature · 1 line
1.7.1
author
#2 lore raw
Quoted 20 lines · Michael S. Tsirkin

Thanks, looks good.
Some minor comments below,

Quoted 2 lines · Jason Wang

It's hard to track all mac addresses and their configurations (e.g vlan or ipv6) in qemu. Without those informations, it's impossible to

s/those informations/this information/

Quoted 2 lines

build proper garp packet after migration. The only possible solution to this is let guest (who knew all configurations) to do this.

s/knew/knows/

Quoted 8 lines

So, this patch introduces a new readonly config status bit of virtio-net, VIRTIO_NET_S_ANNOUNCE which is used to notify guest to announce presence of its link through config update interrupt.When guest has done the announcement, it should ack the notification through VIRTIO_NET_CTRL_ANNOUNCE_ACK cmd. This feature is negotiated by a new feature bit VIRTIO_NET_F_ANNOUNCE (which has already been supported by Linux guest).

Quoted 3 lines
Quoted 1 line

During load, a counter of announcing rounds were set so that the after

s/were/is/
s/the after/after/

Will correct those typos.

Quoted 8 lines
Quoted 6 lines

the vm is running it can trigger rounds of config interrupts to notify the guest to build and send the correct garps.

Tested with ping to guest with vlan during migration. Without the patch, lots of the packets were lost after migration. With the patch, could not notice packet loss after migration.

below changelog should go after ---, until the ack list.

Ok.

Quoted 37 lines
Quoted 37 lines

Reference:

RFC v2: https://lists.gnu.org/archive/html/qemu-devel/2014-04/msg01750.html
RFC v1: https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg02648.html
V7:     https://lists.gnu.org/archive/html/qemu-devel/2013-03/msg01127.html

Changes from RFC v2:
- use QEMU_CLOCK_VIRTUAL instead of QEMU_CLOCK_REALTIME
- compat self announce for 2.0 machine type

Changes from RFC v1:
- clean VIRTIO_NET_S_ANNOUNCE bit during reset
- free announce timer during clean
- make announce work for non-vhost case

Changes from V7:
- Instead of introducing a global method for each kind of nic, this version limits the changes to virtio-net itself.

---
 hw/net/virtio-net.c            |   48 ++++++++++++++++++++++++++++++++++++++++
 include/hw/i386/pc.h           |    5 ++++
 include/hw/virtio/virtio-net.h |   16 +++++++++++++
 3 files changed, 69 insertions(+), 0 deletions(-)
hw/net/virtio-net.c +1 -0
index 940a7cf..98d59e9 100644
@@ -25,6 +25,7 @@
 #include "monitor/monitor.h"
 
 #define VIRTIO_NET_VM_VERSION    11
+#define VIRTIO_NET_ANNOUNCE_ROUNDS    3
 
Quoted 7 lines

hw/net/virtio-net.c line 31

Quoted 2 lines
hw/net/virtio-net.c +0 -0
 #define MAC_TABLE_ENTRIES    64
 #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */

I would make it 5 to be consistent with SELF_ANNOUNCE_ROUNDS in savevm.c

in fact, let's move SELF_ANNOUNCE_ROUNDS to include/migration/vmstate.h and reuse it.

Ok.

Quoted 4 lines
Quoted 4 lines
hw/net/virtio-net.c +0 -0
@@ -99,6 +100,25 @@ static bool virtio_net_started(VirtIONet *n, uint8_t status)
         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
 }
 
Quoted 8 lines

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 108

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+static void virtio_net_announce_timer(void *opaque)
+{
+    VirtIONet *n = opaque;
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    if (n->announce &&

I would make it > 0 here, just in case it becomes negative as a result of some bug.

Sure.

Quoted 9 lines

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 117

Quoted 9 lines
hw/net/virtio-net.c +9 -0
+        virtio_net_started(n, vdev->status) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_GUEST_ANNOUNCE) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_CTRL_VQ)) {
+
+        n->announce--;
+        n->status |= VIRTIO_NET_S_ANNOUNCE;
+        virtio_notify_config(vdev);
+    } else {
+        timer_del(n->announce_timer);
Quoted 5 lines

why do this here?

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 118

Quoted 1 line
hw/net/virtio-net.c +1 -0
+        n->announce = 0;

why is this here?

If guest shuts down the device, there's no need to do the announcing.

Quoted 8 lines
Quoted 8 lines
hw/net/virtio-net.c +3 -0
+    }
+}
+
 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -147,6 +167,8 @@ static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
 
Quoted 5 lines

hw/net/virtio-net.c static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) · line 171

Quoted 4 lines
hw/net/virtio-net.c +2 -0
     virtio_net_vhost_status(n, status);
 
+    virtio_net_announce_timer(n);
+

why do this here? why not right after we set announce counter?

The reasons are:

- The counters were set in load, but the device is not running so we could not inject the interrupt at that time.
- We can stop the progress when guest is shutting down the device.

Quoted 18 lines
Quoted 18 lines
hw/net/virtio-net.c +3 -0
     for (i = 0; i < n->max_queues; i++) {
         q = &n->vqs[i];
 
@@ -322,6 +344,9 @@ static void virtio_net_reset(VirtIODevice *vdev)
     n->nobcast = 0;
     /* multiqueue is disabled by default */
     n->curr_queues = 1;
+    timer_del(n->announce_timer);
+    n->announce = 0;
+    n->status &= ~VIRTIO_NET_S_ANNOUNCE;
 
     /* Flush any MAC and VLAN filter table state */
     n->mac_table.in_use = 0;
@@ -731,6 +756,22 @@ static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
     return VIRTIO_NET_OK;
 }
 
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 764

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
+                                      struct iovec *iov, unsigned int iov_cnt)
+{
+    if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK) {
+        n->status &= ~VIRTIO_NET_S_ANNOUNCE;
+        if (n->announce) {

I would make it > 0 here, just in case it becomes negative as a result of some bug.

Ok.

Quoted 11 lines

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 767

Quoted 3 lines
hw/net/virtio-net.c +3 -0
+            timer_mod(n->announce_timer,
+                      qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 50 +
+                      (VIRTIO_NET_ANNOUNCE_ROUNDS - n->announce - 1) * 100);

savevm.c has this code, and a comment:

        /* delay 50ms, 150ms, 250ms, ... */
	50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100

how about an API in include/migration/vmstate.h

static inline
int64_t self_announce_delay(int round)

Quoted 8 lines
{
	assert(round < SELF_ANNOUNCE_ROUNDS && round > 0);
        /* delay 50ms, 150ms, 250ms, ... */
	return 50 + (SELF_ANNOUNCE_ROUNDS - round - 1) * 100;
}

or something to this end.

Good idea, will do this.

Quoted 23 lines
Quoted 23 lines
hw/net/virtio-net.c +9 -0
+        }
+        return VIRTIO_NET_OK;
+    } else {
+        return VIRTIO_NET_ERR;
+    }
+}
+
 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
                                 struct iovec *iov, unsigned int iov_cnt)
 {
@@ -794,6 +835,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
+        } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
+            status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
@@ -1451,6 +1494,7 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
         qemu_get_subqueue(n->nic, i)->link_down = link_down;
     }
 
Quoted 4 lines

hw/net/virtio-net.c static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) · line 1497

Quoted 1 line
hw/net/virtio-net.c +1 -0
+    n->announce = VIRTIO_NET_ANNOUNCE_ROUNDS;

Well if virtio_net_handle_announce runs now it will start timer in the past, right?
This seems wrong.

Not sure I get the case. When in virtio_net_load() the vm is not even running so looks like virtio_net_handle_announce() could not run in the same time.

Quoted 57 lines
Quoted 57 lines
hw/net/virtio-net.c +4 -0
     return 0;
 }
 
@@ -1562,6 +1606,8 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
     n->status = VIRTIO_NET_S_LINK_UP;
+    n->announce_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                     virtio_net_announce_timer, n);
 
     if (n->netclient_type) {
         /*
@@ -1642,6 +1688,8 @@ static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
         }
     }
 
+    timer_del(n->announce_timer);
+    timer_free(n->announce_timer);
     g_free(n->vqs);
     qemu_del_nic(n->nic);
     virtio_cleanup(vdev);
include/hw/i386/pc.h +5 -0
index 32a7687..f93b427 100644
@@ -271,6 +271,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
             .driver   = "apic",\
             .property = "version",\
             .value    = stringify(0x11),\
+        },\
+        {\
+            .driver   = "virtio-net-pci",\
+            .property = "guest_announce",\
+            .value    = "off",\
         }
 
 #define PC_COMPAT_1_7 \
include/hw/virtio/virtio-net.h +2 -0
index 4b32440..ca1a9c5 100644
@@ -49,12 +49,14 @@
 #define VIRTIO_NET_F_CTRL_RX    18      /* Control channel RX mode support */
 #define VIRTIO_NET_F_CTRL_VLAN  19      /* Control channel VLAN filtering */
 #define VIRTIO_NET_F_CTRL_RX_EXTRA 20   /* Extra RX mode control support */
+#define VIRTIO_NET_F_GUEST_ANNOUNCE 21  /* Guest can announce itself */
 #define VIRTIO_NET_F_MQ         22      /* Device supports Receive Flow
                                          * Steering */
 
 #define VIRTIO_NET_F_CTRL_MAC_ADDR   23 /* Set MAC address */
 
 #define VIRTIO_NET_S_LINK_UP    1       /* Link is up */
+#define VIRTIO_NET_S_ANNOUNCE   2       /* Announcement is needed */
 
 #define TX_TIMER_INTERVAL 150000 /* 150 us */
 
Quoted 9 lines

include/hw/virtio/virtio-net.h typedef struct VirtIONet { · line 199

Quoted 6 lines
include/hw/virtio/virtio-net.h +2 -0
@@ -193,6 +195,8 @@ typedef struct VirtIONet {
     char *netclient_name;
     char *netclient_type;
     uint64_t curr_guest_offloads;
+    QEMUTimer *announce_timer;
+    int announce;

rename announce_counter pls.

Ok.

Quoted 15 lines

include/hw/virtio/virtio-net.h typedef struct VirtIONet { · line 221

Quoted 9 lines
include/hw/virtio/virtio-net.h +2 -0
 } VirtIONet;
 
 #define VIRTIO_NET_CTRL_MAC    1
@@ -213,6 +217,17 @@ typedef struct VirtIONet {
  #define VIRTIO_NET_CTRL_VLAN_DEL             1
 
 /*
+ * Control link announce acknowledgement
+ *

bunch of typos and vagueness below

include/hw/virtio/virtio-net.h typedef struct VirtIONet { · line 225

Quoted 4 lines
include/hw/virtio/virtio-net.h +4 -0
+ * The command VIRTIO_NET_CTRL_ANNOUNCE_ACK is used to indicate that
+ * driver has recevied the notification and device would clear the
+ * VIRTIO_NET_S_ANNOUNCE bit in the status filed after it received
+ * this command.
Quoted 8 lines

fixed version:

VIRTIO_NET_S_ANNOUNCE bit in the status field requests link announcement from guest driver. The driver is notified by config space change interrupt. The command VIRTIO_NET_CTRL_ANNOUNCE_ACK is used to indicate that the driver has received the notification. It makes the device clear the bit VIRTIO_NET_S_ANNOUNCE in the status field.

Sorry, look like I just copied those from a very old RFC. Will correct this. Thanks

Quoted 18 lines
Quoted 18 lines
include/hw/virtio/virtio-net.h +6 -0
+ */
+#define VIRTIO_NET_CTRL_ANNOUNCE       3
+ #define VIRTIO_NET_CTRL_ANNOUNCE_ACK         0
+
+/*
  * Control Multiqueue
  *
  * The command VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
@@ -251,6 +266,7 @@ struct virtio_net_ctrl_mq {
         DEFINE_PROP_BIT("guest_tso6", _state, _field, VIRTIO_NET_F_GUEST_TSO6, true), \
         DEFINE_PROP_BIT("guest_ecn", _state, _field, VIRTIO_NET_F_GUEST_ECN, true), \
         DEFINE_PROP_BIT("guest_ufo", _state, _field, VIRTIO_NET_F_GUEST_UFO, true), \
+        DEFINE_PROP_BIT("guest_announce", _state, _field, VIRTIO_NET_F_GUEST_ANNOUNCE, true), \
         DEFINE_PROP_BIT("host_tso4", _state, _field, VIRTIO_NET_F_HOST_TSO4, true), \
         DEFINE_PROP_BIT("host_tso6", _state, _field, VIRTIO_NET_F_HOST_TSO6, true), \
         DEFINE_PROP_BIT("host_ecn", _state, _field, VIRTIO_NET_F_HOST_ECN, true), \
Signature · 1 line
1.7.1
#3 lore raw
Quoted 105 lines · Jason Wang
Quoted 20 lines · Michael S. Tsirkin

Thanks, looks good.
Some minor comments below,

Quoted 2 lines · Jason Wang

It's hard to track all mac addresses and their configurations (e.g vlan or ipv6) in qemu. Without those informations, it's impossible to

s/those informations/this information/

Quoted 2 lines

build proper garp packet after migration. The only possible solution to this is let guest (who knew all configurations) to do this.

s/knew/knows/

Quoted 8 lines

So, this patch introduces a new readonly config status bit of virtio-net, VIRTIO_NET_S_ANNOUNCE which is used to notify guest to announce presence of its link through config update interrupt.When guest has done the announcement, it should ack the notification through VIRTIO_NET_CTRL_ANNOUNCE_ACK cmd. This feature is negotiated by a new feature bit VIRTIO_NET_F_ANNOUNCE (which has already been supported by Linux guest).

Quoted 3 lines
Quoted 1 line

During load, a counter of announcing rounds were set so that the after

s/were/is/
s/the after/after/

Will correct those typos.

Quoted 8 lines
Quoted 6 lines

the vm is running it can trigger rounds of config interrupts to notify the guest to build and send the correct garps.

Tested with ping to guest with vlan during migration. Without the patch, lots of the packets were lost after migration. With the patch, could not notice packet loss after migration.

below changelog should go after ---, until the ack list.

Ok.

Quoted 37 lines
Quoted 37 lines

Reference:

RFC v2: https://lists.gnu.org/archive/html/qemu-devel/2014-04/msg01750.html
RFC v1: https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg02648.html
V7:     https://lists.gnu.org/archive/html/qemu-devel/2013-03/msg01127.html

Changes from RFC v2:
- use QEMU_CLOCK_VIRTUAL instead of QEMU_CLOCK_REALTIME
- compat self announce for 2.0 machine type

Changes from RFC v1:
- clean VIRTIO_NET_S_ANNOUNCE bit during reset
- free announce timer during clean
- make announce work for non-vhost case

Changes from V7:
- Instead of introducing a global method for each kind of nic, this version limits the changes to virtio-net itself.

---
 hw/net/virtio-net.c            |   48 ++++++++++++++++++++++++++++++++++++++++
 include/hw/i386/pc.h           |    5 ++++
 include/hw/virtio/virtio-net.h |   16 +++++++++++++
 3 files changed, 69 insertions(+), 0 deletions(-)
hw/net/virtio-net.c +1 -0
index 940a7cf..98d59e9 100644
@@ -25,6 +25,7 @@
 #include "monitor/monitor.h"
 
 #define VIRTIO_NET_VM_VERSION    11
+#define VIRTIO_NET_ANNOUNCE_ROUNDS    3
 
Quoted 7 lines

hw/net/virtio-net.c line 31

Quoted 2 lines
hw/net/virtio-net.c +0 -0
 #define MAC_TABLE_ENTRIES    64
 #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */

I would make it 5 to be consistent with SELF_ANNOUNCE_ROUNDS in savevm.c

in fact, let's move SELF_ANNOUNCE_ROUNDS to include/migration/vmstate.h and reuse it.

Ok.

Quoted 4 lines
Quoted 4 lines
hw/net/virtio-net.c +0 -0
@@ -99,6 +100,25 @@ static bool virtio_net_started(VirtIONet *n, uint8_t status)
         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
 }
 
Quoted 8 lines

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 108

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+static void virtio_net_announce_timer(void *opaque)
+{
+    VirtIONet *n = opaque;
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    if (n->announce &&

I would make it > 0 here, just in case it becomes negative as a result of some bug.

Sure.

Quoted 9 lines
Quoted 9 lines
hw/net/virtio-net.c +9 -0
+        virtio_net_started(n, vdev->status) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_GUEST_ANNOUNCE) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_CTRL_VQ)) {
+
+        n->announce--;
+        n->status |= VIRTIO_NET_S_ANNOUNCE;
+        virtio_notify_config(vdev);
+    } else {
+        timer_del(n->announce_timer);
Quoted 7 lines
Quoted 5 lines

why do this here?

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 118

Quoted 1 line
hw/net/virtio-net.c +1 -0
+        n->announce = 0;

why is this here?

If guest shuts down the device, there's no need to do the announcing.

It's still weird.
Either announce is 0 and then timer was not running or it's > 0 and then this won't trigger.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
hw/net/virtio-net.c +3 -0
+    }
+}
+
 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -147,6 +167,8 @@ static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
 
     virtio_net_vhost_status(n, status);
 
Quoted 8 lines
Quoted 3 lines

hw/net/virtio-net.c static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) · line 171

Quoted 2 lines
hw/net/virtio-net.c +2 -0
+    virtio_net_announce_timer(n);
+

why do this here? why not right after we set announce counter?

The reasons are:

- The counters were set in load, but the device is not running so we could not inject the interrupt at that time.

I see. This makes sense - but this isn't intuitive. Why don't we simply start timer with current time? Need to make sure it runs fine if time passes, but I think it's fine.

Quoted 1 line

- We can stop the progress when guest is shutting down the device.

On shut down guest will reset device stopping timer - this seems enough.

Quoted 72 lines
Quoted 18 lines
Quoted 18 lines
hw/net/virtio-net.c +3 -0
     for (i = 0; i < n->max_queues; i++) {
         q = &n->vqs[i];
 
@@ -322,6 +344,9 @@ static void virtio_net_reset(VirtIODevice *vdev)
     n->nobcast = 0;
     /* multiqueue is disabled by default */
     n->curr_queues = 1;
+    timer_del(n->announce_timer);
+    n->announce = 0;
+    n->status &= ~VIRTIO_NET_S_ANNOUNCE;
 
     /* Flush any MAC and VLAN filter table state */
     n->mac_table.in_use = 0;
@@ -731,6 +756,22 @@ static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
     return VIRTIO_NET_OK;
 }
 
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 764

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
+                                      struct iovec *iov, unsigned int iov_cnt)
+{
+    if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK) {
+        n->status &= ~VIRTIO_NET_S_ANNOUNCE;
+        if (n->announce) {

I would make it > 0 here, just in case it becomes negative as a result of some bug.

Ok.

Quoted 11 lines

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 767

Quoted 3 lines
hw/net/virtio-net.c +3 -0
+            timer_mod(n->announce_timer,
+                      qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 50 +
+                      (VIRTIO_NET_ANNOUNCE_ROUNDS - n->announce - 1) * 100);

savevm.c has this code, and a comment:

        /* delay 50ms, 150ms, 250ms, ... */
	50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100

how about an API in include/migration/vmstate.h

static inline
int64_t self_announce_delay(int round)

Quoted 8 lines
{
	assert(round < SELF_ANNOUNCE_ROUNDS && round > 0);
        /* delay 50ms, 150ms, 250ms, ... */
	return 50 + (SELF_ANNOUNCE_ROUNDS - round - 1) * 100;
}

or something to this end.

Good idea, will do this.

Quoted 23 lines
Quoted 23 lines
hw/net/virtio-net.c +9 -0
+        }
+        return VIRTIO_NET_OK;
+    } else {
+        return VIRTIO_NET_ERR;
+    }
+}
+
 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
                                 struct iovec *iov, unsigned int iov_cnt)
 {
@@ -794,6 +835,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
+        } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
+            status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
@@ -1451,6 +1494,7 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
         qemu_get_subqueue(n->nic, i)->link_down = link_down;
     }
 
Quoted 8 lines
Quoted 4 lines

hw/net/virtio-net.c static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) · line 1497

Quoted 1 line
hw/net/virtio-net.c +1 -0
+    n->announce = VIRTIO_NET_ANNOUNCE_ROUNDS;

Well if virtio_net_handle_announce runs now it will start timer in the past, right?
This seems wrong.

Not sure I get the case. When in virtio_net_load() the vm is not even running so looks like virtio_net_handle_announce() could not run in the same time.

I see, this works because you decrement it when VM starts running. I think it's not a good idea to rely on this though, better do everything from timer, and handle
the case of command arriving too early.

Quoted 112 lines
Quoted 57 lines
Quoted 57 lines
hw/net/virtio-net.c +4 -0
     return 0;
 }
 
@@ -1562,6 +1606,8 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
     n->status = VIRTIO_NET_S_LINK_UP;
+    n->announce_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                     virtio_net_announce_timer, n);
 
     if (n->netclient_type) {
         /*
@@ -1642,6 +1688,8 @@ static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
         }
     }
 
+    timer_del(n->announce_timer);
+    timer_free(n->announce_timer);
     g_free(n->vqs);
     qemu_del_nic(n->nic);
     virtio_cleanup(vdev);
include/hw/i386/pc.h +5 -0
index 32a7687..f93b427 100644
@@ -271,6 +271,11 @@ bool e820_get_entry(int, uint32_t, uint64_t *, uint64_t *);
             .driver   = "apic",\
             .property = "version",\
             .value    = stringify(0x11),\
+        },\
+        {\
+            .driver   = "virtio-net-pci",\
+            .property = "guest_announce",\
+            .value    = "off",\
         }
 
 #define PC_COMPAT_1_7 \
include/hw/virtio/virtio-net.h +2 -0
index 4b32440..ca1a9c5 100644
@@ -49,12 +49,14 @@
 #define VIRTIO_NET_F_CTRL_RX    18      /* Control channel RX mode support */
 #define VIRTIO_NET_F_CTRL_VLAN  19      /* Control channel VLAN filtering */
 #define VIRTIO_NET_F_CTRL_RX_EXTRA 20   /* Extra RX mode control support */
+#define VIRTIO_NET_F_GUEST_ANNOUNCE 21  /* Guest can announce itself */
 #define VIRTIO_NET_F_MQ         22      /* Device supports Receive Flow
                                          * Steering */
 
 #define VIRTIO_NET_F_CTRL_MAC_ADDR   23 /* Set MAC address */
 
 #define VIRTIO_NET_S_LINK_UP    1       /* Link is up */
+#define VIRTIO_NET_S_ANNOUNCE   2       /* Announcement is needed */
 
 #define TX_TIMER_INTERVAL 150000 /* 150 us */
 
Quoted 9 lines

include/hw/virtio/virtio-net.h typedef struct VirtIONet { · line 199

Quoted 6 lines
include/hw/virtio/virtio-net.h +2 -0
@@ -193,6 +195,8 @@ typedef struct VirtIONet {
     char *netclient_name;
     char *netclient_type;
     uint64_t curr_guest_offloads;
+    QEMUTimer *announce_timer;
+    int announce;

rename announce_counter pls.

Ok.

Quoted 15 lines

include/hw/virtio/virtio-net.h typedef struct VirtIONet { · line 221

Quoted 9 lines
include/hw/virtio/virtio-net.h +2 -0
 } VirtIONet;
 
 #define VIRTIO_NET_CTRL_MAC    1
@@ -213,6 +217,17 @@ typedef struct VirtIONet {
  #define VIRTIO_NET_CTRL_VLAN_DEL             1
 
 /*
+ * Control link announce acknowledgement
+ *

bunch of typos and vagueness below

include/hw/virtio/virtio-net.h typedef struct VirtIONet { · line 225

Quoted 4 lines
include/hw/virtio/virtio-net.h +4 -0
+ * The command VIRTIO_NET_CTRL_ANNOUNCE_ACK is used to indicate that
+ * driver has recevied the notification and device would clear the
+ * VIRTIO_NET_S_ANNOUNCE bit in the status filed after it received
+ * this command.
Quoted 8 lines

fixed version:

VIRTIO_NET_S_ANNOUNCE bit in the status field requests link announcement from guest driver. The driver is notified by config space change interrupt. The command VIRTIO_NET_CTRL_ANNOUNCE_ACK is used to indicate that the driver has received the notification. It makes the device clear the bit VIRTIO_NET_S_ANNOUNCE in the status field.

Sorry, look like I just copied those from a very old RFC. Will correct this. Thanks

Quoted 18 lines
Quoted 18 lines
include/hw/virtio/virtio-net.h +6 -0
+ */
+#define VIRTIO_NET_CTRL_ANNOUNCE       3
+ #define VIRTIO_NET_CTRL_ANNOUNCE_ACK         0
+
+/*
  * Control Multiqueue
  *
  * The command VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
@@ -251,6 +266,7 @@ struct virtio_net_ctrl_mq {
         DEFINE_PROP_BIT("guest_tso6", _state, _field, VIRTIO_NET_F_GUEST_TSO6, true), \
         DEFINE_PROP_BIT("guest_ecn", _state, _field, VIRTIO_NET_F_GUEST_ECN, true), \
         DEFINE_PROP_BIT("guest_ufo", _state, _field, VIRTIO_NET_F_GUEST_UFO, true), \
+        DEFINE_PROP_BIT("guest_announce", _state, _field, VIRTIO_NET_F_GUEST_ANNOUNCE, true), \
         DEFINE_PROP_BIT("host_tso4", _state, _field, VIRTIO_NET_F_HOST_TSO4, true), \
         DEFINE_PROP_BIT("host_tso6", _state, _field, VIRTIO_NET_F_HOST_TSO6, true), \
         DEFINE_PROP_BIT("host_ecn", _state, _field, VIRTIO_NET_F_HOST_ECN, true), \
Signature · 1 line
1.7.1
author
#4 lore raw
Quoted 104 lines · Michael S. Tsirkin
Quoted 103 lines · Jason Wang
Quoted 20 lines · Michael S. Tsirkin

Thanks, looks good.
Some minor comments below,

Quoted 2 lines · Jason Wang

It's hard to track all mac addresses and their configurations (e.g vlan or ipv6) in qemu. Without those informations, it's impossible to

s/those informations/this information/

Quoted 2 lines

build proper garp packet after migration. The only possible solution to this is let guest (who knew all configurations) to do this.

s/knew/knows/

Quoted 8 lines

So, this patch introduces a new readonly config status bit of virtio-net, VIRTIO_NET_S_ANNOUNCE which is used to notify guest to announce presence of its link through config update interrupt.When guest has done the announcement, it should ack the notification through VIRTIO_NET_CTRL_ANNOUNCE_ACK cmd. This feature is negotiated by a new feature bit VIRTIO_NET_F_ANNOUNCE (which has already been supported by Linux guest).

Quoted 3 lines
Quoted 1 line

During load, a counter of announcing rounds were set so that the after

s/were/is/
s/the after/after/

Will correct those typos.

Quoted 8 lines
Quoted 6 lines

the vm is running it can trigger rounds of config interrupts to notify the guest to build and send the correct garps.

Tested with ping to guest with vlan during migration. Without the patch, lots of the packets were lost after migration. With the patch, could not notice packet loss after migration.

below changelog should go after ---, until the ack list.

Ok.

Quoted 37 lines
Quoted 37 lines

Reference:

RFC v2: https://lists.gnu.org/archive/html/qemu-devel/2014-04/msg01750.html
RFC v1: https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg02648.html
V7:     https://lists.gnu.org/archive/html/qemu-devel/2013-03/msg01127.html

Changes from RFC v2:
- use QEMU_CLOCK_VIRTUAL instead of QEMU_CLOCK_REALTIME
- compat self announce for 2.0 machine type

Changes from RFC v1:
- clean VIRTIO_NET_S_ANNOUNCE bit during reset
- free announce timer during clean
- make announce work for non-vhost case

Changes from V7:
- Instead of introducing a global method for each kind of nic, this version limits the changes to virtio-net itself.

---
 hw/net/virtio-net.c            |   48 ++++++++++++++++++++++++++++++++++++++++
 include/hw/i386/pc.h           |    5 ++++
 include/hw/virtio/virtio-net.h |   16 +++++++++++++
 3 files changed, 69 insertions(+), 0 deletions(-)
hw/net/virtio-net.c +1 -0
index 940a7cf..98d59e9 100644
@@ -25,6 +25,7 @@
 #include "monitor/monitor.h"
 
 #define VIRTIO_NET_VM_VERSION    11
+#define VIRTIO_NET_ANNOUNCE_ROUNDS    3
 
Quoted 7 lines

hw/net/virtio-net.c line 31

Quoted 2 lines
hw/net/virtio-net.c +0 -0
 #define MAC_TABLE_ENTRIES    64
 #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */

I would make it 5 to be consistent with SELF_ANNOUNCE_ROUNDS in savevm.c

in fact, let's move SELF_ANNOUNCE_ROUNDS to include/migration/vmstate.h and reuse it.

Ok.

Quoted 4 lines
Quoted 4 lines
hw/net/virtio-net.c +0 -0
@@ -99,6 +100,25 @@ static bool virtio_net_started(VirtIONet *n, uint8_t status)
         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
 }
 
Quoted 8 lines

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 108

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+static void virtio_net_announce_timer(void *opaque)
+{
+    VirtIONet *n = opaque;
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    if (n->announce &&

I would make it > 0 here, just in case it becomes negative as a result of some bug.

Sure.

Quoted 11 lines

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 117

Quoted 9 lines
hw/net/virtio-net.c +9 -0
+        virtio_net_started(n, vdev->status) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_GUEST_ANNOUNCE) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_CTRL_VQ)) {
+
+        n->announce--;
+        n->status |= VIRTIO_NET_S_ANNOUNCE;
+        virtio_notify_config(vdev);
+    } else {
+        timer_del(n->announce_timer);

why do this here?

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 118

Quoted 1 line
hw/net/virtio-net.c +1 -0
+        n->announce = 0;

why is this here?

If guest shuts down the device, there's no need to do the announcing.

It's still weird.
Either announce is 0 and then timer was not running or it's > 0 and then this won't trigger.

Right, the logic here is for QEMU_CLOCK_REALTIME. But there's another question, we use QEMU_CLOCK_VIRTUAL while qemu is using QEMU_CLOCK_REALTIME for its announcing. This looks fine but not sure whether this is safe. And if the link was down, it's better for us to stop the announcing?

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

hw/net/virtio-net.c static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) · line 171

Quoted 13 lines
hw/net/virtio-net.c +5 -0
+    }
+}
+
 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -147,6 +167,8 @@ static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
 
     virtio_net_vhost_status(n, status);
 
+    virtio_net_announce_timer(n);
+

why do this here? why not right after we set announce counter?

Quoted 8 lines
Quoted 4 lines

The reasons are:

- The counters were set in load, but the device is not running so we could not inject the interrupt at that time.

I see. This makes sense - but this isn't intuitive. Why don't we simply start timer with current time? Need to make sure it runs fine if time passes, but I think it's fine.

Not sure I get the point, I didn't see any differences except for an extra timer fire.

Quoted 3 lines
Quoted 2 lines

- We can stop the progress when guest is shutting down the device.

On shut down guest will reset device stopping timer - this seems enough.

Yes, I see.

Quoted 73 lines
Quoted 73 lines
Quoted 17 lines
Quoted 17 lines
hw/net/virtio-net.c +3 -0
     for (i = 0; i < n->max_queues; i++) {
         q = &n->vqs[i];
 
@@ -322,6 +344,9 @@ static void virtio_net_reset(VirtIODevice *vdev)
     n->nobcast = 0;
     /* multiqueue is disabled by default */
     n->curr_queues = 1;
+    timer_del(n->announce_timer);
+    n->announce = 0;
+    n->status &= ~VIRTIO_NET_S_ANNOUNCE;
 
     /* Flush any MAC and VLAN filter table state */
     n->mac_table.in_use = 0;
@@ -731,6 +756,22 @@ static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
     return VIRTIO_NET_OK;
 }
 
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 764

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
+                                      struct iovec *iov, unsigned int iov_cnt)
+{
+    if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK) {
+        n->status &= ~VIRTIO_NET_S_ANNOUNCE;
+        if (n->announce) {

I would make it > 0 here, just in case it becomes negative as a result of some bug.

Ok.

Quoted 11 lines

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 767

Quoted 3 lines
hw/net/virtio-net.c +3 -0
+            timer_mod(n->announce_timer,
+                      qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 50 +
+                      (VIRTIO_NET_ANNOUNCE_ROUNDS - n->announce - 1) * 100);

savevm.c has this code, and a comment:

        /* delay 50ms, 150ms, 250ms, ... */
	50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100

how about an API in include/migration/vmstate.h

static inline
int64_t self_announce_delay(int round)

Quoted 8 lines
{
	assert(round < SELF_ANNOUNCE_ROUNDS && round > 0);
        /* delay 50ms, 150ms, 250ms, ... */
	return 50 + (SELF_ANNOUNCE_ROUNDS - round - 1) * 100;
}

or something to this end.

Good idea, will do this.

Quoted 27 lines
Quoted 20 lines
hw/net/virtio-net.c +9 -0
+        }
+        return VIRTIO_NET_OK;
+    } else {
+        return VIRTIO_NET_ERR;
+    }
+}
+
 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
                                 struct iovec *iov, unsigned int iov_cnt)
 {
@@ -794,6 +835,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
+        } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
+            status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {

hw/net/virtio-net.c static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) · line 1497

Quoted 4 lines
hw/net/virtio-net.c +1 -0
@@ -1451,6 +1494,7 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
         qemu_get_subqueue(n->nic, i)->link_down = link_down;
     }
 
+    n->announce = VIRTIO_NET_ANNOUNCE_ROUNDS;

Well if virtio_net_handle_announce runs now it will start timer in the past, right?
This seems wrong.

Quoted 8 lines
Quoted 3 lines

Not sure I get the case. When in virtio_net_load() the vm is not even running so looks like virtio_net_handle_announce() could not run in the same time.

I see, this works because you decrement it when VM starts running. I think it's not a good idea to rely on this though, better do everything from timer, and handle
the case of command arriving too early.

Right, if QEMU_CLOCK_VIRTUAL is fine, we can do everything in a timer.

For the case of command arriving too early. Is there anything else we need to do? Since we only start the next timer when we get ack command.

Thanks

#5 lore raw
Quoted 109 lines · Jason Wang
Quoted 108 lines · Michael S. Tsirkin
Quoted 107 lines · Jason Wang
Quoted 20 lines · Michael S. Tsirkin

Thanks, looks good.
Some minor comments below,

Quoted 2 lines · Jason Wang

It's hard to track all mac addresses and their configurations (e.g vlan or ipv6) in qemu. Without those informations, it's impossible to

s/those informations/this information/

Quoted 2 lines

build proper garp packet after migration. The only possible solution to this is let guest (who knew all configurations) to do this.

s/knew/knows/

Quoted 8 lines

So, this patch introduces a new readonly config status bit of virtio-net, VIRTIO_NET_S_ANNOUNCE which is used to notify guest to announce presence of its link through config update interrupt.When guest has done the announcement, it should ack the notification through VIRTIO_NET_CTRL_ANNOUNCE_ACK cmd. This feature is negotiated by a new feature bit VIRTIO_NET_F_ANNOUNCE (which has already been supported by Linux guest).

Quoted 3 lines
Quoted 1 line

During load, a counter of announcing rounds were set so that the after

s/were/is/
s/the after/after/

Will correct those typos.

Quoted 8 lines
Quoted 6 lines

the vm is running it can trigger rounds of config interrupts to notify the guest to build and send the correct garps.

Tested with ping to guest with vlan during migration. Without the patch, lots of the packets were lost after migration. With the patch, could not notice packet loss after migration.

below changelog should go after ---, until the ack list.

Ok.

Quoted 37 lines
Quoted 37 lines

Reference:

RFC v2: https://lists.gnu.org/archive/html/qemu-devel/2014-04/msg01750.html
RFC v1: https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg02648.html
V7:     https://lists.gnu.org/archive/html/qemu-devel/2013-03/msg01127.html

Changes from RFC v2:
- use QEMU_CLOCK_VIRTUAL instead of QEMU_CLOCK_REALTIME
- compat self announce for 2.0 machine type

Changes from RFC v1:
- clean VIRTIO_NET_S_ANNOUNCE bit during reset
- free announce timer during clean
- make announce work for non-vhost case

Changes from V7:
- Instead of introducing a global method for each kind of nic, this version limits the changes to virtio-net itself.

---
 hw/net/virtio-net.c            |   48 ++++++++++++++++++++++++++++++++++++++++
 include/hw/i386/pc.h           |    5 ++++
 include/hw/virtio/virtio-net.h |   16 +++++++++++++
 3 files changed, 69 insertions(+), 0 deletions(-)
hw/net/virtio-net.c +1 -0
index 940a7cf..98d59e9 100644
@@ -25,6 +25,7 @@
 #include "monitor/monitor.h"
 
 #define VIRTIO_NET_VM_VERSION    11
+#define VIRTIO_NET_ANNOUNCE_ROUNDS    3
 
Quoted 7 lines

hw/net/virtio-net.c line 31

Quoted 2 lines
hw/net/virtio-net.c +0 -0
 #define MAC_TABLE_ENTRIES    64
 #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */

I would make it 5 to be consistent with SELF_ANNOUNCE_ROUNDS in savevm.c

in fact, let's move SELF_ANNOUNCE_ROUNDS to include/migration/vmstate.h and reuse it.

Ok.

Quoted 4 lines
Quoted 4 lines
hw/net/virtio-net.c +0 -0
@@ -99,6 +100,25 @@ static bool virtio_net_started(VirtIONet *n, uint8_t status)
         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
 }
 
Quoted 8 lines

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 108

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+static void virtio_net_announce_timer(void *opaque)
+{
+    VirtIONet *n = opaque;
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    if (n->announce &&

I would make it > 0 here, just in case it becomes negative as a result of some bug.

Sure.

Quoted 9 lines

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 117

Quoted 9 lines
hw/net/virtio-net.c +9 -0
+        virtio_net_started(n, vdev->status) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_GUEST_ANNOUNCE) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_CTRL_VQ)) {
+
+        n->announce--;
+        n->status |= VIRTIO_NET_S_ANNOUNCE;
+        virtio_notify_config(vdev);
+    } else {
+        timer_del(n->announce_timer);
Quoted 5 lines

why do this here?

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 118

Quoted 1 line
hw/net/virtio-net.c +1 -0
+        n->announce = 0;

why is this here?

If guest shuts down the device, there's no need to do the announcing.

Quoted 8 lines
Quoted 3 lines

It's still weird.
Either announce is 0 and then timer was not running or it's > 0 and then this won't trigger.

Right, the logic here is for QEMU_CLOCK_REALTIME. But there's another question, we use QEMU_CLOCK_VIRTUAL while qemu is using QEMU_CLOCK_REALTIME for its announcing. This looks fine but not sure whether this is safe.

Meaning QEMU_CLOCK_REALTIME that qemu uses?
Not sure either but it doesn't modify guest state so it seems safe.

Quoted 2 lines

And if the link was down, it's better for us to
stop the announcing?

I think that it doesn't matter: guest won't announce when link is down, anyway.
Not worth it to write extra logic here in the host.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
hw/net/virtio-net.c +3 -0
+    }
+}
+
 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -147,6 +167,8 @@ static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
 
Quoted 5 lines

hw/net/virtio-net.c static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) · line 171

Quoted 4 lines
hw/net/virtio-net.c +2 -0
     virtio_net_vhost_status(n, status);
 
+    virtio_net_announce_timer(n);
+

why do this here? why not right after we set announce counter?

The reasons are:

- The counters were set in load, but the device is not running so we could not inject the interrupt at that time.

Quoted 7 lines
Quoted 4 lines

I see. This makes sense - but this isn't intuitive. Why don't we simply start timer with current time? Need to make sure it runs fine if time passes, but I think it's fine.

Not sure I get the point, I didn't see any differences except for an extra timer fire.

The only reason you call virtio_net_announce_timer from set_status is because it gets run on vm start/stop.
It's true but not intuitive.
Just run timer always from timer, it's clearer this way :)

Quoted 87 lines
Quoted 3 lines
Quoted 2 lines

- We can stop the progress when guest is shutting down the device.

On shut down guest will reset device stopping timer - this seems enough.

Yes, I see.

Quoted 73 lines
Quoted 73 lines
Quoted 17 lines
Quoted 17 lines
hw/net/virtio-net.c +3 -0
     for (i = 0; i < n->max_queues; i++) {
         q = &n->vqs[i];
 
@@ -322,6 +344,9 @@ static void virtio_net_reset(VirtIODevice *vdev)
     n->nobcast = 0;
     /* multiqueue is disabled by default */
     n->curr_queues = 1;
+    timer_del(n->announce_timer);
+    n->announce = 0;
+    n->status &= ~VIRTIO_NET_S_ANNOUNCE;
 
     /* Flush any MAC and VLAN filter table state */
     n->mac_table.in_use = 0;
@@ -731,6 +756,22 @@ static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
     return VIRTIO_NET_OK;
 }
 
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 764

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
+                                      struct iovec *iov, unsigned int iov_cnt)
+{
+    if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK) {
+        n->status &= ~VIRTIO_NET_S_ANNOUNCE;
+        if (n->announce) {

I would make it > 0 here, just in case it becomes negative as a result of some bug.

Ok.

Quoted 11 lines

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 767

Quoted 3 lines
hw/net/virtio-net.c +3 -0
+            timer_mod(n->announce_timer,
+                      qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 50 +
+                      (VIRTIO_NET_ANNOUNCE_ROUNDS - n->announce - 1) * 100);

savevm.c has this code, and a comment:

        /* delay 50ms, 150ms, 250ms, ... */
	50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100

how about an API in include/migration/vmstate.h

static inline
int64_t self_announce_delay(int round)

Quoted 8 lines
{
	assert(round < SELF_ANNOUNCE_ROUNDS && round > 0);
        /* delay 50ms, 150ms, 250ms, ... */
	return 50 + (SELF_ANNOUNCE_ROUNDS - round - 1) * 100;
}

or something to this end.

Good idea, will do this.

Quoted 27 lines
Quoted 20 lines
hw/net/virtio-net.c +9 -0
+        }
+        return VIRTIO_NET_OK;
+    } else {
+        return VIRTIO_NET_ERR;
+    }
+}
+
 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
                                 struct iovec *iov, unsigned int iov_cnt)
 {
@@ -794,6 +835,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
+        } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
+            status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {

hw/net/virtio-net.c static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) · line 1497

Quoted 4 lines
hw/net/virtio-net.c +1 -0
@@ -1451,6 +1494,7 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
         qemu_get_subqueue(n->nic, i)->link_down = link_down;
     }
 
+    n->announce = VIRTIO_NET_ANNOUNCE_ROUNDS;

Well if virtio_net_handle_announce runs now it will start timer in the past, right?
This seems wrong.

Quoted 8 lines
Quoted 3 lines

Not sure I get the case. When in virtio_net_load() the vm is not even running so looks like virtio_net_handle_announce() could not run in the same time.

I see, this works because you decrement it when VM starts running. I think it's not a good idea to rely on this though, better do everything from timer, and handle
the case of command arriving too early.

Quoted 6 lines

Right, if QEMU_CLOCK_VIRTUAL is fine, we can do everything in a timer.

For the case of command arriving too early. Is there anything else we need to do? Since we only start the next timer when we get ack command.

Thanks

I think we need to make sure we don't set the timer in the past or very far in the future.

author
#6 lore raw
Quoted 121 lines · Michael S. Tsirkin
Quoted 108 lines · Jason Wang
Quoted 107 lines · Michael S. Tsirkin
Quoted 106 lines · Jason Wang
Quoted 20 lines · Michael S. Tsirkin

Thanks, looks good.
Some minor comments below,

Quoted 2 lines · Jason Wang

It's hard to track all mac addresses and their configurations (e.g vlan or ipv6) in qemu. Without those informations, it's impossible to

s/those informations/this information/

Quoted 2 lines

build proper garp packet after migration. The only possible solution to this is let guest (who knew all configurations) to do this.

s/knew/knows/

Quoted 8 lines

So, this patch introduces a new readonly config status bit of virtio-net, VIRTIO_NET_S_ANNOUNCE which is used to notify guest to announce presence of its link through config update interrupt.When guest has done the announcement, it should ack the notification through VIRTIO_NET_CTRL_ANNOUNCE_ACK cmd. This feature is negotiated by a new feature bit VIRTIO_NET_F_ANNOUNCE (which has already been supported by Linux guest).

Quoted 3 lines
Quoted 1 line

During load, a counter of announcing rounds were set so that the after

s/were/is/
s/the after/after/

Will correct those typos.

Quoted 8 lines
Quoted 6 lines

the vm is running it can trigger rounds of config interrupts to notify the guest to build and send the correct garps.

Tested with ping to guest with vlan during migration. Without the patch, lots of the packets were lost after migration. With the patch, could not notice packet loss after migration.

below changelog should go after ---, until the ack list.

Ok.

Quoted 37 lines
Quoted 37 lines

Reference:

RFC v2: https://lists.gnu.org/archive/html/qemu-devel/2014-04/msg01750.html
RFC v1: https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg02648.html
V7:     https://lists.gnu.org/archive/html/qemu-devel/2013-03/msg01127.html

Changes from RFC v2:
- use QEMU_CLOCK_VIRTUAL instead of QEMU_CLOCK_REALTIME
- compat self announce for 2.0 machine type

Changes from RFC v1:
- clean VIRTIO_NET_S_ANNOUNCE bit during reset
- free announce timer during clean
- make announce work for non-vhost case

Changes from V7:
- Instead of introducing a global method for each kind of nic, this version limits the changes to virtio-net itself.

---
 hw/net/virtio-net.c            |   48 ++++++++++++++++++++++++++++++++++++++++
 include/hw/i386/pc.h           |    5 ++++
 include/hw/virtio/virtio-net.h |   16 +++++++++++++
 3 files changed, 69 insertions(+), 0 deletions(-)
hw/net/virtio-net.c +1 -0
index 940a7cf..98d59e9 100644
@@ -25,6 +25,7 @@
 #include "monitor/monitor.h"
 
 #define VIRTIO_NET_VM_VERSION    11
+#define VIRTIO_NET_ANNOUNCE_ROUNDS    3
 
Quoted 7 lines

hw/net/virtio-net.c line 31

Quoted 2 lines
hw/net/virtio-net.c +0 -0
 #define MAC_TABLE_ENTRIES    64
 #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */

I would make it 5 to be consistent with SELF_ANNOUNCE_ROUNDS in savevm.c

in fact, let's move SELF_ANNOUNCE_ROUNDS to include/migration/vmstate.h and reuse it.

Ok.

Quoted 4 lines
Quoted 4 lines
hw/net/virtio-net.c +0 -0
@@ -99,6 +100,25 @@ static bool virtio_net_started(VirtIONet *n, uint8_t status)
         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
 }
 
Quoted 8 lines

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 108

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+static void virtio_net_announce_timer(void *opaque)
+{
+    VirtIONet *n = opaque;
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    if (n->announce &&

I would make it > 0 here, just in case it becomes negative as a result of some bug.

Sure.

Quoted 14 lines

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 117

Quoted 9 lines
hw/net/virtio-net.c +9 -0
+        virtio_net_started(n, vdev->status) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_GUEST_ANNOUNCE) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_CTRL_VQ)) {
+
+        n->announce--;
+        n->status |= VIRTIO_NET_S_ANNOUNCE;
+        virtio_notify_config(vdev);
+    } else {
+        timer_del(n->announce_timer);

why do this here?

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 118

Quoted 1 line
hw/net/virtio-net.c +1 -0
+        n->announce = 0;

why is this here?

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

If guest shuts down the device, there's no need to do the announcing.

It's still weird.
Either announce is 0 and then timer was not running or it's > 0 and then this won't trigger.

Right, the logic here is for QEMU_CLOCK_REALTIME. But there's another question, we use QEMU_CLOCK_VIRTUAL while qemu is using QEMU_CLOCK_REALTIME for its announcing. This looks fine but not sure whether this is safe.

Meaning QEMU_CLOCK_REALTIME that qemu uses?
Not sure either but it doesn't modify guest state so it seems safe.

Quoted 5 lines
Quoted 2 lines

And if the link was down, it's better for us to
stop the announcing?

I think that it doesn't matter: guest won't announce when link is down, anyway.
Not worth it to write extra logic here in the host.

Ok.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
hw/net/virtio-net.c +3 -0
+    }
+}
+
 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -147,6 +167,8 @@ static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
 
     virtio_net_vhost_status(n, status);
 
Quoted 7 lines
Quoted 3 lines

hw/net/virtio-net.c static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) · line 171

Quoted 2 lines
hw/net/virtio-net.c +2 -0
+    virtio_net_announce_timer(n);
+

why do this here? why not right after we set announce counter?

The reasons are:

- The counters were set in load, but the device is not running so we could not inject the interrupt at that time.

I see. This makes sense - but this isn't intuitive. Why don't we simply start timer with current time? Need to make sure it runs fine if time passes, but I think it's fine.

Quoted 7 lines
Quoted 2 lines

Not sure I get the point, I didn't see any differences except for an extra timer fire.

The only reason you call virtio_net_announce_timer from set_status is because it gets run on vm start/stop.
It's true but not intuitive.
Just run timer always from timer, it's clearer this way :)

Sure.

Quoted 84 lines
Quoted 84 lines
Quoted 2 lines
Quoted 1 line

- We can stop the progress when guest is shutting down the device.

On shut down guest will reset device stopping timer - this seems enough.

Yes, I see.

Quoted 81 lines
Quoted 69 lines
Quoted 17 lines
Quoted 17 lines
hw/net/virtio-net.c +3 -0
     for (i = 0; i < n->max_queues; i++) {
         q = &n->vqs[i];
 
@@ -322,6 +344,9 @@ static void virtio_net_reset(VirtIODevice *vdev)
     n->nobcast = 0;
     /* multiqueue is disabled by default */
     n->curr_queues = 1;
+    timer_del(n->announce_timer);
+    n->announce = 0;
+    n->status &= ~VIRTIO_NET_S_ANNOUNCE;
 
     /* Flush any MAC and VLAN filter table state */
     n->mac_table.in_use = 0;
@@ -731,6 +756,22 @@ static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
     return VIRTIO_NET_OK;
 }
 
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 764

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
+                                      struct iovec *iov, unsigned int iov_cnt)
+{
+    if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK) {
+        n->status &= ~VIRTIO_NET_S_ANNOUNCE;
+        if (n->announce) {

I would make it > 0 here, just in case it becomes negative as a result of some bug.

Ok.

Quoted 11 lines

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 767

Quoted 3 lines
hw/net/virtio-net.c +3 -0
+            timer_mod(n->announce_timer,
+                      qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 50 +
+                      (VIRTIO_NET_ANNOUNCE_ROUNDS - n->announce - 1) * 100);

savevm.c has this code, and a comment:

        /* delay 50ms, 150ms, 250ms, ... */
	50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100

how about an API in include/migration/vmstate.h

static inline
int64_t self_announce_delay(int round)

Quoted 8 lines
{
	assert(round < SELF_ANNOUNCE_ROUNDS && round > 0);
        /* delay 50ms, 150ms, 250ms, ... */
	return 50 + (SELF_ANNOUNCE_ROUNDS - round - 1) * 100;
}

or something to this end.

Good idea, will do this.

Quoted 23 lines
Quoted 23 lines
hw/net/virtio-net.c +9 -0
+        }
+        return VIRTIO_NET_OK;
+    } else {
+        return VIRTIO_NET_ERR;
+    }
+}
+
 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
                                 struct iovec *iov, unsigned int iov_cnt)
 {
@@ -794,6 +835,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
+        } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
+            status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
@@ -1451,6 +1494,7 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
         qemu_get_subqueue(n->nic, i)->link_down = link_down;
     }
 
Quoted 7 lines
Quoted 4 lines

hw/net/virtio-net.c static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) · line 1497

Quoted 1 line
hw/net/virtio-net.c +1 -0
+    n->announce = VIRTIO_NET_ANNOUNCE_ROUNDS;

Well if virtio_net_handle_announce runs now it will start timer in the past, right?
This seems wrong.

Not sure I get the case. When in virtio_net_load() the vm is not even running so looks like virtio_net_handle_announce() could not run in the same time.

I see, this works because you decrement it when VM starts running. I think it's not a good idea to rely on this though, better do everything from timer, and handle
the case of command arriving too early.

Quoted 8 lines
Quoted 6 lines

Right, if QEMU_CLOCK_VIRTUAL is fine, we can do everything in a timer.

For the case of command arriving too early. Is there anything else we need to do? Since we only start the next timer when we get ack command.

Thanks

I think we need to make sure we don't set the timer in the past or very far in the future.

n->announce is between 0 and VIRTIO_NET_ANNOUNCE_ROUNDS - 1, when doing timer_mod(), so it looks ok. There's another case that if vm was stopped for a long time, the timer will also be delayed, but it's still safe in this case.

#7 lore raw
Quoted 248 lines · Jason Wang
Quoted 121 lines · Michael S. Tsirkin
Quoted 108 lines · Jason Wang
Quoted 107 lines · Michael S. Tsirkin
Quoted 106 lines · Jason Wang
Quoted 20 lines · Michael S. Tsirkin

Thanks, looks good.
Some minor comments below,

Quoted 2 lines · Jason Wang

It's hard to track all mac addresses and their configurations (e.g vlan or ipv6) in qemu. Without those informations, it's impossible to

s/those informations/this information/

Quoted 2 lines

build proper garp packet after migration. The only possible solution to this is let guest (who knew all configurations) to do this.

s/knew/knows/

Quoted 8 lines

So, this patch introduces a new readonly config status bit of virtio-net, VIRTIO_NET_S_ANNOUNCE which is used to notify guest to announce presence of its link through config update interrupt.When guest has done the announcement, it should ack the notification through VIRTIO_NET_CTRL_ANNOUNCE_ACK cmd. This feature is negotiated by a new feature bit VIRTIO_NET_F_ANNOUNCE (which has already been supported by Linux guest).

Quoted 3 lines
Quoted 1 line

During load, a counter of announcing rounds were set so that the after

s/were/is/
s/the after/after/

Will correct those typos.

Quoted 8 lines
Quoted 6 lines

the vm is running it can trigger rounds of config interrupts to notify the guest to build and send the correct garps.

Tested with ping to guest with vlan during migration. Without the patch, lots of the packets were lost after migration. With the patch, could not notice packet loss after migration.

below changelog should go after ---, until the ack list.

Ok.

Quoted 37 lines
Quoted 37 lines

Reference:

RFC v2: https://lists.gnu.org/archive/html/qemu-devel/2014-04/msg01750.html
RFC v1: https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg02648.html
V7:     https://lists.gnu.org/archive/html/qemu-devel/2013-03/msg01127.html

Changes from RFC v2:
- use QEMU_CLOCK_VIRTUAL instead of QEMU_CLOCK_REALTIME
- compat self announce for 2.0 machine type

Changes from RFC v1:
- clean VIRTIO_NET_S_ANNOUNCE bit during reset
- free announce timer during clean
- make announce work for non-vhost case

Changes from V7:
- Instead of introducing a global method for each kind of nic, this version limits the changes to virtio-net itself.

---
 hw/net/virtio-net.c            |   48 ++++++++++++++++++++++++++++++++++++++++
 include/hw/i386/pc.h           |    5 ++++
 include/hw/virtio/virtio-net.h |   16 +++++++++++++
 3 files changed, 69 insertions(+), 0 deletions(-)
hw/net/virtio-net.c +1 -0
index 940a7cf..98d59e9 100644
@@ -25,6 +25,7 @@
 #include "monitor/monitor.h"
 
 #define VIRTIO_NET_VM_VERSION    11
+#define VIRTIO_NET_ANNOUNCE_ROUNDS    3
 
Quoted 7 lines

hw/net/virtio-net.c line 31

Quoted 2 lines
hw/net/virtio-net.c +0 -0
 #define MAC_TABLE_ENTRIES    64
 #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */

I would make it 5 to be consistent with SELF_ANNOUNCE_ROUNDS in savevm.c

in fact, let's move SELF_ANNOUNCE_ROUNDS to include/migration/vmstate.h and reuse it.

Ok.

Quoted 4 lines
Quoted 4 lines
hw/net/virtio-net.c +0 -0
@@ -99,6 +100,25 @@ static bool virtio_net_started(VirtIONet *n, uint8_t status)
         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
 }
 
Quoted 8 lines

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 108

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+static void virtio_net_announce_timer(void *opaque)
+{
+    VirtIONet *n = opaque;
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+    if (n->announce &&

I would make it > 0 here, just in case it becomes negative as a result of some bug.

Sure.

Quoted 14 lines

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 117

Quoted 9 lines
hw/net/virtio-net.c +9 -0
+        virtio_net_started(n, vdev->status) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_GUEST_ANNOUNCE) &&
+        vdev->guest_features & (0x1 << VIRTIO_NET_F_CTRL_VQ)) {
+
+        n->announce--;
+        n->status |= VIRTIO_NET_S_ANNOUNCE;
+        virtio_notify_config(vdev);
+    } else {
+        timer_del(n->announce_timer);

why do this here?

hw/net/virtio-net.c static bool virtio_net_started(VirtIONet *n, uint8_t status) · line 118

Quoted 1 line
hw/net/virtio-net.c +1 -0
+        n->announce = 0;

why is this here?

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

If guest shuts down the device, there's no need to do the announcing.

It's still weird.
Either announce is 0 and then timer was not running or it's > 0 and then this won't trigger.

Right, the logic here is for QEMU_CLOCK_REALTIME. But there's another question, we use QEMU_CLOCK_VIRTUAL while qemu is using QEMU_CLOCK_REALTIME for its announcing. This looks fine but not sure whether this is safe.

Meaning QEMU_CLOCK_REALTIME that qemu uses?
Not sure either but it doesn't modify guest state so it seems safe.

Quoted 5 lines
Quoted 2 lines

And if the link was down, it's better for us to
stop the announcing?

I think that it doesn't matter: guest won't announce when link is down, anyway.
Not worth it to write extra logic here in the host.

Ok.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
hw/net/virtio-net.c +3 -0
+    }
+}
+
 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -147,6 +167,8 @@ static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
 
     virtio_net_vhost_status(n, status);
 
Quoted 7 lines
Quoted 3 lines

hw/net/virtio-net.c static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) · line 171

Quoted 2 lines
hw/net/virtio-net.c +2 -0
+    virtio_net_announce_timer(n);
+

why do this here? why not right after we set announce counter?

The reasons are:

- The counters were set in load, but the device is not running so we could not inject the interrupt at that time.

I see. This makes sense - but this isn't intuitive. Why don't we simply start timer with current time? Need to make sure it runs fine if time passes, but I think it's fine.

Quoted 7 lines
Quoted 2 lines

Not sure I get the point, I didn't see any differences except for an extra timer fire.

The only reason you call virtio_net_announce_timer from set_status is because it gets run on vm start/stop.
It's true but not intuitive.
Just run timer always from timer, it's clearer this way :)

Sure.

Quoted 89 lines
Quoted 89 lines
Quoted 2 lines
Quoted 1 line

- We can stop the progress when guest is shutting down the device.

On shut down guest will reset device stopping timer - this seems enough.

Yes, I see.

Quoted 73 lines
Quoted 73 lines
Quoted 17 lines
Quoted 17 lines
hw/net/virtio-net.c +3 -0
     for (i = 0; i < n->max_queues; i++) {
         q = &n->vqs[i];
 
@@ -322,6 +344,9 @@ static void virtio_net_reset(VirtIODevice *vdev)
     n->nobcast = 0;
     /* multiqueue is disabled by default */
     n->curr_queues = 1;
+    timer_del(n->announce_timer);
+    n->announce = 0;
+    n->status &= ~VIRTIO_NET_S_ANNOUNCE;
 
     /* Flush any MAC and VLAN filter table state */
     n->mac_table.in_use = 0;
@@ -731,6 +756,22 @@ static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
     return VIRTIO_NET_OK;
 }
 
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 764

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
+                                      struct iovec *iov, unsigned int iov_cnt)
+{
+    if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK) {
+        n->status &= ~VIRTIO_NET_S_ANNOUNCE;
+        if (n->announce) {

I would make it > 0 here, just in case it becomes negative as a result of some bug.

Ok.

Quoted 11 lines

hw/net/virtio-net.c static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, · line 767

Quoted 3 lines
hw/net/virtio-net.c +3 -0
+            timer_mod(n->announce_timer,
+                      qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 50 +
+                      (VIRTIO_NET_ANNOUNCE_ROUNDS - n->announce - 1) * 100);

savevm.c has this code, and a comment:

        /* delay 50ms, 150ms, 250ms, ... */
	50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100

how about an API in include/migration/vmstate.h

static inline
int64_t self_announce_delay(int round)

Quoted 8 lines
{
	assert(round < SELF_ANNOUNCE_ROUNDS && round > 0);
        /* delay 50ms, 150ms, 250ms, ... */
	return 50 + (SELF_ANNOUNCE_ROUNDS - round - 1) * 100;
}

or something to this end.

Good idea, will do this.

Quoted 27 lines
Quoted 20 lines
hw/net/virtio-net.c +9 -0
+        }
+        return VIRTIO_NET_OK;
+    } else {
+        return VIRTIO_NET_ERR;
+    }
+}
+
 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
                                 struct iovec *iov, unsigned int iov_cnt)
 {
@@ -794,6 +835,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
+        } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
+            status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {

hw/net/virtio-net.c static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) · line 1497

Quoted 4 lines
hw/net/virtio-net.c +1 -0
@@ -1451,6 +1494,7 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
         qemu_get_subqueue(n->nic, i)->link_down = link_down;
     }
 
+    n->announce = VIRTIO_NET_ANNOUNCE_ROUNDS;

Well if virtio_net_handle_announce runs now it will start timer in the past, right?
This seems wrong.

Quoted 8 lines
Quoted 3 lines

Not sure I get the case. When in virtio_net_load() the vm is not even running so looks like virtio_net_handle_announce() could not run in the same time.

I see, this works because you decrement it when VM starts running. I think it's not a good idea to rely on this though, better do everything from timer, and handle
the case of command arriving too early.

Right, if QEMU_CLOCK_VIRTUAL is fine, we can do everything in a timer.

For the case of command arriving too early. Is there anything else we need to do? Since we only start the next timer when we get ack command.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Thanks

I think we need to make sure we don't set the timer in the past or very far in the future.

n->announce is between 0 and VIRTIO_NET_ANNOUNCE_ROUNDS - 1, when doing timer_mod(), so it looks ok.

All I am saying, make sure it's within the range even if handle announce is called before timer_mod.
Can not happen with your current patch, but must make sure it's still correct after you make changes :) Also, maybe add an assert there.

Quoted 3 lines

There's another case that if vm was stopped
for a long time, the timer will also be delayed, but it's still safe in this case.

author
#8 lore raw
Quoted 253 lines · Michael S. Tsirkin
Quoted 252 lines · Jason Wang
Quoted 252 lines · Michael S. Tsirkin
Quoted 121 lines
Quoted 121 lines · Jason Wang
Quoted 108 lines
Quoted 108 lines · Michael S. Tsirkin
Quoted 107 lines
Quoted 107 lines · Jason Wang
Quoted 106 lines
Quoted 106 lines · Michael S. Tsirkin
Quoted 20 lines
Quoted 20 lines

Thanks, looks good.
Some minor comments below,

On Thu, May 15, 2014 at 03:16:47PM +0800, Jason Wang wrote:
It's hard to track all mac addresses and their configurations (e.g
vlan or ipv6) in qemu. Without those informations, it's impossible to
s/those informations/this information/

build proper garp packet after migration. The only possible solution
to this is let guest (who knew all configurations) to do this.
s/knew/knows/

So, this patch introduces a new readonly config status bit of virtio-net,
VIRTIO_NET_S_ANNOUNCE which is used to notify guest to announce
presence of its link through config update interrupt.When guest has
done the announcement, it should ack the notification through
VIRTIO_NET_CTRL_ANNOUNCE_ACK cmd. This feature is negotiated by a new
feature bit VIRTIO_NET_F_ANNOUNCE (which has already been supported by
Linux guest).

Quoted 3 lines
Quoted 3 lines

During load, a counter of announcing rounds were set so that the after
s/were/is/
s/the after/after/

Will correct those typos.

Quoted 8 lines
Quoted 8 lines

the vm is running it can trigger rounds of config interrupts to notify
the guest to build and send the correct garps.

Tested with ping to guest with vlan during migration. Without the
patch, lots of the packets were lost after migration. With the patch,
could not notice packet loss after migration.
below changelog should go after ---, until the ack list.

Ok.

Quoted 37 lines
Quoted 37 lines

Reference:
RFC v2: https://lists.gnu.org/archive/html/qemu-devel/2014-04/msg01750.html
RFC v1: https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg02648.html
V7: https://lists.gnu.org/archive/html/qemu-devel/2013-03/msg01127.html

Changes from RFC v2:
- use QEMU_CLOCK_VIRTUAL instead of QEMU_CLOCK_REALTIME
- compat self announce for 2.0 machine type

Changes from RFC v1:
- clean VIRTIO_NET_S_ANNOUNCE bit during reset
- free announce timer during clean
- make announce work for non-vhost case

Changes from V7:
- Instead of introducing a global method for each kind of nic, this
version limits the changes to virtio-net itself.

Cc: Liuyongan <[email protected]>
Cc: Amos Kong <[email protected]>
Signed-off-by: Jason Wang <[email protected]>
---
hw/net/virtio-net.c | 48 ++++++++++++++++++++++++++++++++++++++++
include/hw/i386/pc.h | 5 ++++
include/hw/virtio/virtio-net.h | 16 +++++++++++++
3 files changed, 69 insertions(+), 0 deletions(-)

diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
index 940a7cf..98d59e9 100644
--- a/hw/net/virtio-net.c
+++ b/hw/net/virtio-net.c
@@ -25,6 +25,7 @@
#include "monitor/monitor.h"

#define VIRTIO_NET_VM_VERSION 11
+#define VIRTIO_NET_ANNOUNCE_ROUNDS 3

Quoted 7 lines
Quoted 7 lines

#define MAC_TABLE_ENTRIES 64
#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
I would make it 5 to be consistent with SELF_ANNOUNCE_ROUNDS
in savevm.c

in fact, let's move SELF_ANNOUNCE_ROUNDS to include/migration/vmstate.h
and reuse it.

Ok.

Quoted 4 lines
Quoted 4 lines

@@ -99,6 +100,25 @@ static bool virtio_net_started(VirtIONet *n, uint8_t status)
(n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
}

Quoted 8 lines
Quoted 8 lines

+static void virtio_net_announce_timer(void *opaque)
+{
+ VirtIONet *n = opaque;
+ VirtIODevice *vdev = VIRTIO_DEVICE(n);
+
+ if (n->announce &&
I would make it > 0 here, just in case it becomes negative as a result
of some bug.

Sure.

Quoted 14 lines
Quoted 14 lines

+ virtio_net_started(n, vdev->status) &&
+ vdev->guest_features & (0x1 << VIRTIO_NET_F_GUEST_ANNOUNCE) &&
+ vdev->guest_features & (0x1 << VIRTIO_NET_F_CTRL_VQ)) {
+
+ n->announce--;
+ n->status |= VIRTIO_NET_S_ANNOUNCE;
+ virtio_notify_config(vdev);
+ } else {
+ timer_del(n->announce_timer);
why do this here?

+ n->announce = 0;
why is this here?

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

If guest shuts down the device, there's no need to do the announcing.

It's still weird.
Either announce is 0 and then timer was not running or it's > 0 and then this won't trigger.

Right, the logic here is for QEMU_CLOCK_REALTIME. But there's another question, we use QEMU_CLOCK_VIRTUAL while qemu is using QEMU_CLOCK_REALTIME for its announcing. This looks fine but not sure whether this is safe.

Meaning QEMU_CLOCK_REALTIME that qemu uses?
Not sure either but it doesn't modify guest state so it seems safe.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

And if the link was down, it's better for us to
stop the announcing?

I think that it doesn't matter: guest won't announce when link is down, anyway.
Not worth it to write extra logic here in the host.

Ok.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

+ }
+}
+
static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
{
VirtIODevice *vdev = VIRTIO_DEVICE(n);
@@ -147,6 +167,8 @@ static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)

virtio_net_vhost_status(n, status);

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

+ virtio_net_announce_timer(n);
+
why do this here? why not right after we set announce counter?

The reasons are:

- The counters were set in load, but the device is not running so we could not inject the interrupt at that time.

I see. This makes sense - but this isn't intuitive. Why don't we simply start timer with current time? Need to make sure it runs fine if time passes, but I think it's fine.

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

Not sure I get the point, I didn't see any differences except for an extra timer fire.

The only reason you call virtio_net_announce_timer from set_status is because it gets run on vm start/stop.
It's true but not intuitive.
Just run timer always from timer, it's clearer this way :)

Sure.

Quoted 92 lines
Quoted 92 lines
Quoted 84 lines
Quoted 84 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

- We can stop the progress when guest is shutting down the device.

On shut down guest will reset device stopping timer - this seems enough.

Yes, I see.

Quoted 81 lines
Quoted 81 lines
Quoted 69 lines
Quoted 69 lines
Quoted 17 lines
Quoted 17 lines

for (i = 0; i < n->max_queues; i++) {
q = &n->vqs[i];

@@ -322,6 +344,9 @@ static void virtio_net_reset(VirtIODevice *vdev)
n->nobcast = 0;
/* multiqueue is disabled by default */
n->curr_queues = 1;
+ timer_del(n->announce_timer);
+ n->announce = 0;
+ n->status &= ~VIRTIO_NET_S_ANNOUNCE;

/* Flush any MAC and VLAN filter table state */
n->mac_table.in_use = 0;
@@ -731,6 +756,22 @@ static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
return VIRTIO_NET_OK;
}

Quoted 8 lines
Quoted 8 lines

+static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
+ struct iovec *iov, unsigned int iov_cnt)
+{
+ if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK) {
+ n->status &= ~VIRTIO_NET_S_ANNOUNCE;
+ if (n->announce) {
I would make it > 0 here, just in case it becomes negative as a result
of some bug.

Ok.

Quoted 17 lines
Quoted 17 lines

+ timer_mod(n->announce_timer,
+ qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 50 +
+ (VIRTIO_NET_ANNOUNCE_ROUNDS - n->announce - 1) * 100);
savevm.c has this code, and a comment:
/* delay 50ms, 150ms, 250ms, ... */
50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100

how about an API in include/migration/vmstate.h

static inline
int64_t self_announce_delay(int round)
{
assert(round < SELF_ANNOUNCE_ROUNDS && round > 0);
/* delay 50ms, 150ms, 250ms, ... */
return 50 + (SELF_ANNOUNCE_ROUNDS - round - 1) * 100;
}

Quoted 2 lines
Quoted 2 lines

or something to this end.

Good idea, will do this.

Quoted 23 lines
Quoted 23 lines

+ }
+ return VIRTIO_NET_OK;
+ } else {
+ return VIRTIO_NET_ERR;
+ }
+}
+
static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
struct iovec *iov, unsigned int iov_cnt)
{
@@ -794,6 +835,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
} else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
+ } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
+ status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
} else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
} else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
@@ -1451,6 +1494,7 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
qemu_get_subqueue(n->nic, i)->link_down = link_down;
}

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines

+ n->announce = VIRTIO_NET_ANNOUNCE_ROUNDS;
Well if virtio_net_handle_announce runs now it will start timer
in the past, right?
This seems wrong.

Not sure I get the case. When in virtio_net_load() the vm is not even running so looks like virtio_net_handle_announce() could not run in the same time.

I see, this works because you decrement it when VM starts running. I think it's not a good idea to rely on this though, better do everything from timer, and handle
the case of command arriving too early.

Quoted 6 lines
Quoted 6 lines

Right, if QEMU_CLOCK_VIRTUAL is fine, we can do everything in a timer.

For the case of command arriving too early. Is there anything else we need to do? Since we only start the next timer when we get ack command.

Thanks

I think we need to make sure we don't set the timer in the past or very far in the future.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

n->announce is between 0 and VIRTIO_NET_ANNOUNCE_ROUNDS - 1, when doing timer_mod(), so it looks ok.

All I am saying, make sure it's within the range even if handle announce is called before timer_mod.
Can not happen with your current patch, but must make sure it's still correct after you make changes :) Also, maybe add an assert there.

I get your meaning. Will post V2.

Thanks