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
14 class TestVxlan6(BridgeDomain, VppTestCase):
15 """ VXLAN over IPv6 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 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
28 UDP(sport=self.dport, dport=self.dport, chksum=0) /
29 VXLAN(vni=vni, flags=self.flags) /
33 def ip_range(cls, s, e):
34 """ range of remote ip's """
35 tmp = cls.pg0.remote_ip6.rsplit(':', 1)[0]
36 return ("%s:%x" % (tmp, i) for i in range(s, e))
38 def encap_mcast(self, pkt, src_ip, src_mac, vni):
40 Encapsulate the original payload frame by adding VXLAN header with its
41 UDP, IP and Ethernet fields
43 return (Ether(src=src_mac, dst=self.mcast_mac) /
44 IPv6(src=src_ip, dst=self.mcast_ip6) /
45 UDP(sport=self.dport, dport=self.dport, chksum=0) /
46 VXLAN(vni=vni, flags=self.flags) /
49 def decapsulate(self, pkt):
51 Decapsulate the original payload frame by removing VXLAN header
53 # check if is set I flag
54 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
55 return pkt[VXLAN].payload
57 # Method for checking VXLAN encapsulation.
59 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
60 # TODO: add error messages
61 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
63 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
66 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
68 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
69 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
70 self.assertEqual(pkt[IPv6].src, self.pg0.local_ip6)
73 self.assertEqual(pkt[IPv6].dst, self.pg0.remote_ip6)
75 self.assertEqual(pkt[IPv6].dst, type(self).mcast_ip6)
76 # Verify UDP destination port is VXLAN 4789, source UDP port could be
78 self.assertEqual(pkt[UDP].dport, type(self).dport)
79 # TODO: checksum check
81 self.assertEqual(pkt[VXLAN].vni, vni)
84 def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
85 # Create 10 ucast vxlan tunnels under bd
87 end = start + n_ucast_tunnels
88 next_hop = cls.pg0.remote_ip6n
89 for dest_ip6 in cls.ip_range(start, end):
90 dest_ip6n = socket.inet_pton(socket.AF_INET6, dest_ip6)
91 # add host route so dest ip will not be resolved
92 cls.vapi.ip_add_del_route(dst_address=dest_ip6n,
93 dst_address_length=128,
94 next_hop_address=next_hop, is_ipv6=1)
95 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip6n,
96 dst_address=dest_ip6n, is_ipv6=1,
98 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
102 def add_mcast_tunnels_load(cls):
103 cls.add_del_mcast_tunnels_load(is_add=1)
106 def del_mcast_tunnels_load(cls):
107 cls.add_del_mcast_tunnels_load(is_add=0)
109 # Class method to start the VXLAN test case.
110 # Overrides setUpClass method in VppTestCase class.
111 # Python try..except statement is used to ensure that the tear down of
112 # the class will be executed even if exception is raised.
113 # @param cls The class pointer.
116 super(TestVxlan6, cls).setUpClass()
122 # Create 2 pg interfaces.
123 cls.create_pg_interfaces(range(4))
124 for pg in cls.pg_interfaces:
127 # Configure IPv4 addresses on VPP pg0.
130 # Resolve MAC address for VPP's IP address on pg0.
131 cls.pg0.resolve_ndp()
133 cls.mcast_ip6 = 'ff0e::1'
134 cls.mcast_ip6n = socket.inet_pton(socket.AF_INET6, cls.mcast_ip6)
135 cls.mcast_mac = "33:33:00:00:00:%02x" % (1)
137 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
139 cls.single_tunnel_bd = 1
140 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip6n,
141 dst_address=cls.pg0.remote_ip6n,
143 vni=cls.single_tunnel_bd)
144 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
145 bd_id=cls.single_tunnel_bd)
146 cls.vapi.sw_interface_set_l2_bridge(
147 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
149 # Setup vni 2 to test multicast flooding
150 cls.n_ucast_tunnels = 10
151 cls.mcast_flood_bd = 2
152 cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
154 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip6n,
155 dst_address=cls.mcast_ip6n,
156 mcast_sw_if_index=1, is_ipv6=1,
157 vni=cls.mcast_flood_bd)
158 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
159 bd_id=cls.mcast_flood_bd)
160 cls.vapi.sw_interface_set_l2_bridge(
161 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
163 # Setup vni 3 to test unicast flooding
164 cls.ucast_flood_bd = 3
165 cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
167 cls.vapi.sw_interface_set_l2_bridge(
168 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
170 super(TestVxlan6, cls).tearDownClass()
174 def tearDownClass(cls):
175 super(TestVxlan6, cls).tearDownClass()
177 # Method to define VPP actions before tear down of the test case.
178 # Overrides tearDown method in VppTestCase class.
179 # @param self The object pointer.
181 super(TestVxlan6, self).tearDown()
182 if not self.vpp_dead:
183 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
184 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
185 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
186 self.logger.info(self.vapi.cli("show vxlan tunnel"))
189 if __name__ == '__main__':
190 unittest.main(testRunner=VppTestRunner)