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_route import VppIpRoute, VppRoutePath, DpoProto
16 from socket import AF_INET, AF_INET6, inet_pton
19 """ Test_mtu is a subclass of VPPTestCase classes.
24 def reassemble(listoffragments):
25 buffer = StringIO.StringIO()
26 first = listoffragments[0]
28 for pkt in listoffragments:
30 buffer.seek(pkt[IP].frag*8)
31 buffer.write(pkt[IP].payload)
32 first.len = len(buffer.getvalue()) + 20
35 header = str(first[IP])[:20]
36 return first[IP].__class__(header + buffer.getvalue())
39 class TestMTU(VppTestCase):
44 super(TestMTU, cls).setUpClass()
45 cls.create_pg_interfaces(range(2))
46 cls.interfaces = list(cls.pg_interfaces)
49 super(TestMTU, cls).setUp()
50 for i in cls.interfaces:
59 super(TestMTU, self).tearDown()
61 for i in self.pg_interfaces:
66 def validate(self, rx, expected):
67 self.assertEqual(rx, expected.__class__(str(expected)))
69 def validate_bytes(self, rx, expected):
70 self.assertEqual(rx, expected)
72 def payload(self, len):
75 def get_mtu(self, sw_if_index):
76 rv = self.vapi.sw_interface_dump()
78 if i.sw_if_index == sw_if_index:
82 def test_ip4_mtu(self):
86 # TODO: Link MTU is 216 bytes 'off'. Fix when L3 MTU patches committed
89 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
90 p_ip4 = IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4,
93 # TODO: Re-enable when MTU fixes are committed
94 current_mtu = self.get_mtu(self.pg1.sw_if_index)
95 current_mtu -= mtu_offset
97 p_payload = UDP(sport=1234, dport=1234) / self.payload(
100 p4 = p_ether / p_ip4 / p_payload
101 p4_reply = p_ip4 / p_payload
103 rx = self.send_and_expect(self.pg0, p4*11, self.pg1)
105 self.validate(p[1], p4_reply)
108 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, 576 + mtu_offset)
109 self.assertEqual(576, self.get_mtu(self.pg1.sw_if_index) - mtu_offset)
111 # Should fail. Too large MTU
112 p_icmp4 = ICMP(type='dest-unreach', code='fragmentation-needed',
113 nexthopmtu=576, chksum=0x2dbb)
114 icmp4_reply = (IP(src=self.pg0.local_ip4,
115 dst=self.pg0.remote_ip4,
116 ttl=254, len=576, id=0) /
117 p_icmp4 / p_ip4 / p_payload)
118 icmp4_reply[1].ttl -= 1
119 n = icmp4_reply.__class__(str(icmp4_reply))
121 icmp4_reply = s[0:576]
122 rx = self.send_and_expect(self.pg0, p4*11, self.pg0)
126 self.validate_bytes(str(p[1]), icmp4_reply)
129 # Now with DF off. Expect fragments.
130 # First go with 1500 byte packets.
131 p_payload = UDP(sport=1234, dport=1234) / self.payload(
133 p4 = p_ether / p_ip4 / p_payload
135 p4_reply = p_ip4 / p_payload
136 p4_reply.ttl = 62 # check this
139 self.pg_enable_capture()
140 self.pg0.add_stream(p4*1)
142 rx = self.pg1.get_capture(3)
144 reass_pkt = reassemble(rx)
145 self.validate(reass_pkt, p4_reply)
147 # Now what happens with a 9K frame
149 p_payload = UDP(sport=1234, dport=1234) / self.payload(
150 current_mtu - 20 - 8)
151 p4 = p_ether / p_ip4 / p_payload
153 p4_reply = p_ip4 / p_payload
154 p4_reply.ttl = 62 # check this
158 self.pg_enable_capture()
159 self.pg0.add_stream(p4*1)
161 rx = self.pg1.get_capture(16)
162 reass_pkt = reassemble(rx)
165 self.validate(reass_pkt, p4_reply)
168 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index,
169 current_mtu + mtu_offset)
171 def test_ip6_mtu(self):
175 # TODO: Link MTU is 216 bytes 'off'. Fix when L3 MTU patches committed
178 current_mtu = self.get_mtu(self.pg1.sw_if_index)
179 current_mtu -= mtu_offset
181 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
182 p_ip6 = IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6)
184 p_payload = UDP(sport=1234, dport=1234) / self.payload(
185 current_mtu - 40 - 8)
187 p6 = p_ether / p_ip6 / p_payload
188 p6_reply = p_ip6 / p_payload
190 rx = self.send_and_expect(self.pg0, p6*9, self.pg1)
192 self.validate(p[1], p6_reply)
194 # MTU (only checked on encap)
195 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, 1280 + mtu_offset)
196 self.assertEqual(1280, self.get_mtu(self.pg1.sw_if_index) - mtu_offset)
198 # Should fail. Too large MTU
199 p_icmp6 = ICMPv6PacketTooBig(mtu=1280, cksum=0x4c7a)
200 icmp6_reply = (IPv6(src=self.pg0.local_ip6,
201 dst=self.pg0.remote_ip6,
202 hlim=254, plen=1240) /
203 p_icmp6 / p_ip6 / p_payload)
204 icmp6_reply[2].hlim -= 1
205 n = icmp6_reply.__class__(str(icmp6_reply))
207 icmp6_reply = s[0:1280]
209 rx = self.send_and_expect(self.pg0, p6*9, self.pg0)
211 self.validate_bytes(str(p[1]), icmp6_reply)
214 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, current_mtu)
217 if __name__ == '__main__':
218 unittest.main(testRunner=VppTestRunner)