6 from scapy.packet import Raw
7 from scapy.layers.l2 import Ether
8 from scapy.layers.inet import IP, UDP
10 from framework import VppTestCase, VppTestRunner, running_extended_tests
11 from util import Host, ppp
14 class TestL2LearnLimit(VppTestCase):
15 """ L2 Learn limit Test Case """
19 super(TestL2LearnLimit, self).setUpClass()
20 self.create_pg_interfaces(range(3))
23 def tearDownClass(cls):
24 super(TestL2LearnLimit, cls).tearDownClass()
26 def create_hosts(self, pg_if, n_hosts_per_if, subnet):
28 Create required number of host MAC addresses and distribute them among
29 interfaces. Create host IPv4 address for every host MAC address.
31 :param int n_hosts_per_if: Number of per interface hosts to
32 create MAC/IPv4 addresses for.
36 swif = pg_if.sw_if_index
38 def mac(j): return "00:00:%02x:ff:%02x:%02x" % (subnet, swif, j)
40 def ip(j): return "172.%02u.1%02x.%u" % (subnet, swif, j)
42 def h(j): return Host(mac(j), ip(j))
43 hosts[swif] = [h(j) for j in range(n_hosts_per_if)]
47 def learn_hosts(self, pg_if, bd_id, hosts):
49 Create and send per interface L2 MAC broadcast packet stream to
50 let the bridge domain learn these MAC addresses.
52 :param int bd_id: BD to teach
53 :param dict hosts: dict of hosts per interface
56 self.vapi.bridge_flags(bd_id=bd_id, is_set=1, flags=1)
58 swif = pg_if.sw_if_index
59 packets = [Ether(dst="ff:ff:ff:ff:ff:ff", src=host.mac)
60 for host in hosts[swif]]
61 pg_if.add_stream(packets)
62 self.logger.info("Sending broadcast eth frames for MAC learning")
65 def test_l2bd_learnlimit01(self):
66 """ L2BD test without learn Limit
68 hosts = self.create_hosts(self.pg_interfaces[0], 20, 1)
69 self.learn_hosts(self.pg_interfaces[0], 1, hosts)
70 lfs = self.vapi.l2_fib_table_dump(1)
72 # check that 20 macs are learned.
73 self.assertEqual(len(lfs), 20)
75 def test_l2bd_learnlimit02(self):
76 """ L2BD test with learn Limit
78 self.vapi.want_l2_macs_events(enable_disable=1, learn_limit=10)
79 hosts = self.create_hosts(self.pg_interfaces[0], 20, 1)
80 fhosts = self.create_hosts(self.pg_interfaces[1], 1, 2)
82 # inject 20 mac addresses on bd1
83 self.learn_hosts(self.pg_interfaces[0], 1, hosts)
85 # inject 1 mac address on bd2
86 self.learn_hosts(self.pg_interfaces[1], 2, fhosts)
88 lfs1 = self.vapi.l2_fib_table_dump(1)
89 lfs2 = self.vapi.l2_fib_table_dump(2)
91 # check that only 10 macs are learned.
92 self.assertEqual(len(lfs1), 10)
94 # check that bd2 was not able to learn
95 self.assertEqual(len(lfs2), 0)
97 def test_l2bd_learnlimit03(self):
98 """ L2BD test with bridge domain limit
100 self.vapi.bridge_domain_set_default_learn_limit(4)
101 self.vapi.bridge_domain_add_del(bd_id=3)
102 self.vapi.sw_interface_set_l2_bridge(
103 self.pg_interfaces[2].sw_if_index, bd_id=3)
105 self.vapi.bridge_domain_set_learn_limit(2, 5)
107 hosts = self.create_hosts(self.pg_interfaces[1], 20, 2)
108 fhosts = self.create_hosts(self.pg_interfaces[2], 20, 3)
110 # inject 20 mac addresses on bd2
111 self.learn_hosts(self.pg_interfaces[1], 2, hosts)
113 # inject 20 macs address on bd3
114 self.learn_hosts(self.pg_interfaces[2], 3, fhosts)
116 lfs1 = self.vapi.l2_fib_table_dump(2)
117 lfs2 = self.vapi.l2_fib_table_dump(3)
119 # check that only 5 macs are learned.
120 self.assertEqual(len(lfs1), 5)
122 # check that only 4 macs are learned.
123 self.assertEqual(len(lfs2), 4)
125 self.vapi.sw_interface_set_l2_bridge(
126 rx_sw_if_index=self.pg_interfaces[2].sw_if_index,
128 self.vapi.bridge_domain_add_del(is_add=0, bd_id=3)
131 super(TestL2LearnLimit, self).setUp()
133 self.vapi.bridge_domain_add_del(bd_id=1)
134 self.vapi.bridge_domain_add_del(bd_id=2)
136 self.vapi.sw_interface_set_l2_bridge(
137 self.pg_interfaces[0].sw_if_index, bd_id=1)
138 self.vapi.sw_interface_set_l2_bridge(
139 self.pg_interfaces[1].sw_if_index, bd_id=2)
142 super(TestL2LearnLimit, self).tearDown()
143 self.vapi.sw_interface_set_l2_bridge(
144 rx_sw_if_index=self.pg_interfaces[0].sw_if_index,
146 self.vapi.sw_interface_set_l2_bridge(
147 rx_sw_if_index=self.pg_interfaces[1].sw_if_index,
149 self.vapi.bridge_domain_add_del(bd_id=1, is_add=0)
150 self.vapi.bridge_domain_add_del(bd_id=2, is_add=0)
152 while (len(self.vapi.l2_fib_table_dump(bd_id=0xffffffff)) != 0):
156 self.assertTrue(False)
159 if __name__ == '__main__':
160 unittest.main(testRunner=VppTestRunner)