5 from framework import VppTestCase, VppTestRunner
6 from template_bd import BridgeDomain
8 from scapy.layers.l2 import Ether
9 from scapy.layers.inet6 import IPv6, UDP
10 from scapy.layers.vxlan import VXLAN
11 from scapy.utils import atol
12 from vpp_ip_route import VppIpRoute, VppRoutePath
13 from vpp_vxlan_tunnel import VppVxlanTunnel
14 from vpp_ip import INVALID_INDEX
17 class TestVxlan6(BridgeDomain, VppTestCase):
18 """ VXLAN over IPv6 Test Case """
20 def __init__(self, *args):
21 BridgeDomain.__init__(self)
22 VppTestCase.__init__(self, *args)
24 def encapsulate(self, pkt, vni):
26 Encapsulate the original payload frame by adding VXLAN header with its
27 UDP, IP and Ethernet fields
29 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
30 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
31 UDP(sport=self.dport, dport=self.dport, chksum=0) /
32 VXLAN(vni=vni, flags=self.flags) /
36 def ip_range(cls, s, e):
37 """ range of remote ip's """
38 tmp = cls.pg0.remote_ip6.rsplit(':', 1)[0]
39 return ("%s:%x" % (tmp, i) for i in range(s, e))
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 IPv6(src=src_ip, dst=self.mcast_ip6) /
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[IPv6].src, self.pg0.local_ip6)
76 self.assertEqual(pkt[IPv6].dst, self.pg0.remote_ip6)
78 self.assertEqual(pkt[IPv6].dst, type(self).mcast_ip6)
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 end = start + n_ucast_tunnels
91 for dest_ip6 in cls.ip_range(start, end):
92 # add host route so dest ip will not be resolved
93 rip = VppIpRoute(cls, dest_ip6, 128,
94 [VppRoutePath(cls.pg0.remote_ip6, INVALID_INDEX)],
97 r = VppVxlanTunnel(cls, src=cls.pg0.local_ip6,
98 dst=dest_ip6, vni=vni)
100 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
103 def add_mcast_tunnels_load(cls):
104 cls.add_del_mcast_tunnels_load(is_add=1)
107 def del_mcast_tunnels_load(cls):
108 cls.add_del_mcast_tunnels_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(TestVxlan6, 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_ndp()
134 cls.mcast_ip6 = 'ff0e::1'
135 cls.mcast_ip6n = socket.inet_pton(socket.AF_INET6, cls.mcast_ip6)
136 cls.mcast_mac = "33:33:00:00:00:%02x" % (1)
139 super(TestVxlan6, cls).tearDownClass()
143 def tearDownClass(cls):
144 super(TestVxlan6, cls).tearDownClass()
147 super(TestVxlan6, self).setUp()
148 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
150 self.single_tunnel_vni = 0x12345
151 self.single_tunnel_bd = 1
152 r = VppVxlanTunnel(self, src=self.pg0.local_ip6,
153 dst=self.pg0.remote_ip6,
154 vni=self.single_tunnel_vni)
156 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
157 bd_id=self.single_tunnel_bd)
158 self.vapi.sw_interface_set_l2_bridge(
159 rx_sw_if_index=self.pg1.sw_if_index, bd_id=self.single_tunnel_bd)
161 # Setup vni 2 to test multicast flooding
162 self.n_ucast_tunnels = 10
163 self.mcast_flood_bd = 2
164 self.create_vxlan_flood_test_bd(self.mcast_flood_bd,
165 self.n_ucast_tunnels)
166 r = VppVxlanTunnel(self, src=self.pg0.local_ip6, dst=self.mcast_ip6,
167 mcast_sw_if_index=1, vni=self.mcast_flood_bd)
169 self.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
170 bd_id=self.mcast_flood_bd)
171 self.vapi.sw_interface_set_l2_bridge(
172 rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.mcast_flood_bd)
174 # Setup vni 3 to test unicast flooding
175 self.ucast_flood_bd = 3
176 self.create_vxlan_flood_test_bd(self.ucast_flood_bd,
177 self.n_ucast_tunnels)
178 self.vapi.sw_interface_set_l2_bridge(
179 rx_sw_if_index=self.pg3.sw_if_index, bd_id=self.ucast_flood_bd)
181 # Method to define VPP actions before tear down of the test case.
182 # Overrides tearDown method in VppTestCase class.
183 # @param self The object pointer.
185 super(TestVxlan6, self).tearDown()
187 def show_commands_at_teardown(self):
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)