blob: 230335ff169c96ee2681386c710eb23e86325057 [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
Klement Sekerab9ef2732018-06-24 22:49:33 +02004from vpp_udp_encap import 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
88 # v4 through the first two peears, v6 through the second.
89 #
90 udp_encap_0 = VppUdpEncap(self, 0,
91 self.pg0.local_ip4,
92 self.pg0.remote_ip4,
93 330, 440)
94 udp_encap_1 = VppUdpEncap(self, 1,
95 self.pg1.local_ip4,
96 self.pg1.remote_ip4,
97 331, 441,
98 table_id=1)
99 udp_encap_2 = VppUdpEncap(self, 2,
100 self.pg2.local_ip6,
101 self.pg2.remote_ip6,
102 332, 442,
103 table_id=2,
104 is_ip6=1)
105 udp_encap_3 = VppUdpEncap(self, 3,
106 self.pg3.local_ip6,
107 self.pg3.remote_ip6,
108 333, 443,
109 table_id=3,
110 is_ip6=1)
111 udp_encap_0.add_vpp_config()
112 udp_encap_1.add_vpp_config()
113 udp_encap_2.add_vpp_config()
114 udp_encap_3.add_vpp_config()
115
116 #
117 # Routes via each UDP encap object - all combinations of v4 and v6.
118 #
119 route_4o4 = VppIpRoute(self, "1.1.0.1", 32,
120 [VppRoutePath("0.0.0.0",
121 0xFFFFFFFF,
122 is_udp_encap=1,
123 next_hop_id=0)])
124 route_4o6 = VppIpRoute(self, "1.1.2.1", 32,
125 [VppRoutePath("0.0.0.0",
126 0xFFFFFFFF,
127 is_udp_encap=1,
128 next_hop_id=2)])
129 route_6o4 = VppIpRoute(self, "2001::1", 128,
130 [VppRoutePath("0.0.0.0",
131 0xFFFFFFFF,
132 is_udp_encap=1,
133 next_hop_id=1)],
134 is_ip6=1)
135 route_6o6 = VppIpRoute(self, "2001::3", 128,
136 [VppRoutePath("0.0.0.0",
137 0xFFFFFFFF,
138 is_udp_encap=1,
139 next_hop_id=3)],
140 is_ip6=1)
141 route_4o4.add_vpp_config()
142 route_4o6.add_vpp_config()
143 route_6o6.add_vpp_config()
144 route_6o4.add_vpp_config()
145
146 #
147 # 4o4 encap
148 #
149 p_4o4 = (Ether(src=self.pg0.remote_mac,
150 dst=self.pg0.local_mac) /
151 IP(src="2.2.2.2", dst="1.1.0.1") /
152 UDP(sport=1234, dport=1234) /
153 Raw('\xa5' * 100))
Neale Ranns52fae862018-01-08 04:41:42 -0800154 rx = self.send_and_expect(self.pg0, p_4o4*65, self.pg0)
Neale Ranns810086d2017-11-05 16:26:46 -0800155 for p in rx:
156 self.validate_outer4(p, udp_encap_0)
157 p = IP(p["UDP"].payload.load)
158 self.validate_inner4(p, p_4o4)
159
160 #
161 # 4o6 encap
162 #
163 p_4o6 = (Ether(src=self.pg0.remote_mac,
164 dst=self.pg0.local_mac) /
165 IP(src="2.2.2.2", dst="1.1.2.1") /
166 UDP(sport=1234, dport=1234) /
167 Raw('\xa5' * 100))
Neale Ranns52fae862018-01-08 04:41:42 -0800168 rx = self.send_and_expect(self.pg0, p_4o6*65, self.pg2)
Neale Ranns810086d2017-11-05 16:26:46 -0800169 for p in rx:
170 self.validate_outer6(p, udp_encap_2)
171 p = IP(p["UDP"].payload.load)
172 self.validate_inner4(p, p_4o6)
173
174 #
175 # 6o4 encap
176 #
177 p_6o4 = (Ether(src=self.pg0.remote_mac,
178 dst=self.pg0.local_mac) /
179 IPv6(src="2001::100", dst="2001::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_6o4*65, self.pg1)
Neale Ranns810086d2017-11-05 16:26:46 -0800183 for p in rx:
184 self.validate_outer4(p, udp_encap_1)
185 p = IPv6(p["UDP"].payload.load)
186 self.validate_inner6(p, p_6o4)
187
188 #
189 # 6o6 encap
190 #
191 p_6o6 = (Ether(src=self.pg0.remote_mac,
192 dst=self.pg0.local_mac) /
193 IPv6(src="2001::100", dst="2001::3") /
194 UDP(sport=1234, dport=1234) /
195 Raw('\xa5' * 100))
Neale Ranns52fae862018-01-08 04:41:42 -0800196 rx = self.send_and_expect(self.pg0, p_6o6*65, self.pg3)
Neale Ranns810086d2017-11-05 16:26:46 -0800197 for p in rx:
198 self.validate_outer6(p, udp_encap_3)
199 p = IPv6(p["UDP"].payload.load)
200 self.validate_inner6(p, p_6o6)
201
202 #
203 # A route with an output label
204 # the TTL of the inner packet is decremented on LSP ingress
205 #
206 route_4oMPLSo4 = VppIpRoute(self, "1.1.2.22", 32,
207 [VppRoutePath("0.0.0.0",
208 0xFFFFFFFF,
209 is_udp_encap=1,
210 next_hop_id=1,
Neale Ranns31ed7442018-02-23 05:29:09 -0800211 labels=[VppMplsLabel(66)])])
Neale Ranns810086d2017-11-05 16:26:46 -0800212 route_4oMPLSo4.add_vpp_config()
213
214 p_4omo4 = (Ether(src=self.pg0.remote_mac,
215 dst=self.pg0.local_mac) /
216 IP(src="2.2.2.2", dst="1.1.2.22") /
217 UDP(sport=1234, dport=1234) /
218 Raw('\xa5' * 100))
Neale Ranns52fae862018-01-08 04:41:42 -0800219 rx = self.send_and_expect(self.pg0, p_4omo4*65, self.pg1)
Neale Ranns810086d2017-11-05 16:26:46 -0800220 for p in rx:
221 self.validate_outer4(p, udp_encap_1)
222 p = MPLS(p["UDP"].payload.load)
223 self.validate_inner4(p, p_4omo4, ttl=63)
224
225
Florin Coras40903ac2018-06-10 14:41:23 -0700226class TestUDP(VppTestCase):
227 """ UDP Test Case """
228
229 @classmethod
230 def setUpClass(cls):
231 super(TestUDP, cls).setUpClass()
232
233 def setUp(self):
234 super(TestUDP, self).setUp()
235 self.vapi.session_enable_disable(is_enabled=1)
Klement Sekerab9ef2732018-06-24 22:49:33 +0200236 self.create_loopback_interfaces(2)
Florin Coras40903ac2018-06-10 14:41:23 -0700237
238 table_id = 0
239
240 for i in self.lo_interfaces:
241 i.admin_up()
242
243 if table_id != 0:
244 tbl = VppIpTable(self, table_id)
245 tbl.add_vpp_config()
246
247 i.set_table_ip4(table_id)
248 i.config_ip4()
249 table_id += 1
250
251 # Configure namespaces
252 self.vapi.app_namespace_add(namespace_id="0",
253 sw_if_index=self.loop0.sw_if_index)
254 self.vapi.app_namespace_add(namespace_id="1",
255 sw_if_index=self.loop1.sw_if_index)
256
257 def tearDown(self):
258 for i in self.lo_interfaces:
259 i.unconfig_ip4()
260 i.set_table_ip4(0)
261 i.admin_down()
262 self.vapi.session_enable_disable(is_enabled=0)
263 super(TestUDP, self).tearDown()
264
265 def test_udp_transfer(self):
266 """ UDP echo client/server transfer """
267
268 # Add inter-table routes
269 ip_t01 = VppIpRoute(self, self.loop1.local_ip4, 32,
270 [VppRoutePath("0.0.0.0",
271 0xffffffff,
272 nh_table_id=1)])
273 ip_t10 = VppIpRoute(self, self.loop0.local_ip4, 32,
274 [VppRoutePath("0.0.0.0",
275 0xffffffff,
276 nh_table_id=0)], table_id=1)
277 ip_t01.add_vpp_config()
278 ip_t10.add_vpp_config()
279
280 # Start builtin server and client
281 uri = "udp://" + self.loop0.local_ip4 + "/1234"
282 error = self.vapi.cli("test echo server appns 0 fifo-size 4 no-echo" +
283 "uri " + uri)
284 if error:
285 self.logger.critical(error)
286 self.assertEqual(error.find("failed"), -1)
287
288 error = self.vapi.cli("test echo client mbytes 10 appns 1 " +
289 "fifo-size 4 no-output test-bytes " +
290 "syn-timeout 2 no-return uri " + uri)
291 if error:
292 self.logger.critical(error)
293 self.assertEqual(error.find("failed"), -1)
294
295 # Delete inter-table routes
296 ip_t01.remove_vpp_config()
297 ip_t10.remove_vpp_config()
298
299
Neale Ranns810086d2017-11-05 16:26:46 -0800300if __name__ == '__main__':
301 unittest.main(testRunner=VppTestRunner)