blob: 61d86fe07dc08802834999ef1ce1c63ebea4ea81 [file] [log] [blame]
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001#!/usr/bin/env python
2
3import socket
4from util import ip4n_range
5import unittest
6from framework import VppTestCase, VppTestRunner, running_extended_tests
7from template_bd import BridgeDomain
8
9from scapy.layers.l2 import Ether, Raw
10from scapy.layers.inet import IP, UDP
11from scapy.layers.vxlan import VXLAN
12from scapy.utils import atol
13
14
15@unittest.skipUnless(running_extended_tests(), "part of extended tests")
16class TestVxlanGpe(BridgeDomain, VppTestCase):
17 """ VXLAN-GPE 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-GPE header
26 with its UDP, IP and Ethernet fields
27 """
28 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
29 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
30 UDP(sport=self.dport, dport=self.dport, chksum=0) /
31 VXLAN(vni=vni, flags=self.flags) /
32 pkt)
33
34 def encap_mcast(self, pkt, src_ip, src_mac, vni):
35 """
36 Encapsulate the original payload frame by adding VXLAN-GPE header
37 with its UDP, IP and Ethernet fields
38 """
39 return (Ether(src=src_mac, dst=self.mcast_mac) /
40 IP(src=src_ip, dst=self.mcast_ip4) /
41 UDP(sport=self.dport, dport=self.dport, chksum=0) /
42 VXLAN(vni=vni, flags=self.flags) /
43 pkt)
44
45 def decapsulate(self, pkt):
46 """
47 Decapsulate the original payload frame by removing VXLAN-GPE header
48 """
49 # check if is set I and P flag
Gabriel Ganne7e665d62017-11-17 09:18:53 +010050 self.assertEqual(pkt[VXLAN].flags, 0x0c)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080051 return pkt[VXLAN].payload
52
53 # Method for checking VXLAN-GPE encapsulation.
54 #
55 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
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 if not mcast_pkt:
61 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
62 else:
63 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
64 # Verify VXLAN-GPE tunnel src IP is VPP_IP and dst IP is MY_IP.
65 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
66 if not local_only:
67 if not mcast_pkt:
68 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
69 else:
70 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
71 # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
72 # could be arbitrary.
73 self.assertEqual(pkt[UDP].dport, type(self).dport)
74 # Verify VNI
Gabriel Ganne3904a0c2017-11-15 10:55:22 +010075 self.assertEqual(pkt[VXLAN].vni, vni)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080076
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080077 @classmethod
78 def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels):
79 # Create 10 ucast vxlan_gpe tunnels under bd
80 ip_range_start = 10
81 ip_range_end = ip_range_start + n_ucast_tunnels
82 next_hop_address = cls.pg0.remote_ip4n
83 for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
84 ip_range_end):
85 # add host route so dest_ip4n will not be resolved
86 cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
87 r = cls.vapi.vxlan_gpe_add_del_tunnel(
88 src_addr=cls.pg0.local_ip4n,
89 dst_addr=dest_ip4n,
90 vni=vni)
91 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
92
93 @classmethod
94 def add_del_shared_mcast_dst_load(cls, is_add):
95 """
96 add or del tunnels sharing the same mcast dst
97 to test vxlan_gpe ref_count mechanism
98 """
99 n_shared_dst_tunnels = 20
100 vni_start = 1000
101 vni_end = vni_start + n_shared_dst_tunnels
102 for vni in range(vni_start, vni_end):
103 r = cls.vapi.vxlan_gpe_add_del_tunnel(
104 src_addr=cls.pg0.local_ip4n,
105 dst_addr=cls.mcast_ip4n,
106 mcast_sw_if_index=1,
107 vni=vni,
108 is_add=is_add)
109 if r.sw_if_index == 0xffffffff:
110 raise "bad sw_if_index"
111
112 @classmethod
113 def add_shared_mcast_dst_load(cls):
114 cls.add_del_shared_mcast_dst_load(is_add=1)
115
116 @classmethod
117 def del_shared_mcast_dst_load(cls):
118 cls.add_del_shared_mcast_dst_load(is_add=0)
119
120 @classmethod
121 def add_del_mcast_tunnels_load(cls, is_add):
122 """
123 add or del tunnels to test vxlan_gpe stability
124 """
125 n_distinct_dst_tunnels = 20
126 ip_range_start = 10
127 ip_range_end = ip_range_start + n_distinct_dst_tunnels
128 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
129 ip_range_end):
130 vni = bytearray(dest_ip4n)[3]
131 cls.vapi.vxlan_gpe_add_del_tunnel(
132 src_addr=cls.pg0.local_ip4n,
133 dst_addr=dest_ip4n,
134 mcast_sw_if_index=1,
135 vni=vni,
136 is_add=is_add)
137
138 @classmethod
139 def add_mcast_tunnels_load(cls):
140 cls.add_del_mcast_tunnels_load(is_add=1)
141
142 @classmethod
143 def del_mcast_tunnels_load(cls):
144 cls.add_del_mcast_tunnels_load(is_add=0)
145
146 # Class method to start the VXLAN-GPE test case.
147 # Overrides setUpClass method in VppTestCase class.
148 # Python try..except statement is used to ensure that the tear down of
149 # the class will be executed even if exception is raised.
150 # @param cls The class pointer.
151 @classmethod
152 def setUpClass(cls):
153 super(TestVxlanGpe, cls).setUpClass()
154
155 try:
156 cls.dport = 4790
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100157 cls.flags = 0x0c
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800158
159 # Create 2 pg interfaces.
160 cls.create_pg_interfaces(range(4))
161 for pg in cls.pg_interfaces:
162 pg.admin_up()
163
164 # Configure IPv4 addresses on VPP pg0.
165 cls.pg0.config_ip4()
166
167 # Resolve MAC address for VPP's IP address on pg0.
168 cls.pg0.resolve_arp()
169
170 # Our Multicast address
171 cls.mcast_ip4 = '239.1.1.1'
172 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
173 iplong = atol(cls.mcast_ip4)
174 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
175 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
176
177 # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
178 # and pg1 into BD.
179 cls.single_tunnel_bd = 11
180 r = cls.vapi.vxlan_gpe_add_del_tunnel(
181 src_addr=cls.pg0.local_ip4n,
182 dst_addr=cls.pg0.remote_ip4n,
183 vni=cls.single_tunnel_bd)
184 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
185 bd_id=cls.single_tunnel_bd)
186 cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
187 bd_id=cls.single_tunnel_bd)
188
189 # Setup vni 2 to test multicast flooding
190 cls.n_ucast_tunnels = 10
191 cls.mcast_flood_bd = 12
192 cls.create_vxlan_gpe_flood_test_bd(cls.mcast_flood_bd,
193 cls.n_ucast_tunnels)
194 r = cls.vapi.vxlan_gpe_add_del_tunnel(
195 src_addr=cls.pg0.local_ip4n,
196 dst_addr=cls.mcast_ip4n,
197 mcast_sw_if_index=1,
198 vni=cls.mcast_flood_bd)
199 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
200 bd_id=cls.mcast_flood_bd)
201 cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
202 bd_id=cls.mcast_flood_bd)
203
204 # Add and delete mcast tunnels to check stability
205 cls.add_shared_mcast_dst_load()
206 cls.add_mcast_tunnels_load()
207 cls.del_shared_mcast_dst_load()
208 cls.del_mcast_tunnels_load()
209
210 # Setup vni 3 to test unicast flooding
211 cls.ucast_flood_bd = 13
212 cls.create_vxlan_gpe_flood_test_bd(cls.ucast_flood_bd,
213 cls.n_ucast_tunnels)
214 cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
215 bd_id=cls.ucast_flood_bd)
216 except Exception:
217 super(TestVxlanGpe, cls).tearDownClass()
218 raise
219
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100220 @unittest.skip("test disabled for vxlan-gpe")
221 def test_mcast_flood(self):
222 """ inherited from BridgeDomain """
223 pass
224
225 @unittest.skip("test disabled for vxlan-gpe")
226 def test_mcast_rcv(self):
227 """ inherited from BridgeDomain """
228 pass
229
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800230 # Method to define VPP actions before tear down of the test case.
231 # Overrides tearDown method in VppTestCase class.
232 # @param self The object pointer.
233 def tearDown(self):
234 super(TestVxlanGpe, self).tearDown()
235 if not self.vpp_dead:
236 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
237 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
238 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
239 self.logger.info(self.vapi.cli("show int"))
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100240 self.logger.info(self.vapi.cli("show vxlan-gpe"))
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800241 self.logger.info(self.vapi.cli("show trace"))
242
243
244if __name__ == '__main__':
245 unittest.main(testRunner=VppTestRunner)