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: |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 79 | return i.mtu[0] |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 80 | return 0 |
| 81 | |
| 82 | def test_ip4_mtu(self): |
| 83 | """ IP4 MTU test """ |
| 84 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 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, |
| 87 | flags='DF') |
| 88 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 89 | current_mtu = self.get_mtu(self.pg1.sw_if_index) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 90 | |
| 91 | p_payload = UDP(sport=1234, dport=1234) / self.payload( |
| 92 | current_mtu - 20 - 8) |
| 93 | |
| 94 | p4 = p_ether / p_ip4 / p_payload |
| 95 | p4_reply = p_ip4 / p_payload |
| 96 | p4_reply.ttl -= 1 |
| 97 | rx = self.send_and_expect(self.pg0, p4*11, self.pg1) |
| 98 | for p in rx: |
| 99 | self.validate(p[1], p4_reply) |
| 100 | |
| 101 | # MTU |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 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)) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 104 | |
| 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)) |
| 114 | s = str(icmp4_reply) |
| 115 | icmp4_reply = s[0:576] |
| 116 | rx = self.send_and_expect(self.pg0, p4*11, self.pg0) |
| 117 | for p in rx: |
| 118 | # p.show2() |
| 119 | # n.show2() |
| 120 | self.validate_bytes(str(p[1]), icmp4_reply) |
| 121 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 122 | # Now with DF off. Expect fragments. |
| 123 | # First go with 1500 byte packets. |
| 124 | p_payload = UDP(sport=1234, dport=1234) / self.payload( |
| 125 | 1500 - 20 - 8) |
| 126 | p4 = p_ether / p_ip4 / p_payload |
| 127 | p4.flags = 0 |
| 128 | p4_reply = p_ip4 / p_payload |
Ole Troan | 313f7e2 | 2018-04-10 16:02:51 +0200 | [diff] [blame] | 129 | p4_reply.ttl = 62 # check this |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 130 | p4_reply.flags = 0 |
| 131 | p4_reply.id = 256 |
| 132 | self.pg_enable_capture() |
| 133 | self.pg0.add_stream(p4*1) |
| 134 | self.pg_start() |
| 135 | rx = self.pg1.get_capture(3) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 136 | reass_pkt = reassemble(rx) |
| 137 | self.validate(reass_pkt, p4_reply) |
Ole Troan | 313f7e2 | 2018-04-10 16:02:51 +0200 | [diff] [blame] | 138 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 139 | ''' |
| 140 | # Now what happens with a 9K frame |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 141 | p_payload = UDP(sport=1234, dport=1234) / self.payload( |
| 142 | current_mtu - 20 - 8) |
| 143 | p4 = p_ether / p_ip4 / p_payload |
| 144 | p4.flags = 0 |
| 145 | p4_reply = p_ip4 / p_payload |
| 146 | p4_reply.ttl = 62 # check this |
| 147 | p4_reply.flags = 0 |
| 148 | p4_reply.id = 512 |
| 149 | |
| 150 | self.pg_enable_capture() |
| 151 | self.pg0.add_stream(p4*1) |
| 152 | self.pg_start() |
| 153 | rx = self.pg1.get_capture(16) |
| 154 | reass_pkt = reassemble(rx) |
| 155 | reass_pkt.show2() |
| 156 | p4_reply.show2() |
| 157 | self.validate(reass_pkt, p4_reply) |
| 158 | ''' |
Ole Troan | 313f7e2 | 2018-04-10 16:02:51 +0200 | [diff] [blame] | 159 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 160 | # Reset MTU |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame] | 161 | self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 162 | [current_mtu, 0, 0, 0]) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 163 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 164 | def test_ip6_mtu(self): |
| 165 | """ IP6 MTU test """ |
| 166 | |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame] | 167 | current_mtu = self.get_mtu(self.pg1.sw_if_index) |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame] | 168 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 169 | p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) |
| 170 | p_ip6 = IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) |
| 171 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 172 | p_payload = UDP(sport=1234, dport=1234) / self.payload( |
| 173 | current_mtu - 40 - 8) |
| 174 | |
| 175 | p6 = p_ether / p_ip6 / p_payload |
| 176 | p6_reply = p_ip6 / p_payload |
| 177 | p6_reply.hlim -= 1 |
| 178 | rx = self.send_and_expect(self.pg0, p6*9, self.pg1) |
| 179 | for p in rx: |
| 180 | self.validate(p[1], p6_reply) |
| 181 | |
| 182 | # MTU (only checked on encap) |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 183 | self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1280, 0, 0, 0]) |
| 184 | self.assertEqual(1280, self.get_mtu(self.pg1.sw_if_index)) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 185 | |
| 186 | # Should fail. Too large MTU |
| 187 | p_icmp6 = ICMPv6PacketTooBig(mtu=1280, cksum=0x4c7a) |
| 188 | icmp6_reply = (IPv6(src=self.pg0.local_ip6, |
| 189 | dst=self.pg0.remote_ip6, |
| 190 | hlim=254, plen=1240) / |
| 191 | p_icmp6 / p_ip6 / p_payload) |
| 192 | icmp6_reply[2].hlim -= 1 |
| 193 | n = icmp6_reply.__class__(str(icmp6_reply)) |
| 194 | s = str(icmp6_reply) |
| 195 | icmp6_reply = s[0:1280] |
| 196 | |
| 197 | rx = self.send_and_expect(self.pg0, p6*9, self.pg0) |
| 198 | for p in rx: |
| 199 | self.validate_bytes(str(p[1]), icmp6_reply) |
| 200 | |
| 201 | # Reset MTU |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 202 | self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, |
| 203 | [current_mtu, 0, 0, 0]) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 204 | |
| 205 | |
| 206 | if __name__ == '__main__': |
| 207 | unittest.main(testRunner=VppTestRunner) |