blob: 5fcd54cb8f291a4658bfc78f6af21ce98ad1fc1a [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 TestL2LearnLimitEnable(VppTestCase):
Jerome Tolletb716e382021-01-08 22:52:54 +010015 """ L2 Global Learn limit Test Case """
16
17 @classmethod
18 def setUpClass(self):
Tianyu Li6d95f8c2022-02-25 05:51:10 +000019 super(TestL2LearnLimitEnable, 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(TestL2LearnLimitEnable, 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
38 def mac(j): return "00:00:%02x:ff:%02x:%02x" % (subnet, swif, j)
39
40 def ip(j): return "172.%02u.1%02x.%u" % (subnet, swif, j)
41
42 def h(j): return Host(mac(j), ip(j))
43 hosts[swif] = [h(j) for j in range(n_hosts_per_if)]
44
45 return hosts
46
47 def learn_hosts(self, pg_if, bd_id, hosts):
48 """
49 Create and send per interface L2 MAC broadcast packet stream to
50 let the bridge domain learn these MAC addresses.
51
52 :param int bd_id: BD to teach
53 :param dict hosts: dict of hosts per interface
54 """
55
56 self.vapi.bridge_flags(bd_id=bd_id, is_set=1, flags=1)
57
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")
63 self.pg_start()
64
65 def test_l2bd_learnlimit01(self):
66 """ L2BD test with learn Limit
67 """
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)
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
90 self.vapi.bridge_domain_add_del(bd_id=1)
91 self.vapi.bridge_domain_add_del(bd_id=2)
92
93 self.vapi.sw_interface_set_l2_bridge(
94 self.pg_interfaces[0].sw_if_index, bd_id=1)
95 self.vapi.sw_interface_set_l2_bridge(
96 self.pg_interfaces[1].sw_if_index, bd_id=2)
97
98 def tearDown(self):
Tianyu Li6d95f8c2022-02-25 05:51:10 +000099 super(TestL2LearnLimitEnable, self).tearDown()
Jerome Tolletb716e382021-01-08 22:52:54 +0100100 self.vapi.sw_interface_set_l2_bridge(
101 rx_sw_if_index=self.pg_interfaces[0].sw_if_index,
102 bd_id=1, enable=0)
103 self.vapi.sw_interface_set_l2_bridge(
104 rx_sw_if_index=self.pg_interfaces[1].sw_if_index,
105 bd_id=2, enable=0)
106 self.vapi.bridge_domain_add_del(bd_id=1, is_add=0)
107 self.vapi.bridge_domain_add_del(bd_id=2, is_add=0)
108
109
110if __name__ == '__main__':
111 unittest.main(testRunner=VppTestRunner)