tests: replace pycodestyle with black
[vpp.git] / test / test_l2bd_learnlimit_enabled.py
1 #!/usr/bin/env python3
2
3 import unittest
4 import random
5
6 from scapy.packet import Raw
7 from scapy.layers.l2 import Ether
8 from scapy.layers.inet import IP, UDP
9
10 from framework import VppTestCase, VppTestRunner
11 from util import Host, ppp
12
13
14 class TestL2LearnLimitEnable(VppTestCase):
15     """L2 Global Learn limit Test Case"""
16
17     @classmethod
18     def setUpClass(self):
19         super(TestL2LearnLimitEnable, self).setUpClass()
20         self.create_pg_interfaces(range(3))
21
22     @classmethod
23     def tearDownClass(cls):
24         super(TestL2LearnLimitEnable, cls).tearDownClass()
25
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
32              create MAC/IPv4 addresses for.
33         """
34
35         hosts = dict()
36         swif = pg_if.sw_if_index
37
38         def mac(j):
39             return "00:00:%02x:ff:%02x:%02x" % (subnet, swif, j)
40
41         def ip(j):
42             return "172.%02u.1%02x.%u" % (subnet, swif, j)
43
44         def h(j):
45             return Host(mac(j), ip(j))
46
47         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
63         packets = [Ether(dst="ff:ff:ff:ff:ff:ff", src=host.mac) for host in hosts[swif]]
64         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_learnlimit01(self):
69         """L2BD test with learn Limit"""
70         self.vapi.want_l2_macs_events(enable_disable=1, learn_limit=10)
71         hosts = self.create_hosts(self.pg_interfaces[0], 20, 1)
72         fhosts = self.create_hosts(self.pg_interfaces[1], 1, 2)
73
74         # inject 20 mac addresses on bd1
75         self.learn_hosts(self.pg_interfaces[0], 1, hosts)
76
77         # inject 1 mac address on bd2
78         self.learn_hosts(self.pg_interfaces[1], 2, fhosts)
79
80         lfs1 = self.vapi.l2_fib_table_dump(1)
81         lfs2 = self.vapi.l2_fib_table_dump(2)
82
83         # check that only 10 macs are learned.
84         self.assertEqual(len(lfs1), 10)
85
86         # check that bd2 was not able to learn
87         self.assertEqual(len(lfs2), 0)
88
89     def setUp(self):
90         super(TestL2LearnLimitEnable, self).setUp()
91
92         self.vapi.bridge_domain_add_del(bd_id=1)
93         self.vapi.bridge_domain_add_del(bd_id=2)
94
95         self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[0].sw_if_index, bd_id=1)
96         self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[1].sw_if_index, bd_id=2)
97
98     def tearDown(self):
99         super(TestL2LearnLimitEnable, self).tearDown()
100         self.vapi.sw_interface_set_l2_bridge(
101             rx_sw_if_index=self.pg_interfaces[0].sw_if_index, bd_id=1, enable=0
102         )
103         self.vapi.sw_interface_set_l2_bridge(
104             rx_sw_if_index=self.pg_interfaces[1].sw_if_index, bd_id=2, enable=0
105         )
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
110 if __name__ == "__main__":
111     unittest.main(testRunner=VppTestRunner)