From patchwork Thu Sep 8 07:37:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Ding X-Patchwork-Id: 1675449 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::136; helo=smtp3.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MNWDl3SjTz1yhR for ; Thu, 8 Sep 2022 17:37:43 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 7F5806121E; Thu, 8 Sep 2022 07:37:39 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 7F5806121E X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Rsb49VFzIfce; Thu, 8 Sep 2022 07:37:38 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp3.osuosl.org (Postfix) with ESMTPS id AA28561212; Thu, 8 Sep 2022 07:37:37 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org AA28561212 Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 7FC44C0033; Thu, 8 Sep 2022 07:37:37 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp4.osuosl.org (smtp4.osuosl.org [140.211.166.137]) by lists.linuxfoundation.org (Postfix) with ESMTP id 216D9C002D for ; Thu, 8 Sep 2022 07:37:36 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id E1BFD41B3D for ; Thu, 8 Sep 2022 07:37:35 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org E1BFD41B3D X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id t3FoFr5NWpTn for ; Thu, 8 Sep 2022 07:37:35 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org C22B8418F0 Received: from mail-m3164.qiye.163.com (mail-m3164.qiye.163.com [103.74.31.64]) by smtp4.osuosl.org (Postfix) with ESMTPS id C22B8418F0 for ; Thu, 8 Sep 2022 07:37:34 +0000 (UTC) Received: from WINDOWS-PC3SSGC.localdomain (unknown [218.94.118.90]) by mail-m3164.qiye.163.com (Hmail) with ESMTPA id 2B95B6201EA; Thu, 8 Sep 2022 15:37:31 +0800 (CST) From: Daniel Ding To: dev@openvswitch.org Date: Thu, 8 Sep 2022 15:37:26 +0800 Message-Id: <20220908073726.1232-1-zhihui.ding@easystack.cn> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-HM-Spam-Status: e1kfGhgUHx5ZQUpXWQgPGg8OCBgUHx5ZQUlOS1dZFg8aDwILHllBWSg2Ly tZV1koWUFJQjdXWS1ZQUlXWQ8JGhUIEh9ZQVlCTUIYVhpIGEgeTUlIQxkYSlUZERMWGhIXJBQOD1 lXWRgSC1lBWUlKQ1VCT1VKSkNVQktZV1kWGg8SFR0UWUFZT0tIVUpKS0hKQ1VKS0tVS1kG X-HM-Sender-Digest: e1kMHhlZQR0aFwgeV1kSHx4VD1lBWUc6PAg6Hww5GjIRFT5RTFYDOhU0 TEwKFAhVSlVKTU1JTUlJTU5KTUJOVTMWGhIXVQETEhMOElUfEhUcOx4aCAIIDxoYEFUYFUVZV1kS C1lBWUlKQ1VCT1VKSkNVQktZV1kIAVlBSENIQjcG X-HM-Tid: 0a831c0853cc00a4kurm2b95b6201ea Cc: Daniel Ding , i.maximets@ovn.org Subject: [ovs-dev] [PATCH v4] ovs-tcpdump: Cleanup mirror port on SIGHUP/SIGTERM X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" If ovs-tcpdump received HUP or TERM signal, mirror and mirror interface should be destroyed. This often happens, when controlling terminal is closed, like ssh session closed, and other users use kill to terminate it. Acked-by: Mike Pattrick Signed-off-by: Daniel Ding --- utilities/ovs-tcpdump.in | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/utilities/ovs-tcpdump.in b/utilities/ovs-tcpdump.in index 7fd26e405..b3f764c87 100755 --- a/utilities/ovs-tcpdump.in +++ b/utilities/ovs-tcpdump.in @@ -44,6 +44,7 @@ try: from ovs import jsonrpc from ovs.poller import Poller from ovs.stream import Stream + from ovs.fatal_signal import add_hook except Exception: print("ERROR: Please install the correct Open vSwitch python support") print(" libraries (version @VERSION@).") @@ -405,6 +406,17 @@ def py_which(executable): for path in os.environ["PATH"].split(os.pathsep)) +def teardown(db_sock, interface, mirror_interface, tap_created): + def cleanup_mirror(): + ovsdb = OVSDB(db_sock) + ovsdb.destroy_mirror(interface, ovsdb.port_bridge(interface)) + ovsdb.destroy_port(mirror_interface, ovsdb.port_bridge(interface)) + if tap_created is True: + _del_taps[sys.platform](mirror_interface) + + add_hook(cleanup_mirror, None, True) + + def main(): rundir = os.environ.get('OVS_RUNDIR', '@RUNDIR@') db_sock = 'unix:%s' % os.path.join(rundir, "db.sock") @@ -489,6 +501,9 @@ def main(): print("ERROR: Mirror port (%s) exists for port %s." % (mirror_interface, interface)) sys.exit(1) + + teardown(db_sock, interface, mirror_interface, tap_created) + try: ovsdb.make_port(mirror_interface, ovsdb.port_bridge(interface)) ovsdb.bridge_mirror(interface, mirror_interface, @@ -496,12 +511,6 @@ def main(): mirror_select_all) except OVSDBException as oe: print("ERROR: Unable to properly setup the mirror: %s." % str(oe)) - try: - ovsdb.destroy_port(mirror_interface, ovsdb.port_bridge(interface)) - if tap_created is True: - _del_taps[sys.platform](mirror_interface) - except Exception: - pass sys.exit(1) ovsdb.close_idl() @@ -517,12 +526,6 @@ def main(): except KeyboardInterrupt: if pipes.poll() is None: pipes.terminate() - - ovsdb = OVSDB(db_sock) - ovsdb.destroy_mirror(interface, ovsdb.port_bridge(interface)) - ovsdb.destroy_port(mirror_interface, ovsdb.port_bridge(interface)) - if tap_created is True: - _del_taps[sys.platform](mirror_interface) except Exception: print("Unable to tear down the ports and mirrors.") print("Please use ovs-vsctl to remove the ports and mirrors created.")