blob: 3203e40e1148ec97bf3c186ef881ec9846bf8456 [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
Ole Troan7f991832018-12-06 17:35:12 +010018from util import reassemble4
19
Ole Troan8a9c8f12018-05-18 11:01:31 +020020
21""" Test_mtu is a subclass of VPPTestCase classes.
22 MTU tests.
23"""
24
25
Ole Troan8a9c8f12018-05-18 11:01:31 +020026class TestMTU(VppTestCase):
27 """ MTU Test Case """
Ole Troan7f991832018-12-06 17:35:12 +010028 maxDiff = None
Ole Troan8a9c8f12018-05-18 11:01:31 +020029
30 @classmethod
31 def setUpClass(cls):
32 super(TestMTU, cls).setUpClass()
33 cls.create_pg_interfaces(range(2))
34 cls.interfaces = list(cls.pg_interfaces)
35
Paul Vinciguerra0c794532018-11-24 22:02:58 -080036 def setUp(self):
37 super(TestMTU, self).setUp()
38 for i in self.interfaces:
Ole Troan8a9c8f12018-05-18 11:01:31 +020039 i.admin_up()
40 i.config_ip4()
41 i.config_ip6()
42 i.disable_ipv6_ra()
43 i.resolve_arp()
44 i.resolve_ndp()
45
46 def tearDown(self):
47 super(TestMTU, self).tearDown()
48 if not self.vpp_dead:
49 for i in self.pg_interfaces:
50 i.unconfig_ip4()
51 i.unconfig_ip6()
52 i.admin_down()
53
54 def validate(self, rx, expected):
Ole Troan7f991832018-12-06 17:35:12 +010055 self.assertEqual(rx, expected.__class__(expected))
Ole Troan8a9c8f12018-05-18 11:01:31 +020056
57 def validate_bytes(self, rx, expected):
58 self.assertEqual(rx, expected)
59
60 def payload(self, len):
61 return 'x' * len
62
63 def get_mtu(self, sw_if_index):
64 rv = self.vapi.sw_interface_dump()
65 for i in rv:
66 if i.sw_if_index == sw_if_index:
Ole Troand7231612018-06-07 10:17:57 +020067 return i.mtu[0]
Ole Troan8a9c8f12018-05-18 11:01:31 +020068 return 0
69
70 def test_ip4_mtu(self):
71 """ IP4 MTU test """
72
Ole Troan8a9c8f12018-05-18 11:01:31 +020073 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
74 p_ip4 = IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4,
75 flags='DF')
76
Ole Troan8a9c8f12018-05-18 11:01:31 +020077 current_mtu = self.get_mtu(self.pg1.sw_if_index)
Ole Troan8a9c8f12018-05-18 11:01:31 +020078
79 p_payload = UDP(sport=1234, dport=1234) / self.payload(
80 current_mtu - 20 - 8)
81
82 p4 = p_ether / p_ip4 / p_payload
83 p4_reply = p_ip4 / p_payload
84 p4_reply.ttl -= 1
85 rx = self.send_and_expect(self.pg0, p4*11, self.pg1)
86 for p in rx:
87 self.validate(p[1], p4_reply)
88
89 # MTU
Ole Troand7231612018-06-07 10:17:57 +020090 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [576, 0, 0, 0])
91 self.assertEqual(576, self.get_mtu(self.pg1.sw_if_index))
Ole Troan8a9c8f12018-05-18 11:01:31 +020092
93 # Should fail. Too large MTU
94 p_icmp4 = ICMP(type='dest-unreach', code='fragmentation-needed',
95 nexthopmtu=576, chksum=0x2dbb)
96 icmp4_reply = (IP(src=self.pg0.local_ip4,
97 dst=self.pg0.remote_ip4,
98 ttl=254, len=576, id=0) /
99 p_icmp4 / p_ip4 / p_payload)
100 icmp4_reply[1].ttl -= 1
Ole Troan7f991832018-12-06 17:35:12 +0100101 n = icmp4_reply.__class__(icmp4_reply)
102 s = bytes(icmp4_reply)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200103 icmp4_reply = s[0:576]
104 rx = self.send_and_expect(self.pg0, p4*11, self.pg0)
105 for p in rx:
106 # p.show2()
107 # n.show2()
Ole Troan7f991832018-12-06 17:35:12 +0100108 self.validate_bytes(bytes(p[1]), icmp4_reply)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200109
Ole Troan8a9c8f12018-05-18 11:01:31 +0200110 # Now with DF off. Expect fragments.
111 # First go with 1500 byte packets.
112 p_payload = UDP(sport=1234, dport=1234) / self.payload(
113 1500 - 20 - 8)
114 p4 = p_ether / p_ip4 / p_payload
115 p4.flags = 0
116 p4_reply = p_ip4 / p_payload
Ole Troan313f7e22018-04-10 16:02:51 +0200117 p4_reply.ttl = 62 # check this
Ole Troan8a9c8f12018-05-18 11:01:31 +0200118 p4_reply.flags = 0
119 p4_reply.id = 256
120 self.pg_enable_capture()
121 self.pg0.add_stream(p4*1)
122 self.pg_start()
123 rx = self.pg1.get_capture(3)
Ole Troan7f991832018-12-06 17:35:12 +0100124 reass_pkt = reassemble4(rx)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200125 self.validate(reass_pkt, p4_reply)
Ole Troan313f7e22018-04-10 16:02:51 +0200126
Ole Troan8a9c8f12018-05-18 11:01:31 +0200127 '''
128 # Now what happens with a 9K frame
Ole Troan8a9c8f12018-05-18 11:01:31 +0200129 p_payload = UDP(sport=1234, dport=1234) / self.payload(
130 current_mtu - 20 - 8)
131 p4 = p_ether / p_ip4 / p_payload
132 p4.flags = 0
133 p4_reply = p_ip4 / p_payload
134 p4_reply.ttl = 62 # check this
135 p4_reply.flags = 0
136 p4_reply.id = 512
137
138 self.pg_enable_capture()
139 self.pg0.add_stream(p4*1)
140 self.pg_start()
141 rx = self.pg1.get_capture(16)
Ole Troan7f991832018-12-06 17:35:12 +0100142 reass_pkt = reassemble4(rx)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200143 reass_pkt.show2()
144 p4_reply.show2()
145 self.validate(reass_pkt, p4_reply)
146 '''
Ole Troan313f7e22018-04-10 16:02:51 +0200147
Ole Troan8a9c8f12018-05-18 11:01:31 +0200148 # Reset MTU
Ole Troanda6e11b2018-05-23 11:21:42 +0200149 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index,
Ole Troand7231612018-06-07 10:17:57 +0200150 [current_mtu, 0, 0, 0])
Ole Troan8a9c8f12018-05-18 11:01:31 +0200151
Ole Troan8a9c8f12018-05-18 11:01:31 +0200152 def test_ip6_mtu(self):
153 """ IP6 MTU test """
154
Ole Troanda6e11b2018-05-23 11:21:42 +0200155 current_mtu = self.get_mtu(self.pg1.sw_if_index)
Ole Troanda6e11b2018-05-23 11:21:42 +0200156
Ole Troan8a9c8f12018-05-18 11:01:31 +0200157 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
158 p_ip6 = IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6)
159
Ole Troan8a9c8f12018-05-18 11:01:31 +0200160 p_payload = UDP(sport=1234, dport=1234) / self.payload(
161 current_mtu - 40 - 8)
162
163 p6 = p_ether / p_ip6 / p_payload
164 p6_reply = p_ip6 / p_payload
165 p6_reply.hlim -= 1
166 rx = self.send_and_expect(self.pg0, p6*9, self.pg1)
167 for p in rx:
168 self.validate(p[1], p6_reply)
169
170 # MTU (only checked on encap)
Ole Troand7231612018-06-07 10:17:57 +0200171 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1280, 0, 0, 0])
172 self.assertEqual(1280, self.get_mtu(self.pg1.sw_if_index))
Ole Troan8a9c8f12018-05-18 11:01:31 +0200173
174 # Should fail. Too large MTU
175 p_icmp6 = ICMPv6PacketTooBig(mtu=1280, cksum=0x4c7a)
176 icmp6_reply = (IPv6(src=self.pg0.local_ip6,
177 dst=self.pg0.remote_ip6,
Ole Troan282093f2018-09-19 12:38:51 +0200178 hlim=255, plen=1240) /
Ole Troan8a9c8f12018-05-18 11:01:31 +0200179 p_icmp6 / p_ip6 / p_payload)
180 icmp6_reply[2].hlim -= 1
Ole Troan7f991832018-12-06 17:35:12 +0100181 n = icmp6_reply.__class__(icmp6_reply)
182 s = bytes(icmp6_reply)
Ole Troan282093f2018-09-19 12:38:51 +0200183 icmp6_reply_str = s[0:1280]
Ole Troan8a9c8f12018-05-18 11:01:31 +0200184
185 rx = self.send_and_expect(self.pg0, p6*9, self.pg0)
186 for p in rx:
Ole Troan7f991832018-12-06 17:35:12 +0100187 self.validate_bytes(bytes(p[1]), icmp6_reply_str)
Ole Troan8a9c8f12018-05-18 11:01:31 +0200188
189 # Reset MTU
Ole Troand7231612018-06-07 10:17:57 +0200190 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index,
191 [current_mtu, 0, 0, 0])
Ole Troan8a9c8f12018-05-18 11:01:31 +0200192
193
194if __name__ == '__main__':
195 unittest.main(testRunner=VppTestRunner)