blob: 6735cc602a8bfa54405d0d0e134d73877086f11b [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Ole Troan8a9c8f12018-05-18 11:01:31 +02002"""IP4 and IP6 MTU functional tests"""
3
4#
5# Add tests for:
6# - sub interfaces
7# - Verify that adjacencies inherit MTU correctly
8# - Verify that sub-interfaces inherit MTU correctly
9# - Different types of interfaces?
10#
11import unittest
12from scapy.layers.inet6 import IPv6, Ether, IP, UDP, ICMPv6PacketTooBig
13from scapy.layers.inet import ICMP
Dave Wallace8800f732023-08-31 00:47:44 -040014from framework import VppTestCase
15from asfframework import VppTestRunner
Ole Troan7f991832018-12-06 17:35:12 +010016from util import reassemble4
17
Ole Troan8a9c8f12018-05-18 11:01:31 +020018
19""" Test_mtu is a subclass of VPPTestCase classes.
20 MTU tests.
21"""
22
23
Ole Troan8a9c8f12018-05-18 11:01:31 +020024class TestMTU(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020025 """MTU Test Case"""
26
Ole Troan7f991832018-12-06 17:35:12 +010027 maxDiff = None
Ole Troan8a9c8f12018-05-18 11:01:31 +020028
29 @classmethod
30 def setUpClass(cls):
31 super(TestMTU, cls).setUpClass()
32 cls.create_pg_interfaces(range(2))
33 cls.interfaces = list(cls.pg_interfaces)
34
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070035 @classmethod
36 def tearDownClass(cls):
37 super(TestMTU, cls).tearDownClass()
38
Paul Vinciguerra0c794532018-11-24 22:02:58 -080039 def setUp(self):
40 super(TestMTU, self).setUp()
41 for i in self.interfaces:
Ole Troan8a9c8f12018-05-18 11:01:31 +020042 i.admin_up()
43 i.config_ip4()
44 i.config_ip6()
45 i.disable_ipv6_ra()
46 i.resolve_arp()
47 i.resolve_ndp()
48
49 def tearDown(self):
50 super(TestMTU, self).tearDown()
51 if not self.vpp_dead:
52 for i in self.pg_interfaces:
53 i.unconfig_ip4()
54 i.unconfig_ip6()
55 i.admin_down()
56
57 def validate(self, rx, expected):
Ole Troan7f991832018-12-06 17:35:12 +010058 self.assertEqual(rx, expected.__class__(expected))
Ole Troan8a9c8f12018-05-18 11:01:31 +020059
60 def validate_bytes(self, rx, expected):
61 self.assertEqual(rx, expected)
62
63 def payload(self, len):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020064 return "x" * len
Ole Troan8a9c8f12018-05-18 11:01:31 +020065
66 def get_mtu(self, sw_if_index):
Paul Vinciguerra7a998232019-06-07 15:01:12 -040067 rv = self.vapi.sw_interface_dump(sw_if_index=sw_if_index)
Ole Troan8a9c8f12018-05-18 11:01:31 +020068 for i in rv:
69 if i.sw_if_index == sw_if_index:
Ole Troand7231612018-06-07 10:17:57 +020070 return i.mtu[0]
Ole Troan8a9c8f12018-05-18 11:01:31 +020071 return 0
72
73 def test_ip4_mtu(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020074 """IP4 MTU test"""
Ole Troan8a9c8f12018-05-18 11:01:31 +020075
Ole Troan8a9c8f12018-05-18 11:01:31 +020076 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020077 p_ip4 = IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, flags="DF")
Ole Troan8a9c8f12018-05-18 11:01:31 +020078
Ole Troan8a9c8f12018-05-18 11:01:31 +020079 current_mtu = self.get_mtu(self.pg1.sw_if_index)
Ole Troan8a9c8f12018-05-18 11:01:31 +020080
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020081 p_payload = UDP(sport=1234, dport=1234) / self.payload(current_mtu - 20 - 8)
Ole Troan8a9c8f12018-05-18 11:01:31 +020082
83 p4 = p_ether / p_ip4 / p_payload
84 p4_reply = p_ip4 / p_payload
85 p4_reply.ttl -= 1
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020086 rx = self.send_and_expect(self.pg0, p4 * 11, self.pg1)
Ole Troan8a9c8f12018-05-18 11:01:31 +020087 for p in rx:
88 self.validate(p[1], p4_reply)
89
90 # MTU
Ole Troand7231612018-06-07 10:17:57 +020091 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [576, 0, 0, 0])
92 self.assertEqual(576, self.get_mtu(self.pg1.sw_if_index))
Ole Troan8a9c8f12018-05-18 11:01:31 +020093
94 # Should fail. Too large MTU
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020095 p_icmp4 = ICMP(
96 type="dest-unreach",
97 code="fragmentation-needed",
98 nexthopmtu=576,
99 chksum=0x2DBB,
100 )
101 icmp4_reply = (
Maxime Peim2cc14de2024-02-12 10:08:03 +0100102 IP(src=self.pg0.local_ip4, dst=self.pg0.remote_ip4, ttl=255, len=576, id=0)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200103 / p_icmp4
104 / p_ip4
105 / p_payload
106 )
Ole Troan7f991832018-12-06 17:35:12 +0100107 n = icmp4_reply.__class__(icmp4_reply)
108 s = bytes(icmp4_reply)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200109 icmp4_reply = s[0:576]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200110 rx = self.send_and_expect_some(self.pg0, p4 * 11, self.pg0)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200111 for p in rx:
112 # p.show2()
113 # n.show2()
Ole Troan7f991832018-12-06 17:35:12 +0100114 self.validate_bytes(bytes(p[1]), icmp4_reply)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200115
Arthur de Kerhoraf1ddd32024-01-08 19:30:57 +0100116 self.assert_error_counter_equal("/err/ip4-input/mtu_exceeded", 11)
117
Ole Troan8a9c8f12018-05-18 11:01:31 +0200118 # Now with DF off. Expect fragments.
119 # First go with 1500 byte packets.
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200120 p_payload = UDP(sport=1234, dport=1234) / self.payload(1500 - 20 - 8)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200121 p4 = p_ether / p_ip4 / p_payload
122 p4.flags = 0
123 p4_reply = p_ip4 / p_payload
Neale Ranns0b6a8572019-10-30 17:34:14 +0000124 p4_reply.ttl = p_ip4.ttl - 1
Ole Troan8a9c8f12018-05-18 11:01:31 +0200125 p4_reply.flags = 0
126 p4_reply.id = 256
127 self.pg_enable_capture()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200128 self.pg0.add_stream(p4 * 1)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200129 self.pg_start()
130 rx = self.pg1.get_capture(3)
Ole Troan7f991832018-12-06 17:35:12 +0100131 reass_pkt = reassemble4(rx)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200132 self.validate(reass_pkt, p4_reply)
Ole Troan313f7e22018-04-10 16:02:51 +0200133
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200134 """
Ole Troan8a9c8f12018-05-18 11:01:31 +0200135 # Now what happens with a 9K frame
Ole Troan8a9c8f12018-05-18 11:01:31 +0200136 p_payload = UDP(sport=1234, dport=1234) / self.payload(
137 current_mtu - 20 - 8)
138 p4 = p_ether / p_ip4 / p_payload
139 p4.flags = 0
140 p4_reply = p_ip4 / p_payload
141 p4_reply.ttl = 62 # check this
142 p4_reply.flags = 0
143 p4_reply.id = 512
144
145 self.pg_enable_capture()
146 self.pg0.add_stream(p4*1)
147 self.pg_start()
148 rx = self.pg1.get_capture(16)
Ole Troan7f991832018-12-06 17:35:12 +0100149 reass_pkt = reassemble4(rx)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200150 reass_pkt.show2()
151 p4_reply.show2()
152 self.validate(reass_pkt, p4_reply)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200153 """
Ole Troan313f7e22018-04-10 16:02:51 +0200154
Ole Troan8a9c8f12018-05-18 11:01:31 +0200155 # Reset MTU
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200156 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [current_mtu, 0, 0, 0])
Ole Troan8a9c8f12018-05-18 11:01:31 +0200157
Ole Troan8a9c8f12018-05-18 11:01:31 +0200158 def test_ip6_mtu(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200159 """IP6 MTU test"""
Ole Troan8a9c8f12018-05-18 11:01:31 +0200160
Ole Troanda6e11b2018-05-23 11:21:42 +0200161 current_mtu = self.get_mtu(self.pg1.sw_if_index)
Ole Troanda6e11b2018-05-23 11:21:42 +0200162
Ole Troan8a9c8f12018-05-18 11:01:31 +0200163 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
164 p_ip6 = IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6)
165
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200166 p_payload = UDP(sport=1234, dport=1234) / self.payload(current_mtu - 40 - 8)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200167
168 p6 = p_ether / p_ip6 / p_payload
169 p6_reply = p_ip6 / p_payload
170 p6_reply.hlim -= 1
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200171 rx = self.send_and_expect(self.pg0, p6 * 9, self.pg1)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200172 for p in rx:
173 self.validate(p[1], p6_reply)
174
175 # MTU (only checked on encap)
Ole Troand7231612018-06-07 10:17:57 +0200176 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1280, 0, 0, 0])
177 self.assertEqual(1280, self.get_mtu(self.pg1.sw_if_index))
Ole Troan8a9c8f12018-05-18 11:01:31 +0200178
179 # Should fail. Too large MTU
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200180 p_icmp6 = ICMPv6PacketTooBig(mtu=1280, cksum=0x4C7A)
181 icmp6_reply = (
182 IPv6(src=self.pg0.local_ip6, dst=self.pg0.remote_ip6, hlim=255, plen=1240)
183 / p_icmp6
184 / p_ip6
185 / p_payload
186 )
Ole Troan8a9c8f12018-05-18 11:01:31 +0200187 icmp6_reply[2].hlim -= 1
Ole Troan7f991832018-12-06 17:35:12 +0100188 n = icmp6_reply.__class__(icmp6_reply)
189 s = bytes(icmp6_reply)
Ole Troan282093f2018-09-19 12:38:51 +0200190 icmp6_reply_str = s[0:1280]
Ole Troan8a9c8f12018-05-18 11:01:31 +0200191
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200192 rx = self.send_and_expect_some(self.pg0, p6 * 9, self.pg0)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200193 for p in rx:
Ole Troan7f991832018-12-06 17:35:12 +0100194 self.validate_bytes(bytes(p[1]), icmp6_reply_str)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200195
Arthur de Kerhoraf1ddd32024-01-08 19:30:57 +0100196 self.assert_error_counter_equal("/err/ip6-input/mtu_exceeded", 9)
197
Ole Troan8a9c8f12018-05-18 11:01:31 +0200198 # Reset MTU
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200199 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [current_mtu, 0, 0, 0])
Ole Troan8a9c8f12018-05-18 11:01:31 +0200200
201
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200202if __name__ == "__main__":
Ole Troan8a9c8f12018-05-18 11:01:31 +0200203 unittest.main(testRunner=VppTestRunner)