blob: 45c501aa75261bfa96dd48aec9cffb6e3f4acb73 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Marco Varleseb598f1d2017-09-19 14:25:28 +02002
3import socket
Paul Vinciguerra2f156312020-05-02 22:34:40 -04004from util import ip4_range
Marco Varleseb598f1d2017-09-19 14:25:28 +02005import unittest
6from framework import VppTestCase, VppTestRunner
7from template_bd import BridgeDomain
8
Ole Troan7fc88cf2020-06-17 22:57:13 +02009from scapy.layers.l2 import Ether, ARP
10from scapy.layers.inet import IP, UDP, ICMP
snaramre5d4b8912019-12-13 23:39:35 +000011from scapy.contrib.geneve import GENEVE
Paul Vinciguerra2f156312020-05-02 22:34:40 -040012
13import util
Neale Ranns097fa662018-05-01 05:17:55 -070014from vpp_ip_route import VppIpRoute, VppRoutePath
15from vpp_ip import INVALID_INDEX
Marco Varleseb598f1d2017-09-19 14:25:28 +020016
17
18class TestGeneve(BridgeDomain, VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020019 """GENEVE Test Case"""
Marco Varleseb598f1d2017-09-19 14:25:28 +020020
21 def __init__(self, *args):
22 BridgeDomain.__init__(self)
23 VppTestCase.__init__(self, *args)
24
25 def encapsulate(self, pkt, vni):
Marco Varleseb598f1d2017-09-19 14:25:28 +020026 """
27 Encapsulate the original payload frame by adding GENEVE header with its
28 UDP, IP and Ethernet fields
29 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020030 return (
31 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 / GENEVE(vni=vni)
35 / pkt
36 )
Marco Varleseb598f1d2017-09-19 14:25:28 +020037
Eyal Baricef1e2a2018-06-18 13:01:59 +030038 def ip_range(self, start, end):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020039 """range of remote ip's"""
Eyal Baricef1e2a2018-06-18 13:01:59 +030040 return ip4_range(self.pg0.remote_ip4, start, end)
41
Marco Varleseb598f1d2017-09-19 14:25:28 +020042 def encap_mcast(self, pkt, src_ip, src_mac, vni):
43 """
44 Encapsulate the original payload frame by adding GENEVE header with its
45 UDP, IP and Ethernet fields
46 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020047 return (
48 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 / GENEVE(vni=vni)
52 / pkt
53 )
Marco Varleseb598f1d2017-09-19 14:25:28 +020054
55 def decapsulate(self, pkt):
56 """
57 Decapsulate the original payload frame by removing GENEVE header
58 """
59 # check if is set I flag
60 # self.assertEqual(pkt[GENEVE].flags, int('0x8', 16))
61 return pkt[GENEVE].payload
62
63 # Method for checking GENEVE encapsulation.
64 #
65 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
66 # TODO: add error messages
67 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
68 # by VPP using ARP.
69 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
70 if not local_only:
71 if not mcast_pkt:
72 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
73 else:
74 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
75 # Verify GENEVE tunnel source IP is VPP_IP and destination IP is MY_IP.
76 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
77 if not local_only:
78 if not mcast_pkt:
79 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
80 else:
81 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
82 # Verify UDP destination port is GENEVE 4789, source UDP port could be
83 # arbitrary.
84 self.assertEqual(pkt[UDP].dport, type(self).dport)
85 # TODO: checksum check
86 # Verify VNI
87 self.assertEqual(pkt[GENEVE].vni, vni)
88
89 @classmethod
90 def create_geneve_flood_test_bd(cls, vni, n_ucast_tunnels):
91 # Create 10 ucast geneve tunnels under bd
92 ip_range_start = 10
93 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -070094 next_hop_address = cls.pg0.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020095 for dest_ip4 in ip4_range(next_hop_address, ip_range_start, ip_range_end):
Jakub Grajciar2d3282e2019-10-01 12:04:56 +020096 # add host route so dest_ip4 will not be resolved
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020097 rip = VppIpRoute(
98 cls,
99 dest_ip4,
100 32,
101 [VppRoutePath(next_hop_address, INVALID_INDEX)],
102 register=False,
103 )
Neale Ranns097fa662018-05-01 05:17:55 -0700104 rip.add_vpp_config()
Marco Varleseb598f1d2017-09-19 14:25:28 +0200105 r = cls.vapi.geneve_add_del_tunnel(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200106 local_address=cls.pg0.local_ip4, remote_address=dest_ip4, vni=vni
107 )
108 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, bd_id=vni)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200109
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 geneve ref_count mechanism
115 """
Gabriel Ganne97cabc92018-02-08 11:22:33 +0100116 n_shared_dst_tunnels = 10
Marco Varleseb598f1d2017-09-19 14:25:28 +0200117 vni_start = 10000
118 vni_end = vni_start + n_shared_dst_tunnels
119 for vni in range(vni_start, vni_end):
120 r = cls.vapi.geneve_add_del_tunnel(
Jakub Grajciar2d3282e2019-10-01 12:04:56 +0200121 local_address=cls.pg0.local_ip4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200122 remote_address=cls.mcast_ip4,
123 mcast_sw_if_index=1,
124 is_add=is_add,
125 vni=vni,
126 )
127 if r.sw_if_index == 0xFFFFFFFF:
Paul Vinciguerrac599c6f2019-03-12 17:41:27 -0700128 raise ValueError("bad sw_if_index: ~0")
Marco Varleseb598f1d2017-09-19 14:25:28 +0200129
130 @classmethod
131 def add_shared_mcast_dst_load(cls):
132 cls.add_del_shared_mcast_dst_load(is_add=1)
133
134 @classmethod
135 def del_shared_mcast_dst_load(cls):
136 cls.add_del_shared_mcast_dst_load(is_add=0)
137
138 @classmethod
139 def add_del_mcast_tunnels_load(cls, is_add):
140 """
141 add or del tunnels to test geneve stability
142 """
Gabriel Ganne97cabc92018-02-08 11:22:33 +0100143 n_distinct_dst_tunnels = 10
Marco Varleseb598f1d2017-09-19 14:25:28 +0200144 ip_range_start = 10
145 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200146 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start, ip_range_end):
147 vni = int(dest_ip4.split(".")[3])
148 cls.vapi.geneve_add_del_tunnel(
149 local_address=cls.pg0.local_ip4,
150 remote_address=dest_ip4,
151 mcast_sw_if_index=1,
152 is_add=is_add,
153 vni=vni,
154 )
Marco Varleseb598f1d2017-09-19 14:25:28 +0200155
156 @classmethod
157 def add_mcast_tunnels_load(cls):
158 cls.add_del_mcast_tunnels_load(is_add=1)
159
160 @classmethod
161 def del_mcast_tunnels_load(cls):
162 cls.add_del_mcast_tunnels_load(is_add=0)
163
164 # Class method to start the GENEVE test case.
165 # Overrides setUpClass method in VppTestCase class.
166 # Python try..except statement is used to ensure that the tear down of
167 # the class will be executed even if exception is raised.
168 # @param cls The class pointer.
169 @classmethod
170 def setUpClass(cls):
171 super(TestGeneve, cls).setUpClass()
172
173 try:
174 cls.dport = 6081
175
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
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200188 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)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200190
191 # Create GENEVE VTEP on VPP pg0, and put geneve_tunnel0 and pg1
192 # into BD.
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200193 cls.single_tunnel_vni = 0xABCDE
Marco Varleseb598f1d2017-09-19 14:25:28 +0200194 cls.single_tunnel_bd = 1
195 r = cls.vapi.geneve_add_del_tunnel(
Jakub Grajciar2d3282e2019-10-01 12:04:56 +0200196 local_address=cls.pg0.local_ip4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200197 remote_address=cls.pg0.remote_ip4,
198 vni=cls.single_tunnel_vni,
199 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100200 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200201 rx_sw_if_index=r.sw_if_index, bd_id=cls.single_tunnel_bd
202 )
203 cls.vapi.sw_interface_set_l2_bridge(
204 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd
205 )
Marco Varleseb598f1d2017-09-19 14:25:28 +0200206
207 # Setup vni 2 to test multicast flooding
208 cls.n_ucast_tunnels = 10
209 cls.mcast_flood_bd = 2
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200210 cls.create_geneve_flood_test_bd(cls.mcast_flood_bd, cls.n_ucast_tunnels)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200211 r = cls.vapi.geneve_add_del_tunnel(
Jakub Grajciar2d3282e2019-10-01 12:04:56 +0200212 local_address=cls.pg0.local_ip4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200213 remote_address=cls.mcast_ip4,
214 mcast_sw_if_index=1,
215 vni=cls.mcast_flood_bd,
216 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100217 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200218 rx_sw_if_index=r.sw_if_index, bd_id=cls.mcast_flood_bd
219 )
220 cls.vapi.sw_interface_set_l2_bridge(
221 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd
222 )
Marco Varleseb598f1d2017-09-19 14:25:28 +0200223
224 # Add and delete mcast tunnels to check stability
225 cls.add_shared_mcast_dst_load()
226 cls.add_mcast_tunnels_load()
227 cls.del_shared_mcast_dst_load()
228 cls.del_mcast_tunnels_load()
229
230 # Setup vni 3 to test unicast flooding
231 cls.ucast_flood_bd = 3
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200232 cls.create_geneve_flood_test_bd(cls.ucast_flood_bd, cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100233 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200234 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd
235 )
Marco Varleseb598f1d2017-09-19 14:25:28 +0200236 except Exception:
237 super(TestGeneve, cls).tearDownClass()
238 raise
239
240 # Method to define VPP actions before tear down of the test case.
241 # Overrides tearDown method in VppTestCase class.
242 # @param self The object pointer.
243 def tearDown(self):
244 super(TestGeneve, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700245
246 def show_commands_at_teardown(self):
247 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
248 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
249 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
250 self.logger.info(self.vapi.cli("show geneve tunnel"))
Marco Varleseb598f1d2017-09-19 14:25:28 +0200251
252
Ole Troan7fc88cf2020-06-17 22:57:13 +0200253class TestGeneveL3(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200254 """GENEVE L3 Test Case"""
Ole Troan7fc88cf2020-06-17 22:57:13 +0200255
256 @classmethod
257 def setUpClass(cls):
258 super(TestGeneveL3, cls).setUpClass()
259 try:
260 cls.create_pg_interfaces(range(2))
261 cls.interfaces = list(cls.pg_interfaces)
262
263 for i in cls.interfaces:
264 i.admin_up()
265 i.config_ip4()
266 i.resolve_arp()
267 except Exception:
268 super(TestGeneveL3, cls).tearDownClass()
269 raise
270
271 @classmethod
272 def tearDownClass(cls):
273 super(TestGeneveL3, cls).tearDownClass()
274
275 def tearDown(self):
276 super(TestGeneveL3, self).tearDown()
277
278 def show_commands_at_teardown(self):
279 self.logger.info(self.vapi.cli("show geneve tunnel"))
280 self.logger.info(self.vapi.cli("show ip neighbor"))
281
282 def test_l3_packet(self):
283 vni = 1234
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200284 r = self.vapi.add_node_next(
285 node_name="geneve4-input", next_name="ethernet-input"
286 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200287 r = self.vapi.geneve_add_del_tunnel2(
288 is_add=1,
289 local_address=self.pg0.local_ip4,
290 remote_address=self.pg0.remote_ip4,
291 vni=vni,
292 l3_mode=1,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200293 decap_next_index=r.next_index,
294 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200295
296 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200297 sw_if_index=r.sw_if_index, prefix="10.0.0.1/24"
298 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200299
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200300 pkt = (
301 Ether(src=self.pg0.remote_mac, dst="d0:0b:ee:d0:00:00")
302 / IP(src="10.0.0.2", dst="10.0.0.1")
303 / ICMP()
304 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200305
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200306 encap = (
307 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
308 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
309 / UDP(sport=6081, dport=6081, chksum=0)
310 / GENEVE(vni=vni)
311 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200312
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200313 arp = Ether(src=self.pg0.remote_mac, dst="d0:0b:ee:d0:00:00") / ARP(
314 op="is-at",
315 hwsrc=self.pg0.remote_mac,
316 hwdst="d0:0b:ee:d0:00:00",
317 psrc="10.0.0.2",
318 pdst="10.0.0.1",
319 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200320
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200321 rx = self.send_and_expect(self.pg0, encap / pkt * 1, self.pg0)
322 rx = self.send_and_assert_no_replies(self.pg0, encap / arp * 1, self.pg0)
323 rx = self.send_and_expect(self.pg0, encap / pkt * 1, self.pg0)
Ole Troan7fc88cf2020-06-17 22:57:13 +0200324 self.assertEqual(rx[0][ICMP].type, 0) # echo reply
325
326 r = self.vapi.geneve_add_del_tunnel2(
327 is_add=0,
328 local_address=self.pg0.local_ip4,
329 remote_address=self.pg0.remote_ip4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200330 vni=vni,
331 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200332
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200333
334if __name__ == "__main__":
Marco Varleseb598f1d2017-09-19 14:25:28 +0200335 unittest.main(testRunner=VppTestRunner)