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