Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import unittest |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 4 | |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 5 | from scapy.layers.l2 import Ether |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 6 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 7 | from framework import VppTestCase |
| 8 | from asfframework import VppTestRunner |
| 9 | from util import Host |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 10 | |
| 11 | |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 12 | class TestL2LearnLimitBdEnable(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 13 | """L2 Bridge Domain Learn limit Test Case""" |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 14 | |
| 15 | @classmethod |
| 16 | def setUpClass(self): |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 17 | super(TestL2LearnLimitBdEnable, self).setUpClass() |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 18 | self.create_pg_interfaces(range(3)) |
| 19 | |
| 20 | @classmethod |
| 21 | def tearDownClass(cls): |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 22 | super(TestL2LearnLimitBdEnable, cls).tearDownClass() |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 23 | |
| 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 Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 30 | create MAC/IPv4 addresses for. |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 31 | """ |
| 32 | |
| 33 | hosts = dict() |
| 34 | swif = pg_if.sw_if_index |
| 35 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 36 | def mac(j): |
| 37 | return "00:00:%02x:ff:%02x:%02x" % (subnet, swif, j) |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 38 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 39 | def ip(j): |
| 40 | return "172.%02u.1%02x.%u" % (subnet, swif, j) |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 41 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 42 | def h(j): |
| 43 | return Host(mac(j), ip(j)) |
| 44 | |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 45 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 61 | packets = [Ether(dst="ff:ff:ff:ff:ff:ff", src=host.mac) for host in hosts[swif]] |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 62 | 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_learnlimit(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 67 | """L2BD test with bridge domain limit""" |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 68 | self.vapi.want_l2_macs_events(enable_disable=1, learn_limit=1000) |
| 69 | self.vapi.bridge_domain_set_default_learn_limit(4) |
Laszlo Kiraly | 0f8f435 | 2022-09-16 13:20:07 +0200 | [diff] [blame] | 70 | self.vapi.bridge_domain_add_del_v2( |
| 71 | bd_id=3, is_add=1, flood=1, uu_flood=1, forward=1, learn=1 |
| 72 | ) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 73 | self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[2].sw_if_index, bd_id=3) |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 74 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 96 | rx_sw_if_index=self.pg_interfaces[2].sw_if_index, bd_id=3, enable=0 |
| 97 | ) |
Laszlo Kiraly | 0f8f435 | 2022-09-16 13:20:07 +0200 | [diff] [blame] | 98 | self.vapi.bridge_domain_add_del_v2(is_add=0, bd_id=3) |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 99 | |
| 100 | def setUp(self): |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 101 | super(TestL2LearnLimitBdEnable, self).setUp() |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 102 | |
Laszlo Kiraly | 0f8f435 | 2022-09-16 13:20:07 +0200 | [diff] [blame] | 103 | self.vapi.bridge_domain_add_del_v2( |
| 104 | bd_id=1, is_add=1, flood=1, uu_flood=1, forward=1, learn=1 |
| 105 | ) |
| 106 | self.vapi.bridge_domain_add_del_v2( |
| 107 | bd_id=2, is_add=1, flood=1, uu_flood=1, forward=1, learn=1 |
| 108 | ) |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 109 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 110 | self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[0].sw_if_index, bd_id=1) |
| 111 | self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[1].sw_if_index, bd_id=2) |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 112 | |
| 113 | def tearDown(self): |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 114 | super(TestL2LearnLimitBdEnable, self).tearDown() |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 115 | self.vapi.sw_interface_set_l2_bridge( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 116 | rx_sw_if_index=self.pg_interfaces[0].sw_if_index, bd_id=1, enable=0 |
| 117 | ) |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 118 | self.vapi.sw_interface_set_l2_bridge( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 119 | rx_sw_if_index=self.pg_interfaces[1].sw_if_index, bd_id=2, enable=0 |
| 120 | ) |
Laszlo Kiraly | 0f8f435 | 2022-09-16 13:20:07 +0200 | [diff] [blame] | 121 | self.vapi.bridge_domain_add_del_v2(bd_id=1, is_add=0) |
| 122 | self.vapi.bridge_domain_add_del_v2(bd_id=2, is_add=0) |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 123 | |
| 124 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 125 | if __name__ == "__main__": |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 126 | unittest.main(testRunner=VppTestRunner) |