Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * gre_interface.c: gre interfaces |
| 3 | * |
| 4 | * Copyright (c) 2012 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 agreed 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 <vnet/vnet.h> |
| 19 | #include <vnet/pg/pg.h> |
| 20 | #include <vnet/gre/gre.h> |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 21 | #include <vnet/ip/format.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 22 | #include <vnet/fib/ip4_fib.h> |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 23 | #include <vnet/fib/ip6_fib.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 24 | #include <vnet/adj/adj_midchain.h> |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 25 | #include <vnet/adj/adj_nbr.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 26 | #include <vnet/mpls/mpls.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 27 | |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 28 | static const char *gre_tunnel_type_names[] = GRE_TUNNEL_TYPE_NAMES; |
| 29 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 30 | static inline u64 |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 31 | gre4_mk_key (const ip4_address_t * src, |
| 32 | const ip4_address_t * dst, u32 out_fib_index) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 33 | { |
| 34 | // FIXME. the fib index should be part of the key |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 35 | return ((u64) src->as_u32 << 32 | (u64) dst->as_u32); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static u8 * |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 39 | format_gre_tunnel_type (u8 * s, va_list * args) |
| 40 | { |
| 41 | gre_tunnel_type_t type = va_arg (*args, gre_tunnel_type_t); |
| 42 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 43 | return (format (s, "%s", gre_tunnel_type_names[type])); |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static u8 * |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 47 | format_gre_tunnel (u8 * s, va_list * args) |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 48 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 49 | gre_tunnel_t *t = va_arg (*args, gre_tunnel_t *); |
| 50 | gre_main_t *gm = &gre_main; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 51 | u8 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0; |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 52 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 53 | if (!is_ipv6) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 54 | s = format (s, |
| 55 | "[%d] %U (src) %U (dst) payload %U outer_fib_index %d", |
| 56 | t - gm->tunnels, |
| 57 | format_ip4_address, &t->tunnel_src.ip4, |
| 58 | format_ip4_address, &t->tunnel_dst.fp_addr.ip4, |
| 59 | format_gre_tunnel_type, t->type, t->outer_fib_index); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 60 | else |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 61 | s = format (s, |
| 62 | "[%d] %U (src) %U (dst) payload %U outer_fib_index %d", |
| 63 | t - gm->tunnels, |
| 64 | format_ip6_address, &t->tunnel_src.ip6, |
| 65 | format_ip6_address, &t->tunnel_dst.fp_addr.ip6, |
| 66 | format_gre_tunnel_type, t->type, t->outer_fib_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 67 | |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 68 | return s; |
| 69 | } |
| 70 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 71 | static gre_tunnel_t * |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 72 | gre_tunnel_db_find (const ip46_address_t * src, |
| 73 | const ip46_address_t * dst, u32 out_fib_index, u8 is_ipv6) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 74 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 75 | gre_main_t *gm = &gre_main; |
| 76 | uword *p; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 77 | u64 key4, key6[4]; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 78 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 79 | if (!is_ipv6) |
| 80 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 81 | key4 = gre4_mk_key (&src->ip4, &dst->ip4, out_fib_index); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 82 | p = hash_get (gm->tunnel_by_key4, key4); |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | key6[0] = src->ip6.as_u64[0]; |
| 87 | key6[1] = src->ip6.as_u64[1]; |
| 88 | key6[2] = dst->ip6.as_u64[0]; |
| 89 | key6[3] = dst->ip6.as_u64[1]; |
| 90 | p = hash_get_mem (gm->tunnel_by_key6, key6); |
| 91 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 92 | |
| 93 | if (NULL == p) |
| 94 | return (NULL); |
| 95 | |
| 96 | return (pool_elt_at_index (gm->tunnels, p[0])); |
| 97 | } |
| 98 | |
| 99 | static void |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 100 | gre_tunnel_db_add (const gre_tunnel_t * t) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 101 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 102 | gre_main_t *gm = &gre_main; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 103 | u64 key4, key6[4], *key6_copy; |
| 104 | u8 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 105 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 106 | if (!is_ipv6) |
| 107 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 108 | key4 = gre4_mk_key (&t->tunnel_src.ip4, &t->tunnel_dst.fp_addr.ip4, |
| 109 | t->outer_fib_index); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 110 | hash_set (gm->tunnel_by_key4, key4, t - gm->tunnels); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | key6[0] = t->tunnel_src.ip6.as_u64[0]; |
| 115 | key6[1] = t->tunnel_src.ip6.as_u64[1]; |
| 116 | key6[2] = t->tunnel_dst.fp_addr.ip6.as_u64[0]; |
| 117 | key6[3] = t->tunnel_dst.fp_addr.ip6.as_u64[1]; |
| 118 | key6_copy = clib_mem_alloc (sizeof (key6)); |
| 119 | clib_memcpy (key6_copy, key6, sizeof (key6)); |
| 120 | hash_set_mem (gm->tunnel_by_key6, key6_copy, t - gm->tunnels); |
| 121 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 125 | gre_tunnel_db_remove (const gre_tunnel_t * t) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 126 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 127 | gre_main_t *gm = &gre_main; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 128 | u64 key4, key6[4]; |
| 129 | u8 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 130 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 131 | if (!is_ipv6) |
| 132 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 133 | key4 = gre4_mk_key (&t->tunnel_src.ip4, &t->tunnel_dst.fp_addr.ip4, |
| 134 | t->outer_fib_index); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 135 | hash_unset (gm->tunnel_by_key4, key4); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | key6[0] = t->tunnel_src.ip6.as_u64[0]; |
| 140 | key6[1] = t->tunnel_src.ip6.as_u64[1]; |
| 141 | key6[2] = t->tunnel_dst.fp_addr.ip6.as_u64[0]; |
| 142 | key6[3] = t->tunnel_dst.fp_addr.ip6.as_u64[1]; |
| 143 | hash_unset_mem (gm->tunnel_by_key6, key6); |
| 144 | } |
| 145 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static gre_tunnel_t * |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 149 | gre_tunnel_from_fib_node (fib_node_t * node) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 150 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 151 | ASSERT (FIB_NODE_TYPE_GRE_TUNNEL == node->fn_type); |
| 152 | return ((gre_tunnel_t *) (((char *) node) - |
| 153 | STRUCT_OFFSET_OF (gre_tunnel_t, node))); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 156 | /** |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 157 | * gre_tunnel_stack |
| 158 | * |
| 159 | * 'stack' (resolve the recursion for) the tunnel's midchain adjacency |
| 160 | */ |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 161 | void |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 162 | gre_tunnel_stack (adj_index_t ai) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 163 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 164 | gre_main_t *gm = &gre_main; |
| 165 | ip_adjacency_t *adj; |
| 166 | gre_tunnel_t *gt; |
| 167 | u32 sw_if_index; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 168 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 169 | adj = adj_get (ai); |
| 170 | sw_if_index = adj->rewrite_header.sw_if_index; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 171 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 172 | if ((vec_len (gm->tunnel_index_by_sw_if_index) < sw_if_index) || |
| 173 | (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index])) |
| 174 | return; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 175 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 176 | gt = pool_elt_at_index (gm->tunnels, |
| 177 | gm->tunnel_index_by_sw_if_index[sw_if_index]); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 178 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 179 | /* |
| 180 | * find the adjacency that is contributed by the FIB entry |
| 181 | * that this tunnel resovles via, and use it as the next adj |
| 182 | * in the midchain |
| 183 | */ |
| 184 | if (vnet_hw_interface_get_flags (vnet_get_main (), |
| 185 | gt->hw_if_index) & |
| 186 | VNET_HW_INTERFACE_FLAG_LINK_UP) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 187 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 188 | adj_nbr_midchain_stack (ai, |
| 189 | fib_entry_contribute_ip_forwarding |
| 190 | (gt->fib_entry_index)); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 191 | } |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 192 | else |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 193 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 194 | adj_nbr_midchain_unstack (ai); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @brief Call back when restacking all adjacencies on a GRE interface |
| 200 | */ |
| 201 | static adj_walk_rc_t |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 202 | gre_adj_walk_cb (adj_index_t ai, void *ctx) |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 203 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 204 | gre_tunnel_stack (ai); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 205 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 206 | return (ADJ_WALK_RC_CONTINUE); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 210 | gre_tunnel_restack (gre_tunnel_t * gt) |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 211 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 212 | fib_protocol_t proto; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 213 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 214 | /* |
| 215 | * walk all the adjacencies on th GRE interface and restack them |
| 216 | */ |
| 217 | FOR_EACH_FIB_IP_PROTOCOL (proto) |
| 218 | { |
| 219 | adj_nbr_walk (gt->sw_if_index, proto, gre_adj_walk_cb, NULL); |
| 220 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Function definition to backwalk a FIB node |
| 225 | */ |
| 226 | static fib_node_back_walk_rc_t |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 227 | gre_tunnel_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 228 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 229 | gre_tunnel_restack (gre_tunnel_from_fib_node (node)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 230 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 231 | return (FIB_NODE_BACK_WALK_CONTINUE); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Function definition to get a FIB node from its index |
| 236 | */ |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 237 | static fib_node_t * |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 238 | gre_tunnel_fib_node_get (fib_node_index_t index) |
| 239 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 240 | gre_tunnel_t *gt; |
| 241 | gre_main_t *gm; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 242 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 243 | gm = &gre_main; |
| 244 | gt = pool_elt_at_index (gm->tunnels, index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 245 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 246 | return (>->node); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Function definition to inform the FIB node that its last lock has gone. |
| 251 | */ |
| 252 | static void |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 253 | gre_tunnel_last_lock_gone (fib_node_t * node) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 254 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 255 | /* |
| 256 | * The MPLS GRE tunnel is a root of the graph. As such |
| 257 | * it never has children and thus is never locked. |
| 258 | */ |
| 259 | ASSERT (0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Virtual function table registered by MPLS GRE tunnels |
| 264 | * for participation in the FIB object graph. |
| 265 | */ |
| 266 | const static fib_node_vft_t gre_vft = { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 267 | .fnv_get = gre_tunnel_fib_node_get, |
| 268 | .fnv_last_lock = gre_tunnel_last_lock_gone, |
| 269 | .fnv_back_walk = gre_tunnel_back_walk, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 270 | }; |
| 271 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 272 | static int |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 273 | vnet_gre_tunnel_add (vnet_gre_add_del_tunnel_args_t * a, u32 * sw_if_indexp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 275 | gre_main_t *gm = &gre_main; |
| 276 | vnet_main_t *vnm = gm->vnet_main; |
| 277 | ip4_main_t *im4 = &ip4_main; |
| 278 | ip6_main_t *im6 = &ip6_main; |
| 279 | gre_tunnel_t *t; |
| 280 | vnet_hw_interface_t *hi; |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 281 | u32 hw_if_index, sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | u32 outer_fib_index; |
David Hotham | a8cd309 | 2016-09-19 09:55:07 -0700 | [diff] [blame] | 283 | u8 address[6]; |
| 284 | clib_error_t *error; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 285 | u8 is_ipv6 = a->is_ipv6; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 286 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 287 | if (!is_ipv6) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 288 | outer_fib_index = ip4_fib_index_from_table_id (a->outer_fib_id); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 289 | else |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 290 | outer_fib_index = ip6_fib_index_from_table_id (a->outer_fib_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 292 | if (~0 == outer_fib_index) |
| 293 | return VNET_API_ERROR_NO_SUCH_FIB; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 294 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 295 | t = gre_tunnel_db_find (&a->src, &a->dst, a->outer_fib_id, a->is_ipv6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 296 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 297 | if (NULL != t) |
| 298 | return VNET_API_ERROR_INVALID_VALUE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 300 | pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES); |
| 301 | memset (t, 0, sizeof (*t)); |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 302 | fib_node_init (&t->node, FIB_NODE_TYPE_GRE_TUNNEL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 303 | |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 304 | if (a->teb) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 305 | t->type = GRE_TUNNEL_TYPE_TEB; |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 306 | else |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 307 | t->type = GRE_TUNNEL_TYPE_L3; |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 308 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 309 | if (vec_len (gm->free_gre_tunnel_hw_if_indices[t->type]) > 0) |
| 310 | { |
| 311 | vnet_interface_main_t *im = &vnm->interface_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 312 | |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 313 | hw_if_index = gm->free_gre_tunnel_hw_if_indices[t->type] |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 314 | [vec_len (gm->free_gre_tunnel_hw_if_indices[t->type]) - 1]; |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 315 | _vec_len (gm->free_gre_tunnel_hw_if_indices[t->type]) -= 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 316 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 317 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 318 | hi->dev_instance = t - gm->tunnels; |
| 319 | hi->hw_instance = hi->dev_instance; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 320 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 321 | /* clear old stats of freed tunnel before reuse */ |
| 322 | sw_if_index = hi->sw_if_index; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 323 | vnet_interface_counter_lock (im); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 324 | vlib_zero_combined_counter |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 325 | (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], |
| 326 | sw_if_index); |
| 327 | vlib_zero_combined_counter (&im->combined_sw_if_counters |
| 328 | [VNET_INTERFACE_COUNTER_RX], sw_if_index); |
| 329 | vlib_zero_simple_counter (&im->sw_if_counters |
| 330 | [VNET_INTERFACE_COUNTER_DROP], sw_if_index); |
| 331 | vnet_interface_counter_unlock (im); |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 332 | if (GRE_TUNNEL_TYPE_TEB == t->type) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 333 | { |
| 334 | t->l2_tx_arc = vlib_node_add_named_next (vlib_get_main (), |
| 335 | hi->tx_node_index, |
| 336 | "adj-l2-midchain"); |
| 337 | } |
| 338 | } |
| 339 | else |
| 340 | { |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 341 | if (GRE_TUNNEL_TYPE_TEB == t->type) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 342 | { |
| 343 | /* Default MAC address (d00b:eed0:0000 + sw_if_index) */ |
| 344 | memset (address, 0, sizeof (address)); |
| 345 | address[0] = 0xd0; |
| 346 | address[1] = 0x0b; |
| 347 | address[2] = 0xee; |
| 348 | address[3] = 0xd0; |
| 349 | address[4] = t - gm->tunnels; |
David Hotham | a8cd309 | 2016-09-19 09:55:07 -0700 | [diff] [blame] | 350 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 351 | error = ethernet_register_interface (vnm, |
| 352 | gre_device_teb_class.index, |
| 353 | t - gm->tunnels, address, |
| 354 | &hw_if_index, 0); |
David Hotham | a8cd309 | 2016-09-19 09:55:07 -0700 | [diff] [blame] | 355 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 356 | if (error) |
| 357 | { |
| 358 | clib_error_report (error); |
| 359 | return VNET_API_ERROR_INVALID_REGISTRATION; |
| 360 | } |
| 361 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 362 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 363 | t->l2_tx_arc = vlib_node_add_named_next (vlib_get_main (), |
| 364 | hi->tx_node_index, |
| 365 | "adj-l2-midchain"); |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | hw_if_index = vnet_register_interface (vnm, |
| 370 | gre_device_class.index, |
| 371 | t - gm->tunnels, |
| 372 | gre_hw_interface_class.index, |
| 373 | t - gm->tunnels); |
| 374 | } |
David Hotham | a8cd309 | 2016-09-19 09:55:07 -0700 | [diff] [blame] | 375 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 376 | sw_if_index = hi->sw_if_index; |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 377 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 379 | t->hw_if_index = hw_if_index; |
| 380 | t->outer_fib_index = outer_fib_index; |
| 381 | t->sw_if_index = sw_if_index; |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 382 | t->l2_adj_index = ADJ_INDEX_INVALID; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 384 | vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0); |
| 385 | gm->tunnel_index_by_sw_if_index[sw_if_index] = t - gm->tunnels; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 387 | if (!is_ipv6) |
| 388 | { |
| 389 | vec_validate (im4->fib_index_by_sw_if_index, sw_if_index); |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 390 | hi->min_packet_bytes = |
| 391 | 64 + sizeof (gre_header_t) + sizeof (ip4_header_t); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 392 | } |
| 393 | else |
| 394 | { |
| 395 | vec_validate (im6->fib_index_by_sw_if_index, sw_if_index); |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 396 | hi->min_packet_bytes = |
| 397 | 64 + sizeof (gre_header_t) + sizeof (ip6_header_t); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 398 | } |
Chris Luke | 7164900 | 2016-05-06 11:51:54 -0400 | [diff] [blame] | 399 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 400 | hi->per_packet_overhead_bytes = |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 401 | /* preamble */ 8 + /* inter frame gap */ 12; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 402 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 403 | /* Standard default gre MTU. */ |
| 404 | hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 406 | /* |
| 407 | * source the FIB entry for the tunnel's destination |
| 408 | * and become a child thereof. The tunnel will then get poked |
| 409 | * when the forwarding for the entry updates, and the tunnel can |
| 410 | * re-stack accordingly |
| 411 | */ |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 412 | |
| 413 | clib_memcpy (&t->tunnel_src, &a->src, sizeof (t->tunnel_src)); |
| 414 | t->tunnel_dst.fp_len = !is_ipv6 ? 32 : 128; |
| 415 | t->tunnel_dst.fp_proto = !is_ipv6 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 416 | t->tunnel_dst.fp_addr = a->dst; |
| 417 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 418 | gre_tunnel_db_add (t); |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 419 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 420 | t->fib_entry_index = |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 421 | fib_table_entry_special_add (outer_fib_index, |
| 422 | &t->tunnel_dst, |
| 423 | FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 424 | t->sibling_index = |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 425 | fib_entry_child_add (t->fib_entry_index, |
| 426 | FIB_NODE_TYPE_GRE_TUNNEL, t - gm->tunnels); |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 427 | |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 428 | if (GRE_TUNNEL_TYPE_TEB == t->type) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 429 | { |
| 430 | t->l2_adj_index = adj_nbr_add_or_lock (t->tunnel_dst.fp_proto, |
| 431 | VNET_LINK_ETHERNET, |
| 432 | &zero_addr, sw_if_index); |
| 433 | gre_update_adj (vnm, t->sw_if_index, t->l2_adj_index); |
| 434 | } |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 435 | |
| 436 | if (sw_if_indexp) |
| 437 | *sw_if_indexp = sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 442 | static int |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 443 | vnet_gre_tunnel_delete (vnet_gre_add_del_tunnel_args_t * a, |
| 444 | u32 * sw_if_indexp) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 445 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 446 | gre_main_t *gm = &gre_main; |
| 447 | vnet_main_t *vnm = gm->vnet_main; |
| 448 | gre_tunnel_t *t; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 449 | u32 sw_if_index; |
| 450 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 451 | t = gre_tunnel_db_find (&a->src, &a->dst, a->outer_fib_id, a->is_ipv6); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 452 | |
| 453 | if (NULL == t) |
| 454 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 455 | |
| 456 | sw_if_index = t->sw_if_index; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 457 | vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ ); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 458 | /* make sure tunnel is removed from l2 bd or xconnect */ |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 459 | set_int_l2_mode (gm->vlib_main, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0); |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 460 | vec_add1 (gm->free_gre_tunnel_hw_if_indices[t->type], t->hw_if_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 461 | gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0; |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 462 | |
| 463 | if (GRE_TUNNEL_TYPE_TEB == t->type) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 464 | adj_unlock (t->l2_adj_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 465 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 466 | if (t->l2_adj_index != ADJ_INDEX_INVALID) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 467 | adj_unlock (t->l2_adj_index); |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 468 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 469 | fib_entry_child_remove (t->fib_entry_index, t->sibling_index); |
| 470 | fib_table_entry_delete_index (t->fib_entry_index, FIB_SOURCE_RR); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 471 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 472 | gre_tunnel_db_remove (t); |
| 473 | fib_node_deinit (&t->node); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 474 | pool_put (gm->tunnels, t); |
| 475 | |
| 476 | if (sw_if_indexp) |
| 477 | *sw_if_indexp = sw_if_index; |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | int |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 483 | vnet_gre_add_del_tunnel (vnet_gre_add_del_tunnel_args_t * a, |
| 484 | u32 * sw_if_indexp) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 485 | { |
| 486 | if (a->is_add) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 487 | return (vnet_gre_tunnel_add (a, sw_if_indexp)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 488 | else |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 489 | return (vnet_gre_tunnel_delete (a, sw_if_indexp)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 490 | } |
| 491 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 492 | clib_error_t * |
| 493 | gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 494 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 495 | gre_main_t *gm = &gre_main; |
| 496 | vnet_hw_interface_t *hi; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 497 | gre_tunnel_t *t; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 498 | u32 ti; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 499 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 500 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 501 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 502 | if (NULL == gm->tunnel_index_by_sw_if_index || |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 503 | hi->sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) |
| 504 | return (NULL); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 505 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 506 | ti = gm->tunnel_index_by_sw_if_index[hi->sw_if_index]; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 507 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 508 | if (~0 == ti) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 509 | /* not one of ours */ |
| 510 | return (NULL); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 511 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 512 | t = pool_elt_at_index (gm->tunnels, ti); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 513 | |
| 514 | if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 515 | vnet_hw_interface_set_flags (vnm, hw_if_index, |
| 516 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 517 | else |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 518 | vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ ); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 519 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 520 | gre_tunnel_restack (t); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 521 | |
| 522 | return /* no error */ 0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 523 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 524 | |
| 525 | static clib_error_t * |
| 526 | create_gre_tunnel_command_fn (vlib_main_t * vm, |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 527 | unformat_input_t * input, |
| 528 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 529 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 530 | unformat_input_t _line_input, *line_input = &_line_input; |
| 531 | vnet_gre_add_del_tunnel_args_t _a, *a = &_a; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 532 | ip46_address_t src, dst; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 533 | u32 outer_fib_id = 0; |
David Hotham | a8cd309 | 2016-09-19 09:55:07 -0700 | [diff] [blame] | 534 | u8 teb = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 535 | int rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 536 | u32 num_m_args = 0; |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 537 | u8 is_add = 1; |
Pierre Pfister | 78ea9c2 | 2016-05-23 12:51:54 +0100 | [diff] [blame] | 538 | u32 sw_if_index; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 539 | clib_error_t *error = NULL; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 540 | u8 ipv4_set = 0; |
| 541 | u8 ipv6_set = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 542 | |
| 543 | /* Get a line of input. */ |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 544 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 545 | return 0; |
| 546 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 547 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 548 | { |
| 549 | if (unformat (line_input, "del")) |
| 550 | is_add = 0; |
| 551 | else |
| 552 | if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4)) |
| 553 | { |
| 554 | num_m_args++; |
| 555 | ipv4_set = 1; |
| 556 | } |
| 557 | else |
| 558 | if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4)) |
| 559 | { |
| 560 | num_m_args++; |
| 561 | ipv4_set = 1; |
| 562 | } |
| 563 | else |
| 564 | if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6)) |
| 565 | { |
| 566 | num_m_args++; |
| 567 | ipv6_set = 1; |
| 568 | } |
| 569 | else |
| 570 | if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6)) |
| 571 | { |
| 572 | num_m_args++; |
| 573 | ipv6_set = 1; |
| 574 | } |
| 575 | else if (unformat (line_input, "outer-fib-id %d", &outer_fib_id)) |
| 576 | ; |
| 577 | else if (unformat (line_input, "teb")) |
| 578 | teb = 1; |
| 579 | else |
| 580 | { |
| 581 | error = clib_error_return (0, "unknown input `%U'", |
| 582 | format_unformat_error, line_input); |
| 583 | goto done; |
| 584 | } |
| 585 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 586 | |
| 587 | if (num_m_args < 2) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 588 | { |
| 589 | error = clib_error_return (0, "mandatory argument(s) missing"); |
| 590 | goto done; |
| 591 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 592 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 593 | if ((ipv4_set && memcmp (&src.ip4, &dst.ip4, sizeof (src.ip4)) == 0) || |
| 594 | (ipv6_set && memcmp (&src.ip6, &dst.ip6, sizeof (src.ip6)) == 0)) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 595 | { |
| 596 | error = clib_error_return (0, "src and dst are identical"); |
| 597 | goto done; |
| 598 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 599 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 600 | if (ipv4_set && ipv6_set) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 601 | return clib_error_return (0, "both IPv4 and IPv6 addresses specified"); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 602 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 603 | if ((ipv4_set && memcmp (&dst.ip4, &zero_addr.ip4, sizeof (dst.ip4)) == 0) |
| 604 | || (ipv6_set |
| 605 | && memcmp (&dst.ip6, &zero_addr.ip6, sizeof (dst.ip6)) == 0)) |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 606 | { |
| 607 | error = clib_error_return (0, "dst address cannot be zero"); |
| 608 | goto done; |
| 609 | } |
| 610 | |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 611 | memset (a, 0, sizeof (*a)); |
Hongjun Ni | 11bfc2f | 2016-07-22 18:19:19 +0800 | [diff] [blame] | 612 | a->outer_fib_id = outer_fib_id; |
David Hotham | a8cd309 | 2016-09-19 09:55:07 -0700 | [diff] [blame] | 613 | a->teb = teb; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 614 | a->is_ipv6 = ipv6_set; |
| 615 | if (!ipv6_set) |
| 616 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 617 | clib_memcpy (&a->src.ip4, &src.ip4, sizeof (src.ip4)); |
| 618 | clib_memcpy (&a->dst.ip4, &dst.ip4, sizeof (dst.ip4)); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 619 | } |
| 620 | else |
| 621 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 622 | clib_memcpy (&a->src.ip6, &src.ip6, sizeof (src.ip6)); |
| 623 | clib_memcpy (&a->dst.ip6, &dst.ip6, sizeof (dst.ip6)); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 624 | } |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 625 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 626 | if (is_add) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 627 | rv = vnet_gre_tunnel_add (a, &sw_if_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 628 | else |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 629 | rv = vnet_gre_tunnel_delete (a, &sw_if_index); |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 630 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 631 | switch (rv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 632 | { |
| 633 | case 0: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 634 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, |
| 635 | vnet_get_main (), sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 636 | break; |
| 637 | case VNET_API_ERROR_INVALID_VALUE: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 638 | error = clib_error_return (0, "GRE tunnel already exists..."); |
| 639 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 640 | case VNET_API_ERROR_NO_SUCH_FIB: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 641 | error = clib_error_return (0, "outer fib ID %d doesn't exist\n", |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 642 | outer_fib_id); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 643 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 644 | default: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 645 | error = |
| 646 | clib_error_return (0, "vnet_gre_add_del_tunnel returned %d", rv); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 647 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 650 | done: |
| 651 | unformat_free (line_input); |
| 652 | |
| 653 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 656 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 657 | VLIB_CLI_COMMAND (create_gre_tunnel_command, static) = { |
| 658 | .path = "create gre tunnel", |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 659 | .short_help = "create gre tunnel src <addr> dst <addr> " |
David Hotham | a8cd309 | 2016-09-19 09:55:07 -0700 | [diff] [blame] | 660 | "[outer-fib-id <fib>] [teb] [del]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 661 | .function = create_gre_tunnel_command_fn, |
| 662 | }; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 663 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 664 | |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 665 | static clib_error_t * |
| 666 | show_gre_tunnel_command_fn (vlib_main_t * vm, |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 667 | unformat_input_t * input, |
| 668 | vlib_cli_command_t * cmd) |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 669 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 670 | gre_main_t *gm = &gre_main; |
| 671 | gre_tunnel_t *t; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 672 | u32 ti = ~0; |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 673 | |
| 674 | if (pool_elts (gm->tunnels) == 0) |
| 675 | vlib_cli_output (vm, "No GRE tunnels configured..."); |
| 676 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 677 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 678 | { |
| 679 | if (unformat (input, "%d", &ti)) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 680 | ; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 681 | else |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 682 | break; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | if (~0 == ti) |
| 686 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 687 | /* *INDENT-OFF* */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 688 | pool_foreach (t, gm->tunnels, |
| 689 | ({ |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 690 | vlib_cli_output (vm, "%U", format_gre_tunnel, t); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 691 | })); |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 692 | /* *INDENT-ON* */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 693 | } |
| 694 | else |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 695 | { |
| 696 | t = pool_elt_at_index (gm->tunnels, ti); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 697 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 698 | vlib_cli_output (vm, "%U", format_gre_tunnel, t); |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 699 | } |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 704 | /* *INDENT-OFF* */ |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 705 | VLIB_CLI_COMMAND (show_gre_tunnel_command, static) = { |
| 706 | .path = "show gre tunnel", |
| 707 | .function = show_gre_tunnel_command_fn, |
| 708 | }; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 709 | /* *INDENT-ON* */ |
Chris Luke | 27fe48f | 2016-04-28 13:44:38 -0400 | [diff] [blame] | 710 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 711 | /* force inclusion from application's main.c */ |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 712 | clib_error_t * |
| 713 | gre_interface_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 714 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 715 | fib_node_register_type (FIB_NODE_TYPE_GRE_TUNNEL, &gre_vft); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 716 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 717 | return 0; |
| 718 | } |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 719 | |
| 720 | VLIB_INIT_FUNCTION (gre_interface_init); |
| 721 | |
| 722 | /* |
| 723 | * fd.io coding-style-patch-verification: ON |
| 724 | * |
| 725 | * Local Variables: |
| 726 | * eval: (c-set-style "gnu") |
| 727 | * End: |
| 728 | */ |