blob: 45b7d69dd01ba8f3a0d039666759a3fea496373f [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):
Ole Troana5b2eec2019-03-11 19:23:25 +010082 self._test.vapi.bridge_domain_add_del(bd_id=self.bd_id,
83 flood=self.flood,
84 uu_flood=self.uu_flood,
85 forward=self.forward,
86 learn=self.learn,
87 arp_term=self.arp_term, is_add=1)
Neale Ranns93cc3ee2018-10-10 07:22:51 -070088 self._test.registry.register(self, self._test.logger)
89
90 def remove_vpp_config(self):
Ole Troana5b2eec2019-03-11 19:23:25 +010091 self._test.vapi.bridge_domain_add_del(bd_id=self.bd_id, is_add=0)
Neale Ranns93cc3ee2018-10-10 07:22:51 -070092
93 def query_vpp_config(self):
94 return find_bridge_domain(self._test, self.bd_id)
95
96 def __str__(self):
97 return self.object_id()
98
99 def object_id(self):
100 return "bridge-domain-%d" % (self.bd_id)
101
102
103class VppBridgeDomainPort(VppObject):
104
105 def __init__(self, test, bd, itf,
106 port_type=L2_PORT_TYPE.NORMAL):
107 self._test = test
108 self.bd = bd
109 self.itf = itf
110 self.port_type = port_type
111
112 def add_vpp_config(self):
113 self._test.vapi.sw_interface_set_l2_bridge(
Ole Troana5b2eec2019-03-11 19:23:25 +0100114 rx_sw_if_index=self.itf.sw_if_index, bd_id=self.bd.bd_id,
115 port_type=self.port_type, enable=1)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700116 self._test.registry.register(self, self._test.logger)
117
118 def remove_vpp_config(self):
119 self._test.vapi.sw_interface_set_l2_bridge(
Ole Troana5b2eec2019-03-11 19:23:25 +0100120 rx_sw_if_index=self.itf.sw_if_index, bd_id=self.bd.bd_id,
121 port_type=self.port_type, enable=0)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700122
123 def query_vpp_config(self):
124 return find_bridge_domain_port(self._test,
125 self.bd.bd_id,
126 self.itf.sw_if_index)
127
128 def __str__(self):
129 return self.object_id()
130
131 def object_id(self):
132 return "BD-Port-%s-%s" % (self.bd, self.itf)
133
134
135class VppBridgeDomainArpEntry(VppObject):
136
137 def __init__(self, test, bd, mac, ip):
138 self._test = test
139 self.bd = bd
Ole Troan8006c6a2018-12-17 12:02:26 +0100140 self.mac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700141 self.ip = VppIpAddress(ip)
142
143 def add_vpp_config(self):
Ole Troan9a475372019-03-05 16:58:24 +0100144 self._test.vapi.bd_ip_mac_add_del(bd_id=self.bd.bd_id, is_add=1,
145 ip=self.ip.encode(),
146 mac=self.mac.packed)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700147 self._test.registry.register(self, self._test.logger)
148
149 def remove_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=0,
151 ip=self.ip.encode(),
152 mac=self.mac.packed)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700153
154 def query_vpp_config(self):
155 return find_bridge_domain_arp_entry(self._test,
156 self.bd.bd_id,
Ole Troan8006c6a2018-12-17 12:02:26 +0100157 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700158 self.ip.address)
159
160 def __str__(self):
161 return self.object_id()
162
163 def object_id(self):
164 return "BD-Arp-Entry-%s-%s-%s" % (self.bd, self.mac, self.ip.address)
165
166
167class VppL2FibEntry(VppObject):
168
169 def __init__(self, test, bd, mac, itf,
170 static_mac=0, filter_mac=0, bvi_mac=-1):
171 self._test = test
172 self.bd = bd
Ole Troan8006c6a2018-12-17 12:02:26 +0100173 self.mac = MACAddress(mac)
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700174 self.itf = itf
175 self.static_mac = static_mac
176 self.filter_mac = filter_mac
177 if bvi_mac == -1:
178 self.bvi_mac = isinstance(self.itf, VppLoInterface)
179 else:
180 self.bvi_mac = bvi_mac
181
182 def add_vpp_config(self):
183 self._test.vapi.l2fib_add_del(
Ole Troan8006c6a2018-12-17 12:02:26 +0100184 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700185 self.bd.bd_id,
186 self.itf.sw_if_index,
187 is_add=1,
188 static_mac=self.static_mac,
189 filter_mac=self.filter_mac,
190 bvi_mac=self.bvi_mac)
191 self._test.registry.register(self, self._test.logger)
192
193 def remove_vpp_config(self):
194 self._test.vapi.l2fib_add_del(
Ole Troan8006c6a2018-12-17 12:02:26 +0100195 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700196 self.bd.bd_id,
197 self.itf.sw_if_index,
198 is_add=0)
199
200 def query_vpp_config(self):
201 return find_l2_fib_entry(self._test,
202 self.bd.bd_id,
Ole Troan8006c6a2018-12-17 12:02:26 +0100203 self.mac.packed,
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700204 self.itf.sw_if_index)
205
206 def __str__(self):
207 return self.object_id()
208
209 def object_id(self):
210 return "L2-Fib-Entry-%s-%s-%s" % (self.bd, self.mac, self.itf)