Fix warnings reported by gen_doc.sh
[csit.git] / resources / libraries / python / Policer.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Policer utilities library."""
15
16 from enum import Enum
17
18 from ipaddress import ip_address
19
20 from resources.libraries.python.VatExecutor import VatExecutor
21 from resources.libraries.python.VatJsonUtil import VatJsonUtil
22 from resources.libraries.python.topology import Topology
23
24
25 class PolicerRateType(Enum):
26     """Policer rate types."""
27     KBPS = 'kbps'
28     PPS = 'pps'
29
30     def __init__(self, string):
31         self.string = string
32
33
34 # pylint: disable=invalid-name
35 class PolicerRoundType(Enum):
36     """Policer round types."""
37     CLOSEST = 'closest'
38     UP = 'up'
39     DOWN = 'down'
40
41     def __init__(self, string):
42         self.string = string
43
44
45 class PolicerType(Enum):
46     """Policer type."""
47     P_1R2C = '1r2c'
48     P_1R3C = '1r3c'
49     P_2R3C_2698 = '2r3c-2698'
50     P_2R3C_4115 = '2r3c-4115'
51     P_2R3C_MEF5CF1 = '2r3c-mef5cf1'
52
53     def __init__(self, string):
54         self.string = string
55
56
57 class PolicerAction(Enum):
58     """Policer action."""
59     DROP = 'drop'
60     TRANSMIT = 'transmit'
61     MARK_AND_TRANSMIT = 'mark-and-transmit'
62
63     def __init__(self, string):
64         self.string = string
65
66
67 class DSCP(Enum):
68     """DSCP for mark-and-transmit action."""
69     CS0 = ('CS0', 0)
70     CS1 = ('CS1', 8)
71     CS2 = ('CS2', 16)
72     CS3 = ('CS3', 24)
73     CS4 = ('CS4', 32)
74     CS5 = ('CS5', 40)
75     CS6 = ('CS6', 48)
76     CS7 = ('CS7', 56)
77     AF11 = ('AF11', 10)
78     AF12 = ('AF12', 12)
79     AF13 = ('AF13', 14)
80     AF21 = ('AF21', 18)
81     AF22 = ('AF22', 20)
82     AF23 = ('AF23', 22)
83     AF31 = ('AF31', 26)
84     AF32 = ('AF32', 28)
85     AF33 = ('AF33', 30)
86     EF = ('EF', 46)
87
88     def __init__(self, string, num):
89         self.string = string
90         self.num = num
91
92
93 class PolicerClassifyPreColor(Enum):
94     """Policer classify precolor."""
95     CONFORM_COLOR = 'conform-color'
96     EXCEED_COLOR = 'exceed-color'
97
98     def __init__(self, string):
99         self.string = string
100
101
102 class PolicerClassifyTableType(Enum):
103     """Policer classify table type."""
104     IP4_TABLE = 'ip4-table'
105     IP6_TABLE = 'ip6-table'
106     L2_TABLE = 'l2-table'
107
108     def __init__(self, string):
109         self.string = string
110
111
112 # pylint: disable=too-many-instance-attributes
113 class Policer(object):
114     """Policer utilities."""
115
116     def __init__(self):
117         self._cir = 0
118         self._eir = 0
119         self._cb = 0
120         self._eb = 0
121         self._rate_type = None
122         self._round_type = None
123         self._policer_type = None
124         self._conform_action = None
125         self._conform_dscp = None
126         self._exceed_action = None
127         self._exceed_dscp = None
128         self._violate_action = None
129         self._violate_dscp = None
130         self._color_aware = False
131         self._classify_match_ip = ''
132         self._classify_match_is_src = True
133         self._classify_precolor = None
134         self._sw_if_index = 0
135         self._node = None
136         self._policer_name = ''
137
138     def policer_set_configuration(self):
139         """Configure policer on VPP node.
140
141         ...note:: First set all required parameters.
142         """
143         node = self._node
144
145         # create policer
146         color_aware = 'color-aware' if self._color_aware else ''
147
148         # pylint: disable=no-member
149         conform_action = self._conform_action.value
150
151         if PolicerAction.MARK_AND_TRANSMIT == self._conform_action:
152             conform_action += ' {0}'.format(self._conform_dscp.string)
153
154         exceed_action = self._exceed_action.value
155         if PolicerAction.MARK_AND_TRANSMIT == self._exceed_action:
156             exceed_action += ' {0}'.format(self._exceed_dscp.string)
157
158         violate_action = self._violate_action.value
159         if PolicerAction.MARK_AND_TRANSMIT == self._violate_action:
160             violate_action += ' {0}'.format(self._violate_dscp.string)
161
162         out = VatExecutor.cmd_from_template(node,
163                                             "policer/policer_add_3c.vat",
164                                             name=self._policer_name,
165                                             cir=self._cir,
166                                             eir=self._eir,
167                                             cb=self._cb,
168                                             eb=self._eb,
169                                             rate_type=self._rate_type.value,
170                                             round_type=self._round_type.value,
171                                             p_type=self._policer_type.value,
172                                             conform_action=conform_action,
173                                             exceed_action=exceed_action,
174                                             violate_action=violate_action,
175                                             color_aware=color_aware)
176
177         VatJsonUtil.verify_vat_retval(
178             out[0],
179             err_msg='Add policer {0} failed on {1}'.format(self._policer_name,
180                                                            node['host']))
181
182         policer_index = out[0].get('policer_index')
183
184         # create classify table
185         direction = 'src' if self._classify_match_is_src else 'dst'
186
187         if ip_address(unicode(self._classify_match_ip)).version == 6:
188             ip_version = 'ip6'
189             table_type = PolicerClassifyTableType.IP6_TABLE
190         else:
191             ip_version = 'ip4'
192             table_type = PolicerClassifyTableType.IP4_TABLE
193
194         out = VatExecutor.cmd_from_template(node,
195                                             "classify_add_table.vat",
196                                             ip_version=ip_version,
197                                             direction=direction)
198
199         VatJsonUtil.verify_vat_retval(
200             out[0],
201             err_msg='Add classify table failed on {0}'.format(node['host']))
202
203         new_table_index = out[0].get('new_table_index')
204         skip_n_vectors = out[0].get('skip_n_vectors')
205         match_n_vectors = out[0].get('match_n_vectors')
206
207         # create classify session
208         match = 'l3 {0} {1} {2}'.format(ip_version,
209                                         direction,
210                                         self._classify_match_ip)
211
212         out = VatExecutor.cmd_from_template(
213             node,
214             "policer/policer_classify_add_session.vat",
215             policer_index=policer_index,
216             pre_color=self._classify_precolor.value, # pylint: disable=no-member
217             table_index=new_table_index,
218             skip_n=skip_n_vectors,
219             match_n=match_n_vectors,
220             match=match)
221
222         VatJsonUtil.verify_vat_retval(
223             out[0],
224             err_msg='Add classify session failed on {0}'.format(node['host']))
225
226         # set classify interface
227         out = VatExecutor.cmd_from_template(
228             node,
229             "policer/policer_classify_set_interface.vat",
230             sw_if_index=self._sw_if_index,
231             table_type=table_type.value, # pylint: disable=no-member
232             table_index=new_table_index)
233
234         VatJsonUtil.verify_vat_retval(
235             out[0],
236             err_msg='Set classify interface failed on {0}'.format(node['host']))
237
238     def policer_clear_settings(self):
239         """Clear policer settings."""
240         self._cir = 0
241         self._eir = 0
242         self._cb = 0
243         self._eb = 0
244         self._rate_type = None
245         self._round_type = None
246         self._policer_type = None
247         self._conform_action = None
248         self._conform_dscp = None
249         self._exceed_action = None
250         self._exceed_dscp = None
251         self._violate_action = None
252         self._violate_dscp = None
253         self._color_aware = False
254         self._classify_match_ip = ''
255         self._classify_match_is_src = True
256         self._classify_precolor = None
257         self._sw_if_index = 0
258         self._node = None
259         self._policer_name = ''
260
261     def policer_set_name(self, name):
262         """Set policer name.
263
264         :param name: Policer name.
265         :type name: str
266         """
267         self._policer_name = name
268
269     def policer_set_node(self, node):
270         """Set node to setup policer on.
271
272         :param node: VPP node.
273         :type node: dict
274         """
275         self._node = node
276
277     def policer_set_cir(self, cir):
278         """Set policer CIR.
279
280         :param cir: Committed Information Rate.
281         :type cir: int
282         """
283         self._cir = cir
284
285     def policer_set_eir(self, eir):
286         """Set polcier EIR.
287
288         :param eir: Excess Information Rate.
289         :type eir: int
290         """
291         self._eir = eir
292
293     def policer_set_cb(self, cb):
294         """Set policer CB.
295
296         :param cb: Committed Burst size.
297         :type cb: int
298         """
299         self._cb = cb
300
301     def policer_set_eb(self, eb):
302         """Set policer EB.
303
304         :param eb: Excess Burst size.
305         :type eb: int
306         """
307         self._eb = eb
308
309     def policer_set_rate_type_kbps(self):
310         """Set policer rate type to kbps."""
311         self._rate_type = PolicerRateType.KBPS
312
313     def policer_set_rate_type_pps(self):
314         """Set policer rate type to pps."""
315         self._rate_type = PolicerRateType.PPS
316
317     def policer_set_round_type_closest(self):
318         """Set policer round type to closest."""
319         self._round_type = PolicerRoundType.CLOSEST
320
321     def policer_set_round_type_up(self):
322         """Set policer round type to up."""
323         self._round_type = PolicerRoundType.UP
324
325     def policer_set_round_type_down(self):
326         """Set policer round type to down."""
327         self._round_type = PolicerRoundType.DOWN
328
329     def policer_set_type_1r2c(self):
330         """Set policer type to 1r2c."""
331         self._policer_type = PolicerType.P_1R2C
332
333     def policer_set_type_1r3c(self):
334         """Set policer type to 1r3c RFC2697."""
335         self._policer_type = PolicerType.P_1R3C
336
337     def policer_set_type_2r3c_2698(self):
338         """Set policer type to 2r3c RFC2698."""
339         self._policer_type = PolicerType.P_2R3C_2698
340
341     def policer_set_type_2r3c_4115(self):
342         """Set policer type to 2r3c RFC4115."""
343         self._policer_type = PolicerType.P_2R3C_4115
344
345     def policer_set_type_2r3c_mef5cf1(self):
346         """Set policer type to 2r3c MEF5CF1."""
347         self._policer_type = PolicerType.P_2R3C_MEF5CF1
348
349     def policer_set_conform_action_drop(self):
350         """Set policer conform-action to drop."""
351         self._conform_action = PolicerAction.DROP
352
353     def policer_set_conform_action_transmit(self):
354         """Set policer conform-action to transmit."""
355         self._conform_action = PolicerAction.TRANSMIT
356
357     def policer_set_conform_action_mark_and_transmit(self, dscp):
358         """Set policer conform-action to mark-and-transmit.
359
360         :param dscp: DSCP value to mark.
361         :type dscp: DSCP
362         """
363         self._conform_action = PolicerAction.MARK_AND_TRANSMIT
364         self._conform_dscp = dscp
365
366     def policer_set_exceed_action_drop(self):
367         """Set policer exceed-action to drop."""
368         self._exceed_action = PolicerAction.DROP
369
370     def policer_set_exceed_action_transmit(self):
371         """Set policer exceed-action to transmit."""
372         self._exceed_action = PolicerAction.TRANSMIT
373
374     def policer_set_exceed_action_mark_and_transmit(self, dscp):
375         """Set policer exceed-action to mark-and-transmit.
376
377         :param dscp: DSCP value to mark.
378         :type dscp: DSCP
379         """
380         self._exceed_action = PolicerAction.MARK_AND_TRANSMIT
381         self._exceed_dscp = dscp
382
383     def policer_set_violate_action_drop(self):
384         """Set policer violate-action to drop."""
385         self._violate_action = PolicerAction.DROP
386
387     def policer_set_violate_action_transmit(self):
388         """Set policer violate-action to transmit."""
389         self._violate_action = PolicerAction.TRANSMIT
390
391     def policer_set_violate_action_mark_and_transmit(self, dscp):
392         """Set policer violate-action to mark-and-transmit.
393
394         :param dscp: DSCP value to mark.
395         :type dscp: DSCP
396         """
397         self._violate_action = PolicerAction.MARK_AND_TRANSMIT
398         self._violate_dscp = dscp
399
400     def policer_enable_color_aware(self):
401         """Enable color-aware mode for policer."""
402         self._color_aware = True
403
404     def policer_classify_set_precolor_conform(self):
405         """Set policer classify pre-color to conform-color."""
406         self._classify_precolor = PolicerClassifyPreColor.CONFORM_COLOR
407
408     def policer_classify_set_precolor_exceed(self):
409         """Set policer classify pre-color to exceeed-color."""
410         self._classify_precolor = PolicerClassifyPreColor.EXCEED_COLOR
411
412     def policer_classify_set_interface(self, interface):
413         """Set policer classify interface.
414
415         .. note:: First set node with policer_set_node.
416
417         :param interface: Interface name or sw_if_index.
418         :type interface: str or int
419         """
420         if isinstance(interface, basestring):
421             self._sw_if_index = Topology.get_interface_sw_index(self._node,
422                                                                 interface)
423         else:
424             self._sw_if_index = interface
425
426     def policer_classify_set_match_ip(self, ip, is_src=True):
427         """Set policer classify match source IP address.
428
429         :param ip: IPv4 or IPv6 address.
430         :param is_src: Match src IP if True otherwise match dst IP.
431         :type ip: str
432         :type is_src: bool
433         """
434         self._classify_match_ip = ip
435         self._classify_match_is_src = is_src
436
437     @staticmethod
438     def dscp_cs0():
439         """Return DSCP CS0.
440
441         :returns: DSCP enum CS0 object.
442         :rtype: DSCP
443         """
444         return DSCP.CS0
445
446     @staticmethod
447     def dscp_cs1():
448         """Return DSCP CS1.
449
450         :returns: DSCP enum CS1 object.
451         :rtype: DSCP
452         """
453         return DSCP.CS1
454
455     @staticmethod
456     def dscp_cs2():
457         """Return DSCP CS2.
458
459         :returns: DSCP enum CS2 object.
460         :rtype: DSCP
461         """
462         return DSCP.CS2
463
464     @staticmethod
465     def dscp_cs3():
466         """Return DSCP CS3.
467
468         :returns: DSCP enum CS3 object.
469         :rtype: DSCP
470         """
471         return DSCP.CS3
472
473     @staticmethod
474     def dscp_cs4():
475         """Return DSCP CS4.
476
477         :returns: DSCP enum CS4 object.
478         :rtype: DSCP
479         """
480         return DSCP.CS4
481
482     @staticmethod
483     def dscp_cs5():
484         """Return DSCP CS5.
485
486         :returns: DSCP enum CS5 object.
487         :rtype: DSCP
488         """
489         return DSCP.CS5
490
491     @staticmethod
492     def dscp_cs6():
493         """Return DSCP CS6.
494
495         :returns: DSCP enum CS6 object.
496         :rtype: DSCP
497         """
498         return DSCP.CS6
499
500     @staticmethod
501     def dscp_cs7():
502         """Return DSCP CS7.
503
504         :returns: DSCP enum CS7 object.
505         :rtype: DSCP
506         """
507         return DSCP.CS7
508
509     @staticmethod
510     def dscp_ef():
511         """Return DSCP EF.
512
513         :returns: DSCP enum EF object.
514         :rtype: DSCP
515         """
516         return DSCP.EF
517
518     @staticmethod
519     def dscp_af11():
520         """Return DSCP AF11.
521
522         :returns: DSCP enum AF11 object.
523         :rtype: DSCP
524         """
525         return DSCP.AF11
526
527     @staticmethod
528     def dscp_af12():
529         """Return DSCP AF12.
530
531         :returns: DSCP enum AF12 object.
532         :rtype: DSCP
533         """
534         return DSCP.AF12
535
536     @staticmethod
537     def dscp_af13():
538         """Return DSCP AF13.
539
540         :returns: DSCP enum AF13 object.
541         :rtype: DSCP
542         """
543         return DSCP.AF13
544
545     @staticmethod
546     def dscp_af21():
547         """Return DSCP AF21.
548
549         :returns: DSCP enum AF21 object.
550         :rtype: DSCP
551         """
552         return DSCP.AF21
553
554     @staticmethod
555     def dscp_af22():
556         """Return DSCP AF22.
557
558         :returns: DSCP enum AF22 object.
559         :rtype: DSCP
560         """
561         return DSCP.AF22
562
563     @staticmethod
564     def dscp_af23():
565         """Return DSCP AF23.
566
567         :returns: DSCP enum AF23 object.
568         :rtype: DSCP
569         """
570         return DSCP.AF23
571
572     @staticmethod
573     def dscp_af31():
574         """Return DSCP AF31.
575
576         :returns: DSCP enum AF31 object.
577         :rtype: DSCP
578         """
579         return DSCP.AF31
580
581     @staticmethod
582     def dscp_af32():
583         """Return DSCP AF32.
584
585         :returns: DSCP enum AF32 object.
586         :rtype: DSCP
587         """
588         return DSCP.AF32
589
590     @staticmethod
591     def dscp_af33():
592         """Return DSCP AF33.
593
594         :returns: DSCP enum AF33 object.
595         :rtype: DSCP
596         """
597         return DSCP.AF33
598
599     @staticmethod
600     def get_dscp_num_value(dscp):
601         """Return DSCP numeric value.
602
603         :param dscp: DSCP enum object.
604         :type dscp: DSCP
605         :returns: DSCP numeric value.
606         :rtype: int
607         """
608         return dscp.num

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.