tests: test punt policer bound to worker thread
[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 query_vpp_config(self):
65         dump = self._test.vapi.policer_dump(
66             match_name_valid=True, match_name=self.name)
67         for policer in dump:
68             if policer.name == self.name:
69                 return True
70         return False
71
72     def object_id(self):
73         return ("policer-%s" % (self.name))
74
75     def get_stats(self, worker=None):
76         conform = self._test.statistics.get_counter("/net/policer/conform")
77         exceed = self._test.statistics.get_counter("/net/policer/exceed")
78         violate = self._test.statistics.get_counter("/net/policer/violate")
79
80         counters = {"conform": conform, "exceed": exceed, "violate": violate}
81
82         total = {}
83         for name, c in counters.items():
84             total[f'{name}_packets'] = 0
85             total[f'{name}_bytes'] = 0
86             for i in range(len(c)):
87                 t = c[i]
88                 if worker is not None and i != worker + 1:
89                     continue
90                 stat_index = self._policer_index
91                 total[f'{name}_packets'] += t[stat_index]['packets']
92                 total[f'{name}_bytes'] += t[stat_index]['bytes']
93
94         return total