blob: 563ed1ad0cb61ee6766a971fd3974adcd742502f [file] [log] [blame]
Neale Rannsccc70f62018-10-02 07:28:16 -07001#!/usr/bin/env python
2
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07003import unittest
4
Neale Rannsccc70f62018-10-02 07:28:16 -07005from framework import VppTestCase, VppTestRunner
Neale Rannsccc70f62018-10-02 07:28:16 -07006from vpp_ip_route import VppIpTable
7
8from scapy.packet import Raw
9from scapy.layers.l2 import Ether
10from scapy.layers.inet import IP, UDP, ICMP
11from scapy.layers.inet6 import IPv6
12
Ole Troan0685da42018-10-16 14:42:50 +020013from vpp_papi import VppEnum
14
Neale Rannsccc70f62018-10-02 07:28:16 -070015
16class TestSVS(VppTestCase):
17 """ SVS Test Case """
18
19 def setUp(self):
20 super(TestSVS, self).setUp()
21
22 # create 2 pg interfaces
23 self.create_pg_interfaces(range(4))
24
25 table_id = 0
26
27 for i in self.pg_interfaces:
28 i.admin_up()
29
30 if table_id != 0:
31 tbl = VppIpTable(self, table_id)
32 tbl.add_vpp_config()
33 tbl = VppIpTable(self, table_id, is_ip6=1)
34 tbl.add_vpp_config()
35
36 i.set_table_ip4(table_id)
37 i.set_table_ip6(table_id)
38 i.config_ip4()
39 i.resolve_arp()
40 i.config_ip6()
41 i.resolve_ndp()
42 table_id += 1
43
44 def tearDown(self):
45 for i in self.pg_interfaces:
46 i.unconfig_ip4()
47 i.unconfig_ip6()
48 i.ip6_disable()
49 i.set_table_ip4(0)
50 i.set_table_ip6(0)
51 i.admin_down()
52 super(TestSVS, self).tearDown()
53
54 def test_svs4(self):
55 """ Source VRF Select IP4 """
56
57 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070058 # packets destined out of the 3 non-default table interfaces
Neale Rannsccc70f62018-10-02 07:28:16 -070059 #
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) /
63 Raw('\xa5' * 100)),
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) /
67 Raw('\xa5' * 100)),
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) /
71 Raw('\xa5' * 100))]
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) /
75 Raw('\xa5' * 100)),
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) /
79 Raw('\xa5' * 100)),
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) /
83 Raw('\xa5' * 100))]
84
85 #
86 # before adding the SVS config all these packets are dropped when
87 # ingressing on pg0 since pg0 is in the default table
88 #
89 for p in pkts_0:
90 self.send_and_assert_no_replies(self.pg0, p * 1)
91
92 #
93 # Add table 1001 & 1002 into which we'll add the routes
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070094 # determining the source VRF selection
Neale Rannsccc70f62018-10-02 07:28:16 -070095 #
96 table_ids = [101, 102]
97
98 for table_id in table_ids:
Ole Troan0685da42018-10-16 14:42:50 +020099 self.vapi.svs_table_add_del(
100 VppEnum.vl_api_address_family_t.ADDRESS_IP4, table_id)
Neale Rannsccc70f62018-10-02 07:28:16 -0700101
102 #
103 # map X.0.0.0/8 to each SVS table for lookup in table X
104 #
105 for i in range(1, 4):
106 self.vapi.svs_route_add_del(
Ole Troan31555a32018-10-22 09:30:26 +0200107 table_id, "%d.0.0.0/8" % i, i)
Neale Rannsccc70f62018-10-02 07:28:16 -0700108
109 #
110 # Enable SVS on pg0/pg1 using table 1001/1002
111 #
Ole Troan0685da42018-10-16 14:42:50 +0200112 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)
Neale Rannsccc70f62018-10-02 07:28:16 -0700118
119 #
120 # now all the packets should be delivered out the respective interface
121 #
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)
128
129 #
130 # check that if the SVS lookup does not match a route the packet
131 # is forwarded using the interface's routing table
132 #
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) /
136 Raw('\xa5' * 100))
137 self.send_and_expect(self.pg0, p * 65, self.pg0)
138
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) /
142 Raw('\xa5' * 100))
143 self.send_and_expect(self.pg1, p * 65, self.pg1)
144
145 #
146 # dump the SVS configs
147 #
148 ss = self.vapi.svs_dump()
149
150 self.assertEqual(ss[0].table_id, table_ids[0])
151 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
Ole Troan0685da42018-10-16 14:42:50 +0200152 self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
Neale Rannsccc70f62018-10-02 07:28:16 -0700153 self.assertEqual(ss[1].table_id, table_ids[1])
154 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
Ole Troan0685da42018-10-16 14:42:50 +0200155 self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
Neale Rannsccc70f62018-10-02 07:28:16 -0700156
157 #
158 # cleanup
159 #
Ole Troan0685da42018-10-16 14:42:50 +0200160 self.vapi.svs_enable_disable(
161 VppEnum.vl_api_address_family_t.ADDRESS_IP4,
162 table_ids[0],
163 self.pg0.sw_if_index,
164 is_enable=0)
165 self.vapi.svs_enable_disable(
166 VppEnum.vl_api_address_family_t.ADDRESS_IP4,
167 table_ids[1],
168 self.pg1.sw_if_index,
169 is_enable=0)
Neale Rannsccc70f62018-10-02 07:28:16 -0700170
171 for table_id in table_ids:
172 for i in range(1, 4):
173 self.vapi.svs_route_add_del(
Ole Troan31555a32018-10-22 09:30:26 +0200174 table_id, "%d.0.0.0/8" % i,
Neale Rannsccc70f62018-10-02 07:28:16 -0700175 0, is_add=0)
Ole Troan0685da42018-10-16 14:42:50 +0200176 self.vapi.svs_table_add_del(
177 VppEnum.vl_api_address_family_t.ADDRESS_IP4,
178 table_id,
179 is_add=0)
Neale Rannsccc70f62018-10-02 07:28:16 -0700180
181 def test_svs6(self):
182 """ Source VRF Select IP6 """
183
184 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700185 # packets destined out of the 3 non-default table interfaces
Neale Rannsccc70f62018-10-02 07:28:16 -0700186 #
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) /
190 Raw('\xa5' * 100)),
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) /
194 Raw('\xa5' * 100)),
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) /
198 Raw('\xa5' * 100))]
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) /
202 Raw('\xa5' * 100)),
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) /
206 Raw('\xa5' * 100)),
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) /
210 Raw('\xa5' * 100))]
211
212 #
213 # before adding the SVS config all these packets are dropped when
214 # ingressing on pg0 since pg0 is in the default table
215 #
216 for p in pkts_0:
217 self.send_and_assert_no_replies(self.pg0, p * 1)
218
219 #
220 # Add table 1001 & 1002 into which we'll add the routes
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700221 # determining the source VRF selection
Neale Rannsccc70f62018-10-02 07:28:16 -0700222 #
223 table_ids = [101, 102]
224
225 for table_id in table_ids:
Ole Troan0685da42018-10-16 14:42:50 +0200226 self.vapi.svs_table_add_del(
227 VppEnum.vl_api_address_family_t.ADDRESS_IP6, table_id)
Neale Rannsccc70f62018-10-02 07:28:16 -0700228
229 #
230 # map X.0.0.0/8 to each SVS table for lookup in table X
231 #
232 for i in range(1, 4):
233 self.vapi.svs_route_add_del(
Ole Troan31555a32018-10-22 09:30:26 +0200234 table_id, "2001:%d::/32" % i,
Neale Rannsccc70f62018-10-02 07:28:16 -0700235 i)
236
237 #
238 # Enable SVS on pg0/pg1 using table 1001/1002
239 #
Ole Troan0685da42018-10-16 14:42:50 +0200240 self.vapi.svs_enable_disable(
241 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
242 table_ids[0],
243 self.pg0.sw_if_index)
244 self.vapi.svs_enable_disable(
245 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
246 table_ids[1],
247 self.pg1.sw_if_index)
Neale Rannsccc70f62018-10-02 07:28:16 -0700248
249 #
250 # now all the packets should be delivered out the respective interface
251 #
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)
258
259 #
260 # check that if the SVS lookup does not match a route the packet
261 # is forwarded using the interface's routing table
262 #
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) /
266 Raw('\xa5' * 100))
267 self.send_and_expect(self.pg0, p * 65, self.pg0)
268
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) /
272 Raw('\xa5' * 100))
273 self.send_and_expect(self.pg1, p * 65, self.pg1)
274
275 #
276 # dump the SVS configs
277 #
278 ss = self.vapi.svs_dump()
279
280 self.assertEqual(ss[0].table_id, table_ids[0])
281 self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
Ole Troan0685da42018-10-16 14:42:50 +0200282 self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
Neale Rannsccc70f62018-10-02 07:28:16 -0700283 self.assertEqual(ss[1].table_id, table_ids[1])
284 self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
Ole Troan0685da42018-10-16 14:42:50 +0200285 self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
Neale Rannsccc70f62018-10-02 07:28:16 -0700286
287 #
288 # cleanup
289 #
Ole Troan0685da42018-10-16 14:42:50 +0200290 self.vapi.svs_enable_disable(
291 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
292 table_ids[0],
293 self.pg0.sw_if_index,
294 is_enable=0)
295 self.vapi.svs_enable_disable(
296 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
297 table_ids[1],
298 self.pg1.sw_if_index,
299 is_enable=0)
Neale Rannsccc70f62018-10-02 07:28:16 -0700300 for table_id in table_ids:
301 for i in range(1, 4):
302 self.vapi.svs_route_add_del(
Ole Troan31555a32018-10-22 09:30:26 +0200303 table_id, "2001:%d::/32" % i,
Neale Rannsccc70f62018-10-02 07:28:16 -0700304 0, is_add=0)
Ole Troan0685da42018-10-16 14:42:50 +0200305 self.vapi.svs_table_add_del(
306 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
307 table_id,
308 is_add=0)
309
Neale Rannsccc70f62018-10-02 07:28:16 -0700310
311if __name__ == '__main__':
312 unittest.main(testRunner=VppTestRunner)