blob: 922d83dc5ef7f1a76b33ba2d4cc33d413b31b8bc [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
14from framework import VppTestCase, VppTestRunner
Neale Rannsc0a93142018-09-05 15:42:26 -070015from vpp_ip import DpoProto
Neale Ranns097fa662018-05-01 05:17:55 -070016from vpp_ip_route import VppIpRoute, VppRoutePath, FibPathProto
Ole Troan8a9c8f12018-05-18 11:01:31 +020017from socket import AF_INET, AF_INET6, inet_pton
Ole Troan7f991832018-12-06 17:35:12 +010018from util import reassemble4
19
Ole Troan8a9c8f12018-05-18 11:01:31 +020020
21""" Test_mtu is a subclass of VPPTestCase classes.
22 MTU tests.
23"""
24
25
Ole Troan8a9c8f12018-05-18 11:01:31 +020026class TestMTU(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020027 """MTU Test Case"""
28
Ole Troan7f991832018-12-06 17:35:12 +010029 maxDiff = None
Ole Troan8a9c8f12018-05-18 11:01:31 +020030
31 @classmethod
32 def setUpClass(cls):
33 super(TestMTU, cls).setUpClass()
34 cls.create_pg_interfaces(range(2))
35 cls.interfaces = list(cls.pg_interfaces)
36
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070037 @classmethod
38 def tearDownClass(cls):
39 super(TestMTU, cls).tearDownClass()
40
Paul Vinciguerra0c794532018-11-24 22:02:58 -080041 def setUp(self):
42 super(TestMTU, self).setUp()
43 for i in self.interfaces:
Ole Troan8a9c8f12018-05-18 11:01:31 +020044 i.admin_up()
45 i.config_ip4()
46 i.config_ip6()
47 i.disable_ipv6_ra()
48 i.resolve_arp()
49 i.resolve_ndp()
50
51 def tearDown(self):
52 super(TestMTU, self).tearDown()
53 if not self.vpp_dead:
54 for i in self.pg_interfaces:
55 i.unconfig_ip4()
56 i.unconfig_ip6()
57 i.admin_down()
58
59 def validate(self, rx, expected):
Ole Troan7f991832018-12-06 17:35:12 +010060 self.assertEqual(rx, expected.__class__(expected))
Ole Troan8a9c8f12018-05-18 11:01:31 +020061
62 def validate_bytes(self, rx, expected):
63 self.assertEqual(rx, expected)
64
65 def payload(self, len):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020066 return "x" * len
Ole Troan8a9c8f12018-05-18 11:01:31 +020067
68 def get_mtu(self, sw_if_index):
Paul Vinciguerra7a998232019-06-07 15:01:12 -040069 rv = self.vapi.sw_interface_dump(sw_if_index=sw_if_index)
Ole Troan8a9c8f12018-05-18 11:01:31 +020070 for i in rv:
71 if i.sw_if_index == sw_if_index:
Ole Troand7231612018-06-07 10:17:57 +020072 return i.mtu[0]
Ole Troan8a9c8f12018-05-18 11:01:31 +020073 return 0
74
75 def test_ip4_mtu(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020076 """IP4 MTU test"""
Ole Troan8a9c8f12018-05-18 11:01:31 +020077
Ole Troan8a9c8f12018-05-18 11:01:31 +020078 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020079 p_ip4 = IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, flags="DF")
Ole Troan8a9c8f12018-05-18 11:01:31 +020080
Ole Troan8a9c8f12018-05-18 11:01:31 +020081 current_mtu = self.get_mtu(self.pg1.sw_if_index)
Ole Troan8a9c8f12018-05-18 11:01:31 +020082
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020083 p_payload = UDP(sport=1234, dport=1234) / self.payload(current_mtu - 20 - 8)
Ole Troan8a9c8f12018-05-18 11:01:31 +020084
85 p4 = p_ether / p_ip4 / p_payload
86 p4_reply = p_ip4 / p_payload
87 p4_reply.ttl -= 1
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020088 rx = self.send_and_expect(self.pg0, p4 * 11, self.pg1)
Ole Troan8a9c8f12018-05-18 11:01:31 +020089 for p in rx:
90 self.validate(p[1], p4_reply)
91
92 # MTU
Ole Troand7231612018-06-07 10:17:57 +020093 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [576, 0, 0, 0])
94 self.assertEqual(576, self.get_mtu(self.pg1.sw_if_index))
Ole Troan8a9c8f12018-05-18 11:01:31 +020095
96 # Should fail. Too large MTU
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020097 p_icmp4 = ICMP(
98 type="dest-unreach",
99 code="fragmentation-needed",
100 nexthopmtu=576,
101 chksum=0x2DBB,
102 )
103 icmp4_reply = (
104 IP(src=self.pg0.local_ip4, dst=self.pg0.remote_ip4, ttl=254, len=576, id=0)
105 / p_icmp4
106 / p_ip4
107 / p_payload
108 )
Ole Troan7f991832018-12-06 17:35:12 +0100109 n = icmp4_reply.__class__(icmp4_reply)
110 s = bytes(icmp4_reply)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200111 icmp4_reply = s[0:576]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200112 rx = self.send_and_expect_some(self.pg0, p4 * 11, self.pg0)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200113 for p in rx:
114 # p.show2()
115 # n.show2()
Ole Troan7f991832018-12-06 17:35:12 +0100116 self.validate_bytes(bytes(p[1]), icmp4_reply)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200117
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
196 # Reset MTU
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200197 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [current_mtu, 0, 0, 0])
Ole Troan8a9c8f12018-05-18 11:01:31 +0200198
199
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200200if __name__ == "__main__":
Ole Troan8a9c8f12018-05-18 11:01:31 +0200201 unittest.main(testRunner=VppTestRunner)