3 from framework import VppTestCase, VppTestRunner
4 from vpp_ip import IpAddressFamily, VppIpPrefix
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
14 class TestSVS(VppTestCase):
18 super(TestSVS, self).setUp()
20 # create 2 pg interfaces
21 self.create_pg_interfaces(range(4))
25 for i in self.pg_interfaces:
29 tbl = VppIpTable(self, table_id)
31 tbl = VppIpTable(self, table_id, is_ip6=1)
34 i.set_table_ip4(table_id)
35 i.set_table_ip6(table_id)
43 for i in self.pg_interfaces:
50 super(TestSVS, self).tearDown()
53 """ Source VRF Select IP4 """
56 # packets destinet out of the 3 non-default table interfaces
58 pkts_0 = [(Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
59 IP(src="1.1.1.1", dst=self.pg1.remote_ip4) /
60 UDP(sport=1234, dport=1234) /
62 (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
63 IP(src="2.2.2.2", dst=self.pg2.remote_ip4) /
64 UDP(sport=1234, dport=1234) /
66 (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
67 IP(src="3.3.3.3", dst=self.pg3.remote_ip4) /
68 UDP(sport=1234, dport=1234) /
70 pkts_1 = [(Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
71 IP(src="1.1.1.1", dst=self.pg1.remote_ip4) /
72 UDP(sport=1234, dport=1234) /
74 (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
75 IP(src="2.2.2.2", dst=self.pg2.remote_ip4) /
76 UDP(sport=1234, dport=1234) /
78 (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
79 IP(src="3.3.3.3", dst=self.pg3.remote_ip4) /
80 UDP(sport=1234, dport=1234) /
84 # before adding the SVS config all these packets are dropped when
85 # ingressing on pg0 since pg0 is in the default table
88 self.send_and_assert_no_replies(self.pg0, p * 1)
91 # Add table 1001 & 1002 into which we'll add the routes
92 # determing the source VRF selection
94 table_ids = [101, 102]
96 for table_id in table_ids:
97 self.vapi.svs_table_add_del(IpAddressFamily.ADDRESS_IP4, table_id)
100 # map X.0.0.0/8 to each SVS table for lookup in table X
102 for i in range(1, 4):
103 self.vapi.svs_route_add_del(
105 VppIpPrefix("%d.0.0.0" % i, 8).encode(),
109 # Enable SVS on pg0/pg1 using table 1001/1002
111 self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP4,
113 self.pg0.sw_if_index)
114 self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP4,
116 self.pg1.sw_if_index)
119 # now all the packets should be delivered out the respective interface
121 self.send_and_expect(self.pg0, pkts_0[0] * 65, self.pg1)
122 self.send_and_expect(self.pg0, pkts_0[1] * 65, self.pg2)
123 self.send_and_expect(self.pg0, pkts_0[2] * 65, self.pg3)
124 self.send_and_expect(self.pg1, pkts_1[0] * 65, self.pg1)
125 self.send_and_expect(self.pg1, pkts_1[1] * 65, self.pg2)
126 self.send_and_expect(self.pg1, pkts_1[2] * 65, self.pg3)
129 # check that if the SVS lookup does not match a route the packet
130 # is forwarded using the interface's routing table
132 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
133 IP(src=self.pg0.remote_ip4, dst=self.pg0.remote_ip4) /
134 UDP(sport=1234, dport=1234) /
136 self.send_and_expect(self.pg0, p * 65, self.pg0)
138 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
139 IP(src=self.pg1.remote_ip4, dst=self.pg1.remote_ip4) /
140 UDP(sport=1234, dport=1234) /
142 self.send_and_expect(self.pg1, p * 65, self.pg1)
145 # dump the SVS configs
147 ss = self.vapi.svs_dump()
149 self.assertEqual(ss[0].table_id, table_ids[0])
150 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
151 self.assertEqual(ss[0].af, IpAddressFamily.ADDRESS_IP4)
152 self.assertEqual(ss[1].table_id, table_ids[1])
153 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
154 self.assertEqual(ss[1].af, IpAddressFamily.ADDRESS_IP4)
159 self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP4,
161 self.pg0.sw_if_index,
163 self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP4,
165 self.pg1.sw_if_index,
168 for table_id in table_ids:
169 for i in range(1, 4):
170 self.vapi.svs_route_add_del(
172 VppIpPrefix("%d.0.0.0" % i, 8).encode(),
174 self.vapi.svs_table_add_del(IpAddressFamily.ADDRESS_IP4,
179 """ Source VRF Select IP6 """
182 # packets destinet out of the 3 non-default table interfaces
184 pkts_0 = [(Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
185 IPv6(src="2001:1::1", dst=self.pg1.remote_ip6) /
186 UDP(sport=1234, dport=1234) /
188 (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
189 IPv6(src="2001:2::1", dst=self.pg2.remote_ip6) /
190 UDP(sport=1234, dport=1234) /
192 (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
193 IPv6(src="2001:3::1", dst=self.pg3.remote_ip6) /
194 UDP(sport=1234, dport=1234) /
196 pkts_1 = [(Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
197 IPv6(src="2001:1::1", dst=self.pg1.remote_ip6) /
198 UDP(sport=1234, dport=1234) /
200 (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
201 IPv6(src="2001:2::1", dst=self.pg2.remote_ip6) /
202 UDP(sport=1234, dport=1234) /
204 (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
205 IPv6(src="2001:3::1", dst=self.pg3.remote_ip6) /
206 UDP(sport=1234, dport=1234) /
210 # before adding the SVS config all these packets are dropped when
211 # ingressing on pg0 since pg0 is in the default table
214 self.send_and_assert_no_replies(self.pg0, p * 1)
217 # Add table 1001 & 1002 into which we'll add the routes
218 # determing the source VRF selection
220 table_ids = [101, 102]
222 for table_id in table_ids:
223 self.vapi.svs_table_add_del(IpAddressFamily.ADDRESS_IP6, table_id)
226 # map X.0.0.0/8 to each SVS table for lookup in table X
228 for i in range(1, 4):
229 self.vapi.svs_route_add_del(
231 VppIpPrefix("2001:%d::" % i, 32).encode(),
235 # Enable SVS on pg0/pg1 using table 1001/1002
237 self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP6,
239 self.pg0.sw_if_index)
240 self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP6,
242 self.pg1.sw_if_index)
245 # now all the packets should be delivered out the respective interface
247 self.send_and_expect(self.pg0, pkts_0[0] * 65, self.pg1)
248 self.send_and_expect(self.pg0, pkts_0[1] * 65, self.pg2)
249 self.send_and_expect(self.pg0, pkts_0[2] * 65, self.pg3)
250 self.send_and_expect(self.pg1, pkts_1[0] * 65, self.pg1)
251 self.send_and_expect(self.pg1, pkts_1[1] * 65, self.pg2)
252 self.send_and_expect(self.pg1, pkts_1[2] * 65, self.pg3)
255 # check that if the SVS lookup does not match a route the packet
256 # is forwarded using the interface's routing table
258 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
259 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6) /
260 UDP(sport=1234, dport=1234) /
262 self.send_and_expect(self.pg0, p * 65, self.pg0)
264 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
265 IPv6(src=self.pg1.remote_ip6, dst=self.pg1.remote_ip6) /
266 UDP(sport=1234, dport=1234) /
268 self.send_and_expect(self.pg1, p * 65, self.pg1)
271 # dump the SVS configs
273 ss = self.vapi.svs_dump()
275 self.assertEqual(ss[0].table_id, table_ids[0])
276 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
277 self.assertEqual(ss[0].af, IpAddressFamily.ADDRESS_IP6)
278 self.assertEqual(ss[1].table_id, table_ids[1])
279 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
280 self.assertEqual(ss[1].af, IpAddressFamily.ADDRESS_IP6)
285 self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP6,
287 self.pg0.sw_if_index,
289 self.vapi.svs_enable_disable(IpAddressFamily.ADDRESS_IP6,
291 self.pg1.sw_if_index,
293 for table_id in table_ids:
294 for i in range(1, 4):
295 self.vapi.svs_route_add_del(
297 VppIpPrefix("2001:%d::" % i, 32).encode(),
299 self.vapi.svs_table_add_del(IpAddressFamily.ADDRESS_IP6,
303 if __name__ == '__main__':
304 unittest.main(testRunner=VppTestRunner)