blob: aa069dc6e7bfd373f73f2e231bd7a404ad0b24af [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
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
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
Eyal Baricef1e2a2018-06-18 13:01:59 +030033 def ip_range(self, start, end):
34 """ range of remote ip's """
35 return ip4_range(self.pg0.remote_ip4, start, end)
36
Eyal Baric4aaee12016-12-20 18:36:46 +020037 def encap_mcast(self, pkt, src_ip, src_mac, vni):
38 """
39 Encapsulate the original payload frame by adding VXLAN header with its
40 UDP, IP and Ethernet fields
41 """
Eyal Barid81da8c2017-01-11 13:39:54 +020042 return (Ether(src=src_mac, dst=self.mcast_mac) /
Eyal Baric4aaee12016-12-20 18:36:46 +020043 IP(src=src_ip, dst=self.mcast_ip4) /
44 UDP(sport=self.dport, dport=self.dport, chksum=0) /
45 VXLAN(vni=vni, flags=self.flags) /
Damjan Marionf56b77a2016-10-03 19:44:57 +020046 pkt)
47
Damjan Marionf56b77a2016-10-03 19:44:57 +020048 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020049 """
50 Decapsulate the original payload frame by removing VXLAN header
51 """
Matej Klottondeb69842016-12-09 15:05:46 +010052 # check if is set I flag
53 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
Damjan Marionf56b77a2016-10-03 19:44:57 +020054 return pkt[VXLAN].payload
55
Klement Sekeraf62ae122016-10-11 11:47:09 +020056 # Method for checking VXLAN encapsulation.
Damjan Marionf56b77a2016-10-03 19:44:57 +020057 #
Eyal Bari6ae5ee72017-03-23 09:53:51 +020058 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
Damjan Marionf56b77a2016-10-03 19:44:57 +020059 # TODO: add error messages
Klement Sekeraf62ae122016-10-11 11:47:09 +020060 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
Damjan Marionf56b77a2016-10-03 19:44:57 +020061 # by VPP using ARP.
Klement Sekeraf62ae122016-10-11 11:47:09 +020062 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
Eyal Baric4aaee12016-12-20 18:36:46 +020063 if not local_only:
Eyal Bari6ae5ee72017-03-23 09:53:51 +020064 if not mcast_pkt:
65 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
66 else:
67 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
Klement Sekeraf62ae122016-10-11 11:47:09 +020068 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
69 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
Eyal Baric4aaee12016-12-20 18:36:46 +020070 if not local_only:
Eyal Bari6ae5ee72017-03-23 09:53:51 +020071 if not mcast_pkt:
72 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
73 else:
74 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020075 # Verify UDP destination port is VXLAN 4789, source UDP port could be
Damjan Marionf56b77a2016-10-03 19:44:57 +020076 # arbitrary.
Klement Sekeraf62ae122016-10-11 11:47:09 +020077 self.assertEqual(pkt[UDP].dport, type(self).dport)
Damjan Marionf56b77a2016-10-03 19:44:57 +020078 # TODO: checksum check
Eyal Baric4aaee12016-12-20 18:36:46 +020079 # Verify VNI
80 self.assertEqual(pkt[VXLAN].vni, vni)
81
Eyal Baric4aaee12016-12-20 18:36:46 +020082 @classmethod
Eyal Barid81da8c2017-01-11 13:39:54 +020083 def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
Eyal Baric4aaee12016-12-20 18:36:46 +020084 # Create 10 ucast vxlan tunnels under bd
85 ip_range_start = 10
Eyal Barid81da8c2017-01-11 13:39:54 +020086 ip_range_end = ip_range_start + n_ucast_tunnels
Eyal Baric4aaee12016-12-20 18:36:46 +020087 next_hop_address = cls.pg0.remote_ip4n
Eyal Barid81da8c2017-01-11 13:39:54 +020088 for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
89 ip_range_end):
90 # add host route so dest_ip4n will not be resolved
Ole Troana5b2eec2019-03-11 19:23:25 +010091 cls.vapi.ip_add_del_route(dst_address=dest_ip4n,
92 dst_address_length=32,
93 next_hop_address=next_hop_address)
94 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
95 dst_address=dest_ip4n, vni=vni)
96 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
97 bd_id=vni)
Eyal Baric4aaee12016-12-20 18:36:46 +020098
99 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200100 def add_del_shared_mcast_dst_load(cls, is_add):
101 """
102 add or del tunnels sharing the same mcast dst
103 to test vxlan ref_count mechanism
104 """
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100105 n_shared_dst_tunnels = 20
Eyal Bari4bce2902017-01-16 12:02:46 +0200106 vni_start = 10000
107 vni_end = vni_start + n_shared_dst_tunnels
108 for vni in range(vni_start, vni_end):
Ole Troana5b2eec2019-03-11 19:23:25 +0100109 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
110 dst_address=cls.mcast_ip4n,
111 mcast_sw_if_index=1,
112 is_add=is_add, vni=vni)
Eyal Barid9278342017-04-06 03:31:00 +0300113 if r.sw_if_index == 0xffffffff:
Paul Vinciguerrac599c6f2019-03-12 17:41:27 -0700114 raise ValueError("bad sw_if_index: ~0")
Eyal Bari4bce2902017-01-16 12:02:46 +0200115
116 @classmethod
117 def add_shared_mcast_dst_load(cls):
118 cls.add_del_shared_mcast_dst_load(is_add=1)
119
120 @classmethod
121 def del_shared_mcast_dst_load(cls):
122 cls.add_del_shared_mcast_dst_load(is_add=0)
123
124 @classmethod
125 def add_del_mcast_tunnels_load(cls, is_add):
126 """
127 add or del tunnels to test vxlan stability
128 """
129 n_distinct_dst_tunnels = 200
Eyal Baric4aaee12016-12-20 18:36:46 +0200130 ip_range_start = 10
Eyal Bari4bce2902017-01-16 12:02:46 +0200131 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Eyal Barid81da8c2017-01-11 13:39:54 +0200132 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
133 ip_range_end):
134 vni = bytearray(dest_ip4n)[3]
Ole Troana5b2eec2019-03-11 19:23:25 +0100135 cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
136 dst_address=dest_ip4n,
137 mcast_sw_if_index=1, is_add=is_add,
138 vni=vni)
Eyal Baric4aaee12016-12-20 18:36:46 +0200139
140 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200141 def add_mcast_tunnels_load(cls):
142 cls.add_del_mcast_tunnels_load(is_add=1)
Eyal Baric4aaee12016-12-20 18:36:46 +0200143
144 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200145 def del_mcast_tunnels_load(cls):
146 cls.add_del_mcast_tunnels_load(is_add=0)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200147
Klement Sekeraf62ae122016-10-11 11:47:09 +0200148 # Class method to start the VXLAN test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200149 # Overrides setUpClass method in VppTestCase class.
150 # Python try..except statement is used to ensure that the tear down of
151 # the class will be executed even if exception is raised.
152 # @param cls The class pointer.
153 @classmethod
154 def setUpClass(cls):
155 super(TestVxlan, cls).setUpClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200156
Klement Sekeraf62ae122016-10-11 11:47:09 +0200157 try:
158 cls.dport = 4789
Matej Klottondeb69842016-12-09 15:05:46 +0100159 cls.flags = 0x8
Klement Sekeraf62ae122016-10-11 11:47:09 +0200160
161 # Create 2 pg interfaces.
Eyal Baric4aaee12016-12-20 18:36:46 +0200162 cls.create_pg_interfaces(range(4))
163 for pg in cls.pg_interfaces:
164 pg.admin_up()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200165
166 # Configure IPv4 addresses on VPP pg0.
167 cls.pg0.config_ip4()
168
169 # Resolve MAC address for VPP's IP address on pg0.
170 cls.pg0.resolve_arp()
171
Eyal Baric4aaee12016-12-20 18:36:46 +0200172 # Our Multicast address
173 cls.mcast_ip4 = '239.1.1.1'
174 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
175 iplong = atol(cls.mcast_ip4)
Eyal Barid81da8c2017-01-11 13:39:54 +0200176 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
Eyal Baric4aaee12016-12-20 18:36:46 +0200177 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
178
Klement Sekeraf62ae122016-10-11 11:47:09 +0200179 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
Damjan Marionf56b77a2016-10-03 19:44:57 +0200180 # into BD.
Eyal Baric4aaee12016-12-20 18:36:46 +0200181 cls.single_tunnel_bd = 1
Ole Troana5b2eec2019-03-11 19:23:25 +0100182 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
183 dst_address=cls.pg0.remote_ip4n,
184 vni=cls.single_tunnel_bd)
185 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Eyal Baric4aaee12016-12-20 18:36:46 +0200186 bd_id=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100187 cls.vapi.sw_interface_set_l2_bridge(
188 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
Eyal Baric4aaee12016-12-20 18:36:46 +0200189
190 # Setup vni 2 to test multicast flooding
Eyal Barid81da8c2017-01-11 13:39:54 +0200191 cls.n_ucast_tunnels = 10
Eyal Baric4aaee12016-12-20 18:36:46 +0200192 cls.mcast_flood_bd = 2
Eyal Barid81da8c2017-01-11 13:39:54 +0200193 cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
194 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100195 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
196 dst_address=cls.mcast_ip4n,
197 mcast_sw_if_index=1,
198 vni=cls.mcast_flood_bd)
199 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Eyal Baric4aaee12016-12-20 18:36:46 +0200200 bd_id=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100201 cls.vapi.sw_interface_set_l2_bridge(
202 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
Eyal Baric4aaee12016-12-20 18:36:46 +0200203
204 # Add and delete mcast tunnels to check stability
Eyal Bari4bce2902017-01-16 12:02:46 +0200205 cls.add_shared_mcast_dst_load()
206 cls.add_mcast_tunnels_load()
207 cls.del_shared_mcast_dst_load()
208 cls.del_mcast_tunnels_load()
Eyal Baric4aaee12016-12-20 18:36:46 +0200209
210 # Setup vni 3 to test unicast flooding
211 cls.ucast_flood_bd = 3
Eyal Barid81da8c2017-01-11 13:39:54 +0200212 cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
213 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100214 cls.vapi.sw_interface_set_l2_bridge(
215 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200216 except Exception:
217 super(TestVxlan, cls).tearDownClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200218 raise
219
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700220 @classmethod
221 def tearDownClass(cls):
222 super(TestVxlan, cls).tearDownClass()
223
Ole Troanb3655e52018-08-16 22:08:49 +0200224 def test_encap_big_packet(self):
225 """ Encapsulation test send big frame from pg1
226 Verify receipt of encapsulated frames on pg0
227 """
228
229 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
230
231 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
232 IP(src='4.3.2.1', dst='1.2.3.4') /
233 UDP(sport=20000, dport=10000) /
234 Raw('\xa5' * 1450))
235
236 self.pg1.add_stream([frame])
237
238 self.pg0.enable_capture()
239
240 self.pg_start()
241
242 # Pick first received frame and check if it's correctly encapsulated.
243 out = self.pg0.get_capture(2)
244 ether = out[0]
Ole Troan7f991832018-12-06 17:35:12 +0100245 pkt = reassemble4(out)
Ole Troanb3655e52018-08-16 22:08:49 +0200246 pkt = ether / pkt
247 self.check_encapsulation(pkt, self.single_tunnel_bd)
248
249 payload = self.decapsulate(pkt)
250 # TODO: Scapy bug?
251 # self.assert_eq_pkts(payload, frame)
252
Klement Sekeraf62ae122016-10-11 11:47:09 +0200253 # Method to define VPP actions before tear down of the test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200254 # Overrides tearDown method in VppTestCase class.
255 # @param self The object pointer.
256 def tearDown(self):
257 super(TestVxlan, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700258
259 def show_commands_at_teardown(self):
260 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
261 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
262 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
263 self.logger.info(self.vapi.cli("show vxlan tunnel"))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200264
Matej Klottondeb69842016-12-09 15:05:46 +0100265
Damjan Marionf56b77a2016-10-03 19:44:57 +0200266if __name__ == '__main__':
267 unittest.main(testRunner=VppTestRunner)