Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 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 |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame^] | 168 | self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, |
| 169 | current_mtu + mtu_offset) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 170 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 171 | def test_ip6_mtu(self): |
| 172 | """ IP6 MTU test """ |
| 173 | |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame^] | 174 | # |
| 175 | # TODO: Link MTU is 216 bytes 'off'. Fix when L3 MTU patches committed |
| 176 | # |
| 177 | mtu_offset = 216 |
| 178 | current_mtu = self.get_mtu(self.pg1.sw_if_index) |
| 179 | current_mtu -= mtu_offset |
| 180 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 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) |
| 183 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 184 | p_payload = UDP(sport=1234, dport=1234) / self.payload( |
| 185 | current_mtu - 40 - 8) |
| 186 | |
| 187 | p6 = p_ether / p_ip6 / p_payload |
| 188 | p6_reply = p_ip6 / p_payload |
| 189 | p6_reply.hlim -= 1 |
| 190 | rx = self.send_and_expect(self.pg0, p6*9, self.pg1) |
| 191 | for p in rx: |
| 192 | self.validate(p[1], p6_reply) |
| 193 | |
| 194 | # MTU (only checked on encap) |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame^] | 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) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 197 | |
| 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)) |
| 206 | s = str(icmp6_reply) |
| 207 | icmp6_reply = s[0:1280] |
| 208 | |
| 209 | rx = self.send_and_expect(self.pg0, p6*9, self.pg0) |
| 210 | for p in rx: |
| 211 | self.validate_bytes(str(p[1]), icmp6_reply) |
| 212 | |
| 213 | # Reset MTU |
| 214 | self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, current_mtu) |
| 215 | |
| 216 | |
| 217 | if __name__ == '__main__': |
| 218 | unittest.main(testRunner=VppTestRunner) |