2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * @brief Common utility functions for IPv4, IPv6 and L2 LISP-GPE adjacencys.
21 #include <vnet/dpo/dpo.h>
22 #include <vnet/lisp-gpe/lisp_gpe_sub_interface.h>
23 #include <vnet/lisp-gpe/lisp_gpe_adjacency.h>
24 #include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
25 #include <vnet/fib/fib_entry.h>
26 #include <vnet/adj/adj_midchain.h>
29 * Memory pool of all adjacencies
31 static lisp_gpe_adjacency_t *lisp_adj_pool;
34 * Hash table of all adjacencies. key:{nh, itf}
35 * We never have an all zeros address since the interfaces are multi-access,
36 * therefore there is no ambiguity between a v4 and v6 next-hop, so we don't
37 * need to add the protocol to the key.
43 #define LISP_ADJ_SET_KEY(_key, _itf, _nh) \
45 _key.key[0] = (_nh)->ip.v6.as_u64[0]; \
46 _key.key[1] = (_nh)->ip.v6.as_u64[1]; \
47 _key.key[2] = (_itf); \
50 static index_t lisp_adj_find (const ip_address_t * addr, u32 sw_if_index)
52 BVT (clib_bihash_kv) kv;
54 LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
56 if (BV (clib_bihash_search) (&lisp_adj_db, &kv, &kv) < 0)
58 return (INDEX_INVALID);
67 lisp_adj_insert (const ip_address_t * addr, u32 sw_if_index, index_t ai)
69 BVT (clib_bihash_kv) kv;
71 LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
74 BV (clib_bihash_add_del) (&lisp_adj_db, &kv, 1);
78 lisp_adj_remove (const ip_address_t * addr, u32 sw_if_index)
80 BVT (clib_bihash_kv) kv;
82 LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
84 BV (clib_bihash_add_del) (&lisp_adj_db, &kv, 0);
87 static lisp_gpe_adjacency_t *
88 lisp_gpe_adjacency_get_i (index_t lai)
90 return (pool_elt_at_index (lisp_adj_pool, lai));
93 fib_forward_chain_type_t
94 lisp_gpe_adj_get_fib_chain_type (const lisp_gpe_adjacency_t * ladj)
96 switch (ip_addr_version (&ladj->remote_rloc))
99 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
101 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
106 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
110 * @brief Stack the tunnel's midchain on the IP forwarding chain of the via
113 lisp_gpe_adj_stack (lisp_gpe_adjacency_t * ladj)
115 const lisp_gpe_tunnel_2_t *lgt;
116 dpo_id_t tmp = DPO_NULL;
119 lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
120 fib_entry_contribute_forwarding (lgt->fib_entry_index,
121 lisp_gpe_adj_get_fib_chain_type (ladj),
124 FOR_EACH_FIB_IP_LINK (linkt)
126 adj_nbr_midchain_stack (ladj->adjs[linkt], &tmp);
131 static lisp_gpe_next_protocol_e
132 lisp_gpe_adj_proto_from_fib_link_type (fib_link_t linkt)
137 return (LISP_GPE_INPUT_NEXT_IP4_INPUT);
139 return (LISP_GPE_INPUT_NEXT_IP6_INPUT);
143 return (LISP_GPE_INPUT_NEXT_DROP);
147 lisp_gpe_adjacency_find_or_create_and_lock (const locator_pair_t * pair,
148 u32 overlay_table_id, u32 vni)
150 const lisp_gpe_tunnel_2_t *lgt;
151 lisp_gpe_adjacency_t *ladj;
155 * first find the L3 sub-interface that corresponds to the loacl-rloc and vni
157 l3si = lisp_gpe_sub_interface_find_or_create_and_lock (&pair->lcl_loc,
162 * find an existing or create a new adj
164 lai = lisp_adj_find (&pair->rmt_loc, l3si);
166 if (INDEX_INVALID == lai)
168 const lisp_gpe_sub_interface_t *l3s;
173 pool_get (lisp_adj_pool, ladj);
174 memset (ladj, 0, sizeof (*ladj));
175 lai = (ladj - lisp_adj_pool);
177 ladj->remote_rloc = pair->rmt_loc;
179 /* transfer the lock to the adj */
180 ladj->lisp_l3_sub_index = l3si;
182 l3s = lisp_gpe_sub_interface_get (l3si);
183 ladj->sw_if_index = l3s->sw_if_index;
185 /* if vni is non-default */
187 ladj->flags = LISP_GPE_FLAGS_I;
189 /* work in lisp-gpe not legacy mode */
190 ladj->flags |= LISP_GPE_FLAGS_P;
193 * find the tunnel that will provide the underlying transport
194 * and hence the rewrite.
195 * The RLOC FIB index is default table - always.
197 ladj->tunnel_index = lisp_gpe_tunnel_find_or_create_and_lock (pair, 0);
199 lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
202 * become of child of the RLOC FIB entry so we are updated when
203 * its reachability changes, allowing us to re-stack the midcahins
205 ladj->fib_entry_child_index = fib_entry_child_add (lgt->fib_entry_index,
206 FIB_NODE_TYPE_LISP_ADJ,
208 ip_address_to_fib_prefix (&pair->rmt_loc, &nh);
211 * construct and stack the FIB midchain adjacencies
213 FOR_EACH_FIB_IP_LINK (linkt)
215 ladj->adjs[linkt] = adj_nbr_add_or_lock (nh.fp_proto,
221 lisp_gpe_tunnel_build_rewrite (lgt, ladj,
222 lisp_gpe_adj_proto_from_fib_link_type
225 adj_nbr_midchain_update_rewrite (ladj->adjs[linkt],
226 vnet_get_sup_hw_interface
228 ladj->sw_if_index)->tx_node_index,
234 lisp_gpe_adj_stack (ladj);
236 lisp_adj_insert (&ladj->remote_rloc, ladj->lisp_l3_sub_index, lai);
240 /* unlock the interface from the find. */
241 lisp_gpe_sub_interface_unlock (l3si);
242 ladj = lisp_gpe_adjacency_get_i (lai);
251 * @brief Get a pointer to a tunnel from a pointer to a FIB node
253 static lisp_gpe_adjacency_t *
254 lisp_gpe_adjacency_from_fib_node (const fib_node_t * node)
256 return ((lisp_gpe_adjacency_t *)
258 STRUCT_OFFSET_OF (lisp_gpe_adjacency_t, fib_node)));
262 lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_t * ladj)
265 * no children so we are not counting locks. no-op.
266 * at least not counting
268 lisp_adj_remove (&ladj->remote_rloc, ladj->lisp_l3_sub_index);
271 * unlock the resources this adj holds
273 lisp_gpe_tunnel_unlock (ladj->tunnel_index);
274 lisp_gpe_sub_interface_unlock (ladj->lisp_l3_sub_index);
276 pool_put (lisp_adj_pool, ladj);
280 lisp_gpe_adjacency_unlock (index_t lai)
282 lisp_gpe_adjacency_t *ladj;
284 ladj = lisp_gpe_adjacency_get_i (lai);
288 if (0 == ladj->locks)
290 lisp_gpe_adjacency_last_lock_gone (ladj);
294 const lisp_gpe_adjacency_t *
295 lisp_gpe_adjacency_get (index_t lai)
297 return (lisp_gpe_adjacency_get_i (lai));
302 * @brief LISP GPE tunnel back walk
304 * The FIB entry through which this tunnel resolves has been updated.
305 * re-stack the midchain on the new forwarding.
307 static fib_node_back_walk_rc_t
308 lisp_gpe_adjacency_back_walk (fib_node_t * node,
309 fib_node_back_walk_ctx_t * ctx)
311 lisp_gpe_adj_stack (lisp_gpe_adjacency_from_fib_node (node));
313 return (FIB_NODE_BACK_WALK_CONTINUE);
317 lisp_gpe_adjacency_get_fib_node (fib_node_index_t index)
319 lisp_gpe_adjacency_t *ladj;
321 ladj = pool_elt_at_index (lisp_adj_pool, index);
322 return (&ladj->fib_node);
326 lisp_gpe_adjacency_last_fib_lock_gone (fib_node_t * node)
328 lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_from_fib_node (node));
331 const static fib_node_vft_t lisp_gpe_tuennel_vft = {
332 .fnv_get = lisp_gpe_adjacency_get_fib_node,
333 .fnv_back_walk = lisp_gpe_adjacency_back_walk,
334 .fnv_last_lock = lisp_gpe_adjacency_last_fib_lock_gone,
338 format_lisp_gpe_adjacency (u8 * s, va_list * args)
340 lisp_gpe_adjacency_t *ladj = va_arg (*args, lisp_gpe_adjacency_t *);
341 lisp_gpe_adjacency_format_flags_t flags =
342 va_arg (args, lisp_gpe_adjacency_format_flags_t);
344 if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
347 format (s, "index %d locks:%d\n", ladj - lisp_adj_pool, ladj->locks);
350 s = format (s, " vni: %d,", ladj->vni);
351 s = format (s, " remote-RLOC: %U,", format_ip_address, &ladj->remote_rloc);
353 if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
355 s = format (s, " %U\n",
356 format_lisp_gpe_sub_interface,
357 lisp_gpe_sub_interface_get (ladj->lisp_l3_sub_index));
358 s = format (s, " %U\n",
359 format_lisp_gpe_tunnel,
360 lisp_gpe_tunnel_get (ladj->tunnel_index));
361 s = format (s, " FIB adjacencies: IPV4:%d IPv6:%d\n",
362 ladj->adjs[FIB_LINK_IP4], ladj->adjs[FIB_LINK_IP6]);
366 s = format (s, " LISP L3 sub-interface index: %d,",
367 ladj->lisp_l3_sub_index);
368 s = format (s, " LISP tunnel index: %d", ladj->tunnel_index);
375 static clib_error_t *
376 lisp_gpe_adjacency_show (vlib_main_t * vm,
377 unformat_input_t * input, vlib_cli_command_t * cmd)
379 lisp_gpe_adjacency_t *ladj;
382 if (pool_elts (lisp_adj_pool) == 0)
383 vlib_cli_output (vm, "No lisp-gpe Adjacencies");
385 if (unformat (input, "%d", &index))
387 ladj = lisp_gpe_adjacency_get_i (index);
388 vlib_cli_output (vm, "%U", format_lisp_gpe_adjacency, ladj,
389 LISP_GPE_ADJ_FORMAT_FLAG_DETAIL);
394 pool_foreach (ladj, lisp_adj_pool,
396 vlib_cli_output (vm, "[%d] %U\n",
397 ladj - lisp_adj_pool,
398 format_lisp_gpe_adjacency, ladj,
399 LISP_GPE_ADJ_FORMAT_FLAG_NONE);
408 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) =
410 .path = "show lisp gpe adjacency",
411 .function = lisp_gpe_adjacency_show,
415 #define LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS (256)
416 #define LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE (1<<20)
418 static clib_error_t *
419 lisp_gpe_adj_module_init (vlib_main_t * vm)
421 BV (clib_bihash_init) (&lisp_adj_db,
422 "Adjacency Neighbour table",
423 LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS,
424 LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE);
426 fib_node_register_type (FIB_NODE_TYPE_LISP_ADJ, &lisp_gpe_tuennel_vft);
430 VLIB_INIT_FUNCTION (lisp_gpe_adj_module_init)
432 * fd.io coding-style-patch-verification: ON
435 * eval: (c-set-style "gnu")