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