blob: 90de91695f2e60dd0f24fffa42c7b8eb8964eb8d [file] [log] [blame]
Neale Ranns93cc3ee2018-10-10 07:22:51 -07001"""
2 L2/BD Types
3
4"""
5
Paul Vinciguerraa279d9c2019-02-28 09:00:09 -08006from vpp_object import VppObject
Neale Ranns93cc3ee2018-10-10 07:22:51 -07007from vpp_ip import VppIpAddress
Neale Ranns93cc3ee2018-10-10 07:22:51 -07008from vpp_lo_interface import VppLoInterface
Ole Troan8006c6a2018-12-17 12:02:26 +01009from vpp_papi import MACAddress
Paul Vinciguerra95c0ca42019-03-28 13:07:00 -070010from vpp_sub_interface import L2_VTR_OP
Neale Ranns93cc3ee2018-10-10 07:22:51 -070011
12
13class L2_PORT_TYPE:
14 NORMAL = 0
15 BVI = 1
16 UU_FWD = 2
17
18
19class BRIDGE_FLAGS:
20 NONE = 0
21 LEARN = 1
22 FWD = 2
23 FLOOD = 4
24 UU_FLOOD = 8
25 ARP_TERM = 16
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +020026 ARP_UFWD = 32
Neale Ranns93cc3ee2018-10-10 07:22:51 -070027
28
29def find_bridge_domain(test, bd_id):
30 bds = test.vapi.bridge_domain_dump(bd_id)
31 return len(bds) == 1
32
33
34def find_bridge_domain_port(test, bd_id, sw_if_index):
35 bds = test.vapi.bridge_domain_dump(bd_id)
36 for bd in bds:
37 for p in bd.sw_if_details:
38 if p.sw_if_index == sw_if_index:
39 return True
40 return False
41
42
43def find_bridge_domain_arp_entry(test, bd_id, mac, ip):
Ole Troan8006c6a2018-12-17 12:02:26 +010044 vmac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -070045 vip = VppIpAddress(ip)
46
47 if vip.version == 4:
48 n = 4
49 else:
50 n = 16
51
52 arps = test.vapi.bd_ip_mac_dump(bd_id)
53 for arp in arps:
54 # do IP addr comparison too once .api is fixed...
Ole Troan8006c6a2018-12-17 12:02:26 +010055 if vmac.packed == arp.mac_address and \
Ole Troan9a475372019-03-05 16:58:24 +010056 vip.bytes == arp.ip_address[:n]:
Neale Ranns93cc3ee2018-10-10 07:22:51 -070057 return True
58 return False
59
60
61def find_l2_fib_entry(test, bd_id, mac, sw_if_index):
Ole Troan8006c6a2018-12-17 12:02:26 +010062 vmac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -070063 lfs = test.vapi.l2_fib_table_dump(bd_id)
64 for lf in lfs:
Ole Troan8006c6a2018-12-17 12:02:26 +010065 if vmac.packed == lf.mac and sw_if_index == lf.sw_if_index:
Neale Ranns93cc3ee2018-10-10 07:22:51 -070066 return True
67 return False
68
69
70class VppBridgeDomain(VppObject):
71
72 def __init__(self, test, bd_id,
73 flood=1, uu_flood=1, forward=1,
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +020074 learn=1, arp_term=1, arp_ufwd=0):
Neale Ranns93cc3ee2018-10-10 07:22:51 -070075 self._test = test
76 self.bd_id = bd_id
77 self.flood = flood
78 self.uu_flood = uu_flood
79 self.forward = forward
80 self.learn = learn
81 self.arp_term = arp_term
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +020082 self.arp_ufwd = arp_ufwd
Neale Ranns93cc3ee2018-10-10 07:22:51 -070083
84 def add_vpp_config(self):
Ole Troana5b2eec2019-03-11 19:23:25 +010085 self._test.vapi.bridge_domain_add_del(bd_id=self.bd_id,
86 flood=self.flood,
87 uu_flood=self.uu_flood,
88 forward=self.forward,
89 learn=self.learn,
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +020090 arp_term=self.arp_term,
91 arp_ufwd=self.arp_ufwd,
92 is_add=1)
Neale Ranns93cc3ee2018-10-10 07:22:51 -070093 self._test.registry.register(self, self._test.logger)
94
95 def remove_vpp_config(self):
Ole Troana5b2eec2019-03-11 19:23:25 +010096 self._test.vapi.bridge_domain_add_del(bd_id=self.bd_id, is_add=0)
Neale Ranns93cc3ee2018-10-10 07:22:51 -070097
98 def query_vpp_config(self):
99 return find_bridge_domain(self._test, self.bd_id)
100
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700101 def object_id(self):
102 return "bridge-domain-%d" % (self.bd_id)
103
104
105class VppBridgeDomainPort(VppObject):
106
107 def __init__(self, test, bd, itf,
108 port_type=L2_PORT_TYPE.NORMAL):
109 self._test = test
110 self.bd = bd
111 self.itf = itf
112 self.port_type = port_type
113
114 def add_vpp_config(self):
115 self._test.vapi.sw_interface_set_l2_bridge(
Ole Troana5b2eec2019-03-11 19:23:25 +0100116 rx_sw_if_index=self.itf.sw_if_index, bd_id=self.bd.bd_id,
117 port_type=self.port_type, enable=1)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700118 self._test.registry.register(self, self._test.logger)
119
120 def remove_vpp_config(self):
121 self._test.vapi.sw_interface_set_l2_bridge(
Ole Troana5b2eec2019-03-11 19:23:25 +0100122 rx_sw_if_index=self.itf.sw_if_index, bd_id=self.bd.bd_id,
123 port_type=self.port_type, enable=0)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700124
125 def query_vpp_config(self):
126 return find_bridge_domain_port(self._test,
127 self.bd.bd_id,
128 self.itf.sw_if_index)
129
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700130 def object_id(self):
131 return "BD-Port-%s-%s" % (self.bd, self.itf)
132
133
134class VppBridgeDomainArpEntry(VppObject):
135
136 def __init__(self, test, bd, mac, ip):
137 self._test = test
138 self.bd = bd
Ole Troan8006c6a2018-12-17 12:02:26 +0100139 self.mac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700140 self.ip = VppIpAddress(ip)
141
142 def add_vpp_config(self):
Ole Troan9a475372019-03-05 16:58:24 +0100143 self._test.vapi.bd_ip_mac_add_del(bd_id=self.bd.bd_id, is_add=1,
144 ip=self.ip.encode(),
145 mac=self.mac.packed)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700146 self._test.registry.register(self, self._test.logger)
147
148 def remove_vpp_config(self):
Ole Troan9a475372019-03-05 16:58:24 +0100149 self._test.vapi.bd_ip_mac_add_del(bd_id=self.bd.bd_id, is_add=0,
150 ip=self.ip.encode(),
151 mac=self.mac.packed)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700152
153 def query_vpp_config(self):
154 return find_bridge_domain_arp_entry(self._test,
155 self.bd.bd_id,
Ole Troan8006c6a2018-12-17 12:02:26 +0100156 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700157 self.ip.address)
158
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700159 def object_id(self):
160 return "BD-Arp-Entry-%s-%s-%s" % (self.bd, self.mac, self.ip.address)
161
162
163class VppL2FibEntry(VppObject):
164
165 def __init__(self, test, bd, mac, itf,
166 static_mac=0, filter_mac=0, bvi_mac=-1):
167 self._test = test
168 self.bd = bd
Ole Troan8006c6a2018-12-17 12:02:26 +0100169 self.mac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700170 self.itf = itf
171 self.static_mac = static_mac
172 self.filter_mac = filter_mac
173 if bvi_mac == -1:
174 self.bvi_mac = isinstance(self.itf, VppLoInterface)
175 else:
176 self.bvi_mac = bvi_mac
177
178 def add_vpp_config(self):
179 self._test.vapi.l2fib_add_del(
Ole Troan8006c6a2018-12-17 12:02:26 +0100180 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700181 self.bd.bd_id,
182 self.itf.sw_if_index,
183 is_add=1,
184 static_mac=self.static_mac,
185 filter_mac=self.filter_mac,
186 bvi_mac=self.bvi_mac)
187 self._test.registry.register(self, self._test.logger)
188
189 def remove_vpp_config(self):
190 self._test.vapi.l2fib_add_del(
Ole Troan8006c6a2018-12-17 12:02:26 +0100191 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700192 self.bd.bd_id,
193 self.itf.sw_if_index,
194 is_add=0)
195
196 def query_vpp_config(self):
197 return find_l2_fib_entry(self._test,
198 self.bd.bd_id,
Ole Troan8006c6a2018-12-17 12:02:26 +0100199 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700200 self.itf.sw_if_index)
201
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700202 def object_id(self):
203 return "L2-Fib-Entry-%s-%s-%s" % (self.bd, self.mac, self.itf)
Neale Ranns36abbf12019-03-12 02:34:07 -0700204
205
206class VppL2Vtr(VppObject):
207
208 def __init__(self, test, itf, op):
209 self._test = test
210 self.itf = itf
211 self.op = op
212
213 def add_vpp_config(self):
214 self.itf.set_vtr(self.op)
215 self._test.registry.register(self, self._test.logger)
216
217 def remove_vpp_config(self):
218 self.itf.set_vtr(L2_VTR_OP.L2_DISABLED)
219
220 def query_vpp_config(self):
221 ds = self._test.vapi.sw_interface_dump()
222 d = self.itf.get_interface_config_from_dump(ds)
223
224 if d is not None:
225 return (d.vtr_op == self.op)
226 return False
227
Neale Ranns36abbf12019-03-12 02:34:07 -0700228 def object_id(self):
229 return "L2-vtr-%s-%d" % (str(self.itf), self.op)