blob: 4c7a7cd320ba47da2065403541373a4a09c5236f [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Paul Vinciguerraf1f2aa62018-11-25 08:36:47 -08002
Klement Sekera75e7d132017-09-20 08:26:30 +02003import unittest
Klement Sekera630ab582019-07-19 09:14:19 +00004from random import shuffle, choice, randrange
Klement Sekera75e7d132017-09-20 08:26:30 +02005
Klement Sekera947a85c2019-07-24 12:40:37 +00006from framework import VppTestCase, VppTestRunner
7
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -07008import scapy.compat
Klement Sekera75e7d132017-09-20 08:26:30 +02009from scapy.packet import Raw
10from scapy.layers.l2 import Ether, GRE
Klement Sekera01c1fa42021-12-14 18:25:11 +000011from scapy.layers.inet import IP, UDP, ICMP, icmptypes
Klement Sekera769145c2019-03-06 11:59:57 +010012from scapy.layers.inet6 import HBHOptUnknown, ICMPv6ParamProblem,\
Ole Troan03092c12021-11-23 15:55:39 +010013 ICMPv6TimeExceeded, IPv6, IPv6ExtHdrFragment,\
Klement Sekera01c1fa42021-12-14 18:25:11 +000014 IPv6ExtHdrHopByHop, IPv6ExtHdrDestOpt, PadN, ICMPv6EchoRequest,\
15 ICMPv6EchoReply
Paul Vinciguerra69555952019-03-01 08:46:29 -080016from framework import VppTestCase, VppTestRunner
Klement Sekera630ab582019-07-19 09:14:19 +000017from util import ppp, ppc, fragment_rfc791, fragment_rfc8200
Neale Ranns5a8844b2019-04-16 07:15:35 +000018from vpp_gre_interface import VppGreInterface
Neale Rannsc0a93142018-09-05 15:42:26 -070019from vpp_ip import DpoProto
Neale Ranns097fa662018-05-01 05:17:55 -070020from vpp_ip_route import VppIpRoute, VppRoutePath, FibPathProto
Klement Sekera896c8962019-06-24 11:52:49 +000021from vpp_papi import VppEnum
Klement Sekera75e7d132017-09-20 08:26:30 +020022
Klement Sekerad0f70a32018-12-14 17:24:13 +010023# 35 is enough to have >257 400-byte fragments
24test_packet_count = 35
Klement Sekera75e7d132017-09-20 08:26:30 +020025
26
Klement Sekera947a85c2019-07-24 12:40:37 +000027class TestIPv4Reassembly(VppTestCase):
Klement Sekera75e7d132017-09-20 08:26:30 +020028 """ IPv4 Reassembly """
29
30 @classmethod
31 def setUpClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +000032 super().setUpClass()
Klement Sekera75e7d132017-09-20 08:26:30 +020033
Klement Sekera4c533132018-02-22 11:41:12 +010034 cls.create_pg_interfaces([0, 1])
35 cls.src_if = cls.pg0
36 cls.dst_if = cls.pg1
Klement Sekera75e7d132017-09-20 08:26:30 +020037
38 # setup all interfaces
39 for i in cls.pg_interfaces:
40 i.admin_up()
41 i.config_ip4()
42 i.resolve_arp()
43
Klement Sekera75e7d132017-09-20 08:26:30 +020044 # packet sizes
45 cls.packet_sizes = [64, 512, 1518, 9018]
46 cls.padding = " abcdefghijklmn"
47 cls.create_stream(cls.packet_sizes)
48 cls.create_fragments()
49
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070050 @classmethod
51 def tearDownClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +000052 super().tearDownClass()
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070053
Klement Sekera75e7d132017-09-20 08:26:30 +020054 def setUp(self):
55 """ Test setup - force timeout on existing reassemblies """
Klement Sekera01c1fa42021-12-14 18:25:11 +000056 super().setUp()
Klement Sekera4c533132018-02-22 11:41:12 +010057 self.vapi.ip_reassembly_enable_disable(
58 sw_if_index=self.src_if.sw_if_index, enable_ip4=True)
Klement Sekera75e7d132017-09-20 08:26:30 +020059 self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000,
Klement Sekera3a343d42019-05-16 14:35:46 +020060 max_reassembly_length=1000,
Klement Sekera75e7d132017-09-20 08:26:30 +020061 expire_walk_interval_ms=10)
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +020062 self.virtual_sleep(.25)
Klement Sekera75e7d132017-09-20 08:26:30 +020063 self.vapi.ip_reassembly_set(timeout_ms=1000000, max_reassemblies=1000,
Klement Sekera3a343d42019-05-16 14:35:46 +020064 max_reassembly_length=1000,
Klement Sekera75e7d132017-09-20 08:26:30 +020065 expire_walk_interval_ms=10000)
66
67 def tearDown(self):
Klement Sekera01c1fa42021-12-14 18:25:11 +000068 self.vapi.ip_reassembly_enable_disable(
69 sw_if_index=self.src_if.sw_if_index, enable_ip4=False)
70 super().tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -070071
72 def show_commands_at_teardown(self):
Klement Sekera896c8962019-06-24 11:52:49 +000073 self.logger.debug(self.vapi.ppcli("show ip4-full-reassembly details"))
Klement Sekeracae98b72019-02-19 13:53:43 +010074 self.logger.debug(self.vapi.ppcli("show buffers"))
Klement Sekera75e7d132017-09-20 08:26:30 +020075
76 @classmethod
77 def create_stream(cls, packet_sizes, packet_count=test_packet_count):
Klement Sekerad0f70a32018-12-14 17:24:13 +010078 """Create input packet stream
Klement Sekera75e7d132017-09-20 08:26:30 +020079
80 :param list packet_sizes: Required packet sizes.
81 """
82 for i in range(0, packet_count):
Klement Sekera4c533132018-02-22 11:41:12 +010083 info = cls.create_packet_info(cls.src_if, cls.src_if)
Klement Sekera75e7d132017-09-20 08:26:30 +020084 payload = cls.info_to_payload(info)
Klement Sekera4c533132018-02-22 11:41:12 +010085 p = (Ether(dst=cls.src_if.local_mac, src=cls.src_if.remote_mac) /
86 IP(id=info.index, src=cls.src_if.remote_ip4,
87 dst=cls.dst_if.remote_ip4) /
88 UDP(sport=1234, dport=5678) /
Klement Sekera75e7d132017-09-20 08:26:30 +020089 Raw(payload))
90 size = packet_sizes[(i // 2) % len(packet_sizes)]
91 cls.extend_packet(p, size, cls.padding)
92 info.data = p
93
94 @classmethod
95 def create_fragments(cls):
96 infos = cls._packet_infos
97 cls.pkt_infos = []
Paul Vinciguerra090096b2020-12-03 00:42:46 -050098 for index, info in infos.items():
Klement Sekera75e7d132017-09-20 08:26:30 +020099 p = info.data
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700100 # cls.logger.debug(ppp("Packet:",
101 # p.__class__(scapy.compat.raw(p))))
Klement Sekera75e7d132017-09-20 08:26:30 +0200102 fragments_400 = fragment_rfc791(p, 400)
103 fragments_300 = fragment_rfc791(p, 300)
104 fragments_200 = [
105 x for f in fragments_400 for x in fragment_rfc791(f, 200)]
106 cls.pkt_infos.append(
107 (index, fragments_400, fragments_300, fragments_200))
108 cls.fragments_400 = [
109 x for (_, frags, _, _) in cls.pkt_infos for x in frags]
110 cls.fragments_300 = [
111 x for (_, _, frags, _) in cls.pkt_infos for x in frags]
112 cls.fragments_200 = [
113 x for (_, _, _, frags) in cls.pkt_infos for x in frags]
114 cls.logger.debug("Fragmented %s packets into %s 400-byte fragments, "
115 "%s 300-byte fragments and %s 200-byte fragments" %
116 (len(infos), len(cls.fragments_400),
117 len(cls.fragments_300), len(cls.fragments_200)))
118
Klement Sekera947a85c2019-07-24 12:40:37 +0000119 def verify_capture(self, capture, dropped_packet_indexes=[]):
120 """Verify captured packet stream.
121
122 :param list capture: Captured packet stream.
123 """
124 info = None
125 seen = set()
126 for packet in capture:
127 try:
128 self.logger.debug(ppp("Got packet:", packet))
129 ip = packet[IP]
130 udp = packet[UDP]
131 payload_info = self.payload_to_info(packet[Raw])
132 packet_index = payload_info.index
133 self.assertTrue(
134 packet_index not in dropped_packet_indexes,
135 ppp("Packet received, but should be dropped:", packet))
136 if packet_index in seen:
137 raise Exception(ppp("Duplicate packet received", packet))
138 seen.add(packet_index)
139 self.assertEqual(payload_info.dst, self.src_if.sw_if_index)
140 info = self._packet_infos[packet_index]
141 self.assertTrue(info is not None)
142 self.assertEqual(packet_index, info.index)
143 saved_packet = info.data
144 self.assertEqual(ip.src, saved_packet[IP].src)
145 self.assertEqual(ip.dst, saved_packet[IP].dst)
146 self.assertEqual(udp.payload, saved_packet[UDP].payload)
147 except Exception:
148 self.logger.error(ppp("Unexpected or invalid packet:", packet))
149 raise
150 for index in self._packet_infos:
151 self.assertTrue(index in seen or index in dropped_packet_indexes,
152 "Packet with packet_index %d not received" % index)
153
154 def test_reassembly(self):
Klement Sekera75e7d132017-09-20 08:26:30 +0200155 """ basic reassembly """
156
Klement Sekera947a85c2019-07-24 12:40:37 +0000157 self.pg_enable_capture()
158 self.src_if.add_stream(self.fragments_200)
159 self.pg_start()
160
161 packets = self.dst_if.get_capture(len(self.pkt_infos))
162 self.verify_capture(packets)
163 self.src_if.assert_nothing_captured()
164
165 # run it all again to verify correctness
166 self.pg_enable_capture()
167 self.src_if.add_stream(self.fragments_200)
168 self.pg_start()
169
170 packets = self.dst_if.get_capture(len(self.pkt_infos))
171 self.verify_capture(packets)
172 self.src_if.assert_nothing_captured()
173
Klement Sekera53be16d2020-12-15 21:47:36 +0100174 def test_verify_clear_trace_mid_reassembly(self):
175 """ verify clear trace works mid-reassembly """
176
177 self.pg_enable_capture()
178 self.src_if.add_stream(self.fragments_200[0:-1])
179 self.pg_start()
180
181 self.logger.debug(self.vapi.cli("show trace"))
182 self.vapi.cli("clear trace")
183
184 self.src_if.add_stream(self.fragments_200[-1])
185 self.pg_start()
186 packets = self.dst_if.get_capture(len(self.pkt_infos))
187 self.verify_capture(packets)
188
Klement Sekera947a85c2019-07-24 12:40:37 +0000189 def test_reversed(self):
Klement Sekera75e7d132017-09-20 08:26:30 +0200190 """ reverse order reassembly """
191
Klement Sekera947a85c2019-07-24 12:40:37 +0000192 fragments = list(self.fragments_200)
193 fragments.reverse()
194
195 self.pg_enable_capture()
196 self.src_if.add_stream(fragments)
197 self.pg_start()
198
199 packets = self.dst_if.get_capture(len(self.packet_infos))
200 self.verify_capture(packets)
201 self.src_if.assert_nothing_captured()
202
203 # run it all again to verify correctness
204 self.pg_enable_capture()
205 self.src_if.add_stream(fragments)
206 self.pg_start()
207
208 packets = self.dst_if.get_capture(len(self.packet_infos))
209 self.verify_capture(packets)
210 self.src_if.assert_nothing_captured()
Klement Sekera75e7d132017-09-20 08:26:30 +0200211
Klement Sekera3a343d42019-05-16 14:35:46 +0200212 def test_long_fragment_chain(self):
213 """ long fragment chain """
214
215 error_cnt_str = \
Klement Sekera896c8962019-06-24 11:52:49 +0000216 "/err/ip4-full-reassembly-feature/fragment chain too long (drop)"
Klement Sekera3a343d42019-05-16 14:35:46 +0200217
Klement Sekera34641f22019-05-22 20:18:26 +0200218 error_cnt = self.statistics.get_err_counter(error_cnt_str)
Klement Sekera3a343d42019-05-16 14:35:46 +0200219
220 self.vapi.ip_reassembly_set(timeout_ms=100, max_reassemblies=1000,
221 max_reassembly_length=3,
222 expire_walk_interval_ms=50)
223
224 p1 = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
225 IP(id=1000, src=self.src_if.remote_ip4,
226 dst=self.dst_if.remote_ip4) /
227 UDP(sport=1234, dport=5678) /
Ole Troan127fbec2019-10-18 15:22:56 +0200228 Raw(b"X" * 1000))
Klement Sekera3a343d42019-05-16 14:35:46 +0200229 p2 = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
230 IP(id=1001, src=self.src_if.remote_ip4,
231 dst=self.dst_if.remote_ip4) /
232 UDP(sport=1234, dport=5678) /
Ole Troan127fbec2019-10-18 15:22:56 +0200233 Raw(b"X" * 1000))
Klement Sekera3a343d42019-05-16 14:35:46 +0200234 frags = fragment_rfc791(p1, 200) + fragment_rfc791(p2, 500)
235
236 self.pg_enable_capture()
237 self.src_if.add_stream(frags)
238 self.pg_start()
239
240 self.dst_if.get_capture(1)
Klement Sekera34641f22019-05-22 20:18:26 +0200241 self.assert_error_counter_equal(error_cnt_str, error_cnt + 1)
Klement Sekera3a343d42019-05-16 14:35:46 +0200242
Klement Sekera14d7e902018-12-10 13:46:09 +0100243 def test_5737(self):
244 """ fragment length + ip header size > 65535 """
Klement Sekera4ee633e2018-12-14 12:00:44 +0100245 self.vapi.cli("clear errors")
Ole Troan127fbec2019-10-18 15:22:56 +0200246 raw = b'''E\x00\x00\x88,\xf8\x1f\xfe@\x01\x98\x00\xc0\xa8\n-\xc0\xa8\n\
247\x01\x08\x00\xf0J\xed\xcb\xf1\xf5Test-group: IPv4.IPv4.ipv4-message.\
248Ethernet-Payload.IPv4-Packet.IPv4-Header.Fragment-Offset; Test-case: 5737'''
Klement Sekera14d7e902018-12-10 13:46:09 +0100249 malformed_packet = (Ether(dst=self.src_if.local_mac,
250 src=self.src_if.remote_mac) /
251 IP(raw))
252 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
253 IP(id=1000, src=self.src_if.remote_ip4,
254 dst=self.dst_if.remote_ip4) /
255 UDP(sport=1234, dport=5678) /
Ole Troan127fbec2019-10-18 15:22:56 +0200256 Raw(b"X" * 1000))
Klement Sekera14d7e902018-12-10 13:46:09 +0100257 valid_fragments = fragment_rfc791(p, 400)
258
Ole Troan127fbec2019-10-18 15:22:56 +0200259 counter = "/err/ip4-full-reassembly-feature/malformed packets"
260 error_counter = self.statistics.get_err_counter(counter)
Klement Sekera14d7e902018-12-10 13:46:09 +0100261 self.pg_enable_capture()
262 self.src_if.add_stream([malformed_packet] + valid_fragments)
263 self.pg_start()
264
265 self.dst_if.get_capture(1)
Klement Sekera896c8962019-06-24 11:52:49 +0000266 self.logger.debug(self.vapi.ppcli("show error"))
Ole Troan127fbec2019-10-18 15:22:56 +0200267 self.assertEqual(self.statistics.get_err_counter(counter),
268 error_counter + 1)
Klement Sekera14d7e902018-12-10 13:46:09 +0100269
Klement Sekera400f6d82018-12-13 14:35:48 +0100270 def test_44924(self):
271 """ compress tiny fragments """
272 packets = [(Ether(dst=self.src_if.local_mac,
273 src=self.src_if.remote_mac) /
274 IP(id=24339, flags="MF", frag=0, ttl=64,
275 src=self.src_if.remote_ip4,
276 dst=self.dst_if.remote_ip4) /
277 ICMP(type="echo-request", code=0, id=0x1fe6, seq=0x2407) /
278 Raw(load='Test-group: IPv4')),
279 (Ether(dst=self.src_if.local_mac,
280 src=self.src_if.remote_mac) /
281 IP(id=24339, flags="MF", frag=3, ttl=64,
282 src=self.src_if.remote_ip4,
283 dst=self.dst_if.remote_ip4) /
284 ICMP(type="echo-request", code=0, id=0x1fe6, seq=0x2407) /
285 Raw(load='.IPv4.Fragmentation.vali')),
286 (Ether(dst=self.src_if.local_mac,
287 src=self.src_if.remote_mac) /
288 IP(id=24339, frag=6, ttl=64,
289 src=self.src_if.remote_ip4,
290 dst=self.dst_if.remote_ip4) /
291 ICMP(type="echo-request", code=0, id=0x1fe6, seq=0x2407) /
292 Raw(load='d; Test-case: 44924'))
293 ]
294
295 self.pg_enable_capture()
296 self.src_if.add_stream(packets)
297 self.pg_start()
298
299 self.dst_if.get_capture(1)
300
Klement Sekera4ee633e2018-12-14 12:00:44 +0100301 def test_frag_1(self):
302 """ fragment of size 1 """
303 self.vapi.cli("clear errors")
304 malformed_packets = [(Ether(dst=self.src_if.local_mac,
305 src=self.src_if.remote_mac) /
306 IP(id=7, len=21, flags="MF", frag=0, ttl=64,
307 src=self.src_if.remote_ip4,
308 dst=self.dst_if.remote_ip4) /
309 ICMP(type="echo-request")),
310 (Ether(dst=self.src_if.local_mac,
311 src=self.src_if.remote_mac) /
312 IP(id=7, len=21, frag=1, ttl=64,
313 src=self.src_if.remote_ip4,
314 dst=self.dst_if.remote_ip4) /
Ole Troan127fbec2019-10-18 15:22:56 +0200315 Raw(load=b'\x08')),
Klement Sekera4ee633e2018-12-14 12:00:44 +0100316 ]
317
318 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
319 IP(id=1000, src=self.src_if.remote_ip4,
320 dst=self.dst_if.remote_ip4) /
321 UDP(sport=1234, dport=5678) /
Ole Troan127fbec2019-10-18 15:22:56 +0200322 Raw(b"X" * 1000))
Klement Sekera4ee633e2018-12-14 12:00:44 +0100323 valid_fragments = fragment_rfc791(p, 400)
324
325 self.pg_enable_capture()
326 self.src_if.add_stream(malformed_packets + valid_fragments)
327 self.pg_start()
328
329 self.dst_if.get_capture(1)
330
Klement Sekera896c8962019-06-24 11:52:49 +0000331 self.assert_packet_counter_equal("ip4-full-reassembly-feature", 1)
Klement Sekera4ee633e2018-12-14 12:00:44 +0100332 # TODO remove above, uncomment below once clearing of counters
333 # is supported
334 # self.assert_packet_counter_equal(
Klement Sekera896c8962019-06-24 11:52:49 +0000335 # "/err/ip4-full-reassembly-feature/malformed packets", 1)
Klement Sekera4ee633e2018-12-14 12:00:44 +0100336
Klement Sekera947a85c2019-07-24 12:40:37 +0000337 def test_random(self):
338 """ random order reassembly """
339
340 fragments = list(self.fragments_200)
341 shuffle(fragments)
342
343 self.pg_enable_capture()
344 self.src_if.add_stream(fragments)
345 self.pg_start()
346
347 packets = self.dst_if.get_capture(len(self.packet_infos))
348 self.verify_capture(packets)
349 self.src_if.assert_nothing_captured()
350
351 # run it all again to verify correctness
352 self.pg_enable_capture()
353 self.src_if.add_stream(fragments)
354 self.pg_start()
355
356 packets = self.dst_if.get_capture(len(self.packet_infos))
357 self.verify_capture(packets)
358 self.src_if.assert_nothing_captured()
359
360 def test_duplicates(self):
Klement Sekera75e7d132017-09-20 08:26:30 +0200361 """ duplicate fragments """
Klement Sekera947a85c2019-07-24 12:40:37 +0000362
Klement Sekera75e7d132017-09-20 08:26:30 +0200363 fragments = [
364 x for (_, frags, _, _) in self.pkt_infos
365 for x in frags
366 for _ in range(0, min(2, len(frags)))
367 ]
Klement Sekera947a85c2019-07-24 12:40:37 +0000368
369 self.pg_enable_capture()
370 self.src_if.add_stream(fragments)
371 self.pg_start()
372
373 packets = self.dst_if.get_capture(len(self.pkt_infos))
374 self.verify_capture(packets)
375 self.src_if.assert_nothing_captured()
Klement Sekera75e7d132017-09-20 08:26:30 +0200376
377 def test_overlap1(self):
378 """ overlapping fragments case #1 """
379
380 fragments = []
381 for _, _, frags_300, frags_200 in self.pkt_infos:
382 if len(frags_300) == 1:
383 fragments.extend(frags_300)
384 else:
385 for i, j in zip(frags_200, frags_300):
386 fragments.extend(i)
387 fragments.extend(j)
388
389 self.pg_enable_capture()
Klement Sekera4c533132018-02-22 11:41:12 +0100390 self.src_if.add_stream(fragments)
Klement Sekera75e7d132017-09-20 08:26:30 +0200391 self.pg_start()
392
Klement Sekera4c533132018-02-22 11:41:12 +0100393 packets = self.dst_if.get_capture(len(self.pkt_infos))
Klement Sekera947a85c2019-07-24 12:40:37 +0000394 self.verify_capture(packets)
Klement Sekera4c533132018-02-22 11:41:12 +0100395 self.src_if.assert_nothing_captured()
Klement Sekera75e7d132017-09-20 08:26:30 +0200396
397 # run it all to verify correctness
398 self.pg_enable_capture()
Klement Sekera4c533132018-02-22 11:41:12 +0100399 self.src_if.add_stream(fragments)
Klement Sekera75e7d132017-09-20 08:26:30 +0200400 self.pg_start()
401
Klement Sekera4c533132018-02-22 11:41:12 +0100402 packets = self.dst_if.get_capture(len(self.pkt_infos))
Klement Sekera947a85c2019-07-24 12:40:37 +0000403 self.verify_capture(packets)
Klement Sekera4c533132018-02-22 11:41:12 +0100404 self.src_if.assert_nothing_captured()
Klement Sekera75e7d132017-09-20 08:26:30 +0200405
406 def test_overlap2(self):
407 """ overlapping fragments case #2 """
408
409 fragments = []
410 for _, _, frags_300, frags_200 in self.pkt_infos:
411 if len(frags_300) == 1:
412 fragments.extend(frags_300)
413 else:
414 # care must be taken here so that there are no fragments
415 # received by vpp after reassembly is finished, otherwise
416 # new reassemblies will be started and packet generator will
417 # freak out when it detects unfreed buffers
418 zipped = zip(frags_300, frags_200)
Paul Vinciguerrac7834e02019-03-02 10:43:05 -0800419 for i, j in zipped:
Klement Sekera75e7d132017-09-20 08:26:30 +0200420 fragments.extend(i)
421 fragments.extend(j)
Paul Vinciguerrac7834e02019-03-02 10:43:05 -0800422 fragments.pop()
Klement Sekera75e7d132017-09-20 08:26:30 +0200423
424 self.pg_enable_capture()
Klement Sekera4c533132018-02-22 11:41:12 +0100425 self.src_if.add_stream(fragments)
Klement Sekera75e7d132017-09-20 08:26:30 +0200426 self.pg_start()
427
Klement Sekera4c533132018-02-22 11:41:12 +0100428 packets = self.dst_if.get_capture(len(self.pkt_infos))
Klement Sekera947a85c2019-07-24 12:40:37 +0000429 self.verify_capture(packets)
Klement Sekera4c533132018-02-22 11:41:12 +0100430 self.src_if.assert_nothing_captured()
Klement Sekera75e7d132017-09-20 08:26:30 +0200431
432 # run it all to verify correctness
433 self.pg_enable_capture()
Klement Sekera4c533132018-02-22 11:41:12 +0100434 self.src_if.add_stream(fragments)
Klement Sekera75e7d132017-09-20 08:26:30 +0200435 self.pg_start()
436
Klement Sekera4c533132018-02-22 11:41:12 +0100437 packets = self.dst_if.get_capture(len(self.pkt_infos))
Klement Sekera947a85c2019-07-24 12:40:37 +0000438 self.verify_capture(packets)
Klement Sekera4c533132018-02-22 11:41:12 +0100439 self.src_if.assert_nothing_captured()
Klement Sekera75e7d132017-09-20 08:26:30 +0200440
Klement Sekera947a85c2019-07-24 12:40:37 +0000441 def test_timeout_inline(self):
Klement Sekera75e7d132017-09-20 08:26:30 +0200442 """ timeout (inline) """
443
444 dropped_packet_indexes = set(
445 index for (index, frags, _, _) in self.pkt_infos if len(frags) > 1
446 )
447
Klement Sekera947a85c2019-07-24 12:40:37 +0000448 self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000,
449 max_reassembly_length=3,
450 expire_walk_interval_ms=10000)
451
452 self.pg_enable_capture()
453 self.src_if.add_stream(self.fragments_400)
454 self.pg_start()
455
456 packets = self.dst_if.get_capture(
457 len(self.pkt_infos) - len(dropped_packet_indexes))
458 self.verify_capture(packets, dropped_packet_indexes)
Klement Sekera4c533132018-02-22 11:41:12 +0100459 self.src_if.assert_nothing_captured()
Klement Sekera75e7d132017-09-20 08:26:30 +0200460
461 def test_timeout_cleanup(self):
462 """ timeout (cleanup) """
463
464 # whole packets + fragmented packets sans last fragment
465 fragments = [
466 x for (_, frags_400, _, _) in self.pkt_infos
467 for x in frags_400[:-1 if len(frags_400) > 1 else None]
468 ]
469
470 # last fragments for fragmented packets
471 fragments2 = [frags_400[-1]
472 for (_, frags_400, _, _) in self.pkt_infos
473 if len(frags_400) > 1]
474
475 dropped_packet_indexes = set(
476 index for (index, frags_400, _, _) in self.pkt_infos
477 if len(frags_400) > 1)
478
479 self.vapi.ip_reassembly_set(timeout_ms=100, max_reassemblies=1000,
Klement Sekera3a343d42019-05-16 14:35:46 +0200480 max_reassembly_length=1000,
Klement Sekera75e7d132017-09-20 08:26:30 +0200481 expire_walk_interval_ms=50)
482
483 self.pg_enable_capture()
Klement Sekera4c533132018-02-22 11:41:12 +0100484 self.src_if.add_stream(fragments)
Klement Sekera75e7d132017-09-20 08:26:30 +0200485 self.pg_start()
486
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +0200487 self.virtual_sleep(.25, "wait before sending rest of fragments")
Klement Sekera75e7d132017-09-20 08:26:30 +0200488
Klement Sekera4c533132018-02-22 11:41:12 +0100489 self.src_if.add_stream(fragments2)
Klement Sekera75e7d132017-09-20 08:26:30 +0200490 self.pg_start()
Klement Sekera75e7d132017-09-20 08:26:30 +0200491
Klement Sekera4c533132018-02-22 11:41:12 +0100492 packets = self.dst_if.get_capture(
Klement Sekera75e7d132017-09-20 08:26:30 +0200493 len(self.pkt_infos) - len(dropped_packet_indexes))
Klement Sekera947a85c2019-07-24 12:40:37 +0000494 self.verify_capture(packets, dropped_packet_indexes)
Klement Sekera4c533132018-02-22 11:41:12 +0100495 self.src_if.assert_nothing_captured()
Klement Sekera75e7d132017-09-20 08:26:30 +0200496
Klement Sekera947a85c2019-07-24 12:40:37 +0000497 def test_disabled(self):
Klement Sekera75e7d132017-09-20 08:26:30 +0200498 """ reassembly disabled """
499
500 dropped_packet_indexes = set(
501 index for (index, frags_400, _, _) in self.pkt_infos
502 if len(frags_400) > 1)
Klement Sekera947a85c2019-07-24 12:40:37 +0000503
504 self.vapi.ip_reassembly_set(timeout_ms=1000, max_reassemblies=0,
505 max_reassembly_length=3,
506 expire_walk_interval_ms=10000)
507
508 self.pg_enable_capture()
509 self.src_if.add_stream(self.fragments_400)
510 self.pg_start()
511
512 packets = self.dst_if.get_capture(
513 len(self.pkt_infos) - len(dropped_packet_indexes))
514 self.verify_capture(packets, dropped_packet_indexes)
515 self.src_if.assert_nothing_captured()
Klement Sekera75e7d132017-09-20 08:26:30 +0200516
Klement Sekera01c1fa42021-12-14 18:25:11 +0000517 def test_local_enable_disable(self):
518 """ local reassembly enabled/disable """
519 self.vapi.ip_reassembly_enable_disable(
520 sw_if_index=self.src_if.sw_if_index, enable_ip4=False)
521 self.vapi.ip_local_reass_enable_disable(enable_ip4=True)
522 p = (Ether(src=self.src_if.remote_mac, dst=self.src_if.local_mac) /
523 IP(src=self.src_if.remote_ip4, dst=self.src_if.local_ip4) /
524 ICMP(id=1234, type='echo-request') /
525 Raw('x' * 1000))
526 frags = fragment_rfc791(p, 400)
527 r = self.send_and_expect(self.src_if, frags, self.src_if, n_rx=1)[0]
528 self.assertEqual(1234, r[ICMP].id)
529 self.assertEqual(icmptypes[r[ICMP].type], 'echo-reply')
530 self.vapi.ip_local_reass_enable_disable()
531
532 self.send_and_assert_no_replies(self.src_if, frags)
533 self.vapi.ip_local_reass_enable_disable(enable_ip4=True)
534
Klement Sekera75e7d132017-09-20 08:26:30 +0200535
Klement Sekerade34c352019-06-25 11:19:22 +0000536class TestIPv4SVReassembly(VppTestCase):
537 """ IPv4 Shallow Virtual Reassembly """
538
539 @classmethod
540 def setUpClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +0000541 super().setUpClass()
Klement Sekerade34c352019-06-25 11:19:22 +0000542
543 cls.create_pg_interfaces([0, 1])
544 cls.src_if = cls.pg0
545 cls.dst_if = cls.pg1
546
547 # setup all interfaces
548 for i in cls.pg_interfaces:
549 i.admin_up()
550 i.config_ip4()
551 i.resolve_arp()
552
553 def setUp(self):
554 """ Test setup - force timeout on existing reassemblies """
Klement Sekera01c1fa42021-12-14 18:25:11 +0000555 super().setUp()
Klement Sekerade34c352019-06-25 11:19:22 +0000556 self.vapi.ip_reassembly_enable_disable(
557 sw_if_index=self.src_if.sw_if_index, enable_ip4=True,
558 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL)
559 self.vapi.ip_reassembly_set(
560 timeout_ms=0, max_reassemblies=1000,
561 max_reassembly_length=1000,
562 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
563 expire_walk_interval_ms=10)
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +0200564 self.virtual_sleep(.25)
Klement Sekerade34c352019-06-25 11:19:22 +0000565 self.vapi.ip_reassembly_set(
566 timeout_ms=1000000, max_reassemblies=1000,
567 max_reassembly_length=1000,
568 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
569 expire_walk_interval_ms=10000)
570
571 def tearDown(self):
Klement Sekera01c1fa42021-12-14 18:25:11 +0000572 super().tearDown()
Klement Sekerade34c352019-06-25 11:19:22 +0000573 self.logger.debug(self.vapi.ppcli("show ip4-sv-reassembly details"))
574 self.logger.debug(self.vapi.ppcli("show buffers"))
575
576 def test_basic(self):
577 """ basic reassembly """
578 payload_len = 1000
579 payload = ""
580 counter = 0
581 while len(payload) < payload_len:
582 payload += "%u " % counter
583 counter += 1
584
585 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
586 IP(id=1, src=self.src_if.remote_ip4,
587 dst=self.dst_if.remote_ip4) /
588 UDP(sport=1234, dport=5678) /
589 Raw(payload))
590 fragments = fragment_rfc791(p, payload_len/4)
591
592 # send fragment #2 - should be cached inside reassembly
593 self.pg_enable_capture()
594 self.src_if.add_stream(fragments[1])
595 self.pg_start()
596 self.logger.debug(self.vapi.ppcli("show ip4-sv-reassembly details"))
597 self.logger.debug(self.vapi.ppcli("show buffers"))
598 self.logger.debug(self.vapi.ppcli("show trace"))
599 self.dst_if.assert_nothing_captured()
600
601 # send fragment #1 - reassembly is finished now and both fragments
602 # forwarded
603 self.pg_enable_capture()
604 self.src_if.add_stream(fragments[0])
605 self.pg_start()
606 self.logger.debug(self.vapi.ppcli("show ip4-sv-reassembly details"))
607 self.logger.debug(self.vapi.ppcli("show buffers"))
608 self.logger.debug(self.vapi.ppcli("show trace"))
609 c = self.dst_if.get_capture(2)
610 for sent, recvd in zip([fragments[1], fragments[0]], c):
611 self.assertEqual(sent[IP].src, recvd[IP].src)
612 self.assertEqual(sent[IP].dst, recvd[IP].dst)
613 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
614
615 # send rest of fragments - should be immediately forwarded
616 self.pg_enable_capture()
617 self.src_if.add_stream(fragments[2:])
618 self.pg_start()
619 c = self.dst_if.get_capture(len(fragments[2:]))
620 for sent, recvd in zip(fragments[2:], c):
621 self.assertEqual(sent[IP].src, recvd[IP].src)
622 self.assertEqual(sent[IP].dst, recvd[IP].dst)
623 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
624
Klement Sekera53be16d2020-12-15 21:47:36 +0100625 def test_verify_clear_trace_mid_reassembly(self):
626 """ verify clear trace works mid-reassembly """
627 payload_len = 1000
628 payload = ""
629 counter = 0
630 while len(payload) < payload_len:
631 payload += "%u " % counter
632 counter += 1
633
634 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
635 IP(id=1, src=self.src_if.remote_ip4,
636 dst=self.dst_if.remote_ip4) /
637 UDP(sport=1234, dport=5678) /
638 Raw(payload))
639 fragments = fragment_rfc791(p, payload_len/4)
640
641 self.pg_enable_capture()
642 self.src_if.add_stream(fragments[1])
643 self.pg_start()
644
645 self.logger.debug(self.vapi.cli("show trace"))
646 self.vapi.cli("clear trace")
647
648 self.pg_enable_capture()
649 self.src_if.add_stream(fragments[0])
650 self.pg_start()
651 self.dst_if.get_capture(2)
652
653 self.logger.debug(self.vapi.cli("show trace"))
654 self.vapi.cli("clear trace")
655
656 self.pg_enable_capture()
657 self.src_if.add_stream(fragments[2:])
658 self.pg_start()
659 self.dst_if.get_capture(len(fragments[2:]))
660
Klement Sekerade34c352019-06-25 11:19:22 +0000661 def test_timeout(self):
662 """ reassembly timeout """
663 payload_len = 1000
664 payload = ""
665 counter = 0
666 while len(payload) < payload_len:
667 payload += "%u " % counter
668 counter += 1
669
670 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
671 IP(id=1, src=self.src_if.remote_ip4,
672 dst=self.dst_if.remote_ip4) /
673 UDP(sport=1234, dport=5678) /
674 Raw(payload))
675 fragments = fragment_rfc791(p, payload_len/4)
676
677 self.vapi.ip_reassembly_set(
678 timeout_ms=100, max_reassemblies=1000,
679 max_reassembly_length=1000,
680 expire_walk_interval_ms=50,
681 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL)
682
683 # send fragments #2 and #1 - should be forwarded
684 self.pg_enable_capture()
685 self.src_if.add_stream(fragments[0:2])
686 self.pg_start()
687 self.logger.debug(self.vapi.ppcli("show ip4-sv-reassembly details"))
688 self.logger.debug(self.vapi.ppcli("show buffers"))
689 self.logger.debug(self.vapi.ppcli("show trace"))
690 c = self.dst_if.get_capture(2)
691 for sent, recvd in zip([fragments[1], fragments[0]], c):
692 self.assertEqual(sent[IP].src, recvd[IP].src)
693 self.assertEqual(sent[IP].dst, recvd[IP].dst)
694 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
695
696 # wait for cleanup
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +0200697 self.virtual_sleep(.25, "wait before sending rest of fragments")
Klement Sekerade34c352019-06-25 11:19:22 +0000698
699 # send rest of fragments - shouldn't be forwarded
700 self.pg_enable_capture()
701 self.src_if.add_stream(fragments[2:])
702 self.pg_start()
703 self.dst_if.assert_nothing_captured()
704
705 def test_lru(self):
706 """ reassembly reuses LRU element """
707
708 self.vapi.ip_reassembly_set(
709 timeout_ms=1000000, max_reassemblies=1,
710 max_reassembly_length=1000,
711 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
712 expire_walk_interval_ms=10000)
713
714 payload_len = 1000
715 payload = ""
716 counter = 0
717 while len(payload) < payload_len:
718 payload += "%u " % counter
719 counter += 1
720
721 packet_count = 10
722
723 fragments = [f
724 for i in range(packet_count)
725 for p in (Ether(dst=self.src_if.local_mac,
726 src=self.src_if.remote_mac) /
727 IP(id=i, src=self.src_if.remote_ip4,
728 dst=self.dst_if.remote_ip4) /
729 UDP(sport=1234, dport=5678) /
730 Raw(payload))
731 for f in fragment_rfc791(p, payload_len/4)]
732
733 self.pg_enable_capture()
734 self.src_if.add_stream(fragments)
735 self.pg_start()
736 c = self.dst_if.get_capture(len(fragments))
737 for sent, recvd in zip(fragments, c):
738 self.assertEqual(sent[IP].src, recvd[IP].src)
739 self.assertEqual(sent[IP].dst, recvd[IP].dst)
740 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
741
Klement Sekera18c6cd92020-07-10 09:29:48 +0000742 def send_mixed_and_verify_capture(self, traffic):
743 stream = []
744 for t in traffic:
745 for c in range(t['count']):
746 stream.append(
747 (Ether(dst=self.src_if.local_mac,
748 src=self.src_if.remote_mac) /
749 IP(id=self.counter,
750 flags=t['flags'],
751 src=self.src_if.remote_ip4,
752 dst=self.dst_if.remote_ip4) /
753 UDP(sport=1234, dport=5678) /
754 Raw("abcdef")))
755 self.counter = self.counter + 1
756
757 self.pg_enable_capture()
758 self.src_if.add_stream(stream)
759 self.pg_start()
760 self.logger.debug(self.vapi.ppcli("show ip4-sv-reassembly details"))
761 self.logger.debug(self.vapi.ppcli("show buffers"))
762 self.logger.debug(self.vapi.ppcli("show trace"))
763 self.dst_if.get_capture(len(stream))
764
765 def test_mixed(self):
766 """ mixed traffic correctly passes through SVR """
767 self.counter = 1
768
769 self.send_mixed_and_verify_capture([{'count': 1, 'flags': ''}])
770 self.send_mixed_and_verify_capture([{'count': 2, 'flags': ''}])
771 self.send_mixed_and_verify_capture([{'count': 3, 'flags': ''}])
772 self.send_mixed_and_verify_capture([{'count': 8, 'flags': ''}])
773 self.send_mixed_and_verify_capture([{'count': 257, 'flags': ''}])
774
775 self.send_mixed_and_verify_capture([{'count': 1, 'flags': 'MF'}])
776 self.send_mixed_and_verify_capture([{'count': 2, 'flags': 'MF'}])
777 self.send_mixed_and_verify_capture([{'count': 3, 'flags': 'MF'}])
778 self.send_mixed_and_verify_capture([{'count': 8, 'flags': 'MF'}])
779 self.send_mixed_and_verify_capture([{'count': 257, 'flags': 'MF'}])
780
781 self.send_mixed_and_verify_capture(
782 [{'count': 1, 'flags': ''}, {'count': 1, 'flags': 'MF'}])
783 self.send_mixed_and_verify_capture(
784 [{'count': 2, 'flags': ''}, {'count': 2, 'flags': 'MF'}])
785 self.send_mixed_and_verify_capture(
786 [{'count': 3, 'flags': ''}, {'count': 3, 'flags': 'MF'}])
787 self.send_mixed_and_verify_capture(
788 [{'count': 8, 'flags': ''}, {'count': 8, 'flags': 'MF'}])
789 self.send_mixed_and_verify_capture(
790 [{'count': 129, 'flags': ''}, {'count': 129, 'flags': 'MF'}])
791
792 self.send_mixed_and_verify_capture(
793 [{'count': 1, 'flags': ''}, {'count': 1, 'flags': 'MF'},
794 {'count': 1, 'flags': ''}, {'count': 1, 'flags': 'MF'}])
795 self.send_mixed_and_verify_capture(
796 [{'count': 2, 'flags': ''}, {'count': 2, 'flags': 'MF'},
797 {'count': 2, 'flags': ''}, {'count': 2, 'flags': 'MF'}])
798 self.send_mixed_and_verify_capture(
799 [{'count': 3, 'flags': ''}, {'count': 3, 'flags': 'MF'},
800 {'count': 3, 'flags': ''}, {'count': 3, 'flags': 'MF'}])
801 self.send_mixed_and_verify_capture(
802 [{'count': 8, 'flags': ''}, {'count': 8, 'flags': 'MF'},
803 {'count': 8, 'flags': ''}, {'count': 8, 'flags': 'MF'}])
804 self.send_mixed_and_verify_capture(
805 [{'count': 65, 'flags': ''}, {'count': 65, 'flags': 'MF'},
806 {'count': 65, 'flags': ''}, {'count': 65, 'flags': 'MF'}])
807
Klement Sekerade34c352019-06-25 11:19:22 +0000808
Klement Sekera630ab582019-07-19 09:14:19 +0000809class TestIPv4MWReassembly(VppTestCase):
810 """ IPv4 Reassembly (multiple workers) """
Klement Sekera8d815022021-03-15 16:58:10 +0100811 vpp_worker_count = 3
Klement Sekera630ab582019-07-19 09:14:19 +0000812
813 @classmethod
814 def setUpClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +0000815 super().setUpClass()
Klement Sekera630ab582019-07-19 09:14:19 +0000816
Klement Sekera8d815022021-03-15 16:58:10 +0100817 cls.create_pg_interfaces(range(cls.vpp_worker_count+1))
Klement Sekera630ab582019-07-19 09:14:19 +0000818 cls.src_if = cls.pg0
819 cls.send_ifs = cls.pg_interfaces[:-1]
820 cls.dst_if = cls.pg_interfaces[-1]
821
822 # setup all interfaces
823 for i in cls.pg_interfaces:
824 i.admin_up()
825 i.config_ip4()
826 i.resolve_arp()
827
828 # packets sizes reduced here because we are generating packets without
829 # Ethernet headers, which are added later (diff fragments go via
830 # different interfaces)
831 cls.packet_sizes = [64-len(Ether()), 512-len(Ether()),
832 1518-len(Ether()), 9018-len(Ether())]
833 cls.padding = " abcdefghijklmn"
834 cls.create_stream(cls.packet_sizes)
835 cls.create_fragments()
836
837 @classmethod
838 def tearDownClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +0000839 super().tearDownClass()
Klement Sekera630ab582019-07-19 09:14:19 +0000840
841 def setUp(self):
842 """ Test setup - force timeout on existing reassemblies """
Klement Sekera01c1fa42021-12-14 18:25:11 +0000843 super().setUp()
Klement Sekera630ab582019-07-19 09:14:19 +0000844 for intf in self.send_ifs:
845 self.vapi.ip_reassembly_enable_disable(
846 sw_if_index=intf.sw_if_index, enable_ip4=True)
847 self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000,
848 max_reassembly_length=1000,
849 expire_walk_interval_ms=10)
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +0200850 self.virtual_sleep(.25)
Klement Sekera630ab582019-07-19 09:14:19 +0000851 self.vapi.ip_reassembly_set(timeout_ms=1000000, max_reassemblies=1000,
852 max_reassembly_length=1000,
853 expire_walk_interval_ms=10000)
854
855 def tearDown(self):
Klement Sekera01c1fa42021-12-14 18:25:11 +0000856 for intf in self.send_ifs:
857 self.vapi.ip_reassembly_enable_disable(
858 sw_if_index=intf.sw_if_index, enable_ip4=False)
859 super().tearDown()
Klement Sekera630ab582019-07-19 09:14:19 +0000860
861 def show_commands_at_teardown(self):
Klement Sekera896c8962019-06-24 11:52:49 +0000862 self.logger.debug(self.vapi.ppcli("show ip4-full-reassembly details"))
Klement Sekera630ab582019-07-19 09:14:19 +0000863 self.logger.debug(self.vapi.ppcli("show buffers"))
864
865 @classmethod
866 def create_stream(cls, packet_sizes, packet_count=test_packet_count):
867 """Create input packet stream
868
869 :param list packet_sizes: Required packet sizes.
870 """
871 for i in range(0, packet_count):
872 info = cls.create_packet_info(cls.src_if, cls.src_if)
873 payload = cls.info_to_payload(info)
874 p = (IP(id=info.index, src=cls.src_if.remote_ip4,
875 dst=cls.dst_if.remote_ip4) /
876 UDP(sport=1234, dport=5678) /
877 Raw(payload))
878 size = packet_sizes[(i // 2) % len(packet_sizes)]
879 cls.extend_packet(p, size, cls.padding)
880 info.data = p
881
882 @classmethod
883 def create_fragments(cls):
884 infos = cls._packet_infos
885 cls.pkt_infos = []
Paul Vinciguerra090096b2020-12-03 00:42:46 -0500886 for index, info in infos.items():
Klement Sekera630ab582019-07-19 09:14:19 +0000887 p = info.data
888 # cls.logger.debug(ppp("Packet:",
889 # p.__class__(scapy.compat.raw(p))))
890 fragments_400 = fragment_rfc791(p, 400)
891 cls.pkt_infos.append((index, fragments_400))
892 cls.fragments_400 = [
893 x for (_, frags) in cls.pkt_infos for x in frags]
894 cls.logger.debug("Fragmented %s packets into %s 400-byte fragments, " %
895 (len(infos), len(cls.fragments_400)))
896
897 def verify_capture(self, capture, dropped_packet_indexes=[]):
898 """Verify captured packet stream.
899
900 :param list capture: Captured packet stream.
901 """
902 info = None
903 seen = set()
904 for packet in capture:
905 try:
906 self.logger.debug(ppp("Got packet:", packet))
907 ip = packet[IP]
908 udp = packet[UDP]
909 payload_info = self.payload_to_info(packet[Raw])
910 packet_index = payload_info.index
911 self.assertTrue(
912 packet_index not in dropped_packet_indexes,
913 ppp("Packet received, but should be dropped:", packet))
914 if packet_index in seen:
915 raise Exception(ppp("Duplicate packet received", packet))
916 seen.add(packet_index)
917 self.assertEqual(payload_info.dst, self.src_if.sw_if_index)
918 info = self._packet_infos[packet_index]
919 self.assertTrue(info is not None)
920 self.assertEqual(packet_index, info.index)
921 saved_packet = info.data
922 self.assertEqual(ip.src, saved_packet[IP].src)
923 self.assertEqual(ip.dst, saved_packet[IP].dst)
924 self.assertEqual(udp.payload, saved_packet[UDP].payload)
925 except Exception:
926 self.logger.error(ppp("Unexpected or invalid packet:", packet))
927 raise
928 for index in self._packet_infos:
929 self.assertTrue(index in seen or index in dropped_packet_indexes,
930 "Packet with packet_index %d not received" % index)
931
932 def send_packets(self, packets):
Klement Sekera8d815022021-03-15 16:58:10 +0100933 for counter in range(self.vpp_worker_count):
Klement Sekera630ab582019-07-19 09:14:19 +0000934 if 0 == len(packets[counter]):
935 continue
936 send_if = self.send_ifs[counter]
937 send_if.add_stream(
938 (Ether(dst=send_if.local_mac, src=send_if.remote_mac) / x
939 for x in packets[counter]),
940 worker=counter)
941 self.pg_start()
942
943 def test_worker_conflict(self):
944 """ 1st and FO=0 fragments on different workers """
945
946 # in first wave we send fragments which don't start at offset 0
947 # then we send fragments with offset 0 on a different thread
948 # then the rest of packets on a random thread
Klement Sekera8d815022021-03-15 16:58:10 +0100949 first_packets = [[] for n in range(self.vpp_worker_count)]
950 second_packets = [[] for n in range(self.vpp_worker_count)]
951 rest_of_packets = [[] for n in range(self.vpp_worker_count)]
Klement Sekera630ab582019-07-19 09:14:19 +0000952 for (_, p) in self.pkt_infos:
Klement Sekera8d815022021-03-15 16:58:10 +0100953 wi = randrange(self.vpp_worker_count)
Klement Sekera630ab582019-07-19 09:14:19 +0000954 second_packets[wi].append(p[0])
955 if len(p) <= 1:
956 continue
957 wi2 = wi
958 while wi2 == wi:
Klement Sekera8d815022021-03-15 16:58:10 +0100959 wi2 = randrange(self.vpp_worker_count)
Klement Sekera630ab582019-07-19 09:14:19 +0000960 first_packets[wi2].append(p[1])
Klement Sekera8d815022021-03-15 16:58:10 +0100961 wi3 = randrange(self.vpp_worker_count)
Klement Sekera630ab582019-07-19 09:14:19 +0000962 rest_of_packets[wi3].extend(p[2:])
963
964 self.pg_enable_capture()
965 self.send_packets(first_packets)
966 self.send_packets(second_packets)
967 self.send_packets(rest_of_packets)
968
969 packets = self.dst_if.get_capture(len(self.pkt_infos))
970 self.verify_capture(packets)
971 for send_if in self.send_ifs:
972 send_if.assert_nothing_captured()
973
Klement Sekera68bae5b2019-10-10 18:57:34 +0000974 self.logger.debug(self.vapi.ppcli("show trace"))
975 self.logger.debug(self.vapi.ppcli("show ip4-full-reassembly details"))
976 self.logger.debug(self.vapi.ppcli("show buffers"))
977 self.vapi.cli("clear trace")
978
Klement Sekera630ab582019-07-19 09:14:19 +0000979 self.pg_enable_capture()
980 self.send_packets(first_packets)
981 self.send_packets(second_packets)
982 self.send_packets(rest_of_packets)
983
984 packets = self.dst_if.get_capture(len(self.pkt_infos))
985 self.verify_capture(packets)
986 for send_if in self.send_ifs:
987 send_if.assert_nothing_captured()
988
989
Klement Sekera947a85c2019-07-24 12:40:37 +0000990class TestIPv6Reassembly(VppTestCase):
Klement Sekera75e7d132017-09-20 08:26:30 +0200991 """ IPv6 Reassembly """
992
993 @classmethod
994 def setUpClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +0000995 super().setUpClass()
Klement Sekera75e7d132017-09-20 08:26:30 +0200996
Klement Sekera4c533132018-02-22 11:41:12 +0100997 cls.create_pg_interfaces([0, 1])
998 cls.src_if = cls.pg0
999 cls.dst_if = cls.pg1
Klement Sekera75e7d132017-09-20 08:26:30 +02001000
1001 # setup all interfaces
1002 for i in cls.pg_interfaces:
1003 i.admin_up()
1004 i.config_ip6()
1005 i.resolve_ndp()
1006
Klement Sekera75e7d132017-09-20 08:26:30 +02001007 # packet sizes
1008 cls.packet_sizes = [64, 512, 1518, 9018]
1009 cls.padding = " abcdefghijklmn"
1010 cls.create_stream(cls.packet_sizes)
1011 cls.create_fragments()
1012
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07001013 @classmethod
1014 def tearDownClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +00001015 super().tearDownClass()
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07001016
Klement Sekera75e7d132017-09-20 08:26:30 +02001017 def setUp(self):
1018 """ Test setup - force timeout on existing reassemblies """
Klement Sekera01c1fa42021-12-14 18:25:11 +00001019 super().setUp()
Klement Sekera4c533132018-02-22 11:41:12 +01001020 self.vapi.ip_reassembly_enable_disable(
1021 sw_if_index=self.src_if.sw_if_index, enable_ip6=True)
Klement Sekera75e7d132017-09-20 08:26:30 +02001022 self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000,
Klement Sekera3a343d42019-05-16 14:35:46 +02001023 max_reassembly_length=1000,
Klement Sekera75e7d132017-09-20 08:26:30 +02001024 expire_walk_interval_ms=10, is_ip6=1)
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +02001025 self.virtual_sleep(.25)
Klement Sekera75e7d132017-09-20 08:26:30 +02001026 self.vapi.ip_reassembly_set(timeout_ms=1000000, max_reassemblies=1000,
Klement Sekera3a343d42019-05-16 14:35:46 +02001027 max_reassembly_length=1000,
Klement Sekera75e7d132017-09-20 08:26:30 +02001028 expire_walk_interval_ms=10000, is_ip6=1)
Klement Sekera896c8962019-06-24 11:52:49 +00001029 self.logger.debug(self.vapi.ppcli("show ip6-full-reassembly details"))
Klement Sekeracae98b72019-02-19 13:53:43 +01001030 self.logger.debug(self.vapi.ppcli("show buffers"))
Klement Sekera75e7d132017-09-20 08:26:30 +02001031
1032 def tearDown(self):
Klement Sekera01c1fa42021-12-14 18:25:11 +00001033 self.vapi.ip_reassembly_enable_disable(
1034 sw_if_index=self.src_if.sw_if_index, enable_ip6=False)
1035 super().tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -07001036
1037 def show_commands_at_teardown(self):
Klement Sekera896c8962019-06-24 11:52:49 +00001038 self.logger.debug(self.vapi.ppcli("show ip6-full-reassembly details"))
Klement Sekeracae98b72019-02-19 13:53:43 +01001039 self.logger.debug(self.vapi.ppcli("show buffers"))
Klement Sekera75e7d132017-09-20 08:26:30 +02001040
1041 @classmethod
1042 def create_stream(cls, packet_sizes, packet_count=test_packet_count):
1043 """Create input packet stream for defined interface.
1044
1045 :param list packet_sizes: Required packet sizes.
1046 """
1047 for i in range(0, packet_count):
Klement Sekera4c533132018-02-22 11:41:12 +01001048 info = cls.create_packet_info(cls.src_if, cls.src_if)
Klement Sekera75e7d132017-09-20 08:26:30 +02001049 payload = cls.info_to_payload(info)
Klement Sekera4c533132018-02-22 11:41:12 +01001050 p = (Ether(dst=cls.src_if.local_mac, src=cls.src_if.remote_mac) /
1051 IPv6(src=cls.src_if.remote_ip6,
1052 dst=cls.dst_if.remote_ip6) /
1053 UDP(sport=1234, dport=5678) /
Klement Sekera75e7d132017-09-20 08:26:30 +02001054 Raw(payload))
1055 size = packet_sizes[(i // 2) % len(packet_sizes)]
1056 cls.extend_packet(p, size, cls.padding)
1057 info.data = p
1058
1059 @classmethod
1060 def create_fragments(cls):
1061 infos = cls._packet_infos
1062 cls.pkt_infos = []
Paul Vinciguerra090096b2020-12-03 00:42:46 -05001063 for index, info in infos.items():
Klement Sekera75e7d132017-09-20 08:26:30 +02001064 p = info.data
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -07001065 # cls.logger.debug(ppp("Packet:",
1066 # p.__class__(scapy.compat.raw(p))))
Klement Sekera75e7d132017-09-20 08:26:30 +02001067 fragments_400 = fragment_rfc8200(p, info.index, 400)
1068 fragments_300 = fragment_rfc8200(p, info.index, 300)
1069 cls.pkt_infos.append((index, fragments_400, fragments_300))
1070 cls.fragments_400 = [
1071 x for _, frags, _ in cls.pkt_infos for x in frags]
1072 cls.fragments_300 = [
1073 x for _, _, frags in cls.pkt_infos for x in frags]
1074 cls.logger.debug("Fragmented %s packets into %s 400-byte fragments, "
1075 "and %s 300-byte fragments" %
1076 (len(infos), len(cls.fragments_400),
1077 len(cls.fragments_300)))
1078
Klement Sekera947a85c2019-07-24 12:40:37 +00001079 def verify_capture(self, capture, dropped_packet_indexes=[]):
1080 """Verify captured packet strea .
1081
1082 :param list capture: Captured packet stream.
1083 """
1084 info = None
1085 seen = set()
1086 for packet in capture:
1087 try:
1088 self.logger.debug(ppp("Got packet:", packet))
1089 ip = packet[IPv6]
1090 udp = packet[UDP]
1091 payload_info = self.payload_to_info(packet[Raw])
1092 packet_index = payload_info.index
1093 self.assertTrue(
1094 packet_index not in dropped_packet_indexes,
1095 ppp("Packet received, but should be dropped:", packet))
1096 if packet_index in seen:
1097 raise Exception(ppp("Duplicate packet received", packet))
1098 seen.add(packet_index)
1099 self.assertEqual(payload_info.dst, self.src_if.sw_if_index)
1100 info = self._packet_infos[packet_index]
1101 self.assertTrue(info is not None)
1102 self.assertEqual(packet_index, info.index)
1103 saved_packet = info.data
1104 self.assertEqual(ip.src, saved_packet[IPv6].src)
1105 self.assertEqual(ip.dst, saved_packet[IPv6].dst)
1106 self.assertEqual(udp.payload, saved_packet[UDP].payload)
1107 except Exception:
1108 self.logger.error(ppp("Unexpected or invalid packet:", packet))
1109 raise
1110 for index in self._packet_infos:
1111 self.assertTrue(index in seen or index in dropped_packet_indexes,
1112 "Packet with packet_index %d not received" % index)
1113
1114 def test_reassembly(self):
Klement Sekera75e7d132017-09-20 08:26:30 +02001115 """ basic reassembly """
1116
Klement Sekera947a85c2019-07-24 12:40:37 +00001117 self.pg_enable_capture()
1118 self.src_if.add_stream(self.fragments_400)
1119 self.pg_start()
1120
1121 packets = self.dst_if.get_capture(len(self.pkt_infos))
1122 self.verify_capture(packets)
1123 self.src_if.assert_nothing_captured()
1124
1125 # run it all again to verify correctness
1126 self.pg_enable_capture()
1127 self.src_if.add_stream(self.fragments_400)
1128 self.pg_start()
1129
1130 packets = self.dst_if.get_capture(len(self.pkt_infos))
1131 self.verify_capture(packets)
1132 self.src_if.assert_nothing_captured()
1133
Klement Sekera769145c2019-03-06 11:59:57 +01001134 def test_buffer_boundary(self):
1135 """ fragment header crossing buffer boundary """
1136
1137 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
1138 IPv6(src=self.src_if.remote_ip6,
1139 dst=self.src_if.local_ip6) /
1140 IPv6ExtHdrHopByHop(
1141 options=[HBHOptUnknown(otype=0xff, optlen=0)] * 1000) /
1142 IPv6ExtHdrFragment(m=1) /
1143 UDP(sport=1234, dport=5678) /
1144 Raw())
1145 self.pg_enable_capture()
1146 self.src_if.add_stream([p])
1147 self.pg_start()
1148 self.src_if.assert_nothing_captured()
1149 self.dst_if.assert_nothing_captured()
1150
Klement Sekera53be16d2020-12-15 21:47:36 +01001151 def test_verify_clear_trace_mid_reassembly(self):
1152 """ verify clear trace works mid-reassembly """
1153
1154 self.pg_enable_capture()
1155 self.src_if.add_stream(self.fragments_400[0:-1])
1156 self.pg_start()
1157
1158 self.logger.debug(self.vapi.cli("show trace"))
1159 self.vapi.cli("clear trace")
1160
1161 self.src_if.add_stream(self.fragments_400[-1])
1162 self.pg_start()
1163 packets = self.dst_if.get_capture(len(self.pkt_infos))
1164 self.verify_capture(packets)
1165
Klement Sekera947a85c2019-07-24 12:40:37 +00001166 def test_reversed(self):
Klement Sekera75e7d132017-09-20 08:26:30 +02001167 """ reverse order reassembly """
1168
Klement Sekera947a85c2019-07-24 12:40:37 +00001169 fragments = list(self.fragments_400)
1170 fragments.reverse()
1171
1172 self.pg_enable_capture()
1173 self.src_if.add_stream(fragments)
1174 self.pg_start()
1175
1176 packets = self.dst_if.get_capture(len(self.pkt_infos))
1177 self.verify_capture(packets)
1178 self.src_if.assert_nothing_captured()
1179
1180 # run it all again to verify correctness
1181 self.pg_enable_capture()
1182 self.src_if.add_stream(fragments)
1183 self.pg_start()
1184
1185 packets = self.dst_if.get_capture(len(self.pkt_infos))
1186 self.verify_capture(packets)
1187 self.src_if.assert_nothing_captured()
1188
1189 def test_random(self):
Klement Sekera75e7d132017-09-20 08:26:30 +02001190 """ random order reassembly """
1191
Klement Sekera947a85c2019-07-24 12:40:37 +00001192 fragments = list(self.fragments_400)
1193 shuffle(fragments)
1194
1195 self.pg_enable_capture()
1196 self.src_if.add_stream(fragments)
1197 self.pg_start()
1198
1199 packets = self.dst_if.get_capture(len(self.pkt_infos))
1200 self.verify_capture(packets)
1201 self.src_if.assert_nothing_captured()
1202
1203 # run it all again to verify correctness
1204 self.pg_enable_capture()
1205 self.src_if.add_stream(fragments)
1206 self.pg_start()
1207
1208 packets = self.dst_if.get_capture(len(self.pkt_infos))
1209 self.verify_capture(packets)
1210 self.src_if.assert_nothing_captured()
1211
1212 def test_duplicates(self):
Klement Sekera75e7d132017-09-20 08:26:30 +02001213 """ duplicate fragments """
1214
1215 fragments = [
1216 x for (_, frags, _) in self.pkt_infos
1217 for x in frags
1218 for _ in range(0, min(2, len(frags)))
1219 ]
Klement Sekera947a85c2019-07-24 12:40:37 +00001220
1221 self.pg_enable_capture()
1222 self.src_if.add_stream(fragments)
1223 self.pg_start()
1224
1225 packets = self.dst_if.get_capture(len(self.pkt_infos))
1226 self.verify_capture(packets)
1227 self.src_if.assert_nothing_captured()
Klement Sekera75e7d132017-09-20 08:26:30 +02001228
Klement Sekera3a343d42019-05-16 14:35:46 +02001229 def test_long_fragment_chain(self):
1230 """ long fragment chain """
1231
1232 error_cnt_str = \
Klement Sekera896c8962019-06-24 11:52:49 +00001233 "/err/ip6-full-reassembly-feature/fragment chain too long (drop)"
Klement Sekera3a343d42019-05-16 14:35:46 +02001234
Klement Sekera34641f22019-05-22 20:18:26 +02001235 error_cnt = self.statistics.get_err_counter(error_cnt_str)
Klement Sekera3a343d42019-05-16 14:35:46 +02001236
1237 self.vapi.ip_reassembly_set(timeout_ms=100, max_reassemblies=1000,
1238 max_reassembly_length=3,
1239 expire_walk_interval_ms=50, is_ip6=1)
1240
1241 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
1242 IPv6(src=self.src_if.remote_ip6,
1243 dst=self.dst_if.remote_ip6) /
1244 UDP(sport=1234, dport=5678) /
Ole Troan127fbec2019-10-18 15:22:56 +02001245 Raw(b"X" * 1000))
Klement Sekera3a343d42019-05-16 14:35:46 +02001246 frags = fragment_rfc8200(p, 1, 300) + fragment_rfc8200(p, 2, 500)
1247
1248 self.pg_enable_capture()
1249 self.src_if.add_stream(frags)
1250 self.pg_start()
1251
1252 self.dst_if.get_capture(1)
Klement Sekera34641f22019-05-22 20:18:26 +02001253 self.assert_error_counter_equal(error_cnt_str, error_cnt + 1)
Klement Sekera3a343d42019-05-16 14:35:46 +02001254
Klement Sekera75e7d132017-09-20 08:26:30 +02001255 def test_overlap1(self):
Klement Sekera947a85c2019-07-24 12:40:37 +00001256 """ overlapping fragments case #1 """
Klement Sekera75e7d132017-09-20 08:26:30 +02001257
1258 fragments = []
1259 for _, frags_400, frags_300 in self.pkt_infos:
1260 if len(frags_300) == 1:
1261 fragments.extend(frags_400)
1262 else:
1263 for i, j in zip(frags_300, frags_400):
1264 fragments.extend(i)
1265 fragments.extend(j)
1266
1267 dropped_packet_indexes = set(
1268 index for (index, _, frags) in self.pkt_infos if len(frags) > 1
1269 )
1270
1271 self.pg_enable_capture()
Klement Sekera4c533132018-02-22 11:41:12 +01001272 self.src_if.add_stream(fragments)
Klement Sekera75e7d132017-09-20 08:26:30 +02001273 self.pg_start()
1274
Klement Sekera4c533132018-02-22 11:41:12 +01001275 packets = self.dst_if.get_capture(
Klement Sekera75e7d132017-09-20 08:26:30 +02001276 len(self.pkt_infos) - len(dropped_packet_indexes))
Klement Sekera947a85c2019-07-24 12:40:37 +00001277 self.verify_capture(packets, dropped_packet_indexes)
Klement Sekera4c533132018-02-22 11:41:12 +01001278 self.src_if.assert_nothing_captured()
Klement Sekera75e7d132017-09-20 08:26:30 +02001279
1280 def test_overlap2(self):
Klement Sekera947a85c2019-07-24 12:40:37 +00001281 """ overlapping fragments case #2 """
Klement Sekera75e7d132017-09-20 08:26:30 +02001282
1283 fragments = []
Klement Sekera4c533132018-02-22 11:41:12 +01001284 for _, frags_400, frags_300 in self.pkt_infos:
Klement Sekera75e7d132017-09-20 08:26:30 +02001285 if len(frags_400) == 1:
1286 fragments.extend(frags_400)
1287 else:
1288 # care must be taken here so that there are no fragments
1289 # received by vpp after reassembly is finished, otherwise
1290 # new reassemblies will be started and packet generator will
1291 # freak out when it detects unfreed buffers
Klement Sekera4c533132018-02-22 11:41:12 +01001292 zipped = zip(frags_400, frags_300)
Paul Vinciguerrac7834e02019-03-02 10:43:05 -08001293 for i, j in zipped:
Klement Sekera75e7d132017-09-20 08:26:30 +02001294 fragments.extend(i)
1295 fragments.extend(j)
Paul Vinciguerrac7834e02019-03-02 10:43:05 -08001296 fragments.pop()
Klement Sekera75e7d132017-09-20 08:26:30 +02001297
1298 dropped_packet_indexes = set(
1299 index for (index, _, frags) in self.pkt_infos if len(frags) > 1
1300 )
1301
1302 self.pg_enable_capture()
Klement Sekera4c533132018-02-22 11:41:12 +01001303 self.src_if.add_stream(fragments)
Klement Sekera75e7d132017-09-20 08:26:30 +02001304 self.pg_start()
1305
Klement Sekera4c533132018-02-22 11:41:12 +01001306 packets = self.dst_if.get_capture(
Klement Sekera75e7d132017-09-20 08:26:30 +02001307 len(self.pkt_infos) - len(dropped_packet_indexes))
Klement Sekera947a85c2019-07-24 12:40:37 +00001308 self.verify_capture(packets, dropped_packet_indexes)
Klement Sekera4c533132018-02-22 11:41:12 +01001309 self.src_if.assert_nothing_captured()
Klement Sekera75e7d132017-09-20 08:26:30 +02001310
Klement Sekera947a85c2019-07-24 12:40:37 +00001311 def test_timeout_inline(self):
Klement Sekera75e7d132017-09-20 08:26:30 +02001312 """ timeout (inline) """
1313
1314 dropped_packet_indexes = set(
1315 index for (index, frags, _) in self.pkt_infos if len(frags) > 1
1316 )
1317
Klement Sekera947a85c2019-07-24 12:40:37 +00001318 self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000,
1319 max_reassembly_length=3,
1320 expire_walk_interval_ms=10000, is_ip6=1)
1321
1322 self.pg_enable_capture()
1323 self.src_if.add_stream(self.fragments_400)
1324 self.pg_start()
1325
1326 packets = self.dst_if.get_capture(
1327 len(self.pkt_infos) - len(dropped_packet_indexes))
1328 self.verify_capture(packets, dropped_packet_indexes)
Neale Ranns5c6dd172022-02-17 09:08:47 +00001329 pkts = self.src_if._get_capture(1)
Klement Sekera75e7d132017-09-20 08:26:30 +02001330 for icmp in pkts:
1331 self.assertIn(ICMPv6TimeExceeded, icmp)
1332 self.assertIn(IPv6ExtHdrFragment, icmp)
1333 self.assertIn(icmp[IPv6ExtHdrFragment].id, dropped_packet_indexes)
1334 dropped_packet_indexes.remove(icmp[IPv6ExtHdrFragment].id)
1335
1336 def test_timeout_cleanup(self):
1337 """ timeout (cleanup) """
1338
1339 # whole packets + fragmented packets sans last fragment
1340 fragments = [
1341 x for (_, frags_400, _) in self.pkt_infos
1342 for x in frags_400[:-1 if len(frags_400) > 1 else None]
1343 ]
1344
1345 # last fragments for fragmented packets
1346 fragments2 = [frags_400[-1]
1347 for (_, frags_400, _) in self.pkt_infos
1348 if len(frags_400) > 1]
1349
1350 dropped_packet_indexes = set(
1351 index for (index, frags_400, _) in self.pkt_infos
1352 if len(frags_400) > 1)
1353
1354 self.vapi.ip_reassembly_set(timeout_ms=100, max_reassemblies=1000,
Klement Sekera3a343d42019-05-16 14:35:46 +02001355 max_reassembly_length=1000,
Klement Sekera75e7d132017-09-20 08:26:30 +02001356 expire_walk_interval_ms=50)
1357
1358 self.vapi.ip_reassembly_set(timeout_ms=100, max_reassemblies=1000,
Klement Sekera3a343d42019-05-16 14:35:46 +02001359 max_reassembly_length=1000,
Klement Sekera75e7d132017-09-20 08:26:30 +02001360 expire_walk_interval_ms=50, is_ip6=1)
1361
1362 self.pg_enable_capture()
Klement Sekera4c533132018-02-22 11:41:12 +01001363 self.src_if.add_stream(fragments)
Klement Sekera75e7d132017-09-20 08:26:30 +02001364 self.pg_start()
1365
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +02001366 self.virtual_sleep(.25, "wait before sending rest of fragments")
Klement Sekera75e7d132017-09-20 08:26:30 +02001367
Klement Sekera4c533132018-02-22 11:41:12 +01001368 self.src_if.add_stream(fragments2)
Klement Sekera75e7d132017-09-20 08:26:30 +02001369 self.pg_start()
Klement Sekera75e7d132017-09-20 08:26:30 +02001370
Klement Sekera4c533132018-02-22 11:41:12 +01001371 packets = self.dst_if.get_capture(
Klement Sekera75e7d132017-09-20 08:26:30 +02001372 len(self.pkt_infos) - len(dropped_packet_indexes))
Klement Sekera947a85c2019-07-24 12:40:37 +00001373 self.verify_capture(packets, dropped_packet_indexes)
Neale Ranns5c6dd172022-02-17 09:08:47 +00001374 pkts = self.src_if._get_capture(1)
Klement Sekera75e7d132017-09-20 08:26:30 +02001375 for icmp in pkts:
1376 self.assertIn(ICMPv6TimeExceeded, icmp)
1377 self.assertIn(IPv6ExtHdrFragment, icmp)
1378 self.assertIn(icmp[IPv6ExtHdrFragment].id, dropped_packet_indexes)
1379 dropped_packet_indexes.remove(icmp[IPv6ExtHdrFragment].id)
1380
Klement Sekera947a85c2019-07-24 12:40:37 +00001381 def test_disabled(self):
Klement Sekera75e7d132017-09-20 08:26:30 +02001382 """ reassembly disabled """
1383
1384 dropped_packet_indexes = set(
1385 index for (index, frags_400, _) in self.pkt_infos
1386 if len(frags_400) > 1)
Klement Sekera947a85c2019-07-24 12:40:37 +00001387
1388 self.vapi.ip_reassembly_set(timeout_ms=1000, max_reassemblies=0,
1389 max_reassembly_length=3,
1390 expire_walk_interval_ms=10000, is_ip6=1)
1391
1392 self.pg_enable_capture()
1393 self.src_if.add_stream(self.fragments_400)
1394 self.pg_start()
1395
1396 packets = self.dst_if.get_capture(
1397 len(self.pkt_infos) - len(dropped_packet_indexes))
1398 self.verify_capture(packets, dropped_packet_indexes)
Klement Sekera4c533132018-02-22 11:41:12 +01001399 self.src_if.assert_nothing_captured()
Klement Sekera75e7d132017-09-20 08:26:30 +02001400
1401 def test_missing_upper(self):
1402 """ missing upper layer """
Ole Troan03092c12021-11-23 15:55:39 +01001403 optdata = '\x00' * 100
Klement Sekera4c533132018-02-22 11:41:12 +01001404 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
1405 IPv6(src=self.src_if.remote_ip6,
1406 dst=self.src_if.local_ip6) /
Ole Troan03092c12021-11-23 15:55:39 +01001407 IPv6ExtHdrFragment(m=1) /
1408 IPv6ExtHdrDestOpt(nh=17, options=PadN(optdata='\101' * 255) /
1409 PadN(optdata='\102'*255)))
1410
Klement Sekera75e7d132017-09-20 08:26:30 +02001411 self.pg_enable_capture()
Ole Troan03092c12021-11-23 15:55:39 +01001412 self.src_if.add_stream([p])
Klement Sekera75e7d132017-09-20 08:26:30 +02001413 self.pg_start()
Klement Sekera4c533132018-02-22 11:41:12 +01001414 pkts = self.src_if.get_capture(expected_count=1)
Klement Sekera75e7d132017-09-20 08:26:30 +02001415 icmp = pkts[0]
1416 self.assertIn(ICMPv6ParamProblem, icmp)
1417 self.assert_equal(icmp[ICMPv6ParamProblem].code, 3, "ICMP code")
1418
Klement Sekera755042d2021-12-01 10:14:38 +00001419 def test_truncated_fragment(self):
1420 """ truncated fragment """
1421 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
1422 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6,
1423 nh=44, plen=2) /
1424 IPv6ExtHdrFragment(nh=6))
1425
1426 self.send_and_assert_no_replies(self.pg0, [pkt], self.pg0)
1427
Klement Sekera75e7d132017-09-20 08:26:30 +02001428 def test_invalid_frag_size(self):
1429 """ fragment size not a multiple of 8 """
Klement Sekera4c533132018-02-22 11:41:12 +01001430 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
1431 IPv6(src=self.src_if.remote_ip6,
1432 dst=self.src_if.local_ip6) /
1433 UDP(sport=1234, dport=5678) /
Klement Sekera75e7d132017-09-20 08:26:30 +02001434 Raw())
1435 self.extend_packet(p, 1000, self.padding)
1436 fragments = fragment_rfc8200(p, 1, 500)
1437 bad_fragment = fragments[0]
1438 self.extend_packet(bad_fragment, len(bad_fragment) + 5)
1439 self.pg_enable_capture()
Klement Sekera4c533132018-02-22 11:41:12 +01001440 self.src_if.add_stream([bad_fragment])
Klement Sekera75e7d132017-09-20 08:26:30 +02001441 self.pg_start()
Klement Sekera4c533132018-02-22 11:41:12 +01001442 pkts = self.src_if.get_capture(expected_count=1)
Klement Sekera75e7d132017-09-20 08:26:30 +02001443 icmp = pkts[0]
1444 self.assertIn(ICMPv6ParamProblem, icmp)
1445 self.assert_equal(icmp[ICMPv6ParamProblem].code, 0, "ICMP code")
1446
1447 def test_invalid_packet_size(self):
1448 """ total packet size > 65535 """
Klement Sekera4c533132018-02-22 11:41:12 +01001449 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
1450 IPv6(src=self.src_if.remote_ip6,
1451 dst=self.src_if.local_ip6) /
1452 UDP(sport=1234, dport=5678) /
Klement Sekera75e7d132017-09-20 08:26:30 +02001453 Raw())
1454 self.extend_packet(p, 1000, self.padding)
1455 fragments = fragment_rfc8200(p, 1, 500)
1456 bad_fragment = fragments[1]
1457 bad_fragment[IPv6ExtHdrFragment].offset = 65500
1458 self.pg_enable_capture()
Klement Sekera4c533132018-02-22 11:41:12 +01001459 self.src_if.add_stream([bad_fragment])
Klement Sekera75e7d132017-09-20 08:26:30 +02001460 self.pg_start()
Klement Sekera4c533132018-02-22 11:41:12 +01001461 pkts = self.src_if.get_capture(expected_count=1)
Klement Sekera75e7d132017-09-20 08:26:30 +02001462 icmp = pkts[0]
1463 self.assertIn(ICMPv6ParamProblem, icmp)
1464 self.assert_equal(icmp[ICMPv6ParamProblem].code, 0, "ICMP code")
1465
Ole Troan03092c12021-11-23 15:55:39 +01001466 def test_atomic_fragment(self):
1467 """ IPv6 atomic fragment """
1468 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
1469 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6,
1470 nh=44, plen=65535) /
1471 IPv6ExtHdrFragment(offset=8191, m=1, res1=0xFF, res2=0xFF,
1472 nh=255, id=0xffff)/('X'*1452))
1473
1474 rx = self.send_and_expect(self.pg0, [pkt], self.pg0)
1475 self.assertIn(ICMPv6ParamProblem, rx[0])
1476
1477 def test_truncated_fragment(self):
1478 """ IPv6 truncated fragment header """
1479 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
1480 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6,
1481 nh=44, plen=2) /
1482 IPv6ExtHdrFragment(nh=6))
1483
Klement Sekera7c3275e2021-12-07 09:49:53 +00001484 self.send_and_assert_no_replies(self.pg0, [pkt])
Ole Troan03092c12021-11-23 15:55:39 +01001485
1486 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
1487 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6) /
1488 ICMPv6EchoRequest())
1489 rx = self.send_and_expect(self.pg0, [pkt], self.pg0)
1490
Klement Sekera7c3275e2021-12-07 09:49:53 +00001491 def test_one_fragment(self):
1492 """ whole packet in one fragment processed independently """
1493 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
1494 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
1495 ICMPv6EchoRequest()/Raw('X' * 1600))
1496 frags = fragment_rfc8200(pkt, 1, 400)
1497
1498 # send a fragment with known id
1499 self.send_and_assert_no_replies(self.pg0, [frags[0]])
1500
1501 # send an atomic fragment with same id - should be reassembled
1502 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
1503 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
1504 IPv6ExtHdrFragment(id=1)/ICMPv6EchoRequest())
1505 rx = self.send_and_expect(self.pg0, [pkt], self.pg0)
1506 self.assertNotIn(IPv6ExtHdrFragment, rx)
1507
1508 # now finish the original reassembly, this should still be possible
1509 rx = self.send_and_expect(self.pg0, frags[1:], self.pg0, n_rx=1)
1510 self.assertNotIn(IPv6ExtHdrFragment, rx)
1511
1512 def test_bunch_of_fragments(self):
1513 """ valid fragments followed by rogue fragments and atomic fragment"""
1514 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
1515 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
1516 ICMPv6EchoRequest()/Raw('X' * 1600))
1517 frags = fragment_rfc8200(pkt, 1, 400)
1518 self.send_and_expect(self.pg0, frags, self.pg0, n_rx=1)
1519
1520 inc_frag = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
1521 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
1522 IPv6ExtHdrFragment(id=1, nh=58, offset=608)/Raw('X'*308))
1523
1524 self.send_and_assert_no_replies(self.pg0, inc_frag*604)
1525
1526 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
1527 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
1528 IPv6ExtHdrFragment(id=1)/ICMPv6EchoRequest())
1529 rx = self.send_and_expect(self.pg0, [pkt], self.pg0)
1530 self.assertNotIn(IPv6ExtHdrFragment, rx)
1531
Klement Sekera01c1fa42021-12-14 18:25:11 +00001532 def test_local_enable_disable(self):
1533 """ local reassembly enabled/disable """
1534 self.vapi.ip_reassembly_enable_disable(
1535 sw_if_index=self.src_if.sw_if_index, enable_ip6=False)
1536 self.vapi.ip_local_reass_enable_disable(enable_ip6=True)
1537 pkt = (Ether(src=self.src_if.local_mac, dst=self.src_if.remote_mac) /
1538 IPv6(src=self.src_if.remote_ip6, dst=self.src_if.local_ip6) /
1539 ICMPv6EchoRequest(id=1234)/Raw('X' * 1600))
1540 frags = fragment_rfc8200(pkt, 1, 400)
1541 r = self.send_and_expect(self.src_if, frags, self.src_if, n_rx=1)[0]
1542 self.assertEqual(1234, r[ICMPv6EchoReply].id)
1543 self.vapi.ip_local_reass_enable_disable()
1544
1545 self.send_and_assert_no_replies(self.src_if, frags)
1546 self.vapi.ip_local_reass_enable_disable(enable_ip6=True)
1547
Klement Sekera75e7d132017-09-20 08:26:30 +02001548
Klement Sekera630ab582019-07-19 09:14:19 +00001549class TestIPv6MWReassembly(VppTestCase):
1550 """ IPv6 Reassembly (multiple workers) """
Klement Sekera8d815022021-03-15 16:58:10 +01001551 vpp_worker_count = 3
Klement Sekera630ab582019-07-19 09:14:19 +00001552
1553 @classmethod
1554 def setUpClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +00001555 super().setUpClass()
Klement Sekera630ab582019-07-19 09:14:19 +00001556
Klement Sekera8d815022021-03-15 16:58:10 +01001557 cls.create_pg_interfaces(range(cls.vpp_worker_count+1))
Klement Sekera630ab582019-07-19 09:14:19 +00001558 cls.src_if = cls.pg0
1559 cls.send_ifs = cls.pg_interfaces[:-1]
1560 cls.dst_if = cls.pg_interfaces[-1]
1561
1562 # setup all interfaces
1563 for i in cls.pg_interfaces:
1564 i.admin_up()
1565 i.config_ip6()
1566 i.resolve_ndp()
1567
1568 # packets sizes reduced here because we are generating packets without
1569 # Ethernet headers, which are added later (diff fragments go via
1570 # different interfaces)
1571 cls.packet_sizes = [64-len(Ether()), 512-len(Ether()),
1572 1518-len(Ether()), 9018-len(Ether())]
1573 cls.padding = " abcdefghijklmn"
1574 cls.create_stream(cls.packet_sizes)
1575 cls.create_fragments()
1576
1577 @classmethod
1578 def tearDownClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +00001579 super().tearDownClass()
Klement Sekera630ab582019-07-19 09:14:19 +00001580
1581 def setUp(self):
1582 """ Test setup - force timeout on existing reassemblies """
Klement Sekera01c1fa42021-12-14 18:25:11 +00001583 super().setUp()
Klement Sekera630ab582019-07-19 09:14:19 +00001584 for intf in self.send_ifs:
1585 self.vapi.ip_reassembly_enable_disable(
1586 sw_if_index=intf.sw_if_index, enable_ip6=True)
1587 self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000,
1588 max_reassembly_length=1000,
1589 expire_walk_interval_ms=10, is_ip6=1)
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +02001590 self.virtual_sleep(.25)
Klement Sekera630ab582019-07-19 09:14:19 +00001591 self.vapi.ip_reassembly_set(timeout_ms=1000000, max_reassemblies=1000,
1592 max_reassembly_length=1000,
1593 expire_walk_interval_ms=1000, is_ip6=1)
1594
1595 def tearDown(self):
Klement Sekera01c1fa42021-12-14 18:25:11 +00001596 for intf in self.send_ifs:
1597 self.vapi.ip_reassembly_enable_disable(
1598 sw_if_index=intf.sw_if_index, enable_ip6=False)
1599 super().tearDown()
Klement Sekera630ab582019-07-19 09:14:19 +00001600
1601 def show_commands_at_teardown(self):
Klement Sekera896c8962019-06-24 11:52:49 +00001602 self.logger.debug(self.vapi.ppcli("show ip6-full-reassembly details"))
Klement Sekera630ab582019-07-19 09:14:19 +00001603 self.logger.debug(self.vapi.ppcli("show buffers"))
1604
1605 @classmethod
1606 def create_stream(cls, packet_sizes, packet_count=test_packet_count):
1607 """Create input packet stream
1608
1609 :param list packet_sizes: Required packet sizes.
1610 """
1611 for i in range(0, packet_count):
1612 info = cls.create_packet_info(cls.src_if, cls.src_if)
1613 payload = cls.info_to_payload(info)
1614 p = (IPv6(src=cls.src_if.remote_ip6,
1615 dst=cls.dst_if.remote_ip6) /
1616 UDP(sport=1234, dport=5678) /
1617 Raw(payload))
1618 size = packet_sizes[(i // 2) % len(packet_sizes)]
1619 cls.extend_packet(p, size, cls.padding)
1620 info.data = p
1621
1622 @classmethod
1623 def create_fragments(cls):
1624 infos = cls._packet_infos
1625 cls.pkt_infos = []
Paul Vinciguerra090096b2020-12-03 00:42:46 -05001626 for index, info in infos.items():
Klement Sekera630ab582019-07-19 09:14:19 +00001627 p = info.data
1628 # cls.logger.debug(ppp("Packet:",
1629 # p.__class__(scapy.compat.raw(p))))
1630 fragments_400 = fragment_rfc8200(p, index, 400)
1631 cls.pkt_infos.append((index, fragments_400))
1632 cls.fragments_400 = [
1633 x for (_, frags) in cls.pkt_infos for x in frags]
1634 cls.logger.debug("Fragmented %s packets into %s 400-byte fragments, " %
1635 (len(infos), len(cls.fragments_400)))
1636
1637 def verify_capture(self, capture, dropped_packet_indexes=[]):
1638 """Verify captured packet strea .
1639
1640 :param list capture: Captured packet stream.
1641 """
1642 info = None
1643 seen = set()
1644 for packet in capture:
1645 try:
1646 self.logger.debug(ppp("Got packet:", packet))
1647 ip = packet[IPv6]
1648 udp = packet[UDP]
1649 payload_info = self.payload_to_info(packet[Raw])
1650 packet_index = payload_info.index
1651 self.assertTrue(
1652 packet_index not in dropped_packet_indexes,
1653 ppp("Packet received, but should be dropped:", packet))
1654 if packet_index in seen:
1655 raise Exception(ppp("Duplicate packet received", packet))
1656 seen.add(packet_index)
1657 self.assertEqual(payload_info.dst, self.src_if.sw_if_index)
1658 info = self._packet_infos[packet_index]
1659 self.assertTrue(info is not None)
1660 self.assertEqual(packet_index, info.index)
1661 saved_packet = info.data
1662 self.assertEqual(ip.src, saved_packet[IPv6].src)
1663 self.assertEqual(ip.dst, saved_packet[IPv6].dst)
1664 self.assertEqual(udp.payload, saved_packet[UDP].payload)
1665 except Exception:
1666 self.logger.error(ppp("Unexpected or invalid packet:", packet))
1667 raise
1668 for index in self._packet_infos:
1669 self.assertTrue(index in seen or index in dropped_packet_indexes,
1670 "Packet with packet_index %d not received" % index)
1671
1672 def send_packets(self, packets):
Klement Sekera8d815022021-03-15 16:58:10 +01001673 for counter in range(self.vpp_worker_count):
Klement Sekera630ab582019-07-19 09:14:19 +00001674 if 0 == len(packets[counter]):
1675 continue
1676 send_if = self.send_ifs[counter]
1677 send_if.add_stream(
1678 (Ether(dst=send_if.local_mac, src=send_if.remote_mac) / x
1679 for x in packets[counter]),
1680 worker=counter)
1681 self.pg_start()
1682
1683 def test_worker_conflict(self):
1684 """ 1st and FO=0 fragments on different workers """
1685
1686 # in first wave we send fragments which don't start at offset 0
1687 # then we send fragments with offset 0 on a different thread
1688 # then the rest of packets on a random thread
Klement Sekera8d815022021-03-15 16:58:10 +01001689 first_packets = [[] for n in range(self.vpp_worker_count)]
1690 second_packets = [[] for n in range(self.vpp_worker_count)]
1691 rest_of_packets = [[] for n in range(self.vpp_worker_count)]
Klement Sekera630ab582019-07-19 09:14:19 +00001692 for (_, p) in self.pkt_infos:
Klement Sekera8d815022021-03-15 16:58:10 +01001693 wi = randrange(self.vpp_worker_count)
Klement Sekera630ab582019-07-19 09:14:19 +00001694 second_packets[wi].append(p[0])
1695 if len(p) <= 1:
1696 continue
1697 wi2 = wi
1698 while wi2 == wi:
Klement Sekera8d815022021-03-15 16:58:10 +01001699 wi2 = randrange(self.vpp_worker_count)
Klement Sekera630ab582019-07-19 09:14:19 +00001700 first_packets[wi2].append(p[1])
Klement Sekera8d815022021-03-15 16:58:10 +01001701 wi3 = randrange(self.vpp_worker_count)
Klement Sekera630ab582019-07-19 09:14:19 +00001702 rest_of_packets[wi3].extend(p[2:])
1703
1704 self.pg_enable_capture()
1705 self.send_packets(first_packets)
1706 self.send_packets(second_packets)
1707 self.send_packets(rest_of_packets)
1708
1709 packets = self.dst_if.get_capture(len(self.pkt_infos))
1710 self.verify_capture(packets)
1711 for send_if in self.send_ifs:
1712 send_if.assert_nothing_captured()
1713
Klement Sekera68bae5b2019-10-10 18:57:34 +00001714 self.logger.debug(self.vapi.ppcli("show trace"))
1715 self.logger.debug(self.vapi.ppcli("show ip6-full-reassembly details"))
1716 self.logger.debug(self.vapi.ppcli("show buffers"))
1717 self.vapi.cli("clear trace")
1718
Klement Sekera630ab582019-07-19 09:14:19 +00001719 self.pg_enable_capture()
1720 self.send_packets(first_packets)
1721 self.send_packets(second_packets)
1722 self.send_packets(rest_of_packets)
1723
1724 packets = self.dst_if.get_capture(len(self.pkt_infos))
1725 self.verify_capture(packets)
1726 for send_if in self.send_ifs:
1727 send_if.assert_nothing_captured()
1728
1729
Klement Sekerade34c352019-06-25 11:19:22 +00001730class TestIPv6SVReassembly(VppTestCase):
1731 """ IPv6 Shallow Virtual Reassembly """
1732
1733 @classmethod
1734 def setUpClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +00001735 super().setUpClass()
Klement Sekerade34c352019-06-25 11:19:22 +00001736
1737 cls.create_pg_interfaces([0, 1])
1738 cls.src_if = cls.pg0
1739 cls.dst_if = cls.pg1
1740
1741 # setup all interfaces
1742 for i in cls.pg_interfaces:
1743 i.admin_up()
1744 i.config_ip6()
1745 i.resolve_ndp()
1746
1747 def setUp(self):
1748 """ Test setup - force timeout on existing reassemblies """
Klement Sekera01c1fa42021-12-14 18:25:11 +00001749 super().setUp()
Klement Sekerade34c352019-06-25 11:19:22 +00001750 self.vapi.ip_reassembly_enable_disable(
1751 sw_if_index=self.src_if.sw_if_index, enable_ip6=True,
1752 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL)
1753 self.vapi.ip_reassembly_set(
1754 timeout_ms=0, max_reassemblies=1000,
1755 max_reassembly_length=1000,
1756 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
1757 expire_walk_interval_ms=10, is_ip6=1)
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +02001758 self.virtual_sleep(.25)
Klement Sekerade34c352019-06-25 11:19:22 +00001759 self.vapi.ip_reassembly_set(
1760 timeout_ms=1000000, max_reassemblies=1000,
1761 max_reassembly_length=1000,
1762 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
1763 expire_walk_interval_ms=10000, is_ip6=1)
1764
1765 def tearDown(self):
Klement Sekera01c1fa42021-12-14 18:25:11 +00001766 super().tearDown()
Klement Sekerade34c352019-06-25 11:19:22 +00001767 self.logger.debug(self.vapi.ppcli("show ip6-sv-reassembly details"))
1768 self.logger.debug(self.vapi.ppcli("show buffers"))
1769
1770 def test_basic(self):
1771 """ basic reassembly """
1772 payload_len = 1000
1773 payload = ""
1774 counter = 0
1775 while len(payload) < payload_len:
1776 payload += "%u " % counter
1777 counter += 1
1778
1779 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
1780 IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6) /
1781 UDP(sport=1234, dport=5678) /
1782 Raw(payload))
1783 fragments = fragment_rfc8200(p, 1, payload_len/4)
1784
1785 # send fragment #2 - should be cached inside reassembly
1786 self.pg_enable_capture()
1787 self.src_if.add_stream(fragments[1])
1788 self.pg_start()
1789 self.logger.debug(self.vapi.ppcli("show ip6-sv-reassembly details"))
1790 self.logger.debug(self.vapi.ppcli("show buffers"))
1791 self.logger.debug(self.vapi.ppcli("show trace"))
1792 self.dst_if.assert_nothing_captured()
1793
1794 # send fragment #1 - reassembly is finished now and both fragments
1795 # forwarded
1796 self.pg_enable_capture()
1797 self.src_if.add_stream(fragments[0])
1798 self.pg_start()
1799 self.logger.debug(self.vapi.ppcli("show ip6-sv-reassembly details"))
1800 self.logger.debug(self.vapi.ppcli("show buffers"))
1801 self.logger.debug(self.vapi.ppcli("show trace"))
1802 c = self.dst_if.get_capture(2)
1803 for sent, recvd in zip([fragments[1], fragments[0]], c):
1804 self.assertEqual(sent[IPv6].src, recvd[IPv6].src)
1805 self.assertEqual(sent[IPv6].dst, recvd[IPv6].dst)
1806 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
1807
1808 # send rest of fragments - should be immediately forwarded
1809 self.pg_enable_capture()
1810 self.src_if.add_stream(fragments[2:])
1811 self.pg_start()
1812 c = self.dst_if.get_capture(len(fragments[2:]))
1813 for sent, recvd in zip(fragments[2:], c):
1814 self.assertEqual(sent[IPv6].src, recvd[IPv6].src)
1815 self.assertEqual(sent[IPv6].dst, recvd[IPv6].dst)
1816 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
1817
Klement Sekera53be16d2020-12-15 21:47:36 +01001818 def test_verify_clear_trace_mid_reassembly(self):
1819 """ verify clear trace works mid-reassembly """
1820 payload_len = 1000
1821 payload = ""
1822 counter = 0
1823 while len(payload) < payload_len:
1824 payload += "%u " % counter
1825 counter += 1
1826
1827 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
1828 IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6) /
1829 UDP(sport=1234, dport=5678) /
1830 Raw(payload))
1831 fragments = fragment_rfc8200(p, 1, payload_len/4)
1832
1833 self.pg_enable_capture()
1834 self.src_if.add_stream(fragments[1])
1835 self.pg_start()
1836
1837 self.logger.debug(self.vapi.cli("show trace"))
1838 self.vapi.cli("clear trace")
1839
1840 self.pg_enable_capture()
1841 self.src_if.add_stream(fragments[0])
1842 self.pg_start()
1843 self.dst_if.get_capture(2)
1844
1845 self.logger.debug(self.vapi.cli("show trace"))
1846 self.vapi.cli("clear trace")
1847
1848 self.pg_enable_capture()
1849 self.src_if.add_stream(fragments[2:])
1850 self.pg_start()
1851 self.dst_if.get_capture(len(fragments[2:]))
1852
Klement Sekerade34c352019-06-25 11:19:22 +00001853 def test_timeout(self):
1854 """ reassembly timeout """
1855 payload_len = 1000
1856 payload = ""
1857 counter = 0
1858 while len(payload) < payload_len:
1859 payload += "%u " % counter
1860 counter += 1
1861
1862 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
1863 IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6) /
1864 UDP(sport=1234, dport=5678) /
1865 Raw(payload))
1866 fragments = fragment_rfc8200(p, 1, payload_len/4)
1867
1868 self.vapi.ip_reassembly_set(
1869 timeout_ms=100, max_reassemblies=1000,
1870 max_reassembly_length=1000,
1871 expire_walk_interval_ms=50,
1872 is_ip6=1,
1873 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL)
1874
1875 # send fragments #2 and #1 - should be forwarded
1876 self.pg_enable_capture()
1877 self.src_if.add_stream(fragments[0:2])
1878 self.pg_start()
1879 self.logger.debug(self.vapi.ppcli("show ip4-sv-reassembly details"))
1880 self.logger.debug(self.vapi.ppcli("show buffers"))
1881 self.logger.debug(self.vapi.ppcli("show trace"))
1882 c = self.dst_if.get_capture(2)
1883 for sent, recvd in zip([fragments[1], fragments[0]], c):
1884 self.assertEqual(sent[IPv6].src, recvd[IPv6].src)
1885 self.assertEqual(sent[IPv6].dst, recvd[IPv6].dst)
1886 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
1887
1888 # wait for cleanup
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +02001889 self.virtual_sleep(.25, "wait before sending rest of fragments")
Klement Sekerade34c352019-06-25 11:19:22 +00001890
1891 # send rest of fragments - shouldn't be forwarded
1892 self.pg_enable_capture()
1893 self.src_if.add_stream(fragments[2:])
1894 self.pg_start()
1895 self.dst_if.assert_nothing_captured()
1896
1897 def test_lru(self):
1898 """ reassembly reuses LRU element """
1899
1900 self.vapi.ip_reassembly_set(
1901 timeout_ms=1000000, max_reassemblies=1,
1902 max_reassembly_length=1000,
1903 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
1904 is_ip6=1, expire_walk_interval_ms=10000)
1905
1906 payload_len = 1000
1907 payload = ""
1908 counter = 0
1909 while len(payload) < payload_len:
1910 payload += "%u " % counter
1911 counter += 1
1912
1913 packet_count = 10
1914
1915 fragments = [f
1916 for i in range(packet_count)
1917 for p in (Ether(dst=self.src_if.local_mac,
1918 src=self.src_if.remote_mac) /
1919 IPv6(src=self.src_if.remote_ip6,
1920 dst=self.dst_if.remote_ip6) /
1921 UDP(sport=1234, dport=5678) /
1922 Raw(payload))
1923 for f in fragment_rfc8200(p, i, payload_len/4)]
1924
1925 self.pg_enable_capture()
1926 self.src_if.add_stream(fragments)
1927 self.pg_start()
1928 c = self.dst_if.get_capture(len(fragments))
1929 for sent, recvd in zip(fragments, c):
1930 self.assertEqual(sent[IPv6].src, recvd[IPv6].src)
1931 self.assertEqual(sent[IPv6].dst, recvd[IPv6].dst)
1932 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
1933
Klement Sekera7c3275e2021-12-07 09:49:53 +00001934 def test_one_fragment(self):
1935 """ whole packet in one fragment processed independently """
1936 pkt = (Ether(src=self.src_if.local_mac, dst=self.src_if.remote_mac) /
1937 IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6) /
1938 ICMPv6EchoRequest()/Raw('X' * 1600))
1939 frags = fragment_rfc8200(pkt, 1, 400)
1940
1941 # send a fragment with known id
1942 self.send_and_expect(self.src_if, [frags[0]], self.dst_if)
1943
1944 # send an atomic fragment with same id - should be reassembled
1945 pkt = (Ether(src=self.src_if.local_mac, dst=self.src_if.remote_mac) /
1946 IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6) /
1947 IPv6ExtHdrFragment(id=1)/ICMPv6EchoRequest())
1948 rx = self.send_and_expect(self.src_if, [pkt], self.dst_if)
1949
1950 # now forward packets matching original reassembly, should still work
1951 rx = self.send_and_expect(self.src_if, frags[1:], self.dst_if)
1952
1953 def test_bunch_of_fragments(self):
1954 """ valid fragments followed by rogue fragments and atomic fragment"""
1955 pkt = (Ether(src=self.src_if.local_mac, dst=self.src_if.remote_mac) /
1956 IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6) /
1957 ICMPv6EchoRequest()/Raw('X' * 1600))
1958 frags = fragment_rfc8200(pkt, 1, 400)
1959 rx = self.send_and_expect(self.src_if, frags, self.dst_if)
1960
1961 rogue = (Ether(src=self.src_if.local_mac, dst=self.src_if.remote_mac) /
1962 IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6) /
1963 IPv6ExtHdrFragment(id=1, nh=58, offset=608)/Raw('X'*308))
1964
1965 self.send_and_expect(self.src_if, rogue*604, self.dst_if)
1966
1967 pkt = (Ether(src=self.src_if.local_mac, dst=self.src_if.remote_mac) /
1968 IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6) /
1969 IPv6ExtHdrFragment(id=1)/ICMPv6EchoRequest())
1970 rx = self.send_and_expect(self.src_if, [pkt], self.dst_if)
1971
Klement Sekera755042d2021-12-01 10:14:38 +00001972 def test_truncated_fragment(self):
1973 """ truncated fragment """
1974 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
1975 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6,
1976 nh=44, plen=2) /
1977 IPv6ExtHdrFragment(nh=6))
1978
1979 self.send_and_assert_no_replies(self.pg0, [pkt], self.pg0)
1980
Klement Sekerade34c352019-06-25 11:19:22 +00001981
Juraj Sloboda3048b632018-10-02 11:13:53 +02001982class TestIPv4ReassemblyLocalNode(VppTestCase):
1983 """ IPv4 Reassembly for packets coming to ip4-local node """
1984
1985 @classmethod
1986 def setUpClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +00001987 super().setUpClass()
Juraj Sloboda3048b632018-10-02 11:13:53 +02001988
1989 cls.create_pg_interfaces([0])
1990 cls.src_dst_if = cls.pg0
1991
1992 # setup all interfaces
1993 for i in cls.pg_interfaces:
1994 i.admin_up()
1995 i.config_ip4()
1996 i.resolve_arp()
1997
1998 cls.padding = " abcdefghijklmn"
1999 cls.create_stream()
2000 cls.create_fragments()
2001
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002002 @classmethod
2003 def tearDownClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +00002004 super().tearDownClass()
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002005
Juraj Sloboda3048b632018-10-02 11:13:53 +02002006 def setUp(self):
2007 """ Test setup - force timeout on existing reassemblies """
Klement Sekera01c1fa42021-12-14 18:25:11 +00002008 super().setUp()
Juraj Sloboda3048b632018-10-02 11:13:53 +02002009 self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000,
Klement Sekera3a343d42019-05-16 14:35:46 +02002010 max_reassembly_length=1000,
Juraj Sloboda3048b632018-10-02 11:13:53 +02002011 expire_walk_interval_ms=10)
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +02002012 self.virtual_sleep(.25)
Juraj Sloboda3048b632018-10-02 11:13:53 +02002013 self.vapi.ip_reassembly_set(timeout_ms=1000000, max_reassemblies=1000,
Klement Sekera3a343d42019-05-16 14:35:46 +02002014 max_reassembly_length=1000,
Juraj Sloboda3048b632018-10-02 11:13:53 +02002015 expire_walk_interval_ms=10000)
2016
2017 def tearDown(self):
Klement Sekera01c1fa42021-12-14 18:25:11 +00002018 super().tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -07002019
2020 def show_commands_at_teardown(self):
Klement Sekera896c8962019-06-24 11:52:49 +00002021 self.logger.debug(self.vapi.ppcli("show ip4-full-reassembly details"))
Klement Sekeracae98b72019-02-19 13:53:43 +01002022 self.logger.debug(self.vapi.ppcli("show buffers"))
Juraj Sloboda3048b632018-10-02 11:13:53 +02002023
2024 @classmethod
2025 def create_stream(cls, packet_count=test_packet_count):
2026 """Create input packet stream for defined interface.
2027
2028 :param list packet_sizes: Required packet sizes.
2029 """
2030 for i in range(0, packet_count):
2031 info = cls.create_packet_info(cls.src_dst_if, cls.src_dst_if)
2032 payload = cls.info_to_payload(info)
2033 p = (Ether(dst=cls.src_dst_if.local_mac,
2034 src=cls.src_dst_if.remote_mac) /
2035 IP(id=info.index, src=cls.src_dst_if.remote_ip4,
2036 dst=cls.src_dst_if.local_ip4) /
2037 ICMP(type='echo-request', id=1234) /
2038 Raw(payload))
2039 cls.extend_packet(p, 1518, cls.padding)
2040 info.data = p
2041
2042 @classmethod
2043 def create_fragments(cls):
2044 infos = cls._packet_infos
2045 cls.pkt_infos = []
Paul Vinciguerra090096b2020-12-03 00:42:46 -05002046 for index, info in infos.items():
Juraj Sloboda3048b632018-10-02 11:13:53 +02002047 p = info.data
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -07002048 # cls.logger.debug(ppp("Packet:",
2049 # p.__class__(scapy.compat.raw(p))))
Juraj Sloboda3048b632018-10-02 11:13:53 +02002050 fragments_300 = fragment_rfc791(p, 300)
2051 cls.pkt_infos.append((index, fragments_300))
2052 cls.fragments_300 = [x for (_, frags) in cls.pkt_infos for x in frags]
2053 cls.logger.debug("Fragmented %s packets into %s 300-byte fragments" %
2054 (len(infos), len(cls.fragments_300)))
2055
2056 def verify_capture(self, capture):
2057 """Verify captured packet stream.
2058
2059 :param list capture: Captured packet stream.
2060 """
2061 info = None
2062 seen = set()
2063 for packet in capture:
2064 try:
2065 self.logger.debug(ppp("Got packet:", packet))
2066 ip = packet[IP]
2067 icmp = packet[ICMP]
Paul Vinciguerraeaea4212019-03-06 11:58:06 -08002068 payload_info = self.payload_to_info(packet[Raw])
Juraj Sloboda3048b632018-10-02 11:13:53 +02002069 packet_index = payload_info.index
2070 if packet_index in seen:
2071 raise Exception(ppp("Duplicate packet received", packet))
2072 seen.add(packet_index)
2073 self.assertEqual(payload_info.dst, self.src_dst_if.sw_if_index)
2074 info = self._packet_infos[packet_index]
Klement Sekera14d7e902018-12-10 13:46:09 +01002075 self.assertIsNotNone(info)
Juraj Sloboda3048b632018-10-02 11:13:53 +02002076 self.assertEqual(packet_index, info.index)
2077 saved_packet = info.data
2078 self.assertEqual(ip.src, saved_packet[IP].dst)
2079 self.assertEqual(ip.dst, saved_packet[IP].src)
2080 self.assertEqual(icmp.type, 0) # echo reply
2081 self.assertEqual(icmp.id, saved_packet[ICMP].id)
2082 self.assertEqual(icmp.payload, saved_packet[ICMP].payload)
2083 except Exception:
2084 self.logger.error(ppp("Unexpected or invalid packet:", packet))
2085 raise
2086 for index in self._packet_infos:
Klement Sekera14d7e902018-12-10 13:46:09 +01002087 self.assertIn(index, seen,
2088 "Packet with packet_index %d not received" % index)
Juraj Sloboda3048b632018-10-02 11:13:53 +02002089
2090 def test_reassembly(self):
2091 """ basic reassembly """
2092
2093 self.pg_enable_capture()
2094 self.src_dst_if.add_stream(self.fragments_300)
2095 self.pg_start()
2096
2097 packets = self.src_dst_if.get_capture(len(self.pkt_infos))
2098 self.verify_capture(packets)
2099
2100 # run it all again to verify correctness
2101 self.pg_enable_capture()
2102 self.src_dst_if.add_stream(self.fragments_300)
2103 self.pg_start()
2104
2105 packets = self.src_dst_if.get_capture(len(self.pkt_infos))
2106 self.verify_capture(packets)
2107
2108
Klement Sekera75e7d132017-09-20 08:26:30 +02002109class TestFIFReassembly(VppTestCase):
2110 """ Fragments in fragments reassembly """
2111
2112 @classmethod
2113 def setUpClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +00002114 super().setUpClass()
Klement Sekera75e7d132017-09-20 08:26:30 +02002115
Klement Sekera4c533132018-02-22 11:41:12 +01002116 cls.create_pg_interfaces([0, 1])
2117 cls.src_if = cls.pg0
2118 cls.dst_if = cls.pg1
2119 for i in cls.pg_interfaces:
2120 i.admin_up()
2121 i.config_ip4()
2122 i.resolve_arp()
2123 i.config_ip6()
2124 i.resolve_ndp()
Klement Sekera75e7d132017-09-20 08:26:30 +02002125
Klement Sekera75e7d132017-09-20 08:26:30 +02002126 cls.packet_sizes = [64, 512, 1518, 9018]
2127 cls.padding = " abcdefghijklmn"
2128
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002129 @classmethod
2130 def tearDownClass(cls):
Klement Sekera01c1fa42021-12-14 18:25:11 +00002131 super().tearDownClass()
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -07002132
Klement Sekera75e7d132017-09-20 08:26:30 +02002133 def setUp(self):
2134 """ Test setup - force timeout on existing reassemblies """
Klement Sekera01c1fa42021-12-14 18:25:11 +00002135 super().setUp()
Klement Sekera4c533132018-02-22 11:41:12 +01002136 self.vapi.ip_reassembly_enable_disable(
2137 sw_if_index=self.src_if.sw_if_index, enable_ip4=True,
2138 enable_ip6=True)
2139 self.vapi.ip_reassembly_enable_disable(
2140 sw_if_index=self.dst_if.sw_if_index, enable_ip4=True,
2141 enable_ip6=True)
Klement Sekera75e7d132017-09-20 08:26:30 +02002142 self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000,
Klement Sekera3a343d42019-05-16 14:35:46 +02002143 max_reassembly_length=1000,
Klement Sekera75e7d132017-09-20 08:26:30 +02002144 expire_walk_interval_ms=10)
2145 self.vapi.ip_reassembly_set(timeout_ms=0, max_reassemblies=1000,
Klement Sekera3a343d42019-05-16 14:35:46 +02002146 max_reassembly_length=1000,
Klement Sekera75e7d132017-09-20 08:26:30 +02002147 expire_walk_interval_ms=10, is_ip6=1)
BenoƮt Ganne56eccdb2021-08-20 09:18:31 +02002148 self.virtual_sleep(.25)
Klement Sekera75e7d132017-09-20 08:26:30 +02002149 self.vapi.ip_reassembly_set(timeout_ms=1000000, max_reassemblies=1000,
Klement Sekera3a343d42019-05-16 14:35:46 +02002150 max_reassembly_length=1000,
Klement Sekera75e7d132017-09-20 08:26:30 +02002151 expire_walk_interval_ms=10000)
2152 self.vapi.ip_reassembly_set(timeout_ms=1000000, max_reassemblies=1000,
Klement Sekera3a343d42019-05-16 14:35:46 +02002153 max_reassembly_length=1000,
Klement Sekera75e7d132017-09-20 08:26:30 +02002154 expire_walk_interval_ms=10000, is_ip6=1)
2155
2156 def tearDown(self):
Klement Sekera01c1fa42021-12-14 18:25:11 +00002157 super().tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -07002158
2159 def show_commands_at_teardown(self):
Klement Sekera896c8962019-06-24 11:52:49 +00002160 self.logger.debug(self.vapi.ppcli("show ip4-full-reassembly details"))
2161 self.logger.debug(self.vapi.ppcli("show ip6-full-reassembly details"))
Klement Sekeracae98b72019-02-19 13:53:43 +01002162 self.logger.debug(self.vapi.ppcli("show buffers"))
Klement Sekera75e7d132017-09-20 08:26:30 +02002163
2164 def verify_capture(self, capture, ip_class, dropped_packet_indexes=[]):
2165 """Verify captured packet stream.
2166
2167 :param list capture: Captured packet stream.
2168 """
2169 info = None
2170 seen = set()
2171 for packet in capture:
2172 try:
Klement Sekera4c533132018-02-22 11:41:12 +01002173 self.logger.debug(ppp("Got packet:", packet))
Klement Sekera75e7d132017-09-20 08:26:30 +02002174 ip = packet[ip_class]
2175 udp = packet[UDP]
Paul Vinciguerraeaea4212019-03-06 11:58:06 -08002176 payload_info = self.payload_to_info(packet[Raw])
Klement Sekera75e7d132017-09-20 08:26:30 +02002177 packet_index = payload_info.index
2178 self.assertTrue(
2179 packet_index not in dropped_packet_indexes,
2180 ppp("Packet received, but should be dropped:", packet))
2181 if packet_index in seen:
2182 raise Exception(ppp("Duplicate packet received", packet))
2183 seen.add(packet_index)
Klement Sekera4c533132018-02-22 11:41:12 +01002184 self.assertEqual(payload_info.dst, self.dst_if.sw_if_index)
Klement Sekera75e7d132017-09-20 08:26:30 +02002185 info = self._packet_infos[packet_index]
2186 self.assertTrue(info is not None)
2187 self.assertEqual(packet_index, info.index)
2188 saved_packet = info.data
2189 self.assertEqual(ip.src, saved_packet[ip_class].src)
2190 self.assertEqual(ip.dst, saved_packet[ip_class].dst)
2191 self.assertEqual(udp.payload, saved_packet[UDP].payload)
Klement Sekera4c533132018-02-22 11:41:12 +01002192 except Exception:
Klement Sekera75e7d132017-09-20 08:26:30 +02002193 self.logger.error(ppp("Unexpected or invalid packet:", packet))
2194 raise
2195 for index in self._packet_infos:
2196 self.assertTrue(index in seen or index in dropped_packet_indexes,
2197 "Packet with packet_index %d not received" % index)
2198
2199 def test_fif4(self):
2200 """ Fragments in fragments (4o4) """
2201
2202 # TODO this should be ideally in setUpClass, but then we hit a bug
2203 # with VppIpRoute incorrectly reporting it's present when it's not
2204 # so we need to manually remove the vpp config, thus we cannot have
2205 # it shared for multiple test cases
2206 self.tun_ip4 = "1.1.1.2"
2207
Klement Sekera4c533132018-02-22 11:41:12 +01002208 self.gre4 = VppGreInterface(self, self.src_if.local_ip4, self.tun_ip4)
Klement Sekera75e7d132017-09-20 08:26:30 +02002209 self.gre4.add_vpp_config()
2210 self.gre4.admin_up()
2211 self.gre4.config_ip4()
2212
Klement Sekera4c533132018-02-22 11:41:12 +01002213 self.vapi.ip_reassembly_enable_disable(
2214 sw_if_index=self.gre4.sw_if_index, enable_ip4=True)
2215
Klement Sekera75e7d132017-09-20 08:26:30 +02002216 self.route4 = VppIpRoute(self, self.tun_ip4, 32,
Klement Sekera4c533132018-02-22 11:41:12 +01002217 [VppRoutePath(self.src_if.remote_ip4,
2218 self.src_if.sw_if_index)])
Klement Sekera75e7d132017-09-20 08:26:30 +02002219 self.route4.add_vpp_config()
2220
2221 self.reset_packet_infos()
2222 for i in range(test_packet_count):
Klement Sekera4c533132018-02-22 11:41:12 +01002223 info = self.create_packet_info(self.src_if, self.dst_if)
Klement Sekera75e7d132017-09-20 08:26:30 +02002224 payload = self.info_to_payload(info)
Klement Sekera4c533132018-02-22 11:41:12 +01002225 # Ethernet header here is only for size calculation, thus it
2226 # doesn't matter how it's initialized. This is to ensure that
2227 # reassembled packet is not > 9000 bytes, so that it's not dropped
2228 p = (Ether() /
2229 IP(id=i, src=self.src_if.remote_ip4,
2230 dst=self.dst_if.remote_ip4) /
2231 UDP(sport=1234, dport=5678) /
Klement Sekera75e7d132017-09-20 08:26:30 +02002232 Raw(payload))
2233 size = self.packet_sizes[(i // 2) % len(self.packet_sizes)]
2234 self.extend_packet(p, size, self.padding)
Klement Sekera4c533132018-02-22 11:41:12 +01002235 info.data = p[IP] # use only IP part, without ethernet header
Klement Sekera75e7d132017-09-20 08:26:30 +02002236
Paul Vinciguerra090096b2020-12-03 00:42:46 -05002237 fragments = [x for _, p in self._packet_infos.items()
Klement Sekera75e7d132017-09-20 08:26:30 +02002238 for x in fragment_rfc791(p.data, 400)]
2239
2240 encapped_fragments = \
Klement Sekera4c533132018-02-22 11:41:12 +01002241 [Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
2242 IP(src=self.tun_ip4, dst=self.src_if.local_ip4) /
Klement Sekera75e7d132017-09-20 08:26:30 +02002243 GRE() /
2244 p
2245 for p in fragments]
2246
2247 fragmented_encapped_fragments = \
2248 [x for p in encapped_fragments
2249 for x in fragment_rfc791(p, 200)]
2250
Klement Sekera4c533132018-02-22 11:41:12 +01002251 self.src_if.add_stream(fragmented_encapped_fragments)
Klement Sekera75e7d132017-09-20 08:26:30 +02002252
2253 self.pg_enable_capture(self.pg_interfaces)
2254 self.pg_start()
2255
Klement Sekera4c533132018-02-22 11:41:12 +01002256 self.src_if.assert_nothing_captured()
2257 packets = self.dst_if.get_capture(len(self._packet_infos))
Klement Sekera75e7d132017-09-20 08:26:30 +02002258 self.verify_capture(packets, IP)
2259
2260 # TODO remove gre vpp config by hand until VppIpRoute gets fixed
2261 # so that it's query_vpp_config() works as it should
2262 self.gre4.remove_vpp_config()
Klement Sekera4c533132018-02-22 11:41:12 +01002263 self.logger.debug(self.vapi.ppcli("show interface"))
Klement Sekera75e7d132017-09-20 08:26:30 +02002264
2265 def test_fif6(self):
2266 """ Fragments in fragments (6o6) """
2267 # TODO this should be ideally in setUpClass, but then we hit a bug
2268 # with VppIpRoute incorrectly reporting it's present when it's not
2269 # so we need to manually remove the vpp config, thus we cannot have
2270 # it shared for multiple test cases
2271 self.tun_ip6 = "1002::1"
2272
Neale Ranns5a8844b2019-04-16 07:15:35 +00002273 self.gre6 = VppGreInterface(self, self.src_if.local_ip6, self.tun_ip6)
Klement Sekera75e7d132017-09-20 08:26:30 +02002274 self.gre6.add_vpp_config()
2275 self.gre6.admin_up()
2276 self.gre6.config_ip6()
2277
Klement Sekera4c533132018-02-22 11:41:12 +01002278 self.vapi.ip_reassembly_enable_disable(
2279 sw_if_index=self.gre6.sw_if_index, enable_ip6=True)
2280
Klement Sekera75e7d132017-09-20 08:26:30 +02002281 self.route6 = VppIpRoute(self, self.tun_ip6, 128,
Neale Ranns097fa662018-05-01 05:17:55 -07002282 [VppRoutePath(
2283 self.src_if.remote_ip6,
2284 self.src_if.sw_if_index)])
Klement Sekera75e7d132017-09-20 08:26:30 +02002285 self.route6.add_vpp_config()
2286
2287 self.reset_packet_infos()
2288 for i in range(test_packet_count):
Klement Sekera4c533132018-02-22 11:41:12 +01002289 info = self.create_packet_info(self.src_if, self.dst_if)
Klement Sekera75e7d132017-09-20 08:26:30 +02002290 payload = self.info_to_payload(info)
Klement Sekera4c533132018-02-22 11:41:12 +01002291 # Ethernet header here is only for size calculation, thus it
2292 # doesn't matter how it's initialized. This is to ensure that
2293 # reassembled packet is not > 9000 bytes, so that it's not dropped
2294 p = (Ether() /
2295 IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6) /
2296 UDP(sport=1234, dport=5678) /
Klement Sekera75e7d132017-09-20 08:26:30 +02002297 Raw(payload))
2298 size = self.packet_sizes[(i // 2) % len(self.packet_sizes)]
2299 self.extend_packet(p, size, self.padding)
Klement Sekera4c533132018-02-22 11:41:12 +01002300 info.data = p[IPv6] # use only IPv6 part, without ethernet header
Klement Sekera75e7d132017-09-20 08:26:30 +02002301
Paul Vinciguerra090096b2020-12-03 00:42:46 -05002302 fragments = [x for _, i in self._packet_infos.items()
Klement Sekera75e7d132017-09-20 08:26:30 +02002303 for x in fragment_rfc8200(
2304 i.data, i.index, 400)]
2305
2306 encapped_fragments = \
Klement Sekera4c533132018-02-22 11:41:12 +01002307 [Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
2308 IPv6(src=self.tun_ip6, dst=self.src_if.local_ip6) /
Klement Sekera75e7d132017-09-20 08:26:30 +02002309 GRE() /
2310 p
2311 for p in fragments]
2312
2313 fragmented_encapped_fragments = \
2314 [x for p in encapped_fragments for x in (
2315 fragment_rfc8200(
2316 p,
2317 2 * len(self._packet_infos) + p[IPv6ExtHdrFragment].id,
2318 200)
2319 if IPv6ExtHdrFragment in p else [p]
2320 )
2321 ]
2322
Klement Sekera4c533132018-02-22 11:41:12 +01002323 self.src_if.add_stream(fragmented_encapped_fragments)
Klement Sekera75e7d132017-09-20 08:26:30 +02002324
2325 self.pg_enable_capture(self.pg_interfaces)
2326 self.pg_start()
2327
Klement Sekera4c533132018-02-22 11:41:12 +01002328 self.src_if.assert_nothing_captured()
2329 packets = self.dst_if.get_capture(len(self._packet_infos))
Klement Sekera75e7d132017-09-20 08:26:30 +02002330 self.verify_capture(packets, IPv6)
2331
2332 # TODO remove gre vpp config by hand until VppIpRoute gets fixed
2333 # so that it's query_vpp_config() works as it should
Klement Sekera3ecc2212018-03-27 10:34:43 +02002334 self.gre6.remove_vpp_config()
Klement Sekera75e7d132017-09-20 08:26:30 +02002335
2336
2337if __name__ == '__main__':
2338 unittest.main(testRunner=VppTestRunner)