Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ole Troan | 46c1c11 | 2018-03-14 20:39:40 +0100 | [diff] [blame] | 2 | """IP{4,6} over IP{v,6} tunnel functional tests""" |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 3 | |
| 4 | import unittest |
Neale Ranns | 0b6a857 | 2019-10-30 17:34:14 +0000 | [diff] [blame] | 5 | from scapy.layers.inet6 import IPv6, Ether, IP, UDP, IPv6ExtHdrFragment, Raw |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 6 | from scapy.all import fragment, fragment6, RandShort, defragment6 |
Ole Troan | 46c1c11 | 2018-03-14 20:39:40 +0100 | [diff] [blame] | 7 | from framework import VppTestCase, VppTestRunner |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 8 | from vpp_ip import DpoProto |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 9 | from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpTable, FibPathProto |
Ole Troan | 46c1c11 | 2018-03-14 20:39:40 +0100 | [diff] [blame] | 10 | from socket import AF_INET, AF_INET6, inet_pton |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 11 | from util import reassemble4 |
| 12 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 13 | """ Testipip is a subclass of VPPTestCase classes. |
| 14 | |
| 15 | IPIP tests. |
| 16 | |
| 17 | """ |
| 18 | |
| 19 | |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 20 | def ipip_add_tunnel(test, src, dst, table_id=0, tc_tos=0xff): |
| 21 | """ Add a IPIP tunnel """ |
| 22 | return test.vapi.ipip_add_tunnel( |
| 23 | tunnel={ |
| 24 | 'src': src, |
| 25 | 'dst': dst, |
| 26 | 'table_id': table_id, |
| 27 | 'instance': 0xffffffff, |
| 28 | 'tc_tos': tc_tos |
| 29 | } |
| 30 | ) |
| 31 | |
| 32 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 33 | class TestIPIP(VppTestCase): |
| 34 | """ IPIP Test Case """ |
| 35 | |
| 36 | @classmethod |
| 37 | def setUpClass(cls): |
| 38 | super(TestIPIP, cls).setUpClass() |
Ole Troan | 46c1c11 | 2018-03-14 20:39:40 +0100 | [diff] [blame] | 39 | cls.create_pg_interfaces(range(2)) |
| 40 | cls.interfaces = list(cls.pg_interfaces) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 41 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 42 | @classmethod |
| 43 | def tearDownClass(cls): |
| 44 | super(TestIPIP, cls).tearDownClass() |
| 45 | |
Paul Vinciguerra | 741865b | 2018-11-27 06:01:22 -0800 | [diff] [blame] | 46 | def setUp(self): |
| 47 | super(TestIPIP, self).setUp() |
| 48 | for i in self.interfaces: |
Ole Troan | 46c1c11 | 2018-03-14 20:39:40 +0100 | [diff] [blame] | 49 | i.admin_up() |
| 50 | i.config_ip4() |
| 51 | i.config_ip6() |
| 52 | i.disable_ipv6_ra() |
| 53 | i.resolve_arp() |
| 54 | i.resolve_ndp() |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 55 | |
| 56 | def tearDown(self): |
| 57 | super(TestIPIP, self).tearDown() |
| 58 | if not self.vpp_dead: |
Ole Troan | 46c1c11 | 2018-03-14 20:39:40 +0100 | [diff] [blame] | 59 | for i in self.pg_interfaces: |
| 60 | i.unconfig_ip4() |
| 61 | i.unconfig_ip6() |
| 62 | i.admin_down() |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 63 | |
| 64 | def validate(self, rx, expected): |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 65 | self.assertEqual(rx, expected.__class__(expected)) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 66 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 67 | def generate_ip4_frags(self, payload_length, fragment_size): |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 68 | p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) |
| 69 | p_payload = UDP(sport=1234, dport=1234) / self.payload(payload_length) |
| 70 | p_ip4 = IP(src="1.2.3.4", dst=self.pg0.remote_ip4) |
| 71 | outer_ip4 = (p_ether / IP(src=self.pg1.remote_ip4, |
| 72 | id=RandShort(), |
| 73 | dst=self.pg0.local_ip4) / p_ip4 / p_payload) |
| 74 | frags = fragment(outer_ip4, fragment_size) |
| 75 | p4_reply = (p_ip4 / p_payload) |
| 76 | p4_reply.ttl -= 1 |
| 77 | return frags, p4_reply |
| 78 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 79 | def test_ipip4(self): |
| 80 | """ ip{v4,v6} over ip4 test """ |
| 81 | p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 82 | p_ip6 = IPv6(src="1::1", dst="DEAD::1", nh='UDP', tc=42) |
| 83 | p_ip4 = IP(src="1.2.3.4", dst="130.67.0.1", tos=42) |
Neale Ranns | 0b6a857 | 2019-10-30 17:34:14 +0000 | [diff] [blame] | 84 | p_payload = UDP(sport=1234, dport=1234) / Raw(b'X' * 100) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 85 | |
| 86 | # IPv4 transport |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 87 | rv = ipip_add_tunnel(self, |
| 88 | self.pg0.local_ip4, |
| 89 | self.pg1.remote_ip4, |
| 90 | tc_tos=0xFF) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 91 | sw_if_index = rv.sw_if_index |
| 92 | |
| 93 | # Set interface up and enable IP on it |
Ole Troan | 46c1c11 | 2018-03-14 20:39:40 +0100 | [diff] [blame] | 94 | self.vapi.sw_interface_set_flags(sw_if_index, 1) |
| 95 | self.vapi.sw_interface_set_unnumbered( |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 96 | sw_if_index=self.pg0.sw_if_index, |
| 97 | unnumbered_sw_if_index=sw_if_index) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 98 | |
| 99 | # Add IPv4 and IPv6 routes via tunnel interface |
| 100 | ip4_via_tunnel = VppIpRoute( |
| 101 | self, "130.67.0.0", 16, |
| 102 | [VppRoutePath("0.0.0.0", |
| 103 | sw_if_index, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 104 | proto=FibPathProto.FIB_PATH_NH_PROTO_IP4)]) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 105 | ip4_via_tunnel.add_vpp_config() |
| 106 | |
| 107 | ip6_via_tunnel = VppIpRoute( |
| 108 | self, "dead::", 16, |
| 109 | [VppRoutePath("::", |
| 110 | sw_if_index, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 111 | proto=FibPathProto.FIB_PATH_NH_PROTO_IP6)]) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 112 | ip6_via_tunnel.add_vpp_config() |
| 113 | |
| 114 | # IPv6 in to IPv4 tunnel |
| 115 | p6 = (p_ether / p_ip6 / p_payload) |
| 116 | p_inner_ip6 = p_ip6 |
| 117 | p_inner_ip6.hlim -= 1 |
| 118 | p6_reply = (IP(src=self.pg0.local_ip4, dst=self.pg1.remote_ip4, |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 119 | proto='ipv6', id=0, tos=42) / p_inner_ip6 / p_payload) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 120 | p6_reply.ttl -= 1 |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 121 | rx = self.send_and_expect(self.pg0, p6 * 10, self.pg1) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 122 | for p in rx: |
| 123 | self.validate(p[1], p6_reply) |
Neale Ranns | 0b6a857 | 2019-10-30 17:34:14 +0000 | [diff] [blame] | 124 | self.assert_packet_checksums_valid(p) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 125 | |
| 126 | # IPv4 in to IPv4 tunnel |
| 127 | p4 = (p_ether / p_ip4 / p_payload) |
| 128 | p_ip4_inner = p_ip4 |
| 129 | p_ip4_inner.ttl -= 1 |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 130 | p4_reply = (IP(src=self.pg0.local_ip4, dst=self.pg1.remote_ip4, |
| 131 | tos=42) / |
| 132 | p_ip4_inner / p_payload) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 133 | p4_reply.ttl -= 1 |
| 134 | p4_reply.id = 0 |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 135 | rx = self.send_and_expect(self.pg0, p4 * 10, self.pg1) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 136 | for p in rx: |
| 137 | self.validate(p[1], p4_reply) |
Neale Ranns | 0b6a857 | 2019-10-30 17:34:14 +0000 | [diff] [blame] | 138 | self.assert_packet_checksums_valid(p) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 139 | |
| 140 | # Decapsulation |
| 141 | p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) |
| 142 | |
| 143 | # IPv4 tunnel to IPv4 |
| 144 | p_ip4 = IP(src="1.2.3.4", dst=self.pg0.remote_ip4) |
| 145 | p4 = (p_ether / IP(src=self.pg1.remote_ip4, |
| 146 | dst=self.pg0.local_ip4) / p_ip4 / p_payload) |
| 147 | p4_reply = (p_ip4 / p_payload) |
| 148 | p4_reply.ttl -= 1 |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 149 | rx = self.send_and_expect(self.pg1, p4 * 10, self.pg0) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 150 | for p in rx: |
| 151 | self.validate(p[1], p4_reply) |
Neale Ranns | 0b6a857 | 2019-10-30 17:34:14 +0000 | [diff] [blame] | 152 | self.assert_packet_checksums_valid(p) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 153 | |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 154 | err = self.statistics.get_err_counter( |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 155 | '/err/ipip4-input/packets decapsulated') |
| 156 | self.assertEqual(err, 10) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 157 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 158 | # IPv4 tunnel to IPv6 |
| 159 | p_ip6 = IPv6(src="1:2:3::4", dst=self.pg0.remote_ip6) |
| 160 | p6 = (p_ether / IP(src=self.pg1.remote_ip4, |
| 161 | dst=self.pg0.local_ip4) / p_ip6 / p_payload) |
| 162 | p6_reply = (p_ip6 / p_payload) |
| 163 | p6_reply.hlim = 63 |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 164 | rx = self.send_and_expect(self.pg1, p6 * 10, self.pg0) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 165 | for p in rx: |
| 166 | self.validate(p[1], p6_reply) |
Neale Ranns | 0b6a857 | 2019-10-30 17:34:14 +0000 | [diff] [blame] | 167 | self.assert_packet_checksums_valid(p) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 168 | |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 169 | err = self.statistics.get_err_counter( |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 170 | '/err/ipip4-input/packets decapsulated') |
| 171 | self.assertEqual(err, 20) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 172 | |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 173 | # |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 174 | # Fragmentation / Reassembly and Re-fragmentation |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 175 | # |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 176 | rv = self.vapi.ip_reassembly_enable_disable( |
| 177 | sw_if_index=self.pg1.sw_if_index, |
| 178 | enable_ip4=1) |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 179 | |
Klement Sekera | 3a343d4 | 2019-05-16 14:35:46 +0200 | [diff] [blame] | 180 | self.vapi.ip_reassembly_set(timeout_ms=1000, max_reassemblies=1000, |
| 181 | max_reassembly_length=1000, |
| 182 | expire_walk_interval_ms=10000, |
| 183 | is_ip6=0) |
| 184 | |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 185 | # Send lots of fragments, verify reassembled packet |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 186 | frags, p4_reply = self.generate_ip4_frags(3131, 1400) |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 187 | f = [] |
| 188 | for i in range(0, 1000): |
| 189 | f.extend(frags) |
| 190 | self.pg1.add_stream(f) |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 191 | self.pg_enable_capture() |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 192 | self.pg_start() |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 193 | rx = self.pg0.get_capture(1000) |
| 194 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 195 | for p in rx: |
| 196 | self.validate(p[1], p4_reply) |
| 197 | |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 198 | err = self.statistics.get_err_counter( |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 199 | '/err/ipip4-input/packets decapsulated') |
| 200 | self.assertEqual(err, 1020) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 201 | |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 202 | f = [] |
| 203 | r = [] |
| 204 | for i in range(1, 90): |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 205 | frags, p4_reply = self.generate_ip4_frags(i * 100, 1000) |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 206 | f.extend(frags) |
| 207 | r.extend(p4_reply) |
| 208 | self.pg_enable_capture() |
| 209 | self.pg1.add_stream(f) |
| 210 | self.pg_start() |
| 211 | rx = self.pg0.get_capture(89) |
| 212 | i = 0 |
| 213 | for p in rx: |
| 214 | self.validate(p[1], r[i]) |
| 215 | i += 1 |
| 216 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 217 | # Now try with re-fragmentation |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 218 | # |
| 219 | # Send fragments to tunnel head-end, for the tunnel head end |
| 220 | # to reassemble and then refragment |
| 221 | # |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 222 | self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [576, 0, 0, 0]) |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 223 | frags, p4_reply = self.generate_ip4_frags(3123, 1200) |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 224 | self.pg_enable_capture() |
| 225 | self.pg1.add_stream(frags) |
| 226 | self.pg_start() |
| 227 | rx = self.pg0.get_capture(6) |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 228 | reass_pkt = reassemble4(rx) |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 229 | p4_reply.id = 256 |
| 230 | self.validate(reass_pkt, p4_reply) |
| 231 | |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 232 | self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1600, 0, 0, 0]) |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 233 | frags, p4_reply = self.generate_ip4_frags(3123, 1200) |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 234 | self.pg_enable_capture() |
| 235 | self.pg1.add_stream(frags) |
| 236 | self.pg_start() |
| 237 | rx = self.pg0.get_capture(2) |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 238 | reass_pkt = reassemble4(rx) |
Ole Troan | 7eb9d96 | 2018-08-10 14:39:48 +0200 | [diff] [blame] | 239 | p4_reply.id = 512 |
| 240 | self.validate(reass_pkt, p4_reply) |
| 241 | |
Neale Ranns | 0b6a857 | 2019-10-30 17:34:14 +0000 | [diff] [blame] | 242 | # send large packets through the tunnel, expect them to be fragmented |
| 243 | self.vapi.sw_interface_set_mtu(sw_if_index, [600, 0, 0, 0]) |
| 244 | |
| 245 | p4 = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 246 | IP(src="1.2.3.4", dst="130.67.0.1", tos=42) / |
| 247 | UDP(sport=1234, dport=1234) / Raw(b'Q' * 1000)) |
| 248 | rx = self.send_and_expect(self.pg0, p4 * 15, self.pg1, 30) |
| 249 | inners = [] |
| 250 | for p in rx: |
| 251 | inners.append(p[IP].payload) |
| 252 | reass_pkt = reassemble4(inners) |
| 253 | for p in reass_pkt: |
| 254 | self.assert_packet_checksums_valid(p) |
| 255 | self.assertEqual(p[IP].ttl, 63) |
| 256 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 257 | def test_ipip_create(self): |
| 258 | """ ipip create / delete interface test """ |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 259 | rv = ipip_add_tunnel(self, '1.2.3.4', '2.3.4.5') |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 260 | sw_if_index = rv.sw_if_index |
| 261 | self.vapi.ipip_del_tunnel(sw_if_index) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 262 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 263 | def test_ipip_vrf_create(self): |
| 264 | """ ipip create / delete interface VRF test """ |
| 265 | |
| 266 | t = VppIpTable(self, 20) |
| 267 | t.add_vpp_config() |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 268 | rv = ipip_add_tunnel(self, '1.2.3.4', '2.3.4.5', table_id=20) |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 269 | sw_if_index = rv.sw_if_index |
| 270 | self.vapi.ipip_del_tunnel(sw_if_index) |
| 271 | |
| 272 | def payload(self, len): |
| 273 | return 'x' * len |
| 274 | |
| 275 | |
| 276 | class TestIPIP6(VppTestCase): |
| 277 | """ IPIP6 Test Case """ |
| 278 | |
| 279 | @classmethod |
| 280 | def setUpClass(cls): |
| 281 | super(TestIPIP6, cls).setUpClass() |
| 282 | cls.create_pg_interfaces(range(2)) |
| 283 | cls.interfaces = list(cls.pg_interfaces) |
| 284 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 285 | @classmethod |
| 286 | def tearDownClass(cls): |
| 287 | super(TestIPIP6, cls).tearDownClass() |
| 288 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 289 | def setUp(self): |
Paul Vinciguerra | 8d991d9 | 2019-01-25 14:05:48 -0800 | [diff] [blame] | 290 | super(TestIPIP6, self).setUp() |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 291 | for i in self.interfaces: |
| 292 | i.admin_up() |
| 293 | i.config_ip4() |
| 294 | i.config_ip6() |
| 295 | i.disable_ipv6_ra() |
| 296 | i.resolve_arp() |
| 297 | i.resolve_ndp() |
| 298 | self.setup_tunnel() |
| 299 | |
| 300 | def tearDown(self): |
| 301 | if not self.vpp_dead: |
| 302 | self.destroy_tunnel() |
| 303 | for i in self.pg_interfaces: |
| 304 | i.unconfig_ip4() |
| 305 | i.unconfig_ip6() |
| 306 | i.admin_down() |
| 307 | super(TestIPIP6, self).tearDown() |
| 308 | |
| 309 | def setup_tunnel(self): |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 310 | # IPv6 transport |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 311 | rv = ipip_add_tunnel(self, |
| 312 | self.pg0.local_ip6, |
| 313 | self.pg1.remote_ip6, |
| 314 | tc_tos=255) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 315 | |
| 316 | sw_if_index = rv.sw_if_index |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 317 | self.tunnel_if_index = sw_if_index |
Ole Troan | 46c1c11 | 2018-03-14 20:39:40 +0100 | [diff] [blame] | 318 | self.vapi.sw_interface_set_flags(sw_if_index, 1) |
| 319 | self.vapi.sw_interface_set_unnumbered( |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 320 | sw_if_index=self.pg0.sw_if_index, |
| 321 | unnumbered_sw_if_index=sw_if_index) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 322 | |
| 323 | # Add IPv4 and IPv6 routes via tunnel interface |
| 324 | ip4_via_tunnel = VppIpRoute( |
| 325 | self, "130.67.0.0", 16, |
| 326 | [VppRoutePath("0.0.0.0", |
| 327 | sw_if_index, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 328 | proto=FibPathProto.FIB_PATH_NH_PROTO_IP4)]) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 329 | ip4_via_tunnel.add_vpp_config() |
| 330 | |
| 331 | ip6_via_tunnel = VppIpRoute( |
| 332 | self, "dead::", 16, |
| 333 | [VppRoutePath("::", |
| 334 | sw_if_index, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 335 | proto=FibPathProto.FIB_PATH_NH_PROTO_IP6)]) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 336 | ip6_via_tunnel.add_vpp_config() |
| 337 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 338 | self.tunnel_ip6_via_tunnel = ip6_via_tunnel |
| 339 | self.tunnel_ip4_via_tunnel = ip4_via_tunnel |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 340 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 341 | def destroy_tunnel(self): |
| 342 | # IPv6 transport |
| 343 | self.tunnel_ip4_via_tunnel.remove_vpp_config() |
| 344 | self.tunnel_ip6_via_tunnel.remove_vpp_config() |
| 345 | |
| 346 | rv = self.vapi.ipip_del_tunnel(sw_if_index=self.tunnel_if_index) |
| 347 | |
| 348 | def validate(self, rx, expected): |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 349 | self.assertEqual(rx, expected.__class__(expected)) |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 350 | |
| 351 | def generate_ip6_frags(self, payload_length, fragment_size): |
| 352 | p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) |
| 353 | p_payload = UDP(sport=1234, dport=1234) / self.payload(payload_length) |
| 354 | p_ip6 = IPv6(src="1::1", dst=self.pg0.remote_ip6) |
| 355 | outer_ip6 = (p_ether / IPv6(src=self.pg1.remote_ip6, |
| 356 | dst=self.pg0.local_ip6) / |
| 357 | IPv6ExtHdrFragment() / p_ip6 / p_payload) |
| 358 | frags = fragment6(outer_ip6, fragment_size) |
| 359 | p6_reply = (p_ip6 / p_payload) |
| 360 | p6_reply.hlim -= 1 |
| 361 | return frags, p6_reply |
| 362 | |
| 363 | def generate_ip6_hairpin_frags(self, payload_length, fragment_size): |
| 364 | p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) |
| 365 | p_payload = UDP(sport=1234, dport=1234) / self.payload(payload_length) |
| 366 | p_ip6 = IPv6(src="1::1", dst="dead::1") |
| 367 | outer_ip6 = (p_ether / IPv6(src=self.pg1.remote_ip6, |
| 368 | dst=self.pg0.local_ip6) / |
| 369 | IPv6ExtHdrFragment() / p_ip6 / p_payload) |
| 370 | frags = fragment6(outer_ip6, fragment_size) |
| 371 | p_ip6.hlim -= 1 |
| 372 | p6_reply = (IPv6(src=self.pg0.local_ip6, dst=self.pg1.remote_ip6, |
| 373 | hlim=63) / p_ip6 / p_payload) |
| 374 | |
| 375 | return frags, p6_reply |
| 376 | |
| 377 | def test_encap(self): |
| 378 | """ ip{v4,v6} over ip6 test encap """ |
| 379 | p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) |
| 380 | p_ip6 = IPv6(src="1::1", dst="DEAD::1", tc=42, nh='UDP') |
| 381 | p_ip4 = IP(src="1.2.3.4", dst="130.67.0.1", tos=42) |
| 382 | p_payload = UDP(sport=1234, dport=1234) |
| 383 | |
| 384 | # Encapsulation |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 385 | # IPv6 in to IPv6 tunnel |
| 386 | p6 = (p_ether / p_ip6 / p_payload) |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 387 | p6_reply = (IPv6(src=self.pg0.local_ip6, dst=self.pg1.remote_ip6, |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 388 | hlim=64, tc=42) / |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 389 | p_ip6 / p_payload) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 390 | p6_reply[1].hlim -= 1 |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 391 | rx = self.send_and_expect(self.pg0, p6 * 11, self.pg1) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 392 | for p in rx: |
| 393 | self.validate(p[1], p6_reply) |
| 394 | |
| 395 | # IPv4 in to IPv6 tunnel |
| 396 | p4 = (p_ether / p_ip4 / p_payload) |
| 397 | p4_reply = (IPv6(src=self.pg0.local_ip6, |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 398 | dst=self.pg1.remote_ip6, hlim=64, tc=42) / |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 399 | p_ip4 / p_payload) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 400 | p4_reply[1].ttl -= 1 |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 401 | rx = self.send_and_expect(self.pg0, p4 * 11, self.pg1) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 402 | for p in rx: |
| 403 | self.validate(p[1], p4_reply) |
| 404 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 405 | def test_decap(self): |
| 406 | """ ip{v4,v6} over ip6 test decap """ |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 407 | |
| 408 | p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 409 | p_ip6 = IPv6(src="1::1", dst="DEAD::1", tc=42, nh='UDP') |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 410 | p_ip4 = IP(src="1.2.3.4", dst=self.pg0.remote_ip4) |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 411 | p_payload = UDP(sport=1234, dport=1234) |
| 412 | |
| 413 | # Decapsulation |
| 414 | # IPv6 tunnel to IPv4 |
| 415 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 416 | p4 = (p_ether / IPv6(src=self.pg1.remote_ip6, |
| 417 | dst=self.pg0.local_ip6) / p_ip4 / p_payload) |
| 418 | p4_reply = (p_ip4 / p_payload) |
| 419 | p4_reply.ttl -= 1 |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 420 | rx = self.send_and_expect(self.pg1, p4 * 11, self.pg0) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 421 | for p in rx: |
| 422 | self.validate(p[1], p4_reply) |
| 423 | |
| 424 | # IPv6 tunnel to IPv6 |
| 425 | p_ip6 = IPv6(src="1:2:3::4", dst=self.pg0.remote_ip6) |
| 426 | p6 = (p_ether / IPv6(src=self.pg1.remote_ip6, |
| 427 | dst=self.pg0.local_ip6) / p_ip6 / p_payload) |
| 428 | p6_reply = (p_ip6 / p_payload) |
| 429 | p6_reply.hlim = 63 |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 430 | rx = self.send_and_expect(self.pg1, p6 * 11, self.pg0) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 431 | for p in rx: |
| 432 | self.validate(p[1], p6_reply) |
| 433 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 434 | def test_frag(self): |
| 435 | """ ip{v4,v6} over ip6 test frag """ |
| 436 | |
| 437 | p_ether = Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac) |
| 438 | p_ip6 = IPv6(src="1::1", dst="DEAD::1", tc=42, nh='UDP') |
| 439 | p_ip4 = IP(src="1.2.3.4", dst=self.pg0.remote_ip4) |
| 440 | p_payload = UDP(sport=1234, dport=1234) |
| 441 | |
| 442 | # |
| 443 | # Fragmentation / Reassembly and Re-fragmentation |
| 444 | # |
| 445 | rv = self.vapi.ip_reassembly_enable_disable( |
| 446 | sw_if_index=self.pg1.sw_if_index, |
| 447 | enable_ip6=1) |
| 448 | |
Klement Sekera | 3a343d4 | 2019-05-16 14:35:46 +0200 | [diff] [blame] | 449 | self.vapi.ip_reassembly_set(timeout_ms=1000, max_reassemblies=1000, |
| 450 | max_reassembly_length=1000, |
| 451 | expire_walk_interval_ms=10000, |
| 452 | is_ip6=1) |
| 453 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 454 | # Send lots of fragments, verify reassembled packet |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 455 | before_cnt = self.statistics.get_err_counter( |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 456 | '/err/ipip6-input/packets decapsulated') |
| 457 | frags, p6_reply = self.generate_ip6_frags(3131, 1400) |
| 458 | f = [] |
| 459 | for i in range(0, 1000): |
| 460 | f.extend(frags) |
| 461 | self.pg1.add_stream(f) |
| 462 | self.pg_enable_capture() |
| 463 | self.pg_start() |
| 464 | rx = self.pg0.get_capture(1000) |
| 465 | |
| 466 | for p in rx: |
| 467 | self.validate(p[1], p6_reply) |
| 468 | |
Ole Troan | 233e468 | 2019-05-16 15:01:34 +0200 | [diff] [blame] | 469 | cnt = self.statistics.get_err_counter( |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 470 | '/err/ipip6-input/packets decapsulated') |
| 471 | self.assertEqual(cnt, before_cnt + 1000) |
| 472 | |
| 473 | f = [] |
| 474 | r = [] |
| 475 | # TODO: Check out why reassembly of atomic fragments don't work |
| 476 | for i in range(10, 90): |
| 477 | frags, p6_reply = self.generate_ip6_frags(i * 100, 1000) |
| 478 | f.extend(frags) |
| 479 | r.extend(p6_reply) |
| 480 | self.pg_enable_capture() |
| 481 | self.pg1.add_stream(f) |
| 482 | self.pg_start() |
| 483 | rx = self.pg0.get_capture(80) |
| 484 | i = 0 |
| 485 | for p in rx: |
| 486 | self.validate(p[1], r[i]) |
| 487 | i += 1 |
| 488 | |
| 489 | # Simple fragmentation |
| 490 | p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) |
| 491 | self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1280, 0, 0, 0]) |
| 492 | |
| 493 | # IPv6 in to IPv6 tunnel |
| 494 | p_payload = UDP(sport=1234, dport=1234) / self.payload(1300) |
| 495 | |
| 496 | p6 = (p_ether / p_ip6 / p_payload) |
| 497 | p6_reply = (IPv6(src=self.pg0.local_ip6, dst=self.pg1.remote_ip6, |
| 498 | hlim=63, tc=42) / |
| 499 | p_ip6 / p_payload) |
| 500 | p6_reply[1].hlim -= 1 |
| 501 | self.pg_enable_capture() |
| 502 | self.pg0.add_stream(p6) |
| 503 | self.pg_start() |
| 504 | rx = self.pg1.get_capture(2) |
| 505 | |
| 506 | # Scapy defragment doesn't deal well with multiple layers |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 507 | # of same type / Ethernet header first |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 508 | f = [p[1] for p in rx] |
| 509 | reass_pkt = defragment6(f) |
| 510 | self.validate(reass_pkt, p6_reply) |
| 511 | |
| 512 | # Now try with re-fragmentation |
| 513 | # |
| 514 | # Send large fragments to tunnel head-end, for the tunnel head end |
| 515 | # to reassemble and then refragment out the tunnel again. |
| 516 | # Hair-pinning |
| 517 | # |
| 518 | self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1280, 0, 0, 0]) |
| 519 | frags, p6_reply = self.generate_ip6_hairpin_frags(8000, 1200) |
| 520 | self.pg_enable_capture() |
| 521 | self.pg1.add_stream(frags) |
| 522 | self.pg_start() |
| 523 | rx = self.pg1.get_capture(7) |
| 524 | f = [p[1] for p in rx] |
| 525 | reass_pkt = defragment6(f) |
| 526 | p6_reply.id = 256 |
| 527 | self.validate(reass_pkt, p6_reply) |
| 528 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 529 | def test_ipip_create(self): |
| 530 | """ ipip create / delete interface test """ |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 531 | rv = ipip_add_tunnel(self, '1.2.3.4', '2.3.4.5') |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 532 | sw_if_index = rv.sw_if_index |
Ole Troan | 46c1c11 | 2018-03-14 20:39:40 +0100 | [diff] [blame] | 533 | self.vapi.ipip_del_tunnel(sw_if_index) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 534 | |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 535 | def test_ipip_vrf_create(self): |
| 536 | """ ipip create / delete interface VRF test """ |
| 537 | |
| 538 | t = VppIpTable(self, 20) |
| 539 | t.add_vpp_config() |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 540 | rv = ipip_add_tunnel(self, '1.2.3.4', '2.3.4.5', table_id=20) |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 541 | sw_if_index = rv.sw_if_index |
| 542 | self.vapi.ipip_del_tunnel(sw_if_index) |
| 543 | |
Ole Troan | 4146c65 | 2018-08-08 22:23:19 +0200 | [diff] [blame] | 544 | def payload(self, len): |
| 545 | return 'x' * len |
| 546 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 547 | |
| 548 | if __name__ == '__main__': |
| 549 | unittest.main(testRunner=VppTestRunner) |