VTL Cleanup: Fix missing calls to setUpClass/tearDownClass, fix numerous TypeErrors.
[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
13
14 class TestVxlan6(BridgeDomain, VppTestCase):
15     """ VXLAN over IPv6 Test Case """
16
17     def __init__(self, *args):
18         BridgeDomain.__init__(self)
19         VppTestCase.__init__(self, *args)
20
21     def encapsulate(self, pkt, vni):
22         """
23         Encapsulate the original payload frame by adding VXLAN header with its
24         UDP, IP and Ethernet fields
25         """
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) /
30                 pkt)
31
32     @classmethod
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))
37
38     def encap_mcast(self, pkt, src_ip, src_mac, vni):
39         """
40         Encapsulate the original payload frame by adding VXLAN header with its
41         UDP, IP and Ethernet fields
42         """
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) /
47                 pkt)
48
49     def decapsulate(self, pkt):
50         """
51         Decapsulate the original payload frame by removing VXLAN header
52         """
53         # check if is set I flag
54         self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
55         return pkt[VXLAN].payload
56
57     # Method for checking VXLAN encapsulation.
58     #
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
62         #  by VPP using ARP.
63         self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
64         if not local_only:
65             if not mcast_pkt:
66                 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
67             else:
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)
71         if not local_only:
72             if not mcast_pkt:
73                 self.assertEqual(pkt[IPv6].dst, self.pg0.remote_ip6)
74             else:
75                 self.assertEqual(pkt[IPv6].dst, type(self).mcast_ip6)
76         # Verify UDP destination port is VXLAN 4789, source UDP port could be
77         #  arbitrary.
78         self.assertEqual(pkt[UDP].dport, type(self).dport)
79         # TODO: checksum check
80         # Verify VNI
81         self.assertEqual(pkt[VXLAN].vni, vni)
82
83     @classmethod
84     def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
85         # Create 10 ucast vxlan tunnels under bd
86         start = 10
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(dest_ip6n, 128, next_hop, is_ipv6=1)
93             r = cls.vapi.vxlan_add_del_tunnel(
94                 is_ipv6=1,
95                 src_addr=cls.pg0.local_ip6n,
96                 dst_addr=dest_ip6n,
97                 vni=vni)
98             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
99
100     @classmethod
101     def add_mcast_tunnels_load(cls):
102         cls.add_del_mcast_tunnels_load(is_add=1)
103
104     @classmethod
105     def del_mcast_tunnels_load(cls):
106         cls.add_del_mcast_tunnels_load(is_add=0)
107
108     # Class method to start the VXLAN test case.
109     #  Overrides setUpClass method in VppTestCase class.
110     #  Python try..except statement is used to ensure that the tear down of
111     #  the class will be executed even if exception is raised.
112     #  @param cls The class pointer.
113     @classmethod
114     def setUpClass(cls):
115         super(TestVxlan6, cls).setUpClass()
116
117         try:
118             cls.dport = 4789
119             cls.flags = 0x8
120
121             # Create 2 pg interfaces.
122             cls.create_pg_interfaces(range(4))
123             for pg in cls.pg_interfaces:
124                 pg.admin_up()
125
126             # Configure IPv4 addresses on VPP pg0.
127             cls.pg0.config_ip6()
128
129             # Resolve MAC address for VPP's IP address on pg0.
130             cls.pg0.resolve_ndp()
131
132             cls.mcast_ip6 = 'ff0e::1'
133             cls.mcast_ip6n = socket.inet_pton(socket.AF_INET6, cls.mcast_ip6)
134             cls.mcast_mac = "33:33:00:00:00:%02x" % (1)
135
136             # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
137             #  into BD.
138             cls.single_tunnel_bd = 1
139             r = cls.vapi.vxlan_add_del_tunnel(
140                 is_ipv6=1,
141                 src_addr=cls.pg0.local_ip6n,
142                 dst_addr=cls.pg0.remote_ip6n,
143                 vni=cls.single_tunnel_bd)
144             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
145                                                 bd_id=cls.single_tunnel_bd)
146             cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
147                                                 bd_id=cls.single_tunnel_bd)
148
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,
153                                            cls.n_ucast_tunnels)
154             r = cls.vapi.vxlan_add_del_tunnel(
155                 mcast_sw_if_index=1,
156                 src_addr=cls.pg0.local_ip6n,
157                 dst_addr=cls.mcast_ip6n,
158                 vni=cls.mcast_flood_bd, is_ipv6=1)
159             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
160                                                 bd_id=cls.mcast_flood_bd)
161             cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
162                                                 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(cls.pg3.sw_if_index,
169                                                 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         if not self.vpp_dead:
184             self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
185             self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
186             self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
187             self.logger.info(self.vapi.cli("show vxlan tunnel"))
188
189
190 if __name__ == '__main__':
191     unittest.main(testRunner=VppTestRunner)