2 """IP4 and IP6 MTU functional tests"""
7 # - Verify that adjacencies inherit MTU correctly
8 # - Verify that sub-interfaces inherit MTU correctly
9 # - Different types of interfaces?
12 from scapy.layers.inet6 import IPv6, Ether, IP, UDP, ICMPv6PacketTooBig
13 from scapy.layers.inet import ICMP
14 from framework import VppTestCase, VppTestRunner
15 from vpp_ip import DpoProto
16 from vpp_ip_route import VppIpRoute, VppRoutePath, FibPathProto
17 from socket import AF_INET, AF_INET6, inet_pton
18 from util import reassemble4
21 """ Test_mtu is a subclass of VPPTestCase classes.
26 class TestMTU(VppTestCase):
32 super(TestMTU, cls).setUpClass()
33 cls.create_pg_interfaces(range(2))
34 cls.interfaces = list(cls.pg_interfaces)
37 def tearDownClass(cls):
38 super(TestMTU, cls).tearDownClass()
41 super(TestMTU, self).setUp()
42 for i in self.interfaces:
51 super(TestMTU, self).tearDown()
53 for i in self.pg_interfaces:
58 def validate(self, rx, expected):
59 self.assertEqual(rx, expected.__class__(expected))
61 def validate_bytes(self, rx, expected):
62 self.assertEqual(rx, expected)
64 def payload(self, len):
67 def get_mtu(self, sw_if_index):
68 rv = self.vapi.sw_interface_dump(sw_if_index=sw_if_index)
70 if i.sw_if_index == sw_if_index:
74 def test_ip4_mtu(self):
77 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
78 p_ip4 = IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4,
81 current_mtu = self.get_mtu(self.pg1.sw_if_index)
83 p_payload = UDP(sport=1234, dport=1234) / self.payload(
86 p4 = p_ether / p_ip4 / p_payload
87 p4_reply = p_ip4 / p_payload
89 rx = self.send_and_expect(self.pg0, p4*11, self.pg1)
91 self.validate(p[1], p4_reply)
94 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [576, 0, 0, 0])
95 self.assertEqual(576, self.get_mtu(self.pg1.sw_if_index))
97 # Should fail. Too large MTU
98 p_icmp4 = ICMP(type='dest-unreach', code='fragmentation-needed',
99 nexthopmtu=576, chksum=0x2dbb)
100 icmp4_reply = (IP(src=self.pg0.local_ip4,
101 dst=self.pg0.remote_ip4,
102 ttl=254, len=576, id=0) /
103 p_icmp4 / p_ip4 / p_payload)
104 icmp4_reply[1].ttl -= 1
105 n = icmp4_reply.__class__(icmp4_reply)
106 s = bytes(icmp4_reply)
107 icmp4_reply = s[0:576]
108 rx = self.send_and_expect(self.pg0, p4*11, self.pg0)
112 self.validate_bytes(bytes(p[1]), icmp4_reply)
114 # Now with DF off. Expect fragments.
115 # First go with 1500 byte packets.
116 p_payload = UDP(sport=1234, dport=1234) / self.payload(
118 p4 = p_ether / p_ip4 / p_payload
120 p4_reply = p_ip4 / p_payload
121 p4_reply.ttl = 62 # check this
124 self.pg_enable_capture()
125 self.pg0.add_stream(p4*1)
127 rx = self.pg1.get_capture(3)
128 reass_pkt = reassemble4(rx)
129 self.validate(reass_pkt, p4_reply)
132 # Now what happens with a 9K frame
133 p_payload = UDP(sport=1234, dport=1234) / self.payload(
134 current_mtu - 20 - 8)
135 p4 = p_ether / p_ip4 / p_payload
137 p4_reply = p_ip4 / p_payload
138 p4_reply.ttl = 62 # check this
142 self.pg_enable_capture()
143 self.pg0.add_stream(p4*1)
145 rx = self.pg1.get_capture(16)
146 reass_pkt = reassemble4(rx)
149 self.validate(reass_pkt, p4_reply)
153 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index,
154 [current_mtu, 0, 0, 0])
156 def test_ip6_mtu(self):
159 current_mtu = self.get_mtu(self.pg1.sw_if_index)
161 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)
164 p_payload = UDP(sport=1234, dport=1234) / self.payload(
165 current_mtu - 40 - 8)
167 p6 = p_ether / p_ip6 / p_payload
168 p6_reply = p_ip6 / p_payload
170 rx = self.send_and_expect(self.pg0, p6*9, self.pg1)
172 self.validate(p[1], p6_reply)
174 # MTU (only checked on encap)
175 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1280, 0, 0, 0])
176 self.assertEqual(1280, self.get_mtu(self.pg1.sw_if_index))
178 # Should fail. Too large MTU
179 p_icmp6 = ICMPv6PacketTooBig(mtu=1280, cksum=0x4c7a)
180 icmp6_reply = (IPv6(src=self.pg0.local_ip6,
181 dst=self.pg0.remote_ip6,
182 hlim=255, plen=1240) /
183 p_icmp6 / p_ip6 / p_payload)
184 icmp6_reply[2].hlim -= 1
185 n = icmp6_reply.__class__(icmp6_reply)
186 s = bytes(icmp6_reply)
187 icmp6_reply_str = s[0:1280]
189 rx = self.send_and_expect(self.pg0, p6*9, self.pg0)
191 self.validate_bytes(bytes(p[1]), icmp6_reply_str)
194 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index,
195 [current_mtu, 0, 0, 0])
198 if __name__ == '__main__':
199 unittest.main(testRunner=VppTestRunner)