Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 2 | |
| 3 | import socket |
| 4 | import unittest |
| 5 | from framework import VppTestCase, VppTestRunner |
| 6 | from template_bd import BridgeDomain |
| 7 | |
| 8 | from scapy.layers.l2 import Ether |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 9 | from scapy.packet import Raw, bind_layers |
Vladimir Isaev | 698eb87 | 2020-05-21 16:34:17 +0300 | [diff] [blame] | 10 | from scapy.layers.inet6 import IP, IPv6, UDP |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 11 | from scapy.layers.vxlan import VXLAN |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 12 | |
| 13 | import util |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 14 | from vpp_ip_route import VppIpRoute, VppRoutePath |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 15 | from vpp_vxlan_tunnel import VppVxlanTunnel |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 16 | from vpp_ip import INVALID_INDEX |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 17 | |
| 18 | |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 19 | class TestVxlan6(BridgeDomain, VppTestCase): |
| 20 | """ VXLAN over IPv6 Test Case """ |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 21 | |
| 22 | def __init__(self, *args): |
| 23 | BridgeDomain.__init__(self) |
| 24 | VppTestCase.__init__(self, *args) |
| 25 | |
| 26 | def encapsulate(self, pkt, vni): |
| 27 | """ |
| 28 | Encapsulate the original payload frame by adding VXLAN header with its |
| 29 | UDP, IP and Ethernet fields |
| 30 | """ |
| 31 | return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 32 | IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) / |
| 33 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
| 34 | VXLAN(vni=vni, flags=self.flags) / |
| 35 | pkt) |
| 36 | |
| 37 | @classmethod |
| 38 | def ip_range(cls, s, e): |
| 39 | """ range of remote ip's """ |
| 40 | tmp = cls.pg0.remote_ip6.rsplit(':', 1)[0] |
| 41 | return ("%s:%x" % (tmp, i) for i in range(s, e)) |
| 42 | |
| 43 | def encap_mcast(self, pkt, src_ip, src_mac, vni): |
| 44 | """ |
| 45 | Encapsulate the original payload frame by adding VXLAN header with its |
| 46 | UDP, IP and Ethernet fields |
| 47 | """ |
| 48 | return (Ether(src=src_mac, dst=self.mcast_mac) / |
| 49 | IPv6(src=src_ip, dst=self.mcast_ip6) / |
| 50 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
| 51 | VXLAN(vni=vni, flags=self.flags) / |
| 52 | pkt) |
| 53 | |
| 54 | def decapsulate(self, pkt): |
| 55 | """ |
| 56 | Decapsulate the original payload frame by removing VXLAN header |
| 57 | """ |
| 58 | # check if is set I flag |
| 59 | self.assertEqual(pkt[VXLAN].flags, int('0x8', 16)) |
| 60 | return pkt[VXLAN].payload |
| 61 | |
| 62 | # Method for checking VXLAN encapsulation. |
| 63 | # |
| 64 | def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False): |
| 65 | # TODO: add error messages |
| 66 | # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved |
| 67 | # by VPP using ARP. |
| 68 | self.assertEqual(pkt[Ether].src, self.pg0.local_mac) |
| 69 | if not local_only: |
| 70 | if not mcast_pkt: |
| 71 | self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) |
| 72 | else: |
| 73 | self.assertEqual(pkt[Ether].dst, type(self).mcast_mac) |
| 74 | # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP. |
| 75 | self.assertEqual(pkt[IPv6].src, self.pg0.local_ip6) |
| 76 | if not local_only: |
| 77 | if not mcast_pkt: |
| 78 | self.assertEqual(pkt[IPv6].dst, self.pg0.remote_ip6) |
| 79 | else: |
| 80 | self.assertEqual(pkt[IPv6].dst, type(self).mcast_ip6) |
| 81 | # Verify UDP destination port is VXLAN 4789, source UDP port could be |
| 82 | # arbitrary. |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 83 | self.assertEqual(pkt[UDP].dport, self.dport) |
Vladimir Isaev | 698eb87 | 2020-05-21 16:34:17 +0300 | [diff] [blame] | 84 | # Verify UDP checksum |
| 85 | self.assert_udp_checksum_valid(pkt, ignore_zero_checksum=False) |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 86 | # Verify VNI |
| 87 | self.assertEqual(pkt[VXLAN].vni, vni) |
| 88 | |
| 89 | @classmethod |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 90 | def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels, port): |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 91 | # Create 10 ucast vxlan tunnels under bd |
| 92 | start = 10 |
| 93 | end = start + n_ucast_tunnels |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 94 | for dest_ip6 in cls.ip_range(start, end): |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 95 | # add host route so dest ip will not be resolved |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 96 | rip = VppIpRoute(cls, dest_ip6, 128, |
| 97 | [VppRoutePath(cls.pg0.remote_ip6, INVALID_INDEX)], |
| 98 | register=False) |
| 99 | rip.add_vpp_config() |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 100 | r = VppVxlanTunnel(cls, src=cls.pg0.local_ip6, |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 101 | src_port=port, dst_port=port, |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 102 | dst=dest_ip6, vni=vni) |
| 103 | r.add_vpp_config() |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 104 | cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni) |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 105 | |
| 106 | @classmethod |
| 107 | def add_mcast_tunnels_load(cls): |
| 108 | cls.add_del_mcast_tunnels_load(is_add=1) |
| 109 | |
| 110 | @classmethod |
| 111 | def del_mcast_tunnels_load(cls): |
| 112 | cls.add_del_mcast_tunnels_load(is_add=0) |
| 113 | |
| 114 | # Class method to start the VXLAN test case. |
| 115 | # Overrides setUpClass method in VppTestCase class. |
| 116 | # Python try..except statement is used to ensure that the tear down of |
| 117 | # the class will be executed even if exception is raised. |
| 118 | # @param cls The class pointer. |
| 119 | @classmethod |
| 120 | def setUpClass(cls): |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 121 | super(TestVxlan6, cls).setUpClass() |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 122 | |
| 123 | try: |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 124 | cls.flags = 0x8 |
| 125 | |
| 126 | # Create 2 pg interfaces. |
| 127 | cls.create_pg_interfaces(range(4)) |
| 128 | for pg in cls.pg_interfaces: |
| 129 | pg.admin_up() |
| 130 | |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 131 | # Configure IPv6 addresses on VPP pg0. |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 132 | cls.pg0.config_ip6() |
| 133 | |
| 134 | # Resolve MAC address for VPP's IP address on pg0. |
| 135 | cls.pg0.resolve_ndp() |
| 136 | |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 137 | # Our Multicast address |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 138 | cls.mcast_ip6 = 'ff0e::1' |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 139 | cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip6) |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 140 | except Exception: |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 141 | super(TestVxlan6, cls).tearDownClass() |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 142 | raise |
| 143 | |
Paul Vinciguerra | 8d991d9 | 2019-01-25 14:05:48 -0800 | [diff] [blame] | 144 | @classmethod |
| 145 | def tearDownClass(cls): |
| 146 | super(TestVxlan6, cls).tearDownClass() |
| 147 | |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 148 | def setUp(self): |
| 149 | super(TestVxlan6, self).setUp() |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 150 | |
| 151 | def createVxLANInterfaces(self, port=4789): |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 152 | # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1 |
| 153 | # into BD. |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 154 | self.dport = port |
| 155 | |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 156 | self.single_tunnel_vni = 0x12345 |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 157 | self.single_tunnel_bd = 1 |
| 158 | r = VppVxlanTunnel(self, src=self.pg0.local_ip6, |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 159 | dst=self.pg0.remote_ip6, |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 160 | src_port=self.dport, dst_port=self.dport, |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 161 | vni=self.single_tunnel_vni) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 162 | r.add_vpp_config() |
| 163 | self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
| 164 | bd_id=self.single_tunnel_bd) |
| 165 | self.vapi.sw_interface_set_l2_bridge( |
| 166 | rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd) |
| 167 | |
| 168 | # Setup vni 2 to test multicast flooding |
| 169 | self.n_ucast_tunnels = 10 |
| 170 | self.mcast_flood_bd = 2 |
| 171 | self.create_vxlan_flood_test_bd(self.mcast_flood_bd, |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 172 | self.n_ucast_tunnels, |
| 173 | self.dport) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 174 | r = VppVxlanTunnel(self, src=self.pg0.local_ip6, dst=self.mcast_ip6, |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 175 | src_port=self.dport, dst_port=self.dport, |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 176 | mcast_sw_if_index=1, vni=self.mcast_flood_bd) |
| 177 | r.add_vpp_config() |
| 178 | self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
| 179 | bd_id=self.mcast_flood_bd) |
| 180 | self.vapi.sw_interface_set_l2_bridge( |
| 181 | rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd) |
| 182 | |
| 183 | # Setup vni 3 to test unicast flooding |
| 184 | self.ucast_flood_bd = 3 |
| 185 | self.create_vxlan_flood_test_bd(self.ucast_flood_bd, |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 186 | self.n_ucast_tunnels, |
| 187 | self.dport) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 188 | self.vapi.sw_interface_set_l2_bridge( |
| 189 | rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd) |
| 190 | |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 191 | # Set scapy listen custom port for VxLAN |
| 192 | bind_layers(UDP, VXLAN, dport=self.dport) |
| 193 | |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 194 | # Method to define VPP actions before tear down of the test case. |
| 195 | # Overrides tearDown method in VppTestCase class. |
| 196 | # @param self The object pointer. |
| 197 | def tearDown(self): |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 198 | super(TestVxlan6, self).tearDown() |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 199 | |
| 200 | def show_commands_at_teardown(self): |
| 201 | self.logger.info(self.vapi.cli("show bridge-domain 1 detail")) |
| 202 | self.logger.info(self.vapi.cli("show bridge-domain 2 detail")) |
| 203 | self.logger.info(self.vapi.cli("show bridge-domain 3 detail")) |
| 204 | self.logger.info(self.vapi.cli("show vxlan tunnel")) |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 205 | |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 206 | def encap_fragmented_packet(self): |
Vladimir Isaev | 698eb87 | 2020-05-21 16:34:17 +0300 | [diff] [blame] | 207 | frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') / |
| 208 | IP(src='4.3.2.1', dst='1.2.3.4') / |
| 209 | UDP(sport=20000, dport=10000) / |
| 210 | Raw(b'\xa5' * 1000)) |
| 211 | |
| 212 | frags = util.fragment_rfc791(frame, 400) |
| 213 | |
| 214 | self.pg1.add_stream(frags) |
| 215 | |
| 216 | self.pg0.enable_capture() |
| 217 | |
| 218 | self.pg_start() |
| 219 | |
| 220 | out = self.pg0.get_capture(3) |
| 221 | |
| 222 | payload = [] |
| 223 | for pkt in out: |
| 224 | payload.append(self.decapsulate(pkt)) |
| 225 | self.check_encapsulation(pkt, self.single_tunnel_vni) |
| 226 | |
| 227 | reassembled = util.reassemble4(payload) |
| 228 | |
| 229 | self.assertEqual(Ether(raw(frame))[IP], reassembled[IP]) |
| 230 | |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 231 | """ |
| 232 | Tests with default port (4789) |
| 233 | """ |
| 234 | def test_decap(self): |
| 235 | """ Decapsulation test |
| 236 | from BridgeDoman |
| 237 | """ |
| 238 | self.createVxLANInterfaces() |
| 239 | super(TestVxlan6, self).test_decap() |
| 240 | |
| 241 | def test_encap(self): |
| 242 | """ Encapsulation test |
| 243 | from BridgeDoman |
| 244 | """ |
| 245 | self.createVxLANInterfaces() |
| 246 | super(TestVxlan6, self).test_encap() |
| 247 | |
| 248 | def test_encap_fragmented_packet(self): |
| 249 | """ Encapsulation test send fragments from pg1 |
| 250 | Verify receipt of encapsulated frames on pg0 |
| 251 | """ |
| 252 | self.createVxLANInterfaces() |
| 253 | self.encap_fragmented_packet() |
| 254 | |
| 255 | def test_ucast_flood(self): |
| 256 | """ Unicast flood test |
| 257 | from BridgeDoman |
| 258 | """ |
| 259 | self.createVxLANInterfaces() |
| 260 | super(TestVxlan6, self).test_ucast_flood() |
| 261 | |
| 262 | def test_mcast_flood(self): |
| 263 | """ Multicast flood test |
| 264 | from BridgeDoman |
| 265 | """ |
| 266 | self.createVxLANInterfaces() |
| 267 | super(TestVxlan6, self).test_mcast_flood() |
| 268 | |
| 269 | def test_mcast_rcv(self): |
| 270 | """ Multicast receive test |
| 271 | from BridgeDoman |
| 272 | """ |
| 273 | self.createVxLANInterfaces() |
| 274 | super(TestVxlan6, self).test_mcast_rcv() |
| 275 | |
| 276 | """ |
| 277 | Tests with custom port |
| 278 | """ |
| 279 | def test_decap_custom_port(self): |
| 280 | """ Decapsulation test custom port |
| 281 | from BridgeDoman |
| 282 | """ |
| 283 | self.createVxLANInterfaces(1111) |
| 284 | super(TestVxlan6, self).test_decap() |
| 285 | |
| 286 | def test_encap_custom_port(self): |
| 287 | """ Encapsulation test custom port |
| 288 | from BridgeDoman |
| 289 | """ |
| 290 | self.createVxLANInterfaces(1111) |
| 291 | super(TestVxlan6, self).test_encap() |
| 292 | |
| 293 | def test_ucast_flood_custom_port(self): |
| 294 | """ Unicast flood test custom port |
| 295 | from BridgeDoman |
| 296 | """ |
| 297 | self.createVxLANInterfaces(1111) |
| 298 | super(TestVxlan6, self).test_ucast_flood() |
| 299 | |
| 300 | def test_mcast_flood_custom_port(self): |
| 301 | """ Multicast flood test custom port |
| 302 | from BridgeDoman |
| 303 | """ |
| 304 | self.createVxLANInterfaces(1111) |
| 305 | super(TestVxlan6, self).test_mcast_flood() |
| 306 | |
| 307 | def test_mcast_rcv_custom_port(self): |
| 308 | """ Multicast receive test custom port |
| 309 | from BridgeDoman |
| 310 | """ |
| 311 | self.createVxLANInterfaces(1111) |
| 312 | super(TestVxlan6, self).test_mcast_rcv() |
| 313 | |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 314 | |
| 315 | if __name__ == '__main__': |
| 316 | unittest.main(testRunner=VppTestRunner) |