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>
21 #include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
22 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
23 #include <vnet/fib/fib_entry.h>
24 #include <vnet/fib/fib_table.h>
35 vnet_lisp_get_map_request_mode (void)
37 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
38 return lcm->map_request_mode;
42 queue_map_request (gid_address_t * seid, gid_address_t * deid,
43 u8 smr_invoked, u8 is_resend);
45 ip_interface_address_t *
46 ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
47 u32 sw_if_index, u8 loop)
49 vnet_main_t *vnm = vnet_get_main ();
50 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
51 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
52 sw_if_index = swif->unnumbered_sw_if_index;
54 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
55 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
57 return pool_elt_at_index ((lm)->if_address_pool, ia);
61 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
64 ip_interface_address_t *ia;
66 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
69 return ip_interface_address_get_address (lm, ia);
73 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
74 u8 version, ip_address_t * result)
79 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
80 addr = ip_interface_get_first_address (lm, sw_if_index, version);
84 ip_address_set (result, addr, version);
89 * convert from a LISP address to a FIB prefix
92 ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
94 if (addr->version == IP4)
97 prefix->fp_proto = FIB_PROTOCOL_IP4;
98 memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
99 memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
103 prefix->fp_len = 128;
104 prefix->fp_proto = FIB_PROTOCOL_IP6;
105 memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
110 * convert from a LISP to a FIB prefix
113 ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
114 fib_prefix_t * fib_prefix)
116 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
117 fib_prefix->fp_len = ip_prefix->len;
121 * Find the sw_if_index of the interface that would be used to egress towards
125 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
127 fib_node_index_t fei;
130 ip_address_to_fib_prefix (dst, &prefix);
132 fei = fib_table_lookup (0, &prefix);
134 return (fib_entry_get_resolving_interface (fei));
138 * Find first IP of the interface that would be used to egress towards dst.
139 * Returns 1 if the address is found 0 otherwise.
142 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
143 ip_address_t * result)
146 ip_lookup_main_t *lm;
150 ASSERT (result != 0);
152 ipver = ip_addr_version (dst);
154 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
155 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
160 /* find the first ip address */
161 addr = ip_interface_get_first_address (lm, si, ipver);
165 ip_address_set (result, addr, ipver);
170 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
176 dp_table = hash_get (lcm->table_id_by_vni, vni);
180 clib_warning ("vni %d not associated to a vrf!", vni);
181 return VNET_API_ERROR_INVALID_VALUE;
186 dp_table = hash_get (lcm->bd_id_by_vni, vni);
189 clib_warning ("vni %d not associated to a bridge domain!", vni);
190 return VNET_API_ERROR_INVALID_VALUE;
194 /* enable/disable data-plane interface */
198 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
200 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
205 lisp_gpe_tenant_l2_iface_unlock (vni);
207 lisp_gpe_tenant_l3_iface_unlock (vni);
214 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
216 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
219 memset (a, 0, sizeof (*a));
221 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
225 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
227 /* delete dp fwd entry */
230 a->locator_pairs = fe->locator_pairs;
231 a->vni = gid_address_vni (&fe->reid);
232 gid_address_copy (&a->rmt_eid, &fe->reid);
234 gid_address_copy (&a->lcl_eid, &fe->leid);
236 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
238 /* delete entry in fwd table */
239 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
240 vec_free (fe->locator_pairs);
241 pool_put (lcm->fwd_entry_pool, fe);
245 * Finds first remote locator with best (lowest) priority that has a local
246 * peer locator with an underlying route to it.
250 get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
251 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
253 u32 i, limitp = 0, li, found = 0, esi;
254 locator_set_t *rmt_ls, *lcl_ls;
255 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
256 locator_t *lp, *rmt = 0;
261 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
263 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
265 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
272 /* find unvisited remote locator with best priority */
273 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
275 if (0 != hash_get (checked, i))
278 li = vec_elt (rmt_ls->locator_indices, i);
279 lp = pool_elt_at_index (lcm->locator_pool, li);
281 /* we don't support non-IP locators for now */
282 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
285 if ((found && lp->priority == limitp)
286 || (!found && lp->priority >= limitp))
290 /* don't search for locators with lower priority and don't
291 * check this locator again*/
292 limitp = lp->priority;
293 hash_set (checked, i, 1);
297 /* check if a local locator with a route to remote locator exists */
300 /* find egress sw_if_index for rmt locator */
302 ip_fib_get_egress_iface_for_dst (lcm,
303 &gid_address_ip (&rmt->address));
304 if ((u32) ~ 0 == esi)
307 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
309 li = vec_elt (lcl_ls->locator_indices, i);
310 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
312 /* found local locator with the needed sw_if_index */
313 if (sl->sw_if_index == esi)
315 /* and it has an address */
316 if (0 == ip_interface_get_first_ip_address (lcm,
318 gid_address_ip_version
323 memset (&pair, 0, sizeof (pair));
324 ip_address_copy (&pair.rmt_loc,
325 &gid_address_ip (&rmt->address));
326 ip_address_copy (&pair.lcl_loc, lcl_addr);
327 pair.weight = rmt->weight;
328 vec_add1 (locator_pairs[0], pair);
342 gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
345 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
349 switch (fid_addr_type (fid))
351 case FID_ADDR_IP_PREF:
352 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
353 gid_address_ippref (dst) = fid_addr_ippref (fid);
356 gid_address_type (dst) = GID_ADDR_MAC;
357 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
360 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
366 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
368 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
369 mapping_t *src_map, *dst_map;
371 uword *feip = 0, *dpid;
373 u8 type, is_src_dst = 0;
375 memset (a, 0, sizeof (*a));
377 /* remove entry if it already exists */
378 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
380 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
383 src_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
385 src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
386 dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
388 /* insert data plane forwarding entry */
391 if (MR_MODE_SRC_DST == lcm->map_request_mode)
393 if (GID_ADDR_SRC_DST == gid_address_type (&dst_map->eid))
395 gid_address_sd_to_flat (&a->rmt_eid, &dst_map->eid,
396 &gid_address_sd_dst (&dst_map->eid));
397 gid_address_sd_to_flat (&a->lcl_eid, &dst_map->eid,
398 &gid_address_sd_src (&dst_map->eid));
402 gid_address_copy (&a->rmt_eid, &dst_map->eid);
403 gid_address_copy (&a->lcl_eid, &src_map->eid);
408 gid_address_copy (&a->rmt_eid, &dst_map->eid);
410 a->vni = gid_address_vni (&a->rmt_eid);
412 /* get vrf or bd_index associated to vni */
413 type = gid_address_type (&a->rmt_eid);
414 if (GID_ADDR_IP_PREFIX == type)
416 dpid = hash_get (lcm->table_id_by_vni, a->vni);
419 clib_warning ("vni %d not associated to a vrf!", a->vni);
422 a->table_id = dpid[0];
424 else if (GID_ADDR_MAC == type)
426 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
429 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
435 /* find best locator pair that 1) verifies LISP policy 2) are connected */
436 if (0 == get_locator_pairs (lcm, src_map, dst_map, &a->locator_pairs))
440 a->action = dst_map->action;
444 u8 ipver = ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
445 a->decap_next_index = (ipver == IP4) ?
446 LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
448 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
450 /* add tunnel to fwd entry table XXX check return value from DP insertion */
451 pool_get (lcm->fwd_entry_pool, fe);
452 fe->locator_pairs = a->locator_pairs;
453 gid_address_copy (&fe->reid, &a->rmt_eid);
454 gid_address_copy (&fe->leid, &src_map->eid);
455 fe->is_src_dst = is_src_dst;
456 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
457 fe - lcm->fwd_entry_pool);
461 * Returns vector of adjacencies.
463 * The caller must free the vector returned by this function.
465 * @param vni virtual network identifier
466 * @return vector of adjacencies
469 vnet_lisp_adjacencies_get_by_vni (u32 vni)
471 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
473 lisp_adjacency_t *adjs = 0, adj;
476 pool_foreach(fwd, lcm->fwd_entry_pool,
478 if (gid_address_vni (&fwd->reid) != vni)
481 gid_address_copy (&adj.reid, &fwd->reid);
482 gid_address_copy (&adj.leid, &fwd->leid);
483 vec_add1 (adjs, adj);
490 static clib_error_t *
491 lisp_show_adjacencies_command_fn (vlib_main_t * vm,
492 unformat_input_t * input,
493 vlib_cli_command_t * cmd)
495 lisp_adjacency_t *adjs, *adj;
496 vlib_cli_output (vm, "%s %40s\n", "leid", "reid");
497 unformat_input_t _line_input, *line_input = &_line_input;
500 /* Get a line of input. */
501 if (!unformat_user (input, unformat_line_input, line_input))
504 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
506 if (unformat (line_input, "vni %d", &vni))
510 vlib_cli_output (vm, "parse error: '%U'",
511 format_unformat_error, line_input);
518 vlib_cli_output (vm, "error: no vni specified!");
522 adjs = vnet_lisp_adjacencies_get_by_vni (vni);
524 vec_foreach (adj, adjs)
526 vlib_cli_output (vm, "%U %40U\n", format_gid_address, &adj->leid,
527 format_gid_address, &adj->reid);
535 VLIB_CLI_COMMAND (lisp_show_adjacencies_command) = {
536 .path = "show lisp adjacencies",
537 .short_help = "show lisp adjacencies",
538 .function = lisp_show_adjacencies_command_fn,
543 * Add/remove mapping to/from map-cache. Overwriting not allowed.
546 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
547 u32 * map_index_result)
549 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
550 u32 mi, *map_indexp, map_index, i;
551 mapping_t *m, *old_map;
554 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
555 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
558 /* TODO check if overwriting and take appropriate actions */
559 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
561 clib_warning ("eid %U found in the eid-table", format_gid_address,
563 return VNET_API_ERROR_VALUE_EXIST;
566 pool_get (lcm->mapping_pool, m);
567 gid_address_copy (&m->eid, &a->eid);
568 m->locator_set_index = a->locator_set_index;
570 m->action = a->action;
572 m->is_static = a->is_static;
574 map_index = m - lcm->mapping_pool;
575 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
578 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
580 clib_warning ("Locator set with index %d doesn't exist",
581 a->locator_set_index);
582 return VNET_API_ERROR_INVALID_VALUE;
585 /* add eid to list of eids supported by locator-set */
586 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
587 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
588 a->locator_set_index);
589 vec_add1 (eid_indexes[0], map_index);
594 vec_add1 (lcm->local_mappings_indexes, map_index);
596 map_index_result[0] = map_index;
600 if (mi == GID_LOOKUP_MISS)
602 clib_warning ("eid %U not found in the eid-table",
603 format_gid_address, &a->eid);
604 return VNET_API_ERROR_INVALID_VALUE;
607 /* clear locator-set to eids binding */
608 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
609 a->locator_set_index);
610 for (i = 0; i < vec_len (eid_indexes[0]); i++)
612 map_indexp = vec_elt_at_index (eid_indexes[0], i);
613 if (map_indexp[0] == mi)
616 vec_del1 (eid_indexes[0], i);
618 /* remove local mark if needed */
619 m = pool_elt_at_index (lcm->mapping_pool, mi);
623 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
625 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
626 if (lm_indexp[0] == mi)
629 vec_del1 (lcm->local_mappings_indexes, k);
632 /* remove mapping from dictionary */
633 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
634 gid_address_free (&m->eid);
635 pool_put_index (lcm->mapping_pool, mi);
642 * Add/update/delete mapping to/in/from map-cache.
645 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
646 u32 * map_index_result)
652 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
654 if (vnet_lisp_enable_disable_status () == 0)
656 clib_warning ("LISP is disabled!");
657 return VNET_API_ERROR_LISP_DISABLED;
660 vni = gid_address_vni (&a->eid);
661 type = gid_address_type (&a->eid);
662 if (GID_ADDR_IP_PREFIX == type)
663 dp_table = hash_get (lcm->table_id_by_vni, vni);
664 else if (GID_ADDR_MAC == type)
665 dp_table = hash_get (lcm->bd_id_by_vni, vni);
669 clib_warning ("vni %d not associated to a %s!", vni,
670 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
671 return VNET_API_ERROR_INVALID_VALUE;
674 /* store/remove mapping from map-cache */
675 return vnet_lisp_map_cache_add_del (a, map_index_result);
678 static clib_error_t *
679 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
680 vlib_cli_command_t * cmd)
682 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
683 unformat_input_t _line_input, *line_input = &_line_input;
686 gid_address_t *eids = 0;
687 clib_error_t *error = 0;
688 u8 *locator_set_name = 0;
689 u32 locator_set_index = 0, map_index = 0;
691 vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
695 memset (&eid, 0, sizeof (eid));
696 memset (a, 0, sizeof (*a));
698 /* Get a line of input. */
699 if (!unformat_user (input, unformat_line_input, line_input))
702 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
704 if (unformat (line_input, "add"))
706 else if (unformat (line_input, "del"))
708 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
710 else if (unformat (line_input, "vni %d", &vni))
711 gid_address_vni (&eid) = vni;
712 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
714 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
717 error = clib_error_return (0, "locator-set %s doesn't exist",
721 locator_set_index = p[0];
725 error = unformat_parse_error (line_input);
729 /* XXX treat batch configuration */
731 if (GID_ADDR_SRC_DST == gid_address_type (&eid))
734 clib_error_return (0, "src/dst is not supported for local EIDs!");
738 gid_address_copy (&a->eid, &eid);
740 a->locator_set_index = locator_set_index;
743 rv = vnet_lisp_add_del_local_mapping (a, &map_index);
746 error = clib_error_return (0, "failed to %s local mapping!",
747 is_add ? "add" : "delete");
751 if (locator_set_name)
752 vec_free (locator_set_name);
753 gid_address_free (&a->eid);
758 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
759 .path = "lisp eid-table",
760 .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
761 "locator-set <locator-set>",
762 .function = lisp_add_del_local_eid_command_fn,
767 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
769 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
770 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
772 if (vnet_lisp_enable_disable_status () == 0)
774 clib_warning ("LISP is disabled!");
778 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
779 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
781 if (!is_l2 && (vni == 0 || dp_id == 0))
783 clib_warning ("can't add/del default vni-vrf mapping!");
787 dp_idp = hash_get (dp_table_by_vni[0], vni);
788 vnip = hash_get (vni_by_dp_table[0], dp_id);
794 clib_warning ("vni %d or vrf %d already used in vrf/vni "
795 "mapping!", vni, dp_id);
798 hash_set (dp_table_by_vni[0], vni, dp_id);
799 hash_set (vni_by_dp_table[0], dp_id, vni);
801 /* create dp iface */
802 dp_add_del_iface (lcm, vni, is_l2, 1);
806 if (!dp_idp || !vnip)
808 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
809 "mapping!", vni, dp_id);
812 hash_unset (dp_table_by_vni[0], vni);
813 hash_unset (vni_by_dp_table[0], dp_id);
815 /* remove dp iface */
816 dp_add_del_iface (lcm, vni, is_l2, 0);
822 static clib_error_t *
823 lisp_eid_table_map_command_fn (vlib_main_t * vm,
824 unformat_input_t * input,
825 vlib_cli_command_t * cmd)
827 u8 is_add = 1, is_l2 = 0;
828 u32 vni = 0, dp_id = 0;
829 unformat_input_t _line_input, *line_input = &_line_input;
831 /* Get a line of input. */
832 if (!unformat_user (input, unformat_line_input, line_input))
835 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
837 if (unformat (line_input, "del"))
839 else if (unformat (line_input, "vni %d", &vni))
841 else if (unformat (line_input, "vrf %d", &dp_id))
843 else if (unformat (line_input, "bd %d", &dp_id))
847 return unformat_parse_error (line_input);
850 vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
855 VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
856 .path = "lisp eid-table map",
857 .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
858 .function = lisp_eid_table_map_command_fn,
862 /* return 0 if the two locator sets are identical 1 otherwise */
864 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
865 locator_t * new_locators)
868 locator_t *old_loc, *new_loc;
870 if (vec_len (old_ls_indexes) != vec_len (new_locators))
873 for (i = 0; i < vec_len (new_locators); i++)
875 old_li = vec_elt (old_ls_indexes, i);
876 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
878 new_loc = vec_elt_at_index (new_locators, i);
880 if (locator_cmp (old_loc, new_loc))
890 gid_address_t *eids_to_be_deleted;
891 } remove_mapping_args_t;
894 * Callback invoked when a sub-prefix is found
897 remove_mapping_if_needed (u32 mi, void *arg)
900 remove_mapping_args_t *a = arg;
901 lisp_cp_main_t *lcm = a->lcm;
905 m = pool_elt_at_index (lcm->mapping_pool, mi);
909 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
913 if (0 != vec_len (ls->locator_indices))
918 if (0 == vec_len (ls->locator_indices))
923 vec_add1 (a->eids_to_be_deleted, m->eid);
927 * This function searches map cache and looks for IP prefixes that are subset
928 * of the provided one. If such prefix is found depending on 'is_negative'
931 * 1) if is_negative is true and found prefix points to positive mapping,
932 * then the mapping is removed
933 * 2) if is_negative is false and found prefix points to negative mapping,
934 * then the mapping is removed
937 remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
941 remove_mapping_args_t a;
942 memset (&a, 0, sizeof (a));
944 /* do this only in src/dst mode ... */
945 if (MR_MODE_SRC_DST != lcm->map_request_mode)
948 /* ... and only for IP prefix */
949 if (GID_ADDR_SRC_DST != gid_address_type (eid)
950 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
953 a.is_negative = is_negative;
956 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
957 remove_mapping_if_needed, &a);
959 vec_foreach (e, a.eids_to_be_deleted)
960 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
962 vec_free (a.eids_to_be_deleted);
966 * Adds/removes/updates mapping. Does not program forwarding.
968 * @param eid end-host identifier
969 * @param rlocs vector of remote locators
970 * @param action action for negative map-reply
971 * @param is_add add mapping if non-zero, delete otherwise
972 * @param res_map_index the map-index that was created/updated/removed. It is
973 * set to ~0 if no action is taken.
974 * @param is_static used for distinguishing between statically learned
975 remote mappings and mappings obtained from MR
976 * @return return code
979 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
980 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
983 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
984 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
985 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
986 u32 mi, ls_index = 0, dst_map_index;
989 if (vnet_lisp_enable_disable_status () == 0)
991 clib_warning ("LISP is disabled!");
992 return VNET_API_ERROR_LISP_DISABLED;
996 res_map_index[0] = ~0;
998 memset (m_args, 0, sizeof (m_args[0]));
999 memset (ls_args, 0, sizeof (ls_args[0]));
1001 ls_args->locators = rlocs;
1003 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
1004 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1008 /* overwrite: if mapping already exists, decide if locators should be
1009 * updated and be done */
1010 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
1012 if (!is_static && (old_map->is_static || old_map->local))
1014 /* do not overwrite local or static remote mappings */
1015 clib_warning ("mapping %U rejected due to collision with local "
1016 "or static remote mapping!", format_gid_address,
1021 locator_set_t *old_ls;
1023 /* update mapping attributes */
1024 old_map->action = action;
1025 old_map->authoritative = authoritative;
1028 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1029 old_map->locator_set_index);
1030 if (compare_locators (lcm, old_ls->locator_indices,
1033 /* set locator-set index to overwrite */
1034 ls_args->is_add = 1;
1035 ls_args->index = old_map->locator_set_index;
1036 vnet_lisp_add_del_locator_set (ls_args, 0);
1038 res_map_index[0] = mi;
1044 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1046 ls_args->is_add = 1;
1047 ls_args->index = ~0;
1049 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1052 gid_address_copy (&m_args->eid, eid);
1054 m_args->action = action;
1055 m_args->locator_set_index = ls_index;
1056 m_args->is_static = is_static;
1057 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
1060 res_map_index[0] = dst_map_index;
1065 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
1067 clib_warning ("cannot delete mapping for eid %U",
1068 format_gid_address, eid);
1073 gid_address_copy (&m_args->eid, eid);
1074 m_args->locator_set_index = old_map->locator_set_index;
1076 /* delete mapping associated from map-cache */
1077 vnet_lisp_map_cache_add_del (m_args, 0);
1079 ls_args->is_add = 0;
1080 ls_args->index = old_map->locator_set_index;
1081 /* delete locator set */
1082 vnet_lisp_add_del_locator_set (ls_args, 0);
1084 /* return old mapping index */
1086 res_map_index[0] = mi;
1094 vnet_lisp_clear_all_remote_adjacencies (void)
1097 u32 mi, *map_indices = 0, *map_indexp;
1098 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1099 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1100 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1103 pool_foreach_index (mi, lcm->mapping_pool,
1105 vec_add1 (map_indices, mi);
1109 vec_foreach (map_indexp, map_indices)
1111 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1114 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
1116 dm_args->is_add = 0;
1117 gid_address_copy (&dm_args->eid, &map->eid);
1118 dm_args->locator_set_index = map->locator_set_index;
1120 /* delete mapping associated to fwd entry */
1121 vnet_lisp_map_cache_add_del (dm_args, 0);
1125 ls->index = map->locator_set_index;
1126 /* delete locator set */
1127 rv = vnet_lisp_add_del_locator_set (ls, 0);
1135 vec_free (map_indices);
1140 * Adds adjacency or removes forwarding entry associated to remote mapping.
1141 * Note that adjacencies are not stored, they only result in forwarding entries
1145 lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
1146 gid_address_t * remote_eid, u8 is_add)
1148 u32 local_mi, remote_mi = ~0;
1150 if (vnet_lisp_enable_disable_status () == 0)
1152 clib_warning ("LISP is disabled!");
1153 return VNET_API_ERROR_LISP_DISABLED;
1156 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1157 remote_eid, local_eid);
1158 if (GID_LOOKUP_MISS == remote_mi)
1160 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1161 format_gid_address, remote_eid);
1168 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1171 /* check if source eid has an associated mapping. If pitr mode is on,
1172 * just use the pitr's mapping */
1173 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
1174 gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
1177 if (GID_LOOKUP_MISS == local_mi)
1179 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1180 format_gid_address, local_eid);
1185 /* update forwarding */
1186 dp_add_fwd_entry (lcm, local_mi, remote_mi);
1189 dp_del_fwd_entry (lcm, 0, remote_mi);
1195 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1197 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1198 return lisp_add_del_adjacency (lcm, &a->leid, &a->reid, a->is_add);
1202 * Handler for add/del remote mapping CLI.
1204 * @param vm vlib context
1205 * @param input input from user
1207 * @return pointer to clib error structure
1209 static clib_error_t *
1210 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
1211 unformat_input_t * input,
1212 vlib_cli_command_t * cmd)
1214 clib_error_t *error = 0;
1215 unformat_input_t _line_input, *line_input = &_line_input;
1216 u8 is_add = 1, del_all = 0;
1217 locator_t rloc, *rlocs = 0, *curr_rloc = 0;
1220 u32 vni, action = ~0, p, w;
1223 /* Get a line of input. */
1224 if (!unformat_user (input, unformat_line_input, line_input))
1227 memset (&eid, 0, sizeof (eid));
1228 memset (&rloc, 0, sizeof (rloc));
1230 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1232 if (unformat (line_input, "del-all"))
1234 else if (unformat (line_input, "del"))
1236 else if (unformat (line_input, "add"))
1238 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1240 else if (unformat (line_input, "vni %u", &vni))
1242 gid_address_vni (&eid) = vni;
1244 else if (unformat (line_input, "p %d w %d", &p, &w))
1249 ("No RLOC configured for setting priority/weight!");
1252 curr_rloc->priority = p;
1253 curr_rloc->weight = w;
1255 else if (unformat (line_input, "rloc %U", unformat_ip_address,
1256 &gid_address_ip (&rloc.address)))
1258 /* since rloc is stored in ip prefix we need to set prefix length */
1259 ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
1261 u8 version = gid_address_ip_version (&rloc.address);
1262 ip_prefix_len (pref) = ip_address_max_len (version);
1264 vec_add1 (rlocs, rloc);
1265 curr_rloc = &rlocs[vec_len (rlocs) - 1];
1267 else if (unformat (line_input, "action %U",
1268 unformat_negative_mapping_action, &action))
1272 clib_warning ("parse error");
1279 clib_warning ("missing eid!");
1285 if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1287 clib_warning ("no action set for negative map-reply!");
1293 vnet_lisp_clear_all_remote_adjacencies ();
1297 /* TODO build src/dst with seid */
1299 /* if it's a delete, clean forwarding */
1302 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1303 rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1310 /* add as static remote mapping, i.e., not authoritative and infinite
1312 rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
1313 1 /* is_static */ , 0);
1316 clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
1320 unformat_free (line_input);
1324 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) =
1326 .path = "lisp remote-mapping",.short_help =
1327 "lisp remote-mapping add|del [del-all] vni <vni> "
1328 "eid <est-eid> [action <no-action|natively-forward|"
1329 "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
1330 "[rloc <dst-locator> ... ]",.function =
1331 lisp_add_del_remote_mapping_command_fn,};
1334 * Handler for add/del adjacency CLI.
1336 static clib_error_t *
1337 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
1338 vlib_cli_command_t * cmd)
1340 clib_error_t *error = 0;
1341 unformat_input_t _line_input, *line_input = &_line_input;
1342 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1344 ip_prefix_t *reid_ippref, *leid_ippref;
1345 gid_address_t leid, reid;
1346 u8 *dmac = gid_address_mac (&reid);
1347 u8 *smac = gid_address_mac (&leid);
1348 u8 reid_set = 0, leid_set = 0;
1352 /* Get a line of input. */
1353 if (!unformat_user (input, unformat_line_input, line_input))
1356 memset (&reid, 0, sizeof (reid));
1357 memset (&leid, 0, sizeof (leid));
1359 leid_ippref = &gid_address_ippref (&leid);
1360 reid_ippref = &gid_address_ippref (&reid);
1362 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1364 if (unformat (line_input, "del"))
1366 else if (unformat (line_input, "add"))
1368 else if (unformat (line_input, "reid %U",
1369 unformat_ip_prefix, reid_ippref))
1371 gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
1374 else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
1376 gid_address_type (&reid) = GID_ADDR_MAC;
1379 else if (unformat (line_input, "vni %u", &vni))
1381 gid_address_vni (&leid) = vni;
1382 gid_address_vni (&reid) = vni;
1384 else if (unformat (line_input, "leid %U",
1385 unformat_ip_prefix, leid_ippref))
1387 gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
1390 else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
1392 gid_address_type (&leid) = GID_ADDR_MAC;
1397 clib_warning ("parse error");
1402 if (!reid_set || !leid_set)
1404 clib_warning ("missing remote or local eid!");
1408 if ((gid_address_type (&leid) != gid_address_type (&reid))
1409 || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
1410 && ip_prefix_version (reid_ippref)
1411 != ip_prefix_version (leid_ippref)))
1413 clib_warning ("remote and local EIDs are of different types!");
1417 memset (a, 0, sizeof (a[0]));
1418 gid_address_copy (&a->leid, &leid);
1419 gid_address_copy (&a->reid, &reid);
1422 rv = vnet_lisp_add_del_adjacency (a);
1425 clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
1428 unformat_free (line_input);
1433 VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1434 .path = "lisp adjacency",
1435 .short_help = "lisp adjacency add|del vni <vni> reid <remote-eid> "
1437 .function = lisp_add_del_adjacency_command_fn,
1442 vnet_lisp_set_map_request_mode (u8 mode)
1444 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1446 if (vnet_lisp_enable_disable_status () == 0)
1448 clib_warning ("LISP is disabled!");
1449 return VNET_API_ERROR_LISP_DISABLED;
1452 if (mode >= _MR_MODE_MAX)
1454 clib_warning ("Invalid LISP map request mode %d!", mode);
1455 return VNET_API_ERROR_INVALID_ARGUMENT;
1458 lcm->map_request_mode = mode;
1462 static clib_error_t *
1463 lisp_map_request_mode_command_fn (vlib_main_t * vm,
1464 unformat_input_t * input,
1465 vlib_cli_command_t * cmd)
1467 unformat_input_t _i, *i = &_i;
1468 map_request_mode_t mr_mode = _MR_MODE_MAX;
1470 /* Get a line of input. */
1471 if (!unformat_user (input, unformat_line_input, i))
1474 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1476 if (unformat (i, "dst-only"))
1477 mr_mode = MR_MODE_DST_ONLY;
1478 else if (unformat (i, "src-dst"))
1479 mr_mode = MR_MODE_SRC_DST;
1482 clib_warning ("parse error '%U'", format_unformat_error, i);
1487 if (_MR_MODE_MAX == mr_mode)
1489 clib_warning ("No LISP map request mode entered!");
1493 vnet_lisp_set_map_request_mode (mr_mode);
1499 VLIB_CLI_COMMAND (lisp_map_request_mode_command) = {
1500 .path = "lisp map-request mode",
1501 .short_help = "lisp map-request mode dst-only|src-dst",
1502 .function = lisp_map_request_mode_command_fn,
1507 format_lisp_map_request_mode (u8 * s, va_list * args)
1509 u32 mode = va_arg (*args, u32);
1514 return format (0, "dst-only");
1516 return format (0, "src-dst");
1521 static clib_error_t *
1522 lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
1523 unformat_input_t * input,
1524 vlib_cli_command_t * cmd)
1526 vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
1527 vnet_lisp_get_map_request_mode ());
1532 VLIB_CLI_COMMAND (lisp_show_map_request_mode_command) = {
1533 .path = "show lisp map-request mode",
1534 .short_help = "show lisp map-request mode",
1535 .function = lisp_show_map_request_mode_command_fn,
1539 static clib_error_t *
1540 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
1541 unformat_input_t * input,
1542 vlib_cli_command_t * cmd)
1545 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1547 vec_foreach (mr, lcm->map_resolvers)
1549 vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
1555 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1556 .path = "show lisp map-resolvers",
1557 .short_help = "show lisp map-resolvers",
1558 .function = lisp_show_map_resolvers_command_fn,
1563 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1565 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1566 u32 locator_set_index = ~0;
1570 if (vnet_lisp_enable_disable_status () == 0)
1572 clib_warning ("LISP is disabled!");
1573 return VNET_API_ERROR_LISP_DISABLED;
1576 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1579 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1582 locator_set_index = p[0];
1586 pool_get (lcm->mapping_pool, m);
1587 m->locator_set_index = locator_set_index;
1589 lcm->pitr_map_index = m - lcm->mapping_pool;
1591 /* enable pitr mode */
1596 /* remove pitr mapping */
1597 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1599 /* disable pitr mode */
1605 static clib_error_t *
1606 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
1607 unformat_input_t * input,
1608 vlib_cli_command_t * cmd)
1610 u8 locator_name_set = 0;
1611 u8 *locator_set_name = 0;
1613 unformat_input_t _line_input, *line_input = &_line_input;
1614 clib_error_t *error = 0;
1617 /* Get a line of input. */
1618 if (!unformat_user (input, unformat_line_input, line_input))
1621 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1623 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
1624 locator_name_set = 1;
1625 else if (unformat (line_input, "disable"))
1628 return clib_error_return (0, "parse error");
1631 if (!locator_name_set)
1633 clib_warning ("No locator set specified!");
1636 rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1639 error = clib_error_return (0, "failed to %s pitr!",
1640 is_add ? "add" : "delete");
1644 if (locator_set_name)
1645 vec_free (locator_set_name);
1650 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1651 .path = "lisp pitr",
1652 .short_help = "lisp pitr [disable] ls <locator-set-name>",
1653 .function = lisp_pitr_set_locator_set_command_fn,
1657 static clib_error_t *
1658 lisp_show_pitr_command_fn (vlib_main_t * vm,
1659 unformat_input_t * input, vlib_cli_command_t * cmd)
1661 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1666 vlib_cli_output (vm, "%=20s%=16s",
1667 "pitr", lcm->lisp_pitr ? "locator-set" : "");
1669 if (!lcm->lisp_pitr)
1671 vlib_cli_output (vm, "%=20s", "disable");
1675 if (~0 == lcm->pitr_map_index)
1677 tmp_str = format (0, "N/A");
1681 m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1682 if (~0 != m->locator_set_index)
1685 pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1686 tmp_str = format (0, "%s", ls->name);
1690 tmp_str = format (0, "N/A");
1693 vec_add1 (tmp_str, 0);
1695 vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1703 VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1704 .path = "show lisp pitr",
1705 .short_help = "Show pitr",
1706 .function = lisp_show_pitr_command_fn,
1711 format_eid_entry (u8 * s, va_list * args)
1713 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
1714 lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
1715 mapping_t *mapit = va_arg (*args, mapping_t *);
1716 locator_set_t *ls = va_arg (*args, locator_set_t *);
1717 gid_address_t *gid = &mapit->eid;
1718 u32 ttl = mapit->ttl;
1719 u8 aut = mapit->authoritative;
1724 u8 *type = ls->local ? format (0, "local(%s)", ls->name)
1725 : format (0, "remote");
1727 if (vec_len (ls->locator_indices) == 0)
1729 s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
1734 vec_foreach (loc_index, ls->locator_indices)
1736 locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1738 loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1741 loc = format (0, "%U", format_ip_address,
1742 &gid_address_ip (&l->address));
1746 s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
1747 gid, type, loc, ttl, aut);
1751 s = format (s, "%55s%v\n", "", loc);
1757 static clib_error_t *
1758 lisp_show_eid_table_command_fn (vlib_main_t * vm,
1759 unformat_input_t * input,
1760 vlib_cli_command_t * cmd)
1762 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1764 unformat_input_t _line_input, *line_input = &_line_input;
1770 memset (&eid, 0, sizeof (eid));
1772 /* Get a line of input. */
1773 if (!unformat_user (input, unformat_line_input, line_input))
1776 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1778 if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1780 else if (unformat (line_input, "local"))
1782 else if (unformat (line_input, "remote"))
1785 return clib_error_return (0, "parse error: '%U'",
1786 format_unformat_error, line_input);
1789 vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
1790 "EID", "type", "locators", "ttl", "autoritative");
1795 pool_foreach (mapit, lcm->mapping_pool,
1797 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
1798 mapit->locator_set_index);
1799 if (filter && !((1 == filter && ls->local) ||
1800 (2 == filter && !ls->local)))
1804 vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
1811 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
1812 if ((u32) ~ 0 == mi)
1815 mapit = pool_elt_at_index (lcm->mapping_pool, mi);
1816 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
1817 mapit->locator_set_index);
1819 if (filter && !((1 == filter && ls->local) ||
1820 (2 == filter && !ls->local)))
1825 vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
1833 VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
1834 .path = "show lisp eid-table",
1835 .short_help = "Shows EID table",
1836 .function = lisp_show_eid_table_command_fn,
1840 /* cleans locator to locator-set data and removes locators not part of
1841 * any locator-set */
1843 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1845 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
1846 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1847 for (i = 0; i < vec_len (ls->locator_indices); i++)
1849 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1850 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1852 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1854 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1855 if (ls_indexp[0] == lsi)
1859 /* delete index for removed locator-set */
1860 vec_del1 (ls_indexes[0], j);
1862 /* delete locator if it's part of no locator-set */
1863 if (vec_len (ls_indexes[0]) == 0)
1865 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1866 vec_add1 (to_be_deleted, i);
1872 for (i = 0; i < vec_len (to_be_deleted); i++)
1874 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1875 vec_del1 (ls->locator_indices, loc_indexp[0]);
1877 vec_free (to_be_deleted);
1881 static inline uword *
1882 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
1884 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1889 /* find locator-set */
1892 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
1903 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
1909 ASSERT (ls != NULL);
1910 ASSERT (loc != NULL);
1912 vec_foreach (locit, ls->locator_indices)
1914 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1915 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1916 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1918 clib_warning ("Duplicate locator");
1919 return VNET_API_ERROR_VALUE_EXIST;
1927 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1928 u32 ls_index, u32 loc_id)
1930 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1931 u32 **ls_indexes = NULL;
1933 ASSERT (ls != NULL);
1934 ASSERT (locit != NULL);
1936 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1937 pool_put_index (lcm->locator_pool, locit[0]);
1938 vec_del1 (ls->locator_indices, loc_id);
1939 vec_del1 (ls_indexes[0], ls_index);
1943 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1944 locator_set_t * ls, u32 * ls_result)
1946 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1947 locator_t *loc = NULL, *itloc = NULL;
1948 uword _p = (u32) ~ 0, *p = &_p;
1949 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
1955 if (vnet_lisp_enable_disable_status () == 0)
1957 clib_warning ("LISP is disabled!");
1958 return VNET_API_ERROR_LISP_DISABLED;
1961 p = get_locator_set_index (a, p);
1964 clib_warning ("locator-set %v doesn't exist", a->name);
1965 return VNET_API_ERROR_INVALID_ARGUMENT;
1970 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1973 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1975 return VNET_API_ERROR_INVALID_ARGUMENT;
1982 ls_result[0] = p[0];
1984 /* allocate locators */
1985 vec_foreach (itloc, a->locators)
1987 ret = is_locator_in_locator_set (lcm, ls, itloc);
1993 pool_get (lcm->locator_pool, loc);
1995 loc_index = loc - lcm->locator_pool;
1997 vec_add1 (ls->locator_indices, loc_index);
1999 vec_validate (lcm->locator_to_locator_sets, loc_index);
2000 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2002 vec_add1 (ls_indexes[0], p[0]);
2009 itloc = a->locators;
2011 vec_foreach (locit, ls->locator_indices)
2013 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2015 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
2017 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2019 if (0 == loc->local &&
2020 !gid_address_cmp (&loc->address, &itloc->address))
2022 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2033 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
2036 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2038 uword _p = (u32) ~ 0, *p = &_p;
2043 if (vnet_lisp_enable_disable_status () == 0)
2045 clib_warning ("LISP is disabled!");
2046 return VNET_API_ERROR_LISP_DISABLED;
2051 p = get_locator_set_index (a, p);
2054 if (p && p[0] != (u32) ~ 0)
2056 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2059 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2064 /* clean locator to locator-set vectors and remove locators if
2065 * they're not part of another locator-set */
2066 clean_locator_to_locator_set (lcm, p[0]);
2068 /* remove locator indices from locator set */
2069 vec_free (ls->locator_indices);
2074 ls_result[0] = p[0];
2076 /* new locator-set */
2079 pool_get (lcm->locator_set_pool, ls);
2080 memset (ls, 0, sizeof (*ls));
2081 ls_index = ls - lcm->locator_set_pool;
2085 ls->name = vec_dup (a->name);
2087 if (!lcm->locator_set_index_by_name)
2088 lcm->locator_set_index_by_name = hash_create_vec (
2096 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
2099 /* mark as local locator-set */
2100 vec_add1 (lcm->local_locator_set_indexes, ls_index);
2102 ls->local = a->local;
2104 ls_result[0] = ls_index;
2107 ret = vnet_lisp_add_del_locator (a, ls, NULL);
2115 p = get_locator_set_index (a, p);
2118 clib_warning ("locator-set %v doesn't exists", a->name);
2122 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2125 clib_warning ("locator-set with index %d doesn't exists", p[0]);
2129 if (lcm->mreq_itr_rlocs == p[0])
2131 clib_warning ("Can't delete the locator-set used to constrain "
2132 "the itr-rlocs in map-requests!");
2136 if (vec_len (lcm->locator_set_to_eids) != 0)
2138 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
2139 if (vec_len (eid_indexes[0]) != 0)
2142 ("Can't delete a locator that supports a mapping!");
2147 /* clean locator to locator-sets data */
2148 clean_locator_to_locator_set (lcm, p[0]);
2154 vec_foreach_index (it, lcm->local_locator_set_indexes)
2156 lsi = vec_elt (lcm->local_locator_set_indexes, it);
2159 vec_del1 (lcm->local_locator_set_indexes, it);
2163 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2165 vec_free (ls->name);
2166 vec_free (ls->locator_indices);
2167 pool_put (lcm->locator_set_pool, ls);
2173 vnet_lisp_enable_disable (u8 is_enable)
2176 clib_error_t *error = 0;
2177 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2178 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
2180 a->is_en = is_enable;
2181 error = vnet_lisp_gpe_enable_disable (a);
2184 return clib_error_return (0, "failed to %s data-plane!",
2185 a->is_en ? "enable" : "disable");
2190 /* enable all l2 and l3 ifaces */
2193 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2194 dp_add_del_iface(lcm, vni, 0, 1);
2196 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2197 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
2203 /* clear interface table */
2204 hash_free (lcm->fwd_entry_by_mapping_index);
2205 pool_free (lcm->fwd_entry_pool);
2208 /* update global flag */
2209 lcm->is_enabled = is_enable;
2214 static clib_error_t *
2215 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
2216 vlib_cli_command_t * cmd)
2218 unformat_input_t _line_input, *line_input = &_line_input;
2222 /* Get a line of input. */
2223 if (!unformat_user (input, unformat_line_input, line_input))
2226 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2228 if (unformat (line_input, "enable"))
2233 else if (unformat (line_input, "disable"))
2237 return clib_error_return (0, "parse error: '%U'",
2238 format_unformat_error, line_input);
2243 return clib_error_return (0, "state not set");
2245 return vnet_lisp_enable_disable (is_enabled);
2249 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
2251 .short_help = "lisp [enable|disable]",
2252 .function = lisp_enable_disable_command_fn,
2257 vnet_lisp_enable_disable_status (void)
2259 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2260 return lcm->is_enabled;
2264 format_lisp_status (u8 * s, va_list * args)
2266 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2267 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
2270 static clib_error_t *
2271 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
2272 vlib_cli_command_t * cmd)
2275 msg = format (msg, "feature: %U\ngpe: %U\n",
2276 format_lisp_status, format_vnet_lisp_gpe_status);
2277 vlib_cli_output (vm, "%v", msg);
2283 VLIB_CLI_COMMAND (lisp_show_status_command) = {
2284 .path = "show lisp status",
2285 .short_help = "show lisp status",
2286 .function = lisp_show_status_command_fn,
2290 static clib_error_t *
2291 lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
2292 unformat_input_t * input,
2293 vlib_cli_command_t * cmd)
2296 unformat_input_t _line_input, *line_input = &_line_input;
2297 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2298 uword *vni_table = 0;
2301 /* Get a line of input. */
2302 if (!unformat_user (input, unformat_line_input, line_input))
2305 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2307 if (unformat (line_input, "l2"))
2309 vni_table = lcm->bd_id_by_vni;
2312 else if (unformat (line_input, "l3"))
2314 vni_table = lcm->table_id_by_vni;
2318 return clib_error_return (0, "parse error: '%U'",
2319 format_unformat_error, line_input);
2324 vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
2328 vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
2331 hash_foreach_pair (p, vni_table,
2333 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
2341 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
2342 .path = "show lisp eid-table map",
2343 .short_help = "show lisp eid-table l2|l3",
2344 .function = lisp_show_eid_table_map_command_fn,
2348 static clib_error_t *
2349 lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
2350 unformat_input_t * input,
2351 vlib_cli_command_t * cmd)
2353 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2354 vnet_main_t *vnm = lgm->vnet_main;
2355 unformat_input_t _line_input, *line_input = &_line_input;
2357 clib_error_t *error = 0;
2358 u8 *locator_set_name = 0;
2359 locator_t locator, *locators = 0;
2360 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2364 memset (&locator, 0, sizeof (locator));
2365 memset (a, 0, sizeof (a[0]));
2367 /* Get a line of input. */
2368 if (!unformat_user (input, unformat_line_input, line_input))
2371 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2373 if (unformat (line_input, "add %_%v%_", &locator_set_name))
2375 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
2377 else if (unformat (line_input, "iface %U p %d w %d",
2378 unformat_vnet_sw_interface, vnm,
2379 &locator.sw_if_index, &locator.priority,
2383 vec_add1 (locators, locator);
2387 error = unformat_parse_error (line_input);
2392 a->name = locator_set_name;
2393 a->locators = locators;
2397 rv = vnet_lisp_add_del_locator_set (a, &ls_index);
2400 error = clib_error_return (0, "failed to %s locator-set!",
2401 is_add ? "add" : "delete");
2405 vec_free (locators);
2406 if (locator_set_name)
2407 vec_free (locator_set_name);
2412 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2413 .path = "lisp locator-set",
2414 .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2415 "p <priority> w <weight>]",
2416 .function = lisp_add_del_locator_set_command_fn,
2420 static clib_error_t *
2421 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
2422 unformat_input_t * input,
2423 vlib_cli_command_t * cmd)
2425 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2426 vnet_main_t *vnm = lgm->vnet_main;
2427 unformat_input_t _line_input, *line_input = &_line_input;
2429 clib_error_t *error = 0;
2430 u8 *locator_set_name = 0;
2431 u8 locator_set_name_set = 0;
2432 locator_t locator, *locators = 0;
2433 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2436 memset (&locator, 0, sizeof (locator));
2437 memset (a, 0, sizeof (a[0]));
2439 /* Get a line of input. */
2440 if (!unformat_user (input, unformat_line_input, line_input))
2443 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2445 if (unformat (line_input, "add"))
2447 else if (unformat (line_input, "del"))
2449 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
2450 locator_set_name_set = 1;
2451 else if (unformat (line_input, "iface %U p %d w %d",
2452 unformat_vnet_sw_interface, vnm,
2453 &locator.sw_if_index, &locator.priority,
2457 vec_add1 (locators, locator);
2461 error = unformat_parse_error (line_input);
2466 if (!locator_set_name_set)
2468 error = clib_error_return (0, "locator_set name not set!");
2472 a->name = locator_set_name;
2473 a->locators = locators;
2477 vnet_lisp_add_del_locator (a, 0, &ls_index);
2480 vec_free (locators);
2481 vec_free (locator_set_name);
2486 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2487 .path = "lisp locator",
2488 .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2489 "p <priority> w <weight>",
2490 .function = lisp_add_del_locator_in_set_command_fn,
2494 static clib_error_t *
2495 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
2496 unformat_input_t * input,
2497 vlib_cli_command_t * cmd)
2499 locator_set_t *lsit;
2502 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2504 vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
2505 "Priority", "Weight");
2508 pool_foreach (lsit, lcm->locator_set_pool,
2514 msg = format (msg, "%v", lsit->name);
2518 msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
2520 vec_foreach (locit, lsit->locator_indices)
2524 msg = format (msg, "%16s", " ");
2526 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2528 msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
2531 msg = format (msg, "%16U%16d%16d\n", format_ip_address,
2532 &gid_address_ip(&loc->address), loc->priority,
2536 vlib_cli_output (vm, "%v", msg);
2544 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2545 .path = "show lisp locator-set",
2546 .short_help = "Shows locator-sets",
2547 .function = lisp_cp_show_locator_sets_command_fn,
2551 static map_resolver_t *
2552 get_map_resolver (ip_address_t * a)
2554 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2557 vec_foreach (mr, lcm->map_resolvers)
2559 if (!ip_address_cmp (&mr->address, a))
2568 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2570 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2572 map_resolver_t _mr, *mr = &_mr;
2574 if (vnet_lisp_enable_disable_status () == 0)
2576 clib_warning ("LISP is disabled!");
2577 return VNET_API_ERROR_LISP_DISABLED;
2583 if (get_map_resolver (&a->address))
2585 clib_warning ("map-resolver %U already exists!", format_ip_address,
2590 memset (mr, 0, sizeof (*mr));
2591 ip_address_copy (&mr->address, &a->address);
2592 vec_add1 (lcm->map_resolvers, *mr);
2594 if (vec_len (lcm->map_resolvers) == 1)
2595 lcm->do_map_resolver_election = 1;
2599 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2601 mr = vec_elt_at_index (lcm->map_resolvers, i);
2602 if (!ip_address_cmp (&mr->address, &a->address))
2604 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2605 lcm->do_map_resolver_election = 1;
2607 vec_del1 (lcm->map_resolvers, i);
2615 static clib_error_t *
2616 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
2617 unformat_input_t * input,
2618 vlib_cli_command_t * cmd)
2620 unformat_input_t _line_input, *line_input = &_line_input;
2621 u8 is_add = 1, addr_set = 0;
2622 ip_address_t ip_addr;
2623 clib_error_t *error = 0;
2625 vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
2627 /* Get a line of input. */
2628 if (!unformat_user (input, unformat_line_input, line_input))
2631 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2633 if (unformat (line_input, "add"))
2635 else if (unformat (line_input, "del"))
2637 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
2641 error = unformat_parse_error (line_input);
2648 error = clib_error_return (0, "Map-resolver address must be set!");
2653 a->address = ip_addr;
2654 rv = vnet_lisp_add_del_map_resolver (a);
2657 error = clib_error_return (0, "failed to %s map-resolver!",
2658 is_add ? "add" : "delete");
2666 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
2667 .path = "lisp map-resolver",
2668 .short_help = "lisp map-resolver add/del <ip_address>",
2669 .function = lisp_add_del_map_resolver_command_fn,
2674 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2676 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2679 if (vnet_lisp_enable_disable_status () == 0)
2681 clib_warning ("LISP is disabled!");
2682 return VNET_API_ERROR_LISP_DISABLED;
2687 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
2690 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
2691 return VNET_API_ERROR_INVALID_ARGUMENT;
2694 lcm->mreq_itr_rlocs = p[0];
2698 lcm->mreq_itr_rlocs = ~0;
2704 static clib_error_t *
2705 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
2706 unformat_input_t * input,
2707 vlib_cli_command_t * cmd)
2709 unformat_input_t _line_input, *line_input = &_line_input;
2711 u8 *locator_set_name = 0;
2712 clib_error_t *error = 0;
2714 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
2716 /* Get a line of input. */
2717 if (!unformat_user (input, unformat_line_input, line_input))
2720 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2722 if (unformat (line_input, "del"))
2724 else if (unformat (line_input, "add %_%v%_", &locator_set_name))
2728 error = unformat_parse_error (line_input);
2734 a->locator_set_name = locator_set_name;
2735 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
2738 error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
2739 is_add ? "add" : "delete");
2742 vec_free (locator_set_name);
2750 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
2751 .path = "lisp map-request itr-rlocs",
2752 .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
2753 .function = lisp_add_del_mreq_itr_rlocs_command_fn,
2757 static clib_error_t *
2758 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
2759 unformat_input_t * input,
2760 vlib_cli_command_t * cmd)
2762 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2763 locator_set_t *loc_set;
2765 vlib_cli_output (vm, "%=20s", "itr-rlocs");
2767 if (~0 == lcm->mreq_itr_rlocs)
2772 loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
2774 vlib_cli_output (vm, "%=20s", loc_set->name);
2780 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
2781 .path = "show lisp map-request itr-rlocs",
2782 .short_help = "Shows map-request itr-rlocs",
2783 .function = lisp_show_mreq_itr_rlocs_command_fn,
2787 /* Statistics (not really errors) */
2788 #define foreach_lisp_cp_lookup_error \
2790 _(MAP_REQUESTS_SENT, "map-request sent")
2792 static char *lisp_cp_lookup_error_strings[] = {
2793 #define _(sym,string) string,
2794 foreach_lisp_cp_lookup_error
2800 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
2801 foreach_lisp_cp_lookup_error
2803 LISP_CP_LOOKUP_N_ERROR,
2804 } lisp_cp_lookup_error_t;
2808 LISP_CP_LOOKUP_NEXT_DROP,
2809 LISP_CP_LOOKUP_N_NEXT,
2810 } lisp_cp_lookup_next_t;
2814 gid_address_t dst_eid;
2815 ip_address_t map_resolver_ip;
2816 } lisp_cp_lookup_trace_t;
2819 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
2821 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2822 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2823 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
2825 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
2826 format_ip_address, &t->map_resolver_ip, format_gid_address,
2832 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
2833 ip_address_t * sloc)
2835 map_resolver_t *mrit;
2838 if (vec_len (lcm->map_resolvers) == 0)
2840 clib_warning ("No map-resolver configured");
2844 /* find the first mr ip we have a route to and the ip of the
2845 * iface that has a route to it */
2846 vec_foreach (mrit, lcm->map_resolvers)
2849 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
2851 ip_address_copy (mr_ip, a);
2853 /* also update globals */
2858 clib_warning ("Can't find map-resolver and local interface ip!");
2862 static gid_address_t *
2863 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2869 ip_interface_address_t *ia = 0;
2870 gid_address_t gid_data, *gid = &gid_data;
2871 gid_address_t *rlocs = 0;
2872 ip_prefix_t *ippref = &gid_address_ippref (gid);
2873 ip_address_t *rloc = &ip_prefix_addr (ippref);
2875 memset (gid, 0, sizeof (gid[0]));
2876 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
2877 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
2879 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
2880 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2882 /* Add ipv4 locators first TODO sort them */
2885 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2886 loc->sw_if_index, 1 /* unnumbered */,
2888 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2889 ip_address_set (rloc, addr, IP4);
2890 ip_prefix_len (ippref) = 32;
2891 ip_prefix_normalize (ippref);
2892 vec_add1 (rlocs, gid[0]);
2895 /* Add ipv6 locators */
2896 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2897 loc->sw_if_index, 1 /* unnumbered */,
2899 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2900 ip_address_set (rloc, addr, IP6);
2901 ip_prefix_len (ippref) = 128;
2902 ip_prefix_normalize (ippref);
2903 vec_add1 (rlocs, gid[0]);
2911 static vlib_buffer_t *
2912 build_encapsulated_map_request (lisp_cp_main_t * lcm,
2913 gid_address_t * seid, gid_address_t * deid,
2914 locator_set_t * loc_set, ip_address_t * mr_ip,
2915 ip_address_t * sloc, u8 is_smr_invoked,
2916 u64 * nonce_res, u32 * bi_res)
2920 gid_address_t *rlocs = 0;
2921 vlib_main_t *vm = lcm->vlib_main;
2923 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2925 clib_warning ("Can't allocate buffer for Map-Request!");
2929 b = vlib_get_buffer (vm, bi);
2931 /* leave some space for the encap headers */
2932 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2935 rlocs = build_itr_rloc_list (lcm, loc_set);
2937 if (MR_MODE_SRC_DST == lcm->map_request_mode
2938 && GID_ADDR_SRC_DST != gid_address_type (deid))
2941 memset (&sd, 0, sizeof (sd));
2942 build_src_dst (&sd, seid, deid);
2943 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked, nonce_res);
2948 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
2952 /* push ecm: udp-ip-lisp */
2953 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2955 /* push outer ip header */
2956 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2966 reset_pending_mr_counters (pending_map_request_t * r)
2968 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2973 elect_map_resolver (lisp_cp_main_t * lcm)
2977 vec_foreach (mr, lcm->map_resolvers)
2981 ip_address_copy (&lcm->active_map_resolver, &mr->address);
2982 lcm->do_map_resolver_election = 0;
2989 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
2990 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2992 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2993 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2996 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
2997 gid_address_t * seid, gid_address_t * deid,
2998 u8 is_smr_invoked, u8 is_resend)
3001 u32 next_index, bi = 0, *to_next, map_index;
3005 locator_set_t *loc_set;
3007 pending_map_request_t *pmr, *duplicate_pmr = 0;
3011 ASSERT (*lcm->pending_map_request_lock);
3013 /* if there is already a pending request remember it */
3016 pool_foreach(pmr, lcm->pending_map_requests_pool,
3018 if (!gid_address_cmp (&pmr->src, seid)
3019 && !gid_address_cmp (&pmr->dst, deid))
3021 duplicate_pmr = pmr;
3027 if (!is_resend && duplicate_pmr)
3029 /* don't send the request if there is a pending map request already */
3033 /* get locator-set for seid */
3034 if (!lcm->lisp_pitr)
3036 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
3037 if (map_index == ~0)
3039 clib_warning ("No local mapping found in eid-table for %U!",
3040 format_gid_address, seid);
3044 map = pool_elt_at_index (lcm->mapping_pool, map_index);
3049 ("Mapping found for src eid %U is not marked as local!",
3050 format_gid_address, seid);
3053 ls_index = map->locator_set_index;
3057 map_index = lcm->pitr_map_index;
3058 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
3059 ls_index = map->locator_set_index;
3062 /* overwrite locator set if map-request itr-rlocs configured */
3063 if (~0 != lcm->mreq_itr_rlocs)
3065 ls_index = lcm->mreq_itr_rlocs;
3068 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
3070 while (lcm->do_map_resolver_election
3071 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
3072 &lcm->active_map_resolver,
3075 if (0 == elect_map_resolver (lcm))
3076 /* all Mrs are down */
3079 duplicate_pmr->to_be_removed = 1;
3081 /* restart MR checking by marking all of them up */
3082 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
3088 /* build the encapsulated map request */
3089 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
3090 &lcm->active_map_resolver,
3091 &sloc, is_smr_invoked, &nonce, &bi);
3096 /* set fib index to default and lookup node */
3097 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3098 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3099 ip4_lookup_node.index : ip6_lookup_node.index;
3101 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3103 /* Enqueue the packet */
3104 to_next = vlib_frame_vector_args (f);
3107 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3110 /* if there is a pending request already update it */
3112 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
3114 /* remove the oldest nonce */
3115 u64 CLIB_UNUSED (tmp), *nonce_del;
3116 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
3117 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
3118 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
3121 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
3122 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3123 duplicate_pmr - lcm->pending_map_requests_pool);
3127 /* add map-request to pending requests table */
3128 pool_get (lcm->pending_map_requests_pool, pmr);
3129 memset (pmr, 0, sizeof (*pmr));
3130 gid_address_copy (&pmr->src, seid);
3131 gid_address_copy (&pmr->dst, deid);
3132 clib_fifo_add1 (pmr->nonces, nonce);
3133 pmr->is_smr_invoked = is_smr_invoked;
3134 reset_pending_mr_counters (pmr);
3135 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3136 pmr - lcm->pending_map_requests_pool);
3143 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
3145 ip4_header_t *ip4 = hdr;
3148 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3150 ip_address_set (src, &ip4->src_address, IP4);
3151 ip_address_set (dst, &ip4->dst_address, IP4);
3156 ip_address_set (src, &ip6->src_address, IP6);
3157 ip_address_set (dst, &ip6->dst_address, IP6);
3162 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3166 u32 vni = ~0, table_id = ~0;
3168 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
3169 IP4 ? FIB_PROTOCOL_IP4 :
3175 vnip = hash_get (lcm->vni_by_table_id, table_id);
3179 clib_warning ("vrf %d is not mapped to any vni!", table_id);
3185 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3191 l2input_main_t *l2im = &l2input_main;
3192 l2_input_config_t *config;
3193 l2_bridge_domain_t *bd_config;
3195 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3196 config = vec_elt_at_index (l2im->configs, sw_if_index0);
3197 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3199 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
3203 clib_warning ("bridge domain %d is not mapped to any vni!",
3210 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3211 gid_address_t * src, gid_address_t * dst)
3216 memset (src, 0, sizeof (*src));
3217 memset (dst, 0, sizeof (*dst));
3218 type = vnet_buffer (b)->lisp.overlay_afi;
3220 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3223 u8 version, preflen;
3225 gid_address_type (src) = GID_ADDR_IP_PREFIX;
3226 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
3228 ip = vlib_buffer_get_current (b);
3229 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
3231 version = gid_address_ip_version (src);
3232 preflen = ip_address_max_len (version);
3233 gid_address_ippref_len (src) = preflen;
3234 gid_address_ippref_len (dst) = preflen;
3236 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3237 gid_address_vni (dst) = vni;
3238 gid_address_vni (src) = vni;
3240 else if (LISP_AFI_MAC == type)
3242 ethernet_header_t *eh;
3244 eh = vlib_buffer_get_current (b);
3246 gid_address_type (src) = GID_ADDR_MAC;
3247 gid_address_type (dst) = GID_ADDR_MAC;
3248 mac_copy (&gid_address_mac (src), eh->src_address);
3249 mac_copy (&gid_address_mac (dst), eh->dst_address);
3252 vni = lisp_get_vni_from_buffer_eth (lcm, b);
3254 gid_address_vni (dst) = vni;
3255 gid_address_vni (src) = vni;
3260 lisp_cp_lookup_inline (vlib_main_t * vm,
3261 vlib_node_runtime_t * node,
3262 vlib_frame_t * from_frame, int overlay)
3264 u32 *from, *to_next_drop, di, si;
3265 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3266 u32 pkts_mapped = 0;
3267 uword n_left_from, n_left_to_next_drop;
3269 from = vlib_frame_vector_args (from_frame);
3270 n_left_from = from_frame->n_vectors;
3272 while (n_left_from > 0)
3274 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3275 to_next_drop, n_left_to_next_drop);
3277 while (n_left_from > 0 && n_left_to_next_drop > 0)
3281 gid_address_t src, dst;
3286 to_next_drop[0] = pi0;
3288 n_left_to_next_drop -= 1;
3290 b0 = vlib_get_buffer (vm, pi0);
3291 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
3292 vnet_buffer (b0)->lisp.overlay_afi = overlay;
3294 /* src/dst eid pair */
3295 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
3297 /* if we have remote mapping for destination already in map-chache
3298 add forwarding tunnel directly. If not send a map-request */
3299 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3303 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3304 /* send a map-request also in case of negative mapping entry
3305 with corresponding action */
3306 if (m->action == LISP_SEND_MAP_REQUEST)
3308 /* send map-request */
3309 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3310 0 /* is_resend */ );
3315 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3319 dp_add_fwd_entry (lcm, si, di);
3325 /* send map-request */
3326 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3327 0 /* is_resend */ );
3331 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3333 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3336 memset (tr, 0, sizeof (*tr));
3337 gid_address_copy (&tr->dst_eid, &dst);
3338 ip_address_copy (&tr->map_resolver_ip,
3339 &lcm->active_map_resolver);
3341 gid_address_free (&dst);
3342 gid_address_free (&src);
3345 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3346 n_left_to_next_drop);
3348 vlib_node_increment_counter (vm, node->node_index,
3349 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
3351 return from_frame->n_vectors;
3355 lisp_cp_lookup_ip4 (vlib_main_t * vm,
3356 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3358 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3362 lisp_cp_lookup_ip6 (vlib_main_t * vm,
3363 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3365 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
3369 lisp_cp_lookup_l2 (vlib_main_t * vm,
3370 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3372 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
3376 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
3377 .function = lisp_cp_lookup_ip4,
3378 .name = "lisp-cp-lookup-ip4",
3379 .vector_size = sizeof (u32),
3380 .format_trace = format_lisp_cp_lookup_trace,
3381 .type = VLIB_NODE_TYPE_INTERNAL,
3383 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3384 .error_strings = lisp_cp_lookup_error_strings,
3386 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3389 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3395 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
3396 .function = lisp_cp_lookup_ip6,
3397 .name = "lisp-cp-lookup-ip6",
3398 .vector_size = sizeof (u32),
3399 .format_trace = format_lisp_cp_lookup_trace,
3400 .type = VLIB_NODE_TYPE_INTERNAL,
3402 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3403 .error_strings = lisp_cp_lookup_error_strings,
3405 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3408 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3414 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
3415 .function = lisp_cp_lookup_l2,
3416 .name = "lisp-cp-lookup-l2",
3417 .vector_size = sizeof (u32),
3418 .format_trace = format_lisp_cp_lookup_trace,
3419 .type = VLIB_NODE_TYPE_INTERNAL,
3421 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3422 .error_strings = lisp_cp_lookup_error_strings,
3424 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3427 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3432 /* lisp_cp_input statistics */
3433 #define foreach_lisp_cp_input_error \
3435 _(MAP_REPLIES_RECEIVED, "map-replies received")
3437 static char *lisp_cp_input_error_strings[] = {
3438 #define _(sym,string) string,
3439 foreach_lisp_cp_input_error
3445 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
3446 foreach_lisp_cp_input_error
3448 LISP_CP_INPUT_N_ERROR,
3449 } lisp_cp_input_error_t;
3453 LISP_CP_INPUT_NEXT_DROP,
3454 LISP_CP_INPUT_N_NEXT,
3455 } lisp_cp_input_next_t;
3459 gid_address_t dst_eid;
3460 ip4_address_t map_resolver_ip;
3461 } lisp_cp_input_trace_t;
3464 format_lisp_cp_input_trace (u8 * s, va_list * args)
3466 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3467 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3468 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
3469 va_arg (*args, lisp_cp_input_trace_t *);
3471 s = format (s, "LISP-CP-INPUT: TODO");
3476 process_map_reply (void *arg)
3478 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3479 vlib_buffer_t *b = arg;
3480 u32 len = 0, i, ttl, dst_map_index = 0;
3482 pending_map_request_t *pmr;
3484 map_reply_hdr_t *mrep_hdr;
3488 u8 authoritative, action;
3489 locator_t *locators = 0, *loc;
3491 mrep_hdr = vlib_buffer_get_current (b);
3493 lisp_pending_map_request_lock (lcm);
3495 /* Check pending requests table and nonce */
3496 nonce = MREP_NONCE (mrep_hdr);
3497 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, nonce);
3500 clib_warning ("No pending map-request entry with nonce %lu!", nonce);
3503 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
3505 vlib_buffer_pull (b, sizeof (*mrep_hdr));
3507 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3509 h = vlib_buffer_get_current (b);
3510 ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3511 action = MAP_REC_ACTION (h);
3512 authoritative = MAP_REC_AUTH (h);
3514 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3517 clib_warning ("Failed to parse mapping record!");
3518 vec_foreach (loc, locators)
3522 vec_free (locators);
3526 /* insert/update mappings cache */
3527 vnet_lisp_add_del_mapping (&deid, locators, action, authoritative, ttl,
3528 1, 0 /* is_static */ , &dst_map_index);
3530 /* try to program forwarding only if mapping saved or updated */
3531 if ((u32) ~ 0 != dst_map_index)
3532 lisp_add_del_adjacency (lcm, &pmr->src, &deid, 1);
3534 vec_free (locators);
3537 /* remove pending map request entry */
3540 clib_fifo_foreach (noncep, pmr->nonces, ({
3541 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
3545 clib_fifo_free (pmr->nonces);
3546 pool_put (lcm->pending_map_requests_pool, pmr);
3549 lisp_pending_map_request_unlock (lcm);
3554 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm,
3557 map_request_hdr_t *mreq_hdr;
3558 gid_address_t src, dst;
3561 gid_address_t *itr_rlocs = 0, *rloc;
3563 mreq_hdr = vlib_buffer_get_current (b);
3564 vlib_buffer_pull (b, sizeof (*mreq_hdr));
3566 // nonce = MREQ_NONCE(mreq_hdr);
3568 if (!MREQ_SMR (mreq_hdr))
3570 clib_warning ("Only SMR Map-Requests supported for now!");
3575 len = lisp_msg_parse_addr (b, &src);
3579 /* for now we don't do anything with the itr's rlocs */
3581 lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3582 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
3586 /* TODO: RLOCs are currently unused, so free them for now */
3587 vec_foreach (rloc, itr_rlocs)
3589 gid_address_free (rloc);
3592 /* parse eid records and send SMR-invoked map-requests */
3593 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
3595 memset (&dst, 0, sizeof (dst));
3596 len = lisp_msg_parse_eid_rec (b, &dst);
3599 clib_warning ("Can't parse map-request EID-record");
3602 /* send SMR-invoked map-requests */
3603 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3608 queue_map_reply (vlib_buffer_t * b)
3610 vlib_buffer_t *a = clib_mem_alloc (sizeof (a[0]) + b->current_length);
3612 clib_memcpy (a->data, b->data + b->current_data, b->current_length);
3613 a->current_length = b->current_length;
3614 a->current_data = 0;
3616 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (a[0])
3617 + a->current_length);
3622 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
3623 vlib_frame_t * from_frame)
3625 u32 n_left_from, *from, *to_next_drop;
3626 lisp_msg_type_e type;
3627 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3629 from = vlib_frame_vector_args (from_frame);
3630 n_left_from = from_frame->n_vectors;
3633 while (n_left_from > 0)
3635 u32 n_left_to_next_drop;
3637 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3638 to_next_drop, n_left_to_next_drop);
3639 while (n_left_from > 0 && n_left_to_next_drop > 0)
3647 to_next_drop[0] = bi0;
3649 n_left_to_next_drop -= 1;
3651 b0 = vlib_get_buffer (vm, bi0);
3653 type = lisp_msg_type (vlib_buffer_get_current (b0));
3656 case LISP_MAP_REPLY:
3657 queue_map_reply (b0);
3659 case LISP_MAP_REQUEST:
3660 process_map_request (vm, lcm, b0);
3663 clib_warning ("Unsupported LISP message type %d", type);
3667 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
3669 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3675 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3676 n_left_to_next_drop);
3678 return from_frame->n_vectors;
3682 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3683 .function = lisp_cp_input,
3684 .name = "lisp-cp-input",
3685 .vector_size = sizeof (u32),
3686 .format_trace = format_lisp_cp_input_trace,
3687 .type = VLIB_NODE_TYPE_INTERNAL,
3689 .n_errors = LISP_CP_INPUT_N_ERROR,
3690 .error_strings = lisp_cp_input_error_strings,
3692 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3695 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3701 lisp_cp_init (vlib_main_t * vm)
3703 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3704 clib_error_t *error = 0;
3706 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3709 lcm->im4 = &ip4_main;
3710 lcm->im6 = &ip6_main;
3711 lcm->vlib_main = vm;
3712 lcm->vnet_main = vnet_get_main ();
3713 lcm->mreq_itr_rlocs = ~0;
3715 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
3717 lcm->pending_map_request_lock =
3718 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
3720 lcm->pending_map_request_lock[0] = 0;
3721 gid_dictionary_init (&lcm->mapping_index_by_gid);
3722 lcm->do_map_resolver_election = 1;
3723 lcm->map_request_mode = MR_MODE_DST_ONLY;
3725 /* default vrf mapped to vni 0 */
3726 hash_set (lcm->table_id_by_vni, 0, 0);
3727 hash_set (lcm->vni_by_table_id, 0, 0);
3729 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
3730 lisp_cp_input_node.index, 1 /* is_ip4 */ );
3731 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
3732 lisp_cp_input_node.index, 0 /* is_ip4 */ );
3738 send_map_request_thread_fn (void *arg)
3740 map_request_args_t *a = arg;
3741 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3744 /* if resending, we already have the lock */
3745 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3748 /* get lock before sending map-request */
3749 lisp_pending_map_request_lock (lcm);
3750 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3751 lisp_pending_map_request_unlock (lcm);
3759 queue_map_request (gid_address_t * seid, gid_address_t * deid,
3760 u8 smr_invoked, u8 is_resend)
3762 map_request_args_t a;
3764 a.is_resend = is_resend;
3765 gid_address_copy (&a.seid, seid);
3766 gid_address_copy (&a.deid, deid);
3767 a.smr_invoked = smr_invoked;
3769 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
3770 (u8 *) & a, sizeof (a));
3775 * Take an action with a pending map request depending on expiration time
3776 * and re-try counters.
3779 update_pending_request (pending_map_request_t * r, f64 dt)
3781 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3784 if (r->time_to_expire - dt < 0)
3785 /* it's time to decide what to do with this pending request */
3787 if (r->retries_num >= NUMBER_OF_RETRIES)
3788 /* too many retries -> assume current map resolver is not available */
3790 mr = get_map_resolver (&lcm->active_map_resolver);
3793 clib_warning ("Map resolver %U not found - probably deleted "
3794 "by the user recently.", format_ip_address,
3795 &lcm->active_map_resolver);
3799 clib_warning ("map resolver %U is unreachable, ignoring",
3800 format_ip_address, &lcm->active_map_resolver);
3802 /* mark current map resolver unavailable so it won't be
3803 * selected next time */
3805 mr->last_update = vlib_time_now (lcm->vlib_main);
3808 reset_pending_mr_counters (r);
3809 elect_map_resolver (lcm);
3811 /* try to find a next eligible map resolver and re-send */
3812 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3818 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3821 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3825 r->time_to_expire -= dt;
3829 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
3832 pending_map_request_t *pmr;
3833 u32 *to_be_removed = 0, *pmr_index;
3835 ASSERT (*lcm->pending_map_request_lock);
3838 pool_foreach (pmr, lcm->pending_map_requests_pool,
3840 if (pmr->to_be_removed)
3842 clib_fifo_foreach (nonce, pmr->nonces, ({
3843 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
3846 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
3851 vec_foreach (pmr_index, to_be_removed)
3852 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
3854 vec_free (to_be_removed);
3858 send_map_resolver_service (vlib_main_t * vm,
3859 vlib_node_runtime_t * rt, vlib_frame_t * f)
3862 pending_map_request_t *pmr;
3863 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3867 vlib_process_wait_for_event_or_clock (vm, period);
3869 /* currently no signals are expected - just wait for clock */
3870 (void) vlib_process_get_events (vm, 0);
3872 lisp_pending_map_request_lock (lcm);
3875 pool_foreach (pmr, lcm->pending_map_requests_pool,
3877 if (!pmr->to_be_removed)
3878 update_pending_request (pmr, period);
3882 remove_dead_pending_map_requests (lcm);
3884 lisp_pending_map_request_unlock (lcm);
3892 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
3893 .function = send_map_resolver_service,
3894 .type = VLIB_NODE_TYPE_PROCESS,
3895 .name = "lisp-retry-service",
3896 .process_log2_n_stack_bytes = 16,
3900 VLIB_INIT_FUNCTION (lisp_cp_init);
3903 * fd.io coding-style-patch-verification: ON
3906 * eval: (c-set-style "gnu")