Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 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 | |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 10 | from framework import VppTestCase, VppTestRunner |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 11 | from util import Host, ppp |
| 12 | |
| 13 | |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 14 | class TestL2LearnLimitBdEnable(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 15 | """L2 Bridge Domain Learn limit Test Case""" |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 16 | |
| 17 | @classmethod |
| 18 | def setUpClass(self): |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 19 | super(TestL2LearnLimitBdEnable, self).setUpClass() |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 20 | self.create_pg_interfaces(range(3)) |
| 21 | |
| 22 | @classmethod |
| 23 | def tearDownClass(cls): |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 24 | super(TestL2LearnLimitBdEnable, cls).tearDownClass() |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 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 |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 32 | create MAC/IPv4 addresses for. |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 33 | """ |
| 34 | |
| 35 | hosts = dict() |
| 36 | swif = pg_if.sw_if_index |
| 37 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 38 | def mac(j): |
| 39 | return "00:00:%02x:ff:%02x:%02x" % (subnet, swif, j) |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 40 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 41 | def ip(j): |
| 42 | return "172.%02u.1%02x.%u" % (subnet, swif, j) |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 43 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 44 | def h(j): |
| 45 | return Host(mac(j), ip(j)) |
| 46 | |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 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 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 63 | 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] | 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_learnlimit(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 69 | """L2BD test with bridge domain limit""" |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 70 | self.vapi.want_l2_macs_events(enable_disable=1, learn_limit=1000) |
| 71 | self.vapi.bridge_domain_set_default_learn_limit(4) |
Laszlo Kiraly | 0f8f435 | 2022-09-16 13:20:07 +0200 | [diff] [blame^] | 72 | self.vapi.bridge_domain_add_del_v2( |
| 73 | bd_id=3, is_add=1, flood=1, uu_flood=1, forward=1, learn=1 |
| 74 | ) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 75 | 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] | 76 | |
| 77 | self.vapi.bridge_domain_set_learn_limit(2, 5) |
| 78 | |
| 79 | hosts = self.create_hosts(self.pg_interfaces[1], 20, 2) |
| 80 | fhosts = self.create_hosts(self.pg_interfaces[2], 20, 3) |
| 81 | |
| 82 | # inject 20 mac addresses on bd2 |
| 83 | self.learn_hosts(self.pg_interfaces[1], 2, hosts) |
| 84 | |
| 85 | # inject 20 macs address on bd3 |
| 86 | self.learn_hosts(self.pg_interfaces[2], 3, fhosts) |
| 87 | |
| 88 | lfs1 = self.vapi.l2_fib_table_dump(2) |
| 89 | lfs2 = self.vapi.l2_fib_table_dump(3) |
| 90 | |
| 91 | # check that only 5 macs are learned. |
| 92 | self.assertEqual(len(lfs1), 5) |
| 93 | |
| 94 | # check that only 4 macs are learned. |
| 95 | self.assertEqual(len(lfs2), 4) |
| 96 | |
| 97 | self.vapi.sw_interface_set_l2_bridge( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 98 | rx_sw_if_index=self.pg_interfaces[2].sw_if_index, bd_id=3, enable=0 |
| 99 | ) |
Laszlo Kiraly | 0f8f435 | 2022-09-16 13:20:07 +0200 | [diff] [blame^] | 100 | 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] | 101 | |
| 102 | def setUp(self): |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 103 | super(TestL2LearnLimitBdEnable, self).setUp() |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 104 | |
Laszlo Kiraly | 0f8f435 | 2022-09-16 13:20:07 +0200 | [diff] [blame^] | 105 | self.vapi.bridge_domain_add_del_v2( |
| 106 | bd_id=1, is_add=1, flood=1, uu_flood=1, forward=1, learn=1 |
| 107 | ) |
| 108 | self.vapi.bridge_domain_add_del_v2( |
| 109 | bd_id=2, is_add=1, flood=1, uu_flood=1, forward=1, learn=1 |
| 110 | ) |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 111 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 112 | self.vapi.sw_interface_set_l2_bridge(self.pg_interfaces[0].sw_if_index, bd_id=1) |
| 113 | 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] | 114 | |
| 115 | def tearDown(self): |
Tianyu Li | 6d95f8c | 2022-02-25 05:51:10 +0000 | [diff] [blame] | 116 | super(TestL2LearnLimitBdEnable, self).tearDown() |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 117 | self.vapi.sw_interface_set_l2_bridge( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 118 | rx_sw_if_index=self.pg_interfaces[0].sw_if_index, bd_id=1, enable=0 |
| 119 | ) |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 120 | self.vapi.sw_interface_set_l2_bridge( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 121 | rx_sw_if_index=self.pg_interfaces[1].sw_if_index, bd_id=2, enable=0 |
| 122 | ) |
Laszlo Kiraly | 0f8f435 | 2022-09-16 13:20:07 +0200 | [diff] [blame^] | 123 | self.vapi.bridge_domain_add_del_v2(bd_id=1, is_add=0) |
| 124 | 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] | 125 | |
| 126 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 127 | if __name__ == "__main__": |
Jerome Tollet | b716e38 | 2021-01-08 22:52:54 +0100 | [diff] [blame] | 128 | unittest.main(testRunner=VppTestRunner) |