tests: refactor vpp_object.py
[vpp.git] / test / vpp_l2.py
1 """
2   L2/BD Types
3
4 """
5
6 from vpp_object import VppObject
7 from vpp_ip import VppIpAddress
8 from vpp_lo_interface import VppLoInterface
9 from vpp_papi import MACAddress
10
11
12 class L2_PORT_TYPE:
13     NORMAL = 0
14     BVI = 1
15     UU_FWD = 2
16
17
18 class BRIDGE_FLAGS:
19     NONE = 0
20     LEARN = 1
21     FWD = 2
22     FLOOD = 4
23     UU_FLOOD = 8
24     ARP_TERM = 16
25
26
27 def find_bridge_domain(test, bd_id):
28     bds = test.vapi.bridge_domain_dump(bd_id)
29     return len(bds) == 1
30
31
32 def find_bridge_domain_port(test, bd_id, sw_if_index):
33     bds = test.vapi.bridge_domain_dump(bd_id)
34     for bd in bds:
35         for p in bd.sw_if_details:
36             if p.sw_if_index == sw_if_index:
37                 return True
38     return False
39
40
41 def find_bridge_domain_arp_entry(test, bd_id, mac, ip):
42     vmac = MACAddress(mac)
43     vip = VppIpAddress(ip)
44
45     if vip.version == 4:
46         n = 4
47     else:
48         n = 16
49
50     arps = test.vapi.bd_ip_mac_dump(bd_id)
51     for arp in arps:
52         # do IP addr comparison too once .api is fixed...
53         if vmac.packed == arp.mac_address and \
54                 vip.bytes == arp.ip_address[:n]:
55             return True
56     return False
57
58
59 def find_l2_fib_entry(test, bd_id, mac, sw_if_index):
60     vmac = MACAddress(mac)
61     lfs = test.vapi.l2_fib_table_dump(bd_id)
62     for lf in lfs:
63         if vmac.packed == lf.mac and sw_if_index == lf.sw_if_index:
64             return True
65     return False
66
67
68 class VppBridgeDomain(VppObject):
69
70     def __init__(self, test, bd_id,
71                  flood=1, uu_flood=1, forward=1,
72                  learn=1, arp_term=1):
73         self._test = test
74         self.bd_id = bd_id
75         self.flood = flood
76         self.uu_flood = uu_flood
77         self.forward = forward
78         self.learn = learn
79         self.arp_term = arp_term
80
81     def add_vpp_config(self):
82         self._test.vapi.bridge_domain_add_del(bd_id=self.bd_id,
83                                               flood=self.flood,
84                                               uu_flood=self.uu_flood,
85                                               forward=self.forward,
86                                               learn=self.learn,
87                                               arp_term=self.arp_term, is_add=1)
88         self._test.registry.register(self, self._test.logger)
89
90     def remove_vpp_config(self):
91         self._test.vapi.bridge_domain_add_del(bd_id=self.bd_id, is_add=0)
92
93     def query_vpp_config(self):
94         return find_bridge_domain(self._test, self.bd_id)
95
96     def object_id(self):
97         return "bridge-domain-%d" % (self.bd_id)
98
99
100 class VppBridgeDomainPort(VppObject):
101
102     def __init__(self, test, bd, itf,
103                  port_type=L2_PORT_TYPE.NORMAL):
104         self._test = test
105         self.bd = bd
106         self.itf = itf
107         self.port_type = port_type
108
109     def add_vpp_config(self):
110         self._test.vapi.sw_interface_set_l2_bridge(
111             rx_sw_if_index=self.itf.sw_if_index, bd_id=self.bd.bd_id,
112             port_type=self.port_type, enable=1)
113         self._test.registry.register(self, self._test.logger)
114
115     def remove_vpp_config(self):
116         self._test.vapi.sw_interface_set_l2_bridge(
117             rx_sw_if_index=self.itf.sw_if_index, bd_id=self.bd.bd_id,
118             port_type=self.port_type, enable=0)
119
120     def query_vpp_config(self):
121         return find_bridge_domain_port(self._test,
122                                        self.bd.bd_id,
123                                        self.itf.sw_if_index)
124
125     def object_id(self):
126         return "BD-Port-%s-%s" % (self.bd, self.itf)
127
128
129 class VppBridgeDomainArpEntry(VppObject):
130
131     def __init__(self, test, bd, mac, ip):
132         self._test = test
133         self.bd = bd
134         self.mac = MACAddress(mac)
135         self.ip = VppIpAddress(ip)
136
137     def add_vpp_config(self):
138         self._test.vapi.bd_ip_mac_add_del(bd_id=self.bd.bd_id, is_add=1,
139                                           ip=self.ip.encode(),
140                                           mac=self.mac.packed)
141         self._test.registry.register(self, self._test.logger)
142
143     def remove_vpp_config(self):
144         self._test.vapi.bd_ip_mac_add_del(bd_id=self.bd.bd_id, is_add=0,
145                                           ip=self.ip.encode(),
146                                           mac=self.mac.packed)
147
148     def query_vpp_config(self):
149         return find_bridge_domain_arp_entry(self._test,
150                                             self.bd.bd_id,
151                                             self.mac.packed,
152                                             self.ip.address)
153
154     def object_id(self):
155         return "BD-Arp-Entry-%s-%s-%s" % (self.bd, self.mac, self.ip.address)
156
157
158 class VppL2FibEntry(VppObject):
159
160     def __init__(self, test, bd, mac, itf,
161                  static_mac=0, filter_mac=0, bvi_mac=-1):
162         self._test = test
163         self.bd = bd
164         self.mac = MACAddress(mac)
165         self.itf = itf
166         self.static_mac = static_mac
167         self.filter_mac = filter_mac
168         if bvi_mac == -1:
169             self.bvi_mac = isinstance(self.itf, VppLoInterface)
170         else:
171             self.bvi_mac = bvi_mac
172
173     def add_vpp_config(self):
174         self._test.vapi.l2fib_add_del(
175             self.mac.packed,
176             self.bd.bd_id,
177             self.itf.sw_if_index,
178             is_add=1,
179             static_mac=self.static_mac,
180             filter_mac=self.filter_mac,
181             bvi_mac=self.bvi_mac)
182         self._test.registry.register(self, self._test.logger)
183
184     def remove_vpp_config(self):
185         self._test.vapi.l2fib_add_del(
186             self.mac.packed,
187             self.bd.bd_id,
188             self.itf.sw_if_index,
189             is_add=0)
190
191     def query_vpp_config(self):
192         return find_l2_fib_entry(self._test,
193                                  self.bd.bd_id,
194                                  self.mac.packed,
195                                  self.itf.sw_if_index)
196
197     def object_id(self):
198         return "L2-Fib-Entry-%s-%s-%s" % (self.bd, self.mac, self.itf)
199
200
201 class VppL2Vtr(VppObject):
202
203     def __init__(self, test, itf, op):
204         self._test = test
205         self.itf = itf
206         self.op = op
207
208     def add_vpp_config(self):
209         self.itf.set_vtr(self.op)
210         self._test.registry.register(self, self._test.logger)
211
212     def remove_vpp_config(self):
213         self.itf.set_vtr(L2_VTR_OP.L2_DISABLED)
214
215     def query_vpp_config(self):
216         ds = self._test.vapi.sw_interface_dump()
217         d = self.itf.get_interface_config_from_dump(ds)
218
219         if d is not None:
220             return (d.vtr_op == self.op)
221         return False
222
223     def object_id(self):
224         return "L2-vtr-%s-%d" % (str(self.itf), self.op)