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
16 class TestSVS(VppTestCase):
20 super(TestSVS, self).setUp()
22 # create 2 pg interfaces
23 self.create_pg_interfaces(range(4))
27 for i in self.pg_interfaces:
31 tbl = VppIpTable(self, table_id)
33 tbl = VppIpTable(self, table_id, is_ip6=1)
36 i.set_table_ip4(table_id)
37 i.set_table_ip6(table_id)
45 for i in self.pg_interfaces:
52 super(TestSVS, self).tearDown()
55 """ Source VRF Select IP4 """
58 # packets destined out of the 3 non-default table interfaces
60 pkts_0 = [(Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
61 IP(src="1.1.1.1", dst=self.pg1.remote_ip4) /
62 UDP(sport=1234, dport=1234) /
64 (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
65 IP(src="2.2.2.2", dst=self.pg2.remote_ip4) /
66 UDP(sport=1234, dport=1234) /
68 (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
69 IP(src="3.3.3.3", dst=self.pg3.remote_ip4) /
70 UDP(sport=1234, dport=1234) /
72 pkts_1 = [(Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
73 IP(src="1.1.1.1", dst=self.pg1.remote_ip4) /
74 UDP(sport=1234, dport=1234) /
76 (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
77 IP(src="2.2.2.2", dst=self.pg2.remote_ip4) /
78 UDP(sport=1234, dport=1234) /
80 (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
81 IP(src="3.3.3.3", dst=self.pg3.remote_ip4) /
82 UDP(sport=1234, dport=1234) /
86 # before adding the SVS config all these packets are dropped when
87 # ingressing on pg0 since pg0 is in the default table
90 self.send_and_assert_no_replies(self.pg0, p * 1)
93 # Add table 1001 & 1002 into which we'll add the routes
94 # determining the source VRF selection
96 table_ids = [101, 102]
98 for table_id in table_ids:
99 self.vapi.svs_table_add_del(
100 VppEnum.vl_api_address_family_t.ADDRESS_IP4, table_id)
103 # map X.0.0.0/8 to each SVS table for lookup in table X
105 for i in range(1, 4):
106 self.vapi.svs_route_add_del(
107 table_id, "%d.0.0.0/8" % i, i)
110 # Enable SVS on pg0/pg1 using table 1001/1002
112 self.vapi.svs_enable_disable(
113 VppEnum.vl_api_address_family_t.ADDRESS_IP4, table_ids[0],
114 self.pg0.sw_if_index)
115 self.vapi.svs_enable_disable(
116 VppEnum.vl_api_address_family_t.ADDRESS_IP4, table_ids[1],
117 self.pg1.sw_if_index)
120 # now all the packets should be delivered out the respective interface
122 self.send_and_expect(self.pg0, pkts_0[0] * 65, self.pg1)
123 self.send_and_expect(self.pg0, pkts_0[1] * 65, self.pg2)
124 self.send_and_expect(self.pg0, pkts_0[2] * 65, self.pg3)
125 self.send_and_expect(self.pg1, pkts_1[0] * 65, self.pg1)
126 self.send_and_expect(self.pg1, pkts_1[1] * 65, self.pg2)
127 self.send_and_expect(self.pg1, pkts_1[2] * 65, self.pg3)
130 # check that if the SVS lookup does not match a route the packet
131 # is forwarded using the interface's routing table
133 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
134 IP(src=self.pg0.remote_ip4, dst=self.pg0.remote_ip4) /
135 UDP(sport=1234, dport=1234) /
137 self.send_and_expect(self.pg0, p * 65, self.pg0)
139 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
140 IP(src=self.pg1.remote_ip4, dst=self.pg1.remote_ip4) /
141 UDP(sport=1234, dport=1234) /
143 self.send_and_expect(self.pg1, p * 65, self.pg1)
146 # dump the SVS configs
148 ss = self.vapi.svs_dump()
150 self.assertEqual(ss[0].table_id, table_ids[0])
151 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
152 self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
153 self.assertEqual(ss[1].table_id, table_ids[1])
154 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
155 self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
160 self.vapi.svs_enable_disable(
161 VppEnum.vl_api_address_family_t.ADDRESS_IP4,
163 self.pg0.sw_if_index,
165 self.vapi.svs_enable_disable(
166 VppEnum.vl_api_address_family_t.ADDRESS_IP4,
168 self.pg1.sw_if_index,
171 for table_id in table_ids:
172 for i in range(1, 4):
173 self.vapi.svs_route_add_del(
174 table_id, "%d.0.0.0/8" % i,
176 self.vapi.svs_table_add_del(
177 VppEnum.vl_api_address_family_t.ADDRESS_IP4,
182 """ Source VRF Select IP6 """
185 # packets destined out of the 3 non-default table interfaces
187 pkts_0 = [(Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
188 IPv6(src="2001:1::1", dst=self.pg1.remote_ip6) /
189 UDP(sport=1234, dport=1234) /
191 (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
192 IPv6(src="2001:2::1", dst=self.pg2.remote_ip6) /
193 UDP(sport=1234, dport=1234) /
195 (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
196 IPv6(src="2001:3::1", dst=self.pg3.remote_ip6) /
197 UDP(sport=1234, dport=1234) /
199 pkts_1 = [(Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
200 IPv6(src="2001:1::1", dst=self.pg1.remote_ip6) /
201 UDP(sport=1234, dport=1234) /
203 (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
204 IPv6(src="2001:2::1", dst=self.pg2.remote_ip6) /
205 UDP(sport=1234, dport=1234) /
207 (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
208 IPv6(src="2001:3::1", dst=self.pg3.remote_ip6) /
209 UDP(sport=1234, dport=1234) /
213 # before adding the SVS config all these packets are dropped when
214 # ingressing on pg0 since pg0 is in the default table
217 self.send_and_assert_no_replies(self.pg0, p * 1)
220 # Add table 1001 & 1002 into which we'll add the routes
221 # determining the source VRF selection
223 table_ids = [101, 102]
225 for table_id in table_ids:
226 self.vapi.svs_table_add_del(
227 VppEnum.vl_api_address_family_t.ADDRESS_IP6, table_id)
230 # map X.0.0.0/8 to each SVS table for lookup in table X
232 for i in range(1, 4):
233 self.vapi.svs_route_add_del(
234 table_id, "2001:%d::/32" % i,
238 # Enable SVS on pg0/pg1 using table 1001/1002
240 self.vapi.svs_enable_disable(
241 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
243 self.pg0.sw_if_index)
244 self.vapi.svs_enable_disable(
245 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
247 self.pg1.sw_if_index)
250 # now all the packets should be delivered out the respective interface
252 self.send_and_expect(self.pg0, pkts_0[0] * 65, self.pg1)
253 self.send_and_expect(self.pg0, pkts_0[1] * 65, self.pg2)
254 self.send_and_expect(self.pg0, pkts_0[2] * 65, self.pg3)
255 self.send_and_expect(self.pg1, pkts_1[0] * 65, self.pg1)
256 self.send_and_expect(self.pg1, pkts_1[1] * 65, self.pg2)
257 self.send_and_expect(self.pg1, pkts_1[2] * 65, self.pg3)
260 # check that if the SVS lookup does not match a route the packet
261 # is forwarded using the interface's routing table
263 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
264 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6) /
265 UDP(sport=1234, dport=1234) /
267 self.send_and_expect(self.pg0, p * 65, self.pg0)
269 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
270 IPv6(src=self.pg1.remote_ip6, dst=self.pg1.remote_ip6) /
271 UDP(sport=1234, dport=1234) /
273 self.send_and_expect(self.pg1, p * 65, self.pg1)
276 # dump the SVS configs
278 ss = self.vapi.svs_dump()
280 self.assertEqual(ss[0].table_id, table_ids[0])
281 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
282 self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
283 self.assertEqual(ss[1].table_id, table_ids[1])
284 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
285 self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
290 self.vapi.svs_enable_disable(
291 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
293 self.pg0.sw_if_index,
295 self.vapi.svs_enable_disable(
296 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
298 self.pg1.sw_if_index,
300 for table_id in table_ids:
301 for i in range(1, 4):
302 self.vapi.svs_route_add_del(
303 table_id, "2001:%d::/32" % i,
305 self.vapi.svs_table_add_del(
306 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
311 if __name__ == '__main__':
312 unittest.main(testRunner=VppTestRunner)