sctp: move to plugins, disabled by default
[vpp.git] / test / test_udp.py
1 #!/usr/bin/env python
2 import unittest
3 from framework import VppTestCase, VppTestRunner
4
5 from vpp_udp_encap import find_udp_encap, VppUdpEncap
6 from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpTable, VppMplsLabel, \
7     FibPathType
8
9 from scapy.packet import Raw
10 from scapy.layers.l2 import Ether
11 from scapy.layers.inet import IP, UDP
12 from scapy.layers.inet6 import IPv6
13 from scapy.contrib.mpls import MPLS
14
15 NUM_PKTS = 67
16
17
18 class TestUdpEncap(VppTestCase):
19     """ UDP Encap Test Case """
20
21     @classmethod
22     def setUpClass(cls):
23         super(TestUdpEncap, cls).setUpClass()
24
25     @classmethod
26     def tearDownClass(cls):
27         super(TestUdpEncap, cls).tearDownClass()
28
29     def setUp(self):
30         super(TestUdpEncap, self).setUp()
31
32         # create 2 pg interfaces
33         self.create_pg_interfaces(range(4))
34
35         # setup interfaces
36         # assign them different tables.
37         table_id = 0
38         self.tables = []
39
40         for i in self.pg_interfaces:
41             i.admin_up()
42
43             if table_id != 0:
44                 tbl = VppIpTable(self, table_id)
45                 tbl.add_vpp_config()
46                 self.tables.append(tbl)
47                 tbl = VppIpTable(self, table_id, is_ip6=1)
48                 tbl.add_vpp_config()
49                 self.tables.append(tbl)
50
51             i.set_table_ip4(table_id)
52             i.set_table_ip6(table_id)
53             i.config_ip4()
54             i.resolve_arp()
55             i.config_ip6()
56             i.resolve_ndp()
57             table_id += 1
58
59     def tearDown(self):
60         for i in self.pg_interfaces:
61             i.unconfig_ip4()
62             i.unconfig_ip6()
63             i.ip6_disable()
64             i.set_table_ip4(0)
65             i.set_table_ip6(0)
66             i.admin_down()
67         super(TestUdpEncap, self).tearDown()
68
69     def validate_outer4(self, rx, encap_obj):
70         self.assertEqual(rx[IP].src, encap_obj.src_ip_s)
71         self.assertEqual(rx[IP].dst, encap_obj.dst_ip_s)
72         self.assertEqual(rx[UDP].sport, encap_obj.src_port)
73         self.assertEqual(rx[UDP].dport, encap_obj.dst_port)
74
75     def validate_outer6(self, rx, encap_obj):
76         self.assertEqual(rx[IPv6].src, encap_obj.src_ip_s)
77         self.assertEqual(rx[IPv6].dst, encap_obj.dst_ip_s)
78         self.assertEqual(rx[UDP].sport, encap_obj.src_port)
79         self.assertEqual(rx[UDP].dport, encap_obj.dst_port)
80
81     def validate_inner4(self, rx, tx, ttl=None):
82         self.assertEqual(rx[IP].src, tx[IP].src)
83         self.assertEqual(rx[IP].dst, tx[IP].dst)
84         if ttl:
85             self.assertEqual(rx[IP].ttl, ttl)
86         else:
87             self.assertEqual(rx[IP].ttl, tx[IP].ttl)
88
89     def validate_inner6(self, rx, tx):
90         self.assertEqual(rx.src, tx[IPv6].src)
91         self.assertEqual(rx.dst, tx[IPv6].dst)
92         self.assertEqual(rx.hlim, tx[IPv6].hlim)
93
94     def test_udp_encap(self):
95         """ UDP Encap test
96         """
97
98         #
99         # construct a UDP encap object through each of the peers
100         # v4 through the first two peers, v6 through the second.
101         #
102         udp_encap_0 = VppUdpEncap(self,
103                                   self.pg0.local_ip4,
104                                   self.pg0.remote_ip4,
105                                   330, 440)
106         udp_encap_1 = VppUdpEncap(self,
107                                   self.pg1.local_ip4,
108                                   self.pg1.remote_ip4,
109                                   331, 441,
110                                   table_id=1)
111         udp_encap_2 = VppUdpEncap(self,
112                                   self.pg2.local_ip6,
113                                   self.pg2.remote_ip6,
114                                   332, 442,
115                                   table_id=2)
116         udp_encap_3 = VppUdpEncap(self,
117                                   self.pg3.local_ip6,
118                                   self.pg3.remote_ip6,
119                                   333, 443,
120                                   table_id=3)
121         udp_encap_0.add_vpp_config()
122         udp_encap_1.add_vpp_config()
123         udp_encap_2.add_vpp_config()
124         udp_encap_3.add_vpp_config()
125
126         self.logger.info(self.vapi.cli("sh udp encap"))
127
128         self.assertTrue(find_udp_encap(self, udp_encap_2))
129         self.assertTrue(find_udp_encap(self, udp_encap_3))
130         self.assertTrue(find_udp_encap(self, udp_encap_0))
131         self.assertTrue(find_udp_encap(self, udp_encap_1))
132
133         #
134         # Routes via each UDP encap object - all combinations of v4 and v6.
135         #
136         route_4o4 = VppIpRoute(
137             self, "1.1.0.1", 32,
138             [VppRoutePath("0.0.0.0",
139                           0xFFFFFFFF,
140                           type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
141                           next_hop_id=udp_encap_0.id)])
142         route_4o6 = VppIpRoute(
143             self, "1.1.2.1", 32,
144             [VppRoutePath("0.0.0.0",
145                           0xFFFFFFFF,
146                           type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
147                           next_hop_id=udp_encap_2.id)])
148         route_6o4 = VppIpRoute(
149             self, "2001::1", 128,
150             [VppRoutePath("0.0.0.0",
151                           0xFFFFFFFF,
152                           type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
153                           next_hop_id=udp_encap_1.id)])
154         route_6o6 = VppIpRoute(
155             self, "2001::3", 128,
156             [VppRoutePath("0.0.0.0",
157                           0xFFFFFFFF,
158                           type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
159                           next_hop_id=udp_encap_3.id)])
160         route_4o6.add_vpp_config()
161         route_6o6.add_vpp_config()
162         route_6o4.add_vpp_config()
163         route_4o4.add_vpp_config()
164
165         #
166         # 4o4 encap
167         #
168         p_4o4 = (Ether(src=self.pg0.remote_mac,
169                        dst=self.pg0.local_mac) /
170                  IP(src="2.2.2.2", dst="1.1.0.1") /
171                  UDP(sport=1234, dport=1234) /
172                  Raw('\xa5' * 100))
173         rx = self.send_and_expect(self.pg0, p_4o4*NUM_PKTS, self.pg0)
174         for p in rx:
175             self.validate_outer4(p, udp_encap_0)
176             p = IP(p["UDP"].payload.load)
177             self.validate_inner4(p, p_4o4)
178         self.assertEqual(udp_encap_0.get_stats()['packets'], NUM_PKTS)
179
180         #
181         # 4o6 encap
182         #
183         p_4o6 = (Ether(src=self.pg0.remote_mac,
184                        dst=self.pg0.local_mac) /
185                  IP(src="2.2.2.2", dst="1.1.2.1") /
186                  UDP(sport=1234, dport=1234) /
187                  Raw('\xa5' * 100))
188         rx = self.send_and_expect(self.pg0, p_4o6*NUM_PKTS, self.pg2)
189         for p in rx:
190             self.validate_outer6(p, udp_encap_2)
191             p = IP(p["UDP"].payload.load)
192             self.validate_inner4(p, p_4o6)
193         self.assertEqual(udp_encap_2.get_stats()['packets'], NUM_PKTS)
194
195         #
196         # 6o4 encap
197         #
198         p_6o4 = (Ether(src=self.pg0.remote_mac,
199                        dst=self.pg0.local_mac) /
200                  IPv6(src="2001::100", dst="2001::1") /
201                  UDP(sport=1234, dport=1234) /
202                  Raw('\xa5' * 100))
203         rx = self.send_and_expect(self.pg0, p_6o4*NUM_PKTS, self.pg1)
204         for p in rx:
205             self.validate_outer4(p, udp_encap_1)
206             p = IPv6(p["UDP"].payload.load)
207             self.validate_inner6(p, p_6o4)
208         self.assertEqual(udp_encap_1.get_stats()['packets'], NUM_PKTS)
209
210         #
211         # 6o6 encap
212         #
213         p_6o6 = (Ether(src=self.pg0.remote_mac,
214                        dst=self.pg0.local_mac) /
215                  IPv6(src="2001::100", dst="2001::3") /
216                  UDP(sport=1234, dport=1234) /
217                  Raw('\xa5' * 100))
218         rx = self.send_and_expect(self.pg0, p_6o6*NUM_PKTS, self.pg3)
219         for p in rx:
220             self.validate_outer6(p, udp_encap_3)
221             p = IPv6(p["UDP"].payload.load)
222             self.validate_inner6(p, p_6o6)
223         self.assertEqual(udp_encap_3.get_stats()['packets'], NUM_PKTS)
224
225         #
226         # A route with an output label
227         # the TTL of the inner packet is decremented on LSP ingress
228         #
229         route_4oMPLSo4 = VppIpRoute(
230             self, "1.1.2.22", 32,
231             [VppRoutePath("0.0.0.0",
232                           0xFFFFFFFF,
233                           type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP,
234                           next_hop_id=1,
235                           labels=[VppMplsLabel(66)])])
236         route_4oMPLSo4.add_vpp_config()
237
238         p_4omo4 = (Ether(src=self.pg0.remote_mac,
239                          dst=self.pg0.local_mac) /
240                    IP(src="2.2.2.2", dst="1.1.2.22") /
241                    UDP(sport=1234, dport=1234) /
242                    Raw('\xa5' * 100))
243         rx = self.send_and_expect(self.pg0, p_4omo4*NUM_PKTS, self.pg1)
244         for p in rx:
245             self.validate_outer4(p, udp_encap_1)
246             p = MPLS(p["UDP"].payload.load)
247             self.validate_inner4(p, p_4omo4, ttl=63)
248         self.assertEqual(udp_encap_1.get_stats()['packets'], 2*NUM_PKTS)
249
250
251 class TestUDP(VppTestCase):
252     """ UDP Test Case """
253
254     @classmethod
255     def setUpClass(cls):
256         super(TestUDP, cls).setUpClass()
257
258     @classmethod
259     def tearDownClass(cls):
260         super(TestUDP, cls).tearDownClass()
261
262     def setUp(self):
263         super(TestUDP, self).setUp()
264         self.vapi.session_enable_disable(is_enabled=1)
265         self.create_loopback_interfaces(2)
266
267         table_id = 0
268
269         for i in self.lo_interfaces:
270             i.admin_up()
271
272             if table_id != 0:
273                 tbl = VppIpTable(self, table_id)
274                 tbl.add_vpp_config()
275
276             i.set_table_ip4(table_id)
277             i.config_ip4()
278             table_id += 1
279
280         # Configure namespaces
281         self.vapi.app_namespace_add_del(namespace_id="0",
282                                         sw_if_index=self.loop0.sw_if_index)
283         self.vapi.app_namespace_add_del(namespace_id="1",
284                                         sw_if_index=self.loop1.sw_if_index)
285
286     def tearDown(self):
287         for i in self.lo_interfaces:
288             i.unconfig_ip4()
289             i.set_table_ip4(0)
290             i.admin_down()
291         self.vapi.session_enable_disable(is_enabled=0)
292         super(TestUDP, self).tearDown()
293
294     def test_udp_transfer(self):
295         """ UDP echo client/server transfer """
296
297         # Add inter-table routes
298         ip_t01 = VppIpRoute(self, self.loop1.local_ip4, 32,
299                             [VppRoutePath("0.0.0.0",
300                                           0xffffffff,
301                                           nh_table_id=1)])
302         ip_t10 = VppIpRoute(self, self.loop0.local_ip4, 32,
303                             [VppRoutePath("0.0.0.0",
304                                           0xffffffff,
305                                           nh_table_id=0)], table_id=1)
306         ip_t01.add_vpp_config()
307         ip_t10.add_vpp_config()
308
309         # Start builtin server and client
310         uri = "udp://" + self.loop0.local_ip4 + "/1234"
311         error = self.vapi.cli("test echo server appns 0 fifo-size 4 no-echo" +
312                               "uri " + uri)
313         if error:
314             self.logger.critical(error)
315             self.assertNotIn("failed", error)
316
317         error = self.vapi.cli("test echo client mbytes 10 appns 1 " +
318                               "fifo-size 4 no-output test-bytes " +
319                               "syn-timeout 2 no-return uri " + uri)
320         if error:
321             self.logger.critical(error)
322             self.assertNotIn("failed", error)
323
324         # Delete inter-table routes
325         ip_t01.remove_vpp_config()
326         ip_t10.remove_vpp_config()
327
328
329 if __name__ == '__main__':
330     unittest.main(testRunner=VppTestRunner)