test adding and removing shared mcast dst tunnels
[vpp.git] / test / test_vxlan.py
1 #!/usr/bin/env python
2
3 import socket
4 from util import ip4n_range
5 import unittest
6 from framework import VppTestCase, VppTestRunner
7 from template_bd import BridgeDomain
8
9 from scapy.layers.l2 import Ether
10 from scapy.layers.inet import IP, UDP
11 from scapy.layers.vxlan import VXLAN
12 from scapy.utils import atol
13
14
15 class TestVxlan(BridgeDomain, VppTestCase):
16     """ VXLAN Test Case """
17
18     def __init__(self, *args):
19         BridgeDomain.__init__(self)
20         VppTestCase.__init__(self, *args)
21
22     def encapsulate(self, pkt, vni):
23         """
24         Encapsulate the original payload frame by adding VXLAN header with its
25         UDP, IP and Ethernet fields
26         """
27         return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
28                 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
29                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
30                 VXLAN(vni=vni, flags=self.flags) /
31                 pkt)
32
33     def encap_mcast(self, pkt, src_ip, src_mac, vni):
34         """
35         Encapsulate the original payload frame by adding VXLAN header with its
36         UDP, IP and Ethernet fields
37         """
38         return (Ether(src=src_mac, dst=self.mcast_mac) /
39                 IP(src=src_ip, dst=self.mcast_ip4) /
40                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
41                 VXLAN(vni=vni, flags=self.flags) /
42                 pkt)
43
44     def decapsulate(self, pkt):
45         """
46         Decapsulate the original payload frame by removing VXLAN header
47         """
48         # check if is set I flag
49         self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
50         return pkt[VXLAN].payload
51
52     # Method for checking VXLAN encapsulation.
53     #
54     def check_encapsulation(self, pkt, vni, local_only=False):
55         # TODO: add error messages
56         # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
57         #  by VPP using ARP.
58         self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
59         if not local_only:
60             self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
61         # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
62         self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
63         if not local_only:
64             self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
65         # Verify UDP destination port is VXLAN 4789, source UDP port could be
66         #  arbitrary.
67         self.assertEqual(pkt[UDP].dport, type(self).dport)
68         # TODO: checksum check
69         # Verify VNI
70         self.assertEqual(pkt[VXLAN].vni, vni)
71
72     @classmethod
73     def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
74         # Create 10 ucast vxlan tunnels under bd
75         ip_range_start = 10
76         ip_range_end = ip_range_start + n_ucast_tunnels
77         next_hop_address = cls.pg0.remote_ip4n
78         for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
79                                     ip_range_end):
80             # add host route so dest_ip4n will not be resolved
81             cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
82             r = cls.vapi.vxlan_add_del_tunnel(
83                 src_addr=cls.pg0.local_ip4n,
84                 dst_addr=dest_ip4n,
85                 vni=vni)
86             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
87
88     @classmethod
89     def add_del_shared_mcast_dst_load(cls, is_add):
90         """
91         add or del tunnels sharing the same mcast dst
92         to test vxlan ref_count mechanism
93         """
94         n_shared_dst_tunnels = 2000
95         vni_start = 10000
96         vni_end = vni_start + n_shared_dst_tunnels
97         for vni in range(vni_start, vni_end):
98             cls.vapi.vxlan_add_del_tunnel(
99                 src_addr=cls.pg0.local_ip4n,
100                 dst_addr=cls.mcast_ip4n,
101                 mcast_sw_if_index=1,
102                 vni=vni,
103                 is_add=is_add)
104
105     @classmethod
106     def add_shared_mcast_dst_load(cls):
107         cls.add_del_shared_mcast_dst_load(is_add=1)
108
109     @classmethod
110     def del_shared_mcast_dst_load(cls):
111         cls.add_del_shared_mcast_dst_load(is_add=0)
112
113     @classmethod
114     def add_del_mcast_tunnels_load(cls, is_add):
115         """
116         add or del tunnels to test vxlan stability
117         """
118         n_distinct_dst_tunnels = 200
119         ip_range_start = 10
120         ip_range_end = ip_range_start + n_distinct_dst_tunnels
121         for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
122                                     ip_range_end):
123             vni = bytearray(dest_ip4n)[3]
124             cls.vapi.vxlan_add_del_tunnel(
125                 src_addr=cls.pg0.local_ip4n,
126                 dst_addr=dest_ip4n,
127                 mcast_sw_if_index=1,
128                 vni=vni,
129                 is_add=is_add)
130
131     @classmethod
132     def add_mcast_tunnels_load(cls):
133         cls.add_del_mcast_tunnels_load(is_add=1)
134
135     @classmethod
136     def del_mcast_tunnels_load(cls):
137         cls.add_del_mcast_tunnels_load(is_add=0)
138
139     # Class method to start the VXLAN test case.
140     #  Overrides setUpClass method in VppTestCase class.
141     #  Python try..except statement is used to ensure that the tear down of
142     #  the class will be executed even if exception is raised.
143     #  @param cls The class pointer.
144     @classmethod
145     def setUpClass(cls):
146         super(TestVxlan, cls).setUpClass()
147
148         try:
149             cls.dport = 4789
150             cls.flags = 0x8
151
152             # Create 2 pg interfaces.
153             cls.create_pg_interfaces(range(4))
154             for pg in cls.pg_interfaces:
155                 pg.admin_up()
156
157             # Configure IPv4 addresses on VPP pg0.
158             cls.pg0.config_ip4()
159
160             # Resolve MAC address for VPP's IP address on pg0.
161             cls.pg0.resolve_arp()
162
163             # Our Multicast address
164             cls.mcast_ip4 = '239.1.1.1'
165             cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
166             iplong = atol(cls.mcast_ip4)
167             cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
168                 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
169
170             # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
171             #  into BD.
172             cls.single_tunnel_bd = 1
173             r = cls.vapi.vxlan_add_del_tunnel(
174                 src_addr=cls.pg0.local_ip4n,
175                 dst_addr=cls.pg0.remote_ip4n,
176                 vni=cls.single_tunnel_bd)
177             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
178                                                 bd_id=cls.single_tunnel_bd)
179             cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
180                                                 bd_id=cls.single_tunnel_bd)
181
182             # Setup vni 2 to test multicast flooding
183             cls.n_ucast_tunnels = 10
184             cls.mcast_flood_bd = 2
185             cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
186                                            cls.n_ucast_tunnels)
187             r = cls.vapi.vxlan_add_del_tunnel(
188                 src_addr=cls.pg0.local_ip4n,
189                 dst_addr=cls.mcast_ip4n,
190                 mcast_sw_if_index=1,
191                 vni=cls.mcast_flood_bd)
192             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
193                                                 bd_id=cls.mcast_flood_bd)
194             cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
195                                                 bd_id=cls.mcast_flood_bd)
196
197             # Add and delete mcast tunnels to check stability
198             cls.add_shared_mcast_dst_load()
199             cls.add_mcast_tunnels_load()
200             cls.del_shared_mcast_dst_load()
201             cls.del_mcast_tunnels_load()
202
203             # Setup vni 3 to test unicast flooding
204             cls.ucast_flood_bd = 3
205             cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
206                                            cls.n_ucast_tunnels)
207             cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
208                                                 bd_id=cls.ucast_flood_bd)
209         except Exception:
210             super(TestVxlan, cls).tearDownClass()
211             raise
212
213     # Method to define VPP actions before tear down of the test case.
214     #  Overrides tearDown method in VppTestCase class.
215     #  @param self The object pointer.
216     def tearDown(self):
217         super(TestVxlan, self).tearDown()
218         if not self.vpp_dead:
219             self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
220             self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
221             self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
222             self.logger.info(self.vapi.cli("show vxlan tunnel"))
223
224
225 if __name__ == '__main__':
226     unittest.main(testRunner=VppTestRunner)