blob: fc77434184c2e3eadeceb239d4d394847fd77efd [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
Neale Ranns097fa662018-05-01 05:17:55 -07004
Paul Vinciguerraa279d9c2019-02-28 09:00:09 -08005from vpp_udp_encap import find_udp_encap, VppUdpEncap
Neale Ranns097fa662018-05-01 05:17:55 -07006from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpTable, VppMplsLabel, \
7 FibPathType
Neale Ranns810086d2017-11-05 16:26:46 -08008
9from scapy.packet import Raw
Klement Sekerab9ef2732018-06-24 22:49:33 +020010from scapy.layers.l2 import Ether
Neale Ranns810086d2017-11-05 16:26:46 -080011from scapy.layers.inet import IP, UDP
12from scapy.layers.inet6 import IPv6
13from scapy.contrib.mpls import MPLS
14
Paul Vinciguerra4271c972019-05-14 13:25:49 -040015NUM_PKTS = 67
16
Neale Ranns810086d2017-11-05 16:26:46 -080017
18class TestUdpEncap(VppTestCase):
19 """ UDP Encap Test Case """
20
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070021 @classmethod
22 def setUpClass(cls):
23 super(TestUdpEncap, cls).setUpClass()
24
25 @classmethod
26 def tearDownClass(cls):
27 super(TestUdpEncap, cls).tearDownClass()
28
Neale Ranns810086d2017-11-05 16:26:46 -080029 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):
Neale Ranns31ed7442018-02-23 05:29:09 -080082 self.assertEqual(rx[IP].src, tx[IP].src)
83 self.assertEqual(rx[IP].dst, tx[IP].dst)
Neale Ranns810086d2017-11-05 16:26:46 -080084 if ttl:
Neale Ranns31ed7442018-02-23 05:29:09 -080085 self.assertEqual(rx[IP].ttl, ttl)
Neale Ranns810086d2017-11-05 16:26:46 -080086 else:
Neale Ranns31ed7442018-02-23 05:29:09 -080087 self.assertEqual(rx[IP].ttl, tx[IP].ttl)
Neale Ranns810086d2017-11-05 16:26:46 -080088
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
Neale Ranns810086d2017-11-05 16:26:46 -080094 def test_udp_encap(self):
95 """ UDP Encap test
96 """
97
98 #
99 # construct a UDP encap object through each of the peers
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700100 # v4 through the first two peers, v6 through the second.
Neale Ranns810086d2017-11-05 16:26:46 -0800101 #
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700102 udp_encap_0 = VppUdpEncap(self,
Neale Ranns810086d2017-11-05 16:26:46 -0800103 self.pg0.local_ip4,
104 self.pg0.remote_ip4,
105 330, 440)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700106 udp_encap_1 = VppUdpEncap(self,
Neale Ranns810086d2017-11-05 16:26:46 -0800107 self.pg1.local_ip4,
108 self.pg1.remote_ip4,
109 331, 441,
110 table_id=1)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700111 udp_encap_2 = VppUdpEncap(self,
Neale Ranns810086d2017-11-05 16:26:46 -0800112 self.pg2.local_ip6,
113 self.pg2.remote_ip6,
114 332, 442,
Neale Rannsd0df49f2018-08-08 01:06:40 -0700115 table_id=2)
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700116 udp_encap_3 = VppUdpEncap(self,
Neale Ranns810086d2017-11-05 16:26:46 -0800117 self.pg3.local_ip6,
118 self.pg3.remote_ip6,
119 333, 443,
Neale Rannsd0df49f2018-08-08 01:06:40 -0700120 table_id=3)
Neale Ranns810086d2017-11-05 16:26:46 -0800121 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
Neale Rannsd0df49f2018-08-08 01:06:40 -0700126 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
Neale Ranns810086d2017-11-05 16:26:46 -0800133 #
134 # Routes via each UDP encap object - all combinations of v4 and v6.
135 #
Neale Ranns097fa662018-05-01 05:17:55 -0700136 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)])
Neale Ranns810086d2017-11-05 16:26:46 -0800160 route_4o6.add_vpp_config()
161 route_6o6.add_vpp_config()
162 route_6o4.add_vpp_config()
Neale Ranns097fa662018-05-01 05:17:55 -0700163 route_4o4.add_vpp_config()
Neale Ranns810086d2017-11-05 16:26:46 -0800164
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))
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400173 rx = self.send_and_expect(self.pg0, p_4o4*NUM_PKTS, self.pg0)
Neale Ranns810086d2017-11-05 16:26:46 -0800174 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)
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400178 self.assertEqual(udp_encap_0.get_stats()['packets'], NUM_PKTS)
Neale Ranns810086d2017-11-05 16:26:46 -0800179
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))
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400188 rx = self.send_and_expect(self.pg0, p_4o6*NUM_PKTS, self.pg2)
Neale Ranns810086d2017-11-05 16:26:46 -0800189 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)
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400193 self.assertEqual(udp_encap_2.get_stats()['packets'], NUM_PKTS)
Neale Ranns810086d2017-11-05 16:26:46 -0800194
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))
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400203 rx = self.send_and_expect(self.pg0, p_6o4*NUM_PKTS, self.pg1)
Neale Ranns810086d2017-11-05 16:26:46 -0800204 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)
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400208 self.assertEqual(udp_encap_1.get_stats()['packets'], NUM_PKTS)
Neale Ranns810086d2017-11-05 16:26:46 -0800209
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))
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400218 rx = self.send_and_expect(self.pg0, p_6o6*NUM_PKTS, self.pg3)
Neale Ranns810086d2017-11-05 16:26:46 -0800219 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)
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400223 self.assertEqual(udp_encap_3.get_stats()['packets'], NUM_PKTS)
Neale Ranns810086d2017-11-05 16:26:46 -0800224
225 #
226 # A route with an output label
227 # the TTL of the inner packet is decremented on LSP ingress
228 #
Neale Ranns097fa662018-05-01 05:17:55 -0700229 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)])])
Neale Ranns810086d2017-11-05 16:26:46 -0800236 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))
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400243 rx = self.send_and_expect(self.pg0, p_4omo4*NUM_PKTS, self.pg1)
Neale Ranns810086d2017-11-05 16:26:46 -0800244 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)
Paul Vinciguerra4271c972019-05-14 13:25:49 -0400248 self.assertEqual(udp_encap_1.get_stats()['packets'], 2*NUM_PKTS)
Neale Ranns810086d2017-11-05 16:26:46 -0800249
250
Florin Coras40903ac2018-06-10 14:41:23 -0700251class TestUDP(VppTestCase):
252 """ UDP Test Case """
253
254 @classmethod
255 def setUpClass(cls):
256 super(TestUDP, cls).setUpClass()
257
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700258 @classmethod
259 def tearDownClass(cls):
260 super(TestUDP, cls).tearDownClass()
261
Florin Coras40903ac2018-06-10 14:41:23 -0700262 def setUp(self):
263 super(TestUDP, self).setUp()
264 self.vapi.session_enable_disable(is_enabled=1)
Klement Sekerab9ef2732018-06-24 22:49:33 +0200265 self.create_loopback_interfaces(2)
Florin Coras40903ac2018-06-10 14:41:23 -0700266
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
Ole Troane1ade682019-03-04 23:55:43 +0100281 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)
Florin Coras40903ac2018-06-10 14:41:23 -0700285
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)
Paul Vinciguerra9a6dafd2019-03-06 15:11:28 -0800315 self.assertNotIn("failed", error)
Florin Coras40903ac2018-06-10 14:41:23 -0700316
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)
Paul Vinciguerra9a6dafd2019-03-06 15:11:28 -0800322 self.assertNotIn("failed", error)
Florin Coras40903ac2018-06-10 14:41:23 -0700323
324 # Delete inter-table routes
325 ip_t01.remove_vpp_config()
326 ip_t10.remove_vpp_config()
327
328
Neale Ranns810086d2017-11-05 16:26:46 -0800329if __name__ == '__main__':
330 unittest.main(testRunner=VppTestRunner)