When the 'mirror-to' option was conceived, it was intended to allow users
the ability to arbitrarily build their own ports to use the mirror port
rather than always using an algorithmically defined mirror port.  However,
the mirror port code never accommodated a port that the user defined as
part of an OVS bridge.  This could be useful for running against all kinds
of OVS specific ports.

Adjust the 'mirror-to' option so that when the user specifies a port, the
utility searches through system interfaces, as well as the OVS DB.  This
means that we need to drop the port_exists() check for the mirror port,
but that should be okay.

Reported-at: 
https://mail.openvswitch.org/pipermail/ovs-discuss/2025-March/053508.html
Reported-at: https://issues.redhat.com/browse/FDP-1194
Reported-by: Jun Wang <[email protected]>
Acked-by: Mike Pattrick <[email protected]>
Signed-off-by: Aaron Conole <[email protected]>
---
v1->v2: Reorganize variables, per Eelco's comments
v2->v3: Add documentation, per Ilya's comments

 Documentation/ref/ovs-tcpdump.8.rst |  5 ++++-
 NEWS                                |  3 +++
 utilities/ovs-tcpdump.in            | 28 +++++++++++++++++-----------
 3 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/Documentation/ref/ovs-tcpdump.8.rst 
b/Documentation/ref/ovs-tcpdump.8.rst
index e7bd5e9e4f..7576034324 100644
--- a/Documentation/ref/ovs-tcpdump.8.rst
+++ b/Documentation/ref/ovs-tcpdump.8.rst
@@ -55,7 +55,10 @@ Options
 * ``--mirror-to``
 
   The name of the interface which should be the destination of the mirrored
-  packets. The default is ``mi<port>``.
+  packets. If the specified interface does not exist, it will be created as
+  part of the setup process. If the interface already exists, it must be a
+  port type that can be used with the ``tcpdump`` utility.  Mirror ports
+  cannot be used for normal traffic. The default value is ``mi<port>``.
 
 * ``--span``
 
diff --git a/NEWS b/NEWS
index ba52b2f4d9..9667296657 100644
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,9 @@ Post-v3.5.0
    - Tunnels:
      * Support for previously deprecated LISP and STT tunnel port types
        is now removed.
+   - ovs-tcpdump:
+     * Update the --mirror-to option, adding support for specifying an
+       existing port as a mirror interface.
 
 
 v3.5.0 - 17 Feb 2025
diff --git a/utilities/ovs-tcpdump.in b/utilities/ovs-tcpdump.in
index 187eafdf25..f3e90fdbba 100755
--- a/utilities/ovs-tcpdump.in
+++ b/utilities/ovs-tcpdump.in
@@ -421,13 +421,16 @@ def py_which(executable):
                for path in os.environ["PATH"].split(os.pathsep))
 
 
-def teardown(db_sock, interface, mirror_interface, tap_created):
+def teardown(db_sock, interface, mirror_interface, mirror_created,
+             tap_created):
     def cleanup_mirror():
         try:
             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:
+            if mirror_created:
+                ovsdb.destroy_port(mirror_interface,
+                                   ovsdb.port_bridge(interface))
+            if tap_created:
                 _del_taps[sys.platform](mirror_interface)
         except Exception:
             print("Unable to tear down the ports and mirrors.")
@@ -505,15 +508,21 @@ def main():
         if sys.platform in _make_mirror_name:
             mirror_interface = _make_mirror_name[sys.platform](interface)
 
+    mirror_created = True
     if sys.platform in _make_taps and \
-       mirror_interface not in interfaces():
+       mirror_interface not in interfaces() and \
+       not ovsdb.port_exists(mirror_interface):
         _make_taps[sys.platform](mirror_interface,
                                  ovsdb.interface_mtu(interface))
         tap_created = True
     else:
+        if mirror_interface in interfaces() or \
+           ovsdb.port_exists(mirror_interface):
+            mirror_created = False
         tap_created = False
 
-    if mirror_interface not in interfaces():
+    if mirror_interface not in interfaces() and \
+       not ovsdb.port_exists(mirror_interface):
         print("ERROR: Please create an interface called `%s`" %
               mirror_interface)
         print("See your OS guide for how to do this.")
@@ -524,15 +533,12 @@ def main():
     if not ovsdb.port_exists(interface):
         print("ERROR: Port %s does not exist." % interface)
         sys.exit(1)
-    if ovsdb.port_exists(mirror_interface):
-        print("ERROR: Mirror port (%s) exists for port %s." %
-              (mirror_interface, interface))
-        sys.exit(1)
 
-    teardown(db_sock, interface, mirror_interface, tap_created)
+    teardown(db_sock, interface, mirror_interface, mirror_created, tap_created)
 
     try:
-        ovsdb.make_port(mirror_interface, ovsdb.port_bridge(interface))
+        if mirror_created:
+            ovsdb.make_port(mirror_interface, ovsdb.port_bridge(interface))
         ovsdb.bridge_mirror(interface, mirror_interface,
                             ovsdb.port_bridge(interface),
                             mirror_select_all, mirror_filter=mirror_filter)
-- 
2.47.1

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to