blob: 3c824b5761a9c530af86537df8fa5edaade7362b [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 Baricef1e2a2018-06-18 13:01:59 +03004from util import ip4n_range, ip4_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
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
Ole Troanb3655e52018-08-16 22:08:49 +020014import StringIO
15
16
17def reassemble(listoffragments):
18 buffer = StringIO.StringIO()
19 first = listoffragments[0]
20 buffer.seek(20)
21 for pkt in listoffragments:
22 buffer.seek(pkt[IP].frag*8)
23 buffer.write(pkt[IP].payload)
24 first.len = len(buffer.getvalue()) + 20
25 first.flags = 0
26 del(first.chksum)
27 header = str(first[IP])[:20]
28 return first[IP].__class__(header + buffer.getvalue())
29
Damjan Marionf56b77a2016-10-03 19:44:57 +020030
Klement Sekeraf62ae122016-10-11 11:47:09 +020031class TestVxlan(BridgeDomain, VppTestCase):
Damjan Marionf56b77a2016-10-03 19:44:57 +020032 """ VXLAN Test Case """
33
Damjan Marionf56b77a2016-10-03 19:44:57 +020034 def __init__(self, *args):
35 BridgeDomain.__init__(self)
Damjan Marionf56b77a2016-10-03 19:44:57 +020036 VppTestCase.__init__(self, *args)
37
Eyal Baric4aaee12016-12-20 18:36:46 +020038 def encapsulate(self, pkt, vni):
Klement Sekeraf62ae122016-10-11 11:47:09 +020039 """
40 Encapsulate the original payload frame by adding VXLAN header with its
41 UDP, IP and Ethernet fields
42 """
43 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
44 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
45 UDP(sport=self.dport, dport=self.dport, chksum=0) /
Eyal Baric4aaee12016-12-20 18:36:46 +020046 VXLAN(vni=vni, flags=self.flags) /
47 pkt)
48
Eyal Baricef1e2a2018-06-18 13:01:59 +030049 def ip_range(self, start, end):
50 """ range of remote ip's """
51 return ip4_range(self.pg0.remote_ip4, start, end)
52
Eyal Baric4aaee12016-12-20 18:36:46 +020053 def encap_mcast(self, pkt, src_ip, src_mac, vni):
54 """
55 Encapsulate the original payload frame by adding VXLAN header with its
56 UDP, IP and Ethernet fields
57 """
Eyal Barid81da8c2017-01-11 13:39:54 +020058 return (Ether(src=src_mac, dst=self.mcast_mac) /
Eyal Baric4aaee12016-12-20 18:36:46 +020059 IP(src=src_ip, dst=self.mcast_ip4) /
60 UDP(sport=self.dport, dport=self.dport, chksum=0) /
61 VXLAN(vni=vni, flags=self.flags) /
Damjan Marionf56b77a2016-10-03 19:44:57 +020062 pkt)
63
Damjan Marionf56b77a2016-10-03 19:44:57 +020064 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020065 """
66 Decapsulate the original payload frame by removing VXLAN header
67 """
Matej Klottondeb69842016-12-09 15:05:46 +010068 # check if is set I flag
69 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
Damjan Marionf56b77a2016-10-03 19:44:57 +020070 return pkt[VXLAN].payload
71
Klement Sekeraf62ae122016-10-11 11:47:09 +020072 # Method for checking VXLAN encapsulation.
Damjan Marionf56b77a2016-10-03 19:44:57 +020073 #
Eyal Bari6ae5ee72017-03-23 09:53:51 +020074 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
Damjan Marionf56b77a2016-10-03 19:44:57 +020075 # TODO: add error messages
Klement Sekeraf62ae122016-10-11 11:47:09 +020076 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
Damjan Marionf56b77a2016-10-03 19:44:57 +020077 # by VPP using ARP.
Klement Sekeraf62ae122016-10-11 11:47:09 +020078 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
Eyal Baric4aaee12016-12-20 18:36:46 +020079 if not local_only:
Eyal Bari6ae5ee72017-03-23 09:53:51 +020080 if not mcast_pkt:
81 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
82 else:
83 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
Klement Sekeraf62ae122016-10-11 11:47:09 +020084 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
85 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
Eyal Baric4aaee12016-12-20 18:36:46 +020086 if not local_only:
Eyal Bari6ae5ee72017-03-23 09:53:51 +020087 if not mcast_pkt:
88 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
89 else:
90 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020091 # Verify UDP destination port is VXLAN 4789, source UDP port could be
Damjan Marionf56b77a2016-10-03 19:44:57 +020092 # arbitrary.
Klement Sekeraf62ae122016-10-11 11:47:09 +020093 self.assertEqual(pkt[UDP].dport, type(self).dport)
Damjan Marionf56b77a2016-10-03 19:44:57 +020094 # TODO: checksum check
Eyal Baric4aaee12016-12-20 18:36:46 +020095 # Verify VNI
96 self.assertEqual(pkt[VXLAN].vni, vni)
97
Eyal Baric4aaee12016-12-20 18:36:46 +020098 @classmethod
Eyal Barid81da8c2017-01-11 13:39:54 +020099 def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
Eyal Baric4aaee12016-12-20 18:36:46 +0200100 # Create 10 ucast vxlan tunnels under bd
101 ip_range_start = 10
Eyal Barid81da8c2017-01-11 13:39:54 +0200102 ip_range_end = ip_range_start + n_ucast_tunnels
Eyal Baric4aaee12016-12-20 18:36:46 +0200103 next_hop_address = cls.pg0.remote_ip4n
Eyal Barid81da8c2017-01-11 13:39:54 +0200104 for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
105 ip_range_end):
106 # add host route so dest_ip4n will not be resolved
107 cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
Eyal Baric4aaee12016-12-20 18:36:46 +0200108 r = cls.vapi.vxlan_add_del_tunnel(
109 src_addr=cls.pg0.local_ip4n,
Eyal Barid81da8c2017-01-11 13:39:54 +0200110 dst_addr=dest_ip4n,
Eyal Baric4aaee12016-12-20 18:36:46 +0200111 vni=vni)
112 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
113
114 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200115 def add_del_shared_mcast_dst_load(cls, is_add):
116 """
117 add or del tunnels sharing the same mcast dst
118 to test vxlan ref_count mechanism
119 """
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100120 n_shared_dst_tunnels = 20
Eyal Bari4bce2902017-01-16 12:02:46 +0200121 vni_start = 10000
122 vni_end = vni_start + n_shared_dst_tunnels
123 for vni in range(vni_start, vni_end):
Eyal Barid9278342017-04-06 03:31:00 +0300124 r = cls.vapi.vxlan_add_del_tunnel(
Eyal Bari4bce2902017-01-16 12:02:46 +0200125 src_addr=cls.pg0.local_ip4n,
126 dst_addr=cls.mcast_ip4n,
127 mcast_sw_if_index=1,
128 vni=vni,
129 is_add=is_add)
Eyal Barid9278342017-04-06 03:31:00 +0300130 if r.sw_if_index == 0xffffffff:
131 raise "bad sw_if_index"
Eyal Bari4bce2902017-01-16 12:02:46 +0200132
133 @classmethod
134 def add_shared_mcast_dst_load(cls):
135 cls.add_del_shared_mcast_dst_load(is_add=1)
136
137 @classmethod
138 def del_shared_mcast_dst_load(cls):
139 cls.add_del_shared_mcast_dst_load(is_add=0)
140
141 @classmethod
142 def add_del_mcast_tunnels_load(cls, is_add):
143 """
144 add or del tunnels to test vxlan stability
145 """
146 n_distinct_dst_tunnels = 200
Eyal Baric4aaee12016-12-20 18:36:46 +0200147 ip_range_start = 10
Eyal Bari4bce2902017-01-16 12:02:46 +0200148 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Eyal Barid81da8c2017-01-11 13:39:54 +0200149 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
150 ip_range_end):
151 vni = bytearray(dest_ip4n)[3]
Eyal Baric4aaee12016-12-20 18:36:46 +0200152 cls.vapi.vxlan_add_del_tunnel(
153 src_addr=cls.pg0.local_ip4n,
Eyal Barid81da8c2017-01-11 13:39:54 +0200154 dst_addr=dest_ip4n,
Eyal Baric4aaee12016-12-20 18:36:46 +0200155 mcast_sw_if_index=1,
156 vni=vni,
157 is_add=is_add)
158
159 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200160 def add_mcast_tunnels_load(cls):
161 cls.add_del_mcast_tunnels_load(is_add=1)
Eyal Baric4aaee12016-12-20 18:36:46 +0200162
163 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200164 def del_mcast_tunnels_load(cls):
165 cls.add_del_mcast_tunnels_load(is_add=0)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200166
Klement Sekeraf62ae122016-10-11 11:47:09 +0200167 # Class method to start the VXLAN test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200168 # Overrides setUpClass method in VppTestCase class.
169 # Python try..except statement is used to ensure that the tear down of
170 # the class will be executed even if exception is raised.
171 # @param cls The class pointer.
172 @classmethod
173 def setUpClass(cls):
174 super(TestVxlan, cls).setUpClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200175
Klement Sekeraf62ae122016-10-11 11:47:09 +0200176 try:
177 cls.dport = 4789
Matej Klottondeb69842016-12-09 15:05:46 +0100178 cls.flags = 0x8
Klement Sekeraf62ae122016-10-11 11:47:09 +0200179
180 # Create 2 pg interfaces.
Eyal Baric4aaee12016-12-20 18:36:46 +0200181 cls.create_pg_interfaces(range(4))
182 for pg in cls.pg_interfaces:
183 pg.admin_up()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200184
185 # Configure IPv4 addresses on VPP pg0.
186 cls.pg0.config_ip4()
187
188 # Resolve MAC address for VPP's IP address on pg0.
189 cls.pg0.resolve_arp()
190
Eyal Baric4aaee12016-12-20 18:36:46 +0200191 # Our Multicast address
192 cls.mcast_ip4 = '239.1.1.1'
193 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
194 iplong = atol(cls.mcast_ip4)
Eyal Barid81da8c2017-01-11 13:39:54 +0200195 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
Eyal Baric4aaee12016-12-20 18:36:46 +0200196 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
197
Klement Sekeraf62ae122016-10-11 11:47:09 +0200198 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
Damjan Marionf56b77a2016-10-03 19:44:57 +0200199 # into BD.
Eyal Baric4aaee12016-12-20 18:36:46 +0200200 cls.single_tunnel_bd = 1
Klement Sekeraf62ae122016-10-11 11:47:09 +0200201 r = cls.vapi.vxlan_add_del_tunnel(
202 src_addr=cls.pg0.local_ip4n,
203 dst_addr=cls.pg0.remote_ip4n,
Eyal Baric4aaee12016-12-20 18:36:46 +0200204 vni=cls.single_tunnel_bd)
205 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
206 bd_id=cls.single_tunnel_bd)
207 cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
208 bd_id=cls.single_tunnel_bd)
209
210 # Setup vni 2 to test multicast flooding
Eyal Barid81da8c2017-01-11 13:39:54 +0200211 cls.n_ucast_tunnels = 10
Eyal Baric4aaee12016-12-20 18:36:46 +0200212 cls.mcast_flood_bd = 2
Eyal Barid81da8c2017-01-11 13:39:54 +0200213 cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
214 cls.n_ucast_tunnels)
Eyal Baric4aaee12016-12-20 18:36:46 +0200215 r = cls.vapi.vxlan_add_del_tunnel(
216 src_addr=cls.pg0.local_ip4n,
217 dst_addr=cls.mcast_ip4n,
218 mcast_sw_if_index=1,
219 vni=cls.mcast_flood_bd)
220 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
221 bd_id=cls.mcast_flood_bd)
222 cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
223 bd_id=cls.mcast_flood_bd)
224
225 # Add and delete mcast tunnels to check stability
Eyal Bari4bce2902017-01-16 12:02:46 +0200226 cls.add_shared_mcast_dst_load()
227 cls.add_mcast_tunnels_load()
228 cls.del_shared_mcast_dst_load()
229 cls.del_mcast_tunnels_load()
Eyal Baric4aaee12016-12-20 18:36:46 +0200230
231 # Setup vni 3 to test unicast flooding
232 cls.ucast_flood_bd = 3
Eyal Barid81da8c2017-01-11 13:39:54 +0200233 cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
234 cls.n_ucast_tunnels)
Eyal Baric4aaee12016-12-20 18:36:46 +0200235 cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
236 bd_id=cls.ucast_flood_bd)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200237 except Exception:
238 super(TestVxlan, cls).tearDownClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200239 raise
240
Ole Troanb3655e52018-08-16 22:08:49 +0200241 def test_encap_big_packet(self):
242 """ Encapsulation test send big frame from pg1
243 Verify receipt of encapsulated frames on pg0
244 """
245
246 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
247
248 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
249 IP(src='4.3.2.1', dst='1.2.3.4') /
250 UDP(sport=20000, dport=10000) /
251 Raw('\xa5' * 1450))
252
253 self.pg1.add_stream([frame])
254
255 self.pg0.enable_capture()
256
257 self.pg_start()
258
259 # Pick first received frame and check if it's correctly encapsulated.
260 out = self.pg0.get_capture(2)
261 ether = out[0]
262 pkt = reassemble(out)
263 pkt = ether / pkt
264 self.check_encapsulation(pkt, self.single_tunnel_bd)
265
266 payload = self.decapsulate(pkt)
267 # TODO: Scapy bug?
268 # self.assert_eq_pkts(payload, frame)
269
Klement Sekeraf62ae122016-10-11 11:47:09 +0200270 # Method to define VPP actions before tear down of the test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200271 # Overrides tearDown method in VppTestCase class.
272 # @param self The object pointer.
273 def tearDown(self):
274 super(TestVxlan, self).tearDown()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200275 if not self.vpp_dead:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100276 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
Eyal Baric4aaee12016-12-20 18:36:46 +0200277 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
278 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
279 self.logger.info(self.vapi.cli("show vxlan tunnel"))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200280
Matej Klottondeb69842016-12-09 15:05:46 +0100281
Damjan Marionf56b77a2016-10-03 19:44:57 +0200282if __name__ == '__main__':
283 unittest.main(testRunner=VppTestRunner)