VPP-1283: IPv4 PMTU missing MTU value in ICMP4 message.
[vpp.git] / test / test_mtu.py
1 #!/usr/bin/env python
2 """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 #
11 import unittest
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
17 import StringIO
18
19 """ Test_mtu is a subclass of VPPTestCase classes.
20     MTU tests.
21 """
22
23
24 def reassemble(listoffragments):
25     buffer = StringIO.StringIO()
26     first = listoffragments[0]
27     buffer.seek(20)
28     for pkt in listoffragments:
29         # pkt.show2()
30         buffer.seek(pkt[IP].frag*8)
31         buffer.write(pkt[IP].payload)
32     first.len = len(buffer.getvalue()) + 20
33     first.flags = 0
34     del(first.chksum)
35     header = str(first[IP])[:20]
36     return first[IP].__class__(header + buffer.getvalue())
37
38
39 class TestMTU(VppTestCase):
40     """ MTU Test Case """
41
42     @classmethod
43     def setUpClass(cls):
44         super(TestMTU, cls).setUpClass()
45         cls.create_pg_interfaces(range(2))
46         cls.interfaces = list(cls.pg_interfaces)
47
48     def setUp(cls):
49         super(TestMTU, cls).setUp()
50         for i in cls.interfaces:
51             i.admin_up()
52             i.config_ip4()
53             i.config_ip6()
54             i.disable_ipv6_ra()
55             i.resolve_arp()
56             i.resolve_ndp()
57
58     def tearDown(self):
59         super(TestMTU, self).tearDown()
60         if not self.vpp_dead:
61             for i in self.pg_interfaces:
62                 i.unconfig_ip4()
63                 i.unconfig_ip6()
64                 i.admin_down()
65
66     def validate(self, rx, expected):
67         self.assertEqual(rx, expected.__class__(str(expected)))
68
69     def validate_bytes(self, rx, expected):
70         self.assertEqual(rx, expected)
71
72     def payload(self, len):
73         return 'x' * len
74
75     def get_mtu(self, sw_if_index):
76         rv = self.vapi.sw_interface_dump()
77         for i in rv:
78             if i.sw_if_index == sw_if_index:
79                 return i.link_mtu
80         return 0
81
82     def test_ip4_mtu(self):
83         """ IP4 MTU test """
84
85         #
86         # TODO: Link MTU is 216 bytes 'off'. Fix when L3 MTU patches committed
87         #
88         mtu_offset = 216
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,
91                    flags='DF')
92
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
96
97         p_payload = UDP(sport=1234, dport=1234) / self.payload(
98             current_mtu - 20 - 8)
99
100         p4 = p_ether / p_ip4 / p_payload
101         p4_reply = p_ip4 / p_payload
102         p4_reply.ttl -= 1
103         rx = self.send_and_expect(self.pg0, p4*11, self.pg1)
104         for p in rx:
105             self.validate(p[1], p4_reply)
106
107         # MTU
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)
110
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))
120         s = str(icmp4_reply)
121         icmp4_reply = s[0:576]
122         rx = self.send_and_expect(self.pg0, p4*11, self.pg0)
123         for p in rx:
124             # p.show2()
125             # n.show2()
126             self.validate_bytes(str(p[1]), icmp4_reply)
127
128         '''
129         # Now with DF off. Expect fragments.
130         # First go with 1500 byte packets.
131         p_payload = UDP(sport=1234, dport=1234) / self.payload(
132             1500 - 20 - 8)
133         p4 = p_ether / p_ip4 / p_payload
134         p4.flags = 0
135         p4_reply = p_ip4 / p_payload
136         p4_reply.ttl = 62 # check this
137         p4_reply.flags = 0
138         p4_reply.id = 256
139         self.pg_enable_capture()
140         self.pg0.add_stream(p4*1)
141         self.pg_start()
142         rx = self.pg1.get_capture(3)
143         print('RX', len(rx))
144         reass_pkt = reassemble(rx)
145         self.validate(reass_pkt, p4_reply)
146         '''
147         # Now what happens with a 9K frame
148         '''
149         p_payload = UDP(sport=1234, dport=1234) / self.payload(
150             current_mtu - 20 - 8)
151         p4 = p_ether / p_ip4 / p_payload
152         p4.flags = 0
153         p4_reply = p_ip4 / p_payload
154         p4_reply.ttl = 62 # check this
155         p4_reply.flags = 0
156         p4_reply.id = 512
157
158         self.pg_enable_capture()
159         self.pg0.add_stream(p4*1)
160         self.pg_start()
161         rx = self.pg1.get_capture(16)
162         reass_pkt = reassemble(rx)
163         reass_pkt.show2()
164         p4_reply.show2()
165         self.validate(reass_pkt, p4_reply)
166         '''
167         # Reset MTU
168         self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, current_mtu)
169
170     @unittest.skip("Enable when IPv6 fragmentation is added")
171     def test_ip6_mtu(self):
172         """ IP6 MTU test """
173
174         p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
175         p_ip6 = IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6)
176
177         current_mtu = self.get_mtu(self.pg1.sw_if_index)
178         p_payload = UDP(sport=1234, dport=1234) / self.payload(
179             current_mtu - 40 - 8)
180
181         p6 = p_ether / p_ip6 / p_payload
182         p6_reply = p_ip6 / p_payload
183         p6_reply.hlim -= 1
184         rx = self.send_and_expect(self.pg0, p6*9, self.pg1)
185         for p in rx:
186             self.validate(p[1], p6_reply)
187
188         # MTU (only checked on encap)
189         self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, 1280)
190         self.assertEqual(1280, self.get_mtu(self.pg1.sw_if_index))
191
192         # Should fail. Too large MTU
193         p_icmp6 = ICMPv6PacketTooBig(mtu=1280, cksum=0x4c7a)
194         icmp6_reply = (IPv6(src=self.pg0.local_ip6,
195                             dst=self.pg0.remote_ip6,
196                             hlim=254, plen=1240) /
197                        p_icmp6 / p_ip6 / p_payload)
198         icmp6_reply[2].hlim -= 1
199         n = icmp6_reply.__class__(str(icmp6_reply))
200         s = str(icmp6_reply)
201         icmp6_reply = s[0:1280]
202
203         rx = self.send_and_expect(self.pg0, p6*9, self.pg0)
204         for p in rx:
205             self.validate_bytes(str(p[1]), icmp6_reply)
206
207         # Reset MTU
208         self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, current_mtu)
209
210
211 if __name__ == '__main__':
212     unittest.main(testRunner=VppTestRunner)