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:
62 super(TestSVS, self).tearDown()
65 """ Source VRF Select IP4 """
68 # packets destined out of the 3 non-default table interfaces
70 pkts_0 = [(Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
71 IP(src="1.1.1.1", dst=self.pg1.remote_ip4) /
72 UDP(sport=1234, dport=1234) /
74 (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
75 IP(src="2.2.2.2", dst=self.pg2.remote_ip4) /
76 UDP(sport=1234, dport=1234) /
78 (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
79 IP(src="3.3.3.3", dst=self.pg3.remote_ip4) /
80 UDP(sport=1234, dport=1234) /
82 pkts_1 = [(Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
83 IP(src="1.1.1.1", dst=self.pg1.remote_ip4) /
84 UDP(sport=1234, dport=1234) /
86 (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
87 IP(src="2.2.2.2", dst=self.pg2.remote_ip4) /
88 UDP(sport=1234, dport=1234) /
90 (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
91 IP(src="3.3.3.3", dst=self.pg3.remote_ip4) /
92 UDP(sport=1234, dport=1234) /
96 # before adding the SVS config all these packets are dropped when
97 # ingressing on pg0 since pg0 is in the default table
100 self.send_and_assert_no_replies(self.pg0, p * 1)
103 # Add table 1001 & 1002 into which we'll add the routes
104 # determining the source VRF selection
106 table_ids = [101, 102]
108 for table_id in table_ids:
109 self.vapi.svs_table_add_del(
110 VppEnum.vl_api_address_family_t.ADDRESS_IP4, table_id)
113 # map X.0.0.0/8 to each SVS table for lookup in table X
115 for i in range(1, 4):
116 self.vapi.svs_route_add_del(
117 table_id, "%d.0.0.0/8" % i, i)
120 # Enable SVS on pg0/pg1 using table 1001/1002
122 self.vapi.svs_enable_disable(
123 VppEnum.vl_api_address_family_t.ADDRESS_IP4, table_ids[0],
124 self.pg0.sw_if_index)
125 self.vapi.svs_enable_disable(
126 VppEnum.vl_api_address_family_t.ADDRESS_IP4, table_ids[1],
127 self.pg1.sw_if_index)
130 # now all the packets should be delivered out the respective interface
132 self.send_and_expect(self.pg0, pkts_0[0] * NUM_PKTS, self.pg1)
133 self.send_and_expect(self.pg0, pkts_0[1] * NUM_PKTS, self.pg2)
134 self.send_and_expect(self.pg0, pkts_0[2] * NUM_PKTS, self.pg3)
135 self.send_and_expect(self.pg1, pkts_1[0] * NUM_PKTS, self.pg1)
136 self.send_and_expect(self.pg1, pkts_1[1] * NUM_PKTS, self.pg2)
137 self.send_and_expect(self.pg1, pkts_1[2] * NUM_PKTS, self.pg3)
140 # check that if the SVS lookup does not match a route the packet
141 # is forwarded using the interface's routing table
143 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
144 IP(src=self.pg0.remote_ip4, dst=self.pg0.remote_ip4) /
145 UDP(sport=1234, dport=1234) /
147 self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg0)
149 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
150 IP(src=self.pg1.remote_ip4, dst=self.pg1.remote_ip4) /
151 UDP(sport=1234, dport=1234) /
153 self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg1)
156 # dump the SVS configs
158 ss = self.vapi.svs_dump()
160 self.assertEqual(ss[0].table_id, table_ids[0])
161 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
162 self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
163 self.assertEqual(ss[1].table_id, table_ids[1])
164 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
165 self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
170 self.vapi.svs_enable_disable(
171 VppEnum.vl_api_address_family_t.ADDRESS_IP4,
173 self.pg0.sw_if_index,
175 self.vapi.svs_enable_disable(
176 VppEnum.vl_api_address_family_t.ADDRESS_IP4,
178 self.pg1.sw_if_index,
181 for table_id in table_ids:
182 for i in range(1, 4):
183 self.vapi.svs_route_add_del(
184 table_id, "%d.0.0.0/8" % i,
186 self.vapi.svs_table_add_del(
187 VppEnum.vl_api_address_family_t.ADDRESS_IP4,
192 """ Source VRF Select IP6 """
195 # packets destined out of the 3 non-default table interfaces
197 pkts_0 = [(Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
198 IPv6(src="2001:1::1", dst=self.pg1.remote_ip6) /
199 UDP(sport=1234, dport=1234) /
201 (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
202 IPv6(src="2001:2::1", dst=self.pg2.remote_ip6) /
203 UDP(sport=1234, dport=1234) /
205 (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
206 IPv6(src="2001:3::1", dst=self.pg3.remote_ip6) /
207 UDP(sport=1234, dport=1234) /
209 pkts_1 = [(Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
210 IPv6(src="2001:1::1", dst=self.pg1.remote_ip6) /
211 UDP(sport=1234, dport=1234) /
213 (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
214 IPv6(src="2001:2::1", dst=self.pg2.remote_ip6) /
215 UDP(sport=1234, dport=1234) /
217 (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
218 IPv6(src="2001:3::1", dst=self.pg3.remote_ip6) /
219 UDP(sport=1234, dport=1234) /
223 # before adding the SVS config all these packets are dropped when
224 # ingressing on pg0 since pg0 is in the default table
227 self.send_and_assert_no_replies(self.pg0, p * 1)
230 # Add table 1001 & 1002 into which we'll add the routes
231 # determining the source VRF selection
233 table_ids = [101, 102]
235 for table_id in table_ids:
236 self.vapi.svs_table_add_del(
237 VppEnum.vl_api_address_family_t.ADDRESS_IP6, table_id)
240 # map X.0.0.0/8 to each SVS table for lookup in table X
242 for i in range(1, 4):
243 self.vapi.svs_route_add_del(
244 table_id, "2001:%d::/32" % i,
248 # Enable SVS on pg0/pg1 using table 1001/1002
250 self.vapi.svs_enable_disable(
251 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
253 self.pg0.sw_if_index)
254 self.vapi.svs_enable_disable(
255 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
257 self.pg1.sw_if_index)
260 # now all the packets should be delivered out the respective interface
262 self.send_and_expect(self.pg0, pkts_0[0] * NUM_PKTS, self.pg1)
263 self.send_and_expect(self.pg0, pkts_0[1] * NUM_PKTS, self.pg2)
264 self.send_and_expect(self.pg0, pkts_0[2] * NUM_PKTS, self.pg3)
265 self.send_and_expect(self.pg1, pkts_1[0] * NUM_PKTS, self.pg1)
266 self.send_and_expect(self.pg1, pkts_1[1] * NUM_PKTS, self.pg2)
267 self.send_and_expect(self.pg1, pkts_1[2] * NUM_PKTS, self.pg3)
270 # check that if the SVS lookup does not match a route the packet
271 # is forwarded using the interface's routing table
273 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
274 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6) /
275 UDP(sport=1234, dport=1234) /
277 self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg0)
279 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
280 IPv6(src=self.pg1.remote_ip6, dst=self.pg1.remote_ip6) /
281 UDP(sport=1234, dport=1234) /
283 self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg1)
286 # dump the SVS configs
288 ss = self.vapi.svs_dump()
290 self.assertEqual(ss[0].table_id, table_ids[0])
291 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
292 self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
293 self.assertEqual(ss[1].table_id, table_ids[1])
294 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
295 self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
300 self.vapi.svs_enable_disable(
301 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
303 self.pg0.sw_if_index,
305 self.vapi.svs_enable_disable(
306 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
308 self.pg1.sw_if_index,
310 for table_id in table_ids:
311 for i in range(1, 4):
312 self.vapi.svs_route_add_del(
313 table_id, "2001:%d::/32" % i,
315 self.vapi.svs_table_add_del(
316 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
321 if __name__ == '__main__':
322 unittest.main(testRunner=VppTestRunner)