4 from random import shuffle, choice, randrange
6 from framework import VppTestCase, VppTestRunner
9 from scapy.packet import Raw
10 from scapy.layers.l2 import Ether, GRE
11 from scapy.layers.inet import IP, UDP, ICMP, icmptypes
12 from scapy.layers.inet6 import (
24 from framework import VppTestCase, VppTestRunner
25 from util import ppp, ppc, fragment_rfc791, fragment_rfc8200
26 from vpp_gre_interface import VppGreInterface
27 from vpp_ip import DpoProto
28 from vpp_ip_route import VppIpRoute, VppRoutePath, FibPathProto
29 from vpp_papi import VppEnum
31 # 35 is enough to have >257 400-byte fragments
32 test_packet_count = 35
35 class TestIPv4Reassembly(VppTestCase):
42 cls.create_pg_interfaces([0, 1])
46 # setup all interfaces
47 for i in cls.pg_interfaces:
53 cls.packet_sizes = [64, 512, 1518, 9018]
54 cls.padding = " abcdefghijklmn"
55 cls.create_stream(cls.packet_sizes)
56 cls.create_fragments()
59 def tearDownClass(cls):
60 super().tearDownClass()
63 """Test setup - force timeout on existing reassemblies"""
65 self.vapi.ip_reassembly_enable_disable(
66 sw_if_index=self.src_if.sw_if_index, enable_ip4=True
68 self.vapi.ip_reassembly_set(
70 max_reassemblies=1000,
71 max_reassembly_length=1000,
72 expire_walk_interval_ms=10,
74 self.virtual_sleep(0.25)
75 self.vapi.ip_reassembly_set(
77 max_reassemblies=1000,
78 max_reassembly_length=1000,
79 expire_walk_interval_ms=10000,
83 self.vapi.ip_reassembly_enable_disable(
84 sw_if_index=self.src_if.sw_if_index, enable_ip4=False
88 def show_commands_at_teardown(self):
89 self.logger.debug(self.vapi.ppcli("show ip4-full-reassembly details"))
90 self.logger.debug(self.vapi.ppcli("show buffers"))
93 def create_stream(cls, packet_sizes, packet_count=test_packet_count):
94 """Create input packet stream
96 :param list packet_sizes: Required packet sizes.
98 for i in range(0, packet_count):
99 info = cls.create_packet_info(cls.src_if, cls.src_if)
100 payload = cls.info_to_payload(info)
102 Ether(dst=cls.src_if.local_mac, src=cls.src_if.remote_mac)
104 id=info.index, src=cls.src_if.remote_ip4, dst=cls.dst_if.remote_ip4
106 / UDP(sport=1234, dport=5678)
109 size = packet_sizes[(i // 2) % len(packet_sizes)]
110 cls.extend_packet(p, size, cls.padding)
114 def create_fragments(cls):
115 infos = cls._packet_infos
117 for index, info in infos.items():
119 # cls.logger.debug(ppp("Packet:",
120 # p.__class__(scapy.compat.raw(p))))
121 fragments_400 = fragment_rfc791(p, 400)
122 fragments_300 = fragment_rfc791(p, 300)
123 fragments_200 = [x for f in fragments_400 for x in fragment_rfc791(f, 200)]
124 cls.pkt_infos.append((index, fragments_400, fragments_300, fragments_200))
125 cls.fragments_400 = [x for (_, frags, _, _) in cls.pkt_infos for x in frags]
126 cls.fragments_300 = [x for (_, _, frags, _) in cls.pkt_infos for x in frags]
127 cls.fragments_200 = [x for (_, _, _, frags) in cls.pkt_infos for x in frags]
129 "Fragmented %s packets into %s 400-byte fragments, "
130 "%s 300-byte fragments and %s 200-byte fragments"
133 len(cls.fragments_400),
134 len(cls.fragments_300),
135 len(cls.fragments_200),
139 def verify_capture(self, capture, dropped_packet_indexes=[]):
140 """Verify captured packet stream.
142 :param list capture: Captured packet stream.
146 for packet in capture:
148 self.logger.debug(ppp("Got packet:", packet))
151 payload_info = self.payload_to_info(packet[Raw])
152 packet_index = payload_info.index
154 packet_index not in dropped_packet_indexes,
155 ppp("Packet received, but should be dropped:", packet),
157 if packet_index in seen:
158 raise Exception(ppp("Duplicate packet received", packet))
159 seen.add(packet_index)
160 self.assertEqual(payload_info.dst, self.src_if.sw_if_index)
161 info = self._packet_infos[packet_index]
162 self.assertTrue(info is not None)
163 self.assertEqual(packet_index, info.index)
164 saved_packet = info.data
165 self.assertEqual(ip.src, saved_packet[IP].src)
166 self.assertEqual(ip.dst, saved_packet[IP].dst)
167 self.assertEqual(udp.payload, saved_packet[UDP].payload)
169 self.logger.error(ppp("Unexpected or invalid packet:", packet))
171 for index in self._packet_infos:
173 index in seen or index in dropped_packet_indexes,
174 "Packet with packet_index %d not received" % index,
177 def test_reassembly(self):
178 """basic reassembly"""
180 self.pg_enable_capture()
181 self.src_if.add_stream(self.fragments_200)
184 packets = self.dst_if.get_capture(len(self.pkt_infos))
185 self.verify_capture(packets)
186 self.src_if.assert_nothing_captured()
188 # run it all again to verify correctness
189 self.pg_enable_capture()
190 self.src_if.add_stream(self.fragments_200)
193 packets = self.dst_if.get_capture(len(self.pkt_infos))
194 self.verify_capture(packets)
195 self.src_if.assert_nothing_captured()
197 def test_verify_clear_trace_mid_reassembly(self):
198 """verify clear trace works mid-reassembly"""
200 self.pg_enable_capture()
201 self.src_if.add_stream(self.fragments_200[0:-1])
204 self.logger.debug(self.vapi.cli("show trace"))
205 self.vapi.cli("clear trace")
207 self.src_if.add_stream(self.fragments_200[-1])
209 packets = self.dst_if.get_capture(len(self.pkt_infos))
210 self.verify_capture(packets)
212 def test_reversed(self):
213 """reverse order reassembly"""
215 fragments = list(self.fragments_200)
218 self.pg_enable_capture()
219 self.src_if.add_stream(fragments)
222 packets = self.dst_if.get_capture(len(self.packet_infos))
223 self.verify_capture(packets)
224 self.src_if.assert_nothing_captured()
226 # run it all again to verify correctness
227 self.pg_enable_capture()
228 self.src_if.add_stream(fragments)
231 packets = self.dst_if.get_capture(len(self.packet_infos))
232 self.verify_capture(packets)
233 self.src_if.assert_nothing_captured()
235 def test_long_fragment_chain(self):
236 """long fragment chain"""
238 error_cnt_str = "/err/ip4-full-reassembly-feature/reass_fragment_chain_too_long"
240 error_cnt = self.statistics.get_err_counter(error_cnt_str)
242 self.vapi.ip_reassembly_set(
244 max_reassemblies=1000,
245 max_reassembly_length=3,
246 expire_walk_interval_ms=50,
250 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
251 / IP(id=1000, src=self.src_if.remote_ip4, dst=self.dst_if.remote_ip4)
252 / UDP(sport=1234, dport=5678)
256 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
257 / IP(id=1001, src=self.src_if.remote_ip4, dst=self.dst_if.remote_ip4)
258 / UDP(sport=1234, dport=5678)
261 frags = fragment_rfc791(p1, 200) + fragment_rfc791(p2, 500)
263 self.pg_enable_capture()
264 self.src_if.add_stream(frags)
267 self.dst_if.get_capture(1)
268 self.assert_error_counter_equal(error_cnt_str, error_cnt + 1)
271 """fragment length + ip header size > 65535"""
272 self.vapi.cli("clear errors")
273 raw = b"""E\x00\x00\x88,\xf8\x1f\xfe@\x01\x98\x00\xc0\xa8\n-\xc0\xa8\n\
274 \x01\x08\x00\xf0J\xed\xcb\xf1\xf5Test-group: IPv4.IPv4.ipv4-message.\
275 Ethernet-Payload.IPv4-Packet.IPv4-Header.Fragment-Offset; Test-case: 5737"""
276 malformed_packet = Ether(
277 dst=self.src_if.local_mac, src=self.src_if.remote_mac
280 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
281 / IP(id=1000, src=self.src_if.remote_ip4, dst=self.dst_if.remote_ip4)
282 / UDP(sport=1234, dport=5678)
285 valid_fragments = fragment_rfc791(p, 400)
287 counter = "/err/ip4-full-reassembly-feature/reass_malformed_packet"
288 error_counter = self.statistics.get_err_counter(counter)
289 self.pg_enable_capture()
290 self.src_if.add_stream([malformed_packet] + valid_fragments)
293 self.dst_if.get_capture(1)
294 self.logger.debug(self.vapi.ppcli("show error"))
295 self.assertEqual(self.statistics.get_err_counter(counter), error_counter + 1)
297 def test_44924(self):
298 """compress tiny fragments"""
301 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
307 src=self.src_if.remote_ip4,
308 dst=self.dst_if.remote_ip4,
310 / ICMP(type="echo-request", code=0, id=0x1FE6, seq=0x2407)
311 / Raw(load="Test-group: IPv4")
314 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
320 src=self.src_if.remote_ip4,
321 dst=self.dst_if.remote_ip4,
323 / ICMP(type="echo-request", code=0, id=0x1FE6, seq=0x2407)
324 / Raw(load=".IPv4.Fragmentation.vali")
327 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
332 src=self.src_if.remote_ip4,
333 dst=self.dst_if.remote_ip4,
335 / ICMP(type="echo-request", code=0, id=0x1FE6, seq=0x2407)
336 / Raw(load="d; Test-case: 44924")
340 self.pg_enable_capture()
341 self.src_if.add_stream(packets)
344 self.dst_if.get_capture(1)
346 def test_frag_1(self):
347 """fragment of size 1"""
348 self.vapi.cli("clear errors")
349 malformed_packets = [
351 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
358 src=self.src_if.remote_ip4,
359 dst=self.dst_if.remote_ip4,
361 / ICMP(type="echo-request")
364 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
370 src=self.src_if.remote_ip4,
371 dst=self.dst_if.remote_ip4,
378 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
379 / IP(id=1000, src=self.src_if.remote_ip4, dst=self.dst_if.remote_ip4)
380 / UDP(sport=1234, dport=5678)
383 valid_fragments = fragment_rfc791(p, 400)
385 self.pg_enable_capture()
386 self.src_if.add_stream(malformed_packets + valid_fragments)
389 self.dst_if.get_capture(1)
391 self.assert_packet_counter_equal("ip4-full-reassembly-feature", 1)
392 # TODO remove above, uncomment below once clearing of counters
394 # self.assert_packet_counter_equal(
395 # "/err/ip4-full-reassembly-feature/reass_malformed_packet", 1)
397 def test_random(self):
398 """random order reassembly"""
400 fragments = list(self.fragments_200)
403 self.pg_enable_capture()
404 self.src_if.add_stream(fragments)
407 packets = self.dst_if.get_capture(len(self.packet_infos))
408 self.verify_capture(packets)
409 self.src_if.assert_nothing_captured()
411 # run it all again to verify correctness
412 self.pg_enable_capture()
413 self.src_if.add_stream(fragments)
416 packets = self.dst_if.get_capture(len(self.packet_infos))
417 self.verify_capture(packets)
418 self.src_if.assert_nothing_captured()
420 def test_duplicates(self):
421 """duplicate fragments"""
425 for (_, frags, _, _) in self.pkt_infos
427 for _ in range(0, min(2, len(frags)))
430 self.pg_enable_capture()
431 self.src_if.add_stream(fragments)
434 packets = self.dst_if.get_capture(len(self.pkt_infos))
435 self.verify_capture(packets)
436 self.src_if.assert_nothing_captured()
438 def test_overlap1(self):
439 """overlapping fragments case #1"""
442 for _, _, frags_300, frags_200 in self.pkt_infos:
443 if len(frags_300) == 1:
444 fragments.extend(frags_300)
446 for i, j in zip(frags_200, frags_300):
450 self.pg_enable_capture()
451 self.src_if.add_stream(fragments)
454 packets = self.dst_if.get_capture(len(self.pkt_infos))
455 self.verify_capture(packets)
456 self.src_if.assert_nothing_captured()
458 # run it all to verify correctness
459 self.pg_enable_capture()
460 self.src_if.add_stream(fragments)
463 packets = self.dst_if.get_capture(len(self.pkt_infos))
464 self.verify_capture(packets)
465 self.src_if.assert_nothing_captured()
467 def test_overlap2(self):
468 """overlapping fragments case #2"""
471 for _, _, frags_300, frags_200 in self.pkt_infos:
472 if len(frags_300) == 1:
473 fragments.extend(frags_300)
475 # care must be taken here so that there are no fragments
476 # received by vpp after reassembly is finished, otherwise
477 # new reassemblies will be started and packet generator will
478 # freak out when it detects unfreed buffers
479 zipped = zip(frags_300, frags_200)
485 self.pg_enable_capture()
486 self.src_if.add_stream(fragments)
489 packets = self.dst_if.get_capture(len(self.pkt_infos))
490 self.verify_capture(packets)
491 self.src_if.assert_nothing_captured()
493 # run it all to verify correctness
494 self.pg_enable_capture()
495 self.src_if.add_stream(fragments)
498 packets = self.dst_if.get_capture(len(self.pkt_infos))
499 self.verify_capture(packets)
500 self.src_if.assert_nothing_captured()
502 def test_timeout_inline(self):
503 """timeout (inline)"""
505 dropped_packet_indexes = set(
506 index for (index, frags, _, _) in self.pkt_infos if len(frags) > 1
509 self.vapi.ip_reassembly_set(
511 max_reassemblies=1000,
512 max_reassembly_length=3,
513 expire_walk_interval_ms=10000,
516 self.pg_enable_capture()
517 self.src_if.add_stream(self.fragments_400)
520 packets = self.dst_if.get_capture(
521 len(self.pkt_infos) - len(dropped_packet_indexes)
523 self.verify_capture(packets, dropped_packet_indexes)
524 self.src_if.assert_nothing_captured()
526 def test_timeout_cleanup(self):
527 """timeout (cleanup)"""
529 # whole packets + fragmented packets sans last fragment
532 for (_, frags_400, _, _) in self.pkt_infos
533 for x in frags_400[: -1 if len(frags_400) > 1 else None]
536 # last fragments for fragmented packets
539 for (_, frags_400, _, _) in self.pkt_infos
540 if len(frags_400) > 1
543 dropped_packet_indexes = set(
544 index for (index, frags_400, _, _) in self.pkt_infos if len(frags_400) > 1
547 self.vapi.ip_reassembly_set(
549 max_reassemblies=1000,
550 max_reassembly_length=1000,
551 expire_walk_interval_ms=50,
554 self.pg_enable_capture()
555 self.src_if.add_stream(fragments)
558 self.virtual_sleep(0.25, "wait before sending rest of fragments")
560 self.src_if.add_stream(fragments2)
563 packets = self.dst_if.get_capture(
564 len(self.pkt_infos) - len(dropped_packet_indexes)
566 self.verify_capture(packets, dropped_packet_indexes)
567 self.src_if.assert_nothing_captured()
569 def test_disabled(self):
570 """reassembly disabled"""
572 dropped_packet_indexes = set(
573 index for (index, frags_400, _, _) in self.pkt_infos if len(frags_400) > 1
576 self.vapi.ip_reassembly_set(
579 max_reassembly_length=3,
580 expire_walk_interval_ms=10000,
583 self.pg_enable_capture()
584 self.src_if.add_stream(self.fragments_400)
587 packets = self.dst_if.get_capture(
588 len(self.pkt_infos) - len(dropped_packet_indexes)
590 self.verify_capture(packets, dropped_packet_indexes)
591 self.src_if.assert_nothing_captured()
593 def test_local_enable_disable(self):
594 """local reassembly enabled/disable"""
595 self.vapi.ip_reassembly_enable_disable(
596 sw_if_index=self.src_if.sw_if_index, enable_ip4=False
598 self.vapi.ip_local_reass_enable_disable(enable_ip4=True)
600 Ether(src=self.src_if.remote_mac, dst=self.src_if.local_mac)
601 / IP(src=self.src_if.remote_ip4, dst=self.src_if.local_ip4)
602 / ICMP(id=1234, type="echo-request")
605 frags = fragment_rfc791(p, 400)
606 r = self.send_and_expect(self.src_if, frags, self.src_if, n_rx=1)[0]
607 self.assertEqual(1234, r[ICMP].id)
608 self.assertEqual(icmptypes[r[ICMP].type], "echo-reply")
609 self.vapi.ip_local_reass_enable_disable()
611 self.send_and_assert_no_replies(self.src_if, frags)
612 self.vapi.ip_local_reass_enable_disable(enable_ip4=True)
615 class TestIPv4SVReassembly(VppTestCase):
616 """IPv4 Shallow Virtual Reassembly"""
622 cls.create_pg_interfaces([0, 1])
626 # setup all interfaces
627 for i in cls.pg_interfaces:
633 """Test setup - force timeout on existing reassemblies"""
635 self.vapi.ip_reassembly_enable_disable(
636 sw_if_index=self.src_if.sw_if_index,
638 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
640 self.vapi.ip_reassembly_set(
642 max_reassemblies=1000,
643 max_reassembly_length=1000,
644 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
645 expire_walk_interval_ms=10,
647 self.virtual_sleep(0.25)
648 self.vapi.ip_reassembly_set(
650 max_reassemblies=1000,
651 max_reassembly_length=1000,
652 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
653 expire_walk_interval_ms=10000,
658 self.logger.debug(self.vapi.ppcli("show ip4-sv-reassembly details"))
659 self.logger.debug(self.vapi.ppcli("show buffers"))
661 def test_basic(self):
662 """basic reassembly"""
666 while len(payload) < payload_len:
667 payload += "%u " % counter
671 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
672 / IP(id=1, src=self.src_if.remote_ip4, dst=self.dst_if.remote_ip4)
673 / UDP(sport=1234, dport=5678)
676 fragments = fragment_rfc791(p, payload_len / 4)
678 # send fragment #2 - should be cached inside reassembly
679 self.pg_enable_capture()
680 self.src_if.add_stream(fragments[1])
682 self.logger.debug(self.vapi.ppcli("show ip4-sv-reassembly details"))
683 self.logger.debug(self.vapi.ppcli("show buffers"))
684 self.logger.debug(self.vapi.ppcli("show trace"))
685 self.dst_if.assert_nothing_captured()
687 # send fragment #1 - reassembly is finished now and both fragments
689 self.pg_enable_capture()
690 self.src_if.add_stream(fragments[0])
692 self.logger.debug(self.vapi.ppcli("show ip4-sv-reassembly details"))
693 self.logger.debug(self.vapi.ppcli("show buffers"))
694 self.logger.debug(self.vapi.ppcli("show trace"))
695 c = self.dst_if.get_capture(2)
696 for sent, recvd in zip([fragments[1], fragments[0]], c):
697 self.assertEqual(sent[IP].src, recvd[IP].src)
698 self.assertEqual(sent[IP].dst, recvd[IP].dst)
699 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
701 # send rest of fragments - should be immediately forwarded
702 self.pg_enable_capture()
703 self.src_if.add_stream(fragments[2:])
705 c = self.dst_if.get_capture(len(fragments[2:]))
706 for sent, recvd in zip(fragments[2:], c):
707 self.assertEqual(sent[IP].src, recvd[IP].src)
708 self.assertEqual(sent[IP].dst, recvd[IP].dst)
709 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
711 def test_verify_clear_trace_mid_reassembly(self):
712 """verify clear trace works mid-reassembly"""
716 while len(payload) < payload_len:
717 payload += "%u " % counter
721 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
722 / IP(id=1, src=self.src_if.remote_ip4, dst=self.dst_if.remote_ip4)
723 / UDP(sport=1234, dport=5678)
726 fragments = fragment_rfc791(p, payload_len / 4)
728 self.pg_enable_capture()
729 self.src_if.add_stream(fragments[1])
732 self.logger.debug(self.vapi.cli("show trace"))
733 self.vapi.cli("clear trace")
735 self.pg_enable_capture()
736 self.src_if.add_stream(fragments[0])
738 self.dst_if.get_capture(2)
740 self.logger.debug(self.vapi.cli("show trace"))
741 self.vapi.cli("clear trace")
743 self.pg_enable_capture()
744 self.src_if.add_stream(fragments[2:])
746 self.dst_if.get_capture(len(fragments[2:]))
748 def test_timeout(self):
749 """reassembly timeout"""
753 while len(payload) < payload_len:
754 payload += "%u " % counter
758 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
759 / IP(id=1, src=self.src_if.remote_ip4, dst=self.dst_if.remote_ip4)
760 / UDP(sport=1234, dport=5678)
763 fragments = fragment_rfc791(p, payload_len / 4)
765 self.vapi.ip_reassembly_set(
767 max_reassemblies=1000,
768 max_reassembly_length=1000,
769 expire_walk_interval_ms=50,
770 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
773 # send fragments #2 and #1 - should be forwarded
774 self.pg_enable_capture()
775 self.src_if.add_stream(fragments[0:2])
777 self.logger.debug(self.vapi.ppcli("show ip4-sv-reassembly details"))
778 self.logger.debug(self.vapi.ppcli("show buffers"))
779 self.logger.debug(self.vapi.ppcli("show trace"))
780 c = self.dst_if.get_capture(2)
781 for sent, recvd in zip([fragments[1], fragments[0]], c):
782 self.assertEqual(sent[IP].src, recvd[IP].src)
783 self.assertEqual(sent[IP].dst, recvd[IP].dst)
784 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
787 self.virtual_sleep(0.25, "wait before sending rest of fragments")
789 # send rest of fragments - shouldn't be forwarded
790 self.pg_enable_capture()
791 self.src_if.add_stream(fragments[2:])
793 self.dst_if.assert_nothing_captured()
796 """reassembly reuses LRU element"""
798 self.vapi.ip_reassembly_set(
801 max_reassembly_length=1000,
802 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
803 expire_walk_interval_ms=10000,
809 while len(payload) < payload_len:
810 payload += "%u " % counter
817 for i in range(packet_count)
819 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
820 / IP(id=i, src=self.src_if.remote_ip4, dst=self.dst_if.remote_ip4)
821 / UDP(sport=1234, dport=5678)
824 for f in fragment_rfc791(p, payload_len / 4)
827 self.pg_enable_capture()
828 self.src_if.add_stream(fragments)
830 c = self.dst_if.get_capture(len(fragments))
831 for sent, recvd in zip(fragments, c):
832 self.assertEqual(sent[IP].src, recvd[IP].src)
833 self.assertEqual(sent[IP].dst, recvd[IP].dst)
834 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
836 def send_mixed_and_verify_capture(self, traffic):
839 for c in range(t["count"]):
842 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
846 src=self.src_if.remote_ip4,
847 dst=self.dst_if.remote_ip4,
849 / UDP(sport=1234, dport=5678)
853 self.counter = self.counter + 1
855 self.pg_enable_capture()
856 self.src_if.add_stream(stream)
858 self.logger.debug(self.vapi.ppcli("show ip4-sv-reassembly details"))
859 self.logger.debug(self.vapi.ppcli("show buffers"))
860 self.logger.debug(self.vapi.ppcli("show trace"))
861 self.dst_if.get_capture(len(stream))
863 def test_mixed(self):
864 """mixed traffic correctly passes through SVR"""
867 self.send_mixed_and_verify_capture([{"count": 1, "flags": ""}])
868 self.send_mixed_and_verify_capture([{"count": 2, "flags": ""}])
869 self.send_mixed_and_verify_capture([{"count": 3, "flags": ""}])
870 self.send_mixed_and_verify_capture([{"count": 8, "flags": ""}])
871 self.send_mixed_and_verify_capture([{"count": 257, "flags": ""}])
873 self.send_mixed_and_verify_capture([{"count": 1, "flags": "MF"}])
874 self.send_mixed_and_verify_capture([{"count": 2, "flags": "MF"}])
875 self.send_mixed_and_verify_capture([{"count": 3, "flags": "MF"}])
876 self.send_mixed_and_verify_capture([{"count": 8, "flags": "MF"}])
877 self.send_mixed_and_verify_capture([{"count": 257, "flags": "MF"}])
879 self.send_mixed_and_verify_capture(
880 [{"count": 1, "flags": ""}, {"count": 1, "flags": "MF"}]
882 self.send_mixed_and_verify_capture(
883 [{"count": 2, "flags": ""}, {"count": 2, "flags": "MF"}]
885 self.send_mixed_and_verify_capture(
886 [{"count": 3, "flags": ""}, {"count": 3, "flags": "MF"}]
888 self.send_mixed_and_verify_capture(
889 [{"count": 8, "flags": ""}, {"count": 8, "flags": "MF"}]
891 self.send_mixed_and_verify_capture(
892 [{"count": 129, "flags": ""}, {"count": 129, "flags": "MF"}]
895 self.send_mixed_and_verify_capture(
897 {"count": 1, "flags": ""},
898 {"count": 1, "flags": "MF"},
899 {"count": 1, "flags": ""},
900 {"count": 1, "flags": "MF"},
903 self.send_mixed_and_verify_capture(
905 {"count": 2, "flags": ""},
906 {"count": 2, "flags": "MF"},
907 {"count": 2, "flags": ""},
908 {"count": 2, "flags": "MF"},
911 self.send_mixed_and_verify_capture(
913 {"count": 3, "flags": ""},
914 {"count": 3, "flags": "MF"},
915 {"count": 3, "flags": ""},
916 {"count": 3, "flags": "MF"},
919 self.send_mixed_and_verify_capture(
921 {"count": 8, "flags": ""},
922 {"count": 8, "flags": "MF"},
923 {"count": 8, "flags": ""},
924 {"count": 8, "flags": "MF"},
927 self.send_mixed_and_verify_capture(
929 {"count": 65, "flags": ""},
930 {"count": 65, "flags": "MF"},
931 {"count": 65, "flags": ""},
932 {"count": 65, "flags": "MF"},
937 class TestIPv4MWReassembly(VppTestCase):
938 """IPv4 Reassembly (multiple workers)"""
946 cls.create_pg_interfaces(range(cls.vpp_worker_count + 1))
948 cls.send_ifs = cls.pg_interfaces[:-1]
949 cls.dst_if = cls.pg_interfaces[-1]
951 # setup all interfaces
952 for i in cls.pg_interfaces:
957 # packets sizes reduced here because we are generating packets without
958 # Ethernet headers, which are added later (diff fragments go via
959 # different interfaces)
966 cls.padding = " abcdefghijklmn"
967 cls.create_stream(cls.packet_sizes)
968 cls.create_fragments()
971 def tearDownClass(cls):
972 super().tearDownClass()
975 """Test setup - force timeout on existing reassemblies"""
977 for intf in self.send_ifs:
978 self.vapi.ip_reassembly_enable_disable(
979 sw_if_index=intf.sw_if_index, enable_ip4=True
981 self.vapi.ip_reassembly_set(
983 max_reassemblies=1000,
984 max_reassembly_length=1000,
985 expire_walk_interval_ms=10,
987 self.virtual_sleep(0.25)
988 self.vapi.ip_reassembly_set(
990 max_reassemblies=1000,
991 max_reassembly_length=1000,
992 expire_walk_interval_ms=10000,
996 for intf in self.send_ifs:
997 self.vapi.ip_reassembly_enable_disable(
998 sw_if_index=intf.sw_if_index, enable_ip4=False
1002 def show_commands_at_teardown(self):
1003 self.logger.debug(self.vapi.ppcli("show ip4-full-reassembly details"))
1004 self.logger.debug(self.vapi.ppcli("show buffers"))
1007 def create_stream(cls, packet_sizes, packet_count=test_packet_count):
1008 """Create input packet stream
1010 :param list packet_sizes: Required packet sizes.
1012 for i in range(0, packet_count):
1013 info = cls.create_packet_info(cls.src_if, cls.src_if)
1014 payload = cls.info_to_payload(info)
1016 IP(id=info.index, src=cls.src_if.remote_ip4, dst=cls.dst_if.remote_ip4)
1017 / UDP(sport=1234, dport=5678)
1020 size = packet_sizes[(i // 2) % len(packet_sizes)]
1021 cls.extend_packet(p, size, cls.padding)
1025 def create_fragments(cls):
1026 infos = cls._packet_infos
1028 for index, info in infos.items():
1030 # cls.logger.debug(ppp("Packet:",
1031 # p.__class__(scapy.compat.raw(p))))
1032 fragments_400 = fragment_rfc791(p, 400)
1033 cls.pkt_infos.append((index, fragments_400))
1034 cls.fragments_400 = [x for (_, frags) in cls.pkt_infos for x in frags]
1036 "Fragmented %s packets into %s 400-byte fragments, "
1037 % (len(infos), len(cls.fragments_400))
1040 def verify_capture(self, capture, dropped_packet_indexes=[]):
1041 """Verify captured packet stream.
1043 :param list capture: Captured packet stream.
1047 for packet in capture:
1049 self.logger.debug(ppp("Got packet:", packet))
1052 payload_info = self.payload_to_info(packet[Raw])
1053 packet_index = payload_info.index
1055 packet_index not in dropped_packet_indexes,
1056 ppp("Packet received, but should be dropped:", packet),
1058 if packet_index in seen:
1059 raise Exception(ppp("Duplicate packet received", packet))
1060 seen.add(packet_index)
1061 self.assertEqual(payload_info.dst, self.src_if.sw_if_index)
1062 info = self._packet_infos[packet_index]
1063 self.assertTrue(info is not None)
1064 self.assertEqual(packet_index, info.index)
1065 saved_packet = info.data
1066 self.assertEqual(ip.src, saved_packet[IP].src)
1067 self.assertEqual(ip.dst, saved_packet[IP].dst)
1068 self.assertEqual(udp.payload, saved_packet[UDP].payload)
1070 self.logger.error(ppp("Unexpected or invalid packet:", packet))
1072 for index in self._packet_infos:
1074 index in seen or index in dropped_packet_indexes,
1075 "Packet with packet_index %d not received" % index,
1078 def send_packets(self, packets):
1079 for counter in range(self.vpp_worker_count):
1080 if 0 == len(packets[counter]):
1082 send_if = self.send_ifs[counter]
1085 Ether(dst=send_if.local_mac, src=send_if.remote_mac) / x
1086 for x in packets[counter]
1092 def test_worker_conflict(self):
1093 """1st and FO=0 fragments on different workers"""
1095 # in first wave we send fragments which don't start at offset 0
1096 # then we send fragments with offset 0 on a different thread
1097 # then the rest of packets on a random thread
1098 first_packets = [[] for n in range(self.vpp_worker_count)]
1099 second_packets = [[] for n in range(self.vpp_worker_count)]
1100 rest_of_packets = [[] for n in range(self.vpp_worker_count)]
1101 for _, p in self.pkt_infos:
1102 wi = randrange(self.vpp_worker_count)
1103 second_packets[wi].append(p[0])
1108 wi2 = randrange(self.vpp_worker_count)
1109 first_packets[wi2].append(p[1])
1110 wi3 = randrange(self.vpp_worker_count)
1111 rest_of_packets[wi3].extend(p[2:])
1113 self.pg_enable_capture()
1114 self.send_packets(first_packets)
1115 self.send_packets(second_packets)
1116 self.send_packets(rest_of_packets)
1118 packets = self.dst_if.get_capture(len(self.pkt_infos))
1119 self.verify_capture(packets)
1120 for send_if in self.send_ifs:
1121 send_if.assert_nothing_captured()
1123 self.logger.debug(self.vapi.ppcli("show trace"))
1124 self.logger.debug(self.vapi.ppcli("show ip4-full-reassembly details"))
1125 self.logger.debug(self.vapi.ppcli("show buffers"))
1126 self.vapi.cli("clear trace")
1128 self.pg_enable_capture()
1129 self.send_packets(first_packets)
1130 self.send_packets(second_packets)
1131 self.send_packets(rest_of_packets)
1133 packets = self.dst_if.get_capture(len(self.pkt_infos))
1134 self.verify_capture(packets)
1135 for send_if in self.send_ifs:
1136 send_if.assert_nothing_captured()
1139 class TestIPv6Reassembly(VppTestCase):
1140 """IPv6 Reassembly"""
1143 def setUpClass(cls):
1144 super().setUpClass()
1146 cls.create_pg_interfaces([0, 1])
1147 cls.src_if = cls.pg0
1148 cls.dst_if = cls.pg1
1150 # setup all interfaces
1151 for i in cls.pg_interfaces:
1157 cls.packet_sizes = [64, 512, 1518, 9018]
1158 cls.padding = " abcdefghijklmn"
1159 cls.create_stream(cls.packet_sizes)
1160 cls.create_fragments()
1163 def tearDownClass(cls):
1164 super().tearDownClass()
1167 """Test setup - force timeout on existing reassemblies"""
1169 self.vapi.ip_reassembly_enable_disable(
1170 sw_if_index=self.src_if.sw_if_index, enable_ip6=True
1172 self.vapi.ip_reassembly_set(
1174 max_reassemblies=1000,
1175 max_reassembly_length=1000,
1176 expire_walk_interval_ms=10,
1179 self.virtual_sleep(0.25)
1180 self.vapi.ip_reassembly_set(
1182 max_reassemblies=1000,
1183 max_reassembly_length=1000,
1184 expire_walk_interval_ms=10000,
1187 self.logger.debug(self.vapi.ppcli("show ip6-full-reassembly details"))
1188 self.logger.debug(self.vapi.ppcli("show buffers"))
1191 self.vapi.ip_reassembly_enable_disable(
1192 sw_if_index=self.src_if.sw_if_index, enable_ip6=False
1196 def show_commands_at_teardown(self):
1197 self.logger.debug(self.vapi.ppcli("show ip6-full-reassembly details"))
1198 self.logger.debug(self.vapi.ppcli("show buffers"))
1201 def create_stream(cls, packet_sizes, packet_count=test_packet_count):
1202 """Create input packet stream for defined interface.
1204 :param list packet_sizes: Required packet sizes.
1206 for i in range(0, packet_count):
1207 info = cls.create_packet_info(cls.src_if, cls.src_if)
1208 payload = cls.info_to_payload(info)
1210 Ether(dst=cls.src_if.local_mac, src=cls.src_if.remote_mac)
1211 / IPv6(src=cls.src_if.remote_ip6, dst=cls.dst_if.remote_ip6)
1212 / UDP(sport=1234, dport=5678)
1215 size = packet_sizes[(i // 2) % len(packet_sizes)]
1216 cls.extend_packet(p, size, cls.padding)
1220 def create_fragments(cls):
1221 infos = cls._packet_infos
1223 for index, info in infos.items():
1225 # cls.logger.debug(ppp("Packet:",
1226 # p.__class__(scapy.compat.raw(p))))
1227 fragments_400 = fragment_rfc8200(p, info.index, 400)
1228 fragments_300 = fragment_rfc8200(p, info.index, 300)
1229 cls.pkt_infos.append((index, fragments_400, fragments_300))
1230 cls.fragments_400 = [x for _, frags, _ in cls.pkt_infos for x in frags]
1231 cls.fragments_300 = [x for _, _, frags in cls.pkt_infos for x in frags]
1233 "Fragmented %s packets into %s 400-byte fragments, "
1234 "and %s 300-byte fragments"
1235 % (len(infos), len(cls.fragments_400), len(cls.fragments_300))
1238 def verify_capture(self, capture, dropped_packet_indexes=[]):
1239 """Verify captured packet strea .
1241 :param list capture: Captured packet stream.
1245 for packet in capture:
1247 self.logger.debug(ppp("Got packet:", packet))
1250 payload_info = self.payload_to_info(packet[Raw])
1251 packet_index = payload_info.index
1253 packet_index not in dropped_packet_indexes,
1254 ppp("Packet received, but should be dropped:", packet),
1256 if packet_index in seen:
1257 raise Exception(ppp("Duplicate packet received", packet))
1258 seen.add(packet_index)
1259 self.assertEqual(payload_info.dst, self.src_if.sw_if_index)
1260 info = self._packet_infos[packet_index]
1261 self.assertTrue(info is not None)
1262 self.assertEqual(packet_index, info.index)
1263 saved_packet = info.data
1264 self.assertEqual(ip.src, saved_packet[IPv6].src)
1265 self.assertEqual(ip.dst, saved_packet[IPv6].dst)
1266 self.assertEqual(udp.payload, saved_packet[UDP].payload)
1268 self.logger.error(ppp("Unexpected or invalid packet:", packet))
1270 for index in self._packet_infos:
1272 index in seen or index in dropped_packet_indexes,
1273 "Packet with packet_index %d not received" % index,
1276 def test_reassembly(self):
1277 """basic reassembly"""
1279 self.pg_enable_capture()
1280 self.src_if.add_stream(self.fragments_400)
1283 packets = self.dst_if.get_capture(len(self.pkt_infos))
1284 self.verify_capture(packets)
1285 self.src_if.assert_nothing_captured()
1287 # run it all again to verify correctness
1288 self.pg_enable_capture()
1289 self.src_if.add_stream(self.fragments_400)
1292 packets = self.dst_if.get_capture(len(self.pkt_infos))
1293 self.verify_capture(packets)
1294 self.src_if.assert_nothing_captured()
1296 def test_buffer_boundary(self):
1297 """fragment header crossing buffer boundary"""
1300 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
1301 / IPv6(src=self.src_if.remote_ip6, dst=self.src_if.local_ip6)
1302 / IPv6ExtHdrHopByHop(options=[HBHOptUnknown(otype=0xFF, optlen=0)] * 1000)
1303 / IPv6ExtHdrFragment(m=1)
1304 / UDP(sport=1234, dport=5678)
1307 self.pg_enable_capture()
1308 self.src_if.add_stream([p])
1310 self.src_if.assert_nothing_captured()
1311 self.dst_if.assert_nothing_captured()
1313 def test_verify_clear_trace_mid_reassembly(self):
1314 """verify clear trace works mid-reassembly"""
1316 self.pg_enable_capture()
1317 self.src_if.add_stream(self.fragments_400[0:-1])
1320 self.logger.debug(self.vapi.cli("show trace"))
1321 self.vapi.cli("clear trace")
1323 self.src_if.add_stream(self.fragments_400[-1])
1325 packets = self.dst_if.get_capture(len(self.pkt_infos))
1326 self.verify_capture(packets)
1328 def test_reversed(self):
1329 """reverse order reassembly"""
1331 fragments = list(self.fragments_400)
1334 self.pg_enable_capture()
1335 self.src_if.add_stream(fragments)
1338 packets = self.dst_if.get_capture(len(self.pkt_infos))
1339 self.verify_capture(packets)
1340 self.src_if.assert_nothing_captured()
1342 # run it all again to verify correctness
1343 self.pg_enable_capture()
1344 self.src_if.add_stream(fragments)
1347 packets = self.dst_if.get_capture(len(self.pkt_infos))
1348 self.verify_capture(packets)
1349 self.src_if.assert_nothing_captured()
1351 def test_random(self):
1352 """random order reassembly"""
1354 fragments = list(self.fragments_400)
1357 self.pg_enable_capture()
1358 self.src_if.add_stream(fragments)
1361 packets = self.dst_if.get_capture(len(self.pkt_infos))
1362 self.verify_capture(packets)
1363 self.src_if.assert_nothing_captured()
1365 # run it all again to verify correctness
1366 self.pg_enable_capture()
1367 self.src_if.add_stream(fragments)
1370 packets = self.dst_if.get_capture(len(self.pkt_infos))
1371 self.verify_capture(packets)
1372 self.src_if.assert_nothing_captured()
1374 def test_duplicates(self):
1375 """duplicate fragments"""
1379 for (_, frags, _) in self.pkt_infos
1381 for _ in range(0, min(2, len(frags)))
1384 self.pg_enable_capture()
1385 self.src_if.add_stream(fragments)
1388 packets = self.dst_if.get_capture(len(self.pkt_infos))
1389 self.verify_capture(packets)
1390 self.src_if.assert_nothing_captured()
1392 def test_long_fragment_chain(self):
1393 """long fragment chain"""
1395 error_cnt_str = "/err/ip6-full-reassembly-feature/reass_fragment_chain_too_long"
1397 error_cnt = self.statistics.get_err_counter(error_cnt_str)
1399 self.vapi.ip_reassembly_set(
1401 max_reassemblies=1000,
1402 max_reassembly_length=3,
1403 expire_walk_interval_ms=50,
1408 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
1409 / IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6)
1410 / UDP(sport=1234, dport=5678)
1413 frags = fragment_rfc8200(p, 1, 300) + fragment_rfc8200(p, 2, 500)
1415 self.pg_enable_capture()
1416 self.src_if.add_stream(frags)
1419 self.dst_if.get_capture(1)
1420 self.assert_error_counter_equal(error_cnt_str, error_cnt + 1)
1422 def test_overlap1(self):
1423 """overlapping fragments case #1"""
1426 for _, frags_400, frags_300 in self.pkt_infos:
1427 if len(frags_300) == 1:
1428 fragments.extend(frags_400)
1430 for i, j in zip(frags_300, frags_400):
1434 dropped_packet_indexes = set(
1435 index for (index, _, frags) in self.pkt_infos if len(frags) > 1
1438 self.pg_enable_capture()
1439 self.src_if.add_stream(fragments)
1442 packets = self.dst_if.get_capture(
1443 len(self.pkt_infos) - len(dropped_packet_indexes)
1445 self.verify_capture(packets, dropped_packet_indexes)
1446 self.src_if.assert_nothing_captured()
1448 def test_overlap2(self):
1449 """overlapping fragments case #2"""
1452 for _, frags_400, frags_300 in self.pkt_infos:
1453 if len(frags_400) == 1:
1454 fragments.extend(frags_400)
1456 # care must be taken here so that there are no fragments
1457 # received by vpp after reassembly is finished, otherwise
1458 # new reassemblies will be started and packet generator will
1459 # freak out when it detects unfreed buffers
1460 zipped = zip(frags_400, frags_300)
1466 dropped_packet_indexes = set(
1467 index for (index, _, frags) in self.pkt_infos if len(frags) > 1
1470 self.pg_enable_capture()
1471 self.src_if.add_stream(fragments)
1474 packets = self.dst_if.get_capture(
1475 len(self.pkt_infos) - len(dropped_packet_indexes)
1477 self.verify_capture(packets, dropped_packet_indexes)
1478 self.src_if.assert_nothing_captured()
1480 def test_timeout_inline(self):
1481 """timeout (inline)"""
1483 dropped_packet_indexes = set(
1484 index for (index, frags, _) in self.pkt_infos if len(frags) > 1
1487 self.vapi.ip_reassembly_set(
1489 max_reassemblies=1000,
1490 max_reassembly_length=3,
1491 expire_walk_interval_ms=10000,
1495 self.pg_enable_capture()
1496 self.src_if.add_stream(self.fragments_400)
1499 packets = self.dst_if.get_capture(
1500 len(self.pkt_infos) - len(dropped_packet_indexes)
1502 self.verify_capture(packets, dropped_packet_indexes)
1503 pkts = self.src_if._get_capture(1)
1505 self.assertIn(ICMPv6TimeExceeded, icmp)
1506 self.assertIn(IPv6ExtHdrFragment, icmp)
1507 self.assertIn(icmp[IPv6ExtHdrFragment].id, dropped_packet_indexes)
1508 dropped_packet_indexes.remove(icmp[IPv6ExtHdrFragment].id)
1510 def test_timeout_cleanup(self):
1511 """timeout (cleanup)"""
1513 # whole packets + fragmented packets sans last fragment
1516 for (_, frags_400, _) in self.pkt_infos
1517 for x in frags_400[: -1 if len(frags_400) > 1 else None]
1520 # last fragments for fragmented packets
1522 frags_400[-1] for (_, frags_400, _) in self.pkt_infos if len(frags_400) > 1
1525 dropped_packet_indexes = set(
1526 index for (index, frags_400, _) in self.pkt_infos if len(frags_400) > 1
1529 self.vapi.ip_reassembly_set(
1531 max_reassemblies=1000,
1532 max_reassembly_length=1000,
1533 expire_walk_interval_ms=50,
1536 self.vapi.ip_reassembly_set(
1538 max_reassemblies=1000,
1539 max_reassembly_length=1000,
1540 expire_walk_interval_ms=50,
1544 self.pg_enable_capture()
1545 self.src_if.add_stream(fragments)
1548 self.virtual_sleep(0.25, "wait before sending rest of fragments")
1550 self.src_if.add_stream(fragments2)
1553 packets = self.dst_if.get_capture(
1554 len(self.pkt_infos) - len(dropped_packet_indexes)
1556 self.verify_capture(packets, dropped_packet_indexes)
1557 pkts = self.src_if._get_capture(1)
1559 self.assertIn(ICMPv6TimeExceeded, icmp)
1560 self.assertIn(IPv6ExtHdrFragment, icmp)
1561 self.assertIn(icmp[IPv6ExtHdrFragment].id, dropped_packet_indexes)
1562 dropped_packet_indexes.remove(icmp[IPv6ExtHdrFragment].id)
1564 def test_disabled(self):
1565 """reassembly disabled"""
1567 dropped_packet_indexes = set(
1568 index for (index, frags_400, _) in self.pkt_infos if len(frags_400) > 1
1571 self.vapi.ip_reassembly_set(
1574 max_reassembly_length=3,
1575 expire_walk_interval_ms=10000,
1579 self.pg_enable_capture()
1580 self.src_if.add_stream(self.fragments_400)
1583 packets = self.dst_if.get_capture(
1584 len(self.pkt_infos) - len(dropped_packet_indexes)
1586 self.verify_capture(packets, dropped_packet_indexes)
1587 self.src_if.assert_nothing_captured()
1589 def test_missing_upper(self):
1590 """missing upper layer"""
1591 optdata = "\x00" * 100
1593 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
1594 / IPv6(src=self.src_if.remote_ip6, dst=self.src_if.local_ip6)
1595 / IPv6ExtHdrFragment(m=1)
1596 / IPv6ExtHdrDestOpt(
1597 nh=17, options=PadN(optdata="\101" * 255) / PadN(optdata="\102" * 255)
1601 self.pg_enable_capture()
1602 self.src_if.add_stream([p])
1604 pkts = self.src_if.get_capture(expected_count=1)
1606 self.assertIn(ICMPv6ParamProblem, icmp)
1607 self.assert_equal(icmp[ICMPv6ParamProblem].code, 3, "ICMP code")
1609 def test_truncated_fragment(self):
1610 """truncated fragment"""
1612 Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac)
1613 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6, nh=44, plen=2)
1614 / IPv6ExtHdrFragment(nh=6)
1617 self.send_and_assert_no_replies(self.pg0, [pkt], self.pg0)
1619 def test_invalid_frag_size(self):
1620 """fragment size not a multiple of 8"""
1622 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
1623 / IPv6(src=self.src_if.remote_ip6, dst=self.src_if.local_ip6)
1624 / UDP(sport=1234, dport=5678)
1627 self.extend_packet(p, 1000, self.padding)
1628 fragments = fragment_rfc8200(p, 1, 500)
1629 bad_fragment = fragments[0]
1630 self.extend_packet(bad_fragment, len(bad_fragment) + 5)
1631 self.pg_enable_capture()
1632 self.src_if.add_stream([bad_fragment])
1634 pkts = self.src_if.get_capture(expected_count=1)
1636 self.assertIn(ICMPv6ParamProblem, icmp)
1637 self.assert_equal(icmp[ICMPv6ParamProblem].code, 0, "ICMP code")
1639 def test_invalid_packet_size(self):
1640 """total packet size > 65535"""
1642 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
1643 / IPv6(src=self.src_if.remote_ip6, dst=self.src_if.local_ip6)
1644 / UDP(sport=1234, dport=5678)
1647 self.extend_packet(p, 1000, self.padding)
1648 fragments = fragment_rfc8200(p, 1, 500)
1649 bad_fragment = fragments[1]
1650 bad_fragment[IPv6ExtHdrFragment].offset = 65500
1651 self.pg_enable_capture()
1652 self.src_if.add_stream([bad_fragment])
1654 pkts = self.src_if.get_capture(expected_count=1)
1656 self.assertIn(ICMPv6ParamProblem, icmp)
1657 self.assert_equal(icmp[ICMPv6ParamProblem].code, 0, "ICMP code")
1659 def test_atomic_fragment(self):
1660 """IPv6 atomic fragment"""
1662 Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac)
1663 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6, nh=44, plen=65535)
1664 / IPv6ExtHdrFragment(
1665 offset=8191, m=1, res1=0xFF, res2=0xFF, nh=255, id=0xFFFF
1670 rx = self.send_and_expect(self.pg0, [pkt], self.pg0)
1671 self.assertIn(ICMPv6ParamProblem, rx[0])
1673 def test_truncated_fragment(self):
1674 """IPv6 truncated fragment header"""
1676 Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac)
1677 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6, nh=44, plen=2)
1678 / IPv6ExtHdrFragment(nh=6)
1681 self.send_and_assert_no_replies(self.pg0, [pkt])
1684 Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac)
1685 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.remote_ip6)
1686 / ICMPv6EchoRequest()
1688 rx = self.send_and_expect(self.pg0, [pkt], self.pg0)
1690 def test_one_fragment(self):
1691 """whole packet in one fragment processed independently"""
1693 Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac)
1694 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6)
1695 / ICMPv6EchoRequest()
1698 frags = fragment_rfc8200(pkt, 1, 400)
1700 # send a fragment with known id
1701 self.send_and_assert_no_replies(self.pg0, [frags[0]])
1703 # send an atomic fragment with same id - should be reassembled
1705 Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac)
1706 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6)
1707 / IPv6ExtHdrFragment(id=1)
1708 / ICMPv6EchoRequest()
1710 rx = self.send_and_expect(self.pg0, [pkt], self.pg0)
1711 self.assertNotIn(IPv6ExtHdrFragment, rx)
1713 # now finish the original reassembly, this should still be possible
1714 rx = self.send_and_expect(self.pg0, frags[1:], self.pg0, n_rx=1)
1715 self.assertNotIn(IPv6ExtHdrFragment, rx)
1717 def test_bunch_of_fragments(self):
1718 """valid fragments followed by rogue fragments and atomic fragment"""
1720 Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac)
1721 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6)
1722 / ICMPv6EchoRequest()
1725 frags = fragment_rfc8200(pkt, 1, 400)
1726 self.send_and_expect(self.pg0, frags, self.pg0, n_rx=1)
1729 Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac)
1730 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6)
1731 / IPv6ExtHdrFragment(id=1, nh=58, offset=608)
1735 self.send_and_assert_no_replies(self.pg0, inc_frag * 604)
1738 Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac)
1739 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6)
1740 / IPv6ExtHdrFragment(id=1)
1741 / ICMPv6EchoRequest()
1743 rx = self.send_and_expect(self.pg0, [pkt], self.pg0)
1744 self.assertNotIn(IPv6ExtHdrFragment, rx)
1746 def test_local_enable_disable(self):
1747 """local reassembly enabled/disable"""
1748 self.vapi.ip_reassembly_enable_disable(
1749 sw_if_index=self.src_if.sw_if_index, enable_ip6=False
1751 self.vapi.ip_local_reass_enable_disable(enable_ip6=True)
1753 Ether(src=self.src_if.local_mac, dst=self.src_if.remote_mac)
1754 / IPv6(src=self.src_if.remote_ip6, dst=self.src_if.local_ip6)
1755 / ICMPv6EchoRequest(id=1234)
1758 frags = fragment_rfc8200(pkt, 1, 400)
1759 r = self.send_and_expect(self.src_if, frags, self.src_if, n_rx=1)[0]
1760 self.assertEqual(1234, r[ICMPv6EchoReply].id)
1761 self.vapi.ip_local_reass_enable_disable()
1763 self.send_and_assert_no_replies(self.src_if, frags)
1764 self.vapi.ip_local_reass_enable_disable(enable_ip6=True)
1767 class TestIPv6MWReassembly(VppTestCase):
1768 """IPv6 Reassembly (multiple workers)"""
1770 vpp_worker_count = 3
1773 def setUpClass(cls):
1774 super().setUpClass()
1776 cls.create_pg_interfaces(range(cls.vpp_worker_count + 1))
1777 cls.src_if = cls.pg0
1778 cls.send_ifs = cls.pg_interfaces[:-1]
1779 cls.dst_if = cls.pg_interfaces[-1]
1781 # setup all interfaces
1782 for i in cls.pg_interfaces:
1787 # packets sizes reduced here because we are generating packets without
1788 # Ethernet headers, which are added later (diff fragments go via
1789 # different interfaces)
1790 cls.packet_sizes = [
1793 1518 - len(Ether()),
1794 9018 - len(Ether()),
1796 cls.padding = " abcdefghijklmn"
1797 cls.create_stream(cls.packet_sizes)
1798 cls.create_fragments()
1801 def tearDownClass(cls):
1802 super().tearDownClass()
1805 """Test setup - force timeout on existing reassemblies"""
1807 for intf in self.send_ifs:
1808 self.vapi.ip_reassembly_enable_disable(
1809 sw_if_index=intf.sw_if_index, enable_ip6=True
1811 self.vapi.ip_reassembly_set(
1813 max_reassemblies=1000,
1814 max_reassembly_length=1000,
1815 expire_walk_interval_ms=10,
1818 self.virtual_sleep(0.25)
1819 self.vapi.ip_reassembly_set(
1821 max_reassemblies=1000,
1822 max_reassembly_length=1000,
1823 expire_walk_interval_ms=1000,
1828 for intf in self.send_ifs:
1829 self.vapi.ip_reassembly_enable_disable(
1830 sw_if_index=intf.sw_if_index, enable_ip6=False
1834 def show_commands_at_teardown(self):
1835 self.logger.debug(self.vapi.ppcli("show ip6-full-reassembly details"))
1836 self.logger.debug(self.vapi.ppcli("show buffers"))
1839 def create_stream(cls, packet_sizes, packet_count=test_packet_count):
1840 """Create input packet stream
1842 :param list packet_sizes: Required packet sizes.
1844 for i in range(0, packet_count):
1845 info = cls.create_packet_info(cls.src_if, cls.src_if)
1846 payload = cls.info_to_payload(info)
1848 IPv6(src=cls.src_if.remote_ip6, dst=cls.dst_if.remote_ip6)
1849 / UDP(sport=1234, dport=5678)
1852 size = packet_sizes[(i // 2) % len(packet_sizes)]
1853 cls.extend_packet(p, size, cls.padding)
1857 def create_fragments(cls):
1858 infos = cls._packet_infos
1860 for index, info in infos.items():
1862 # cls.logger.debug(ppp("Packet:",
1863 # p.__class__(scapy.compat.raw(p))))
1864 fragments_400 = fragment_rfc8200(p, index, 400)
1865 cls.pkt_infos.append((index, fragments_400))
1866 cls.fragments_400 = [x for (_, frags) in cls.pkt_infos for x in frags]
1868 "Fragmented %s packets into %s 400-byte fragments, "
1869 % (len(infos), len(cls.fragments_400))
1872 def verify_capture(self, capture, dropped_packet_indexes=[]):
1873 """Verify captured packet strea .
1875 :param list capture: Captured packet stream.
1879 for packet in capture:
1881 self.logger.debug(ppp("Got packet:", packet))
1884 payload_info = self.payload_to_info(packet[Raw])
1885 packet_index = payload_info.index
1887 packet_index not in dropped_packet_indexes,
1888 ppp("Packet received, but should be dropped:", packet),
1890 if packet_index in seen:
1891 raise Exception(ppp("Duplicate packet received", packet))
1892 seen.add(packet_index)
1893 self.assertEqual(payload_info.dst, self.src_if.sw_if_index)
1894 info = self._packet_infos[packet_index]
1895 self.assertTrue(info is not None)
1896 self.assertEqual(packet_index, info.index)
1897 saved_packet = info.data
1898 self.assertEqual(ip.src, saved_packet[IPv6].src)
1899 self.assertEqual(ip.dst, saved_packet[IPv6].dst)
1900 self.assertEqual(udp.payload, saved_packet[UDP].payload)
1902 self.logger.error(ppp("Unexpected or invalid packet:", packet))
1904 for index in self._packet_infos:
1906 index in seen or index in dropped_packet_indexes,
1907 "Packet with packet_index %d not received" % index,
1910 def send_packets(self, packets):
1911 for counter in range(self.vpp_worker_count):
1912 if 0 == len(packets[counter]):
1914 send_if = self.send_ifs[counter]
1917 Ether(dst=send_if.local_mac, src=send_if.remote_mac) / x
1918 for x in packets[counter]
1924 def test_worker_conflict(self):
1925 """1st and FO=0 fragments on different workers"""
1927 # in first wave we send fragments which don't start at offset 0
1928 # then we send fragments with offset 0 on a different thread
1929 # then the rest of packets on a random thread
1930 first_packets = [[] for n in range(self.vpp_worker_count)]
1931 second_packets = [[] for n in range(self.vpp_worker_count)]
1932 rest_of_packets = [[] for n in range(self.vpp_worker_count)]
1933 for _, p in self.pkt_infos:
1934 wi = randrange(self.vpp_worker_count)
1935 second_packets[wi].append(p[0])
1940 wi2 = randrange(self.vpp_worker_count)
1941 first_packets[wi2].append(p[1])
1942 wi3 = randrange(self.vpp_worker_count)
1943 rest_of_packets[wi3].extend(p[2:])
1945 self.pg_enable_capture()
1946 self.send_packets(first_packets)
1947 self.send_packets(second_packets)
1948 self.send_packets(rest_of_packets)
1950 packets = self.dst_if.get_capture(len(self.pkt_infos))
1951 self.verify_capture(packets)
1952 for send_if in self.send_ifs:
1953 send_if.assert_nothing_captured()
1955 self.logger.debug(self.vapi.ppcli("show trace"))
1956 self.logger.debug(self.vapi.ppcli("show ip6-full-reassembly details"))
1957 self.logger.debug(self.vapi.ppcli("show buffers"))
1958 self.vapi.cli("clear trace")
1960 self.pg_enable_capture()
1961 self.send_packets(first_packets)
1962 self.send_packets(second_packets)
1963 self.send_packets(rest_of_packets)
1965 packets = self.dst_if.get_capture(len(self.pkt_infos))
1966 self.verify_capture(packets)
1967 for send_if in self.send_ifs:
1968 send_if.assert_nothing_captured()
1971 class TestIPv6SVReassembly(VppTestCase):
1972 """IPv6 Shallow Virtual Reassembly"""
1975 def setUpClass(cls):
1976 super().setUpClass()
1978 cls.create_pg_interfaces([0, 1])
1979 cls.src_if = cls.pg0
1980 cls.dst_if = cls.pg1
1982 # setup all interfaces
1983 for i in cls.pg_interfaces:
1989 """Test setup - force timeout on existing reassemblies"""
1991 self.vapi.ip_reassembly_enable_disable(
1992 sw_if_index=self.src_if.sw_if_index,
1994 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
1996 self.vapi.ip_reassembly_set(
1998 max_reassemblies=1000,
1999 max_reassembly_length=1000,
2000 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
2001 expire_walk_interval_ms=10,
2004 self.virtual_sleep(0.25)
2005 self.vapi.ip_reassembly_set(
2007 max_reassemblies=1000,
2008 max_reassembly_length=1000,
2009 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
2010 expire_walk_interval_ms=10000,
2016 self.logger.debug(self.vapi.ppcli("show ip6-sv-reassembly details"))
2017 self.logger.debug(self.vapi.ppcli("show buffers"))
2019 def test_basic(self):
2020 """basic reassembly"""
2024 while len(payload) < payload_len:
2025 payload += "%u " % counter
2029 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
2030 / IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6)
2031 / UDP(sport=1234, dport=5678)
2034 fragments = fragment_rfc8200(p, 1, payload_len / 4)
2036 # send fragment #2 - should be cached inside reassembly
2037 self.pg_enable_capture()
2038 self.src_if.add_stream(fragments[1])
2040 self.logger.debug(self.vapi.ppcli("show ip6-sv-reassembly details"))
2041 self.logger.debug(self.vapi.ppcli("show buffers"))
2042 self.logger.debug(self.vapi.ppcli("show trace"))
2043 self.dst_if.assert_nothing_captured()
2045 # send fragment #1 - reassembly is finished now and both fragments
2047 self.pg_enable_capture()
2048 self.src_if.add_stream(fragments[0])
2050 self.logger.debug(self.vapi.ppcli("show ip6-sv-reassembly details"))
2051 self.logger.debug(self.vapi.ppcli("show buffers"))
2052 self.logger.debug(self.vapi.ppcli("show trace"))
2053 c = self.dst_if.get_capture(2)
2054 for sent, recvd in zip([fragments[1], fragments[0]], c):
2055 self.assertEqual(sent[IPv6].src, recvd[IPv6].src)
2056 self.assertEqual(sent[IPv6].dst, recvd[IPv6].dst)
2057 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
2059 # send rest of fragments - should be immediately forwarded
2060 self.pg_enable_capture()
2061 self.src_if.add_stream(fragments[2:])
2063 c = self.dst_if.get_capture(len(fragments[2:]))
2064 for sent, recvd in zip(fragments[2:], c):
2065 self.assertEqual(sent[IPv6].src, recvd[IPv6].src)
2066 self.assertEqual(sent[IPv6].dst, recvd[IPv6].dst)
2067 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
2069 def test_verify_clear_trace_mid_reassembly(self):
2070 """verify clear trace works mid-reassembly"""
2074 while len(payload) < payload_len:
2075 payload += "%u " % counter
2079 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
2080 / IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6)
2081 / UDP(sport=1234, dport=5678)
2084 fragments = fragment_rfc8200(p, 1, payload_len / 4)
2086 self.pg_enable_capture()
2087 self.src_if.add_stream(fragments[1])
2090 self.logger.debug(self.vapi.cli("show trace"))
2091 self.vapi.cli("clear trace")
2093 self.pg_enable_capture()
2094 self.src_if.add_stream(fragments[0])
2096 self.dst_if.get_capture(2)
2098 self.logger.debug(self.vapi.cli("show trace"))
2099 self.vapi.cli("clear trace")
2101 self.pg_enable_capture()
2102 self.src_if.add_stream(fragments[2:])
2104 self.dst_if.get_capture(len(fragments[2:]))
2106 def test_timeout(self):
2107 """reassembly timeout"""
2111 while len(payload) < payload_len:
2112 payload += "%u " % counter
2116 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
2117 / IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6)
2118 / UDP(sport=1234, dport=5678)
2121 fragments = fragment_rfc8200(p, 1, payload_len / 4)
2123 self.vapi.ip_reassembly_set(
2125 max_reassemblies=1000,
2126 max_reassembly_length=1000,
2127 expire_walk_interval_ms=50,
2129 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
2132 # send fragments #2 and #1 - should be forwarded
2133 self.pg_enable_capture()
2134 self.src_if.add_stream(fragments[0:2])
2136 self.logger.debug(self.vapi.ppcli("show ip4-sv-reassembly details"))
2137 self.logger.debug(self.vapi.ppcli("show buffers"))
2138 self.logger.debug(self.vapi.ppcli("show trace"))
2139 c = self.dst_if.get_capture(2)
2140 for sent, recvd in zip([fragments[1], fragments[0]], c):
2141 self.assertEqual(sent[IPv6].src, recvd[IPv6].src)
2142 self.assertEqual(sent[IPv6].dst, recvd[IPv6].dst)
2143 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
2146 self.virtual_sleep(0.25, "wait before sending rest of fragments")
2148 # send rest of fragments - shouldn't be forwarded
2149 self.pg_enable_capture()
2150 self.src_if.add_stream(fragments[2:])
2152 self.dst_if.assert_nothing_captured()
2155 """reassembly reuses LRU element"""
2157 self.vapi.ip_reassembly_set(
2160 max_reassembly_length=1000,
2161 type=VppEnum.vl_api_ip_reass_type_t.IP_REASS_TYPE_SHALLOW_VIRTUAL,
2163 expire_walk_interval_ms=10000,
2169 while len(payload) < payload_len:
2170 payload += "%u " % counter
2177 for i in range(packet_count)
2179 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
2180 / IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6)
2181 / UDP(sport=1234, dport=5678)
2184 for f in fragment_rfc8200(p, i, payload_len / 4)
2187 self.pg_enable_capture()
2188 self.src_if.add_stream(fragments)
2190 c = self.dst_if.get_capture(len(fragments))
2191 for sent, recvd in zip(fragments, c):
2192 self.assertEqual(sent[IPv6].src, recvd[IPv6].src)
2193 self.assertEqual(sent[IPv6].dst, recvd[IPv6].dst)
2194 self.assertEqual(sent[Raw].payload, recvd[Raw].payload)
2196 def test_one_fragment(self):
2197 """whole packet in one fragment processed independently"""
2199 Ether(src=self.src_if.local_mac, dst=self.src_if.remote_mac)
2200 / IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6)
2201 / ICMPv6EchoRequest()
2204 frags = fragment_rfc8200(pkt, 1, 400)
2206 # send a fragment with known id
2207 self.send_and_expect(self.src_if, [frags[0]], self.dst_if)
2209 # send an atomic fragment with same id - should be reassembled
2211 Ether(src=self.src_if.local_mac, dst=self.src_if.remote_mac)
2212 / IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6)
2213 / IPv6ExtHdrFragment(id=1)
2214 / ICMPv6EchoRequest()
2216 rx = self.send_and_expect(self.src_if, [pkt], self.dst_if)
2218 # now forward packets matching original reassembly, should still work
2219 rx = self.send_and_expect(self.src_if, frags[1:], self.dst_if)
2221 def test_bunch_of_fragments(self):
2222 """valid fragments followed by rogue fragments and atomic fragment"""
2224 Ether(src=self.src_if.local_mac, dst=self.src_if.remote_mac)
2225 / IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6)
2226 / ICMPv6EchoRequest()
2229 frags = fragment_rfc8200(pkt, 1, 400)
2230 rx = self.send_and_expect(self.src_if, frags, self.dst_if)
2233 Ether(src=self.src_if.local_mac, dst=self.src_if.remote_mac)
2234 / IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6)
2235 / IPv6ExtHdrFragment(id=1, nh=58, offset=608)
2239 self.send_and_expect(self.src_if, rogue * 604, self.dst_if)
2242 Ether(src=self.src_if.local_mac, dst=self.src_if.remote_mac)
2243 / IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6)
2244 / IPv6ExtHdrFragment(id=1)
2245 / ICMPv6EchoRequest()
2247 rx = self.send_and_expect(self.src_if, [pkt], self.dst_if)
2249 def test_truncated_fragment(self):
2250 """truncated fragment"""
2252 Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac)
2253 / IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6, nh=44, plen=2)
2254 / IPv6ExtHdrFragment(nh=6)
2257 self.send_and_assert_no_replies(self.pg0, [pkt], self.pg0)
2260 class TestIPv4ReassemblyLocalNode(VppTestCase):
2261 """IPv4 Reassembly for packets coming to ip4-local node"""
2264 def setUpClass(cls):
2265 super().setUpClass()
2267 cls.create_pg_interfaces([0])
2268 cls.src_dst_if = cls.pg0
2270 # setup all interfaces
2271 for i in cls.pg_interfaces:
2276 cls.padding = " abcdefghijklmn"
2278 cls.create_fragments()
2281 def tearDownClass(cls):
2282 super().tearDownClass()
2285 """Test setup - force timeout on existing reassemblies"""
2287 self.vapi.ip_reassembly_set(
2289 max_reassemblies=1000,
2290 max_reassembly_length=1000,
2291 expire_walk_interval_ms=10,
2293 self.virtual_sleep(0.25)
2294 self.vapi.ip_reassembly_set(
2296 max_reassemblies=1000,
2297 max_reassembly_length=1000,
2298 expire_walk_interval_ms=10000,
2304 def show_commands_at_teardown(self):
2305 self.logger.debug(self.vapi.ppcli("show ip4-full-reassembly details"))
2306 self.logger.debug(self.vapi.ppcli("show buffers"))
2309 def create_stream(cls, packet_count=test_packet_count):
2310 """Create input packet stream for defined interface.
2312 :param list packet_sizes: Required packet sizes.
2314 for i in range(0, packet_count):
2315 info = cls.create_packet_info(cls.src_dst_if, cls.src_dst_if)
2316 payload = cls.info_to_payload(info)
2318 Ether(dst=cls.src_dst_if.local_mac, src=cls.src_dst_if.remote_mac)
2321 src=cls.src_dst_if.remote_ip4,
2322 dst=cls.src_dst_if.local_ip4,
2324 / ICMP(type="echo-request", id=1234)
2327 cls.extend_packet(p, 1518, cls.padding)
2331 def create_fragments(cls):
2332 infos = cls._packet_infos
2334 for index, info in infos.items():
2336 # cls.logger.debug(ppp("Packet:",
2337 # p.__class__(scapy.compat.raw(p))))
2338 fragments_300 = fragment_rfc791(p, 300)
2339 cls.pkt_infos.append((index, fragments_300))
2340 cls.fragments_300 = [x for (_, frags) in cls.pkt_infos for x in frags]
2342 "Fragmented %s packets into %s 300-byte fragments"
2343 % (len(infos), len(cls.fragments_300))
2346 def verify_capture(self, capture):
2347 """Verify captured packet stream.
2349 :param list capture: Captured packet stream.
2353 for packet in capture:
2355 self.logger.debug(ppp("Got packet:", packet))
2358 payload_info = self.payload_to_info(packet[Raw])
2359 packet_index = payload_info.index
2360 if packet_index in seen:
2361 raise Exception(ppp("Duplicate packet received", packet))
2362 seen.add(packet_index)
2363 self.assertEqual(payload_info.dst, self.src_dst_if.sw_if_index)
2364 info = self._packet_infos[packet_index]
2365 self.assertIsNotNone(info)
2366 self.assertEqual(packet_index, info.index)
2367 saved_packet = info.data
2368 self.assertEqual(ip.src, saved_packet[IP].dst)
2369 self.assertEqual(ip.dst, saved_packet[IP].src)
2370 self.assertEqual(icmp.type, 0) # echo reply
2371 self.assertEqual(icmp.id, saved_packet[ICMP].id)
2372 self.assertEqual(icmp.payload, saved_packet[ICMP].payload)
2374 self.logger.error(ppp("Unexpected or invalid packet:", packet))
2376 for index in self._packet_infos:
2378 index, seen, "Packet with packet_index %d not received" % index
2381 def test_reassembly(self):
2382 """basic reassembly"""
2384 self.pg_enable_capture()
2385 self.src_dst_if.add_stream(self.fragments_300)
2388 packets = self.src_dst_if.get_capture(len(self.pkt_infos))
2389 self.verify_capture(packets)
2391 # run it all again to verify correctness
2392 self.pg_enable_capture()
2393 self.src_dst_if.add_stream(self.fragments_300)
2396 packets = self.src_dst_if.get_capture(len(self.pkt_infos))
2397 self.verify_capture(packets)
2400 class TestFIFReassembly(VppTestCase):
2401 """Fragments in fragments reassembly"""
2404 def setUpClass(cls):
2405 super().setUpClass()
2407 cls.create_pg_interfaces([0, 1])
2408 cls.src_if = cls.pg0
2409 cls.dst_if = cls.pg1
2410 for i in cls.pg_interfaces:
2417 cls.packet_sizes = [64, 512, 1518, 9018]
2418 cls.padding = " abcdefghijklmn"
2421 def tearDownClass(cls):
2422 super().tearDownClass()
2425 """Test setup - force timeout on existing reassemblies"""
2427 self.vapi.ip_reassembly_enable_disable(
2428 sw_if_index=self.src_if.sw_if_index, enable_ip4=True, enable_ip6=True
2430 self.vapi.ip_reassembly_enable_disable(
2431 sw_if_index=self.dst_if.sw_if_index, enable_ip4=True, enable_ip6=True
2433 self.vapi.ip_reassembly_set(
2435 max_reassemblies=1000,
2436 max_reassembly_length=1000,
2437 expire_walk_interval_ms=10,
2439 self.vapi.ip_reassembly_set(
2441 max_reassemblies=1000,
2442 max_reassembly_length=1000,
2443 expire_walk_interval_ms=10,
2446 self.virtual_sleep(0.25)
2447 self.vapi.ip_reassembly_set(
2449 max_reassemblies=1000,
2450 max_reassembly_length=1000,
2451 expire_walk_interval_ms=10000,
2453 self.vapi.ip_reassembly_set(
2455 max_reassemblies=1000,
2456 max_reassembly_length=1000,
2457 expire_walk_interval_ms=10000,
2464 def show_commands_at_teardown(self):
2465 self.logger.debug(self.vapi.ppcli("show ip4-full-reassembly details"))
2466 self.logger.debug(self.vapi.ppcli("show ip6-full-reassembly details"))
2467 self.logger.debug(self.vapi.ppcli("show buffers"))
2469 def verify_capture(self, capture, ip_class, dropped_packet_indexes=[]):
2470 """Verify captured packet stream.
2472 :param list capture: Captured packet stream.
2476 for packet in capture:
2478 self.logger.debug(ppp("Got packet:", packet))
2479 ip = packet[ip_class]
2481 payload_info = self.payload_to_info(packet[Raw])
2482 packet_index = payload_info.index
2484 packet_index not in dropped_packet_indexes,
2485 ppp("Packet received, but should be dropped:", packet),
2487 if packet_index in seen:
2488 raise Exception(ppp("Duplicate packet received", packet))
2489 seen.add(packet_index)
2490 self.assertEqual(payload_info.dst, self.dst_if.sw_if_index)
2491 info = self._packet_infos[packet_index]
2492 self.assertTrue(info is not None)
2493 self.assertEqual(packet_index, info.index)
2494 saved_packet = info.data
2495 self.assertEqual(ip.src, saved_packet[ip_class].src)
2496 self.assertEqual(ip.dst, saved_packet[ip_class].dst)
2497 self.assertEqual(udp.payload, saved_packet[UDP].payload)
2499 self.logger.error(ppp("Unexpected or invalid packet:", packet))
2501 for index in self._packet_infos:
2503 index in seen or index in dropped_packet_indexes,
2504 "Packet with packet_index %d not received" % index,
2507 def test_fif4(self):
2508 """Fragments in fragments (4o4)"""
2510 # TODO this should be ideally in setUpClass, but then we hit a bug
2511 # with VppIpRoute incorrectly reporting it's present when it's not
2512 # so we need to manually remove the vpp config, thus we cannot have
2513 # it shared for multiple test cases
2514 self.tun_ip4 = "1.1.1.2"
2516 self.gre4 = VppGreInterface(self, self.src_if.local_ip4, self.tun_ip4)
2517 self.gre4.add_vpp_config()
2518 self.gre4.admin_up()
2519 self.gre4.config_ip4()
2521 self.vapi.ip_reassembly_enable_disable(
2522 sw_if_index=self.gre4.sw_if_index, enable_ip4=True
2525 self.route4 = VppIpRoute(
2529 [VppRoutePath(self.src_if.remote_ip4, self.src_if.sw_if_index)],
2531 self.route4.add_vpp_config()
2533 self.reset_packet_infos()
2534 for i in range(test_packet_count):
2535 info = self.create_packet_info(self.src_if, self.dst_if)
2536 payload = self.info_to_payload(info)
2537 # Ethernet header here is only for size calculation, thus it
2538 # doesn't matter how it's initialized. This is to ensure that
2539 # reassembled packet is not > 9000 bytes, so that it's not dropped
2542 / IP(id=i, src=self.src_if.remote_ip4, dst=self.dst_if.remote_ip4)
2543 / UDP(sport=1234, dport=5678)
2546 size = self.packet_sizes[(i // 2) % len(self.packet_sizes)]
2547 self.extend_packet(p, size, self.padding)
2548 info.data = p[IP] # use only IP part, without ethernet header
2552 for _, p in self._packet_infos.items()
2553 for x in fragment_rfc791(p.data, 400)
2556 encapped_fragments = [
2557 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
2558 / IP(src=self.tun_ip4, dst=self.src_if.local_ip4)
2564 fragmented_encapped_fragments = [
2565 x for p in encapped_fragments for x in fragment_rfc791(p, 200)
2568 self.src_if.add_stream(fragmented_encapped_fragments)
2570 self.pg_enable_capture(self.pg_interfaces)
2573 self.src_if.assert_nothing_captured()
2574 packets = self.dst_if.get_capture(len(self._packet_infos))
2575 self.verify_capture(packets, IP)
2577 # TODO remove gre vpp config by hand until VppIpRoute gets fixed
2578 # so that it's query_vpp_config() works as it should
2579 self.gre4.remove_vpp_config()
2580 self.logger.debug(self.vapi.ppcli("show interface"))
2582 def test_fif6(self):
2583 """Fragments in fragments (6o6)"""
2584 # TODO this should be ideally in setUpClass, but then we hit a bug
2585 # with VppIpRoute incorrectly reporting it's present when it's not
2586 # so we need to manually remove the vpp config, thus we cannot have
2587 # it shared for multiple test cases
2588 self.tun_ip6 = "1002::1"
2590 self.gre6 = VppGreInterface(self, self.src_if.local_ip6, self.tun_ip6)
2591 self.gre6.add_vpp_config()
2592 self.gre6.admin_up()
2593 self.gre6.config_ip6()
2595 self.vapi.ip_reassembly_enable_disable(
2596 sw_if_index=self.gre6.sw_if_index, enable_ip6=True
2599 self.route6 = VppIpRoute(
2603 [VppRoutePath(self.src_if.remote_ip6, self.src_if.sw_if_index)],
2605 self.route6.add_vpp_config()
2607 self.reset_packet_infos()
2608 for i in range(test_packet_count):
2609 info = self.create_packet_info(self.src_if, self.dst_if)
2610 payload = self.info_to_payload(info)
2611 # Ethernet header here is only for size calculation, thus it
2612 # doesn't matter how it's initialized. This is to ensure that
2613 # reassembled packet is not > 9000 bytes, so that it's not dropped
2616 / IPv6(src=self.src_if.remote_ip6, dst=self.dst_if.remote_ip6)
2617 / UDP(sport=1234, dport=5678)
2620 size = self.packet_sizes[(i // 2) % len(self.packet_sizes)]
2621 self.extend_packet(p, size, self.padding)
2622 info.data = p[IPv6] # use only IPv6 part, without ethernet header
2626 for _, i in self._packet_infos.items()
2627 for x in fragment_rfc8200(i.data, i.index, 400)
2630 encapped_fragments = [
2631 Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac)
2632 / IPv6(src=self.tun_ip6, dst=self.src_if.local_ip6)
2638 fragmented_encapped_fragments = [
2640 for p in encapped_fragments
2643 p, 2 * len(self._packet_infos) + p[IPv6ExtHdrFragment].id, 200
2645 if IPv6ExtHdrFragment in p
2650 self.src_if.add_stream(fragmented_encapped_fragments)
2652 self.pg_enable_capture(self.pg_interfaces)
2655 self.src_if.assert_nothing_captured()
2656 packets = self.dst_if.get_capture(len(self._packet_infos))
2657 self.verify_capture(packets, IPv6)
2659 # TODO remove gre vpp config by hand until VppIpRoute gets fixed
2660 # so that it's query_vpp_config() works as it should
2661 self.gre6.remove_vpp_config()
2664 if __name__ == "__main__":
2665 unittest.main(testRunner=VppTestRunner)