blob: 36661a9c1fa47f1f82b9f319305a096d1cb44358 [file] [log] [blame]
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08001#!/usr/bin/env python
2
3import socket
Eyal Baricef1e2a2018-06-18 13:01:59 +03004from util import ip4n_range, ip4_range
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08005import 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
Paul Vinciguerradefde0f2018-12-06 07:46:13 -080015@unittest.skipUnless(running_extended_tests, "part of extended tests")
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080016class 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
Eyal Baricef1e2a2018-06-18 13:01:59 +030034 def ip_range(self, start, end):
35 """ range of remote ip's """
36 return ip4_range(self.pg0.remote_ip4, start, end)
37
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080038 def encap_mcast(self, pkt, src_ip, src_mac, vni):
39 """
40 Encapsulate the original payload frame by adding VXLAN-GPE header
41 with its UDP, IP and Ethernet fields
42 """
43 return (Ether(src=src_mac, dst=self.mcast_mac) /
44 IP(src=src_ip, dst=self.mcast_ip4) /
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-GPE header
52 """
53 # check if is set I and P flag
Gabriel Ganne7e665d62017-11-17 09:18:53 +010054 self.assertEqual(pkt[VXLAN].flags, 0x0c)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080055 return pkt[VXLAN].payload
56
57 # Method for checking VXLAN-GPE encapsulation.
58 #
59 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
60 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
61 # by VPP using ARP.
62 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
63 if not local_only:
64 if not mcast_pkt:
65 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
66 else:
67 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
68 # Verify VXLAN-GPE tunnel src IP is VPP_IP and dst IP is MY_IP.
69 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
70 if not local_only:
71 if not mcast_pkt:
72 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
73 else:
74 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
75 # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
76 # could be arbitrary.
77 self.assertEqual(pkt[UDP].dport, type(self).dport)
78 # Verify VNI
Gabriel Ganne3904a0c2017-11-15 10:55:22 +010079 self.assertEqual(pkt[VXLAN].vni, vni)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080080
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080081 @classmethod
82 def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels):
83 # Create 10 ucast vxlan_gpe tunnels under bd
84 ip_range_start = 10
85 ip_range_end = ip_range_start + n_ucast_tunnels
86 next_hop_address = cls.pg0.remote_ip4n
87 for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
88 ip_range_end):
89 # add host route so dest_ip4n will not be resolved
Ole Troana5b2eec2019-03-11 19:23:25 +010090 cls.vapi.ip_add_del_route(dst_address=dest_ip4n,
91 dst_address_length=32,
92 next_hop_address=next_hop_address)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080093 r = cls.vapi.vxlan_gpe_add_del_tunnel(
94 src_addr=cls.pg0.local_ip4n,
95 dst_addr=dest_ip4n,
96 vni=vni)
Ole Troana5b2eec2019-03-11 19:23:25 +010097 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
98 bd_id=vni)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080099
100 @classmethod
101 def add_del_shared_mcast_dst_load(cls, is_add):
102 """
103 add or del tunnels sharing the same mcast dst
104 to test vxlan_gpe ref_count mechanism
105 """
106 n_shared_dst_tunnels = 20
107 vni_start = 1000
108 vni_end = vni_start + n_shared_dst_tunnels
109 for vni in range(vni_start, vni_end):
110 r = cls.vapi.vxlan_gpe_add_del_tunnel(
111 src_addr=cls.pg0.local_ip4n,
112 dst_addr=cls.mcast_ip4n,
113 mcast_sw_if_index=1,
114 vni=vni,
115 is_add=is_add)
116 if r.sw_if_index == 0xffffffff:
Paul Vinciguerrac599c6f2019-03-12 17:41:27 -0700117 raise ValueError("bad sw_if_index: ~0")
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800118
119 @classmethod
120 def add_shared_mcast_dst_load(cls):
121 cls.add_del_shared_mcast_dst_load(is_add=1)
122
123 @classmethod
124 def del_shared_mcast_dst_load(cls):
125 cls.add_del_shared_mcast_dst_load(is_add=0)
126
127 @classmethod
128 def add_del_mcast_tunnels_load(cls, is_add):
129 """
130 add or del tunnels to test vxlan_gpe stability
131 """
132 n_distinct_dst_tunnels = 20
133 ip_range_start = 10
134 ip_range_end = ip_range_start + n_distinct_dst_tunnels
135 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
136 ip_range_end):
137 vni = bytearray(dest_ip4n)[3]
138 cls.vapi.vxlan_gpe_add_del_tunnel(
139 src_addr=cls.pg0.local_ip4n,
140 dst_addr=dest_ip4n,
141 mcast_sw_if_index=1,
142 vni=vni,
143 is_add=is_add)
144
145 @classmethod
146 def add_mcast_tunnels_load(cls):
147 cls.add_del_mcast_tunnels_load(is_add=1)
148
149 @classmethod
150 def del_mcast_tunnels_load(cls):
151 cls.add_del_mcast_tunnels_load(is_add=0)
152
153 # Class method to start the VXLAN-GPE test case.
154 # Overrides setUpClass method in VppTestCase class.
155 # Python try..except statement is used to ensure that the tear down of
156 # the class will be executed even if exception is raised.
157 # @param cls The class pointer.
158 @classmethod
159 def setUpClass(cls):
160 super(TestVxlanGpe, cls).setUpClass()
161
162 try:
163 cls.dport = 4790
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100164 cls.flags = 0x0c
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800165
166 # Create 2 pg interfaces.
167 cls.create_pg_interfaces(range(4))
168 for pg in cls.pg_interfaces:
169 pg.admin_up()
170
171 # Configure IPv4 addresses on VPP pg0.
172 cls.pg0.config_ip4()
173
174 # Resolve MAC address for VPP's IP address on pg0.
175 cls.pg0.resolve_arp()
176
177 # Our Multicast address
178 cls.mcast_ip4 = '239.1.1.1'
179 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
180 iplong = atol(cls.mcast_ip4)
181 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
182 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
183
184 # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
185 # and pg1 into BD.
186 cls.single_tunnel_bd = 11
187 r = cls.vapi.vxlan_gpe_add_del_tunnel(
188 src_addr=cls.pg0.local_ip4n,
189 dst_addr=cls.pg0.remote_ip4n,
190 vni=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100191 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800192 bd_id=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100193 cls.vapi.sw_interface_set_l2_bridge(
194 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800195
196 # Setup vni 2 to test multicast flooding
197 cls.n_ucast_tunnels = 10
198 cls.mcast_flood_bd = 12
199 cls.create_vxlan_gpe_flood_test_bd(cls.mcast_flood_bd,
200 cls.n_ucast_tunnels)
201 r = cls.vapi.vxlan_gpe_add_del_tunnel(
202 src_addr=cls.pg0.local_ip4n,
203 dst_addr=cls.mcast_ip4n,
204 mcast_sw_if_index=1,
205 vni=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100206 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800207 bd_id=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100208 cls.vapi.sw_interface_set_l2_bridge(
209 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800210
211 # Add and delete mcast tunnels to check stability
212 cls.add_shared_mcast_dst_load()
213 cls.add_mcast_tunnels_load()
214 cls.del_shared_mcast_dst_load()
215 cls.del_mcast_tunnels_load()
216
217 # Setup vni 3 to test unicast flooding
218 cls.ucast_flood_bd = 13
219 cls.create_vxlan_gpe_flood_test_bd(cls.ucast_flood_bd,
220 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100221 cls.vapi.sw_interface_set_l2_bridge(
222 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800223 except Exception:
224 super(TestVxlanGpe, cls).tearDownClass()
225 raise
226
Paul Vinciguerra8d991d92019-01-25 14:05:48 -0800227 @classmethod
228 def tearDownClass(cls):
229 super(TestVxlanGpe, cls).tearDownClass()
230
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100231 @unittest.skip("test disabled for vxlan-gpe")
232 def test_mcast_flood(self):
233 """ inherited from BridgeDomain """
234 pass
235
236 @unittest.skip("test disabled for vxlan-gpe")
237 def test_mcast_rcv(self):
238 """ inherited from BridgeDomain """
239 pass
240
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800241 # Method to define VPP actions before tear down of the test case.
242 # Overrides tearDown method in VppTestCase class.
243 # @param self The object pointer.
244 def tearDown(self):
245 super(TestVxlanGpe, self).tearDown()
246 if not self.vpp_dead:
247 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
248 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
249 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
250 self.logger.info(self.vapi.cli("show int"))
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100251 self.logger.info(self.vapi.cli("show vxlan-gpe"))
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800252 self.logger.info(self.vapi.cli("show trace"))
253
254
255if __name__ == '__main__':
256 unittest.main(testRunner=VppTestRunner)