5 from framework import VppTestCase, VppTestRunner
6 from vpp_ip_route import VppIpTable
8 from scapy.packet import Raw
9 from scapy.layers.l2 import Ether
10 from scapy.layers.inet import IP, UDP, ICMP
11 from scapy.layers.inet6 import IPv6
13 from vpp_papi import VppEnum
18 class TestSVS(VppTestCase):
23 super(TestSVS, cls).setUpClass()
26 def tearDownClass(cls):
27 super(TestSVS, cls).tearDownClass()
30 super(TestSVS, self).setUp()
32 # create 2 pg interfaces
33 self.create_pg_interfaces(range(4))
37 for i in self.pg_interfaces:
41 tbl = VppIpTable(self, table_id)
43 tbl = VppIpTable(self, table_id, is_ip6=1)
46 i.set_table_ip4(table_id)
47 i.set_table_ip6(table_id)
55 for i in self.pg_interfaces:
61 super(TestSVS, self).tearDown()
64 """Source VRF Select IP4"""
67 # packets destined out of the 3 non-default table interfaces
71 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
72 / IP(src="1.1.1.1", dst=self.pg1.remote_ip4)
73 / UDP(sport=1234, dport=1234)
77 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
78 / IP(src="2.2.2.2", dst=self.pg2.remote_ip4)
79 / UDP(sport=1234, dport=1234)
83 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
84 / IP(src="3.3.3.3", dst=self.pg3.remote_ip4)
85 / UDP(sport=1234, dport=1234)
91 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
92 / IP(src="1.1.1.1", dst=self.pg1.remote_ip4)
93 / UDP(sport=1234, dport=1234)
97 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
98 / IP(src="2.2.2.2", dst=self.pg2.remote_ip4)
99 / UDP(sport=1234, dport=1234)
103 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
104 / IP(src="3.3.3.3", dst=self.pg3.remote_ip4)
105 / UDP(sport=1234, dport=1234)
111 # before adding the SVS config all these packets are dropped when
112 # ingressing on pg0 since pg0 is in the default table
115 self.send_and_assert_no_replies(self.pg0, p * 1)
118 # Add table 1001 & 1002 into which we'll add the routes
119 # determining the source VRF selection
121 table_ids = [101, 102]
123 for table_id in table_ids:
124 self.vapi.svs_table_add_del(
126 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
131 # map X.0.0.0/8 to each SVS table for lookup in table X
133 for i in range(1, 4):
134 self.vapi.svs_route_add_del(
136 prefix="%d.0.0.0/8" % i,
142 # Enable SVS on pg0/pg1 using table 1001/1002
144 self.vapi.svs_enable_disable(
146 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
147 table_id=table_ids[0],
148 sw_if_index=self.pg0.sw_if_index,
150 self.vapi.svs_enable_disable(
152 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
153 table_id=table_ids[1],
154 sw_if_index=self.pg1.sw_if_index,
158 # now all the packets should be delivered out the respective interface
160 self.send_and_expect(self.pg0, pkts_0[0] * NUM_PKTS, self.pg1)
161 self.send_and_expect(self.pg0, pkts_0[1] * NUM_PKTS, self.pg2)
162 self.send_and_expect(self.pg0, pkts_0[2] * NUM_PKTS, self.pg3)
163 self.send_and_expect(self.pg1, pkts_1[0] * NUM_PKTS, self.pg1)
164 self.send_and_expect(self.pg1, pkts_1[1] * NUM_PKTS, self.pg2)
165 self.send_and_expect(self.pg1, pkts_1[2] * NUM_PKTS, self.pg3)
168 # check that if the SVS lookup does not match a route the packet
169 # is forwarded using the interface's routing table
172 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
173 / IP(src=self.pg0.remote_ip4, dst=self.pg0.remote_ip4)
174 / UDP(sport=1234, dport=1234)
177 self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg0)
180 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
181 / IP(src=self.pg1.remote_ip4, dst=self.pg1.remote_ip4)
182 / UDP(sport=1234, dport=1234)
185 self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg1)
188 # dump the SVS configs
190 ss = self.vapi.svs_dump()
192 self.assertEqual(ss[0].table_id, table_ids[0])
193 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
194 self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
195 self.assertEqual(ss[1].table_id, table_ids[1])
196 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
197 self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
202 self.vapi.svs_enable_disable(
204 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
205 table_id=table_ids[0],
206 sw_if_index=self.pg0.sw_if_index,
208 self.vapi.svs_enable_disable(
210 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
211 table_id=table_ids[1],
212 sw_if_index=self.pg1.sw_if_index,
215 for table_id in table_ids:
216 for i in range(1, 4):
217 self.vapi.svs_route_add_del(
219 prefix="%d.0.0.0/8" % i,
224 self.vapi.svs_table_add_del(
226 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
231 """Source VRF Select IP6"""
234 # packets destined out of the 3 non-default table interfaces
238 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
239 / IPv6(src="2001:1::1", dst=self.pg1.remote_ip6)
240 / UDP(sport=1234, dport=1234)
244 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
245 / IPv6(src="2001:2::1", dst=self.pg2.remote_ip6)
246 / UDP(sport=1234, dport=1234)
250 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
251 / IPv6(src="2001:3::1", dst=self.pg3.remote_ip6)
252 / UDP(sport=1234, dport=1234)
258 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
259 / IPv6(src="2001:1::1", dst=self.pg1.remote_ip6)
260 / UDP(sport=1234, dport=1234)
264 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
265 / IPv6(src="2001:2::1", dst=self.pg2.remote_ip6)
266 / UDP(sport=1234, dport=1234)
270 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
271 / IPv6(src="2001:3::1", dst=self.pg3.remote_ip6)
272 / UDP(sport=1234, dport=1234)
278 # before adding the SVS config all these packets are dropped when
279 # ingressing on pg0 since pg0 is in the default table
282 self.send_and_assert_no_replies(self.pg0, p * 1)
285 # Add table 1001 & 1002 into which we'll add the routes
286 # determining the source VRF selection
288 table_ids = [101, 102]
290 for table_id in table_ids:
291 self.vapi.svs_table_add_del(
293 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
298 # map X.0.0.0/8 to each SVS table for lookup in table X
300 for i in range(1, 4):
301 self.vapi.svs_route_add_del(
303 prefix="2001:%d::/32" % i,
309 # Enable SVS on pg0/pg1 using table 1001/1002
311 self.vapi.svs_enable_disable(
313 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
314 table_id=table_ids[0],
315 sw_if_index=self.pg0.sw_if_index,
317 self.vapi.svs_enable_disable(
319 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
320 table_id=table_ids[1],
321 sw_if_index=self.pg1.sw_if_index,
325 # now all the packets should be delivered out the respective interface
327 self.send_and_expect(self.pg0, pkts_0[0] * NUM_PKTS, self.pg1)
328 self.send_and_expect(self.pg0, pkts_0[1] * NUM_PKTS, self.pg2)
329 self.send_and_expect(self.pg0, pkts_0[2] * NUM_PKTS, self.pg3)
330 self.send_and_expect(self.pg1, pkts_1[0] * NUM_PKTS, self.pg1)
331 self.send_and_expect(self.pg1, pkts_1[1] * NUM_PKTS, self.pg2)
332 self.send_and_expect(self.pg1, pkts_1[2] * NUM_PKTS, self.pg3)
335 # check that if the SVS lookup does not match a route the packet
336 # is forwarded using the interface's routing table
339 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
340 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6)
341 / UDP(sport=1234, dport=1234)
344 self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg0)
347 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
348 / IPv6(src=self.pg1.remote_ip6, dst=self.pg1.remote_ip6)
349 / UDP(sport=1234, dport=1234)
352 self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg1)
355 # dump the SVS configs
357 ss = self.vapi.svs_dump()
359 self.assertEqual(ss[0].table_id, table_ids[0])
360 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
361 self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
362 self.assertEqual(ss[1].table_id, table_ids[1])
363 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
364 self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
369 self.vapi.svs_enable_disable(
371 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
372 table_id=table_ids[0],
373 sw_if_index=self.pg0.sw_if_index,
375 self.vapi.svs_enable_disable(
377 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
378 table_id=table_ids[1],
379 sw_if_index=self.pg1.sw_if_index,
382 for table_id in table_ids:
383 for i in range(1, 4):
384 self.vapi.svs_route_add_del(
386 prefix="2001:%d::/32" % i,
391 self.vapi.svs_table_add_del(
393 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
398 if __name__ == "__main__":
399 unittest.main(testRunner=VppTestRunner)