blob: 692930eb1f29cb301c00d12159f7e272e559da5f [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Damjan Marionf56b77a2016-10-03 19:44:57 +02002
Eyal Baric4aaee12016-12-20 18:36:46 +02003import socket
Ole Troan7f991832018-12-06 17:35:12 +01004from util import ip4n_range, ip4_range, reassemble4
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
Ole Troanb3655e52018-08-16 22:08:49 +02009from scapy.layers.l2 import Ether, Raw
Damjan Marionf56b77a2016-10-03 19:44:57 +020010from 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
Neale Ranns097fa662018-05-01 05:17:55 -070013from vpp_ip_route import VppIpRoute, VppRoutePath
14from vpp_ip import INVALID_INDEX
Damjan Marionf56b77a2016-10-03 19:44:57 +020015
16
Klement Sekeraf62ae122016-10-11 11:47:09 +020017class TestVxlan(BridgeDomain, VppTestCase):
Damjan Marionf56b77a2016-10-03 19:44:57 +020018 """ VXLAN Test Case """
19
Damjan Marionf56b77a2016-10-03 19:44:57 +020020 def __init__(self, *args):
21 BridgeDomain.__init__(self)
Damjan Marionf56b77a2016-10-03 19:44:57 +020022 VppTestCase.__init__(self, *args)
23
Eyal Baric4aaee12016-12-20 18:36:46 +020024 def encapsulate(self, pkt, vni):
Klement Sekeraf62ae122016-10-11 11:47:09 +020025 """
26 Encapsulate the original payload frame by adding VXLAN header with its
27 UDP, IP and Ethernet fields
28 """
29 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
30 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
31 UDP(sport=self.dport, dport=self.dport, chksum=0) /
Eyal Baric4aaee12016-12-20 18:36:46 +020032 VXLAN(vni=vni, flags=self.flags) /
33 pkt)
34
Eyal Baricef1e2a2018-06-18 13:01:59 +030035 def ip_range(self, start, end):
36 """ range of remote ip's """
37 return ip4_range(self.pg0.remote_ip4, start, end)
38
Eyal Baric4aaee12016-12-20 18:36:46 +020039 def encap_mcast(self, pkt, src_ip, src_mac, vni):
40 """
41 Encapsulate the original payload frame by adding VXLAN header with its
42 UDP, IP and Ethernet fields
43 """
Eyal Barid81da8c2017-01-11 13:39:54 +020044 return (Ether(src=src_mac, dst=self.mcast_mac) /
Eyal Baric4aaee12016-12-20 18:36:46 +020045 IP(src=src_ip, dst=self.mcast_ip4) /
46 UDP(sport=self.dport, dport=self.dport, chksum=0) /
47 VXLAN(vni=vni, flags=self.flags) /
Damjan Marionf56b77a2016-10-03 19:44:57 +020048 pkt)
49
Damjan Marionf56b77a2016-10-03 19:44:57 +020050 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020051 """
52 Decapsulate the original payload frame by removing VXLAN header
53 """
Matej Klottondeb69842016-12-09 15:05:46 +010054 # check if is set I flag
55 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
Damjan Marionf56b77a2016-10-03 19:44:57 +020056 return pkt[VXLAN].payload
57
Klement Sekeraf62ae122016-10-11 11:47:09 +020058 # Method for checking VXLAN encapsulation.
Damjan Marionf56b77a2016-10-03 19:44:57 +020059 #
Eyal Bari6ae5ee72017-03-23 09:53:51 +020060 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
Damjan Marionf56b77a2016-10-03 19:44:57 +020061 # TODO: add error messages
Klement Sekeraf62ae122016-10-11 11:47:09 +020062 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
Damjan Marionf56b77a2016-10-03 19:44:57 +020063 # by VPP using ARP.
Klement Sekeraf62ae122016-10-11 11:47:09 +020064 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
Eyal Baric4aaee12016-12-20 18:36:46 +020065 if not local_only:
Eyal Bari6ae5ee72017-03-23 09:53:51 +020066 if not mcast_pkt:
67 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
68 else:
69 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
Klement Sekeraf62ae122016-10-11 11:47:09 +020070 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
71 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
Eyal Baric4aaee12016-12-20 18:36:46 +020072 if not local_only:
Eyal Bari6ae5ee72017-03-23 09:53:51 +020073 if not mcast_pkt:
74 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
75 else:
76 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020077 # Verify UDP destination port is VXLAN 4789, source UDP port could be
Damjan Marionf56b77a2016-10-03 19:44:57 +020078 # arbitrary.
Klement Sekeraf62ae122016-10-11 11:47:09 +020079 self.assertEqual(pkt[UDP].dport, type(self).dport)
Damjan Marionf56b77a2016-10-03 19:44:57 +020080 # TODO: checksum check
Eyal Baric4aaee12016-12-20 18:36:46 +020081 # Verify VNI
82 self.assertEqual(pkt[VXLAN].vni, vni)
83
Eyal Baric4aaee12016-12-20 18:36:46 +020084 @classmethod
Eyal Barid81da8c2017-01-11 13:39:54 +020085 def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
Eyal Baric4aaee12016-12-20 18:36:46 +020086 # Create 10 ucast vxlan tunnels under bd
87 ip_range_start = 10
Eyal Barid81da8c2017-01-11 13:39:54 +020088 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -070089 next_hop_address = cls.pg0.remote_ip4
90 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
91 ip_range_end):
Eyal Barid81da8c2017-01-11 13:39:54 +020092 # add host route so dest_ip4n will not be resolved
Neale Ranns097fa662018-05-01 05:17:55 -070093 rip = VppIpRoute(cls, dest_ip4, 32,
94 [VppRoutePath(next_hop_address,
95 INVALID_INDEX)],
96 register=False)
97 rip.add_vpp_config()
98 dest_ip4n = socket.inet_pton(socket.AF_INET, dest_ip4)
99
Ole Troana5b2eec2019-03-11 19:23:25 +0100100 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
101 dst_address=dest_ip4n, vni=vni)
Neale Ranns097fa662018-05-01 05:17:55 -0700102 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
Eyal Baric4aaee12016-12-20 18:36:46 +0200103
104 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200105 def add_del_shared_mcast_dst_load(cls, is_add):
106 """
107 add or del tunnels sharing the same mcast dst
108 to test vxlan ref_count mechanism
109 """
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100110 n_shared_dst_tunnels = 20
Eyal Bari4bce2902017-01-16 12:02:46 +0200111 vni_start = 10000
112 vni_end = vni_start + n_shared_dst_tunnels
113 for vni in range(vni_start, vni_end):
Ole Troana5b2eec2019-03-11 19:23:25 +0100114 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
115 dst_address=cls.mcast_ip4n,
116 mcast_sw_if_index=1,
117 is_add=is_add, vni=vni)
Eyal Barid9278342017-04-06 03:31:00 +0300118 if r.sw_if_index == 0xffffffff:
Paul Vinciguerrac599c6f2019-03-12 17:41:27 -0700119 raise ValueError("bad sw_if_index: ~0")
Eyal Bari4bce2902017-01-16 12:02:46 +0200120
121 @classmethod
122 def add_shared_mcast_dst_load(cls):
123 cls.add_del_shared_mcast_dst_load(is_add=1)
124
125 @classmethod
126 def del_shared_mcast_dst_load(cls):
127 cls.add_del_shared_mcast_dst_load(is_add=0)
128
129 @classmethod
130 def add_del_mcast_tunnels_load(cls, is_add):
131 """
132 add or del tunnels to test vxlan stability
133 """
134 n_distinct_dst_tunnels = 200
Eyal Baric4aaee12016-12-20 18:36:46 +0200135 ip_range_start = 10
Eyal Bari4bce2902017-01-16 12:02:46 +0200136 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Eyal Barid81da8c2017-01-11 13:39:54 +0200137 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
138 ip_range_end):
139 vni = bytearray(dest_ip4n)[3]
Ole Troana5b2eec2019-03-11 19:23:25 +0100140 cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
141 dst_address=dest_ip4n,
142 mcast_sw_if_index=1, is_add=is_add,
143 vni=vni)
Eyal Baric4aaee12016-12-20 18:36:46 +0200144
145 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200146 def add_mcast_tunnels_load(cls):
147 cls.add_del_mcast_tunnels_load(is_add=1)
Eyal Baric4aaee12016-12-20 18:36:46 +0200148
149 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200150 def del_mcast_tunnels_load(cls):
151 cls.add_del_mcast_tunnels_load(is_add=0)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200152
Klement Sekeraf62ae122016-10-11 11:47:09 +0200153 # Class method to start the VXLAN test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200154 # Overrides setUpClass method in VppTestCase class.
155 # Python try..except statement is used to ensure that the tear down of
156 # the class will be executed even if exception is raised.
157 # @param cls The class pointer.
158 @classmethod
159 def setUpClass(cls):
160 super(TestVxlan, cls).setUpClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200161
Klement Sekeraf62ae122016-10-11 11:47:09 +0200162 try:
163 cls.dport = 4789
Matej Klottondeb69842016-12-09 15:05:46 +0100164 cls.flags = 0x8
Klement Sekeraf62ae122016-10-11 11:47:09 +0200165
166 # Create 2 pg interfaces.
Eyal Baric4aaee12016-12-20 18:36:46 +0200167 cls.create_pg_interfaces(range(4))
168 for pg in cls.pg_interfaces:
169 pg.admin_up()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200170
171 # Configure IPv4 addresses on VPP pg0.
172 cls.pg0.config_ip4()
173
174 # Resolve MAC address for VPP's IP address on pg0.
175 cls.pg0.resolve_arp()
176
Eyal Baric4aaee12016-12-20 18:36:46 +0200177 # Our Multicast address
178 cls.mcast_ip4 = '239.1.1.1'
179 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
180 iplong = atol(cls.mcast_ip4)
Eyal Barid81da8c2017-01-11 13:39:54 +0200181 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
Eyal Baric4aaee12016-12-20 18:36:46 +0200182 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
183
Klement Sekeraf62ae122016-10-11 11:47:09 +0200184 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
Damjan Marionf56b77a2016-10-03 19:44:57 +0200185 # into BD.
Eyal Baric4aaee12016-12-20 18:36:46 +0200186 cls.single_tunnel_bd = 1
Ole Troana5b2eec2019-03-11 19:23:25 +0100187 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
188 dst_address=cls.pg0.remote_ip4n,
189 vni=cls.single_tunnel_bd)
190 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Eyal Baric4aaee12016-12-20 18:36:46 +0200191 bd_id=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100192 cls.vapi.sw_interface_set_l2_bridge(
193 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
Eyal Baric4aaee12016-12-20 18:36:46 +0200194
195 # Setup vni 2 to test multicast flooding
Eyal Barid81da8c2017-01-11 13:39:54 +0200196 cls.n_ucast_tunnels = 10
Eyal Baric4aaee12016-12-20 18:36:46 +0200197 cls.mcast_flood_bd = 2
Eyal Barid81da8c2017-01-11 13:39:54 +0200198 cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
199 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100200 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
201 dst_address=cls.mcast_ip4n,
202 mcast_sw_if_index=1,
203 vni=cls.mcast_flood_bd)
204 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Eyal Baric4aaee12016-12-20 18:36:46 +0200205 bd_id=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100206 cls.vapi.sw_interface_set_l2_bridge(
207 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
Eyal Baric4aaee12016-12-20 18:36:46 +0200208
209 # Add and delete mcast tunnels to check stability
Eyal Bari4bce2902017-01-16 12:02:46 +0200210 cls.add_shared_mcast_dst_load()
211 cls.add_mcast_tunnels_load()
212 cls.del_shared_mcast_dst_load()
213 cls.del_mcast_tunnels_load()
Eyal Baric4aaee12016-12-20 18:36:46 +0200214
215 # Setup vni 3 to test unicast flooding
216 cls.ucast_flood_bd = 3
Eyal Barid81da8c2017-01-11 13:39:54 +0200217 cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
218 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100219 cls.vapi.sw_interface_set_l2_bridge(
220 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200221 except Exception:
222 super(TestVxlan, cls).tearDownClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200223 raise
224
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700225 @classmethod
226 def tearDownClass(cls):
227 super(TestVxlan, cls).tearDownClass()
228
Ole Troanb3655e52018-08-16 22:08:49 +0200229 def test_encap_big_packet(self):
230 """ Encapsulation test send big frame from pg1
231 Verify receipt of encapsulated frames on pg0
232 """
233
234 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
235
236 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
237 IP(src='4.3.2.1', dst='1.2.3.4') /
238 UDP(sport=20000, dport=10000) /
Ole Troan770a0de2019-11-07 13:52:21 +0100239 Raw(b'\xa5' * 1450))
Ole Troanb3655e52018-08-16 22:08:49 +0200240
241 self.pg1.add_stream([frame])
242
243 self.pg0.enable_capture()
244
245 self.pg_start()
246
247 # Pick first received frame and check if it's correctly encapsulated.
248 out = self.pg0.get_capture(2)
249 ether = out[0]
Ole Troan7f991832018-12-06 17:35:12 +0100250 pkt = reassemble4(out)
Ole Troanb3655e52018-08-16 22:08:49 +0200251 pkt = ether / pkt
252 self.check_encapsulation(pkt, self.single_tunnel_bd)
253
254 payload = self.decapsulate(pkt)
255 # TODO: Scapy bug?
256 # self.assert_eq_pkts(payload, frame)
257
Klement Sekeraf62ae122016-10-11 11:47:09 +0200258 # Method to define VPP actions before tear down of the test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200259 # Overrides tearDown method in VppTestCase class.
260 # @param self The object pointer.
261 def tearDown(self):
262 super(TestVxlan, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700263
264 def show_commands_at_teardown(self):
265 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
266 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
267 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
268 self.logger.info(self.vapi.cli("show vxlan tunnel"))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200269
Matej Klottondeb69842016-12-09 15:05:46 +0100270
Damjan Marionf56b77a2016-10-03 19:44:57 +0200271if __name__ == '__main__':
272 unittest.main(testRunner=VppTestRunner)