Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import socket |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 4 | from util import ip4n_range, 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 | |
| 9 | from scapy.layers.l2 import Ether |
| 10 | from scapy.layers.inet import IP, UDP |
| 11 | from scapy.layers.geneve import GENEVE |
| 12 | from scapy.utils import atol |
| 13 | |
| 14 | |
| 15 | class TestGeneve(BridgeDomain, VppTestCase): |
| 16 | """ GENEVE Test Case """ |
| 17 | |
| 18 | def __init__(self, *args): |
| 19 | BridgeDomain.__init__(self) |
| 20 | VppTestCase.__init__(self, *args) |
| 21 | |
| 22 | def encapsulate(self, pkt, vni): |
| 23 | |
| 24 | """ |
| 25 | Encapsulate the original payload frame by adding GENEVE header with its |
| 26 | UDP, IP and Ethernet fields |
| 27 | """ |
| 28 | return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 29 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / |
| 30 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
| 31 | GENEVE(vni=vni) / |
| 32 | pkt) |
| 33 | |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 34 | def ip_range(self, start, end): |
| 35 | """ range of remote ip's """ |
| 36 | return ip4_range(self.pg0.remote_ip4, start, end) |
| 37 | |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 38 | def encap_mcast(self, pkt, src_ip, src_mac, vni): |
| 39 | """ |
| 40 | Encapsulate the original payload frame by adding GENEVE header with its |
| 41 | UDP, IP and Ethernet fields |
| 42 | """ |
| 43 | return (Ether(src=src_mac, dst=self.mcast_mac) / |
| 44 | IP(src=src_ip, dst=self.mcast_ip4) / |
| 45 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
| 46 | GENEVE(vni=vni) / |
| 47 | pkt) |
| 48 | |
| 49 | def decapsulate(self, pkt): |
| 50 | """ |
| 51 | Decapsulate the original payload frame by removing GENEVE header |
| 52 | """ |
| 53 | # check if is set I flag |
| 54 | # self.assertEqual(pkt[GENEVE].flags, int('0x8', 16)) |
| 55 | return pkt[GENEVE].payload |
| 56 | |
| 57 | # Method for checking GENEVE encapsulation. |
| 58 | # |
| 59 | def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False): |
| 60 | # TODO: add error messages |
| 61 | # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved |
| 62 | # by VPP using ARP. |
| 63 | self.assertEqual(pkt[Ether].src, self.pg0.local_mac) |
| 64 | if not local_only: |
| 65 | if not mcast_pkt: |
| 66 | self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) |
| 67 | else: |
| 68 | self.assertEqual(pkt[Ether].dst, type(self).mcast_mac) |
| 69 | # Verify GENEVE tunnel source IP is VPP_IP and destination IP is MY_IP. |
| 70 | self.assertEqual(pkt[IP].src, self.pg0.local_ip4) |
| 71 | if not local_only: |
| 72 | if not mcast_pkt: |
| 73 | self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4) |
| 74 | else: |
| 75 | self.assertEqual(pkt[IP].dst, type(self).mcast_ip4) |
| 76 | # Verify UDP destination port is GENEVE 4789, source UDP port could be |
| 77 | # arbitrary. |
| 78 | self.assertEqual(pkt[UDP].dport, type(self).dport) |
| 79 | # TODO: checksum check |
| 80 | # Verify VNI |
| 81 | self.assertEqual(pkt[GENEVE].vni, vni) |
| 82 | |
| 83 | @classmethod |
| 84 | def create_geneve_flood_test_bd(cls, vni, n_ucast_tunnels): |
| 85 | # Create 10 ucast geneve tunnels under bd |
| 86 | ip_range_start = 10 |
| 87 | ip_range_end = ip_range_start + n_ucast_tunnels |
| 88 | next_hop_address = cls.pg0.remote_ip4n |
| 89 | for dest_ip4n in ip4n_range(next_hop_address, ip_range_start, |
| 90 | ip_range_end): |
| 91 | # add host route so dest_ip4n will not be resolved |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame^] | 92 | cls.vapi.ip_add_del_route(dst_address=dest_ip4n, |
| 93 | dst_address_length=32, |
| 94 | next_hop_address=next_hop_address) |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 95 | r = cls.vapi.geneve_add_del_tunnel( |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame^] | 96 | local_address=cls.pg0.local_ip4n, remote_address=dest_ip4n, |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 97 | vni=vni) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame^] | 98 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
| 99 | bd_id=vni) |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 100 | |
| 101 | @classmethod |
| 102 | def add_del_shared_mcast_dst_load(cls, is_add): |
| 103 | """ |
| 104 | add or del tunnels sharing the same mcast dst |
| 105 | to test geneve ref_count mechanism |
| 106 | """ |
Gabriel Ganne | 97cabc9 | 2018-02-08 11:22:33 +0100 | [diff] [blame] | 107 | n_shared_dst_tunnels = 10 |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 108 | vni_start = 10000 |
| 109 | vni_end = vni_start + n_shared_dst_tunnels |
| 110 | for vni in range(vni_start, vni_end): |
| 111 | r = cls.vapi.geneve_add_del_tunnel( |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame^] | 112 | local_address=cls.pg0.local_ip4n, |
| 113 | remote_address=cls.mcast_ip4n, mcast_sw_if_index=1, |
| 114 | is_add=is_add, vni=vni) |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 115 | if r.sw_if_index == 0xffffffff: |
| 116 | raise "bad sw_if_index" |
| 117 | |
| 118 | @classmethod |
| 119 | def add_shared_mcast_dst_load(cls): |
| 120 | cls.add_del_shared_mcast_dst_load(is_add=1) |
| 121 | |
| 122 | @classmethod |
| 123 | def del_shared_mcast_dst_load(cls): |
| 124 | cls.add_del_shared_mcast_dst_load(is_add=0) |
| 125 | |
| 126 | @classmethod |
| 127 | def add_del_mcast_tunnels_load(cls, is_add): |
| 128 | """ |
| 129 | add or del tunnels to test geneve stability |
| 130 | """ |
Gabriel Ganne | 97cabc9 | 2018-02-08 11:22:33 +0100 | [diff] [blame] | 131 | n_distinct_dst_tunnels = 10 |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 132 | ip_range_start = 10 |
| 133 | ip_range_end = ip_range_start + n_distinct_dst_tunnels |
| 134 | for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start, |
| 135 | ip_range_end): |
| 136 | vni = bytearray(dest_ip4n)[3] |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame^] | 137 | cls.vapi.geneve_add_del_tunnel(local_address=cls.pg0.local_ip4n, |
| 138 | remote_address=dest_ip4n, |
| 139 | mcast_sw_if_index=1, is_add=is_add, |
| 140 | vni=vni) |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 141 | |
| 142 | @classmethod |
| 143 | def add_mcast_tunnels_load(cls): |
| 144 | cls.add_del_mcast_tunnels_load(is_add=1) |
| 145 | |
| 146 | @classmethod |
| 147 | def del_mcast_tunnels_load(cls): |
| 148 | cls.add_del_mcast_tunnels_load(is_add=0) |
| 149 | |
| 150 | # Class method to start the GENEVE test case. |
| 151 | # Overrides setUpClass method in VppTestCase class. |
| 152 | # Python try..except statement is used to ensure that the tear down of |
| 153 | # the class will be executed even if exception is raised. |
| 154 | # @param cls The class pointer. |
| 155 | @classmethod |
| 156 | def setUpClass(cls): |
| 157 | super(TestGeneve, cls).setUpClass() |
| 158 | |
| 159 | try: |
| 160 | cls.dport = 6081 |
| 161 | |
| 162 | # Create 2 pg interfaces. |
| 163 | cls.create_pg_interfaces(range(4)) |
| 164 | for pg in cls.pg_interfaces: |
| 165 | pg.admin_up() |
| 166 | |
| 167 | # Configure IPv4 addresses on VPP pg0. |
| 168 | cls.pg0.config_ip4() |
| 169 | |
| 170 | # Resolve MAC address for VPP's IP address on pg0. |
| 171 | cls.pg0.resolve_arp() |
| 172 | |
| 173 | # Our Multicast address |
| 174 | cls.mcast_ip4 = '239.1.1.1' |
| 175 | cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4) |
| 176 | iplong = atol(cls.mcast_ip4) |
| 177 | cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % ( |
| 178 | (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF) |
| 179 | |
| 180 | # Create GENEVE VTEP on VPP pg0, and put geneve_tunnel0 and pg1 |
| 181 | # into BD. |
| 182 | cls.single_tunnel_bd = 1 |
| 183 | r = cls.vapi.geneve_add_del_tunnel( |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame^] | 184 | local_address=cls.pg0.local_ip4n, |
| 185 | remote_address=cls.pg0.remote_ip4n, vni=cls.single_tunnel_bd) |
| 186 | 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] | 187 | bd_id=cls.single_tunnel_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame^] | 188 | cls.vapi.sw_interface_set_l2_bridge( |
| 189 | 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] | 190 | |
| 191 | # Setup vni 2 to test multicast flooding |
| 192 | cls.n_ucast_tunnels = 10 |
| 193 | cls.mcast_flood_bd = 2 |
| 194 | cls.create_geneve_flood_test_bd(cls.mcast_flood_bd, |
| 195 | cls.n_ucast_tunnels) |
| 196 | r = cls.vapi.geneve_add_del_tunnel( |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame^] | 197 | local_address=cls.pg0.local_ip4n, |
| 198 | remote_address=cls.mcast_ip4n, mcast_sw_if_index=1, |
Marco Varlese | b598f1d | 2017-09-19 14:25:28 +0200 | [diff] [blame] | 199 | vni=cls.mcast_flood_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame^] | 200 | 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] | 201 | bd_id=cls.mcast_flood_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame^] | 202 | cls.vapi.sw_interface_set_l2_bridge( |
| 203 | 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] | 204 | |
| 205 | # Add and delete mcast tunnels to check stability |
| 206 | cls.add_shared_mcast_dst_load() |
| 207 | cls.add_mcast_tunnels_load() |
| 208 | cls.del_shared_mcast_dst_load() |
| 209 | cls.del_mcast_tunnels_load() |
| 210 | |
| 211 | # Setup vni 3 to test unicast flooding |
| 212 | cls.ucast_flood_bd = 3 |
| 213 | cls.create_geneve_flood_test_bd(cls.ucast_flood_bd, |
| 214 | cls.n_ucast_tunnels) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame^] | 215 | cls.vapi.sw_interface_set_l2_bridge( |
| 216 | 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] | 217 | except Exception: |
| 218 | super(TestGeneve, cls).tearDownClass() |
| 219 | raise |
| 220 | |
| 221 | # Method to define VPP actions before tear down of the test case. |
| 222 | # Overrides tearDown method in VppTestCase class. |
| 223 | # @param self The object pointer. |
| 224 | def tearDown(self): |
| 225 | super(TestGeneve, self).tearDown() |
| 226 | if not self.vpp_dead: |
| 227 | self.logger.info(self.vapi.cli("show bridge-domain 1 detail")) |
| 228 | self.logger.info(self.vapi.cli("show bridge-domain 2 detail")) |
| 229 | self.logger.info(self.vapi.cli("show bridge-domain 3 detail")) |
| 230 | self.logger.info(self.vapi.cli("show geneve tunnel")) |
| 231 | |
| 232 | |
| 233 | if __name__ == '__main__': |
| 234 | unittest.main(testRunner=VppTestRunner) |