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 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
236 /* delete entry in fwd table */
237 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
238 vec_free (fe->locator_pairs);
239 pool_put (lcm->fwd_entry_pool, fe);
243 * Finds first remote locator with best (lowest) priority that has a local
244 * peer locator with an underlying route to it.
248 get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
249 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
251 u32 i, limitp = 0, li, found = 0, esi;
252 locator_set_t *rmt_ls, *lcl_ls;
253 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
254 locator_t *lp, *rmt = 0;
259 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
261 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
263 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
270 /* find unvisited remote locator with best priority */
271 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
273 if (0 != hash_get (checked, i))
276 li = vec_elt (rmt_ls->locator_indices, i);
277 lp = pool_elt_at_index (lcm->locator_pool, li);
279 /* we don't support non-IP locators for now */
280 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
283 if ((found && lp->priority == limitp)
284 || (!found && lp->priority >= limitp))
288 /* don't search for locators with lower priority and don't
289 * check this locator again*/
290 limitp = lp->priority;
291 hash_set (checked, i, 1);
295 /* check if a local locator with a route to remote locator exists */
298 /* find egress sw_if_index for rmt locator */
300 ip_fib_get_egress_iface_for_dst (lcm,
301 &gid_address_ip (&rmt->address));
302 if ((u32) ~ 0 == esi)
305 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
307 li = vec_elt (lcl_ls->locator_indices, i);
308 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
310 /* found local locator with the needed sw_if_index */
311 if (sl->sw_if_index == esi)
313 /* and it has an address */
314 if (0 == ip_interface_get_first_ip_address (lcm,
316 gid_address_ip_version
321 memset (&pair, 0, sizeof (pair));
322 ip_address_copy (&pair.rmt_loc,
323 &gid_address_ip (&rmt->address));
324 ip_address_copy (&pair.lcl_loc, lcl_addr);
325 pair.weight = rmt->weight;
326 vec_add1 (locator_pairs[0], pair);
340 gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
343 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
347 switch (fid_addr_type (fid))
349 case FID_ADDR_IP_PREF:
350 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
351 gid_address_ippref (dst) = fid_addr_ippref (fid);
354 gid_address_type (dst) = GID_ADDR_MAC;
355 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
358 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
364 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
366 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
367 mapping_t *src_map, *dst_map;
369 uword *feip = 0, *dpid;
371 u8 type, is_src_dst = 0;
373 memset (a, 0, sizeof (*a));
375 /* remove entry if it already exists */
376 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
378 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
381 src_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
383 src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
384 dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
386 /* insert data plane forwarding entry */
389 if (GID_ADDR_SRC_DST == gid_address_type (&dst_map->eid))
391 gid_address_sd_to_flat (&a->rmt_eid, &dst_map->eid,
392 &gid_address_sd_dst (&dst_map->eid));
393 gid_address_sd_to_flat (&a->lcl_eid, &dst_map->eid,
394 &gid_address_sd_src (&dst_map->eid));
398 gid_address_copy (&a->rmt_eid, &dst_map->eid);
400 a->vni = gid_address_vni (&a->rmt_eid);
402 /* get vrf or bd_index associated to vni */
403 type = gid_address_type (&a->rmt_eid);
404 if (GID_ADDR_IP_PREFIX == type)
406 dpid = hash_get (lcm->table_id_by_vni, a->vni);
409 clib_warning ("vni %d not associated to a vrf!", a->vni);
412 a->table_id = dpid[0];
414 else if (GID_ADDR_MAC == type)
416 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
419 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
425 /* find best locator pair that 1) verifies LISP policy 2) are connected */
426 if (0 == get_locator_pairs (lcm, src_map, dst_map, &a->locator_pairs))
430 a->action = dst_map->action;
434 u8 ipver = ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
435 a->decap_next_index = (ipver == IP4) ?
436 LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
438 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
440 /* add tunnel to fwd entry table XXX check return value from DP insertion */
441 pool_get (lcm->fwd_entry_pool, fe);
442 fe->locator_pairs = a->locator_pairs;
443 gid_address_copy (&fe->reid, &a->rmt_eid);
444 gid_address_copy (&fe->leid, &src_map->eid);
445 fe->is_src_dst = is_src_dst;
446 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
447 fe - lcm->fwd_entry_pool);
451 * Returns vector of adjacencies.
453 * The caller must free the vector returned by this function.
455 * @param vni virtual network identifier
456 * @return vector of adjacencies
459 vnet_lisp_adjacencies_get_by_vni (u32 vni)
461 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
463 lisp_adjacency_t *adjs = 0, adj;
466 pool_foreach(fwd, lcm->fwd_entry_pool,
468 if (gid_address_vni (&fwd->reid) != vni)
471 gid_address_copy (&adj.reid, &fwd->reid);
472 gid_address_copy (&adj.leid, &fwd->leid);
473 vec_add1 (adjs, adj);
480 static clib_error_t *
481 lisp_show_adjacencies_command_fn (vlib_main_t * vm,
482 unformat_input_t * input,
483 vlib_cli_command_t * cmd)
485 lisp_adjacency_t *adjs, *adj;
486 vlib_cli_output (vm, "%s %40s\n", "leid", "reid");
487 unformat_input_t _line_input, *line_input = &_line_input;
490 /* Get a line of input. */
491 if (!unformat_user (input, unformat_line_input, line_input))
494 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
496 if (unformat (line_input, "vni %d", &vni))
500 vlib_cli_output (vm, "parse error: '%U'",
501 format_unformat_error, line_input);
508 vlib_cli_output (vm, "error: no vni specified!");
512 adjs = vnet_lisp_adjacencies_get_by_vni (vni);
514 vec_foreach (adj, adjs)
516 vlib_cli_output (vm, "%U %40U\n", format_gid_address, &adj->leid,
517 format_gid_address, &adj->reid);
525 VLIB_CLI_COMMAND (lisp_show_adjacencies_command) = {
526 .path = "show lisp adjacencies",
527 .short_help = "show lisp adjacencies",
528 .function = lisp_show_adjacencies_command_fn,
533 * Add/remove mapping to/from map-cache. Overwriting not allowed.
536 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
537 u32 * map_index_result)
539 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
540 u32 mi, *map_indexp, map_index, i;
541 mapping_t *m, *old_map;
544 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
545 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
548 /* TODO check if overwriting and take appropriate actions */
549 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
551 clib_warning ("eid %U found in the eid-table", format_gid_address,
553 return VNET_API_ERROR_VALUE_EXIST;
556 pool_get (lcm->mapping_pool, m);
557 gid_address_copy (&m->eid, &a->eid);
558 m->locator_set_index = a->locator_set_index;
560 m->action = a->action;
562 m->is_static = a->is_static;
564 map_index = m - lcm->mapping_pool;
565 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
568 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
570 clib_warning ("Locator set with index %d doesn't exist",
571 a->locator_set_index);
572 return VNET_API_ERROR_INVALID_VALUE;
575 /* add eid to list of eids supported by locator-set */
576 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
577 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
578 a->locator_set_index);
579 vec_add1 (eid_indexes[0], map_index);
584 vec_add1 (lcm->local_mappings_indexes, map_index);
586 map_index_result[0] = map_index;
590 if (mi == GID_LOOKUP_MISS)
592 clib_warning ("eid %U not found in the eid-table",
593 format_gid_address, &a->eid);
594 return VNET_API_ERROR_INVALID_VALUE;
597 /* clear locator-set to eids binding */
598 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
599 a->locator_set_index);
600 for (i = 0; i < vec_len (eid_indexes[0]); i++)
602 map_indexp = vec_elt_at_index (eid_indexes[0], i);
603 if (map_indexp[0] == mi)
606 vec_del1 (eid_indexes[0], i);
608 /* remove local mark if needed */
609 m = pool_elt_at_index (lcm->mapping_pool, mi);
613 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
615 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
616 if (lm_indexp[0] == mi)
619 vec_del1 (lcm->local_mappings_indexes, k);
622 /* remove mapping from dictionary */
623 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
624 gid_address_free (&m->eid);
625 pool_put_index (lcm->mapping_pool, mi);
632 * Add/update/delete mapping to/in/from map-cache.
635 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
636 u32 * map_index_result)
642 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
644 if (vnet_lisp_enable_disable_status () == 0)
646 clib_warning ("LISP is disabled!");
647 return VNET_API_ERROR_LISP_DISABLED;
650 vni = gid_address_vni (&a->eid);
651 type = gid_address_type (&a->eid);
652 if (GID_ADDR_IP_PREFIX == type)
653 dp_table = hash_get (lcm->table_id_by_vni, vni);
654 else if (GID_ADDR_MAC == type)
655 dp_table = hash_get (lcm->bd_id_by_vni, vni);
659 clib_warning ("vni %d not associated to a %s!", vni,
660 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
661 return VNET_API_ERROR_INVALID_VALUE;
664 /* store/remove mapping from map-cache */
665 return vnet_lisp_map_cache_add_del (a, map_index_result);
668 static clib_error_t *
669 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
670 vlib_cli_command_t * cmd)
672 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
673 unformat_input_t _line_input, *line_input = &_line_input;
676 gid_address_t *eids = 0;
677 clib_error_t *error = 0;
678 u8 *locator_set_name = 0;
679 u32 locator_set_index = 0, map_index = 0;
681 vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
685 memset (&eid, 0, sizeof (eid));
686 memset (a, 0, sizeof (*a));
688 /* Get a line of input. */
689 if (!unformat_user (input, unformat_line_input, line_input))
692 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
694 if (unformat (line_input, "add"))
696 else if (unformat (line_input, "del"))
698 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
700 else if (unformat (line_input, "vni %d", &vni))
701 gid_address_vni (&eid) = vni;
702 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
704 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
707 error = clib_error_return (0, "locator-set %s doesn't exist",
711 locator_set_index = p[0];
715 error = unformat_parse_error (line_input);
719 /* XXX treat batch configuration */
721 if (GID_ADDR_SRC_DST == gid_address_type (&eid))
724 clib_error_return (0, "src/dst is not supported for local EIDs!");
728 gid_address_copy (&a->eid, &eid);
730 a->locator_set_index = locator_set_index;
733 rv = vnet_lisp_add_del_local_mapping (a, &map_index);
736 error = clib_error_return (0, "failed to %s local mapping!",
737 is_add ? "add" : "delete");
741 if (locator_set_name)
742 vec_free (locator_set_name);
743 gid_address_free (&a->eid);
748 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
749 .path = "lisp eid-table",
750 .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
751 "locator-set <locator-set>",
752 .function = lisp_add_del_local_eid_command_fn,
757 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
759 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
760 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
762 if (vnet_lisp_enable_disable_status () == 0)
764 clib_warning ("LISP is disabled!");
768 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
769 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
771 if (!is_l2 && (vni == 0 || dp_id == 0))
773 clib_warning ("can't add/del default vni-vrf mapping!");
777 dp_idp = hash_get (dp_table_by_vni[0], vni);
778 vnip = hash_get (vni_by_dp_table[0], dp_id);
784 clib_warning ("vni %d or vrf %d already used in vrf/vni "
785 "mapping!", vni, dp_id);
788 hash_set (dp_table_by_vni[0], vni, dp_id);
789 hash_set (vni_by_dp_table[0], dp_id, vni);
791 /* create dp iface */
792 dp_add_del_iface (lcm, vni, is_l2, 1);
796 if (!dp_idp || !vnip)
798 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
799 "mapping!", vni, dp_id);
802 hash_unset (dp_table_by_vni[0], vni);
803 hash_unset (vni_by_dp_table[0], dp_id);
805 /* remove dp iface */
806 dp_add_del_iface (lcm, vni, is_l2, 0);
812 static clib_error_t *
813 lisp_eid_table_map_command_fn (vlib_main_t * vm,
814 unformat_input_t * input,
815 vlib_cli_command_t * cmd)
817 u8 is_add = 1, is_l2 = 0;
818 u32 vni = 0, dp_id = 0;
819 unformat_input_t _line_input, *line_input = &_line_input;
821 /* Get a line of input. */
822 if (!unformat_user (input, unformat_line_input, line_input))
825 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
827 if (unformat (line_input, "del"))
829 else if (unformat (line_input, "vni %d", &vni))
831 else if (unformat (line_input, "vrf %d", &dp_id))
833 else if (unformat (line_input, "bd %d", &dp_id))
837 return unformat_parse_error (line_input);
840 vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
845 VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
846 .path = "lisp eid-table map",
847 .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
848 .function = lisp_eid_table_map_command_fn,
852 /* return 0 if the two locator sets are identical 1 otherwise */
854 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
855 locator_t * new_locators)
858 locator_t *old_loc, *new_loc;
860 if (vec_len (old_ls_indexes) != vec_len (new_locators))
863 for (i = 0; i < vec_len (new_locators); i++)
865 old_li = vec_elt (old_ls_indexes, i);
866 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
868 new_loc = vec_elt_at_index (new_locators, i);
870 if (locator_cmp (old_loc, new_loc))
877 * Adds/removes/updates mapping. Does not program forwarding.
879 * @param eid end-host identifier
880 * @param rlocs vector of remote locators
881 * @param action action for negative map-reply
882 * @param is_add add mapping if non-zero, delete otherwise
883 * @param res_map_index the map-index that was created/updated/removed. It is
884 * set to ~0 if no action is taken.
885 * @param is_static used for distinguishing between statically learned
886 remote mappings and mappings obtained from MR
887 * @return return code
890 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
891 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
894 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
895 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
896 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
897 u32 mi, ls_index = 0, dst_map_index;
900 if (vnet_lisp_enable_disable_status () == 0)
902 clib_warning ("LISP is disabled!");
903 return VNET_API_ERROR_LISP_DISABLED;
907 res_map_index[0] = ~0;
909 memset (m_args, 0, sizeof (m_args[0]));
910 memset (ls_args, 0, sizeof (ls_args[0]));
912 ls_args->locators = rlocs;
914 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
915 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
919 /* overwrite: if mapping already exists, decide if locators should be
920 * updated and be done */
921 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
923 if (!is_static && (old_map->is_static || old_map->local))
925 /* do not overwrite local or static remote mappings */
926 clib_warning ("mapping %U rejected due to collision with local "
927 "or static remote mapping!", format_gid_address,
932 locator_set_t *old_ls;
934 /* update mapping attributes */
935 old_map->action = action;
936 old_map->authoritative = authoritative;
939 old_ls = pool_elt_at_index (lcm->locator_set_pool,
940 old_map->locator_set_index);
941 if (compare_locators (lcm, old_ls->locator_indices,
944 /* set locator-set index to overwrite */
946 ls_args->index = old_map->locator_set_index;
947 vnet_lisp_add_del_locator_set (ls_args, 0);
949 res_map_index[0] = mi;
958 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
961 gid_address_copy (&m_args->eid, eid);
963 m_args->action = action;
964 m_args->locator_set_index = ls_index;
965 m_args->is_static = is_static;
966 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
969 res_map_index[0] = dst_map_index;
974 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
976 clib_warning ("cannot delete mapping for eid %U",
977 format_gid_address, eid);
982 gid_address_copy (&m_args->eid, eid);
983 m_args->locator_set_index = old_map->locator_set_index;
985 /* delete mapping associated from map-cache */
986 vnet_lisp_map_cache_add_del (m_args, 0);
989 ls_args->index = old_map->locator_set_index;
990 /* delete locator set */
991 vnet_lisp_add_del_locator_set (ls_args, 0);
993 /* return old mapping index */
995 res_map_index[0] = mi;
1003 vnet_lisp_clear_all_remote_adjacencies (void)
1006 u32 mi, *map_indices = 0, *map_indexp;
1007 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1008 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1009 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1012 pool_foreach_index (mi, lcm->mapping_pool,
1014 vec_add1 (map_indices, mi);
1018 vec_foreach (map_indexp, map_indices)
1020 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1023 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
1025 dm_args->is_add = 0;
1026 gid_address_copy (&dm_args->eid, &map->eid);
1027 dm_args->locator_set_index = map->locator_set_index;
1029 /* delete mapping associated to fwd entry */
1030 vnet_lisp_map_cache_add_del (dm_args, 0);
1034 ls->index = map->locator_set_index;
1035 /* delete locator set */
1036 rv = vnet_lisp_add_del_locator_set (ls, 0);
1044 vec_free (map_indices);
1049 * Adds adjacency or removes forwarding entry associated to remote mapping.
1050 * Note that adjacencies are not stored, they only result in forwarding entries
1054 lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
1055 gid_address_t * remote_eid, u8 is_add)
1057 u32 local_mi, remote_mi = ~0;
1059 if (vnet_lisp_enable_disable_status () == 0)
1061 clib_warning ("LISP is disabled!");
1062 return VNET_API_ERROR_LISP_DISABLED;
1065 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1066 remote_eid, local_eid);
1067 if (GID_LOOKUP_MISS == remote_mi)
1069 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1070 format_gid_address, remote_eid);
1077 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1080 /* check if source eid has an associated mapping. If pitr mode is on,
1081 * just use the pitr's mapping */
1082 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
1083 gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
1086 if (GID_LOOKUP_MISS == local_mi)
1088 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1089 format_gid_address, local_eid);
1094 /* update forwarding */
1095 dp_add_fwd_entry (lcm, local_mi, remote_mi);
1098 dp_del_fwd_entry (lcm, 0, remote_mi);
1104 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1106 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1107 return lisp_add_del_adjacency (lcm, &a->leid, &a->reid, a->is_add);
1111 * Handler for add/del remote mapping CLI.
1113 * @param vm vlib context
1114 * @param input input from user
1116 * @return pointer to clib error structure
1118 static clib_error_t *
1119 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
1120 unformat_input_t * input,
1121 vlib_cli_command_t * cmd)
1123 clib_error_t *error = 0;
1124 unformat_input_t _line_input, *line_input = &_line_input;
1125 u8 is_add = 1, del_all = 0;
1126 locator_t rloc, *rlocs = 0, *curr_rloc = 0;
1129 u32 vni, action = ~0, p, w;
1132 /* Get a line of input. */
1133 if (!unformat_user (input, unformat_line_input, line_input))
1136 memset (&eid, 0, sizeof (eid));
1137 memset (&rloc, 0, sizeof (rloc));
1139 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1141 if (unformat (line_input, "del-all"))
1143 else if (unformat (line_input, "del"))
1145 else if (unformat (line_input, "add"))
1147 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1149 else if (unformat (line_input, "vni %u", &vni))
1151 gid_address_vni (&eid) = vni;
1153 else if (unformat (line_input, "p %d w %d", &p, &w))
1158 ("No RLOC configured for setting priority/weight!");
1161 curr_rloc->priority = p;
1162 curr_rloc->weight = w;
1164 else if (unformat (line_input, "rloc %U", unformat_ip_address,
1165 &gid_address_ip (&rloc.address)))
1167 /* since rloc is stored in ip prefix we need to set prefix length */
1168 ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
1170 u8 version = gid_address_ip_version (&rloc.address);
1171 ip_prefix_len (pref) = ip_address_max_len (version);
1173 vec_add1 (rlocs, rloc);
1174 curr_rloc = &rlocs[vec_len (rlocs) - 1];
1176 else if (unformat (line_input, "action %U",
1177 unformat_negative_mapping_action, &action))
1181 clib_warning ("parse error");
1188 clib_warning ("missing eid!");
1194 if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1196 clib_warning ("no action set for negative map-reply!");
1202 vnet_lisp_clear_all_remote_adjacencies ();
1206 /* TODO build src/dst with seid */
1208 /* if it's a delete, clean forwarding */
1211 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1212 rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1219 /* add as static remote mapping, i.e., not authoritative and infinite
1221 rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
1222 1 /* is_static */ , 0);
1225 clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
1229 unformat_free (line_input);
1233 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) =
1235 .path = "lisp remote-mapping",.short_help =
1236 "lisp remote-mapping add|del [del-all] vni <vni> "
1237 "eid <est-eid> [action <no-action|natively-forward|"
1238 "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
1239 "[rloc <dst-locator> ... ]",.function =
1240 lisp_add_del_remote_mapping_command_fn,};
1243 * Handler for add/del adjacency CLI.
1245 static clib_error_t *
1246 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
1247 vlib_cli_command_t * cmd)
1249 clib_error_t *error = 0;
1250 unformat_input_t _line_input, *line_input = &_line_input;
1251 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1253 ip_prefix_t *reid_ippref, *leid_ippref;
1254 gid_address_t leid, reid;
1255 u8 *dmac = gid_address_mac (&reid);
1256 u8 *smac = gid_address_mac (&leid);
1257 u8 reid_set = 0, leid_set = 0;
1261 /* Get a line of input. */
1262 if (!unformat_user (input, unformat_line_input, line_input))
1265 memset (&reid, 0, sizeof (reid));
1266 memset (&leid, 0, sizeof (leid));
1268 leid_ippref = &gid_address_ippref (&leid);
1269 reid_ippref = &gid_address_ippref (&reid);
1271 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1273 if (unformat (line_input, "del"))
1275 else if (unformat (line_input, "add"))
1277 else if (unformat (line_input, "reid %U",
1278 unformat_ip_prefix, reid_ippref))
1280 gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
1283 else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
1285 gid_address_type (&reid) = GID_ADDR_MAC;
1288 else if (unformat (line_input, "vni %u", &vni))
1290 gid_address_vni (&leid) = vni;
1291 gid_address_vni (&reid) = vni;
1293 else if (unformat (line_input, "leid %U",
1294 unformat_ip_prefix, leid_ippref))
1296 gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
1299 else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
1301 gid_address_type (&leid) = GID_ADDR_MAC;
1306 clib_warning ("parse error");
1311 if (!reid_set || !leid_set)
1313 clib_warning ("missing remote or local eid!");
1317 if ((gid_address_type (&leid) != gid_address_type (&reid))
1318 || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
1319 && ip_prefix_version (reid_ippref)
1320 != ip_prefix_version (leid_ippref)))
1322 clib_warning ("remote and local EIDs are of different types!");
1326 memset (a, 0, sizeof (a[0]));
1327 gid_address_copy (&a->leid, &leid);
1328 gid_address_copy (&a->reid, &reid);
1331 rv = vnet_lisp_add_del_adjacency (a);
1334 clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
1337 unformat_free (line_input);
1342 VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1343 .path = "lisp adjacency",
1344 .short_help = "lisp adjacency add|del vni <vni> reid <remote-eid> "
1346 .function = lisp_add_del_adjacency_command_fn,
1351 vnet_lisp_set_map_request_mode (u8 mode)
1353 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1355 if (vnet_lisp_enable_disable_status () == 0)
1357 clib_warning ("LISP is disabled!");
1358 return VNET_API_ERROR_LISP_DISABLED;
1361 if (mode >= _MR_MODE_MAX)
1363 clib_warning ("Invalid LISP map request mode %d!", mode);
1364 return VNET_API_ERROR_INVALID_ARGUMENT;
1367 lcm->map_request_mode = mode;
1371 static clib_error_t *
1372 lisp_map_request_mode_command_fn (vlib_main_t * vm,
1373 unformat_input_t * input,
1374 vlib_cli_command_t * cmd)
1376 unformat_input_t _i, *i = &_i;
1377 map_request_mode_t mr_mode = _MR_MODE_MAX;
1379 /* Get a line of input. */
1380 if (!unformat_user (input, unformat_line_input, i))
1383 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1385 if (unformat (i, "dst-only"))
1386 mr_mode = MR_MODE_DST_ONLY;
1387 else if (unformat (i, "src-dst"))
1388 mr_mode = MR_MODE_SRC_DST;
1391 clib_warning ("parse error '%U'", format_unformat_error, i);
1396 if (_MR_MODE_MAX == mr_mode)
1398 clib_warning ("No LISP map request mode entered!");
1402 vnet_lisp_set_map_request_mode (mr_mode);
1408 VLIB_CLI_COMMAND (lisp_map_request_mode_command) = {
1409 .path = "lisp map-request mode",
1410 .short_help = "lisp map-request mode dst-only|src-dst",
1411 .function = lisp_map_request_mode_command_fn,
1416 format_lisp_map_request_mode (u8 * s, va_list * args)
1418 u32 mode = va_arg (*args, u32);
1423 return format (0, "dst-only");
1425 return format (0, "src-dst");
1430 static clib_error_t *
1431 lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
1432 unformat_input_t * input,
1433 vlib_cli_command_t * cmd)
1435 vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
1436 vnet_lisp_get_map_request_mode ());
1441 VLIB_CLI_COMMAND (lisp_show_map_request_mode_command) = {
1442 .path = "show lisp map-request mode",
1443 .short_help = "show lisp map-request mode",
1444 .function = lisp_show_map_request_mode_command_fn,
1448 static clib_error_t *
1449 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
1450 unformat_input_t * input,
1451 vlib_cli_command_t * cmd)
1454 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1456 vec_foreach (mr, lcm->map_resolvers)
1458 vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
1464 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1465 .path = "show lisp map-resolvers",
1466 .short_help = "show lisp map-resolvers",
1467 .function = lisp_show_map_resolvers_command_fn,
1472 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1474 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1475 u32 locator_set_index = ~0;
1479 if (vnet_lisp_enable_disable_status () == 0)
1481 clib_warning ("LISP is disabled!");
1482 return VNET_API_ERROR_LISP_DISABLED;
1485 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1488 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1491 locator_set_index = p[0];
1495 pool_get (lcm->mapping_pool, m);
1496 m->locator_set_index = locator_set_index;
1498 lcm->pitr_map_index = m - lcm->mapping_pool;
1500 /* enable pitr mode */
1505 /* remove pitr mapping */
1506 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1508 /* disable pitr mode */
1514 static clib_error_t *
1515 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
1516 unformat_input_t * input,
1517 vlib_cli_command_t * cmd)
1519 u8 locator_name_set = 0;
1520 u8 *locator_set_name = 0;
1522 unformat_input_t _line_input, *line_input = &_line_input;
1523 clib_error_t *error = 0;
1526 /* Get a line of input. */
1527 if (!unformat_user (input, unformat_line_input, line_input))
1530 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1532 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
1533 locator_name_set = 1;
1534 else if (unformat (line_input, "disable"))
1537 return clib_error_return (0, "parse error");
1540 if (!locator_name_set)
1542 clib_warning ("No locator set specified!");
1545 rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1548 error = clib_error_return (0, "failed to %s pitr!",
1549 is_add ? "add" : "delete");
1553 if (locator_set_name)
1554 vec_free (locator_set_name);
1559 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1560 .path = "lisp pitr",
1561 .short_help = "lisp pitr [disable] ls <locator-set-name>",
1562 .function = lisp_pitr_set_locator_set_command_fn,
1566 static clib_error_t *
1567 lisp_show_pitr_command_fn (vlib_main_t * vm,
1568 unformat_input_t * input, vlib_cli_command_t * cmd)
1570 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1575 vlib_cli_output (vm, "%=20s%=16s",
1576 "pitr", lcm->lisp_pitr ? "locator-set" : "");
1578 if (!lcm->lisp_pitr)
1580 vlib_cli_output (vm, "%=20s", "disable");
1584 if (~0 == lcm->pitr_map_index)
1586 tmp_str = format (0, "N/A");
1590 m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1591 if (~0 != m->locator_set_index)
1594 pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1595 tmp_str = format (0, "%s", ls->name);
1599 tmp_str = format (0, "N/A");
1602 vec_add1 (tmp_str, 0);
1604 vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1612 VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1613 .path = "show lisp pitr",
1614 .short_help = "Show pitr",
1615 .function = lisp_show_pitr_command_fn,
1620 format_eid_entry (u8 * s, va_list * args)
1622 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
1623 lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
1624 mapping_t *mapit = va_arg (*args, mapping_t *);
1625 locator_set_t *ls = va_arg (*args, locator_set_t *);
1626 gid_address_t *gid = &mapit->eid;
1627 u32 ttl = mapit->ttl;
1628 u8 aut = mapit->authoritative;
1633 u8 *type = ls->local ? format (0, "local(%s)", ls->name)
1634 : format (0, "remote");
1636 if (vec_len (ls->locator_indices) == 0)
1638 s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
1643 vec_foreach (loc_index, ls->locator_indices)
1645 locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1647 loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1650 loc = format (0, "%U", format_ip_address,
1651 &gid_address_ip (&l->address));
1655 s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
1656 gid, type, loc, ttl, aut);
1660 s = format (s, "%55s%v\n", "", loc);
1666 static clib_error_t *
1667 lisp_show_eid_table_command_fn (vlib_main_t * vm,
1668 unformat_input_t * input,
1669 vlib_cli_command_t * cmd)
1671 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1673 unformat_input_t _line_input, *line_input = &_line_input;
1679 memset (&eid, 0, sizeof (eid));
1681 /* Get a line of input. */
1682 if (!unformat_user (input, unformat_line_input, line_input))
1685 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1687 if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1689 else if (unformat (line_input, "local"))
1691 else if (unformat (line_input, "remote"))
1694 return clib_error_return (0, "parse error: '%U'",
1695 format_unformat_error, line_input);
1698 vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
1699 "EID", "type", "locators", "ttl", "autoritative");
1704 pool_foreach (mapit, lcm->mapping_pool,
1706 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
1707 mapit->locator_set_index);
1708 if (filter && !((1 == filter && ls->local) ||
1709 (2 == filter && !ls->local)))
1713 vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
1720 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
1721 if ((u32) ~ 0 == mi)
1724 mapit = pool_elt_at_index (lcm->mapping_pool, mi);
1725 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
1726 mapit->locator_set_index);
1728 if (filter && !((1 == filter && ls->local) ||
1729 (2 == filter && !ls->local)))
1734 vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
1742 VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
1743 .path = "show lisp eid-table",
1744 .short_help = "Shows EID table",
1745 .function = lisp_show_eid_table_command_fn,
1749 /* cleans locator to locator-set data and removes locators not part of
1750 * any locator-set */
1752 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1754 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
1755 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1756 for (i = 0; i < vec_len (ls->locator_indices); i++)
1758 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1759 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1761 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1763 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1764 if (ls_indexp[0] == lsi)
1768 /* delete index for removed locator-set */
1769 vec_del1 (ls_indexes[0], j);
1771 /* delete locator if it's part of no locator-set */
1772 if (vec_len (ls_indexes[0]) == 0)
1774 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1775 vec_add1 (to_be_deleted, i);
1781 for (i = 0; i < vec_len (to_be_deleted); i++)
1783 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1784 vec_del1 (ls->locator_indices, loc_indexp[0]);
1786 vec_free (to_be_deleted);
1790 static inline uword *
1791 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
1793 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1798 /* find locator-set */
1801 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
1812 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
1818 ASSERT (ls != NULL);
1819 ASSERT (loc != NULL);
1821 vec_foreach (locit, ls->locator_indices)
1823 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1824 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1825 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1827 clib_warning ("Duplicate locator");
1828 return VNET_API_ERROR_VALUE_EXIST;
1836 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1837 u32 ls_index, u32 loc_id)
1839 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1840 u32 **ls_indexes = NULL;
1842 ASSERT (ls != NULL);
1843 ASSERT (locit != NULL);
1845 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1846 pool_put_index (lcm->locator_pool, locit[0]);
1847 vec_del1 (ls->locator_indices, loc_id);
1848 vec_del1 (ls_indexes[0], ls_index);
1852 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1853 locator_set_t * ls, u32 * ls_result)
1855 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1856 locator_t *loc = NULL, *itloc = NULL;
1857 uword _p = (u32) ~ 0, *p = &_p;
1858 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
1864 if (vnet_lisp_enable_disable_status () == 0)
1866 clib_warning ("LISP is disabled!");
1867 return VNET_API_ERROR_LISP_DISABLED;
1870 p = get_locator_set_index (a, p);
1873 clib_warning ("locator-set %v doesn't exist", a->name);
1874 return VNET_API_ERROR_INVALID_ARGUMENT;
1879 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1882 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1884 return VNET_API_ERROR_INVALID_ARGUMENT;
1891 ls_result[0] = p[0];
1893 /* allocate locators */
1894 vec_foreach (itloc, a->locators)
1896 ret = is_locator_in_locator_set (lcm, ls, itloc);
1902 pool_get (lcm->locator_pool, loc);
1904 loc_index = loc - lcm->locator_pool;
1906 vec_add1 (ls->locator_indices, loc_index);
1908 vec_validate (lcm->locator_to_locator_sets, loc_index);
1909 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1911 vec_add1 (ls_indexes[0], p[0]);
1918 itloc = a->locators;
1920 vec_foreach (locit, ls->locator_indices)
1922 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1924 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1926 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1928 if (0 == loc->local &&
1929 !gid_address_cmp (&loc->address, &itloc->address))
1931 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1942 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
1945 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1947 uword _p = (u32) ~ 0, *p = &_p;
1952 if (vnet_lisp_enable_disable_status () == 0)
1954 clib_warning ("LISP is disabled!");
1955 return VNET_API_ERROR_LISP_DISABLED;
1960 p = get_locator_set_index (a, p);
1963 if (p && p[0] != (u32) ~ 0)
1965 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1968 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1973 /* clean locator to locator-set vectors and remove locators if
1974 * they're not part of another locator-set */
1975 clean_locator_to_locator_set (lcm, p[0]);
1977 /* remove locator indices from locator set */
1978 vec_free (ls->locator_indices);
1983 ls_result[0] = p[0];
1985 /* new locator-set */
1988 pool_get (lcm->locator_set_pool, ls);
1989 memset (ls, 0, sizeof (*ls));
1990 ls_index = ls - lcm->locator_set_pool;
1994 ls->name = vec_dup (a->name);
1996 if (!lcm->locator_set_index_by_name)
1997 lcm->locator_set_index_by_name = hash_create_vec (
2005 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
2008 /* mark as local locator-set */
2009 vec_add1 (lcm->local_locator_set_indexes, ls_index);
2011 ls->local = a->local;
2013 ls_result[0] = ls_index;
2016 ret = vnet_lisp_add_del_locator (a, ls, NULL);
2024 p = get_locator_set_index (a, p);
2027 clib_warning ("locator-set %v doesn't exists", a->name);
2031 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2034 clib_warning ("locator-set with index %d doesn't exists", p[0]);
2038 if (lcm->mreq_itr_rlocs == p[0])
2040 clib_warning ("Can't delete the locator-set used to constrain "
2041 "the itr-rlocs in map-requests!");
2045 if (vec_len (lcm->locator_set_to_eids) != 0)
2047 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
2048 if (vec_len (eid_indexes[0]) != 0)
2051 ("Can't delete a locator that supports a mapping!");
2056 /* clean locator to locator-sets data */
2057 clean_locator_to_locator_set (lcm, p[0]);
2063 vec_foreach_index (it, lcm->local_locator_set_indexes)
2065 lsi = vec_elt (lcm->local_locator_set_indexes, it);
2068 vec_del1 (lcm->local_locator_set_indexes, it);
2072 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2074 vec_free (ls->name);
2075 vec_free (ls->locator_indices);
2076 pool_put (lcm->locator_set_pool, ls);
2082 vnet_lisp_enable_disable (u8 is_enable)
2085 clib_error_t *error = 0;
2086 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2087 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
2089 a->is_en = is_enable;
2090 error = vnet_lisp_gpe_enable_disable (a);
2093 return clib_error_return (0, "failed to %s data-plane!",
2094 a->is_en ? "enable" : "disable");
2099 /* enable all l2 and l3 ifaces */
2102 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2103 dp_add_del_iface(lcm, vni, 0, 1);
2105 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2106 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
2112 /* clear interface table */
2113 hash_free (lcm->fwd_entry_by_mapping_index);
2114 pool_free (lcm->fwd_entry_pool);
2117 /* update global flag */
2118 lcm->is_enabled = is_enable;
2123 static clib_error_t *
2124 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
2125 vlib_cli_command_t * cmd)
2127 unformat_input_t _line_input, *line_input = &_line_input;
2131 /* Get a line of input. */
2132 if (!unformat_user (input, unformat_line_input, line_input))
2135 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2137 if (unformat (line_input, "enable"))
2142 else if (unformat (line_input, "disable"))
2146 return clib_error_return (0, "parse error: '%U'",
2147 format_unformat_error, line_input);
2152 return clib_error_return (0, "state not set");
2154 return vnet_lisp_enable_disable (is_enabled);
2158 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
2160 .short_help = "lisp [enable|disable]",
2161 .function = lisp_enable_disable_command_fn,
2166 vnet_lisp_enable_disable_status (void)
2168 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2169 return lcm->is_enabled;
2173 format_lisp_status (u8 * s, va_list * args)
2175 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2176 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
2179 static clib_error_t *
2180 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
2181 vlib_cli_command_t * cmd)
2184 msg = format (msg, "feature: %U\ngpe: %U\n",
2185 format_lisp_status, format_vnet_lisp_gpe_status);
2186 vlib_cli_output (vm, "%v", msg);
2192 VLIB_CLI_COMMAND (lisp_show_status_command) = {
2193 .path = "show lisp status",
2194 .short_help = "show lisp status",
2195 .function = lisp_show_status_command_fn,
2199 static clib_error_t *
2200 lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
2201 unformat_input_t * input,
2202 vlib_cli_command_t * cmd)
2205 unformat_input_t _line_input, *line_input = &_line_input;
2206 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2207 uword *vni_table = 0;
2210 /* Get a line of input. */
2211 if (!unformat_user (input, unformat_line_input, line_input))
2214 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2216 if (unformat (line_input, "l2"))
2218 vni_table = lcm->bd_id_by_vni;
2221 else if (unformat (line_input, "l3"))
2223 vni_table = lcm->table_id_by_vni;
2227 return clib_error_return (0, "parse error: '%U'",
2228 format_unformat_error, line_input);
2233 vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
2237 vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
2240 hash_foreach_pair (p, vni_table,
2242 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
2250 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
2251 .path = "show lisp eid-table map",
2252 .short_help = "show lisp eid-table l2|l3",
2253 .function = lisp_show_eid_table_map_command_fn,
2257 static clib_error_t *
2258 lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
2259 unformat_input_t * input,
2260 vlib_cli_command_t * cmd)
2262 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2263 vnet_main_t *vnm = lgm->vnet_main;
2264 unformat_input_t _line_input, *line_input = &_line_input;
2266 clib_error_t *error = 0;
2267 u8 *locator_set_name = 0;
2268 locator_t locator, *locators = 0;
2269 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2273 memset (&locator, 0, sizeof (locator));
2274 memset (a, 0, sizeof (a[0]));
2276 /* Get a line of input. */
2277 if (!unformat_user (input, unformat_line_input, line_input))
2280 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2282 if (unformat (line_input, "add %_%v%_", &locator_set_name))
2284 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
2286 else if (unformat (line_input, "iface %U p %d w %d",
2287 unformat_vnet_sw_interface, vnm,
2288 &locator.sw_if_index, &locator.priority,
2292 vec_add1 (locators, locator);
2296 error = unformat_parse_error (line_input);
2301 a->name = locator_set_name;
2302 a->locators = locators;
2306 rv = vnet_lisp_add_del_locator_set (a, &ls_index);
2309 error = clib_error_return (0, "failed to %s locator-set!",
2310 is_add ? "add" : "delete");
2314 vec_free (locators);
2315 if (locator_set_name)
2316 vec_free (locator_set_name);
2321 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2322 .path = "lisp locator-set",
2323 .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2324 "p <priority> w <weight>]",
2325 .function = lisp_add_del_locator_set_command_fn,
2329 static clib_error_t *
2330 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
2331 unformat_input_t * input,
2332 vlib_cli_command_t * cmd)
2334 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2335 vnet_main_t *vnm = lgm->vnet_main;
2336 unformat_input_t _line_input, *line_input = &_line_input;
2338 clib_error_t *error = 0;
2339 u8 *locator_set_name = 0;
2340 u8 locator_set_name_set = 0;
2341 locator_t locator, *locators = 0;
2342 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2345 memset (&locator, 0, sizeof (locator));
2346 memset (a, 0, sizeof (a[0]));
2348 /* Get a line of input. */
2349 if (!unformat_user (input, unformat_line_input, line_input))
2352 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2354 if (unformat (line_input, "add"))
2356 else if (unformat (line_input, "del"))
2358 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
2359 locator_set_name_set = 1;
2360 else if (unformat (line_input, "iface %U p %d w %d",
2361 unformat_vnet_sw_interface, vnm,
2362 &locator.sw_if_index, &locator.priority,
2366 vec_add1 (locators, locator);
2370 error = unformat_parse_error (line_input);
2375 if (!locator_set_name_set)
2377 error = clib_error_return (0, "locator_set name not set!");
2381 a->name = locator_set_name;
2382 a->locators = locators;
2386 vnet_lisp_add_del_locator (a, 0, &ls_index);
2389 vec_free (locators);
2390 vec_free (locator_set_name);
2395 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2396 .path = "lisp locator",
2397 .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2398 "p <priority> w <weight>",
2399 .function = lisp_add_del_locator_in_set_command_fn,
2403 static clib_error_t *
2404 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
2405 unformat_input_t * input,
2406 vlib_cli_command_t * cmd)
2408 locator_set_t *lsit;
2411 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2413 vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
2414 "Priority", "Weight");
2417 pool_foreach (lsit, lcm->locator_set_pool,
2423 msg = format (msg, "%v", lsit->name);
2427 msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
2429 vec_foreach (locit, lsit->locator_indices)
2433 msg = format (msg, "%16s", " ");
2435 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2437 msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
2440 msg = format (msg, "%16U%16d%16d\n", format_ip_address,
2441 &gid_address_ip(&loc->address), loc->priority,
2445 vlib_cli_output (vm, "%v", msg);
2453 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2454 .path = "show lisp locator-set",
2455 .short_help = "Shows locator-sets",
2456 .function = lisp_cp_show_locator_sets_command_fn,
2460 static map_resolver_t *
2461 get_map_resolver (ip_address_t * a)
2463 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2466 vec_foreach (mr, lcm->map_resolvers)
2468 if (!ip_address_cmp (&mr->address, a))
2477 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2479 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2481 map_resolver_t _mr, *mr = &_mr;
2483 if (vnet_lisp_enable_disable_status () == 0)
2485 clib_warning ("LISP is disabled!");
2486 return VNET_API_ERROR_LISP_DISABLED;
2492 if (get_map_resolver (&a->address))
2494 clib_warning ("map-resolver %U already exists!", format_ip_address,
2499 memset (mr, 0, sizeof (*mr));
2500 ip_address_copy (&mr->address, &a->address);
2501 vec_add1 (lcm->map_resolvers, *mr);
2503 if (vec_len (lcm->map_resolvers) == 1)
2504 lcm->do_map_resolver_election = 1;
2508 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2510 mr = vec_elt_at_index (lcm->map_resolvers, i);
2511 if (!ip_address_cmp (&mr->address, &a->address))
2513 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2514 lcm->do_map_resolver_election = 1;
2516 vec_del1 (lcm->map_resolvers, i);
2524 static clib_error_t *
2525 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
2526 unformat_input_t * input,
2527 vlib_cli_command_t * cmd)
2529 unformat_input_t _line_input, *line_input = &_line_input;
2530 u8 is_add = 1, addr_set = 0;
2531 ip_address_t ip_addr;
2532 clib_error_t *error = 0;
2534 vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
2536 /* Get a line of input. */
2537 if (!unformat_user (input, unformat_line_input, line_input))
2540 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2542 if (unformat (line_input, "add"))
2544 else if (unformat (line_input, "del"))
2546 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
2550 error = unformat_parse_error (line_input);
2557 error = clib_error_return (0, "Map-resolver address must be set!");
2562 a->address = ip_addr;
2563 rv = vnet_lisp_add_del_map_resolver (a);
2566 error = clib_error_return (0, "failed to %s map-resolver!",
2567 is_add ? "add" : "delete");
2575 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
2576 .path = "lisp map-resolver",
2577 .short_help = "lisp map-resolver add/del <ip_address>",
2578 .function = lisp_add_del_map_resolver_command_fn,
2583 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2585 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2588 if (vnet_lisp_enable_disable_status () == 0)
2590 clib_warning ("LISP is disabled!");
2591 return VNET_API_ERROR_LISP_DISABLED;
2596 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
2599 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
2600 return VNET_API_ERROR_INVALID_ARGUMENT;
2603 lcm->mreq_itr_rlocs = p[0];
2607 lcm->mreq_itr_rlocs = ~0;
2613 static clib_error_t *
2614 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
2615 unformat_input_t * input,
2616 vlib_cli_command_t * cmd)
2618 unformat_input_t _line_input, *line_input = &_line_input;
2620 u8 *locator_set_name = 0;
2621 clib_error_t *error = 0;
2623 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
2625 /* Get a line of input. */
2626 if (!unformat_user (input, unformat_line_input, line_input))
2629 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2631 if (unformat (line_input, "del"))
2633 else if (unformat (line_input, "add %_%v%_", &locator_set_name))
2637 error = unformat_parse_error (line_input);
2643 a->locator_set_name = locator_set_name;
2644 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
2647 error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
2648 is_add ? "add" : "delete");
2651 vec_free (locator_set_name);
2659 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
2660 .path = "lisp map-request itr-rlocs",
2661 .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
2662 .function = lisp_add_del_mreq_itr_rlocs_command_fn,
2666 static clib_error_t *
2667 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
2668 unformat_input_t * input,
2669 vlib_cli_command_t * cmd)
2671 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2672 locator_set_t *loc_set;
2674 vlib_cli_output (vm, "%=20s", "itr-rlocs");
2676 if (~0 == lcm->mreq_itr_rlocs)
2681 loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
2683 vlib_cli_output (vm, "%=20s", loc_set->name);
2689 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
2690 .path = "show lisp map-request itr-rlocs",
2691 .short_help = "Shows map-request itr-rlocs",
2692 .function = lisp_show_mreq_itr_rlocs_command_fn,
2696 /* Statistics (not really errors) */
2697 #define foreach_lisp_cp_lookup_error \
2699 _(MAP_REQUESTS_SENT, "map-request sent")
2701 static char *lisp_cp_lookup_error_strings[] = {
2702 #define _(sym,string) string,
2703 foreach_lisp_cp_lookup_error
2709 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
2710 foreach_lisp_cp_lookup_error
2712 LISP_CP_LOOKUP_N_ERROR,
2713 } lisp_cp_lookup_error_t;
2717 LISP_CP_LOOKUP_NEXT_DROP,
2718 LISP_CP_LOOKUP_N_NEXT,
2719 } lisp_cp_lookup_next_t;
2723 gid_address_t dst_eid;
2724 ip_address_t map_resolver_ip;
2725 } lisp_cp_lookup_trace_t;
2728 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
2730 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2731 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2732 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
2734 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
2735 format_ip_address, &t->map_resolver_ip, format_gid_address,
2741 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
2742 ip_address_t * sloc)
2744 map_resolver_t *mrit;
2747 if (vec_len (lcm->map_resolvers) == 0)
2749 clib_warning ("No map-resolver configured");
2753 /* find the first mr ip we have a route to and the ip of the
2754 * iface that has a route to it */
2755 vec_foreach (mrit, lcm->map_resolvers)
2758 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
2760 ip_address_copy (mr_ip, a);
2762 /* also update globals */
2767 clib_warning ("Can't find map-resolver and local interface ip!");
2771 static gid_address_t *
2772 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2778 ip_interface_address_t *ia = 0;
2779 gid_address_t gid_data, *gid = &gid_data;
2780 gid_address_t *rlocs = 0;
2781 ip_prefix_t *ippref = &gid_address_ippref (gid);
2782 ip_address_t *rloc = &ip_prefix_addr (ippref);
2784 memset (gid, 0, sizeof (gid[0]));
2785 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
2786 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
2788 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
2789 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2791 /* Add ipv4 locators first TODO sort them */
2794 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2795 loc->sw_if_index, 1 /* unnumbered */,
2797 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2798 ip_address_set (rloc, addr, IP4);
2799 ip_prefix_len (ippref) = 32;
2800 ip_prefix_normalize (ippref);
2801 vec_add1 (rlocs, gid[0]);
2804 /* Add ipv6 locators */
2805 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2806 loc->sw_if_index, 1 /* unnumbered */,
2808 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2809 ip_address_set (rloc, addr, IP6);
2810 ip_prefix_len (ippref) = 128;
2811 ip_prefix_normalize (ippref);
2812 vec_add1 (rlocs, gid[0]);
2820 static vlib_buffer_t *
2821 build_encapsulated_map_request (lisp_cp_main_t * lcm,
2822 gid_address_t * seid, gid_address_t * deid,
2823 locator_set_t * loc_set, ip_address_t * mr_ip,
2824 ip_address_t * sloc, u8 is_smr_invoked,
2825 u64 * nonce_res, u32 * bi_res)
2829 gid_address_t *rlocs = 0;
2830 vlib_main_t *vm = lcm->vlib_main;
2832 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2834 clib_warning ("Can't allocate buffer for Map-Request!");
2838 b = vlib_get_buffer (vm, bi);
2840 /* leave some space for the encap headers */
2841 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2844 rlocs = build_itr_rloc_list (lcm, loc_set);
2846 if (MR_MODE_SRC_DST == lcm->map_request_mode)
2849 memset (&sd, 0, sizeof (sd));
2850 build_src_dst (&sd, seid, deid);
2851 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked, nonce_res);
2856 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
2860 /* push ecm: udp-ip-lisp */
2861 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2863 /* push outer ip header */
2864 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2874 reset_pending_mr_counters (pending_map_request_t * r)
2876 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2881 elect_map_resolver (lisp_cp_main_t * lcm)
2885 vec_foreach (mr, lcm->map_resolvers)
2889 ip_address_copy (&lcm->active_map_resolver, &mr->address);
2890 lcm->do_map_resolver_election = 0;
2897 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
2898 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2900 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2901 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2904 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
2905 gid_address_t * seid, gid_address_t * deid,
2906 u8 is_smr_invoked, u8 is_resend)
2909 u32 next_index, bi = 0, *to_next, map_index;
2913 locator_set_t *loc_set;
2915 pending_map_request_t *pmr, *duplicate_pmr = 0;
2919 ASSERT (*lcm->pending_map_request_lock);
2921 /* if there is already a pending request remember it */
2924 pool_foreach(pmr, lcm->pending_map_requests_pool,
2926 if (!gid_address_cmp (&pmr->src, seid)
2927 && !gid_address_cmp (&pmr->dst, deid))
2929 duplicate_pmr = pmr;
2935 if (!is_resend && duplicate_pmr)
2937 /* don't send the request if there is a pending map request already */
2941 /* get locator-set for seid */
2942 if (!lcm->lisp_pitr)
2944 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2945 if (map_index == ~0)
2947 clib_warning ("No local mapping found in eid-table for %U!",
2948 format_gid_address, seid);
2952 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2957 ("Mapping found for src eid %U is not marked as local!",
2958 format_gid_address, seid);
2961 ls_index = map->locator_set_index;
2965 map_index = lcm->pitr_map_index;
2966 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
2967 ls_index = map->locator_set_index;
2970 /* overwrite locator set if map-request itr-rlocs configured */
2971 if (~0 != lcm->mreq_itr_rlocs)
2973 ls_index = lcm->mreq_itr_rlocs;
2976 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
2978 while (lcm->do_map_resolver_election
2979 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
2980 &lcm->active_map_resolver,
2983 if (0 == elect_map_resolver (lcm))
2984 /* all Mrs are down */
2987 duplicate_pmr->to_be_removed = 1;
2989 /* restart MR checking by marking all of them up */
2990 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
2996 /* build the encapsulated map request */
2997 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
2998 &lcm->active_map_resolver,
2999 &sloc, is_smr_invoked, &nonce, &bi);
3004 /* set fib index to default and lookup node */
3005 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3006 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3007 ip4_lookup_node.index : ip6_lookup_node.index;
3009 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3011 /* Enqueue the packet */
3012 to_next = vlib_frame_vector_args (f);
3015 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3018 /* if there is a pending request already update it */
3020 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
3022 /* remove the oldest nonce */
3023 u64 CLIB_UNUSED (tmp), *nonce_del;
3024 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
3025 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
3026 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
3029 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
3030 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3031 duplicate_pmr - lcm->pending_map_requests_pool);
3035 /* add map-request to pending requests table */
3036 pool_get (lcm->pending_map_requests_pool, pmr);
3037 memset (pmr, 0, sizeof (*pmr));
3038 gid_address_copy (&pmr->src, seid);
3039 gid_address_copy (&pmr->dst, deid);
3040 clib_fifo_add1 (pmr->nonces, nonce);
3041 pmr->is_smr_invoked = is_smr_invoked;
3042 reset_pending_mr_counters (pmr);
3043 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3044 pmr - lcm->pending_map_requests_pool);
3051 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
3053 ip4_header_t *ip4 = hdr;
3056 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3058 ip_address_set (src, &ip4->src_address, IP4);
3059 ip_address_set (dst, &ip4->dst_address, IP4);
3064 ip_address_set (src, &ip6->src_address, IP6);
3065 ip_address_set (dst, &ip6->dst_address, IP6);
3070 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3074 u32 vni = ~0, table_id = ~0;
3076 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
3077 IP4 ? FIB_PROTOCOL_IP4 :
3083 vnip = hash_get (lcm->vni_by_table_id, table_id);
3087 clib_warning ("vrf %d is not mapped to any vni!", table_id);
3093 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3099 l2input_main_t *l2im = &l2input_main;
3100 l2_input_config_t *config;
3101 l2_bridge_domain_t *bd_config;
3103 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3104 config = vec_elt_at_index (l2im->configs, sw_if_index0);
3105 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3107 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
3111 clib_warning ("bridge domain %d is not mapped to any vni!",
3118 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3119 gid_address_t * src, gid_address_t * dst)
3124 memset (src, 0, sizeof (*src));
3125 memset (dst, 0, sizeof (*dst));
3126 type = vnet_buffer (b)->lisp.overlay_afi;
3128 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3131 u8 version, preflen;
3133 gid_address_type (src) = GID_ADDR_IP_PREFIX;
3134 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
3136 ip = vlib_buffer_get_current (b);
3137 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
3139 version = gid_address_ip_version (src);
3140 preflen = ip_address_max_len (version);
3141 gid_address_ippref_len (src) = preflen;
3142 gid_address_ippref_len (dst) = preflen;
3144 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3145 gid_address_vni (dst) = vni;
3146 gid_address_vni (src) = vni;
3148 else if (LISP_AFI_MAC == type)
3150 ethernet_header_t *eh;
3152 eh = vlib_buffer_get_current (b);
3154 gid_address_type (src) = GID_ADDR_MAC;
3155 gid_address_type (dst) = GID_ADDR_MAC;
3156 mac_copy (&gid_address_mac (src), eh->src_address);
3157 mac_copy (&gid_address_mac (dst), eh->dst_address);
3160 vni = lisp_get_vni_from_buffer_eth (lcm, b);
3162 gid_address_vni (dst) = vni;
3163 gid_address_vni (src) = vni;
3168 lisp_cp_lookup_inline (vlib_main_t * vm,
3169 vlib_node_runtime_t * node,
3170 vlib_frame_t * from_frame, int overlay)
3172 u32 *from, *to_next_drop, di, si;
3173 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3174 u32 pkts_mapped = 0;
3175 uword n_left_from, n_left_to_next_drop;
3177 from = vlib_frame_vector_args (from_frame);
3178 n_left_from = from_frame->n_vectors;
3180 while (n_left_from > 0)
3182 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3183 to_next_drop, n_left_to_next_drop);
3185 while (n_left_from > 0 && n_left_to_next_drop > 0)
3189 gid_address_t src, dst;
3194 to_next_drop[0] = pi0;
3196 n_left_to_next_drop -= 1;
3198 b0 = vlib_get_buffer (vm, pi0);
3199 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
3200 vnet_buffer (b0)->lisp.overlay_afi = overlay;
3202 /* src/dst eid pair */
3203 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
3205 /* if we have remote mapping for destination already in map-chache
3206 add forwarding tunnel directly. If not send a map-request */
3207 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3211 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3212 /* send a map-request also in case of negative mapping entry
3213 with corresponding action */
3214 if (m->action == LISP_SEND_MAP_REQUEST)
3216 /* send map-request */
3217 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3218 0 /* is_resend */ );
3223 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3227 dp_add_fwd_entry (lcm, si, di);
3233 /* send map-request */
3234 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3235 0 /* is_resend */ );
3239 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3241 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3244 memset (tr, 0, sizeof (*tr));
3245 gid_address_copy (&tr->dst_eid, &dst);
3246 ip_address_copy (&tr->map_resolver_ip,
3247 &lcm->active_map_resolver);
3249 gid_address_free (&dst);
3250 gid_address_free (&src);
3253 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3254 n_left_to_next_drop);
3256 vlib_node_increment_counter (vm, node->node_index,
3257 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
3259 return from_frame->n_vectors;
3263 lisp_cp_lookup_ip4 (vlib_main_t * vm,
3264 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3266 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3270 lisp_cp_lookup_ip6 (vlib_main_t * vm,
3271 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3273 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
3277 lisp_cp_lookup_l2 (vlib_main_t * vm,
3278 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3280 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
3284 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
3285 .function = lisp_cp_lookup_ip4,
3286 .name = "lisp-cp-lookup-ip4",
3287 .vector_size = sizeof (u32),
3288 .format_trace = format_lisp_cp_lookup_trace,
3289 .type = VLIB_NODE_TYPE_INTERNAL,
3291 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3292 .error_strings = lisp_cp_lookup_error_strings,
3294 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3297 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3303 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
3304 .function = lisp_cp_lookup_ip6,
3305 .name = "lisp-cp-lookup-ip6",
3306 .vector_size = sizeof (u32),
3307 .format_trace = format_lisp_cp_lookup_trace,
3308 .type = VLIB_NODE_TYPE_INTERNAL,
3310 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3311 .error_strings = lisp_cp_lookup_error_strings,
3313 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3316 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3322 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
3323 .function = lisp_cp_lookup_l2,
3324 .name = "lisp-cp-lookup-l2",
3325 .vector_size = sizeof (u32),
3326 .format_trace = format_lisp_cp_lookup_trace,
3327 .type = VLIB_NODE_TYPE_INTERNAL,
3329 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3330 .error_strings = lisp_cp_lookup_error_strings,
3332 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3335 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3340 /* lisp_cp_input statistics */
3341 #define foreach_lisp_cp_input_error \
3343 _(MAP_REPLIES_RECEIVED, "map-replies received")
3345 static char *lisp_cp_input_error_strings[] = {
3346 #define _(sym,string) string,
3347 foreach_lisp_cp_input_error
3353 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
3354 foreach_lisp_cp_input_error
3356 LISP_CP_INPUT_N_ERROR,
3357 } lisp_cp_input_error_t;
3361 LISP_CP_INPUT_NEXT_DROP,
3362 LISP_CP_INPUT_N_NEXT,
3363 } lisp_cp_input_next_t;
3367 gid_address_t dst_eid;
3368 ip4_address_t map_resolver_ip;
3369 } lisp_cp_input_trace_t;
3372 format_lisp_cp_input_trace (u8 * s, va_list * args)
3374 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3375 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3376 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
3377 va_arg (*args, lisp_cp_input_trace_t *);
3379 s = format (s, "LISP-CP-INPUT: TODO");
3384 process_map_reply (void *arg)
3386 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3387 vlib_buffer_t *b = arg;
3388 u32 len = 0, i, ttl, dst_map_index = 0;
3390 pending_map_request_t *pmr;
3392 map_reply_hdr_t *mrep_hdr;
3396 u8 authoritative, action;
3397 locator_t *locators = 0, *loc;
3399 mrep_hdr = vlib_buffer_get_current (b);
3401 lisp_pending_map_request_lock (lcm);
3403 /* Check pending requests table and nonce */
3404 nonce = MREP_NONCE (mrep_hdr);
3405 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, nonce);
3408 clib_warning ("No pending map-request entry with nonce %lu!", nonce);
3411 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
3413 vlib_buffer_pull (b, sizeof (*mrep_hdr));
3415 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3417 h = vlib_buffer_get_current (b);
3418 ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3419 action = MAP_REC_ACTION (h);
3420 authoritative = MAP_REC_AUTH (h);
3422 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3425 clib_warning ("Failed to parse mapping record!");
3426 vec_foreach (loc, locators)
3430 vec_free (locators);
3434 /* insert/update mappings cache */
3435 vnet_lisp_add_del_mapping (&deid, locators, action, authoritative, ttl,
3436 1, 0 /* is_static */ , &dst_map_index);
3438 /* try to program forwarding only if mapping saved or updated */
3439 if ((u32) ~ 0 != dst_map_index)
3440 lisp_add_del_adjacency (lcm, &pmr->src, &deid, 1);
3442 vec_free (locators);
3445 /* remove pending map request entry */
3448 clib_fifo_foreach (noncep, pmr->nonces, ({
3449 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
3453 clib_fifo_free (pmr->nonces);
3454 pool_put (lcm->pending_map_requests_pool, pmr);
3457 lisp_pending_map_request_unlock (lcm);
3462 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm,
3465 map_request_hdr_t *mreq_hdr;
3466 gid_address_t src, dst;
3469 gid_address_t *itr_rlocs = 0, *rloc;
3471 mreq_hdr = vlib_buffer_get_current (b);
3472 vlib_buffer_pull (b, sizeof (*mreq_hdr));
3474 // nonce = MREQ_NONCE(mreq_hdr);
3476 if (!MREQ_SMR (mreq_hdr))
3478 clib_warning ("Only SMR Map-Requests supported for now!");
3483 len = lisp_msg_parse_addr (b, &src);
3487 /* for now we don't do anything with the itr's rlocs */
3489 lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3490 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
3494 /* TODO: RLOCs are currently unused, so free them for now */
3495 vec_foreach (rloc, itr_rlocs)
3497 gid_address_free (rloc);
3500 /* parse eid records and send SMR-invoked map-requests */
3501 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
3503 memset (&dst, 0, sizeof (dst));
3504 len = lisp_msg_parse_eid_rec (b, &dst);
3507 clib_warning ("Can't parse map-request EID-record");
3510 /* send SMR-invoked map-requests */
3511 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3516 queue_map_reply (vlib_buffer_t * b)
3518 vlib_buffer_t *a = clib_mem_alloc (sizeof (a[0]) + b->current_length);
3520 clib_memcpy (a->data, b->data + b->current_data, b->current_length);
3521 a->current_length = b->current_length;
3522 a->current_data = 0;
3524 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (a[0])
3525 + a->current_length);
3530 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
3531 vlib_frame_t * from_frame)
3533 u32 n_left_from, *from, *to_next_drop;
3534 lisp_msg_type_e type;
3535 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3537 from = vlib_frame_vector_args (from_frame);
3538 n_left_from = from_frame->n_vectors;
3541 while (n_left_from > 0)
3543 u32 n_left_to_next_drop;
3545 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3546 to_next_drop, n_left_to_next_drop);
3547 while (n_left_from > 0 && n_left_to_next_drop > 0)
3555 to_next_drop[0] = bi0;
3557 n_left_to_next_drop -= 1;
3559 b0 = vlib_get_buffer (vm, bi0);
3561 type = lisp_msg_type (vlib_buffer_get_current (b0));
3564 case LISP_MAP_REPLY:
3565 queue_map_reply (b0);
3567 case LISP_MAP_REQUEST:
3568 process_map_request (vm, lcm, b0);
3571 clib_warning ("Unsupported LISP message type %d", type);
3575 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
3577 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3583 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3584 n_left_to_next_drop);
3586 return from_frame->n_vectors;
3590 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3591 .function = lisp_cp_input,
3592 .name = "lisp-cp-input",
3593 .vector_size = sizeof (u32),
3594 .format_trace = format_lisp_cp_input_trace,
3595 .type = VLIB_NODE_TYPE_INTERNAL,
3597 .n_errors = LISP_CP_INPUT_N_ERROR,
3598 .error_strings = lisp_cp_input_error_strings,
3600 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3603 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3609 lisp_cp_init (vlib_main_t * vm)
3611 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3612 clib_error_t *error = 0;
3614 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3617 lcm->im4 = &ip4_main;
3618 lcm->im6 = &ip6_main;
3619 lcm->vlib_main = vm;
3620 lcm->vnet_main = vnet_get_main ();
3621 lcm->mreq_itr_rlocs = ~0;
3623 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
3625 lcm->pending_map_request_lock =
3626 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
3628 lcm->pending_map_request_lock[0] = 0;
3629 gid_dictionary_init (&lcm->mapping_index_by_gid);
3630 lcm->do_map_resolver_election = 1;
3631 lcm->map_request_mode = MR_MODE_DST_ONLY;
3633 /* default vrf mapped to vni 0 */
3634 hash_set (lcm->table_id_by_vni, 0, 0);
3635 hash_set (lcm->vni_by_table_id, 0, 0);
3637 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
3638 lisp_cp_input_node.index, 1 /* is_ip4 */ );
3639 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
3640 lisp_cp_input_node.index, 0 /* is_ip4 */ );
3646 send_map_request_thread_fn (void *arg)
3648 map_request_args_t *a = arg;
3649 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3652 /* if resending, we already have the lock */
3653 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3656 /* get lock before sending map-request */
3657 lisp_pending_map_request_lock (lcm);
3658 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3659 lisp_pending_map_request_unlock (lcm);
3667 queue_map_request (gid_address_t * seid, gid_address_t * deid,
3668 u8 smr_invoked, u8 is_resend)
3670 map_request_args_t a;
3672 a.is_resend = is_resend;
3673 gid_address_copy (&a.seid, seid);
3674 gid_address_copy (&a.deid, deid);
3675 a.smr_invoked = smr_invoked;
3677 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
3678 (u8 *) & a, sizeof (a));
3683 * Take an action with a pending map request depending on expiration time
3684 * and re-try counters.
3687 update_pending_request (pending_map_request_t * r, f64 dt)
3689 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3692 if (r->time_to_expire - dt < 0)
3693 /* it's time to decide what to do with this pending request */
3695 if (r->retries_num >= NUMBER_OF_RETRIES)
3696 /* too many retries -> assume current map resolver is not available */
3698 mr = get_map_resolver (&lcm->active_map_resolver);
3701 clib_warning ("Map resolver %U not found - probably deleted "
3702 "by the user recently.", format_ip_address,
3703 &lcm->active_map_resolver);
3707 clib_warning ("map resolver %U is unreachable, ignoring",
3708 format_ip_address, &lcm->active_map_resolver);
3710 /* mark current map resolver unavailable so it won't be
3711 * selected next time */
3713 mr->last_update = vlib_time_now (lcm->vlib_main);
3716 reset_pending_mr_counters (r);
3717 elect_map_resolver (lcm);
3719 /* try to find a next eligible map resolver and re-send */
3720 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3726 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3729 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3733 r->time_to_expire -= dt;
3737 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
3740 pending_map_request_t *pmr;
3741 u32 *to_be_removed = 0, *pmr_index;
3743 ASSERT (*lcm->pending_map_request_lock);
3746 pool_foreach (pmr, lcm->pending_map_requests_pool,
3748 if (pmr->to_be_removed)
3750 clib_fifo_foreach (nonce, pmr->nonces, ({
3751 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
3754 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
3759 vec_foreach (pmr_index, to_be_removed)
3760 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
3762 vec_free (to_be_removed);
3766 send_map_resolver_service (vlib_main_t * vm,
3767 vlib_node_runtime_t * rt, vlib_frame_t * f)
3770 pending_map_request_t *pmr;
3771 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3775 vlib_process_wait_for_event_or_clock (vm, period);
3777 /* currently no signals are expected - just wait for clock */
3778 (void) vlib_process_get_events (vm, 0);
3780 lisp_pending_map_request_lock (lcm);
3783 pool_foreach (pmr, lcm->pending_map_requests_pool,
3785 if (!pmr->to_be_removed)
3786 update_pending_request (pmr, period);
3790 remove_dead_pending_map_requests (lcm);
3792 lisp_pending_map_request_unlock (lcm);
3800 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
3801 .function = send_map_resolver_service,
3802 .type = VLIB_NODE_TYPE_PROCESS,
3803 .name = "lisp-retry-service",
3804 .process_log2_n_stack_bytes = 16,
3808 VLIB_INIT_FUNCTION (lisp_cp_init);
3811 * fd.io coding-style-patch-verification: ON
3814 * eval: (c-set-style "gnu")