blob: ee829a31a288909e18fce222a097961821bd3152 [file] [log] [blame]
Damjan Marionf56b77a2016-10-03 19:44:57 +02001#!/usr/bin/env python
2
Eyal Baric4aaee12016-12-20 18:36:46 +02003import socket
Eyal Barid81da8c2017-01-11 13:39:54 +02004from util import ip4n_range
Damjan Marionf56b77a2016-10-03 19:44:57 +02005import unittest
6from framework import VppTestCase, VppTestRunner
Damjan Marionf56b77a2016-10-03 19:44:57 +02007from template_bd import BridgeDomain
8
9from scapy.layers.l2 import Ether
10from scapy.layers.inet import IP, UDP
Matej Klottondeb69842016-12-09 15:05:46 +010011from scapy.layers.vxlan import VXLAN
Eyal Baric4aaee12016-12-20 18:36:46 +020012from scapy.utils import atol
Damjan Marionf56b77a2016-10-03 19:44:57 +020013
14
Klement Sekeraf62ae122016-10-11 11:47:09 +020015class TestVxlan(BridgeDomain, VppTestCase):
Damjan Marionf56b77a2016-10-03 19:44:57 +020016 """ VXLAN Test Case """
17
Damjan Marionf56b77a2016-10-03 19:44:57 +020018 def __init__(self, *args):
19 BridgeDomain.__init__(self)
Damjan Marionf56b77a2016-10-03 19:44:57 +020020 VppTestCase.__init__(self, *args)
21
Eyal Baric4aaee12016-12-20 18:36:46 +020022 def encapsulate(self, pkt, vni):
Klement Sekeraf62ae122016-10-11 11:47:09 +020023 """
24 Encapsulate the original payload frame by adding VXLAN header with its
25 UDP, IP and Ethernet fields
26 """
27 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
28 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
29 UDP(sport=self.dport, dport=self.dport, chksum=0) /
Eyal Baric4aaee12016-12-20 18:36:46 +020030 VXLAN(vni=vni, flags=self.flags) /
31 pkt)
32
33 def encap_mcast(self, pkt, src_ip, src_mac, vni):
34 """
35 Encapsulate the original payload frame by adding VXLAN header with its
36 UDP, IP and Ethernet fields
37 """
Eyal Barid81da8c2017-01-11 13:39:54 +020038 return (Ether(src=src_mac, dst=self.mcast_mac) /
Eyal Baric4aaee12016-12-20 18:36:46 +020039 IP(src=src_ip, dst=self.mcast_ip4) /
40 UDP(sport=self.dport, dport=self.dport, chksum=0) /
41 VXLAN(vni=vni, flags=self.flags) /
Damjan Marionf56b77a2016-10-03 19:44:57 +020042 pkt)
43
Damjan Marionf56b77a2016-10-03 19:44:57 +020044 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020045 """
46 Decapsulate the original payload frame by removing VXLAN header
47 """
Matej Klottondeb69842016-12-09 15:05:46 +010048 # check if is set I flag
49 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
Damjan Marionf56b77a2016-10-03 19:44:57 +020050 return pkt[VXLAN].payload
51
Klement Sekeraf62ae122016-10-11 11:47:09 +020052 # Method for checking VXLAN encapsulation.
Damjan Marionf56b77a2016-10-03 19:44:57 +020053 #
Eyal Bari6ae5ee72017-03-23 09:53:51 +020054 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
Damjan Marionf56b77a2016-10-03 19:44:57 +020055 # TODO: add error messages
Klement Sekeraf62ae122016-10-11 11:47:09 +020056 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
Damjan Marionf56b77a2016-10-03 19:44:57 +020057 # by VPP using ARP.
Klement Sekeraf62ae122016-10-11 11:47:09 +020058 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
Eyal Baric4aaee12016-12-20 18:36:46 +020059 if not local_only:
Eyal Bari6ae5ee72017-03-23 09:53:51 +020060 if not mcast_pkt:
61 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
62 else:
63 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
Klement Sekeraf62ae122016-10-11 11:47:09 +020064 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
65 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
Eyal Baric4aaee12016-12-20 18:36:46 +020066 if not local_only:
Eyal Bari6ae5ee72017-03-23 09:53:51 +020067 if not mcast_pkt:
68 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
69 else:
70 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020071 # Verify UDP destination port is VXLAN 4789, source UDP port could be
Damjan Marionf56b77a2016-10-03 19:44:57 +020072 # arbitrary.
Klement Sekeraf62ae122016-10-11 11:47:09 +020073 self.assertEqual(pkt[UDP].dport, type(self).dport)
Damjan Marionf56b77a2016-10-03 19:44:57 +020074 # TODO: checksum check
Eyal Baric4aaee12016-12-20 18:36:46 +020075 # Verify VNI
76 self.assertEqual(pkt[VXLAN].vni, vni)
77
Eyal Baric4aaee12016-12-20 18:36:46 +020078 @classmethod
Eyal Barid81da8c2017-01-11 13:39:54 +020079 def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
Eyal Baric4aaee12016-12-20 18:36:46 +020080 # Create 10 ucast vxlan tunnels under bd
81 ip_range_start = 10
Eyal Barid81da8c2017-01-11 13:39:54 +020082 ip_range_end = ip_range_start + n_ucast_tunnels
Eyal Baric4aaee12016-12-20 18:36:46 +020083 next_hop_address = cls.pg0.remote_ip4n
Eyal Barid81da8c2017-01-11 13:39:54 +020084 for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
85 ip_range_end):
86 # add host route so dest_ip4n will not be resolved
87 cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
Eyal Baric4aaee12016-12-20 18:36:46 +020088 r = cls.vapi.vxlan_add_del_tunnel(
89 src_addr=cls.pg0.local_ip4n,
Eyal Barid81da8c2017-01-11 13:39:54 +020090 dst_addr=dest_ip4n,
Eyal Baric4aaee12016-12-20 18:36:46 +020091 vni=vni)
92 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
93
94 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +020095 def add_del_shared_mcast_dst_load(cls, is_add):
96 """
97 add or del tunnels sharing the same mcast dst
98 to test vxlan ref_count mechanism
99 """
100 n_shared_dst_tunnels = 2000
101 vni_start = 10000
102 vni_end = vni_start + n_shared_dst_tunnels
103 for vni in range(vni_start, vni_end):
104 cls.vapi.vxlan_add_del_tunnel(
105 src_addr=cls.pg0.local_ip4n,
106 dst_addr=cls.mcast_ip4n,
107 mcast_sw_if_index=1,
108 vni=vni,
109 is_add=is_add)
110
111 @classmethod
112 def add_shared_mcast_dst_load(cls):
113 cls.add_del_shared_mcast_dst_load(is_add=1)
114
115 @classmethod
116 def del_shared_mcast_dst_load(cls):
117 cls.add_del_shared_mcast_dst_load(is_add=0)
118
119 @classmethod
120 def add_del_mcast_tunnels_load(cls, is_add):
121 """
122 add or del tunnels to test vxlan stability
123 """
124 n_distinct_dst_tunnels = 200
Eyal Baric4aaee12016-12-20 18:36:46 +0200125 ip_range_start = 10
Eyal Bari4bce2902017-01-16 12:02:46 +0200126 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Eyal Barid81da8c2017-01-11 13:39:54 +0200127 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
128 ip_range_end):
129 vni = bytearray(dest_ip4n)[3]
Eyal Baric4aaee12016-12-20 18:36:46 +0200130 cls.vapi.vxlan_add_del_tunnel(
131 src_addr=cls.pg0.local_ip4n,
Eyal Barid81da8c2017-01-11 13:39:54 +0200132 dst_addr=dest_ip4n,
Eyal Baric4aaee12016-12-20 18:36:46 +0200133 mcast_sw_if_index=1,
134 vni=vni,
135 is_add=is_add)
136
137 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200138 def add_mcast_tunnels_load(cls):
139 cls.add_del_mcast_tunnels_load(is_add=1)
Eyal Baric4aaee12016-12-20 18:36:46 +0200140
141 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200142 def del_mcast_tunnels_load(cls):
143 cls.add_del_mcast_tunnels_load(is_add=0)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200144
Klement Sekeraf62ae122016-10-11 11:47:09 +0200145 # Class method to start the VXLAN test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200146 # Overrides setUpClass method in VppTestCase class.
147 # Python try..except statement is used to ensure that the tear down of
148 # the class will be executed even if exception is raised.
149 # @param cls The class pointer.
150 @classmethod
151 def setUpClass(cls):
152 super(TestVxlan, cls).setUpClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200153
Klement Sekeraf62ae122016-10-11 11:47:09 +0200154 try:
155 cls.dport = 4789
Matej Klottondeb69842016-12-09 15:05:46 +0100156 cls.flags = 0x8
Klement Sekeraf62ae122016-10-11 11:47:09 +0200157
158 # Create 2 pg interfaces.
Eyal Baric4aaee12016-12-20 18:36:46 +0200159 cls.create_pg_interfaces(range(4))
160 for pg in cls.pg_interfaces:
161 pg.admin_up()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200162
163 # Configure IPv4 addresses on VPP pg0.
164 cls.pg0.config_ip4()
165
166 # Resolve MAC address for VPP's IP address on pg0.
167 cls.pg0.resolve_arp()
168
Eyal Baric4aaee12016-12-20 18:36:46 +0200169 # Our Multicast address
170 cls.mcast_ip4 = '239.1.1.1'
171 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
172 iplong = atol(cls.mcast_ip4)
Eyal Barid81da8c2017-01-11 13:39:54 +0200173 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
Eyal Baric4aaee12016-12-20 18:36:46 +0200174 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
175
Klement Sekeraf62ae122016-10-11 11:47:09 +0200176 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
Damjan Marionf56b77a2016-10-03 19:44:57 +0200177 # into BD.
Eyal Baric4aaee12016-12-20 18:36:46 +0200178 cls.single_tunnel_bd = 1
Klement Sekeraf62ae122016-10-11 11:47:09 +0200179 r = cls.vapi.vxlan_add_del_tunnel(
180 src_addr=cls.pg0.local_ip4n,
181 dst_addr=cls.pg0.remote_ip4n,
Eyal Baric4aaee12016-12-20 18:36:46 +0200182 vni=cls.single_tunnel_bd)
183 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
184 bd_id=cls.single_tunnel_bd)
185 cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
186 bd_id=cls.single_tunnel_bd)
187
188 # Setup vni 2 to test multicast flooding
Eyal Barid81da8c2017-01-11 13:39:54 +0200189 cls.n_ucast_tunnels = 10
Eyal Baric4aaee12016-12-20 18:36:46 +0200190 cls.mcast_flood_bd = 2
Eyal Barid81da8c2017-01-11 13:39:54 +0200191 cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
192 cls.n_ucast_tunnels)
Eyal Baric4aaee12016-12-20 18:36:46 +0200193 r = cls.vapi.vxlan_add_del_tunnel(
194 src_addr=cls.pg0.local_ip4n,
195 dst_addr=cls.mcast_ip4n,
196 mcast_sw_if_index=1,
197 vni=cls.mcast_flood_bd)
198 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
199 bd_id=cls.mcast_flood_bd)
200 cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
201 bd_id=cls.mcast_flood_bd)
202
203 # Add and delete mcast tunnels to check stability
Eyal Bari4bce2902017-01-16 12:02:46 +0200204 cls.add_shared_mcast_dst_load()
205 cls.add_mcast_tunnels_load()
206 cls.del_shared_mcast_dst_load()
207 cls.del_mcast_tunnels_load()
Eyal Baric4aaee12016-12-20 18:36:46 +0200208
209 # Setup vni 3 to test unicast flooding
210 cls.ucast_flood_bd = 3
Eyal Barid81da8c2017-01-11 13:39:54 +0200211 cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
212 cls.n_ucast_tunnels)
Eyal Baric4aaee12016-12-20 18:36:46 +0200213 cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
214 bd_id=cls.ucast_flood_bd)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200215 except Exception:
216 super(TestVxlan, cls).tearDownClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200217 raise
218
Klement Sekeraf62ae122016-10-11 11:47:09 +0200219 # Method to define VPP actions before tear down of the test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200220 # Overrides tearDown method in VppTestCase class.
221 # @param self The object pointer.
222 def tearDown(self):
223 super(TestVxlan, self).tearDown()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200224 if not self.vpp_dead:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100225 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
Eyal Baric4aaee12016-12-20 18:36:46 +0200226 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
227 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
228 self.logger.info(self.vapi.cli("show vxlan tunnel"))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200229
Matej Klottondeb69842016-12-09 15:05:46 +0100230
Damjan Marionf56b77a2016-10-03 19:44:57 +0200231if __name__ == '__main__':
232 unittest.main(testRunner=VppTestRunner)