4 from util import ip4n_range
6 from framework import VppTestCase, VppTestRunner
7 from template_bd import BridgeDomain
9 from scapy.layers.l2 import Ether
10 from scapy.layers.inet import IP, UDP
11 from scapy.layers.vxlan import VXLAN
12 from scapy.utils import atol
15 class TestVxlan(BridgeDomain, VppTestCase):
16 """ VXLAN Test Case """
18 def __init__(self, *args):
19 BridgeDomain.__init__(self)
20 VppTestCase.__init__(self, *args)
22 def encapsulate(self, pkt, vni):
24 Encapsulate the original payload frame by adding VXLAN header with its
25 UDP, IP and Ethernet fields
27 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
28 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
29 UDP(sport=self.dport, dport=self.dport, chksum=0) /
30 VXLAN(vni=vni, flags=self.flags) /
33 def encap_mcast(self, pkt, src_ip, src_mac, vni):
35 Encapsulate the original payload frame by adding VXLAN header with its
36 UDP, IP and Ethernet fields
38 return (Ether(src=src_mac, dst=self.mcast_mac) /
39 IP(src=src_ip, dst=self.mcast_ip4) /
40 UDP(sport=self.dport, dport=self.dport, chksum=0) /
41 VXLAN(vni=vni, flags=self.flags) /
44 def decapsulate(self, pkt):
46 Decapsulate the original payload frame by removing VXLAN header
48 # check if is set I flag
49 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
50 return pkt[VXLAN].payload
52 # Method for checking VXLAN encapsulation.
54 def check_encapsulation(self, pkt, vni, local_only=False):
55 # TODO: add error messages
56 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
58 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
60 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
61 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
62 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
64 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
65 # Verify UDP destination port is VXLAN 4789, source UDP port could be
67 self.assertEqual(pkt[UDP].dport, type(self).dport)
68 # TODO: checksum check
70 self.assertEqual(pkt[VXLAN].vni, vni)
73 def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
74 # Create 10 ucast vxlan tunnels under bd
76 ip_range_end = ip_range_start + n_ucast_tunnels
77 next_hop_address = cls.pg0.remote_ip4n
78 for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
80 # add host route so dest_ip4n will not be resolved
81 cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
82 r = cls.vapi.vxlan_add_del_tunnel(
83 src_addr=cls.pg0.local_ip4n,
86 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
89 def add_del_mcast_load(cls, is_add):
92 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
94 vni = bytearray(dest_ip4n)[3]
95 cls.vapi.vxlan_add_del_tunnel(
96 src_addr=cls.pg0.local_ip4n,
103 def add_mcast_load(cls):
104 cls.add_del_mcast_load(is_add=1)
107 def del_mcast_load(cls):
108 cls.add_del_mcast_load(is_add=0)
110 # Class method to start the VXLAN test case.
111 # Overrides setUpClass method in VppTestCase class.
112 # Python try..except statement is used to ensure that the tear down of
113 # the class will be executed even if exception is raised.
114 # @param cls The class pointer.
117 super(TestVxlan, cls).setUpClass()
123 # Create 2 pg interfaces.
124 cls.create_pg_interfaces(range(4))
125 for pg in cls.pg_interfaces:
128 # Configure IPv4 addresses on VPP pg0.
131 # Resolve MAC address for VPP's IP address on pg0.
132 cls.pg0.resolve_arp()
134 # Our Multicast address
135 cls.mcast_ip4 = '239.1.1.1'
136 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
137 iplong = atol(cls.mcast_ip4)
138 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
139 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
141 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
143 cls.single_tunnel_bd = 1
144 r = cls.vapi.vxlan_add_del_tunnel(
145 src_addr=cls.pg0.local_ip4n,
146 dst_addr=cls.pg0.remote_ip4n,
147 vni=cls.single_tunnel_bd)
148 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
149 bd_id=cls.single_tunnel_bd)
150 cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
151 bd_id=cls.single_tunnel_bd)
153 # Setup vni 2 to test multicast flooding
154 cls.n_ucast_tunnels = 10
155 cls.mcast_flood_bd = 2
156 cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
158 r = cls.vapi.vxlan_add_del_tunnel(
159 src_addr=cls.pg0.local_ip4n,
160 dst_addr=cls.mcast_ip4n,
162 vni=cls.mcast_flood_bd)
163 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
164 bd_id=cls.mcast_flood_bd)
165 cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
166 bd_id=cls.mcast_flood_bd)
168 # Add and delete mcast tunnels to check stability
172 # Setup vni 3 to test unicast flooding
173 cls.ucast_flood_bd = 3
174 cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
176 cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
177 bd_id=cls.ucast_flood_bd)
179 super(TestVxlan, cls).tearDownClass()
182 # Method to define VPP actions before tear down of the test case.
183 # Overrides tearDown method in VppTestCase class.
184 # @param self The object pointer.
186 super(TestVxlan, self).tearDown()
187 if not self.vpp_dead:
188 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
189 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
190 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
191 self.logger.info(self.vapi.cli("show vxlan tunnel"))
194 if __name__ == '__main__':
195 unittest.main(testRunner=VppTestRunner)