blob: 2c47eed75fd770c8f60063c6770bf60eecef71ce [file] [log] [blame]
Jakub Grajciarcd01fb42020-03-02 13:16:53 +01001from vpp_object import VppObject
2from vpp_ip import INVALID_INDEX
3
4
5class 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
16class 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
Brian Russellbb983142021-02-10 13:56:06 +000060 def bind_vpp_config(self, worker, bind):
61 self._test.vapi.policer_bind(name=self.name, worker_index=worker,
62 bind_enable=bind)
63
Brian Russell6e6920d2021-02-17 15:54:52 +000064 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
Jakub Grajciarcd01fb42020-03-02 13:16:53 +010068 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))
Brian Russelle9887262021-01-27 14:45:22 +000078
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