4 from util import ip4n_range, ip4_range
6 from framework import VppTestCase, VppTestRunner
7 from template_bd import BridgeDomain
9 from scapy.layers.l2 import Ether, Raw
10 from scapy.layers.inet import IP, UDP
11 from scapy.layers.vxlan import VXLAN
12 from scapy.utils import atol
17 def reassemble(listoffragments):
18 buffer = StringIO.StringIO()
19 first = listoffragments[0]
21 for pkt in listoffragments:
22 buffer.seek(pkt[IP].frag*8)
23 buffer.write(pkt[IP].payload)
24 first.len = len(buffer.getvalue()) + 20
27 header = str(first[IP])[:20]
28 return first[IP].__class__(header + buffer.getvalue())
31 class TestVxlan(BridgeDomain, VppTestCase):
32 """ VXLAN Test Case """
34 def __init__(self, *args):
35 BridgeDomain.__init__(self)
36 VppTestCase.__init__(self, *args)
38 def encapsulate(self, pkt, vni):
40 Encapsulate the original payload frame by adding VXLAN header with its
41 UDP, IP and Ethernet fields
43 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
44 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
45 UDP(sport=self.dport, dport=self.dport, chksum=0) /
46 VXLAN(vni=vni, flags=self.flags) /
49 def ip_range(self, start, end):
50 """ range of remote ip's """
51 return ip4_range(self.pg0.remote_ip4, start, end)
53 def encap_mcast(self, pkt, src_ip, src_mac, vni):
55 Encapsulate the original payload frame by adding VXLAN header with its
56 UDP, IP and Ethernet fields
58 return (Ether(src=src_mac, dst=self.mcast_mac) /
59 IP(src=src_ip, dst=self.mcast_ip4) /
60 UDP(sport=self.dport, dport=self.dport, chksum=0) /
61 VXLAN(vni=vni, flags=self.flags) /
64 def decapsulate(self, pkt):
66 Decapsulate the original payload frame by removing VXLAN header
68 # check if is set I flag
69 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
70 return pkt[VXLAN].payload
72 # Method for checking VXLAN encapsulation.
74 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
75 # TODO: add error messages
76 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
78 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
81 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
83 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
84 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
85 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
88 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
90 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
91 # Verify UDP destination port is VXLAN 4789, source UDP port could be
93 self.assertEqual(pkt[UDP].dport, type(self).dport)
94 # TODO: checksum check
96 self.assertEqual(pkt[VXLAN].vni, vni)
99 def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
100 # Create 10 ucast vxlan tunnels under bd
102 ip_range_end = ip_range_start + n_ucast_tunnels
103 next_hop_address = cls.pg0.remote_ip4n
104 for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
106 # add host route so dest_ip4n will not be resolved
107 cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
108 r = cls.vapi.vxlan_add_del_tunnel(
109 src_addr=cls.pg0.local_ip4n,
112 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
115 def add_del_shared_mcast_dst_load(cls, is_add):
117 add or del tunnels sharing the same mcast dst
118 to test vxlan ref_count mechanism
120 n_shared_dst_tunnels = 20
122 vni_end = vni_start + n_shared_dst_tunnels
123 for vni in range(vni_start, vni_end):
124 r = cls.vapi.vxlan_add_del_tunnel(
125 src_addr=cls.pg0.local_ip4n,
126 dst_addr=cls.mcast_ip4n,
130 if r.sw_if_index == 0xffffffff:
131 raise "bad sw_if_index"
134 def add_shared_mcast_dst_load(cls):
135 cls.add_del_shared_mcast_dst_load(is_add=1)
138 def del_shared_mcast_dst_load(cls):
139 cls.add_del_shared_mcast_dst_load(is_add=0)
142 def add_del_mcast_tunnels_load(cls, is_add):
144 add or del tunnels to test vxlan stability
146 n_distinct_dst_tunnels = 200
148 ip_range_end = ip_range_start + n_distinct_dst_tunnels
149 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
151 vni = bytearray(dest_ip4n)[3]
152 cls.vapi.vxlan_add_del_tunnel(
153 src_addr=cls.pg0.local_ip4n,
160 def add_mcast_tunnels_load(cls):
161 cls.add_del_mcast_tunnels_load(is_add=1)
164 def del_mcast_tunnels_load(cls):
165 cls.add_del_mcast_tunnels_load(is_add=0)
167 # Class method to start the VXLAN test case.
168 # Overrides setUpClass method in VppTestCase class.
169 # Python try..except statement is used to ensure that the tear down of
170 # the class will be executed even if exception is raised.
171 # @param cls The class pointer.
174 super(TestVxlan, cls).setUpClass()
180 # Create 2 pg interfaces.
181 cls.create_pg_interfaces(range(4))
182 for pg in cls.pg_interfaces:
185 # Configure IPv4 addresses on VPP pg0.
188 # Resolve MAC address for VPP's IP address on pg0.
189 cls.pg0.resolve_arp()
191 # Our Multicast address
192 cls.mcast_ip4 = '239.1.1.1'
193 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
194 iplong = atol(cls.mcast_ip4)
195 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
196 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
198 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
200 cls.single_tunnel_bd = 1
201 r = cls.vapi.vxlan_add_del_tunnel(
202 src_addr=cls.pg0.local_ip4n,
203 dst_addr=cls.pg0.remote_ip4n,
204 vni=cls.single_tunnel_bd)
205 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
206 bd_id=cls.single_tunnel_bd)
207 cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
208 bd_id=cls.single_tunnel_bd)
210 # Setup vni 2 to test multicast flooding
211 cls.n_ucast_tunnels = 10
212 cls.mcast_flood_bd = 2
213 cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
215 r = cls.vapi.vxlan_add_del_tunnel(
216 src_addr=cls.pg0.local_ip4n,
217 dst_addr=cls.mcast_ip4n,
219 vni=cls.mcast_flood_bd)
220 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
221 bd_id=cls.mcast_flood_bd)
222 cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
223 bd_id=cls.mcast_flood_bd)
225 # Add and delete mcast tunnels to check stability
226 cls.add_shared_mcast_dst_load()
227 cls.add_mcast_tunnels_load()
228 cls.del_shared_mcast_dst_load()
229 cls.del_mcast_tunnels_load()
231 # Setup vni 3 to test unicast flooding
232 cls.ucast_flood_bd = 3
233 cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
235 cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
236 bd_id=cls.ucast_flood_bd)
238 super(TestVxlan, cls).tearDownClass()
241 def test_encap_big_packet(self):
242 """ Encapsulation test send big frame from pg1
243 Verify receipt of encapsulated frames on pg0
246 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
248 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
249 IP(src='4.3.2.1', dst='1.2.3.4') /
250 UDP(sport=20000, dport=10000) /
253 self.pg1.add_stream([frame])
255 self.pg0.enable_capture()
259 # Pick first received frame and check if it's correctly encapsulated.
260 out = self.pg0.get_capture(2)
262 pkt = reassemble(out)
264 self.check_encapsulation(pkt, self.single_tunnel_bd)
266 payload = self.decapsulate(pkt)
268 # self.assert_eq_pkts(payload, frame)
270 # Method to define VPP actions before tear down of the test case.
271 # Overrides tearDown method in VppTestCase class.
272 # @param self The object pointer.
274 super(TestVxlan, self).tearDown()
275 if not self.vpp_dead:
276 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
277 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
278 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
279 self.logger.info(self.vapi.cli("show vxlan tunnel"))
282 if __name__ == '__main__':
283 unittest.main(testRunner=VppTestRunner)