blob: f8c135bed108106f21f9e5e296a97fdd3f61bc85 [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
Paul Vinciguerra2f156312020-05-02 22:34:40 -04003from util import ip4_range
Marco Varleseb598f1d2017-09-19 14:25:28 +02004import unittest
Dave Wallace8800f732023-08-31 00:47:44 -04005from framework import VppTestCase
6from asfframework import VppTestRunner
Marco Varleseb598f1d2017-09-19 14:25:28 +02007from template_bd import BridgeDomain
Dmitry Valter34fa0ce2024-03-11 10:38:46 +00008from config import config
Marco Varleseb598f1d2017-09-19 14:25:28 +02009
Ole Troan7fc88cf2020-06-17 22:57:13 +020010from scapy.layers.l2 import Ether, ARP
11from scapy.layers.inet import IP, UDP, ICMP
snaramre5d4b8912019-12-13 23:39:35 +000012from scapy.contrib.geneve import GENEVE
Paul Vinciguerra2f156312020-05-02 22:34:40 -040013
14import util
Neale Ranns097fa662018-05-01 05:17:55 -070015from vpp_ip_route import VppIpRoute, VppRoutePath
16from vpp_ip import INVALID_INDEX
Marco Varleseb598f1d2017-09-19 14:25:28 +020017
18
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000019@unittest.skipIf("geneve" in config.excluded_plugins, "Exclude GENEVE plugin tests")
Marco Varleseb598f1d2017-09-19 14:25:28 +020020class TestGeneve(BridgeDomain, VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020021 """GENEVE Test Case"""
Marco Varleseb598f1d2017-09-19 14:25:28 +020022
23 def __init__(self, *args):
24 BridgeDomain.__init__(self)
25 VppTestCase.__init__(self, *args)
26
27 def encapsulate(self, pkt, vni):
Marco Varleseb598f1d2017-09-19 14:25:28 +020028 """
29 Encapsulate the original payload frame by adding GENEVE header with its
30 UDP, IP and Ethernet fields
31 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020032 return (
33 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 / GENEVE(vni=vni)
37 / pkt
38 )
Marco Varleseb598f1d2017-09-19 14:25:28 +020039
Eyal Baricef1e2a2018-06-18 13:01:59 +030040 def ip_range(self, start, end):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020041 """range of remote ip's"""
Eyal Baricef1e2a2018-06-18 13:01:59 +030042 return ip4_range(self.pg0.remote_ip4, start, end)
43
Marco Varleseb598f1d2017-09-19 14:25:28 +020044 def encap_mcast(self, pkt, src_ip, src_mac, vni):
45 """
46 Encapsulate the original payload frame by adding GENEVE header with its
47 UDP, IP and Ethernet fields
48 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020049 return (
50 Ether(src=src_mac, dst=self.mcast_mac)
51 / IP(src=src_ip, dst=self.mcast_ip4)
52 / UDP(sport=self.dport, dport=self.dport, chksum=0)
53 / GENEVE(vni=vni)
54 / pkt
55 )
Marco Varleseb598f1d2017-09-19 14:25:28 +020056
57 def decapsulate(self, pkt):
58 """
59 Decapsulate the original payload frame by removing GENEVE header
60 """
61 # check if is set I flag
62 # self.assertEqual(pkt[GENEVE].flags, int('0x8', 16))
63 return pkt[GENEVE].payload
64
65 # Method for checking GENEVE encapsulation.
66 #
67 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
68 # TODO: add error messages
69 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
70 # by VPP using ARP.
71 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
72 if not local_only:
73 if not mcast_pkt:
74 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
75 else:
76 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
77 # Verify GENEVE tunnel source IP is VPP_IP and destination IP is MY_IP.
78 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
79 if not local_only:
80 if not mcast_pkt:
81 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
82 else:
83 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
84 # Verify UDP destination port is GENEVE 4789, source UDP port could be
85 # arbitrary.
86 self.assertEqual(pkt[UDP].dport, type(self).dport)
87 # TODO: checksum check
88 # Verify VNI
89 self.assertEqual(pkt[GENEVE].vni, vni)
90
91 @classmethod
92 def create_geneve_flood_test_bd(cls, vni, n_ucast_tunnels):
93 # Create 10 ucast geneve tunnels under bd
94 ip_range_start = 10
95 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -070096 next_hop_address = cls.pg0.remote_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020097 for dest_ip4 in ip4_range(next_hop_address, ip_range_start, ip_range_end):
Jakub Grajciar2d3282e2019-10-01 12:04:56 +020098 # add host route so dest_ip4 will not be resolved
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020099 rip = VppIpRoute(
100 cls,
101 dest_ip4,
102 32,
103 [VppRoutePath(next_hop_address, INVALID_INDEX)],
104 register=False,
105 )
Neale Ranns097fa662018-05-01 05:17:55 -0700106 rip.add_vpp_config()
Marco Varleseb598f1d2017-09-19 14:25:28 +0200107 r = cls.vapi.geneve_add_del_tunnel(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200108 local_address=cls.pg0.local_ip4, remote_address=dest_ip4, vni=vni
109 )
110 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 +0200111
112 @classmethod
113 def add_del_shared_mcast_dst_load(cls, is_add):
114 """
115 add or del tunnels sharing the same mcast dst
116 to test geneve ref_count mechanism
117 """
Gabriel Ganne97cabc92018-02-08 11:22:33 +0100118 n_shared_dst_tunnels = 10
Marco Varleseb598f1d2017-09-19 14:25:28 +0200119 vni_start = 10000
120 vni_end = vni_start + n_shared_dst_tunnels
121 for vni in range(vni_start, vni_end):
122 r = cls.vapi.geneve_add_del_tunnel(
Jakub Grajciar2d3282e2019-10-01 12:04:56 +0200123 local_address=cls.pg0.local_ip4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200124 remote_address=cls.mcast_ip4,
125 mcast_sw_if_index=1,
126 is_add=is_add,
127 vni=vni,
128 )
129 if r.sw_if_index == 0xFFFFFFFF:
Paul Vinciguerrac599c6f2019-03-12 17:41:27 -0700130 raise ValueError("bad sw_if_index: ~0")
Marco Varleseb598f1d2017-09-19 14:25:28 +0200131
132 @classmethod
133 def add_shared_mcast_dst_load(cls):
134 cls.add_del_shared_mcast_dst_load(is_add=1)
135
136 @classmethod
137 def del_shared_mcast_dst_load(cls):
138 cls.add_del_shared_mcast_dst_load(is_add=0)
139
140 @classmethod
141 def add_del_mcast_tunnels_load(cls, is_add):
142 """
143 add or del tunnels to test geneve stability
144 """
Gabriel Ganne97cabc92018-02-08 11:22:33 +0100145 n_distinct_dst_tunnels = 10
Marco Varleseb598f1d2017-09-19 14:25:28 +0200146 ip_range_start = 10
147 ip_range_end = ip_range_start + n_distinct_dst_tunnels
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200148 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start, ip_range_end):
149 vni = int(dest_ip4.split(".")[3])
150 cls.vapi.geneve_add_del_tunnel(
151 local_address=cls.pg0.local_ip4,
152 remote_address=dest_ip4,
153 mcast_sw_if_index=1,
154 is_add=is_add,
155 vni=vni,
156 )
Marco Varleseb598f1d2017-09-19 14:25:28 +0200157
158 @classmethod
159 def add_mcast_tunnels_load(cls):
160 cls.add_del_mcast_tunnels_load(is_add=1)
161
162 @classmethod
163 def del_mcast_tunnels_load(cls):
164 cls.add_del_mcast_tunnels_load(is_add=0)
165
166 # Class method to start the GENEVE test case.
167 # Overrides setUpClass method in VppTestCase class.
168 # Python try..except statement is used to ensure that the tear down of
169 # the class will be executed even if exception is raised.
170 # @param cls The class pointer.
171 @classmethod
172 def setUpClass(cls):
173 super(TestGeneve, cls).setUpClass()
174
175 try:
176 cls.dport = 6081
177
178 # Create 2 pg interfaces.
179 cls.create_pg_interfaces(range(4))
180 for pg in cls.pg_interfaces:
181 pg.admin_up()
182
183 # Configure IPv4 addresses on VPP pg0.
184 cls.pg0.config_ip4()
185
186 # Resolve MAC address for VPP's IP address on pg0.
187 cls.pg0.resolve_arp()
188
189 # Our Multicast address
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200190 cls.mcast_ip4 = "239.1.1.1"
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400191 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200192
193 # Create GENEVE VTEP on VPP pg0, and put geneve_tunnel0 and pg1
194 # into BD.
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200195 cls.single_tunnel_vni = 0xABCDE
Marco Varleseb598f1d2017-09-19 14:25:28 +0200196 cls.single_tunnel_bd = 1
197 r = cls.vapi.geneve_add_del_tunnel(
Jakub Grajciar2d3282e2019-10-01 12:04:56 +0200198 local_address=cls.pg0.local_ip4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200199 remote_address=cls.pg0.remote_ip4,
200 vni=cls.single_tunnel_vni,
201 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100202 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200203 rx_sw_if_index=r.sw_if_index, bd_id=cls.single_tunnel_bd
204 )
205 cls.vapi.sw_interface_set_l2_bridge(
206 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd
207 )
Marco Varleseb598f1d2017-09-19 14:25:28 +0200208
209 # Setup vni 2 to test multicast flooding
210 cls.n_ucast_tunnels = 10
211 cls.mcast_flood_bd = 2
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200212 cls.create_geneve_flood_test_bd(cls.mcast_flood_bd, cls.n_ucast_tunnels)
Marco Varleseb598f1d2017-09-19 14:25:28 +0200213 r = cls.vapi.geneve_add_del_tunnel(
Jakub Grajciar2d3282e2019-10-01 12:04:56 +0200214 local_address=cls.pg0.local_ip4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200215 remote_address=cls.mcast_ip4,
216 mcast_sw_if_index=1,
217 vni=cls.mcast_flood_bd,
218 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100219 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200220 rx_sw_if_index=r.sw_if_index, bd_id=cls.mcast_flood_bd
221 )
222 cls.vapi.sw_interface_set_l2_bridge(
223 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd
224 )
Marco Varleseb598f1d2017-09-19 14:25:28 +0200225
226 # Add and delete mcast tunnels to check stability
227 cls.add_shared_mcast_dst_load()
228 cls.add_mcast_tunnels_load()
229 cls.del_shared_mcast_dst_load()
230 cls.del_mcast_tunnels_load()
231
232 # Setup vni 3 to test unicast flooding
233 cls.ucast_flood_bd = 3
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200234 cls.create_geneve_flood_test_bd(cls.ucast_flood_bd, cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100235 cls.vapi.sw_interface_set_l2_bridge(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200236 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd
237 )
Marco Varleseb598f1d2017-09-19 14:25:28 +0200238 except Exception:
239 super(TestGeneve, cls).tearDownClass()
240 raise
241
242 # Method to define VPP actions before tear down of the test case.
243 # Overrides tearDown method in VppTestCase class.
244 # @param self The object pointer.
245 def tearDown(self):
246 super(TestGeneve, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700247
248 def show_commands_at_teardown(self):
249 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
250 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
251 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
252 self.logger.info(self.vapi.cli("show geneve tunnel"))
Marco Varleseb598f1d2017-09-19 14:25:28 +0200253
254
Dmitry Valter34fa0ce2024-03-11 10:38:46 +0000255@unittest.skipIf("geneve" in config.excluded_plugins, "Exclude GENEVE plugin tests")
Ole Troan7fc88cf2020-06-17 22:57:13 +0200256class TestGeneveL3(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200257 """GENEVE L3 Test Case"""
Ole Troan7fc88cf2020-06-17 22:57:13 +0200258
259 @classmethod
260 def setUpClass(cls):
261 super(TestGeneveL3, cls).setUpClass()
262 try:
263 cls.create_pg_interfaces(range(2))
264 cls.interfaces = list(cls.pg_interfaces)
265
266 for i in cls.interfaces:
267 i.admin_up()
268 i.config_ip4()
269 i.resolve_arp()
270 except Exception:
271 super(TestGeneveL3, cls).tearDownClass()
272 raise
273
274 @classmethod
275 def tearDownClass(cls):
276 super(TestGeneveL3, cls).tearDownClass()
277
278 def tearDown(self):
279 super(TestGeneveL3, self).tearDown()
280
281 def show_commands_at_teardown(self):
282 self.logger.info(self.vapi.cli("show geneve tunnel"))
283 self.logger.info(self.vapi.cli("show ip neighbor"))
284
285 def test_l3_packet(self):
286 vni = 1234
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200287 r = self.vapi.add_node_next(
288 node_name="geneve4-input", next_name="ethernet-input"
289 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200290 r = self.vapi.geneve_add_del_tunnel2(
291 is_add=1,
292 local_address=self.pg0.local_ip4,
293 remote_address=self.pg0.remote_ip4,
294 vni=vni,
295 l3_mode=1,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200296 decap_next_index=r.next_index,
297 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200298
299 self.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200300 sw_if_index=r.sw_if_index, prefix="10.0.0.1/24"
301 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200302
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200303 pkt = (
304 Ether(src=self.pg0.remote_mac, dst="d0:0b:ee:d0:00:00")
305 / IP(src="10.0.0.2", dst="10.0.0.1")
306 / ICMP()
307 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200308
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200309 encap = (
310 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
311 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
312 / UDP(sport=6081, dport=6081, chksum=0)
313 / GENEVE(vni=vni)
314 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200315
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200316 arp = Ether(src=self.pg0.remote_mac, dst="d0:0b:ee:d0:00:00") / ARP(
317 op="is-at",
318 hwsrc=self.pg0.remote_mac,
319 hwdst="d0:0b:ee:d0:00:00",
320 psrc="10.0.0.2",
321 pdst="10.0.0.1",
322 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200323
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200324 rx = self.send_and_expect(self.pg0, encap / pkt * 1, self.pg0)
325 rx = self.send_and_assert_no_replies(self.pg0, encap / arp * 1, self.pg0)
326 rx = self.send_and_expect(self.pg0, encap / pkt * 1, self.pg0)
Ole Troan7fc88cf2020-06-17 22:57:13 +0200327 self.assertEqual(rx[0][ICMP].type, 0) # echo reply
328
329 r = self.vapi.geneve_add_del_tunnel2(
330 is_add=0,
331 local_address=self.pg0.local_ip4,
332 remote_address=self.pg0.remote_ip4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200333 vni=vni,
334 )
Ole Troan7fc88cf2020-06-17 22:57:13 +0200335
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200336
337if __name__ == "__main__":
Marco Varleseb598f1d2017-09-19 14:25:28 +0200338 unittest.main(testRunner=VppTestRunner)