4 from framework import VppTestCase, VppTestRunner
5 from template_bd import BridgeDomain
7 from scapy.layers.l2 import Ether
8 from scapy.layers.inet import IP, UDP
9 from scapy.layers.vxlan import VXLAN
12 class TestVxlan(BridgeDomain, VppTestCase):
13 """ VXLAN Test Case """
15 def __init__(self, *args):
16 BridgeDomain.__init__(self)
17 VppTestCase.__init__(self, *args)
19 def encapsulate(self, pkt):
21 Encapsulate the original payload frame by adding VXLAN header with its
22 UDP, IP and Ethernet fields
24 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
25 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
26 UDP(sport=self.dport, dport=self.dport, chksum=0) /
27 VXLAN(vni=self.vni, flags=self.flags) /
30 def decapsulate(self, pkt):
32 Decapsulate the original payload frame by removing VXLAN header
34 # check if is set I flag
35 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
36 return pkt[VXLAN].payload
38 # Method for checking VXLAN encapsulation.
40 def check_encapsulation(self, pkt):
41 # TODO: add error messages
42 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
44 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
45 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
46 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
47 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
48 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
49 # Verify UDP destination port is VXLAN 4789, source UDP port could be
51 self.assertEqual(pkt[UDP].dport, type(self).dport)
52 # TODO: checksum check
53 # Verify VNI, based on configuration it must be 1.
54 self.assertEqual(pkt[VXLAN].vni, type(self).vni)
56 # Class method to start the VXLAN test case.
57 # Overrides setUpClass method in VppTestCase class.
58 # Python try..except statement is used to ensure that the tear down of
59 # the class will be executed even if exception is raised.
60 # @param cls The class pointer.
63 super(TestVxlan, cls).setUpClass()
70 # Create 2 pg interfaces.
71 cls.create_pg_interfaces(range(2))
75 # Configure IPv4 addresses on VPP pg0.
78 # Resolve MAC address for VPP's IP address on pg0.
81 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
83 r = cls.vapi.vxlan_add_del_tunnel(
84 src_addr=cls.pg0.local_ip4n,
85 dst_addr=cls.pg0.remote_ip4n,
87 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=1)
88 cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index, bd_id=1)
90 super(TestVxlan, cls).tearDownClass()
93 # Method to define VPP actions before tear down of the test case.
94 # Overrides tearDown method in VppTestCase class.
95 # @param self The object pointer.
97 super(TestVxlan, self).tearDown()
99 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
102 if __name__ == '__main__':
103 unittest.main(testRunner=VppTestRunner)