blob: 4159deb414e413938aa2e7577039fca013d4fd74 [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 = (
102 IP(src=self.pg0.local_ip4, dst=self.pg0.remote_ip4, ttl=254, len=576, id=0)
103 / 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
Ole Troan8a9c8f12018-05-18 11:01:31 +0200116 # Now with DF off. Expect fragments.
117 # First go with 1500 byte packets.
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200118 p_payload = UDP(sport=1234, dport=1234) / self.payload(1500 - 20 - 8)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200119 p4 = p_ether / p_ip4 / p_payload
120 p4.flags = 0
121 p4_reply = p_ip4 / p_payload
Neale Ranns0b6a8572019-10-30 17:34:14 +0000122 p4_reply.ttl = p_ip4.ttl - 1
Ole Troan8a9c8f12018-05-18 11:01:31 +0200123 p4_reply.flags = 0
124 p4_reply.id = 256
125 self.pg_enable_capture()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200126 self.pg0.add_stream(p4 * 1)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200127 self.pg_start()
128 rx = self.pg1.get_capture(3)
Ole Troan7f991832018-12-06 17:35:12 +0100129 reass_pkt = reassemble4(rx)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200130 self.validate(reass_pkt, p4_reply)
Ole Troan313f7e22018-04-10 16:02:51 +0200131
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200132 """
Ole Troan8a9c8f12018-05-18 11:01:31 +0200133 # Now what happens with a 9K frame
Ole Troan8a9c8f12018-05-18 11:01:31 +0200134 p_payload = UDP(sport=1234, dport=1234) / self.payload(
135 current_mtu - 20 - 8)
136 p4 = p_ether / p_ip4 / p_payload
137 p4.flags = 0
138 p4_reply = p_ip4 / p_payload
139 p4_reply.ttl = 62 # check this
140 p4_reply.flags = 0
141 p4_reply.id = 512
142
143 self.pg_enable_capture()
144 self.pg0.add_stream(p4*1)
145 self.pg_start()
146 rx = self.pg1.get_capture(16)
Ole Troan7f991832018-12-06 17:35:12 +0100147 reass_pkt = reassemble4(rx)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200148 reass_pkt.show2()
149 p4_reply.show2()
150 self.validate(reass_pkt, p4_reply)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200151 """
Ole Troan313f7e22018-04-10 16:02:51 +0200152
Ole Troan8a9c8f12018-05-18 11:01:31 +0200153 # Reset MTU
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200154 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [current_mtu, 0, 0, 0])
Ole Troan8a9c8f12018-05-18 11:01:31 +0200155
Ole Troan8a9c8f12018-05-18 11:01:31 +0200156 def test_ip6_mtu(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200157 """IP6 MTU test"""
Ole Troan8a9c8f12018-05-18 11:01:31 +0200158
Ole Troanda6e11b2018-05-23 11:21:42 +0200159 current_mtu = self.get_mtu(self.pg1.sw_if_index)
Ole Troanda6e11b2018-05-23 11:21:42 +0200160
Ole Troan8a9c8f12018-05-18 11:01:31 +0200161 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
162 p_ip6 = IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6)
163
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200164 p_payload = UDP(sport=1234, dport=1234) / self.payload(current_mtu - 40 - 8)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200165
166 p6 = p_ether / p_ip6 / p_payload
167 p6_reply = p_ip6 / p_payload
168 p6_reply.hlim -= 1
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200169 rx = self.send_and_expect(self.pg0, p6 * 9, self.pg1)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200170 for p in rx:
171 self.validate(p[1], p6_reply)
172
173 # MTU (only checked on encap)
Ole Troand7231612018-06-07 10:17:57 +0200174 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1280, 0, 0, 0])
175 self.assertEqual(1280, self.get_mtu(self.pg1.sw_if_index))
Ole Troan8a9c8f12018-05-18 11:01:31 +0200176
177 # Should fail. Too large MTU
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200178 p_icmp6 = ICMPv6PacketTooBig(mtu=1280, cksum=0x4C7A)
179 icmp6_reply = (
180 IPv6(src=self.pg0.local_ip6, dst=self.pg0.remote_ip6, hlim=255, plen=1240)
181 / p_icmp6
182 / p_ip6
183 / p_payload
184 )
Ole Troan8a9c8f12018-05-18 11:01:31 +0200185 icmp6_reply[2].hlim -= 1
Ole Troan7f991832018-12-06 17:35:12 +0100186 n = icmp6_reply.__class__(icmp6_reply)
187 s = bytes(icmp6_reply)
Ole Troan282093f2018-09-19 12:38:51 +0200188 icmp6_reply_str = s[0:1280]
Ole Troan8a9c8f12018-05-18 11:01:31 +0200189
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200190 rx = self.send_and_expect_some(self.pg0, p6 * 9, self.pg0)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200191 for p in rx:
Ole Troan7f991832018-12-06 17:35:12 +0100192 self.validate_bytes(bytes(p[1]), icmp6_reply_str)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200193
194 # Reset MTU
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200195 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [current_mtu, 0, 0, 0])
Ole Troan8a9c8f12018-05-18 11:01:31 +0200196
197
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200198if __name__ == "__main__":
Ole Troan8a9c8f12018-05-18 11:01:31 +0200199 unittest.main(testRunner=VppTestRunner)