blob: 01173a201b7e057152352748d74df980f8fcb513 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Neale Rannsccc70f62018-10-02 07:28:16 -07002
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07003import unittest
4
Dave Wallace8800f732023-08-31 00:47:44 -04005from framework import VppTestCase
6from asfframework import VppTestRunner
Neale Rannsccc70f62018-10-02 07:28:16 -07007from vpp_ip_route import VppIpTable
8
9from scapy.packet import Raw
10from scapy.layers.l2 import Ether
Dave Wallace8800f732023-08-31 00:47:44 -040011from scapy.layers.inet import IP, UDP
Neale Rannsccc70f62018-10-02 07:28:16 -070012from scapy.layers.inet6 import IPv6
13
Ole Troan0685da42018-10-16 14:42:50 +020014from vpp_papi import VppEnum
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000015from config import config
Ole Troan0685da42018-10-16 14:42:50 +020016
Paul Vinciguerra4271c972019-05-14 13:25:49 -040017NUM_PKTS = 67
18
Neale Rannsccc70f62018-10-02 07:28:16 -070019
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000020@unittest.skipIf("svs" in config.excluded_plugins, "Exclude SVS plugin tests")
Neale Rannsccc70f62018-10-02 07:28:16 -070021class TestSVS(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020022 """SVS Test Case"""
Neale Rannsccc70f62018-10-02 07:28:16 -070023
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070024 @classmethod
25 def setUpClass(cls):
26 super(TestSVS, cls).setUpClass()
27
28 @classmethod
29 def tearDownClass(cls):
30 super(TestSVS, cls).tearDownClass()
31
Neale Rannsccc70f62018-10-02 07:28:16 -070032 def setUp(self):
33 super(TestSVS, self).setUp()
34
35 # create 2 pg interfaces
36 self.create_pg_interfaces(range(4))
37
38 table_id = 0
39
40 for i in self.pg_interfaces:
41 i.admin_up()
42
43 if table_id != 0:
44 tbl = VppIpTable(self, table_id)
45 tbl.add_vpp_config()
46 tbl = VppIpTable(self, table_id, is_ip6=1)
47 tbl.add_vpp_config()
48
49 i.set_table_ip4(table_id)
50 i.set_table_ip6(table_id)
51 i.config_ip4()
52 i.resolve_arp()
53 i.config_ip6()
54 i.resolve_ndp()
55 table_id += 1
56
57 def tearDown(self):
58 for i in self.pg_interfaces:
59 i.unconfig_ip4()
60 i.unconfig_ip6()
Neale Rannsccc70f62018-10-02 07:28:16 -070061 i.set_table_ip4(0)
62 i.set_table_ip6(0)
63 i.admin_down()
64 super(TestSVS, self).tearDown()
65
66 def test_svs4(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020067 """Source VRF Select IP4"""
Neale Rannsccc70f62018-10-02 07:28:16 -070068
69 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070070 # packets destined out of the 3 non-default table interfaces
Neale Rannsccc70f62018-10-02 07:28:16 -070071 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020072 pkts_0 = [
73 (
74 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
75 / IP(src="1.1.1.1", dst=self.pg1.remote_ip4)
76 / UDP(sport=1234, dport=1234)
77 / Raw(b"\xa5" * 100)
78 ),
79 (
80 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
81 / IP(src="2.2.2.2", dst=self.pg2.remote_ip4)
82 / UDP(sport=1234, dport=1234)
83 / Raw(b"\xa5" * 100)
84 ),
85 (
86 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
87 / IP(src="3.3.3.3", dst=self.pg3.remote_ip4)
88 / UDP(sport=1234, dport=1234)
89 / Raw(b"\xa5" * 100)
90 ),
91 ]
92 pkts_1 = [
93 (
94 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
95 / IP(src="1.1.1.1", dst=self.pg1.remote_ip4)
96 / UDP(sport=1234, dport=1234)
97 / Raw(b"\xa5" * 100)
98 ),
99 (
100 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
101 / IP(src="2.2.2.2", dst=self.pg2.remote_ip4)
102 / UDP(sport=1234, dport=1234)
103 / Raw(b"\xa5" * 100)
104 ),
105 (
106 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
107 / IP(src="3.3.3.3", dst=self.pg3.remote_ip4)
108 / UDP(sport=1234, dport=1234)
109 / Raw(b"\xa5" * 100)
110 ),
111 ]
Neale Rannsccc70f62018-10-02 07:28:16 -0700112
113 #
114 # before adding the SVS config all these packets are dropped when
115 # ingressing on pg0 since pg0 is in the default table
116 #
117 for p in pkts_0:
118 self.send_and_assert_no_replies(self.pg0, p * 1)
119
120 #
121 # Add table 1001 & 1002 into which we'll add the routes
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700122 # determining the source VRF selection
Neale Rannsccc70f62018-10-02 07:28:16 -0700123 #
124 table_ids = [101, 102]
125
126 for table_id in table_ids:
Ole Troan0685da42018-10-16 14:42:50 +0200127 self.vapi.svs_table_add_del(
Ole Troan5c2a2372020-11-19 16:01:23 +0100128 is_add=1,
129 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200130 table_id=table_id,
131 )
Neale Rannsccc70f62018-10-02 07:28:16 -0700132
133 #
134 # map X.0.0.0/8 to each SVS table for lookup in table X
135 #
136 for i in range(1, 4):
137 self.vapi.svs_route_add_del(
Ole Troan5c2a2372020-11-19 16:01:23 +0100138 is_add=1,
139 prefix="%d.0.0.0/8" % i,
140 table_id=table_id,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200141 source_table_id=i,
142 )
Neale Rannsccc70f62018-10-02 07:28:16 -0700143
144 #
145 # Enable SVS on pg0/pg1 using table 1001/1002
146 #
Ole Troan0685da42018-10-16 14:42:50 +0200147 self.vapi.svs_enable_disable(
Ole Troan5c2a2372020-11-19 16:01:23 +0100148 is_enable=1,
149 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
150 table_id=table_ids[0],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200151 sw_if_index=self.pg0.sw_if_index,
152 )
Ole Troan0685da42018-10-16 14:42:50 +0200153 self.vapi.svs_enable_disable(
Ole Troan5c2a2372020-11-19 16:01:23 +0100154 is_enable=1,
155 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
156 table_id=table_ids[1],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200157 sw_if_index=self.pg1.sw_if_index,
158 )
Neale Rannsccc70f62018-10-02 07:28:16 -0700159
160 #
161 # now all the packets should be delivered out the respective interface
162 #
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400163 self.send_and_expect(self.pg0, pkts_0[0] * NUM_PKTS, self.pg1)
164 self.send_and_expect(self.pg0, pkts_0[1] * NUM_PKTS, self.pg2)
165 self.send_and_expect(self.pg0, pkts_0[2] * NUM_PKTS, self.pg3)
166 self.send_and_expect(self.pg1, pkts_1[0] * NUM_PKTS, self.pg1)
167 self.send_and_expect(self.pg1, pkts_1[1] * NUM_PKTS, self.pg2)
168 self.send_and_expect(self.pg1, pkts_1[2] * NUM_PKTS, self.pg3)
Neale Rannsccc70f62018-10-02 07:28:16 -0700169
170 #
171 # check that if the SVS lookup does not match a route the packet
172 # is forwarded using the interface's routing table
173 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200174 p = (
175 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
176 / IP(src=self.pg0.remote_ip4, dst=self.pg0.remote_ip4)
177 / UDP(sport=1234, dport=1234)
178 / Raw(b"\xa5" * 100)
179 )
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400180 self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg0)
Neale Rannsccc70f62018-10-02 07:28:16 -0700181
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200182 p = (
183 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
184 / IP(src=self.pg1.remote_ip4, dst=self.pg1.remote_ip4)
185 / UDP(sport=1234, dport=1234)
186 / Raw(b"\xa5" * 100)
187 )
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400188 self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg1)
Neale Rannsccc70f62018-10-02 07:28:16 -0700189
190 #
191 # dump the SVS configs
192 #
193 ss = self.vapi.svs_dump()
194
195 self.assertEqual(ss[0].table_id, table_ids[0])
196 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
Ole Troan0685da42018-10-16 14:42:50 +0200197 self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
Neale Rannsccc70f62018-10-02 07:28:16 -0700198 self.assertEqual(ss[1].table_id, table_ids[1])
199 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
Ole Troan0685da42018-10-16 14:42:50 +0200200 self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
Neale Rannsccc70f62018-10-02 07:28:16 -0700201
202 #
203 # cleanup
204 #
Ole Troan0685da42018-10-16 14:42:50 +0200205 self.vapi.svs_enable_disable(
Ole Troan5c2a2372020-11-19 16:01:23 +0100206 is_enable=0,
207 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
208 table_id=table_ids[0],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200209 sw_if_index=self.pg0.sw_if_index,
210 )
Ole Troan0685da42018-10-16 14:42:50 +0200211 self.vapi.svs_enable_disable(
Ole Troan5c2a2372020-11-19 16:01:23 +0100212 is_enable=0,
213 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
214 table_id=table_ids[1],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200215 sw_if_index=self.pg1.sw_if_index,
216 )
Neale Rannsccc70f62018-10-02 07:28:16 -0700217
218 for table_id in table_ids:
219 for i in range(1, 4):
220 self.vapi.svs_route_add_del(
Ole Troan5c2a2372020-11-19 16:01:23 +0100221 is_add=0,
222 prefix="%d.0.0.0/8" % i,
223 table_id=table_id,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200224 source_table_id=0,
225 )
Ole Troan5c2a2372020-11-19 16:01:23 +0100226
Ole Troan0685da42018-10-16 14:42:50 +0200227 self.vapi.svs_table_add_del(
Ole Troan5c2a2372020-11-19 16:01:23 +0100228 is_add=0,
229 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200230 table_id=table_id,
231 )
Neale Rannsccc70f62018-10-02 07:28:16 -0700232
233 def test_svs6(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200234 """Source VRF Select IP6"""
Neale Rannsccc70f62018-10-02 07:28:16 -0700235
236 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700237 # packets destined out of the 3 non-default table interfaces
Neale Rannsccc70f62018-10-02 07:28:16 -0700238 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200239 pkts_0 = [
240 (
241 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
242 / IPv6(src="2001:1::1", dst=self.pg1.remote_ip6)
243 / UDP(sport=1234, dport=1234)
244 / Raw(b"\xa5" * 100)
245 ),
246 (
247 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
248 / IPv6(src="2001:2::1", dst=self.pg2.remote_ip6)
249 / UDP(sport=1234, dport=1234)
250 / Raw(b"\xa5" * 100)
251 ),
252 (
253 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
254 / IPv6(src="2001:3::1", dst=self.pg3.remote_ip6)
255 / UDP(sport=1234, dport=1234)
256 / Raw(b"\xa5" * 100)
257 ),
258 ]
259 pkts_1 = [
260 (
261 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
262 / IPv6(src="2001:1::1", dst=self.pg1.remote_ip6)
263 / UDP(sport=1234, dport=1234)
264 / Raw(b"\xa5" * 100)
265 ),
266 (
267 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
268 / IPv6(src="2001:2::1", dst=self.pg2.remote_ip6)
269 / UDP(sport=1234, dport=1234)
270 / Raw(b"\xa5" * 100)
271 ),
272 (
273 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
274 / IPv6(src="2001:3::1", dst=self.pg3.remote_ip6)
275 / UDP(sport=1234, dport=1234)
276 / Raw(b"\xa5" * 100)
277 ),
278 ]
Neale Rannsccc70f62018-10-02 07:28:16 -0700279
280 #
281 # before adding the SVS config all these packets are dropped when
282 # ingressing on pg0 since pg0 is in the default table
283 #
284 for p in pkts_0:
285 self.send_and_assert_no_replies(self.pg0, p * 1)
286
287 #
288 # Add table 1001 & 1002 into which we'll add the routes
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700289 # determining the source VRF selection
Neale Rannsccc70f62018-10-02 07:28:16 -0700290 #
291 table_ids = [101, 102]
292
293 for table_id in table_ids:
Ole Troan0685da42018-10-16 14:42:50 +0200294 self.vapi.svs_table_add_del(
Ole Troan5c2a2372020-11-19 16:01:23 +0100295 is_add=1,
296 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200297 table_id=table_id,
298 )
Neale Rannsccc70f62018-10-02 07:28:16 -0700299
300 #
301 # map X.0.0.0/8 to each SVS table for lookup in table X
302 #
303 for i in range(1, 4):
304 self.vapi.svs_route_add_del(
Ole Troan5c2a2372020-11-19 16:01:23 +0100305 is_add=1,
306 prefix="2001:%d::/32" % i,
307 table_id=table_id,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200308 source_table_id=i,
309 )
Neale Rannsccc70f62018-10-02 07:28:16 -0700310
311 #
312 # Enable SVS on pg0/pg1 using table 1001/1002
313 #
Ole Troan0685da42018-10-16 14:42:50 +0200314 self.vapi.svs_enable_disable(
Ole Troan5c2a2372020-11-19 16:01:23 +0100315 is_enable=1,
316 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
317 table_id=table_ids[0],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200318 sw_if_index=self.pg0.sw_if_index,
319 )
Ole Troan0685da42018-10-16 14:42:50 +0200320 self.vapi.svs_enable_disable(
Ole Troan5c2a2372020-11-19 16:01:23 +0100321 is_enable=1,
322 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
323 table_id=table_ids[1],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200324 sw_if_index=self.pg1.sw_if_index,
325 )
Neale Rannsccc70f62018-10-02 07:28:16 -0700326
327 #
328 # now all the packets should be delivered out the respective interface
329 #
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400330 self.send_and_expect(self.pg0, pkts_0[0] * NUM_PKTS, self.pg1)
331 self.send_and_expect(self.pg0, pkts_0[1] * NUM_PKTS, self.pg2)
332 self.send_and_expect(self.pg0, pkts_0[2] * NUM_PKTS, self.pg3)
333 self.send_and_expect(self.pg1, pkts_1[0] * NUM_PKTS, self.pg1)
334 self.send_and_expect(self.pg1, pkts_1[1] * NUM_PKTS, self.pg2)
335 self.send_and_expect(self.pg1, pkts_1[2] * NUM_PKTS, self.pg3)
Neale Rannsccc70f62018-10-02 07:28:16 -0700336
337 #
338 # check that if the SVS lookup does not match a route the packet
339 # is forwarded using the interface's routing table
340 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200341 p = (
342 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
343 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6)
344 / UDP(sport=1234, dport=1234)
345 / Raw(b"\xa5" * 100)
346 )
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400347 self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg0)
Neale Rannsccc70f62018-10-02 07:28:16 -0700348
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200349 p = (
350 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
351 / IPv6(src=self.pg1.remote_ip6, dst=self.pg1.remote_ip6)
352 / UDP(sport=1234, dport=1234)
353 / Raw(b"\xa5" * 100)
354 )
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400355 self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg1)
Neale Rannsccc70f62018-10-02 07:28:16 -0700356
357 #
358 # dump the SVS configs
359 #
360 ss = self.vapi.svs_dump()
361
362 self.assertEqual(ss[0].table_id, table_ids[0])
363 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
Ole Troan0685da42018-10-16 14:42:50 +0200364 self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
Neale Rannsccc70f62018-10-02 07:28:16 -0700365 self.assertEqual(ss[1].table_id, table_ids[1])
366 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
Ole Troan0685da42018-10-16 14:42:50 +0200367 self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
Neale Rannsccc70f62018-10-02 07:28:16 -0700368
369 #
370 # cleanup
371 #
Ole Troan0685da42018-10-16 14:42:50 +0200372 self.vapi.svs_enable_disable(
Ole Troan5c2a2372020-11-19 16:01:23 +0100373 is_enable=0,
374 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
375 table_id=table_ids[0],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200376 sw_if_index=self.pg0.sw_if_index,
377 )
Ole Troan0685da42018-10-16 14:42:50 +0200378 self.vapi.svs_enable_disable(
Ole Troan5c2a2372020-11-19 16:01:23 +0100379 is_enable=0,
380 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
381 table_id=table_ids[1],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200382 sw_if_index=self.pg1.sw_if_index,
383 )
Ole Troan5c2a2372020-11-19 16:01:23 +0100384
Neale Rannsccc70f62018-10-02 07:28:16 -0700385 for table_id in table_ids:
386 for i in range(1, 4):
387 self.vapi.svs_route_add_del(
Ole Troan5c2a2372020-11-19 16:01:23 +0100388 is_add=0,
389 prefix="2001:%d::/32" % i,
390 table_id=table_id,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200391 source_table_id=0,
392 )
Ole Troan0685da42018-10-16 14:42:50 +0200393
Ole Troan5c2a2372020-11-19 16:01:23 +0100394 self.vapi.svs_table_add_del(
395 is_add=0,
396 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200397 table_id=table_id,
398 )
Neale Rannsccc70f62018-10-02 07:28:16 -0700399
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200400
401if __name__ == "__main__":
Neale Rannsccc70f62018-10-02 07:28:16 -0700402 unittest.main(testRunner=VppTestRunner)