blob: 67fb996848baad3062577f40a50fcae2d5e68b76 [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
Neale Ranns93cc3ee2018-10-10 07:22:51 -070010
11
12class L2_PORT_TYPE:
13 NORMAL = 0
14 BVI = 1
15 UU_FWD = 2
16
17
18class BRIDGE_FLAGS:
19 NONE = 0
20 LEARN = 1
21 FWD = 2
22 FLOOD = 4
23 UU_FLOOD = 8
24 ARP_TERM = 16
25
26
27def find_bridge_domain(test, bd_id):
28 bds = test.vapi.bridge_domain_dump(bd_id)
29 return len(bds) == 1
30
31
32def 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
41def find_bridge_domain_arp_entry(test, bd_id, mac, ip):
Ole Troan8006c6a2018-12-17 12:02:26 +010042 vmac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -070043 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...
Ole Troan8006c6a2018-12-17 12:02:26 +010053 if vmac.packed == arp.mac_address and \
Neale Ranns93cc3ee2018-10-10 07:22:51 -070054 vip.bytes == arp.ip_address[:n]:
55 return True
56 return False
57
58
59def find_l2_fib_entry(test, bd_id, mac, sw_if_index):
Ole Troan8006c6a2018-12-17 12:02:26 +010060 vmac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -070061 lfs = test.vapi.l2_fib_table_dump(bd_id)
62 for lf in lfs:
Ole Troan8006c6a2018-12-17 12:02:26 +010063 if vmac.packed == lf.mac and sw_if_index == lf.sw_if_index:
Neale Ranns93cc3ee2018-10-10 07:22:51 -070064 return True
65 return False
66
67
68class 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(
83 self.bd_id,
84 is_add=1,
85 flood=self.flood,
86 uu_flood=self.uu_flood,
87 forward=self.forward,
88 learn=self.learn,
89 arp_term=self.arp_term)
90 self._test.registry.register(self, self._test.logger)
91
92 def remove_vpp_config(self):
93 self._test.vapi.bridge_domain_add_del(self.bd_id, is_add=0)
94
95 def query_vpp_config(self):
96 return find_bridge_domain(self._test, self.bd_id)
97
98 def __str__(self):
99 return self.object_id()
100
101 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(
116 self.itf.sw_if_index,
117 self.bd.bd_id,
118 port_type=self.port_type,
119 enable=1)
120 self._test.registry.register(self, self._test.logger)
121
122 def remove_vpp_config(self):
123 self._test.vapi.sw_interface_set_l2_bridge(
124 self.itf.sw_if_index,
125 self.bd.bd_id,
126 port_type=self.port_type,
127 enable=0)
128
129 def query_vpp_config(self):
130 return find_bridge_domain_port(self._test,
131 self.bd.bd_id,
132 self.itf.sw_if_index)
133
134 def __str__(self):
135 return self.object_id()
136
137 def object_id(self):
138 return "BD-Port-%s-%s" % (self.bd, self.itf)
139
140
141class VppBridgeDomainArpEntry(VppObject):
142
143 def __init__(self, test, bd, mac, ip):
144 self._test = test
145 self.bd = bd
Ole Troan8006c6a2018-12-17 12:02:26 +0100146 self.mac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700147 self.ip = VppIpAddress(ip)
148
149 def add_vpp_config(self):
150 self._test.vapi.bd_ip_mac_add_del(
151 self.bd.bd_id,
Ole Troan8006c6a2018-12-17 12:02:26 +0100152 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700153 self.ip.encode(),
154 is_add=1)
155 self._test.registry.register(self, self._test.logger)
156
157 def remove_vpp_config(self):
158 self._test.vapi.bd_ip_mac_add_del(
159 self.bd.bd_id,
Ole Troan8006c6a2018-12-17 12:02:26 +0100160 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700161 self.ip.encode(),
162 is_add=0)
163
164 def query_vpp_config(self):
165 return find_bridge_domain_arp_entry(self._test,
166 self.bd.bd_id,
Ole Troan8006c6a2018-12-17 12:02:26 +0100167 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700168 self.ip.address)
169
170 def __str__(self):
171 return self.object_id()
172
173 def object_id(self):
174 return "BD-Arp-Entry-%s-%s-%s" % (self.bd, self.mac, self.ip.address)
175
176
177class VppL2FibEntry(VppObject):
178
179 def __init__(self, test, bd, mac, itf,
180 static_mac=0, filter_mac=0, bvi_mac=-1):
181 self._test = test
182 self.bd = bd
Ole Troan8006c6a2018-12-17 12:02:26 +0100183 self.mac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700184 self.itf = itf
185 self.static_mac = static_mac
186 self.filter_mac = filter_mac
187 if bvi_mac == -1:
188 self.bvi_mac = isinstance(self.itf, VppLoInterface)
189 else:
190 self.bvi_mac = bvi_mac
191
192 def add_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=1,
198 static_mac=self.static_mac,
199 filter_mac=self.filter_mac,
200 bvi_mac=self.bvi_mac)
201 self._test.registry.register(self, self._test.logger)
202
203 def remove_vpp_config(self):
204 self._test.vapi.l2fib_add_del(
Ole Troan8006c6a2018-12-17 12:02:26 +0100205 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700206 self.bd.bd_id,
207 self.itf.sw_if_index,
208 is_add=0)
209
210 def query_vpp_config(self):
211 return find_l2_fib_entry(self._test,
212 self.bd.bd_id,
Ole Troan8006c6a2018-12-17 12:02:26 +0100213 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700214 self.itf.sw_if_index)
215
216 def __str__(self):
217 return self.object_id()
218
219 def object_id(self):
220 return "L2-Fib-Entry-%s-%s-%s" % (self.bd, self.mac, self.itf)