blob: ca1ad9c60bf379da1cc0147f0b5c9abaf32b5d03 [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
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
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
13from scapy.utils import atol
Neale Ranns097fa662018-05-01 05:17:55 -070014from vpp_ip_route import VppIpRoute, VppRoutePath
15from vpp_ip import INVALID_INDEX
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080016
17
Paul Vinciguerradefde0f2018-12-06 07:46:13 -080018@unittest.skipUnless(running_extended_tests, "part of extended tests")
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080019class TestVxlanGpe(BridgeDomain, VppTestCase):
20 """ VXLAN-GPE Test Case """
21
22 def __init__(self, *args):
23 BridgeDomain.__init__(self)
24 VppTestCase.__init__(self, *args)
25
26 def encapsulate(self, pkt, vni):
27 """
28 Encapsulate the original payload frame by adding VXLAN-GPE header
29 with its UDP, IP and Ethernet fields
30 """
31 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
32 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
33 UDP(sport=self.dport, dport=self.dport, chksum=0) /
34 VXLAN(vni=vni, flags=self.flags) /
35 pkt)
36
Eyal Baricef1e2a2018-06-18 13:01:59 +030037 def ip_range(self, start, end):
38 """ range of remote ip's """
39 return ip4_range(self.pg0.remote_ip4, start, end)
40
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080041 def encap_mcast(self, pkt, src_ip, src_mac, vni):
42 """
43 Encapsulate the original payload frame by adding VXLAN-GPE header
44 with its UDP, IP and Ethernet fields
45 """
46 return (Ether(src=src_mac, dst=self.mcast_mac) /
47 IP(src=src_ip, dst=self.mcast_ip4) /
48 UDP(sport=self.dport, dport=self.dport, chksum=0) /
49 VXLAN(vni=vni, flags=self.flags) /
50 pkt)
51
52 def decapsulate(self, pkt):
53 """
54 Decapsulate the original payload frame by removing VXLAN-GPE header
55 """
56 # check if is set I and P flag
Gabriel Ganne7e665d62017-11-17 09:18:53 +010057 self.assertEqual(pkt[VXLAN].flags, 0x0c)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080058 return pkt[VXLAN].payload
59
60 # Method for checking VXLAN-GPE encapsulation.
61 #
62 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
63 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
64 # by VPP using ARP.
65 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
66 if not local_only:
67 if not mcast_pkt:
68 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
69 else:
70 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
71 # Verify VXLAN-GPE tunnel src IP is VPP_IP and dst IP is MY_IP.
72 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
73 if not local_only:
74 if not mcast_pkt:
75 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
76 else:
77 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
78 # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
79 # could be arbitrary.
80 self.assertEqual(pkt[UDP].dport, type(self).dport)
81 # Verify VNI
Gabriel Ganne3904a0c2017-11-15 10:55:22 +010082 self.assertEqual(pkt[VXLAN].vni, vni)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080083
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080084 @classmethod
85 def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels):
Neale Ranns097fa662018-05-01 05:17:55 -070086 # Create 10 ucast vxlan tunnels under bd
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080087 ip_range_start = 10
88 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -070089 next_hop_address = cls.pg0.remote_ip4
90 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
91 ip_range_end):
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080092 # add host route so dest_ip4n will not be resolved
Neale Ranns097fa662018-05-01 05:17:55 -070093 rip = VppIpRoute(cls, dest_ip4, 32,
94 [VppRoutePath(next_hop_address,
95 INVALID_INDEX)],
96 register=False)
97 rip.add_vpp_config()
98 dest_ip4n = socket.inet_pton(socket.AF_INET, dest_ip4)
99
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800100 r = cls.vapi.vxlan_gpe_add_del_tunnel(
101 src_addr=cls.pg0.local_ip4n,
102 dst_addr=dest_ip4n,
103 vni=vni)
Ole Troana5b2eec2019-03-11 19:23:25 +0100104 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
105 bd_id=vni)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800106
107 @classmethod
108 def add_del_shared_mcast_dst_load(cls, is_add):
109 """
110 add or del tunnels sharing the same mcast dst
111 to test vxlan_gpe ref_count mechanism
112 """
113 n_shared_dst_tunnels = 20
114 vni_start = 1000
115 vni_end = vni_start + n_shared_dst_tunnels
116 for vni in range(vni_start, vni_end):
117 r = cls.vapi.vxlan_gpe_add_del_tunnel(
118 src_addr=cls.pg0.local_ip4n,
119 dst_addr=cls.mcast_ip4n,
120 mcast_sw_if_index=1,
121 vni=vni,
122 is_add=is_add)
123 if r.sw_if_index == 0xffffffff:
Paul Vinciguerrac599c6f2019-03-12 17:41:27 -0700124 raise ValueError("bad sw_if_index: ~0")
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800125
126 @classmethod
127 def add_shared_mcast_dst_load(cls):
128 cls.add_del_shared_mcast_dst_load(is_add=1)
129
130 @classmethod
131 def del_shared_mcast_dst_load(cls):
132 cls.add_del_shared_mcast_dst_load(is_add=0)
133
134 @classmethod
135 def add_del_mcast_tunnels_load(cls, is_add):
136 """
137 add or del tunnels to test vxlan_gpe stability
138 """
139 n_distinct_dst_tunnels = 20
140 ip_range_start = 10
141 ip_range_end = ip_range_start + n_distinct_dst_tunnels
142 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
143 ip_range_end):
144 vni = bytearray(dest_ip4n)[3]
145 cls.vapi.vxlan_gpe_add_del_tunnel(
146 src_addr=cls.pg0.local_ip4n,
147 dst_addr=dest_ip4n,
148 mcast_sw_if_index=1,
149 vni=vni,
150 is_add=is_add)
151
152 @classmethod
153 def add_mcast_tunnels_load(cls):
154 cls.add_del_mcast_tunnels_load(is_add=1)
155
156 @classmethod
157 def del_mcast_tunnels_load(cls):
158 cls.add_del_mcast_tunnels_load(is_add=0)
159
160 # Class method to start the VXLAN-GPE test case.
161 # Overrides setUpClass method in VppTestCase class.
162 # Python try..except statement is used to ensure that the tear down of
163 # the class will be executed even if exception is raised.
164 # @param cls The class pointer.
165 @classmethod
166 def setUpClass(cls):
167 super(TestVxlanGpe, cls).setUpClass()
168
169 try:
170 cls.dport = 4790
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100171 cls.flags = 0x0c
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800172
173 # Create 2 pg interfaces.
174 cls.create_pg_interfaces(range(4))
175 for pg in cls.pg_interfaces:
176 pg.admin_up()
177
178 # Configure IPv4 addresses on VPP pg0.
179 cls.pg0.config_ip4()
180
181 # Resolve MAC address for VPP's IP address on pg0.
182 cls.pg0.resolve_arp()
183
184 # Our Multicast address
185 cls.mcast_ip4 = '239.1.1.1'
186 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
187 iplong = atol(cls.mcast_ip4)
188 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
189 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
190
191 # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
192 # and pg1 into BD.
193 cls.single_tunnel_bd = 11
194 r = cls.vapi.vxlan_gpe_add_del_tunnel(
195 src_addr=cls.pg0.local_ip4n,
196 dst_addr=cls.pg0.remote_ip4n,
197 vni=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100198 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800199 bd_id=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100200 cls.vapi.sw_interface_set_l2_bridge(
201 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800202
203 # Setup vni 2 to test multicast flooding
204 cls.n_ucast_tunnels = 10
205 cls.mcast_flood_bd = 12
206 cls.create_vxlan_gpe_flood_test_bd(cls.mcast_flood_bd,
207 cls.n_ucast_tunnels)
208 r = cls.vapi.vxlan_gpe_add_del_tunnel(
209 src_addr=cls.pg0.local_ip4n,
210 dst_addr=cls.mcast_ip4n,
211 mcast_sw_if_index=1,
212 vni=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100213 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800214 bd_id=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100215 cls.vapi.sw_interface_set_l2_bridge(
216 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800217
218 # Add and delete mcast tunnels to check stability
219 cls.add_shared_mcast_dst_load()
220 cls.add_mcast_tunnels_load()
221 cls.del_shared_mcast_dst_load()
222 cls.del_mcast_tunnels_load()
223
224 # Setup vni 3 to test unicast flooding
225 cls.ucast_flood_bd = 13
226 cls.create_vxlan_gpe_flood_test_bd(cls.ucast_flood_bd,
227 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100228 cls.vapi.sw_interface_set_l2_bridge(
229 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800230 except Exception:
231 super(TestVxlanGpe, cls).tearDownClass()
232 raise
233
Paul Vinciguerra8d991d92019-01-25 14:05:48 -0800234 @classmethod
235 def tearDownClass(cls):
236 super(TestVxlanGpe, cls).tearDownClass()
237
Gabriel Ganne7e665d62017-11-17 09:18:53 +0100238 @unittest.skip("test disabled for vxlan-gpe")
239 def test_mcast_flood(self):
240 """ inherited from BridgeDomain """
241 pass
242
243 @unittest.skip("test disabled for vxlan-gpe")
244 def test_mcast_rcv(self):
245 """ inherited from BridgeDomain """
246 pass
247
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800248 # Method to define VPP actions before tear down of the test case.
249 # Overrides tearDown method in VppTestCase class.
250 # @param self The object pointer.
251 def tearDown(self):
252 super(TestVxlanGpe, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700253
254 def show_commands_at_teardown(self):
255 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
256 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
257 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
258 self.logger.info(self.vapi.cli("show int"))
259 self.logger.info(self.vapi.cli("show vxlan-gpe"))
260 self.logger.info(self.vapi.cli("show trace"))
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800261
262
263if __name__ == '__main__':
264 unittest.main(testRunner=VppTestRunner)