blob: 0578cedbe4a95841deb91c8cfa8287a1ef496ad7 [file] [log] [blame]
Jerome Tolletb716e382021-01-08 22:52:54 +01001#!/usr/bin/env python3
2
3import unittest
4import random
5
6from scapy.packet import Raw
7from scapy.layers.l2 import Ether
8from scapy.layers.inet import IP, UDP
9
Klement Sekerab23ffd72021-05-31 16:08:53 +020010from framework import VppTestCase, VppTestRunner
Jerome Tolletb716e382021-01-08 22:52:54 +010011from util import Host, ppp
12
13
Tianyu Li6d95f8c2022-02-25 05:51:10 +000014class TestL2LearnLimitBdEnable(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020015 """L2 Bridge Domain Learn limit Test Case"""
Jerome Tolletb716e382021-01-08 22:52:54 +010016
17 @classmethod
18 def setUpClass(self):
Tianyu Li6d95f8c2022-02-25 05:51:10 +000019 super(TestL2LearnLimitBdEnable, self).setUpClass()
Jerome Tolletb716e382021-01-08 22:52:54 +010020 self.create_pg_interfaces(range(3))
21
22 @classmethod
23 def tearDownClass(cls):
Tianyu Li6d95f8c2022-02-25 05:51:10 +000024 super(TestL2LearnLimitBdEnable, cls).tearDownClass()
Jerome Tolletb716e382021-01-08 22:52:54 +010025
26 def create_hosts(self, pg_if, n_hosts_per_if, subnet):
27 """
28 Create required number of host MAC addresses and distribute them among
29 interfaces. Create host IPv4 address for every host MAC address.
30
31 :param int n_hosts_per_if: Number of per interface hosts to
Dave Wallaced1706812021-08-12 18:36:02 -040032 create MAC/IPv4 addresses for.
Jerome Tolletb716e382021-01-08 22:52:54 +010033 """
34
35 hosts = dict()
36 swif = pg_if.sw_if_index
37
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020038 def mac(j):
39 return "00:00:%02x:ff:%02x:%02x" % (subnet, swif, j)
Jerome Tolletb716e382021-01-08 22:52:54 +010040
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020041 def ip(j):
42 return "172.%02u.1%02x.%u" % (subnet, swif, j)
Jerome Tolletb716e382021-01-08 22:52:54 +010043
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020044 def h(j):
45 return Host(mac(j), ip(j))
46
Jerome Tolletb716e382021-01-08 22:52:54 +010047 hosts[swif] = [h(j) for j in range(n_hosts_per_if)]
48
49 return hosts
50
51 def learn_hosts(self, pg_if, bd_id, hosts):
52 """
53 Create and send per interface L2 MAC broadcast packet stream to
54 let the bridge domain learn these MAC addresses.
55
56 :param int bd_id: BD to teach
57 :param dict hosts: dict of hosts per interface
58 """
59
60 self.vapi.bridge_flags(bd_id=bd_id, is_set=1, flags=1)
61
62 swif = pg_if.sw_if_index
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020063 packets = [Ether(dst="ff:ff:ff:ff:ff:ff", src=host.mac) for host in hosts[swif]]
Jerome Tolletb716e382021-01-08 22:52:54 +010064 pg_if.add_stream(packets)
65 self.logger.info("Sending broadcast eth frames for MAC learning")
66 self.pg_start()
67
68 def test_l2bd_learnlimit(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020069 """L2BD test with bridge domain limit"""
Jerome Tolletb716e382021-01-08 22:52:54 +010070 self.vapi.want_l2_macs_events(enable_disable=1, learn_limit=1000)
71 self.vapi.bridge_domain_set_default_learn_limit(4)
72 self.vapi.bridge_domain_add_del(bd_id=3)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020073 self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[2].sw_if_index, bd_id=3)
Jerome Tolletb716e382021-01-08 22:52:54 +010074
75 self.vapi.bridge_domain_set_learn_limit(2, 5)
76
77 hosts = self.create_hosts(self.pg_interfaces[1], 20, 2)
78 fhosts = self.create_hosts(self.pg_interfaces[2], 20, 3)
79
80 # inject 20 mac addresses on bd2
81 self.learn_hosts(self.pg_interfaces[1], 2, hosts)
82
83 # inject 20 macs address on bd3
84 self.learn_hosts(self.pg_interfaces[2], 3, fhosts)
85
86 lfs1 = self.vapi.l2_fib_table_dump(2)
87 lfs2 = self.vapi.l2_fib_table_dump(3)
88
89 # check that only 5 macs are learned.
90 self.assertEqual(len(lfs1), 5)
91
92 # check that only 4 macs are learned.
93 self.assertEqual(len(lfs2), 4)
94
95 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020096 rx_sw_if_index=self.pg_interfaces[2].sw_if_index, bd_id=3, enable=0
97 )
Jerome Tolletb716e382021-01-08 22:52:54 +010098 self.vapi.bridge_domain_add_del(is_add=0, bd_id=3)
99
100 def setUp(self):
Tianyu Li6d95f8c2022-02-25 05:51:10 +0000101 super(TestL2LearnLimitBdEnable, self).setUp()
Jerome Tolletb716e382021-01-08 22:52:54 +0100102
103 self.vapi.bridge_domain_add_del(bd_id=1)
104 self.vapi.bridge_domain_add_del(bd_id=2)
105
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200106 self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[0].sw_if_index, bd_id=1)
107 self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[1].sw_if_index, bd_id=2)
Jerome Tolletb716e382021-01-08 22:52:54 +0100108
109 def tearDown(self):
Tianyu Li6d95f8c2022-02-25 05:51:10 +0000110 super(TestL2LearnLimitBdEnable, self).tearDown()
Jerome Tolletb716e382021-01-08 22:52:54 +0100111 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200112 rx_sw_if_index=self.pg_interfaces[0].sw_if_index, bd_id=1, enable=0
113 )
Jerome Tolletb716e382021-01-08 22:52:54 +0100114 self.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200115 rx_sw_if_index=self.pg_interfaces[1].sw_if_index, bd_id=2, enable=0
116 )
Jerome Tolletb716e382021-01-08 22:52:54 +0100117 self.vapi.bridge_domain_add_del(bd_id=1, is_add=0)
118 self.vapi.bridge_domain_add_del(bd_id=2, is_add=0)
119
120
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200121if __name__ == "__main__":
Jerome Tolletb716e382021-01-08 22:52:54 +0100122 unittest.main(testRunner=VppTestRunner)