aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python/Policer.py
blob: a51f99609ceba57482473abdfa8bf38d1d8a9673 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# Copyright (c) 2019 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:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Policer utilities library."""

from enum import IntEnum

from resources.libraries.python.Constants import Constants
from resources.libraries.python.PapiExecutor import PapiSocketExecutor
from resources.libraries.python.topology import Topology


class PolicerRateType(IntEnum):
    """Policer rate types."""
    KBPS = 0
    PPS = 1
    INVALID = 2


class PolicerRoundType(IntEnum):
    """Policer round types."""
    ROUND_TO_CLOSEST = 0
    ROUND_TO_UP = 1
    ROUND_TO_DOWN = 2
    ROUND_TO_INVALID = 3


class PolicerType(IntEnum):
    """Policer type."""
    TYPE_1R2C = 0
    TYPE_1R3C_RFC_2697 = 1
    TYPE_2R3C_RFC_2698 = 2
    TYPE_2R3C_RFC_4115 = 3
    TYPE_2R3C_RFC_MEF5CF1 = 4
    TYPE_MAX = 5


class PolicerAction(IntEnum):
    """Policer action."""
    DROP = 0
    TRANSMIT = 1
    MARK_AND_TRANSMIT = 2


class PolicerPreColor(IntEnum):
    """Policer Pre-color."""
    CONFORM_COLOR = 0
    EXCEED_COLOR = 1
    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(object):
    """Policer utilities."""

    # pylint: disable=too-many-arguments, too-many-locals
    @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,
            exceed_dscp=None, violate_dscp=None):
        """Configure policer on VPP node.

        :param node: VPP node.
        :param policer_name: Name of the policer.
        :param cir: Committed information rate.
        :param eir: Excess (or Peak) information rate.
        :param cbs: Committed burst size.
        :param ebs: Excess (or Peak) burst size.
        :param rate_type: Rate type.
        :param round_type: Round type.
        :param policer_type: Policer algorithm.
        :param conform_action_type: Conform action type.
        :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.
        :type node: dict
        :type policer_name: str
        :type cir: int
        :type eir: int
        :type cbs: int
        :type ebs: int
        :type rate_type: str
        :type round_type: str
        :type policer_type: str
        :type conform_action_type: str
        :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 = 'policer_add_del'
        args = dict(
            is_add=int(is_add),
            name=str(policer_name),
            cir=int(cir),
            eir=int(eir),
            cb=int(cbs),
            eb=int(ebs),
            rate_type=getattr(PolicerRateType, rate_type.upper()).value,
            round_type=getattr(
                PolicerRoundType, 'ROUND_TO_{rt}'.format(
                    rt=round_type.upper())).value,
            type=getattr(PolicerType, 'TYPE_{pt}'.format(
                pt=policer_type.upper())).value,
            conform_action_type=getattr(
                PolicerAction, conform_action_type.upper()).value,
            conform_dscp=getattr(DSCP, 'D_{dscp}'.format(
                dscp=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, 'D_{dscp}'.format(
                dscp=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, 'D_{dscp}'.format(
                dscp=violate_dscp.upper())).value
            if
            violate_action_type.upper() == PolicerAction.MARK_AND_TRANSMIT.name
            else 0,
            color_aware=1 if color_aware == "'ca'" else 0
        )
        err_msg = 'Failed to configure policer {pn} on host {host}'.format(
            pn=policer_name, host=node['host'])

        with PapiSocketExecutor(node) as papi_exec:
            reply = papi_exec.add(cmd, **args).get_reply(err_msg)

        return reply['policer_index']

    @staticmethod
    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):
        """Set/unset policer classify interface.

        :param node: VPP node.
        :param interface: Interface name or sw_if_index to set/unset policer
            classify.
        :param ip4_table_index: IP4 classify table index (~0 to skip).
            (Default value = ~0)
        :param ip6_table_index: IP6 classify table index (~0 to skip).
            (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.
        :type node: dict
        :type interface: str or int
        :type ip4_table_index: int
        :type ip6_table_index: int
        :type l2_table_index: int
        """
        if isinstance(interface, basestring):
            sw_if_index = Topology.get_interface_sw_index(node, interface)
        else:
            sw_if_index = interface

        cmd = 'policer_classify_set_interface'

        args = dict(
            is_add=int(is_add),
            sw_if_index=sw_if_index,
            ip4_table_index=int(ip4_table_index),
            ip6_table_index=int(ip6_table_index),
            l2_table_index=int(l2_table_index)
        )
        err_msg = 'Failed to set/unset policer classify interface {ifc} ' \
                  'on host {host}'.format(ifc=interface, host=node['host'])

        with PapiSocketExecutor(node) as papi_exec:
            papi_exec.add(cmd, **args).get_reply(err_msg)

    @staticmethod
    def policer_classify_get_precolor(precolor):
        """Return policer pre-color numeric value.

        :param precolor: Policer pre-color name.
        :type precolor: str
        :returns: Policer pre-color numeric value.
        :rtype: int
        """
        return getattr(PolicerPreColor, precolor.upper()).value

    @staticmethod
    def get_dscp_num_value(dscp):
        """Return DSCP numeric value.

        :param dscp: DSCP name.
        :type dscp: str
        :returns: DSCP numeric value.
        :rtype: int
        """
        return getattr(DSCP, 'D_{dscp}'.format(dscp=dscp.upper())).value