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 |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 15 | from vpp_ip import DpoProto |
| 16 | from vpp_ip_route import VppIpRoute, VppRoutePath |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 17 | from socket import AF_INET, AF_INET6, inet_pton |
| 18 | import StringIO |
| 19 | |
| 20 | """ Test_mtu is a subclass of VPPTestCase classes. |
| 21 | MTU tests. |
| 22 | """ |
| 23 | |
| 24 | |
| 25 | def 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 | |
| 40 | class 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 Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 80 | return i.mtu[0] |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 81 | return 0 |
| 82 | |
| 83 | def test_ip4_mtu(self): |
| 84 | """ IP4 MTU test """ |
| 85 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 86 | 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 Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 90 | current_mtu = self.get_mtu(self.pg1.sw_if_index) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 91 | |
| 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 Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 103 | 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 Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 105 | |
| 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 Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 123 | # 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 Troan | 313f7e2 | 2018-04-10 16:02:51 +0200 | [diff] [blame] | 130 | p4_reply.ttl = 62 # check this |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 131 | 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 Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 137 | reass_pkt = reassemble(rx) |
| 138 | self.validate(reass_pkt, p4_reply) |
Ole Troan | 313f7e2 | 2018-04-10 16:02:51 +0200 | [diff] [blame] | 139 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 140 | ''' |
| 141 | # Now what happens with a 9K frame |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 142 | 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 Troan | 313f7e2 | 2018-04-10 16:02:51 +0200 | [diff] [blame] | 160 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 161 | # Reset MTU |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame] | 162 | self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 163 | [current_mtu, 0, 0, 0]) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 164 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 165 | def test_ip6_mtu(self): |
| 166 | """ IP6 MTU test """ |
| 167 | |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame] | 168 | current_mtu = self.get_mtu(self.pg1.sw_if_index) |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame] | 169 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 170 | 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 Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 173 | 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 Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 184 | 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 Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 186 | |
| 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 Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame^] | 191 | hlim=255, plen=1240) / |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 192 | 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 Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame^] | 196 | icmp6_reply_str = s[0:1280] |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 197 | |
| 198 | rx = self.send_and_expect(self.pg0, p6*9, self.pg0) |
| 199 | for p in rx: |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame^] | 200 | self.validate_bytes(str(p[1]), icmp6_reply_str) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 201 | |
| 202 | # Reset MTU |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 203 | self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, |
| 204 | [current_mtu, 0, 0, 0]) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 205 | |
| 206 | |
| 207 | if __name__ == '__main__': |
| 208 | unittest.main(testRunner=VppTestRunner) |