blob: 2c55fa8b97683d212d7745cc21fec9743cc2deaf [file] [log] [blame]
Jerome Tolletb716e382021-01-08 22:52:54 +01001#!/usr/bin/env python3
2
3import unittest
Jerome Tolletb716e382021-01-08 22:52:54 +01004
Jerome Tolletb716e382021-01-08 22:52:54 +01005from scapy.layers.l2 import Ether
Jerome Tolletb716e382021-01-08 22:52:54 +01006
Dave Wallace8800f732023-08-31 00:47:44 -04007from framework import VppTestCase
8from asfframework import VppTestRunner
9from util import Host
Jerome Tolletb716e382021-01-08 22:52:54 +010010
11
Tianyu Li6d95f8c2022-02-25 05:51:10 +000012class TestL2LearnLimitEnable(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020013 """L2 Global Learn limit Test Case"""
Jerome Tolletb716e382021-01-08 22:52:54 +010014
15 @classmethod
16 def setUpClass(self):
Tianyu Li6d95f8c2022-02-25 05:51:10 +000017 super(TestL2LearnLimitEnable, self).setUpClass()
Jerome Tolletb716e382021-01-08 22:52:54 +010018 self.create_pg_interfaces(range(3))
19
20 @classmethod
21 def tearDownClass(cls):
Tianyu Li6d95f8c2022-02-25 05:51:10 +000022 super(TestL2LearnLimitEnable, cls).tearDownClass()
Jerome Tolletb716e382021-01-08 22:52:54 +010023
24 def create_hosts(self, pg_if, n_hosts_per_if, subnet):
25 """
26 Create required number of host MAC addresses and distribute them among
27 interfaces. Create host IPv4 address for every host MAC address.
28
29 :param int n_hosts_per_if: Number of per interface hosts to
Dave Wallaced1706812021-08-12 18:36:02 -040030 create MAC/IPv4 addresses for.
Jerome Tolletb716e382021-01-08 22:52:54 +010031 """
32
33 hosts = dict()
34 swif = pg_if.sw_if_index
35
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020036 def mac(j):
37 return "00:00:%02x:ff:%02x:%02x" % (subnet, swif, j)
Jerome Tolletb716e382021-01-08 22:52:54 +010038
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020039 def ip(j):
40 return "172.%02u.1%02x.%u" % (subnet, swif, j)
Jerome Tolletb716e382021-01-08 22:52:54 +010041
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020042 def h(j):
43 return Host(mac(j), ip(j))
44
Jerome Tolletb716e382021-01-08 22:52:54 +010045 hosts[swif] = [h(j) for j in range(n_hosts_per_if)]
46
47 return hosts
48
49 def learn_hosts(self, pg_if, bd_id, hosts):
50 """
51 Create and send per interface L2 MAC broadcast packet stream to
52 let the bridge domain learn these MAC addresses.
53
54 :param int bd_id: BD to teach
55 :param dict hosts: dict of hosts per interface
56 """
57
58 self.vapi.bridge_flags(bd_id=bd_id, is_set=1, flags=1)
59
60 swif = pg_if.sw_if_index
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020061 packets = [Ether(dst="ff:ff:ff:ff:ff:ff", src=host.mac) for host in hosts[swif]]
Jerome Tolletb716e382021-01-08 22:52:54 +010062 pg_if.add_stream(packets)
63 self.logger.info("Sending broadcast eth frames for MAC learning")
64 self.pg_start()
65
66 def test_l2bd_learnlimit01(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020067 """L2BD test with learn Limit"""
Jerome Tolletb716e382021-01-08 22:52:54 +010068 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)
71
72 # inject 20 mac addresses on bd1
73 self.learn_hosts(self.pg_interfaces[0], 1, hosts)
74
75 # inject 1 mac address on bd2
76 self.learn_hosts(self.pg_interfaces[1], 2, fhosts)
77
78 lfs1 = self.vapi.l2_fib_table_dump(1)
79 lfs2 = self.vapi.l2_fib_table_dump(2)
80
81 # check that only 10 macs are learned.
82 self.assertEqual(len(lfs1), 10)
83
84 # check that bd2 was not able to learn
85 self.assertEqual(len(lfs2), 0)
86
87 def setUp(self):
Tianyu Li6d95f8c2022-02-25 05:51:10 +000088 super(TestL2LearnLimitEnable, self).setUp()
Jerome Tolletb716e382021-01-08 22:52:54 +010089
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +020090 self.vapi.bridge_domain_add_del_v2(
91 bd_id=1, is_add=1, flood=1, forward=1, uu_flood=1, learn=1
92 )
93 self.vapi.bridge_domain_add_del_v2(
94 bd_id=2, is_add=1, flood=1, forward=1, uu_flood=1, learn=1
95 )
Jerome Tolletb716e382021-01-08 22:52:54 +010096
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020097 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)
Jerome Tolletb716e382021-01-08 22:52:54 +010099
100 def tearDown(self):
Tianyu Li6d95f8c2022-02-25 05:51:10 +0000101 super(TestL2LearnLimitEnable, self).tearDown()
Jerome Tolletb716e382021-01-08 22:52:54 +0100102 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200103 rx_sw_if_index=self.pg_interfaces[0].sw_if_index, bd_id=1, enable=0
104 )
Jerome Tolletb716e382021-01-08 22:52:54 +0100105 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200106 rx_sw_if_index=self.pg_interfaces[1].sw_if_index, bd_id=2, enable=0
107 )
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +0200108 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)
Jerome Tolletb716e382021-01-08 22:52:54 +0100110
111
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200112if __name__ == "__main__":
Jerome Tolletb716e382021-01-08 22:52:54 +0100113 unittest.main(testRunner=VppTestRunner)