blob: e9b4ffed9c1b41f84c3427e41d8e716ada79c36f [file] [log] [blame]
Ole Troan8a9c8f12018-05-18 11:01:31 +02001#!/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#
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
16from vpp_ip_route import VppIpRoute, VppRoutePath
Ole Troan8a9c8f12018-05-18 11:01:31 +020017from socket import AF_INET, AF_INET6, inet_pton
18import StringIO
19
20""" Test_mtu is a subclass of VPPTestCase classes.
21 MTU tests.
22"""
23
24
25def reassemble(listoffragments):
26 buffer = StringIO.StringIO()
27 first = listoffragments[0]
28 buffer.seek(20)
29 for pkt in listoffragments:
30 # pkt.show2()
31 buffer.seek(pkt[IP].frag*8)
32 buffer.write(pkt[IP].payload)
33 first.len = len(buffer.getvalue()) + 20
34 first.flags = 0
35 del(first.chksum)
36 header = str(first[IP])[:20]
37 return first[IP].__class__(header + buffer.getvalue())
38
39
40class TestMTU(VppTestCase):
41 """ MTU Test Case """
42
43 @classmethod
44 def setUpClass(cls):
45 super(TestMTU, cls).setUpClass()
46 cls.create_pg_interfaces(range(2))
47 cls.interfaces = list(cls.pg_interfaces)
48
49 def setUp(cls):
50 super(TestMTU, cls).setUp()
51 for i in cls.interfaces:
52 i.admin_up()
53 i.config_ip4()
54 i.config_ip6()
55 i.disable_ipv6_ra()
56 i.resolve_arp()
57 i.resolve_ndp()
58
59 def tearDown(self):
60 super(TestMTU, self).tearDown()
61 if not self.vpp_dead:
62 for i in self.pg_interfaces:
63 i.unconfig_ip4()
64 i.unconfig_ip6()
65 i.admin_down()
66
67 def validate(self, rx, expected):
68 self.assertEqual(rx, expected.__class__(str(expected)))
69
70 def validate_bytes(self, rx, expected):
71 self.assertEqual(rx, expected)
72
73 def payload(self, len):
74 return 'x' * len
75
76 def get_mtu(self, sw_if_index):
77 rv = self.vapi.sw_interface_dump()
78 for i in rv:
79 if i.sw_if_index == sw_if_index:
Ole Troand7231612018-06-07 10:17:57 +020080 return i.mtu[0]
Ole Troan8a9c8f12018-05-18 11:01:31 +020081 return 0
82
83 def test_ip4_mtu(self):
84 """ IP4 MTU test """
85
Ole Troan8a9c8f12018-05-18 11:01:31 +020086 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
87 p_ip4 = IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4,
88 flags='DF')
89
Ole Troan8a9c8f12018-05-18 11:01:31 +020090 current_mtu = self.get_mtu(self.pg1.sw_if_index)
Ole Troan8a9c8f12018-05-18 11:01:31 +020091
92 p_payload = UDP(sport=1234, dport=1234) / self.payload(
93 current_mtu - 20 - 8)
94
95 p4 = p_ether / p_ip4 / p_payload
96 p4_reply = p_ip4 / p_payload
97 p4_reply.ttl -= 1
98 rx = self.send_and_expect(self.pg0, p4*11, self.pg1)
99 for p in rx:
100 self.validate(p[1], p4_reply)
101
102 # MTU
Ole Troand7231612018-06-07 10:17:57 +0200103 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [576, 0, 0, 0])
104 self.assertEqual(576, self.get_mtu(self.pg1.sw_if_index))
Ole Troan8a9c8f12018-05-18 11:01:31 +0200105
106 # Should fail. Too large MTU
107 p_icmp4 = ICMP(type='dest-unreach', code='fragmentation-needed',
108 nexthopmtu=576, chksum=0x2dbb)
109 icmp4_reply = (IP(src=self.pg0.local_ip4,
110 dst=self.pg0.remote_ip4,
111 ttl=254, len=576, id=0) /
112 p_icmp4 / p_ip4 / p_payload)
113 icmp4_reply[1].ttl -= 1
114 n = icmp4_reply.__class__(str(icmp4_reply))
115 s = str(icmp4_reply)
116 icmp4_reply = s[0:576]
117 rx = self.send_and_expect(self.pg0, p4*11, self.pg0)
118 for p in rx:
119 # p.show2()
120 # n.show2()
121 self.validate_bytes(str(p[1]), icmp4_reply)
122
Ole Troan8a9c8f12018-05-18 11:01:31 +0200123 # Now with DF off. Expect fragments.
124 # First go with 1500 byte packets.
125 p_payload = UDP(sport=1234, dport=1234) / self.payload(
126 1500 - 20 - 8)
127 p4 = p_ether / p_ip4 / p_payload
128 p4.flags = 0
129 p4_reply = p_ip4 / p_payload
Ole Troan313f7e22018-04-10 16:02:51 +0200130 p4_reply.ttl = 62 # check this
Ole Troan8a9c8f12018-05-18 11:01:31 +0200131 p4_reply.flags = 0
132 p4_reply.id = 256
133 self.pg_enable_capture()
134 self.pg0.add_stream(p4*1)
135 self.pg_start()
136 rx = self.pg1.get_capture(3)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200137 reass_pkt = reassemble(rx)
138 self.validate(reass_pkt, p4_reply)
Ole Troan313f7e22018-04-10 16:02:51 +0200139
Ole Troan8a9c8f12018-05-18 11:01:31 +0200140 '''
141 # Now what happens with a 9K frame
Ole Troan8a9c8f12018-05-18 11:01:31 +0200142 p_payload = UDP(sport=1234, dport=1234) / self.payload(
143 current_mtu - 20 - 8)
144 p4 = p_ether / p_ip4 / p_payload
145 p4.flags = 0
146 p4_reply = p_ip4 / p_payload
147 p4_reply.ttl = 62 # check this
148 p4_reply.flags = 0
149 p4_reply.id = 512
150
151 self.pg_enable_capture()
152 self.pg0.add_stream(p4*1)
153 self.pg_start()
154 rx = self.pg1.get_capture(16)
155 reass_pkt = reassemble(rx)
156 reass_pkt.show2()
157 p4_reply.show2()
158 self.validate(reass_pkt, p4_reply)
159 '''
Ole Troan313f7e22018-04-10 16:02:51 +0200160
Ole Troan8a9c8f12018-05-18 11:01:31 +0200161 # Reset MTU
Ole Troanda6e11b2018-05-23 11:21:42 +0200162 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index,
Ole Troand7231612018-06-07 10:17:57 +0200163 [current_mtu, 0, 0, 0])
Ole Troan8a9c8f12018-05-18 11:01:31 +0200164
Ole Troan8a9c8f12018-05-18 11:01:31 +0200165 def test_ip6_mtu(self):
166 """ IP6 MTU test """
167
Ole Troanda6e11b2018-05-23 11:21:42 +0200168 current_mtu = self.get_mtu(self.pg1.sw_if_index)
Ole Troanda6e11b2018-05-23 11:21:42 +0200169
Ole Troan8a9c8f12018-05-18 11:01:31 +0200170 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)
172
Ole Troan8a9c8f12018-05-18 11:01:31 +0200173 p_payload = UDP(sport=1234, dport=1234) / self.payload(
174 current_mtu - 40 - 8)
175
176 p6 = p_ether / p_ip6 / p_payload
177 p6_reply = p_ip6 / p_payload
178 p6_reply.hlim -= 1
179 rx = self.send_and_expect(self.pg0, p6*9, self.pg1)
180 for p in rx:
181 self.validate(p[1], p6_reply)
182
183 # MTU (only checked on encap)
Ole Troand7231612018-06-07 10:17:57 +0200184 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))
Ole Troan8a9c8f12018-05-18 11:01:31 +0200186
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,
Ole Troan282093f2018-09-19 12:38:51 +0200191 hlim=255, plen=1240) /
Ole Troan8a9c8f12018-05-18 11:01:31 +0200192 p_icmp6 / p_ip6 / p_payload)
193 icmp6_reply[2].hlim -= 1
194 n = icmp6_reply.__class__(str(icmp6_reply))
195 s = str(icmp6_reply)
Ole Troan282093f2018-09-19 12:38:51 +0200196 icmp6_reply_str = s[0:1280]
Ole Troan8a9c8f12018-05-18 11:01:31 +0200197
198 rx = self.send_and_expect(self.pg0, p6*9, self.pg0)
199 for p in rx:
Ole Troan282093f2018-09-19 12:38:51 +0200200 self.validate_bytes(str(p[1]), icmp6_reply_str)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200201
202 # Reset MTU
Ole Troand7231612018-06-07 10:17:57 +0200203 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index,
204 [current_mtu, 0, 0, 0])
Ole Troan8a9c8f12018-05-18 11:01:31 +0200205
206
207if __name__ == '__main__':
208 unittest.main(testRunner=VppTestRunner)