From nobody Mon Apr 29 18:45:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1631931287281755.0833318530302; Fri, 17 Sep 2021 19:14:47 -0700 (PDT) Received: from localhost ([::1]:39686 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mRPsL-0006YJ-J4 for importer@patchew.org; Fri, 17 Sep 2021 22:14:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54920) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mRPrQ-0005sU-MR for qemu-devel@nongnu.org; Fri, 17 Sep 2021 22:13:48 -0400 Received: from mga02.intel.com ([134.134.136.20]:31335) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mRPrM-0002zw-BZ for qemu-devel@nongnu.org; Fri, 17 Sep 2021 22:13:48 -0400 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Sep 2021 19:13:36 -0700 Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Sep 2021 19:13:33 -0700 X-IronPort-AV: E=McAfee;i="6200,9189,10110"; a="210143393" X-IronPort-AV: E=Sophos;i="5.85,303,1624345200"; d="scan'208";a="210143393" X-IronPort-AV: E=Sophos;i="5.85,303,1624345200"; d="scan'208";a="546747395" From: Zhang Chen To: Jason Wang , Eric Blake , Markus Armbruster Subject: [PATCH V3] net/colo: check vnet_hdr_support flag when using virtio-net Date: Sat, 18 Sep 2021 10:04:37 +0800 Message-Id: <20210918020437.1822937-1-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=134.134.136.20; envelope-from=chen.zhang@intel.com; helo=mga02.intel.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zhang Chen , Tao Xu , Lukas Straub , qemu-dev , Li Zhijian Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1631931289323100001 Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Tao Xu Signed-off-by: Zhang Chen --- 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(-) diff --git a/net/colo-compare.c b/net/colo-compare.c index b100e7b51f..870bd05a41 100644 --- a/net/colo-compare.c +++ b/net/colo-compare.c @@ -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; } =20 +static int colo_set_default_netdev(void *opaque, QemuOpts *opts, Error **e= rrp) +{ + const char *colo_obj_type, *netdev_from_filter; + char **netdev =3D (char **)opaque; + + colo_obj_type =3D qemu_opt_get(opts, "qom-type"); + + if (colo_obj_type && + (strcmp(colo_obj_type, "filter-mirror") =3D=3D 0 || + strcmp(colo_obj_type, "filter-redirector") =3D=3D 0 || + strcmp(colo_obj_type, "filter-rewriter") =3D=3D 0)) { + netdev_from_filter =3D qemu_opt_get(opts, "netdev"); + if (*netdev =3D=3D NULL) { + *netdev =3D g_strdup(netdev_from_filter); + } else if (strcmp(*netdev, netdev_from_filter) !=3D 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 =3D value; } =20 +static char *compare_get_netdev(Object *obj, Error **errp) +{ + CompareState *s =3D COLO_COMPARE(obj); + + return g_strdup(s->netdev); +} + +static void compare_set_netdev(Object *obj, const char *value, Error **err= p) +{ + CompareState *s =3D COLO_COMPARE(obj); + + g_free(s->netdev); + s->netdev =3D g_strdup(value); +} + static char *compare_get_notify_dev(Object *obj, Error **errp) { CompareState *s =3D COLO_COMPARE(obj); @@ -1274,6 +1312,12 @@ static void colo_compare_complete(UserCreatable *uc,= Error **errp) max_queue_size =3D MAX_QUEUE_SIZE; } =20 + 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; } =20 + 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-n= et, + * add 'vnet_hdr_support' automatically + */ + s->vnet_hdr =3D 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); =20 @@ -1400,6 +1454,9 @@ static void colo_compare_init(Object *obj) s->vnet_hdr =3D 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); } =20 void colo_compare_cleanup(void) diff --git a/net/colo.c b/net/colo.c index 3a3e6e89a0..4a03780f45 100644 --- a/net/colo.c +++ b/net/colo.c @@ -243,3 +243,23 @@ bool connection_has_tracked(GHashTable *connection_tra= ck_table, =20 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 =3D (char *)opaque; + + driver_type =3D qemu_opt_get(opts, "driver"); + netdev_from_driver =3D 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) =3D=3D 0) { + return 1; + } + return 0; +} diff --git a/net/colo.h b/net/colo.h index d91cd245c4..d401fc76b6 100644 --- a/net/colo.h +++ b/net/colo.h @@ -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" =20 #define HASHTABLE_MAX_SIZE 16384 =20 @@ -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); =20 #endif /* NET_COLO_H */ diff --git a/net/filter-mirror.c b/net/filter-mirror.c index f20240cc9f..69ca9c9839 100644 --- a/net/filter-mirror.c +++ b/net/filter-mirror.c @@ -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, Er= ror **errp) return; } =20 + 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 =3D true; + } + qemu_chr_fe_init(&s->chr_out, chr, errp); } =20 @@ -252,6 +263,16 @@ static void filter_redirector_setup(NetFilterState *nf= , Error **errp) } } =20 + 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 mod= ules + * work on virtio-net, add 'vnet_hdr_support' automatically + */ + s->vnet_hdr =3D true; + } + net_socket_rs_init(&s->rs, redirector_rs_finalize, s->vnet_hdr); =20 if (s->indev) { diff --git a/net/filter-rewriter.c b/net/filter-rewriter.c index cb3a96cde1..637ef4ce71 100644 --- a/net/filter-rewriter.c +++ b/net/filter-rewriter.c @@ -388,6 +388,16 @@ static void colo_rewriter_setup(NetFilterState *nf, Er= ror **errp) { RewriterState *s =3D FILTER_REWRITER(nf); =20 + 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 modul= es + * work on virtio-net, add 'vnet_hdr_support' automatically + */ + s->vnet_hdr =3D true; + } + s->connection_track_table =3D g_hash_table_new_full(connection_key_has= h, connection_key_equal, g_free, diff --git a/qapi/qom.json b/qapi/qom.json index a25616bc7a..5760107160 100644 --- a/qapi/qom.json +++ b/qapi/qom.json @@ -241,6 +241,11 @@ # @notify_dev: name of the character device backend to be used to communic= ate # 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', diff --git a/qemu-options.hx b/qemu-options.hx index 8f603cc7e6..250937fbbf 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -4964,12 +4964,14 @@ SRST stored. The file format is libpcap, so it can be analyzed with tools such as tcpdump or Wireshark. =20 - ``-object colo-compare,id=3Did,primary_in=3Dchardevid,secondary_in=3Dc= hardevid,outdev=3Dchardevid,iothread=3Did[,vnet_hdr_support][,notify_dev=3D= id][,compare_timeout=3D@var{ms}][,expired_scan_cycle=3D@var{ms}][,max_queue= _size=3D@var{size}]`` + ``-object colo-compare,id=3Did,primary_in=3Dchardevid,secondary_in=3Dc= hardevid,outdev=3Dchardevid,iothread=3Did[,netdev=3Dnetdevid][,vnet_hdr_sup= port][,notify_dev=3Did][,compare_timeout=3D@var{ms}][,expired_scan_cycle=3D= @var{ms}][,max_queue_size=3D@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-comp= are + 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. --=20 2.25.1