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