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/load_balance.h>
22 #include <vnet/lisp-cp/control.h>
23 #include <vnet/lisp-cp/lisp_types.h>
24 #include <vnet/lisp-gpe/lisp_gpe_sub_interface.h>
25 #include <vnet/lisp-gpe/lisp_gpe_adjacency.h>
26 #include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
27 #include <vnet/fib/fib_entry.h>
28 #include <vnet/adj/adj_midchain.h>
29 #include <vppinfra/bihash_24_8.h>
30 #include <vppinfra/bihash_template.h>
33 * Memory pool of all adjacencies
35 static lisp_gpe_adjacency_t *lisp_adj_pool;
38 * Hash table of all adjacencies. key:{nh, itf}
39 * We never have an all zeros address since the interfaces are multi-access,
40 * therefore there is no ambiguity between a v4 and v6 next-hop, so we don't
41 * need to add the protocol to the key.
47 #define LISP_ADJ_SET_KEY(_key, _itf, _nh) \
49 _key.key[0] = (_nh)->ip.v6.as_u64[0]; \
50 _key.key[1] = (_nh)->ip.v6.as_u64[1]; \
51 _key.key[2] = (_itf); \
54 static index_t lisp_adj_find (const ip_address_t * addr, u32 sw_if_index)
56 BVT (clib_bihash_kv) kv;
58 LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
60 if (BV (clib_bihash_search) (&lisp_adj_db, &kv, &kv) < 0)
62 return (INDEX_INVALID);
71 lisp_adj_insert (const ip_address_t * addr, u32 sw_if_index, index_t ai)
73 BVT (clib_bihash_kv) kv;
75 LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
78 BV (clib_bihash_add_del) (&lisp_adj_db, &kv, 1);
82 lisp_adj_remove (const ip_address_t * addr, u32 sw_if_index)
84 BVT (clib_bihash_kv) kv;
86 LISP_ADJ_SET_KEY (kv, sw_if_index, addr);
88 BV (clib_bihash_add_del) (&lisp_adj_db, &kv, 0);
91 static lisp_gpe_adjacency_t *
92 lisp_gpe_adjacency_get_i (index_t lai)
94 return (pool_elt_at_index (lisp_adj_pool, lai));
97 fib_forward_chain_type_t
98 lisp_gpe_adj_get_fib_chain_type (const lisp_gpe_adjacency_t * ladj)
100 switch (ip_addr_version (&ladj->remote_rloc))
103 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
105 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
110 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
114 ip46_address_to_ip_address (const ip46_address_t * a, ip_address_t * b)
116 if (ip46_address_is_ip4 (a))
118 clib_memset (b, 0, sizeof (*b));
119 ip_address_set (b, &a->ip4, IP4);
123 ip_address_set (b, &a->ip6, IP6);
128 * @brief Stack the tunnel's midchain on the IP forwarding chain of the via
131 lisp_gpe_adj_stack_one (lisp_gpe_adjacency_t * ladj, adj_index_t ai)
133 const lisp_gpe_tunnel_t *lgt;
134 dpo_id_t tmp = DPO_INVALID;
136 lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
137 fib_entry_contribute_forwarding (lgt->fib_entry_index,
138 lisp_gpe_adj_get_fib_chain_type (ladj),
141 if (DPO_LOAD_BALANCE == tmp.dpoi_type)
144 * post LISP rewrite we will load-balance. However, the LISP encap
145 * is always the same for this adjacency/tunnel and hence the IP/UDP src,dst
146 * hash is always the same result too. So we do that hash now and
147 * stack on the choice.
148 * If the choice is an incomplete adj then we will need a poke when
149 * it becomes complete. This happens since the adj update walk propagates
150 * as far a recursive paths.
152 const dpo_id_t *choice;
156 lb = load_balance_get (tmp.dpoi_index);
158 if (IP4 == ip_addr_version (&ladj->remote_rloc))
160 hash = ip4_compute_flow_hash ((ip4_header_t *) adj_get_rewrite (ai),
165 hash = ip6_compute_flow_hash ((ip6_header_t *) adj_get_rewrite (ai),
170 load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
171 dpo_copy (&tmp, choice);
174 adj_nbr_midchain_stack (ai, &tmp);
179 * @brief Call back when restacking all adjacencies on a GRE interface
182 lisp_gpe_adj_walk_cb (adj_index_t ai, void *ctx)
184 lisp_gpe_adjacency_t *ladj = ctx;
186 lisp_gpe_adj_stack_one (ladj, ai);
188 return (ADJ_WALK_RC_CONTINUE);
192 lisp_gpe_adj_stack (lisp_gpe_adjacency_t * ladj)
194 fib_protocol_t nh_proto;
197 ip_address_to_46 (&ladj->remote_rloc, &nh, &nh_proto);
200 * walk all the adjacencies on th lisp interface and restack them
202 adj_nbr_walk_nh (ladj->sw_if_index,
203 nh_proto, &nh, lisp_gpe_adj_walk_cb, ladj);
206 static lisp_gpe_next_protocol_e
207 lisp_gpe_adj_proto_from_vnet_link_type (vnet_link_t linkt)
212 return (LISP_GPE_NEXT_PROTO_IP4);
214 return (LISP_GPE_NEXT_PROTO_IP6);
215 case VNET_LINK_ETHERNET:
216 return (LISP_GPE_NEXT_PROTO_ETHERNET);
218 return (LISP_GPE_NEXT_PROTO_NSH);
222 return (LISP_GPE_NEXT_PROTO_IP4);
225 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
228 lisp_afi_from_vnet_link_type (vnet_link_t link)
236 case VNET_LINK_ETHERNET:
239 return LISP_AFI_NO_ADDR;
244 lisp_gpe_increment_stats_counters (lisp_cp_main_t * lcm, ip_adjacency_t * adj,
247 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
248 lisp_gpe_adjacency_t *ladj;
252 gid_address_t src, dst;
255 ip46_address_to_ip_address (&adj->sub_type.nbr.next_hop, &rloc);
256 si = vnet_buffer (b)->sw_if_index[VLIB_TX];
257 lai = lisp_adj_find (&rloc, si);
258 ASSERT (INDEX_INVALID != lai);
260 ladj = pool_elt_at_index (lisp_adj_pool, lai);
262 u8 *lisp_data = (u8 *) vlib_buffer_get_current (b);
265 if (is_v4_packet (lisp_data))
266 lisp_data += sizeof (ip4_header_t);
268 lisp_data += sizeof (ip6_header_t);
270 /* skip UDP header */
271 lisp_data += sizeof (udp_header_t);
274 /* skip LISP GPE header */
275 lisp_data += sizeof (lisp_gpe_header_t);
277 i16 saved_current_data = b->current_data;
278 b->current_data = lisp_data - b->data;
280 lisp_afi_e afi = lisp_afi_from_vnet_link_type (adj->ia_link);
281 get_src_and_dst_eids_from_buffer (lcm, b, &src, &dst, afi);
282 b->current_data = saved_current_data;
283 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst, &src);
284 if (PREDICT_FALSE (~0 == di))
286 clib_warning ("dst mapping not found (%U, %U)", format_gid_address,
287 &src, format_gid_address, &dst);
291 feip = hash_get (lcm->fwd_entry_by_mapping_index, di);
292 if (PREDICT_FALSE (!feip))
295 lisp_stats_key_t key;
296 clib_memset (&key, 0, sizeof (key));
297 key.fwd_entry_index = feip[0];
298 key.tunnel_index = ladj->tunnel_index;
300 uword *p = hash_get_mem (lgm->lisp_stats_index_by_key, &key);
303 /* compute payload length starting after GPE */
304 u32 bytes = b->current_length - (lisp_data - b->data - b->current_data);
305 vlib_increment_combined_counter (&lgm->counters, vlib_get_thread_index (),
310 lisp_gpe_fixup (vlib_main_t * vm,
311 ip_adjacency_t * adj, vlib_buffer_t * b, const void *data)
313 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
315 if (lcm->flags & LISP_FLAG_STATS_ENABLED)
316 lisp_gpe_increment_stats_counters (lcm, adj, b);
318 /* Fixup the checksum and len fields in the LISP tunnel encap
319 * that was applied at the midchain node */
320 ip_udp_fixup_one (vm, b, is_v4_packet (vlib_buffer_get_current (b)));
324 * @brief The LISP-GPE interface registered function to update, i.e.
325 * provide an rewrite string for, an adjacency.
328 lisp_gpe_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
330 const lisp_gpe_tunnel_t *lgt;
331 lisp_gpe_adjacency_t *ladj;
338 ip46_address_to_ip_address (&adj->sub_type.nbr.next_hop, &rloc);
341 * find an existing or create a new adj
343 lai = lisp_adj_find (&rloc, sw_if_index);
345 ASSERT (INDEX_INVALID != lai);
347 ladj = pool_elt_at_index (lisp_adj_pool, lai);
348 lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
349 linkt = adj_get_link_type (ai);
350 adj_nbr_midchain_update_rewrite
353 (VNET_LINK_ETHERNET == linkt ?
354 ADJ_FLAG_MIDCHAIN_NO_COUNT :
356 lisp_gpe_tunnel_build_rewrite (lgt, ladj,
357 lisp_gpe_adj_proto_from_vnet_link_type
360 lisp_gpe_adj_stack_one (ladj, ai);
364 lisp_gpe_build_rewrite (vnet_main_t * vnm,
366 vnet_link_t link_type, const void *dst_address)
373 lisp_gpe_adjacency_find_or_create_and_lock (const locator_pair_t * pair,
374 u32 overlay_table_id, u32 vni)
376 const lisp_gpe_sub_interface_t *l3s;
377 const lisp_gpe_tunnel_t *lgt;
378 lisp_gpe_adjacency_t *ladj;
382 * first find the L3 sub-interface that corresponds to the loacl-rloc and vni
384 l3si = lisp_gpe_sub_interface_find_or_create_and_lock (&pair->lcl_loc,
387 l3s = lisp_gpe_sub_interface_get (l3si);
390 * find an existing or create a new adj
392 lai = lisp_adj_find (&pair->rmt_loc, l3s->sw_if_index);
394 if (INDEX_INVALID == lai)
397 pool_get (lisp_adj_pool, ladj);
398 clib_memset (ladj, 0, sizeof (*ladj));
399 lai = (ladj - lisp_adj_pool);
401 ip_address_copy (&ladj->remote_rloc, &pair->rmt_loc);
403 /* transfer the lock to the adj */
404 ladj->lisp_l3_sub_index = l3si;
405 ladj->sw_if_index = l3s->sw_if_index;
407 /* if vni is non-default */
409 ladj->flags = LISP_GPE_FLAGS_I;
411 /* work in lisp-gpe not legacy mode */
412 ladj->flags |= LISP_GPE_FLAGS_P;
415 * find the tunnel that will provide the underlying transport
416 * and hence the rewrite.
417 * The RLOC FIB index is default table - always.
419 ladj->tunnel_index = lisp_gpe_tunnel_find_or_create_and_lock (pair, 0);
421 lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
424 * become of child of the RLOC FIB entry so we are updated when
425 * its reachability changes, allowing us to re-stack the midcahins
427 ladj->fib_entry_child_index = fib_entry_child_add (lgt->fib_entry_index,
428 FIB_NODE_TYPE_LISP_ADJ,
431 lisp_adj_insert (&ladj->remote_rloc, ladj->sw_if_index, lai);
435 /* unlock the interface from the find. */
436 lisp_gpe_sub_interface_unlock (l3si);
437 ladj = lisp_gpe_adjacency_get_i (lai);
446 * @brief Get a pointer to a tunnel from a pointer to a FIB node
448 static lisp_gpe_adjacency_t *
449 lisp_gpe_adjacency_from_fib_node (const fib_node_t * node)
451 return ((lisp_gpe_adjacency_t *)
453 STRUCT_OFFSET_OF (lisp_gpe_adjacency_t, fib_node)));
457 lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_t * ladj)
459 const lisp_gpe_tunnel_t *lgt;
462 * no children so we are not counting locks. no-op.
463 * at least not counting
465 lisp_adj_remove (&ladj->remote_rloc, ladj->sw_if_index);
468 * unlock the resources this adj holds
470 lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
472 fib_entry_child_remove (lgt->fib_entry_index, ladj->fib_entry_child_index);
474 lisp_gpe_tunnel_unlock (ladj->tunnel_index);
475 lisp_gpe_sub_interface_unlock (ladj->lisp_l3_sub_index);
477 pool_put (lisp_adj_pool, ladj);
481 lisp_gpe_adjacency_unlock (index_t lai)
483 lisp_gpe_adjacency_t *ladj;
485 ladj = lisp_gpe_adjacency_get_i (lai);
489 if (0 == ladj->locks)
491 lisp_gpe_adjacency_last_lock_gone (ladj);
495 const lisp_gpe_adjacency_t *
496 lisp_gpe_adjacency_get (index_t lai)
498 return (lisp_gpe_adjacency_get_i (lai));
503 * @brief LISP GPE tunnel back walk
505 * The FIB entry through which this tunnel resolves has been updated.
506 * re-stack the midchain on the new forwarding.
508 static fib_node_back_walk_rc_t
509 lisp_gpe_adjacency_back_walk (fib_node_t * node,
510 fib_node_back_walk_ctx_t * ctx)
512 lisp_gpe_adj_stack (lisp_gpe_adjacency_from_fib_node (node));
514 return (FIB_NODE_BACK_WALK_CONTINUE);
518 lisp_gpe_adjacency_get_fib_node (fib_node_index_t index)
520 lisp_gpe_adjacency_t *ladj;
522 ladj = pool_elt_at_index (lisp_adj_pool, index);
523 return (&ladj->fib_node);
527 lisp_gpe_adjacency_last_fib_lock_gone (fib_node_t * node)
529 lisp_gpe_adjacency_last_lock_gone (lisp_gpe_adjacency_from_fib_node (node));
532 const static fib_node_vft_t lisp_gpe_tuennel_vft = {
533 .fnv_get = lisp_gpe_adjacency_get_fib_node,
534 .fnv_back_walk = lisp_gpe_adjacency_back_walk,
535 .fnv_last_lock = lisp_gpe_adjacency_last_fib_lock_gone,
539 format_lisp_gpe_adjacency (u8 * s, va_list * args)
541 lisp_gpe_adjacency_t *ladj = va_arg (*args, lisp_gpe_adjacency_t *);
542 lisp_gpe_adjacency_format_flags_t flags =
543 va_arg (*args, lisp_gpe_adjacency_format_flags_t);
545 if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
548 format (s, "index %d locks:%d\n", ladj - lisp_adj_pool, ladj->locks);
551 s = format (s, " vni: %d,", ladj->vni);
552 s = format (s, " remote-RLOC: %U,", format_ip_address, &ladj->remote_rloc);
554 if (flags & LISP_GPE_ADJ_FORMAT_FLAG_DETAIL)
556 s = format (s, " %U\n",
557 format_lisp_gpe_sub_interface,
558 lisp_gpe_sub_interface_get (ladj->lisp_l3_sub_index));
559 s = format (s, " %U\n",
560 format_lisp_gpe_tunnel,
561 lisp_gpe_tunnel_get (ladj->tunnel_index));
565 s = format (s, " LISP L3 sub-interface index: %d,",
566 ladj->lisp_l3_sub_index);
567 s = format (s, " LISP tunnel index: %d", ladj->tunnel_index);
574 static clib_error_t *
575 lisp_gpe_adjacency_show (vlib_main_t * vm,
576 unformat_input_t * input, vlib_cli_command_t * cmd)
578 lisp_gpe_adjacency_t *ladj;
581 if (pool_elts (lisp_adj_pool) == 0)
582 vlib_cli_output (vm, "No lisp-gpe Adjacencies");
584 if (unformat (input, "%d", &index))
586 ladj = lisp_gpe_adjacency_get_i (index);
587 vlib_cli_output (vm, "%U", format_lisp_gpe_adjacency, ladj,
588 LISP_GPE_ADJ_FORMAT_FLAG_DETAIL);
593 pool_foreach (ladj, lisp_adj_pool,
595 vlib_cli_output (vm, "[%d] %U\n",
596 ladj - lisp_adj_pool,
597 format_lisp_gpe_adjacency, ladj,
598 LISP_GPE_ADJ_FORMAT_FLAG_NONE);
607 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) =
609 .path = "show gpe adjacency",
610 .function = lisp_gpe_adjacency_show,
614 #define LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS (256)
615 #define LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE (1<<20)
617 static clib_error_t *
618 lisp_gpe_adj_module_init (vlib_main_t * vm)
620 BV (clib_bihash_init) (&lisp_adj_db,
621 "Adjacency Neighbour table",
622 LISP_ADJ_NBR_DEFAULT_HASH_NUM_BUCKETS,
623 LISP_ADJ_NBR_DEFAULT_HASH_MEMORY_SIZE);
625 fib_node_register_type (FIB_NODE_TYPE_LISP_ADJ, &lisp_gpe_tuennel_vft);
629 VLIB_INIT_FUNCTION (lisp_gpe_adj_module_init)
631 * fd.io coding-style-patch-verification: ON
634 * eval: (c-set-style "gnu")