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.
16 #include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
17 #include <vnet/lisp-gpe/lisp_gpe_adjacency.h>
18 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
19 #include <vnet/lisp-cp/lisp_cp_dpo.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/fib/fib_entry.h>
22 #include <vnet/fib/fib_path_list.h>
23 #include <vnet/fib/ip6_fib.h>
24 #include <vnet/fib/ip4_fib.h>
25 #include <vnet/dpo/drop_dpo.h>
26 #include <vnet/dpo/lookup_dpo.h>
27 #include <vnet/dpo/load_balance.h>
28 #include <vnet/adj/adj_midchain.h>
31 * @brief Add route to IP4 or IP6 Destination FIB.
33 * Add a route to the destination FIB that results in the lookup
34 * in the SRC FIB. The SRC FIB is created is it does not yet exist.
36 * @param[in] dst_table_id Destination FIB Table-ID
37 * @param[in] dst_prefix Destination IP prefix.
39 * @return src_fib_index The index/ID of the SRC FIB created.
42 ip_dst_fib_add_route (u32 dst_fib_index, const ip_prefix_t * dst_prefix)
44 fib_node_index_t src_fib_index;
45 fib_prefix_t dst_fib_prefix;
46 fib_node_index_t dst_fei;
48 ASSERT (NULL != dst_prefix);
50 ip_prefix_to_fib_prefix (dst_prefix, &dst_fib_prefix);
53 * lookup the destination prefix in the VRF table and retrieve the
54 * LISP associated data
56 dst_fei = fib_table_lookup_exact_match (dst_fib_index, &dst_fib_prefix);
59 * If the FIB entry is not present, or not LISP sourced, add it
61 if (dst_fei == FIB_NODE_INDEX_INVALID ||
62 NULL == fib_entry_get_source_data (dst_fei, FIB_SOURCE_LISP))
64 dpo_id_t src_lkup_dpo = DPO_INVALID;
66 /* create a new src FIB. */
68 fib_table_create_and_lock (dst_fib_prefix.fp_proto,
69 "LISP-src for [%d,%U]",
71 format_fib_prefix, &dst_fib_prefix);
73 * add src fib default route
75 fib_prefix_t prefix = {
76 .fp_proto = dst_fib_prefix.fp_proto,
78 fib_table_entry_special_dpo_add (src_fib_index, &prefix,
80 FIB_ENTRY_FLAG_EXCLUSIVE,
81 lisp_cp_dpo_get (fib_proto_to_dpo
82 (dst_fib_prefix.fp_proto)));
84 * create a data-path object to perform the source address lookup
87 lookup_dpo_add_or_lock_w_fib_index (src_fib_index,
88 (ip_prefix_version (dst_prefix) ==
91 LOOKUP_INPUT_SRC_ADDR,
92 LOOKUP_TABLE_FROM_CONFIG,
96 * add the entry to the destination FIB that uses the lookup DPO
98 dst_fei = fib_table_entry_special_dpo_add (dst_fib_index,
101 FIB_ENTRY_FLAG_EXCLUSIVE,
105 * the DPO is locked by the FIB entry, and we have no further
108 dpo_unlock (&src_lkup_dpo);
111 * save the SRC FIB index on the entry so we can retrieve it for
114 fib_entry_set_source_data (dst_fei, FIB_SOURCE_LISP, &src_fib_index);
119 * destination FIB entry already present
121 src_fib_index = *(u32 *) fib_entry_get_source_data (dst_fei,
125 return (src_fib_index);
129 * @brief Del route to IP4 or IP6 SD FIB.
131 * Remove routes from both destination and source FIBs.
133 * @param[in] src_fib_index The index/ID of the SRC FIB
134 * @param[in] src_prefix Source IP prefix.
135 * @param[in] dst_fib_index The index/ID of the DST FIB
136 * @param[in] dst_prefix Destination IP prefix.
139 ip_src_dst_fib_del_route (u32 src_fib_index,
140 const ip_prefix_t * src_prefix,
141 u32 dst_fib_index, const ip_prefix_t * dst_prefix)
143 fib_prefix_t dst_fib_prefix, src_fib_prefix;
147 ASSERT (NULL != dst_prefix);
148 ASSERT (NULL != src_prefix);
150 ip_prefix_to_fib_prefix (dst_prefix, &dst_fib_prefix);
151 ip_prefix_to_fib_prefix (src_prefix, &src_fib_prefix);
153 fib_table_entry_delete (src_fib_index, &src_fib_prefix, FIB_SOURCE_LISP);
155 /* check if only default left or empty */
156 fib_prefix_t default_pref = {
157 .fp_proto = dst_fib_prefix.fp_proto
160 if (fib_table_lookup_exact_match (src_fib_index,
161 &default_pref) != FIB_NODE_INDEX_INVALID)
164 n_entries = fib_table_get_num_entries (src_fib_index,
165 src_fib_prefix.fp_proto,
167 if (n_entries == 0 || (have_default && n_entries == 1))
170 * remove src FIB default route
173 fib_table_entry_special_remove (src_fib_index, &default_pref,
177 * there's nothing left now, unlock the source FIB and the
180 fib_table_entry_special_remove (dst_fib_index,
181 &dst_fib_prefix, FIB_SOURCE_LISP);
182 fib_table_unlock (src_fib_index, src_fib_prefix.fp_proto);
187 * @brief Add route to IP4 or IP6 SRC FIB.
189 * Adds a route to in the LISP SRC FIB with the result of the route
190 * being the DPO passed.
192 * @param[in] src_fib_index The index/ID of the SRC FIB
193 * @param[in] src_prefix Source IP prefix.
194 * @param[in] src_dpo The DPO the route will link to.
197 ip_src_fib_add_route_w_dpo (u32 src_fib_index,
198 const ip_prefix_t * src_prefix,
199 const dpo_id_t * src_dpo)
201 fib_prefix_t src_fib_prefix;
203 ip_prefix_to_fib_prefix (src_prefix, &src_fib_prefix);
206 * add the entry into the source fib.
208 fib_node_index_t src_fei;
210 src_fei = fib_table_lookup_exact_match (src_fib_index, &src_fib_prefix);
212 if (FIB_NODE_INDEX_INVALID == src_fei ||
213 !fib_entry_is_sourced (src_fei, FIB_SOURCE_LISP))
215 fib_table_entry_special_dpo_add (src_fib_index,
218 FIB_ENTRY_FLAG_EXCLUSIVE, src_dpo);
222 static fib_route_path_t *
223 lisp_gpe_mk_fib_paths (const lisp_fwd_path_t * paths)
225 const lisp_gpe_adjacency_t *ladj;
226 fib_route_path_t *rpaths = NULL;
230 vec_validate (rpaths, vec_len (paths) - 1);
232 best_priority = paths[0].priority;
234 vec_foreach_index (ii, paths)
236 if (paths[0].priority != best_priority)
239 ladj = lisp_gpe_adjacency_get (paths[ii].lisp_adj);
241 ip_address_to_46 (&ladj->remote_rloc,
242 &rpaths[ii].frp_addr, &rpaths[ii].frp_proto);
244 rpaths[ii].frp_sw_if_index = ladj->sw_if_index;
245 rpaths[ii].frp_weight = (paths[ii].weight ? paths[ii].weight : 1);
248 ASSERT (0 != vec_len (rpaths));
254 * @brief Add route to IP4 or IP6 SRC FIB.
256 * Adds a route to in the LISP SRC FIB for the tunnel.
258 * @param[in] src_fib_index The index/ID of the SRC FIB
259 * @param[in] src_prefix Source IP prefix.
260 * @param[in] paths The paths from which to construct the
264 ip_src_fib_add_route (u32 src_fib_index,
265 const ip_prefix_t * src_prefix,
266 const lisp_fwd_path_t * paths)
268 fib_prefix_t src_fib_prefix;
269 fib_route_path_t *rpaths;
271 ip_prefix_to_fib_prefix (src_prefix, &src_fib_prefix);
273 rpaths = lisp_gpe_mk_fib_paths (paths);
275 fib_table_entry_update (src_fib_index,
277 FIB_SOURCE_LISP, FIB_ENTRY_FLAG_NONE, rpaths);
283 create_fib_entries (lisp_gpe_fwd_entry_t * lfe)
287 dproto = (ip_prefix_version (&lfe->key->rmt.ippref) == IP4 ?
288 DPO_PROTO_IP4 : DPO_PROTO_IP6);
290 lfe->src_fib_index = ip_dst_fib_add_route (lfe->eid_fib_index,
291 &lfe->key->rmt.ippref);
293 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
295 dpo_id_t dpo = DPO_INVALID;
300 /* TODO update timers? */
301 case LISP_FORWARD_NATIVE:
302 /* TODO check if route/next-hop for eid exists in fib and add
303 * more specific for the eid with the next-hop found */
304 case LISP_SEND_MAP_REQUEST:
305 /* insert tunnel that always sends map-request */
306 dpo_copy (&dpo, lisp_cp_dpo_get (dproto));
309 /* for drop fwd entries, just add route, no need to add encap tunnel */
310 dpo_copy (&dpo, drop_dpo_get (dproto));
313 ip_src_fib_add_route_w_dpo (lfe->src_fib_index,
314 &lfe->key->lcl.ippref, &dpo);
319 ip_src_fib_add_route (lfe->src_fib_index,
320 &lfe->key->lcl.ippref, lfe->paths);
325 delete_fib_entries (lisp_gpe_fwd_entry_t * lfe)
327 ip_src_dst_fib_del_route (lfe->src_fib_index,
328 &lfe->key->lcl.ippref,
329 lfe->eid_fib_index, &lfe->key->rmt.ippref);
332 static lisp_gpe_fwd_entry_t *
333 find_fwd_entry (lisp_gpe_main_t * lgm,
334 vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
335 lisp_gpe_fwd_entry_key_t * key)
339 memset (key, 0, sizeof (*key));
341 if (GID_ADDR_IP_PREFIX == gid_address_type (&a->rmt_eid))
344 * the ip version of the source is not set to ip6 when the
345 * source is all zeros. force it.
347 ip_prefix_version (&gid_address_ippref (&a->lcl_eid)) =
348 ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
351 gid_to_dp_address (&a->rmt_eid, &key->rmt);
352 gid_to_dp_address (&a->lcl_eid, &key->lcl);
355 p = hash_get_mem (lgm->lisp_gpe_fwd_entries, key);
359 return (pool_elt_at_index (lgm->lisp_fwd_entry_pool, p[0]));
365 lisp_gpe_fwd_entry_path_sort (void *a1, void *a2)
367 lisp_fwd_path_t *p1 = a1, *p2 = a2;
369 return (p1->priority - p2->priority);
373 lisp_gpe_fwd_entry_mk_paths (lisp_gpe_fwd_entry_t * lfe,
374 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
376 lisp_fwd_path_t *path;
379 vec_validate (lfe->paths, vec_len (a->locator_pairs) - 1);
381 vec_foreach_index (index, a->locator_pairs)
383 path = &lfe->paths[index];
385 path->priority = a->locator_pairs[index].priority;
386 path->weight = a->locator_pairs[index].weight;
389 lisp_gpe_adjacency_find_or_create_and_lock (&a->locator_pairs
391 a->dp_table, lfe->key->vni);
393 vec_sort_with_function (lfe->paths, lisp_gpe_fwd_entry_path_sort);
397 * @brief Add/Delete LISP IP forwarding entry.
399 * creation of forwarding entries for IP LISP overlay:
401 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
402 * @param[in] a Parameters for building the forwarding entry.
404 * @return 0 on success.
407 add_ip_fwd_entry (lisp_gpe_main_t * lgm,
408 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
410 lisp_gpe_fwd_entry_key_t key;
411 lisp_gpe_fwd_entry_t *lfe;
412 fib_protocol_t fproto;
414 lfe = find_fwd_entry (lgm, a, &key);
417 /* don't support updates */
418 return VNET_API_ERROR_INVALID_VALUE;
420 pool_get (lgm->lisp_fwd_entry_pool, lfe);
421 memset (lfe, 0, sizeof (*lfe));
422 lfe->key = clib_mem_alloc (sizeof (key));
423 memcpy (lfe->key, &key, sizeof (key));
425 hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
426 lfe - lgm->lisp_fwd_entry_pool);
428 fproto = (IP4 == ip_prefix_version (&fid_addr_ippref (&lfe->key->rmt)) ?
429 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
431 lfe->type = (a->is_negative ?
432 LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
433 LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
434 lfe->tenant = lisp_gpe_tenant_find_or_create (lfe->key->vni);
435 lfe->eid_table_id = a->table_id;
436 lfe->eid_fib_index = fib_table_find_or_create_and_lock (fproto,
439 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
441 lisp_gpe_fwd_entry_mk_paths (lfe, a);
444 create_fib_entries (lfe);
450 del_ip_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
452 lisp_fwd_path_t *path;
453 fib_protocol_t fproto;
455 vec_foreach (path, lfe->paths)
457 lisp_gpe_adjacency_unlock (path->lisp_adj);
460 delete_fib_entries (lfe);
462 fproto = (IP4 == ip_prefix_version (&fid_addr_ippref (&lfe->key->rmt)) ?
463 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
464 fib_table_unlock (lfe->eid_fib_index, fproto);
466 hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
467 clib_mem_free (lfe->key);
468 pool_put (lgm->lisp_fwd_entry_pool, lfe);
472 * @brief Add/Delete LISP IP forwarding entry.
474 * removal of forwarding entries for IP LISP overlay:
476 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
477 * @param[in] a Parameters for building the forwarding entry.
479 * @return 0 on success.
482 del_ip_fwd_entry (lisp_gpe_main_t * lgm,
483 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
485 lisp_gpe_fwd_entry_key_t key;
486 lisp_gpe_fwd_entry_t *lfe;
488 lfe = find_fwd_entry (lgm, a, &key);
492 return VNET_API_ERROR_INVALID_VALUE;
494 del_ip_fwd_entry_i (lgm, lfe);
500 make_mac_fib_key (BVT (clib_bihash_kv) * kv, u16 bd_index, u8 src_mac[6],
503 kv->key[0] = (((u64) bd_index) << 48) | mac_to_u64 (dst_mac);
504 kv->key[1] = mac_to_u64 (src_mac);
509 * @brief Lookup L2 SD FIB entry
511 * Does a vni + dest + source lookup in the L2 LISP FIB. If the lookup fails
512 * it tries a second time with source set to 0 (i.e., a simple dest lookup).
514 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
515 * @param[in] bd_index Bridge domain index.
516 * @param[in] src_mac Source mac address.
517 * @param[in] dst_mac Destination mac address.
519 * @return index of mapping matching the lookup key.
522 lisp_l2_fib_lookup (lisp_gpe_main_t * lgm, u16 bd_index, u8 src_mac[6],
526 BVT (clib_bihash_kv) kv, value;
528 make_mac_fib_key (&kv, bd_index, src_mac, dst_mac);
529 rv = BV (clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
531 /* no match, try with src 0, catch all for dst */
535 rv = BV (clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
542 return lisp_gpe_main.l2_lb_cp_lkup.dpoi_index;
546 * @brief Add/del L2 SD FIB entry
548 * Inserts value in L2 FIB keyed by vni + dest + source. If entry is
549 * overwritten the associated value is returned.
551 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
552 * @param[in] bd_index Bridge domain index.
553 * @param[in] src_mac Source mac address.
554 * @param[in] dst_mac Destination mac address.
555 * @param[in] val Value to add.
556 * @param[in] is_add Add/del flag.
558 * @return ~0 or value of overwritten entry.
561 lisp_l2_fib_add_del_entry (u16 bd_index, u8 src_mac[6],
562 u8 dst_mac[6], const dpo_id_t * dpo, u8 is_add)
564 lisp_gpe_main_t *lgm = &lisp_gpe_main;
565 BVT (clib_bihash_kv) kv, value;
568 make_mac_fib_key (&kv, bd_index, src_mac, dst_mac);
570 if (BV (clib_bihash_search) (&lgm->l2_fib, &kv, &value) == 0)
571 old_val = value.value;
574 BV (clib_bihash_add_del) (&lgm->l2_fib, &kv, 0 /* is_add */ );
577 kv.value = dpo->dpoi_index;
578 BV (clib_bihash_add_del) (&lgm->l2_fib, &kv, 1 /* is_add */ );
583 #define L2_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
584 #define L2_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
587 l2_fib_init (lisp_gpe_main_t * lgm)
591 BV (clib_bihash_init) (&lgm->l2_fib, "l2 fib",
592 1 << max_log2 (L2_FIB_DEFAULT_HASH_NUM_BUCKETS),
593 L2_FIB_DEFAULT_HASH_MEMORY_SIZE);
596 * the result from a 'miss' in a L2 Table
598 lbi = load_balance_create (1, DPO_PROTO_ETHERNET, 0);
599 load_balance_set_bucket (lbi, 0, lisp_cp_dpo_get (DPO_PROTO_ETHERNET));
601 dpo_set (&lgm->l2_lb_cp_lkup, DPO_LOAD_BALANCE, DPO_PROTO_ETHERNET, lbi);
605 del_l2_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
607 lisp_fwd_path_t *path;
609 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
611 vec_foreach (path, lfe->paths)
613 lisp_gpe_adjacency_unlock (path->lisp_adj);
615 fib_path_list_child_remove (lfe->l2.path_list_index,
616 lfe->l2.child_index);
619 lisp_l2_fib_add_del_entry (lfe->l2.eid_bd_index,
620 fid_addr_mac (&lfe->key->lcl),
621 fid_addr_mac (&lfe->key->rmt), NULL, 0);
623 hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
624 clib_mem_free (lfe->key);
625 pool_put (lgm->lisp_fwd_entry_pool, lfe);
629 * @brief Delete LISP L2 forwarding entry.
631 * Coordinates the removal of forwarding entries for L2 LISP overlay:
633 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
634 * @param[in] a Parameters for building the forwarding entry.
636 * @return 0 on success.
639 del_l2_fwd_entry (lisp_gpe_main_t * lgm,
640 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
642 lisp_gpe_fwd_entry_key_t key;
643 lisp_gpe_fwd_entry_t *lfe;
645 lfe = find_fwd_entry (lgm, a, &key);
648 return VNET_API_ERROR_INVALID_VALUE;
650 del_l2_fwd_entry_i (lgm, lfe);
656 * @brief Construct and insert the forwarding information used by an L2 entry
659 lisp_gpe_l2_update_fwding (lisp_gpe_fwd_entry_t * lfe)
661 lisp_gpe_main_t *lgm = &lisp_gpe_main;
662 dpo_id_t dpo = DPO_INVALID;
664 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
666 fib_path_list_contribute_forwarding (lfe->l2.path_list_index,
667 FIB_FORW_CHAIN_TYPE_ETHERNET,
669 dpo_copy (&dpo, &lfe->l2.dpo);
675 case SEND_MAP_REQUEST:
676 dpo_copy (&dpo, &lgm->l2_lb_cp_lkup);
681 dpo_copy (&dpo, drop_dpo_get (DPO_PROTO_ETHERNET));
685 /* add entry to l2 lisp fib */
686 lisp_l2_fib_add_del_entry (lfe->l2.eid_bd_index,
687 fid_addr_mac (&lfe->key->lcl),
688 fid_addr_mac (&lfe->key->rmt), &dpo, 1);
694 * @brief Add LISP L2 forwarding entry.
696 * Coordinates the creation of forwarding entries for L2 LISP overlay:
697 * creates lisp-gpe tunnel and injects new entry in Source/Dest L2 FIB.
699 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
700 * @param[in] a Parameters for building the forwarding entry.
702 * @return 0 on success.
705 add_l2_fwd_entry (lisp_gpe_main_t * lgm,
706 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
708 lisp_gpe_fwd_entry_key_t key;
709 bd_main_t *bdm = &bd_main;
710 lisp_gpe_fwd_entry_t *lfe;
713 bd_indexp = hash_get (bdm->bd_index_by_bd_id, a->bd_id);
716 clib_warning ("bridge domain %d doesn't exist", a->bd_id);
720 lfe = find_fwd_entry (lgm, a, &key);
723 /* don't support updates */
724 return VNET_API_ERROR_INVALID_VALUE;
726 pool_get (lgm->lisp_fwd_entry_pool, lfe);
727 memset (lfe, 0, sizeof (*lfe));
728 lfe->key = clib_mem_alloc (sizeof (key));
729 memcpy (lfe->key, &key, sizeof (key));
731 hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
732 lfe - lgm->lisp_fwd_entry_pool);
734 lfe->type = (a->is_negative ?
735 LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
736 LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
737 lfe->l2.eid_bd_id = a->bd_id;
738 lfe->l2.eid_bd_index = bd_indexp[0];
739 lfe->tenant = lisp_gpe_tenant_find_or_create (lfe->key->vni);
741 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
743 fib_route_path_t *rpaths;
746 * Make the sorted array of LISP paths with their resp. adjacency
748 lisp_gpe_fwd_entry_mk_paths (lfe, a);
751 * From the LISP paths, construct a FIB path list that will
752 * contribute a load-balance.
754 rpaths = lisp_gpe_mk_fib_paths (lfe->paths);
756 lfe->l2.path_list_index =
757 fib_path_list_create (FIB_PATH_LIST_FLAG_NONE, rpaths);
760 * become a child of the path-list so we receive updates when
761 * its forwarding state changes. this includes an implicit lock.
763 lfe->l2.child_index =
764 fib_path_list_child_add (lfe->l2.path_list_index,
765 FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY,
766 lfe - lgm->lisp_fwd_entry_pool);
770 lfe->action = a->action;
773 lisp_gpe_l2_update_fwding (lfe);
779 * @brief Lookup NSH SD FIB entry
781 * Does an SPI+SI lookup in the NSH LISP FIB.
783 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
784 * @param[in] spi_si SPI + SI.
786 * @return next node index.
789 lisp_nsh_fib_lookup (lisp_gpe_main_t * lgm, u32 spi_si_net_order)
792 BVT (clib_bihash_kv) kv, value;
794 memset (&kv, 0, sizeof (kv));
795 kv.key[0] = spi_si_net_order;
796 rv = BV (clib_bihash_search_inline_2) (&lgm->nsh_fib, &kv, &value);
800 return lgm->nsh_cp_lkup;
804 lisp_gpe_fwd_entry_t *lfe;
805 lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, value.value);
806 return &lfe->nsh.choice;
811 * @brief Add/del NSH FIB entry
813 * Inserts value in NSH FIB keyed by SPI+SI. If entry is
814 * overwritten the associated value is returned.
816 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
817 * @param[in] spi_si SPI + SI.
818 * @param[in] dpo Load balanced mapped to SPI + SI
820 * @return ~0 or value of overwritten entry.
823 lisp_nsh_fib_add_del_entry (u32 spi_si_host_order, u32 lfei, u8 is_add)
825 lisp_gpe_main_t *lgm = &lisp_gpe_main;
826 BVT (clib_bihash_kv) kv, value;
829 memset (&kv, 0, sizeof (kv));
830 kv.key[0] = clib_host_to_net_u32 (spi_si_host_order);
833 if (BV (clib_bihash_search) (&lgm->nsh_fib, &kv, &value) == 0)
834 old_val = value.value;
837 BV (clib_bihash_add_del) (&lgm->nsh_fib, &kv, 0 /* is_add */ );
841 BV (clib_bihash_add_del) (&lgm->nsh_fib, &kv, 1 /* is_add */ );
846 #define NSH_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
847 #define NSH_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
850 nsh_fib_init (lisp_gpe_main_t * lgm)
852 BV (clib_bihash_init) (&lgm->nsh_fib, "nsh fib",
853 1 << max_log2 (NSH_FIB_DEFAULT_HASH_NUM_BUCKETS),
854 NSH_FIB_DEFAULT_HASH_MEMORY_SIZE);
857 * the result from a 'miss' in a NSH Table
859 lgm->nsh_cp_lkup = lisp_cp_dpo_get (DPO_PROTO_NSH);
863 del_nsh_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
865 lisp_fwd_path_t *path;
867 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
869 vec_foreach (path, lfe->paths)
871 lisp_gpe_adjacency_unlock (path->lisp_adj);
873 fib_path_list_child_remove (lfe->nsh.path_list_index,
874 lfe->nsh.child_index);
875 dpo_reset (&lfe->nsh.choice);
878 lisp_nsh_fib_add_del_entry (fid_addr_nsh (&lfe->key->rmt), (u32) ~ 0, 0);
880 hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
881 clib_mem_free (lfe->key);
882 pool_put (lgm->lisp_fwd_entry_pool, lfe);
886 * @brief Delete LISP NSH forwarding entry.
888 * Coordinates the removal of forwarding entries for NSH LISP overlay:
890 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
891 * @param[in] a Parameters for building the forwarding entry.
893 * @return 0 on success.
896 del_nsh_fwd_entry (lisp_gpe_main_t * lgm,
897 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
899 lisp_gpe_fwd_entry_key_t key;
900 lisp_gpe_fwd_entry_t *lfe;
902 lfe = find_fwd_entry (lgm, a, &key);
905 return VNET_API_ERROR_INVALID_VALUE;
907 del_nsh_fwd_entry_i (lgm, lfe);
913 * @brief Construct and insert the forwarding information used by an NSH entry
916 lisp_gpe_nsh_update_fwding (lisp_gpe_fwd_entry_t * lfe)
918 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
919 dpo_id_t dpo = DPO_INVALID;
920 vnet_hw_interface_t *hi;
923 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
925 fib_path_list_contribute_forwarding (lfe->nsh.path_list_index,
926 FIB_FORW_CHAIN_TYPE_NSH,
930 * LISP encap is always the same for this SPI+SI so we do that hash now
931 * and stack on the choice.
933 if (DPO_LOAD_BALANCE == lfe->nsh.dpo.dpoi_type)
936 const load_balance_t *lb;
939 lb = load_balance_get (lfe->nsh.dpo.dpoi_index);
940 hash = fid_addr_nsh (&lfe->key->rmt) % lb->lb_n_buckets;
942 load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
944 dpo_copy (&dpo, tmp);
951 case SEND_MAP_REQUEST:
952 dpo_copy (&dpo, lgm->nsh_cp_lkup);
957 dpo_copy (&dpo, drop_dpo_get (DPO_PROTO_NSH));
961 /* We have only one nsh-lisp interface (no NSH virtualization) */
962 hip = hash_get (lgm->nsh_ifaces.hw_if_index_by_dp_table, 0);
965 hi = vnet_get_hw_interface (lgm->vnet_main, hip[0]);
966 dpo_stack_from_node (hi->tx_node_index, &lfe->nsh.choice, &dpo);
968 /* add entry to nsh lisp fib */
969 lisp_nsh_fib_add_del_entry (fid_addr_nsh (&lfe->key->rmt),
970 lfe - lgm->lisp_fwd_entry_pool, 1);
976 * @brief Add LISP NSH forwarding entry.
978 * Coordinates the creation of forwarding entries for L2 LISP overlay:
979 * creates lisp-gpe tunnel and injects new entry in Source/Dest L2 FIB.
981 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
982 * @param[in] a Parameters for building the forwarding entry.
984 * @return 0 on success.
987 add_nsh_fwd_entry (lisp_gpe_main_t * lgm,
988 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
990 lisp_gpe_fwd_entry_key_t key;
991 lisp_gpe_fwd_entry_t *lfe;
993 lfe = find_fwd_entry (lgm, a, &key);
996 /* don't support updates */
997 return VNET_API_ERROR_INVALID_VALUE;
999 pool_get (lgm->lisp_fwd_entry_pool, lfe);
1000 memset (lfe, 0, sizeof (*lfe));
1001 lfe->key = clib_mem_alloc (sizeof (key));
1002 memcpy (lfe->key, &key, sizeof (key));
1004 hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
1005 lfe - lgm->lisp_fwd_entry_pool);
1007 lfe->type = (a->is_negative ?
1008 LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
1009 LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
1012 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
1014 fib_route_path_t *rpaths;
1017 * Make the sorted array of LISP paths with their resp. adjacency
1019 lisp_gpe_fwd_entry_mk_paths (lfe, a);
1022 * From the LISP paths, construct a FIB path list that will
1023 * contribute a load-balance.
1025 rpaths = lisp_gpe_mk_fib_paths (lfe->paths);
1027 lfe->nsh.path_list_index =
1028 fib_path_list_create (FIB_PATH_LIST_FLAG_NONE, rpaths);
1031 * become a child of the path-list so we receive updates when
1032 * its forwarding state changes. this includes an implicit lock.
1034 lfe->nsh.child_index =
1035 fib_path_list_child_add (lfe->nsh.path_list_index,
1036 FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY,
1037 lfe - lgm->lisp_fwd_entry_pool);
1041 lfe->action = a->action;
1044 lisp_gpe_nsh_update_fwding (lfe);
1050 * @brief conver from the embedded fib_node_t struct to the LSIP entry
1052 static lisp_gpe_fwd_entry_t *
1053 lisp_gpe_fwd_entry_from_fib_node (fib_node_t * node)
1055 return ((lisp_gpe_fwd_entry_t *) (((char *) node) -
1056 STRUCT_OFFSET_OF (lisp_gpe_fwd_entry_t,
1061 * @brief Function invoked during a backwalk of the FIB graph
1063 static fib_node_back_walk_rc_t
1064 lisp_gpe_fib_node_back_walk (fib_node_t * node,
1065 fib_node_back_walk_ctx_t * ctx)
1067 lisp_gpe_fwd_entry_t *lfe = lisp_gpe_fwd_entry_from_fib_node (node);
1069 if (fid_addr_type (&lfe->key->rmt) == FID_ADDR_MAC)
1070 lisp_gpe_l2_update_fwding (lfe);
1071 else if (fid_addr_type (&lfe->key->rmt) == FID_ADDR_NSH)
1072 lisp_gpe_nsh_update_fwding (lfe);
1074 return (FIB_NODE_BACK_WALK_CONTINUE);
1078 * @brief Get a fib_node_t struct from the index of a LISP fwd entry
1081 lisp_gpe_fwd_entry_get_fib_node (fib_node_index_t index)
1083 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1084 lisp_gpe_fwd_entry_t *lfe;
1086 lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, index);
1088 return (&(lfe->node));
1092 * @brief An indication from the graph that the last lock has gone
1095 lisp_gpe_fwd_entry_fib_node_last_lock_gone (fib_node_t * node)
1097 /* We don't manage the locks of the LISP objects via the graph, since
1098 * this object has no children. so this is a no-op. */
1102 * @brief Virtual function table to register with FIB for the LISP type
1104 const static fib_node_vft_t lisp_fwd_vft = {
1105 .fnv_get = lisp_gpe_fwd_entry_get_fib_node,
1106 .fnv_last_lock = lisp_gpe_fwd_entry_fib_node_last_lock_gone,
1107 .fnv_back_walk = lisp_gpe_fib_node_back_walk,
1111 * @brief Forwarding entry create/remove dispatcher.
1113 * Calls l2 or l3 forwarding entry add/del function based on input data.
1115 * @param[in] a Forwarding entry parameters.
1116 * @param[out] hw_if_indexp NOT USED
1118 * @return 0 on success.
1121 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
1124 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1127 if (vnet_lisp_gpe_enable_disable_status () == 0)
1129 clib_warning ("LISP is disabled!");
1130 return VNET_API_ERROR_LISP_DISABLED;
1133 type = gid_address_type (&a->rmt_eid);
1136 case GID_ADDR_IP_PREFIX:
1138 return add_ip_fwd_entry (lgm, a);
1140 return del_ip_fwd_entry (lgm, a);
1144 return add_l2_fwd_entry (lgm, a);
1146 return del_l2_fwd_entry (lgm, a);
1149 return add_nsh_fwd_entry (lgm, a);
1151 return del_nsh_fwd_entry (lgm, a);
1153 clib_warning ("Forwarding entries for type %d not supported!", type);
1159 vnet_lisp_flush_stats (void)
1161 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1165 pool_foreach (stat, lgm->lisp_stats_pool,
1167 stat->pkt_count = 0;
1174 lisp_del_adj_stats (lisp_gpe_main_t * lgm, u32 fwd_entry_index, u32 ti)
1177 lisp_stats_key_t key;
1182 memset (&key, 0, sizeof (key));
1183 key.fwd_entry_index = fwd_entry_index;
1184 key.tunnel_index = ti;
1186 p = hash_get_mem (lgm->lisp_stats_index_by_key, &key);
1189 s = pool_elt_at_index (lgm->lisp_stats_pool, p[0]);
1190 hp = hash_get_pair (lgm->lisp_stats_index_by_key, &key);
1191 key_copy = (void *) (hp->key);
1192 hash_unset_mem (lgm->lisp_stats_index_by_key, &key);
1193 clib_mem_free (key_copy);
1194 pool_put (lgm->lisp_stats_pool, s);
1199 vnet_lisp_del_fwd_stats (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
1200 u32 fwd_entry_index)
1202 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1203 lisp_gpe_fwd_entry_key_t fe_key;
1204 lisp_gpe_fwd_entry_t *lfe;
1205 lisp_fwd_path_t *path;
1206 const lisp_gpe_adjacency_t *ladj;
1208 lfe = find_fwd_entry (lgm, a, &fe_key);
1212 if (LISP_GPE_FWD_ENTRY_TYPE_NORMAL != lfe->type)
1215 vec_foreach (path, lfe->paths)
1217 ladj = lisp_gpe_adjacency_get (path->lisp_adj);
1218 lisp_del_adj_stats (lgm, fwd_entry_index, ladj->tunnel_index);
1223 * @brief Flush all the forwrding entries
1226 vnet_lisp_gpe_fwd_entry_flush (void)
1228 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1229 lisp_gpe_fwd_entry_t *lfe;
1232 pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1234 switch (fid_addr_type(&lfe->key->rmt))
1237 del_l2_fwd_entry_i (lgm, lfe);
1239 case FID_ADDR_IP_PREF:
1240 del_ip_fwd_entry_i (lgm, lfe);
1243 del_nsh_fwd_entry_i (lgm, lfe);
1251 format_lisp_fwd_path (u8 * s, va_list ap)
1253 lisp_fwd_path_t *lfp = va_arg (ap, lisp_fwd_path_t *);
1255 s = format (s, "weight:%d ", lfp->weight);
1256 s = format (s, "adj:[%U]\n",
1257 format_lisp_gpe_adjacency,
1258 lisp_gpe_adjacency_get (lfp->lisp_adj),
1259 LISP_GPE_ADJ_FORMAT_FLAG_NONE);
1264 typedef enum lisp_gpe_fwd_entry_format_flag_t_
1266 LISP_GPE_FWD_ENTRY_FORMAT_NONE = (0 << 0),
1267 LISP_GPE_FWD_ENTRY_FORMAT_DETAIL = (1 << 1),
1268 } lisp_gpe_fwd_entry_format_flag_t;
1272 format_lisp_gpe_fwd_entry (u8 * s, va_list ap)
1274 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1275 lisp_gpe_fwd_entry_t *lfe = va_arg (ap, lisp_gpe_fwd_entry_t *);
1276 lisp_gpe_fwd_entry_format_flag_t flags =
1277 va_arg (ap, lisp_gpe_fwd_entry_format_flag_t);
1279 s = format (s, "VNI:%d VRF:%d EID: %U -> %U [index:%d]",
1280 lfe->key->vni, lfe->eid_table_id,
1281 format_fid_address, &lfe->key->lcl,
1282 format_fid_address, &lfe->key->rmt,
1283 lfe - lgm->lisp_fwd_entry_pool);
1285 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
1287 s = format (s, "\n Negative - action:%U",
1288 format_negative_mapping_action, lfe->action);
1292 lisp_fwd_path_t *path;
1294 s = format (s, "\n via:");
1295 vec_foreach (path, lfe->paths)
1297 s = format (s, "\n %U", format_lisp_fwd_path, path);
1301 if (flags & LISP_GPE_FWD_ENTRY_FORMAT_DETAIL)
1303 switch (fid_addr_type (&lfe->key->rmt))
1306 s = format (s, " fib-path-list:%d\n", lfe->l2.path_list_index);
1307 s = format (s, " dpo:%U\n", format_dpo_id, &lfe->l2.dpo, 0);
1310 s = format (s, " fib-path-list:%d\n", lfe->nsh.path_list_index);
1311 s = format (s, " dpo:%U\n", format_dpo_id, &lfe->nsh.dpo, 0);
1313 case FID_ADDR_IP_PREF:
1321 static clib_error_t *
1322 lisp_gpe_fwd_entry_show (vlib_main_t * vm,
1323 unformat_input_t * input, vlib_cli_command_t * cmd)
1325 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1326 lisp_gpe_fwd_entry_t *lfe;
1330 if (unformat (input, "vni %d", &vni))
1332 else if (unformat (input, "%d", &index))
1334 if (!pool_is_free_index (lgm->lisp_fwd_entry_pool, index))
1336 lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, index);
1338 vlib_cli_output (vm, "[%d@] %U",
1340 format_lisp_gpe_fwd_entry, lfe,
1341 LISP_GPE_FWD_ENTRY_FORMAT_DETAIL);
1345 vlib_cli_output (vm, "entry %d invalid", index);
1352 pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1355 (lfe->key->vni == vni))
1356 vlib_cli_output (vm, "%U", format_lisp_gpe_fwd_entry, lfe,
1357 LISP_GPE_FWD_ENTRY_FORMAT_NONE);
1365 VLIB_CLI_COMMAND (lisp_gpe_fwd_entry_show_command, static) = {
1366 .path = "show gpe entry",
1367 .short_help = "show gpe entry vni <vni> vrf <vrf> [leid <leid>] reid <reid>",
1368 .function = lisp_gpe_fwd_entry_show,
1373 lisp_gpe_fwd_entry_init (vlib_main_t * vm)
1375 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1376 clib_error_t *error = NULL;
1378 if ((error = vlib_call_init_function (vm, lisp_cp_dpo_module_init)))
1384 fib_node_register_type (FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY, &lisp_fwd_vft);
1389 lisp_api_gpe_fwd_entry_t *
1390 vnet_lisp_gpe_fwd_entries_get_by_vni (u32 vni)
1392 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1393 lisp_gpe_fwd_entry_t *lfe;
1394 lisp_api_gpe_fwd_entry_t *entries = 0, e;
1397 pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1399 if (lfe->key->vni == vni)
1401 memset (&e, 0, sizeof (e));
1402 e.dp_table = lfe->eid_table_id;
1403 e.vni = lfe->key->vni;
1404 e.fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
1405 memcpy (&e.reid, &lfe->key->rmt, sizeof (e.reid));
1406 memcpy (&e.leid, &lfe->key->lcl, sizeof (e.leid));
1407 vec_add1 (entries, e);
1415 VLIB_INIT_FUNCTION (lisp_gpe_fwd_entry_init);
1418 * fd.io coding-style-patch-verification: ON
1421 * eval: (c-set-style "gnu")