1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Policer utilities library."""
18 from ipaddress import ip_address
20 from resources.libraries.python.VatExecutor import VatExecutor
21 from resources.libraries.python.VatJsonUtil import VatJsonUtil
22 from resources.libraries.python.topology import Topology
25 class PolicerRateType(Enum):
26 """Policer rate types."""
30 def __init__(self, string):
34 # pylint: disable=invalid-name
35 class PolicerRoundType(Enum):
36 """Policer round types."""
41 def __init__(self, string):
45 class PolicerType(Enum):
49 P_2R3C_2698 = '2r3c-2698'
50 P_2R3C_4115 = '2r3c-4115'
51 P_2R3C_MEF5CF1 = '2r3c-mef5cf1'
53 def __init__(self, string):
57 class PolicerAction(Enum):
61 MARK_AND_TRANSMIT = 'mark-and-transmit'
63 def __init__(self, string):
68 """DSCP for mark-and-transmit action."""
88 def __init__(self, string, num):
93 class PolicerClassifyPreColor(Enum):
94 """Policer classify precolor."""
95 CONFORM_COLOR = 'conform-color'
96 EXCEED_COLOR = 'exceed-color'
98 def __init__(self, string):
102 class PolicerClassifyTableType(Enum):
103 """Policer classify table type."""
104 IP4_TABLE = 'ip4-table'
105 IP6_TABLE = 'ip6-table'
106 L2_TABLE = 'l2-table'
108 def __init__(self, string):
112 class Policer(object):
113 """Policer utilities."""
120 self._rate_type = None
121 self._round_type = None
122 self._policer_type = None
123 self._conform_action = None
124 self._conform_dscp = None
125 self._exceed_action = None
126 self._exceed_dscp = None
127 self._violate_action = None
128 self._violate_dscp = None
129 self._color_aware = False
130 self._classify_match_ip = ''
131 self._classify_match_is_src = True
132 self._classify_precolor = None
133 self._sw_if_index = 0
135 self._policer_name = ''
137 def policer_set_configuration(self):
138 """Configure policer on VPP node.
140 ...note:: First set all required parameters.
145 color_aware = 'color-aware' if self._color_aware else ''
147 conform_action = self._conform_action.value
149 if PolicerAction.MARK_AND_TRANSMIT == self._conform_action:
150 conform_action += ' {0}'.format(self._conform_dscp.string)
152 exceed_action = self._exceed_action.value
153 if PolicerAction.MARK_AND_TRANSMIT == self._exceed_action:
154 exceed_action += ' {0}'.format(self._exceed_dscp.string)
156 violate_action = self._violate_action.value
157 if PolicerAction.MARK_AND_TRANSMIT == self._violate_action:
158 violate_action += ' {0}'.format(self._violate_dscp.string)
160 out = VatExecutor.cmd_from_template(node,
161 "policer/policer_add_3c.vat",
162 name=self._policer_name,
167 rate_type=self._rate_type.value,
168 round_type=self._round_type.value,
169 p_type=self._policer_type.value,
170 conform_action=conform_action,
171 exceed_action=exceed_action,
172 violate_action=violate_action,
173 color_aware=color_aware)
175 VatJsonUtil.verify_vat_retval(
177 err_msg='Add policer {0} failed on {1}'.format(self._policer_name,
180 policer_index = out[0].get('policer_index')
182 # create classify table
183 direction = 'src' if self._classify_match_is_src else 'dst'
185 if ip_address(unicode(self._classify_match_ip)).version == 6:
187 table_type = PolicerClassifyTableType.IP6_TABLE
190 table_type = PolicerClassifyTableType.IP4_TABLE
192 out = VatExecutor.cmd_from_template(node,
193 "classify_add_table.vat",
194 ip_version=ip_version,
197 VatJsonUtil.verify_vat_retval(
199 err_msg='Add classify table failed on {0}'.format(node['host']))
201 new_table_index = out[0].get('new_table_index')
202 skip_n_vectors = out[0].get('skip_n_vectors')
203 match_n_vectors = out[0].get('match_n_vectors')
205 # create classify session
206 match = 'l3 {0} {1} {2}'.format(ip_version,
208 self._classify_match_ip)
210 out = VatExecutor.cmd_from_template(
212 "policer/policer_classify_add_session.vat",
213 policer_index=policer_index,
214 pre_color=self._classify_precolor.value,
215 table_index=new_table_index,
216 skip_n=skip_n_vectors,
217 match_n=match_n_vectors,
220 VatJsonUtil.verify_vat_retval(
222 err_msg='Add classify session failed on {0}'.format(node['host']))
224 # set classify interface
225 out = VatExecutor.cmd_from_template(
227 "policer/policer_classify_set_interface.vat",
228 sw_if_index=self._sw_if_index,
229 table_type=table_type.value,
230 table_index=new_table_index)
232 VatJsonUtil.verify_vat_retval(
234 err_msg='Set classify interface failed on {0}'.format(node['host']))
236 def policer_clear_settings(self):
237 """Clear policer settings."""
242 self._rate_type = None
243 self._round_type = None
244 self._policer_type = None
245 self._conform_action = None
246 self._conform_dscp = None
247 self._exceed_action = None
248 self._exceed_dscp = None
249 self._violate_action = None
250 self._violate_dscp = None
251 self._color_aware = False
252 self._classify_match_ip = ''
253 self._classify_match_is_src = True
254 self._classify_precolor = None
255 self._sw_if_index = 0
257 self._policer_name = ''
259 def policer_set_name(self, name):
262 :param name: Policer name.
265 self._policer_name = name
267 def policer_set_node(self, node):
268 """Set node to setup policer on.
270 :param node: VPP node.
275 def policer_set_cir(self, cir):
278 :param cir: Committed Information Rate.
283 def policer_set_eir(self, eir):
286 :param eir: Excess Information Rate.
291 def policer_set_cb(self, cb):
294 :param cb: Committed Burst size.
297 if cb == "IMIX_v4_1":
302 def policer_set_eb(self, eb):
305 :param eb: Excess Burst size.
308 if eb == "IMIX_v4_1":
313 def policer_set_rate_type_kbps(self):
314 """Set policer rate type to kbps."""
315 self._rate_type = PolicerRateType.KBPS
317 def policer_set_rate_type_pps(self):
318 """Set policer rate type to pps."""
319 self._rate_type = PolicerRateType.PPS
321 def policer_set_round_type_closest(self):
322 """Set policer round type to closest."""
323 self._round_type = PolicerRoundType.CLOSEST
325 def policer_set_round_type_up(self):
326 """Set policer round type to up."""
327 self._round_type = PolicerRoundType.UP
329 def policer_set_round_type_down(self):
330 """Set policer round type to down."""
331 self._round_type = PolicerRoundType.DOWN
333 def policer_set_type_1r2c(self):
334 """Set policer type to 1r2c."""
335 self._policer_type = PolicerType.P_1R2C
337 def policer_set_type_1r3c(self):
338 """Set policer type to 1r3c RFC2697."""
339 self._policer_type = PolicerType.P_1R3C
341 def policer_set_type_2r3c_2698(self):
342 """Set policer type to 2r3c RFC2698."""
343 self._policer_type = PolicerType.P_2R3C_2698
345 def policer_set_type_2r3c_4115(self):
346 """Set policer type to 2r3c RFC4115."""
347 self._policer_type = PolicerType.P_2R3C_4115
349 def policer_set_type_2r3c_mef5cf1(self):
350 """Set policer type to 2r3c MEF5CF1."""
351 self._policer_type = PolicerType.P_2R3C_MEF5CF1
353 def policer_set_conform_action_drop(self):
354 """Set policer conform-action to drop."""
355 self._conform_action = PolicerAction.DROP
357 def policer_set_conform_action_transmit(self):
358 """Set policer conform-action to transmit."""
359 self._conform_action = PolicerAction.TRANSMIT
361 def policer_set_conform_action_mark_and_transmit(self, dscp):
362 """Set policer conform-action to mark-and-transmit.
364 :param dscp: DSCP value to mark.
367 self._conform_action = PolicerAction.MARK_AND_TRANSMIT
368 self._conform_dscp = dscp
370 def policer_set_exceed_action_drop(self):
371 """Set policer exceed-action to drop."""
372 self._exceed_action = PolicerAction.DROP
374 def policer_set_exceed_action_transmit(self):
375 """Set policer exceed-action to transmit."""
376 self._exceed_action = PolicerAction.TRANSMIT
378 def policer_set_exceed_action_mark_and_transmit(self, dscp):
379 """Set policer exceed-action to mark-and-transmit.
381 :param dscp: DSCP value to mark.
384 self._exceed_action = PolicerAction.MARK_AND_TRANSMIT
385 self._exceed_dscp = dscp
387 def policer_set_violate_action_drop(self):
388 """Set policer violate-action to drop."""
389 self._violate_action = PolicerAction.DROP
391 def policer_set_violate_action_transmit(self):
392 """Set policer violate-action to transmit."""
393 self._violate_action = PolicerAction.TRANSMIT
395 def policer_set_violate_action_mark_and_transmit(self, dscp):
396 """Set policer violate-action to mark-and-transmit.
398 :param dscp: DSCP value to mark.
401 self._violate_action = PolicerAction.MARK_AND_TRANSMIT
402 self._violate_dscp = dscp
404 def policer_enable_color_aware(self):
405 """Enable color-aware mode for policer."""
406 self._color_aware = True
408 def policer_classify_set_precolor_conform(self):
409 """Set policer classify pre-color to conform-color."""
410 self._classify_precolor = PolicerClassifyPreColor.CONFORM_COLOR
412 def policer_classify_set_precolor_exceed(self):
413 """Set policer classify pre-color to exceeed-color."""
414 self._classify_precolor = PolicerClassifyPreColor.EXCEED_COLOR
416 def policer_classify_set_interface(self, interface):
417 """Set policer classify interface.
419 .. note:: First set node with policer_set_node.
421 :param interface: Interface name or sw_if_index.
422 :type interface: str or int
424 if isinstance(interface, basestring):
425 self._sw_if_index = Topology.get_interface_sw_index(self._node,
428 self._sw_if_index = interface
430 def policer_classify_set_match_ip(self, ip, is_src=True):
431 """Set policer classify match source IP address.
433 :param ip: IPv4 or IPv6 address.
434 :param is_src: Match src IP if True otherwise match dst IP.
438 self._classify_match_ip = ip
439 self._classify_match_is_src = is_src
445 :returns: DSCP enum CS0 object.
454 :returns: DSCP enum CS1 object.
463 :returns: DSCP enum CS2 object.
472 :returns: DSCP enum CS3 object.
481 :returns: DSCP enum CS4 object.
490 :returns: DSCP enum CS5 object.
499 :returns: DSCP enum CS6 object.
508 :returns: DSCP enum CS7 object.
517 :returns: DSCP enum EF object.
526 :returns: DSCP enum AF11 object.
535 :returns: DSCP enum AF12 object.
544 :returns: DSCP enum AF13 object.
553 :returns: DSCP enum AF21 object.
562 :returns: DSCP enum AF22 object.
571 :returns: DSCP enum AF23 object.
580 :returns: DSCP enum AF31 object.
589 :returns: DSCP enum AF32 object.
598 :returns: DSCP enum AF33 object.
604 def get_dscp_num_value(dscp):
605 """Return DSCP numeric value.
607 :param dscp: DSCP enum object.
609 :returns: DSCP numeric value.