Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 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 |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 16 | from vpp_ip_route import VppIpRoute, VppRoutePath, FibPathProto |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 17 | from socket import AF_INET, AF_INET6, inet_pton |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 18 | from util import reassemble4 |
| 19 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 20 | |
| 21 | """ Test_mtu is a subclass of VPPTestCase classes. |
| 22 | MTU tests. |
| 23 | """ |
| 24 | |
| 25 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 26 | class TestMTU(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 27 | """MTU Test Case""" |
| 28 | |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 29 | maxDiff = None |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 30 | |
| 31 | @classmethod |
| 32 | def setUpClass(cls): |
| 33 | super(TestMTU, cls).setUpClass() |
| 34 | cls.create_pg_interfaces(range(2)) |
| 35 | cls.interfaces = list(cls.pg_interfaces) |
| 36 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 37 | @classmethod |
| 38 | def tearDownClass(cls): |
| 39 | super(TestMTU, cls).tearDownClass() |
| 40 | |
Paul Vinciguerra | 0c79453 | 2018-11-24 22:02:58 -0800 | [diff] [blame] | 41 | def setUp(self): |
| 42 | super(TestMTU, self).setUp() |
| 43 | for i in self.interfaces: |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 44 | i.admin_up() |
| 45 | i.config_ip4() |
| 46 | i.config_ip6() |
| 47 | i.disable_ipv6_ra() |
| 48 | i.resolve_arp() |
| 49 | i.resolve_ndp() |
| 50 | |
| 51 | def tearDown(self): |
| 52 | super(TestMTU, self).tearDown() |
| 53 | if not self.vpp_dead: |
| 54 | for i in self.pg_interfaces: |
| 55 | i.unconfig_ip4() |
| 56 | i.unconfig_ip6() |
| 57 | i.admin_down() |
| 58 | |
| 59 | def validate(self, rx, expected): |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 60 | self.assertEqual(rx, expected.__class__(expected)) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 61 | |
| 62 | def validate_bytes(self, rx, expected): |
| 63 | self.assertEqual(rx, expected) |
| 64 | |
| 65 | def payload(self, len): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 66 | return "x" * len |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 67 | |
| 68 | def get_mtu(self, sw_if_index): |
Paul Vinciguerra | 7a99823 | 2019-06-07 15:01:12 -0400 | [diff] [blame] | 69 | rv = self.vapi.sw_interface_dump(sw_if_index=sw_if_index) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 70 | for i in rv: |
| 71 | if i.sw_if_index == sw_if_index: |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 72 | return i.mtu[0] |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 73 | return 0 |
| 74 | |
| 75 | def test_ip4_mtu(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 76 | """IP4 MTU test""" |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 77 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 78 | p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 79 | p_ip4 = IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, flags="DF") |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 80 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 81 | current_mtu = self.get_mtu(self.pg1.sw_if_index) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 82 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 83 | p_payload = UDP(sport=1234, dport=1234) / self.payload(current_mtu - 20 - 8) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 84 | |
| 85 | p4 = p_ether / p_ip4 / p_payload |
| 86 | p4_reply = p_ip4 / p_payload |
| 87 | p4_reply.ttl -= 1 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 88 | rx = self.send_and_expect(self.pg0, p4 * 11, self.pg1) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 89 | for p in rx: |
| 90 | self.validate(p[1], p4_reply) |
| 91 | |
| 92 | # MTU |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 93 | self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [576, 0, 0, 0]) |
| 94 | self.assertEqual(576, self.get_mtu(self.pg1.sw_if_index)) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 95 | |
| 96 | # Should fail. Too large MTU |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 97 | p_icmp4 = ICMP( |
| 98 | type="dest-unreach", |
| 99 | code="fragmentation-needed", |
| 100 | nexthopmtu=576, |
| 101 | chksum=0x2DBB, |
| 102 | ) |
| 103 | icmp4_reply = ( |
| 104 | IP(src=self.pg0.local_ip4, dst=self.pg0.remote_ip4, ttl=254, len=576, id=0) |
| 105 | / p_icmp4 |
| 106 | / p_ip4 |
| 107 | / p_payload |
| 108 | ) |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 109 | n = icmp4_reply.__class__(icmp4_reply) |
| 110 | s = bytes(icmp4_reply) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 111 | icmp4_reply = s[0:576] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 112 | rx = self.send_and_expect_some(self.pg0, p4 * 11, self.pg0) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 113 | for p in rx: |
| 114 | # p.show2() |
| 115 | # n.show2() |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 116 | self.validate_bytes(bytes(p[1]), icmp4_reply) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 117 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 118 | # Now with DF off. Expect fragments. |
| 119 | # First go with 1500 byte packets. |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 120 | p_payload = UDP(sport=1234, dport=1234) / self.payload(1500 - 20 - 8) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 121 | p4 = p_ether / p_ip4 / p_payload |
| 122 | p4.flags = 0 |
| 123 | p4_reply = p_ip4 / p_payload |
Neale Ranns | 0b6a857 | 2019-10-30 17:34:14 +0000 | [diff] [blame] | 124 | p4_reply.ttl = p_ip4.ttl - 1 |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 125 | p4_reply.flags = 0 |
| 126 | p4_reply.id = 256 |
| 127 | self.pg_enable_capture() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 128 | self.pg0.add_stream(p4 * 1) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 129 | self.pg_start() |
| 130 | rx = self.pg1.get_capture(3) |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 131 | reass_pkt = reassemble4(rx) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 132 | self.validate(reass_pkt, p4_reply) |
Ole Troan | 313f7e2 | 2018-04-10 16:02:51 +0200 | [diff] [blame] | 133 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 134 | """ |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 135 | # Now what happens with a 9K frame |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 136 | p_payload = UDP(sport=1234, dport=1234) / self.payload( |
| 137 | current_mtu - 20 - 8) |
| 138 | p4 = p_ether / p_ip4 / p_payload |
| 139 | p4.flags = 0 |
| 140 | p4_reply = p_ip4 / p_payload |
| 141 | p4_reply.ttl = 62 # check this |
| 142 | p4_reply.flags = 0 |
| 143 | p4_reply.id = 512 |
| 144 | |
| 145 | self.pg_enable_capture() |
| 146 | self.pg0.add_stream(p4*1) |
| 147 | self.pg_start() |
| 148 | rx = self.pg1.get_capture(16) |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 149 | reass_pkt = reassemble4(rx) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 150 | reass_pkt.show2() |
| 151 | p4_reply.show2() |
| 152 | self.validate(reass_pkt, p4_reply) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 153 | """ |
Ole Troan | 313f7e2 | 2018-04-10 16:02:51 +0200 | [diff] [blame] | 154 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 155 | # Reset MTU |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 156 | self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [current_mtu, 0, 0, 0]) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 157 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 158 | def test_ip6_mtu(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 159 | """IP6 MTU test""" |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 160 | |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame] | 161 | current_mtu = self.get_mtu(self.pg1.sw_if_index) |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame] | 162 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 163 | p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) |
| 164 | p_ip6 = IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) |
| 165 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 166 | p_payload = UDP(sport=1234, dport=1234) / self.payload(current_mtu - 40 - 8) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 167 | |
| 168 | p6 = p_ether / p_ip6 / p_payload |
| 169 | p6_reply = p_ip6 / p_payload |
| 170 | p6_reply.hlim -= 1 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 171 | rx = self.send_and_expect(self.pg0, p6 * 9, self.pg1) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 172 | for p in rx: |
| 173 | self.validate(p[1], p6_reply) |
| 174 | |
| 175 | # MTU (only checked on encap) |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 176 | self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1280, 0, 0, 0]) |
| 177 | self.assertEqual(1280, self.get_mtu(self.pg1.sw_if_index)) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 178 | |
| 179 | # Should fail. Too large MTU |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 180 | p_icmp6 = ICMPv6PacketTooBig(mtu=1280, cksum=0x4C7A) |
| 181 | icmp6_reply = ( |
| 182 | IPv6(src=self.pg0.local_ip6, dst=self.pg0.remote_ip6, hlim=255, plen=1240) |
| 183 | / p_icmp6 |
| 184 | / p_ip6 |
| 185 | / p_payload |
| 186 | ) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 187 | icmp6_reply[2].hlim -= 1 |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 188 | n = icmp6_reply.__class__(icmp6_reply) |
| 189 | s = bytes(icmp6_reply) |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 190 | icmp6_reply_str = s[0:1280] |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 191 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 192 | rx = self.send_and_expect_some(self.pg0, p6 * 9, self.pg0) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 193 | for p in rx: |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 194 | self.validate_bytes(bytes(p[1]), icmp6_reply_str) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 195 | |
| 196 | # Reset MTU |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 197 | self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [current_mtu, 0, 0, 0]) |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 198 | |
| 199 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 200 | if __name__ == "__main__": |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 201 | unittest.main(testRunner=VppTestRunner) |