Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 2 | |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 3 | import socket |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 4 | from util import ip4_range, reassemble4 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 5 | import unittest |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 6 | from framework import VppTestCase |
| 7 | from asfframework import VppTestRunner |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 8 | from template_bd import BridgeDomain |
| 9 | |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 10 | from scapy.layers.l2 import Ether |
Artem Glazychev | 23e5f09 | 2022-02-21 17:51:29 +0700 | [diff] [blame] | 11 | from scapy.layers.l2 import ARP |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 12 | from scapy.packet import Raw, bind_layers |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 13 | from scapy.layers.inet import IP, UDP |
Vladislav Grishenko | f2fc97a | 2024-01-24 20:33:12 +0500 | [diff] [blame] | 14 | from scapy.layers.inet6 import IPv6 |
Matej Klotton | deb6984 | 2016-12-09 15:05:46 +0100 | [diff] [blame] | 15 | from scapy.layers.vxlan import VXLAN |
Vladislav Grishenko | f2fc97a | 2024-01-24 20:33:12 +0500 | [diff] [blame] | 16 | from scapy.contrib.mpls import MPLS |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 17 | |
| 18 | import util |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 19 | from vpp_ip_route import VppIpRoute, VppRoutePath |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 20 | from vpp_vxlan_tunnel import VppVxlanTunnel |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 21 | from vpp_ip import INVALID_INDEX |
Artem Glazychev | 23e5f09 | 2022-02-21 17:51:29 +0700 | [diff] [blame] | 22 | from vpp_neighbor import VppNeighbor |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 23 | from config import config |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 24 | |
| 25 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 26 | @unittest.skipIf("vxlan" in config.excluded_plugins, "Exclude VXLAN plugin tests") |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 27 | class TestVxlan(BridgeDomain, VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 28 | """VXLAN Test Case""" |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 29 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 30 | def __init__(self, *args): |
| 31 | BridgeDomain.__init__(self) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 32 | VppTestCase.__init__(self, *args) |
| 33 | |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 34 | def encapsulate(self, pkt, vni): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 35 | """ |
| 36 | Encapsulate the original payload frame by adding VXLAN header with its |
| 37 | UDP, IP and Ethernet fields |
| 38 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 39 | return ( |
| 40 | Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) |
| 41 | / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) |
| 42 | / UDP(sport=self.dport, dport=self.dport, chksum=0) |
| 43 | / VXLAN(vni=vni, flags=self.flags) |
| 44 | / pkt |
| 45 | ) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 46 | |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 47 | def ip_range(self, start, end): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 48 | """range of remote ip's""" |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 49 | return ip4_range(self.pg0.remote_ip4, start, end) |
| 50 | |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 51 | def encap_mcast(self, pkt, src_ip, src_mac, vni): |
| 52 | """ |
| 53 | Encapsulate the original payload frame by adding VXLAN header with its |
| 54 | UDP, IP and Ethernet fields |
| 55 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 56 | return ( |
| 57 | Ether(src=src_mac, dst=self.mcast_mac) |
| 58 | / IP(src=src_ip, dst=self.mcast_ip4) |
| 59 | / UDP(sport=self.dport, dport=self.dport, chksum=0) |
| 60 | / VXLAN(vni=vni, flags=self.flags) |
| 61 | / pkt |
| 62 | ) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 63 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 64 | def decapsulate(self, pkt): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 65 | """ |
| 66 | Decapsulate the original payload frame by removing VXLAN header |
| 67 | """ |
Matej Klotton | deb6984 | 2016-12-09 15:05:46 +0100 | [diff] [blame] | 68 | # check if is set I flag |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 69 | self.assertEqual(pkt[VXLAN].flags, int("0x8", 16)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 70 | return pkt[VXLAN].payload |
| 71 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 72 | # Method for checking VXLAN encapsulation. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 73 | # |
Eyal Bari | 6ae5ee7 | 2017-03-23 09:53:51 +0200 | [diff] [blame] | 74 | def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False): |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 75 | # TODO: add error messages |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 76 | # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 77 | # by VPP using ARP. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 78 | self.assertEqual(pkt[Ether].src, self.pg0.local_mac) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 79 | if not local_only: |
Eyal Bari | 6ae5ee7 | 2017-03-23 09:53:51 +0200 | [diff] [blame] | 80 | if not mcast_pkt: |
| 81 | self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) |
| 82 | else: |
| 83 | self.assertEqual(pkt[Ether].dst, type(self).mcast_mac) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 84 | # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP. |
| 85 | self.assertEqual(pkt[IP].src, self.pg0.local_ip4) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 86 | if not local_only: |
Eyal Bari | 6ae5ee7 | 2017-03-23 09:53:51 +0200 | [diff] [blame] | 87 | if not mcast_pkt: |
| 88 | self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4) |
| 89 | else: |
| 90 | self.assertEqual(pkt[IP].dst, type(self).mcast_ip4) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 91 | # Verify UDP destination port is VXLAN 4789, source UDP port could be |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 92 | # arbitrary. |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 93 | self.assertEqual(pkt[UDP].dport, self.dport) |
Vladimir Isaev | 698eb87 | 2020-05-21 16:34:17 +0300 | [diff] [blame] | 94 | # Verify UDP checksum |
| 95 | self.assert_udp_checksum_valid(pkt) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 96 | # Verify VNI |
| 97 | self.assertEqual(pkt[VXLAN].vni, vni) |
| 98 | |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 99 | @classmethod |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 100 | def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels, port): |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 101 | # Create 10 ucast vxlan tunnels under bd |
| 102 | ip_range_start = 10 |
Eyal Bari | d81da8c | 2017-01-11 13:39:54 +0200 | [diff] [blame] | 103 | ip_range_end = ip_range_start + n_ucast_tunnels |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 104 | next_hop_address = cls.pg0.remote_ip4 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 105 | for dest_ip4 in ip4_range(next_hop_address, ip_range_start, ip_range_end): |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 106 | # add host route so dest_ip4 will not be resolved |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 107 | rip = VppIpRoute( |
| 108 | cls, |
| 109 | dest_ip4, |
| 110 | 32, |
| 111 | [VppRoutePath(next_hop_address, INVALID_INDEX)], |
| 112 | register=False, |
| 113 | ) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 114 | rip.add_vpp_config() |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 115 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 116 | r = VppVxlanTunnel( |
| 117 | cls, |
| 118 | src=cls.pg0.local_ip4, |
| 119 | src_port=port, |
| 120 | dst_port=port, |
| 121 | dst=dest_ip4, |
| 122 | vni=vni, |
| 123 | ) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 124 | r.add_vpp_config() |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 125 | cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 126 | |
| 127 | @classmethod |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 128 | def add_del_shared_mcast_dst_load(cls, port, is_add): |
Eyal Bari | 4bce290 | 2017-01-16 12:02:46 +0200 | [diff] [blame] | 129 | """ |
| 130 | add or del tunnels sharing the same mcast dst |
| 131 | to test vxlan ref_count mechanism |
| 132 | """ |
Gabriel Ganne | 7e665d6 | 2017-11-17 09:18:53 +0100 | [diff] [blame] | 133 | n_shared_dst_tunnels = 20 |
Eyal Bari | 4bce290 | 2017-01-16 12:02:46 +0200 | [diff] [blame] | 134 | vni_start = 10000 |
| 135 | vni_end = vni_start + n_shared_dst_tunnels |
| 136 | for vni in range(vni_start, vni_end): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 137 | r = VppVxlanTunnel( |
| 138 | cls, |
| 139 | src=cls.pg0.local_ip4, |
| 140 | src_port=port, |
| 141 | dst_port=port, |
| 142 | dst=cls.mcast_ip4, |
| 143 | mcast_sw_if_index=1, |
| 144 | vni=vni, |
| 145 | ) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 146 | if is_add: |
| 147 | r.add_vpp_config() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 148 | if r.sw_if_index == 0xFFFFFFFF: |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 149 | raise ValueError("bad sw_if_index: ~0") |
| 150 | else: |
| 151 | r.remove_vpp_config() |
Eyal Bari | 4bce290 | 2017-01-16 12:02:46 +0200 | [diff] [blame] | 152 | |
| 153 | @classmethod |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 154 | def add_shared_mcast_dst_load(cls, port): |
| 155 | cls.add_del_shared_mcast_dst_load(port=port, is_add=1) |
Eyal Bari | 4bce290 | 2017-01-16 12:02:46 +0200 | [diff] [blame] | 156 | |
| 157 | @classmethod |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 158 | def del_shared_mcast_dst_load(cls, port): |
| 159 | cls.add_del_shared_mcast_dst_load(port=port, is_add=0) |
Eyal Bari | 4bce290 | 2017-01-16 12:02:46 +0200 | [diff] [blame] | 160 | |
| 161 | @classmethod |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 162 | def add_del_mcast_tunnels_load(cls, port, is_add): |
Eyal Bari | 4bce290 | 2017-01-16 12:02:46 +0200 | [diff] [blame] | 163 | """ |
| 164 | add or del tunnels to test vxlan stability |
| 165 | """ |
| 166 | n_distinct_dst_tunnels = 200 |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 167 | ip_range_start = 10 |
Eyal Bari | 4bce290 | 2017-01-16 12:02:46 +0200 | [diff] [blame] | 168 | ip_range_end = ip_range_start + n_distinct_dst_tunnels |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 169 | for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start, ip_range_end): |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 170 | vni = bytearray(socket.inet_pton(socket.AF_INET, dest_ip4))[3] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 171 | r = VppVxlanTunnel( |
| 172 | cls, |
| 173 | src=cls.pg0.local_ip4, |
| 174 | src_port=port, |
| 175 | dst_port=port, |
| 176 | dst=dest_ip4, |
| 177 | mcast_sw_if_index=1, |
| 178 | vni=vni, |
| 179 | ) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 180 | if is_add: |
| 181 | r.add_vpp_config() |
| 182 | else: |
| 183 | r.remove_vpp_config() |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 184 | |
| 185 | @classmethod |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 186 | def add_mcast_tunnels_load(cls, port): |
| 187 | cls.add_del_mcast_tunnels_load(port=port, is_add=1) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 188 | |
| 189 | @classmethod |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 190 | def del_mcast_tunnels_load(cls, port): |
| 191 | cls.add_del_mcast_tunnels_load(port=port, is_add=0) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 192 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 193 | # Class method to start the VXLAN test case. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 194 | # Overrides setUpClass method in VppTestCase class. |
| 195 | # Python try..except statement is used to ensure that the tear down of |
| 196 | # the class will be executed even if exception is raised. |
| 197 | # @param cls The class pointer. |
| 198 | @classmethod |
| 199 | def setUpClass(cls): |
| 200 | super(TestVxlan, cls).setUpClass() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 201 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 202 | try: |
Matej Klotton | deb6984 | 2016-12-09 15:05:46 +0100 | [diff] [blame] | 203 | cls.flags = 0x8 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 204 | |
| 205 | # Create 2 pg interfaces. |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 206 | cls.create_pg_interfaces(range(4)) |
| 207 | for pg in cls.pg_interfaces: |
| 208 | pg.admin_up() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 209 | |
| 210 | # Configure IPv4 addresses on VPP pg0. |
| 211 | cls.pg0.config_ip4() |
| 212 | |
| 213 | # Resolve MAC address for VPP's IP address on pg0. |
| 214 | cls.pg0.resolve_arp() |
| 215 | |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 216 | # Our Multicast address |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 217 | cls.mcast_ip4 = "239.1.1.1" |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 218 | cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 219 | except Exception: |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 220 | cls.tearDownClass() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 221 | raise |
| 222 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 223 | @classmethod |
| 224 | def tearDownClass(cls): |
| 225 | super(TestVxlan, cls).tearDownClass() |
| 226 | |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 227 | def setUp(self): |
| 228 | super(TestVxlan, self).setUp() |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 229 | |
| 230 | def createVxLANInterfaces(self, port=4789): |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 231 | # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1 |
| 232 | # into BD. |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 233 | self.dport = port |
| 234 | |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 235 | self.single_tunnel_vni = 0x12345 |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 236 | self.single_tunnel_bd = 1 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 237 | r = VppVxlanTunnel( |
| 238 | self, |
| 239 | src=self.pg0.local_ip4, |
| 240 | dst=self.pg0.remote_ip4, |
| 241 | src_port=self.dport, |
| 242 | dst_port=self.dport, |
| 243 | vni=self.single_tunnel_vni, |
| 244 | ) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 245 | r.add_vpp_config() |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 246 | self.vapi.sw_interface_set_l2_bridge( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 247 | rx_sw_if_index=r.sw_if_index, bd_id=self.single_tunnel_bd |
| 248 | ) |
| 249 | self.vapi.sw_interface_set_l2_bridge( |
| 250 | rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd |
| 251 | ) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 252 | |
| 253 | # Setup vni 2 to test multicast flooding |
| 254 | self.n_ucast_tunnels = 10 |
| 255 | self.mcast_flood_bd = 2 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 256 | self.create_vxlan_flood_test_bd( |
| 257 | self.mcast_flood_bd, self.n_ucast_tunnels, self.dport |
| 258 | ) |
| 259 | r = VppVxlanTunnel( |
| 260 | self, |
| 261 | src=self.pg0.local_ip4, |
| 262 | dst=self.mcast_ip4, |
| 263 | src_port=self.dport, |
| 264 | dst_port=self.dport, |
| 265 | mcast_sw_if_index=1, |
| 266 | vni=self.mcast_flood_bd, |
| 267 | ) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 268 | r.add_vpp_config() |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 269 | self.vapi.sw_interface_set_l2_bridge( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 270 | rx_sw_if_index=r.sw_if_index, bd_id=self.mcast_flood_bd |
| 271 | ) |
| 272 | self.vapi.sw_interface_set_l2_bridge( |
| 273 | rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd |
| 274 | ) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 275 | |
| 276 | # Add and delete mcast tunnels to check stability |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 277 | self.add_shared_mcast_dst_load(self.dport) |
| 278 | self.add_mcast_tunnels_load(self.dport) |
| 279 | self.del_shared_mcast_dst_load(self.dport) |
| 280 | self.del_mcast_tunnels_load(self.dport) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 281 | |
| 282 | # Setup vni 3 to test unicast flooding |
| 283 | self.ucast_flood_bd = 3 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 284 | self.create_vxlan_flood_test_bd( |
| 285 | self.ucast_flood_bd, self.n_ucast_tunnels, self.dport |
| 286 | ) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 287 | self.vapi.sw_interface_set_l2_bridge( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 288 | rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd |
| 289 | ) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 290 | |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 291 | # Set scapy listen custom port for VxLAN |
| 292 | bind_layers(UDP, VXLAN, dport=self.dport) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 293 | |
Vladislav Grishenko | f2fc97a | 2024-01-24 20:33:12 +0500 | [diff] [blame] | 294 | def encap_packets(self): |
| 295 | def encap_frames(frame, n=10): |
| 296 | frames = [] |
| 297 | |
| 298 | # Provide IP flow hash difference. |
| 299 | for i in range(n): |
| 300 | p = frame.copy() |
| 301 | p[UDP].dport += i |
| 302 | frames.append(p) |
| 303 | |
| 304 | self.pg1.add_stream(frames) |
| 305 | |
| 306 | self.pg0.enable_capture() |
| 307 | self.pg_start() |
| 308 | |
| 309 | # Pick received frames and check if they're correctly encapsulated. |
| 310 | out = self.pg0.get_capture(n) |
| 311 | sports = set() |
| 312 | for i in range(n): |
| 313 | pkt = out[i] |
| 314 | self.check_encapsulation(pkt, self.single_tunnel_vni) |
| 315 | |
| 316 | payload = self.decapsulate(pkt) |
| 317 | self.assert_eq_pkts(payload, frames[i]) |
| 318 | |
| 319 | sports.add(pkt[UDP].sport) |
| 320 | |
| 321 | # Check src port randomization presence, not concerned with the |
| 322 | # src ports split ratio, just as long as there are more then one. |
| 323 | self.assertGreaterEqual(len(sports), min(n, 2)) |
| 324 | |
| 325 | frame_ip4 = ( |
| 326 | Ether(src="00:00:00:00:00:02", dst="00:00:00:00:00:01") |
| 327 | / IP(src="4.3.2.1", dst="1.2.3.4") |
| 328 | / UDP(sport=20000, dport=10000) |
| 329 | / Raw("\xa5" * 100) |
| 330 | ) |
| 331 | encap_frames(frame_ip4) |
| 332 | |
| 333 | frame_ip6 = ( |
| 334 | Ether(src="00:00:00:00:00:02", dst="00:00:00:00:00:01") |
| 335 | / IPv6(src="2001:db8::4321", dst="2001:db8::1234") |
| 336 | / UDP(sport=20000, dport=10000) |
| 337 | / Raw("\xa5" * 100) |
| 338 | ) |
| 339 | encap_frames(frame_ip6) |
| 340 | |
| 341 | frame_mpls4 = ( |
| 342 | Ether(src="00:00:00:00:00:02", dst="00:00:00:00:00:01") |
| 343 | / MPLS(label=44, ttl=64) |
| 344 | / IP(src="4.3.2.1", dst="1.2.3.4") |
| 345 | / UDP(sport=20000, dport=10000) |
| 346 | / Raw("\xa5" * 100) |
| 347 | ) |
| 348 | encap_frames(frame_mpls4) |
| 349 | |
| 350 | frame_mpls6 = ( |
| 351 | Ether(src="00:00:00:00:00:02", dst="00:00:00:00:00:01") |
| 352 | / MPLS(label=44, ttl=64) |
| 353 | / IPv6(src="2001:db8::4321", dst="2001:db8::1234") |
| 354 | / UDP(sport=20000, dport=10000) |
| 355 | / Raw("\xa5" * 100) |
| 356 | ) |
| 357 | encap_frames(frame_mpls6) |
| 358 | |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 359 | def encap_big_packet(self): |
Ole Troan | b3655e5 | 2018-08-16 22:08:49 +0200 | [diff] [blame] | 360 | self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0]) |
| 361 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 362 | frame = ( |
| 363 | Ether(src="00:00:00:00:00:02", dst="00:00:00:00:00:01") |
| 364 | / IP(src="4.3.2.1", dst="1.2.3.4") |
| 365 | / UDP(sport=20000, dport=10000) |
| 366 | / Raw(b"\xa5" * 1450) |
| 367 | ) |
Ole Troan | b3655e5 | 2018-08-16 22:08:49 +0200 | [diff] [blame] | 368 | |
| 369 | self.pg1.add_stream([frame]) |
| 370 | |
| 371 | self.pg0.enable_capture() |
| 372 | |
| 373 | self.pg_start() |
| 374 | |
| 375 | # Pick first received frame and check if it's correctly encapsulated. |
| 376 | out = self.pg0.get_capture(2) |
| 377 | ether = out[0] |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 378 | pkt = reassemble4(out) |
Ole Troan | b3655e5 | 2018-08-16 22:08:49 +0200 | [diff] [blame] | 379 | pkt = ether / pkt |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 380 | self.check_encapsulation(pkt, self.single_tunnel_vni) |
Ole Troan | b3655e5 | 2018-08-16 22:08:49 +0200 | [diff] [blame] | 381 | |
| 382 | payload = self.decapsulate(pkt) |
| 383 | # TODO: Scapy bug? |
| 384 | # self.assert_eq_pkts(payload, frame) |
| 385 | |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 386 | """ |
| 387 | Tests with default port (4789) |
| 388 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 389 | |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 390 | def test_decap(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 391 | """Decapsulation test |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 392 | from BridgeDoman |
| 393 | """ |
| 394 | self.createVxLANInterfaces() |
| 395 | super(TestVxlan, self).test_decap() |
| 396 | |
| 397 | def test_encap(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 398 | """Encapsulation test |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 399 | from BridgeDoman |
| 400 | """ |
| 401 | self.createVxLANInterfaces() |
Vladislav Grishenko | f2fc97a | 2024-01-24 20:33:12 +0500 | [diff] [blame] | 402 | self.encap_packets() |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 403 | |
| 404 | def test_encap_big_packet(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 405 | """Encapsulation test send big frame from pg1 |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 406 | Verify receipt of encapsulated frames on pg0 |
| 407 | """ |
| 408 | self.createVxLANInterfaces() |
| 409 | self.encap_big_packet() |
| 410 | |
| 411 | def test_ucast_flood(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 412 | """Unicast flood test |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 413 | from BridgeDoman |
| 414 | """ |
| 415 | self.createVxLANInterfaces() |
| 416 | super(TestVxlan, self).test_ucast_flood() |
| 417 | |
| 418 | def test_mcast_flood(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 419 | """Multicast flood test |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 420 | from BridgeDoman |
| 421 | """ |
| 422 | self.createVxLANInterfaces() |
| 423 | super(TestVxlan, self).test_mcast_flood() |
| 424 | |
| 425 | def test_mcast_rcv(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 426 | """Multicast receive test |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 427 | from BridgeDoman |
| 428 | """ |
| 429 | self.createVxLANInterfaces() |
| 430 | super(TestVxlan, self).test_mcast_rcv() |
| 431 | |
| 432 | """ |
| 433 | Tests with custom port |
| 434 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 435 | |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 436 | def test_decap_custom_port(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 437 | """Decapsulation test custom port |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 438 | from BridgeDoman |
| 439 | """ |
| 440 | self.createVxLANInterfaces(1111) |
| 441 | super(TestVxlan, self).test_decap() |
| 442 | |
| 443 | def test_encap_custom_port(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 444 | """Encapsulation test custom port |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 445 | from BridgeDoman |
| 446 | """ |
| 447 | self.createVxLANInterfaces(1111) |
| 448 | super(TestVxlan, self).test_encap() |
| 449 | |
| 450 | def test_ucast_flood_custom_port(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 451 | """Unicast flood test custom port |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 452 | from BridgeDoman |
| 453 | """ |
| 454 | self.createVxLANInterfaces(1111) |
| 455 | super(TestVxlan, self).test_ucast_flood() |
| 456 | |
| 457 | def test_mcast_flood_custom_port(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 458 | """Multicast flood test custom port |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 459 | from BridgeDoman |
| 460 | """ |
| 461 | self.createVxLANInterfaces(1111) |
| 462 | super(TestVxlan, self).test_mcast_flood() |
| 463 | |
| 464 | def test_mcast_rcv_custom_port(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 465 | """Multicast receive test custom port |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 466 | from BridgeDoman |
| 467 | """ |
| 468 | self.createVxLANInterfaces(1111) |
| 469 | super(TestVxlan, self).test_mcast_rcv() |
| 470 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 471 | # Method to define VPP actions before tear down of the test case. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 472 | # Overrides tearDown method in VppTestCase class. |
| 473 | # @param self The object pointer. |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 474 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 475 | def tearDown(self): |
| 476 | super(TestVxlan, self).tearDown() |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 477 | |
| 478 | def show_commands_at_teardown(self): |
| 479 | self.logger.info(self.vapi.cli("show bridge-domain 1 detail")) |
| 480 | self.logger.info(self.vapi.cli("show bridge-domain 2 detail")) |
| 481 | self.logger.info(self.vapi.cli("show bridge-domain 3 detail")) |
| 482 | self.logger.info(self.vapi.cli("show vxlan tunnel")) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 483 | |
Matej Klotton | deb6984 | 2016-12-09 15:05:46 +0100 | [diff] [blame] | 484 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 485 | @unittest.skipIf("vxlan" in config.excluded_plugins, "Exclude VXLAN plugin tests") |
Neale Ranns | 1b5ca98 | 2020-12-16 13:06:58 +0000 | [diff] [blame] | 486 | class TestVxlan2(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 487 | """VXLAN Test Case""" |
| 488 | |
Neale Ranns | 1b5ca98 | 2020-12-16 13:06:58 +0000 | [diff] [blame] | 489 | def setUp(self): |
| 490 | super(TestVxlan2, self).setUp() |
| 491 | |
| 492 | # Create 2 pg interfaces. |
| 493 | self.create_pg_interfaces(range(4)) |
| 494 | for pg in self.pg_interfaces: |
| 495 | pg.admin_up() |
| 496 | |
| 497 | # Configure IPv4 addresses on VPP pg0. |
| 498 | self.pg0.config_ip4() |
| 499 | self.pg0.resolve_arp() |
| 500 | |
| 501 | def tearDown(self): |
| 502 | super(TestVxlan2, self).tearDown() |
| 503 | |
| 504 | def test_xconnect(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 505 | """VXLAN source address not local""" |
Neale Ranns | 1b5ca98 | 2020-12-16 13:06:58 +0000 | [diff] [blame] | 506 | |
| 507 | # |
| 508 | # test the broken configuration of a VXLAN tunnel whose |
| 509 | # source address is not local ot the box. packets sent |
| 510 | # through the tunnel should be dropped |
| 511 | # |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 512 | t = VppVxlanTunnel(self, src="10.0.0.5", dst=self.pg0.local_ip4, vni=1000) |
Neale Ranns | 1b5ca98 | 2020-12-16 13:06:58 +0000 | [diff] [blame] | 513 | t.add_vpp_config() |
| 514 | t.admin_up() |
| 515 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 516 | self.vapi.sw_interface_set_l2_xconnect( |
| 517 | t.sw_if_index, self.pg1.sw_if_index, enable=1 |
| 518 | ) |
| 519 | self.vapi.sw_interface_set_l2_xconnect( |
| 520 | self.pg1.sw_if_index, t.sw_if_index, enable=1 |
| 521 | ) |
Neale Ranns | 1b5ca98 | 2020-12-16 13:06:58 +0000 | [diff] [blame] | 522 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 523 | p = ( |
| 524 | Ether(src="00:11:22:33:44:55", dst="00:00:00:11:22:33") |
| 525 | / IP(src="4.3.2.1", dst="1.2.3.4") |
| 526 | / UDP(sport=20000, dport=10000) |
| 527 | / Raw(b"\xa5" * 1450) |
| 528 | ) |
Neale Ranns | 1b5ca98 | 2020-12-16 13:06:58 +0000 | [diff] [blame] | 529 | |
| 530 | rx = self.send_and_assert_no_replies(self.pg1, [p]) |
| 531 | |
| 532 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 533 | @unittest.skipIf("vxlan" in config.excluded_plugins, "Exclude VXLAN plugin tests") |
Artem Glazychev | 23e5f09 | 2022-02-21 17:51:29 +0700 | [diff] [blame] | 534 | class TestVxlanL2Mode(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 535 | """VXLAN Test Case""" |
| 536 | |
Artem Glazychev | 23e5f09 | 2022-02-21 17:51:29 +0700 | [diff] [blame] | 537 | def setUp(self): |
| 538 | super(TestVxlanL2Mode, self).setUp() |
| 539 | |
| 540 | # Create 2 pg interfaces. |
| 541 | self.create_pg_interfaces(range(2)) |
| 542 | for pg in self.pg_interfaces: |
| 543 | pg.admin_up() |
| 544 | |
| 545 | # Configure IPv4 addresses on VPP pg0. |
| 546 | self.pg0.config_ip4() |
| 547 | self.pg0.resolve_arp() |
| 548 | |
| 549 | # Configure IPv4 addresses on VPP pg1. |
| 550 | self.pg1.config_ip4() |
| 551 | |
| 552 | def tearDown(self): |
| 553 | super(TestVxlanL2Mode, self).tearDown() |
| 554 | |
| 555 | def test_l2_mode(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 556 | """VXLAN L2 mode""" |
| 557 | t = VppVxlanTunnel( |
| 558 | self, src=self.pg0.local_ip4, dst=self.pg0.remote_ip4, vni=1000, is_l3=False |
| 559 | ) |
Artem Glazychev | 23e5f09 | 2022-02-21 17:51:29 +0700 | [diff] [blame] | 560 | t.add_vpp_config() |
| 561 | t.config_ip4() |
| 562 | t.admin_up() |
| 563 | |
| 564 | dstIP = t.local_ip4[:-1] + "2" |
| 565 | |
| 566 | # Create a packet to send |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 567 | p = ( |
| 568 | Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) |
| 569 | / IP(src=self.pg1.local_ip4, dst=dstIP) |
| 570 | / UDP(sport=555, dport=556) |
| 571 | / Raw(b"\x00" * 80) |
| 572 | ) |
Artem Glazychev | 23e5f09 | 2022-02-21 17:51:29 +0700 | [diff] [blame] | 573 | |
| 574 | # Expect ARP request |
| 575 | rx = self.send_and_expect(self.pg1, [p], self.pg0) |
| 576 | for p in rx: |
| 577 | self.assertEqual(p[Ether].dst, self.pg0.remote_mac) |
| 578 | self.assertEqual(p[Ether].src, self.pg0.local_mac) |
| 579 | self.assertEqual(p[ARP].op, 1) |
| 580 | self.assertEqual(p[ARP].pdst, dstIP) |
| 581 | |
| 582 | # Resolve ARP |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 583 | VppNeighbor(self, t.sw_if_index, self.pg1.remote_mac, dstIP).add_vpp_config() |
Artem Glazychev | 23e5f09 | 2022-02-21 17:51:29 +0700 | [diff] [blame] | 584 | |
| 585 | # Send packets |
| 586 | NUM_PKTS = 128 |
Steven Luong | e4238aa | 2024-04-19 09:49:20 -0700 | [diff] [blame] | 587 | p.dst = self.pg1.local_mac |
Artem Glazychev | 23e5f09 | 2022-02-21 17:51:29 +0700 | [diff] [blame] | 588 | rx = self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg0) |
| 589 | self.assertEqual(NUM_PKTS, len(rx)) |
| 590 | |
| 591 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 592 | if __name__ == "__main__": |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 593 | unittest.main(testRunner=VppTestRunner) |