blob: 7307d28ad13f781384f46f88771fc1599c640ab1 [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_lo_interface import VppLoInterface
Ole Troan8006c6a2018-12-17 12:02:26 +01008from vpp_papi import MACAddress
Paul Vinciguerra95c0ca42019-03-28 13:07:00 -07009from vpp_sub_interface import L2_VTR_OP
Neale Rannsbc764c82019-06-19 07:07:13 -070010try:
11 text_type = unicode
12except NameError:
13 text_type = str
Neale Ranns93cc3ee2018-10-10 07:22:51 -070014
15
16class L2_PORT_TYPE:
17 NORMAL = 0
18 BVI = 1
19 UU_FWD = 2
20
21
22class BRIDGE_FLAGS:
23 NONE = 0
24 LEARN = 1
25 FWD = 2
26 FLOOD = 4
27 UU_FLOOD = 8
28 ARP_TERM = 16
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +020029 ARP_UFWD = 32
Neale Ranns93cc3ee2018-10-10 07:22:51 -070030
31
32def find_bridge_domain(test, bd_id):
33 bds = test.vapi.bridge_domain_dump(bd_id)
34 return len(bds) == 1
35
36
37def find_bridge_domain_port(test, bd_id, sw_if_index):
38 bds = test.vapi.bridge_domain_dump(bd_id)
39 for bd in bds:
40 for p in bd.sw_if_details:
41 if p.sw_if_index == sw_if_index:
42 return True
43 return False
44
45
46def find_bridge_domain_arp_entry(test, bd_id, mac, ip):
Neale Ranns93cc3ee2018-10-10 07:22:51 -070047 arps = test.vapi.bd_ip_mac_dump(bd_id)
48 for arp in arps:
49 # do IP addr comparison too once .api is fixed...
Neale Rannsbc764c82019-06-19 07:07:13 -070050 if mac == str(arp.entry.mac) and \
51 ip == str(arp.entry.ip):
Neale Ranns93cc3ee2018-10-10 07:22:51 -070052 return True
53 return False
54
55
56def find_l2_fib_entry(test, bd_id, mac, sw_if_index):
Ole Troan8006c6a2018-12-17 12:02:26 +010057 vmac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -070058 lfs = test.vapi.l2_fib_table_dump(bd_id)
59 for lf in lfs:
Ole Troan8006c6a2018-12-17 12:02:26 +010060 if vmac.packed == lf.mac and sw_if_index == lf.sw_if_index:
Neale Ranns93cc3ee2018-10-10 07:22:51 -070061 return True
62 return False
63
64
65class VppBridgeDomain(VppObject):
66
67 def __init__(self, test, bd_id,
68 flood=1, uu_flood=1, forward=1,
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +020069 learn=1, arp_term=1, arp_ufwd=0):
Neale Ranns93cc3ee2018-10-10 07:22:51 -070070 self._test = test
71 self.bd_id = bd_id
72 self.flood = flood
73 self.uu_flood = uu_flood
74 self.forward = forward
75 self.learn = learn
76 self.arp_term = arp_term
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +020077 self.arp_ufwd = arp_ufwd
Neale Ranns93cc3ee2018-10-10 07:22:51 -070078
79 def add_vpp_config(self):
Ole Troana5b2eec2019-03-11 19:23:25 +010080 self._test.vapi.bridge_domain_add_del(bd_id=self.bd_id,
81 flood=self.flood,
82 uu_flood=self.uu_flood,
83 forward=self.forward,
84 learn=self.learn,
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +020085 arp_term=self.arp_term,
86 arp_ufwd=self.arp_ufwd,
87 is_add=1)
Neale Ranns93cc3ee2018-10-10 07:22:51 -070088 self._test.registry.register(self, self._test.logger)
Neale Ranns61aad0a2021-10-25 09:13:00 +000089 return self
Neale Ranns93cc3ee2018-10-10 07:22:51 -070090
91 def remove_vpp_config(self):
Ole Troana5b2eec2019-03-11 19:23:25 +010092 self._test.vapi.bridge_domain_add_del(bd_id=self.bd_id, is_add=0)
Neale Ranns93cc3ee2018-10-10 07:22:51 -070093
94 def query_vpp_config(self):
95 return find_bridge_domain(self._test, self.bd_id)
96
Neale Ranns93cc3ee2018-10-10 07:22:51 -070097 def object_id(self):
98 return "bridge-domain-%d" % (self.bd_id)
99
100
101class VppBridgeDomainPort(VppObject):
102
103 def __init__(self, test, bd, itf,
104 port_type=L2_PORT_TYPE.NORMAL):
105 self._test = test
106 self.bd = bd
107 self.itf = itf
108 self.port_type = port_type
109
110 def add_vpp_config(self):
111 self._test.vapi.sw_interface_set_l2_bridge(
Ole Troana5b2eec2019-03-11 19:23:25 +0100112 rx_sw_if_index=self.itf.sw_if_index, bd_id=self.bd.bd_id,
113 port_type=self.port_type, enable=1)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700114 self._test.registry.register(self, self._test.logger)
Neale Ranns61aad0a2021-10-25 09:13:00 +0000115 return self
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700116
117 def remove_vpp_config(self):
118 self._test.vapi.sw_interface_set_l2_bridge(
Ole Troana5b2eec2019-03-11 19:23:25 +0100119 rx_sw_if_index=self.itf.sw_if_index, bd_id=self.bd.bd_id,
120 port_type=self.port_type, enable=0)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700121
122 def query_vpp_config(self):
123 return find_bridge_domain_port(self._test,
124 self.bd.bd_id,
125 self.itf.sw_if_index)
126
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700127 def object_id(self):
128 return "BD-Port-%s-%s" % (self.bd, self.itf)
129
130
131class VppBridgeDomainArpEntry(VppObject):
132
133 def __init__(self, test, bd, mac, ip):
134 self._test = test
135 self.bd = bd
Neale Rannsbc764c82019-06-19 07:07:13 -0700136 self.mac = mac
137 self.ip = ip
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700138
139 def add_vpp_config(self):
Neale Rannsbc764c82019-06-19 07:07:13 -0700140 self._test.vapi.bd_ip_mac_add_del(is_add=1,
141 entry={
142 'bd_id': self.bd.bd_id,
143 'ip': self.ip,
144 'mac': self.mac})
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700145 self._test.registry.register(self, self._test.logger)
Neale Ranns61aad0a2021-10-25 09:13:00 +0000146 return self
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700147
148 def remove_vpp_config(self):
Neale Rannsbc764c82019-06-19 07:07:13 -0700149 self._test.vapi.bd_ip_mac_add_del(is_add=0,
150 entry={
151 'bd_id': self.bd.bd_id,
152 'ip': self.ip,
153 'mac': self.mac})
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700154
155 def query_vpp_config(self):
156 return find_bridge_domain_arp_entry(self._test,
157 self.bd.bd_id,
Neale Rannsbc764c82019-06-19 07:07:13 -0700158 self.mac,
159 self.ip)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700160
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700161 def object_id(self):
Neale Rannsbc764c82019-06-19 07:07:13 -0700162 return "BD-Arp-Entry-%s-%s-%s" % (self.bd, self.mac, self.ip)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700163
164
165class VppL2FibEntry(VppObject):
166
167 def __init__(self, test, bd, mac, itf,
168 static_mac=0, filter_mac=0, bvi_mac=-1):
169 self._test = test
170 self.bd = bd
Ole Troan8006c6a2018-12-17 12:02:26 +0100171 self.mac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700172 self.itf = itf
173 self.static_mac = static_mac
174 self.filter_mac = filter_mac
175 if bvi_mac == -1:
176 self.bvi_mac = isinstance(self.itf, VppLoInterface)
177 else:
178 self.bvi_mac = bvi_mac
179
180 def add_vpp_config(self):
181 self._test.vapi.l2fib_add_del(
Ole Troan8006c6a2018-12-17 12:02:26 +0100182 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700183 self.bd.bd_id,
184 self.itf.sw_if_index,
185 is_add=1,
186 static_mac=self.static_mac,
187 filter_mac=self.filter_mac,
188 bvi_mac=self.bvi_mac)
189 self._test.registry.register(self, self._test.logger)
Neale Ranns61aad0a2021-10-25 09:13:00 +0000190 return self
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700191
192 def remove_vpp_config(self):
193 self._test.vapi.l2fib_add_del(
Ole Troan8006c6a2018-12-17 12:02:26 +0100194 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700195 self.bd.bd_id,
196 self.itf.sw_if_index,
197 is_add=0)
198
199 def query_vpp_config(self):
200 return find_l2_fib_entry(self._test,
201 self.bd.bd_id,
Ole Troan8006c6a2018-12-17 12:02:26 +0100202 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700203 self.itf.sw_if_index)
204
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700205 def object_id(self):
206 return "L2-Fib-Entry-%s-%s-%s" % (self.bd, self.mac, self.itf)
Neale Ranns36abbf12019-03-12 02:34:07 -0700207
208
209class VppL2Vtr(VppObject):
210
211 def __init__(self, test, itf, op):
212 self._test = test
213 self.itf = itf
214 self.op = op
215
216 def add_vpp_config(self):
217 self.itf.set_vtr(self.op)
218 self._test.registry.register(self, self._test.logger)
Neale Ranns61aad0a2021-10-25 09:13:00 +0000219 return self
Neale Ranns36abbf12019-03-12 02:34:07 -0700220
221 def remove_vpp_config(self):
222 self.itf.set_vtr(L2_VTR_OP.L2_DISABLED)
223
224 def query_vpp_config(self):
225 ds = self._test.vapi.sw_interface_dump()
226 d = self.itf.get_interface_config_from_dump(ds)
227
228 if d is not None:
229 return (d.vtr_op == self.op)
230 return False
231
Neale Ranns36abbf12019-03-12 02:34:07 -0700232 def object_id(self):
233 return "L2-vtr-%s-%d" % (str(self.itf), self.op)