5 from framework import VppTestCase, VppTestRunner
6 from template_bd import BridgeDomain
8 from scapy.layers.l2 import Ether
9 from scapy.layers.inet import IP, UDP
10 from scapy.layers.vxlan import VXLAN
11 from scapy.utils import atol
14 class TestVxlan(BridgeDomain, VppTestCase):
15 """ VXLAN Test Case """
17 def __init__(self, *args):
18 BridgeDomain.__init__(self)
19 VppTestCase.__init__(self, *args)
21 def encapsulate(self, pkt, vni):
23 Encapsulate the original payload frame by adding VXLAN header with its
24 UDP, IP and Ethernet fields
26 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
27 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
28 UDP(sport=self.dport, dport=self.dport, chksum=0) /
29 VXLAN(vni=vni, flags=self.flags) /
32 def encap_mcast(self, pkt, src_ip, src_mac, vni):
34 Encapsulate the original payload frame by adding VXLAN header with its
35 UDP, IP and Ethernet fields
37 return (Ether(src=src_mac, dst=self.mcast_mac4) /
38 IP(src=src_ip, dst=self.mcast_ip4) /
39 UDP(sport=self.dport, dport=self.dport, chksum=0) /
40 VXLAN(vni=vni, flags=self.flags) /
43 def decapsulate(self, pkt):
45 Decapsulate the original payload frame by removing VXLAN header
47 # check if is set I flag
48 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
49 return pkt[VXLAN].payload
51 # Method for checking VXLAN encapsulation.
53 def check_encapsulation(self, pkt, vni, local_only=False):
54 # TODO: add error messages
55 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
57 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
59 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
60 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
61 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
63 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
64 # Verify UDP destination port is VXLAN 4789, source UDP port could be
66 self.assertEqual(pkt[UDP].dport, type(self).dport)
67 # TODO: checksum check
69 self.assertEqual(pkt[VXLAN].vni, vni)
72 def ip4_range(ip4n, s=10, e=20):
73 base = str(bytearray(ip4n)[:3])
74 return ((base + ip) for ip in str(bytearray(range(s, e))))
77 def create_vxlan_flood_test_bd(cls, vni):
78 # Create 10 ucast vxlan tunnels under bd
81 next_hop_address = cls.pg0.remote_ip4n
82 for dest_addr in cls.ip4_range(next_hop_address, ip_range_start,
84 # add host route so dest_addr will not be resolved
85 cls.vapi.ip_add_del_route(dest_addr, 32, next_hop_address)
86 r = cls.vapi.vxlan_add_del_tunnel(
87 src_addr=cls.pg0.local_ip4n,
90 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
93 def add_del_mcast_load(cls, is_add):
96 for dest_addr in cls.ip4_range(cls.mcast_ip4n, ip_range_start,
98 vni = bytearray(dest_addr)[3]
99 cls.vapi.vxlan_add_del_tunnel(
100 src_addr=cls.pg0.local_ip4n,
107 def add_mcast_load(cls):
108 cls.add_del_mcast_load(is_add=1)
111 def del_mcast_load(cls):
112 cls.add_del_mcast_load(is_add=0)
114 # Class method to start the VXLAN test case.
115 # Overrides setUpClass method in VppTestCase class.
116 # Python try..except statement is used to ensure that the tear down of
117 # the class will be executed even if exception is raised.
118 # @param cls The class pointer.
121 super(TestVxlan, cls).setUpClass()
127 # Create 2 pg interfaces.
128 cls.create_pg_interfaces(range(4))
129 for pg in cls.pg_interfaces:
132 # Configure IPv4 addresses on VPP pg0.
135 # Resolve MAC address for VPP's IP address on pg0.
136 cls.pg0.resolve_arp()
138 # Our Multicast address
139 cls.mcast_ip4 = '239.1.1.1'
140 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
141 iplong = atol(cls.mcast_ip4)
142 cls.mcast_mac4 = "01:00:5e:%02x:%02x:%02x" % (
143 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
145 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
147 cls.single_tunnel_bd = 1
148 r = cls.vapi.vxlan_add_del_tunnel(
149 src_addr=cls.pg0.local_ip4n,
150 dst_addr=cls.pg0.remote_ip4n,
151 vni=cls.single_tunnel_bd)
152 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
153 bd_id=cls.single_tunnel_bd)
154 cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
155 bd_id=cls.single_tunnel_bd)
157 # Setup vni 2 to test multicast flooding
158 cls.mcast_flood_bd = 2
159 cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd)
160 r = cls.vapi.vxlan_add_del_tunnel(
161 src_addr=cls.pg0.local_ip4n,
162 dst_addr=cls.mcast_ip4n,
164 vni=cls.mcast_flood_bd)
165 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
166 bd_id=cls.mcast_flood_bd)
167 cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
168 bd_id=cls.mcast_flood_bd)
170 # Add and delete mcast tunnels to check stability
174 # Setup vni 3 to test unicast flooding
175 cls.ucast_flood_bd = 3
176 cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd)
177 cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
178 bd_id=cls.ucast_flood_bd)
180 super(TestVxlan, cls).tearDownClass()
183 # Method to define VPP actions before tear down of the test case.
184 # Overrides tearDown method in VppTestCase class.
185 # @param self The object pointer.
187 super(TestVxlan, self).tearDown()
188 if not self.vpp_dead:
189 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
190 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
191 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
192 self.logger.info(self.vapi.cli("show vxlan tunnel"))
195 if __name__ == '__main__':
196 unittest.main(testRunner=VppTestRunner)