blob: e723950b7a7530c20dc788572bad42252ec7f9ac [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Ole Troan46c1c112018-03-14 20:39:40 +01002"""IP{4,6} over IP{v,6} tunnel functional tests"""
Ole Troan298c6952018-03-08 12:30:43 +01003
4import unittest
Neale Ranns0b6a8572019-10-30 17:34:14 +00005from scapy.layers.inet6 import IPv6, Ether, IP, UDP, IPv6ExtHdrFragment, Raw
Ole Troan282093f2018-09-19 12:38:51 +02006from scapy.all import fragment, fragment6, RandShort, defragment6
Ole Troan46c1c112018-03-14 20:39:40 +01007from framework import VppTestCase, VppTestRunner
Neale Rannsc0a93142018-09-05 15:42:26 -07008from vpp_ip import DpoProto
Neale Ranns097fa662018-05-01 05:17:55 -07009from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpTable, FibPathProto
Ole Troan46c1c112018-03-14 20:39:40 +010010from socket import AF_INET, AF_INET6, inet_pton
Ole Troan7f991832018-12-06 17:35:12 +010011from util import reassemble4
12
Ole Troan298c6952018-03-08 12:30:43 +010013""" Testipip is a subclass of VPPTestCase classes.
14
15IPIP tests.
16
17"""
18
19
Neale Rannscbd08242019-05-26 11:34:27 -070020def ipip_add_tunnel(test, src, dst, table_id=0, tc_tos=0xff):
21 """ Add a IPIP tunnel """
22 return test.vapi.ipip_add_tunnel(
23 tunnel={
24 'src': src,
25 'dst': dst,
26 'table_id': table_id,
27 'instance': 0xffffffff,
28 'tc_tos': tc_tos
29 }
30 )
31
32
Ole Troan298c6952018-03-08 12:30:43 +010033class TestIPIP(VppTestCase):
34 """ IPIP Test Case """
35
36 @classmethod
37 def setUpClass(cls):
38 super(TestIPIP, cls).setUpClass()
Ole Troan46c1c112018-03-14 20:39:40 +010039 cls.create_pg_interfaces(range(2))
40 cls.interfaces = list(cls.pg_interfaces)
Ole Troan298c6952018-03-08 12:30:43 +010041
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070042 @classmethod
43 def tearDownClass(cls):
44 super(TestIPIP, cls).tearDownClass()
45
Paul Vinciguerra741865b2018-11-27 06:01:22 -080046 def setUp(self):
47 super(TestIPIP, self).setUp()
48 for i in self.interfaces:
Ole Troan46c1c112018-03-14 20:39:40 +010049 i.admin_up()
50 i.config_ip4()
51 i.config_ip6()
52 i.disable_ipv6_ra()
53 i.resolve_arp()
54 i.resolve_ndp()
Ole Troan298c6952018-03-08 12:30:43 +010055
56 def tearDown(self):
57 super(TestIPIP, self).tearDown()
58 if not self.vpp_dead:
Ole Troan46c1c112018-03-14 20:39:40 +010059 for i in self.pg_interfaces:
60 i.unconfig_ip4()
61 i.unconfig_ip6()
62 i.admin_down()
Ole Troan298c6952018-03-08 12:30:43 +010063
64 def validate(self, rx, expected):
Ole Troan7f991832018-12-06 17:35:12 +010065 self.assertEqual(rx, expected.__class__(expected))
Ole Troan298c6952018-03-08 12:30:43 +010066
Ole Troan282093f2018-09-19 12:38:51 +020067 def generate_ip4_frags(self, payload_length, fragment_size):
Ole Troan7eb9d962018-08-10 14:39:48 +020068 p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
69 p_payload = UDP(sport=1234, dport=1234) / self.payload(payload_length)
70 p_ip4 = IP(src="1.2.3.4", dst=self.pg0.remote_ip4)
71 outer_ip4 = (p_ether / IP(src=self.pg1.remote_ip4,
72 id=RandShort(),
73 dst=self.pg0.local_ip4) / p_ip4 / p_payload)
74 frags = fragment(outer_ip4, fragment_size)
75 p4_reply = (p_ip4 / p_payload)
76 p4_reply.ttl -= 1
77 return frags, p4_reply
78
Ole Troan298c6952018-03-08 12:30:43 +010079 def test_ipip4(self):
80 """ ip{v4,v6} over ip4 test """
81 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
Ole Troand57f6362018-05-24 13:21:43 +020082 p_ip6 = IPv6(src="1::1", dst="DEAD::1", nh='UDP', tc=42)
83 p_ip4 = IP(src="1.2.3.4", dst="130.67.0.1", tos=42)
Neale Ranns0b6a8572019-10-30 17:34:14 +000084 p_payload = UDP(sport=1234, dport=1234) / Raw(b'X' * 100)
Ole Troan298c6952018-03-08 12:30:43 +010085
86 # IPv4 transport
Neale Rannscbd08242019-05-26 11:34:27 -070087 rv = ipip_add_tunnel(self,
88 self.pg0.local_ip4,
89 self.pg1.remote_ip4,
90 tc_tos=0xFF)
Ole Troan298c6952018-03-08 12:30:43 +010091 sw_if_index = rv.sw_if_index
92
93 # Set interface up and enable IP on it
Ole Troan46c1c112018-03-14 20:39:40 +010094 self.vapi.sw_interface_set_flags(sw_if_index, 1)
95 self.vapi.sw_interface_set_unnumbered(
Ole Troan9a475372019-03-05 16:58:24 +010096 sw_if_index=self.pg0.sw_if_index,
97 unnumbered_sw_if_index=sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +010098
99 # Add IPv4 and IPv6 routes via tunnel interface
100 ip4_via_tunnel = VppIpRoute(
101 self, "130.67.0.0", 16,
102 [VppRoutePath("0.0.0.0",
103 sw_if_index,
Neale Ranns097fa662018-05-01 05:17:55 -0700104 proto=FibPathProto.FIB_PATH_NH_PROTO_IP4)])
Ole Troan298c6952018-03-08 12:30:43 +0100105 ip4_via_tunnel.add_vpp_config()
106
107 ip6_via_tunnel = VppIpRoute(
108 self, "dead::", 16,
109 [VppRoutePath("::",
110 sw_if_index,
Neale Ranns097fa662018-05-01 05:17:55 -0700111 proto=FibPathProto.FIB_PATH_NH_PROTO_IP6)])
Ole Troan298c6952018-03-08 12:30:43 +0100112 ip6_via_tunnel.add_vpp_config()
113
114 # IPv6 in to IPv4 tunnel
115 p6 = (p_ether / p_ip6 / p_payload)
116 p_inner_ip6 = p_ip6
117 p_inner_ip6.hlim -= 1
118 p6_reply = (IP(src=self.pg0.local_ip4, dst=self.pg1.remote_ip4,
Ole Troand57f6362018-05-24 13:21:43 +0200119 proto='ipv6', id=0, tos=42) / p_inner_ip6 / p_payload)
Ole Troan298c6952018-03-08 12:30:43 +0100120 p6_reply.ttl -= 1
Ole Troan9a475372019-03-05 16:58:24 +0100121 rx = self.send_and_expect(self.pg0, p6 * 10, self.pg1)
Ole Troan298c6952018-03-08 12:30:43 +0100122 for p in rx:
123 self.validate(p[1], p6_reply)
Neale Ranns0b6a8572019-10-30 17:34:14 +0000124 self.assert_packet_checksums_valid(p)
Ole Troan298c6952018-03-08 12:30:43 +0100125
126 # IPv4 in to IPv4 tunnel
127 p4 = (p_ether / p_ip4 / p_payload)
128 p_ip4_inner = p_ip4
129 p_ip4_inner.ttl -= 1
Ole Troand57f6362018-05-24 13:21:43 +0200130 p4_reply = (IP(src=self.pg0.local_ip4, dst=self.pg1.remote_ip4,
131 tos=42) /
132 p_ip4_inner / p_payload)
Ole Troan298c6952018-03-08 12:30:43 +0100133 p4_reply.ttl -= 1
134 p4_reply.id = 0
Ole Troan9a475372019-03-05 16:58:24 +0100135 rx = self.send_and_expect(self.pg0, p4 * 10, self.pg1)
Ole Troan298c6952018-03-08 12:30:43 +0100136 for p in rx:
137 self.validate(p[1], p4_reply)
Neale Ranns0b6a8572019-10-30 17:34:14 +0000138 self.assert_packet_checksums_valid(p)
Ole Troan298c6952018-03-08 12:30:43 +0100139
140 # Decapsulation
141 p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
142
143 # IPv4 tunnel to IPv4
144 p_ip4 = IP(src="1.2.3.4", dst=self.pg0.remote_ip4)
145 p4 = (p_ether / IP(src=self.pg1.remote_ip4,
146 dst=self.pg0.local_ip4) / p_ip4 / p_payload)
147 p4_reply = (p_ip4 / p_payload)
148 p4_reply.ttl -= 1
Ole Troan9a475372019-03-05 16:58:24 +0100149 rx = self.send_and_expect(self.pg1, p4 * 10, self.pg0)
Ole Troan298c6952018-03-08 12:30:43 +0100150 for p in rx:
151 self.validate(p[1], p4_reply)
Neale Ranns0b6a8572019-10-30 17:34:14 +0000152 self.assert_packet_checksums_valid(p)
Ole Troan298c6952018-03-08 12:30:43 +0100153
Ole Troan233e4682019-05-16 15:01:34 +0200154 err = self.statistics.get_err_counter(
Ole Troan58492a82018-09-04 13:19:12 +0200155 '/err/ipip4-input/packets decapsulated')
156 self.assertEqual(err, 10)
Ole Troan73202102018-08-31 00:29:48 +0200157
Ole Troan298c6952018-03-08 12:30:43 +0100158 # IPv4 tunnel to IPv6
159 p_ip6 = IPv6(src="1:2:3::4", dst=self.pg0.remote_ip6)
160 p6 = (p_ether / IP(src=self.pg1.remote_ip4,
161 dst=self.pg0.local_ip4) / p_ip6 / p_payload)
162 p6_reply = (p_ip6 / p_payload)
163 p6_reply.hlim = 63
Ole Troan9a475372019-03-05 16:58:24 +0100164 rx = self.send_and_expect(self.pg1, p6 * 10, self.pg0)
Ole Troan298c6952018-03-08 12:30:43 +0100165 for p in rx:
166 self.validate(p[1], p6_reply)
Neale Ranns0b6a8572019-10-30 17:34:14 +0000167 self.assert_packet_checksums_valid(p)
Ole Troan298c6952018-03-08 12:30:43 +0100168
Ole Troan233e4682019-05-16 15:01:34 +0200169 err = self.statistics.get_err_counter(
Ole Troan58492a82018-09-04 13:19:12 +0200170 '/err/ipip4-input/packets decapsulated')
171 self.assertEqual(err, 20)
Ole Troan73202102018-08-31 00:29:48 +0200172
Ole Troan7eb9d962018-08-10 14:39:48 +0200173 #
Ole Troan4146c652018-08-08 22:23:19 +0200174 # Fragmentation / Reassembly and Re-fragmentation
Ole Troan7eb9d962018-08-10 14:39:48 +0200175 #
Ole Troan4146c652018-08-08 22:23:19 +0200176 rv = self.vapi.ip_reassembly_enable_disable(
177 sw_if_index=self.pg1.sw_if_index,
178 enable_ip4=1)
Ole Troan4146c652018-08-08 22:23:19 +0200179
Klement Sekera3a343d42019-05-16 14:35:46 +0200180 self.vapi.ip_reassembly_set(timeout_ms=1000, max_reassemblies=1000,
181 max_reassembly_length=1000,
182 expire_walk_interval_ms=10000,
183 is_ip6=0)
184
Ole Troan7eb9d962018-08-10 14:39:48 +0200185 # Send lots of fragments, verify reassembled packet
Ole Troan282093f2018-09-19 12:38:51 +0200186 frags, p4_reply = self.generate_ip4_frags(3131, 1400)
Ole Troan7eb9d962018-08-10 14:39:48 +0200187 f = []
188 for i in range(0, 1000):
189 f.extend(frags)
190 self.pg1.add_stream(f)
Ole Troan4146c652018-08-08 22:23:19 +0200191 self.pg_enable_capture()
Ole Troan4146c652018-08-08 22:23:19 +0200192 self.pg_start()
Ole Troan7eb9d962018-08-10 14:39:48 +0200193 rx = self.pg0.get_capture(1000)
194
Ole Troan4146c652018-08-08 22:23:19 +0200195 for p in rx:
196 self.validate(p[1], p4_reply)
197
Ole Troan233e4682019-05-16 15:01:34 +0200198 err = self.statistics.get_err_counter(
Ole Troan58492a82018-09-04 13:19:12 +0200199 '/err/ipip4-input/packets decapsulated')
200 self.assertEqual(err, 1020)
Ole Troan73202102018-08-31 00:29:48 +0200201
Ole Troan7eb9d962018-08-10 14:39:48 +0200202 f = []
203 r = []
204 for i in range(1, 90):
Ole Troan282093f2018-09-19 12:38:51 +0200205 frags, p4_reply = self.generate_ip4_frags(i * 100, 1000)
Ole Troan7eb9d962018-08-10 14:39:48 +0200206 f.extend(frags)
207 r.extend(p4_reply)
208 self.pg_enable_capture()
209 self.pg1.add_stream(f)
210 self.pg_start()
211 rx = self.pg0.get_capture(89)
212 i = 0
213 for p in rx:
214 self.validate(p[1], r[i])
215 i += 1
216
Ole Troan4146c652018-08-08 22:23:19 +0200217 # Now try with re-fragmentation
Ole Troan7eb9d962018-08-10 14:39:48 +0200218 #
219 # Send fragments to tunnel head-end, for the tunnel head end
220 # to reassemble and then refragment
221 #
Ole Troan4146c652018-08-08 22:23:19 +0200222 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [576, 0, 0, 0])
Ole Troan282093f2018-09-19 12:38:51 +0200223 frags, p4_reply = self.generate_ip4_frags(3123, 1200)
Ole Troan4146c652018-08-08 22:23:19 +0200224 self.pg_enable_capture()
225 self.pg1.add_stream(frags)
226 self.pg_start()
227 rx = self.pg0.get_capture(6)
Ole Troan7f991832018-12-06 17:35:12 +0100228 reass_pkt = reassemble4(rx)
Ole Troan4146c652018-08-08 22:23:19 +0200229 p4_reply.id = 256
230 self.validate(reass_pkt, p4_reply)
231
Ole Troan7eb9d962018-08-10 14:39:48 +0200232 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1600, 0, 0, 0])
Ole Troan282093f2018-09-19 12:38:51 +0200233 frags, p4_reply = self.generate_ip4_frags(3123, 1200)
Ole Troan7eb9d962018-08-10 14:39:48 +0200234 self.pg_enable_capture()
235 self.pg1.add_stream(frags)
236 self.pg_start()
237 rx = self.pg0.get_capture(2)
Ole Troan7f991832018-12-06 17:35:12 +0100238 reass_pkt = reassemble4(rx)
Ole Troan7eb9d962018-08-10 14:39:48 +0200239 p4_reply.id = 512
240 self.validate(reass_pkt, p4_reply)
241
Neale Ranns0b6a8572019-10-30 17:34:14 +0000242 # send large packets through the tunnel, expect them to be fragmented
243 self.vapi.sw_interface_set_mtu(sw_if_index, [600, 0, 0, 0])
244
245 p4 = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
246 IP(src="1.2.3.4", dst="130.67.0.1", tos=42) /
247 UDP(sport=1234, dport=1234) / Raw(b'Q' * 1000))
248 rx = self.send_and_expect(self.pg0, p4 * 15, self.pg1, 30)
249 inners = []
250 for p in rx:
251 inners.append(p[IP].payload)
252 reass_pkt = reassemble4(inners)
253 for p in reass_pkt:
254 self.assert_packet_checksums_valid(p)
255 self.assertEqual(p[IP].ttl, 63)
256
Ole Troan282093f2018-09-19 12:38:51 +0200257 def test_ipip_create(self):
258 """ ipip create / delete interface test """
Neale Rannscbd08242019-05-26 11:34:27 -0700259 rv = ipip_add_tunnel(self, '1.2.3.4', '2.3.4.5')
Ole Troan282093f2018-09-19 12:38:51 +0200260 sw_if_index = rv.sw_if_index
261 self.vapi.ipip_del_tunnel(sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +0100262
Ole Troan282093f2018-09-19 12:38:51 +0200263 def test_ipip_vrf_create(self):
264 """ ipip create / delete interface VRF test """
265
266 t = VppIpTable(self, 20)
267 t.add_vpp_config()
Neale Rannscbd08242019-05-26 11:34:27 -0700268 rv = ipip_add_tunnel(self, '1.2.3.4', '2.3.4.5', table_id=20)
Ole Troan282093f2018-09-19 12:38:51 +0200269 sw_if_index = rv.sw_if_index
270 self.vapi.ipip_del_tunnel(sw_if_index)
271
272 def payload(self, len):
273 return 'x' * len
274
275
276class TestIPIP6(VppTestCase):
277 """ IPIP6 Test Case """
278
279 @classmethod
280 def setUpClass(cls):
281 super(TestIPIP6, cls).setUpClass()
282 cls.create_pg_interfaces(range(2))
283 cls.interfaces = list(cls.pg_interfaces)
284
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700285 @classmethod
286 def tearDownClass(cls):
287 super(TestIPIP6, cls).tearDownClass()
288
Ole Troan282093f2018-09-19 12:38:51 +0200289 def setUp(self):
Paul Vinciguerra8d991d92019-01-25 14:05:48 -0800290 super(TestIPIP6, self).setUp()
Ole Troan282093f2018-09-19 12:38:51 +0200291 for i in self.interfaces:
292 i.admin_up()
293 i.config_ip4()
294 i.config_ip6()
295 i.disable_ipv6_ra()
296 i.resolve_arp()
297 i.resolve_ndp()
298 self.setup_tunnel()
299
300 def tearDown(self):
301 if not self.vpp_dead:
302 self.destroy_tunnel()
303 for i in self.pg_interfaces:
304 i.unconfig_ip4()
305 i.unconfig_ip6()
306 i.admin_down()
307 super(TestIPIP6, self).tearDown()
308
309 def setup_tunnel(self):
Ole Troan298c6952018-03-08 12:30:43 +0100310 # IPv6 transport
Neale Rannscbd08242019-05-26 11:34:27 -0700311 rv = ipip_add_tunnel(self,
312 self.pg0.local_ip6,
313 self.pg1.remote_ip6,
314 tc_tos=255)
Ole Troan298c6952018-03-08 12:30:43 +0100315
316 sw_if_index = rv.sw_if_index
Ole Troan282093f2018-09-19 12:38:51 +0200317 self.tunnel_if_index = sw_if_index
Ole Troan46c1c112018-03-14 20:39:40 +0100318 self.vapi.sw_interface_set_flags(sw_if_index, 1)
319 self.vapi.sw_interface_set_unnumbered(
Ole Troan9a475372019-03-05 16:58:24 +0100320 sw_if_index=self.pg0.sw_if_index,
321 unnumbered_sw_if_index=sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +0100322
323 # Add IPv4 and IPv6 routes via tunnel interface
324 ip4_via_tunnel = VppIpRoute(
325 self, "130.67.0.0", 16,
326 [VppRoutePath("0.0.0.0",
327 sw_if_index,
Neale Ranns097fa662018-05-01 05:17:55 -0700328 proto=FibPathProto.FIB_PATH_NH_PROTO_IP4)])
Ole Troan298c6952018-03-08 12:30:43 +0100329 ip4_via_tunnel.add_vpp_config()
330
331 ip6_via_tunnel = VppIpRoute(
332 self, "dead::", 16,
333 [VppRoutePath("::",
334 sw_if_index,
Neale Ranns097fa662018-05-01 05:17:55 -0700335 proto=FibPathProto.FIB_PATH_NH_PROTO_IP6)])
Ole Troan298c6952018-03-08 12:30:43 +0100336 ip6_via_tunnel.add_vpp_config()
337
Ole Troan282093f2018-09-19 12:38:51 +0200338 self.tunnel_ip6_via_tunnel = ip6_via_tunnel
339 self.tunnel_ip4_via_tunnel = ip4_via_tunnel
Ole Troan298c6952018-03-08 12:30:43 +0100340
Ole Troan282093f2018-09-19 12:38:51 +0200341 def destroy_tunnel(self):
342 # IPv6 transport
343 self.tunnel_ip4_via_tunnel.remove_vpp_config()
344 self.tunnel_ip6_via_tunnel.remove_vpp_config()
345
346 rv = self.vapi.ipip_del_tunnel(sw_if_index=self.tunnel_if_index)
347
348 def validate(self, rx, expected):
Ole Troan7f991832018-12-06 17:35:12 +0100349 self.assertEqual(rx, expected.__class__(expected))
Ole Troan282093f2018-09-19 12:38:51 +0200350
351 def generate_ip6_frags(self, payload_length, fragment_size):
352 p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
353 p_payload = UDP(sport=1234, dport=1234) / self.payload(payload_length)
354 p_ip6 = IPv6(src="1::1", dst=self.pg0.remote_ip6)
355 outer_ip6 = (p_ether / IPv6(src=self.pg1.remote_ip6,
356 dst=self.pg0.local_ip6) /
357 IPv6ExtHdrFragment() / p_ip6 / p_payload)
358 frags = fragment6(outer_ip6, fragment_size)
359 p6_reply = (p_ip6 / p_payload)
360 p6_reply.hlim -= 1
361 return frags, p6_reply
362
363 def generate_ip6_hairpin_frags(self, payload_length, fragment_size):
364 p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
365 p_payload = UDP(sport=1234, dport=1234) / self.payload(payload_length)
366 p_ip6 = IPv6(src="1::1", dst="dead::1")
367 outer_ip6 = (p_ether / IPv6(src=self.pg1.remote_ip6,
368 dst=self.pg0.local_ip6) /
369 IPv6ExtHdrFragment() / p_ip6 / p_payload)
370 frags = fragment6(outer_ip6, fragment_size)
371 p_ip6.hlim -= 1
372 p6_reply = (IPv6(src=self.pg0.local_ip6, dst=self.pg1.remote_ip6,
373 hlim=63) / p_ip6 / p_payload)
374
375 return frags, p6_reply
376
377 def test_encap(self):
378 """ ip{v4,v6} over ip6 test encap """
379 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
380 p_ip6 = IPv6(src="1::1", dst="DEAD::1", tc=42, nh='UDP')
381 p_ip4 = IP(src="1.2.3.4", dst="130.67.0.1", tos=42)
382 p_payload = UDP(sport=1234, dport=1234)
383
384 # Encapsulation
Ole Troan298c6952018-03-08 12:30:43 +0100385 # IPv6 in to IPv6 tunnel
386 p6 = (p_ether / p_ip6 / p_payload)
Ole Troand57f6362018-05-24 13:21:43 +0200387 p6_reply = (IPv6(src=self.pg0.local_ip6, dst=self.pg1.remote_ip6,
Ole Troan282093f2018-09-19 12:38:51 +0200388 hlim=64, tc=42) /
Ole Troand57f6362018-05-24 13:21:43 +0200389 p_ip6 / p_payload)
Ole Troan298c6952018-03-08 12:30:43 +0100390 p6_reply[1].hlim -= 1
Ole Troan9a475372019-03-05 16:58:24 +0100391 rx = self.send_and_expect(self.pg0, p6 * 11, self.pg1)
Ole Troan298c6952018-03-08 12:30:43 +0100392 for p in rx:
393 self.validate(p[1], p6_reply)
394
395 # IPv4 in to IPv6 tunnel
396 p4 = (p_ether / p_ip4 / p_payload)
397 p4_reply = (IPv6(src=self.pg0.local_ip6,
Ole Troan282093f2018-09-19 12:38:51 +0200398 dst=self.pg1.remote_ip6, hlim=64, tc=42) /
Ole Troand57f6362018-05-24 13:21:43 +0200399 p_ip4 / p_payload)
Ole Troan298c6952018-03-08 12:30:43 +0100400 p4_reply[1].ttl -= 1
Ole Troan9a475372019-03-05 16:58:24 +0100401 rx = self.send_and_expect(self.pg0, p4 * 11, self.pg1)
Ole Troan298c6952018-03-08 12:30:43 +0100402 for p in rx:
403 self.validate(p[1], p4_reply)
404
Ole Troan282093f2018-09-19 12:38:51 +0200405 def test_decap(self):
406 """ ip{v4,v6} over ip6 test decap """
Ole Troan298c6952018-03-08 12:30:43 +0100407
408 p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
Ole Troan282093f2018-09-19 12:38:51 +0200409 p_ip6 = IPv6(src="1::1", dst="DEAD::1", tc=42, nh='UDP')
Ole Troan298c6952018-03-08 12:30:43 +0100410 p_ip4 = IP(src="1.2.3.4", dst=self.pg0.remote_ip4)
Ole Troan282093f2018-09-19 12:38:51 +0200411 p_payload = UDP(sport=1234, dport=1234)
412
413 # Decapsulation
414 # IPv6 tunnel to IPv4
415
Ole Troan298c6952018-03-08 12:30:43 +0100416 p4 = (p_ether / IPv6(src=self.pg1.remote_ip6,
417 dst=self.pg0.local_ip6) / p_ip4 / p_payload)
418 p4_reply = (p_ip4 / p_payload)
419 p4_reply.ttl -= 1
Ole Troan9a475372019-03-05 16:58:24 +0100420 rx = self.send_and_expect(self.pg1, p4 * 11, self.pg0)
Ole Troan298c6952018-03-08 12:30:43 +0100421 for p in rx:
422 self.validate(p[1], p4_reply)
423
424 # IPv6 tunnel to IPv6
425 p_ip6 = IPv6(src="1:2:3::4", dst=self.pg0.remote_ip6)
426 p6 = (p_ether / IPv6(src=self.pg1.remote_ip6,
427 dst=self.pg0.local_ip6) / p_ip6 / p_payload)
428 p6_reply = (p_ip6 / p_payload)
429 p6_reply.hlim = 63
Ole Troan9a475372019-03-05 16:58:24 +0100430 rx = self.send_and_expect(self.pg1, p6 * 11, self.pg0)
Ole Troan298c6952018-03-08 12:30:43 +0100431 for p in rx:
432 self.validate(p[1], p6_reply)
433
Ole Troan282093f2018-09-19 12:38:51 +0200434 def test_frag(self):
435 """ ip{v4,v6} over ip6 test frag """
436
437 p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
438 p_ip6 = IPv6(src="1::1", dst="DEAD::1", tc=42, nh='UDP')
439 p_ip4 = IP(src="1.2.3.4", dst=self.pg0.remote_ip4)
440 p_payload = UDP(sport=1234, dport=1234)
441
442 #
443 # Fragmentation / Reassembly and Re-fragmentation
444 #
445 rv = self.vapi.ip_reassembly_enable_disable(
446 sw_if_index=self.pg1.sw_if_index,
447 enable_ip6=1)
448
Klement Sekera3a343d42019-05-16 14:35:46 +0200449 self.vapi.ip_reassembly_set(timeout_ms=1000, max_reassemblies=1000,
450 max_reassembly_length=1000,
451 expire_walk_interval_ms=10000,
452 is_ip6=1)
453
Ole Troan282093f2018-09-19 12:38:51 +0200454 # Send lots of fragments, verify reassembled packet
Ole Troan233e4682019-05-16 15:01:34 +0200455 before_cnt = self.statistics.get_err_counter(
Ole Troan282093f2018-09-19 12:38:51 +0200456 '/err/ipip6-input/packets decapsulated')
457 frags, p6_reply = self.generate_ip6_frags(3131, 1400)
458 f = []
459 for i in range(0, 1000):
460 f.extend(frags)
461 self.pg1.add_stream(f)
462 self.pg_enable_capture()
463 self.pg_start()
464 rx = self.pg0.get_capture(1000)
465
466 for p in rx:
467 self.validate(p[1], p6_reply)
468
Ole Troan233e4682019-05-16 15:01:34 +0200469 cnt = self.statistics.get_err_counter(
Ole Troan282093f2018-09-19 12:38:51 +0200470 '/err/ipip6-input/packets decapsulated')
471 self.assertEqual(cnt, before_cnt + 1000)
472
473 f = []
474 r = []
475 # TODO: Check out why reassembly of atomic fragments don't work
476 for i in range(10, 90):
477 frags, p6_reply = self.generate_ip6_frags(i * 100, 1000)
478 f.extend(frags)
479 r.extend(p6_reply)
480 self.pg_enable_capture()
481 self.pg1.add_stream(f)
482 self.pg_start()
483 rx = self.pg0.get_capture(80)
484 i = 0
485 for p in rx:
486 self.validate(p[1], r[i])
487 i += 1
488
489 # Simple fragmentation
490 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
491 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1280, 0, 0, 0])
492
493 # IPv6 in to IPv6 tunnel
494 p_payload = UDP(sport=1234, dport=1234) / self.payload(1300)
495
496 p6 = (p_ether / p_ip6 / p_payload)
497 p6_reply = (IPv6(src=self.pg0.local_ip6, dst=self.pg1.remote_ip6,
498 hlim=63, tc=42) /
499 p_ip6 / p_payload)
500 p6_reply[1].hlim -= 1
501 self.pg_enable_capture()
502 self.pg0.add_stream(p6)
503 self.pg_start()
504 rx = self.pg1.get_capture(2)
505
506 # Scapy defragment doesn't deal well with multiple layers
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700507 # of same type / Ethernet header first
Ole Troan282093f2018-09-19 12:38:51 +0200508 f = [p[1] for p in rx]
509 reass_pkt = defragment6(f)
510 self.validate(reass_pkt, p6_reply)
511
512 # Now try with re-fragmentation
513 #
514 # Send large fragments to tunnel head-end, for the tunnel head end
515 # to reassemble and then refragment out the tunnel again.
516 # Hair-pinning
517 #
518 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1280, 0, 0, 0])
519 frags, p6_reply = self.generate_ip6_hairpin_frags(8000, 1200)
520 self.pg_enable_capture()
521 self.pg1.add_stream(frags)
522 self.pg_start()
523 rx = self.pg1.get_capture(7)
524 f = [p[1] for p in rx]
525 reass_pkt = defragment6(f)
526 p6_reply.id = 256
527 self.validate(reass_pkt, p6_reply)
528
Ole Troan298c6952018-03-08 12:30:43 +0100529 def test_ipip_create(self):
530 """ ipip create / delete interface test """
Neale Rannscbd08242019-05-26 11:34:27 -0700531 rv = ipip_add_tunnel(self, '1.2.3.4', '2.3.4.5')
Ole Troan298c6952018-03-08 12:30:43 +0100532 sw_if_index = rv.sw_if_index
Ole Troan46c1c112018-03-14 20:39:40 +0100533 self.vapi.ipip_del_tunnel(sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +0100534
Neale Ranns61502112018-08-22 00:21:14 -0700535 def test_ipip_vrf_create(self):
536 """ ipip create / delete interface VRF test """
537
538 t = VppIpTable(self, 20)
539 t.add_vpp_config()
Neale Rannscbd08242019-05-26 11:34:27 -0700540 rv = ipip_add_tunnel(self, '1.2.3.4', '2.3.4.5', table_id=20)
Neale Ranns61502112018-08-22 00:21:14 -0700541 sw_if_index = rv.sw_if_index
542 self.vapi.ipip_del_tunnel(sw_if_index)
543
Ole Troan4146c652018-08-08 22:23:19 +0200544 def payload(self, len):
545 return 'x' * len
546
Ole Troan298c6952018-03-08 12:30:43 +0100547
548if __name__ == '__main__':
549 unittest.main(testRunner=VppTestRunner)