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