From nobody Sat May 18 14:01:30 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 1636687347843255.2316398808963; Thu, 11 Nov 2021 19:22:27 -0800 (PST) Received: from localhost ([::1]:46850 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mlN90-0008EI-Hv for importer@patchew.org; Thu, 11 Nov 2021 22:22:26 -0500 Received: from eggs.gnu.org ([209.51.188.92]:37538) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlN82-0005pV-4N for qemu-devel@nongnu.org; Thu, 11 Nov 2021 22:21:26 -0500 Received: from mga03.intel.com ([134.134.136.65]:4407) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlN7z-0005Ty-Pp for qemu-devel@nongnu.org; Thu, 11 Nov 2021 22:21:25 -0500 Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2021 19:21:21 -0800 Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2021 19:21:19 -0800 X-IronPort-AV: E=McAfee;i="6200,9189,10165"; a="233009080" X-IronPort-AV: E=Sophos;i="5.87,227,1631602800"; d="scan'208";a="233009080" X-IronPort-AV: E=Sophos;i="5.87,227,1631602800"; d="scan'208";a="504711459" From: Zhang Chen To: Jason Wang , Markus Armbruster , "Dr. David Alan Gilbert" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eric Blake Subject: [PATCH for 7.0 V10 1/6] qapi/net: Add IPFlowSpec and QMP command for filter passthrough Date: Fri, 12 Nov 2021 11:11:07 +0800 Message-Id: <20211112031112.9303-2-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211112031112.9303-1-chen.zhang@intel.com> References: <20211112031112.9303-1-chen.zhang@intel.com> 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.65; envelope-from=chen.zhang@intel.com; helo=mga03.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, 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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zhang Chen , qemu-dev , Li Zhijian Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1636687349642100002 Content-Type: text/plain; charset="utf-8" Since the real user scenario does not need to monitor all traffic. Add passthrough-filter-add and passthrough-filter-del to maintain a network passthrough list in object with network packet processing function. Add IPFlowSpec struct for all QMP commands. Most the fields of IPFlowSpec are optional,except object-name. Signed-off-by: Zhang Chen --- net/net.c | 10 +++++++ qapi/net.json | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/net/net.c b/net/net.c index f0d14dbfc1..5d0d5914fb 100644 --- a/net/net.c +++ b/net/net.c @@ -1215,6 +1215,16 @@ void qmp_netdev_del(const char *id, Error **errp) } } =20 +void qmp_passthrough_filter_add(IPFlowSpec *spec, Error **errp) +{ + /* TODO implement setup passthrough rule */ +} + +void qmp_passthrough_filter_del(IPFlowSpec *spec, Error **errp) +{ + /* TODO implement delete passthrough rule */ +} + static void netfilter_print_info(Monitor *mon, NetFilterState *nf) { char *str; diff --git a/qapi/net.json b/qapi/net.json index 7fab2e7cd8..5194aedcf5 100644 --- a/qapi/net.json +++ b/qapi/net.json @@ -7,6 +7,7 @@ ## =20 { 'include': 'common.json' } +{ 'include': 'sockets.json' } =20 ## # @set_link: @@ -696,3 +697,75 @@ ## { 'event': 'FAILOVER_NEGOTIATED', 'data': {'device-id': 'str'} } + +## +# @IPFlowSpec: +# +# IP flow specification. +# +# @protocol: Transport layer protocol like TCP/UDP, etc. This will be +# passed to getprotobyname(3). +# +# @object-name: The @object-name means a QEMU object with network +# packet processing function, for example colo-compare, +# filter-redirector, filter-mirror, etc. QOM path to +# a QOM object that implements their own passthrough +# work in the original data processing flow. What is +# exposed to the outside world is an operable +# passthrough list. +# +# @source: Source address and port. +# +# @destination: Destination address and port. +# +# Since: 7.0 +## +{ 'struct': 'IPFlowSpec', + 'data': { '*protocol': 'str', 'object-name': 'str', + '*source': 'InetSocketAddressBase', + '*destination': 'InetSocketAddressBase' } } + +## +# @passthrough-filter-add: +# +# Add an entry to the QOM object own network passthrough list. +# Absent protocol, host addresses and ports match anything. +# +# Returns: Nothing on success +# +# Since: 7.0 +# +# Example: +# +# -> { "execute": "passthrough-filter-add", +# "arguments": { "protocol": "tcp", "object-name": "object0", +# "source": {"host": "192.168.1.1", "port": "1234"}, +# "destination": {"host": "192.168.1.2", "port": "4321"} } } +# <- { "return": {} } +# +## +{ 'command': 'passthrough-filter-add', 'boxed': true, + 'data': 'IPFlowSpec' } + +## +# @passthrough-filter-del: +# +# Delete an entry from the QOM object own network passthrough list. +# Deletes the entry with exactly this protocol, host addresses +# and ports. +# +# Returns: Nothing on success +# +# Since: 7.0 +# +# Example: +# +# -> { "execute": "passthrough-filter-del", +# "arguments": { "protocol": "tcp", "object-name": "object0", +# "source": {"host": "192.168.1.1", "port": "1234"}, +# "destination": {"host": "192.168.1.2", "port": "4321"} } } +# <- { "return": {} } +# +## +{ 'command': 'passthrough-filter-del', 'boxed': true, + 'data': 'IPFlowSpec' } --=20 2.25.1 From nobody Sat May 18 14:01:30 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 163668744503963.652516461213395; Thu, 11 Nov 2021 19:24:05 -0800 (PST) Received: from localhost ([::1]:53778 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mlNAZ-0004No-LH for importer@patchew.org; Thu, 11 Nov 2021 22:24:04 -0500 Received: from eggs.gnu.org ([209.51.188.92]:37570) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlN84-0005q1-6V for qemu-devel@nongnu.org; Thu, 11 Nov 2021 22:21:28 -0500 Received: from mga03.intel.com ([134.134.136.65]:4409) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlN81-0005U8-FF for qemu-devel@nongnu.org; Thu, 11 Nov 2021 22:21:27 -0500 Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2021 19:21:24 -0800 Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2021 19:21:21 -0800 X-IronPort-AV: E=McAfee;i="6200,9189,10165"; a="233009086" X-IronPort-AV: E=Sophos;i="5.87,227,1631602800"; d="scan'208";a="233009086" X-IronPort-AV: E=Sophos;i="5.87,227,1631602800"; d="scan'208";a="504711467" From: Zhang Chen To: Jason Wang , Markus Armbruster , "Dr. David Alan Gilbert" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eric Blake Subject: [PATCH for 7.0 V10 2/6] util/qemu-sockets.c: Add inet_parse_base to handle InetSocketAddressBase Date: Fri, 12 Nov 2021 11:11:08 +0800 Message-Id: <20211112031112.9303-3-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211112031112.9303-1-chen.zhang@intel.com> References: <20211112031112.9303-1-chen.zhang@intel.com> 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.65; envelope-from=chen.zhang@intel.com; helo=mga03.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, 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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zhang Chen , qemu-dev , Li Zhijian Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1636687445274100001 Content-Type: text/plain; charset="utf-8" No need to carry the flag all the time in many scenarios. Signed-off-by: Zhang Chen --- include/qemu/sockets.h | 1 + util/qemu-sockets.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h index 0c34bf2398..3a0f8fa8f2 100644 --- a/include/qemu/sockets.h +++ b/include/qemu/sockets.h @@ -32,6 +32,7 @@ int socket_set_fast_reuse(int fd); int inet_ai_family_from_address(InetSocketAddress *addr, Error **errp); int inet_parse(InetSocketAddress *addr, const char *str, Error **errp); +int inet_parse_base(InetSocketAddressBase *addr, const char *str, Error **= errp); int inet_connect(const char *str, Error **errp); int inet_connect_saddr(InetSocketAddress *saddr, Error **errp); =20 diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 0585e7a629..f444921918 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -713,6 +713,20 @@ int inet_parse(InetSocketAddress *addr, const char *st= r, Error **errp) return 0; } =20 +int inet_parse_base(InetSocketAddressBase *base, const char *str, Error **= errp) +{ + InetSocketAddress *addr; + int ret =3D 0; + + addr =3D g_new0(InetSocketAddress, 1); + ret =3D inet_parse(addr, str, errp); + + base->host =3D addr->host; + base->port =3D addr->port; + + g_free(addr); + return ret; +} =20 /** * Create a blocking socket and connect it to an address. --=20 2.25.1 From nobody Sat May 18 14:01:30 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 1636687352708368.32391048727663; Thu, 11 Nov 2021 19:22:32 -0800 (PST) Received: from localhost ([::1]:47266 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mlN95-0008Ua-Is for importer@patchew.org; Thu, 11 Nov 2021 22:22:31 -0500 Received: from eggs.gnu.org ([209.51.188.92]:37604) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlN86-0005vx-FX for qemu-devel@nongnu.org; Thu, 11 Nov 2021 22:21:30 -0500 Received: from mga03.intel.com ([134.134.136.65]:4409) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlN84-0005U8-39 for qemu-devel@nongnu.org; Thu, 11 Nov 2021 22:21:30 -0500 Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2021 19:21:27 -0800 Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2021 19:21:24 -0800 X-IronPort-AV: E=McAfee;i="6200,9189,10165"; a="233009091" X-IronPort-AV: E=Sophos;i="5.87,227,1631602800"; d="scan'208";a="233009091" X-IronPort-AV: E=Sophos;i="5.87,227,1631602800"; d="scan'208";a="504711476" From: Zhang Chen To: Jason Wang , Markus Armbruster , "Dr. David Alan Gilbert" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eric Blake Subject: [PATCH for 7.0 V10 3/6] hmp-commands: Add new HMP command for filter passthrough Date: Fri, 12 Nov 2021 11:11:09 +0800 Message-Id: <20211112031112.9303-4-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211112031112.9303-1-chen.zhang@intel.com> References: <20211112031112.9303-1-chen.zhang@intel.com> 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.65; envelope-from=chen.zhang@intel.com; helo=mga03.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, 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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zhang Chen , qemu-dev , Li Zhijian Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1636687353348100001 Content-Type: text/plain; charset="utf-8" Add hmp_passthrough_filter_add and hmp_passthrough_filter_del make user can maintain object network passthrough list in human monitor Signed-off-by: Zhang Chen --- hmp-commands.hx | 26 ++++++++++++++++++ include/monitor/hmp.h | 2 ++ monitor/hmp-cmds.c | 63 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/hmp-commands.hx b/hmp-commands.hx index 70a9136ac2..e57e099361 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -1292,6 +1292,32 @@ SRST Remove host network device. ERST =20 + { + .name =3D "passthrough_filter_add", + .args_type =3D "protocol:s?,object-name:s,src:s?,dst:s?", + .params =3D "[protocol] object-name [src] [dst]", + .help =3D "Add network passthrough rule to object passthroug= h list", + .cmd =3D hmp_passthrough_filter_add, + }, + +SRST +``passthrough_filter_add`` + Add network stream to object passthrough list. +ERST + + { + .name =3D "passthrough_filter_del", + .args_type =3D "protocol:s?,object-name:s,src:s?,dst:s?", + .params =3D "[protocol] object-name [src] [dst]", + .help =3D "Delete network passthrough rule from object passt= hrough list", + .cmd =3D hmp_passthrough_filter_del, + }, + +SRST +``passthrough_filter_del`` + Delete network stream from object passthrough list. +ERST + { .name =3D "object_add", .args_type =3D "object:S", diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h index 96d014826a..020b86212e 100644 --- a/include/monitor/hmp.h +++ b/include/monitor/hmp.h @@ -78,6 +78,8 @@ void hmp_device_del(Monitor *mon, const QDict *qdict); void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict); void hmp_netdev_add(Monitor *mon, const QDict *qdict); void hmp_netdev_del(Monitor *mon, const QDict *qdict); +void hmp_passthrough_filter_add(Monitor *mon, const QDict *qdict); +void hmp_passthrough_filter_del(Monitor *mon, const QDict *qdict); void hmp_getfd(Monitor *mon, const QDict *qdict); void hmp_closefd(Monitor *mon, const QDict *qdict); void hmp_sendkey(Monitor *mon, const QDict *qdict); diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c index 9c91bf93e9..19e91f7599 100644 --- a/monitor/hmp-cmds.c +++ b/monitor/hmp-cmds.c @@ -1570,6 +1570,69 @@ void hmp_netdev_del(Monitor *mon, const QDict *qdict) hmp_handle_error(mon, err); } =20 +static IPFlowSpec *hmp_parse_IPFlowSpec(Monitor *mon, const QDict *qdict) +{ + IPFlowSpec *spec =3D g_new0(IPFlowSpec, 1); + g_autofree char *src =3D NULL, *dst =3D NULL; + + spec->protocol =3D g_strdup(qdict_get_try_str(qdict, "protocol")); + spec->object_name =3D g_strdup(qdict_get_try_str(qdict, "object-name")= ); + src =3D g_strdup(qdict_get_try_str(qdict, "src")); + dst =3D g_strdup(qdict_get_try_str(qdict, "dst")); + + if (src) { + spec->source =3D g_new0(InetSocketAddressBase, 1); + + if (inet_parse_base(spec->source, src, NULL)) { + monitor_printf(mon, "Incorrect passthrough src address\n"); + goto err; + } + } + + if (dst) { + spec->destination =3D g_new0(InetSocketAddressBase, 1); + + if (inet_parse_base(spec->destination, dst, NULL)) { + monitor_printf(mon, "Incorrect passthrough dst address\n"); + goto err; + } + } + + return spec; + +err: + g_free(spec->source); + g_free(spec->destination); + g_free(spec); + return NULL; +} + +void hmp_passthrough_filter_add(Monitor *mon, const QDict *qdict) +{ + IPFlowSpec *spec; + Error *err =3D NULL; + + spec =3D hmp_parse_IPFlowSpec(mon, qdict); + if (spec) { + qmp_passthrough_filter_add(spec, &err); + } + + hmp_handle_error(mon, err); +} + +void hmp_passthrough_filter_del(Monitor *mon, const QDict *qdict) +{ + IPFlowSpec *spec; + Error *err =3D NULL; + + spec =3D hmp_parse_IPFlowSpec(mon, qdict); + if (spec) { + qmp_passthrough_filter_del(spec, &err); + } + + hmp_handle_error(mon, err); +} + void hmp_object_add(Monitor *mon, const QDict *qdict) { const char *options =3D qdict_get_str(qdict, "object"); --=20 2.25.1 From nobody Sat May 18 14:01:30 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 1636687446109838.7335623560612; Thu, 11 Nov 2021 19:24:06 -0800 (PST) Received: from localhost ([::1]:53806 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mlNAb-0004Om-55 for importer@patchew.org; Thu, 11 Nov 2021 22:24:05 -0500 Received: from eggs.gnu.org ([209.51.188.92]:37644) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlN8C-0006FR-Fq for qemu-devel@nongnu.org; Thu, 11 Nov 2021 22:21:36 -0500 Received: from mga03.intel.com ([134.134.136.65]:4413) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlN86-0005UX-Bq for qemu-devel@nongnu.org; Thu, 11 Nov 2021 22:21:36 -0500 Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2021 19:21:29 -0800 Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2021 19:21:27 -0800 X-IronPort-AV: E=McAfee;i="6200,9189,10165"; a="233009097" X-IronPort-AV: E=Sophos;i="5.87,227,1631602800"; d="scan'208";a="233009097" X-IronPort-AV: E=Sophos;i="5.87,227,1631602800"; d="scan'208";a="504711484" From: Zhang Chen To: Jason Wang , Markus Armbruster , "Dr. David Alan Gilbert" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eric Blake Subject: [PATCH for 7.0 V10 4/6] net/colo-compare: Move data structure and define to .h file. Date: Fri, 12 Nov 2021 11:11:10 +0800 Message-Id: <20211112031112.9303-5-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211112031112.9303-1-chen.zhang@intel.com> References: <20211112031112.9303-1-chen.zhang@intel.com> 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.65; envelope-from=chen.zhang@intel.com; helo=mga03.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, 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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zhang Chen , qemu-dev , Li Zhijian Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1636687447418100001 Content-Type: text/plain; charset="utf-8" Rename structure with COLO index and move it to .h file, It make other modules can reuse COLO code. Signed-off-by: Zhang Chen --- net/colo-compare.c | 132 ++++++++------------------------------------- net/colo-compare.h | 86 +++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 109 deletions(-) diff --git a/net/colo-compare.c b/net/colo-compare.c index b8876d7fd9..9114b687de 100644 --- a/net/colo-compare.c +++ b/net/colo-compare.c @@ -17,29 +17,18 @@ #include "qemu/error-report.h" #include "trace.h" #include "qapi/error.h" -#include "net/net.h" #include "net/eth.h" #include "qom/object_interfaces.h" #include "qemu/iov.h" #include "qom/object.h" #include "net/queue.h" -#include "chardev/char-fe.h" #include "qemu/sockets.h" -#include "colo.h" -#include "sysemu/iothread.h" #include "net/colo-compare.h" -#include "migration/colo.h" -#include "migration/migration.h" #include "util.h" =20 #include "block/aio-wait.h" #include "qemu/coroutine.h" =20 -#define TYPE_COLO_COMPARE "colo-compare" -typedef struct CompareState CompareState; -DECLARE_INSTANCE_CHECKER(CompareState, COLO_COMPARE, - TYPE_COLO_COMPARE) - static QTAILQ_HEAD(, CompareState) net_compares =3D QTAILQ_HEAD_INITIALIZER(net_compares); =20 @@ -47,13 +36,13 @@ static NotifierList colo_compare_notifiers =3D NOTIFIER_LIST_INITIALIZER(colo_compare_notifiers); =20 #define COMPARE_READ_LEN_MAX NET_BUFSIZE -#define MAX_QUEUE_SIZE 1024 +#define MAX_COLO_QUEUE_SIZE 1024 =20 #define COLO_COMPARE_FREE_PRIMARY 0x01 #define COLO_COMPARE_FREE_SECONDARY 0x02 =20 -#define REGULAR_PACKET_CHECK_MS 1000 -#define DEFAULT_TIME_OUT_MS 3000 +#define COLO_REGULAR_PACKET_CHECK_MS 1000 +#define COLO_DEFAULT_TIME_OUT_MS 3000 =20 /* #define DEBUG_COLO_PACKETS */ =20 @@ -64,87 +53,6 @@ static QemuCond event_complete_cond; static int event_unhandled_count; static uint32_t max_queue_size; =20 -/* - * + CompareState ++ - * | | - * +---------------+ +---------------+ +---------------+ - * | conn list + - > conn + ------- > conn + -- > .= ..... - * +---------------+ +---------------+ +---------------+ - * | | | | | | - * +---------------+ +---v----+ +---v----+ +---v----+ +---v----+ - * |primary | |secondary |primary | |secondary - * |packet | |packet + |packet | |packet + - * +--------+ +--------+ +--------+ +--------+ - * | | | | - * +---v----+ +---v----+ +---v----+ +---v----+ - * |primary | |secondary |primary | |secondary - * |packet | |packet + |packet | |packet + - * +--------+ +--------+ +--------+ +--------+ - * | | | | - * +---v----+ +---v----+ +---v----+ +---v----+ - * |primary | |secondary |primary | |secondary - * |packet | |packet + |packet | |packet + - * +--------+ +--------+ +--------+ +--------+ - */ - -typedef struct SendCo { - Coroutine *co; - struct CompareState *s; - CharBackend *chr; - GQueue send_list; - bool notify_remote_frame; - bool done; - int ret; -} SendCo; - -typedef struct SendEntry { - uint32_t size; - uint32_t vnet_hdr_len; - uint8_t *buf; -} SendEntry; - -struct CompareState { - Object parent; - - char *pri_indev; - char *sec_indev; - char *outdev; - char *notify_dev; - CharBackend chr_pri_in; - CharBackend chr_sec_in; - CharBackend chr_out; - CharBackend chr_notify_dev; - SocketReadState pri_rs; - SocketReadState sec_rs; - SocketReadState notify_rs; - SendCo out_sendco; - SendCo notify_sendco; - bool vnet_hdr; - uint64_t compare_timeout; - uint32_t expired_scan_cycle; - - /* - * Record the connection that through the NIC - * Element type: Connection - */ - GQueue conn_list; - /* Record the connection without repetition */ - GHashTable *connection_track_table; - - IOThread *iothread; - GMainContext *worker_context; - QEMUTimer *packet_check_timer; - - QEMUBH *event_bh; - enum colo_event event; - - QTAILQ_ENTRY(CompareState) next; -}; - -typedef struct CompareClass { - ObjectClass parent_class; -} CompareClass; - enum { PRIMARY_IN =3D 0, SECONDARY_IN, @@ -155,6 +63,12 @@ static const char *colo_mode[] =3D { [SECONDARY_IN] =3D "secondary", }; =20 +typedef struct COLOSendEntry { + uint32_t size; + uint32_t vnet_hdr_len; + uint8_t *buf; +} COLOSendEntry; + static int compare_chr_send(CompareState *s, uint8_t *buf, uint32_t size, @@ -724,19 +638,19 @@ static void colo_compare_connection(void *opaque, voi= d *user_data) =20 static void coroutine_fn _compare_chr_send(void *opaque) { - SendCo *sendco =3D opaque; + COLOSendCo *sendco =3D opaque; CompareState *s =3D sendco->s; int ret =3D 0; =20 while (!g_queue_is_empty(&sendco->send_list)) { - SendEntry *entry =3D g_queue_pop_tail(&sendco->send_list); + COLOSendEntry *entry =3D g_queue_pop_tail(&sendco->send_list); uint32_t len =3D htonl(entry->size); =20 ret =3D qemu_chr_fe_write_all(sendco->chr, (uint8_t *)&len, sizeof= (len)); =20 if (ret !=3D sizeof(len)) { g_free(entry->buf); - g_slice_free(SendEntry, entry); + g_slice_free(COLOSendEntry, entry); goto err; } =20 @@ -753,7 +667,7 @@ static void coroutine_fn _compare_chr_send(void *opaque) =20 if (ret !=3D sizeof(len)) { g_free(entry->buf); - g_slice_free(SendEntry, entry); + g_slice_free(COLOSendEntry, entry); goto err; } } @@ -764,12 +678,12 @@ static void coroutine_fn _compare_chr_send(void *opaq= ue) =20 if (ret !=3D entry->size) { g_free(entry->buf); - g_slice_free(SendEntry, entry); + g_slice_free(COLOSendEntry, entry); goto err; } =20 g_free(entry->buf); - g_slice_free(SendEntry, entry); + g_slice_free(COLOSendEntry, entry); } =20 sendco->ret =3D 0; @@ -777,9 +691,9 @@ static void coroutine_fn _compare_chr_send(void *opaque) =20 err: while (!g_queue_is_empty(&sendco->send_list)) { - SendEntry *entry =3D g_queue_pop_tail(&sendco->send_list); + COLOSendEntry *entry =3D g_queue_pop_tail(&sendco->send_list); g_free(entry->buf); - g_slice_free(SendEntry, entry); + g_slice_free(COLOSendEntry, entry); } sendco->ret =3D ret < 0 ? ret : -EIO; out: @@ -795,8 +709,8 @@ static int compare_chr_send(CompareState *s, bool notify_remote_frame, bool zero_copy) { - SendCo *sendco; - SendEntry *entry; + COLOSendCo *sendco; + COLOSendEntry *entry; =20 if (notify_remote_frame) { sendco =3D &s->notify_sendco; @@ -808,7 +722,7 @@ static int compare_chr_send(CompareState *s, return 0; } =20 - entry =3D g_slice_new(SendEntry); + entry =3D g_slice_new(COLOSendEntry); entry->size =3D size; entry->vnet_hdr_len =3D vnet_hdr_len; if (zero_copy) { @@ -1261,17 +1175,17 @@ static void colo_compare_complete(UserCreatable *uc= , Error **errp) =20 if (!s->compare_timeout) { /* Set default value to 3000 MS */ - s->compare_timeout =3D DEFAULT_TIME_OUT_MS; + s->compare_timeout =3D COLO_DEFAULT_TIME_OUT_MS; } =20 if (!s->expired_scan_cycle) { /* Set default value to 3000 MS */ - s->expired_scan_cycle =3D REGULAR_PACKET_CHECK_MS; + s->expired_scan_cycle =3D COLO_REGULAR_PACKET_CHECK_MS; } =20 if (!max_queue_size) { /* Set default queue size to 1024 */ - max_queue_size =3D MAX_QUEUE_SIZE; + max_queue_size =3D MAX_COLO_QUEUE_SIZE; } =20 if (find_and_check_chardev(&chr, s->pri_indev, errp) || diff --git a/net/colo-compare.h b/net/colo-compare.h index b055270da2..031b627a2f 100644 --- a/net/colo-compare.h +++ b/net/colo-compare.h @@ -17,6 +17,92 @@ #ifndef QEMU_COLO_COMPARE_H #define QEMU_COLO_COMPARE_H =20 +#include "net/net.h" +#include "chardev/char-fe.h" +#include "migration/colo.h" +#include "migration/migration.h" +#include "sysemu/iothread.h" +#include "colo.h" + +#define TYPE_COLO_COMPARE "colo-compare" +typedef struct CompareState CompareState; +DECLARE_INSTANCE_CHECKER(CompareState, COLO_COMPARE, + TYPE_COLO_COMPARE) + +typedef struct COLOSendCo { + Coroutine *co; + struct CompareState *s; + CharBackend *chr; + GQueue send_list; + bool notify_remote_frame; + bool done; + int ret; +} COLOSendCo; + +/* + * + CompareState ++ + * | | + * +---------------+ +---------------+ +---------------+ + * | conn list + - > conn + ------- > conn + -- > .= ..... + * +---------------+ +---------------+ +---------------+ + * | | | | | | + * +---------------+ +---v----+ +---v----+ +---v----+ +---v----+ + * |primary | |secondary |primary | |secondary + * |packet | |packet + |packet | |packet + + * +--------+ +--------+ +--------+ +--------+ + * | | | | + * +---v----+ +---v----+ +---v----+ +---v----+ + * |primary | |secondary |primary | |secondary + * |packet | |packet + |packet | |packet + + * +--------+ +--------+ +--------+ +--------+ + * | | | | + * +---v----+ +---v----+ +---v----+ +---v----+ + * |primary | |secondary |primary | |secondary + * |packet | |packet + |packet | |packet + + * +--------+ +--------+ +--------+ +--------+ + */ +struct CompareState { + Object parent; + + char *pri_indev; + char *sec_indev; + char *outdev; + char *notify_dev; + CharBackend chr_pri_in; + CharBackend chr_sec_in; + CharBackend chr_out; + CharBackend chr_notify_dev; + SocketReadState pri_rs; + SocketReadState sec_rs; + SocketReadState notify_rs; + COLOSendCo out_sendco; + COLOSendCo notify_sendco; + bool vnet_hdr; + uint64_t compare_timeout; + uint32_t expired_scan_cycle; + + /* + * Record the connection that through the NIC + * Element type: Connection + */ + GQueue conn_list; + /* Record the connection without repetition */ + GHashTable *connection_track_table; + + IOThread *iothread; + GMainContext *worker_context; + QEMUTimer *packet_check_timer; + + QEMUBH *event_bh; + enum colo_event event; + + QTAILQ_ENTRY(CompareState) next; +}; + +typedef struct CompareClass { + ObjectClass parent_class; +} CompareClass; + void colo_notify_compares_event(void *opaque, int event, Error **errp); void colo_compare_register_notifier(Notifier *notify); void colo_compare_unregister_notifier(Notifier *notify); --=20 2.25.1 From nobody Sat May 18 14:01:30 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 1636687543959255.68481039315418; Thu, 11 Nov 2021 19:25:43 -0800 (PST) Received: from localhost ([::1]:57996 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mlNC9-0007CM-Mi for importer@patchew.org; Thu, 11 Nov 2021 22:25:41 -0500 Received: from eggs.gnu.org ([209.51.188.92]:37630) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlN8B-0006A1-4L for qemu-devel@nongnu.org; Thu, 11 Nov 2021 22:21:35 -0500 Received: from mga03.intel.com ([134.134.136.65]:4409) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlN89-0005U8-1T for qemu-devel@nongnu.org; Thu, 11 Nov 2021 22:21:34 -0500 Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2021 19:21:32 -0800 Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2021 19:21:29 -0800 X-IronPort-AV: E=McAfee;i="6200,9189,10165"; a="233009110" X-IronPort-AV: E=Sophos;i="5.87,227,1631602800"; d="scan'208";a="233009110" X-IronPort-AV: E=Sophos;i="5.87,227,1631602800"; d="scan'208";a="504711489" From: Zhang Chen To: Jason Wang , Markus Armbruster , "Dr. David Alan Gilbert" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eric Blake Subject: [PATCH for 7.0 V10 5/6] net/colo-compare: Add passthrough list to CompareState Date: Fri, 12 Nov 2021 11:11:11 +0800 Message-Id: <20211112031112.9303-6-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211112031112.9303-1-chen.zhang@intel.com> References: <20211112031112.9303-1-chen.zhang@intel.com> 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.65; envelope-from=chen.zhang@intel.com; helo=mga03.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, 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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zhang Chen , qemu-dev , Li Zhijian Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1636687545095100001 Content-Type: text/plain; charset="utf-8" Add passthrough list for each CompareState. Signed-off-by: Zhang Chen --- net/colo-compare.c | 28 ++++++++++++++++++++++++++++ net/colo-compare.h | 12 ++++++++++++ 2 files changed, 40 insertions(+) diff --git a/net/colo-compare.c b/net/colo-compare.c index 9114b687de..df8bc5acce 100644 --- a/net/colo-compare.c +++ b/net/colo-compare.c @@ -161,6 +161,7 @@ static int packet_enqueue(CompareState *s, int mode, Co= nnection **con) ConnectionKey key; Packet *pkt =3D NULL; Connection *conn; + COLOPassthroughEntry *pass, *next; int ret; =20 if (mode =3D=3D PRIMARY_IN) { @@ -180,6 +181,31 @@ static int packet_enqueue(CompareState *s, int mode, C= onnection **con) } fill_connection_key(pkt, &key, false); =20 + /* Check COLO passthrough specifications */ + qemu_mutex_lock(&s->passthroughlist_mutex); + if (!QLIST_EMPTY(&s->passthroughlist)) { + QLIST_FOREACH_SAFE(pass, &s->passthroughlist, node, next) { + if (key.ip_proto =3D=3D pass->l4_protocol.p_proto) { + if (pass->src_port =3D=3D 0 || pass->src_port =3D=3D key.d= st_port) { + if (pass->src_ip.s_addr =3D=3D 0 || + pass->src_ip.s_addr =3D=3D key.src.s_addr) { + if (pass->dst_port =3D=3D 0 || + pass->dst_port =3D=3D key.src_port) { + if (pass->dst_ip.s_addr =3D=3D 0 || + pass->dst_ip.s_addr =3D=3D key.dst.s_addr)= { + packet_destroy(pkt, NULL); + pkt =3D NULL; + qemu_mutex_unlock(&s->passthroughlist_mute= x); + return -1; + } + } + } + } + } + } + } + qemu_mutex_unlock(&s->passthroughlist_mutex); + conn =3D connection_get(s->connection_track_table, &key, &s->conn_list); @@ -1232,6 +1258,7 @@ static void colo_compare_complete(UserCreatable *uc, = Error **errp) } =20 g_queue_init(&s->conn_list); + QLIST_INIT(&s->passthroughlist); =20 s->connection_track_table =3D g_hash_table_new_full(connection_key_has= h, connection_key_equal, @@ -1246,6 +1273,7 @@ static void colo_compare_complete(UserCreatable *uc, = Error **errp) qemu_cond_init(&event_complete_cond); colo_compare_active =3D true; } + qemu_mutex_init(&s->passthroughlist_mutex); QTAILQ_INSERT_TAIL(&net_compares, s, next); qemu_mutex_unlock(&colo_compare_mutex); =20 diff --git a/net/colo-compare.h b/net/colo-compare.h index 031b627a2f..995f28b833 100644 --- a/net/colo-compare.h +++ b/net/colo-compare.h @@ -23,6 +23,7 @@ #include "migration/migration.h" #include "sysemu/iothread.h" #include "colo.h" +#include =20 #define TYPE_COLO_COMPARE "colo-compare" typedef struct CompareState CompareState; @@ -39,6 +40,15 @@ typedef struct COLOSendCo { int ret; } COLOSendCo; =20 +typedef struct COLOPassthroughEntry { + struct protoent l4_protocol; + int src_port; + int dst_port; + struct in_addr src_ip; + struct in_addr dst_ip; + QLIST_ENTRY(COLOPassthroughEntry) node; +} COLOPassthroughEntry; + /* * + CompareState ++ * | | @@ -95,6 +105,8 @@ struct CompareState { =20 QEMUBH *event_bh; enum colo_event event; + QLIST_HEAD(, COLOPassthroughEntry) passthroughlist; + QemuMutex passthroughlist_mutex; =20 QTAILQ_ENTRY(CompareState) next; }; --=20 2.25.1 From nobody Sat May 18 14:01:30 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 1636687362656396.2669273358131; Thu, 11 Nov 2021 19:22:42 -0800 (PST) Received: from localhost ([::1]:48102 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mlN9F-0000c4-Kf for importer@patchew.org; Thu, 11 Nov 2021 22:22:41 -0500 Received: from eggs.gnu.org ([209.51.188.92]:37652) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlN8D-0006Kf-ND for qemu-devel@nongnu.org; Thu, 11 Nov 2021 22:21:37 -0500 Received: from mga03.intel.com ([134.134.136.65]:4409) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlN8B-0005U8-G6 for qemu-devel@nongnu.org; Thu, 11 Nov 2021 22:21:37 -0500 Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2021 19:21:34 -0800 Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2021 19:21:32 -0800 X-IronPort-AV: E=McAfee;i="6200,9189,10165"; a="233009118" X-IronPort-AV: E=Sophos;i="5.87,227,1631602800"; d="scan'208";a="233009118" X-IronPort-AV: E=Sophos;i="5.87,227,1631602800"; d="scan'208";a="504711499" From: Zhang Chen To: Jason Wang , Markus Armbruster , "Dr. David Alan Gilbert" , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eric Blake Subject: [PATCH for 7.0 V10 6/6] net/net.c: Add handler for passthrough filter command Date: Fri, 12 Nov 2021 11:11:12 +0800 Message-Id: <20211112031112.9303-7-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211112031112.9303-1-chen.zhang@intel.com> References: <20211112031112.9303-1-chen.zhang@intel.com> 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.65; envelope-from=chen.zhang@intel.com; helo=mga03.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, 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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zhang Chen , qemu-dev , Li Zhijian Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1636687363844100001 Content-Type: text/plain; charset="utf-8" Use the connection protocol,src port,dst port,src ip,dst ip as the key to passthrough certain network traffic in object with network packet processing function. Signed-off-by: Zhang Chen --- net/net.c | 199 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 197 insertions(+), 2 deletions(-) diff --git a/net/net.c b/net/net.c index 5d0d5914fb..443e88d396 100644 --- a/net/net.c +++ b/net/net.c @@ -55,6 +55,8 @@ #include "net/colo-compare.h" #include "net/filter.h" #include "qapi/string-output-visitor.h" +#include "net/colo-compare.h" +#include "qom/object_interfaces.h" =20 /* Net bridge is currently not supported for W32. */ #if !defined(_WIN32) @@ -1215,14 +1217,207 @@ void qmp_netdev_del(const char *id, Error **errp) } } =20 +static int check_addr(InetSocketAddressBase *addr) +{ + if (!addr || (addr->host && !qemu_isdigit(addr->host[0]))) { + return -1; + } + + if (atoi(addr->port) > 65536 || atoi(addr->port) < 0) { + return -1; + } + + return 0; +} + +/* The initial version only supports colo-compare */ +static CompareState *passthrough_filter_check(IPFlowSpec *spec, Error **er= rp) +{ + Object *container; + Object *obj; + CompareState *s; + + if (!spec->object_name) { + error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "object-name", + "Need input object name"); + return NULL; + } + + container =3D object_get_objects_root(); + obj =3D object_resolve_path_component(container, spec->object_name); + if (!obj) { + error_setg(errp, "object '%s' not found", spec->object_name); + return NULL; + } + + s =3D COLO_COMPARE(obj); + + if (!getprotobyname(spec->protocol)) { + error_setg(errp, "Passthrough filter get wrong protocol"); + return NULL; + } + + if (spec->source) { + if (check_addr(spec->source)) { + error_setg(errp, "Passthrough filter get wrong source"); + return NULL; + } + } + + if (spec->destination) { + if (check_addr(spec->destination)) { + error_setg(errp, "Passthrough filter get wrong destination"); + return NULL; + } + } + + return s; +} + +/* The initial version only supports colo-compare */ +static COLOPassthroughEntry *passthrough_filter_find(CompareState *s, + COLOPassthroughEntry = *ent) +{ + COLOPassthroughEntry *next =3D NULL, *origin =3D NULL; + + if (!QLIST_EMPTY(&s->passthroughlist)) { + QLIST_FOREACH_SAFE(origin, &s->passthroughlist, node, next) { + if ((ent->l4_protocol.p_proto =3D=3D origin->l4_protocol.p_pro= to) && + (ent->src_port =3D=3D origin->src_port) && + (ent->dst_port =3D=3D origin->dst_port) && + (ent->src_ip.s_addr =3D=3D origin->src_ip.s_addr) && + (ent->dst_ip.s_addr =3D=3D origin->dst_ip.s_addr)) { + return origin; + } + } + } + + return NULL; +} + +/* The initial version only supports colo-compare */ +static void passthrough_filter_add(CompareState *s, + IPFlowSpec *spec, + Error **errp) +{ + COLOPassthroughEntry *pass =3D NULL; + + pass =3D g_new0(COLOPassthroughEntry, 1); + + if (spec->protocol) { + memcpy(&pass->l4_protocol, getprotobyname(spec->protocol), + sizeof(struct protoent)); + } + + if (spec->source) { + if (!inet_aton(spec->source->host, &pass->src_ip)) { + pass->src_ip.s_addr =3D 0; + } + + pass->src_port =3D atoi(spec->source->port); + } + + if (spec->destination) { + if (!inet_aton(spec->destination->host, &pass->dst_ip)) { + pass->dst_ip.s_addr =3D 0; + } + + pass->dst_port =3D atoi(spec->destination->port); + } + + qemu_mutex_lock(&s->passthroughlist_mutex); + if (passthrough_filter_find(s, pass)) { + error_setg(errp, "The pass through connection already exists"); + g_free(pass); + qemu_mutex_unlock(&s->passthroughlist_mutex); + return; + } + + QLIST_INSERT_HEAD(&s->passthroughlist, pass, node); + qemu_mutex_unlock(&s->passthroughlist_mutex); +} + +/* The initial version only supports colo-compare */ +static void passthrough_filter_del(CompareState *s, + IPFlowSpec *spec, + Error **errp) +{ + COLOPassthroughEntry *pass =3D NULL, *result =3D NULL; + + pass =3D g_new0(COLOPassthroughEntry, 1); + + if (spec->protocol) { + memcpy(&pass->l4_protocol, getprotobyname(spec->protocol), + sizeof(struct protoent)); + } + + if (spec->source) { + if (!inet_aton(spec->source->host, &pass->src_ip)) { + pass->src_ip.s_addr =3D 0; + } + + pass->src_port =3D atoi(spec->source->port); + } + + if (spec->destination) { + if (!inet_aton(spec->destination->host, &pass->dst_ip)) { + pass->dst_ip.s_addr =3D 0; + } + + pass->dst_port =3D atoi(spec->destination->port); + } + + qemu_mutex_lock(&s->passthroughlist_mutex); + + result =3D passthrough_filter_find(s, pass); + if (result) { + QLIST_REMOVE(result, node); + g_free(result); + } else { + error_setg(errp, "Can't find the IP flow Spec"); + } + + g_free(pass); + g_free(spec); + qemu_mutex_unlock(&s->passthroughlist_mutex); +} + +/* The initial version only supports colo-compare */ void qmp_passthrough_filter_add(IPFlowSpec *spec, Error **errp) { - /* TODO implement setup passthrough rule */ + CompareState *s; + Error *err =3D NULL; + + s =3D passthrough_filter_check(spec, &err); + if (err) { + error_propagate(errp, err); + return; + } + + passthrough_filter_add(s, spec, &err); + if (err) { + error_propagate(errp, err); + return; + } } =20 +/* The initial version only supports colo-compare */ void qmp_passthrough_filter_del(IPFlowSpec *spec, Error **errp) { - /* TODO implement delete passthrough rule */ + CompareState *s; + Error *err =3D NULL; + + s =3D passthrough_filter_check(spec, &err); + if (err) { + error_propagate(errp, err); + return; + } + + passthrough_filter_del(s, spec, &err); + if (err) { + error_propagate(errp, err); + return; + } } =20 static void netfilter_print_info(Monitor *mon, NetFilterState *nf) --=20 2.25.1