diff mbox series

[ovs-dev,v3] ovs-tcpdump: Cleanup mirror port on SIGHUP/SIGTERM

Message ID 20220908070718.1175-1-zhihui.ding@easystack.cn
State Superseded
Headers show
Series [ovs-dev,v3] ovs-tcpdump: Cleanup mirror port on SIGHUP/SIGTERM | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test fail github build: failed
ovsrobot/intel-ovs-compilation success test: success

Commit Message

Daniel Ding Sept. 8, 2022, 7:07 a.m. UTC
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 <mkp@redhat.com>
Signed-off-by: Daniel Ding <zhihui.ding@easystack.cn>
---
 utilities/ovs-tcpdump.in | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

Comments

Daniel Ding Sept. 8, 2022, 7:34 a.m. UTC | #1
Excuse me,

This patch is invalid because of indentation.


On 2022/9/8 15:07, Daniel Ding wrote:
> 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<mkp@redhat.com>
> Signed-off-by: Daniel Ding<zhihui.ding@easystack.cn>
> ---
>   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..7d5abba76 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.")
diff mbox series

Patch

diff --git a/utilities/ovs-tcpdump.in b/utilities/ovs-tcpdump.in
index 7fd26e405..7d5abba76 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.")