blob: d69052bc93906e1fd834c05fb1c089459ecf567a [file] [log] [blame]
Neale Ranns810086d2017-11-05 16:26:46 -08001#!/usr/bin/env python
Klement Sekerab9ef2732018-06-24 22:49:33 +02002import unittest
Neale Ranns810086d2017-11-05 16:26:46 -08003from framework import VppTestCase, VppTestRunner
Paul Vinciguerraa279d9c2019-02-28 09:00:09 -08004from vpp_udp_encap import find_udp_encap, VppUdpEncap
Neale Ranns31ed7442018-02-23 05:29:09 -08005from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpTable, VppMplsLabel
Neale Ranns810086d2017-11-05 16:26:46 -08006
7from scapy.packet import Raw
Klement Sekerab9ef2732018-06-24 22:49:33 +02008from scapy.layers.l2 import Ether
Neale Ranns810086d2017-11-05 16:26:46 -08009from scapy.layers.inet import IP, UDP
10from scapy.layers.inet6 import IPv6
11from scapy.contrib.mpls import MPLS
12
13
14class TestUdpEncap(VppTestCase):
15 """ UDP Encap Test Case """
16
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070017 @classmethod
18 def setUpClass(cls):
19 super(TestUdpEncap, cls).setUpClass()
20
21 @classmethod
22 def tearDownClass(cls):
23 super(TestUdpEncap, cls).tearDownClass()
24
Neale Ranns810086d2017-11-05 16:26:46 -080025 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):
Neale Ranns31ed7442018-02-23 05:29:09 -080078 self.assertEqual(rx[IP].src, tx[IP].src)
79 self.assertEqual(rx[IP].dst, tx[IP].dst)
Neale Ranns810086d2017-11-05 16:26:46 -080080 if ttl:
Neale Ranns31ed7442018-02-23 05:29:09 -080081 self.assertEqual(rx[IP].ttl, ttl)
Neale Ranns810086d2017-11-05 16:26:46 -080082 else:
Neale Ranns31ed7442018-02-23 05:29:09 -080083 self.assertEqual(rx[IP].ttl, tx[IP].ttl)
Neale Ranns810086d2017-11-05 16:26:46 -080084
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
Neale Ranns810086d2017-11-05 16:26:46 -080090 def test_udp_encap(self):
91 """ UDP Encap test
92 """
93
94 #
95 # construct a UDP encap object through each of the peers
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070096 # v4 through the first two peers, v6 through the second.
Neale Ranns810086d2017-11-05 16:26:46 -080097 #
Neale Ranns9c0a3c42018-09-07 08:57:41 -070098 udp_encap_0 = VppUdpEncap(self,
Neale Ranns810086d2017-11-05 16:26:46 -080099 self.pg0.local_ip4,
100 self.pg0.remote_ip4,
101 330, 440)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700102 udp_encap_1 = VppUdpEncap(self,
Neale Ranns810086d2017-11-05 16:26:46 -0800103 self.pg1.local_ip4,
104 self.pg1.remote_ip4,
105 331, 441,
106 table_id=1)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700107 udp_encap_2 = VppUdpEncap(self,
Neale Ranns810086d2017-11-05 16:26:46 -0800108 self.pg2.local_ip6,
109 self.pg2.remote_ip6,
110 332, 442,
Neale Rannsd0df49f2018-08-08 01:06:40 -0700111 table_id=2)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700112 udp_encap_3 = VppUdpEncap(self,
Neale Ranns810086d2017-11-05 16:26:46 -0800113 self.pg3.local_ip6,
114 self.pg3.remote_ip6,
115 333, 443,
Neale Rannsd0df49f2018-08-08 01:06:40 -0700116 table_id=3)
Neale Ranns810086d2017-11-05 16:26:46 -0800117 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
Neale Rannsd0df49f2018-08-08 01:06:40 -0700122 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
Neale Ranns810086d2017-11-05 16:26:46 -0800129 #
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,
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700136 next_hop_id=udp_encap_0.id)])
Neale Ranns810086d2017-11-05 16:26:46 -0800137 route_4o6 = VppIpRoute(self, "1.1.2.1", 32,
138 [VppRoutePath("0.0.0.0",
139 0xFFFFFFFF,
140 is_udp_encap=1,
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700141 next_hop_id=udp_encap_2.id)])
Neale Ranns810086d2017-11-05 16:26:46 -0800142 route_6o4 = VppIpRoute(self, "2001::1", 128,
143 [VppRoutePath("0.0.0.0",
144 0xFFFFFFFF,
145 is_udp_encap=1,
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700146 next_hop_id=udp_encap_1.id)],
Neale Ranns810086d2017-11-05 16:26:46 -0800147 is_ip6=1)
148 route_6o6 = VppIpRoute(self, "2001::3", 128,
149 [VppRoutePath("0.0.0.0",
150 0xFFFFFFFF,
151 is_udp_encap=1,
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700152 next_hop_id=udp_encap_3.id)],
Neale Ranns810086d2017-11-05 16:26:46 -0800153 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))
Neale Ranns52fae862018-01-08 04:41:42 -0800167 rx = self.send_and_expect(self.pg0, p_4o4*65, self.pg0)
Neale Ranns810086d2017-11-05 16:26:46 -0800168 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)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700172 self.assertEqual(udp_encap_0.get_stats()['packets'], 65)
Neale Ranns810086d2017-11-05 16:26:46 -0800173
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))
Neale Ranns52fae862018-01-08 04:41:42 -0800182 rx = self.send_and_expect(self.pg0, p_4o6*65, self.pg2)
Neale Ranns810086d2017-11-05 16:26:46 -0800183 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)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700187 self.assertEqual(udp_encap_2.get_stats()['packets'], 65)
Neale Ranns810086d2017-11-05 16:26:46 -0800188
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))
Neale Ranns52fae862018-01-08 04:41:42 -0800197 rx = self.send_and_expect(self.pg0, p_6o4*65, self.pg1)
Neale Ranns810086d2017-11-05 16:26:46 -0800198 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)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700202 self.assertEqual(udp_encap_1.get_stats()['packets'], 65)
Neale Ranns810086d2017-11-05 16:26:46 -0800203
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))
Neale Ranns52fae862018-01-08 04:41:42 -0800212 rx = self.send_and_expect(self.pg0, p_6o6*65, self.pg3)
Neale Ranns810086d2017-11-05 16:26:46 -0800213 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)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700217 self.assertEqual(udp_encap_3.get_stats()['packets'], 65)
Neale Ranns810086d2017-11-05 16:26:46 -0800218
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,
Neale Ranns31ed7442018-02-23 05:29:09 -0800228 labels=[VppMplsLabel(66)])])
Neale Ranns810086d2017-11-05 16:26:46 -0800229 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))
Neale Ranns52fae862018-01-08 04:41:42 -0800236 rx = self.send_and_expect(self.pg0, p_4omo4*65, self.pg1)
Neale Ranns810086d2017-11-05 16:26:46 -0800237 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)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700241 self.assertEqual(udp_encap_1.get_stats()['packets'], 130)
Neale Ranns810086d2017-11-05 16:26:46 -0800242
243
Florin Coras40903ac2018-06-10 14:41:23 -0700244class TestUDP(VppTestCase):
245 """ UDP Test Case """
246
247 @classmethod
248 def setUpClass(cls):
249 super(TestUDP, cls).setUpClass()
250
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700251 @classmethod
252 def tearDownClass(cls):
253 super(TestUDP, cls).tearDownClass()
254
Florin Coras40903ac2018-06-10 14:41:23 -0700255 def setUp(self):
256 super(TestUDP, self).setUp()
257 self.vapi.session_enable_disable(is_enabled=1)
Klement Sekerab9ef2732018-06-24 22:49:33 +0200258 self.create_loopback_interfaces(2)
Florin Coras40903ac2018-06-10 14:41:23 -0700259
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
Ole Troane1ade682019-03-04 23:55:43 +0100274 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)
Florin Coras40903ac2018-06-10 14:41:23 -0700278
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)
Paul Vinciguerra9a6dafd2019-03-06 15:11:28 -0800308 self.assertNotIn("failed", error)
Florin Coras40903ac2018-06-10 14:41:23 -0700309
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)
Paul Vinciguerra9a6dafd2019-03-06 15:11:28 -0800315 self.assertNotIn("failed", error)
Florin Coras40903ac2018-06-10 14:41:23 -0700316
317 # Delete inter-table routes
318 ip_t01.remove_vpp_config()
319 ip_t10.remove_vpp_config()
320
321
Neale Ranns810086d2017-11-05 16:26:46 -0800322if __name__ == '__main__':
323 unittest.main(testRunner=VppTestRunner)