Tests: Refactor tearDown show command logging, add lifecycle markers.
[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(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,
97                                               vni=vni)
98             cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
99                                                 bd_id=vni)
100
101     @classmethod
102     def add_mcast_tunnels_load(cls):
103         cls.add_del_mcast_tunnels_load(is_add=1)
104
105     @classmethod
106     def del_mcast_tunnels_load(cls):
107         cls.add_del_mcast_tunnels_load(is_add=0)
108
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.
114     @classmethod
115     def setUpClass(cls):
116         super(TestVxlan6, cls).setUpClass()
117
118         try:
119             cls.dport = 4789
120             cls.flags = 0x8
121
122             # Create 2 pg interfaces.
123             cls.create_pg_interfaces(range(4))
124             for pg in cls.pg_interfaces:
125                 pg.admin_up()
126
127             # Configure IPv4 addresses on VPP pg0.
128             cls.pg0.config_ip6()
129
130             # Resolve MAC address for VPP's IP address on pg0.
131             cls.pg0.resolve_ndp()
132
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)
136
137             # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
138             #  into BD.
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,
142                                               is_ipv6=1,
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)
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(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)
162
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,
166                                            cls.n_ucast_tunnels)
167             cls.vapi.sw_interface_set_l2_bridge(
168                 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
169         except Exception:
170             super(TestVxlan6, cls).tearDownClass()
171             raise
172
173     @classmethod
174     def tearDownClass(cls):
175         super(TestVxlan6, cls).tearDownClass()
176
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.
180     def tearDown(self):
181         super(TestVxlan6, self).tearDown()
182
183     def show_commands_at_teardown(self):
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)