Skip to contentSkip to conversation

net/colo: check vnet_hdr_support flag when using virtio-net

PATCH In review Zhang Chen 3 participants 9 messages Sep 18, 2021 – Oct 21, 2021 Networking

lore
Share & tools
lore

Zhang Chen

author
#0 lore raw

PATCH v3 1/1 net/colo: check vnet_hdr_support flag when using virtio-net

When COLO use only one vnet_hdr_support parameter between COLO network filter(filter-mirror, filter-redirector or filter-rewriter and colo-compare, packet will not be parsed correctly. Acquire network driver related to COLO, if it is nirtio-net, check vnet_hdr_support flag of COLO network filter and colo-compare.

---

Changelog:
v3:
Fix some typos.
Rebased for Qemu 6.2.

v2:
Detect virtio-net driver and apply vnet_hdr_support automatically. (Jason)

---
 net/colo-compare.c    | 57 +++++++++++++++++++++++++++++++++++++++++++
 net/colo.c            | 20 +++++++++++++++
 net/colo.h            |  4 +++
 net/filter-mirror.c   | 21 ++++++++++++++++
 net/filter-rewriter.c | 10 ++++++++
 qapi/qom.json         |  6 +++++
 qemu-options.hx       |  6 +++--
 7 files changed, 122 insertions(+), 2 deletions(-)
net/colo-compare.c +57 -0
index b100e7b51f..870bd05a41 100644
@@ -110,6 +110,7 @@ struct CompareState {
     char *sec_indev;
     char *outdev;
     char *notify_dev;
+    char *netdev;
     CharBackend chr_pri_in;
     CharBackend chr_sec_in;
     CharBackend chr_out;
@@ -838,6 +839,28 @@ static int compare_chr_can_read(void *opaque)
     return COMPARE_READ_LEN_MAX;
 }
 
+static int colo_set_default_netdev(void *opaque, QemuOpts *opts, Error **errp)
+{
+    const char *colo_obj_type, *netdev_from_filter;
+    char **netdev = (char **)opaque;
+
+    colo_obj_type = qemu_opt_get(opts, "qom-type");
+
+    if (colo_obj_type &&
+        (strcmp(colo_obj_type, "filter-mirror") == 0 ||
+         strcmp(colo_obj_type, "filter-redirector") == 0 ||
+         strcmp(colo_obj_type, "filter-rewriter") == 0)) {
+        netdev_from_filter = qemu_opt_get(opts, "netdev");
+        if (*netdev == NULL) {
+            *netdev = g_strdup(netdev_from_filter);
+        } else if (strcmp(*netdev, netdev_from_filter) != 0) {
+            warn_report("%s is using a different netdev from other COLO "
+                        "component", colo_obj_type);
+        }
+    }
+    return 0;
+}
+
 /*
  * Called from the main thread on the primary for packets
  * arriving over the socket from the primary.
@@ -1050,6 +1073,21 @@ static void compare_set_vnet_hdr(Object *obj,
     s->vnet_hdr = value;
 }
 
+static char *compare_get_netdev(Object *obj, Error **errp)
+{
+    CompareState *s = COLO_COMPARE(obj);
+
+    return g_strdup(s->netdev);
+}
+
+static void compare_set_netdev(Object *obj, const char *value, Error **errp)
+{
+    CompareState *s = COLO_COMPARE(obj);
+
+    g_free(s->netdev);
+    s->netdev = g_strdup(value);
+}
+
 static char *compare_get_notify_dev(Object *obj, Error **errp)
 {
     CompareState *s = COLO_COMPARE(obj);
@@ -1274,6 +1312,12 @@ static void colo_compare_complete(UserCreatable *uc, Error **errp)
         max_queue_size = MAX_QUEUE_SIZE;
     }
 
+    if (!s->netdev) {
+        /* Set default netdev as the first colo netfilter found */
+        qemu_opts_foreach(qemu_find_opts("object"),
+                          colo_set_default_netdev, &s->netdev, NULL);
+    }
+
     if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
         !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
         return;
@@ -1289,6 +1333,16 @@ static void colo_compare_complete(UserCreatable *uc, Error **errp)
         return;
     }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, s->netdev, NULL)) {
+        /*
+         * colo compare needs 'vnet_hdr_support' when it works on virtio-net,
+         * add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
     net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr);
     net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize, s->vnet_hdr);
 
@@ -1400,6 +1454,9 @@ static void colo_compare_init(Object *obj)
     s->vnet_hdr = false;
     object_property_add_bool(obj, "vnet_hdr_support", compare_get_vnet_hdr,
                              compare_set_vnet_hdr);
+    /* colo compare can't varify that netdev is correct */
+    object_property_add_str(obj, "netdev", compare_get_netdev,
+                            compare_set_netdev);
 }
 
 void colo_compare_cleanup(void)
net/colo.c +20 -0
index 3a3e6e89a0..4a03780f45 100644
@@ -243,3 +243,23 @@ bool connection_has_tracked(GHashTable *connection_track_table,
 
     return conn ? true : false;
 }
+
+/* check the network driver related to COLO, return 1 if it is virtio-net */
+int vnet_driver_check(void *opaque, QemuOpts *opts, Error **errp)
+{
+    const char *driver_type, *netdev_from_driver;
+    char *netdev_from_filter = (char *)opaque;
+
+    driver_type = qemu_opt_get(opts, "driver");
+    netdev_from_driver = qemu_opt_get(opts, "netdev");
+
+    if (!driver_type || !netdev_from_driver || !netdev_from_filter) {
+        return 0;
+    }
+
+    if (g_str_has_prefix(driver_type, "virtio-net") &&
+        strcmp(netdev_from_driver, netdev_from_filter) == 0) {
+        return 1;
+    }
+    return 0;
+}
net/colo.h +4 -0
index d91cd245c4..d401fc76b6 100644
@@ -18,6 +18,9 @@
 #include "qemu/jhash.h"
 #include "qemu/timer.h"
 #include "net/eth.h"
+#include "qemu/option.h"
+#include "qemu/option_int.h"
+#include "qemu/config-file.h"
 
 #define HASHTABLE_MAX_SIZE 16384
 
@@ -104,5 +107,6 @@ Packet *packet_new(const void *data, int size, int vnet_hdr_len);
 Packet *packet_new_nocopy(void *data, int size, int vnet_hdr_len);
 void packet_destroy(void *opaque, void *user_data);
 void packet_destroy_partial(void *opaque, void *user_data);
+int vnet_driver_check(void *opaque, QemuOpts *opts, Error **errp);
 
 #endif /* NET_COLO_H */
net/filter-mirror.c +21 -0
index f20240cc9f..69ca9c9839 100644
@@ -12,6 +12,7 @@
 #include "qemu/osdep.h"
 #include "net/filter.h"
 #include "net/net.h"
+#include "net/colo.h"
 #include "qapi/error.h"
 #include "qom/object.h"
 #include "qemu/main-loop.h"
@@ -224,6 +225,16 @@ static void filter_mirror_setup(NetFilterState *nf, Error **errp)
         return;
     }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter mirror needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
     qemu_chr_fe_init(&s->chr_out, chr, errp);
 }
 
@@ -252,6 +263,16 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
         }
     }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter redirector needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
     net_socket_rs_init(&s->rs, redirector_rs_finalize, s->vnet_hdr);
 
     if (s->indev) {
net/filter-rewriter.c +10 -0
index cb3a96cde1..637ef4ce71 100644
@@ -388,6 +388,16 @@ static void colo_rewriter_setup(NetFilterState *nf, Error **errp)
 {
     RewriterState *s = FILTER_REWRITER(nf);
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter rewriter needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
     s->connection_track_table = g_hash_table_new_full(connection_key_hash,
                                                       connection_key_equal,
                                                       g_free,
qapi/qom.json +6 -0
index a25616bc7a..5760107160 100644
@@ -241,6 +241,11 @@
 # @notify_dev: name of the character device backend to be used to communicate
 #              with the remote colo-frame (only for Xen COLO)
 #
+# @netdev: id of the network device backend to colo-compare. Although
+#          colo-compare doesn't depend on network device directly, this
+#          parameter helps colo-compare know what network driver it is
+#          working on.(since 6.2)
+#
 # @compare_timeout: the maximum time to hold a packet from @primary_in for
 #                   comparison with an incoming packet on @secondary_in in
 #                   milliseconds (default: 3000)
@@ -264,6 +269,7 @@
             'outdev': 'str',
             'iothread': 'str',
             '*notify_dev': 'str',
+            '*netdev': 'str',
             '*compare_timeout': 'uint64',
             '*expired_scan_cycle': 'uint32',
             '*max_queue_size': 'uint32',
qemu-options.hx +4 -2
index 8f603cc7e6..250937fbbf 100644
@@ -4964,12 +4964,14 @@ SRST
         stored. The file format is libpcap, so it can be analyzed with
         tools such as tcpdump or Wireshark.
 
-    ``-object colo-compare,id=id,primary_in=chardevid,secondary_in=chardevid,outdev=chardevid,iothread=id[,vnet_hdr_support][,notify_dev=id][,compare_timeout=@var{ms}][,expired_scan_cycle=@var{ms}][,max_queue_size=@var{size}]``
+    ``-object colo-compare,id=id,primary_in=chardevid,secondary_in=chardevid,outdev=chardevid,iothread=id[,netdev=netdevid][,vnet_hdr_support][,notify_dev=id][,compare_timeout=@var{ms}][,expired_scan_cycle=@var{ms}][,max_queue_size=@var{size}]``
         Colo-compare gets packet from primary\_in chardevid and
         secondary\_in, then compare whether the payload of primary packet
         and secondary packet are the same. If same, it will output
         primary packet to out\_dev, else it will notify COLO-framework to do
-        checkpoint and send primary packet to out\_dev. In order to
+        checkpoint and send primary packet to out\_dev. Although colo-compare
+        does not depend on network device directly, netdevid helps
+        colo-compare know what network driver it is working on. In order to
         improve efficiency, we need to put the task of comparison in
         another iothread. If it has the vnet\_hdr\_support flag,
         colo compare will send/recv packet with vnet\_hdr\_len.
Signature · 1 line
2.25.1
#1 lore raw

Jason, did this fall through the cracks?

Quoted 293 lines · Zhang Chen

When COLO use only one vnet_hdr_support parameter between COLO network filter(filter-mirror, filter-redirector or filter-rewriter and colo-compare, packet will not be parsed correctly. Acquire network driver related to COLO, if it is nirtio-net, check vnet_hdr_support flag of COLO network filter and colo-compare.

---

Changelog:
v3:
Fix some typos.
Rebased for Qemu 6.2.

v2:
Detect virtio-net driver and apply vnet_hdr_support automatically. (Jason)

---
 net/colo-compare.c    | 57 +++++++++++++++++++++++++++++++++++++++++++
 net/colo.c            | 20 +++++++++++++++
 net/colo.h            |  4 +++
 net/filter-mirror.c   | 21 ++++++++++++++++
 net/filter-rewriter.c | 10 ++++++++
 qapi/qom.json         |  6 +++++
 qemu-options.hx       |  6 +++--
 7 files changed, 122 insertions(+), 2 deletions(-)
net/colo-compare.c +57 -0
index b100e7b51f..870bd05a41 100644
@@ -110,6 +110,7 @@ struct CompareState {
     char *sec_indev;
     char *outdev;
     char *notify_dev;
+    char *netdev;
     CharBackend chr_pri_in;
     CharBackend chr_sec_in;
     CharBackend chr_out;
@@ -838,6 +839,28 @@ static int compare_chr_can_read(void *opaque)
     return COMPARE_READ_LEN_MAX;
 }
 
+static int colo_set_default_netdev(void *opaque, QemuOpts *opts, Error **errp)
+{
+    const char *colo_obj_type, *netdev_from_filter;
+    char **netdev = (char **)opaque;
+
+    colo_obj_type = qemu_opt_get(opts, "qom-type");
+
+    if (colo_obj_type &&
+        (strcmp(colo_obj_type, "filter-mirror") == 0 ||
+         strcmp(colo_obj_type, "filter-redirector") == 0 ||
+         strcmp(colo_obj_type, "filter-rewriter") == 0)) {
+        netdev_from_filter = qemu_opt_get(opts, "netdev");
+        if (*netdev == NULL) {
+            *netdev = g_strdup(netdev_from_filter);
+        } else if (strcmp(*netdev, netdev_from_filter) != 0) {
+            warn_report("%s is using a different netdev from other COLO "
+                        "component", colo_obj_type);
+        }
+    }
+    return 0;
+}
+
 /*
  * Called from the main thread on the primary for packets
  * arriving over the socket from the primary.
@@ -1050,6 +1073,21 @@ static void compare_set_vnet_hdr(Object *obj,
     s->vnet_hdr = value;
 }
 
+static char *compare_get_netdev(Object *obj, Error **errp)
+{
+    CompareState *s = COLO_COMPARE(obj);
+
+    return g_strdup(s->netdev);
+}
+
+static void compare_set_netdev(Object *obj, const char *value, Error **errp)
+{
+    CompareState *s = COLO_COMPARE(obj);
+
+    g_free(s->netdev);
+    s->netdev = g_strdup(value);
+}
+
 static char *compare_get_notify_dev(Object *obj, Error **errp)
 {
     CompareState *s = COLO_COMPARE(obj);
@@ -1274,6 +1312,12 @@ static void colo_compare_complete(UserCreatable *uc, Error **errp)
         max_queue_size = MAX_QUEUE_SIZE;
     }
 
+    if (!s->netdev) {
+        /* Set default netdev as the first colo netfilter found */
+        qemu_opts_foreach(qemu_find_opts("object"),
+                          colo_set_default_netdev, &s->netdev, NULL);
+    }
+
     if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
         !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
         return;
@@ -1289,6 +1333,16 @@ static void colo_compare_complete(UserCreatable *uc, Error **errp)
         return;
     }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, s->netdev, NULL)) {
+        /*
+         * colo compare needs 'vnet_hdr_support' when it works on virtio-net,
+         * add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
     net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr);
     net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize, s->vnet_hdr);
 
@@ -1400,6 +1454,9 @@ static void colo_compare_init(Object *obj)
     s->vnet_hdr = false;
     object_property_add_bool(obj, "vnet_hdr_support", compare_get_vnet_hdr,
                              compare_set_vnet_hdr);
+    /* colo compare can't varify that netdev is correct */
+    object_property_add_str(obj, "netdev", compare_get_netdev,
+                            compare_set_netdev);
 }
 
 void colo_compare_cleanup(void)
net/colo.c +20 -0
index 3a3e6e89a0..4a03780f45 100644
@@ -243,3 +243,23 @@ bool connection_has_tracked(GHashTable *connection_track_table,
 
     return conn ? true : false;
 }
+
+/* check the network driver related to COLO, return 1 if it is virtio-net */
+int vnet_driver_check(void *opaque, QemuOpts *opts, Error **errp)
+{
+    const char *driver_type, *netdev_from_driver;
+    char *netdev_from_filter = (char *)opaque;
+
+    driver_type = qemu_opt_get(opts, "driver");
+    netdev_from_driver = qemu_opt_get(opts, "netdev");
+
+    if (!driver_type || !netdev_from_driver || !netdev_from_filter) {
+        return 0;
+    }
+
+    if (g_str_has_prefix(driver_type, "virtio-net") &&
+        strcmp(netdev_from_driver, netdev_from_filter) == 0) {
+        return 1;
+    }
+    return 0;
+}
net/colo.h +4 -0
index d91cd245c4..d401fc76b6 100644
@@ -18,6 +18,9 @@
 #include "qemu/jhash.h"
 #include "qemu/timer.h"
 #include "net/eth.h"
+#include "qemu/option.h"
+#include "qemu/option_int.h"
+#include "qemu/config-file.h"
 
 #define HASHTABLE_MAX_SIZE 16384
 
@@ -104,5 +107,6 @@ Packet *packet_new(const void *data, int size, int vnet_hdr_len);
 Packet *packet_new_nocopy(void *data, int size, int vnet_hdr_len);
 void packet_destroy(void *opaque, void *user_data);
 void packet_destroy_partial(void *opaque, void *user_data);
+int vnet_driver_check(void *opaque, QemuOpts *opts, Error **errp);
 
 #endif /* NET_COLO_H */
net/filter-mirror.c +21 -0
index f20240cc9f..69ca9c9839 100644
@@ -12,6 +12,7 @@
 #include "qemu/osdep.h"
 #include "net/filter.h"
 #include "net/net.h"
+#include "net/colo.h"
 #include "qapi/error.h"
 #include "qom/object.h"
 #include "qemu/main-loop.h"
@@ -224,6 +225,16 @@ static void filter_mirror_setup(NetFilterState *nf, Error **errp)
         return;
     }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter mirror needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
     qemu_chr_fe_init(&s->chr_out, chr, errp);
 }
 
@@ -252,6 +263,16 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
         }
     }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter redirector needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
     net_socket_rs_init(&s->rs, redirector_rs_finalize, s->vnet_hdr);
 
     if (s->indev) {
net/filter-rewriter.c +10 -0
index cb3a96cde1..637ef4ce71 100644
@@ -388,6 +388,16 @@ static void colo_rewriter_setup(NetFilterState *nf, Error **errp)
 {
     RewriterState *s = FILTER_REWRITER(nf);
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter rewriter needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
     s->connection_track_table = g_hash_table_new_full(connection_key_hash,
                                                       connection_key_equal,
                                                       g_free,
qapi/qom.json +6 -0
index a25616bc7a..5760107160 100644
@@ -241,6 +241,11 @@
 # @notify_dev: name of the character device backend to be used to communicate
 #              with the remote colo-frame (only for Xen COLO)
 #
+# @netdev: id of the network device backend to colo-compare. Although
+#          colo-compare doesn't depend on network device directly, this
+#          parameter helps colo-compare know what network driver it is
+#          working on.(since 6.2)
+#
 # @compare_timeout: the maximum time to hold a packet from @primary_in for
 #                   comparison with an incoming packet on @secondary_in in
 #                   milliseconds (default: 3000)
@@ -264,6 +269,7 @@
             'outdev': 'str',
             'iothread': 'str',
             '*notify_dev': 'str',
+            '*netdev': 'str',
             '*compare_timeout': 'uint64',
             '*expired_scan_cycle': 'uint32',
             '*max_queue_size': 'uint32',
qemu-options.hx +4 -2
index 8f603cc7e6..250937fbbf 100644
@@ -4964,12 +4964,14 @@ SRST
         stored. The file format is libpcap, so it can be analyzed with
         tools such as tcpdump or Wireshark.
 
-    ``-object colo-compare,id=id,primary_in=chardevid,secondary_in=chardevid,outdev=chardevid,iothread=id[,vnet_hdr_support][,notify_dev=id][,compare_timeout=@var{ms}][,expired_scan_cycle=@var{ms}][,max_queue_size=@var{size}]``
+    ``-object colo-compare,id=id,primary_in=chardevid,secondary_in=chardevid,outdev=chardevid,iothread=id[,netdev=netdevid][,vnet_hdr_support][,notify_dev=id][,compare_timeout=@var{ms}][,expired_scan_cycle=@var{ms}][,max_queue_size=@var{size}]``
         Colo-compare gets packet from primary\_in chardevid and
         secondary\_in, then compare whether the payload of primary packet
         and secondary packet are the same. If same, it will output
         primary packet to out\_dev, else it will notify COLO-framework to do
-        checkpoint and send primary packet to out\_dev. In order to
+        checkpoint and send primary packet to out\_dev. Although colo-compare
+        does not depend on network device directly, netdevid helps
+        colo-compare know what network driver it is working on. In order to
         improve efficiency, we need to put the task of comparison in
         another iothread. If it has the vnet\_hdr\_support flag,
         colo compare will send/recv packet with vnet\_hdr\_len.
#2 lore raw

Hi Jason~

Have any comments or updates for this patch?

Thanks
Chen

Quoted 325 lines

-----Original Message-----
From: Markus Armbruster <[email protected]>
Sent: Tuesday, October 12, 2021 5:31 PM
To: Zhang, Chen <[email protected]>
Cc: Jason Wang <[email protected]>; Eric Blake <[email protected]>; qemu-dev <[email protected]>; Li Zhijian <[email protected]>; Lukas Straub <[email protected]>; Tao Xu <[email protected]>
Subject: Re: [PATCH V3] net/colo: check vnet_hdr_support flag when using virtio-net

Jason, did this fall through the cracks?

Quoted 1 line · Zhang Chen

When COLO use only one vnet_hdr_support parameter between COLO

network

Quoted 14 lines

filter(filter-mirror, filter-redirector or filter-rewriter and colo-compare, packet will not be parsed correctly. Acquire network driver related to COLO, if it is nirtio-net, check vnet_hdr_support flag of COLO network filter and colo-compare.

---

Changelog:
v3:
Fix some typos.
Rebased for Qemu 6.2.

Quoted 5 lines

v2:
Detect virtio-net driver and apply vnet_hdr_support automatically. (Jason)

---
 net/colo-compare.c    | 57

+++++++++++++++++++++++++++++++++++++++++++

net/colo-compare.c static void

Quoted 83 lines
 net/colo.c            | 20 +++++++++++++++
 net/colo.h            |  4 +++
 net/filter-mirror.c   | 21 ++++++++++++++++
 net/filter-rewriter.c | 10 ++++++++
 qapi/qom.json         |  6 +++++
 qemu-options.hx       |  6 +++--
 7 files changed, 122 insertions(+), 2 deletions(-)
net/colo-compare.c index renamed from net/colo-compare.c +0 -0

b100e7b51f..870bd05a41 100644

net/colo-compare.c +43 -0
@@ -110,6 +110,7 @@ struct CompareState {
     char *sec_indev;
     char *outdev;
     char *notify_dev;
+    char *netdev;
     CharBackend chr_pri_in;
     CharBackend chr_sec_in;
     CharBackend chr_out;
@@ -838,6 +839,28 @@ static int compare_chr_can_read(void *opaque)
     return COMPARE_READ_LEN_MAX;
 }
 
+static int colo_set_default_netdev(void *opaque, QemuOpts *opts,
+Error **errp) {
+    const char *colo_obj_type, *netdev_from_filter;
+    char **netdev = (char **)opaque;
+
+    colo_obj_type = qemu_opt_get(opts, "qom-type");
+
+    if (colo_obj_type &&
+        (strcmp(colo_obj_type, "filter-mirror") == 0 ||
+         strcmp(colo_obj_type, "filter-redirector") == 0 ||
+         strcmp(colo_obj_type, "filter-rewriter") == 0)) {
+        netdev_from_filter = qemu_opt_get(opts, "netdev");
+        if (*netdev == NULL) {
+            *netdev = g_strdup(netdev_from_filter);
+        } else if (strcmp(*netdev, netdev_from_filter) != 0) {
+            warn_report("%s is using a different netdev from other COLO "
+                        "component", colo_obj_type);
+        }
+    }
+    return 0;
+}
+
 /*
  * Called from the main thread on the primary for packets
  * arriving over the socket from the primary.
@@ -1050,6 +1073,21 @@ static void compare_set_vnet_hdr(Object *obj,
     s->vnet_hdr = value;
 }
 
+static char *compare_get_netdev(Object *obj, Error **errp) {
+    CompareState *s = COLO_COMPARE(obj);
+
+    return g_strdup(s->netdev);
+}
+
+static void compare_set_netdev(Object *obj, const char *value, Error
+**errp) {
+    CompareState *s = COLO_COMPARE(obj);
+
+    g_free(s->netdev);
+    s->netdev = g_strdup(value);
+}
+
 static char *compare_get_notify_dev(Object *obj, Error **errp)  {
     CompareState *s = COLO_COMPARE(obj); @@ -1274,6 +1312,12 @@
 static void colo_compare_complete(UserCreatable *uc, Error **errp)
         max_queue_size = MAX_QUEUE_SIZE;
     }
 
+    if (!s->netdev) {
+        /* Set default netdev as the first colo netfilter found */
+        qemu_opts_foreach(qemu_find_opts("object"),
+                          colo_set_default_netdev, &s->netdev, NULL);
+    }
+
     if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
         !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
         return;
@@ -1289,6 +1333,16 @@ static void

colo_compare_complete(UserCreatable *uc, Error **errp)

Quoted 18 lines
net/colo-compare.c +10 -0
         return;
     }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, s->netdev, NULL)) {
+        /*
+         * colo compare needs 'vnet_hdr_support' when it works on virtio-net,
+         * add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
     net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr);
     net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize,
 s->vnet_hdr);

net/colo-compare.c static void colo_compare_init(Object *obj) · line 1455

Quoted 2 lines
net/colo-compare.c +0 -0
@@ -1400,6 +1454,9 @@ static void colo_compare_init(Object *obj)
     s->vnet_hdr = false;
     object_property_add_bool(obj, "vnet_hdr_support",

compare_get_vnet_hdr,

net/filter-mirror.c static void filter_mirror_setup(NetFilterState *nf,

Quoted 70 lines
net/colo-compare.c +3 -0
                              compare_set_vnet_hdr);
+    /* colo compare can't varify that netdev is correct */
+    object_property_add_str(obj, "netdev", compare_get_netdev,
+                            compare_set_netdev);
 }
 
 void colo_compare_cleanup(void)
net/colo.c +20 -0
index 3a3e6e89a0..4a03780f45 100644
@@ -243,3 +243,23 @@ bool connection_has_tracked(GHashTable
 *connection_track_table,
 
     return conn ? true : false;
 }
+
+/* check the network driver related to COLO, return 1 if it is
+virtio-net */ int vnet_driver_check(void *opaque, QemuOpts *opts,
+Error **errp) {
+    const char *driver_type, *netdev_from_driver;
+    char *netdev_from_filter = (char *)opaque;
+
+    driver_type = qemu_opt_get(opts, "driver");
+    netdev_from_driver = qemu_opt_get(opts, "netdev");
+
+    if (!driver_type || !netdev_from_driver || !netdev_from_filter) {
+        return 0;
+    }
+
+    if (g_str_has_prefix(driver_type, "virtio-net") &&
+        strcmp(netdev_from_driver, netdev_from_filter) == 0) {
+        return 1;
+    }
+    return 0;
+}
net/colo.h +3 -0
index d91cd245c4..d401fc76b6 100644
@@ -18,6 +18,9 @@
 #include "qemu/jhash.h"
 #include "qemu/timer.h"
 #include "net/eth.h"
+#include "qemu/option.h"
+#include "qemu/option_int.h"
+#include "qemu/config-file.h"
 
 #define HASHTABLE_MAX_SIZE 16384
 
@@ -104,5 +107,6 @@ Packet *packet_new(const void *data, int size, int

vnet_hdr_len); Packet *packet_new_nocopy(void *data, int size, int vnet_hdr_len); void packet_destroy(void *opaque, void *user_data); void packet_destroy_partial(void *opaque, void *user_data);

net/colo.h +1 -0
+int vnet_driver_check(void *opaque, QemuOpts *opts, Error **errp);
 
 #endif /* NET_COLO_H */
net/filter-mirror.c index renamed from net/filter-mirror.c +0 -0

f20240cc9f..69ca9c9839 100644

net/filter-mirror.c +1 -0
@@ -12,6 +12,7 @@
 #include "qemu/osdep.h"
 #include "net/filter.h"
 #include "net/net.h"
+#include "net/colo.h"
 #include "qapi/error.h"
 #include "qom/object.h"
 #include "qemu/main-loop.h"
@@ -224,6 +225,16 @@ static void filter_mirror_setup(NetFilterState *nf,

Error **errp)

net/filter-mirror.c static void filter_redirector_setup(NetFilterState

Quoted 16 lines
net/filter-mirror.c +10 -0
         return;
     }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter mirror needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
     qemu_chr_fe_init(&s->chr_out, chr, errp);  }
 
@@ -252,6 +263,16 @@ static void filter_redirector_setup(NetFilterState

*nf, Error **errp)

Quoted 25 lines
net/filter-mirror.c +10 -0
         }
     }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter redirector needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
     net_socket_rs_init(&s->rs, redirector_rs_finalize, s->vnet_hdr);
 
     if (s->indev) {
net/filter-rewriter.c index renamed from net/filter-rewriter.c +0 -0

cb3a96cde1..637ef4ce71 100644

net/filter-rewriter.c +1 -0
@@ -388,6 +388,16 @@ static void colo_rewriter_setup(NetFilterState
 *nf, Error **errp)  {
     RewriterState *s = FILTER_REWRITER(nf);
 
+    if (!s->vnet_hdr &&

net/filter-rewriter.c static void colo_rewriter_setup(NetFilterState · line 401

Quoted 10 lines
net/filter-rewriter.c +9 -0
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter rewriter needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
     s->connection_track_table =

g_hash_table_new_full(connection_key_hash,

qapi/qom.json line 241

Quoted 8 lines
net/filter-rewriter.c +0 -1
                                                       connection_key_equal,
                                                       g_free, diff
--git a/qapi/qom.json b/qapi/qom.json index a25616bc7a..5760107160

100644

qapi/qom.json +0 -0
@@ -241,6 +241,11 @@
 # @notify_dev: name of the character device backend to be used to

communicate

qapi/qom.json

Quoted 8 lines
qapi/qom.json +5 -0
 #              with the remote colo-frame (only for Xen COLO)
 #
+# @netdev: id of the network device backend to colo-compare. Although
+#          colo-compare doesn't depend on network device directly, this
+#          parameter helps colo-compare know what network driver it is
+#          working on.(since 6.2)
+#
 # @compare_timeout: the maximum time to hold a packet from

@primary_in for

Quoted 14 lines
qapi/qom.json +1 -0
 #                   comparison with an incoming packet on @secondary_in in
 #                   milliseconds (default: 3000)
@@ -264,6 +269,7 @@
             'outdev': 'str',
             'iothread': 'str',
             '*notify_dev': 'str',
+            '*netdev': 'str',
             '*compare_timeout': 'uint64',
             '*expired_scan_cycle': 'uint32',
             '*max_queue_size': 'uint32', diff --git a/qemu-options.hx

b/qemu-options.hx index 8f603cc7e6..250937fbbf 100644

qemu-options.hx SRST · line 4967

Quoted 4 lines
qemu-options.hx +0 -1
@@ -4964,12 +4964,14 @@ SRST
         stored. The file format is libpcap, so it can be analyzed with
         tools such as tcpdump or Wireshark.
 
-    ``-object colo-

compare,id=id,primary_in=chardevid,secondary_in=chardevid,outdev=chard evid,iothread=id[,vnet_hdr_support][,notify_dev=id][,compare_timeout=@ var{ms}][,expired_scan_cycle=@var{ms}][,max_queue_size=@var{size}]``

qemu-options.hx SRST · line 4968

Quoted 2 lines
qemu-options.hx +2 -0
+    ``-object
+ colo-

compare,id=id,primary_in=chardevid,secondary_in=chardevid,outde

qemu-options.hx SRST · line 4970

Quoted 2 lines
qemu-options.hx +2 -0
+ v=chardevid,iothread=id[,netdev=netdevid][,vnet_hdr_support][,notify
+

_dev=id][,compare_timeout=@var{ms}][,expired_scan_cycle=@var{ms}][, m

qemu-options.hx SRST

Quoted 7 lines
qemu-options.hx +2 -1
+ ax_queue_size=@var{size}]``
         Colo-compare gets packet from primary\_in chardevid and
         secondary\_in, then compare whether the payload of primary packet
         and secondary packet are the same. If same, it will output
         primary packet to out\_dev, else it will notify COLO-framework to do
-        checkpoint and send primary packet to out\_dev. In order to
+        checkpoint and send primary packet to out\_dev. Although colo-

compare

Quoted 6 lines
qemu-options.hx +3 -0
+        does not depend on network device directly, netdevid helps
+        colo-compare know what network driver it is working on. In
+ order to
         improve efficiency, we need to put the task of comparison in
         another iothread. If it has the vnet\_hdr\_support flag,
         colo compare will send/recv packet with vnet\_hdr\_len.
#3 lore raw
In reply to Zhang Chen
Quoted 5 lines · Zhang Chen

When COLO use only one vnet_hdr_support parameter between COLO network filter(filter-mirror, filter-redirector or filter-rewriter and colo-compare, packet will not be parsed correctly. Acquire network driver related to COLO, if it is nirtio-net,

Typo.

Quoted 249 lines

check vnet_hdr_support flag of COLO network filter
and colo-compare.

---

Changelog:
v3:
Fix some typos.
Rebased for Qemu 6.2.

v2:
Detect virtio-net driver and apply vnet_hdr_support automatically. (Jason)

---
  net/colo-compare.c    | 57 +++++++++++++++++++++++++++++++++++++++++++
  net/colo.c            | 20 +++++++++++++++
  net/colo.h            |  4 +++
  net/filter-mirror.c   | 21 ++++++++++++++++
  net/filter-rewriter.c | 10 ++++++++
  qapi/qom.json         |  6 +++++
  qemu-options.hx       |  6 +++--
  7 files changed, 122 insertions(+), 2 deletions(-)
net/colo-compare.c +57 -0
index b100e7b51f..870bd05a41 100644
@@ -110,6 +110,7 @@ struct CompareState {
      char *sec_indev;
      char *outdev;
      char *notify_dev;
+    char *netdev;
      CharBackend chr_pri_in;
      CharBackend chr_sec_in;
      CharBackend chr_out;
@@ -838,6 +839,28 @@ static int compare_chr_can_read(void *opaque)
      return COMPARE_READ_LEN_MAX;
  }
 
+static int colo_set_default_netdev(void *opaque, QemuOpts *opts, Error **errp)
+{
+    const char *colo_obj_type, *netdev_from_filter;
+    char **netdev = (char **)opaque;
+
+    colo_obj_type = qemu_opt_get(opts, "qom-type");
+
+    if (colo_obj_type &&
+        (strcmp(colo_obj_type, "filter-mirror") == 0 ||
+         strcmp(colo_obj_type, "filter-redirector") == 0 ||
+         strcmp(colo_obj_type, "filter-rewriter") == 0)) {
+        netdev_from_filter = qemu_opt_get(opts, "netdev");
+        if (*netdev == NULL) {
+            *netdev = g_strdup(netdev_from_filter);
+        } else if (strcmp(*netdev, netdev_from_filter) != 0) {
+            warn_report("%s is using a different netdev from other COLO "
+                        "component", colo_obj_type);
+        }
+    }
+    return 0;
+}
+
  /*
   * Called from the main thread on the primary for packets
   * arriving over the socket from the primary.
@@ -1050,6 +1073,21 @@ static void compare_set_vnet_hdr(Object *obj,
      s->vnet_hdr = value;
  }
 
+static char *compare_get_netdev(Object *obj, Error **errp)
+{
+    CompareState *s = COLO_COMPARE(obj);
+
+    return g_strdup(s->netdev);
+}
+
+static void compare_set_netdev(Object *obj, const char *value, Error **errp)
+{
+    CompareState *s = COLO_COMPARE(obj);
+
+    g_free(s->netdev);
+    s->netdev = g_strdup(value);
+}
+
  static char *compare_get_notify_dev(Object *obj, Error **errp)
  {
      CompareState *s = COLO_COMPARE(obj);
@@ -1274,6 +1312,12 @@ static void colo_compare_complete(UserCreatable *uc, Error **errp)
          max_queue_size = MAX_QUEUE_SIZE;
      }
 
+    if (!s->netdev) {
+        /* Set default netdev as the first colo netfilter found */
+        qemu_opts_foreach(qemu_find_opts("object"),
+                          colo_set_default_netdev, &s->netdev, NULL);
+    }
+
      if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
          !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
          return;
@@ -1289,6 +1333,16 @@ static void colo_compare_complete(UserCreatable *uc, Error **errp)
          return;
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, s->netdev, NULL)) {
+        /*
+         * colo compare needs 'vnet_hdr_support' when it works on virtio-net,
+         * add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr);
      net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize, s->vnet_hdr);
 
@@ -1400,6 +1454,9 @@ static void colo_compare_init(Object *obj)
      s->vnet_hdr = false;
      object_property_add_bool(obj, "vnet_hdr_support", compare_get_vnet_hdr,
                               compare_set_vnet_hdr);
+    /* colo compare can't varify that netdev is correct */
+    object_property_add_str(obj, "netdev", compare_get_netdev,
+                            compare_set_netdev);
  }
 
  void colo_compare_cleanup(void)
net/colo.c +20 -0
index 3a3e6e89a0..4a03780f45 100644
@@ -243,3 +243,23 @@ bool connection_has_tracked(GHashTable *connection_track_table,
 
      return conn ? true : false;
  }
+
+/* check the network driver related to COLO, return 1 if it is virtio-net */
+int vnet_driver_check(void *opaque, QemuOpts *opts, Error **errp)
+{
+    const char *driver_type, *netdev_from_driver;
+    char *netdev_from_filter = (char *)opaque;
+
+    driver_type = qemu_opt_get(opts, "driver");
+    netdev_from_driver = qemu_opt_get(opts, "netdev");
+
+    if (!driver_type || !netdev_from_driver || !netdev_from_filter) {
+        return 0;
+    }
+
+    if (g_str_has_prefix(driver_type, "virtio-net") &&
+        strcmp(netdev_from_driver, netdev_from_filter) == 0) {
+        return 1;
+    }
+    return 0;
+}
net/colo.h +4 -0
index d91cd245c4..d401fc76b6 100644
@@ -18,6 +18,9 @@
  #include "qemu/jhash.h"
  #include "qemu/timer.h"
  #include "net/eth.h"
+#include "qemu/option.h"
+#include "qemu/option_int.h"
+#include "qemu/config-file.h"
 
  #define HASHTABLE_MAX_SIZE 16384
 
@@ -104,5 +107,6 @@ Packet *packet_new(const void *data, int size, int vnet_hdr_len);
  Packet *packet_new_nocopy(void *data, int size, int vnet_hdr_len);
  void packet_destroy(void *opaque, void *user_data);
  void packet_destroy_partial(void *opaque, void *user_data);
+int vnet_driver_check(void *opaque, QemuOpts *opts, Error **errp);
 
  #endif /* NET_COLO_H */
net/filter-mirror.c +21 -0
index f20240cc9f..69ca9c9839 100644
@@ -12,6 +12,7 @@
  #include "qemu/osdep.h"
  #include "net/filter.h"
  #include "net/net.h"
+#include "net/colo.h"
  #include "qapi/error.h"
  #include "qom/object.h"
  #include "qemu/main-loop.h"
@@ -224,6 +225,16 @@ static void filter_mirror_setup(NetFilterState *nf, Error **errp)
          return;
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter mirror needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      qemu_chr_fe_init(&s->chr_out, chr, errp);
  }
 
@@ -252,6 +263,16 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
          }
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter redirector needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      net_socket_rs_init(&s->rs, redirector_rs_finalize, s->vnet_hdr);
 
      if (s->indev) {
net/filter-rewriter.c +10 -0
index cb3a96cde1..637ef4ce71 100644
@@ -388,6 +388,16 @@ static void colo_rewriter_setup(NetFilterState *nf, Error **errp)
  {
      RewriterState *s = FILTER_REWRITER(nf);
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter rewriter needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      s->connection_track_table = g_hash_table_new_full(connection_key_hash,
                                                        connection_key_equal,
                                                        g_free,
qapi/qom.json +0 -0
index a25616bc7a..5760107160 100644

qapi/qom.json line 247

Quoted 7 lines
qapi/qom.json +4 -0
@@ -241,6 +241,11 @@
  # @notify_dev: name of the character device backend to be used to communicate
  #              with the remote colo-frame (only for Xen COLO)
  #
+# @netdev: id of the network device backend to colo-compare. Although
+#          colo-compare doesn't depend on network device directly, this
+#          parameter helps colo-compare know what network driver it is
+#          working on.(since 6.2)

This looks kind of not elegant. We don't want to couple COLO with netdev.

I wonder if we can do this without adding netdev.

Thanks

Quoted 33 lines
qapi/qom.json +2 -0
+#
  # @compare_timeout: the maximum time to hold a packet from @primary_in for
  #                   comparison with an incoming packet on @secondary_in in
  #                   milliseconds (default: 3000)
@@ -264,6 +269,7 @@
              'outdev': 'str',
              'iothread': 'str',
              '*notify_dev': 'str',
+            '*netdev': 'str',
              '*compare_timeout': 'uint64',
              '*expired_scan_cycle': 'uint32',
              '*max_queue_size': 'uint32',
qemu-options.hx +4 -2
index 8f603cc7e6..250937fbbf 100644
@@ -4964,12 +4964,14 @@ SRST
          stored. The file format is libpcap, so it can be analyzed with
          tools such as tcpdump or Wireshark.
 
-    ``-object colo-compare,id=id,primary_in=chardevid,secondary_in=chardevid,outdev=chardevid,iothread=id[,vnet_hdr_support][,notify_dev=id][,compare_timeout=@var{ms}][,expired_scan_cycle=@var{ms}][,max_queue_size=@var{size}]``
+    ``-object colo-compare,id=id,primary_in=chardevid,secondary_in=chardevid,outdev=chardevid,iothread=id[,netdev=netdevid][,vnet_hdr_support][,notify_dev=id][,compare_timeout=@var{ms}][,expired_scan_cycle=@var{ms}][,max_queue_size=@var{size}]``
          Colo-compare gets packet from primary\_in chardevid and
          secondary\_in, then compare whether the payload of primary packet
          and secondary packet are the same. If same, it will output
          primary packet to out\_dev, else it will notify COLO-framework to do
-        checkpoint and send primary packet to out\_dev. In order to
+        checkpoint and send primary packet to out\_dev. Although colo-compare
+        does not depend on network device directly, netdevid helps
+        colo-compare know what network driver it is working on. In order to
          improve efficiency, we need to put the task of comparison in
          another iothread. If it has the vnet\_hdr\_support flag,
          colo compare will send/recv packet with vnet\_hdr\_len.
#4 lore raw
Quoted 15 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Tuesday, October 19, 2021 3:39 PM
To: Zhang, Chen <[email protected]>; Eric Blake <[email protected]>; Markus Armbruster <[email protected]>
Cc: qemu-dev <[email protected]>; Li Zhijian <[email protected]>; Lukas Straub <[email protected]>; Tao Xu <[email protected]>
Subject: Re: [PATCH V3] net/colo: check vnet_hdr_support flag when using virtio-net

在 2021/9/18 上午10:04, Zhang Chen 写道:

Quoted 8 lines
Quoted 1 line

When COLO use only one vnet_hdr_support parameter between COLO

network

Quoted 3 lines

filter(filter-mirror, filter-redirector or filter-rewriter and colo-compare, packet will not be parsed correctly. Acquire network driver related to COLO, if it is nirtio-net,

Typo.

Oh~ will fix in next version.

Quoted 271 lines
Quoted 13 lines

check vnet_hdr_support flag of COLO network filter and colo-compare.

---

Changelog:
v3:
Fix some typos.
Rebased for Qemu 6.2.

Quoted 5 lines

v2:
Detect virtio-net driver and apply vnet_hdr_support automatically. (Jason)

---
  net/colo-compare.c    | 57

+++++++++++++++++++++++++++++++++++++++++++

net/colo-compare.c static void

Quoted 84 lines
  net/colo.c            | 20 +++++++++++++++
  net/colo.h            |  4 +++
  net/filter-mirror.c   | 21 ++++++++++++++++
  net/filter-rewriter.c | 10 ++++++++
  qapi/qom.json         |  6 +++++
  qemu-options.hx       |  6 +++--
  7 files changed, 122 insertions(+), 2 deletions(-)
net/colo-compare.c index renamed from net/colo-compare.c +0 -0

b100e7b51f..870bd05a41 100644

net/colo-compare.c +37 -0
@@ -110,6 +110,7 @@ struct CompareState {
      char *sec_indev;
      char *outdev;
      char *notify_dev;
+    char *netdev;
      CharBackend chr_pri_in;
      CharBackend chr_sec_in;
      CharBackend chr_out;
@@ -838,6 +839,28 @@ static int compare_chr_can_read(void *opaque)
      return COMPARE_READ_LEN_MAX;
  }
 
+static int colo_set_default_netdev(void *opaque, QemuOpts *opts,
+Error **errp) {
+    const char *colo_obj_type, *netdev_from_filter;
+    char **netdev = (char **)opaque;
+
+    colo_obj_type = qemu_opt_get(opts, "qom-type");
+
+    if (colo_obj_type &&
+        (strcmp(colo_obj_type, "filter-mirror") == 0 ||
+         strcmp(colo_obj_type, "filter-redirector") == 0 ||
+         strcmp(colo_obj_type, "filter-rewriter") == 0)) {
+        netdev_from_filter = qemu_opt_get(opts, "netdev");
+        if (*netdev == NULL) {
+            *netdev = g_strdup(netdev_from_filter);
+        } else if (strcmp(*netdev, netdev_from_filter) != 0) {
+            warn_report("%s is using a different netdev from other COLO "
+                        "component", colo_obj_type);
+        }
+    }
+    return 0;
+}
+
  /*
   * Called from the main thread on the primary for packets
   * arriving over the socket from the primary.
@@ -1050,6 +1073,21 @@ static void compare_set_vnet_hdr(Object *obj,
      s->vnet_hdr = value;
  }
 
+static char *compare_get_netdev(Object *obj, Error **errp) {
+    CompareState *s = COLO_COMPARE(obj);
+
+    return g_strdup(s->netdev);
+}
+
+static void compare_set_netdev(Object *obj, const char *value, Error
+**errp) {
+    CompareState *s = COLO_COMPARE(obj);
+
+    g_free(s->netdev);
+    s->netdev = g_strdup(value);
+}
+
  static char *compare_get_notify_dev(Object *obj, Error **errp)
  {
      CompareState *s = COLO_COMPARE(obj); @@ -1274,6 +1312,12 @@

static void colo_compare_complete(UserCreatable *uc, Error **errp)

          max_queue_size = MAX_QUEUE_SIZE;
      }
net/colo-compare.c +6 -0
+    if (!s->netdev) {
+        /* Set default netdev as the first colo netfilter found */
+        qemu_opts_foreach(qemu_find_opts("object"),
+                          colo_set_default_netdev, &s->netdev, NULL);
+    }
+
      if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
          !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
          return;
@@ -1289,6 +1333,16 @@ static void

colo_compare_complete(UserCreatable *uc, Error **errp)

Quoted 18 lines
net/colo-compare.c +10 -0
          return;
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, s->netdev, NULL)) {
+        /*
+         * colo compare needs 'vnet_hdr_support' when it works on virtio-net,
+         * add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr);
      net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize,
 s->vnet_hdr);

net/colo-compare.c static void colo_compare_init(Object *obj) · line 1455

Quoted 2 lines
net/colo-compare.c +0 -0
@@ -1400,6 +1454,9 @@ static void colo_compare_init(Object *obj)
      s->vnet_hdr = false;
      object_property_add_bool(obj, "vnet_hdr_support",

compare_get_vnet_hdr,

net/colo.h Packet *packet_new(const void *data, int size, int

Quoted 51 lines
net/colo-compare.c +3 -0
                               compare_set_vnet_hdr);
+    /* colo compare can't varify that netdev is correct */
+    object_property_add_str(obj, "netdev", compare_get_netdev,
+                            compare_set_netdev);
  }
 
  void colo_compare_cleanup(void)
net/colo.c +20 -0
index 3a3e6e89a0..4a03780f45 100644
@@ -243,3 +243,23 @@ bool connection_has_tracked(GHashTable
 *connection_track_table,
 
      return conn ? true : false;
  }
+
+/* check the network driver related to COLO, return 1 if it is
+virtio-net */ int vnet_driver_check(void *opaque, QemuOpts *opts,
+Error **errp) {
+    const char *driver_type, *netdev_from_driver;
+    char *netdev_from_filter = (char *)opaque;
+
+    driver_type = qemu_opt_get(opts, "driver");
+    netdev_from_driver = qemu_opt_get(opts, "netdev");
+
+    if (!driver_type || !netdev_from_driver || !netdev_from_filter) {
+        return 0;
+    }
+
+    if (g_str_has_prefix(driver_type, "virtio-net") &&
+        strcmp(netdev_from_driver, netdev_from_filter) == 0) {
+        return 1;
+    }
+    return 0;
+}
net/colo.h +3 -0
index d91cd245c4..d401fc76b6 100644
@@ -18,6 +18,9 @@
  #include "qemu/jhash.h"
  #include "qemu/timer.h"
  #include "net/eth.h"
+#include "qemu/option.h"
+#include "qemu/option_int.h"
+#include "qemu/config-file.h"
 
  #define HASHTABLE_MAX_SIZE 16384
 
@@ -104,5 +107,6 @@ Packet *packet_new(const void *data, int size, int

vnet_hdr_len);

net/filter-mirror.c static void filter_mirror_setup(NetFilterState *nf,

Quoted 19 lines
net/colo.h +1 -0
  Packet *packet_new_nocopy(void *data, int size, int vnet_hdr_len);
  void packet_destroy(void *opaque, void *user_data);
  void packet_destroy_partial(void *opaque, void *user_data);
+int vnet_driver_check(void *opaque, QemuOpts *opts, Error **errp);
 
  #endif /* NET_COLO_H */
net/filter-mirror.c index renamed from net/filter-mirror.c +0 -0

f20240cc9f..69ca9c9839 100644

net/filter-mirror.c +1 -0
@@ -12,6 +12,7 @@
  #include "qemu/osdep.h"
  #include "net/filter.h"
  #include "net/net.h"
+#include "net/colo.h"
  #include "qapi/error.h"
  #include "qom/object.h"
  #include "qemu/main-loop.h"
@@ -224,6 +225,16 @@ static void filter_mirror_setup(NetFilterState *nf,

Error **errp)

net/filter-mirror.c static void filter_redirector_setup(NetFilterState

Quoted 17 lines
net/filter-mirror.c +10 -0
          return;
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter mirror needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      qemu_chr_fe_init(&s->chr_out, chr, errp);
  }
 
@@ -252,6 +263,16 @@ static void filter_redirector_setup(NetFilterState

*nf, Error **errp)

net/filter-rewriter.c static void colo_rewriter_setup(NetFilterState *nf,

Quoted 21 lines
net/filter-mirror.c +10 -0
          }
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter redirector needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      net_socket_rs_init(&s->rs, redirector_rs_finalize, s->vnet_hdr);
 
      if (s->indev) {
net/filter-rewriter.c index renamed from net/filter-rewriter.c +0 -0

cb3a96cde1..637ef4ce71 100644

net/filter-rewriter.c +0 -0
@@ -388,6 +388,16 @@ static void colo_rewriter_setup(NetFilterState *nf,

Error **errp)

Quoted 4 lines
net/filter-rewriter.c +1 -0
  {
      RewriterState *s = FILTER_REWRITER(nf);
 
+    if (!s->vnet_hdr &&

net/filter-rewriter.c static void colo_rewriter_setup(NetFilterState *nf, Error **errp)

Quoted 10 lines
net/filter-rewriter.c +9 -0
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter rewriter needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      s->connection_track_table =

g_hash_table_new_full(connection_key_hash,

qapi/qom.json line 241

Quoted 8 lines
net/filter-rewriter.c +0 -1
                                                        connection_key_equal,
                                                        g_free, diff
--git a/qapi/qom.json b/qapi/qom.json index a25616bc7a..5760107160

100644

qapi/qom.json +0 -0
@@ -241,6 +241,11 @@
  # @notify_dev: name of the character device backend to be used to

communicate

qapi/qom.json line 247

Quoted 6 lines
qapi/qom.json +4 -0
  #              with the remote colo-frame (only for Xen COLO)
  #
+# @netdev: id of the network device backend to colo-compare. Although
+#          colo-compare doesn't depend on network device directly, this
+#          parameter helps colo-compare know what network driver it is
+#          working on.(since 6.2)

This looks kind of not elegant. We don't want to couple COLO with netdev.

Quoted 1 line

I wonder if we can do this without adding netdev.

Yes, current COLO is not bind to netdev, but here we want to auto fix configuration issue.
To avoid wrong setting other non-colo related existing filter, COLO need know the colo-effect netdev to setup. And this parameter is optional, this patch will setup default colo-effect netdev in colo_set_default_netdev(). Or any good idea?

Thanks
Chen

Quoted 50 lines

Thanks

qapi/qom.json line 249

Quoted 2 lines
qapi/qom.json +1 -0
+#
  # @compare_timeout: the maximum time to hold a packet from

@primary_in for

Quoted 15 lines
qapi/qom.json +1 -0
  #                   comparison with an incoming packet on @secondary_in in
  #                   milliseconds (default: 3000)
@@ -264,6 +269,7 @@
              'outdev': 'str',
              'iothread': 'str',
              '*notify_dev': 'str',
+            '*netdev': 'str',
              '*compare_timeout': 'uint64',
              '*expired_scan_cycle': 'uint32',
              '*max_queue_size': 'uint32', diff --git

a/qemu-options.hx b/qemu-options.hx index 8f603cc7e6..250937fbbf 100644

qemu-options.hx SRST · line 4967

Quoted 4 lines
qemu-options.hx +0 -1
@@ -4964,12 +4964,14 @@ SRST
          stored. The file format is libpcap, so it can be analyzed with
          tools such as tcpdump or Wireshark.
 
-    ``-object colo-

compare,id=id,primary_in=chardevid,secondary_in=chardevid,outdev=chard evid,iothread=id[,vnet_hdr_support][,notify_dev=id][,compare_timeout=@ var{ms}][,expired_scan_cycle=@var{ms}][,max_queue_size=@var{size}]``

qemu-options.hx SRST · line 4968

Quoted 2 lines
qemu-options.hx +2 -0
+    ``-object
+ colo-

compare,id=id,primary_in=chardevid,secondary_in=chardevid,outde

qemu-options.hx SRST · line 4970

Quoted 2 lines
qemu-options.hx +2 -0
+ v=chardevid,iothread=id[,netdev=netdevid][,vnet_hdr_support][,notify
+

_dev=id][,compare_timeout=@var{ms}][,expired_scan_cycle=@var{ms}][, m

qemu-options.hx SRST

Quoted 7 lines
qemu-options.hx +2 -1
+ ax_queue_size=@var{size}]``
          Colo-compare gets packet from primary\_in chardevid and
          secondary\_in, then compare whether the payload of primary packet
          and secondary packet are the same. If same, it will output
          primary packet to out\_dev, else it will notify COLO-framework to do
-        checkpoint and send primary packet to out\_dev. In order to
+        checkpoint and send primary packet to out\_dev. Although colo-

compare

Quoted 6 lines
qemu-options.hx +3 -0
+        does not depend on network device directly, netdevid helps
+        colo-compare know what network driver it is working on. In
+ order to
          improve efficiency, we need to put the task of comparison in
          another iothread. If it has the vnet\_hdr\_support flag,
          colo compare will send/recv packet with vnet\_hdr\_len.
#5 lore raw
Quoted 298 lines · Chen
Quoted 16 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Tuesday, October 19, 2021 3:39 PM
To: Zhang, Chen <[email protected]>; Eric Blake <[email protected]>; Markus Armbruster <[email protected]>
Cc: qemu-dev <[email protected]>; Li Zhijian <[email protected]>; Lukas Straub <[email protected]>; Tao Xu <[email protected]>
Subject: Re: [PATCH V3] net/colo: check vnet_hdr_support flag when using virtio-net

在 2021/9/18 上午10:04, Zhang Chen 写道:

Quoted 8 lines
Quoted 1 line

When COLO use only one vnet_hdr_support parameter between COLO

network

Quoted 3 lines

filter(filter-mirror, filter-redirector or filter-rewriter and colo-compare, packet will not be parsed correctly. Acquire network driver related to COLO, if it is nirtio-net,

Typo.

Oh~ will fix in next version.

Quoted 271 lines
Quoted 13 lines

check vnet_hdr_support flag of COLO network filter and colo-compare.

---

Changelog:
v3:
Fix some typos.
Rebased for Qemu 6.2.

Quoted 5 lines

v2:
Detect virtio-net driver and apply vnet_hdr_support automatically. (Jason)

---
  net/colo-compare.c    | 57

+++++++++++++++++++++++++++++++++++++++++++

net/colo-compare.c static void

Quoted 84 lines
  net/colo.c            | 20 +++++++++++++++
  net/colo.h            |  4 +++
  net/filter-mirror.c   | 21 ++++++++++++++++
  net/filter-rewriter.c | 10 ++++++++
  qapi/qom.json         |  6 +++++
  qemu-options.hx       |  6 +++--
  7 files changed, 122 insertions(+), 2 deletions(-)
net/colo-compare.c index renamed from net/colo-compare.c +0 -0

b100e7b51f..870bd05a41 100644

net/colo-compare.c +37 -0
@@ -110,6 +110,7 @@ struct CompareState {
      char *sec_indev;
      char *outdev;
      char *notify_dev;
+    char *netdev;
      CharBackend chr_pri_in;
      CharBackend chr_sec_in;
      CharBackend chr_out;
@@ -838,6 +839,28 @@ static int compare_chr_can_read(void *opaque)
      return COMPARE_READ_LEN_MAX;
  }
 
+static int colo_set_default_netdev(void *opaque, QemuOpts *opts,
+Error **errp) {
+    const char *colo_obj_type, *netdev_from_filter;
+    char **netdev = (char **)opaque;
+
+    colo_obj_type = qemu_opt_get(opts, "qom-type");
+
+    if (colo_obj_type &&
+        (strcmp(colo_obj_type, "filter-mirror") == 0 ||
+         strcmp(colo_obj_type, "filter-redirector") == 0 ||
+         strcmp(colo_obj_type, "filter-rewriter") == 0)) {
+        netdev_from_filter = qemu_opt_get(opts, "netdev");
+        if (*netdev == NULL) {
+            *netdev = g_strdup(netdev_from_filter);
+        } else if (strcmp(*netdev, netdev_from_filter) != 0) {
+            warn_report("%s is using a different netdev from other COLO "
+                        "component", colo_obj_type);
+        }
+    }
+    return 0;
+}
+
  /*
   * Called from the main thread on the primary for packets
   * arriving over the socket from the primary.
@@ -1050,6 +1073,21 @@ static void compare_set_vnet_hdr(Object *obj,
      s->vnet_hdr = value;
  }
 
+static char *compare_get_netdev(Object *obj, Error **errp) {
+    CompareState *s = COLO_COMPARE(obj);
+
+    return g_strdup(s->netdev);
+}
+
+static void compare_set_netdev(Object *obj, const char *value, Error
+**errp) {
+    CompareState *s = COLO_COMPARE(obj);
+
+    g_free(s->netdev);
+    s->netdev = g_strdup(value);
+}
+
  static char *compare_get_notify_dev(Object *obj, Error **errp)
  {
      CompareState *s = COLO_COMPARE(obj); @@ -1274,6 +1312,12 @@

static void colo_compare_complete(UserCreatable *uc, Error **errp)

          max_queue_size = MAX_QUEUE_SIZE;
      }
net/colo-compare.c +6 -0
+    if (!s->netdev) {
+        /* Set default netdev as the first colo netfilter found */
+        qemu_opts_foreach(qemu_find_opts("object"),
+                          colo_set_default_netdev, &s->netdev, NULL);
+    }
+
      if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
          !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
          return;
@@ -1289,6 +1333,16 @@ static void

colo_compare_complete(UserCreatable *uc, Error **errp)

Quoted 18 lines
net/colo-compare.c +10 -0
          return;
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, s->netdev, NULL)) {
+        /*
+         * colo compare needs 'vnet_hdr_support' when it works on virtio-net,
+         * add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s->vnet_hdr);
      net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize,
 s->vnet_hdr);

net/colo-compare.c static void colo_compare_init(Object *obj) · line 1455

Quoted 2 lines
net/colo-compare.c +0 -0
@@ -1400,6 +1454,9 @@ static void colo_compare_init(Object *obj)
      s->vnet_hdr = false;
      object_property_add_bool(obj, "vnet_hdr_support",

compare_get_vnet_hdr,

net/colo.h Packet *packet_new(const void *data, int size, int

Quoted 51 lines
net/colo-compare.c +3 -0
                               compare_set_vnet_hdr);
+    /* colo compare can't varify that netdev is correct */
+    object_property_add_str(obj, "netdev", compare_get_netdev,
+                            compare_set_netdev);
  }
 
  void colo_compare_cleanup(void)
net/colo.c +20 -0
index 3a3e6e89a0..4a03780f45 100644
@@ -243,3 +243,23 @@ bool connection_has_tracked(GHashTable
 *connection_track_table,
 
      return conn ? true : false;
  }
+
+/* check the network driver related to COLO, return 1 if it is
+virtio-net */ int vnet_driver_check(void *opaque, QemuOpts *opts,
+Error **errp) {
+    const char *driver_type, *netdev_from_driver;
+    char *netdev_from_filter = (char *)opaque;
+
+    driver_type = qemu_opt_get(opts, "driver");
+    netdev_from_driver = qemu_opt_get(opts, "netdev");
+
+    if (!driver_type || !netdev_from_driver || !netdev_from_filter) {
+        return 0;
+    }
+
+    if (g_str_has_prefix(driver_type, "virtio-net") &&
+        strcmp(netdev_from_driver, netdev_from_filter) == 0) {
+        return 1;
+    }
+    return 0;
+}
net/colo.h +3 -0
index d91cd245c4..d401fc76b6 100644
@@ -18,6 +18,9 @@
  #include "qemu/jhash.h"
  #include "qemu/timer.h"
  #include "net/eth.h"
+#include "qemu/option.h"
+#include "qemu/option_int.h"
+#include "qemu/config-file.h"
 
  #define HASHTABLE_MAX_SIZE 16384
 
@@ -104,5 +107,6 @@ Packet *packet_new(const void *data, int size, int

vnet_hdr_len);

net/filter-mirror.c static void filter_mirror_setup(NetFilterState *nf,

Quoted 19 lines
net/colo.h +1 -0
  Packet *packet_new_nocopy(void *data, int size, int vnet_hdr_len);
  void packet_destroy(void *opaque, void *user_data);
  void packet_destroy_partial(void *opaque, void *user_data);
+int vnet_driver_check(void *opaque, QemuOpts *opts, Error **errp);
 
  #endif /* NET_COLO_H */
net/filter-mirror.c index renamed from net/filter-mirror.c +0 -0

f20240cc9f..69ca9c9839 100644

net/filter-mirror.c +1 -0
@@ -12,6 +12,7 @@
  #include "qemu/osdep.h"
  #include "net/filter.h"
  #include "net/net.h"
+#include "net/colo.h"
  #include "qapi/error.h"
  #include "qom/object.h"
  #include "qemu/main-loop.h"
@@ -224,6 +225,16 @@ static void filter_mirror_setup(NetFilterState *nf,

Error **errp)

net/filter-mirror.c static void filter_redirector_setup(NetFilterState

Quoted 17 lines
net/filter-mirror.c +10 -0
          return;
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter mirror needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      qemu_chr_fe_init(&s->chr_out, chr, errp);
  }
 
@@ -252,6 +263,16 @@ static void filter_redirector_setup(NetFilterState

*nf, Error **errp)

net/filter-rewriter.c static void colo_rewriter_setup(NetFilterState *nf,

Quoted 21 lines
net/filter-mirror.c +10 -0
          }
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter redirector needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      net_socket_rs_init(&s->rs, redirector_rs_finalize, s->vnet_hdr);
 
      if (s->indev) {
net/filter-rewriter.c index renamed from net/filter-rewriter.c +0 -0

cb3a96cde1..637ef4ce71 100644

net/filter-rewriter.c +0 -0
@@ -388,6 +388,16 @@ static void colo_rewriter_setup(NetFilterState *nf,

Error **errp)

Quoted 4 lines
net/filter-rewriter.c +1 -0
  {
      RewriterState *s = FILTER_REWRITER(nf);
 
+    if (!s->vnet_hdr &&

net/filter-rewriter.c static void colo_rewriter_setup(NetFilterState *nf, Error **errp)

Quoted 10 lines
net/filter-rewriter.c +9 -0
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter rewriter needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      s->connection_track_table =

g_hash_table_new_full(connection_key_hash,

qapi/qom.json line 241

Quoted 8 lines
net/filter-rewriter.c +0 -1
                                                        connection_key_equal,
                                                        g_free, diff
--git a/qapi/qom.json b/qapi/qom.json index a25616bc7a..5760107160

100644

qapi/qom.json +0 -0
@@ -241,6 +241,11 @@
  # @notify_dev: name of the character device backend to be used to

communicate

qapi/qom.json line 247

Quoted 6 lines
qapi/qom.json +4 -0
  #              with the remote colo-frame (only for Xen COLO)
  #
+# @netdev: id of the network device backend to colo-compare. Although
+#          colo-compare doesn't depend on network device directly, this
+#          parameter helps colo-compare know what network driver it is
+#          working on.(since 6.2)

This looks kind of not elegant. We don't want to couple COLO with netdev.

Quoted 3 lines
Quoted 1 line

I wonder if we can do this without adding netdev.

Yes, current COLO is not bind to netdev, but here we want to auto fix configuration issue.

If my memory is correct, the current design allows an external comparing module to work.

Quoted 3 lines

To avoid wrong setting other non-colo related existing filter, COLO need know the colo-effect netdev to setup. And this parameter is optional, this patch will setup default colo-effect netdev in colo_set_default_netdev(). Or any good idea?

How about

1) adding a handshake in the protocol before receiving packets or
2) add a header to the payload to show if it contains a vnet header or
3) go back to let mgmt set vnet_hdr=on/off (looks like the first proposal but it avoids the coupling).

Thanks

Quoted 55 lines

Thanks
Chen

Quoted 50 lines

Thanks

qapi/qom.json line 249

Quoted 2 lines
qapi/qom.json +1 -0
+#
  # @compare_timeout: the maximum time to hold a packet from

@primary_in for

Quoted 15 lines
qapi/qom.json +1 -0
  #                   comparison with an incoming packet on @secondary_in in
  #                   milliseconds (default: 3000)
@@ -264,6 +269,7 @@
              'outdev': 'str',
              'iothread': 'str',
              '*notify_dev': 'str',
+            '*netdev': 'str',
              '*compare_timeout': 'uint64',
              '*expired_scan_cycle': 'uint32',
              '*max_queue_size': 'uint32', diff --git

a/qemu-options.hx b/qemu-options.hx index 8f603cc7e6..250937fbbf 100644

qemu-options.hx SRST · line 4967

Quoted 4 lines
qemu-options.hx +0 -1
@@ -4964,12 +4964,14 @@ SRST
          stored. The file format is libpcap, so it can be analyzed with
          tools such as tcpdump or Wireshark.
 
-    ``-object colo-

compare,id=id,primary_in=chardevid,secondary_in=chardevid,outdev=chard evid,iothread=id[,vnet_hdr_support][,notify_dev=id][,compare_timeout=@ var{ms}][,expired_scan_cycle=@var{ms}][,max_queue_size=@var{size}]``

qemu-options.hx SRST · line 4968

Quoted 2 lines
qemu-options.hx +2 -0
+    ``-object
+ colo-

compare,id=id,primary_in=chardevid,secondary_in=chardevid,outde

qemu-options.hx SRST · line 4970

Quoted 2 lines
qemu-options.hx +2 -0
+ v=chardevid,iothread=id[,netdev=netdevid][,vnet_hdr_support][,notify
+

_dev=id][,compare_timeout=@var{ms}][,expired_scan_cycle=@var{ms}][, m

qemu-options.hx SRST

Quoted 7 lines
qemu-options.hx +2 -1
+ ax_queue_size=@var{size}]``
          Colo-compare gets packet from primary\_in chardevid and
          secondary\_in, then compare whether the payload of primary packet
          and secondary packet are the same. If same, it will output
          primary packet to out\_dev, else it will notify COLO-framework to do
-        checkpoint and send primary packet to out\_dev. In order to
+        checkpoint and send primary packet to out\_dev. Although colo-

compare

Quoted 6 lines
qemu-options.hx +3 -0
+        does not depend on network device directly, netdevid helps
+        colo-compare know what network driver it is working on. In
+ order to
          improve efficiency, we need to put the task of comparison in
          another iothread. If it has the vnet\_hdr\_support flag,
          colo compare will send/recv packet with vnet\_hdr\_len.
#6 lore raw
Quoted 328 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Wednesday, October 20, 2021 11:13 AM
To: Zhang, Chen <[email protected]>
Cc: Eric Blake <[email protected]>; Markus Armbruster <[email protected]>; qemu-dev <[email protected]>; Li Zhijian <[email protected]>; Lukas Straub <[email protected]>
Subject: Re: [PATCH V3] net/colo: check vnet_hdr_support flag when using virtio-net

Quoted 88 lines · Chen
Quoted 16 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Tuesday, October 19, 2021 3:39 PM
To: Zhang, Chen <[email protected]>; Eric Blake <[email protected]>; Markus Armbruster <[email protected]>
Cc: qemu-dev <[email protected]>; Li Zhijian <[email protected]>; Lukas Straub <[email protected]>; Tao Xu <[email protected]>
Subject: Re: [PATCH V3] net/colo: check vnet_hdr_support flag when using virtio-net

在 2021/9/18 上午10:04, Zhang Chen 写道:

Quoted 8 lines
Quoted 1 line

When COLO use only one vnet_hdr_support parameter between COLO

network

Quoted 3 lines

filter(filter-mirror, filter-redirector or filter-rewriter and colo-compare, packet will not be parsed correctly. Acquire network driver related to COLO, if it is nirtio-net,

Typo.

Oh~ will fix in next version.

Quoted 61 lines
Quoted 13 lines

check vnet_hdr_support flag of COLO network filter and colo-compare.

---

Changelog:
v3:
Fix some typos.
Rebased for Qemu 6.2.

Quoted 5 lines

v2:
Detect virtio-net driver and apply vnet_hdr_support automatically. (Jason)

---
  net/colo-compare.c    | 57

+++++++++++++++++++++++++++++++++++++++++++

Quoted 42 lines
  net/colo.c            | 20 +++++++++++++++
  net/colo.h            |  4 +++
  net/filter-mirror.c   | 21 ++++++++++++++++
  net/filter-rewriter.c | 10 ++++++++
  qapi/qom.json         |  6 +++++
  qemu-options.hx       |  6 +++--
  7 files changed, 122 insertions(+), 2 deletions(-)
net/colo-compare.c index renamed from net/colo-compare.c +0 -0

b100e7b51f..870bd05a41 100644

net/colo-compare.c +19 -0
@@ -110,6 +110,7 @@ struct CompareState {
      char *sec_indev;
      char *outdev;
      char *notify_dev;
+    char *netdev;
      CharBackend chr_pri_in;
      CharBackend chr_sec_in;
      CharBackend chr_out;
@@ -838,6 +839,28 @@ static int compare_chr_can_read(void *opaque)
      return COMPARE_READ_LEN_MAX;
  }
 
+static int colo_set_default_netdev(void *opaque, QemuOpts *opts,
+Error **errp) {
+    const char *colo_obj_type, *netdev_from_filter;
+    char **netdev = (char **)opaque;
+
+    colo_obj_type = qemu_opt_get(opts, "qom-type");
+
+    if (colo_obj_type &&
+        (strcmp(colo_obj_type, "filter-mirror") == 0 ||
+         strcmp(colo_obj_type, "filter-redirector") == 0 ||
+         strcmp(colo_obj_type, "filter-rewriter") == 0)) {
+        netdev_from_filter = qemu_opt_get(opts, "netdev");
+        if (*netdev == NULL) {
+            *netdev = g_strdup(netdev_from_filter);
+        } else if (strcmp(*netdev, netdev_from_filter) != 0) {
+            warn_report("%s is using a different netdev from other COLO "
+                        "component", colo_obj_type);
+        }
Quoted 8 lines
Quoted 8 lines

net/colo-compare.c static void compare_set_vnet_hdr(Object

Quoted 8 lines
net/colo-compare.c +4 -0
+    }
+    return 0;
+}
+
  /*
   * Called from the main thread on the primary for packets
   * arriving over the socket from the primary.
@@ -1050,6 +1073,21 @@ static void compare_set_vnet_hdr(Object

*obj,

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
net/colo-compare.c +10 -0
      s->vnet_hdr = value;
  }
 
+static char *compare_get_netdev(Object *obj, Error **errp) {
+    CompareState *s = COLO_COMPARE(obj);
+
+    return g_strdup(s->netdev);
+}
+
+static void compare_set_netdev(Object *obj, const char *value,
+Error
+**errp) {
+    CompareState *s = COLO_COMPARE(obj);
Quoted 8 lines
Quoted 8 lines

net/colo-compare.c static void compare_set_vnet_hdr(Object *obj,

Quoted 8 lines
net/colo-compare.c +5 -0
+
+    g_free(s->netdev);
+    s->netdev = g_strdup(value);
+}
+
  static char *compare_get_notify_dev(Object *obj, Error **errp)
  {
      CompareState *s = COLO_COMPARE(obj); @@ -1274,6 +1312,12

@@

Quoted 15 lines
Quoted 15 lines

net/colo-compare.c static void

Quoted 14 lines

static void colo_compare_complete(UserCreatable *uc, Error **errp)

          max_queue_size = MAX_QUEUE_SIZE;
      }
net/colo-compare.c +6 -0
+    if (!s->netdev) {
+        /* Set default netdev as the first colo netfilter found */
+        qemu_opts_foreach(qemu_find_opts("object"),
+                          colo_set_default_netdev, &s->netdev, NULL);
+    }
+
      if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
          !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
          return;
@@ -1289,6 +1333,16 @@ static void

colo_compare_complete(UserCreatable *uc, Error **errp)

Quoted 8 lines
Quoted 8 lines

net/colo-compare.c static void colo_compare_complete(UserCreatable *uc, Error **errp)

Quoted 8 lines
net/colo-compare.c +5 -0
          return;
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, s->netdev, NULL)) {
+        /*
+         * colo compare needs 'vnet_hdr_support' when it works on

virtio-net,

Quoted 6 lines
Quoted 6 lines

net/colo-compare.c static void colo_compare_complete(UserCreatable *uc, Error **errp)

Quoted 6 lines
net/colo-compare.c +5 -0
+         * add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s-
Quoted 110 lines

vnet_hdr);

Quoted 109 lines

net/colo-compare.c static void colo_compare_init(Object *obj) · line 1455

Quoted 6 lines
net/colo-compare.c +0 -0
      net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize,
 s->vnet_hdr);
@@ -1400,6 +1454,9 @@ static void colo_compare_init(Object *obj)
      s->vnet_hdr = false;
      object_property_add_bool(obj, "vnet_hdr_support",

compare_get_vnet_hdr,

Quoted 51 lines
net/colo-compare.c +3 -0
                               compare_set_vnet_hdr);
+    /* colo compare can't varify that netdev is correct */
+    object_property_add_str(obj, "netdev", compare_get_netdev,
+                            compare_set_netdev);
  }
 
  void colo_compare_cleanup(void)
net/colo.c index 3a3e6e89a0..4a03780f45 renamed from net/colo.c +0 -0

100644

net/colo.c +20 -0
@@ -243,3 +243,23 @@ bool connection_has_tracked(GHashTable
 *connection_track_table,
 
      return conn ? true : false;
  }
+
+/* check the network driver related to COLO, return 1 if it is
+virtio-net */ int vnet_driver_check(void *opaque, QemuOpts *opts,
+Error **errp) {
+    const char *driver_type, *netdev_from_driver;
+    char *netdev_from_filter = (char *)opaque;
+
+    driver_type = qemu_opt_get(opts, "driver");
+    netdev_from_driver = qemu_opt_get(opts, "netdev");
+
+    if (!driver_type || !netdev_from_driver || !netdev_from_filter) {
+        return 0;
+    }
+
+    if (g_str_has_prefix(driver_type, "virtio-net") &&
+        strcmp(netdev_from_driver, netdev_from_filter) == 0) {
+        return 1;
+    }
+    return 0;
+}
net/colo.h index d91cd245c4..d401fc76b6 renamed from net/colo.h +0 -0

100644

net/colo.h +3 -0
@@ -18,6 +18,9 @@
  #include "qemu/jhash.h"
  #include "qemu/timer.h"
  #include "net/eth.h"
+#include "qemu/option.h"
+#include "qemu/option_int.h"
+#include "qemu/config-file.h"
 
  #define HASHTABLE_MAX_SIZE 16384
 
@@ -104,5 +107,6 @@ Packet *packet_new(const void *data, int size,
Quoted 1 line

int

vnet_hdr_len);

Quoted 20 lines
  Packet *packet_new_nocopy(void *data, int size, int vnet_hdr_len);
  void packet_destroy(void *opaque, void *user_data);
  void packet_destroy_partial(void *opaque, void *user_data);
net/colo.h +2 -0
+int vnet_driver_check(void *opaque, QemuOpts *opts, Error
+**errp);
 
  #endif /* NET_COLO_H */
net/filter-mirror.c index renamed from net/filter-mirror.c +0 -0

f20240cc9f..69ca9c9839 100644

net/filter-mirror.c +1 -0
@@ -12,6 +12,7 @@
  #include "qemu/osdep.h"
  #include "net/filter.h"
  #include "net/net.h"
+#include "net/colo.h"
  #include "qapi/error.h"
  #include "qom/object.h"
  #include "qemu/main-loop.h"

net/filter-mirror.c static void · line 225

Quoted 1 line
net/filter-mirror.c +0 -0
@@ -224,6 +225,16 @@ static void
 filter_mirror_setup(NetFilterState *nf,

Error **errp)

Quoted 17 lines
net/filter-mirror.c +10 -0
          return;
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter mirror needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      qemu_chr_fe_init(&s->chr_out, chr, errp);
  }
@@ -252,6 +263,16 @@ static void
Quoted 1 line

filter_redirector_setup(NetFilterState

*nf, Error **errp)

Quoted 8 lines
          }
      }
net/filter-mirror.c +5 -0
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter redirector needs 'vnet_hdr_support' when colo filter

modules

Quoted 16 lines
Quoted 16 lines
Quoted 14 lines
net/filter-mirror.c +5 -0
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      net_socket_rs_init(&s->rs, redirector_rs_finalize,

s->vnet_hdr);

      if (s->indev) {
net/filter-rewriter.c index renamed from net/filter-rewriter.c +0 -0

cb3a96cde1..637ef4ce71 100644

net/filter-rewriter.c static void · line 388

Quoted 1 line
net/filter-rewriter.c +0 -0
@@ -388,6 +388,16 @@ static void
 colo_rewriter_setup(NetFilterState *nf,

Error **errp)

Quoted 8 lines
Quoted 8 lines

net/filter-rewriter.c static void colo_rewriter_setup(NetFilterState *nf, Error **errp)

Quoted 8 lines
net/filter-rewriter.c +5 -0
  {
      RewriterState *s = FILTER_REWRITER(nf);
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter rewriter needs 'vnet_hdr_support' when colo filter

modules

Quoted 18 lines
Quoted 18 lines

net/filter-rewriter.c static void · line 402

Quoted 6 lines
net/filter-rewriter.c +5 -0
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      s->connection_track_table =

g_hash_table_new_full(connection_key_hash,

Quoted 10 lines
net/filter-rewriter.c +0 -0
                                                        connection_key_equal,
                                                        g_free,
qapi/qom.json index renamed from qapi/qom.json +0 -0

a25616bc7a..5760107160
100644

qapi/qom.json +0 -0
@@ -241,6 +241,11 @@
  # @notify_dev: name of the character device backend to be used

to

communicate

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

# with the remote colo-frame (only for Xen COLO)
#
+# @netdev: id of the network device backend to colo-compare.

Although

Quoted 7 lines
Quoted 7 lines

qapi/qom.json line 247

Quoted 3 lines
qapi/qom.json +3 -0
+#          colo-compare doesn't depend on network device directly, this
+#          parameter helps colo-compare know what network driver it is
+#          working on.(since 6.2)

This looks kind of not elegant. We don't want to couple COLO with netdev.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I wonder if we can do this without adding netdev.

Yes, current COLO is not bind to netdev, but here we want to auto fix

configuration issue.

If my memory is correct, the current design allows an external comparing module to work.

Yes, this netdev used by identify a series of comparing modules (include filters).

Quoted 5 lines
Quoted 2 lines

To avoid wrong setting other non-colo related existing filter, COLO need

know the colo-effect netdev to setup.

Quoted 1 line

And this parameter is optional, this patch will setup default colo-effect

netdev in colo_set_default_netdev().

Quoted 8 lines
Quoted 1 line

Or any good idea?

How about

1) adding a handshake in the protocol before receiving packets or
2) add a header to the payload to show if it contains a vnet header or
3) go back to let mgmt set vnet_hdr=on/off (looks like the first proposal but it avoids the coupling).

Current code already have a header to the payload to show the vnet header(in net/filter-mirror.c filter_send()). But the vnet_hdr_len is not necessary:
Disabled vnet_hdr payload:
1. size ---> 2. Real network payload.
Enbaled vnet_hdr payload:
1. size ---> 2. Vnet_hdr_len ----> 3. Real network payload.

It looks we can define the vnet_hdr_len as necessary part of protocol. If no vnet_hdr, we set the vnet_hdr_len to 0.
1. size ---> 2. Vnet_hdr_len ----> 3. Real network payload.

How about this proposal?

Thanks
Chen

Quoted 67 lines

Thanks

Quoted 31 lines

Thanks
Chen

Quoted 27 lines

Thanks

Quoted 2 lines

+#
# @compare_timeout: the maximum time to hold a packet from

@primary_in for

Quoted 19 lines

# comparison with an incoming packet on @secondary_in in
# milliseconds (default: 3000)

qapi/qom.json +1 -0
@@ -264,6 +269,7 @@
              'outdev': 'str',
              'iothread': 'str',
              '*notify_dev': 'str',
+            '*netdev': 'str',
              '*compare_timeout': 'uint64',
              '*expired_scan_cycle': 'uint32',
              '*max_queue_size': 'uint32', diff --git

a/qemu-options.hx b/qemu-options.hx index 8f603cc7e6..250937fbbf 100644

qemu-options.hx +0 -1
@@ -4964,12 +4964,14 @@ SRST
          stored. The file format is libpcap, so it can be analyzed with
          tools such as tcpdump or Wireshark.
 
-    ``-object colo-

compare,id=id,primary_in=chardevid,secondary_in=chardevid,outdev=cha

Quoted 4 lines
Quoted 4 lines

rd evid,iothread=id[,vnet_hdr_support][,notify_dev=id][,compare_timeout =@

var{ms}][,expired_scan_cycle=@var{ms}][,max_queue_size=@var{size}]``

Quoted 7 lines
Quoted 7 lines

qemu-options.hx SRST · line 4968

Quoted 2 lines
qemu-options.hx +2 -0
+    ``-object
+ colo-

compare,id=id,primary_in=chardevid,secondary_in=chardevid,outde

Quoted 3 lines
qemu-options.hx +3 -0
+ v=chardevid,iothread=id[,netdev=netdevid][,vnet_hdr_support][,no
+ tify
+

_dev=id][,compare_timeout=@var{ms}][,expired_scan_cycle=@var{ms}][,

Quoted 4 lines
Quoted 4 lines

m

Quoted 3 lines
qemu-options.hx +1 -0
+ ax_queue_size=@var{size}]``
          Colo-compare gets packet from primary\_in chardevid and
          secondary\_in, then compare whether the payload of primary

packet

Quoted 2 lines
Quoted 2 lines

qemu-options.hx SRST · line 4976

Quoted 2 lines
qemu-options.hx +0 -0
          and secondary packet are the same. If same, it will output
          primary packet to out\_dev, else it will notify COLO-framework to

do

Quoted 11 lines
Quoted 10 lines

qemu-options.hx SRST

Quoted 3 lines
qemu-options.hx +2 -1
-        checkpoint and send primary packet to out\_dev. In order to
+        checkpoint and send primary packet to out\_dev. Although
+ colo-

compare

Quoted 6 lines
qemu-options.hx +3 -0
+        does not depend on network device directly, netdevid helps
+        colo-compare know what network driver it is working on.
+ In order to
          improve efficiency, we need to put the task of comparison in
          another iothread. If it has the vnet\_hdr\_support flag,
          colo compare will send/recv packet with vnet\_hdr\_len.
#7 lore raw
Quoted 360 lines · Chen
Quoted 329 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Wednesday, October 20, 2021 11:13 AM
To: Zhang, Chen <[email protected]>
Cc: Eric Blake <[email protected]>; Markus Armbruster <[email protected]>; qemu-dev <[email protected]>; Li Zhijian <[email protected]>; Lukas Straub <[email protected]>
Subject: Re: [PATCH V3] net/colo: check vnet_hdr_support flag when using virtio-net

Quoted 88 lines · Chen
Quoted 16 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Tuesday, October 19, 2021 3:39 PM
To: Zhang, Chen <[email protected]>; Eric Blake <[email protected]>; Markus Armbruster <[email protected]>
Cc: qemu-dev <[email protected]>; Li Zhijian <[email protected]>; Lukas Straub <[email protected]>; Tao Xu <[email protected]>
Subject: Re: [PATCH V3] net/colo: check vnet_hdr_support flag when using virtio-net

在 2021/9/18 上午10:04, Zhang Chen 写道:

Quoted 8 lines
Quoted 1 line

When COLO use only one vnet_hdr_support parameter between COLO

network

Quoted 3 lines

filter(filter-mirror, filter-redirector or filter-rewriter and colo-compare, packet will not be parsed correctly. Acquire network driver related to COLO, if it is nirtio-net,

Typo.

Oh~ will fix in next version.

Quoted 61 lines
Quoted 13 lines

check vnet_hdr_support flag of COLO network filter and colo-compare.

---

Changelog:
v3:
Fix some typos.
Rebased for Qemu 6.2.

Quoted 5 lines

v2:
Detect virtio-net driver and apply vnet_hdr_support automatically. (Jason)

---
  net/colo-compare.c    | 57

+++++++++++++++++++++++++++++++++++++++++++

Quoted 42 lines
  net/colo.c            | 20 +++++++++++++++
  net/colo.h            |  4 +++
  net/filter-mirror.c   | 21 ++++++++++++++++
  net/filter-rewriter.c | 10 ++++++++
  qapi/qom.json         |  6 +++++
  qemu-options.hx       |  6 +++--
  7 files changed, 122 insertions(+), 2 deletions(-)
net/colo-compare.c index renamed from net/colo-compare.c +0 -0

b100e7b51f..870bd05a41 100644

net/colo-compare.c +19 -0
@@ -110,6 +110,7 @@ struct CompareState {
      char *sec_indev;
      char *outdev;
      char *notify_dev;
+    char *netdev;
      CharBackend chr_pri_in;
      CharBackend chr_sec_in;
      CharBackend chr_out;
@@ -838,6 +839,28 @@ static int compare_chr_can_read(void *opaque)
      return COMPARE_READ_LEN_MAX;
  }
 
+static int colo_set_default_netdev(void *opaque, QemuOpts *opts,
+Error **errp) {
+    const char *colo_obj_type, *netdev_from_filter;
+    char **netdev = (char **)opaque;
+
+    colo_obj_type = qemu_opt_get(opts, "qom-type");
+
+    if (colo_obj_type &&
+        (strcmp(colo_obj_type, "filter-mirror") == 0 ||
+         strcmp(colo_obj_type, "filter-redirector") == 0 ||
+         strcmp(colo_obj_type, "filter-rewriter") == 0)) {
+        netdev_from_filter = qemu_opt_get(opts, "netdev");
+        if (*netdev == NULL) {
+            *netdev = g_strdup(netdev_from_filter);
+        } else if (strcmp(*netdev, netdev_from_filter) != 0) {
+            warn_report("%s is using a different netdev from other COLO "
+                        "component", colo_obj_type);
+        }
Quoted 8 lines
Quoted 8 lines

net/colo-compare.c static void compare_set_vnet_hdr(Object

Quoted 8 lines
net/colo-compare.c +4 -0
+    }
+    return 0;
+}
+
  /*
   * Called from the main thread on the primary for packets
   * arriving over the socket from the primary.
@@ -1050,6 +1073,21 @@ static void compare_set_vnet_hdr(Object

*obj,

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
net/colo-compare.c +10 -0
      s->vnet_hdr = value;
  }
 
+static char *compare_get_netdev(Object *obj, Error **errp) {
+    CompareState *s = COLO_COMPARE(obj);
+
+    return g_strdup(s->netdev);
+}
+
+static void compare_set_netdev(Object *obj, const char *value,
+Error
+**errp) {
+    CompareState *s = COLO_COMPARE(obj);
Quoted 8 lines
Quoted 8 lines

net/colo-compare.c static void compare_set_vnet_hdr(Object *obj,

Quoted 8 lines
net/colo-compare.c +5 -0
+
+    g_free(s->netdev);
+    s->netdev = g_strdup(value);
+}
+
  static char *compare_get_notify_dev(Object *obj, Error **errp)
  {
      CompareState *s = COLO_COMPARE(obj); @@ -1274,6 +1312,12

@@

Quoted 15 lines
Quoted 15 lines

net/colo-compare.c static void

Quoted 14 lines

static void colo_compare_complete(UserCreatable *uc, Error **errp)

          max_queue_size = MAX_QUEUE_SIZE;
      }
net/colo-compare.c +6 -0
+    if (!s->netdev) {
+        /* Set default netdev as the first colo netfilter found */
+        qemu_opts_foreach(qemu_find_opts("object"),
+                          colo_set_default_netdev, &s->netdev, NULL);
+    }
+
      if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
          !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
          return;
@@ -1289,6 +1333,16 @@ static void

colo_compare_complete(UserCreatable *uc, Error **errp)

Quoted 8 lines
Quoted 8 lines

net/colo-compare.c static void colo_compare_complete(UserCreatable *uc, Error **errp)

Quoted 8 lines
net/colo-compare.c +5 -0
          return;
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, s->netdev, NULL)) {
+        /*
+         * colo compare needs 'vnet_hdr_support' when it works on

virtio-net,

Quoted 6 lines
Quoted 6 lines

net/colo-compare.c static void colo_compare_complete(UserCreatable *uc, Error **errp)

Quoted 6 lines
net/colo-compare.c +5 -0
+         * add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize, s-
Quoted 110 lines

vnet_hdr);

Quoted 109 lines

net/colo-compare.c static void colo_compare_init(Object *obj) · line 1455

Quoted 6 lines
net/colo-compare.c +0 -0
      net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize,
 s->vnet_hdr);
@@ -1400,6 +1454,9 @@ static void colo_compare_init(Object *obj)
      s->vnet_hdr = false;
      object_property_add_bool(obj, "vnet_hdr_support",

compare_get_vnet_hdr,

Quoted 51 lines
net/colo-compare.c +3 -0
                               compare_set_vnet_hdr);
+    /* colo compare can't varify that netdev is correct */
+    object_property_add_str(obj, "netdev", compare_get_netdev,
+                            compare_set_netdev);
  }
 
  void colo_compare_cleanup(void)
net/colo.c index 3a3e6e89a0..4a03780f45 renamed from net/colo.c +0 -0

100644

net/colo.c +20 -0
@@ -243,3 +243,23 @@ bool connection_has_tracked(GHashTable
 *connection_track_table,
 
      return conn ? true : false;
  }
+
+/* check the network driver related to COLO, return 1 if it is
+virtio-net */ int vnet_driver_check(void *opaque, QemuOpts *opts,
+Error **errp) {
+    const char *driver_type, *netdev_from_driver;
+    char *netdev_from_filter = (char *)opaque;
+
+    driver_type = qemu_opt_get(opts, "driver");
+    netdev_from_driver = qemu_opt_get(opts, "netdev");
+
+    if (!driver_type || !netdev_from_driver || !netdev_from_filter) {
+        return 0;
+    }
+
+    if (g_str_has_prefix(driver_type, "virtio-net") &&
+        strcmp(netdev_from_driver, netdev_from_filter) == 0) {
+        return 1;
+    }
+    return 0;
+}
net/colo.h index d91cd245c4..d401fc76b6 renamed from net/colo.h +0 -0

100644

net/colo.h +3 -0
@@ -18,6 +18,9 @@
  #include "qemu/jhash.h"
  #include "qemu/timer.h"
  #include "net/eth.h"
+#include "qemu/option.h"
+#include "qemu/option_int.h"
+#include "qemu/config-file.h"
 
  #define HASHTABLE_MAX_SIZE 16384
 
@@ -104,5 +107,6 @@ Packet *packet_new(const void *data, int size,
Quoted 1 line

int

vnet_hdr_len);

Quoted 20 lines
  Packet *packet_new_nocopy(void *data, int size, int vnet_hdr_len);
  void packet_destroy(void *opaque, void *user_data);
  void packet_destroy_partial(void *opaque, void *user_data);
net/colo.h +2 -0
+int vnet_driver_check(void *opaque, QemuOpts *opts, Error
+**errp);
 
  #endif /* NET_COLO_H */
net/filter-mirror.c index renamed from net/filter-mirror.c +0 -0

f20240cc9f..69ca9c9839 100644

net/filter-mirror.c +1 -0
@@ -12,6 +12,7 @@
  #include "qemu/osdep.h"
  #include "net/filter.h"
  #include "net/net.h"
+#include "net/colo.h"
  #include "qapi/error.h"
  #include "qom/object.h"
  #include "qemu/main-loop.h"

net/filter-mirror.c static void · line 225

Quoted 1 line
net/filter-mirror.c +0 -0
@@ -224,6 +225,16 @@ static void
 filter_mirror_setup(NetFilterState *nf,

Error **errp)

Quoted 17 lines
net/filter-mirror.c +10 -0
          return;
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter mirror needs 'vnet_hdr_support' when colo filter modules
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      qemu_chr_fe_init(&s->chr_out, chr, errp);
  }
@@ -252,6 +263,16 @@ static void
Quoted 1 line

filter_redirector_setup(NetFilterState

*nf, Error **errp)

Quoted 8 lines
          }
      }
net/filter-mirror.c +5 -0
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter redirector needs 'vnet_hdr_support' when colo filter

modules

Quoted 16 lines
Quoted 16 lines
Quoted 14 lines
net/filter-mirror.c +5 -0
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      net_socket_rs_init(&s->rs, redirector_rs_finalize,

s->vnet_hdr);

      if (s->indev) {
net/filter-rewriter.c index renamed from net/filter-rewriter.c +0 -0

cb3a96cde1..637ef4ce71 100644

net/filter-rewriter.c static void · line 388

Quoted 1 line
net/filter-rewriter.c +0 -0
@@ -388,6 +388,16 @@ static void
 colo_rewriter_setup(NetFilterState *nf,

Error **errp)

Quoted 8 lines
Quoted 8 lines

net/filter-rewriter.c static void colo_rewriter_setup(NetFilterState *nf, Error **errp)

Quoted 8 lines
net/filter-rewriter.c +5 -0
  {
      RewriterState *s = FILTER_REWRITER(nf);
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter rewriter needs 'vnet_hdr_support' when colo filter

modules

Quoted 18 lines
Quoted 18 lines

net/filter-rewriter.c static void · line 402

Quoted 6 lines
net/filter-rewriter.c +5 -0
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      s->connection_track_table =

g_hash_table_new_full(connection_key_hash,

Quoted 10 lines
net/filter-rewriter.c +0 -0
                                                        connection_key_equal,
                                                        g_free,
qapi/qom.json index renamed from qapi/qom.json +0 -0

a25616bc7a..5760107160
100644

qapi/qom.json +0 -0
@@ -241,6 +241,11 @@
  # @notify_dev: name of the character device backend to be used

to

communicate

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

# with the remote colo-frame (only for Xen COLO)
#
+# @netdev: id of the network device backend to colo-compare.

Although

Quoted 7 lines
Quoted 7 lines

qapi/qom.json line 247

Quoted 3 lines
qapi/qom.json +3 -0
+#          colo-compare doesn't depend on network device directly, this
+#          parameter helps colo-compare know what network driver it is
+#          working on.(since 6.2)

This looks kind of not elegant. We don't want to couple COLO with netdev.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I wonder if we can do this without adding netdev.

Yes, current COLO is not bind to netdev, but here we want to auto fix

configuration issue.

If my memory is correct, the current design allows an external comparing module to work.

Yes, this netdev used by identify a series of comparing modules (include filters).

Quoted 5 lines
Quoted 2 lines

To avoid wrong setting other non-colo related existing filter, COLO need

know the colo-effect netdev to setup.

Quoted 1 line

And this parameter is optional, this patch will setup default colo-effect

netdev in colo_set_default_netdev().

Quoted 8 lines
Quoted 1 line

Or any good idea?

How about

1) adding a handshake in the protocol before receiving packets or
2) add a header to the payload to show if it contains a vnet header or
3) go back to let mgmt set vnet_hdr=on/off (looks like the first proposal but it avoids the coupling).

Current code already have a header to the payload to show the vnet header(in net/filter-mirror.c filter_send()). But the vnet_hdr_len is not necessary:
Disabled vnet_hdr payload:
1. size ---> 2. Real network payload.
Enbaled vnet_hdr payload:
1. size ---> 2. Vnet_hdr_len ----> 3. Real network payload.

Quoted 5 lines

It looks we can define the vnet_hdr_len as necessary part of protocol. If no vnet_hdr, we set the vnet_hdr_len to 0.
1. size ---> 2. Vnet_hdr_len ----> 3. Real network payload.

How about this proposal?

I think it should work.

Thanks

Quoted 73 lines

Thanks
Chen

Quoted 67 lines

Thanks

Quoted 31 lines

Thanks
Chen

Quoted 27 lines

Thanks

Quoted 2 lines

+#
# @compare_timeout: the maximum time to hold a packet from

@primary_in for

Quoted 19 lines

# comparison with an incoming packet on @secondary_in in
# milliseconds (default: 3000)

qapi/qom.json +1 -0
@@ -264,6 +269,7 @@
              'outdev': 'str',
              'iothread': 'str',
              '*notify_dev': 'str',
+            '*netdev': 'str',
              '*compare_timeout': 'uint64',
              '*expired_scan_cycle': 'uint32',
              '*max_queue_size': 'uint32', diff --git

a/qemu-options.hx b/qemu-options.hx index 8f603cc7e6..250937fbbf 100644

qemu-options.hx +0 -1
@@ -4964,12 +4964,14 @@ SRST
          stored. The file format is libpcap, so it can be analyzed with
          tools such as tcpdump or Wireshark.
 
-    ``-object colo-

compare,id=id,primary_in=chardevid,secondary_in=chardevid,outdev=cha

Quoted 4 lines
Quoted 4 lines

rd evid,iothread=id[,vnet_hdr_support][,notify_dev=id][,compare_timeout =@

var{ms}][,expired_scan_cycle=@var{ms}][,max_queue_size=@var{size}]``

Quoted 7 lines
Quoted 7 lines

qemu-options.hx SRST · line 4968

Quoted 2 lines
qemu-options.hx +2 -0
+    ``-object
+ colo-

compare,id=id,primary_in=chardevid,secondary_in=chardevid,outde

Quoted 3 lines
qemu-options.hx +3 -0
+ v=chardevid,iothread=id[,netdev=netdevid][,vnet_hdr_support][,no
+ tify
+

_dev=id][,compare_timeout=@var{ms}][,expired_scan_cycle=@var{ms}][,

Quoted 4 lines
Quoted 4 lines

m

Quoted 3 lines
qemu-options.hx +1 -0
+ ax_queue_size=@var{size}]``
          Colo-compare gets packet from primary\_in chardevid and
          secondary\_in, then compare whether the payload of primary

packet

Quoted 2 lines
Quoted 2 lines

qemu-options.hx SRST · line 4976

Quoted 2 lines
qemu-options.hx +0 -0
          and secondary packet are the same. If same, it will output
          primary packet to out\_dev, else it will notify COLO-framework to

do

Quoted 11 lines
Quoted 10 lines

qemu-options.hx SRST

Quoted 3 lines
qemu-options.hx +2 -1
-        checkpoint and send primary packet to out\_dev. In order to
+        checkpoint and send primary packet to out\_dev. Although
+ colo-

compare

Quoted 6 lines
qemu-options.hx +3 -0
+        does not depend on network device directly, netdevid helps
+        colo-compare know what network driver it is working on.
+ In order to
          improve efficiency, we need to put the task of comparison in
          another iothread. If it has the vnet\_hdr\_support flag,
          colo compare will send/recv packet with vnet\_hdr\_len.
#8 lore raw
Quoted 397 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Thursday, October 21, 2021 11:02 AM
To: Zhang, Chen <[email protected]>
Cc: Eric Blake <[email protected]>; Markus Armbruster <[email protected]>; qemu-dev <[email protected]>; Li Zhijian <[email protected]>; Lukas Straub <[email protected]>
Subject: Re: [PATCH V3] net/colo: check vnet_hdr_support flag when using virtio-net

Quoted 9 lines · Chen
Quoted 9 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Wednesday, October 20, 2021 11:13 AM
To: Zhang, Chen <[email protected]>
Cc: Eric Blake <[email protected]>; Markus Armbruster <[email protected]>; qemu-dev <[email protected]>; Li

Zhijian

Quoted 21 lines
Quoted 21 lines

<[email protected]>; Lukas Straub <[email protected]>
Subject: Re: [PATCH V3] net/colo: check vnet_hdr_support flag when using virtio-net

Quoted 15 lines · Chen
Quoted 15 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Tuesday, October 19, 2021 3:39 PM
To: Zhang, Chen <[email protected]>; Eric Blake <[email protected]>; Markus Armbruster <[email protected]>
Cc: qemu-dev <[email protected]>; Li Zhijian <[email protected]>; Lukas Straub <[email protected]>; Tao Xu <[email protected]>
Subject: Re: [PATCH V3] net/colo: check vnet_hdr_support flag when using virtio-net

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines · Zhang Chen
Quoted 1 line

When COLO use only one vnet_hdr_support parameter between

COLO

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

network

Quoted 3 lines

filter(filter-mirror, filter-redirector or filter-rewriter and colo-compare, packet will not be parsed correctly. Acquire network driver related to COLO, if it is nirtio-net,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

Typo.

Oh~ will fix in next version.

Quoted 3 lines
Quoted 3 lines

check vnet_hdr_support flag of COLO network filter and colo-

compare.

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 10 lines

---

Changelog:
v3:
Fix some typos.
Rebased for Qemu 6.2.

Quoted 5 lines

v2:
Detect virtio-net driver and apply vnet_hdr_support automatically. (Jason)

---
  net/colo-compare.c    | 57

+++++++++++++++++++++++++++++++++++++++++++

Quoted 8 lines
  net/colo.c            | 20 +++++++++++++++
  net/colo.h            |  4 +++
  net/filter-mirror.c   | 21 ++++++++++++++++
  net/filter-rewriter.c | 10 ++++++++
  qapi/qom.json         |  6 +++++
  qemu-options.hx       |  6 +++--
  7 files changed, 122 insertions(+), 2 deletions(-)
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
net/colo-compare.c index renamed from net/colo-compare.c +0 -0

b100e7b51f..870bd05a41 100644

net/colo-compare.c +0 -0
@@ -110,6 +110,7 @@ struct CompareState {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

net/colo-compare.c static int compare_chr_can_read(void

Quoted 8 lines
net/colo-compare.c +1 -0
      char *sec_indev;
      char *outdev;
      char *notify_dev;
+    char *netdev;
      CharBackend chr_pri_in;
      CharBackend chr_sec_in;
      CharBackend chr_out;
@@ -838,6 +839,28 @@ static int compare_chr_can_read(void

*opaque)

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 9 lines
net/colo-compare.c +6 -0
      return COMPARE_READ_LEN_MAX;
  }
 
+static int colo_set_default_netdev(void *opaque, QemuOpts
+*opts, Error **errp) {
+    const char *colo_obj_type, *netdev_from_filter;
+    char **netdev = (char **)opaque;
+
+    colo_obj_type = qemu_opt_get(opts, "qom-type");

net/colo-compare.c static int compare_chr_can_read(void *opaque)

Quoted 10 lines
net/colo-compare.c +10 -0
+
+    if (colo_obj_type &&
+        (strcmp(colo_obj_type, "filter-mirror") == 0 ||
+         strcmp(colo_obj_type, "filter-redirector") == 0 ||
+         strcmp(colo_obj_type, "filter-rewriter") == 0)) {
+        netdev_from_filter = qemu_opt_get(opts, "netdev");
+        if (*netdev == NULL) {
+            *netdev = g_strdup(netdev_from_filter);
+        } else if (strcmp(*netdev, netdev_from_filter) != 0) {
+            warn_report("%s is using a different netdev from other

COLO "

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

net/colo-compare.c static void

Quoted 10 lines
net/colo-compare.c +6 -0
+                        "component", colo_obj_type);
+        }
+    }
+    return 0;
+}
+
  /*
   * Called from the main thread on the primary for packets
   * arriving over the socket from the primary.
@@ -1050,6 +1073,21 @@ static void

compare_set_vnet_hdr(Object

Quoted 21 lines
Quoted 21 lines

*obj,

Quoted 19 lines
Quoted 19 lines
Quoted 9 lines
net/colo-compare.c +6 -0
      s->vnet_hdr = value;
  }
 
+static char *compare_get_netdev(Object *obj, Error **errp) {
+    CompareState *s = COLO_COMPARE(obj);
+
+    return g_strdup(s->netdev); }
+
+static void compare_set_netdev(Object *obj, const char

net/colo-compare.c static void compare_set_vnet_hdr(Object *obj,

Quoted 10 lines
net/colo-compare.c +7 -0
+*value, Error
+**errp) {
+    CompareState *s = COLO_COMPARE(obj);
+
+    g_free(s->netdev);
+    s->netdev = g_strdup(value); }
+
  static char *compare_get_notify_dev(Object *obj, Error **errp)
  {
      CompareState *s = COLO_COMPARE(obj); @@ -1274,6 +1312,12

@@

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

static void colo_compare_complete(UserCreatable *uc, Error

**errp)

Quoted 53 lines
Quoted 53 lines
Quoted 17 lines
Quoted 17 lines

net/colo-compare.c static void

Quoted 13 lines
          max_queue_size = MAX_QUEUE_SIZE;
      }
net/colo-compare.c +6 -0
+    if (!s->netdev) {
+        /* Set default netdev as the first colo netfilter found */
+        qemu_opts_foreach(qemu_find_opts("object"),
+                          colo_set_default_netdev, &s->netdev, NULL);
+    }
+
      if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
          !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
          return;
@@ -1289,6 +1333,16 @@ static void

colo_compare_complete(UserCreatable *uc, Error **errp)

Quoted 3 lines
net/colo-compare.c +0 -0
          return;
      }
 
Quoted 6 lines
Quoted 6 lines

net/colo-compare.c static void colo_compare_complete(UserCreatable *uc, Error **errp)

Quoted 6 lines
net/colo-compare.c +6 -0
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, s->netdev, NULL)) {
+        /*
+         * colo compare needs 'vnet_hdr_support' when it
+ works on

virtio-net,

Quoted 29 lines
Quoted 7 lines
Quoted 7 lines
net/colo-compare.c +5 -0
+         * add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize,

s-

vnet_hdr);

Quoted 21 lines

net/colo-compare.c static void colo_compare_init(Object *obj) · line 1455

Quoted 6 lines
      net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize,

s->vnet_hdr);

net/colo-compare.c +0 -0
@@ -1400,6 +1454,9 @@ static void colo_compare_init(Object *obj)
      s->vnet_hdr = false;
      object_property_add_bool(obj, "vnet_hdr_support",

compare_get_vnet_hdr,

Quoted 14 lines
net/colo-compare.c +3 -0
                               compare_set_vnet_hdr);
+    /* colo compare can't varify that netdev is correct */
+    object_property_add_str(obj, "netdev", compare_get_netdev,
+                            compare_set_netdev);
  }
 
  void colo_compare_cleanup(void) diff --git a/net/colo.c

b/net/colo.c index 3a3e6e89a0..4a03780f45
100644

net/colo.c +0 -0
@@ -243,3 +243,23 @@ bool connection_has_tracked(GHashTable
 *connection_track_table,
 
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
net/colo.c +3 -0
      return conn ? true : false;
  }
+
+/* check the network driver related to COLO, return 1 if it
+is virtio-net */ int vnet_driver_check(void *opaque, QemuOpts
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

net/colo.c bool connection_has_tracked(GHashTable *connection_track_table,

Quoted 8 lines
net/colo.c +8 -0
+*opts, Error **errp) {
+    const char *driver_type, *netdev_from_driver;
+    char *netdev_from_filter = (char *)opaque;
+
+    driver_type = qemu_opt_get(opts, "driver");
+    netdev_from_driver = qemu_opt_get(opts, "netdev");
+
+    if (!driver_type || !netdev_from_driver || !netdev_from_filter)

{

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
net/colo.c +9 -0
+        return 0;
+    }
+
+    if (g_str_has_prefix(driver_type, "virtio-net") &&
+        strcmp(netdev_from_driver, netdev_from_filter) == 0) {
+        return 1;
+    }
+    return 0;
+}
net/colo.h index renamed from net/colo.h +0 -0

d91cd245c4..d401fc76b6
100644

net/colo.h +3 -0
@@ -18,6 +18,9 @@
  #include "qemu/jhash.h"
  #include "qemu/timer.h"
  #include "net/eth.h"
+#include "qemu/option.h"
+#include "qemu/option_int.h"
+#include "qemu/config-file.h"
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
net/colo.h +0 -0
  #define HASHTABLE_MAX_SIZE 16384
 
@@ -104,5 +107,6 @@ Packet *packet_new(const void *data, int

size, int

vnet_hdr_len);

Quoted 1 line

Packet *packet_new_nocopy(void *data, int size, int

vnet_hdr_len);

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 19 lines
  void packet_destroy(void *opaque, void *user_data);
  void packet_destroy_partial(void *opaque, void *user_data);
net/colo.h +2 -0
+int vnet_driver_check(void *opaque, QemuOpts *opts, Error
+**errp);
 
  #endif /* NET_COLO_H */
net/filter-mirror.c index renamed from net/filter-mirror.c +0 -0

f20240cc9f..69ca9c9839 100644

net/filter-mirror.c +1 -0
@@ -12,6 +12,7 @@
  #include "qemu/osdep.h"
  #include "net/filter.h"
  #include "net/net.h"
+#include "net/colo.h"
  #include "qapi/error.h"
  #include "qom/object.h"
  #include "qemu/main-loop.h"

net/filter-mirror.c static void · line 225

Quoted 1 line
net/filter-mirror.c +0 -0
@@ -224,6 +225,16 @@ static void
 filter_mirror_setup(NetFilterState *nf,

Error **errp)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

net/filter-mirror.c static void filter_mirror_setup(NetFilterState *nf, Error **errp)

Quoted 8 lines
net/filter-mirror.c +5 -0
          return;
      }
 
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter mirror needs 'vnet_hdr_support' when colo filter

modules

Quoted 66 lines
Quoted 66 lines
Quoted 14 lines
Quoted 14 lines
Quoted 10 lines
net/filter-mirror.c +5 -0
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      qemu_chr_fe_init(&s->chr_out, chr, errp);
  }
@@ -252,6 +263,16 @@ static void

filter_redirector_setup(NetFilterState

*nf, Error **errp)

Quoted 3 lines
          }
      }
Quoted 6 lines
Quoted 6 lines

net/filter-mirror.c static void filter_mirror_setup(NetFilterState *nf, Error **errp)

Quoted 6 lines
net/filter-mirror.c +6 -0
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter redirector needs 'vnet_hdr_support' when
+ colo filter

modules

Quoted 20 lines
Quoted 20 lines
Quoted 15 lines
net/filter-mirror.c +5 -0
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      net_socket_rs_init(&s->rs, redirector_rs_finalize,

s->vnet_hdr);

      if (s->indev) {
net/filter-rewriter.c +0 -0

index
cb3a96cde1..637ef4ce71 100644

net/filter-rewriter.c static void · line 388

Quoted 1 line
net/filter-rewriter.c +0 -0
@@ -388,6 +388,16 @@ static void
 colo_rewriter_setup(NetFilterState *nf,

Error **errp)

Quoted 3 lines
net/filter-rewriter.c +0 -0
  {
      RewriterState *s = FILTER_REWRITER(nf);
 
Quoted 6 lines
Quoted 6 lines

net/filter-rewriter.c static void · line 397

Quoted 6 lines
net/filter-rewriter.c +6 -0
+    if (!s->vnet_hdr &&
+        qemu_opts_foreach(qemu_find_opts("device"),
+                          vnet_driver_check, nf->netdev_id, NULL)) {
+        /*
+         * filter rewriter needs 'vnet_hdr_support' when colo
+ filter

modules

Quoted 18 lines
Quoted 18 lines

net/filter-rewriter.c static void · line 403

Quoted 6 lines
net/filter-rewriter.c +5 -0
+         * work on virtio-net, add 'vnet_hdr_support' automatically
+         */
+        s->vnet_hdr = true;
+    }
+
      s->connection_track_table =

g_hash_table_new_full(connection_key_hash,

Quoted 10 lines

connection_key_equal,

g_free, diff --git a/qapi/qom.json b/qapi/qom.json index a25616bc7a..5760107160
100644

qapi/qom.json +0 -0
@@ -241,6 +241,11 @@
  # @notify_dev: name of the character device backend to be

used to

communicate

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

# with the remote colo-frame (only for Xen COLO)
#
+# @netdev: id of the network device backend to colo-compare.

Although

Quoted 1 line
Quoted 1 line
Quoted 1 line

+# colo-compare doesn't depend on network device directly,

this

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+# parameter helps colo-compare know what network driver it

is

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

+# working on.(since 6.2)

This looks kind of not elegant. We don't want to couple COLO with

netdev.

Quoted 11 lines
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

I wonder if we can do this without adding netdev.

Yes, current COLO is not bind to netdev, but here we want to auto fix

configuration issue.

If my memory is correct, the current design allows an external comparing module to work.

Yes, this netdev used by identify a series of comparing modules (include

filters).

Quoted 12 lines
Quoted 12 lines
Quoted 4 lines

To avoid wrong setting other non-colo related existing filter, COLO need

know the colo-effect netdev to setup.

Quoted 2 lines

And this parameter is optional, this patch will setup default colo-effect

netdev in colo_set_default_netdev().

Quoted 1 line

Or any good idea?

How about

Quoted 7 lines
Quoted 5 lines

1) adding a handshake in the protocol before receiving packets or
2) add a header to the payload to show if it contains a vnet header or
3) go back to let mgmt set vnet_hdr=on/off (looks like the first proposal but it avoids the coupling).

Current code already have a header to the payload to show the vnet

header(in net/filter-mirror.c filter_send()).

Quoted 6 lines

But the vnet_hdr_len is not necessary:
Disabled vnet_hdr payload:
1. size ---> 2. Real network payload.
Enbaled vnet_hdr payload:
1. size ---> 2. Vnet_hdr_len ----> 3. Real network payload.

Quoted 8 lines
Quoted 5 lines

It looks we can define the vnet_hdr_len as necessary part of protocol. If no vnet_hdr, we set the vnet_hdr_len to 0.
1. size ---> 2. Vnet_hdr_len ----> 3. Real network payload.

How about this proposal?

I think it should work.

OK~ I will do it in the V4 patch.

Thanks your comments.

Thanks
Chen

Quoted 85 lines

Thanks

Quoted 20 lines

Thanks
Chen

Quoted 15 lines

Thanks

Quoted 12 lines

Thanks
Chen

Quoted 8 lines

Thanks

Quoted 2 lines

+#
# @compare_timeout: the maximum time to hold a packet from

@primary_in for

Quoted 1 line

# comparison with an incoming packet on @secondary_in

in

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

# milliseconds (default: 3000)

qapi/qom.json +1 -0
@@ -264,6 +269,7 @@
              'outdev': 'str',
              'iothread': 'str',
              '*notify_dev': 'str',
+            '*netdev': 'str',
              '*compare_timeout': 'uint64',
              '*expired_scan_cycle': 'uint32',
              '*max_queue_size': 'uint32', diff --git

a/qemu-options.hx b/qemu-options.hx index
8f603cc7e6..250937fbbf
100644

qemu-options.hx +0 -0
@@ -4964,12 +4964,14 @@ SRST
          stored. The file format is libpcap, so it can be analyzed with
          tools such as tcpdump or Wireshark.
 
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines

qemu-options.hx SRST · line 4967

Quoted 1 line
qemu-options.hx +0 -1
-    ``-object colo-

compare,id=id,primary_in=chardevid,secondary_in=chardevid,outdev=cha

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

rd evid,iothread=id[,vnet_hdr_support][,notify_dev=id][,compare_tim eout
=@

var{ms}][,expired_scan_cycle=@var{ms}][,max_queue_size=@var{size}]``

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines

qemu-options.hx SRST · line 4968

Quoted 2 lines
qemu-options.hx +2 -0
+    ``-object
+ colo-

compare,id=id,primary_in=chardevid,secondary_in=chardevid,outde

Quoted 4 lines
qemu-options.hx +4 -0
+ v=chardevid,iothread=id[,netdev=netdevid][,vnet_hdr_support]
+ [,no
+ tify
+

_dev=id][,compare_timeout=@var{ms}][,expired_scan_cycle=@var{ms}][,

Quoted 23 lines
Quoted 22 lines
Quoted 5 lines
Quoted 5 lines

m

Quoted 4 lines
qemu-options.hx +1 -0
+ ax_queue_size=@var{size}]``
          Colo-compare gets packet from primary\_in chardevid and
          secondary\_in, then compare whether the payload of

primary

packet

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

and secondary packet are the same. If same, it will output primary packet to out\_dev, else it will notify
COLO-framework to

do

Quoted 12 lines
Quoted 11 lines

qemu-options.hx

Quoted 4 lines
qemu-options.hx +3 -1
-        checkpoint and send primary packet to out\_dev. In order to
+        checkpoint and send primary packet to out\_dev.
+ Although
+ colo-

compare

Quoted 6 lines
qemu-options.hx +3 -0
+        does not depend on network device directly, netdevid helps
+        colo-compare know what network driver it is working on.
+ In order to
          improve efficiency, we need to put the task of comparison in
          another iothread. If it has the vnet\_hdr\_support flag,
          colo compare will send/recv packet with vnet\_hdr\_len.