GBP: L3 out fixes
[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 __str__(self):
97         return self.object_id()
98
99     def object_id(self):
100         return "bridge-domain-%d" % (self.bd_id)
101
102
103 class VppBridgeDomainPort(VppObject):
104
105     def __init__(self, test, bd, itf,
106                  port_type=L2_PORT_TYPE.NORMAL):
107         self._test = test
108         self.bd = bd
109         self.itf = itf
110         self.port_type = port_type
111
112     def add_vpp_config(self):
113         self._test.vapi.sw_interface_set_l2_bridge(
114             rx_sw_if_index=self.itf.sw_if_index, bd_id=self.bd.bd_id,
115             port_type=self.port_type, enable=1)
116         self._test.registry.register(self, self._test.logger)
117
118     def remove_vpp_config(self):
119         self._test.vapi.sw_interface_set_l2_bridge(
120             rx_sw_if_index=self.itf.sw_if_index, bd_id=self.bd.bd_id,
121             port_type=self.port_type, enable=0)
122
123     def query_vpp_config(self):
124         return find_bridge_domain_port(self._test,
125                                        self.bd.bd_id,
126                                        self.itf.sw_if_index)
127
128     def __str__(self):
129         return self.object_id()
130
131     def object_id(self):
132         return "BD-Port-%s-%s" % (self.bd, self.itf)
133
134
135 class VppBridgeDomainArpEntry(VppObject):
136
137     def __init__(self, test, bd, mac, ip):
138         self._test = test
139         self.bd = bd
140         self.mac = MACAddress(mac)
141         self.ip = VppIpAddress(ip)
142
143     def add_vpp_config(self):
144         self._test.vapi.bd_ip_mac_add_del(bd_id=self.bd.bd_id, is_add=1,
145                                           ip=self.ip.encode(),
146                                           mac=self.mac.packed)
147         self._test.registry.register(self, self._test.logger)
148
149     def remove_vpp_config(self):
150         self._test.vapi.bd_ip_mac_add_del(bd_id=self.bd.bd_id, is_add=0,
151                                           ip=self.ip.encode(),
152                                           mac=self.mac.packed)
153
154     def query_vpp_config(self):
155         return find_bridge_domain_arp_entry(self._test,
156                                             self.bd.bd_id,
157                                             self.mac.packed,
158                                             self.ip.address)
159
160     def __str__(self):
161         return self.object_id()
162
163     def object_id(self):
164         return "BD-Arp-Entry-%s-%s-%s" % (self.bd, self.mac, self.ip.address)
165
166
167 class VppL2FibEntry(VppObject):
168
169     def __init__(self, test, bd, mac, itf,
170                  static_mac=0, filter_mac=0, bvi_mac=-1):
171         self._test = test
172         self.bd = bd
173         self.mac = MACAddress(mac)
174         self.itf = itf
175         self.static_mac = static_mac
176         self.filter_mac = filter_mac
177         if bvi_mac == -1:
178             self.bvi_mac = isinstance(self.itf, VppLoInterface)
179         else:
180             self.bvi_mac = bvi_mac
181
182     def add_vpp_config(self):
183         self._test.vapi.l2fib_add_del(
184             self.mac.packed,
185             self.bd.bd_id,
186             self.itf.sw_if_index,
187             is_add=1,
188             static_mac=self.static_mac,
189             filter_mac=self.filter_mac,
190             bvi_mac=self.bvi_mac)
191         self._test.registry.register(self, self._test.logger)
192
193     def remove_vpp_config(self):
194         self._test.vapi.l2fib_add_del(
195             self.mac.packed,
196             self.bd.bd_id,
197             self.itf.sw_if_index,
198             is_add=0)
199
200     def query_vpp_config(self):
201         return find_l2_fib_entry(self._test,
202                                  self.bd.bd_id,
203                                  self.mac.packed,
204                                  self.itf.sw_if_index)
205
206     def __str__(self):
207         return self.object_id()
208
209     def object_id(self):
210         return "L2-Fib-Entry-%s-%s-%s" % (self.bd, self.mac, self.itf)
211
212
213 class VppL2Vtr(VppObject):
214
215     def __init__(self, test, itf, op):
216         self._test = test
217         self.itf = itf
218         self.op = op
219
220     def add_vpp_config(self):
221         self.itf.set_vtr(self.op)
222         self._test.registry.register(self, self._test.logger)
223
224     def remove_vpp_config(self):
225         self.itf.set_vtr(L2_VTR_OP.L2_DISABLED)
226
227     def query_vpp_config(self):
228         ds = self._test.vapi.sw_interface_dump()
229         d = self.itf.get_interface_config_from_dump(ds)
230
231         if d is not None:
232             return (d.vtr_op == self.op)
233         return False
234
235     def __str__(self):
236         return self.object_id()
237
238     def object_id(self):
239         return "L2-vtr-%s-%d" % (str(self.itf), self.op)