blob: fb964ec8a64888a9eb23d8dd1092311a97752ccf [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Jan4af521d2016-11-15 17:05:00 +01002"""L2 FIB Test Case HLD:
3
4**config 1**
5 - add 4 pg-l2 interfaces
6 - configure them into l2bd
7 - configure 100 MAC entries in L2 fib - 25 MACs per interface
8 - L2 MAC learning and unknown unicast flooding disabled in l2bd
9 - configure 100 MAC entries in L2 fib - 25 MACs per interface
10
11**test 1**
12 - send L2 MAC frames between all 4 pg-l2 interfaces for all of 100 MAC \
13 entries in the FIB
14
15**verify 1**
16 - all packets received correctly
17
18**config 2**
19 - delete 12 MAC entries - 3 MACs per interface
20
21**test 2a**
22 - send L2 MAC frames between all 4 pg-l2 interfaces for non-deleted MAC \
23 entries
24
25**verify 2a**
26 - all packets received correctly
27
28**test 2b**
29 - send L2 MAC frames between all 4 pg-l2 interfaces for all of 12 deleted \
30 MAC entries
31
32**verify 2b**
33 - no packet received on all 4 pg-l2 interfaces
34
35**config 3**
36 - configure new 100 MAC entries in L2 fib - 25 MACs per interface
37
38**test 3**
39 - send L2 MAC frames between all 4 pg-l2 interfaces for all of 188 MAC \
40 entries in the FIB
41
42**verify 3**
43 - all packets received correctly
44
45**config 4**
46 - delete 160 MAC entries, 40 MACs per interface
47
48**test 4a**
49 - send L2 MAC frames between all 4 pg-l2 interfaces for all of 28 \
50 non-deleted MAC entries
51
52**verify 4a**
53 - all packets received correctly
54
55**test 4b**
56 - try send L2 MAC frames between all 4 pg-l2 interfaces for all of 172 \
57 deleted MAC entries
58
59**verify 4b**
60 - no packet received on all 4 pg-l2 interfaces
61"""
62
63import unittest
64import random
65
66from scapy.packet import Raw
67from scapy.layers.l2 import Ether
68from scapy.layers.inet import IP, UDP
69
Dave Wallace8800f732023-08-31 00:47:44 -040070from framework import VppTestCase
71from asfframework import VppTestRunner
Klement Sekera9225dee2016-12-12 08:36:58 +010072from util import Host, ppp
Jakub Grajciar145e3302019-10-24 13:52:42 +020073from vpp_papi import mac_pton, VppEnum
John Loe23c99e2018-03-13 21:53:18 -040074
Jan4af521d2016-11-15 17:05:00 +010075
76class TestL2fib(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020077 """L2 FIB Test Case"""
Jan4af521d2016-11-15 17:05:00 +010078
79 @classmethod
Eyal Bari93b503e2017-05-15 10:13:15 +030080 def bd_ifs(cls, bd_id):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020081 return range((bd_id - 1) * cls.n_ifs_per_bd, bd_id * cls.n_ifs_per_bd - 1)
Eyal Bari93b503e2017-05-15 10:13:15 +030082
83 @classmethod
Jan4af521d2016-11-15 17:05:00 +010084 def setUpClass(cls):
85 """
86 Perform standard class setup (defined by class method setUpClass in
87 class VppTestCase) before running the test case, set test case related
88 variables and configure VPP.
89
90 :var int bd_id: Bridge domain ID.
Jan4af521d2016-11-15 17:05:00 +010091 """
92 super(TestL2fib, cls).setUpClass()
93
Jan4af521d2016-11-15 17:05:00 +010094 try:
Eyal Bari93b503e2017-05-15 10:13:15 +030095 n_brs = cls.n_brs = range(1, 3)
96 cls.n_ifs_per_bd = 4
97 n_ifs = range(cls.n_ifs_per_bd * len(cls.n_brs))
Pavel Kotucek7303ee82018-11-21 13:20:41 +010098 # Create pg interfaces
Eyal Bari93b503e2017-05-15 10:13:15 +030099 cls.create_pg_interfaces(n_ifs)
Jan4af521d2016-11-15 17:05:00 +0100100
Jan4af521d2016-11-15 17:05:00 +0100101 cls.flows = dict()
Eyal Bari93b503e2017-05-15 10:13:15 +0300102 for bd_id in n_brs:
103 # Packet flows mapping pg0 -> pg1, pg2, pg3 etc.
104 ifs = cls.bd_ifs(bd_id)
105 for j in ifs:
106 cls.flows[cls.pg_interfaces[j]] = [
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200107 cls.pg_interfaces[x] for x in ifs if x != j
108 ]
Jan4af521d2016-11-15 17:05:00 +0100109
110 # Packet sizes
111 cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
112
Eyal Bari93b503e2017-05-15 10:13:15 +0300113 for bd_id in n_brs:
114 # Create BD with MAC learning and unknown unicast flooding
115 # disabled and put interfaces to this BD
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +0200116 cls.vapi.bridge_domain_add_del_v2(
117 bd_id=bd_id, is_add=1, uu_flood=0, learn=0, flood=1, forward=1
118 )
Eyal Bari93b503e2017-05-15 10:13:15 +0300119 ifs = [cls.pg_interfaces[i] for i in cls.bd_ifs(bd_id)]
120 for pg_if in ifs:
Ole Troana5b2eec2019-03-11 19:23:25 +0100121 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200122 rx_sw_if_index=pg_if.sw_if_index, bd_id=bd_id
123 )
Jan4af521d2016-11-15 17:05:00 +0100124
125 # Set up all interfaces
126 for i in cls.pg_interfaces:
127 i.admin_up()
Jan4af521d2016-11-15 17:05:00 +0100128 except Exception:
129 super(TestL2fib, cls).tearDownClass()
130 raise
131
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700132 @classmethod
133 def tearDownClass(cls):
134 super(TestL2fib, cls).tearDownClass()
135
Jan4af521d2016-11-15 17:05:00 +0100136 def setUp(self):
Jan4af521d2016-11-15 17:05:00 +0100137 super(TestL2fib, self).setUp()
Klement Sekeradab231a2016-12-21 08:50:14 +0100138 self.reset_packet_infos()
Jan4af521d2016-11-15 17:05:00 +0100139
140 def tearDown(self):
141 """
142 Show various debug prints after each test.
143 """
144 super(TestL2fib, self).tearDown()
145 if not self.vpp_dead:
Eyal Bari93b503e2017-05-15 10:13:15 +0300146 for bd_id in self.n_brs:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200147 self.logger.info(
148 self.vapi.ppcli("show bridge-domain %s detail" % bd_id)
149 )
Jan4af521d2016-11-15 17:05:00 +0100150
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700151 def show_commands_at_teardown(self):
152 self.logger.info(self.vapi.ppcli("show l2fib verbose"))
153
Eyal Bari93b503e2017-05-15 10:13:15 +0300154 def create_hosts(self, n_hosts_per_if, subnet):
Jan4af521d2016-11-15 17:05:00 +0100155 """
156 Create required number of host MAC addresses and distribute them among
157 interfaces. Create host IPv4 address for every host MAC address.
158
Eyal Bari93b503e2017-05-15 10:13:15 +0300159 :param int n_hosts_per_if: Number of per interface hosts to
Dave Wallaced1706812021-08-12 18:36:02 -0400160 create MAC/IPv4 addresses for.
Jan4af521d2016-11-15 17:05:00 +0100161 """
Eyal Bari93b503e2017-05-15 10:13:15 +0300162
Eyal Bari755b1402017-10-08 15:53:34 +0300163 hosts = dict()
Jan4af521d2016-11-15 17:05:00 +0100164 for pg_if in self.pg_interfaces:
Eyal Bari93b503e2017-05-15 10:13:15 +0300165 swif = pg_if.sw_if_index
Jan4af521d2016-11-15 17:05:00 +0100166
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200167 def mac(j):
168 return "00:00:%02x:ff:%02x:%02x" % (subnet, swif, j)
Eyal Bari93b503e2017-05-15 10:13:15 +0300169
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200170 def ip(j):
171 return "172.%02u.1%02x.%u" % (subnet, swif, j)
Eyal Bari93b503e2017-05-15 10:13:15 +0300172
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200173 def h(j):
174 return Host(mac(j), ip(j))
175
Eyal Bari755b1402017-10-08 15:53:34 +0300176 hosts[swif] = [h(j) for j in range(n_hosts_per_if)]
177 return hosts
Eyal Bari93b503e2017-05-15 10:13:15 +0300178
Eyal Bari755b1402017-10-08 15:53:34 +0300179 def split_hosts(self, hosts, n):
180 splits = dict()
181 for pg_if in self.pg_interfaces:
182 swif = pg_if.sw_if_index
183 splits[swif] = hosts[swif][:n]
184 hosts[swif] = hosts[swif][n:]
185 return splits
186
187 def learn_hosts(self, bd_id, hosts):
Eyal Bari521202b2017-05-24 10:11:20 +0300188 """
Eyal Bari755b1402017-10-08 15:53:34 +0300189 Create and send per interface L2 MAC broadcast packet stream to
Eyal Bari521202b2017-05-24 10:11:20 +0300190 let the bridge domain learn these MAC addresses.
191
192 :param int bd_id: BD to teach
Eyal Bari755b1402017-10-08 15:53:34 +0300193 :param dict hosts: dict of hosts per interface
Eyal Bari521202b2017-05-24 10:11:20 +0300194 """
Ole Troana5b2eec2019-03-11 19:23:25 +0100195 self.vapi.bridge_flags(bd_id=bd_id, is_set=1, flags=1)
Eyal Bari521202b2017-05-24 10:11:20 +0300196 ifs = [self.pg_interfaces[i] for i in self.bd_ifs(bd_id)]
197 for pg_if in ifs:
198 swif = pg_if.sw_if_index
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200199 packets = [
200 Ether(dst="ff:ff:ff:ff:ff:ff", src=host.mac) for host in hosts[swif]
201 ]
Eyal Bari521202b2017-05-24 10:11:20 +0300202 pg_if.add_stream(packets)
203 self.logger.info("Sending broadcast eth frames for MAC learning")
204 self.pg_start()
205
Eyal Bari755b1402017-10-08 15:53:34 +0300206 def config_l2_fib_entries(self, bd_id, hosts):
Jan4af521d2016-11-15 17:05:00 +0100207 """
Eyal Bari93b503e2017-05-15 10:13:15 +0300208 Config required number of L2 FIB entries.
Jan4af521d2016-11-15 17:05:00 +0100209
Eyal Bari93b503e2017-05-15 10:13:15 +0300210 :param int bd_id: BD's id
Jan4af521d2016-11-15 17:05:00 +0100211 :param int count: Number of L2 FIB entries to be created.
212 :param int start: Starting index of the host list. (Default value = 0)
213 """
Eyal Bari93b503e2017-05-15 10:13:15 +0300214 ifs = [self.pg_interfaces[i] for i in self.bd_ifs(bd_id)]
215 for pg_if in ifs:
216 swif = pg_if.sw_if_index
Eyal Bari755b1402017-10-08 15:53:34 +0300217 for host in hosts[swif]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200218 self.vapi.l2fib_add_del(mac_pton(host.mac), bd_id, swif, static_mac=1)
Jan4af521d2016-11-15 17:05:00 +0100219
Eyal Bari755b1402017-10-08 15:53:34 +0300220 def delete_l2_fib_entry(self, bd_id, hosts):
Jan4af521d2016-11-15 17:05:00 +0100221 """
222 Delete required number of L2 FIB entries.
223
224 :param int count: Number of L2 FIB entries to be created.
225 """
Eyal Bari93b503e2017-05-15 10:13:15 +0300226 ifs = [self.pg_interfaces[i] for i in self.bd_ifs(bd_id)]
227 for pg_if in ifs:
228 swif = pg_if.sw_if_index
Eyal Bari755b1402017-10-08 15:53:34 +0300229 for host in hosts[swif]:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200230 self.vapi.l2fib_add_del(mac_pton(host.mac), bd_id, swif, is_add=0)
Eyal Bari93b503e2017-05-15 10:13:15 +0300231
Eyal Bari755b1402017-10-08 15:53:34 +0300232 def flush_int(self, swif, learned_hosts):
Eyal Bari93b503e2017-05-15 10:13:15 +0300233 """
234 Flush swif L2 FIB entries.
235
236 :param int swif: sw if index.
237 """
Eyal Bari521202b2017-05-24 10:11:20 +0300238 flushed = dict()
Eyal Bari93b503e2017-05-15 10:13:15 +0300239 self.vapi.l2fib_flush_int(swif)
Eyal Bari755b1402017-10-08 15:53:34 +0300240 flushed[swif] = learned_hosts[swif]
241 learned_hosts[swif] = []
Eyal Bari521202b2017-05-24 10:11:20 +0300242 return flushed
Eyal Bari93b503e2017-05-15 10:13:15 +0300243
Eyal Bari755b1402017-10-08 15:53:34 +0300244 def flush_bd(self, bd_id, learned_hosts):
Eyal Bari93b503e2017-05-15 10:13:15 +0300245 """
246 Flush bd_id L2 FIB entries.
247
248 :param int bd_id: Bridge Domain id.
249 """
250 self.vapi.l2fib_flush_bd(bd_id)
Eyal Bari521202b2017-05-24 10:11:20 +0300251 flushed = dict()
Eyal Bari93b503e2017-05-15 10:13:15 +0300252 ifs = [self.pg_interfaces[i] for i in self.bd_ifs(bd_id)]
253 for pg_if in ifs:
254 swif = pg_if.sw_if_index
Eyal Bari755b1402017-10-08 15:53:34 +0300255 flushed[swif] = learned_hosts[swif]
256 learned_hosts[swif] = []
Eyal Bari521202b2017-05-24 10:11:20 +0300257 return flushed
Eyal Bari93b503e2017-05-15 10:13:15 +0300258
259 def flush_all(self):
260 """
261 Flush All L2 FIB entries.
262 """
263 self.vapi.l2fib_flush_all()
Jan4af521d2016-11-15 17:05:00 +0100264
Eyal Bari755b1402017-10-08 15:53:34 +0300265 def create_stream(self, src_if, packet_sizes, if_src_hosts, if_dst_hosts):
Jan4af521d2016-11-15 17:05:00 +0100266 """
267 Create input packet stream for defined interface using hosts or
268 deleted_hosts list.
269
270 :param object src_if: Interface to create packet stream for.
271 :param list packet_sizes: List of required packet sizes.
272 :param boolean deleted: Set to True if deleted_hosts list required.
273 :return: Stream of packets.
274 """
Eyal Bari521202b2017-05-24 10:11:20 +0300275 src_hosts = if_src_hosts[src_if.sw_if_index]
Eyal Bari93b503e2017-05-15 10:13:15 +0300276 if not src_hosts:
277 return []
Jan4af521d2016-11-15 17:05:00 +0100278 pkts = []
Jan4af521d2016-11-15 17:05:00 +0100279 for dst_if in self.flows[src_if]:
Eyal Bari521202b2017-05-24 10:11:20 +0300280 dst_swif = dst_if.sw_if_index
281 if dst_swif not in if_dst_hosts:
282 continue
283 dst_hosts = if_dst_hosts[dst_swif]
Eyal Bari755b1402017-10-08 15:53:34 +0300284 for dst_host in dst_hosts:
Jan4af521d2016-11-15 17:05:00 +0100285 src_host = random.choice(src_hosts)
Klement Sekeradab231a2016-12-21 08:50:14 +0100286 pkt_info = self.create_packet_info(src_if, dst_if)
Jan4af521d2016-11-15 17:05:00 +0100287 payload = self.info_to_payload(pkt_info)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200288 p = (
289 Ether(dst=dst_host.mac, src=src_host.mac)
290 / IP(src=src_host.ip4, dst=dst_host.ip4)
291 / UDP(sport=1234, dport=1234)
292 / Raw(payload)
293 )
Jan4af521d2016-11-15 17:05:00 +0100294 pkt_info.data = p.copy()
295 size = random.choice(packet_sizes)
296 self.extend_packet(p, size)
297 pkts.append(p)
298 return pkts
299
300 def verify_capture(self, pg_if, capture):
301 """
302 Verify captured input packet stream for defined interface.
303
304 :param object pg_if: Interface to verify captured packet stream for.
305 :param list capture: Captured packet stream.
306 """
307 last_info = dict()
308 for i in self.pg_interfaces:
309 last_info[i.sw_if_index] = None
310 dst_sw_if_index = pg_if.sw_if_index
311 for packet in capture:
Paul Vinciguerraeaea4212019-03-06 11:58:06 -0800312 payload_info = self.payload_to_info(packet[Raw])
Jan4af521d2016-11-15 17:05:00 +0100313 try:
314 ip = packet[IP]
315 udp = packet[UDP]
316 packet_index = payload_info.index
317 self.assertEqual(payload_info.dst, dst_sw_if_index)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200318 self.logger.debug(
319 "Got packet on port %s: src=%u (id=%u)"
320 % (pg_if.name, payload_info.src, packet_index)
321 )
Jan4af521d2016-11-15 17:05:00 +0100322 next_info = self.get_next_packet_info_for_interface2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200323 payload_info.src, dst_sw_if_index, last_info[payload_info.src]
324 )
Jan4af521d2016-11-15 17:05:00 +0100325 last_info[payload_info.src] = next_info
326 self.assertTrue(next_info is not None)
327 self.assertEqual(packet_index, next_info.index)
328 saved_packet = next_info.data
329 # Check standard fields
330 self.assertEqual(ip.src, saved_packet[IP].src)
331 self.assertEqual(ip.dst, saved_packet[IP].dst)
332 self.assertEqual(udp.sport, saved_packet[UDP].sport)
333 self.assertEqual(udp.dport, saved_packet[UDP].dport)
Jakub Grajciar145e3302019-10-24 13:52:42 +0200334 except BaseException:
Klement Sekera9225dee2016-12-12 08:36:58 +0100335 self.logger.error(ppp("Unexpected or invalid packet:", packet))
Jan4af521d2016-11-15 17:05:00 +0100336 raise
337 for i in self.pg_interfaces:
338 remaining_packet = self.get_next_packet_info_for_interface2(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200339 i, dst_sw_if_index, last_info[i.sw_if_index]
340 )
Jan4af521d2016-11-15 17:05:00 +0100341 self.assertTrue(
342 remaining_packet is None,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200343 "Port %u: Packet expected from source %u didn't arrive"
344 % (dst_sw_if_index, i.sw_if_index),
345 )
Jan4af521d2016-11-15 17:05:00 +0100346
Eyal Bari755b1402017-10-08 15:53:34 +0300347 def run_verify_test(self, bd_id, src_hosts, dst_hosts):
Jan4af521d2016-11-15 17:05:00 +0100348 # Test
349 # Create incoming packet streams for packet-generator interfaces
Eyal Bari93b503e2017-05-15 10:13:15 +0300350 self.reset_packet_infos()
351 ifs = [self.pg_interfaces[i] for i in self.bd_ifs(bd_id)]
352 for i in ifs:
Eyal Bari521202b2017-05-24 10:11:20 +0300353 pkts = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200354 i,
355 self.pg_if_packet_sizes,
Eyal Bari755b1402017-10-08 15:53:34 +0300356 if_src_hosts=src_hosts,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200357 if_dst_hosts=dst_hosts,
358 )
Eyal Bari755b1402017-10-08 15:53:34 +0300359 if pkts:
360 i.add_stream(pkts)
Jan4af521d2016-11-15 17:05:00 +0100361
Ole Troana5b2eec2019-03-11 19:23:25 +0100362 self.vapi.bridge_flags(bd_id=bd_id, is_set=0, flags=1)
Jan4af521d2016-11-15 17:05:00 +0100363 # Enable packet capture and start packet sending
Eyal Bari93b503e2017-05-15 10:13:15 +0300364 self.pg_enable_capture(ifs)
Jan4af521d2016-11-15 17:05:00 +0100365 self.pg_start()
366
367 # Verify
368 # Verify outgoing packet streams per packet-generator interface
Eyal Bari93b503e2017-05-15 10:13:15 +0300369 for i in ifs:
Eyal Bari521202b2017-05-24 10:11:20 +0300370 if not dst_hosts[i.sw_if_index]:
371 continue
Jan4af521d2016-11-15 17:05:00 +0100372 capture = i.get_capture()
373 self.logger.info("Verifying capture on interface %s" % i.name)
374 self.verify_capture(i, capture)
375
Eyal Bari755b1402017-10-08 15:53:34 +0300376 def run_verify_negat_test(self, bd_id, src_hosts, dst_hosts):
Jan4af521d2016-11-15 17:05:00 +0100377 # Test
378 # Create incoming packet streams for packet-generator interfaces for
379 # deleted MAC addresses
Klement Sekeradab231a2016-12-21 08:50:14 +0100380 self.reset_packet_infos()
Eyal Bari93b503e2017-05-15 10:13:15 +0300381 ifs = [self.pg_interfaces[i] for i in self.bd_ifs(bd_id)]
382 for i in ifs:
Eyal Bari521202b2017-05-24 10:11:20 +0300383 pkts = self.create_stream(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200384 i,
385 self.pg_if_packet_sizes,
Eyal Bari755b1402017-10-08 15:53:34 +0300386 if_src_hosts=src_hosts,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200387 if_dst_hosts=dst_hosts,
388 )
Eyal Bari93b503e2017-05-15 10:13:15 +0300389 if pkts:
390 i.add_stream(pkts)
Jan4af521d2016-11-15 17:05:00 +0100391
Ole Troana5b2eec2019-03-11 19:23:25 +0100392 self.vapi.bridge_flags(bd_id=bd_id, is_set=0, flags=1)
Jan4af521d2016-11-15 17:05:00 +0100393 # Enable packet capture and start packet sending
Eyal Bari93b503e2017-05-15 10:13:15 +0300394 self.pg_enable_capture(ifs)
Jan4af521d2016-11-15 17:05:00 +0100395 self.pg_start()
396
397 # Verify
398 # Verify outgoing packet streams per packet-generator interface
Neale Ranns4b8d3be2017-05-22 02:46:01 -0700399 timeout = 1
Eyal Bari93b503e2017-05-15 10:13:15 +0300400 for i in ifs:
Neale Ranns4b8d3be2017-05-22 02:46:01 -0700401 i.get_capture(0, timeout=timeout)
Klement Sekera9225dee2016-12-12 08:36:58 +0100402 i.assert_nothing_captured(remark="outgoing interface")
Neale Ranns4b8d3be2017-05-22 02:46:01 -0700403 timeout = 0.1
Jan4af521d2016-11-15 17:05:00 +0100404
Eyal Bari755b1402017-10-08 15:53:34 +0300405 def test_l2_fib_program100(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200406 """L2 FIB - program 100 MACs"""
Eyal Bari755b1402017-10-08 15:53:34 +0300407 bd_id = 1
408 hosts = self.create_hosts(100, subnet=17)
409 self.config_l2_fib_entries(bd_id, hosts)
410 self.run_verify_test(bd_id, hosts, hosts)
Jan4af521d2016-11-15 17:05:00 +0100411
Pavel Kotucek7303ee82018-11-21 13:20:41 +0100412 def test_l2_fib_program100_delete12(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200413 """L2 FIB - program 100, delete 12 MACs"""
Eyal Bari755b1402017-10-08 15:53:34 +0300414 bd_id = 1
415 hosts = self.create_hosts(100, subnet=17)
416 self.config_l2_fib_entries(bd_id, hosts)
417 del_hosts = self.split_hosts(hosts, 12)
418 self.delete_l2_fib_entry(bd_id, del_hosts)
Jan4af521d2016-11-15 17:05:00 +0100419
Eyal Bari755b1402017-10-08 15:53:34 +0300420 self.run_verify_test(bd_id, hosts, hosts)
421 self.run_verify_negat_test(bd_id, hosts, del_hosts)
Jan4af521d2016-11-15 17:05:00 +0100422
Pavel Kotucek7303ee82018-11-21 13:20:41 +0100423 def test_l2_fib_program100_add100(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200424 """L2 FIB - program 100, add 100 MACs"""
Eyal Bari755b1402017-10-08 15:53:34 +0300425 bd_id = 1
426 hosts = self.create_hosts(100, subnet=17)
427 self.config_l2_fib_entries(bd_id, hosts)
428 hosts2 = self.create_hosts(100, subnet=22)
429 self.config_l2_fib_entries(bd_id, hosts2)
430 self.run_verify_test(bd_id, hosts, hosts2)
Jan4af521d2016-11-15 17:05:00 +0100431
Eyal Bari755b1402017-10-08 15:53:34 +0300432 def test_l2_fib_program10_learn10(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200433 """L2 FIB - program 10 MACs, learn 10"""
Eyal Bari755b1402017-10-08 15:53:34 +0300434 hosts = self.create_hosts(20, subnet=35)
435 lhosts = self.split_hosts(hosts, 10)
Jan4af521d2016-11-15 17:05:00 +0100436
Eyal Bari755b1402017-10-08 15:53:34 +0300437 bd1 = 1
438 bd2 = 2
439 self.learn_hosts(bd1, lhosts)
440 self.learn_hosts(bd2, lhosts)
441 self.config_l2_fib_entries(bd1, hosts)
442 self.config_l2_fib_entries(bd2, hosts)
443 self.run_verify_test(bd1, lhosts, hosts)
444 self.run_verify_test(bd2, lhosts, hosts)
Eyal Bari93b503e2017-05-15 10:13:15 +0300445
Eyal Bari755b1402017-10-08 15:53:34 +0300446 def test_l2_fib_flush_int(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200447 """L2 FIB - flush interface"""
Eyal Bari755b1402017-10-08 15:53:34 +0300448 hosts = self.create_hosts(20, subnet=36)
449 lhosts = self.split_hosts(hosts, 10)
Eyal Bari93b503e2017-05-15 10:13:15 +0300450
Eyal Bari755b1402017-10-08 15:53:34 +0300451 bd1 = 1
452 self.learn_hosts(bd1, lhosts)
453 self.config_l2_fib_entries(bd1, hosts)
454 self.run_verify_test(bd1, lhosts, hosts)
455 flushed = self.flush_int(self.pg_interfaces[0].sw_if_index, lhosts)
456 self.run_verify_test(bd1, hosts, lhosts)
457 self.run_verify_negat_test(bd1, hosts, flushed)
Eyal Bari521202b2017-05-24 10:11:20 +0300458
Eyal Bari755b1402017-10-08 15:53:34 +0300459 def test_l2_fib_flush_bd(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200460 """L2 FIB - flush BD"""
Eyal Bari755b1402017-10-08 15:53:34 +0300461 hosts = self.create_hosts(20, subnet=37)
462 lhosts = self.split_hosts(hosts, 10)
Eyal Bari521202b2017-05-24 10:11:20 +0300463
Eyal Bari755b1402017-10-08 15:53:34 +0300464 bd1 = 1
465 self.learn_hosts(bd1, lhosts)
466 self.config_l2_fib_entries(bd1, hosts)
467 self.run_verify_test(bd1, lhosts, hosts)
468 flushed = self.flush_bd(bd1, lhosts)
469 self.run_verify_negat_test(bd1, hosts, flushed)
Eyal Bari521202b2017-05-24 10:11:20 +0300470
Eyal Bari755b1402017-10-08 15:53:34 +0300471 def test_l2_fib_flush_all(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200472 """L2 FIB - flush all"""
Eyal Bari755b1402017-10-08 15:53:34 +0300473 hosts = self.create_hosts(20, subnet=38)
474 lhosts = self.split_hosts(hosts, 10)
Eyal Bari521202b2017-05-24 10:11:20 +0300475
Eyal Bari755b1402017-10-08 15:53:34 +0300476 bd1 = 1
477 bd2 = 2
478 self.learn_hosts(bd1, lhosts)
479 self.learn_hosts(bd2, lhosts)
480 self.config_l2_fib_entries(bd1, hosts)
481 self.config_l2_fib_entries(bd2, hosts)
482 self.run_verify_test(bd1, hosts, lhosts)
483 self.run_verify_test(bd2, hosts, lhosts)
Eyal Bari521202b2017-05-24 10:11:20 +0300484
Eyal Bari755b1402017-10-08 15:53:34 +0300485 self.flush_all()
486
487 self.run_verify_negat_test(bd1, hosts, lhosts)
488 self.run_verify_negat_test(bd2, hosts, lhosts)
489
490 def test_l2_fib_mac_learn_evs(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200491 """L2 FIB - mac learning events"""
Eyal Bari755b1402017-10-08 15:53:34 +0300492 bd1 = 1
493 hosts = self.create_hosts(10, subnet=39)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300494
Ole Troane1ade682019-03-04 23:55:43 +0100495 self.vapi.want_l2_macs_events()
Eyal Bari755b1402017-10-08 15:53:34 +0300496 self.learn_hosts(bd1, hosts)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300497
Benoît Ganne56eccdb2021-08-20 09:18:31 +0200498 self.virtual_sleep(1)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300499 self.logger.info(self.vapi.ppcli("show l2fib"))
500 evs = self.vapi.collect_events()
Jakub Grajciar145e3302019-10-24 13:52:42 +0200501 action = VppEnum.vl_api_mac_event_action_t.MAC_EVENT_ACTION_API_ADD
Eyal Bari24db0ec2017-09-27 21:43:51 +0300502 learned_macs = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200503 e.mac[i].mac_addr.packed
504 for e in evs
505 for i in range(e.n_macs)
506 if e.mac[i].action == action
507 }
508 macs = {
509 h.bin_mac
510 for swif in self.bd_ifs(bd1)
511 for h in hosts[self.pg_interfaces[swif].sw_if_index]
512 }
Ole Troane1ade682019-03-04 23:55:43 +0100513 self.vapi.want_l2_macs_events(enable_disable=0)
Eyal Bari24db0ec2017-09-27 21:43:51 +0300514 self.assertEqual(len(learned_macs ^ macs), 0)
515
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100516 def test_l2_fib_mac_learn_evs2(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200517 """L2 FIB - mac learning events using want_l2_macs_events2"""
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100518 bd1 = 1
519 hosts = self.create_hosts(10, subnet=39)
520
521 self.vapi.l2fib_set_scan_delay(scan_delay=10)
522 self.vapi.want_l2_macs_events2()
523 self.sleep(1)
524 self.learn_hosts(bd1, hosts)
525
Benoît Ganne56eccdb2021-08-20 09:18:31 +0200526 self.virtual_sleep(1)
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100527 self.logger.info(self.vapi.ppcli("show l2fib"))
528 evs = self.vapi.collect_events()
529 action = VppEnum.vl_api_mac_event_action_t.MAC_EVENT_ACTION_API_ADD
530 learned_macs = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200531 e.mac[i].mac_addr.packed
532 for e in evs
533 for i in range(e.n_macs)
534 if e.mac[i].action == action
535 }
536 macs = {
537 h.bin_mac
538 for swif in self.bd_ifs(bd1)
539 for h in hosts[self.pg_interfaces[swif].sw_if_index]
540 }
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100541 self.vapi.want_l2_macs_events2(enable_disable=0)
542 self.assertEqual(len(learned_macs ^ macs), 0)
543
Eyal Bari755b1402017-10-08 15:53:34 +0300544 def test_l2_fib_macs_learn_max(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200545 """L2 FIB - mac learning max macs in event"""
Eyal Bari755b1402017-10-08 15:53:34 +0300546 bd1 = 1
547 hosts = self.create_hosts(10, subnet=40)
eyal baric6038c92017-10-03 12:25:07 +0300548
549 ev_macs = 1
Ole Troane1ade682019-03-04 23:55:43 +0100550 self.vapi.want_l2_macs_events(max_macs_in_event=ev_macs)
Eyal Bari755b1402017-10-08 15:53:34 +0300551 self.learn_hosts(bd1, hosts)
eyal baric6038c92017-10-03 12:25:07 +0300552
553 self.sleep(1)
554 self.logger.info(self.vapi.ppcli("show l2fib"))
555 evs = self.vapi.collect_events()
Ole Troane1ade682019-03-04 23:55:43 +0100556 self.vapi.want_l2_macs_events(enable_disable=0)
Eyal Bari755b1402017-10-08 15:53:34 +0300557
558 self.assertGreater(len(evs), 0)
Jakub Grajciar145e3302019-10-24 13:52:42 +0200559 action = VppEnum.vl_api_mac_event_action_t.MAC_EVENT_ACTION_API_ADD
Eyal Bari755b1402017-10-08 15:53:34 +0300560 learned_macs = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200561 e.mac[i].mac_addr.packed
562 for e in evs
563 for i in range(e.n_macs)
564 if e.mac[i].action == action
565 }
566 macs = {
567 h.bin_mac
568 for swif in self.bd_ifs(bd1)
569 for h in hosts[self.pg_interfaces[swif].sw_if_index]
570 }
Eyal Bari755b1402017-10-08 15:53:34 +0300571
eyal baric6038c92017-10-03 12:25:07 +0300572 for e in evs:
573 self.assertLess(len(e), ev_macs * 10)
574 self.assertEqual(len(learned_macs ^ macs), 0)
575
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100576 def test_l2_fib_macs_learn_max2(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200577 """L2 FIB - mac learning max macs in event using want_l2_macs_events2"""
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100578 bd1 = 1
579 hosts = self.create_hosts(10, subnet=40)
580
581 ev_macs = 1
582 self.vapi.l2fib_set_scan_delay(scan_delay=10)
583 self.vapi.want_l2_macs_events2(max_macs_in_event=ev_macs)
584 self.sleep(1)
585 self.learn_hosts(bd1, hosts)
586
Benoît Ganne56eccdb2021-08-20 09:18:31 +0200587 self.virtual_sleep(1)
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100588 self.logger.info(self.vapi.ppcli("show l2fib"))
589 evs = self.vapi.collect_events()
590 self.vapi.want_l2_macs_events2(enable_disable=0)
591
592 self.assertGreater(len(evs), 0)
593 action = VppEnum.vl_api_mac_event_action_t.MAC_EVENT_ACTION_API_ADD
594 learned_macs = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200595 e.mac[i].mac_addr.packed
596 for e in evs
597 for i in range(e.n_macs)
598 if e.mac[i].action == action
599 }
600 macs = {
601 h.bin_mac
602 for swif in self.bd_ifs(bd1)
603 for h in hosts[self.pg_interfaces[swif].sw_if_index]
604 }
Jerome Tollet0f8d1002021-01-07 12:44:17 +0100605
606 for e in evs:
607 self.assertLess(len(e), ev_macs * 10)
608 self.assertEqual(len(learned_macs ^ macs), 0)
609
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200610
611if __name__ == "__main__":
Jan4af521d2016-11-15 17:05:00 +0100612 unittest.main(testRunner=VppTestRunner)