blob: 3010a1dd7e14f13b188a1d976a4ac6c60c70ac92 [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
17 def setUp(self):
18 super(TestUdpEncap, self).setUp()
19
20 # create 2 pg interfaces
21 self.create_pg_interfaces(range(4))
22
23 # setup interfaces
24 # assign them different tables.
25 table_id = 0
26 self.tables = []
27
28 for i in self.pg_interfaces:
29 i.admin_up()
30
31 if table_id != 0:
32 tbl = VppIpTable(self, table_id)
33 tbl.add_vpp_config()
34 self.tables.append(tbl)
35 tbl = VppIpTable(self, table_id, is_ip6=1)
36 tbl.add_vpp_config()
37 self.tables.append(tbl)
38
39 i.set_table_ip4(table_id)
40 i.set_table_ip6(table_id)
41 i.config_ip4()
42 i.resolve_arp()
43 i.config_ip6()
44 i.resolve_ndp()
45 table_id += 1
46
47 def tearDown(self):
48 for i in self.pg_interfaces:
49 i.unconfig_ip4()
50 i.unconfig_ip6()
51 i.ip6_disable()
52 i.set_table_ip4(0)
53 i.set_table_ip6(0)
54 i.admin_down()
55 super(TestUdpEncap, self).tearDown()
56
57 def validate_outer4(self, rx, encap_obj):
58 self.assertEqual(rx[IP].src, encap_obj.src_ip_s)
59 self.assertEqual(rx[IP].dst, encap_obj.dst_ip_s)
60 self.assertEqual(rx[UDP].sport, encap_obj.src_port)
61 self.assertEqual(rx[UDP].dport, encap_obj.dst_port)
62
63 def validate_outer6(self, rx, encap_obj):
64 self.assertEqual(rx[IPv6].src, encap_obj.src_ip_s)
65 self.assertEqual(rx[IPv6].dst, encap_obj.dst_ip_s)
66 self.assertEqual(rx[UDP].sport, encap_obj.src_port)
67 self.assertEqual(rx[UDP].dport, encap_obj.dst_port)
68
69 def validate_inner4(self, rx, tx, ttl=None):
Neale Ranns31ed7442018-02-23 05:29:09 -080070 self.assertEqual(rx[IP].src, tx[IP].src)
71 self.assertEqual(rx[IP].dst, tx[IP].dst)
Neale Ranns810086d2017-11-05 16:26:46 -080072 if ttl:
Neale Ranns31ed7442018-02-23 05:29:09 -080073 self.assertEqual(rx[IP].ttl, ttl)
Neale Ranns810086d2017-11-05 16:26:46 -080074 else:
Neale Ranns31ed7442018-02-23 05:29:09 -080075 self.assertEqual(rx[IP].ttl, tx[IP].ttl)
Neale Ranns810086d2017-11-05 16:26:46 -080076
77 def validate_inner6(self, rx, tx):
78 self.assertEqual(rx.src, tx[IPv6].src)
79 self.assertEqual(rx.dst, tx[IPv6].dst)
80 self.assertEqual(rx.hlim, tx[IPv6].hlim)
81
Neale Ranns810086d2017-11-05 16:26:46 -080082 def test_udp_encap(self):
83 """ UDP Encap test
84 """
85
86 #
87 # construct a UDP encap object through each of the peers
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070088 # v4 through the first two peers, v6 through the second.
Neale Ranns810086d2017-11-05 16:26:46 -080089 #
Neale Ranns9c0a3c42018-09-07 08:57:41 -070090 udp_encap_0 = VppUdpEncap(self,
Neale Ranns810086d2017-11-05 16:26:46 -080091 self.pg0.local_ip4,
92 self.pg0.remote_ip4,
93 330, 440)
Neale Ranns9c0a3c42018-09-07 08:57:41 -070094 udp_encap_1 = VppUdpEncap(self,
Neale Ranns810086d2017-11-05 16:26:46 -080095 self.pg1.local_ip4,
96 self.pg1.remote_ip4,
97 331, 441,
98 table_id=1)
Neale Ranns9c0a3c42018-09-07 08:57:41 -070099 udp_encap_2 = VppUdpEncap(self,
Neale Ranns810086d2017-11-05 16:26:46 -0800100 self.pg2.local_ip6,
101 self.pg2.remote_ip6,
102 332, 442,
Neale Rannsd0df49f2018-08-08 01:06:40 -0700103 table_id=2)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700104 udp_encap_3 = VppUdpEncap(self,
Neale Ranns810086d2017-11-05 16:26:46 -0800105 self.pg3.local_ip6,
106 self.pg3.remote_ip6,
107 333, 443,
Neale Rannsd0df49f2018-08-08 01:06:40 -0700108 table_id=3)
Neale Ranns810086d2017-11-05 16:26:46 -0800109 udp_encap_0.add_vpp_config()
110 udp_encap_1.add_vpp_config()
111 udp_encap_2.add_vpp_config()
112 udp_encap_3.add_vpp_config()
113
Neale Rannsd0df49f2018-08-08 01:06:40 -0700114 self.logger.info(self.vapi.cli("sh udp encap"))
115
116 self.assertTrue(find_udp_encap(self, udp_encap_2))
117 self.assertTrue(find_udp_encap(self, udp_encap_3))
118 self.assertTrue(find_udp_encap(self, udp_encap_0))
119 self.assertTrue(find_udp_encap(self, udp_encap_1))
120
Neale Ranns810086d2017-11-05 16:26:46 -0800121 #
122 # Routes via each UDP encap object - all combinations of v4 and v6.
123 #
124 route_4o4 = VppIpRoute(self, "1.1.0.1", 32,
125 [VppRoutePath("0.0.0.0",
126 0xFFFFFFFF,
127 is_udp_encap=1,
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700128 next_hop_id=udp_encap_0.id)])
Neale Ranns810086d2017-11-05 16:26:46 -0800129 route_4o6 = VppIpRoute(self, "1.1.2.1", 32,
130 [VppRoutePath("0.0.0.0",
131 0xFFFFFFFF,
132 is_udp_encap=1,
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700133 next_hop_id=udp_encap_2.id)])
Neale Ranns810086d2017-11-05 16:26:46 -0800134 route_6o4 = VppIpRoute(self, "2001::1", 128,
135 [VppRoutePath("0.0.0.0",
136 0xFFFFFFFF,
137 is_udp_encap=1,
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700138 next_hop_id=udp_encap_1.id)],
Neale Ranns810086d2017-11-05 16:26:46 -0800139 is_ip6=1)
140 route_6o6 = VppIpRoute(self, "2001::3", 128,
141 [VppRoutePath("0.0.0.0",
142 0xFFFFFFFF,
143 is_udp_encap=1,
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700144 next_hop_id=udp_encap_3.id)],
Neale Ranns810086d2017-11-05 16:26:46 -0800145 is_ip6=1)
146 route_4o4.add_vpp_config()
147 route_4o6.add_vpp_config()
148 route_6o6.add_vpp_config()
149 route_6o4.add_vpp_config()
150
151 #
152 # 4o4 encap
153 #
154 p_4o4 = (Ether(src=self.pg0.remote_mac,
155 dst=self.pg0.local_mac) /
156 IP(src="2.2.2.2", dst="1.1.0.1") /
157 UDP(sport=1234, dport=1234) /
158 Raw('\xa5' * 100))
Neale Ranns52fae862018-01-08 04:41:42 -0800159 rx = self.send_and_expect(self.pg0, p_4o4*65, self.pg0)
Neale Ranns810086d2017-11-05 16:26:46 -0800160 for p in rx:
161 self.validate_outer4(p, udp_encap_0)
162 p = IP(p["UDP"].payload.load)
163 self.validate_inner4(p, p_4o4)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700164 self.assertEqual(udp_encap_0.get_stats()['packets'], 65)
Neale Ranns810086d2017-11-05 16:26:46 -0800165
166 #
167 # 4o6 encap
168 #
169 p_4o6 = (Ether(src=self.pg0.remote_mac,
170 dst=self.pg0.local_mac) /
171 IP(src="2.2.2.2", dst="1.1.2.1") /
172 UDP(sport=1234, dport=1234) /
173 Raw('\xa5' * 100))
Neale Ranns52fae862018-01-08 04:41:42 -0800174 rx = self.send_and_expect(self.pg0, p_4o6*65, self.pg2)
Neale Ranns810086d2017-11-05 16:26:46 -0800175 for p in rx:
176 self.validate_outer6(p, udp_encap_2)
177 p = IP(p["UDP"].payload.load)
178 self.validate_inner4(p, p_4o6)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700179 self.assertEqual(udp_encap_2.get_stats()['packets'], 65)
Neale Ranns810086d2017-11-05 16:26:46 -0800180
181 #
182 # 6o4 encap
183 #
184 p_6o4 = (Ether(src=self.pg0.remote_mac,
185 dst=self.pg0.local_mac) /
186 IPv6(src="2001::100", dst="2001::1") /
187 UDP(sport=1234, dport=1234) /
188 Raw('\xa5' * 100))
Neale Ranns52fae862018-01-08 04:41:42 -0800189 rx = self.send_and_expect(self.pg0, p_6o4*65, self.pg1)
Neale Ranns810086d2017-11-05 16:26:46 -0800190 for p in rx:
191 self.validate_outer4(p, udp_encap_1)
192 p = IPv6(p["UDP"].payload.load)
193 self.validate_inner6(p, p_6o4)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700194 self.assertEqual(udp_encap_1.get_stats()['packets'], 65)
Neale Ranns810086d2017-11-05 16:26:46 -0800195
196 #
197 # 6o6 encap
198 #
199 p_6o6 = (Ether(src=self.pg0.remote_mac,
200 dst=self.pg0.local_mac) /
201 IPv6(src="2001::100", dst="2001::3") /
202 UDP(sport=1234, dport=1234) /
203 Raw('\xa5' * 100))
Neale Ranns52fae862018-01-08 04:41:42 -0800204 rx = self.send_and_expect(self.pg0, p_6o6*65, self.pg3)
Neale Ranns810086d2017-11-05 16:26:46 -0800205 for p in rx:
206 self.validate_outer6(p, udp_encap_3)
207 p = IPv6(p["UDP"].payload.load)
208 self.validate_inner6(p, p_6o6)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700209 self.assertEqual(udp_encap_3.get_stats()['packets'], 65)
Neale Ranns810086d2017-11-05 16:26:46 -0800210
211 #
212 # A route with an output label
213 # the TTL of the inner packet is decremented on LSP ingress
214 #
215 route_4oMPLSo4 = VppIpRoute(self, "1.1.2.22", 32,
216 [VppRoutePath("0.0.0.0",
217 0xFFFFFFFF,
218 is_udp_encap=1,
219 next_hop_id=1,
Neale Ranns31ed7442018-02-23 05:29:09 -0800220 labels=[VppMplsLabel(66)])])
Neale Ranns810086d2017-11-05 16:26:46 -0800221 route_4oMPLSo4.add_vpp_config()
222
223 p_4omo4 = (Ether(src=self.pg0.remote_mac,
224 dst=self.pg0.local_mac) /
225 IP(src="2.2.2.2", dst="1.1.2.22") /
226 UDP(sport=1234, dport=1234) /
227 Raw('\xa5' * 100))
Neale Ranns52fae862018-01-08 04:41:42 -0800228 rx = self.send_and_expect(self.pg0, p_4omo4*65, self.pg1)
Neale Ranns810086d2017-11-05 16:26:46 -0800229 for p in rx:
230 self.validate_outer4(p, udp_encap_1)
231 p = MPLS(p["UDP"].payload.load)
232 self.validate_inner4(p, p_4omo4, ttl=63)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700233 self.assertEqual(udp_encap_1.get_stats()['packets'], 130)
Neale Ranns810086d2017-11-05 16:26:46 -0800234
235
Florin Coras40903ac2018-06-10 14:41:23 -0700236class TestUDP(VppTestCase):
237 """ UDP Test Case """
238
239 @classmethod
240 def setUpClass(cls):
241 super(TestUDP, cls).setUpClass()
242
243 def setUp(self):
244 super(TestUDP, self).setUp()
245 self.vapi.session_enable_disable(is_enabled=1)
Klement Sekerab9ef2732018-06-24 22:49:33 +0200246 self.create_loopback_interfaces(2)
Florin Coras40903ac2018-06-10 14:41:23 -0700247
248 table_id = 0
249
250 for i in self.lo_interfaces:
251 i.admin_up()
252
253 if table_id != 0:
254 tbl = VppIpTable(self, table_id)
255 tbl.add_vpp_config()
256
257 i.set_table_ip4(table_id)
258 i.config_ip4()
259 table_id += 1
260
261 # Configure namespaces
Ole Troane1ade682019-03-04 23:55:43 +0100262 self.vapi.app_namespace_add_del(namespace_id="0",
263 sw_if_index=self.loop0.sw_if_index)
264 self.vapi.app_namespace_add_del(namespace_id="1",
265 sw_if_index=self.loop1.sw_if_index)
Florin Coras40903ac2018-06-10 14:41:23 -0700266
267 def tearDown(self):
268 for i in self.lo_interfaces:
269 i.unconfig_ip4()
270 i.set_table_ip4(0)
271 i.admin_down()
272 self.vapi.session_enable_disable(is_enabled=0)
273 super(TestUDP, self).tearDown()
274
275 def test_udp_transfer(self):
276 """ UDP echo client/server transfer """
277
278 # Add inter-table routes
279 ip_t01 = VppIpRoute(self, self.loop1.local_ip4, 32,
280 [VppRoutePath("0.0.0.0",
281 0xffffffff,
282 nh_table_id=1)])
283 ip_t10 = VppIpRoute(self, self.loop0.local_ip4, 32,
284 [VppRoutePath("0.0.0.0",
285 0xffffffff,
286 nh_table_id=0)], table_id=1)
287 ip_t01.add_vpp_config()
288 ip_t10.add_vpp_config()
289
290 # Start builtin server and client
291 uri = "udp://" + self.loop0.local_ip4 + "/1234"
292 error = self.vapi.cli("test echo server appns 0 fifo-size 4 no-echo" +
293 "uri " + uri)
294 if error:
295 self.logger.critical(error)
Paul Vinciguerra9a6dafd2019-03-06 15:11:28 -0800296 self.assertNotIn("failed", error)
Florin Coras40903ac2018-06-10 14:41:23 -0700297
298 error = self.vapi.cli("test echo client mbytes 10 appns 1 " +
299 "fifo-size 4 no-output test-bytes " +
300 "syn-timeout 2 no-return uri " + uri)
301 if error:
302 self.logger.critical(error)
Paul Vinciguerra9a6dafd2019-03-06 15:11:28 -0800303 self.assertNotIn("failed", error)
Florin Coras40903ac2018-06-10 14:41:23 -0700304
305 # Delete inter-table routes
306 ip_t01.remove_vpp_config()
307 ip_t10.remove_vpp_config()
308
309
Neale Ranns810086d2017-11-05 16:26:46 -0800310if __name__ == '__main__':
311 unittest.main(testRunner=VppTestRunner)