From patchwork Thu Oct 14 16:41:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Moreno X-Patchwork-Id: 1541027 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=d/BQJrkx; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HVZv55CHFz9s0r for ; Fri, 15 Oct 2021 03:42:25 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id AAB788392B; Thu, 14 Oct 2021 16:42:23 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id tJdzGZNAD4dk; Thu, 14 Oct 2021 16:42:21 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp1.osuosl.org (Postfix) with ESMTPS id 0568D835BA; Thu, 14 Oct 2021 16:42:21 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 897C1C001C; Thu, 14 Oct 2021 16:42:20 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 1C716C0022 for ; Thu, 14 Oct 2021 16:42:19 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 6376F403EC for ; Thu, 14 Oct 2021 16:42:12 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Authentication-Results: smtp2.osuosl.org (amavisd-new); dkim=pass (1024-bit key) header.d=redhat.com Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4bRHMF73wY34 for ; Thu, 14 Oct 2021 16:42:10 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by smtp2.osuosl.org (Postfix) with ESMTPS id 4636040585 for ; Thu, 14 Oct 2021 16:42:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634229729; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wkuLeJuP07SOlVP8pgIMX5/feH/2sw7nIAjmvDedYqo=; b=d/BQJrkxs1lT8LjvmDwwnXUnfUfKx7AEM13RDZYDeb8uCYK7pUTFrbTqWQvqNUgEqDpJmL HNNz+Lydk471nRkkARANkjYkHeRy3RWW4r6S8qCg6Vlxbj09JX2D3pQc+M4SmbeWxNaikq tvqyL8EBxTI0OOQ/Rea7kbe7AM3Oc9k= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-375-GR8mG9vDMHiGukHxcXgUvg-1; Thu, 14 Oct 2021 12:41:57 -0400 X-MC-Unique: GR8mG9vDMHiGukHxcXgUvg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4F05256BFF for ; Thu, 14 Oct 2021 16:41:55 +0000 (UTC) Received: from amorenoz.users.ipa.redhat.com (unknown [10.39.193.133]) by smtp.corp.redhat.com (Postfix) with ESMTP id AD4F460657; Thu, 14 Oct 2021 16:41:53 +0000 (UTC) From: Adrian Moreno To: dev@openvswitch.org Date: Thu, 14 Oct 2021 18:41:27 +0200 Message-Id: <20211014164130.606220-3-amorenoz@redhat.com> In-Reply-To: <20211014164130.606220-1-amorenoz@redhat.com> References: <20211014164130.606220-1-amorenoz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=amorenoz@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Cc: dceara@redhat.com Subject: [ovs-dev] [RFC ovn 2/5] ovn-detrace: use configurable printer object 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" Intead of declaring global printing functions, use a configurable printer object that has to declare print_p and print_h. That way external programs that may want to use ovn-detrace can obtain the information provided by ovn-detrace by just re-implementing the printer object Signed-off-by: Adrian Moreno --- utilities/ovn-detrace.in | 177 +++++++++++++++++++++------------------ 1 file changed, 95 insertions(+), 82 deletions(-) diff --git a/utilities/ovn-detrace.in b/utilities/ovn-detrace.in index 1e4f76dd7..d26efbd16 100755 --- a/utilities/ovn-detrace.in +++ b/utilities/ovn-detrace.in @@ -56,8 +56,15 @@ The following options are also available: """ % {'argv0': argv0}) sys.exit(0) -print_p = functools.partial(print, ' * ') -print_h = functools.partial(print, ' * ') +class Printer(object): + def __init__(self): + pass + + def print_p(self, string): + print(' * ' + string) + + def print_h(self, string): + print(' * ' + string) def datapath_str(datapath): return '"%s" (%s)' % (str(datapath.external_ids.get('name')), @@ -134,12 +141,16 @@ class OVSDB(object): return next(iter(table_rows)) class CookieHandler(object): - def __init__(self, db, table): + def __init__(self, db, table, printer): self._db = db self._table = table + self._printer = printer or Printer() - def print(self, msg): - print_h(msg) + def print_h(self, msg): + self._printer.print_h(msg) + + def print_p(self, msg): + self._printer.print_p(msg) def get_records(self, cookie): return [] @@ -151,8 +162,8 @@ class CookieHandler(object): pass class CookieHandlerByUUUID(CookieHandler): - def __init__(self, db, table): - super(CookieHandlerByUUUID, self).__init__(db, table) + def __init__(self, db, table, printer): + super(CookieHandlerByUUUID, self).__init__(db, table, printer) def get_records(self, cookie): # Adjust cookie to include leading zeroes if needed. @@ -160,8 +171,8 @@ class CookieHandlerByUUUID(CookieHandler): return self._db.find_rows_by_partial_uuid(self._table, cookie) class ACLHintHandler(CookieHandlerByUUUID): - def __init__(self, ovnnb_db): - super(ACLHintHandler, self).__init__(ovnnb_db, 'ACL') + def __init__(self, ovnnb_db, printer): + super(ACLHintHandler, self).__init__(ovnnb_db, 'ACL', printer) def print_record(self, acl): output = 'ACL: %s, priority=%s, ' \ @@ -171,112 +182,112 @@ class ACLHintHandler(CookieHandlerByUUUID): acl.action) if acl.log: output += ' (log)' - print_h(output) + self.print_h(output) class DHCPOptionsHintHandler(CookieHandlerByUUUID): - def __init__(self, ovnnb_db): - super(DHCPOptionsHintHandler, self).__init__(ovnnb_db, 'DHCP_Options') + def __init__(self, ovnnb_db, printer): + super(DHCPOptionsHintHandler, self).__init__(ovnnb_db, 'DHCP_Options', printer) def print_record(self, dhcp_opt): - print_h('DHCP Options: cidr %s options (%s)' % ( + self.print_h('DHCP Options: cidr %s options (%s)' % ( dhcp_opt.cidr, dhcp_opt.options)) class ForwardingGroupHintHandler(CookieHandlerByUUUID): - def __init__(self, ovnnb_db): + def __init__(self, ovnnb_db, printer): super(ForwardingGroupHintHandler, self).__init__(ovnnb_db, - 'Forwarding_Group') + 'Forwarding_Group', printer) def print_record(self, fwd_group): - print_h('Forwarding Group: name %s vip %s vmac %s liveness %s child ports (%s)' % ( + self.print_h('Forwarding Group: name %s vip %s vmac %s liveness %s child ports (%s)' % ( fwd_group.name, fwd_group.vip, fwd_group.vmac, fwd_group.liveness, fwd_group.child_port)) class LSPHintHandler(CookieHandlerByUUUID): - def __init__(self, ovnnb_db): - super(LSPHintHandler, self).__init__(ovnnb_db, 'Logical_Switch_Port') + def __init__(self, ovnnb_db, printer): + super(LSPHintHandler, self).__init__(ovnnb_db, 'Logical_Switch_Port', printer) def print_record(self, lsp): - print_h('Logical Switch Port: %s type %s (addresses %s, dynamic addresses %s, security %s' % ( + self.print_h('Logical Switch Port: %s type %s (addresses %s, dynamic addresses %s, security %s' % ( lsp.name, lsp.type, lsp.addresses, lsp.dynamic_addresses, lsp.port_security)) class LRPHintHandler(CookieHandlerByUUUID): - def __init__(self, ovnnb_db): - super(LRPHintHandler, self).__init__(ovnnb_db, 'Logical_Router_Port') + def __init__(self, ovnnb_db, printer): + super(LRPHintHandler, self).__init__(ovnnb_db, 'Logical_Router_Port', printer) def print_record(self, lrp): - print_h('Logical Router Port: %s mac %s networks %s ipv6_ra_configs %s' % ( + self.print_h('Logical Router Port: %s mac %s networks %s ipv6_ra_configs %s' % ( lrp.name, lrp.mac, lrp.networks, lrp.ipv6_ra_configs)) class LRPolicyHandler(CookieHandlerByUUUID): - def __init__(self, ovnnb_db): - super(LRPolicyHandler, self).__init__(ovnnb_db, 'Logical_Router_Policy') + def __init__(self, ovnnb_db, printer): + super(LRPolicyHandler, self).__init__(ovnnb_db, 'Logical_Router_Policy', printer) def print_record(self, policy): - print_h('Logical Router Policy: priority %s match %s action %s nexthop %s' % ( + self.print_h('Logical Router Policy: priority %s match %s action %s nexthop %s' % ( policy.priority, policy.match, policy.action, policy.nexthop)) class LoadBalancerHintHandler(CookieHandlerByUUUID): - def __init__(self, ovnnb_db): - super(LoadBalancerHintHandler, self).__init__(ovnnb_db, 'Load_Balancer') + def __init__(self, ovnnb_db, printer): + super(LoadBalancerHintHandler, self).__init__(ovnnb_db, 'Load_Balancer', printer) def print_record(self, lb): - print_h('Load Balancer: %s protocol %s vips %s ip_port_mappings %s' % ( + self.print_h('Load Balancer: %s protocol %s vips %s ip_port_mappings %s' % ( lb.name, lb.protocol, lb.vips, lb.ip_port_mappings)) class NATHintHandler(CookieHandlerByUUUID): - def __init__(self, ovnnb_db): - super(NATHintHandler, self).__init__(ovnnb_db, 'NAT') + def __init__(self, ovnnb_db, printer): + super(NATHintHandler, self).__init__(ovnnb_db, 'NAT', printer) def print_record(self, nat): - print_h('NAT: external IP %s external_mac %s logical_ip %s logical_port %s type %s' % ( + self.print_h('NAT: external IP %s external_mac %s logical_ip %s logical_port %s type %s' % ( nat.external_ip, nat.external_mac, nat.logical_ip, nat.logical_port, nat.type)) class StaticRouteHintHandler(CookieHandlerByUUUID): - def __init__(self, ovnnb_db): + def __init__(self, ovnnb_db, printer): super(StaticRouteHintHandler, self).__init__(ovnnb_db, - 'Logical_Router_Static_Route') + 'Logical_Router_Static_Route', printer) def print_record(self, route): - print_h('Route: %s via %s (port %s), policy=%s' % ( + self.print_h('Route: %s via %s (port %s), policy=%s' % ( route.ip_prefix, route.nexthop, route.output_port, route.policy)) class QoSHintHandler(CookieHandlerByUUUID): - def __init__(self, ovnnb_db): - super(QoSHintHandler, self).__init__(ovnnb_db, 'QoS') + def __init__(self, ovnnb_db, printer): + super(QoSHintHandler, self).__init__(ovnnb_db, 'QoS', printer) def print_record(self, qos): - print_h('QoS: priority %s direction %s match %s action %s bandwidth %s' % ( + self.print_h('QoS: priority %s direction %s match %s action %s bandwidth %s' % ( qos.priority, qos.direction, qos.match, qos.action, qos.bandwidth)) class LogicalFlowHandler(CookieHandlerByUUUID): - def __init__(self, ovnnb_db, ovnsb_db): - super(LogicalFlowHandler, self).__init__(ovnsb_db, 'Logical_Flow') + def __init__(self, ovnnb_db, ovnsb_db, printer): + super(LogicalFlowHandler, self).__init__(ovnsb_db, 'Logical_Flow', printer) self._hint_handlers = [ - ACLHintHandler(ovnnb_db), - DHCPOptionsHintHandler(ovnnb_db), - ForwardingGroupHintHandler(ovnnb_db), - LSPHintHandler(ovnnb_db), - LRPHintHandler(ovnnb_db), - LRPolicyHandler(ovnnb_db), - LoadBalancerHintHandler(ovnnb_db), - NATHintHandler(ovnnb_db), - StaticRouteHintHandler(ovnnb_db), - QoSHintHandler(ovnnb_db), + ACLHintHandler(ovnnb_db, printer), + DHCPOptionsHintHandler(ovnnb_db, printer), + ForwardingGroupHintHandler(ovnnb_db, printer), + LSPHintHandler(ovnnb_db, printer), + LRPHintHandler(ovnnb_db, printer), + LRPolicyHandler(ovnnb_db, printer), + LoadBalancerHintHandler(ovnnb_db, printer), + NATHintHandler(ovnnb_db, printer), + StaticRouteHintHandler(ovnnb_db, printer), + QoSHintHandler(ovnnb_db, printer), ] def print_record(self, lflow): - print_p('Logical datapaths:') + self.print_p('Logical datapaths:') datapaths = lflow.logical_datapath if lflow.logical_dp_group: datapaths.extend(lflow.logical_dp_group[0].datapaths) for datapath in datapaths: - print_p(' %s [%s]' % (datapath_str(datapath), lflow.pipeline)) - print_p('Logical flow: table=%s (%s), priority=%s, ' + self.print_p(' %s [%s]' % (datapath_str(datapath), lflow.pipeline)) + self.print_p('Logical flow: table=%s (%s), priority=%s, ' 'match=(%s), actions=(%s)' % (lflow.table_id, lflow.external_ids.get('stage-name'), lflow.priority, @@ -291,58 +302,58 @@ class LogicalFlowHandler(CookieHandlerByUUUID): for handler in self._hint_handlers: for i, record in enumerate(handler.get_records(hint)): if i > 0: - print_h('[Duplicate uuid hint]') + self.print_h('[Duplicate uuid hint]') handler.print_record(record) class PortBindingHandler(CookieHandlerByUUUID): - def __init__(self, ovnsb_db): - super(PortBindingHandler, self).__init__(ovnsb_db, 'Port_Binding') + def __init__(self, ovnsb_db, printer): + super(PortBindingHandler, self).__init__(ovnsb_db, 'Port_Binding', printer) def print_record(self, pb): - print_p('Logical datapath: %s' % (datapath_str(pb.datapath))) - print_p('Port Binding: logical_port "%s", tunnel_key %ld, %s' % + self.print_p('Logical datapath: %s' % (datapath_str(pb.datapath))) + self.print_p('Port Binding: logical_port "%s", tunnel_key %ld, %s' % (pb.logical_port, pb.tunnel_key, chassis_str(pb.chassis))) class MacBindingHandler(CookieHandlerByUUUID): - def __init__(self, ovnsb_db): - super(MacBindingHandler, self).__init__(ovnsb_db, 'MAC_Binding') + def __init__(self, ovnsb_db, printer): + super(MacBindingHandler, self).__init__(ovnsb_db, 'MAC_Binding', printer) def print_record(self, mb): - print_p('Logical datapath: %s' % (datapath_str(mb.datapath))) - print_p('MAC Binding: ip "%s", logical_port "%s", mac "%s"' % + self.print_p('Logical datapath: %s' % (datapath_str(mb.datapath))) + self.print_p('MAC Binding: ip "%s", logical_port "%s", mac "%s"' % (mb.ip, mb.logical_port, mb.mac)) class MulticastGroupHandler(CookieHandlerByUUUID): - def __init__(self, ovnsb_db): + def __init__(self, ovnsb_db, printer): super(MulticastGroupHandler, self).__init__(ovnsb_db, - 'Multicast_Group') + 'Multicast_Group', printer) def print_record(self, mc): mc_ports = ', '.join([pb.logical_port for pb in mc.ports]) - print_p('Logical datapath: %s' % (datapath_str(mc.datapath))) - print_p('Multicast Group: name "%s", tunnel_key %ld ports: (%s)' % + self.print_p('Logical datapath: %s' % (datapath_str(mc.datapath))) + self.print_p('Multicast Group: name "%s", tunnel_key %ld ports: (%s)' % (mc.name, mc.tunnel_key, mc_ports)) class ChassisHandler(CookieHandlerByUUUID): - def __init__(self, ovnsb_db): - super(ChassisHandler, self).__init__(ovnsb_db, 'Chassis') + def __init__(self, ovnsb_db, printer): + super(ChassisHandler, self).__init__(ovnsb_db, 'Chassis', printer) def print_record(self, chassis): - print_p('Chassis: %s' % (chassis_str([chassis]))) + self.print_p('Chassis: %s' % (chassis_str([chassis]))) class SBLoadBalancerHandler(CookieHandlerByUUUID): - def __init__(self, ovnsb_db): - super(SBLoadBalancerHandler, self).__init__(ovnsb_db, 'Load_Balancer') + def __init__(self, ovnsb_db, printer): + super(SBLoadBalancerHandler, self).__init__(ovnsb_db, 'Load_Balancer', printer) def print_record(self, lb): - print_p('Load Balancer: %s protocol %s vips %s' % ( + self.print_p('Load Balancer: %s protocol %s vips %s' % ( lb.name, lb.protocol, lb.vips)) class OvsInterfaceHandler(CookieHandler): - def __init__(self, ovs_db): - super(OvsInterfaceHandler, self).__init__(ovs_db, 'Interface') + def __init__(self, ovs_db, printer): + super(OvsInterfaceHandler, self).__init__(ovs_db, 'Interface', printer) # Store the interfaces connected to the integration bridge in a dict # indexed by ofport. @@ -365,7 +376,7 @@ class OvsInterfaceHandler(CookieHandler): return [intf] if intf else [] def print_record(self, intf): - print_p('OVS Interface: %s (%s)' % + self.print_p('OVS Interface: %s (%s)' % (intf.name, intf.external_ids.get('iface-id'))) def print_record_from_cookie(ovnnb_db, cookie_handlers, cookie): @@ -373,7 +384,7 @@ def print_record_from_cookie(ovnnb_db, cookie_handlers, cookie): records = list(handler.get_records(cookie)) for i, record in enumerate(records): if i > 0: - handler.print('[Duplicate uuid cookie]') + handler.print_h('[Duplicate uuid cookie]') handler.print_record(record) handler.print_hint(record, ovnnb_db) @@ -459,13 +470,15 @@ def main(): ovsdb_ovnsb = OVSDB(ovnsb_db, 'OVN_Southbound') ovsdb_ovnnb = OVSDB(ovnnb_db, 'OVN_Northbound') + printer = Printer() + cookie_handlers = [ - LogicalFlowHandler(ovsdb_ovnnb, ovsdb_ovnsb), - PortBindingHandler(ovsdb_ovnsb), - MacBindingHandler(ovsdb_ovnsb), - MulticastGroupHandler(ovsdb_ovnsb), - ChassisHandler(ovsdb_ovnsb), - SBLoadBalancerHandler(ovsdb_ovnsb) + LogicalFlowHandler(ovsdb_ovnnb, ovsdb_ovnsb, printer), + PortBindingHandler(ovsdb_ovnsb, printer), + MacBindingHandler(ovsdb_ovnsb, printer), + MulticastGroupHandler(ovsdb_ovnsb, printer), + ChassisHandler(ovsdb_ovnsb, printer), + SBLoadBalancerHandler(ovsdb_ovnsb, printer) ] regex_cookie = re.compile(r'^.*cookie 0x([0-9a-fA-F]+)') @@ -478,7 +491,7 @@ def main(): regex_inport = re.compile(r'^ *[0-9]+\. *in_port=([0-9])+') regex_outport = re.compile(r'^ *output:([0-9]+)') ofport_handlers = [ - OvsInterfaceHandler(ovsdb_ovs) + OvsInterfaceHandler(ovsdb_ovs, printer) ] regex_handlers += [ (regex_outport, ofport_handlers),