blob: 0f3b073d6e349614b98e85c1b5206f4c5be0f9b3 [file] [log] [blame]
Jakub Grajciarcd01fb42020-03-02 13:16:53 +01001from vpp_object import VppObject
2from vpp_ip import INVALID_INDEX
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +02003from enum import Enum
4
5
6class Dir(Enum):
7 RX = 0
8 TX = 1
Jakub Grajciarcd01fb42020-03-02 13:16:53 +01009
10
11class PolicerAction():
12 """ sse2 qos action """
13
14 def __init__(self, type, dscp):
15 self.type = type
16 self.dscp = dscp
17
18 def encode(self):
19 return {'type': self.type, 'dscp': self.dscp}
20
21
22class VppPolicer(VppObject):
23 """ Policer """
24
25 def __init__(self, test, name, cir, eir, commited_burst, excess_burst,
26 rate_type=0, round_type=0, type=0, color_aware=False,
27 conform_action=PolicerAction(1, 0),
28 exceed_action=PolicerAction(0, 0),
29 violate_action=PolicerAction(0, 0)):
30 self._test = test
31 self.name = name
32 self.cir = cir
33 self.eir = eir
34 self.commited_burst = commited_burst
35 self.excess_burst = excess_burst
36 self.rate_type = rate_type
37 self.round_type = round_type
38 self.type = type
39 self.color_aware = color_aware
40 self.conform_action = conform_action
41 self.exceed_action = exceed_action
42 self.violate_action = violate_action
43 self._policer_index = INVALID_INDEX
44
45 @property
46 def policer_index(self):
47 return self._policer_index
48
49 def add_vpp_config(self):
50 r = self._test.vapi.policer_add_del(
51 name=self.name, cir=self.cir,
52 eir=self.eir, cb=self.commited_burst, eb=self.excess_burst,
53 rate_type=self.rate_type, round_type=self.round_type,
54 type=self.type, color_aware=self.color_aware,
55 conform_action=self.conform_action.encode(),
56 exceed_action=self.exceed_action.encode(),
57 violate_action=self.violate_action.encode())
58 self._test.registry.register(self, self._test.logger)
59 self._policer_index = r.policer_index
60 return self
61
62 def remove_vpp_config(self):
63 self._test.vapi.policer_add_del(is_add=False, name=self.name)
64 self._policer_index = INVALID_INDEX
65
Brian Russellbb983142021-02-10 13:56:06 +000066 def bind_vpp_config(self, worker, bind):
67 self._test.vapi.policer_bind(name=self.name, worker_index=worker,
68 bind_enable=bind)
69
Stanislav Zaikine5a3ae02022-04-05 19:23:12 +020070 def apply_vpp_config(self, if_index, dir: Dir, apply):
71 if dir == Dir.RX:
72 self._test.vapi.policer_input(
73 name=self.name, sw_if_index=if_index, apply=apply)
74 else:
75 self._test.vapi.policer_output(
76 name=self.name, sw_if_index=if_index, apply=apply)
Brian Russell6e6920d2021-02-17 15:54:52 +000077
Jakub Grajciarcd01fb42020-03-02 13:16:53 +010078 def query_vpp_config(self):
79 dump = self._test.vapi.policer_dump(
80 match_name_valid=True, match_name=self.name)
81 for policer in dump:
82 if policer.name == self.name:
83 return True
84 return False
85
86 def object_id(self):
87 return ("policer-%s" % (self.name))
Brian Russelle9887262021-01-27 14:45:22 +000088
89 def get_stats(self, worker=None):
90 conform = self._test.statistics.get_counter("/net/policer/conform")
91 exceed = self._test.statistics.get_counter("/net/policer/exceed")
92 violate = self._test.statistics.get_counter("/net/policer/violate")
93
94 counters = {"conform": conform, "exceed": exceed, "violate": violate}
95
96 total = {}
97 for name, c in counters.items():
98 total[f'{name}_packets'] = 0
99 total[f'{name}_bytes'] = 0
100 for i in range(len(c)):
101 t = c[i]
102 if worker is not None and i != worker + 1:
103 continue
104 stat_index = self._policer_index
105 total[f'{name}_packets'] += t[stat_index]['packets']
106 total[f'{name}_bytes'] += t[stat_index]['bytes']
107
108 return total