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):
85 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
86 p_ip4 = IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4,
89 current_mtu = self.get_mtu(self.pg1.sw_if_index)
91 p_payload = UDP(sport=1234, dport=1234) / self.payload(
94 p4 = p_ether / p_ip4 / p_payload
95 p4_reply = p_ip4 / p_payload
97 rx = self.send_and_expect(self.pg0, p4*11, self.pg1)
99 self.validate(p[1], p4_reply)
102 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [576, 0, 0, 0])
103 self.assertEqual(576, self.get_mtu(self.pg1.sw_if_index))
105 # Should fail. Too large MTU
106 p_icmp4 = ICMP(type='dest-unreach', code='fragmentation-needed',
107 nexthopmtu=576, chksum=0x2dbb)
108 icmp4_reply = (IP(src=self.pg0.local_ip4,
109 dst=self.pg0.remote_ip4,
110 ttl=254, len=576, id=0) /
111 p_icmp4 / p_ip4 / p_payload)
112 icmp4_reply[1].ttl -= 1
113 n = icmp4_reply.__class__(str(icmp4_reply))
115 icmp4_reply = s[0:576]
116 rx = self.send_and_expect(self.pg0, p4*11, self.pg0)
120 self.validate_bytes(str(p[1]), icmp4_reply)
123 # Now with DF off. Expect fragments.
124 # First go with 1500 byte packets.
125 p_payload = UDP(sport=1234, dport=1234) / self.payload(
127 p4 = p_ether / p_ip4 / p_payload
129 p4_reply = p_ip4 / p_payload
130 p4_reply.ttl = 62 # check this
133 self.pg_enable_capture()
134 self.pg0.add_stream(p4*1)
136 rx = self.pg1.get_capture(3)
138 reass_pkt = reassemble(rx)
139 self.validate(reass_pkt, p4_reply)
141 # Now what happens with a 9K frame
143 p_payload = UDP(sport=1234, dport=1234) / self.payload(
144 current_mtu - 20 - 8)
145 p4 = p_ether / p_ip4 / p_payload
147 p4_reply = p_ip4 / p_payload
148 p4_reply.ttl = 62 # check this
152 self.pg_enable_capture()
153 self.pg0.add_stream(p4*1)
155 rx = self.pg1.get_capture(16)
156 reass_pkt = reassemble(rx)
159 self.validate(reass_pkt, p4_reply)
162 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index,
163 [current_mtu, 0, 0, 0])
165 def test_ip6_mtu(self):
168 current_mtu = self.get_mtu(self.pg1.sw_if_index)
170 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
171 p_ip6 = IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6)
173 p_payload = UDP(sport=1234, dport=1234) / self.payload(
174 current_mtu - 40 - 8)
176 p6 = p_ether / p_ip6 / p_payload
177 p6_reply = p_ip6 / p_payload
179 rx = self.send_and_expect(self.pg0, p6*9, self.pg1)
181 self.validate(p[1], p6_reply)
183 # MTU (only checked on encap)
184 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1280, 0, 0, 0])
185 self.assertEqual(1280, self.get_mtu(self.pg1.sw_if_index))
187 # Should fail. Too large MTU
188 p_icmp6 = ICMPv6PacketTooBig(mtu=1280, cksum=0x4c7a)
189 icmp6_reply = (IPv6(src=self.pg0.local_ip6,
190 dst=self.pg0.remote_ip6,
191 hlim=254, plen=1240) /
192 p_icmp6 / p_ip6 / p_payload)
193 icmp6_reply[2].hlim -= 1
194 n = icmp6_reply.__class__(str(icmp6_reply))
196 icmp6_reply = s[0:1280]
198 rx = self.send_and_expect(self.pg0, p6*9, self.pg0)
200 self.validate_bytes(str(p[1]), icmp6_reply)
203 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index,
204 [current_mtu, 0, 0, 0])
207 if __name__ == '__main__':
208 unittest.main(testRunner=VppTestRunner)