Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 2 | |
| 3 | import socket |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 4 | from util import ip4_range |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 5 | import unittest |
| 6 | from framework import VppTestCase, VppTestRunner |
| 7 | from template_bd import BridgeDomain |
| 8 | |
Ole Troan | 7fc88cf | 2020-06-17 22:57:13 +0200 | [diff] [blame] | 9 | from scapy.layers.l2 import Ether, ARP |
| 10 | from scapy.layers.inet import IP, UDP, ICMP |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 11 | from scapy.contrib.geneve import GENEVE |
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 |
| 15 | from vpp_ip import INVALID_INDEX |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 16 | |
| 17 | |
| 18 | class TestGeneve(BridgeDomain, VppTestCase): |
| 19 | """ GENEVE Test Case """ |
| 20 | |
| 21 | def __init__(self, *args): |
| 22 | BridgeDomain.__init__(self) |
| 23 | VppTestCase.__init__(self, *args) |
| 24 | |
| 25 | def encapsulate(self, pkt, vni): |
| 26 | |
| 27 | """ |
| 28 | Encapsulate the original payload frame by adding GENEVE header with its |
| 29 | UDP, IP and Ethernet fields |
| 30 | """ |
| 31 | return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 32 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / |
| 33 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
| 34 | GENEVE(vni=vni) / |
| 35 | pkt) |
| 36 | |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 37 | def ip_range(self, start, end): |
| 38 | """ range of remote ip's """ |
| 39 | return ip4_range(self.pg0.remote_ip4, start, end) |
| 40 | |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 41 | def encap_mcast(self, pkt, src_ip, src_mac, vni): |
| 42 | """ |
| 43 | Encapsulate the original payload frame by adding GENEVE header with its |
| 44 | UDP, IP and Ethernet fields |
| 45 | """ |
| 46 | return (Ether(src=src_mac, dst=self.mcast_mac) / |
| 47 | IP(src=src_ip, dst=self.mcast_ip4) / |
| 48 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
| 49 | GENEVE(vni=vni) / |
| 50 | pkt) |
| 51 | |
| 52 | def decapsulate(self, pkt): |
| 53 | """ |
| 54 | Decapsulate the original payload frame by removing GENEVE header |
| 55 | """ |
| 56 | # check if is set I flag |
| 57 | # self.assertEqual(pkt[GENEVE].flags, int('0x8', 16)) |
| 58 | return pkt[GENEVE].payload |
| 59 | |
| 60 | # Method for checking GENEVE encapsulation. |
| 61 | # |
| 62 | def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False): |
| 63 | # TODO: add error messages |
| 64 | # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved |
| 65 | # by VPP using ARP. |
| 66 | self.assertEqual(pkt[Ether].src, self.pg0.local_mac) |
| 67 | if not local_only: |
| 68 | if not mcast_pkt: |
| 69 | self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) |
| 70 | else: |
| 71 | self.assertEqual(pkt[Ether].dst, type(self).mcast_mac) |
| 72 | # Verify GENEVE tunnel source IP is VPP_IP and destination IP is MY_IP. |
| 73 | self.assertEqual(pkt[IP].src, self.pg0.local_ip4) |
| 74 | if not local_only: |
| 75 | if not mcast_pkt: |
| 76 | self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4) |
| 77 | else: |
| 78 | self.assertEqual(pkt[IP].dst, type(self).mcast_ip4) |
| 79 | # Verify UDP destination port is GENEVE 4789, source UDP port could be |
| 80 | # arbitrary. |
| 81 | self.assertEqual(pkt[UDP].dport, type(self).dport) |
| 82 | # TODO: checksum check |
| 83 | # Verify VNI |
| 84 | self.assertEqual(pkt[GENEVE].vni, vni) |
| 85 | |
| 86 | @classmethod |
| 87 | def create_geneve_flood_test_bd(cls, vni, n_ucast_tunnels): |
| 88 | # Create 10 ucast geneve tunnels under bd |
| 89 | ip_range_start = 10 |
| 90 | ip_range_end = ip_range_start + n_ucast_tunnels |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 91 | next_hop_address = cls.pg0.remote_ip4 |
| 92 | for dest_ip4 in ip4_range(next_hop_address, ip_range_start, |
| 93 | ip_range_end): |
Jakub Grajciar | 2d3282e | 2019-10-01 12:04:56 +0200 | [diff] [blame] | 94 | # add host route so dest_ip4 will not be resolved |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 95 | rip = VppIpRoute(cls, dest_ip4, 32, |
| 96 | [VppRoutePath(next_hop_address, |
| 97 | INVALID_INDEX)], |
| 98 | register=False) |
| 99 | rip.add_vpp_config() |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 100 | r = cls.vapi.geneve_add_del_tunnel( |
Jakub Grajciar | 2d3282e | 2019-10-01 12:04:56 +0200 | [diff] [blame] | 101 | local_address=cls.pg0.local_ip4, remote_address=dest_ip4, |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 102 | vni=vni) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 103 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
| 104 | bd_id=vni) |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 105 | |
| 106 | @classmethod |
| 107 | def add_del_shared_mcast_dst_load(cls, is_add): |
| 108 | """ |
| 109 | add or del tunnels sharing the same mcast dst |
| 110 | to test geneve ref_count mechanism |
| 111 | """ |
Gabriel Ganne | 97cabc9 | 2018-02-08 11:22:33 +0100 | [diff] [blame] | 112 | n_shared_dst_tunnels = 10 |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 113 | vni_start = 10000 |
| 114 | vni_end = vni_start + n_shared_dst_tunnels |
| 115 | for vni in range(vni_start, vni_end): |
| 116 | r = cls.vapi.geneve_add_del_tunnel( |
Jakub Grajciar | 2d3282e | 2019-10-01 12:04:56 +0200 | [diff] [blame] | 117 | local_address=cls.pg0.local_ip4, |
| 118 | remote_address=cls.mcast_ip4, mcast_sw_if_index=1, |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 119 | is_add=is_add, vni=vni) |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 120 | if r.sw_if_index == 0xffffffff: |
Paul Vinciguerra | c599c6f | 2019-03-12 17:41:27 -0700 | [diff] [blame] | 121 | raise ValueError("bad sw_if_index: ~0") |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 122 | |
| 123 | @classmethod |
| 124 | def add_shared_mcast_dst_load(cls): |
| 125 | cls.add_del_shared_mcast_dst_load(is_add=1) |
| 126 | |
| 127 | @classmethod |
| 128 | def del_shared_mcast_dst_load(cls): |
| 129 | cls.add_del_shared_mcast_dst_load(is_add=0) |
| 130 | |
| 131 | @classmethod |
| 132 | def add_del_mcast_tunnels_load(cls, is_add): |
| 133 | """ |
| 134 | add or del tunnels to test geneve stability |
| 135 | """ |
Gabriel Ganne | 97cabc9 | 2018-02-08 11:22:33 +0100 | [diff] [blame] | 136 | n_distinct_dst_tunnels = 10 |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 137 | ip_range_start = 10 |
| 138 | ip_range_end = ip_range_start + n_distinct_dst_tunnels |
Jakub Grajciar | 2d3282e | 2019-10-01 12:04:56 +0200 | [diff] [blame] | 139 | for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start, |
| 140 | ip_range_end): |
Ole Troan | 6769473 | 2019-10-21 21:03:39 +0200 | [diff] [blame] | 141 | vni = int(dest_ip4.split('.')[3]) |
Jakub Grajciar | 2d3282e | 2019-10-01 12:04:56 +0200 | [diff] [blame] | 142 | cls.vapi.geneve_add_del_tunnel(local_address=cls.pg0.local_ip4, |
| 143 | remote_address=dest_ip4, |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 144 | mcast_sw_if_index=1, is_add=is_add, |
| 145 | vni=vni) |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 146 | |
| 147 | @classmethod |
| 148 | def add_mcast_tunnels_load(cls): |
| 149 | cls.add_del_mcast_tunnels_load(is_add=1) |
| 150 | |
| 151 | @classmethod |
| 152 | def del_mcast_tunnels_load(cls): |
| 153 | cls.add_del_mcast_tunnels_load(is_add=0) |
| 154 | |
| 155 | # Class method to start the GENEVE test case. |
| 156 | # Overrides setUpClass method in VppTestCase class. |
| 157 | # Python try..except statement is used to ensure that the tear down of |
| 158 | # the class will be executed even if exception is raised. |
| 159 | # @param cls The class pointer. |
| 160 | @classmethod |
| 161 | def setUpClass(cls): |
| 162 | super(TestGeneve, cls).setUpClass() |
| 163 | |
| 164 | try: |
| 165 | cls.dport = 6081 |
| 166 | |
| 167 | # Create 2 pg interfaces. |
| 168 | cls.create_pg_interfaces(range(4)) |
| 169 | for pg in cls.pg_interfaces: |
| 170 | pg.admin_up() |
| 171 | |
| 172 | # Configure IPv4 addresses on VPP pg0. |
| 173 | cls.pg0.config_ip4() |
| 174 | |
| 175 | # Resolve MAC address for VPP's IP address on pg0. |
| 176 | cls.pg0.resolve_arp() |
| 177 | |
| 178 | # Our Multicast address |
| 179 | cls.mcast_ip4 = '239.1.1.1' |
Paul Vinciguerra | 2f15631 | 2020-05-02 22:34:40 -0400 | [diff] [blame] | 180 | cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4) |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 181 | |
| 182 | # Create GENEVE VTEP on VPP pg0, and put geneve_tunnel0 and pg1 |
| 183 | # into BD. |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 184 | cls.single_tunnel_vni = 0xabcde |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 185 | cls.single_tunnel_bd = 1 |
| 186 | r = cls.vapi.geneve_add_del_tunnel( |
Jakub Grajciar | 2d3282e | 2019-10-01 12:04:56 +0200 | [diff] [blame] | 187 | local_address=cls.pg0.local_ip4, |
Neale Ranns | 91fd910 | 2020-04-03 07:46:28 +0000 | [diff] [blame] | 188 | remote_address=cls.pg0.remote_ip4, vni=cls.single_tunnel_vni) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 189 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 190 | bd_id=cls.single_tunnel_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 191 | cls.vapi.sw_interface_set_l2_bridge( |
| 192 | rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd) |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 193 | |
| 194 | # Setup vni 2 to test multicast flooding |
| 195 | cls.n_ucast_tunnels = 10 |
| 196 | cls.mcast_flood_bd = 2 |
| 197 | cls.create_geneve_flood_test_bd(cls.mcast_flood_bd, |
| 198 | cls.n_ucast_tunnels) |
| 199 | r = cls.vapi.geneve_add_del_tunnel( |
Jakub Grajciar | 2d3282e | 2019-10-01 12:04:56 +0200 | [diff] [blame] | 200 | local_address=cls.pg0.local_ip4, |
| 201 | remote_address=cls.mcast_ip4, mcast_sw_if_index=1, |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 202 | vni=cls.mcast_flood_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 203 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 204 | bd_id=cls.mcast_flood_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 205 | cls.vapi.sw_interface_set_l2_bridge( |
| 206 | rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd) |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 207 | |
| 208 | # Add and delete mcast tunnels to check stability |
| 209 | cls.add_shared_mcast_dst_load() |
| 210 | cls.add_mcast_tunnels_load() |
| 211 | cls.del_shared_mcast_dst_load() |
| 212 | cls.del_mcast_tunnels_load() |
| 213 | |
| 214 | # Setup vni 3 to test unicast flooding |
| 215 | cls.ucast_flood_bd = 3 |
| 216 | cls.create_geneve_flood_test_bd(cls.ucast_flood_bd, |
| 217 | cls.n_ucast_tunnels) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 218 | cls.vapi.sw_interface_set_l2_bridge( |
| 219 | rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd) |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 220 | except Exception: |
| 221 | super(TestGeneve, cls).tearDownClass() |
| 222 | raise |
| 223 | |
| 224 | # Method to define VPP actions before tear down of the test case. |
| 225 | # Overrides tearDown method in VppTestCase class. |
| 226 | # @param self The object pointer. |
| 227 | def tearDown(self): |
| 228 | super(TestGeneve, self).tearDown() |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 229 | |
| 230 | def show_commands_at_teardown(self): |
| 231 | self.logger.info(self.vapi.cli("show bridge-domain 1 detail")) |
| 232 | self.logger.info(self.vapi.cli("show bridge-domain 2 detail")) |
| 233 | self.logger.info(self.vapi.cli("show bridge-domain 3 detail")) |
| 234 | self.logger.info(self.vapi.cli("show geneve tunnel")) |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 235 | |
| 236 | |
Ole Troan | 7fc88cf | 2020-06-17 22:57:13 +0200 | [diff] [blame] | 237 | class TestGeneveL3(VppTestCase): |
| 238 | """ GENEVE L3 Test Case """ |
| 239 | |
| 240 | @classmethod |
| 241 | def setUpClass(cls): |
| 242 | super(TestGeneveL3, cls).setUpClass() |
| 243 | try: |
| 244 | cls.create_pg_interfaces(range(2)) |
| 245 | cls.interfaces = list(cls.pg_interfaces) |
| 246 | |
| 247 | for i in cls.interfaces: |
| 248 | i.admin_up() |
| 249 | i.config_ip4() |
| 250 | i.resolve_arp() |
| 251 | except Exception: |
| 252 | super(TestGeneveL3, cls).tearDownClass() |
| 253 | raise |
| 254 | |
| 255 | @classmethod |
| 256 | def tearDownClass(cls): |
| 257 | super(TestGeneveL3, cls).tearDownClass() |
| 258 | |
| 259 | def tearDown(self): |
| 260 | super(TestGeneveL3, self).tearDown() |
| 261 | |
| 262 | def show_commands_at_teardown(self): |
| 263 | self.logger.info(self.vapi.cli("show geneve tunnel")) |
| 264 | self.logger.info(self.vapi.cli("show ip neighbor")) |
| 265 | |
| 266 | def test_l3_packet(self): |
| 267 | vni = 1234 |
| 268 | r = self.vapi.add_node_next(node_name="geneve4-input", |
| 269 | next_name="ethernet-input") |
| 270 | r = self.vapi.geneve_add_del_tunnel2( |
| 271 | is_add=1, |
| 272 | local_address=self.pg0.local_ip4, |
| 273 | remote_address=self.pg0.remote_ip4, |
| 274 | vni=vni, |
| 275 | l3_mode=1, |
| 276 | decap_next_index=r.node_index) |
| 277 | |
| 278 | self.vapi.sw_interface_add_del_address( |
| 279 | sw_if_index=r.sw_if_index, prefix="10.0.0.1/24") |
| 280 | |
| 281 | pkt = (Ether(src=self.pg0.remote_mac, dst="d0:0b:ee:d0:00:00") / |
| 282 | IP(src='10.0.0.2', dst='10.0.0.1') / |
| 283 | ICMP()) |
| 284 | |
| 285 | encap = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 286 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / |
| 287 | UDP(sport=6081, dport=6081, chksum=0) / |
| 288 | GENEVE(vni=vni)) |
| 289 | |
| 290 | arp = (Ether(src=self.pg0.remote_mac, dst="d0:0b:ee:d0:00:00") / |
| 291 | ARP(op="is-at", hwsrc=self.pg0.remote_mac, |
| 292 | hwdst="d0:0b:ee:d0:00:00", psrc="10.0.0.2", |
| 293 | pdst="10.0.0.1")) |
| 294 | |
| 295 | rx = self.send_and_expect(self.pg0, encap/pkt*1, self.pg0) |
| 296 | rx = self.send_and_assert_no_replies(self.pg0, encap/arp*1, self.pg0) |
| 297 | rx = self.send_and_expect(self.pg0, encap/pkt*1, self.pg0) |
| 298 | self.assertEqual(rx[0][ICMP].type, 0) # echo reply |
| 299 | |
| 300 | r = self.vapi.geneve_add_del_tunnel2( |
| 301 | is_add=0, |
| 302 | local_address=self.pg0.local_ip4, |
| 303 | remote_address=self.pg0.remote_ip4, |
| 304 | vni=vni) |
| 305 | |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 306 | if __name__ == '__main__': |
| 307 | unittest.main(testRunner=VppTestRunner) |