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_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_LINK (linkt)
126 if (FIB_LINK_MPLS == linkt)
128 adj_nbr_midchain_stack (ladj->adjs[linkt], &tmp);
133 static lisp_gpe_next_protocol_e
134 lisp_gpe_adj_proto_from_fib_link_type (fib_link_t linkt)
139 return (LISP_GPE_NEXT_PROTO_IP4);
141 return (LISP_GPE_NEXT_PROTO_IP6);
142 case FIB_LINK_ETHERNET:
143 return (LISP_GPE_NEXT_PROTO_ETHERNET);
147 return (LISP_GPE_NEXT_PROTO_IP4);
150 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
153 lisp_gpe_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b)
155 /* Fixup the checksum and len fields in the LISP tunnel encap
156 * that was applied at the midchain node */
157 ip_udp_fixup_one (vm, b, is_v4_packet (vlib_buffer_get_current (b)));
161 lisp_gpe_adjacency_find_or_create_and_lock (const locator_pair_t * pair,
162 u32 overlay_table_id, u32 vni)
164 const lisp_gpe_tunnel_t *lgt;
165 lisp_gpe_adjacency_t *ladj;
169 * first find the L3 sub-interface that corresponds to the loacl-rloc and vni
171 l3si = lisp_gpe_sub_interface_find_or_create_and_lock (&pair->lcl_loc,
176 * find an existing or create a new adj
178 lai = lisp_adj_find (&pair->rmt_loc, l3si);
180 if (INDEX_INVALID == lai)
182 const lisp_gpe_sub_interface_t *l3s;
187 pool_get (lisp_adj_pool, ladj);
188 memset (ladj, 0, sizeof (*ladj));
189 lai = (ladj - lisp_adj_pool);
191 ladj->remote_rloc = pair->rmt_loc;
193 /* transfer the lock to the adj */
194 ladj->lisp_l3_sub_index = l3si;
196 l3s = lisp_gpe_sub_interface_get (l3si);
197 ladj->sw_if_index = l3s->sw_if_index;
199 /* if vni is non-default */
201 ladj->flags = LISP_GPE_FLAGS_I;
203 /* work in lisp-gpe not legacy mode */
204 ladj->flags |= LISP_GPE_FLAGS_P;
207 * find the tunnel that will provide the underlying transport
208 * and hence the rewrite.
209 * The RLOC FIB index is default table - always.
211 ladj->tunnel_index = lisp_gpe_tunnel_find_or_create_and_lock (pair, 0);
213 lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
216 * become of child of the RLOC FIB entry so we are updated when
217 * its reachability changes, allowing us to re-stack the midcahins
219 ladj->fib_entry_child_index = fib_entry_child_add (lgt->fib_entry_index,
220 FIB_NODE_TYPE_LISP_ADJ,
222 ip_address_to_fib_prefix (&pair->rmt_loc, &nh);
225 * construct and stack the FIB midchain adjacencies
227 FOR_EACH_FIB_LINK (linkt)
229 if (FIB_LINK_MPLS == linkt)
232 ladj->adjs[linkt] = adj_nbr_add_or_lock (nh.fp_proto,
238 lisp_gpe_tunnel_build_rewrite (lgt, ladj,
239 lisp_gpe_adj_proto_from_fib_link_type
242 adj_nbr_midchain_update_rewrite (ladj->adjs[linkt],
244 (FIB_LINK_ETHERNET == linkt ?
245 ADJ_MIDCHAIN_FLAG_NO_COUNT :
246 ADJ_MIDCHAIN_FLAG_NONE), rewrite);
251 lisp_gpe_adj_stack (ladj);
253 lisp_adj_insert (&ladj->remote_rloc, ladj->lisp_l3_sub_index, lai);
257 /* unlock the interface from the find. */
258 lisp_gpe_sub_interface_unlock (l3si);
259 ladj = lisp_gpe_adjacency_get_i (lai);
268 * @brief Get a pointer to a tunnel from a pointer to a FIB node
270 static lisp_gpe_adjacency_t *
271 lisp_gpe_adjacency_from_fib_node (const fib_node_t * node)
273 return ((lisp_gpe_adjacency_t *)
275 STRUCT_OFFSET_OF (lisp_gpe_adjacency_t, fib_node)));
279 lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_t * ladj)
282 * no children so we are not counting locks. no-op.
283 * at least not counting
285 lisp_adj_remove (&ladj->remote_rloc, ladj->lisp_l3_sub_index);
288 * unlock the resources this adj holds
290 lisp_gpe_tunnel_unlock (ladj->tunnel_index);
291 lisp_gpe_sub_interface_unlock (ladj->lisp_l3_sub_index);
293 pool_put (lisp_adj_pool, ladj);
297 lisp_gpe_adjacency_unlock (index_t lai)
299 lisp_gpe_adjacency_t *ladj;
301 ladj = lisp_gpe_adjacency_get_i (lai);
305 if (0 == ladj->locks)
307 lisp_gpe_adjacency_last_lock_gone (ladj);
311 const lisp_gpe_adjacency_t *
312 lisp_gpe_adjacency_get (index_t lai)
314 return (lisp_gpe_adjacency_get_i (lai));
319 * @brief LISP GPE tunnel back walk
321 * The FIB entry through which this tunnel resolves has been updated.
322 * re-stack the midchain on the new forwarding.
324 static fib_node_back_walk_rc_t
325 lisp_gpe_adjacency_back_walk (fib_node_t * node,
326 fib_node_back_walk_ctx_t * ctx)
328 lisp_gpe_adj_stack (lisp_gpe_adjacency_from_fib_node (node));
330 return (FIB_NODE_BACK_WALK_CONTINUE);
334 lisp_gpe_adjacency_get_fib_node (fib_node_index_t index)
336 lisp_gpe_adjacency_t *ladj;
338 ladj = pool_elt_at_index (lisp_adj_pool, index);
339 return (&ladj->fib_node);
343 lisp_gpe_adjacency_last_fib_lock_gone (fib_node_t * node)
345 lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_from_fib_node (node));
348 const static fib_node_vft_t lisp_gpe_tuennel_vft = {
349 .fnv_get = lisp_gpe_adjacency_get_fib_node,
350 .fnv_back_walk = lisp_gpe_adjacency_back_walk,
351 .fnv_last_lock = lisp_gpe_adjacency_last_fib_lock_gone,
355 format_lisp_gpe_adjacency (u8 * s, va_list * args)
357 lisp_gpe_adjacency_t *ladj = va_arg (*args, lisp_gpe_adjacency_t *);
358 lisp_gpe_adjacency_format_flags_t flags =
359 va_arg (args, lisp_gpe_adjacency_format_flags_t);
361 if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
364 format (s, "index %d locks:%d\n", ladj - lisp_adj_pool, ladj->locks);
367 s = format (s, " vni: %d,", ladj->vni);
368 s = format (s, " remote-RLOC: %U,", format_ip_address, &ladj->remote_rloc);
370 if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
372 s = format (s, " %U\n",
373 format_lisp_gpe_sub_interface,
374 lisp_gpe_sub_interface_get (ladj->lisp_l3_sub_index));
375 s = format (s, " %U\n",
376 format_lisp_gpe_tunnel,
377 lisp_gpe_tunnel_get (ladj->tunnel_index));
378 s = format (s, " FIB adjacencies: IPV4:%d IPv6:%d L2:%d\n",
379 ladj->adjs[FIB_LINK_IP4],
380 ladj->adjs[FIB_LINK_IP6], ladj->adjs[FIB_LINK_ETHERNET]);
384 s = format (s, " LISP L3 sub-interface index: %d,",
385 ladj->lisp_l3_sub_index);
386 s = format (s, " LISP tunnel index: %d", ladj->tunnel_index);
393 static clib_error_t *
394 lisp_gpe_adjacency_show (vlib_main_t * vm,
395 unformat_input_t * input, vlib_cli_command_t * cmd)
397 lisp_gpe_adjacency_t *ladj;
400 if (pool_elts (lisp_adj_pool) == 0)
401 vlib_cli_output (vm, "No lisp-gpe Adjacencies");
403 if (unformat (input, "%d", &index))
405 ladj = lisp_gpe_adjacency_get_i (index);
406 vlib_cli_output (vm, "%U", format_lisp_gpe_adjacency, ladj,
407 LISP_GPE_ADJ_FORMAT_FLAG_DETAIL);
412 pool_foreach (ladj, lisp_adj_pool,
414 vlib_cli_output (vm, "[%d] %U\n",
415 ladj - lisp_adj_pool,
416 format_lisp_gpe_adjacency, ladj,
417 LISP_GPE_ADJ_FORMAT_FLAG_NONE);
426 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) =
428 .path = "show lisp gpe adjacency",
429 .function = lisp_gpe_adjacency_show,
433 #define LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS (256)
434 #define LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE (1<<20)
436 static clib_error_t *
437 lisp_gpe_adj_module_init (vlib_main_t * vm)
439 BV (clib_bihash_init) (&lisp_adj_db,
440 "Adjacency Neighbour table",
441 LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS,
442 LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE);
444 fib_node_register_type (FIB_NODE_TYPE_LISP_ADJ, &lisp_gpe_tuennel_vft);
448 VLIB_INIT_FUNCTION (lisp_gpe_adj_module_init)
450 * fd.io coding-style-patch-verification: ON
453 * eval: (c-set-style "gnu")