blob: 72f14dcfa3c9e5920640733b7ebc887ca337e195 [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 \
Ole Troan9a475372019-03-05 16:58:24 +010054 vip.bytes == arp.ip_address[:n]:
Neale Ranns93cc3ee2018-10-10 07:22:51 -070055 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):
Ole Troan9a475372019-03-05 16:58:24 +0100150 self._test.vapi.bd_ip_mac_add_del(bd_id=self.bd.bd_id, is_add=1,
151 ip=self.ip.encode(),
152 mac=self.mac.packed)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700153 self._test.registry.register(self, self._test.logger)
154
155 def remove_vpp_config(self):
Ole Troan9a475372019-03-05 16:58:24 +0100156 self._test.vapi.bd_ip_mac_add_del(bd_id=self.bd.bd_id, is_add=0,
157 ip=self.ip.encode(),
158 mac=self.mac.packed)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700159
160 def query_vpp_config(self):
161 return find_bridge_domain_arp_entry(self._test,
162 self.bd.bd_id,
Ole Troan8006c6a2018-12-17 12:02:26 +0100163 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700164 self.ip.address)
165
166 def __str__(self):
167 return self.object_id()
168
169 def object_id(self):
170 return "BD-Arp-Entry-%s-%s-%s" % (self.bd, self.mac, self.ip.address)
171
172
173class VppL2FibEntry(VppObject):
174
175 def __init__(self, test, bd, mac, itf,
176 static_mac=0, filter_mac=0, bvi_mac=-1):
177 self._test = test
178 self.bd = bd
Ole Troan8006c6a2018-12-17 12:02:26 +0100179 self.mac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700180 self.itf = itf
181 self.static_mac = static_mac
182 self.filter_mac = filter_mac
183 if bvi_mac == -1:
184 self.bvi_mac = isinstance(self.itf, VppLoInterface)
185 else:
186 self.bvi_mac = bvi_mac
187
188 def add_vpp_config(self):
189 self._test.vapi.l2fib_add_del(
Ole Troan8006c6a2018-12-17 12:02:26 +0100190 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700191 self.bd.bd_id,
192 self.itf.sw_if_index,
193 is_add=1,
194 static_mac=self.static_mac,
195 filter_mac=self.filter_mac,
196 bvi_mac=self.bvi_mac)
197 self._test.registry.register(self, self._test.logger)
198
199 def remove_vpp_config(self):
200 self._test.vapi.l2fib_add_del(
Ole Troan8006c6a2018-12-17 12:02:26 +0100201 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700202 self.bd.bd_id,
203 self.itf.sw_if_index,
204 is_add=0)
205
206 def query_vpp_config(self):
207 return find_l2_fib_entry(self._test,
208 self.bd.bd_id,
Ole Troan8006c6a2018-12-17 12:02:26 +0100209 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700210 self.itf.sw_if_index)
211
212 def __str__(self):
213 return self.object_id()
214
215 def object_id(self):
216 return "L2-Fib-Entry-%s-%s-%s" % (self.bd, self.mac, self.itf)