blob: 910611c63cc18f67898546737849865daa20f333 [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
snaramre5d4b8912019-12-13 23:39:35 +00009from scapy.layers.l2 import Ether
10from scapy.packet import Raw
Damjan Marionf56b77a2016-10-03 19:44:57 +020011from scapy.layers.inet import IP, UDP
Matej Klottondeb69842016-12-09 15:05:46 +010012from scapy.layers.vxlan import VXLAN
Eyal Baric4aaee12016-12-20 18:36:46 +020013from scapy.utils import atol
Neale Ranns097fa662018-05-01 05:17:55 -070014from vpp_ip_route import VppIpRoute, VppRoutePath
Jakub Grajciar7c0eb562020-03-02 13:55:31 +010015from vpp_vxlan_tunnel import VppVxlanTunnel
Neale Ranns097fa662018-05-01 05:17:55 -070016from vpp_ip import INVALID_INDEX
Damjan Marionf56b77a2016-10-03 19:44:57 +020017
18
Klement Sekeraf62ae122016-10-11 11:47:09 +020019class TestVxlan(BridgeDomain, VppTestCase):
Damjan Marionf56b77a2016-10-03 19:44:57 +020020 """ VXLAN Test Case """
21
Damjan Marionf56b77a2016-10-03 19:44:57 +020022 def __init__(self, *args):
23 BridgeDomain.__init__(self)
Damjan Marionf56b77a2016-10-03 19:44:57 +020024 VppTestCase.__init__(self, *args)
25
Eyal Baric4aaee12016-12-20 18:36:46 +020026 def encapsulate(self, pkt, vni):
Klement Sekeraf62ae122016-10-11 11:47:09 +020027 """
28 Encapsulate the original payload frame by adding VXLAN header with its
29 UDP, IP and Ethernet fields
30 """
31 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
32 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
33 UDP(sport=self.dport, dport=self.dport, chksum=0) /
Eyal Baric4aaee12016-12-20 18:36:46 +020034 VXLAN(vni=vni, flags=self.flags) /
35 pkt)
36
Eyal Baricef1e2a2018-06-18 13:01:59 +030037 def ip_range(self, start, end):
38 """ range of remote ip's """
39 return ip4_range(self.pg0.remote_ip4, start, end)
40
Eyal Baric4aaee12016-12-20 18:36:46 +020041 def encap_mcast(self, pkt, src_ip, src_mac, vni):
42 """
43 Encapsulate the original payload frame by adding VXLAN header with its
44 UDP, IP and Ethernet fields
45 """
Eyal Barid81da8c2017-01-11 13:39:54 +020046 return (Ether(src=src_mac, dst=self.mcast_mac) /
Eyal Baric4aaee12016-12-20 18:36:46 +020047 IP(src=src_ip, dst=self.mcast_ip4) /
48 UDP(sport=self.dport, dport=self.dport, chksum=0) /
49 VXLAN(vni=vni, flags=self.flags) /
Damjan Marionf56b77a2016-10-03 19:44:57 +020050 pkt)
51
Damjan Marionf56b77a2016-10-03 19:44:57 +020052 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020053 """
54 Decapsulate the original payload frame by removing VXLAN header
55 """
Matej Klottondeb69842016-12-09 15:05:46 +010056 # check if is set I flag
57 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
Damjan Marionf56b77a2016-10-03 19:44:57 +020058 return pkt[VXLAN].payload
59
Klement Sekeraf62ae122016-10-11 11:47:09 +020060 # Method for checking VXLAN encapsulation.
Damjan Marionf56b77a2016-10-03 19:44:57 +020061 #
Eyal Bari6ae5ee72017-03-23 09:53:51 +020062 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
Damjan Marionf56b77a2016-10-03 19:44:57 +020063 # TODO: add error messages
Klement Sekeraf62ae122016-10-11 11:47:09 +020064 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
Damjan Marionf56b77a2016-10-03 19:44:57 +020065 # by VPP using ARP.
Klement Sekeraf62ae122016-10-11 11:47:09 +020066 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
Eyal Baric4aaee12016-12-20 18:36:46 +020067 if not local_only:
Eyal Bari6ae5ee72017-03-23 09:53:51 +020068 if not mcast_pkt:
69 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
70 else:
71 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
Klement Sekeraf62ae122016-10-11 11:47:09 +020072 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
73 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
Eyal Baric4aaee12016-12-20 18:36:46 +020074 if not local_only:
Eyal Bari6ae5ee72017-03-23 09:53:51 +020075 if not mcast_pkt:
76 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
77 else:
78 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020079 # Verify UDP destination port is VXLAN 4789, source UDP port could be
Damjan Marionf56b77a2016-10-03 19:44:57 +020080 # arbitrary.
Klement Sekeraf62ae122016-10-11 11:47:09 +020081 self.assertEqual(pkt[UDP].dport, type(self).dport)
Damjan Marionf56b77a2016-10-03 19:44:57 +020082 # TODO: checksum check
Eyal Baric4aaee12016-12-20 18:36:46 +020083 # Verify VNI
84 self.assertEqual(pkt[VXLAN].vni, vni)
85
Eyal Baric4aaee12016-12-20 18:36:46 +020086 @classmethod
Eyal Barid81da8c2017-01-11 13:39:54 +020087 def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
Eyal Baric4aaee12016-12-20 18:36:46 +020088 # Create 10 ucast vxlan tunnels under bd
89 ip_range_start = 10
Eyal Barid81da8c2017-01-11 13:39:54 +020090 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -070091 next_hop_address = cls.pg0.remote_ip4
92 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
93 ip_range_end):
Eyal Barid81da8c2017-01-11 13:39:54 +020094 # add host route so dest_ip4n will not be resolved
Neale Ranns097fa662018-05-01 05:17:55 -070095 rip = VppIpRoute(cls, dest_ip4, 32,
96 [VppRoutePath(next_hop_address,
97 INVALID_INDEX)],
98 register=False)
99 rip.add_vpp_config()
100 dest_ip4n = socket.inet_pton(socket.AF_INET, dest_ip4)
101
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100102 r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
103 dst=dest_ip4, vni=vni)
104 r.add_vpp_config()
Neale Ranns097fa662018-05-01 05:17:55 -0700105 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
Eyal Baric4aaee12016-12-20 18:36:46 +0200106
107 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200108 def add_del_shared_mcast_dst_load(cls, is_add):
109 """
110 add or del tunnels sharing the same mcast dst
111 to test vxlan ref_count mechanism
112 """
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100113 n_shared_dst_tunnels = 20
Eyal Bari4bce2902017-01-16 12:02:46 +0200114 vni_start = 10000
115 vni_end = vni_start + n_shared_dst_tunnels
116 for vni in range(vni_start, vni_end):
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100117 r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
118 dst=cls.mcast_ip4, mcast_sw_if_index=1, vni=vni)
119 if is_add:
120 r.add_vpp_config()
121 if r.sw_if_index == 0xffffffff:
122 raise ValueError("bad sw_if_index: ~0")
123 else:
124 r.remove_vpp_config()
Eyal Bari4bce2902017-01-16 12:02:46 +0200125
126 @classmethod
127 def add_shared_mcast_dst_load(cls):
128 cls.add_del_shared_mcast_dst_load(is_add=1)
129
130 @classmethod
131 def del_shared_mcast_dst_load(cls):
132 cls.add_del_shared_mcast_dst_load(is_add=0)
133
134 @classmethod
135 def add_del_mcast_tunnels_load(cls, is_add):
136 """
137 add or del tunnels to test vxlan stability
138 """
139 n_distinct_dst_tunnels = 200
Eyal Baric4aaee12016-12-20 18:36:46 +0200140 ip_range_start = 10
Eyal Bari4bce2902017-01-16 12:02:46 +0200141 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100142 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
143 ip_range_end):
144 vni = bytearray(socket.inet_pton(socket.AF_INET, dest_ip4))[3]
145 r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
146 dst=dest_ip4, mcast_sw_if_index=1, vni=vni)
147 if is_add:
148 r.add_vpp_config()
149 else:
150 r.remove_vpp_config()
Eyal Baric4aaee12016-12-20 18:36:46 +0200151
152 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200153 def add_mcast_tunnels_load(cls):
154 cls.add_del_mcast_tunnels_load(is_add=1)
Eyal Baric4aaee12016-12-20 18:36:46 +0200155
156 @classmethod
Eyal Bari4bce2902017-01-16 12:02:46 +0200157 def del_mcast_tunnels_load(cls):
158 cls.add_del_mcast_tunnels_load(is_add=0)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200159
Klement Sekeraf62ae122016-10-11 11:47:09 +0200160 # Class method to start the VXLAN test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200161 # Overrides setUpClass method in VppTestCase class.
162 # Python try..except statement is used to ensure that the tear down of
163 # the class will be executed even if exception is raised.
164 # @param cls The class pointer.
165 @classmethod
166 def setUpClass(cls):
167 super(TestVxlan, cls).setUpClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200168
Klement Sekeraf62ae122016-10-11 11:47:09 +0200169 try:
170 cls.dport = 4789
Matej Klottondeb69842016-12-09 15:05:46 +0100171 cls.flags = 0x8
Klement Sekeraf62ae122016-10-11 11:47:09 +0200172
173 # Create 2 pg interfaces.
Eyal Baric4aaee12016-12-20 18:36:46 +0200174 cls.create_pg_interfaces(range(4))
175 for pg in cls.pg_interfaces:
176 pg.admin_up()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200177
178 # Configure IPv4 addresses on VPP pg0.
179 cls.pg0.config_ip4()
180
181 # Resolve MAC address for VPP's IP address on pg0.
182 cls.pg0.resolve_arp()
183
Eyal Baric4aaee12016-12-20 18:36:46 +0200184 # Our Multicast address
185 cls.mcast_ip4 = '239.1.1.1'
186 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
187 iplong = atol(cls.mcast_ip4)
Eyal Barid81da8c2017-01-11 13:39:54 +0200188 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
Eyal Baric4aaee12016-12-20 18:36:46 +0200189 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
190
Klement Sekeraf62ae122016-10-11 11:47:09 +0200191 except Exception:
192 super(TestVxlan, cls).tearDownClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200193 raise
194
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700195 @classmethod
196 def tearDownClass(cls):
197 super(TestVxlan, cls).tearDownClass()
198
Jakub Grajciar7c0eb562020-03-02 13:55:31 +0100199 def setUp(self):
200 super(TestVxlan, self).setUp()
201 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
202 # into BD.
203 self.single_tunnel_bd = 1
204 r = VppVxlanTunnel(self, src=self.pg0.local_ip4,
205 dst=self.pg0.remote_ip4, vni=self.single_tunnel_bd)
206 r.add_vpp_config()
207 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
208 bd_id=self.single_tunnel_bd)
209 self.vapi.sw_interface_set_l2_bridge(
210 rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd)
211
212 # Setup vni 2 to test multicast flooding
213 self.n_ucast_tunnels = 10
214 self.mcast_flood_bd = 2
215 self.create_vxlan_flood_test_bd(self.mcast_flood_bd,
216 self.n_ucast_tunnels)
217 r = VppVxlanTunnel(self, src=self.pg0.local_ip4, dst=self.mcast_ip4,
218 mcast_sw_if_index=1, vni=self.mcast_flood_bd)
219 r.add_vpp_config()
220 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
221 bd_id=self.mcast_flood_bd)
222 self.vapi.sw_interface_set_l2_bridge(
223 rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd)
224
225 # Add and delete mcast tunnels to check stability
226 self.add_shared_mcast_dst_load()
227 self.add_mcast_tunnels_load()
228 self.del_shared_mcast_dst_load()
229 self.del_mcast_tunnels_load()
230
231 # Setup vni 3 to test unicast flooding
232 self.ucast_flood_bd = 3
233 self.create_vxlan_flood_test_bd(self.ucast_flood_bd,
234 self.n_ucast_tunnels)
235 self.vapi.sw_interface_set_l2_bridge(
236 rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd)
237
238 def test_decap(self):
239 """ Decapsulation test
240 from BridgeDoman
241 """
242 super(TestVxlan, self).test_decap()
243
Ole Troanb3655e52018-08-16 22:08:49 +0200244 def test_encap_big_packet(self):
245 """ Encapsulation test send big frame from pg1
246 Verify receipt of encapsulated frames on pg0
247 """
248
249 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
250
251 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
252 IP(src='4.3.2.1', dst='1.2.3.4') /
253 UDP(sport=20000, dport=10000) /
Ole Troan770a0de2019-11-07 13:52:21 +0100254 Raw(b'\xa5' * 1450))
Ole Troanb3655e52018-08-16 22:08:49 +0200255
256 self.pg1.add_stream([frame])
257
258 self.pg0.enable_capture()
259
260 self.pg_start()
261
262 # Pick first received frame and check if it's correctly encapsulated.
263 out = self.pg0.get_capture(2)
264 ether = out[0]
Ole Troan7f991832018-12-06 17:35:12 +0100265 pkt = reassemble4(out)
Ole Troanb3655e52018-08-16 22:08:49 +0200266 pkt = ether / pkt
267 self.check_encapsulation(pkt, self.single_tunnel_bd)
268
269 payload = self.decapsulate(pkt)
270 # TODO: Scapy bug?
271 # self.assert_eq_pkts(payload, frame)
272
Klement Sekeraf62ae122016-10-11 11:47:09 +0200273 # Method to define VPP actions before tear down of the test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200274 # Overrides tearDown method in VppTestCase class.
275 # @param self The object pointer.
276 def tearDown(self):
277 super(TestVxlan, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700278
279 def show_commands_at_teardown(self):
280 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
281 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
282 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
283 self.logger.info(self.vapi.cli("show vxlan tunnel"))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200284
Matej Klottondeb69842016-12-09 15:05:46 +0100285
Damjan Marionf56b77a2016-10-03 19:44:57 +0200286if __name__ == '__main__':
287 unittest.main(testRunner=VppTestRunner)