4 from util import ip4n_range, 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.packet import Raw
11 from scapy.layers.inet import IP, UDP
12 from scapy.layers.vxlan import VXLAN
13 from scapy.utils import atol
14 from vpp_ip_route import VppIpRoute, VppRoutePath
15 from vpp_vxlan_tunnel import VppVxlanTunnel
16 from vpp_ip import INVALID_INDEX
19 class TestVxlan(BridgeDomain, VppTestCase):
20 """ VXLAN Test Case """
22 def __init__(self, *args):
23 BridgeDomain.__init__(self)
24 VppTestCase.__init__(self, *args)
26 def encapsulate(self, pkt, vni):
28 Encapsulate the original payload frame by adding VXLAN header with its
29 UDP, IP and Ethernet fields
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) /
34 VXLAN(vni=vni, flags=self.flags) /
37 def ip_range(self, start, end):
38 """ range of remote ip's """
39 return ip4_range(self.pg0.remote_ip4, start, end)
41 def encap_mcast(self, pkt, src_ip, src_mac, vni):
43 Encapsulate the original payload frame by adding VXLAN header with its
44 UDP, IP and Ethernet fields
46 return (Ether(src=src_mac, dst=self.mcast_mac) /
47 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) /
52 def decapsulate(self, pkt):
54 Decapsulate the original payload frame by removing VXLAN header
56 # check if is set I flag
57 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
58 return pkt[VXLAN].payload
60 # Method for checking VXLAN encapsulation.
62 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
63 # TODO: add error messages
64 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
66 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
69 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
71 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
72 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
73 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
76 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
78 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
79 # Verify UDP destination port is VXLAN 4789, source UDP port could be
81 self.assertEqual(pkt[UDP].dport, type(self).dport)
82 # TODO: checksum check
84 self.assertEqual(pkt[VXLAN].vni, vni)
87 def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
88 # Create 10 ucast vxlan tunnels under bd
90 ip_range_end = ip_range_start + n_ucast_tunnels
91 next_hop_address = cls.pg0.remote_ip4
92 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
94 # add host route so dest_ip4n will not be resolved
95 rip = VppIpRoute(cls, dest_ip4, 32,
96 [VppRoutePath(next_hop_address,
100 dest_ip4n = socket.inet_pton(socket.AF_INET, dest_ip4)
102 r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
103 dst=dest_ip4, vni=vni)
105 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
108 def add_del_shared_mcast_dst_load(cls, is_add):
110 add or del tunnels sharing the same mcast dst
111 to test vxlan ref_count mechanism
113 n_shared_dst_tunnels = 20
115 vni_end = vni_start + n_shared_dst_tunnels
116 for vni in range(vni_start, vni_end):
117 r = VppVxlanTunnel(cls, src=cls.pg0.local_ip4,
118 dst=cls.mcast_ip4, mcast_sw_if_index=1, vni=vni)
121 if r.sw_if_index == 0xffffffff:
122 raise ValueError("bad sw_if_index: ~0")
124 r.remove_vpp_config()
127 def add_shared_mcast_dst_load(cls):
128 cls.add_del_shared_mcast_dst_load(is_add=1)
131 def del_shared_mcast_dst_load(cls):
132 cls.add_del_shared_mcast_dst_load(is_add=0)
135 def add_del_mcast_tunnels_load(cls, is_add):
137 add or del tunnels to test vxlan stability
139 n_distinct_dst_tunnels = 200
141 ip_range_end = ip_range_start + n_distinct_dst_tunnels
142 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
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)
150 r.remove_vpp_config()
153 def add_mcast_tunnels_load(cls):
154 cls.add_del_mcast_tunnels_load(is_add=1)
157 def del_mcast_tunnels_load(cls):
158 cls.add_del_mcast_tunnels_load(is_add=0)
160 # Class method to start the VXLAN test case.
161 # 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.
167 super(TestVxlan, cls).setUpClass()
173 # Create 2 pg interfaces.
174 cls.create_pg_interfaces(range(4))
175 for pg in cls.pg_interfaces:
178 # Configure IPv4 addresses on VPP pg0.
181 # Resolve MAC address for VPP's IP address on pg0.
182 cls.pg0.resolve_arp()
184 # 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)
188 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
189 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
192 super(TestVxlan, cls).tearDownClass()
196 def tearDownClass(cls):
197 super(TestVxlan, cls).tearDownClass()
200 super(TestVxlan, self).setUp()
201 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
203 self.single_tunnel_vni = 0x12345
204 self.single_tunnel_bd = 1
205 r = VppVxlanTunnel(self, src=self.pg0.local_ip4,
206 dst=self.pg0.remote_ip4,
207 vni=self.single_tunnel_vni)
209 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
210 bd_id=self.single_tunnel_bd)
211 self.vapi.sw_interface_set_l2_bridge(
212 rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd)
214 # Setup vni 2 to test multicast flooding
215 self.n_ucast_tunnels = 10
216 self.mcast_flood_bd = 2
217 self.create_vxlan_flood_test_bd(self.mcast_flood_bd,
218 self.n_ucast_tunnels)
219 r = VppVxlanTunnel(self, src=self.pg0.local_ip4, dst=self.mcast_ip4,
220 mcast_sw_if_index=1, vni=self.mcast_flood_bd)
222 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
223 bd_id=self.mcast_flood_bd)
224 self.vapi.sw_interface_set_l2_bridge(
225 rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd)
227 # Add and delete mcast tunnels to check stability
228 self.add_shared_mcast_dst_load()
229 self.add_mcast_tunnels_load()
230 self.del_shared_mcast_dst_load()
231 self.del_mcast_tunnels_load()
233 # Setup vni 3 to test unicast flooding
234 self.ucast_flood_bd = 3
235 self.create_vxlan_flood_test_bd(self.ucast_flood_bd,
236 self.n_ucast_tunnels)
237 self.vapi.sw_interface_set_l2_bridge(
238 rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd)
240 def test_decap(self):
241 """ Decapsulation test
244 super(TestVxlan, self).test_decap()
246 def test_encap_big_packet(self):
247 """ Encapsulation test send big frame from pg1
248 Verify receipt of encapsulated frames on pg0
251 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
253 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
254 IP(src='4.3.2.1', dst='1.2.3.4') /
255 UDP(sport=20000, dport=10000) /
258 self.pg1.add_stream([frame])
260 self.pg0.enable_capture()
264 # Pick first received frame and check if it's correctly encapsulated.
265 out = self.pg0.get_capture(2)
267 pkt = reassemble4(out)
269 self.check_encapsulation(pkt, self.single_tunnel_vni)
271 payload = self.decapsulate(pkt)
273 # self.assert_eq_pkts(payload, frame)
275 # Method to define VPP actions before tear down of the test case.
276 # Overrides tearDown method in VppTestCase class.
277 # @param self The object pointer.
279 super(TestVxlan, self).tearDown()
281 def show_commands_at_teardown(self):
282 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
283 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
284 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
285 self.logger.info(self.vapi.cli("show vxlan tunnel"))
288 if __name__ == '__main__':
289 unittest.main(testRunner=VppTestRunner)