ip: Protocol Independent IP Neighbors
[vpp.git] / src / plugins / svs / test / test_svs.py
1 #!/usr/bin/env python3
2
3 import unittest
4
5 from framework import VppTestCase, VppTestRunner
6 from vpp_ip_route import VppIpTable
7
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
12
13 from vpp_papi import VppEnum
14
15 NUM_PKTS = 67
16
17
18 class TestSVS(VppTestCase):
19     """ SVS Test Case """
20
21     @classmethod
22     def setUpClass(cls):
23         super(TestSVS, cls).setUpClass()
24
25     @classmethod
26     def tearDownClass(cls):
27         super(TestSVS, cls).tearDownClass()
28
29     def setUp(self):
30         super(TestSVS, self).setUp()
31
32         # create 2 pg interfaces
33         self.create_pg_interfaces(range(4))
34
35         table_id = 0
36
37         for i in self.pg_interfaces:
38             i.admin_up()
39
40             if table_id != 0:
41                 tbl = VppIpTable(self, table_id)
42                 tbl.add_vpp_config()
43                 tbl = VppIpTable(self, table_id, is_ip6=1)
44                 tbl.add_vpp_config()
45
46             i.set_table_ip4(table_id)
47             i.set_table_ip6(table_id)
48             i.config_ip4()
49             i.resolve_arp()
50             i.config_ip6()
51             i.resolve_ndp()
52             table_id += 1
53
54     def tearDown(self):
55         for i in self.pg_interfaces:
56             i.unconfig_ip4()
57             i.unconfig_ip6()
58             i.set_table_ip4(0)
59             i.set_table_ip6(0)
60             i.admin_down()
61         super(TestSVS, self).tearDown()
62
63     def test_svs4(self):
64         """ Source VRF Select IP4 """
65
66         #
67         # packets destined out of the 3 non-default table interfaces
68         #
69         pkts_0 = [(Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
70                    IP(src="1.1.1.1", dst=self.pg1.remote_ip4) /
71                    UDP(sport=1234, dport=1234) /
72                    Raw(b'\xa5' * 100)),
73                   (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
74                    IP(src="2.2.2.2", dst=self.pg2.remote_ip4) /
75                    UDP(sport=1234, dport=1234) /
76                    Raw(b'\xa5' * 100)),
77                   (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
78                    IP(src="3.3.3.3", dst=self.pg3.remote_ip4) /
79                    UDP(sport=1234, dport=1234) /
80                    Raw(b'\xa5' * 100))]
81         pkts_1 = [(Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
82                    IP(src="1.1.1.1", dst=self.pg1.remote_ip4) /
83                    UDP(sport=1234, dport=1234) /
84                    Raw(b'\xa5' * 100)),
85                   (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
86                    IP(src="2.2.2.2", dst=self.pg2.remote_ip4) /
87                    UDP(sport=1234, dport=1234) /
88                    Raw(b'\xa5' * 100)),
89                   (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
90                    IP(src="3.3.3.3", dst=self.pg3.remote_ip4) /
91                    UDP(sport=1234, dport=1234) /
92                    Raw(b'\xa5' * 100))]
93
94         #
95         # before adding the SVS config all these packets are dropped when
96         # ingressing on pg0 since pg0 is in the default table
97         #
98         for p in pkts_0:
99             self.send_and_assert_no_replies(self.pg0, p * 1)
100
101         #
102         # Add table 1001 & 1002 into which we'll add the routes
103         # determining the source VRF selection
104         #
105         table_ids = [101, 102]
106
107         for table_id in table_ids:
108             self.vapi.svs_table_add_del(
109                 VppEnum.vl_api_address_family_t.ADDRESS_IP4, table_id)
110
111             #
112             # map X.0.0.0/8 to each SVS table for lookup in table X
113             #
114             for i in range(1, 4):
115                 self.vapi.svs_route_add_del(
116                     table_id, "%d.0.0.0/8" % i, i)
117
118         #
119         # Enable SVS on pg0/pg1 using table 1001/1002
120         #
121         self.vapi.svs_enable_disable(
122             VppEnum.vl_api_address_family_t.ADDRESS_IP4, table_ids[0],
123             self.pg0.sw_if_index)
124         self.vapi.svs_enable_disable(
125             VppEnum.vl_api_address_family_t.ADDRESS_IP4, table_ids[1],
126             self.pg1.sw_if_index)
127
128         #
129         # now all the packets should be delivered out the respective interface
130         #
131         self.send_and_expect(self.pg0, pkts_0[0] * NUM_PKTS, self.pg1)
132         self.send_and_expect(self.pg0, pkts_0[1] * NUM_PKTS, self.pg2)
133         self.send_and_expect(self.pg0, pkts_0[2] * NUM_PKTS, self.pg3)
134         self.send_and_expect(self.pg1, pkts_1[0] * NUM_PKTS, self.pg1)
135         self.send_and_expect(self.pg1, pkts_1[1] * NUM_PKTS, self.pg2)
136         self.send_and_expect(self.pg1, pkts_1[2] * NUM_PKTS, self.pg3)
137
138         #
139         # check that if the SVS lookup does not match a route the packet
140         # is forwarded using the interface's routing table
141         #
142         p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
143              IP(src=self.pg0.remote_ip4, dst=self.pg0.remote_ip4) /
144              UDP(sport=1234, dport=1234) /
145              Raw(b'\xa5' * 100))
146         self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg0)
147
148         p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
149              IP(src=self.pg1.remote_ip4, dst=self.pg1.remote_ip4) /
150              UDP(sport=1234, dport=1234) /
151              Raw(b'\xa5' * 100))
152         self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg1)
153
154         #
155         # dump the SVS configs
156         #
157         ss = self.vapi.svs_dump()
158
159         self.assertEqual(ss[0].table_id, table_ids[0])
160         self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
161         self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
162         self.assertEqual(ss[1].table_id, table_ids[1])
163         self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
164         self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP4)
165
166         #
167         # cleanup
168         #
169         self.vapi.svs_enable_disable(
170             VppEnum.vl_api_address_family_t.ADDRESS_IP4,
171             table_ids[0],
172             self.pg0.sw_if_index,
173             is_enable=0)
174         self.vapi.svs_enable_disable(
175             VppEnum.vl_api_address_family_t.ADDRESS_IP4,
176             table_ids[1],
177             self.pg1.sw_if_index,
178             is_enable=0)
179
180         for table_id in table_ids:
181             for i in range(1, 4):
182                 self.vapi.svs_route_add_del(
183                     table_id, "%d.0.0.0/8" % i,
184                     0, is_add=0)
185             self.vapi.svs_table_add_del(
186                 VppEnum.vl_api_address_family_t.ADDRESS_IP4,
187                 table_id,
188                 is_add=0)
189
190     def test_svs6(self):
191         """ Source VRF Select IP6 """
192
193         #
194         # packets destined out of the 3 non-default table interfaces
195         #
196         pkts_0 = [(Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
197                    IPv6(src="2001:1::1", dst=self.pg1.remote_ip6) /
198                    UDP(sport=1234, dport=1234) /
199                    Raw(b'\xa5' * 100)),
200                   (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
201                    IPv6(src="2001:2::1", dst=self.pg2.remote_ip6) /
202                    UDP(sport=1234, dport=1234) /
203                    Raw(b'\xa5' * 100)),
204                   (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
205                    IPv6(src="2001:3::1", dst=self.pg3.remote_ip6) /
206                    UDP(sport=1234, dport=1234) /
207                    Raw(b'\xa5' * 100))]
208         pkts_1 = [(Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
209                    IPv6(src="2001:1::1", dst=self.pg1.remote_ip6) /
210                    UDP(sport=1234, dport=1234) /
211                    Raw(b'\xa5' * 100)),
212                   (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
213                    IPv6(src="2001:2::1", dst=self.pg2.remote_ip6) /
214                    UDP(sport=1234, dport=1234) /
215                    Raw(b'\xa5' * 100)),
216                   (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
217                    IPv6(src="2001:3::1", dst=self.pg3.remote_ip6) /
218                    UDP(sport=1234, dport=1234) /
219                    Raw(b'\xa5' * 100))]
220
221         #
222         # before adding the SVS config all these packets are dropped when
223         # ingressing on pg0 since pg0 is in the default table
224         #
225         for p in pkts_0:
226             self.send_and_assert_no_replies(self.pg0, p * 1)
227
228         #
229         # Add table 1001 & 1002 into which we'll add the routes
230         # determining the source VRF selection
231         #
232         table_ids = [101, 102]
233
234         for table_id in table_ids:
235             self.vapi.svs_table_add_del(
236                 VppEnum.vl_api_address_family_t.ADDRESS_IP6, table_id)
237
238             #
239             # map X.0.0.0/8 to each SVS table for lookup in table X
240             #
241             for i in range(1, 4):
242                 self.vapi.svs_route_add_del(
243                     table_id, "2001:%d::/32" % i,
244                     i)
245
246         #
247         # Enable SVS on pg0/pg1 using table 1001/1002
248         #
249         self.vapi.svs_enable_disable(
250             VppEnum.vl_api_address_family_t.ADDRESS_IP6,
251             table_ids[0],
252             self.pg0.sw_if_index)
253         self.vapi.svs_enable_disable(
254             VppEnum.vl_api_address_family_t.ADDRESS_IP6,
255             table_ids[1],
256             self.pg1.sw_if_index)
257
258         #
259         # now all the packets should be delivered out the respective interface
260         #
261         self.send_and_expect(self.pg0, pkts_0[0] * NUM_PKTS, self.pg1)
262         self.send_and_expect(self.pg0, pkts_0[1] * NUM_PKTS, self.pg2)
263         self.send_and_expect(self.pg0, pkts_0[2] * NUM_PKTS, self.pg3)
264         self.send_and_expect(self.pg1, pkts_1[0] * NUM_PKTS, self.pg1)
265         self.send_and_expect(self.pg1, pkts_1[1] * NUM_PKTS, self.pg2)
266         self.send_and_expect(self.pg1, pkts_1[2] * NUM_PKTS, self.pg3)
267
268         #
269         # check that if the SVS lookup does not match a route the packet
270         # is forwarded using the interface's routing table
271         #
272         p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
273              IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6) /
274              UDP(sport=1234, dport=1234) /
275              Raw(b'\xa5' * 100))
276         self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg0)
277
278         p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
279              IPv6(src=self.pg1.remote_ip6, dst=self.pg1.remote_ip6) /
280              UDP(sport=1234, dport=1234) /
281              Raw(b'\xa5' * 100))
282         self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg1)
283
284         #
285         # dump the SVS configs
286         #
287         ss = self.vapi.svs_dump()
288
289         self.assertEqual(ss[0].table_id, table_ids[0])
290         self.assertEqual(ss[0].sw_if_index, self.pg0.sw_if_index)
291         self.assertEqual(ss[0].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
292         self.assertEqual(ss[1].table_id, table_ids[1])
293         self.assertEqual(ss[1].sw_if_index, self.pg1.sw_if_index)
294         self.assertEqual(ss[1].af, VppEnum.vl_api_address_family_t.ADDRESS_IP6)
295
296         #
297         # cleanup
298         #
299         self.vapi.svs_enable_disable(
300             VppEnum.vl_api_address_family_t.ADDRESS_IP6,
301             table_ids[0],
302             self.pg0.sw_if_index,
303             is_enable=0)
304         self.vapi.svs_enable_disable(
305             VppEnum.vl_api_address_family_t.ADDRESS_IP6,
306             table_ids[1],
307             self.pg1.sw_if_index,
308             is_enable=0)
309         for table_id in table_ids:
310             for i in range(1, 4):
311                 self.vapi.svs_route_add_del(
312                     table_id, "2001:%d::/32" % i,
313                     0, is_add=0)
314             self.vapi.svs_table_add_del(
315                 VppEnum.vl_api_address_family_t.ADDRESS_IP6,
316                 table_id,
317                 is_add=0)
318
319
320 if __name__ == '__main__':
321     unittest.main(testRunner=VppTestRunner)