5 from framework import VppTestCase
6 from asfframework import VppTestRunner
7 from vpp_ip_route import VppIpTable
9 from scapy.packet import Raw
10 from scapy.layers.l2 import Ether
11 from scapy.layers.inet import IP, UDP
12 from scapy.layers.inet6 import IPv6
14 from vpp_papi import VppEnum
19 class TestSVS(VppTestCase):
24 super(TestSVS, cls).setUpClass()
27 def tearDownClass(cls):
28 super(TestSVS, cls).tearDownClass()
31 super(TestSVS, self).setUp()
33 # create 2 pg interfaces
34 self.create_pg_interfaces(range(4))
38 for i in self.pg_interfaces:
42 tbl = VppIpTable(self, table_id)
44 tbl = VppIpTable(self, table_id, is_ip6=1)
47 i.set_table_ip4(table_id)
48 i.set_table_ip6(table_id)
56 for i in self.pg_interfaces:
62 super(TestSVS, self).tearDown()
65 """Source VRF Select IP4"""
68 # packets destined out of the 3 non-default table interfaces
72 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
73 / IP(src="1.1.1.1", dst=self.pg1.remote_ip4)
74 / UDP(sport=1234, dport=1234)
78 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
79 / IP(src="2.2.2.2", dst=self.pg2.remote_ip4)
80 / UDP(sport=1234, dport=1234)
84 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
85 / IP(src="3.3.3.3", dst=self.pg3.remote_ip4)
86 / UDP(sport=1234, dport=1234)
92 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
93 / IP(src="1.1.1.1", dst=self.pg1.remote_ip4)
94 / UDP(sport=1234, dport=1234)
98 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
99 / IP(src="2.2.2.2", dst=self.pg2.remote_ip4)
100 / UDP(sport=1234, dport=1234)
104 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
105 / IP(src="3.3.3.3", dst=self.pg3.remote_ip4)
106 / UDP(sport=1234, dport=1234)
112 # before adding the SVS config all these packets are dropped when
113 # ingressing on pg0 since pg0 is in the default table
116 self.send_and_assert_no_replies(self.pg0, p * 1)
119 # Add table 1001 & 1002 into which we'll add the routes
120 # determining the source VRF selection
122 table_ids = [101, 102]
124 for table_id in table_ids:
125 self.vapi.svs_table_add_del(
127 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
132 # map X.0.0.0/8 to each SVS table for lookup in table X
134 for i in range(1, 4):
135 self.vapi.svs_route_add_del(
137 prefix="%d.0.0.0/8" % i,
143 # Enable SVS on pg0/pg1 using table 1001/1002
145 self.vapi.svs_enable_disable(
147 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
148 table_id=table_ids[0],
149 sw_if_index=self.pg0.sw_if_index,
151 self.vapi.svs_enable_disable(
153 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
154 table_id=table_ids[1],
155 sw_if_index=self.pg1.sw_if_index,
159 # now all the packets should be delivered out the respective interface
161 self.send_and_expect(self.pg0, pkts_0[0] * NUM_PKTS, self.pg1)
162 self.send_and_expect(self.pg0, pkts_0[1] * NUM_PKTS, self.pg2)
163 self.send_and_expect(self.pg0, pkts_0[2] * NUM_PKTS, self.pg3)
164 self.send_and_expect(self.pg1, pkts_1[0] * NUM_PKTS, self.pg1)
165 self.send_and_expect(self.pg1, pkts_1[1] * NUM_PKTS, self.pg2)
166 self.send_and_expect(self.pg1, pkts_1[2] * NUM_PKTS, self.pg3)
169 # check that if the SVS lookup does not match a route the packet
170 # is forwarded using the interface's routing table
173 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
174 / IP(src=self.pg0.remote_ip4, dst=self.pg0.remote_ip4)
175 / UDP(sport=1234, dport=1234)
178 self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg0)
181 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
182 / IP(src=self.pg1.remote_ip4, dst=self.pg1.remote_ip4)
183 / UDP(sport=1234, dport=1234)
186 self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg1)
189 # dump the SVS configs
191 ss = self.vapi.svs_dump()
193 self.assertEqual(ss[0].table_id, table_ids[0])
194 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
195 self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
196 self.assertEqual(ss[1].table_id, table_ids[1])
197 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
198 self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
203 self.vapi.svs_enable_disable(
205 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
206 table_id=table_ids[0],
207 sw_if_index=self.pg0.sw_if_index,
209 self.vapi.svs_enable_disable(
211 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
212 table_id=table_ids[1],
213 sw_if_index=self.pg1.sw_if_index,
216 for table_id in table_ids:
217 for i in range(1, 4):
218 self.vapi.svs_route_add_del(
220 prefix="%d.0.0.0/8" % i,
225 self.vapi.svs_table_add_del(
227 af=VppEnum.vl_api_address_family_t.ADDRESS_IP4,
232 """Source VRF Select IP6"""
235 # packets destined out of the 3 non-default table interfaces
239 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
240 / IPv6(src="2001:1::1", dst=self.pg1.remote_ip6)
241 / UDP(sport=1234, dport=1234)
245 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
246 / IPv6(src="2001:2::1", dst=self.pg2.remote_ip6)
247 / UDP(sport=1234, dport=1234)
251 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
252 / IPv6(src="2001:3::1", dst=self.pg3.remote_ip6)
253 / UDP(sport=1234, dport=1234)
259 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
260 / IPv6(src="2001:1::1", dst=self.pg1.remote_ip6)
261 / UDP(sport=1234, dport=1234)
265 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
266 / IPv6(src="2001:2::1", dst=self.pg2.remote_ip6)
267 / UDP(sport=1234, dport=1234)
271 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
272 / IPv6(src="2001:3::1", dst=self.pg3.remote_ip6)
273 / UDP(sport=1234, dport=1234)
279 # before adding the SVS config all these packets are dropped when
280 # ingressing on pg0 since pg0 is in the default table
283 self.send_and_assert_no_replies(self.pg0, p * 1)
286 # Add table 1001 & 1002 into which we'll add the routes
287 # determining the source VRF selection
289 table_ids = [101, 102]
291 for table_id in table_ids:
292 self.vapi.svs_table_add_del(
294 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
299 # map X.0.0.0/8 to each SVS table for lookup in table X
301 for i in range(1, 4):
302 self.vapi.svs_route_add_del(
304 prefix="2001:%d::/32" % i,
310 # Enable SVS on pg0/pg1 using table 1001/1002
312 self.vapi.svs_enable_disable(
314 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
315 table_id=table_ids[0],
316 sw_if_index=self.pg0.sw_if_index,
318 self.vapi.svs_enable_disable(
320 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
321 table_id=table_ids[1],
322 sw_if_index=self.pg1.sw_if_index,
326 # now all the packets should be delivered out the respective interface
328 self.send_and_expect(self.pg0, pkts_0[0] * NUM_PKTS, self.pg1)
329 self.send_and_expect(self.pg0, pkts_0[1] * NUM_PKTS, self.pg2)
330 self.send_and_expect(self.pg0, pkts_0[2] * NUM_PKTS, self.pg3)
331 self.send_and_expect(self.pg1, pkts_1[0] * NUM_PKTS, self.pg1)
332 self.send_and_expect(self.pg1, pkts_1[1] * NUM_PKTS, self.pg2)
333 self.send_and_expect(self.pg1, pkts_1[2] * NUM_PKTS, self.pg3)
336 # check that if the SVS lookup does not match a route the packet
337 # is forwarded using the interface's routing table
340 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
341 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6)
342 / UDP(sport=1234, dport=1234)
345 self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg0)
348 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
349 / IPv6(src=self.pg1.remote_ip6, dst=self.pg1.remote_ip6)
350 / UDP(sport=1234, dport=1234)
353 self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg1)
356 # dump the SVS configs
358 ss = self.vapi.svs_dump()
360 self.assertEqual(ss[0].table_id, table_ids[0])
361 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
362 self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
363 self.assertEqual(ss[1].table_id, table_ids[1])
364 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
365 self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
370 self.vapi.svs_enable_disable(
372 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
373 table_id=table_ids[0],
374 sw_if_index=self.pg0.sw_if_index,
376 self.vapi.svs_enable_disable(
378 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
379 table_id=table_ids[1],
380 sw_if_index=self.pg1.sw_if_index,
383 for table_id in table_ids:
384 for i in range(1, 4):
385 self.vapi.svs_route_add_del(
387 prefix="2001:%d::/32" % i,
392 self.vapi.svs_table_add_del(
394 af=VppEnum.vl_api_address_family_t.ADDRESS_IP6,
399 if __name__ == "__main__":
400 unittest.main(testRunner=VppTestRunner)