4 from util import ip4_range, reassemble4
6 from framework import VppTestCase, VppTestRunner
7 from template_bd import BridgeDomain
9 from scapy.layers.l2 import Ether
10 from scapy.layers.l2 import ARP
11 from scapy.packet import Raw, bind_layers
12 from scapy.layers.inet import IP, UDP
13 from scapy.layers.vxlan import VXLAN
16 from vpp_ip_route import VppIpRoute, VppRoutePath
17 from vpp_vxlan_tunnel import VppVxlanTunnel
18 from vpp_ip import INVALID_INDEX
19 from vpp_neighbor import VppNeighbor
22 class TestVxlan(BridgeDomain, VppTestCase):
23 """ VXLAN Test Case """
25 def __init__(self, *args):
26 BridgeDomain.__init__(self)
27 VppTestCase.__init__(self, *args)
29 def encapsulate(self, pkt, vni):
31 Encapsulate the original payload frame by adding VXLAN header with its
32 UDP, IP and Ethernet fields
34 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
35 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
36 UDP(sport=self.dport, dport=self.dport, chksum=0) /
37 VXLAN(vni=vni, flags=self.flags) /
40 def ip_range(self, start, end):
41 """ range of remote ip's """
42 return ip4_range(self.pg0.remote_ip4, start, end)
44 def encap_mcast(self, pkt, src_ip, src_mac, vni):
46 Encapsulate the original payload frame by adding VXLAN header with its
47 UDP, IP and Ethernet fields
49 return (Ether(src=src_mac, dst=self.mcast_mac) /
50 IP(src=src_ip, dst=self.mcast_ip4) /
51 UDP(sport=self.dport, dport=self.dport, chksum=0) /
52 VXLAN(vni=vni, flags=self.flags) /
55 def decapsulate(self, pkt):
57 Decapsulate the original payload frame by removing VXLAN header
59 # check if is set I flag
60 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
61 return pkt[VXLAN].payload
63 # Method for checking VXLAN encapsulation.
65 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
66 # TODO: add error messages
67 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
69 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
72 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
74 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
75 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
76 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
79 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
81 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
82 # Verify UDP destination port is VXLAN 4789, source UDP port could be
84 self.assertEqual(pkt[UDP].dport, self.dport)
86 self.assert_udp_checksum_valid(pkt)
88 self.assertEqual(pkt[VXLAN].vni, vni)
91 def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels, port):
92 # Create 10 ucast vxlan tunnels under bd
94 ip_range_end = ip_range_start + n_ucast_tunnels
95 next_hop_address = cls.pg0.remote_ip4
96 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
98 # add host route so dest_ip4 will not be resolved
99 rip = VppIpRoute(cls, dest_ip4, 32,
100 [VppRoutePath(next_hop_address,
105 r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
106 src_port=port, dst_port=port,
107 dst=dest_ip4, vni=vni)
109 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
112 def add_del_shared_mcast_dst_load(cls, port, is_add):
114 add or del tunnels sharing the same mcast dst
115 to test vxlan ref_count mechanism
117 n_shared_dst_tunnels = 20
119 vni_end = vni_start + n_shared_dst_tunnels
120 for vni in range(vni_start, vni_end):
121 r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
122 src_port=port, dst_port=port,
123 dst=cls.mcast_ip4, mcast_sw_if_index=1, vni=vni)
126 if r.sw_if_index == 0xffffffff:
127 raise ValueError("bad sw_if_index: ~0")
129 r.remove_vpp_config()
132 def add_shared_mcast_dst_load(cls, port):
133 cls.add_del_shared_mcast_dst_load(port=port, is_add=1)
136 def del_shared_mcast_dst_load(cls, port):
137 cls.add_del_shared_mcast_dst_load(port=port, is_add=0)
140 def add_del_mcast_tunnels_load(cls, port, is_add):
142 add or del tunnels to test vxlan stability
144 n_distinct_dst_tunnels = 200
146 ip_range_end = ip_range_start + n_distinct_dst_tunnels
147 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
149 vni = bytearray(socket.inet_pton(socket.AF_INET, dest_ip4))[3]
150 r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
151 src_port=port, dst_port=port,
152 dst=dest_ip4, mcast_sw_if_index=1, vni=vni)
156 r.remove_vpp_config()
159 def add_mcast_tunnels_load(cls, port):
160 cls.add_del_mcast_tunnels_load(port=port, is_add=1)
163 def del_mcast_tunnels_load(cls, port):
164 cls.add_del_mcast_tunnels_load(port=port, is_add=0)
166 # Class method to start the VXLAN test case.
167 # Overrides setUpClass method in VppTestCase class.
168 # Python try..except statement is used to ensure that the tear down of
169 # the class will be executed even if exception is raised.
170 # @param cls The class pointer.
173 super(TestVxlan, cls).setUpClass()
178 # Create 2 pg interfaces.
179 cls.create_pg_interfaces(range(4))
180 for pg in cls.pg_interfaces:
183 # Configure IPv4 addresses on VPP pg0.
186 # Resolve MAC address for VPP's IP address on pg0.
187 cls.pg0.resolve_arp()
189 # Our Multicast address
190 cls.mcast_ip4 = '239.1.1.1'
191 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
197 def tearDownClass(cls):
198 super(TestVxlan, cls).tearDownClass()
201 super(TestVxlan, self).setUp()
203 def createVxLANInterfaces(self, port=4789):
204 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
208 self.single_tunnel_vni = 0x12345
209 self.single_tunnel_bd = 1
210 r = VppVxlanTunnel(self, src=self.pg0.local_ip4,
211 dst=self.pg0.remote_ip4,
212 src_port=self.dport, dst_port=self.dport,
213 vni=self.single_tunnel_vni)
215 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
216 bd_id=self.single_tunnel_bd)
217 self.vapi.sw_interface_set_l2_bridge(
218 rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd)
220 # Setup vni 2 to test multicast flooding
221 self.n_ucast_tunnels = 10
222 self.mcast_flood_bd = 2
223 self.create_vxlan_flood_test_bd(self.mcast_flood_bd,
224 self.n_ucast_tunnels,
226 r = VppVxlanTunnel(self, src=self.pg0.local_ip4, dst=self.mcast_ip4,
227 src_port=self.dport, dst_port=self.dport,
228 mcast_sw_if_index=1, vni=self.mcast_flood_bd)
230 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
231 bd_id=self.mcast_flood_bd)
232 self.vapi.sw_interface_set_l2_bridge(
233 rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd)
235 # Add and delete mcast tunnels to check stability
236 self.add_shared_mcast_dst_load(self.dport)
237 self.add_mcast_tunnels_load(self.dport)
238 self.del_shared_mcast_dst_load(self.dport)
239 self.del_mcast_tunnels_load(self.dport)
241 # Setup vni 3 to test unicast flooding
242 self.ucast_flood_bd = 3
243 self.create_vxlan_flood_test_bd(self.ucast_flood_bd,
244 self.n_ucast_tunnels,
246 self.vapi.sw_interface_set_l2_bridge(
247 rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd)
249 # Set scapy listen custom port for VxLAN
250 bind_layers(UDP, VXLAN, dport=self.dport)
252 def encap_big_packet(self):
253 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
255 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
256 IP(src='4.3.2.1', dst='1.2.3.4') /
257 UDP(sport=20000, dport=10000) /
260 self.pg1.add_stream([frame])
262 self.pg0.enable_capture()
266 # Pick first received frame and check if it's correctly encapsulated.
267 out = self.pg0.get_capture(2)
269 pkt = reassemble4(out)
271 self.check_encapsulation(pkt, self.single_tunnel_vni)
273 payload = self.decapsulate(pkt)
275 # self.assert_eq_pkts(payload, frame)
278 Tests with default port (4789)
280 def test_decap(self):
281 """ Decapsulation test
284 self.createVxLANInterfaces()
285 super(TestVxlan, self).test_decap()
287 def test_encap(self):
288 """ Encapsulation test
291 self.createVxLANInterfaces()
292 super(TestVxlan, self).test_encap()
294 def test_encap_big_packet(self):
295 """ Encapsulation test send big frame from pg1
296 Verify receipt of encapsulated frames on pg0
298 self.createVxLANInterfaces()
299 self.encap_big_packet()
301 def test_ucast_flood(self):
302 """ Unicast flood test
305 self.createVxLANInterfaces()
306 super(TestVxlan, self).test_ucast_flood()
308 def test_mcast_flood(self):
309 """ Multicast flood test
312 self.createVxLANInterfaces()
313 super(TestVxlan, self).test_mcast_flood()
315 def test_mcast_rcv(self):
316 """ Multicast receive test
319 self.createVxLANInterfaces()
320 super(TestVxlan, self).test_mcast_rcv()
323 Tests with custom port
325 def test_decap_custom_port(self):
326 """ Decapsulation test custom port
329 self.createVxLANInterfaces(1111)
330 super(TestVxlan, self).test_decap()
332 def test_encap_custom_port(self):
333 """ Encapsulation test custom port
336 self.createVxLANInterfaces(1111)
337 super(TestVxlan, self).test_encap()
339 def test_ucast_flood_custom_port(self):
340 """ Unicast flood test custom port
343 self.createVxLANInterfaces(1111)
344 super(TestVxlan, self).test_ucast_flood()
346 def test_mcast_flood_custom_port(self):
347 """ Multicast flood test custom port
350 self.createVxLANInterfaces(1111)
351 super(TestVxlan, self).test_mcast_flood()
353 def test_mcast_rcv_custom_port(self):
354 """ Multicast receive test custom port
357 self.createVxLANInterfaces(1111)
358 super(TestVxlan, self).test_mcast_rcv()
360 # Method to define VPP actions before tear down of the test case.
361 # Overrides tearDown method in VppTestCase class.
362 # @param self The object pointer.
365 super(TestVxlan, self).tearDown()
367 def show_commands_at_teardown(self):
368 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
369 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
370 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
371 self.logger.info(self.vapi.cli("show vxlan tunnel"))
374 class TestVxlan2(VppTestCase):
375 """ VXLAN Test Case """
377 super(TestVxlan2, self).setUp()
379 # Create 2 pg interfaces.
380 self.create_pg_interfaces(range(4))
381 for pg in self.pg_interfaces:
384 # Configure IPv4 addresses on VPP pg0.
385 self.pg0.config_ip4()
386 self.pg0.resolve_arp()
389 super(TestVxlan2, self).tearDown()
391 def test_xconnect(self):
392 """ VXLAN source address not local """
395 # test the broken configuration of a VXLAN tunnel whose
396 # source address is not local ot the box. packets sent
397 # through the tunnel should be dropped
399 t = VppVxlanTunnel(self,
401 dst=self.pg0.local_ip4,
406 self.vapi.sw_interface_set_l2_xconnect(t.sw_if_index,
407 self.pg1.sw_if_index,
409 self.vapi.sw_interface_set_l2_xconnect(self.pg1.sw_if_index,
413 p = (Ether(src="00:11:22:33:44:55",
414 dst="00:00:00:11:22:33") /
415 IP(src="4.3.2.1", dst="1.2.3.4") /
416 UDP(sport=20000, dport=10000) /
419 rx = self.send_and_assert_no_replies(self.pg1, [p])
422 class TestVxlanL2Mode(VppTestCase):
423 """ VXLAN Test Case """
425 super(TestVxlanL2Mode, self).setUp()
427 # Create 2 pg interfaces.
428 self.create_pg_interfaces(range(2))
429 for pg in self.pg_interfaces:
432 # Configure IPv4 addresses on VPP pg0.
433 self.pg0.config_ip4()
434 self.pg0.resolve_arp()
436 # Configure IPv4 addresses on VPP pg1.
437 self.pg1.config_ip4()
440 super(TestVxlanL2Mode, self).tearDown()
442 def test_l2_mode(self):
443 """ VXLAN L2 mode """
444 t = VppVxlanTunnel(self,
445 src=self.pg0.local_ip4,
446 dst=self.pg0.remote_ip4,
447 vni=1000, is_l3=False)
452 dstIP = t.local_ip4[:-1] + "2"
454 # Create a packet to send
455 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
456 IP(src=self.pg1.local_ip4, dst=dstIP) /
457 UDP(sport=555, dport=556) /
461 rx = self.send_and_expect(self.pg1, [p], self.pg0)
463 self.assertEqual(p[Ether].dst, self.pg0.remote_mac)
464 self.assertEqual(p[Ether].src, self.pg0.local_mac)
465 self.assertEqual(p[ARP].op, 1)
466 self.assertEqual(p[ARP].pdst, dstIP)
469 VppNeighbor(self, t.sw_if_index,
471 dstIP).add_vpp_config()
475 rx = self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg0)
476 self.assertEqual(NUM_PKTS, len(rx))
479 if __name__ == '__main__':
480 unittest.main(testRunner=VppTestRunner)