Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 3 | import socket |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 4 | import unittest |
| 5 | from framework import VppTestCase, VppTestRunner |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 6 | from template_bd import BridgeDomain |
| 7 | |
| 8 | from scapy.layers.l2 import Ether |
| 9 | from scapy.layers.inet import IP, UDP |
Matej Klotton | deb6984 | 2016-12-09 15:05:46 +0100 | [diff] [blame] | 10 | from scapy.layers.vxlan import VXLAN |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 11 | from scapy.utils import atol |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 12 | |
| 13 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 14 | class TestVxlan(BridgeDomain, VppTestCase): |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 15 | """ VXLAN Test Case """ |
| 16 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 17 | def __init__(self, *args): |
| 18 | BridgeDomain.__init__(self) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 19 | VppTestCase.__init__(self, *args) |
| 20 | |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 21 | def encapsulate(self, pkt, vni): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 22 | """ |
| 23 | Encapsulate the original payload frame by adding VXLAN header with its |
| 24 | UDP, IP and Ethernet fields |
| 25 | """ |
| 26 | return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 27 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / |
| 28 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 29 | VXLAN(vni=vni, flags=self.flags) / |
| 30 | pkt) |
| 31 | |
| 32 | def encap_mcast(self, pkt, src_ip, src_mac, vni): |
| 33 | """ |
| 34 | Encapsulate the original payload frame by adding VXLAN header with its |
| 35 | UDP, IP and Ethernet fields |
| 36 | """ |
| 37 | return (Ether(src=src_mac, dst=self.mcast_mac4) / |
| 38 | IP(src=src_ip, dst=self.mcast_ip4) / |
| 39 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
| 40 | VXLAN(vni=vni, flags=self.flags) / |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 41 | pkt) |
| 42 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 43 | def decapsulate(self, pkt): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 44 | """ |
| 45 | Decapsulate the original payload frame by removing VXLAN header |
| 46 | """ |
Matej Klotton | deb6984 | 2016-12-09 15:05:46 +0100 | [diff] [blame] | 47 | # check if is set I flag |
| 48 | self.assertEqual(pkt[VXLAN].flags, int('0x8', 16)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 49 | return pkt[VXLAN].payload |
| 50 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 51 | # Method for checking VXLAN encapsulation. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 52 | # |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 53 | def check_encapsulation(self, pkt, vni, local_only=False): |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 54 | # TODO: add error messages |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 55 | # 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] | 56 | # by VPP using ARP. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 57 | self.assertEqual(pkt[Ether].src, self.pg0.local_mac) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 58 | if not local_only: |
| 59 | self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 60 | # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP. |
| 61 | self.assertEqual(pkt[IP].src, self.pg0.local_ip4) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 62 | if not local_only: |
| 63 | self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 64 | # Verify UDP destination port is VXLAN 4789, source UDP port could be |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 65 | # arbitrary. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 66 | self.assertEqual(pkt[UDP].dport, type(self).dport) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 67 | # TODO: checksum check |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 68 | # Verify VNI |
| 69 | self.assertEqual(pkt[VXLAN].vni, vni) |
| 70 | |
| 71 | @staticmethod |
| 72 | def ip4_range(ip4n, s=10, e=20): |
| 73 | base = str(bytearray(ip4n)[:3]) |
| 74 | return ((base + ip) for ip in str(bytearray(range(s, e)))) |
| 75 | |
| 76 | @classmethod |
| 77 | def create_vxlan_flood_test_bd(cls, vni): |
| 78 | # Create 10 ucast vxlan tunnels under bd |
| 79 | ip_range_start = 10 |
| 80 | ip_range_end = 20 |
| 81 | next_hop_address = cls.pg0.remote_ip4n |
| 82 | for dest_addr in cls.ip4_range(next_hop_address, ip_range_start, |
| 83 | ip_range_end): |
| 84 | # add host route so dest_addr will not be resolved |
| 85 | cls.vapi.ip_add_del_route(dest_addr, 32, next_hop_address) |
| 86 | r = cls.vapi.vxlan_add_del_tunnel( |
| 87 | src_addr=cls.pg0.local_ip4n, |
| 88 | dst_addr=dest_addr, |
| 89 | vni=vni) |
| 90 | cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni) |
| 91 | |
| 92 | @classmethod |
| 93 | def add_del_mcast_load(cls, is_add): |
| 94 | ip_range_start = 10 |
| 95 | ip_range_end = 210 |
| 96 | for dest_addr in cls.ip4_range(cls.mcast_ip4n, ip_range_start, |
| 97 | ip_range_end): |
| 98 | vni = bytearray(dest_addr)[3] |
| 99 | cls.vapi.vxlan_add_del_tunnel( |
| 100 | src_addr=cls.pg0.local_ip4n, |
| 101 | dst_addr=dest_addr, |
| 102 | mcast_sw_if_index=1, |
| 103 | vni=vni, |
| 104 | is_add=is_add) |
| 105 | |
| 106 | @classmethod |
| 107 | def add_mcast_load(cls): |
| 108 | cls.add_del_mcast_load(is_add=1) |
| 109 | |
| 110 | @classmethod |
| 111 | def del_mcast_load(cls): |
| 112 | cls.add_del_mcast_load(is_add=0) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 113 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 114 | # Class method to start the VXLAN test case. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 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): |
| 121 | super(TestVxlan, cls).setUpClass() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 122 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 123 | try: |
| 124 | cls.dport = 4789 |
Matej Klotton | deb6984 | 2016-12-09 15:05:46 +0100 | [diff] [blame] | 125 | cls.flags = 0x8 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 126 | |
| 127 | # Create 2 pg interfaces. |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 128 | cls.create_pg_interfaces(range(4)) |
| 129 | for pg in cls.pg_interfaces: |
| 130 | pg.admin_up() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 131 | |
| 132 | # Configure IPv4 addresses on VPP pg0. |
| 133 | cls.pg0.config_ip4() |
| 134 | |
| 135 | # Resolve MAC address for VPP's IP address on pg0. |
| 136 | cls.pg0.resolve_arp() |
| 137 | |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 138 | # Our Multicast address |
| 139 | cls.mcast_ip4 = '239.1.1.1' |
| 140 | cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4) |
| 141 | iplong = atol(cls.mcast_ip4) |
| 142 | cls.mcast_mac4 = "01:00:5e:%02x:%02x:%02x" % ( |
| 143 | (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF) |
| 144 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 145 | # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 146 | # into BD. |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 147 | cls.single_tunnel_bd = 1 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 148 | r = cls.vapi.vxlan_add_del_tunnel( |
| 149 | src_addr=cls.pg0.local_ip4n, |
| 150 | dst_addr=cls.pg0.remote_ip4n, |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 151 | vni=cls.single_tunnel_bd) |
| 152 | cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, |
| 153 | bd_id=cls.single_tunnel_bd) |
| 154 | cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index, |
| 155 | bd_id=cls.single_tunnel_bd) |
| 156 | |
| 157 | # Setup vni 2 to test multicast flooding |
| 158 | cls.mcast_flood_bd = 2 |
| 159 | cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd) |
| 160 | r = cls.vapi.vxlan_add_del_tunnel( |
| 161 | src_addr=cls.pg0.local_ip4n, |
| 162 | dst_addr=cls.mcast_ip4n, |
| 163 | mcast_sw_if_index=1, |
| 164 | vni=cls.mcast_flood_bd) |
| 165 | cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, |
| 166 | bd_id=cls.mcast_flood_bd) |
| 167 | cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index, |
| 168 | bd_id=cls.mcast_flood_bd) |
| 169 | |
| 170 | # Add and delete mcast tunnels to check stability |
| 171 | cls.add_mcast_load() |
| 172 | cls.del_mcast_load() |
| 173 | |
| 174 | # Setup vni 3 to test unicast flooding |
| 175 | cls.ucast_flood_bd = 3 |
| 176 | cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd) |
| 177 | cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index, |
| 178 | bd_id=cls.ucast_flood_bd) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 179 | except Exception: |
| 180 | super(TestVxlan, cls).tearDownClass() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 181 | raise |
| 182 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 183 | # Method to define VPP actions before tear down of the test case. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 184 | # Overrides tearDown method in VppTestCase class. |
| 185 | # @param self The object pointer. |
| 186 | def tearDown(self): |
| 187 | super(TestVxlan, self).tearDown() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 188 | if not self.vpp_dead: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 189 | self.logger.info(self.vapi.cli("show bridge-domain 1 detail")) |
Eyal Bari | c4aaee1 | 2016-12-20 18:36:46 +0200 | [diff] [blame] | 190 | self.logger.info(self.vapi.cli("show bridge-domain 2 detail")) |
| 191 | self.logger.info(self.vapi.cli("show bridge-domain 3 detail")) |
| 192 | self.logger.info(self.vapi.cli("show vxlan tunnel")) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 193 | |
Matej Klotton | deb6984 | 2016-12-09 15:05:46 +0100 | [diff] [blame] | 194 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 195 | if __name__ == '__main__': |
| 196 | unittest.main(testRunner=VppTestRunner) |