crypto crypto-openssl: support hashing operations
[vpp.git] / test / vpp_policer.py
1 from vpp_object import VppObject
2 from vpp_ip import INVALID_INDEX
3
4
5 class PolicerAction():
6     """ sse2 qos action """
7
8     def __init__(self, type, dscp):
9         self.type = type
10         self.dscp = dscp
11
12     def encode(self):
13         return {'type': self.type, 'dscp': self.dscp}
14
15
16 class VppPolicer(VppObject):
17     """ Policer """
18
19     def __init__(self, test, name, cir, eir, commited_burst, excess_burst,
20                  rate_type=0, round_type=0, type=0, color_aware=False,
21                  conform_action=PolicerAction(1, 0),
22                  exceed_action=PolicerAction(0, 0),
23                  violate_action=PolicerAction(0, 0)):
24         self._test = test
25         self.name = name
26         self.cir = cir
27         self.eir = eir
28         self.commited_burst = commited_burst
29         self.excess_burst = excess_burst
30         self.rate_type = rate_type
31         self.round_type = round_type
32         self.type = type
33         self.color_aware = color_aware
34         self.conform_action = conform_action
35         self.exceed_action = exceed_action
36         self.violate_action = violate_action
37         self._policer_index = INVALID_INDEX
38
39     @property
40     def policer_index(self):
41         return self._policer_index
42
43     def add_vpp_config(self):
44         r = self._test.vapi.policer_add_del(
45             name=self.name, cir=self.cir,
46             eir=self.eir, cb=self.commited_burst, eb=self.excess_burst,
47             rate_type=self.rate_type, round_type=self.round_type,
48             type=self.type, color_aware=self.color_aware,
49             conform_action=self.conform_action.encode(),
50             exceed_action=self.exceed_action.encode(),
51             violate_action=self.violate_action.encode())
52         self._test.registry.register(self, self._test.logger)
53         self._policer_index = r.policer_index
54         return self
55
56     def remove_vpp_config(self):
57         self._test.vapi.policer_add_del(is_add=False, name=self.name)
58         self._policer_index = INVALID_INDEX
59
60     def bind_vpp_config(self, worker, bind):
61         self._test.vapi.policer_bind(name=self.name, worker_index=worker,
62                                      bind_enable=bind)
63
64     def apply_vpp_config(self, if_index, apply):
65         self._test.vapi.policer_input(name=self.name, sw_if_index=if_index,
66                                       apply=apply)
67
68     def query_vpp_config(self):
69         dump = self._test.vapi.policer_dump(
70             match_name_valid=True, match_name=self.name)
71         for policer in dump:
72             if policer.name == self.name:
73                 return True
74         return False
75
76     def object_id(self):
77         return ("policer-%s" % (self.name))
78
79     def get_stats(self, worker=None):
80         conform = self._test.statistics.get_counter("/net/policer/conform")
81         exceed = self._test.statistics.get_counter("/net/policer/exceed")
82         violate = self._test.statistics.get_counter("/net/policer/violate")
83
84         counters = {"conform": conform, "exceed": exceed, "violate": violate}
85
86         total = {}
87         for name, c in counters.items():
88             total[f'{name}_packets'] = 0
89             total[f'{name}_bytes'] = 0
90             for i in range(len(c)):
91                 t = c[i]
92                 if worker is not None and i != worker + 1:
93                     continue
94                 stat_index = self._policer_index
95                 total[f'{name}_packets'] += t[stat_index]['packets']
96                 total[f'{name}_bytes'] += t[stat_index]['bytes']
97
98         return total