ebb439
From 3b3a88ed94814b44ba958785ea93b4e2e7bbb4ac Mon Sep 17 00:00:00 2001
ebb439
From: Dumitru Ceara <dceara@redhat.com>
ebb439
Date: Fri, 23 Oct 2020 10:07:21 +0200
ebb439
Subject: [PATCH 1/2] ovn-detrace: Only decode br-int OVS interfaces.
ebb439
ebb439
Do not assume 'ofport' is unique for all OVS interfaces in the system.  This
ebb439
is true only for interfaces within the same OVS bridge.  Also, only decode
ebb439
br-int related interfaces.
ebb439
ebb439
Also, fix printing of potential duplicate UUIDs decoded from cookies.
ebb439
ebb439
Reported-by: Michael Cambria <mcambria@redhat.com>
ebb439
Reported-at: https://bugzilla.redhat.com/1890803
ebb439
Fixes: 8051499a6c1b ("ovn-detrace: Add support for other types of SB cookies.")
ebb439
Signed-off-by: Dumitru Ceara <dceara@redhat.com>
ebb439
Signed-off-by: Numan Siddique <numans@ovn.org>
ebb439
(cherry picked from upstream commit fcdac56feb20203e1bcc8e960624a8bdd9162cc7)
ebb439
ebb439
Change-Id: I9e7fa37fa9f0276f260345f9596b1bca9717045e
ebb439
---
ebb439
 utilities/ovn-detrace.in | 35 ++++++++++++++++++++++++++++++-----
ebb439
 1 file changed, 30 insertions(+), 5 deletions(-)
ebb439
ebb439
diff --git a/utilities/ovn-detrace.in b/utilities/ovn-detrace.in
ebb439
index 4f8dd5f..2344f52 100755
ebb439
--- a/utilities/ovn-detrace.in
ebb439
+++ b/utilities/ovn-detrace.in
ebb439
@@ -117,18 +117,27 @@ class OVSDB(object):
ebb439
     def _find_rows(self, table_name, find_fn):
ebb439
         return filter(find_fn, self.get_table(table_name).rows.values())
ebb439
 
ebb439
-    def _find_rows_by_name(self, table_name, value):
ebb439
+    def find_rows_by_name(self, table_name, value):
ebb439
         return self._find_rows(table_name, lambda row: row.name == value)
ebb439
 
ebb439
     def find_rows_by_partial_uuid(self, table_name, value):
ebb439
         return self._find_rows(table_name,
ebb439
                                lambda row: str(row.uuid).startswith(value))
ebb439
 
ebb439
+    def get_first_record(self, table_name):
ebb439
+        table_rows = self.get_table(table_name).rows.values()
ebb439
+        if len(table_rows) == 0:
ebb439
+            return None
ebb439
+        return next(iter(table_rows))
ebb439
+
ebb439
 class CookieHandler(object):
ebb439
     def __init__(self, db, table):
ebb439
         self._db = db
ebb439
         self._table = table
ebb439
 
ebb439
+    def print(self, msg):
ebb439
+        print_h(msg)
ebb439
+
ebb439
     def get_records(self, cookie):
ebb439
         return []
ebb439
 
ebb439
@@ -320,10 +329,25 @@ class OvsInterfaceHandler(CookieHandler):
ebb439
     def __init__(self, ovs_db):
ebb439
         super(OvsInterfaceHandler, self).__init__(ovs_db, 'Interface')
ebb439
 
ebb439
+        # Store the interfaces connected to the integration bridge in a dict
ebb439
+        # indexed by ofport.
ebb439
+        br = self.get_br_int()
ebb439
+        self._intfs = {
ebb439
+            i.ofport[0] : i for p in br.ports
ebb439
+                            for i in p.interfaces if len(i.ofport) > 0
ebb439
+        }
ebb439
+
ebb439
+    def get_br_int(self):
ebb439
+        ovsrec = self._db.get_first_record('Open_vSwitch')
ebb439
+        if ovsrec:
ebb439
+            br_name = ovsrec.external_ids.get('ovn-bridge', 'br-int')
ebb439
+        else:
ebb439
+            br_name = 'br-int'
ebb439
+        return next(iter(self._db.find_rows_by_name('Bridge', br_name)))
ebb439
+
ebb439
     def get_records(self, ofport):
ebb439
-        return self._db._find_rows(self._table,
ebb439
-                                   lambda intf: len(intf.ofport) > 0 and
ebb439
-                                        str(intf.ofport[0]) == ofport)
ebb439
+        intf = self._intfs.get(int(ofport))
ebb439
+        return [intf] if intf else []
ebb439
 
ebb439
     def print_record(self, intf):
ebb439
         print_p('OVS Interface: %s (%s)' %
ebb439
@@ -331,7 +355,8 @@ class OvsInterfaceHandler(CookieHandler):
ebb439
 
ebb439
 def print_record_from_cookie(ovnnb_db, cookie_handlers, cookie):
ebb439
     for handler in cookie_handlers:
ebb439
-        for i, record in enumerate(handler.get_records(cookie)):
ebb439
+        records = list(handler.get_records(cookie))
ebb439
+        for i, record in enumerate(records):
ebb439
             if i > 0:
ebb439
                 handler.print('[Duplicate uuid cookie]')
ebb439
             handler.print_record(record)
ebb439
-- 
ebb439
1.8.3.1
ebb439