blob: c5d6bf07f7ce4913088cdd22a69bf94757774cfe [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +08002
3import socket
Paul Vinciguerra2f156312020-05-02 22:34:40 -04004from util import 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
snaramre5d4b8912019-12-13 23:39:35 +00009from scapy.layers.l2 import Ether
10from scapy.packet import Raw
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080011from scapy.layers.inet import IP, UDP
12from scapy.layers.vxlan import VXLAN
Paul Vinciguerra2f156312020-05-02 22:34:40 -040013
14import util
Neale Ranns097fa662018-05-01 05:17:55 -070015from vpp_ip_route import VppIpRoute, VppRoutePath
Paul Vinciguerra2f156312020-05-02 22:34:40 -040016
Neale Ranns097fa662018-05-01 05:17:55 -070017from vpp_ip import INVALID_INDEX
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080018
19
Paul Vinciguerradefde0f2018-12-06 07:46:13 -080020@unittest.skipUnless(running_extended_tests, "part of extended tests")
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080021class TestVxlanGpe(BridgeDomain, VppTestCase):
22 """ VXLAN-GPE Test Case """
23
24 def __init__(self, *args):
25 BridgeDomain.__init__(self)
26 VppTestCase.__init__(self, *args)
27
28 def encapsulate(self, pkt, vni):
29 """
30 Encapsulate the original payload frame by adding VXLAN-GPE header
31 with its UDP, IP and Ethernet fields
32 """
33 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
34 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
35 UDP(sport=self.dport, dport=self.dport, chksum=0) /
36 VXLAN(vni=vni, flags=self.flags) /
37 pkt)
38
Eyal Baricef1e2a2018-06-18 13:01:59 +030039 def ip_range(self, start, end):
40 """ range of remote ip's """
41 return ip4_range(self.pg0.remote_ip4, start, end)
42
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080043 def encap_mcast(self, pkt, src_ip, src_mac, vni):
44 """
45 Encapsulate the original payload frame by adding VXLAN-GPE header
46 with its UDP, IP and Ethernet fields
47 """
48 return (Ether(src=src_mac, dst=self.mcast_mac) /
49 IP(src=src_ip, dst=self.mcast_ip4) /
50 UDP(sport=self.dport, dport=self.dport, chksum=0) /
51 VXLAN(vni=vni, flags=self.flags) /
52 pkt)
53
54 def decapsulate(self, pkt):
55 """
56 Decapsulate the original payload frame by removing VXLAN-GPE header
57 """
58 # check if is set I and P flag
Gabriel Ganne7e665d62017-11-17 09:18:53 +010059 self.assertEqual(pkt[VXLAN].flags, 0x0c)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080060 return pkt[VXLAN].payload
61
62 # Method for checking VXLAN-GPE encapsulation.
63 #
64 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
65 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
66 # by VPP using ARP.
67 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
68 if not local_only:
69 if not mcast_pkt:
70 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
71 else:
72 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
73 # Verify VXLAN-GPE tunnel src IP is VPP_IP and dst IP is MY_IP.
74 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
75 if not local_only:
76 if not mcast_pkt:
77 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
78 else:
79 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
80 # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
81 # could be arbitrary.
82 self.assertEqual(pkt[UDP].dport, type(self).dport)
Vladimir Isaev698eb872020-05-21 16:34:17 +030083 # Verify UDP checksum
84 self.assert_udp_checksum_valid(pkt)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080085 # Verify VNI
Gabriel Ganne3904a0c2017-11-15 10:55:22 +010086 self.assertEqual(pkt[VXLAN].vni, vni)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080087
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080088 @classmethod
89 def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels):
Neale Ranns097fa662018-05-01 05:17:55 -070090 # Create 10 ucast vxlan tunnels under bd
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080091 ip_range_start = 10
92 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -070093 next_hop_address = cls.pg0.remote_ip4
94 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
95 ip_range_end):
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080096 # add host route so dest_ip4n will not be resolved
Neale Ranns097fa662018-05-01 05:17:55 -070097 rip = VppIpRoute(cls, dest_ip4, 32,
98 [VppRoutePath(next_hop_address,
99 INVALID_INDEX)],
100 register=False)
101 rip.add_vpp_config()
Neale Ranns097fa662018-05-01 05:17:55 -0700102
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800103 r = cls.vapi.vxlan_gpe_add_del_tunnel(
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400104 src_addr=cls.pg0.local_ip4,
105 dst_addr=dest_ip4,
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800106 vni=vni)
Ole Troana5b2eec2019-03-11 19:23:25 +0100107 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
108 bd_id=vni)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800109
110 @classmethod
111 def add_del_shared_mcast_dst_load(cls, is_add):
112 """
113 add or del tunnels sharing the same mcast dst
114 to test vxlan_gpe ref_count mechanism
115 """
116 n_shared_dst_tunnels = 20
117 vni_start = 1000
118 vni_end = vni_start + n_shared_dst_tunnels
119 for vni in range(vni_start, vni_end):
120 r = cls.vapi.vxlan_gpe_add_del_tunnel(
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400121 local=cls.pg0.local_ip4,
122 remote=cls.mcast_ip4,
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800123 mcast_sw_if_index=1,
124 vni=vni,
125 is_add=is_add)
126 if r.sw_if_index == 0xffffffff:
Paul Vinciguerrac599c6f2019-03-12 17:41:27 -0700127 raise ValueError("bad sw_if_index: ~0")
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800128
129 @classmethod
130 def add_shared_mcast_dst_load(cls):
131 cls.add_del_shared_mcast_dst_load(is_add=1)
132
133 @classmethod
134 def del_shared_mcast_dst_load(cls):
135 cls.add_del_shared_mcast_dst_load(is_add=0)
136
137 @classmethod
138 def add_del_mcast_tunnels_load(cls, is_add):
139 """
140 add or del tunnels to test vxlan_gpe stability
141 """
142 n_distinct_dst_tunnels = 20
143 ip_range_start = 10
144 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400145 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
146 ip_range_end):
147 vni = int(dest_ip4.split(".")[3])
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800148 cls.vapi.vxlan_gpe_add_del_tunnel(
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400149 src_addr=cls.pg0.local_ip4,
150 dst_addr=dest_ip4,
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800151 mcast_sw_if_index=1,
152 vni=vni,
153 is_add=is_add)
154
155 @classmethod
156 def add_mcast_tunnels_load(cls):
157 cls.add_del_mcast_tunnels_load(is_add=1)
158
159 @classmethod
160 def del_mcast_tunnels_load(cls):
161 cls.add_del_mcast_tunnels_load(is_add=0)
162
163 # Class method to start the VXLAN-GPE test case.
164 # Overrides setUpClass method in VppTestCase class.
165 # Python try..except statement is used to ensure that the tear down of
166 # the class will be executed even if exception is raised.
167 # @param cls The class pointer.
168 @classmethod
169 def setUpClass(cls):
170 super(TestVxlanGpe, cls).setUpClass()
171
172 try:
173 cls.dport = 4790
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100174 cls.flags = 0x0c
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800175
176 # Create 2 pg interfaces.
177 cls.create_pg_interfaces(range(4))
178 for pg in cls.pg_interfaces:
179 pg.admin_up()
180
181 # Configure IPv4 addresses on VPP pg0.
182 cls.pg0.config_ip4()
183
184 # Resolve MAC address for VPP's IP address on pg0.
185 cls.pg0.resolve_arp()
186
187 # Our Multicast address
188 cls.mcast_ip4 = '239.1.1.1'
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400189 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800190
191 # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
192 # and pg1 into BD.
Neale Ranns91fd9102020-04-03 07:46:28 +0000193 cls.single_tunnel_vni = 0xabcde
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800194 cls.single_tunnel_bd = 11
195 r = cls.vapi.vxlan_gpe_add_del_tunnel(
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400196 src_addr=cls.pg0.local_ip4,
197 dst_addr=cls.pg0.remote_ip4,
Neale Ranns91fd9102020-04-03 07:46:28 +0000198 vni=cls.single_tunnel_vni)
Ole Troana5b2eec2019-03-11 19:23:25 +0100199 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800200 bd_id=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100201 cls.vapi.sw_interface_set_l2_bridge(
202 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800203
204 # Setup vni 2 to test multicast flooding
205 cls.n_ucast_tunnels = 10
206 cls.mcast_flood_bd = 12
207 cls.create_vxlan_gpe_flood_test_bd(cls.mcast_flood_bd,
208 cls.n_ucast_tunnels)
209 r = cls.vapi.vxlan_gpe_add_del_tunnel(
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400210 src_addr=cls.pg0.local_ip4,
211 dst_addr=cls.mcast_ip4,
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800212 mcast_sw_if_index=1,
213 vni=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100214 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800215 bd_id=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100216 cls.vapi.sw_interface_set_l2_bridge(
217 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800218
219 # Add and delete mcast tunnels to check stability
220 cls.add_shared_mcast_dst_load()
221 cls.add_mcast_tunnels_load()
222 cls.del_shared_mcast_dst_load()
223 cls.del_mcast_tunnels_load()
224
225 # Setup vni 3 to test unicast flooding
226 cls.ucast_flood_bd = 13
227 cls.create_vxlan_gpe_flood_test_bd(cls.ucast_flood_bd,
228 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100229 cls.vapi.sw_interface_set_l2_bridge(
230 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800231 except Exception:
232 super(TestVxlanGpe, cls).tearDownClass()
233 raise
234
Paul Vinciguerra8d991d92019-01-25 14:05:48 -0800235 @classmethod
236 def tearDownClass(cls):
237 super(TestVxlanGpe, cls).tearDownClass()
238
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100239 @unittest.skip("test disabled for vxlan-gpe")
240 def test_mcast_flood(self):
241 """ inherited from BridgeDomain """
242 pass
243
244 @unittest.skip("test disabled for vxlan-gpe")
245 def test_mcast_rcv(self):
246 """ inherited from BridgeDomain """
247 pass
248
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800249 # Method to define VPP actions before tear down of the test case.
250 # Overrides tearDown method in VppTestCase class.
251 # @param self The object pointer.
252 def tearDown(self):
253 super(TestVxlanGpe, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700254
255 def show_commands_at_teardown(self):
256 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
257 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
258 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
259 self.logger.info(self.vapi.cli("show int"))
260 self.logger.info(self.vapi.cli("show vxlan-gpe"))
261 self.logger.info(self.vapi.cli("show trace"))
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800262
263
264if __name__ == '__main__':
265 unittest.main(testRunner=VppTestRunner)