fib: fib api updates
[vpp.git] / test / test_vxlan6.py
1 #!/usr/bin/env python
2
3 import socket
4 import unittest
5 from framework import VppTestCase, VppTestRunner
6 from template_bd import BridgeDomain
7
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_ip import INVALID_INDEX
14
15
16 class TestVxlan6(BridgeDomain, VppTestCase):
17     """ VXLAN over IPv6 Test Case """
18
19     def __init__(self, *args):
20         BridgeDomain.__init__(self)
21         VppTestCase.__init__(self, *args)
22
23     def encapsulate(self, pkt, vni):
24         """
25         Encapsulate the original payload frame by adding VXLAN header with its
26         UDP, IP and Ethernet fields
27         """
28         return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
29                 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
30                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
31                 VXLAN(vni=vni, flags=self.flags) /
32                 pkt)
33
34     @classmethod
35     def ip_range(cls, s, e):
36         """ range of remote ip's """
37         tmp = cls.pg0.remote_ip6.rsplit(':', 1)[0]
38         return ("%s:%x" % (tmp, i) for i in range(s, e))
39
40     def encap_mcast(self, pkt, src_ip, src_mac, vni):
41         """
42         Encapsulate the original payload frame by adding VXLAN header with its
43         UDP, IP and Ethernet fields
44         """
45         return (Ether(src=src_mac, dst=self.mcast_mac) /
46                 IPv6(src=src_ip, dst=self.mcast_ip6) /
47                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
48                 VXLAN(vni=vni, flags=self.flags) /
49                 pkt)
50
51     def decapsulate(self, pkt):
52         """
53         Decapsulate the original payload frame by removing VXLAN header
54         """
55         # check if is set I flag
56         self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
57         return pkt[VXLAN].payload
58
59     # Method for checking VXLAN encapsulation.
60     #
61     def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
62         # TODO: add error messages
63         # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
64         #  by VPP using ARP.
65         self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
66         if not local_only:
67             if not mcast_pkt:
68                 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
69             else:
70                 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
71         # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
72         self.assertEqual(pkt[IPv6].src, self.pg0.local_ip6)
73         if not local_only:
74             if not mcast_pkt:
75                 self.assertEqual(pkt[IPv6].dst, self.pg0.remote_ip6)
76             else:
77                 self.assertEqual(pkt[IPv6].dst, type(self).mcast_ip6)
78         # Verify UDP destination port is VXLAN 4789, source UDP port could be
79         #  arbitrary.
80         self.assertEqual(pkt[UDP].dport, type(self).dport)
81         # TODO: checksum check
82         # Verify VNI
83         self.assertEqual(pkt[VXLAN].vni, vni)
84
85     @classmethod
86     def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
87         # Create 10 ucast vxlan tunnels under bd
88         start = 10
89         end = start + n_ucast_tunnels
90         for dest_ip6 in cls.ip_range(start, end):
91             dest_ip6n = socket.inet_pton(socket.AF_INET6, dest_ip6)
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)],
95                              register=False)
96             rip.add_vpp_config()
97             r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip6n,
98                                               dst_address=dest_ip6n, is_ipv6=1,
99                                               vni=vni)
100             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
101
102     @classmethod
103     def add_mcast_tunnels_load(cls):
104         cls.add_del_mcast_tunnels_load(is_add=1)
105
106     @classmethod
107     def del_mcast_tunnels_load(cls):
108         cls.add_del_mcast_tunnels_load(is_add=0)
109
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.
115     @classmethod
116     def setUpClass(cls):
117         super(TestVxlan6, cls).setUpClass()
118
119         try:
120             cls.dport = 4789
121             cls.flags = 0x8
122
123             # Create 2 pg interfaces.
124             cls.create_pg_interfaces(range(4))
125             for pg in cls.pg_interfaces:
126                 pg.admin_up()
127
128             # Configure IPv4 addresses on VPP pg0.
129             cls.pg0.config_ip6()
130
131             # Resolve MAC address for VPP's IP address on pg0.
132             cls.pg0.resolve_ndp()
133
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)
137
138             # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
139             #  into BD.
140             cls.single_tunnel_bd = 1
141             r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip6n,
142                                               dst_address=cls.pg0.remote_ip6n,
143                                               is_ipv6=1,
144                                               vni=cls.single_tunnel_bd)
145             cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
146                                                 bd_id=cls.single_tunnel_bd)
147             cls.vapi.sw_interface_set_l2_bridge(
148                 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
149
150             # Setup vni 2 to test multicast flooding
151             cls.n_ucast_tunnels = 10
152             cls.mcast_flood_bd = 2
153             cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
154                                            cls.n_ucast_tunnels)
155             r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip6n,
156                                               dst_address=cls.mcast_ip6n,
157                                               mcast_sw_if_index=1, is_ipv6=1,
158                                               vni=cls.mcast_flood_bd)
159             cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
160                                                 bd_id=cls.mcast_flood_bd)
161             cls.vapi.sw_interface_set_l2_bridge(
162                 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
163
164             # Setup vni 3 to test unicast flooding
165             cls.ucast_flood_bd = 3
166             cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
167                                            cls.n_ucast_tunnels)
168             cls.vapi.sw_interface_set_l2_bridge(
169                 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
170         except Exception:
171             super(TestVxlan6, cls).tearDownClass()
172             raise
173
174     @classmethod
175     def tearDownClass(cls):
176         super(TestVxlan6, cls).tearDownClass()
177
178     # Method to define VPP actions before tear down of the test case.
179     #  Overrides tearDown method in VppTestCase class.
180     #  @param self The object pointer.
181     def tearDown(self):
182         super(TestVxlan6, self).tearDown()
183
184     def show_commands_at_teardown(self):
185         self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
186         self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
187         self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
188         self.logger.info(self.vapi.cli("show vxlan tunnel"))
189
190
191 if __name__ == '__main__':
192     unittest.main(testRunner=VppTestRunner)