5 from scapy.layers.l2 import Ether
7 from framework import VppTestCase
8 from asfframework import VppTestRunner
12 class TestL2LearnLimitEnable(VppTestCase):
13 """L2 Global Learn limit Test Case"""
17 super(TestL2LearnLimitEnable, self).setUpClass()
18 self.create_pg_interfaces(range(3))
21 def tearDownClass(cls):
22 super(TestL2LearnLimitEnable, cls).tearDownClass()
24 def create_hosts(self, pg_if, n_hosts_per_if, subnet):
26 Create required number of host MAC addresses and distribute them among
27 interfaces. Create host IPv4 address for every host MAC address.
29 :param int n_hosts_per_if: Number of per interface hosts to
30 create MAC/IPv4 addresses for.
34 swif = pg_if.sw_if_index
37 return "00:00:%02x:ff:%02x:%02x" % (subnet, swif, j)
40 return "172.%02u.1%02x.%u" % (subnet, swif, j)
43 return Host(mac(j), ip(j))
45 hosts[swif] = [h(j) for j in range(n_hosts_per_if)]
49 def learn_hosts(self, pg_if, bd_id, hosts):
51 Create and send per interface L2 MAC broadcast packet stream to
52 let the bridge domain learn these MAC addresses.
54 :param int bd_id: BD to teach
55 :param dict hosts: dict of hosts per interface
58 self.vapi.bridge_flags(bd_id=bd_id, is_set=1, flags=1)
60 swif = pg_if.sw_if_index
61 packets = [Ether(dst="ff:ff:ff:ff:ff:ff", src=host.mac) for host in hosts[swif]]
62 pg_if.add_stream(packets)
63 self.logger.info("Sending broadcast eth frames for MAC learning")
66 def test_l2bd_learnlimit01(self):
67 """L2BD test with learn Limit"""
68 self.vapi.want_l2_macs_events(enable_disable=1, learn_limit=10)
69 hosts = self.create_hosts(self.pg_interfaces[0], 20, 1)
70 fhosts = self.create_hosts(self.pg_interfaces[1], 1, 2)
72 # inject 20 mac addresses on bd1
73 self.learn_hosts(self.pg_interfaces[0], 1, hosts)
75 # inject 1 mac address on bd2
76 self.learn_hosts(self.pg_interfaces[1], 2, fhosts)
78 lfs1 = self.vapi.l2_fib_table_dump(1)
79 lfs2 = self.vapi.l2_fib_table_dump(2)
81 # check that only 10 macs are learned.
82 self.assertEqual(len(lfs1), 10)
84 # check that bd2 was not able to learn
85 self.assertEqual(len(lfs2), 0)
88 super(TestL2LearnLimitEnable, self).setUp()
90 self.vapi.bridge_domain_add_del_v2(
91 bd_id=1, is_add=1, flood=1, forward=1, uu_flood=1, learn=1
93 self.vapi.bridge_domain_add_del_v2(
94 bd_id=2, is_add=1, flood=1, forward=1, uu_flood=1, learn=1
97 self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[0].sw_if_index, bd_id=1)
98 self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[1].sw_if_index, bd_id=2)
101 super(TestL2LearnLimitEnable, self).tearDown()
102 self.vapi.sw_interface_set_l2_bridge(
103 rx_sw_if_index=self.pg_interfaces[0].sw_if_index, bd_id=1, enable=0
105 self.vapi.sw_interface_set_l2_bridge(
106 rx_sw_if_index=self.pg_interfaces[1].sw_if_index, bd_id=2, enable=0
108 self.vapi.bridge_domain_add_del_v2(bd_id=1, is_add=0)
109 self.vapi.bridge_domain_add_del_v2(bd_id=2, is_add=0)
112 if __name__ == "__main__":
113 unittest.main(testRunner=VppTestRunner)