X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FPolicer.py;h=37b1c7f7456eb79570a4c727ab4667661d0ef281;hp=f6a852e417308591ce7580445323c964d97d8b0f;hb=HEAD;hpb=d68951ac245150eeefa6e0f4156e4c1b5c9e9325 diff --git a/resources/libraries/python/Policer.py b/resources/libraries/python/Policer.py index f6a852e417..28ed0b0aa9 100644 --- a/resources/libraries/python/Policer.py +++ b/resources/libraries/python/Policer.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019 Cisco and/or its affiliates. +# Copyright (c) 2024 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -16,6 +16,7 @@ from enum import IntEnum from resources.libraries.python.Constants import Constants +from resources.libraries.python.IPUtil import IpDscp from resources.libraries.python.PapiExecutor import PapiSocketExecutor from resources.libraries.python.topology import Topology @@ -32,7 +33,7 @@ class PolicerRoundType(IntEnum): ROUND_TO_CLOSEST = 0 ROUND_TO_UP = 1 ROUND_TO_DOWN = 2 - ROUND_TO_INVALID = 3 + ROUND_INVALID = 3 class PolicerType(IntEnum): @@ -59,37 +60,19 @@ class PolicerPreColor(IntEnum): VIOLATE_COLOR = 2 -class DSCP(IntEnum): - """DSCP for mark-and-transmit action.""" - D_CS0 = 0 - D_CS1 = 8 - D_CS2 = 16 - D_CS3 = 24 - D_CS4 = 32 - D_vCS5 = 40 - D_CS6 = 48 - D_CS7 = 56 - D_AF11 = 10 - D_AF12 = 12 - D_AF13 = 14 - D_AF21 = 18 - D_AF22 = 20 - D_AF23 = 22 - D_AF31 = 26 - D_AF32 = 28 - D_AF33 = 30 - D_EF = 46 - - class Policer: """Policer utilities.""" - # pylint: disable=too-many-arguments, too-many-locals + # TODO: Pylint says too-many-arguments and too-many-locals. + # It is right, we should refactor the code + # and group similar arguments together (into documented classes). + # Note that even the call from Robot Framework + # is not very readable with this many arguments. @staticmethod def policer_set_configuration( node, policer_name, cir, eir, cbs, ebs, rate_type, round_type, policer_type, conform_action_type, exceed_action_type, - violate_action_type, color_aware, is_add=True, conform_dscp=None, + violate_action_type, color_aware, conform_dscp=None, exceed_dscp=None, violate_dscp=None): """Configure policer on VPP node. @@ -106,7 +89,6 @@ class Policer: :param exceed_action_type: Exceed action type. :param violate_action_type: Violate action type. :param color_aware: Color-blind (cb) or color-aware (ca). - :param is_add: Add policer if True, else delete. :param conform_dscp: DSCP for conform mark_and_transmit action. :param exceed_dscp: DSCP for exceed mark_and_transmit action. :param violate_dscp: DSCP for vilate mark_and_transmit action. @@ -123,15 +105,31 @@ class Policer: :type exceed_action_type: str :type violate_action_type: str :type color_aware: str - :type is_add: bool :type conform_dscp: str :type exceed_dscp: str :type violate_dscp: str """ - cmd = u"policer_add_del" - args = dict( - is_add=int(is_add), - name=str(policer_name).encode(encoding=u"utf-8"), + conform_action = dict( + type=getattr(PolicerAction, conform_action_type.upper()).value, + dscp=Policer.get_dscp_num_value(conform_dscp) if + conform_action_type.upper() == PolicerAction.MARK_AND_TRANSMIT.name + else 0 + ) + exceed_action = dict( + type=getattr(PolicerAction, exceed_action_type.upper()).value, + dscp=Policer.get_dscp_num_value(exceed_dscp) if + exceed_action_type.upper() == PolicerAction.MARK_AND_TRANSMIT.name + else 0 + ) + violate_action = dict( + type=getattr(PolicerAction, violate_action_type.upper()).value, + dscp=Policer.get_dscp_num_value(violate_dscp) if + violate_action_type.upper() == PolicerAction.MARK_AND_TRANSMIT.name + else 0 + ) + + cmd = u"policer_add" + infos = dict( cir=int(cir), eir=int(eir), cb=int(cbs), @@ -141,28 +139,14 @@ class Policer: PolicerRoundType, f"ROUND_TO_{round_type.upper()}" ).value, type=getattr(PolicerType, f"TYPE_{policer_type.upper()}").value, - conform_action_type=getattr( - PolicerAction, conform_action_type.upper() - ).value, - conform_dscp=getattr(DSCP, f"D_{conform_dscp.upper()}").value - if - conform_action_type.upper() == PolicerAction.MARK_AND_TRANSMIT.name - else 0, - exceed_action_type=getattr( - PolicerAction, exceed_action_type.upper() - ).value, - exceed_dscp=getattr(DSCP, f"D_{exceed_dscp.upper()}").value - if - exceed_action_type.upper() == PolicerAction.MARK_AND_TRANSMIT.name - else 0, - violate_action_type=getattr( - PolicerAction, violate_action_type.upper() - ).value, - violate_dscp=getattr(DSCP, f"D_{violate_dscp.upper()}").value - if - violate_action_type.upper() == PolicerAction.MARK_AND_TRANSMIT.name - else 0, - color_aware=1 if color_aware == u"'ca'" else 0 + conform_action=conform_action, + exceed_action=exceed_action, + violate_action=violate_action, + color_aware=bool(color_aware == u"'ca'") + ) + args = dict( + name=str(policer_name), + infos=infos, ) err_msg = f"Failed to configure policer {policer_name} " \ f"on host {node['host']}" @@ -176,7 +160,7 @@ class Policer: def policer_classify_set_interface( node, interface, ip4_table_index=Constants.BITWISE_NON_ZERO, ip6_table_index=Constants.BITWISE_NON_ZERO, - l2_table_index=Constants.BITWISE_NON_ZERO, is_add=1): + l2_table_index=Constants.BITWISE_NON_ZERO, is_add=True): """Set/unset policer classify interface. :param node: VPP node. @@ -188,12 +172,13 @@ class Policer: (Default value = ~0) :param l2_table_index: L2 classify table index (~0 to skip). (Default value = ~0) - :param is_add: Set if non-zero, else unset. + :param is_add: Set if True, else unset. :type node: dict :type interface: str or int :type ip4_table_index: int :type ip6_table_index: int :type l2_table_index: int + :type is_add: bool """ if isinstance(interface, str): sw_if_index = Topology.get_interface_sw_index(node, interface) @@ -202,8 +187,8 @@ class Policer: cmd = u"policer_classify_set_interface" args = dict( - is_add=int(is_add), - sw_if_index=sw_if_index, + is_add=is_add, + sw_if_index=int(sw_if_index), ip4_table_index=int(ip4_table_index), ip6_table_index=int(ip6_table_index), l2_table_index=int(l2_table_index) @@ -234,4 +219,4 @@ class Policer: :returns: DSCP numeric value. :rtype: int """ - return getattr(DSCP, f"D_{dscp.upper()}").value + return getattr(IpDscp, f"IP_API_DSCP_{dscp.upper()}").value