[RFC v2 07/14] virtio-net: detect VirtIONet MAC table mid-migration changes

Jonah Palmer posted 14 patches 2 days ago
Maintainers: Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Yanan Wang <wangyanan55@huawei.com>, Zhao Liu <zhao1.liu@intel.com>, "Michael S. Tsirkin" <mst@redhat.com>, Stefano Garzarella <sgarzare@redhat.com>, Jason Wang <jasowang@redhat.com>
[RFC v2 07/14] virtio-net: detect VirtIONet MAC table mid-migration changes
Posted by Jonah Palmer 2 days ago
This patch saves the virtio-net device's MAC table information to
compare with later during the stop-and-copy phase.

We leave out mac_table.first_multi since it's derived post-load.

Note: we store the exact mac_table.in_use value and the MIN macro is
only used to bound copy & compare byte spans for safety.

Signed-off-by: Jonah Palmer <jonah.palmer@oracle.com>
---
 hw/net/virtio-net.c            | 34 ++++++++++++++++++++++++++++++++++
 include/hw/virtio/virtio-net.h |  8 ++++++++
 2 files changed, 42 insertions(+)

diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
index 4f14bba510..2dd130777e 100644
--- a/hw/net/virtio-net.c
+++ b/hw/net/virtio-net.c
@@ -3884,6 +3884,26 @@ static int virtio_net_early_pre_save(void *opaque)
 
     /* VirtIONet MAC info snapshot */
     memcpy(vnet_mig->mac_early, n->mac, ETH_ALEN);
+    vnet_mig->mtable_in_use_early = n->mac_table.in_use;
+    vnet_mig->mtable_uni_overflow_early = n->mac_table.uni_overflow;
+    vnet_mig->mtable_multi_overflow_early = n->mac_table.multi_overflow;
+    /*
+     * Allocate baseline buffer once. Only copy the used slice each time.
+     * This avoids repeated allocations and keeps memcmp fast
+     */
+    if (!vnet_mig->mtable_macs_early) {
+        vnet_mig->mtable_macs_early =
+            g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
+    }
+    /*
+     * Copy used portion only. Cap byte span with MIN for safety even if
+     * a buggy config reports more than capacity.
+     */
+    if (vnet_mig->mtable_in_use_early) {
+        uint32_t used = MIN(vnet_mig->mtable_in_use_early, (uint32_t)MAC_TABLE_ENTRIES);
+        size_t bytes = (size_t)used * ETH_ALEN;
+        memcpy(vnet_mig->mtable_macs_early, n->mac_table.macs, bytes);
+    }
 
     return 0;
 }
@@ -4171,6 +4191,8 @@ static void virtio_net_device_unrealize(DeviceState *dev)
         g_free(vdev->migration);
         vdev->migration = NULL;
 
+        g_free(n->migration->mtable_macs_early);
+        n->migration->mtable_macs_early = NULL;
         g_free(n->migration);
         n->migration = NULL;
 
@@ -4286,6 +4308,18 @@ static bool virtio_net_has_delta(VirtIONet *n, VirtIODevice *vdev)
     if (memcmp(n->mac, vnet_mig->mac_early, ETH_ALEN) != 0) {
         return true;
     }
+    if (n->mac_table.in_use != vnet_mig->mtable_in_use_early ||
+        n->mac_table.uni_overflow != vnet_mig->mtable_uni_overflow_early ||
+        n->mac_table.multi_overflow != vnet_mig->mtable_multi_overflow_early) {
+        return true;
+    }
+    if (n->mac_table.in_use) {
+        uint32_t used = MIN(n->mac_table.in_use, (uint32_t)MAC_TABLE_ENTRIES);
+        size_t bytes = (size_t)used * ETH_ALEN;
+        if (memcmp(n->mac_table.macs, vnet_mig->mtable_macs_early, bytes) != 0) {
+            return true;
+        }
+    }
 
     /*
      * Always return true for now until we're able to detect all possible
diff --git a/include/hw/virtio/virtio-net.h b/include/hw/virtio/virtio-net.h
index 280155366c..1e1f1a3995 100644
--- a/include/hw/virtio/virtio-net.h
+++ b/include/hw/virtio/virtio-net.h
@@ -170,9 +170,17 @@ typedef struct VirtIONetQueue {
 /**
  * struct VirtIONetMigration - VirtIONet migration structure
  * @mac_early: MAC address early migration snapshot.
+ * @mtable_in_use_early: In-use MAC table entries.
+ * @mtable_uni_overflow_early: Unicast overflow MAC table entries.
+ * @mtable_multi_overflow_early: Multicast overflow MAC table entries.
+ * @mtable_macs_early: MAC table entries.
  */
 typedef struct VirtIONetMigration {
     uint8_t mac_early[ETH_ALEN];
+    uint32_t mtable_in_use_early;
+    uint8_t mtable_uni_overflow_early;
+    uint8_t mtable_multi_overflow_early;
+    uint8_t *mtable_macs_early;
 } VirtIONetMigration;
 
 struct VirtIONet {
-- 
2.51.0