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 <vlibmemory/api.h>
17 #include <vnet/lisp-cp/control.h>
18 #include <vnet/lisp-cp/packets.h>
19 #include <vnet/lisp-cp/lisp_msg_serdes.h>
20 #include <vnet/lisp-gpe/lisp_gpe.h>
31 queue_map_request (gid_address_t * seid, gid_address_t * deid,
32 u8 smr_invoked, u8 is_resend);
34 ip_interface_address_t *
35 ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
36 u32 sw_if_index, u8 loop)
38 vnet_main_t *vnm = vnet_get_main ();
39 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
40 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
41 sw_if_index = swif->unnumbered_sw_if_index;
43 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
44 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
46 return pool_elt_at_index ((lm)->if_address_pool, ia);
50 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
53 ip_interface_address_t *ia;
55 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
58 return ip_interface_address_get_address (lm, ia);
62 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
63 u8 version, ip_address_t * result)
68 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
69 addr = ip_interface_get_first_address (lm, sw_if_index, version);
73 ip_address_set (result, addr, version);
78 ip_fib_lookup_with_table (lisp_cp_main_t * lcm, u32 fib_index,
81 if (ip_addr_version (dst) == IP4)
82 return ip4_fib_lookup_with_table (lcm->im4, fib_index, &ip_addr_v4 (dst),
85 return ip6_fib_lookup_with_table (lcm->im6, fib_index, &ip_addr_v6 (dst));
89 ip_fib_get_egress_iface_for_dst_with_lm (lisp_cp_main_t * lcm,
91 ip_lookup_main_t * lm)
96 adj_index = ip_fib_lookup_with_table (lcm, 0, dst);
97 adj = ip_get_adjacency (lm, adj_index);
102 /* we only want outgoing routes */
103 if (adj->lookup_next_index != IP_LOOKUP_NEXT_ARP
104 && adj->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
107 return adj->rewrite_header.sw_if_index;
111 * Find the sw_if_index of the interface that would be used to egress towards
115 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
117 ip_lookup_main_t *lm;
119 lm = ip_addr_version (dst) == IP4 ?
120 &lcm->im4->lookup_main : &lcm->im6->lookup_main;
122 return ip_fib_get_egress_iface_for_dst_with_lm (lcm, dst, lm);
126 * Find first IP of the interface that would be used to egress towards dst.
127 * Returns 1 if the address is found 0 otherwise.
130 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
131 ip_address_t * result)
134 ip_lookup_main_t *lm;
138 ASSERT (result != 0);
140 ipver = ip_addr_version (dst);
142 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
143 si = ip_fib_get_egress_iface_for_dst_with_lm (lcm, dst, lm);
148 /* find the first ip address */
149 addr = ip_interface_get_first_address (lm, si, ipver);
153 ip_address_set (result, addr, ipver);
158 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
160 uword *dp_table, *intf;
161 vnet_lisp_gpe_add_del_iface_args_t _ai, *ai = &_ai;
165 dp_table = hash_get (lcm->table_id_by_vni, vni);
169 clib_warning ("vni %d not associated to a vrf!", vni);
170 return VNET_API_ERROR_INVALID_VALUE;
175 dp_table = hash_get (lcm->bd_id_by_vni, vni);
178 clib_warning ("vni %d not associated to a bridge domain!", vni);
179 return VNET_API_ERROR_INVALID_VALUE;
183 intf = hash_get (is_l2 ? lcm->l2_dp_intf_by_vni : lcm->dp_intf_by_vni, vni);
185 /* enable/disable data-plane interface */
188 /* create interface */
194 ai->dp_table = dp_table[0];
196 vnet_lisp_gpe_add_del_iface (ai, 0);
198 /* keep track of vnis for which interfaces have been created */
199 hash_set (lcm->dp_intf_by_vni, vni, 1);
206 clib_warning ("interface for vni %d doesn't exist!", vni);
207 return VNET_API_ERROR_INVALID_VALUE;
212 ai->dp_table = dp_table[0];
213 vnet_lisp_gpe_add_del_iface (ai, 0);
214 hash_unset (lcm->dp_intf_by_vni, vni);
221 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
223 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
226 memset (a, 0, sizeof (*a));
228 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
232 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
234 /* delete dp fwd entry */
237 a->locator_pairs = fe->locator_pairs;
238 a->vni = gid_address_vni (&a->rmt_eid);
239 gid_address_copy (&a->rmt_eid, &fe->deid);
241 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
243 /* delete entry in fwd table */
244 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
245 vec_free (fe->locator_pairs);
246 pool_put (lcm->fwd_entry_pool, fe);
250 * Finds first remote locator with best (lowest) priority that has a local
251 * peer locator with an underlying route to it.
255 get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
256 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
258 u32 i, limitp = 0, li, found = 0, esi;
259 locator_set_t *rmt_ls, *lcl_ls;
260 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
261 locator_t *lp, *rmt = 0;
266 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
268 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
270 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
277 /* find unvisited remote locator with best priority */
278 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
280 if (0 != hash_get (checked, i))
283 li = vec_elt (rmt_ls->locator_indices, i);
284 lp = pool_elt_at_index (lcm->locator_pool, li);
286 /* we don't support non-IP locators for now */
287 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
290 if ((found && lp->priority == limitp)
291 || (!found && lp->priority >= limitp))
295 /* don't search for locators with lower priority and don't
296 * check this locator again*/
297 limitp = lp->priority;
298 hash_set (checked, i, 1);
302 /* check if a local locator with a route to remote locator exists */
305 /* find egress sw_if_index for rmt locator */
307 ip_fib_get_egress_iface_for_dst (lcm,
308 &gid_address_ip (&rmt->address));
309 if ((u32) ~ 0 == esi)
312 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
314 li = vec_elt (lcl_ls->locator_indices, i);
315 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
317 /* found local locator with the needed sw_if_index */
318 if (sl->sw_if_index == esi)
320 /* and it has an address */
321 if (0 == ip_interface_get_first_ip_address (lcm,
323 gid_address_ip_version
328 memset (&pair, 0, sizeof (pair));
329 ip_address_copy (&pair.rmt_loc,
330 &gid_address_ip (&rmt->address));
331 ip_address_copy (&pair.lcl_loc, lcl_addr);
332 pair.weight = rmt->weight;
333 vec_add1 (locator_pairs[0], pair);
347 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
349 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
350 mapping_t *src_map, *dst_map;
352 uword *feip = 0, *dpid;
356 memset (a, 0, sizeof (*a));
358 /* remove entry if it already exists */
359 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
361 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
363 src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
364 dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
366 /* insert data plane forwarding entry */
369 gid_address_copy (&a->rmt_eid, &dst_map->eid);
370 a->vni = gid_address_vni (&a->rmt_eid);
372 /* get vrf or bd_index associated to vni */
373 type = gid_address_type (&dst_map->eid);
374 if (GID_ADDR_IP_PREFIX == type)
376 dpid = hash_get (lcm->table_id_by_vni, a->vni);
379 clib_warning ("vni %d not associated to a vrf!", a->vni);
382 a->table_id = dpid[0];
384 else if (GID_ADDR_MAC == type)
386 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
389 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
395 /* find best locator pair that 1) verifies LISP policy 2) are connected */
396 if (0 == get_locator_pairs (lcm, src_map, dst_map, &a->locator_pairs))
400 a->action = dst_map->action;
404 u8 ipver = ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
405 a->decap_next_index = (ipver == IP4) ?
406 LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
408 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
410 /* add tunnel to fwd entry table XXX check return value from DP insertion */
411 pool_get (lcm->fwd_entry_pool, fe);
412 fe->locator_pairs = a->locator_pairs;
413 gid_address_copy (&fe->deid, &a->rmt_eid);
414 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
415 fe - lcm->fwd_entry_pool);
419 * Add/remove mapping to/from map-cache. Overwriting not allowed.
422 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
423 u32 * map_index_result)
425 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
426 u32 mi, *map_indexp, map_index, i;
427 mapping_t *m, *old_map;
430 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
431 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
434 /* TODO check if overwriting and take appropriate actions */
435 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
437 clib_warning ("eid %U found in the eid-table", format_gid_address,
439 return VNET_API_ERROR_VALUE_EXIST;
442 pool_get (lcm->mapping_pool, m);
443 gid_address_copy (&m->eid, &a->eid);
444 m->locator_set_index = a->locator_set_index;
446 m->action = a->action;
449 map_index = m - lcm->mapping_pool;
450 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
453 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
455 clib_warning ("Locator set with index %d doesn't exist",
456 a->locator_set_index);
457 return VNET_API_ERROR_INVALID_VALUE;
460 /* add eid to list of eids supported by locator-set */
461 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
462 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
463 a->locator_set_index);
464 vec_add1 (eid_indexes[0], map_index);
469 vec_add1 (lcm->local_mappings_indexes, map_index);
471 map_index_result[0] = map_index;
475 if (mi == GID_LOOKUP_MISS)
477 clib_warning ("eid %U not found in the eid-table",
478 format_gid_address, &a->eid);
479 return VNET_API_ERROR_INVALID_VALUE;
482 /* clear locator-set to eids binding */
483 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
484 a->locator_set_index);
485 for (i = 0; i < vec_len (eid_indexes[0]); i++)
487 map_indexp = vec_elt_at_index (eid_indexes[0], i);
488 if (map_indexp[0] == mi)
491 vec_del1 (eid_indexes[0], i);
493 /* remove local mark if needed */
494 m = pool_elt_at_index (lcm->mapping_pool, mi);
498 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
500 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
501 if (lm_indexp[0] == mi)
504 vec_del1 (lcm->local_mappings_indexes, k);
507 /* remove mapping from dictionary */
508 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
509 gid_address_free (&m->eid);
510 pool_put_index (lcm->mapping_pool, mi);
517 * Add/update/delete mapping to/in/from map-cache.
520 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
521 u32 * map_index_result)
527 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
529 if (vnet_lisp_enable_disable_status () == 0)
531 clib_warning ("LISP is disabled!");
532 return VNET_API_ERROR_LISP_DISABLED;
535 vni = gid_address_vni (&a->eid);
536 type = gid_address_type (&a->eid);
537 if (GID_ADDR_IP_PREFIX == type)
538 dp_table = hash_get (lcm->table_id_by_vni, vni);
539 else if (GID_ADDR_MAC == type)
540 dp_table = hash_get (lcm->bd_id_by_vni, vni);
544 clib_warning ("vni %d not associated to a %s!", vni,
545 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
546 return VNET_API_ERROR_INVALID_VALUE;
549 /* store/remove mapping from map-cache */
550 return vnet_lisp_map_cache_add_del (a, map_index_result);
553 static clib_error_t *
554 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
555 vlib_cli_command_t * cmd)
557 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
558 unformat_input_t _line_input, *line_input = &_line_input;
561 gid_address_t *eids = 0;
562 clib_error_t *error = 0;
563 u8 *locator_set_name = 0;
564 u32 locator_set_index = 0, map_index = 0;
566 vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
570 memset (&eid, 0, sizeof (eid));
571 memset (a, 0, sizeof (*a));
573 /* Get a line of input. */
574 if (!unformat_user (input, unformat_line_input, line_input))
577 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
579 if (unformat (line_input, "add"))
581 else if (unformat (line_input, "del"))
583 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
585 else if (unformat (line_input, "vni %d", &vni))
586 gid_address_vni (&eid) = vni;
587 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
589 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
592 error = clib_error_return (0, "locator-set %s doesn't exist",
596 locator_set_index = p[0];
600 error = unformat_parse_error (line_input);
604 /* XXX treat batch configuration */
606 if (GID_ADDR_SRC_DST == gid_address_type (&eid))
609 clib_error_return (0, "src/dst is not supported for local EIDs!");
613 gid_address_copy (&a->eid, &eid);
615 a->locator_set_index = locator_set_index;
618 rv = vnet_lisp_add_del_local_mapping (a, &map_index);
621 error = clib_error_return (0, "failed to %s local mapping!",
622 is_add ? "add" : "delete");
626 if (locator_set_name)
627 vec_free (locator_set_name);
628 gid_address_free (&a->eid);
633 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
634 .path = "lisp eid-table",
635 .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
636 "locator-set <locator-set>",
637 .function = lisp_add_del_local_eid_command_fn,
642 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
644 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
645 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
647 if (vnet_lisp_enable_disable_status () == 0)
649 clib_warning ("LISP is disabled!");
653 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
654 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
656 if (!is_l2 && (vni == 0 || dp_id == 0))
658 clib_warning ("can't add/del default vni-vrf mapping!");
662 dp_idp = hash_get (dp_table_by_vni[0], vni);
663 vnip = hash_get (vni_by_dp_table[0], dp_id);
669 clib_warning ("vni %d or vrf %d already used in vrf/vni "
670 "mapping!", vni, dp_id);
673 hash_set (dp_table_by_vni[0], vni, dp_id);
674 hash_set (vni_by_dp_table[0], dp_id, vni);
676 /* create dp iface */
677 dp_add_del_iface (lcm, vni, is_l2, 1);
681 if (!dp_idp || !vnip)
683 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
684 "mapping!", vni, dp_id);
687 hash_unset (dp_table_by_vni[0], vni);
688 hash_unset (vni_by_dp_table[0], dp_id);
690 /* remove dp iface */
691 dp_add_del_iface (lcm, vni, is_l2, 0);
697 static clib_error_t *
698 lisp_eid_table_map_command_fn (vlib_main_t * vm,
699 unformat_input_t * input,
700 vlib_cli_command_t * cmd)
702 u8 is_add = 1, is_l2 = 0;
703 u32 vni = 0, dp_id = 0;
704 unformat_input_t _line_input, *line_input = &_line_input;
706 /* Get a line of input. */
707 if (!unformat_user (input, unformat_line_input, line_input))
710 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
712 if (unformat (line_input, "del"))
714 else if (unformat (line_input, "vni %d", &vni))
716 else if (unformat (line_input, "vrf %d", &dp_id))
718 else if (unformat (line_input, "bd %d", &dp_id))
722 return unformat_parse_error (line_input);
725 vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
730 VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
731 .path = "lisp eid-table map",
732 .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
733 .function = lisp_eid_table_map_command_fn,
737 /* return 0 if the two locator sets are identical 1 otherwise */
739 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
740 locator_t * new_locators)
743 locator_t *old_loc, *new_loc;
745 if (vec_len (old_ls_indexes) != vec_len (new_locators))
748 for (i = 0; i < vec_len (new_locators); i++)
750 old_li = vec_elt (old_ls_indexes, i);
751 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
753 new_loc = vec_elt_at_index (new_locators, i);
755 if (locator_cmp (old_loc, new_loc))
762 * Adds/removes/updates mapping. Does not program forwarding.
764 * @param eid end-host identifier
765 * @param rlocs vector of remote locators
766 * @param action action for negative map-reply
767 * @param is_add add mapping if non-zero, delete otherwise
768 * @param res_map_index the map-index that was created/updated/removed. It is
769 * set to ~0 if no action is taken.
770 * @return return code
773 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
774 u8 authoritative, u32 ttl, u8 is_add,
777 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
778 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
779 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
780 u32 mi, ls_index = 0, dst_map_index;
783 if (vnet_lisp_enable_disable_status () == 0)
785 clib_warning ("LISP is disabled!");
786 return VNET_API_ERROR_LISP_DISABLED;
790 res_map_index[0] = ~0;
792 memset (m_args, 0, sizeof (m_args[0]));
793 memset (ls_args, 0, sizeof (ls_args[0]));
795 ls_args->locators = rlocs;
797 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
798 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
802 /* overwrite: if mapping already exists, decide if locators should be
803 * updated and be done */
804 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
806 locator_set_t *old_ls;
808 /* update mapping attributes */
809 old_map->action = action;
810 old_map->authoritative = authoritative;
813 old_ls = pool_elt_at_index (lcm->locator_set_pool,
814 old_map->locator_set_index);
815 if (compare_locators (lcm, old_ls->locator_indices,
818 /* set locator-set index to overwrite */
820 ls_args->index = old_map->locator_set_index;
821 vnet_lisp_add_del_locator_set (ls_args, 0);
823 res_map_index[0] = mi;
832 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
835 gid_address_copy (&m_args->eid, eid);
837 m_args->action = action;
838 m_args->locator_set_index = ls_index;
839 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
842 res_map_index[0] = dst_map_index;
847 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
849 clib_warning ("cannot delete mapping for eid %U",
850 format_gid_address, eid);
855 gid_address_copy (&m_args->eid, eid);
856 m_args->locator_set_index = old_map->locator_set_index;
858 /* delete mapping associated from map-cache */
859 vnet_lisp_map_cache_add_del (m_args, 0);
862 ls_args->index = old_map->locator_set_index;
863 /* delete locator set */
864 vnet_lisp_add_del_locator_set (ls_args, 0);
866 /* return old mapping index */
868 res_map_index[0] = mi;
876 vnet_lisp_clear_all_remote_adjacencies (void)
879 u32 mi, *map_indices = 0, *map_indexp;
880 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
881 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
882 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
885 pool_foreach_index (mi, lcm->mapping_pool,
887 vec_add1 (map_indices, mi);
891 vec_foreach (map_indexp, map_indices)
893 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
896 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
899 gid_address_copy (&dm_args->eid, &map->eid);
900 dm_args->locator_set_index = map->locator_set_index;
902 /* delete mapping associated to fwd entry */
903 vnet_lisp_map_cache_add_del (dm_args, 0);
907 ls->index = map->locator_set_index;
908 /* delete locator set */
909 rv = vnet_lisp_add_del_locator_set (ls, 0);
917 vec_free (map_indices);
922 * Adds adjacency or removes forwarding entry associated to remote mapping.
923 * Note that adjacencies are not stored, they only result in forwarding entries
927 lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
928 gid_address_t * remote_eid, u8 is_add)
930 u32 local_mi, remote_mi = ~0;
932 if (vnet_lisp_enable_disable_status () == 0)
934 clib_warning ("LISP is disabled!");
935 return VNET_API_ERROR_LISP_DISABLED;
938 remote_mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, remote_eid);
939 if (GID_LOOKUP_MISS == remote_mi)
941 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
942 format_gid_address, remote_eid);
949 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
952 /* check if source eid has an associated mapping. If pitr mode is on,
953 * just use the pitr's mapping */
954 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
955 gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
958 if (GID_LOOKUP_MISS == local_mi)
960 clib_warning ("Local eid %U not found. Cannot add adjacency!",
961 format_gid_address, local_eid);
966 /* update forwarding */
967 dp_add_fwd_entry (lcm, local_mi, remote_mi);
970 dp_del_fwd_entry (lcm, 0, remote_mi);
976 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
978 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
979 return lisp_add_del_adjacency (lcm, &a->seid, &a->deid, a->is_add);
983 * Handler for add/del remote mapping CLI.
985 * @param vm vlib context
986 * @param input input from user
988 * @return pointer to clib error structure
990 static clib_error_t *
991 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
992 unformat_input_t * input,
993 vlib_cli_command_t * cmd)
995 clib_error_t *error = 0;
996 unformat_input_t _line_input, *line_input = &_line_input;
997 u8 is_add = 1, del_all = 0;
998 locator_t rloc, *rlocs = 0, *curr_rloc = 0;
1001 u32 vni, action = ~0, p, w;
1004 /* Get a line of input. */
1005 if (!unformat_user (input, unformat_line_input, line_input))
1008 memset (&eid, 0, sizeof (eid));
1009 memset (&rloc, 0, sizeof (rloc));
1011 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1013 if (unformat (line_input, "del-all"))
1015 else if (unformat (line_input, "del"))
1017 else if (unformat (line_input, "add"))
1019 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1021 else if (unformat (line_input, "vni %u", &vni))
1023 gid_address_vni (&eid) = vni;
1025 else if (unformat (line_input, "p %d w %d", &p, &w))
1030 ("No RLOC configured for setting priority/weight!");
1033 curr_rloc->priority = p;
1034 curr_rloc->weight = w;
1036 else if (unformat (line_input, "rloc %U", unformat_ip_address,
1037 &gid_address_ip (&rloc.address)))
1039 vec_add1 (rlocs, rloc);
1040 curr_rloc = &rlocs[vec_len (rlocs) - 1];
1042 else if (unformat (line_input, "action %U",
1043 unformat_negative_mapping_action, &action))
1047 clib_warning ("parse error");
1054 clib_warning ("missing eid!");
1060 if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1062 clib_warning ("no action set for negative map-reply!");
1068 vnet_lisp_clear_all_remote_adjacencies ();
1072 /* TODO build src/dst with seid */
1074 /* if it's a delete, clean forwarding */
1077 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1078 rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1085 /* add as static remote mapping, i.e., not authoritative and infinite
1087 rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add, 0);
1090 clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
1094 unformat_free (line_input);
1098 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) =
1100 .path = "lisp remote-mapping",.short_help =
1101 "lisp remote-mapping add|del [del-all] vni <vni> "
1102 "eid <est-eid> [action <no-action|natively-forward|"
1103 "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
1104 "[rloc <dst-locator> ... ]",.function =
1105 lisp_add_del_remote_mapping_command_fn,};
1108 * Handler for add/del adjacency CLI.
1110 static clib_error_t *
1111 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
1112 vlib_cli_command_t * cmd)
1114 clib_error_t *error = 0;
1115 unformat_input_t _line_input, *line_input = &_line_input;
1116 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1118 locator_t rloc, *rlocs = 0;
1119 ip_prefix_t *deid_ippref, *seid_ippref;
1120 gid_address_t seid, deid;
1121 u8 *dmac = gid_address_mac (&deid);
1122 u8 *smac = gid_address_mac (&seid);
1123 u8 deid_set = 0, seid_set = 0;
1125 u32 vni, action = ~0;
1128 /* Get a line of input. */
1129 if (!unformat_user (input, unformat_line_input, line_input))
1132 memset (&deid, 0, sizeof (deid));
1133 memset (&seid, 0, sizeof (seid));
1134 memset (&rloc, 0, sizeof (rloc));
1136 seid_ippref = &gid_address_ippref (&seid);
1137 deid_ippref = &gid_address_ippref (&deid);
1139 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1141 if (unformat (line_input, "del"))
1143 else if (unformat (line_input, "add"))
1145 else if (unformat (line_input, "deid %U",
1146 unformat_ip_prefix, deid_ippref))
1148 gid_address_type (&deid) = GID_ADDR_IP_PREFIX;
1151 else if (unformat (line_input, "deid %U", unformat_mac_address, dmac))
1153 gid_address_type (&deid) = GID_ADDR_MAC;
1156 else if (unformat (line_input, "vni %u", &vni))
1158 gid_address_vni (&seid) = vni;
1159 gid_address_vni (&deid) = vni;
1161 else if (unformat (line_input, "seid %U",
1162 unformat_ip_prefix, seid_ippref))
1164 gid_address_type (&seid) = GID_ADDR_IP_PREFIX;
1167 else if (unformat (line_input, "seid %U", unformat_mac_address, smac))
1169 gid_address_type (&seid) = GID_ADDR_MAC;
1174 clib_warning ("parse error");
1181 clib_warning ("missing deid!");
1185 if (GID_ADDR_IP_PREFIX == gid_address_type (&deid))
1187 /* if seid not set, make sure the ip version is the same as that
1188 * of the deid. This ensures the seid to be configured will be
1189 * either 0/0 or ::/0 */
1191 ip_prefix_version (seid_ippref) = ip_prefix_version (deid_ippref);
1194 (ip_prefix_version (deid_ippref)
1195 != ip_prefix_version (seid_ippref)))
1197 clib_warning ("source and destination EIDs are not"
1198 " in the same IP family!");
1203 if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1205 clib_warning ("no action set for negative map-reply!");
1209 memset (a, 0, sizeof (a[0]));
1210 gid_address_copy (&a->seid, &deid);
1211 gid_address_copy (&a->deid, &seid);
1214 rv = vnet_lisp_add_del_adjacency (a);
1217 clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
1220 unformat_free (line_input);
1227 VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1228 .path = "lisp adjacency",
1229 .short_help = "lisp adjacency add|del vni <vni>"
1230 "deid <dest-eid> seid <src-eid> [action <no-action|natively-forward|"
1231 "send-map-request|drop>] rloc <dst-locator> [rloc <dst-locator> ... ]",
1232 .function = lisp_add_del_adjacency_command_fn,
1236 static clib_error_t *
1237 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
1238 unformat_input_t * input,
1239 vlib_cli_command_t * cmd)
1242 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1244 vec_foreach (mr, lcm->map_resolvers)
1246 vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
1252 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1253 .path = "show lisp map-resolvers",
1254 .short_help = "show lisp map-resolvers",
1255 .function = lisp_show_map_resolvers_command_fn,
1260 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1262 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1263 u32 locator_set_index = ~0;
1267 if (vnet_lisp_enable_disable_status () == 0)
1269 clib_warning ("LISP is disabled!");
1270 return VNET_API_ERROR_LISP_DISABLED;
1273 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1276 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1279 locator_set_index = p[0];
1283 pool_get (lcm->mapping_pool, m);
1284 m->locator_set_index = locator_set_index;
1286 lcm->pitr_map_index = m - lcm->mapping_pool;
1288 /* enable pitr mode */
1293 /* remove pitr mapping */
1294 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1296 /* disable pitr mode */
1302 static clib_error_t *
1303 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
1304 unformat_input_t * input,
1305 vlib_cli_command_t * cmd)
1307 u8 locator_name_set = 0;
1308 u8 *locator_set_name = 0;
1310 unformat_input_t _line_input, *line_input = &_line_input;
1311 clib_error_t *error = 0;
1314 /* Get a line of input. */
1315 if (!unformat_user (input, unformat_line_input, line_input))
1318 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1320 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
1321 locator_name_set = 1;
1322 else if (unformat (line_input, "disable"))
1325 return clib_error_return (0, "parse error");
1328 if (!locator_name_set)
1330 clib_warning ("No locator set specified!");
1333 rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1336 error = clib_error_return (0, "failed to %s pitr!",
1337 is_add ? "add" : "delete");
1341 if (locator_set_name)
1342 vec_free (locator_set_name);
1347 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1348 .path = "lisp pitr",
1349 .short_help = "lisp pitr [disable] ls <locator-set-name>",
1350 .function = lisp_pitr_set_locator_set_command_fn,
1354 static clib_error_t *
1355 lisp_show_pitr_command_fn (vlib_main_t * vm,
1356 unformat_input_t * input, vlib_cli_command_t * cmd)
1358 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1363 vlib_cli_output (vm, "%=20s%=16s",
1364 "pitr", lcm->lisp_pitr ? "locator-set" : "");
1366 if (!lcm->lisp_pitr)
1368 vlib_cli_output (vm, "%=20s", "disable");
1372 if (~0 == lcm->pitr_map_index)
1374 tmp_str = format (0, "N/A");
1378 m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1379 if (~0 != m->locator_set_index)
1382 pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1383 tmp_str = format (0, "%s", ls->name);
1387 tmp_str = format (0, "N/A");
1390 vec_add1 (tmp_str, 0);
1392 vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1400 VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1401 .path = "show lisp pitr",
1402 .short_help = "Show pitr",
1403 .function = lisp_show_pitr_command_fn,
1408 format_eid_entry (u8 * s, va_list * args)
1410 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
1411 lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
1412 mapping_t *mapit = va_arg (*args, mapping_t *);
1413 locator_set_t *ls = va_arg (*args, locator_set_t *);
1414 gid_address_t *gid = &mapit->eid;
1415 u32 ttl = mapit->ttl;
1416 u8 aut = mapit->authoritative;
1421 u8 *type = ls->local ? format (0, "local(%s)", ls->name)
1422 : format (0, "remote");
1424 if (vec_len (ls->locator_indices) == 0)
1426 s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
1431 vec_foreach (loc_index, ls->locator_indices)
1433 locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1435 loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1438 loc = format (0, "%U", format_ip_address,
1439 &gid_address_ip (&l->address));
1443 s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
1444 gid, type, loc, ttl, aut);
1448 s = format (s, "%55s%v\n", "", loc);
1454 static clib_error_t *
1455 lisp_show_eid_table_command_fn (vlib_main_t * vm,
1456 unformat_input_t * input,
1457 vlib_cli_command_t * cmd)
1459 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1461 unformat_input_t _line_input, *line_input = &_line_input;
1467 memset (&eid, 0, sizeof (eid));
1469 /* Get a line of input. */
1470 if (!unformat_user (input, unformat_line_input, line_input))
1473 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1475 if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1477 else if (unformat (line_input, "local"))
1479 else if (unformat (line_input, "remote"))
1482 return clib_error_return (0, "parse error: '%U'",
1483 format_unformat_error, line_input);
1486 vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
1487 "EID", "type", "locators", "ttl", "autoritative");
1492 pool_foreach (mapit, lcm->mapping_pool,
1494 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
1495 mapit->locator_set_index);
1496 if (filter && !((1 == filter && ls->local) ||
1497 (2 == filter && !ls->local)))
1501 vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
1508 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
1509 if ((u32) ~ 0 == mi)
1512 mapit = pool_elt_at_index (lcm->mapping_pool, mi);
1513 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
1514 mapit->locator_set_index);
1516 if (filter && !((1 == filter && ls->local) ||
1517 (2 == filter && !ls->local)))
1522 vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
1530 VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
1531 .path = "show lisp eid-table",
1532 .short_help = "Shows EID table",
1533 .function = lisp_show_eid_table_command_fn,
1537 /* cleans locator to locator-set data and removes locators not part of
1538 * any locator-set */
1540 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1542 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
1543 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1544 for (i = 0; i < vec_len (ls->locator_indices); i++)
1546 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1547 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1549 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1551 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1552 if (ls_indexp[0] == lsi)
1556 /* delete index for removed locator-set */
1557 vec_del1 (ls_indexes[0], j);
1559 /* delete locator if it's part of no locator-set */
1560 if (vec_len (ls_indexes[0]) == 0)
1562 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1563 vec_add1 (to_be_deleted, i);
1569 for (i = 0; i < vec_len (to_be_deleted); i++)
1571 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1572 vec_del1 (ls->locator_indices, loc_indexp[0]);
1574 vec_free (to_be_deleted);
1578 static inline uword *
1579 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
1581 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1586 /* find locator-set */
1589 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
1600 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
1606 ASSERT (ls != NULL);
1607 ASSERT (loc != NULL);
1609 vec_foreach (locit, ls->locator_indices)
1611 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1612 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1613 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1615 clib_warning ("Duplicate locator");
1616 return VNET_API_ERROR_VALUE_EXIST;
1624 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1625 u32 ls_index, u32 loc_id)
1627 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1628 u32 **ls_indexes = NULL;
1630 ASSERT (ls != NULL);
1631 ASSERT (locit != NULL);
1633 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1634 pool_put_index (lcm->locator_pool, locit[0]);
1635 vec_del1 (ls->locator_indices, loc_id);
1636 vec_del1 (ls_indexes[0], ls_index);
1640 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1641 locator_set_t * ls, u32 * ls_result)
1643 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1644 locator_t *loc = NULL, *itloc = NULL;
1645 uword _p = (u32) ~ 0, *p = &_p;
1646 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
1652 if (vnet_lisp_enable_disable_status () == 0)
1654 clib_warning ("LISP is disabled!");
1655 return VNET_API_ERROR_LISP_DISABLED;
1658 p = get_locator_set_index (a, p);
1661 clib_warning ("locator-set %v doesn't exist", a->name);
1662 return VNET_API_ERROR_INVALID_ARGUMENT;
1667 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1670 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1672 return VNET_API_ERROR_INVALID_ARGUMENT;
1679 ls_result[0] = p[0];
1681 /* allocate locators */
1682 vec_foreach (itloc, a->locators)
1684 ret = is_locator_in_locator_set (lcm, ls, itloc);
1690 pool_get (lcm->locator_pool, loc);
1692 loc_index = loc - lcm->locator_pool;
1694 vec_add1 (ls->locator_indices, loc_index);
1696 vec_validate (lcm->locator_to_locator_sets, loc_index);
1697 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1699 vec_add1 (ls_indexes[0], ls_index);
1706 itloc = a->locators;
1708 vec_foreach (locit, ls->locator_indices)
1710 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1712 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1714 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1716 if (0 == loc->local &&
1717 !gid_address_cmp (&loc->address, &itloc->address))
1719 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1730 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
1733 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1735 uword _p = (u32) ~ 0, *p = &_p;
1740 if (vnet_lisp_enable_disable_status () == 0)
1742 clib_warning ("LISP is disabled!");
1743 return VNET_API_ERROR_LISP_DISABLED;
1748 p = get_locator_set_index (a, p);
1751 if (p && p[0] != (u32) ~ 0)
1753 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1756 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1761 /* clean locator to locator-set vectors and remove locators if
1762 * they're not part of another locator-set */
1763 clean_locator_to_locator_set (lcm, p[0]);
1765 /* remove locator indices from locator set */
1766 vec_free (ls->locator_indices);
1771 ls_result[0] = p[0];
1773 /* new locator-set */
1776 pool_get (lcm->locator_set_pool, ls);
1777 memset (ls, 0, sizeof (*ls));
1778 ls_index = ls - lcm->locator_set_pool;
1782 ls->name = vec_dup (a->name);
1784 if (!lcm->locator_set_index_by_name)
1785 lcm->locator_set_index_by_name = hash_create_vec (
1793 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
1796 /* mark as local locator-set */
1797 vec_add1 (lcm->local_locator_set_indexes, ls_index);
1799 ls->local = a->local;
1801 ls_result[0] = ls_index;
1804 ret = vnet_lisp_add_del_locator (a, ls, NULL);
1812 p = get_locator_set_index (a, p);
1815 clib_warning ("locator-set %v doesn't exists", a->name);
1819 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1822 clib_warning ("locator-set with index %d doesn't exists", p[0]);
1826 if (lcm->mreq_itr_rlocs == p[0])
1828 clib_warning ("Can't delete the locator-set used to constrain "
1829 "the itr-rlocs in map-requests!");
1833 if (vec_len (lcm->locator_set_to_eids) != 0)
1835 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
1836 if (vec_len (eid_indexes[0]) != 0)
1839 ("Can't delete a locator that supports a mapping!");
1844 /* clean locator to locator-sets data */
1845 clean_locator_to_locator_set (lcm, p[0]);
1851 vec_foreach_index (it, lcm->local_locator_set_indexes)
1853 lsi = vec_elt (lcm->local_locator_set_indexes, it);
1856 vec_del1 (lcm->local_locator_set_indexes, it);
1860 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
1862 vec_free (ls->name);
1863 vec_free (ls->locator_indices);
1864 pool_put (lcm->locator_set_pool, ls);
1870 vnet_lisp_enable_disable (u8 is_enable)
1873 clib_error_t *error = 0;
1874 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1875 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
1877 a->is_en = is_enable;
1878 error = vnet_lisp_gpe_enable_disable (a);
1881 return clib_error_return (0, "failed to %s data-plane!",
1882 a->is_en ? "enable" : "disable");
1887 /* enable all l2 and l3 ifaces */
1890 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
1891 dp_add_del_iface(lcm, vni, 0, 1);
1893 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
1894 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
1900 /* clear interface table */
1901 hash_free (lcm->dp_intf_by_vni);
1902 hash_free (lcm->fwd_entry_by_mapping_index);
1903 pool_free (lcm->fwd_entry_pool);
1906 /* update global flag */
1907 lcm->is_enabled = is_enable;
1912 static clib_error_t *
1913 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
1914 vlib_cli_command_t * cmd)
1916 unformat_input_t _line_input, *line_input = &_line_input;
1920 /* Get a line of input. */
1921 if (!unformat_user (input, unformat_line_input, line_input))
1924 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1926 if (unformat (line_input, "enable"))
1931 else if (unformat (line_input, "disable"))
1935 return clib_error_return (0, "parse error: '%U'",
1936 format_unformat_error, line_input);
1941 return clib_error_return (0, "state not set");
1943 return vnet_lisp_enable_disable (is_enabled);
1947 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
1949 .short_help = "lisp [enable|disable]",
1950 .function = lisp_enable_disable_command_fn,
1955 vnet_lisp_enable_disable_status (void)
1957 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1958 return lcm->is_enabled;
1962 format_lisp_status (u8 * s, va_list * args)
1964 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1965 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
1968 static clib_error_t *
1969 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
1970 vlib_cli_command_t * cmd)
1973 msg = format (msg, "feature: %U\ngpe: %U\n",
1974 format_lisp_status, format_vnet_lisp_gpe_status);
1975 vlib_cli_output (vm, "%v", msg);
1981 VLIB_CLI_COMMAND (lisp_show_status_command) = {
1982 .path = "show lisp status",
1983 .short_help = "show lisp status",
1984 .function = lisp_show_status_command_fn,
1988 static clib_error_t *
1989 lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
1990 unformat_input_t * input,
1991 vlib_cli_command_t * cmd)
1994 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1996 vlib_cli_output (vm, "%=10s%=10s", "VNI", "VRF");
1999 hash_foreach_pair (p, lcm->table_id_by_vni,
2001 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
2009 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
2010 .path = "show lisp eid-table map",
2011 .short_help = "show lisp eid-table vni to vrf mappings",
2012 .function = lisp_show_eid_table_map_command_fn,
2016 static clib_error_t *
2017 lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
2018 unformat_input_t * input,
2019 vlib_cli_command_t * cmd)
2021 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2022 vnet_main_t *vnm = lgm->vnet_main;
2023 unformat_input_t _line_input, *line_input = &_line_input;
2025 clib_error_t *error = 0;
2026 u8 *locator_set_name = 0;
2027 locator_t locator, *locators = 0;
2028 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2032 memset (&locator, 0, sizeof (locator));
2033 memset (a, 0, sizeof (a[0]));
2035 /* Get a line of input. */
2036 if (!unformat_user (input, unformat_line_input, line_input))
2039 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2041 if (unformat (line_input, "add %_%v%_", &locator_set_name))
2043 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
2045 else if (unformat (line_input, "iface %U p %d w %d",
2046 unformat_vnet_sw_interface, vnm,
2047 &locator.sw_if_index, &locator.priority,
2051 vec_add1 (locators, locator);
2055 error = unformat_parse_error (line_input);
2060 a->name = locator_set_name;
2061 a->locators = locators;
2065 rv = vnet_lisp_add_del_locator_set (a, &ls_index);
2068 error = clib_error_return (0, "failed to %s locator-set!",
2069 is_add ? "add" : "delete");
2073 vec_free (locators);
2074 if (locator_set_name)
2075 vec_free (locator_set_name);
2080 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2081 .path = "lisp locator-set",
2082 .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2083 "p <priority> w <weight>]",
2084 .function = lisp_add_del_locator_set_command_fn,
2088 static clib_error_t *
2089 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
2090 unformat_input_t * input,
2091 vlib_cli_command_t * cmd)
2093 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2094 vnet_main_t *vnm = lgm->vnet_main;
2095 unformat_input_t _line_input, *line_input = &_line_input;
2097 clib_error_t *error = 0;
2098 u8 *locator_set_name = 0;
2099 u8 locator_set_name_set = 0;
2100 locator_t locator, *locators = 0;
2101 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2104 memset (&locator, 0, sizeof (locator));
2105 memset (a, 0, sizeof (a[0]));
2107 /* Get a line of input. */
2108 if (!unformat_user (input, unformat_line_input, line_input))
2111 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2113 if (unformat (line_input, "add"))
2115 else if (unformat (line_input, "del"))
2117 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
2118 locator_set_name_set = 1;
2119 else if (unformat (line_input, "iface %U p %d w %d",
2120 unformat_vnet_sw_interface, vnm,
2121 &locator.sw_if_index, &locator.priority,
2125 vec_add1 (locators, locator);
2129 error = unformat_parse_error (line_input);
2134 if (!locator_set_name_set)
2136 error = clib_error_return (0, "locator_set name not set!");
2140 a->name = locator_set_name;
2141 a->locators = locators;
2145 vnet_lisp_add_del_locator (a, 0, &ls_index);
2148 vec_free (locators);
2149 vec_free (locator_set_name);
2154 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2155 .path = "lisp locator",
2156 .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2157 "p <priority> w <weight>",
2158 .function = lisp_add_del_locator_in_set_command_fn,
2162 static clib_error_t *
2163 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
2164 unformat_input_t * input,
2165 vlib_cli_command_t * cmd)
2167 locator_set_t *lsit;
2170 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2172 vlib_cli_output (vm, "%=20s%=16s%=16s%=16s", "Locator-set", "Locator",
2173 "Priority", "Weight");
2176 pool_foreach (lsit, lcm->locator_set_pool,
2182 msg = format (msg, "%=16v", lsit->name);
2186 msg = format (msg, "%=16s", "remote");
2188 vec_foreach (locit, lsit->locator_indices)
2192 msg = format (msg, "%16s", " ");
2194 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2196 msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
2199 msg = format (msg, "%16U%16d%16d\n", format_ip_address,
2200 &gid_address_ip(&loc->address), loc->priority,
2204 vlib_cli_output (vm, "%v", msg);
2212 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2213 .path = "show lisp locator-set",
2214 .short_help = "Shows locator-sets",
2215 .function = lisp_cp_show_locator_sets_command_fn,
2219 static map_resolver_t *
2220 get_map_resolver (ip_address_t * a)
2222 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2225 vec_foreach (mr, lcm->map_resolvers)
2227 if (!ip_address_cmp (&mr->address, a))
2236 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2238 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2240 map_resolver_t _mr, *mr = &_mr;
2242 if (vnet_lisp_enable_disable_status () == 0)
2244 clib_warning ("LISP is disabled!");
2245 return VNET_API_ERROR_LISP_DISABLED;
2251 if (get_map_resolver (&a->address))
2253 clib_warning ("map-resolver %U already exists!", format_ip_address,
2258 memset (mr, 0, sizeof (*mr));
2259 ip_address_copy (&mr->address, &a->address);
2260 vec_add1 (lcm->map_resolvers, *mr);
2262 if (vec_len (lcm->map_resolvers) == 1)
2263 lcm->do_map_resolver_election = 1;
2267 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2269 mr = vec_elt_at_index (lcm->map_resolvers, i);
2270 if (!ip_address_cmp (&mr->address, &a->address))
2272 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2273 lcm->do_map_resolver_election = 1;
2275 vec_del1 (lcm->map_resolvers, i);
2283 static clib_error_t *
2284 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
2285 unformat_input_t * input,
2286 vlib_cli_command_t * cmd)
2288 unformat_input_t _line_input, *line_input = &_line_input;
2289 u8 is_add = 1, addr_set = 0;
2290 ip_address_t ip_addr;
2291 clib_error_t *error = 0;
2293 vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
2295 /* Get a line of input. */
2296 if (!unformat_user (input, unformat_line_input, line_input))
2299 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2301 if (unformat (line_input, "add"))
2303 else if (unformat (line_input, "del"))
2305 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
2309 error = unformat_parse_error (line_input);
2316 error = clib_error_return (0, "Map-resolver address must be set!");
2321 a->address = ip_addr;
2322 rv = vnet_lisp_add_del_map_resolver (a);
2325 error = clib_error_return (0, "failed to %s map-resolver!",
2326 is_add ? "add" : "delete");
2334 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
2335 .path = "lisp map-resolver",
2336 .short_help = "lisp map-resolver add/del <ip_address>",
2337 .function = lisp_add_del_map_resolver_command_fn,
2342 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2344 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2347 if (vnet_lisp_enable_disable_status () == 0)
2349 clib_warning ("LISP is disabled!");
2350 return VNET_API_ERROR_LISP_DISABLED;
2355 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
2358 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
2359 return VNET_API_ERROR_INVALID_ARGUMENT;
2362 lcm->mreq_itr_rlocs = p[0];
2366 lcm->mreq_itr_rlocs = ~0;
2372 static clib_error_t *
2373 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
2374 unformat_input_t * input,
2375 vlib_cli_command_t * cmd)
2377 unformat_input_t _line_input, *line_input = &_line_input;
2379 u8 *locator_set_name = 0;
2380 clib_error_t *error = 0;
2382 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
2384 /* Get a line of input. */
2385 if (!unformat_user (input, unformat_line_input, line_input))
2388 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2390 if (unformat (line_input, "del"))
2392 else if (unformat (line_input, "add %s", &locator_set_name))
2396 error = unformat_parse_error (line_input);
2402 a->locator_set_name = locator_set_name;
2403 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
2406 error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
2407 is_add ? "add" : "delete");
2410 vec_free (locator_set_name);
2418 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
2419 .path = "lisp map-request itr-rlocs",
2420 .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
2421 .function = lisp_add_del_mreq_itr_rlocs_command_fn,
2425 static clib_error_t *
2426 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
2427 unformat_input_t * input,
2428 vlib_cli_command_t * cmd)
2430 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2431 locator_set_t *loc_set;
2433 vlib_cli_output (vm, "%=20s", "itr-rlocs");
2435 if (~0 == lcm->mreq_itr_rlocs)
2440 loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
2442 vlib_cli_output (vm, "%=20s", loc_set->name);
2448 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
2449 .path = "show lisp map-request itr-rlocs",
2450 .short_help = "Shows map-request itr-rlocs",
2451 .function = lisp_show_mreq_itr_rlocs_command_fn,
2455 /* Statistics (not really errors) */
2456 #define foreach_lisp_cp_lookup_error \
2458 _(MAP_REQUESTS_SENT, "map-request sent")
2460 static char *lisp_cp_lookup_error_strings[] = {
2461 #define _(sym,string) string,
2462 foreach_lisp_cp_lookup_error
2468 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
2469 foreach_lisp_cp_lookup_error
2471 LISP_CP_LOOKUP_N_ERROR,
2472 } lisp_cp_lookup_error_t;
2476 LISP_CP_LOOKUP_NEXT_DROP,
2477 LISP_CP_LOOKUP_NEXT_IP4_LOOKUP,
2478 LISP_CP_LOOKUP_NEXT_IP6_LOOKUP,
2479 LISP_CP_LOOKUP_N_NEXT,
2480 } lisp_cp_lookup_next_t;
2484 gid_address_t dst_eid;
2485 ip_address_t map_resolver_ip;
2486 } lisp_cp_lookup_trace_t;
2489 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
2491 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2492 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2493 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
2495 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
2496 format_ip_address, &t->map_resolver_ip, format_gid_address,
2502 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
2503 ip_address_t * sloc)
2505 map_resolver_t *mrit;
2508 if (vec_len (lcm->map_resolvers) == 0)
2510 clib_warning ("No map-resolver configured");
2514 /* find the first mr ip we have a route to and the ip of the
2515 * iface that has a route to it */
2516 vec_foreach (mrit, lcm->map_resolvers)
2519 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
2521 ip_address_copy (mr_ip, a);
2523 /* also update globals */
2528 clib_warning ("Can't find map-resolver and local interface ip!");
2532 static gid_address_t *
2533 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2539 ip_interface_address_t *ia = 0;
2540 gid_address_t gid_data, *gid = &gid_data;
2541 gid_address_t *rlocs = 0;
2542 ip_prefix_t *ippref = &gid_address_ippref (gid);
2543 ip_address_t *rloc = &ip_prefix_addr (ippref);
2545 memset (gid, 0, sizeof (gid[0]));
2546 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
2547 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
2549 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
2550 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2552 /* Add ipv4 locators first TODO sort them */
2555 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2556 loc->sw_if_index, 1 /* unnumbered */,
2558 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2559 ip_address_set (rloc, addr, IP4);
2560 ip_prefix_len (ippref) = 32;
2561 ip_prefix_normalize (ippref);
2562 vec_add1 (rlocs, gid[0]);
2565 /* Add ipv6 locators */
2566 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2567 loc->sw_if_index, 1 /* unnumbered */,
2569 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2570 ip_address_set (rloc, addr, IP6);
2571 ip_prefix_len (ippref) = 128;
2572 ip_prefix_normalize (ippref);
2573 vec_add1 (rlocs, gid[0]);
2581 static vlib_buffer_t *
2582 build_encapsulated_map_request (lisp_cp_main_t * lcm,
2583 gid_address_t * seid, gid_address_t * deid,
2584 locator_set_t * loc_set, ip_address_t * mr_ip,
2585 ip_address_t * sloc, u8 is_smr_invoked,
2586 u64 * nonce_res, u32 * bi_res)
2590 gid_address_t *rlocs = 0;
2591 vlib_main_t *vm = lcm->vlib_main;
2593 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2595 clib_warning ("Can't allocate buffer for Map-Request!");
2599 b = vlib_get_buffer (vm, bi);
2601 /* leave some space for the encap headers */
2602 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2605 rlocs = build_itr_rloc_list (lcm, loc_set);
2608 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked, nonce_res);
2610 /* push ecm: udp-ip-lisp */
2611 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2613 /* push outer ip header */
2614 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2624 reset_pending_mr_counters (pending_map_request_t * r)
2626 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2631 elect_map_resolver (lisp_cp_main_t * lcm)
2635 vec_foreach (mr, lcm->map_resolvers)
2639 ip_address_copy (&lcm->active_map_resolver, &mr->address);
2640 lcm->do_map_resolver_election = 0;
2647 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
2648 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2650 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2651 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2654 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
2655 gid_address_t * seid, gid_address_t * deid,
2656 u8 is_smr_invoked, u8 is_resend)
2659 u32 next_index, bi = 0, *to_next, map_index;
2663 locator_set_t *loc_set;
2665 pending_map_request_t *pmr, *duplicate_pmr = 0;
2669 ASSERT (*lcm->pending_map_request_lock);
2671 /* if there is already a pending request remember it */
2674 pool_foreach(pmr, lcm->pending_map_requests_pool,
2676 if (!gid_address_cmp (&pmr->src, seid)
2677 && !gid_address_cmp (&pmr->dst, deid))
2679 duplicate_pmr = pmr;
2685 if (!is_resend && duplicate_pmr)
2687 /* don't send the request if there is a pending map request already */
2691 /* get locator-set for seid */
2692 if (!lcm->lisp_pitr)
2694 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2695 if (map_index == ~0)
2697 clib_warning ("No local mapping found in eid-table for %U!",
2698 format_gid_address, seid);
2702 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2707 ("Mapping found for src eid %U is not marked as local!",
2708 format_gid_address, seid);
2711 ls_index = map->locator_set_index;
2715 map_index = lcm->pitr_map_index;
2716 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
2717 ls_index = map->locator_set_index;
2720 /* overwrite locator set if map-request itr-rlocs configured */
2721 if (~0 != lcm->mreq_itr_rlocs)
2723 ls_index = lcm->mreq_itr_rlocs;
2726 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
2728 while (lcm->do_map_resolver_election
2729 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
2730 &lcm->active_map_resolver,
2733 if (0 == elect_map_resolver (lcm))
2734 /* all Mrs are down */
2737 duplicate_pmr->to_be_removed = 1;
2739 /* restart MR checking by marking all of them up */
2740 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
2746 /* build the encapsulated map request */
2747 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
2748 &lcm->active_map_resolver,
2749 &sloc, is_smr_invoked, &nonce, &bi);
2754 /* set fib index to default and lookup node */
2755 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2756 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2757 ip4_lookup_node.index : ip6_lookup_node.index;
2759 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2761 /* Enqueue the packet */
2762 to_next = vlib_frame_vector_args (f);
2765 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2768 /* if there is a pending request already update it */
2770 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
2772 /* remove the oldest nonce */
2773 u64 CLIB_UNUSED (tmp), *nonce_del;
2774 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
2775 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
2776 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
2779 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
2780 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2781 duplicate_pmr - lcm->pending_map_requests_pool);
2785 /* add map-request to pending requests table */
2786 pool_get (lcm->pending_map_requests_pool, pmr);
2787 memset (pmr, 0, sizeof (*pmr));
2788 gid_address_copy (&pmr->src, seid);
2789 gid_address_copy (&pmr->dst, deid);
2790 clib_fifo_add1 (pmr->nonces, nonce);
2791 pmr->is_smr_invoked = is_smr_invoked;
2792 reset_pending_mr_counters (pmr);
2793 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2794 pmr - lcm->pending_map_requests_pool);
2801 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
2803 ip4_header_t *ip4 = hdr;
2806 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2808 ip_address_set (src, &ip4->src_address, IP4);
2809 ip_address_set (dst, &ip4->dst_address, IP4);
2814 ip_address_set (src, &ip6->src_address, IP6);
2815 ip_address_set (dst, &ip6->dst_address, IP6);
2820 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2824 u32 vni = ~0, table_id = ~0, fib_index;
2829 ip4_main_t *im4 = &ip4_main;
2830 fib_index = vec_elt (im4->fib_index_by_sw_if_index,
2831 vnet_buffer (b)->sw_if_index[VLIB_RX]);
2832 fib = find_ip4_fib_by_table_index_or_id (im4, fib_index,
2833 IP4_ROUTE_FLAG_FIB_INDEX);
2834 table_id = fib->table_id;
2839 ip6_main_t *im6 = &ip6_main;
2840 fib_index = vec_elt (im6->fib_index_by_sw_if_index,
2841 vnet_buffer (b)->sw_if_index[VLIB_RX]);
2842 fib = find_ip6_fib_by_table_index_or_id (im6, fib_index,
2843 IP6_ROUTE_FLAG_FIB_INDEX);
2844 table_id = fib->table_id;
2847 vnip = hash_get (lcm->vni_by_table_id, table_id);
2851 clib_warning ("vrf %d is not mapped to any vni!", table_id);
2857 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2863 l2input_main_t *l2im = &l2input_main;
2864 l2_input_config_t *config;
2865 l2_bridge_domain_t *bd_config;
2867 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
2868 config = vec_elt_at_index (l2im->configs, sw_if_index0);
2869 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
2871 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
2875 clib_warning ("bridge domain %d is not mapped to any vni!",
2882 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2883 gid_address_t * src, gid_address_t * dst)
2888 memset (src, 0, sizeof (*src));
2889 memset (dst, 0, sizeof (*dst));
2890 type = vnet_buffer (b)->lisp.overlay_afi;
2892 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
2895 u8 version, preflen;
2897 gid_address_type (src) = GID_ADDR_IP_PREFIX;
2898 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
2900 ip = vlib_buffer_get_current (b);
2901 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
2903 version = gid_address_ip_version (src);
2904 preflen = ip_address_max_len (version);
2905 gid_address_ippref_len (src) = preflen;
2906 gid_address_ippref_len (dst) = preflen;
2908 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
2909 gid_address_vni (dst) = vni;
2910 gid_address_vni (src) = vni;
2912 else if (LISP_AFI_MAC == type)
2914 ethernet_header_t *eh;
2916 eh = vlib_buffer_get_current (b);
2918 gid_address_type (src) = GID_ADDR_MAC;
2919 gid_address_type (dst) = GID_ADDR_MAC;
2920 mac_copy (&gid_address_mac (src), eh->src_address);
2921 mac_copy (&gid_address_mac (dst), eh->dst_address);
2924 vni = lisp_get_vni_from_buffer_eth (lcm, b);
2926 gid_address_vni (dst) = vni;
2927 gid_address_vni (src) = vni;
2932 lisp_cp_lookup (vlib_main_t * vm, vlib_node_runtime_t * node,
2933 vlib_frame_t * from_frame)
2935 u32 *from, *to_next_drop, di, si;
2936 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2937 u32 pkts_mapped = 0;
2938 uword n_left_from, n_left_to_next_drop;
2940 from = vlib_frame_vector_args (from_frame);
2941 n_left_from = from_frame->n_vectors;
2943 while (n_left_from > 0)
2945 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2946 to_next_drop, n_left_to_next_drop);
2948 while (n_left_from > 0 && n_left_to_next_drop > 0)
2952 gid_address_t src, dst;
2957 to_next_drop[0] = pi0;
2959 n_left_to_next_drop -= 1;
2961 b0 = vlib_get_buffer (vm, pi0);
2962 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
2964 /* src/dst eid pair */
2965 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
2967 /* if we have remote mapping for destination already in map-chache
2968 add forwarding tunnel directly. If not send a map-request */
2969 di = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
2972 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
2973 /* send a map-request also in case of negative mapping entry
2974 with corresponding action */
2975 if (m->action == LISP_SEND_MAP_REQUEST)
2977 /* send map-request */
2978 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2979 0 /* is_resend */ );
2984 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
2988 dp_add_fwd_entry (lcm, si, di);
2994 /* send map-request */
2995 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2996 0 /* is_resend */ );
3000 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3002 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3005 memset (tr, 0, sizeof (*tr));
3006 gid_address_copy (&tr->dst_eid, &dst);
3007 if (vec_len (lcm->map_resolvers) > 0)
3009 clib_memcpy (&tr->map_resolver_ip,
3010 vec_elt_at_index (lcm->map_resolvers, 0),
3011 sizeof (ip_address_t));
3014 gid_address_free (&dst);
3015 gid_address_free (&src);
3018 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3019 n_left_to_next_drop);
3021 vlib_node_increment_counter (vm, node->node_index,
3022 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
3024 return from_frame->n_vectors;
3028 VLIB_REGISTER_NODE (lisp_cp_lookup_node) = {
3029 .function = lisp_cp_lookup,
3030 .name = "lisp-cp-lookup",
3031 .vector_size = sizeof (u32),
3032 .format_trace = format_lisp_cp_lookup_trace,
3033 .type = VLIB_NODE_TYPE_INTERNAL,
3035 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3036 .error_strings = lisp_cp_lookup_error_strings,
3038 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3041 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3042 [LISP_CP_LOOKUP_NEXT_IP4_LOOKUP] = "ip4-lookup",
3043 [LISP_CP_LOOKUP_NEXT_IP6_LOOKUP] = "ip6-lookup",
3048 /* lisp_cp_input statistics */
3049 #define foreach_lisp_cp_input_error \
3051 _(MAP_REPLIES_RECEIVED, "map-replies received")
3053 static char *lisp_cp_input_error_strings[] = {
3054 #define _(sym,string) string,
3055 foreach_lisp_cp_input_error
3061 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
3062 foreach_lisp_cp_input_error
3064 LISP_CP_INPUT_N_ERROR,
3065 } lisp_cp_input_error_t;
3069 LISP_CP_INPUT_NEXT_DROP,
3070 LISP_CP_INPUT_N_NEXT,
3071 } lisp_cp_input_next_t;
3075 gid_address_t dst_eid;
3076 ip4_address_t map_resolver_ip;
3077 } lisp_cp_input_trace_t;
3080 format_lisp_cp_input_trace (u8 * s, va_list * args)
3082 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3083 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3084 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
3085 va_arg (*args, lisp_cp_input_trace_t *);
3087 s = format (s, "LISP-CP-INPUT: TODO");
3092 process_map_reply (void *arg)
3094 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3095 vlib_buffer_t *b = arg;
3096 u32 len = 0, i, ttl, dst_map_index = 0;
3098 pending_map_request_t *pmr;
3100 map_reply_hdr_t *mrep_hdr;
3104 u8 authoritative, action;
3105 locator_t *locators = 0, *loc;
3107 mrep_hdr = vlib_buffer_get_current (b);
3109 lisp_pending_map_request_lock (lcm);
3111 /* Check pending requests table and nonce */
3112 nonce = MREP_NONCE (mrep_hdr);
3113 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, nonce);
3116 clib_warning ("No pending map-request entry with nonce %lu!", nonce);
3119 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
3121 vlib_buffer_pull (b, sizeof (*mrep_hdr));
3123 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3125 h = vlib_buffer_get_current (b);
3126 ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3127 action = MAP_REC_ACTION (h);
3128 authoritative = MAP_REC_AUTH (h);
3130 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3133 clib_warning ("Failed to parse mapping record!");
3134 vec_foreach (loc, locators)
3138 vec_free (locators);
3142 /* insert/update mappings cache */
3143 vnet_lisp_add_del_mapping (&deid, locators, action, authoritative, ttl,
3146 /* try to program forwarding only if mapping saved or updated */
3147 if ((u32) ~ 0 != dst_map_index)
3148 lisp_add_del_adjacency (lcm, &pmr->src, &deid, 1);
3150 vec_free (locators);
3153 /* remove pending map request entry */
3156 clib_fifo_foreach (noncep, pmr->nonces, ({
3157 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
3161 clib_fifo_free (pmr->nonces);
3162 pool_put (lcm->pending_map_requests_pool, pmr);
3165 lisp_pending_map_request_unlock (lcm);
3170 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm,
3173 map_request_hdr_t *mreq_hdr;
3174 gid_address_t src, dst;
3177 gid_address_t *itr_rlocs = 0, *rloc;
3179 mreq_hdr = vlib_buffer_get_current (b);
3180 vlib_buffer_pull (b, sizeof (*mreq_hdr));
3182 // nonce = MREQ_NONCE(mreq_hdr);
3184 if (!MREQ_SMR (mreq_hdr))
3186 clib_warning ("Only SMR Map-Requests supported for now!");
3191 len = lisp_msg_parse_addr (b, &src);
3195 /* for now we don't do anything with the itr's rlocs */
3197 lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3198 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
3202 /* TODO: RLOCs are currently unused, so free them for now */
3203 vec_foreach (rloc, itr_rlocs)
3205 gid_address_free (rloc);
3208 /* parse eid records and send SMR-invoked map-requests */
3209 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
3211 memset (&dst, 0, sizeof (dst));
3212 len = lisp_msg_parse_eid_rec (b, &dst);
3215 clib_warning ("Can't parse map-request EID-record");
3218 /* send SMR-invoked map-requests */
3219 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3224 queue_map_reply (vlib_buffer_t * b)
3226 vlib_buffer_t *a = clib_mem_alloc (sizeof (a[0]) + b->current_length);
3228 clib_memcpy (a->data, b->data + b->current_data, b->current_length);
3229 a->current_length = b->current_length;
3230 a->current_data = 0;
3232 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (a[0])
3233 + a->current_length);
3238 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
3239 vlib_frame_t * from_frame)
3241 u32 n_left_from, *from, *to_next_drop;
3242 lisp_msg_type_e type;
3243 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3245 from = vlib_frame_vector_args (from_frame);
3246 n_left_from = from_frame->n_vectors;
3249 while (n_left_from > 0)
3251 u32 n_left_to_next_drop;
3253 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3254 to_next_drop, n_left_to_next_drop);
3255 while (n_left_from > 0 && n_left_to_next_drop > 0)
3263 to_next_drop[0] = bi0;
3265 n_left_to_next_drop -= 1;
3267 b0 = vlib_get_buffer (vm, bi0);
3269 type = lisp_msg_type (vlib_buffer_get_current (b0));
3272 case LISP_MAP_REPLY:
3273 queue_map_reply (b0);
3275 case LISP_MAP_REQUEST:
3276 process_map_request (vm, lcm, b0);
3279 clib_warning ("Unsupported LISP message type %d", type);
3283 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
3285 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3291 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3292 n_left_to_next_drop);
3294 return from_frame->n_vectors;
3298 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3299 .function = lisp_cp_input,
3300 .name = "lisp-cp-input",
3301 .vector_size = sizeof (u32),
3302 .format_trace = format_lisp_cp_input_trace,
3303 .type = VLIB_NODE_TYPE_INTERNAL,
3305 .n_errors = LISP_CP_INPUT_N_ERROR,
3306 .error_strings = lisp_cp_input_error_strings,
3308 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3311 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3317 lisp_cp_init (vlib_main_t * vm)
3319 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3320 clib_error_t *error = 0;
3322 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3325 lcm->im4 = &ip4_main;
3326 lcm->im6 = &ip6_main;
3327 lcm->vlib_main = vm;
3328 lcm->vnet_main = vnet_get_main ();
3329 lcm->mreq_itr_rlocs = ~0;
3332 lcm->pending_map_request_lock =
3333 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
3335 lcm->pending_map_request_lock[0] = 0;
3336 gid_dictionary_init (&lcm->mapping_index_by_gid);
3337 lcm->do_map_resolver_election = 1;
3339 /* default vrf mapped to vni 0 */
3340 hash_set (lcm->table_id_by_vni, 0, 0);
3341 hash_set (lcm->vni_by_table_id, 0, 0);
3343 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
3344 lisp_cp_input_node.index, 1 /* is_ip4 */ );
3345 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
3346 lisp_cp_input_node.index, 0 /* is_ip4 */ );
3352 send_map_request_thread_fn (void *arg)
3354 map_request_args_t *a = arg;
3355 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3357 lisp_pending_map_request_lock (lcm);
3360 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3362 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3364 lisp_pending_map_request_unlock (lcm);
3370 queue_map_request (gid_address_t * seid, gid_address_t * deid,
3371 u8 smr_invoked, u8 is_resend)
3373 map_request_args_t a;
3375 a.is_resend = is_resend;
3376 gid_address_copy (&a.seid, seid);
3377 gid_address_copy (&a.deid, deid);
3378 a.smr_invoked = smr_invoked;
3380 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
3381 (u8 *) & a, sizeof (a));
3386 * Take an action with a pending map request depending on expiration time
3387 * and re-try counters.
3390 update_pending_request (pending_map_request_t * r, f64 dt)
3392 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3395 if (r->time_to_expire - dt < 0)
3396 /* it's time to decide what to do with this pending request */
3398 if (r->retries_num >= NUMBER_OF_RETRIES)
3399 /* too many retries -> assume current map resolver is not available */
3401 mr = get_map_resolver (&lcm->active_map_resolver);
3404 clib_warning ("Map resolver %U not found - probably deleted "
3405 "by the user recently.", format_ip_address,
3406 &lcm->active_map_resolver);
3410 clib_warning ("map resolver %U is unreachable, ignoring",
3411 format_ip_address, &lcm->active_map_resolver);
3413 /* mark current map resolver unavailable so it won't be
3414 * selected next time */
3416 mr->last_update = vlib_time_now (lcm->vlib_main);
3419 reset_pending_mr_counters (r);
3420 elect_map_resolver (lcm);
3422 /* try to find a next eligible map resolver and re-send */
3423 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3429 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3432 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3436 r->time_to_expire -= dt;
3440 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
3443 pending_map_request_t *pmr;
3444 u32 *to_be_removed = 0, *pmr_index;
3446 ASSERT (*lcm->pending_map_request_lock);
3449 pool_foreach (pmr, lcm->pending_map_requests_pool,
3451 if (pmr->to_be_removed)
3453 clib_fifo_foreach (nonce, pmr->nonces, ({
3454 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
3457 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
3462 vec_foreach (pmr_index, to_be_removed)
3463 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
3465 vec_free (to_be_removed);
3469 send_map_resolver_service (vlib_main_t * vm,
3470 vlib_node_runtime_t * rt, vlib_frame_t * f)
3473 pending_map_request_t *pmr;
3474 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3478 vlib_process_wait_for_event_or_clock (vm, period);
3480 /* currently no signals are expected - just wait for clock */
3481 (void) vlib_process_get_events (vm, 0);
3483 lisp_pending_map_request_lock (lcm);
3486 pool_foreach (pmr, lcm->pending_map_requests_pool,
3488 if (!pmr->to_be_removed)
3489 update_pending_request (pmr, period);
3493 remove_dead_pending_map_requests (lcm);
3495 lisp_pending_map_request_unlock (lcm);
3503 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
3504 .function = send_map_resolver_service,
3505 .type = VLIB_NODE_TYPE_PROCESS,
3506 .name = "lisp-retry-service",
3507 .process_log2_n_stack_bytes = 16,
3511 VLIB_INIT_FUNCTION (lisp_cp_init);
3514 * fd.io coding-style-patch-verification: ON
3517 * eval: (c-set-style "gnu")