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) ==
92 LOOKUP_INPUT_SRC_ADDR,
93 LOOKUP_TABLE_FROM_CONFIG,
97 * add the entry to the destination FIB that uses the lookup DPO
99 dst_fei = fib_table_entry_special_dpo_add (dst_fib_index,
102 FIB_ENTRY_FLAG_EXCLUSIVE,
106 * the DPO is locked by the FIB entry, and we have no further
109 dpo_unlock (&src_lkup_dpo);
112 * save the SRC FIB index on the entry so we can retrieve it for
115 fib_entry_set_source_data (dst_fei, FIB_SOURCE_LISP, &src_fib_index);
120 * destination FIB entry already present
122 src_fib_index = *(u32 *) fib_entry_get_source_data (dst_fei,
126 return (src_fib_index);
130 * @brief Del route to IP4 or IP6 SD FIB.
132 * Remove routes from both destination and source FIBs.
134 * @param[in] src_fib_index The index/ID of the SRC FIB
135 * @param[in] src_prefix Source IP prefix.
136 * @param[in] dst_fib_index The index/ID of the DST FIB
137 * @param[in] dst_prefix Destination IP prefix.
140 ip_src_dst_fib_del_route (u32 src_fib_index,
141 const ip_prefix_t * src_prefix,
142 u32 dst_fib_index, const ip_prefix_t * dst_prefix)
144 fib_prefix_t dst_fib_prefix, src_fib_prefix;
148 ASSERT (NULL != dst_prefix);
149 ASSERT (NULL != src_prefix);
151 ip_prefix_to_fib_prefix (dst_prefix, &dst_fib_prefix);
152 ip_prefix_to_fib_prefix (src_prefix, &src_fib_prefix);
154 fib_table_entry_delete (src_fib_index, &src_fib_prefix, FIB_SOURCE_LISP);
156 /* check if only default left or empty */
157 fib_prefix_t default_pref = {
158 .fp_proto = dst_fib_prefix.fp_proto
161 if (fib_table_lookup_exact_match (src_fib_index,
162 &default_pref) != FIB_NODE_INDEX_INVALID)
165 n_entries = fib_table_get_num_entries (src_fib_index,
166 src_fib_prefix.fp_proto,
168 if (n_entries == 0 || (have_default && n_entries == 1))
171 * remove src FIB default route
174 fib_table_entry_special_remove (src_fib_index, &default_pref,
178 * there's nothing left now, unlock the source FIB and the
181 fib_table_entry_special_remove (dst_fib_index,
182 &dst_fib_prefix, FIB_SOURCE_LISP);
183 fib_table_unlock (src_fib_index, src_fib_prefix.fp_proto);
188 * @brief Add route to IP4 or IP6 SRC FIB.
190 * Adds a route to in the LISP SRC FIB with the result of the route
191 * being the DPO passed.
193 * @param[in] src_fib_index The index/ID of the SRC FIB
194 * @param[in] src_prefix Source IP prefix.
195 * @param[in] src_dpo The DPO the route will link to.
198 ip_src_fib_add_route_w_dpo (u32 src_fib_index,
199 const ip_prefix_t * src_prefix,
200 const dpo_id_t * src_dpo)
202 fib_prefix_t src_fib_prefix;
204 ip_prefix_to_fib_prefix (src_prefix, &src_fib_prefix);
207 * add the entry into the source fib.
209 fib_node_index_t src_fei;
211 src_fei = fib_table_lookup_exact_match (src_fib_index, &src_fib_prefix);
213 if (FIB_NODE_INDEX_INVALID == src_fei ||
214 !fib_entry_is_sourced (src_fei, FIB_SOURCE_LISP))
216 fib_table_entry_special_dpo_add (src_fib_index,
219 FIB_ENTRY_FLAG_EXCLUSIVE, src_dpo);
223 static fib_route_path_t *
224 lisp_gpe_mk_fib_paths (const lisp_fwd_path_t * paths)
226 const lisp_gpe_adjacency_t *ladj;
227 fib_route_path_t *rpaths = NULL;
231 vec_validate (rpaths, vec_len (paths) - 1);
233 best_priority = paths[0].priority;
235 vec_foreach_index (ii, paths)
237 if (paths[0].priority != best_priority)
240 ladj = lisp_gpe_adjacency_get (paths[ii].lisp_adj);
242 ip_address_to_46 (&ladj->remote_rloc,
243 &rpaths[ii].frp_addr, &rpaths[ii].frp_proto);
245 rpaths[ii].frp_sw_if_index = ladj->sw_if_index;
246 rpaths[ii].frp_weight = (paths[ii].weight ? paths[ii].weight : 1);
249 ASSERT (0 != vec_len (rpaths));
255 * @brief Add route to IP4 or IP6 SRC FIB.
257 * Adds a route to in the LISP SRC FIB for the tunnel.
259 * @param[in] src_fib_index The index/ID of the SRC FIB
260 * @param[in] src_prefix Source IP prefix.
261 * @param[in] paths The paths from which to construct the
265 ip_src_fib_add_route (u32 src_fib_index,
266 const ip_prefix_t * src_prefix,
267 const lisp_fwd_path_t * paths)
269 fib_prefix_t src_fib_prefix;
270 fib_route_path_t *rpaths;
272 ip_prefix_to_fib_prefix (src_prefix, &src_fib_prefix);
274 rpaths = lisp_gpe_mk_fib_paths (paths);
276 fib_table_entry_update (src_fib_index,
278 FIB_SOURCE_LISP, FIB_ENTRY_FLAG_NONE, rpaths);
284 create_fib_entries (lisp_gpe_fwd_entry_t * lfe)
288 dproto = (ip_prefix_version (&lfe->key->rmt.ippref) == IP4 ?
289 DPO_PROTO_IP4 : DPO_PROTO_IP6);
293 lfe->src_fib_index = ip_dst_fib_add_route (lfe->eid_fib_index,
294 &lfe->key->rmt.ippref);
295 memcpy (&ippref, &lfe->key->lcl.ippref, sizeof (ippref));
299 lfe->src_fib_index = lfe->eid_fib_index;
300 memcpy (&ippref, &lfe->key->rmt.ippref, sizeof (ippref));
303 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
305 dpo_id_t dpo = DPO_INVALID;
310 /* TODO update timers? */
311 case LISP_FORWARD_NATIVE:
312 /* TODO check if route/next-hop for eid exists in fib and add
313 * more specific for the eid with the next-hop found */
314 case LISP_SEND_MAP_REQUEST:
315 /* insert tunnel that always sends map-request */
316 dpo_copy (&dpo, lisp_cp_dpo_get (dproto));
319 /* for drop fwd entries, just add route, no need to add encap tunnel */
320 dpo_copy (&dpo, drop_dpo_get (dproto));
323 ip_src_fib_add_route_w_dpo (lfe->src_fib_index, &ippref, &dpo);
328 ip_src_fib_add_route (lfe->src_fib_index, &ippref, lfe->paths);
333 delete_fib_entries (lisp_gpe_fwd_entry_t * lfe)
335 fib_prefix_t dst_fib_prefix;
338 ip_src_dst_fib_del_route (lfe->src_fib_index,
339 &lfe->key->lcl.ippref,
340 lfe->eid_fib_index, &lfe->key->rmt.ippref);
343 ip_prefix_to_fib_prefix (&lfe->key->rmt.ippref, &dst_fib_prefix);
344 fib_table_entry_delete (lfe->src_fib_index, &dst_fib_prefix,
349 static lisp_gpe_fwd_entry_t *
350 find_fwd_entry (lisp_gpe_main_t * lgm,
351 vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
352 lisp_gpe_fwd_entry_key_t * key)
356 memset (key, 0, sizeof (*key));
358 if (GID_ADDR_IP_PREFIX == gid_address_type (&a->rmt_eid))
361 * the ip version of the source is not set to ip6 when the
362 * source is all zeros. force it.
364 ip_prefix_version (&gid_address_ippref (&a->lcl_eid)) =
365 ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
368 gid_to_dp_address (&a->rmt_eid, &key->rmt);
369 gid_to_dp_address (&a->lcl_eid, &key->lcl);
372 p = hash_get_mem (lgm->lisp_gpe_fwd_entries, key);
376 return (pool_elt_at_index (lgm->lisp_fwd_entry_pool, p[0]));
382 lisp_gpe_fwd_entry_path_sort (void *a1, void *a2)
384 lisp_fwd_path_t *p1 = a1, *p2 = a2;
386 return (p1->priority - p2->priority);
390 lisp_gpe_fwd_entry_mk_paths (lisp_gpe_fwd_entry_t * lfe,
391 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
393 lisp_fwd_path_t *path;
396 vec_validate (lfe->paths, vec_len (a->locator_pairs) - 1);
398 vec_foreach_index (index, a->locator_pairs)
400 path = &lfe->paths[index];
402 path->priority = a->locator_pairs[index].priority;
403 path->weight = a->locator_pairs[index].weight;
406 lisp_gpe_adjacency_find_or_create_and_lock (&a->locator_pairs
408 a->dp_table, lfe->key->vni);
410 vec_sort_with_function (lfe->paths, lisp_gpe_fwd_entry_path_sort);
414 vnet_lisp_gpe_add_fwd_counters (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
417 const lisp_gpe_adjacency_t *ladj;
418 lisp_fwd_path_t *path;
419 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
421 lisp_gpe_fwd_entry_t *lfe;
422 lisp_gpe_fwd_entry_key_t fe_key;
423 lisp_stats_key_t key;
425 lfe = find_fwd_entry (lgm, a, &fe_key);
427 if (LISP_GPE_FWD_ENTRY_TYPE_NORMAL != lfe->type)
430 memset (&key, 0, sizeof (key));
431 key.fwd_entry_index = fwd_entry_index;
433 vec_foreach (path, lfe->paths)
435 ladj = lisp_gpe_adjacency_get (path->lisp_adj);
436 key.tunnel_index = ladj->tunnel_index;
437 lisp_stats_key_t *key_copy = clib_mem_alloc (sizeof (*key_copy));
438 memcpy (key_copy, &key, sizeof (*key_copy));
439 pool_get (lgm->dummy_stats_pool, dummy_elt);
440 hash_set_mem (lgm->lisp_stats_index_by_key, key_copy,
441 dummy_elt - lgm->dummy_stats_pool);
443 vlib_validate_combined_counter (&lgm->counters,
444 dummy_elt - lgm->dummy_stats_pool);
445 vlib_zero_combined_counter (&lgm->counters,
446 dummy_elt - lgm->dummy_stats_pool);
451 * @brief Add/Delete LISP IP forwarding entry.
453 * creation of forwarding entries for IP LISP overlay:
455 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
456 * @param[in] a Parameters for building the forwarding entry.
458 * @return 0 on success.
461 add_ip_fwd_entry (lisp_gpe_main_t * lgm,
462 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
464 lisp_gpe_fwd_entry_key_t key;
465 lisp_gpe_fwd_entry_t *lfe;
466 fib_protocol_t fproto;
468 lfe = find_fwd_entry (lgm, a, &key);
471 /* don't support updates */
472 return VNET_API_ERROR_INVALID_VALUE;
474 pool_get (lgm->lisp_fwd_entry_pool, lfe);
475 memset (lfe, 0, sizeof (*lfe));
476 lfe->key = clib_mem_alloc (sizeof (key));
477 memcpy (lfe->key, &key, sizeof (key));
479 hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
480 lfe - lgm->lisp_fwd_entry_pool);
481 a->fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
483 fproto = (IP4 == ip_prefix_version (&fid_addr_ippref (&lfe->key->rmt)) ?
484 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
486 lfe->type = (a->is_negative ?
487 LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
488 LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
489 lfe->tenant = lisp_gpe_tenant_find_or_create (lfe->key->vni);
490 lfe->eid_table_id = a->table_id;
491 lfe->eid_fib_index = fib_table_find_or_create_and_lock (fproto,
493 lfe->is_src_dst = a->is_src_dst;
495 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
497 lisp_gpe_fwd_entry_mk_paths (lfe, a);
501 lfe->action = a->action;
504 create_fib_entries (lfe);
509 del_ip_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
511 lisp_fwd_path_t *path;
512 fib_protocol_t fproto;
514 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
516 vec_foreach (path, lfe->paths)
518 lisp_gpe_adjacency_unlock (path->lisp_adj);
522 delete_fib_entries (lfe);
524 fproto = (IP4 == ip_prefix_version (&fid_addr_ippref (&lfe->key->rmt)) ?
525 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
526 fib_table_unlock (lfe->eid_fib_index, fproto);
528 hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
529 clib_mem_free (lfe->key);
530 pool_put (lgm->lisp_fwd_entry_pool, lfe);
534 * @brief Add/Delete LISP IP forwarding entry.
536 * removal of forwarding entries for IP LISP overlay:
538 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
539 * @param[in] a Parameters for building the forwarding entry.
541 * @return 0 on success.
544 del_ip_fwd_entry (lisp_gpe_main_t * lgm,
545 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
547 lisp_gpe_fwd_entry_key_t key;
548 lisp_gpe_fwd_entry_t *lfe;
550 lfe = find_fwd_entry (lgm, a, &key);
554 return VNET_API_ERROR_INVALID_VALUE;
556 del_ip_fwd_entry_i (lgm, lfe);
562 make_mac_fib_key (BVT (clib_bihash_kv) * kv, u16 bd_index, u8 src_mac[6],
565 kv->key[0] = (((u64) bd_index) << 48) | mac_to_u64 (dst_mac);
566 kv->key[1] = mac_to_u64 (src_mac);
571 * @brief Lookup L2 SD FIB entry
573 * Does a vni + dest + source lookup in the L2 LISP FIB. If the lookup fails
574 * it tries a second time with source set to 0 (i.e., a simple dest lookup).
576 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
577 * @param[in] bd_index Bridge domain index.
578 * @param[in] src_mac Source mac address.
579 * @param[in] dst_mac Destination mac address.
581 * @return index of mapping matching the lookup key.
584 lisp_l2_fib_lookup (lisp_gpe_main_t * lgm, u16 bd_index, u8 src_mac[6],
588 BVT (clib_bihash_kv) kv, value;
590 make_mac_fib_key (&kv, bd_index, src_mac, dst_mac);
591 rv = BV (clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
593 /* no match, try with src 0, catch all for dst */
597 rv = BV (clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
604 return lisp_gpe_main.l2_lb_cp_lkup.dpoi_index;
608 * @brief Add/del L2 SD FIB entry
610 * Inserts value in L2 FIB keyed by vni + dest + source. If entry is
611 * overwritten the associated value is returned.
613 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
614 * @param[in] bd_index Bridge domain index.
615 * @param[in] src_mac Source mac address.
616 * @param[in] dst_mac Destination mac address.
617 * @param[in] val Value to add.
618 * @param[in] is_add Add/del flag.
620 * @return ~0 or value of overwritten entry.
623 lisp_l2_fib_add_del_entry (u16 bd_index, u8 src_mac[6],
624 u8 dst_mac[6], const dpo_id_t * dpo, u8 is_add)
626 lisp_gpe_main_t *lgm = &lisp_gpe_main;
627 BVT (clib_bihash_kv) kv, value;
630 make_mac_fib_key (&kv, bd_index, src_mac, dst_mac);
632 if (BV (clib_bihash_search) (&lgm->l2_fib, &kv, &value) == 0)
633 old_val = value.value;
636 BV (clib_bihash_add_del) (&lgm->l2_fib, &kv, 0 /* is_add */ );
639 kv.value = dpo->dpoi_index;
640 BV (clib_bihash_add_del) (&lgm->l2_fib, &kv, 1 /* is_add */ );
645 #define L2_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
646 #define L2_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
649 l2_fib_init (lisp_gpe_main_t * lgm)
653 BV (clib_bihash_init) (&lgm->l2_fib, "l2 fib",
654 1 << max_log2 (L2_FIB_DEFAULT_HASH_NUM_BUCKETS),
655 L2_FIB_DEFAULT_HASH_MEMORY_SIZE);
658 * the result from a 'miss' in a L2 Table
660 lbi = load_balance_create (1, DPO_PROTO_ETHERNET, 0);
661 load_balance_set_bucket (lbi, 0, lisp_cp_dpo_get (DPO_PROTO_ETHERNET));
663 dpo_set (&lgm->l2_lb_cp_lkup, DPO_LOAD_BALANCE, DPO_PROTO_ETHERNET, lbi);
667 del_l2_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
669 lisp_fwd_path_t *path;
671 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
673 vec_foreach (path, lfe->paths)
675 lisp_gpe_adjacency_unlock (path->lisp_adj);
677 fib_path_list_child_remove (lfe->l2.path_list_index,
678 lfe->l2.child_index);
681 lisp_l2_fib_add_del_entry (lfe->l2.eid_bd_index,
682 fid_addr_mac (&lfe->key->lcl),
683 fid_addr_mac (&lfe->key->rmt), NULL, 0);
685 hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
686 clib_mem_free (lfe->key);
687 pool_put (lgm->lisp_fwd_entry_pool, lfe);
691 * @brief Delete LISP L2 forwarding entry.
693 * Coordinates the removal of forwarding entries for L2 LISP overlay:
695 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
696 * @param[in] a Parameters for building the forwarding entry.
698 * @return 0 on success.
701 del_l2_fwd_entry (lisp_gpe_main_t * lgm,
702 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
704 lisp_gpe_fwd_entry_key_t key;
705 lisp_gpe_fwd_entry_t *lfe;
707 lfe = find_fwd_entry (lgm, a, &key);
710 return VNET_API_ERROR_INVALID_VALUE;
712 del_l2_fwd_entry_i (lgm, lfe);
718 * @brief Construct and insert the forwarding information used by an L2 entry
721 lisp_gpe_l2_update_fwding (lisp_gpe_fwd_entry_t * lfe)
723 lisp_gpe_main_t *lgm = &lisp_gpe_main;
724 dpo_id_t dpo = DPO_INVALID;
726 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
728 fib_path_list_contribute_forwarding (lfe->l2.path_list_index,
729 FIB_FORW_CHAIN_TYPE_ETHERNET,
731 dpo_copy (&dpo, &lfe->l2.dpo);
737 case SEND_MAP_REQUEST:
738 dpo_copy (&dpo, &lgm->l2_lb_cp_lkup);
743 dpo_copy (&dpo, drop_dpo_get (DPO_PROTO_ETHERNET));
747 /* add entry to l2 lisp fib */
748 lisp_l2_fib_add_del_entry (lfe->l2.eid_bd_index,
749 fid_addr_mac (&lfe->key->lcl),
750 fid_addr_mac (&lfe->key->rmt), &dpo, 1);
756 * @brief Add LISP L2 forwarding entry.
758 * Coordinates the creation of forwarding entries for L2 LISP overlay:
759 * creates lisp-gpe tunnel and injects new entry in Source/Dest L2 FIB.
761 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
762 * @param[in] a Parameters for building the forwarding entry.
764 * @return 0 on success.
767 add_l2_fwd_entry (lisp_gpe_main_t * lgm,
768 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
770 lisp_gpe_fwd_entry_key_t key;
771 bd_main_t *bdm = &bd_main;
772 lisp_gpe_fwd_entry_t *lfe;
775 bd_indexp = hash_get (bdm->bd_index_by_bd_id, a->bd_id);
778 clib_warning ("bridge domain %d doesn't exist", a->bd_id);
782 lfe = find_fwd_entry (lgm, a, &key);
785 /* don't support updates */
786 return VNET_API_ERROR_INVALID_VALUE;
788 pool_get (lgm->lisp_fwd_entry_pool, lfe);
789 memset (lfe, 0, sizeof (*lfe));
790 lfe->key = clib_mem_alloc (sizeof (key));
791 memcpy (lfe->key, &key, sizeof (key));
793 hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
794 lfe - lgm->lisp_fwd_entry_pool);
795 a->fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
797 lfe->type = (a->is_negative ?
798 LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
799 LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
800 lfe->l2.eid_bd_id = a->bd_id;
801 lfe->l2.eid_bd_index = bd_indexp[0];
802 lfe->tenant = lisp_gpe_tenant_find_or_create (lfe->key->vni);
804 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
806 fib_route_path_t *rpaths;
809 * Make the sorted array of LISP paths with their resp. adjacency
811 lisp_gpe_fwd_entry_mk_paths (lfe, a);
814 * From the LISP paths, construct a FIB path list that will
815 * contribute a load-balance.
817 rpaths = lisp_gpe_mk_fib_paths (lfe->paths);
819 lfe->l2.path_list_index =
820 fib_path_list_create (FIB_PATH_LIST_FLAG_NONE, rpaths);
823 * become a child of the path-list so we receive updates when
824 * its forwarding state changes. this includes an implicit lock.
826 lfe->l2.child_index =
827 fib_path_list_child_add (lfe->l2.path_list_index,
828 FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY,
829 lfe - lgm->lisp_fwd_entry_pool);
833 lfe->action = a->action;
836 lisp_gpe_l2_update_fwding (lfe);
842 * @brief Lookup NSH SD FIB entry
844 * Does an SPI+SI lookup in the NSH LISP FIB.
846 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
847 * @param[in] spi_si SPI + SI.
849 * @return next node index.
852 lisp_nsh_fib_lookup (lisp_gpe_main_t * lgm, u32 spi_si_net_order)
855 BVT (clib_bihash_kv) kv, value;
857 memset (&kv, 0, sizeof (kv));
858 kv.key[0] = spi_si_net_order;
859 rv = BV (clib_bihash_search_inline_2) (&lgm->nsh_fib, &kv, &value);
863 return lgm->nsh_cp_lkup;
867 lisp_gpe_fwd_entry_t *lfe;
868 lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, value.value);
869 return &lfe->nsh.choice;
874 * @brief Add/del NSH FIB entry
876 * Inserts value in NSH FIB keyed by SPI+SI. If entry is
877 * overwritten the associated value is returned.
879 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
880 * @param[in] spi_si SPI + SI.
881 * @param[in] dpo Load balanced mapped to SPI + SI
883 * @return ~0 or value of overwritten entry.
886 lisp_nsh_fib_add_del_entry (u32 spi_si_host_order, u32 lfei, u8 is_add)
888 lisp_gpe_main_t *lgm = &lisp_gpe_main;
889 BVT (clib_bihash_kv) kv, value;
892 memset (&kv, 0, sizeof (kv));
893 kv.key[0] = clib_host_to_net_u32 (spi_si_host_order);
896 if (BV (clib_bihash_search) (&lgm->nsh_fib, &kv, &value) == 0)
897 old_val = value.value;
900 BV (clib_bihash_add_del) (&lgm->nsh_fib, &kv, 0 /* is_add */ );
904 BV (clib_bihash_add_del) (&lgm->nsh_fib, &kv, 1 /* is_add */ );
909 #define NSH_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
910 #define NSH_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
913 nsh_fib_init (lisp_gpe_main_t * lgm)
915 BV (clib_bihash_init) (&lgm->nsh_fib, "nsh fib",
916 1 << max_log2 (NSH_FIB_DEFAULT_HASH_NUM_BUCKETS),
917 NSH_FIB_DEFAULT_HASH_MEMORY_SIZE);
920 * the result from a 'miss' in a NSH Table
922 lgm->nsh_cp_lkup = lisp_cp_dpo_get (DPO_PROTO_NSH);
926 del_nsh_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
928 lisp_fwd_path_t *path;
930 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
932 vec_foreach (path, lfe->paths)
934 lisp_gpe_adjacency_unlock (path->lisp_adj);
936 fib_path_list_child_remove (lfe->nsh.path_list_index,
937 lfe->nsh.child_index);
938 dpo_reset (&lfe->nsh.choice);
941 lisp_nsh_fib_add_del_entry (fid_addr_nsh (&lfe->key->rmt), (u32) ~ 0, 0);
943 hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
944 clib_mem_free (lfe->key);
945 pool_put (lgm->lisp_fwd_entry_pool, lfe);
949 * @brief Delete LISP NSH forwarding entry.
951 * Coordinates the removal of forwarding entries for NSH LISP overlay:
953 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
954 * @param[in] a Parameters for building the forwarding entry.
956 * @return 0 on success.
959 del_nsh_fwd_entry (lisp_gpe_main_t * lgm,
960 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
962 lisp_gpe_fwd_entry_key_t key;
963 lisp_gpe_fwd_entry_t *lfe;
965 lfe = find_fwd_entry (lgm, a, &key);
968 return VNET_API_ERROR_INVALID_VALUE;
970 del_nsh_fwd_entry_i (lgm, lfe);
976 * @brief Construct and insert the forwarding information used by an NSH entry
979 lisp_gpe_nsh_update_fwding (lisp_gpe_fwd_entry_t * lfe)
981 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
982 dpo_id_t dpo = DPO_INVALID;
983 vnet_hw_interface_t *hi;
986 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
988 fib_path_list_contribute_forwarding (lfe->nsh.path_list_index,
989 FIB_FORW_CHAIN_TYPE_NSH,
993 * LISP encap is always the same for this SPI+SI so we do that hash now
994 * and stack on the choice.
996 if (DPO_LOAD_BALANCE == lfe->nsh.dpo.dpoi_type)
999 const load_balance_t *lb;
1002 lb = load_balance_get (lfe->nsh.dpo.dpoi_index);
1003 hash = fid_addr_nsh (&lfe->key->rmt) % lb->lb_n_buckets;
1005 load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
1007 dpo_copy (&dpo, tmp);
1012 switch (lfe->action)
1014 case SEND_MAP_REQUEST:
1015 dpo_copy (&dpo, lgm->nsh_cp_lkup);
1018 case FORWARD_NATIVE:
1020 dpo_copy (&dpo, drop_dpo_get (DPO_PROTO_NSH));
1024 /* We have only one nsh-lisp interface (no NSH virtualization) */
1025 hip = hash_get (lgm->nsh_ifaces.hw_if_index_by_dp_table, 0);
1028 hi = vnet_get_hw_interface (lgm->vnet_main, hip[0]);
1029 dpo_stack_from_node (hi->tx_node_index, &lfe->nsh.choice, &dpo);
1031 /* add entry to nsh lisp fib */
1032 lisp_nsh_fib_add_del_entry (fid_addr_nsh (&lfe->key->rmt),
1033 lfe - lgm->lisp_fwd_entry_pool, 1);
1039 * @brief Add LISP NSH forwarding entry.
1041 * Coordinates the creation of forwarding entries for L2 LISP overlay:
1042 * creates lisp-gpe tunnel and injects new entry in Source/Dest L2 FIB.
1044 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
1045 * @param[in] a Parameters for building the forwarding entry.
1047 * @return 0 on success.
1050 add_nsh_fwd_entry (lisp_gpe_main_t * lgm,
1051 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
1053 lisp_gpe_fwd_entry_key_t key;
1054 lisp_gpe_fwd_entry_t *lfe;
1056 lfe = find_fwd_entry (lgm, a, &key);
1059 /* don't support updates */
1060 return VNET_API_ERROR_INVALID_VALUE;
1062 pool_get (lgm->lisp_fwd_entry_pool, lfe);
1063 memset (lfe, 0, sizeof (*lfe));
1064 lfe->key = clib_mem_alloc (sizeof (key));
1065 memcpy (lfe->key, &key, sizeof (key));
1067 hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
1068 lfe - lgm->lisp_fwd_entry_pool);
1069 a->fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
1071 lfe->type = (a->is_negative ?
1072 LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
1073 LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
1076 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
1078 fib_route_path_t *rpaths;
1081 * Make the sorted array of LISP paths with their resp. adjacency
1083 lisp_gpe_fwd_entry_mk_paths (lfe, a);
1086 * From the LISP paths, construct a FIB path list that will
1087 * contribute a load-balance.
1089 rpaths = lisp_gpe_mk_fib_paths (lfe->paths);
1091 lfe->nsh.path_list_index =
1092 fib_path_list_create (FIB_PATH_LIST_FLAG_NONE, rpaths);
1095 * become a child of the path-list so we receive updates when
1096 * its forwarding state changes. this includes an implicit lock.
1098 lfe->nsh.child_index =
1099 fib_path_list_child_add (lfe->nsh.path_list_index,
1100 FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY,
1101 lfe - lgm->lisp_fwd_entry_pool);
1105 lfe->action = a->action;
1108 lisp_gpe_nsh_update_fwding (lfe);
1114 * @brief conver from the embedded fib_node_t struct to the LSIP entry
1116 static lisp_gpe_fwd_entry_t *
1117 lisp_gpe_fwd_entry_from_fib_node (fib_node_t * node)
1119 return ((lisp_gpe_fwd_entry_t *) (((char *) node) -
1120 STRUCT_OFFSET_OF (lisp_gpe_fwd_entry_t,
1125 * @brief Function invoked during a backwalk of the FIB graph
1127 static fib_node_back_walk_rc_t
1128 lisp_gpe_fib_node_back_walk (fib_node_t * node,
1129 fib_node_back_walk_ctx_t * ctx)
1131 lisp_gpe_fwd_entry_t *lfe = lisp_gpe_fwd_entry_from_fib_node (node);
1133 if (fid_addr_type (&lfe->key->rmt) == FID_ADDR_MAC)
1134 lisp_gpe_l2_update_fwding (lfe);
1135 else if (fid_addr_type (&lfe->key->rmt) == FID_ADDR_NSH)
1136 lisp_gpe_nsh_update_fwding (lfe);
1138 return (FIB_NODE_BACK_WALK_CONTINUE);
1142 * @brief Get a fib_node_t struct from the index of a LISP fwd entry
1145 lisp_gpe_fwd_entry_get_fib_node (fib_node_index_t index)
1147 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1148 lisp_gpe_fwd_entry_t *lfe;
1150 lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, index);
1152 return (&(lfe->node));
1156 * @brief An indication from the graph that the last lock has gone
1159 lisp_gpe_fwd_entry_fib_node_last_lock_gone (fib_node_t * node)
1161 /* We don't manage the locks of the LISP objects via the graph, since
1162 * this object has no children. so this is a no-op. */
1166 * @brief Virtual function table to register with FIB for the LISP type
1168 const static fib_node_vft_t lisp_fwd_vft = {
1169 .fnv_get = lisp_gpe_fwd_entry_get_fib_node,
1170 .fnv_last_lock = lisp_gpe_fwd_entry_fib_node_last_lock_gone,
1171 .fnv_back_walk = lisp_gpe_fib_node_back_walk,
1175 * @brief Forwarding entry create/remove dispatcher.
1177 * Calls l2 or l3 forwarding entry add/del function based on input data.
1179 * @param[in] a Forwarding entry parameters.
1180 * @param[out] hw_if_indexp NOT USED
1182 * @return 0 on success.
1185 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
1188 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1191 if (vnet_lisp_gpe_enable_disable_status () == 0)
1193 clib_warning ("LISP is disabled!");
1194 return VNET_API_ERROR_LISP_DISABLED;
1197 type = gid_address_type (&a->rmt_eid);
1200 case GID_ADDR_IP_PREFIX:
1202 return add_ip_fwd_entry (lgm, a);
1204 return del_ip_fwd_entry (lgm, a);
1208 return add_l2_fwd_entry (lgm, a);
1210 return del_l2_fwd_entry (lgm, a);
1213 return add_nsh_fwd_entry (lgm, a);
1215 return del_nsh_fwd_entry (lgm, a);
1217 clib_warning ("Forwarding entries for type %d not supported!", type);
1223 vnet_lisp_flush_stats (void)
1225 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1226 vlib_combined_counter_main_t *cm = &lgm->counters;
1229 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
1230 vlib_zero_combined_counter (cm, i);
1236 lisp_del_adj_stats (lisp_gpe_main_t * lgm, u32 fwd_entry_index, u32 ti)
1239 lisp_stats_key_t key;
1244 memset (&key, 0, sizeof (key));
1245 key.fwd_entry_index = fwd_entry_index;
1246 key.tunnel_index = ti;
1248 p = hash_get_mem (lgm->lisp_stats_index_by_key, &key);
1251 s = pool_elt_at_index (lgm->dummy_stats_pool, p[0]);
1252 hp = hash_get_pair (lgm->lisp_stats_index_by_key, &key);
1253 key_copy = (void *) (hp->key);
1254 hash_unset_mem (lgm->lisp_stats_index_by_key, &key);
1255 clib_mem_free (key_copy);
1256 pool_put (lgm->dummy_stats_pool, s);
1261 vnet_lisp_gpe_del_fwd_counters (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
1262 u32 fwd_entry_index)
1264 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1265 lisp_gpe_fwd_entry_key_t fe_key;
1266 lisp_gpe_fwd_entry_t *lfe;
1267 lisp_fwd_path_t *path;
1268 const lisp_gpe_adjacency_t *ladj;
1270 lfe = find_fwd_entry (lgm, a, &fe_key);
1274 if (LISP_GPE_FWD_ENTRY_TYPE_NORMAL != lfe->type)
1277 vec_foreach (path, lfe->paths)
1279 ladj = lisp_gpe_adjacency_get (path->lisp_adj);
1280 lisp_del_adj_stats (lgm, fwd_entry_index, ladj->tunnel_index);
1285 * @brief Flush all the forwrding entries
1288 vnet_lisp_gpe_fwd_entry_flush (void)
1290 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1291 lisp_gpe_fwd_entry_t *lfe;
1294 pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1296 switch (fid_addr_type(&lfe->key->rmt))
1299 del_l2_fwd_entry_i (lgm, lfe);
1301 case FID_ADDR_IP_PREF:
1302 del_ip_fwd_entry_i (lgm, lfe);
1305 del_nsh_fwd_entry_i (lgm, lfe);
1313 format_lisp_fwd_path (u8 * s, va_list ap)
1315 lisp_fwd_path_t *lfp = va_arg (ap, lisp_fwd_path_t *);
1317 s = format (s, "weight:%d ", lfp->weight);
1318 s = format (s, "adj:[%U]\n",
1319 format_lisp_gpe_adjacency,
1320 lisp_gpe_adjacency_get (lfp->lisp_adj),
1321 LISP_GPE_ADJ_FORMAT_FLAG_NONE);
1326 typedef enum lisp_gpe_fwd_entry_format_flag_t_
1328 LISP_GPE_FWD_ENTRY_FORMAT_NONE = (0 << 0),
1329 LISP_GPE_FWD_ENTRY_FORMAT_DETAIL = (1 << 1),
1330 } lisp_gpe_fwd_entry_format_flag_t;
1334 format_lisp_gpe_fwd_entry (u8 * s, va_list ap)
1336 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1337 lisp_gpe_fwd_entry_t *lfe = va_arg (ap, lisp_gpe_fwd_entry_t *);
1338 lisp_gpe_fwd_entry_format_flag_t flags =
1339 va_arg (ap, lisp_gpe_fwd_entry_format_flag_t);
1341 s = format (s, "VNI:%d VRF:%d EID: %U -> %U [index:%d]",
1342 lfe->key->vni, lfe->eid_table_id,
1343 format_fid_address, &lfe->key->lcl,
1344 format_fid_address, &lfe->key->rmt,
1345 lfe - lgm->lisp_fwd_entry_pool);
1347 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
1349 s = format (s, "\n Negative - action:%U",
1350 format_negative_mapping_action, lfe->action);
1354 lisp_fwd_path_t *path;
1356 s = format (s, "\n via:");
1357 vec_foreach (path, lfe->paths)
1359 s = format (s, "\n %U", format_lisp_fwd_path, path);
1363 if (flags & LISP_GPE_FWD_ENTRY_FORMAT_DETAIL)
1365 switch (fid_addr_type (&lfe->key->rmt))
1368 s = format (s, " fib-path-list:%d\n", lfe->l2.path_list_index);
1369 s = format (s, " dpo:%U\n", format_dpo_id, &lfe->l2.dpo, 0);
1372 s = format (s, " fib-path-list:%d\n", lfe->nsh.path_list_index);
1373 s = format (s, " dpo:%U\n", format_dpo_id, &lfe->nsh.dpo, 0);
1375 case FID_ADDR_IP_PREF:
1383 static clib_error_t *
1384 lisp_gpe_fwd_entry_show (vlib_main_t * vm,
1385 unformat_input_t * input, vlib_cli_command_t * cmd)
1387 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1388 lisp_gpe_fwd_entry_t *lfe;
1392 if (unformat (input, "vni %d", &vni))
1394 else if (unformat (input, "%d", &index))
1396 if (!pool_is_free_index (lgm->lisp_fwd_entry_pool, index))
1398 lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, index);
1400 vlib_cli_output (vm, "[%d@] %U",
1402 format_lisp_gpe_fwd_entry, lfe,
1403 LISP_GPE_FWD_ENTRY_FORMAT_DETAIL);
1407 vlib_cli_output (vm, "entry %d invalid", index);
1414 pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1417 (lfe->key->vni == vni))
1418 vlib_cli_output (vm, "%U", format_lisp_gpe_fwd_entry, lfe,
1419 LISP_GPE_FWD_ENTRY_FORMAT_NONE);
1427 VLIB_CLI_COMMAND (lisp_gpe_fwd_entry_show_command, static) = {
1428 .path = "show gpe entry",
1429 .short_help = "show gpe entry vni <vni> vrf <vrf> [leid <leid>] reid <reid>",
1430 .function = lisp_gpe_fwd_entry_show,
1435 lisp_gpe_fwd_entry_init (vlib_main_t * vm)
1437 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1438 clib_error_t *error = NULL;
1440 if ((error = vlib_call_init_function (vm, lisp_cp_dpo_module_init)))
1446 fib_node_register_type (FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY, &lisp_fwd_vft);
1452 vnet_lisp_gpe_get_fwd_entry_vnis (void)
1454 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1455 lisp_gpe_fwd_entry_t *lfe;
1459 pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1461 hash_set (vnis, lfe->key->vni, 0);
1468 lisp_api_gpe_fwd_entry_t *
1469 vnet_lisp_gpe_fwd_entries_get_by_vni (u32 vni)
1471 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1472 lisp_gpe_fwd_entry_t *lfe;
1473 lisp_api_gpe_fwd_entry_t *entries = 0, e;
1476 pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1478 if (lfe->key->vni == vni)
1480 memset (&e, 0, sizeof (e));
1481 e.dp_table = lfe->eid_table_id;
1482 e.vni = lfe->key->vni;
1483 if (lfe->type == LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE)
1484 e.action = lfe->action;
1485 e.fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
1486 memcpy (&e.reid, &lfe->key->rmt, sizeof (e.reid));
1487 memcpy (&e.leid, &lfe->key->lcl, sizeof (e.leid));
1488 vec_add1 (entries, e);
1496 VLIB_INIT_FUNCTION (lisp_gpe_fwd_entry_init);
1499 * fd.io coding-style-patch-verification: ON
1502 * eval: (c-set-style "gnu")