Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ipip.c: ipip |
| 3 | * |
| 4 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or aipiped to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <vnet/adj/adj_midchain.h> |
| 20 | #include <vnet/ipip/ipip.h> |
| 21 | #include <vnet/vnet.h> |
| 22 | #include <vnet/adj/adj_nbr.h> |
Neale Ranns | 4c3ba81 | 2019-03-26 07:02:58 +0000 | [diff] [blame] | 23 | #include <vnet/adj/adj_midchain.h> |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 24 | #include <vnet/fib/ip4_fib.h> |
| 25 | #include <vnet/fib/ip6_fib.h> |
| 26 | #include <vnet/ip/format.h> |
| 27 | #include <vnet/ipip/ipip.h> |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 28 | #include <vnet/teib/teib.h> |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 29 | #include <vnet/tunnel/tunnel_dp.h> |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 30 | |
| 31 | ipip_main_t ipip_main; |
| 32 | |
| 33 | /* Packet trace structure */ |
| 34 | typedef struct |
| 35 | { |
| 36 | u32 tunnel_id; |
| 37 | u32 length; |
| 38 | ip46_address_t src; |
| 39 | ip46_address_t dst; |
| 40 | } ipip_tx_trace_t; |
| 41 | |
| 42 | u8 * |
| 43 | format_ipip_tx_trace (u8 * s, va_list * args) |
| 44 | { |
| 45 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 46 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 47 | ipip_tx_trace_t *t = va_arg (*args, ipip_tx_trace_t *); |
| 48 | |
| 49 | s = |
| 50 | format (s, "IPIP: tunnel %d len %d src %U dst %U", t->tunnel_id, |
| 51 | t->length, format_ip46_address, &t->src, IP46_TYPE_ANY, |
| 52 | format_ip46_address, &t->dst, IP46_TYPE_ANY); |
| 53 | return s; |
| 54 | } |
| 55 | |
| 56 | static u8 * |
| 57 | ipip_build_rewrite (vnet_main_t * vnm, u32 sw_if_index, |
| 58 | vnet_link_t link_type, const void *dst_address) |
| 59 | { |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 60 | const ip46_address_t *dst; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 61 | ip4_header_t *ip4; |
| 62 | ip6_header_t *ip6; |
| 63 | u8 *rewrite = NULL; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 64 | ipip_tunnel_t *t; |
| 65 | |
| 66 | dst = dst_address; |
| 67 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 68 | |
| 69 | if (!t) |
| 70 | /* not one of ours */ |
| 71 | return (0); |
| 72 | |
| 73 | switch (t->transport) |
| 74 | { |
| 75 | case IPIP_TRANSPORT_IP4: |
| 76 | vec_validate (rewrite, sizeof (*ip4) - 1); |
| 77 | ip4 = (ip4_header_t *) rewrite; |
| 78 | ip4->ip_version_and_header_length = 0x45; |
| 79 | ip4->ttl = 64; |
| 80 | /* fixup ip4 header length, protocol and checksum after-the-fact */ |
| 81 | ip4->src_address.as_u32 = t->tunnel_src.ip4.as_u32; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 82 | ip4->dst_address.as_u32 = dst->ip4.as_u32; |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 83 | if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 84 | ip4_header_set_dscp (ip4, t->dscp); |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 85 | if (t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_SET_DF) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 86 | ip4_header_set_df (ip4); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 87 | |
| 88 | switch (link_type) |
| 89 | { |
| 90 | case VNET_LINK_IP6: |
| 91 | ip4->protocol = IP_PROTOCOL_IPV6; |
| 92 | break; |
| 93 | case VNET_LINK_IP4: |
| 94 | ip4->protocol = IP_PROTOCOL_IP_IN_IP; |
| 95 | break; |
| 96 | default: |
| 97 | break; |
| 98 | } |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 99 | ip4->checksum = ip4_header_checksum (ip4); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 100 | break; |
| 101 | |
| 102 | case IPIP_TRANSPORT_IP6: |
| 103 | vec_validate (rewrite, sizeof (*ip6) - 1); |
| 104 | ip6 = (ip6_header_t *) rewrite; |
| 105 | ip6->ip_version_traffic_class_and_flow_label = |
| 106 | clib_host_to_net_u32 (6 << 28); |
| 107 | ip6->hop_limit = 64; |
| 108 | /* fixup ip6 header length and protocol after-the-fact */ |
| 109 | ip6->src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0]; |
| 110 | ip6->src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1]; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 111 | ip6->dst_address.as_u64[0] = dst->ip6.as_u64[0]; |
| 112 | ip6->dst_address.as_u64[1] = dst->ip6.as_u64[1]; |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 113 | if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)) |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 114 | ip6_set_dscp_network_order (ip6, t->dscp); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 115 | |
| 116 | switch (link_type) |
| 117 | { |
| 118 | case VNET_LINK_IP6: |
| 119 | ip6->protocol = IP_PROTOCOL_IPV6; |
| 120 | break; |
| 121 | case VNET_LINK_IP4: |
| 122 | ip6->protocol = IP_PROTOCOL_IP_IN_IP; |
| 123 | break; |
| 124 | default: |
| 125 | break; |
| 126 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 127 | break; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 128 | } |
| 129 | return (rewrite); |
| 130 | } |
| 131 | |
| 132 | static void |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 133 | ipip64_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b, |
| 134 | const void *data) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 135 | { |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 136 | tunnel_encap_decap_flags_t flags; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 137 | ip4_header_t *ip4; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 138 | |
| 139 | flags = pointer_to_uword (data); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 140 | |
| 141 | ip4 = vlib_buffer_get_current (b); |
| 142 | ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b)); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 143 | tunnel_encap_fixup_6o4 (flags, ((ip6_header_t *) (ip4 + 1)), ip4); |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 144 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 145 | ip4->checksum = ip4_header_checksum (ip4); |
| 146 | } |
| 147 | |
| 148 | static void |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 149 | ipip44_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b, |
| 150 | const void *data) |
| 151 | { |
| 152 | tunnel_encap_decap_flags_t flags; |
| 153 | ip4_header_t *ip4; |
| 154 | |
| 155 | flags = pointer_to_uword (data); |
| 156 | |
| 157 | ip4 = vlib_buffer_get_current (b); |
| 158 | ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b)); |
| 159 | tunnel_encap_fixup_4o4 (flags, ip4 + 1, ip4); |
| 160 | |
| 161 | ip4->checksum = ip4_header_checksum (ip4); |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | ipip46_fixup (vlib_main_t * vm, const ip_adjacency_t * adj, vlib_buffer_t * b, |
| 166 | const void *data) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 167 | { |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 168 | tunnel_encap_decap_flags_t flags; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 169 | ip6_header_t *ip6; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 170 | |
| 171 | flags = pointer_to_uword (data); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 172 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 173 | /* Must set locally originated otherwise we're not allowed to |
| 174 | fragment the packet later */ |
| 175 | b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 176 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 177 | ip6 = vlib_buffer_get_current (b); |
| 178 | ip6->payload_length = |
| 179 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) - |
| 180 | sizeof (*ip6)); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 181 | tunnel_encap_fixup_4o6 (flags, ((ip4_header_t *) (ip6 + 1)), ip6); |
| 182 | } |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 183 | |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 184 | static void |
| 185 | ipip66_fixup (vlib_main_t * vm, |
| 186 | const ip_adjacency_t * adj, vlib_buffer_t * b, const void *data) |
| 187 | { |
| 188 | tunnel_encap_decap_flags_t flags; |
| 189 | ip6_header_t *ip6; |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 190 | |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 191 | flags = pointer_to_uword (data); |
| 192 | |
| 193 | /* Must set locally originated otherwise we're not allowed to |
| 194 | fragment the packet later */ |
| 195 | b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 196 | |
| 197 | ip6 = vlib_buffer_get_current (b); |
| 198 | ip6->payload_length = |
| 199 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) - |
| 200 | sizeof (*ip6)); |
| 201 | tunnel_encap_fixup_6o6 (flags, ip6 + 1, ip6); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void |
| 205 | ipip_tunnel_stack (adj_index_t ai) |
| 206 | { |
| 207 | ip_adjacency_t *adj; |
| 208 | ipip_tunnel_t *t; |
| 209 | u32 sw_if_index; |
| 210 | |
| 211 | adj = adj_get (ai); |
| 212 | sw_if_index = adj->rewrite_header.sw_if_index; |
| 213 | |
| 214 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 215 | if (!t) |
| 216 | return; |
| 217 | |
| 218 | if ((vnet_hw_interface_get_flags (vnet_get_main (), t->hw_if_index) & |
| 219 | VNET_HW_INTERFACE_FLAG_LINK_UP) == 0) |
| 220 | { |
Neale Ranns | 4c3ba81 | 2019-03-26 07:02:58 +0000 | [diff] [blame] | 221 | adj_midchain_delegate_unstack (ai); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 222 | } |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 223 | else |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 224 | { |
Neale Ranns | 4c3ba81 | 2019-03-26 07:02:58 +0000 | [diff] [blame] | 225 | /* *INDENT-OFF* */ |
| 226 | fib_prefix_t dst = { |
| 227 | .fp_len = t->transport == IPIP_TRANSPORT_IP6 ? 128 : 32, |
| 228 | .fp_proto = (t->transport == IPIP_TRANSPORT_IP6 ? |
| 229 | FIB_PROTOCOL_IP6 : |
| 230 | FIB_PROTOCOL_IP4), |
| 231 | .fp_addr = t->tunnel_dst |
| 232 | }; |
| 233 | /* *INDENT-ON* */ |
| 234 | |
| 235 | adj_midchain_delegate_stack (ai, t->fib_index, &dst); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 236 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static adj_walk_rc_t |
| 240 | ipip_adj_walk_cb (adj_index_t ai, void *ctx) |
| 241 | { |
| 242 | ipip_tunnel_stack (ai); |
| 243 | |
| 244 | return (ADJ_WALK_RC_CONTINUE); |
| 245 | } |
| 246 | |
| 247 | static void |
| 248 | ipip_tunnel_restack (ipip_tunnel_t * gt) |
| 249 | { |
| 250 | fib_protocol_t proto; |
| 251 | |
| 252 | /* |
| 253 | * walk all the adjacencies on th IPIP interface and restack them |
| 254 | */ |
| 255 | FOR_EACH_FIB_IP_PROTOCOL (proto) |
| 256 | { |
| 257 | adj_nbr_walk (gt->sw_if_index, proto, ipip_adj_walk_cb, NULL); |
| 258 | } |
| 259 | } |
| 260 | |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 261 | static adj_midchain_fixup_t |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 262 | ipip_get_fixup (const ipip_tunnel_t * t, vnet_link_t lt, adj_flags_t * aflags) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 263 | { |
| 264 | if (t->transport == IPIP_TRANSPORT_IP6 && lt == VNET_LINK_IP6) |
| 265 | return (ipip66_fixup); |
| 266 | if (t->transport == IPIP_TRANSPORT_IP6 && lt == VNET_LINK_IP4) |
| 267 | return (ipip46_fixup); |
| 268 | if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_IP6) |
| 269 | return (ipip64_fixup); |
| 270 | if (t->transport == IPIP_TRANSPORT_IP4 && lt == VNET_LINK_IP4) |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 271 | { |
| 272 | *aflags = *aflags | ADJ_FLAG_MIDCHAIN_FIXUP_IP4O4_HDR; |
| 273 | return (ipip44_fixup); |
| 274 | } |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 275 | |
| 276 | ASSERT (0); |
| 277 | return (ipip44_fixup); |
| 278 | } |
| 279 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 280 | void |
| 281 | ipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai) |
| 282 | { |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 283 | adj_midchain_fixup_t fixup; |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 284 | ipip_tunnel_t *t; |
| 285 | adj_flags_t af; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 286 | |
| 287 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 288 | if (!t) |
| 289 | return; |
| 290 | |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 291 | af = ADJ_FLAG_MIDCHAIN_IP_STACK; |
| 292 | if (VNET_LINK_ETHERNET == adj_get_link_type (ai)) |
| 293 | af |= ADJ_FLAG_MIDCHAIN_NO_COUNT; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 294 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 295 | fixup = ipip_get_fixup (t, adj_get_link_type (ai), &af); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 296 | adj_nbr_midchain_update_rewrite |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 297 | (ai, fixup, |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 298 | uword_to_pointer (t->flags, void *), af, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 299 | ipip_build_rewrite (vnm, sw_if_index, |
| 300 | adj_get_link_type (ai), &t->tunnel_dst)); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 301 | ipip_tunnel_stack (ai); |
| 302 | } |
| 303 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 304 | typedef struct mipip_walk_ctx_t_ |
| 305 | { |
| 306 | const ipip_tunnel_t *t; |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 307 | const teib_entry_t *ne; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 308 | } mipip_walk_ctx_t; |
| 309 | |
| 310 | static adj_walk_rc_t |
| 311 | mipip_mk_complete_walk (adj_index_t ai, void *data) |
| 312 | { |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 313 | adj_midchain_fixup_t fixup; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 314 | mipip_walk_ctx_t *ctx = data; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 315 | adj_flags_t af; |
| 316 | |
| 317 | af = ADJ_FLAG_NONE; |
| 318 | fixup = ipip_get_fixup (ctx->t, adj_get_link_type (ai), &af); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 319 | |
| 320 | adj_nbr_midchain_update_rewrite |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 321 | (ai, fixup, |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 322 | uword_to_pointer (ctx->t->flags, void *), |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 323 | ADJ_FLAG_MIDCHAIN_IP_STACK, ipip_build_rewrite (vnet_get_main (), |
| 324 | ctx->t->sw_if_index, |
| 325 | adj_get_link_type (ai), |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 326 | &teib_entry_get_nh |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 327 | (ctx->ne)->fp_addr)); |
| 328 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 329 | teib_entry_adj_stack (ctx->ne, ai); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 330 | |
| 331 | return (ADJ_WALK_RC_CONTINUE); |
| 332 | } |
| 333 | |
| 334 | static adj_walk_rc_t |
| 335 | mipip_mk_incomplete_walk (adj_index_t ai, void *data) |
| 336 | { |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 337 | adj_midchain_fixup_t fixup; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 338 | ipip_tunnel_t *t = data; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 339 | adj_flags_t af; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 340 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 341 | af = ADJ_FLAG_NONE; |
| 342 | fixup = ipip_get_fixup (t, adj_get_link_type (ai), &af); |
| 343 | |
| 344 | adj_nbr_midchain_update_rewrite (ai, fixup, NULL, ADJ_FLAG_NONE, NULL); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 345 | |
| 346 | adj_midchain_delegate_unstack (ai); |
| 347 | |
| 348 | return (ADJ_WALK_RC_CONTINUE); |
| 349 | } |
| 350 | |
| 351 | void |
| 352 | mipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai) |
| 353 | { |
| 354 | ipip_main_t *gm = &ipip_main; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 355 | adj_midchain_fixup_t fixup; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 356 | ip_adjacency_t *adj; |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 357 | teib_entry_t *ne; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 358 | ipip_tunnel_t *t; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 359 | adj_flags_t af; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 360 | u32 ti; |
| 361 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 362 | af = ADJ_FLAG_NONE; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 363 | adj = adj_get (ai); |
| 364 | ti = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 365 | t = pool_elt_at_index (gm->tunnels, ti); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 366 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 367 | ne = teib_entry_find (sw_if_index, &adj->sub_type.nbr.next_hop); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 368 | |
| 369 | if (NULL == ne) |
| 370 | { |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 371 | // no TEIB entry to provide the next-hop |
| 372 | fixup = ipip_get_fixup (t, adj_get_link_type (ai), &af); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 373 | adj_nbr_midchain_update_rewrite |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 374 | (ai, fixup, uword_to_pointer (t->flags, void *), ADJ_FLAG_NONE, NULL); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 375 | return; |
| 376 | } |
| 377 | |
| 378 | mipip_walk_ctx_t ctx = { |
| 379 | .t = t, |
| 380 | .ne = ne |
| 381 | }; |
| 382 | adj_nbr_walk_nh (sw_if_index, |
| 383 | adj->ia_nh_proto, |
| 384 | &adj->sub_type.nbr.next_hop, mipip_mk_complete_walk, &ctx); |
| 385 | } |
| 386 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 387 | static u8 * |
| 388 | format_ipip_tunnel_name (u8 * s, va_list * args) |
| 389 | { |
| 390 | u32 dev_instance = va_arg (*args, u32); |
| 391 | ipip_main_t *gm = &ipip_main; |
| 392 | ipip_tunnel_t *t; |
| 393 | |
| 394 | if (dev_instance >= vec_len (gm->tunnels)) |
| 395 | return format (s, "<improperly-referenced>"); |
| 396 | |
| 397 | t = pool_elt_at_index (gm->tunnels, dev_instance); |
| 398 | return format (s, "ipip%d", t->user_instance); |
| 399 | } |
| 400 | |
| 401 | static u8 * |
| 402 | format_ipip_device (u8 * s, va_list * args) |
| 403 | { |
| 404 | u32 dev_instance = va_arg (*args, u32); |
| 405 | CLIB_UNUSED (int verbose) = va_arg (*args, int); |
| 406 | |
| 407 | s = format (s, "IPIP tunnel: id %d\n", dev_instance); |
| 408 | return s; |
| 409 | } |
| 410 | |
| 411 | static clib_error_t * |
| 412 | ipip_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 413 | { |
| 414 | vnet_hw_interface_t *hi; |
| 415 | ipip_tunnel_t *t; |
| 416 | |
| 417 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 418 | |
| 419 | t = ipip_tunnel_db_find_by_sw_if_index (hi->sw_if_index); |
| 420 | if (!t) |
| 421 | return 0; |
| 422 | |
| 423 | if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
| 424 | vnet_hw_interface_set_flags (vnm, hw_if_index, |
| 425 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 426 | else |
| 427 | vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ ); |
| 428 | |
| 429 | ipip_tunnel_restack (t); |
| 430 | |
| 431 | return /* no error */ 0; |
| 432 | } |
| 433 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 434 | static int |
| 435 | ipip_tunnel_desc (u32 sw_if_index, |
| 436 | ip46_address_t * src, ip46_address_t * dst, u8 * is_l2) |
| 437 | { |
| 438 | ipip_tunnel_t *t; |
| 439 | |
| 440 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 441 | if (!t) |
| 442 | return -1; |
| 443 | |
| 444 | *src = t->tunnel_src; |
| 445 | *dst = t->tunnel_dst; |
| 446 | *is_l2 = 0; |
| 447 | |
| 448 | return (0); |
| 449 | } |
| 450 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 451 | /* *INDENT-OFF* */ |
| 452 | VNET_DEVICE_CLASS(ipip_device_class) = { |
| 453 | .name = "IPIP tunnel device", |
| 454 | .format_device_name = format_ipip_tunnel_name, |
| 455 | .format_device = format_ipip_device, |
| 456 | .format_tx_trace = format_ipip_tx_trace, |
| 457 | .admin_up_down_function = ipip_interface_admin_up_down, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 458 | .ip_tun_desc = ipip_tunnel_desc, |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 459 | #ifdef SOON |
| 460 | .clear counter = 0; |
| 461 | #endif |
| 462 | }; |
| 463 | |
| 464 | VNET_HW_INTERFACE_CLASS(ipip_hw_interface_class) = { |
| 465 | .name = "IPIP", |
| 466 | //.format_header = format_ipip_header_with_length, |
| 467 | //.unformat_header = unformat_ipip_header, |
| 468 | .build_rewrite = ipip_build_rewrite, |
| 469 | .update_adjacency = ipip_update_adj, |
| 470 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
| 471 | }; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 472 | |
| 473 | VNET_HW_INTERFACE_CLASS(mipip_hw_interface_class) = { |
| 474 | .name = "mIPIP", |
| 475 | //.format_header = format_ipip_header_with_length, |
| 476 | //.unformat_header = unformat_ipip_header, |
| 477 | .build_rewrite = ipip_build_rewrite, |
| 478 | .update_adjacency = mipip_update_adj, |
| 479 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA, |
| 480 | }; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 481 | /* *INDENT-ON* */ |
| 482 | |
| 483 | ipip_tunnel_t * |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 484 | ipip_tunnel_db_find (const ipip_tunnel_key_t * key) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 485 | { |
| 486 | ipip_main_t *gm = &ipip_main; |
| 487 | uword *p; |
| 488 | |
| 489 | p = hash_get_mem (gm->tunnel_by_key, key); |
| 490 | if (!p) |
| 491 | return (NULL); |
| 492 | return (pool_elt_at_index (gm->tunnels, p[0])); |
| 493 | } |
| 494 | |
| 495 | ipip_tunnel_t * |
| 496 | ipip_tunnel_db_find_by_sw_if_index (u32 sw_if_index) |
| 497 | { |
| 498 | ipip_main_t *gm = &ipip_main; |
Eyal Bari | cd30774 | 2018-07-22 12:45:15 +0300 | [diff] [blame] | 499 | if (vec_len (gm->tunnel_index_by_sw_if_index) <= sw_if_index) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 500 | return NULL; |
| 501 | u32 ti = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 502 | if (ti == ~0) |
| 503 | return NULL; |
| 504 | return pool_elt_at_index (gm->tunnels, ti); |
| 505 | } |
| 506 | |
| 507 | void |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 508 | ipip_tunnel_db_add (ipip_tunnel_t * t, const ipip_tunnel_key_t * key) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 509 | { |
| 510 | ipip_main_t *gm = &ipip_main; |
| 511 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 512 | hash_set_mem_alloc (&gm->tunnel_by_key, key, t->dev_instance); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | void |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 516 | ipip_tunnel_db_remove (ipip_tunnel_t * t, const ipip_tunnel_key_t * key) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 517 | { |
| 518 | ipip_main_t *gm = &ipip_main; |
| 519 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 520 | hash_unset_mem_free (&gm->tunnel_by_key, key); |
| 521 | } |
| 522 | |
| 523 | void |
| 524 | ipip_mk_key_i (ipip_transport_t transport, |
| 525 | ipip_mode_t mode, |
| 526 | const ip46_address_t * src, |
| 527 | const ip46_address_t * dst, |
| 528 | u32 fib_index, ipip_tunnel_key_t * key) |
| 529 | { |
| 530 | key->transport = transport; |
| 531 | key->mode = mode; |
| 532 | key->src = *src; |
| 533 | key->dst = *dst; |
| 534 | key->fib_index = fib_index; |
| 535 | key->__pad = 0;; |
| 536 | } |
| 537 | |
| 538 | void |
| 539 | ipip_mk_key (const ipip_tunnel_t * t, ipip_tunnel_key_t * key) |
| 540 | { |
| 541 | ipip_mk_key_i (t->transport, t->mode, |
| 542 | &t->tunnel_src, &t->tunnel_dst, t->fib_index, key); |
| 543 | } |
| 544 | |
| 545 | static void |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 546 | ipip_teib_mk_key (const ipip_tunnel_t * t, |
| 547 | const teib_entry_t * ne, ipip_tunnel_key_t * key) |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 548 | { |
| 549 | const fib_prefix_t *nh; |
| 550 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 551 | nh = teib_entry_get_nh (ne); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 552 | |
| 553 | /* construct the key using mode P2P so it can be found in the DP */ |
| 554 | ipip_mk_key_i (t->transport, IPIP_MODE_P2P, |
| 555 | &t->tunnel_src, &nh->fp_addr, |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 556 | teib_entry_get_fib_index (ne), key); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static void |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 560 | ipip_teib_entry_added (const teib_entry_t * ne) |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 561 | { |
| 562 | ipip_main_t *gm = &ipip_main; |
| 563 | const ip46_address_t *nh; |
| 564 | ipip_tunnel_key_t key; |
| 565 | ipip_tunnel_t *t; |
| 566 | u32 sw_if_index; |
| 567 | u32 t_idx; |
| 568 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 569 | sw_if_index = teib_entry_get_sw_if_index (ne); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 570 | if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index) |
| 571 | return; |
| 572 | |
| 573 | t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 574 | |
| 575 | if (INDEX_INVALID == t_idx) |
| 576 | return; |
| 577 | |
| 578 | t = pool_elt_at_index (gm->tunnels, t_idx); |
| 579 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 580 | ipip_teib_mk_key (t, ne, &key); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 581 | ipip_tunnel_db_add (t, &key); |
| 582 | |
| 583 | // update the rewrites for each of the adjacencies for this next-hop |
| 584 | mipip_walk_ctx_t ctx = { |
| 585 | .t = t, |
| 586 | .ne = ne |
| 587 | }; |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 588 | nh = teib_entry_get_peer (ne); |
| 589 | adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne), |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 590 | (ip46_address_is_ip4 (nh) ? |
| 591 | FIB_PROTOCOL_IP4 : |
| 592 | FIB_PROTOCOL_IP6), nh, mipip_mk_complete_walk, &ctx); |
| 593 | } |
| 594 | |
| 595 | static void |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 596 | ipip_teib_entry_deleted (const teib_entry_t * ne) |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 597 | { |
| 598 | ipip_main_t *gm = &ipip_main; |
| 599 | const ip46_address_t *nh; |
| 600 | ipip_tunnel_key_t key; |
| 601 | ipip_tunnel_t *t; |
| 602 | u32 sw_if_index; |
| 603 | u32 t_idx; |
| 604 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 605 | sw_if_index = teib_entry_get_sw_if_index (ne); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 606 | if (vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index) |
| 607 | return; |
| 608 | |
| 609 | t_idx = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 610 | |
| 611 | if (INDEX_INVALID == t_idx) |
| 612 | return; |
| 613 | |
| 614 | t = pool_elt_at_index (gm->tunnels, t_idx); |
| 615 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 616 | ipip_teib_mk_key (t, ne, &key); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 617 | ipip_tunnel_db_remove (t, &key); |
| 618 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 619 | nh = teib_entry_get_peer (ne); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 620 | |
| 621 | /* make all the adjacencies incomplete */ |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 622 | adj_nbr_walk_nh (teib_entry_get_sw_if_index (ne), |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 623 | (ip46_address_is_ip4 (nh) ? |
| 624 | FIB_PROTOCOL_IP4 : |
| 625 | FIB_PROTOCOL_IP6), nh, mipip_mk_incomplete_walk, t); |
| 626 | } |
| 627 | |
| 628 | static walk_rc_t |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 629 | ipip_tunnel_delete_teib_walk (index_t nei, void *ctx) |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 630 | { |
| 631 | ipip_tunnel_t *t = ctx; |
| 632 | ipip_tunnel_key_t key; |
| 633 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 634 | ipip_teib_mk_key (t, teib_entry_get (nei), &key); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 635 | ipip_tunnel_db_remove (t, &key); |
| 636 | |
| 637 | return (WALK_CONTINUE); |
| 638 | } |
| 639 | |
| 640 | static walk_rc_t |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 641 | ipip_tunnel_add_teib_walk (index_t nei, void *ctx) |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 642 | { |
| 643 | ipip_tunnel_t *t = ctx; |
| 644 | ipip_tunnel_key_t key; |
| 645 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 646 | ipip_teib_mk_key (t, teib_entry_get (nei), &key); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 647 | ipip_tunnel_db_add (t, &key); |
| 648 | |
| 649 | return (WALK_CONTINUE); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 652 | int |
| 653 | ipip_add_tunnel (ipip_transport_t transport, |
| 654 | u32 instance, ip46_address_t * src, ip46_address_t * dst, |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 655 | u32 fib_index, tunnel_encap_decap_flags_t flags, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 656 | ip_dscp_t dscp, tunnel_mode_t tmode, u32 * sw_if_indexp) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 657 | { |
| 658 | ipip_main_t *gm = &ipip_main; |
| 659 | vnet_main_t *vnm = gm->vnet_main; |
| 660 | ip4_main_t *im4 = &ip4_main; |
| 661 | ip6_main_t *im6 = &ip6_main; |
| 662 | ipip_tunnel_t *t; |
| 663 | vnet_hw_interface_t *hi; |
| 664 | u32 hw_if_index, sw_if_index; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 665 | ipip_tunnel_key_t key; |
| 666 | ipip_mode_t mode; |
| 667 | |
| 668 | if (tmode == TUNNEL_MODE_MP && !ip46_address_is_zero (dst)) |
| 669 | return (VNET_API_ERROR_INVALID_DST_ADDRESS); |
| 670 | |
| 671 | mode = (tmode == TUNNEL_MODE_P2P ? IPIP_MODE_P2P : IPIP_MODE_P2MP); |
| 672 | ipip_mk_key_i (transport, mode, src, dst, fib_index, &key); |
| 673 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 674 | t = ipip_tunnel_db_find (&key); |
| 675 | if (t) |
Filip Tehlar | 4c6b1b6 | 2019-11-30 20:49:40 +0000 | [diff] [blame] | 676 | { |
| 677 | if (sw_if_indexp) |
| 678 | sw_if_indexp[0] = t->sw_if_index; |
| 679 | return VNET_API_ERROR_IF_ALREADY_EXISTS; |
| 680 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 681 | |
| 682 | pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 683 | clib_memset (t, 0, sizeof (*t)); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 684 | |
| 685 | /* Reconcile the real dev_instance and a possible requested instance */ |
| 686 | u32 t_idx = t - gm->tunnels; /* tunnel index (or instance) */ |
| 687 | u32 u_idx = instance; /* user specified instance */ |
| 688 | if (u_idx == ~0) |
| 689 | u_idx = t_idx; |
| 690 | if (hash_get (gm->instance_used, u_idx)) |
| 691 | { |
| 692 | pool_put (gm->tunnels, t); |
| 693 | return VNET_API_ERROR_INSTANCE_IN_USE; |
| 694 | } |
| 695 | hash_set (gm->instance_used, u_idx, 1); |
| 696 | |
| 697 | t->dev_instance = t_idx; /* actual */ |
| 698 | t->user_instance = u_idx; /* name */ |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 699 | |
| 700 | hw_if_index = vnet_register_interface (vnm, ipip_device_class.index, t_idx, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 701 | (mode == IPIP_MODE_P2P ? |
| 702 | ipip_hw_interface_class.index : |
| 703 | mipip_hw_interface_class.index), |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 704 | t_idx); |
| 705 | |
| 706 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 707 | sw_if_index = hi->sw_if_index; |
| 708 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 709 | t->mode = mode; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 710 | t->hw_if_index = hw_if_index; |
| 711 | t->fib_index = fib_index; |
| 712 | t->sw_if_index = sw_if_index; |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 713 | t->dscp = dscp; |
| 714 | t->flags = flags; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 715 | t->transport = transport; |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 716 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 717 | vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0); |
| 718 | gm->tunnel_index_by_sw_if_index[sw_if_index] = t_idx; |
| 719 | |
| 720 | if (t->transport == IPIP_TRANSPORT_IP4) |
| 721 | { |
| 722 | vec_validate (im4->fib_index_by_sw_if_index, sw_if_index); |
| 723 | hi->min_packet_bytes = 64 + sizeof (ip4_header_t); |
| 724 | } |
| 725 | else |
| 726 | { |
| 727 | vec_validate (im6->fib_index_by_sw_if_index, sw_if_index); |
| 728 | hi->min_packet_bytes = 64 + sizeof (ip6_header_t); |
| 729 | } |
| 730 | |
Damjan Marion | fe7d4a2 | 2018-04-13 19:43:39 +0200 | [diff] [blame] | 731 | /* Standard default ipip MTU. */ |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 732 | vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 733 | |
| 734 | t->tunnel_src = *src; |
| 735 | t->tunnel_dst = *dst; |
| 736 | |
| 737 | ipip_tunnel_db_add (t, &key); |
| 738 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 739 | if (t->mode == IPIP_MODE_P2MP) |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 740 | teib_walk_itf (t->sw_if_index, ipip_tunnel_add_teib_walk, t); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 741 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 742 | if (sw_if_indexp) |
| 743 | *sw_if_indexp = sw_if_index; |
| 744 | |
| 745 | if (t->transport == IPIP_TRANSPORT_IP6 && !gm->ip6_protocol_registered) |
| 746 | { |
| 747 | ip6_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip6_input_node.index); |
| 748 | ip6_register_protocol (IP_PROTOCOL_IPV6, ipip6_input_node.index); |
| 749 | gm->ip6_protocol_registered = true; |
| 750 | } |
| 751 | else if (t->transport == IPIP_TRANSPORT_IP4 && !gm->ip4_protocol_registered) |
| 752 | { |
| 753 | ip4_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip4_input_node.index); |
| 754 | ip4_register_protocol (IP_PROTOCOL_IPV6, ipip4_input_node.index); |
| 755 | gm->ip4_protocol_registered = true; |
| 756 | } |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | int |
| 761 | ipip_del_tunnel (u32 sw_if_index) |
| 762 | { |
| 763 | ipip_main_t *gm = &ipip_main; |
| 764 | vnet_main_t *vnm = gm->vnet_main; |
| 765 | ipip_tunnel_t *t; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 766 | ipip_tunnel_key_t key; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 767 | |
| 768 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 769 | if (t == NULL) |
| 770 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 771 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 772 | if (t->mode == IPIP_MODE_P2MP) |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 773 | teib_walk_itf (t->sw_if_index, ipip_tunnel_delete_teib_walk, t); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 774 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 775 | vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ ); |
| 776 | gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0; |
| 777 | vnet_delete_hw_interface (vnm, t->hw_if_index); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 778 | hash_unset (gm->instance_used, t->user_instance); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 779 | |
| 780 | ipip_mk_key (t, &key); |
| 781 | ipip_tunnel_db_remove (t, &key); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 782 | pool_put (gm->tunnels, t); |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 787 | const static teib_vft_t ipip_teib_vft = { |
| 788 | .nv_added = ipip_teib_entry_added, |
| 789 | .nv_deleted = ipip_teib_entry_deleted, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 790 | }; |
| 791 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 792 | static clib_error_t * |
| 793 | ipip_init (vlib_main_t * vm) |
| 794 | { |
| 795 | ipip_main_t *gm = &ipip_main; |
| 796 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 797 | clib_memset (gm, 0, sizeof (gm[0])); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 798 | gm->vlib_main = vm; |
| 799 | gm->vnet_main = vnet_get_main (); |
| 800 | gm->tunnel_by_key = |
| 801 | hash_create_mem (0, sizeof (ipip_tunnel_key_t), sizeof (uword)); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 802 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 803 | teib_register (&ipip_teib_vft); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 804 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | VLIB_INIT_FUNCTION (ipip_init); |
| 809 | |
| 810 | /* |
| 811 | * fd.io coding-style-patch-verification: ON |
| 812 | * |
| 813 | * Local Variables: |
| 814 | * eval: (c-set-style "gnu") |
| 815 | * End: |
| 816 | */ |