blob: 21972bf4148a5638744afdc8e5591a56d36a6752 [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
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020010
Neale Rannsbc764c82019-06-19 07:07:13 -070011try:
12 text_type = unicode
13except NameError:
14 text_type = str
Neale Ranns93cc3ee2018-10-10 07:22:51 -070015
16
17class L2_PORT_TYPE:
18 NORMAL = 0
19 BVI = 1
20 UU_FWD = 2
21
22
23class BRIDGE_FLAGS:
24 NONE = 0
25 LEARN = 1
26 FWD = 2
27 FLOOD = 4
28 UU_FLOOD = 8
29 ARP_TERM = 16
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +020030 ARP_UFWD = 32
Neale Ranns93cc3ee2018-10-10 07:22:51 -070031
32
33def find_bridge_domain(test, bd_id):
34 bds = test.vapi.bridge_domain_dump(bd_id)
35 return len(bds) == 1
36
37
38def find_bridge_domain_port(test, bd_id, sw_if_index):
39 bds = test.vapi.bridge_domain_dump(bd_id)
40 for bd in bds:
41 for p in bd.sw_if_details:
42 if p.sw_if_index == sw_if_index:
43 return True
44 return False
45
46
47def find_bridge_domain_arp_entry(test, bd_id, mac, ip):
Neale Ranns93cc3ee2018-10-10 07:22:51 -070048 arps = test.vapi.bd_ip_mac_dump(bd_id)
49 for arp in arps:
50 # do IP addr comparison too once .api is fixed...
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020051 if mac == str(arp.entry.mac) and 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):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020066 def __init__(
67 self,
68 test,
69 bd_id,
70 flood=1,
71 uu_flood=1,
72 forward=1,
73 learn=1,
74 arp_term=1,
75 arp_ufwd=0,
76 ):
Neale Ranns93cc3ee2018-10-10 07:22:51 -070077 self._test = test
78 self.bd_id = bd_id
79 self.flood = flood
80 self.uu_flood = uu_flood
81 self.forward = forward
82 self.learn = learn
83 self.arp_term = arp_term
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +020084 self.arp_ufwd = arp_ufwd
Neale Ranns93cc3ee2018-10-10 07:22:51 -070085
86 def add_vpp_config(self):
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +020087 self._test.vapi.bridge_domain_add_del_v2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020088 bd_id=self.bd_id,
89 flood=self.flood,
90 uu_flood=self.uu_flood,
91 forward=self.forward,
92 learn=self.learn,
93 arp_term=self.arp_term,
94 arp_ufwd=self.arp_ufwd,
95 is_add=1,
96 )
Neale Ranns93cc3ee2018-10-10 07:22:51 -070097 self._test.registry.register(self, self._test.logger)
Neale Ranns61aad0a2021-10-25 09:13:00 +000098 return self
Neale Ranns93cc3ee2018-10-10 07:22:51 -070099
100 def remove_vpp_config(self):
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +0200101 self._test.vapi.bridge_domain_add_del_v2(bd_id=self.bd_id, is_add=0)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700102
103 def query_vpp_config(self):
104 return find_bridge_domain(self._test, self.bd_id)
105
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700106 def object_id(self):
107 return "bridge-domain-%d" % (self.bd_id)
108
109
110class VppBridgeDomainPort(VppObject):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200111 def __init__(self, test, bd, itf, port_type=L2_PORT_TYPE.NORMAL):
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700112 self._test = test
113 self.bd = bd
114 self.itf = itf
115 self.port_type = port_type
116
117 def add_vpp_config(self):
118 self._test.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200119 rx_sw_if_index=self.itf.sw_if_index,
120 bd_id=self.bd.bd_id,
121 port_type=self.port_type,
122 enable=1,
123 )
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700124 self._test.registry.register(self, self._test.logger)
Neale Ranns61aad0a2021-10-25 09:13:00 +0000125 return self
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700126
127 def remove_vpp_config(self):
128 self._test.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200129 rx_sw_if_index=self.itf.sw_if_index,
130 bd_id=self.bd.bd_id,
131 port_type=self.port_type,
132 enable=0,
133 )
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700134
135 def query_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200136 return find_bridge_domain_port(self._test, self.bd.bd_id, self.itf.sw_if_index)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700137
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700138 def object_id(self):
139 return "BD-Port-%s-%s" % (self.bd, self.itf)
140
141
142class VppBridgeDomainArpEntry(VppObject):
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700143 def __init__(self, test, bd, mac, ip):
144 self._test = test
145 self.bd = bd
Neale Rannsbc764c82019-06-19 07:07:13 -0700146 self.mac = mac
147 self.ip = ip
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700148
149 def add_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200150 self._test.vapi.bd_ip_mac_add_del(
151 is_add=1, entry={"bd_id": self.bd.bd_id, "ip": self.ip, "mac": self.mac}
152 )
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700153 self._test.registry.register(self, self._test.logger)
Neale Ranns61aad0a2021-10-25 09:13:00 +0000154 return self
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700155
156 def remove_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200157 self._test.vapi.bd_ip_mac_add_del(
158 is_add=0, entry={"bd_id": self.bd.bd_id, "ip": self.ip, "mac": self.mac}
159 )
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700160
161 def query_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200162 return find_bridge_domain_arp_entry(
163 self._test, self.bd.bd_id, self.mac, self.ip
164 )
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700165
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700166 def object_id(self):
Neale Rannsbc764c82019-06-19 07:07:13 -0700167 return "BD-Arp-Entry-%s-%s-%s" % (self.bd, self.mac, self.ip)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700168
169
170class VppL2FibEntry(VppObject):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200171 def __init__(self, test, bd, mac, itf, static_mac=0, filter_mac=0, bvi_mac=-1):
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700172 self._test = test
173 self.bd = bd
Ole Troan8006c6a2018-12-17 12:02:26 +0100174 self.mac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700175 self.itf = itf
176 self.static_mac = static_mac
177 self.filter_mac = filter_mac
178 if bvi_mac == -1:
179 self.bvi_mac = isinstance(self.itf, VppLoInterface)
180 else:
181 self.bvi_mac = bvi_mac
182
183 def add_vpp_config(self):
184 self._test.vapi.l2fib_add_del(
Ole Troan8006c6a2018-12-17 12:02:26 +0100185 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700186 self.bd.bd_id,
187 self.itf.sw_if_index,
188 is_add=1,
189 static_mac=self.static_mac,
190 filter_mac=self.filter_mac,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200191 bvi_mac=self.bvi_mac,
192 )
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700193 self._test.registry.register(self, self._test.logger)
Neale Ranns61aad0a2021-10-25 09:13:00 +0000194 return self
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700195
196 def remove_vpp_config(self):
197 self._test.vapi.l2fib_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200198 self.mac.packed, self.bd.bd_id, self.itf.sw_if_index, is_add=0
199 )
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700200
201 def query_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200202 return find_l2_fib_entry(
203 self._test, self.bd.bd_id, self.mac.packed, self.itf.sw_if_index
204 )
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700205
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700206 def object_id(self):
207 return "L2-Fib-Entry-%s-%s-%s" % (self.bd, self.mac, self.itf)
Neale Ranns36abbf12019-03-12 02:34:07 -0700208
209
210class VppL2Vtr(VppObject):
Neale Ranns36abbf12019-03-12 02:34:07 -0700211 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:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200229 return d.vtr_op == self.op
Neale Ranns36abbf12019-03-12 02:34:07 -0700230 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)