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>
26 #include <openssl/evp.h>
27 #include <openssl/hmac.h>
38 vnet_lisp_get_map_request_mode (void)
40 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
41 return lcm->map_request_mode;
45 auth_data_len_by_key_id (lisp_key_type_t key_id)
50 return SHA1_AUTH_DATA_LEN;
51 case HMAC_SHA_256_128:
52 return SHA256_AUTH_DATA_LEN;
54 clib_warning ("unsupported key type: %d!", key_id);
61 get_encrypt_fcn (lisp_key_type_t key_id)
67 case HMAC_SHA_256_128:
70 clib_warning ("unsupported encryption key type: %d!", key_id);
77 queue_map_request (gid_address_t * seid, gid_address_t * deid,
78 u8 smr_invoked, u8 is_resend);
80 ip_interface_address_t *
81 ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
82 u32 sw_if_index, u8 loop)
84 vnet_main_t *vnm = vnet_get_main ();
85 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
86 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
87 sw_if_index = swif->unnumbered_sw_if_index;
89 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
90 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
92 return pool_elt_at_index ((lm)->if_address_pool, ia);
96 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
99 ip_interface_address_t *ia;
101 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
104 return ip_interface_address_get_address (lm, ia);
108 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
109 u8 version, ip_address_t * result)
111 ip_lookup_main_t *lm;
114 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
115 addr = ip_interface_get_first_address (lm, sw_if_index, version);
119 ip_address_set (result, addr, version);
124 * convert from a LISP address to a FIB prefix
127 ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
129 if (addr->version == IP4)
132 prefix->fp_proto = FIB_PROTOCOL_IP4;
133 memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
134 memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
138 prefix->fp_len = 128;
139 prefix->fp_proto = FIB_PROTOCOL_IP6;
140 memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
145 * convert from a LISP to a FIB prefix
148 ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
149 fib_prefix_t * fib_prefix)
151 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
152 fib_prefix->fp_len = ip_prefix->len;
156 * Find the sw_if_index of the interface that would be used to egress towards
160 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
162 fib_node_index_t fei;
165 ip_address_to_fib_prefix (dst, &prefix);
167 fei = fib_table_lookup (0, &prefix);
169 return (fib_entry_get_resolving_interface (fei));
173 * Find first IP of the interface that would be used to egress towards dst.
174 * Returns 1 if the address is found 0 otherwise.
177 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
178 ip_address_t * result)
181 ip_lookup_main_t *lm;
185 ASSERT (result != 0);
187 ipver = ip_addr_version (dst);
189 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
190 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
195 /* find the first ip address */
196 addr = ip_interface_get_first_address (lm, si, ipver);
200 ip_address_set (result, addr, ipver);
205 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
211 dp_table = hash_get (lcm->table_id_by_vni, vni);
215 clib_warning ("vni %d not associated to a vrf!", vni);
216 return VNET_API_ERROR_INVALID_VALUE;
221 dp_table = hash_get (lcm->bd_id_by_vni, vni);
224 clib_warning ("vni %d not associated to a bridge domain!", vni);
225 return VNET_API_ERROR_INVALID_VALUE;
229 /* enable/disable data-plane interface */
233 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
235 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
240 lisp_gpe_tenant_l2_iface_unlock (vni);
242 lisp_gpe_tenant_l3_iface_unlock (vni);
249 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
251 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
254 memset (a, 0, sizeof (*a));
256 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
260 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
262 /* delete dp fwd entry */
265 a->locator_pairs = fe->locator_pairs;
266 a->vni = gid_address_vni (&fe->reid);
267 gid_address_copy (&a->rmt_eid, &fe->reid);
269 gid_address_copy (&a->lcl_eid, &fe->leid);
271 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
273 /* delete entry in fwd table */
274 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
275 vec_free (fe->locator_pairs);
276 pool_put (lcm->fwd_entry_pool, fe);
280 * Finds first remote locator with best (lowest) priority that has a local
281 * peer locator with an underlying route to it.
285 get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
286 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
288 u32 i, limitp = 0, li, found = 0, esi;
289 locator_set_t *rmt_ls, *lcl_ls;
290 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
291 locator_t *lp, *rmt = 0;
296 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
298 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
300 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
307 /* find unvisited remote locator with best priority */
308 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
310 if (0 != hash_get (checked, i))
313 li = vec_elt (rmt_ls->locator_indices, i);
314 lp = pool_elt_at_index (lcm->locator_pool, li);
316 /* we don't support non-IP locators for now */
317 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
320 if ((found && lp->priority == limitp)
321 || (!found && lp->priority >= limitp))
325 /* don't search for locators with lower priority and don't
326 * check this locator again*/
327 limitp = lp->priority;
328 hash_set (checked, i, 1);
332 /* check if a local locator with a route to remote locator exists */
335 /* find egress sw_if_index for rmt locator */
337 ip_fib_get_egress_iface_for_dst (lcm,
338 &gid_address_ip (&rmt->address));
339 if ((u32) ~ 0 == esi)
342 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
344 li = vec_elt (lcl_ls->locator_indices, i);
345 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
347 /* found local locator with the needed sw_if_index */
348 if (sl->sw_if_index == esi)
350 /* and it has an address */
351 if (0 == ip_interface_get_first_ip_address (lcm,
353 gid_address_ip_version
358 memset (&pair, 0, sizeof (pair));
359 ip_address_copy (&pair.rmt_loc,
360 &gid_address_ip (&rmt->address));
361 ip_address_copy (&pair.lcl_loc, lcl_addr);
362 pair.weight = rmt->weight;
363 vec_add1 (locator_pairs[0], pair);
377 gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
380 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
384 switch (fid_addr_type (fid))
386 case FID_ADDR_IP_PREF:
387 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
388 gid_address_ippref (dst) = fid_addr_ippref (fid);
391 gid_address_type (dst) = GID_ADDR_MAC;
392 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
395 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
401 vnet_lisp_map_register_state_get (void)
403 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
404 return lcm->map_registering;
408 vnet_lisp_rloc_probe_state_get (void)
410 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
411 return lcm->rloc_probing;
415 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
417 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
418 mapping_t *src_map, *dst_map;
420 uword *feip = 0, *dpid;
422 u8 type, is_src_dst = 0;
424 memset (a, 0, sizeof (*a));
426 /* remove entry if it already exists */
427 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
429 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
432 src_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
434 src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
435 dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
437 /* insert data plane forwarding entry */
440 if (MR_MODE_SRC_DST == lcm->map_request_mode)
442 if (GID_ADDR_SRC_DST == gid_address_type (&dst_map->eid))
444 gid_address_sd_to_flat (&a->rmt_eid, &dst_map->eid,
445 &gid_address_sd_dst (&dst_map->eid));
446 gid_address_sd_to_flat (&a->lcl_eid, &dst_map->eid,
447 &gid_address_sd_src (&dst_map->eid));
451 gid_address_copy (&a->rmt_eid, &dst_map->eid);
452 gid_address_copy (&a->lcl_eid, &src_map->eid);
457 gid_address_copy (&a->rmt_eid, &dst_map->eid);
459 a->vni = gid_address_vni (&a->rmt_eid);
461 /* get vrf or bd_index associated to vni */
462 type = gid_address_type (&a->rmt_eid);
463 if (GID_ADDR_IP_PREFIX == type)
465 dpid = hash_get (lcm->table_id_by_vni, a->vni);
468 clib_warning ("vni %d not associated to a vrf!", a->vni);
471 a->table_id = dpid[0];
473 else if (GID_ADDR_MAC == type)
475 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
478 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
484 /* find best locator pair that 1) verifies LISP policy 2) are connected */
485 if (0 == get_locator_pairs (lcm, src_map, dst_map, &a->locator_pairs))
489 a->action = dst_map->action;
493 u8 ipver = ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
494 a->decap_next_index = (ipver == IP4) ?
495 LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
497 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
499 /* add tunnel to fwd entry table XXX check return value from DP insertion */
500 pool_get (lcm->fwd_entry_pool, fe);
501 fe->locator_pairs = a->locator_pairs;
502 gid_address_copy (&fe->reid, &a->rmt_eid);
503 gid_address_copy (&fe->leid, &src_map->eid);
504 fe->is_src_dst = is_src_dst;
505 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
506 fe - lcm->fwd_entry_pool);
510 * Returns vector of adjacencies.
512 * The caller must free the vector returned by this function.
514 * @param vni virtual network identifier
515 * @return vector of adjacencies
518 vnet_lisp_adjacencies_get_by_vni (u32 vni)
520 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
522 lisp_adjacency_t *adjs = 0, adj;
525 pool_foreach(fwd, lcm->fwd_entry_pool,
527 if (gid_address_vni (&fwd->reid) != vni)
530 gid_address_copy (&adj.reid, &fwd->reid);
531 gid_address_copy (&adj.leid, &fwd->leid);
532 vec_add1 (adjs, adj);
539 static clib_error_t *
540 lisp_show_adjacencies_command_fn (vlib_main_t * vm,
541 unformat_input_t * input,
542 vlib_cli_command_t * cmd)
544 lisp_adjacency_t *adjs, *adj;
545 vlib_cli_output (vm, "%s %40s\n", "leid", "reid");
546 unformat_input_t _line_input, *line_input = &_line_input;
549 /* Get a line of input. */
550 if (!unformat_user (input, unformat_line_input, line_input))
553 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
555 if (unformat (line_input, "vni %d", &vni))
559 vlib_cli_output (vm, "parse error: '%U'",
560 format_unformat_error, line_input);
567 vlib_cli_output (vm, "error: no vni specified!");
571 adjs = vnet_lisp_adjacencies_get_by_vni (vni);
573 vec_foreach (adj, adjs)
575 vlib_cli_output (vm, "%U %40U\n", format_gid_address, &adj->leid,
576 format_gid_address, &adj->reid);
584 VLIB_CLI_COMMAND (lisp_show_adjacencies_command) = {
585 .path = "show lisp adjacencies",
586 .short_help = "show lisp adjacencies",
587 .function = lisp_show_adjacencies_command_fn,
592 get_map_server (ip_address_t * a)
594 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
597 vec_foreach (m, lcm->map_servers)
599 if (!ip_address_cmp (&m->address, a))
608 get_map_resolver (ip_address_t * a)
610 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
613 vec_foreach (m, lcm->map_resolvers)
615 if (!ip_address_cmp (&m->address, a))
624 vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
627 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
628 lisp_msmr_t _ms, *ms = &_ms;
630 if (vnet_lisp_enable_disable_status () == 0)
632 clib_warning ("LISP is disabled!");
633 return VNET_API_ERROR_LISP_DISABLED;
638 if (get_map_server (addr))
640 clib_warning ("map-server %U already exists!", format_ip_address,
645 memset (ms, 0, sizeof (*ms));
646 ip_address_copy (&ms->address, addr);
647 vec_add1 (lcm->map_servers, ms[0]);
651 for (i = 0; i < vec_len (lcm->map_servers); i++)
653 ms = vec_elt_at_index (lcm->map_servers, i);
654 if (!ip_address_cmp (&ms->address, addr))
656 vec_del1 (lcm->map_servers, i);
665 static clib_error_t *
666 lisp_add_del_map_server_command_fn (vlib_main_t * vm,
667 unformat_input_t * input,
668 vlib_cli_command_t * cmd)
671 u8 is_add = 1, ip_set = 0;
673 unformat_input_t _line_input, *line_input = &_line_input;
675 /* Get a line of input. */
676 if (!unformat_user (input, unformat_line_input, line_input))
679 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
681 if (unformat (line_input, "add"))
683 else if (unformat (line_input, "del"))
685 else if (unformat (line_input, "%U", unformat_ip_address, &ip))
689 vlib_cli_output (vm, "parse error: '%U'",
690 format_unformat_error, line_input);
697 vlib_cli_output (vm, "map-server ip address not set!");
701 rv = vnet_lisp_add_del_map_server (&ip, is_add);
703 vlib_cli_output (vm, "failed to %s map-server!",
704 is_add ? "add" : "delete");
710 VLIB_CLI_COMMAND (lisp_add_del_map_server_command) = {
711 .path = "lisp map-server",
712 .short_help = "lisp map-server add|del <ip>",
713 .function = lisp_add_del_map_server_command_fn,
718 * Add/remove mapping to/from map-cache. Overwriting not allowed.
721 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
722 u32 * map_index_result)
724 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
725 u32 mi, *map_indexp, map_index, i;
726 mapping_t *m, *old_map;
729 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
730 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
733 /* TODO check if overwriting and take appropriate actions */
734 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
736 clib_warning ("eid %U found in the eid-table", format_gid_address,
738 return VNET_API_ERROR_VALUE_EXIST;
741 pool_get (lcm->mapping_pool, m);
742 gid_address_copy (&m->eid, &a->eid);
743 m->locator_set_index = a->locator_set_index;
745 m->action = a->action;
747 m->is_static = a->is_static;
748 m->key = vec_dup (a->key);
749 m->key_id = a->key_id;
751 map_index = m - lcm->mapping_pool;
752 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
755 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
757 clib_warning ("Locator set with index %d doesn't exist",
758 a->locator_set_index);
759 return VNET_API_ERROR_INVALID_VALUE;
762 /* add eid to list of eids supported by locator-set */
763 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
764 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
765 a->locator_set_index);
766 vec_add1 (eid_indexes[0], map_index);
771 vec_add1 (lcm->local_mappings_indexes, map_index);
773 map_index_result[0] = map_index;
777 if (mi == GID_LOOKUP_MISS)
779 clib_warning ("eid %U not found in the eid-table",
780 format_gid_address, &a->eid);
781 return VNET_API_ERROR_INVALID_VALUE;
784 /* clear locator-set to eids binding */
785 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
786 a->locator_set_index);
787 for (i = 0; i < vec_len (eid_indexes[0]); i++)
789 map_indexp = vec_elt_at_index (eid_indexes[0], i);
790 if (map_indexp[0] == mi)
793 vec_del1 (eid_indexes[0], i);
795 /* remove local mark if needed */
796 m = pool_elt_at_index (lcm->mapping_pool, mi);
800 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
802 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
803 if (lm_indexp[0] == mi)
806 vec_del1 (lcm->local_mappings_indexes, k);
809 /* remove mapping from dictionary */
810 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
811 gid_address_free (&m->eid);
812 pool_put_index (lcm->mapping_pool, mi);
819 * Add/update/delete mapping to/in/from map-cache.
822 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
823 u32 * map_index_result)
829 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
831 if (vnet_lisp_enable_disable_status () == 0)
833 clib_warning ("LISP is disabled!");
834 return VNET_API_ERROR_LISP_DISABLED;
837 vni = gid_address_vni (&a->eid);
838 type = gid_address_type (&a->eid);
839 if (GID_ADDR_IP_PREFIX == type)
840 dp_table = hash_get (lcm->table_id_by_vni, vni);
841 else if (GID_ADDR_MAC == type)
842 dp_table = hash_get (lcm->bd_id_by_vni, vni);
846 clib_warning ("vni %d not associated to a %s!", vni,
847 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
848 return VNET_API_ERROR_INVALID_VALUE;
851 /* store/remove mapping from map-cache */
852 return vnet_lisp_map_cache_add_del (a, map_index_result);
855 static clib_error_t *
856 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
857 vlib_cli_command_t * cmd)
859 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
860 unformat_input_t _line_input, *line_input = &_line_input;
863 gid_address_t *eids = 0;
864 clib_error_t *error = 0;
865 u8 *locator_set_name = 0;
866 u32 locator_set_index = 0, map_index = 0;
868 vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
874 memset (&eid, 0, sizeof (eid));
875 memset (a, 0, sizeof (*a));
877 /* Get a line of input. */
878 if (!unformat_user (input, unformat_line_input, line_input))
881 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
883 if (unformat (line_input, "add"))
885 else if (unformat (line_input, "del"))
887 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
889 else if (unformat (line_input, "vni %d", &vni))
890 gid_address_vni (&eid) = vni;
891 else if (unformat (line_input, "secret-key %_%v%_", &key))
893 else if (unformat (line_input, "key-id %U", unformat_hmac_key_id,
896 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
898 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
901 error = clib_error_return (0, "locator-set %s doesn't exist",
905 locator_set_index = p[0];
909 error = unformat_parse_error (line_input);
913 /* XXX treat batch configuration */
915 if (GID_ADDR_SRC_DST == gid_address_type (&eid))
918 clib_error_return (0, "src/dst is not supported for local EIDs!");
922 if (key && (0 == key_id))
924 vlib_cli_output (vm, "invalid key_id!");
928 gid_address_copy (&a->eid, &eid);
930 a->locator_set_index = locator_set_index;
935 rv = vnet_lisp_add_del_local_mapping (a, &map_index);
938 error = clib_error_return (0, "failed to %s local mapping!",
939 is_add ? "add" : "delete");
943 if (locator_set_name)
944 vec_free (locator_set_name);
945 gid_address_free (&a->eid);
951 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
952 .path = "lisp eid-table",
953 .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
954 "locator-set <locator-set> [key <secret-key> key-id sha1|sha256 ]",
955 .function = lisp_add_del_local_eid_command_fn,
960 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
962 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
963 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
965 if (vnet_lisp_enable_disable_status () == 0)
967 clib_warning ("LISP is disabled!");
971 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
972 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
974 if (!is_l2 && (vni == 0 || dp_id == 0))
976 clib_warning ("can't add/del default vni-vrf mapping!");
980 dp_idp = hash_get (dp_table_by_vni[0], vni);
981 vnip = hash_get (vni_by_dp_table[0], dp_id);
987 clib_warning ("vni %d or vrf %d already used in vrf/vni "
988 "mapping!", vni, dp_id);
991 hash_set (dp_table_by_vni[0], vni, dp_id);
992 hash_set (vni_by_dp_table[0], dp_id, vni);
994 /* create dp iface */
995 dp_add_del_iface (lcm, vni, is_l2, 1);
999 if (!dp_idp || !vnip)
1001 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
1002 "mapping!", vni, dp_id);
1005 hash_unset (dp_table_by_vni[0], vni);
1006 hash_unset (vni_by_dp_table[0], dp_id);
1008 /* remove dp iface */
1009 dp_add_del_iface (lcm, vni, is_l2, 0);
1015 static clib_error_t *
1016 lisp_eid_table_map_command_fn (vlib_main_t * vm,
1017 unformat_input_t * input,
1018 vlib_cli_command_t * cmd)
1020 u8 is_add = 1, is_l2 = 0;
1021 u32 vni = 0, dp_id = 0;
1022 unformat_input_t _line_input, *line_input = &_line_input;
1024 /* Get a line of input. */
1025 if (!unformat_user (input, unformat_line_input, line_input))
1028 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1030 if (unformat (line_input, "del"))
1032 else if (unformat (line_input, "vni %d", &vni))
1034 else if (unformat (line_input, "vrf %d", &dp_id))
1036 else if (unformat (line_input, "bd %d", &dp_id))
1040 return unformat_parse_error (line_input);
1043 vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
1048 VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
1049 .path = "lisp eid-table map",
1050 .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
1051 .function = lisp_eid_table_map_command_fn,
1055 /* return 0 if the two locator sets are identical 1 otherwise */
1057 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
1058 locator_t * new_locators)
1061 locator_t *old_loc, *new_loc;
1063 if (vec_len (old_ls_indexes) != vec_len (new_locators))
1066 for (i = 0; i < vec_len (new_locators); i++)
1068 old_li = vec_elt (old_ls_indexes, i);
1069 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
1071 new_loc = vec_elt_at_index (new_locators, i);
1073 if (locator_cmp (old_loc, new_loc))
1083 gid_address_t *eids_to_be_deleted;
1084 } remove_mapping_args_t;
1087 * Callback invoked when a sub-prefix is found
1090 remove_mapping_if_needed (u32 mi, void *arg)
1093 remove_mapping_args_t *a = arg;
1094 lisp_cp_main_t *lcm = a->lcm;
1098 m = pool_elt_at_index (lcm->mapping_pool, mi);
1102 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1106 if (0 != vec_len (ls->locator_indices))
1111 if (0 == vec_len (ls->locator_indices))
1116 vec_add1 (a->eids_to_be_deleted, m->eid);
1120 * This function searches map cache and looks for IP prefixes that are subset
1121 * of the provided one. If such prefix is found depending on 'is_negative'
1124 * 1) if is_negative is true and found prefix points to positive mapping,
1125 * then the mapping is removed
1126 * 2) if is_negative is false and found prefix points to negative mapping,
1127 * then the mapping is removed
1130 remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1134 remove_mapping_args_t a;
1135 memset (&a, 0, sizeof (a));
1137 /* do this only in src/dst mode ... */
1138 if (MR_MODE_SRC_DST != lcm->map_request_mode)
1141 /* ... and only for IP prefix */
1142 if (GID_ADDR_SRC_DST != gid_address_type (eid)
1143 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1146 a.is_negative = is_negative;
1149 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1150 remove_mapping_if_needed, &a);
1152 vec_foreach (e, a.eids_to_be_deleted)
1153 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
1155 vec_free (a.eids_to_be_deleted);
1159 * Adds/removes/updates mapping. Does not program forwarding.
1161 * @param eid end-host identifier
1162 * @param rlocs vector of remote locators
1163 * @param action action for negative map-reply
1164 * @param is_add add mapping if non-zero, delete otherwise
1165 * @param res_map_index the map-index that was created/updated/removed. It is
1166 * set to ~0 if no action is taken.
1167 * @param is_static used for distinguishing between statically learned
1168 remote mappings and mappings obtained from MR
1169 * @return return code
1172 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
1173 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
1174 u32 * res_map_index)
1176 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1177 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1178 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1179 u32 mi, ls_index = 0, dst_map_index;
1182 if (vnet_lisp_enable_disable_status () == 0)
1184 clib_warning ("LISP is disabled!");
1185 return VNET_API_ERROR_LISP_DISABLED;
1189 res_map_index[0] = ~0;
1191 memset (m_args, 0, sizeof (m_args[0]));
1192 memset (ls_args, 0, sizeof (ls_args[0]));
1194 ls_args->locators = rlocs;
1196 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
1197 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1201 /* overwrite: if mapping already exists, decide if locators should be
1202 * updated and be done */
1203 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
1205 if (!is_static && (old_map->is_static || old_map->local))
1207 /* do not overwrite local or static remote mappings */
1208 clib_warning ("mapping %U rejected due to collision with local "
1209 "or static remote mapping!", format_gid_address,
1214 locator_set_t *old_ls;
1216 /* update mapping attributes */
1217 old_map->action = action;
1218 old_map->authoritative = authoritative;
1221 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1222 old_map->locator_set_index);
1223 if (compare_locators (lcm, old_ls->locator_indices,
1226 /* set locator-set index to overwrite */
1227 ls_args->is_add = 1;
1228 ls_args->index = old_map->locator_set_index;
1229 vnet_lisp_add_del_locator_set (ls_args, 0);
1231 res_map_index[0] = mi;
1237 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1239 ls_args->is_add = 1;
1240 ls_args->index = ~0;
1242 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1245 gid_address_copy (&m_args->eid, eid);
1247 m_args->action = action;
1248 m_args->locator_set_index = ls_index;
1249 m_args->is_static = is_static;
1250 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
1253 res_map_index[0] = dst_map_index;
1258 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
1260 clib_warning ("cannot delete mapping for eid %U",
1261 format_gid_address, eid);
1266 gid_address_copy (&m_args->eid, eid);
1267 m_args->locator_set_index = old_map->locator_set_index;
1269 /* delete mapping associated from map-cache */
1270 vnet_lisp_map_cache_add_del (m_args, 0);
1272 ls_args->is_add = 0;
1273 ls_args->index = old_map->locator_set_index;
1274 /* delete locator set */
1275 vnet_lisp_add_del_locator_set (ls_args, 0);
1277 /* return old mapping index */
1279 res_map_index[0] = mi;
1287 vnet_lisp_clear_all_remote_adjacencies (void)
1290 u32 mi, *map_indices = 0, *map_indexp;
1291 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1292 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1293 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1296 pool_foreach_index (mi, lcm->mapping_pool,
1298 vec_add1 (map_indices, mi);
1302 vec_foreach (map_indexp, map_indices)
1304 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1307 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
1309 dm_args->is_add = 0;
1310 gid_address_copy (&dm_args->eid, &map->eid);
1311 dm_args->locator_set_index = map->locator_set_index;
1313 /* delete mapping associated to fwd entry */
1314 vnet_lisp_map_cache_add_del (dm_args, 0);
1318 ls->index = map->locator_set_index;
1319 /* delete locator set */
1320 rv = vnet_lisp_add_del_locator_set (ls, 0);
1328 vec_free (map_indices);
1333 * Adds adjacency or removes forwarding entry associated to remote mapping.
1334 * Note that adjacencies are not stored, they only result in forwarding entries
1338 lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
1339 gid_address_t * remote_eid, u8 is_add)
1341 u32 local_mi, remote_mi = ~0;
1343 if (vnet_lisp_enable_disable_status () == 0)
1345 clib_warning ("LISP is disabled!");
1346 return VNET_API_ERROR_LISP_DISABLED;
1349 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1350 remote_eid, local_eid);
1351 if (GID_LOOKUP_MISS == remote_mi)
1353 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1354 format_gid_address, remote_eid);
1361 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1364 /* check if source eid has an associated mapping. If pitr mode is on,
1365 * just use the pitr's mapping */
1366 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
1367 gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
1370 if (GID_LOOKUP_MISS == local_mi)
1372 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1373 format_gid_address, local_eid);
1378 /* update forwarding */
1379 dp_add_fwd_entry (lcm, local_mi, remote_mi);
1382 dp_del_fwd_entry (lcm, 0, remote_mi);
1388 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1390 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1391 return lisp_add_del_adjacency (lcm, &a->leid, &a->reid, a->is_add);
1395 * Handler for add/del remote mapping CLI.
1397 * @param vm vlib context
1398 * @param input input from user
1400 * @return pointer to clib error structure
1402 static clib_error_t *
1403 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
1404 unformat_input_t * input,
1405 vlib_cli_command_t * cmd)
1407 clib_error_t *error = 0;
1408 unformat_input_t _line_input, *line_input = &_line_input;
1409 u8 is_add = 1, del_all = 0;
1410 locator_t rloc, *rlocs = 0, *curr_rloc = 0;
1413 u32 vni, action = ~0, p, w;
1416 /* Get a line of input. */
1417 if (!unformat_user (input, unformat_line_input, line_input))
1420 memset (&eid, 0, sizeof (eid));
1421 memset (&rloc, 0, sizeof (rloc));
1423 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1425 if (unformat (line_input, "del-all"))
1427 else if (unformat (line_input, "del"))
1429 else if (unformat (line_input, "add"))
1431 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1433 else if (unformat (line_input, "vni %u", &vni))
1435 gid_address_vni (&eid) = vni;
1437 else if (unformat (line_input, "p %d w %d", &p, &w))
1442 ("No RLOC configured for setting priority/weight!");
1445 curr_rloc->priority = p;
1446 curr_rloc->weight = w;
1448 else if (unformat (line_input, "rloc %U", unformat_ip_address,
1449 &gid_address_ip (&rloc.address)))
1451 /* since rloc is stored in ip prefix we need to set prefix length */
1452 ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
1454 u8 version = gid_address_ip_version (&rloc.address);
1455 ip_prefix_len (pref) = ip_address_max_len (version);
1457 vec_add1 (rlocs, rloc);
1458 curr_rloc = &rlocs[vec_len (rlocs) - 1];
1460 else if (unformat (line_input, "action %U",
1461 unformat_negative_mapping_action, &action))
1465 clib_warning ("parse error");
1472 clib_warning ("missing eid!");
1478 if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1480 clib_warning ("no action set for negative map-reply!");
1486 vnet_lisp_clear_all_remote_adjacencies ();
1490 /* TODO build src/dst with seid */
1492 /* if it's a delete, clean forwarding */
1495 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1496 rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1503 /* add as static remote mapping, i.e., not authoritative and infinite
1505 rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
1506 1 /* is_static */ , 0);
1509 clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
1513 unformat_free (line_input);
1517 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) =
1519 .path = "lisp remote-mapping",.short_help =
1520 "lisp remote-mapping add|del [del-all] vni <vni> "
1521 "eid <est-eid> [action <no-action|natively-forward|"
1522 "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
1523 "[rloc <dst-locator> ... ]",.function =
1524 lisp_add_del_remote_mapping_command_fn,};
1527 * Handler for add/del adjacency CLI.
1529 static clib_error_t *
1530 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
1531 vlib_cli_command_t * cmd)
1533 clib_error_t *error = 0;
1534 unformat_input_t _line_input, *line_input = &_line_input;
1535 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1537 ip_prefix_t *reid_ippref, *leid_ippref;
1538 gid_address_t leid, reid;
1539 u8 *dmac = gid_address_mac (&reid);
1540 u8 *smac = gid_address_mac (&leid);
1541 u8 reid_set = 0, leid_set = 0;
1545 /* Get a line of input. */
1546 if (!unformat_user (input, unformat_line_input, line_input))
1549 memset (&reid, 0, sizeof (reid));
1550 memset (&leid, 0, sizeof (leid));
1552 leid_ippref = &gid_address_ippref (&leid);
1553 reid_ippref = &gid_address_ippref (&reid);
1555 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1557 if (unformat (line_input, "del"))
1559 else if (unformat (line_input, "add"))
1561 else if (unformat (line_input, "reid %U",
1562 unformat_ip_prefix, reid_ippref))
1564 gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
1567 else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
1569 gid_address_type (&reid) = GID_ADDR_MAC;
1572 else if (unformat (line_input, "vni %u", &vni))
1574 gid_address_vni (&leid) = vni;
1575 gid_address_vni (&reid) = vni;
1577 else if (unformat (line_input, "leid %U",
1578 unformat_ip_prefix, leid_ippref))
1580 gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
1583 else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
1585 gid_address_type (&leid) = GID_ADDR_MAC;
1590 clib_warning ("parse error");
1595 if (!reid_set || !leid_set)
1597 clib_warning ("missing remote or local eid!");
1601 if ((gid_address_type (&leid) != gid_address_type (&reid))
1602 || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
1603 && ip_prefix_version (reid_ippref)
1604 != ip_prefix_version (leid_ippref)))
1606 clib_warning ("remote and local EIDs are of different types!");
1610 memset (a, 0, sizeof (a[0]));
1611 gid_address_copy (&a->leid, &leid);
1612 gid_address_copy (&a->reid, &reid);
1615 rv = vnet_lisp_add_del_adjacency (a);
1618 clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
1621 unformat_free (line_input);
1626 VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1627 .path = "lisp adjacency",
1628 .short_help = "lisp adjacency add|del vni <vni> reid <remote-eid> "
1630 .function = lisp_add_del_adjacency_command_fn,
1635 vnet_lisp_set_map_request_mode (u8 mode)
1637 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1639 if (vnet_lisp_enable_disable_status () == 0)
1641 clib_warning ("LISP is disabled!");
1642 return VNET_API_ERROR_LISP_DISABLED;
1645 if (mode >= _MR_MODE_MAX)
1647 clib_warning ("Invalid LISP map request mode %d!", mode);
1648 return VNET_API_ERROR_INVALID_ARGUMENT;
1651 lcm->map_request_mode = mode;
1655 static clib_error_t *
1656 lisp_map_request_mode_command_fn (vlib_main_t * vm,
1657 unformat_input_t * input,
1658 vlib_cli_command_t * cmd)
1660 unformat_input_t _i, *i = &_i;
1661 map_request_mode_t mr_mode = _MR_MODE_MAX;
1663 /* Get a line of input. */
1664 if (!unformat_user (input, unformat_line_input, i))
1667 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1669 if (unformat (i, "dst-only"))
1670 mr_mode = MR_MODE_DST_ONLY;
1671 else if (unformat (i, "src-dst"))
1672 mr_mode = MR_MODE_SRC_DST;
1675 clib_warning ("parse error '%U'", format_unformat_error, i);
1680 if (_MR_MODE_MAX == mr_mode)
1682 clib_warning ("No LISP map request mode entered!");
1686 vnet_lisp_set_map_request_mode (mr_mode);
1692 VLIB_CLI_COMMAND (lisp_map_request_mode_command) = {
1693 .path = "lisp map-request mode",
1694 .short_help = "lisp map-request mode dst-only|src-dst",
1695 .function = lisp_map_request_mode_command_fn,
1700 format_lisp_map_request_mode (u8 * s, va_list * args)
1702 u32 mode = va_arg (*args, u32);
1707 return format (0, "dst-only");
1709 return format (0, "src-dst");
1714 static clib_error_t *
1715 lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
1716 unformat_input_t * input,
1717 vlib_cli_command_t * cmd)
1719 vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
1720 vnet_lisp_get_map_request_mode ());
1725 VLIB_CLI_COMMAND (lisp_show_map_request_mode_command) = {
1726 .path = "show lisp map-request mode",
1727 .short_help = "show lisp map-request mode",
1728 .function = lisp_show_map_request_mode_command_fn,
1732 static clib_error_t *
1733 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
1734 unformat_input_t * input,
1735 vlib_cli_command_t * cmd)
1738 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1740 vec_foreach (mr, lcm->map_resolvers)
1742 vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
1748 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1749 .path = "show lisp map-resolvers",
1750 .short_help = "show lisp map-resolvers",
1751 .function = lisp_show_map_resolvers_command_fn,
1756 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1758 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1759 u32 locator_set_index = ~0;
1763 if (vnet_lisp_enable_disable_status () == 0)
1765 clib_warning ("LISP is disabled!");
1766 return VNET_API_ERROR_LISP_DISABLED;
1769 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1772 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1775 locator_set_index = p[0];
1779 pool_get (lcm->mapping_pool, m);
1780 m->locator_set_index = locator_set_index;
1782 lcm->pitr_map_index = m - lcm->mapping_pool;
1784 /* enable pitr mode */
1789 /* remove pitr mapping */
1790 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1792 /* disable pitr mode */
1798 static clib_error_t *
1799 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
1800 unformat_input_t * input,
1801 vlib_cli_command_t * cmd)
1803 u8 locator_name_set = 0;
1804 u8 *locator_set_name = 0;
1806 unformat_input_t _line_input, *line_input = &_line_input;
1807 clib_error_t *error = 0;
1810 /* Get a line of input. */
1811 if (!unformat_user (input, unformat_line_input, line_input))
1814 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1816 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
1817 locator_name_set = 1;
1818 else if (unformat (line_input, "disable"))
1821 return clib_error_return (0, "parse error");
1824 if (!locator_name_set)
1826 clib_warning ("No locator set specified!");
1829 rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1832 error = clib_error_return (0, "failed to %s pitr!",
1833 is_add ? "add" : "delete");
1837 if (locator_set_name)
1838 vec_free (locator_set_name);
1843 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1844 .path = "lisp pitr",
1845 .short_help = "lisp pitr [disable] ls <locator-set-name>",
1846 .function = lisp_pitr_set_locator_set_command_fn,
1850 static clib_error_t *
1851 lisp_show_pitr_command_fn (vlib_main_t * vm,
1852 unformat_input_t * input, vlib_cli_command_t * cmd)
1854 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1859 vlib_cli_output (vm, "%=20s%=16s",
1860 "pitr", lcm->lisp_pitr ? "locator-set" : "");
1862 if (!lcm->lisp_pitr)
1864 vlib_cli_output (vm, "%=20s", "disable");
1868 if (~0 == lcm->pitr_map_index)
1870 tmp_str = format (0, "N/A");
1874 m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1875 if (~0 != m->locator_set_index)
1878 pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1879 tmp_str = format (0, "%s", ls->name);
1883 tmp_str = format (0, "N/A");
1886 vec_add1 (tmp_str, 0);
1888 vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1896 VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1897 .path = "show lisp pitr",
1898 .short_help = "Show pitr",
1899 .function = lisp_show_pitr_command_fn,
1904 format_eid_entry (u8 * s, va_list * args)
1906 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
1907 lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
1908 mapping_t *mapit = va_arg (*args, mapping_t *);
1909 locator_set_t *ls = va_arg (*args, locator_set_t *);
1910 gid_address_t *gid = &mapit->eid;
1911 u32 ttl = mapit->ttl;
1912 u8 aut = mapit->authoritative;
1917 u8 *type = ls->local ? format (0, "local(%s)", ls->name)
1918 : format (0, "remote");
1920 if (vec_len (ls->locator_indices) == 0)
1922 s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
1927 vec_foreach (loc_index, ls->locator_indices)
1929 locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1931 loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1934 loc = format (0, "%U", format_ip_address,
1935 &gid_address_ip (&l->address));
1939 s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
1940 gid, type, loc, ttl, aut);
1944 s = format (s, "%55s%v\n", "", loc);
1950 static clib_error_t *
1951 lisp_show_eid_table_command_fn (vlib_main_t * vm,
1952 unformat_input_t * input,
1953 vlib_cli_command_t * cmd)
1955 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1957 unformat_input_t _line_input, *line_input = &_line_input;
1963 memset (&eid, 0, sizeof (eid));
1965 /* Get a line of input. */
1966 if (!unformat_user (input, unformat_line_input, line_input))
1969 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1971 if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1973 else if (unformat (line_input, "local"))
1975 else if (unformat (line_input, "remote"))
1978 return clib_error_return (0, "parse error: '%U'",
1979 format_unformat_error, line_input);
1982 vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
1983 "EID", "type", "locators", "ttl", "autoritative");
1988 pool_foreach (mapit, lcm->mapping_pool,
1990 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
1991 mapit->locator_set_index);
1992 if (filter && !((1 == filter && ls->local) ||
1993 (2 == filter && !ls->local)))
1997 vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
2004 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
2005 if ((u32) ~ 0 == mi)
2008 mapit = pool_elt_at_index (lcm->mapping_pool, mi);
2009 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2010 mapit->locator_set_index);
2012 if (filter && !((1 == filter && ls->local) ||
2013 (2 == filter && !ls->local)))
2018 vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
2026 VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
2027 .path = "show lisp eid-table",
2028 .short_help = "Shows EID table",
2029 .function = lisp_show_eid_table_command_fn,
2033 /* cleans locator to locator-set data and removes locators not part of
2034 * any locator-set */
2036 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
2038 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
2039 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
2040 for (i = 0; i < vec_len (ls->locator_indices); i++)
2042 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
2043 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2045 for (j = 0; j < vec_len (ls_indexes[0]); j++)
2047 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
2048 if (ls_indexp[0] == lsi)
2052 /* delete index for removed locator-set */
2053 vec_del1 (ls_indexes[0], j);
2055 /* delete locator if it's part of no locator-set */
2056 if (vec_len (ls_indexes[0]) == 0)
2058 pool_put_index (lcm->locator_pool, loc_indexp[0]);
2059 vec_add1 (to_be_deleted, i);
2065 for (i = 0; i < vec_len (to_be_deleted); i++)
2067 loc_indexp = vec_elt_at_index (to_be_deleted, i);
2068 vec_del1 (ls->locator_indices, loc_indexp[0]);
2070 vec_free (to_be_deleted);
2074 static inline uword *
2075 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
2077 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2082 /* find locator-set */
2085 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
2096 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
2102 ASSERT (ls != NULL);
2103 ASSERT (loc != NULL);
2105 vec_foreach (locit, ls->locator_indices)
2107 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2108 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
2109 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
2111 clib_warning ("Duplicate locator");
2112 return VNET_API_ERROR_VALUE_EXIST;
2120 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
2121 u32 ls_index, u32 loc_id)
2123 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2124 u32 **ls_indexes = NULL;
2126 ASSERT (ls != NULL);
2127 ASSERT (locit != NULL);
2129 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
2130 pool_put_index (lcm->locator_pool, locit[0]);
2131 vec_del1 (ls->locator_indices, loc_id);
2132 vec_del1 (ls_indexes[0], ls_index);
2136 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
2137 locator_set_t * ls, u32 * ls_result)
2139 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2140 locator_t *loc = NULL, *itloc = NULL;
2141 uword _p = (u32) ~ 0, *p = &_p;
2142 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
2148 if (vnet_lisp_enable_disable_status () == 0)
2150 clib_warning ("LISP is disabled!");
2151 return VNET_API_ERROR_LISP_DISABLED;
2154 p = get_locator_set_index (a, p);
2157 clib_warning ("locator-set %v doesn't exist", a->name);
2158 return VNET_API_ERROR_INVALID_ARGUMENT;
2163 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2166 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2168 return VNET_API_ERROR_INVALID_ARGUMENT;
2175 ls_result[0] = p[0];
2177 /* allocate locators */
2178 vec_foreach (itloc, a->locators)
2180 ret = is_locator_in_locator_set (lcm, ls, itloc);
2186 pool_get (lcm->locator_pool, loc);
2188 loc_index = loc - lcm->locator_pool;
2190 vec_add1 (ls->locator_indices, loc_index);
2192 vec_validate (lcm->locator_to_locator_sets, loc_index);
2193 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2195 vec_add1 (ls_indexes[0], p[0]);
2202 itloc = a->locators;
2204 vec_foreach (locit, ls->locator_indices)
2206 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2208 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
2210 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2212 if (0 == loc->local &&
2213 !gid_address_cmp (&loc->address, &itloc->address))
2215 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2226 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
2229 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2231 uword _p = (u32) ~ 0, *p = &_p;
2236 if (vnet_lisp_enable_disable_status () == 0)
2238 clib_warning ("LISP is disabled!");
2239 return VNET_API_ERROR_LISP_DISABLED;
2244 p = get_locator_set_index (a, p);
2247 if (p && p[0] != (u32) ~ 0)
2249 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2252 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2257 /* clean locator to locator-set vectors and remove locators if
2258 * they're not part of another locator-set */
2259 clean_locator_to_locator_set (lcm, p[0]);
2261 /* remove locator indices from locator set */
2262 vec_free (ls->locator_indices);
2267 ls_result[0] = p[0];
2269 /* new locator-set */
2272 pool_get (lcm->locator_set_pool, ls);
2273 memset (ls, 0, sizeof (*ls));
2274 ls_index = ls - lcm->locator_set_pool;
2278 ls->name = vec_dup (a->name);
2280 if (!lcm->locator_set_index_by_name)
2281 lcm->locator_set_index_by_name = hash_create_vec (
2289 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
2292 /* mark as local locator-set */
2293 vec_add1 (lcm->local_locator_set_indexes, ls_index);
2295 ls->local = a->local;
2297 ls_result[0] = ls_index;
2300 ret = vnet_lisp_add_del_locator (a, ls, NULL);
2308 p = get_locator_set_index (a, p);
2311 clib_warning ("locator-set %v doesn't exists", a->name);
2315 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2318 clib_warning ("locator-set with index %d doesn't exists", p[0]);
2322 if (lcm->mreq_itr_rlocs == p[0])
2324 clib_warning ("Can't delete the locator-set used to constrain "
2325 "the itr-rlocs in map-requests!");
2329 if (vec_len (lcm->locator_set_to_eids) != 0)
2331 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
2332 if (vec_len (eid_indexes[0]) != 0)
2335 ("Can't delete a locator that supports a mapping!");
2340 /* clean locator to locator-sets data */
2341 clean_locator_to_locator_set (lcm, p[0]);
2347 vec_foreach_index (it, lcm->local_locator_set_indexes)
2349 lsi = vec_elt (lcm->local_locator_set_indexes, it);
2352 vec_del1 (lcm->local_locator_set_indexes, it);
2356 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2358 vec_free (ls->name);
2359 vec_free (ls->locator_indices);
2360 pool_put (lcm->locator_set_pool, ls);
2366 vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
2368 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2370 lcm->rloc_probing = is_enable;
2375 vnet_lisp_map_register_enable_disable (u8 is_enable)
2377 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2379 lcm->map_registering = is_enable;
2384 vnet_lisp_enable_disable (u8 is_enable)
2387 clib_error_t *error = 0;
2388 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2389 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
2391 a->is_en = is_enable;
2392 error = vnet_lisp_gpe_enable_disable (a);
2395 return clib_error_return (0, "failed to %s data-plane!",
2396 a->is_en ? "enable" : "disable");
2401 /* enable all l2 and l3 ifaces */
2404 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2405 dp_add_del_iface(lcm, vni, 0, 1);
2407 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2408 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
2414 /* clear interface table */
2415 hash_free (lcm->fwd_entry_by_mapping_index);
2416 pool_free (lcm->fwd_entry_pool);
2419 /* update global flag */
2420 lcm->is_enabled = is_enable;
2425 static clib_error_t *
2426 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
2427 vlib_cli_command_t * cmd)
2429 unformat_input_t _line_input, *line_input = &_line_input;
2433 /* Get a line of input. */
2434 if (!unformat_user (input, unformat_line_input, line_input))
2437 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2439 if (unformat (line_input, "enable"))
2444 else if (unformat (line_input, "disable"))
2448 return clib_error_return (0, "parse error: '%U'",
2449 format_unformat_error, line_input);
2454 return clib_error_return (0, "state not set");
2456 vnet_lisp_enable_disable (is_enabled);
2461 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
2463 .short_help = "lisp [enable|disable]",
2464 .function = lisp_enable_disable_command_fn,
2468 static clib_error_t *
2469 lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
2470 unformat_input_t * input,
2471 vlib_cli_command_t * cmd)
2473 unformat_input_t _line_input, *line_input = &_line_input;
2477 /* Get a line of input. */
2478 if (!unformat_user (input, unformat_line_input, line_input))
2481 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2483 if (unformat (line_input, "enable"))
2488 else if (unformat (line_input, "disable"))
2492 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2500 vlib_cli_output (vm, "state not set!");
2504 vnet_lisp_map_register_enable_disable (is_enabled);
2509 VLIB_CLI_COMMAND (lisp_map_register_enable_disable_command) = {
2510 .path = "lisp map-register",
2511 .short_help = "lisp map-register [enable|disable]",
2512 .function = lisp_map_register_enable_disable_command_fn,
2516 static clib_error_t *
2517 lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
2518 unformat_input_t * input,
2519 vlib_cli_command_t * cmd)
2521 unformat_input_t _line_input, *line_input = &_line_input;
2525 /* Get a line of input. */
2526 if (!unformat_user (input, unformat_line_input, line_input))
2529 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2531 if (unformat (line_input, "enable"))
2536 else if (unformat (line_input, "disable"))
2540 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2548 vlib_cli_output (vm, "state not set!");
2552 vnet_lisp_rloc_probe_enable_disable (is_enabled);
2557 VLIB_CLI_COMMAND (lisp_rloc_probe_enable_disable_command) = {
2558 .path = "lisp rloc-probe",
2559 .short_help = "lisp rloc-probe [enable|disable]",
2560 .function = lisp_rloc_probe_enable_disable_command_fn,
2565 vnet_lisp_enable_disable_status (void)
2567 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2568 return lcm->is_enabled;
2572 format_lisp_status (u8 * s, va_list * args)
2574 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2575 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
2578 static clib_error_t *
2579 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
2580 vlib_cli_command_t * cmd)
2583 msg = format (msg, "feature: %U\ngpe: %U\n",
2584 format_lisp_status, format_vnet_lisp_gpe_status);
2585 vlib_cli_output (vm, "%v", msg);
2591 VLIB_CLI_COMMAND (lisp_show_status_command) = {
2592 .path = "show lisp status",
2593 .short_help = "show lisp status",
2594 .function = lisp_show_status_command_fn,
2598 static clib_error_t *
2599 lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
2600 unformat_input_t * input,
2601 vlib_cli_command_t * cmd)
2604 unformat_input_t _line_input, *line_input = &_line_input;
2605 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2606 uword *vni_table = 0;
2609 /* Get a line of input. */
2610 if (!unformat_user (input, unformat_line_input, line_input))
2613 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2615 if (unformat (line_input, "l2"))
2617 vni_table = lcm->bd_id_by_vni;
2620 else if (unformat (line_input, "l3"))
2622 vni_table = lcm->table_id_by_vni;
2626 return clib_error_return (0, "parse error: '%U'",
2627 format_unformat_error, line_input);
2632 vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
2636 vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
2639 hash_foreach_pair (p, vni_table,
2641 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
2649 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
2650 .path = "show lisp eid-table map",
2651 .short_help = "show lisp eid-table l2|l3",
2652 .function = lisp_show_eid_table_map_command_fn,
2656 static clib_error_t *
2657 lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
2658 unformat_input_t * input,
2659 vlib_cli_command_t * cmd)
2661 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2662 vnet_main_t *vnm = lgm->vnet_main;
2663 unformat_input_t _line_input, *line_input = &_line_input;
2665 clib_error_t *error = 0;
2666 u8 *locator_set_name = 0;
2667 locator_t locator, *locators = 0;
2668 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2672 memset (&locator, 0, sizeof (locator));
2673 memset (a, 0, sizeof (a[0]));
2675 /* Get a line of input. */
2676 if (!unformat_user (input, unformat_line_input, line_input))
2679 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2681 if (unformat (line_input, "add %_%v%_", &locator_set_name))
2683 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
2685 else if (unformat (line_input, "iface %U p %d w %d",
2686 unformat_vnet_sw_interface, vnm,
2687 &locator.sw_if_index, &locator.priority,
2691 vec_add1 (locators, locator);
2695 error = unformat_parse_error (line_input);
2700 a->name = locator_set_name;
2701 a->locators = locators;
2705 rv = vnet_lisp_add_del_locator_set (a, &ls_index);
2708 error = clib_error_return (0, "failed to %s locator-set!",
2709 is_add ? "add" : "delete");
2713 vec_free (locators);
2714 if (locator_set_name)
2715 vec_free (locator_set_name);
2720 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2721 .path = "lisp locator-set",
2722 .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2723 "p <priority> w <weight>]",
2724 .function = lisp_add_del_locator_set_command_fn,
2728 static clib_error_t *
2729 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
2730 unformat_input_t * input,
2731 vlib_cli_command_t * cmd)
2733 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2734 vnet_main_t *vnm = lgm->vnet_main;
2735 unformat_input_t _line_input, *line_input = &_line_input;
2737 clib_error_t *error = 0;
2738 u8 *locator_set_name = 0;
2739 u8 locator_set_name_set = 0;
2740 locator_t locator, *locators = 0;
2741 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2744 memset (&locator, 0, sizeof (locator));
2745 memset (a, 0, sizeof (a[0]));
2747 /* Get a line of input. */
2748 if (!unformat_user (input, unformat_line_input, line_input))
2751 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2753 if (unformat (line_input, "add"))
2755 else if (unformat (line_input, "del"))
2757 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
2758 locator_set_name_set = 1;
2759 else if (unformat (line_input, "iface %U p %d w %d",
2760 unformat_vnet_sw_interface, vnm,
2761 &locator.sw_if_index, &locator.priority,
2765 vec_add1 (locators, locator);
2769 error = unformat_parse_error (line_input);
2774 if (!locator_set_name_set)
2776 error = clib_error_return (0, "locator_set name not set!");
2780 a->name = locator_set_name;
2781 a->locators = locators;
2785 vnet_lisp_add_del_locator (a, 0, &ls_index);
2788 vec_free (locators);
2789 vec_free (locator_set_name);
2794 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2795 .path = "lisp locator",
2796 .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2797 "p <priority> w <weight>",
2798 .function = lisp_add_del_locator_in_set_command_fn,
2802 static clib_error_t *
2803 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
2804 unformat_input_t * input,
2805 vlib_cli_command_t * cmd)
2807 locator_set_t *lsit;
2810 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2812 vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
2813 "Priority", "Weight");
2816 pool_foreach (lsit, lcm->locator_set_pool,
2822 msg = format (msg, "%v", lsit->name);
2826 msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
2828 vec_foreach (locit, lsit->locator_indices)
2832 msg = format (msg, "%16s", " ");
2834 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2836 msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
2839 msg = format (msg, "%16U%16d%16d\n", format_ip_address,
2840 &gid_address_ip(&loc->address), loc->priority,
2844 vlib_cli_output (vm, "%v", msg);
2852 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2853 .path = "show lisp locator-set",
2854 .short_help = "Shows locator-sets",
2855 .function = lisp_cp_show_locator_sets_command_fn,
2860 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2862 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2864 lisp_msmr_t _mr, *mr = &_mr;
2866 if (vnet_lisp_enable_disable_status () == 0)
2868 clib_warning ("LISP is disabled!");
2869 return VNET_API_ERROR_LISP_DISABLED;
2875 if (get_map_resolver (&a->address))
2877 clib_warning ("map-resolver %U already exists!", format_ip_address,
2882 memset (mr, 0, sizeof (*mr));
2883 ip_address_copy (&mr->address, &a->address);
2884 vec_add1 (lcm->map_resolvers, *mr);
2886 if (vec_len (lcm->map_resolvers) == 1)
2887 lcm->do_map_resolver_election = 1;
2891 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2893 mr = vec_elt_at_index (lcm->map_resolvers, i);
2894 if (!ip_address_cmp (&mr->address, &a->address))
2896 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2897 lcm->do_map_resolver_election = 1;
2899 vec_del1 (lcm->map_resolvers, i);
2907 static clib_error_t *
2908 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
2909 unformat_input_t * input,
2910 vlib_cli_command_t * cmd)
2912 unformat_input_t _line_input, *line_input = &_line_input;
2913 u8 is_add = 1, addr_set = 0;
2914 ip_address_t ip_addr;
2915 clib_error_t *error = 0;
2917 vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
2919 /* Get a line of input. */
2920 if (!unformat_user (input, unformat_line_input, line_input))
2923 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2925 if (unformat (line_input, "add"))
2927 else if (unformat (line_input, "del"))
2929 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
2933 error = unformat_parse_error (line_input);
2940 error = clib_error_return (0, "Map-resolver address must be set!");
2945 a->address = ip_addr;
2946 rv = vnet_lisp_add_del_map_resolver (a);
2949 error = clib_error_return (0, "failed to %s map-resolver!",
2950 is_add ? "add" : "delete");
2958 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
2959 .path = "lisp map-resolver",
2960 .short_help = "lisp map-resolver add/del <ip_address>",
2961 .function = lisp_add_del_map_resolver_command_fn,
2966 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2968 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2971 if (vnet_lisp_enable_disable_status () == 0)
2973 clib_warning ("LISP is disabled!");
2974 return VNET_API_ERROR_LISP_DISABLED;
2979 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
2982 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
2983 return VNET_API_ERROR_INVALID_ARGUMENT;
2986 lcm->mreq_itr_rlocs = p[0];
2990 lcm->mreq_itr_rlocs = ~0;
2996 static clib_error_t *
2997 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
2998 unformat_input_t * input,
2999 vlib_cli_command_t * cmd)
3001 unformat_input_t _line_input, *line_input = &_line_input;
3003 u8 *locator_set_name = 0;
3004 clib_error_t *error = 0;
3006 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
3008 /* Get a line of input. */
3009 if (!unformat_user (input, unformat_line_input, line_input))
3012 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3014 if (unformat (line_input, "del"))
3016 else if (unformat (line_input, "add %_%v%_", &locator_set_name))
3020 error = unformat_parse_error (line_input);
3026 a->locator_set_name = locator_set_name;
3027 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
3030 error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
3031 is_add ? "add" : "delete");
3034 vec_free (locator_set_name);
3042 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
3043 .path = "lisp map-request itr-rlocs",
3044 .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
3045 .function = lisp_add_del_mreq_itr_rlocs_command_fn,
3049 static clib_error_t *
3050 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
3051 unformat_input_t * input,
3052 vlib_cli_command_t * cmd)
3054 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3055 locator_set_t *loc_set;
3057 vlib_cli_output (vm, "%=20s", "itr-rlocs");
3059 if (~0 == lcm->mreq_itr_rlocs)
3064 loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
3066 vlib_cli_output (vm, "%=20s", loc_set->name);
3072 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
3073 .path = "show lisp map-request itr-rlocs",
3074 .short_help = "Shows map-request itr-rlocs",
3075 .function = lisp_show_mreq_itr_rlocs_command_fn,
3079 /* Statistics (not really errors) */
3080 #define foreach_lisp_cp_lookup_error \
3082 _(MAP_REQUESTS_SENT, "map-request sent")
3084 static char *lisp_cp_lookup_error_strings[] = {
3085 #define _(sym,string) string,
3086 foreach_lisp_cp_lookup_error
3092 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
3093 foreach_lisp_cp_lookup_error
3095 LISP_CP_LOOKUP_N_ERROR,
3096 } lisp_cp_lookup_error_t;
3100 LISP_CP_LOOKUP_NEXT_DROP,
3101 LISP_CP_LOOKUP_N_NEXT,
3102 } lisp_cp_lookup_next_t;
3106 gid_address_t dst_eid;
3107 ip_address_t map_resolver_ip;
3108 } lisp_cp_lookup_trace_t;
3111 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
3113 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3114 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3115 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
3117 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
3118 format_ip_address, &t->map_resolver_ip, format_gid_address,
3124 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
3125 ip_address_t * sloc)
3130 if (vec_len (lcm->map_resolvers) == 0)
3132 clib_warning ("No map-resolver configured");
3136 /* find the first mr ip we have a route to and the ip of the
3137 * iface that has a route to it */
3138 vec_foreach (mrit, lcm->map_resolvers)
3141 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
3143 ip_address_copy (mr_ip, a);
3145 /* also update globals */
3150 clib_warning ("Can't find map-resolver and local interface ip!");
3154 static gid_address_t *
3155 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
3161 ip_interface_address_t *ia = 0;
3162 gid_address_t gid_data, *gid = &gid_data;
3163 gid_address_t *rlocs = 0;
3164 ip_prefix_t *ippref = &gid_address_ippref (gid);
3165 ip_address_t *rloc = &ip_prefix_addr (ippref);
3167 memset (gid, 0, sizeof (gid[0]));
3168 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
3169 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
3171 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
3172 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
3174 /* Add ipv4 locators first TODO sort them */
3177 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3178 loc->sw_if_index, 1 /* unnumbered */,
3180 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
3181 ip_address_set (rloc, addr, IP4);
3182 ip_prefix_len (ippref) = 32;
3183 ip_prefix_normalize (ippref);
3184 vec_add1 (rlocs, gid[0]);
3187 /* Add ipv6 locators */
3188 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3189 loc->sw_if_index, 1 /* unnumbered */,
3191 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
3192 ip_address_set (rloc, addr, IP6);
3193 ip_prefix_len (ippref) = 128;
3194 ip_prefix_normalize (ippref);
3195 vec_add1 (rlocs, gid[0]);
3203 static vlib_buffer_t *
3204 build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
3205 ip_address_t * sloc, ip_address_t * rloc,
3206 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
3210 vlib_main_t *vm = lcm->vlib_main;
3212 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3214 clib_warning ("Can't allocate buffer for Map-Request!");
3218 b = vlib_get_buffer (vm, bi);
3220 /* leave some space for the encap headers */
3221 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3224 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
3225 1 /* rloc probe */ , nonce_res);
3227 /* push outer ip header */
3228 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3236 static vlib_buffer_t *
3237 build_encapsulated_map_request (lisp_cp_main_t * lcm,
3238 gid_address_t * seid, gid_address_t * deid,
3239 locator_set_t * loc_set, ip_address_t * mr_ip,
3240 ip_address_t * sloc, u8 is_smr_invoked,
3241 u64 * nonce_res, u32 * bi_res)
3245 gid_address_t *rlocs = 0;
3246 vlib_main_t *vm = lcm->vlib_main;
3248 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3250 clib_warning ("Can't allocate buffer for Map-Request!");
3254 b = vlib_get_buffer (vm, bi);
3256 /* leave some space for the encap headers */
3257 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3260 rlocs = build_itr_rloc_list (lcm, loc_set);
3262 if (MR_MODE_SRC_DST == lcm->map_request_mode
3263 && GID_ADDR_SRC_DST != gid_address_type (deid))
3266 memset (&sd, 0, sizeof (sd));
3267 build_src_dst (&sd, seid, deid);
3268 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
3269 0 /* rloc probe */ , nonce_res);
3274 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
3275 0 /* rloc probe */ , nonce_res);
3278 /* push ecm: udp-ip-lisp */
3279 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
3281 /* push outer ip header */
3282 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3292 reset_pending_mr_counters (pending_map_request_t * r)
3294 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3299 elect_map_resolver (lisp_cp_main_t * lcm)
3303 vec_foreach (mr, lcm->map_resolvers)
3307 ip_address_copy (&lcm->active_map_resolver, &mr->address);
3308 lcm->do_map_resolver_election = 0;
3316 free_map_register_records (mapping_t * maps)
3319 vec_foreach (map, maps) vec_free (map->locators);
3325 add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
3326 ip_address_t * probed_loc)
3329 locator_t *loc, new;
3330 ip_interface_address_t *ia = 0;
3332 ip_address_t *new_ip = &gid_address_ip (&new.address);
3335 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
3337 vec_foreach (li, ls->locator_indices)
3339 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
3344 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3345 loc->sw_if_index, 1 /* unnumbered */,
3347 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
3349 ip_address_set (new_ip, addr, IP4);
3352 /* Add ipv6 locators */
3353 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3354 loc->sw_if_index, 1 /* unnumbered */,
3356 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
3358 ip_address_set (new_ip, addr, IP6);
3362 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
3365 vec_add1 (m->locators, new);
3370 build_map_register_record_list (lisp_cp_main_t * lcm)
3372 mapping_t *recs = 0, rec, *m;
3375 pool_foreach(m, lcm->mapping_pool,
3377 /* for now build only local mappings */
3382 add_locators (lcm, &rec, m->locator_set_index, NULL);
3383 vec_add1 (recs, rec);
3391 update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
3392 lisp_key_type_t key_id, u8 * key,
3393 u16 auth_data_len, u32 msg_len)
3395 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
3396 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
3398 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3399 (unsigned char *) map_reg_hdr, msg_len, NULL,
3401 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
3406 static vlib_buffer_t *
3407 build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
3408 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
3409 mapping_t * records, lisp_key_type_t key_id, u8 * key,
3414 u32 bi, auth_data_len = 0, msg_len = 0;
3415 vlib_main_t *vm = lcm->vlib_main;
3417 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3419 clib_warning ("Can't allocate buffer for Map-Register!");
3423 b = vlib_get_buffer (vm, bi);
3425 /* leave some space for the encap headers */
3426 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3428 auth_data_len = auth_data_len_by_key_id (key_id);
3429 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
3430 auth_data_len, nonce_res,
3433 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
3436 /* push outer ip header */
3437 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3445 get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
3448 while (lcm->do_map_resolver_election
3449 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
3450 &lcm->active_map_resolver,
3453 if (0 == elect_map_resolver (lcm))
3454 /* all map resolvers are down */
3456 /* restart MR checking by marking all of them up */
3457 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
3465 send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
3466 u32 local_locator_set_index, ip_address_t * sloc,
3467 ip_address_t * rloc)
3474 u32 next_index, *to_next;
3475 gid_address_t *itr_rlocs;
3477 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
3478 itr_rlocs = build_itr_rloc_list (lcm, ls);
3480 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
3481 vec_free (itr_rlocs);
3485 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3487 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3488 ip4_lookup_node.index : ip6_lookup_node.index;
3490 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3492 /* Enqueue the packet */
3493 to_next = vlib_frame_vector_args (f);
3496 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3498 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3503 send_rloc_probes (lisp_cp_main_t * lcm)
3510 pool_foreach (e, lcm->fwd_entry_pool,
3512 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
3515 clib_warning ("internal error: cannot find local eid %U in "
3516 "map-cache!", format_gid_address, &e->leid);
3519 lm = pool_elt_at_index (lcm->mapping_pool, si);
3521 if (vec_len (e->locator_pairs) == 0)
3524 /* TODO send rloc-probe for each pair? for now assume there is only one
3526 locator_pair_t *lp = &e->locator_pairs[0];
3528 /* get first remote locator */
3529 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
3538 send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
3545 u32 next_index, *to_next;
3546 ip_address_t *ms = 0;
3548 // TODO: support multiple map servers and do election
3549 if (0 == vec_len (lcm->map_servers))
3552 ms = &lcm->map_servers[0].address;
3554 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
3556 clib_warning ("no eligible interface address found for %U!",
3557 format_ip_address, &lcm->map_servers[0]);
3561 /* TODO build mapping records grouped by secret key and send one map-register
3562 per group. Currently only one secret key per vpp is supported */
3563 mapping_t *records = build_map_register_record_list (lcm);
3567 u8 *key = records[0].key;
3568 u8 key_id = records[0].key_id;
3571 return 0; /* no secret key -> map-register cannot be sent */
3573 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, records,
3577 free_map_register_records (records);
3579 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3581 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3582 ip4_lookup_node.index : ip6_lookup_node.index;
3584 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3586 /* Enqueue the packet */
3587 to_next = vlib_frame_vector_args (f);
3590 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3592 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3596 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
3597 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
3599 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
3600 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
3603 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
3604 gid_address_t * seid, gid_address_t * deid,
3605 u8 is_smr_invoked, u8 is_resend)
3607 u32 next_index, bi = 0, *to_next, map_index;
3611 locator_set_t *loc_set;
3613 pending_map_request_t *pmr, *duplicate_pmr = 0;
3617 ASSERT (*lcm->pending_map_request_lock);
3619 /* if there is already a pending request remember it */
3622 pool_foreach(pmr, lcm->pending_map_requests_pool,
3624 if (!gid_address_cmp (&pmr->src, seid)
3625 && !gid_address_cmp (&pmr->dst, deid))
3627 duplicate_pmr = pmr;
3633 if (!is_resend && duplicate_pmr)
3635 /* don't send the request if there is a pending map request already */
3639 /* get locator-set for seid */
3640 if (!lcm->lisp_pitr)
3642 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
3643 if (map_index == ~0)
3645 clib_warning ("No local mapping found in eid-table for %U!",
3646 format_gid_address, seid);
3650 map = pool_elt_at_index (lcm->mapping_pool, map_index);
3655 ("Mapping found for src eid %U is not marked as local!",
3656 format_gid_address, seid);
3659 ls_index = map->locator_set_index;
3663 map_index = lcm->pitr_map_index;
3664 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
3665 ls_index = map->locator_set_index;
3668 /* overwrite locator set if map-request itr-rlocs configured */
3669 if (~0 != lcm->mreq_itr_rlocs)
3671 ls_index = lcm->mreq_itr_rlocs;
3674 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
3676 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
3679 duplicate_pmr->to_be_removed = 1;
3683 /* build the encapsulated map request */
3684 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
3685 &lcm->active_map_resolver,
3686 &sloc, is_smr_invoked, &nonce, &bi);
3691 /* set fib index to default and lookup node */
3692 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3693 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3694 ip4_lookup_node.index : ip6_lookup_node.index;
3696 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3698 /* Enqueue the packet */
3699 to_next = vlib_frame_vector_args (f);
3702 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3705 /* if there is a pending request already update it */
3707 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
3709 /* remove the oldest nonce */
3710 u64 CLIB_UNUSED (tmp), *nonce_del;
3711 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
3712 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
3713 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
3716 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
3717 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3718 duplicate_pmr - lcm->pending_map_requests_pool);
3722 /* add map-request to pending requests table */
3723 pool_get (lcm->pending_map_requests_pool, pmr);
3724 memset (pmr, 0, sizeof (*pmr));
3725 gid_address_copy (&pmr->src, seid);
3726 gid_address_copy (&pmr->dst, deid);
3727 clib_fifo_add1 (pmr->nonces, nonce);
3728 pmr->is_smr_invoked = is_smr_invoked;
3729 reset_pending_mr_counters (pmr);
3730 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3731 pmr - lcm->pending_map_requests_pool);
3738 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
3740 ip4_header_t *ip4 = hdr;
3743 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3745 ip_address_set (src, &ip4->src_address, IP4);
3746 ip_address_set (dst, &ip4->dst_address, IP4);
3751 ip_address_set (src, &ip6->src_address, IP6);
3752 ip_address_set (dst, &ip6->dst_address, IP6);
3757 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3761 u32 vni = ~0, table_id = ~0;
3763 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
3764 IP4 ? FIB_PROTOCOL_IP4 :
3770 vnip = hash_get (lcm->vni_by_table_id, table_id);
3774 clib_warning ("vrf %d is not mapped to any vni!", table_id);
3780 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3786 l2input_main_t *l2im = &l2input_main;
3787 l2_input_config_t *config;
3788 l2_bridge_domain_t *bd_config;
3790 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3791 config = vec_elt_at_index (l2im->configs, sw_if_index0);
3792 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3794 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
3798 clib_warning ("bridge domain %d is not mapped to any vni!",
3805 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3806 gid_address_t * src, gid_address_t * dst)
3811 memset (src, 0, sizeof (*src));
3812 memset (dst, 0, sizeof (*dst));
3813 type = vnet_buffer (b)->lisp.overlay_afi;
3815 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3818 u8 version, preflen;
3820 gid_address_type (src) = GID_ADDR_IP_PREFIX;
3821 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
3823 ip = vlib_buffer_get_current (b);
3824 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
3826 version = gid_address_ip_version (src);
3827 preflen = ip_address_max_len (version);
3828 gid_address_ippref_len (src) = preflen;
3829 gid_address_ippref_len (dst) = preflen;
3831 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3832 gid_address_vni (dst) = vni;
3833 gid_address_vni (src) = vni;
3835 else if (LISP_AFI_MAC == type)
3837 ethernet_header_t *eh;
3839 eh = vlib_buffer_get_current (b);
3841 gid_address_type (src) = GID_ADDR_MAC;
3842 gid_address_type (dst) = GID_ADDR_MAC;
3843 mac_copy (&gid_address_mac (src), eh->src_address);
3844 mac_copy (&gid_address_mac (dst), eh->dst_address);
3847 vni = lisp_get_vni_from_buffer_eth (lcm, b);
3849 gid_address_vni (dst) = vni;
3850 gid_address_vni (src) = vni;
3855 lisp_cp_lookup_inline (vlib_main_t * vm,
3856 vlib_node_runtime_t * node,
3857 vlib_frame_t * from_frame, int overlay)
3859 u32 *from, *to_next_drop, di, si;
3860 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3861 u32 pkts_mapped = 0;
3862 uword n_left_from, n_left_to_next_drop;
3864 from = vlib_frame_vector_args (from_frame);
3865 n_left_from = from_frame->n_vectors;
3867 while (n_left_from > 0)
3869 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3870 to_next_drop, n_left_to_next_drop);
3872 while (n_left_from > 0 && n_left_to_next_drop > 0)
3876 gid_address_t src, dst;
3881 to_next_drop[0] = pi0;
3883 n_left_to_next_drop -= 1;
3885 b0 = vlib_get_buffer (vm, pi0);
3886 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
3887 vnet_buffer (b0)->lisp.overlay_afi = overlay;
3889 /* src/dst eid pair */
3890 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
3892 /* if we have remote mapping for destination already in map-chache
3893 add forwarding tunnel directly. If not send a map-request */
3894 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3898 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3899 /* send a map-request also in case of negative mapping entry
3900 with corresponding action */
3901 if (m->action == LISP_SEND_MAP_REQUEST)
3903 /* send map-request */
3904 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3905 0 /* is_resend */ );
3910 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3914 dp_add_fwd_entry (lcm, si, di);
3920 /* send map-request */
3921 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3922 0 /* is_resend */ );
3926 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3928 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3931 memset (tr, 0, sizeof (*tr));
3932 gid_address_copy (&tr->dst_eid, &dst);
3933 ip_address_copy (&tr->map_resolver_ip,
3934 &lcm->active_map_resolver);
3936 gid_address_free (&dst);
3937 gid_address_free (&src);
3940 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3941 n_left_to_next_drop);
3943 vlib_node_increment_counter (vm, node->node_index,
3944 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
3946 return from_frame->n_vectors;
3950 lisp_cp_lookup_ip4 (vlib_main_t * vm,
3951 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3953 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3957 lisp_cp_lookup_ip6 (vlib_main_t * vm,
3958 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3960 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
3964 lisp_cp_lookup_l2 (vlib_main_t * vm,
3965 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3967 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
3971 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
3972 .function = lisp_cp_lookup_ip4,
3973 .name = "lisp-cp-lookup-ip4",
3974 .vector_size = sizeof (u32),
3975 .format_trace = format_lisp_cp_lookup_trace,
3976 .type = VLIB_NODE_TYPE_INTERNAL,
3978 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3979 .error_strings = lisp_cp_lookup_error_strings,
3981 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3984 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3990 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
3991 .function = lisp_cp_lookup_ip6,
3992 .name = "lisp-cp-lookup-ip6",
3993 .vector_size = sizeof (u32),
3994 .format_trace = format_lisp_cp_lookup_trace,
3995 .type = VLIB_NODE_TYPE_INTERNAL,
3997 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3998 .error_strings = lisp_cp_lookup_error_strings,
4000 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4003 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4009 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
4010 .function = lisp_cp_lookup_l2,
4011 .name = "lisp-cp-lookup-l2",
4012 .vector_size = sizeof (u32),
4013 .format_trace = format_lisp_cp_lookup_trace,
4014 .type = VLIB_NODE_TYPE_INTERNAL,
4016 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4017 .error_strings = lisp_cp_lookup_error_strings,
4019 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4022 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4027 /* lisp_cp_input statistics */
4028 #define foreach_lisp_cp_input_error \
4030 _(MAP_REPLIES_RECEIVED, "map-replies received")
4032 static char *lisp_cp_input_error_strings[] = {
4033 #define _(sym,string) string,
4034 foreach_lisp_cp_input_error
4040 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
4041 foreach_lisp_cp_input_error
4043 LISP_CP_INPUT_N_ERROR,
4044 } lisp_cp_input_error_t;
4048 LISP_CP_INPUT_NEXT_DROP,
4049 LISP_CP_INPUT_N_NEXT,
4050 } lisp_cp_input_next_t;
4054 gid_address_t dst_eid;
4055 ip4_address_t map_resolver_ip;
4056 } lisp_cp_input_trace_t;
4059 format_lisp_cp_input_trace (u8 * s, va_list * args)
4061 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
4062 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
4063 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
4064 va_arg (*args, lisp_cp_input_trace_t *);
4066 s = format (s, "LISP-CP-INPUT: TODO");
4071 process_map_reply (void *arg)
4073 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4074 vlib_buffer_t *b = arg;
4075 u32 len = 0, i, ttl, dst_map_index = 0;
4077 pending_map_request_t *pmr;
4079 map_reply_hdr_t *mrep_hdr;
4083 u8 authoritative, action;
4084 locator_t *locators = 0, *loc;
4086 mrep_hdr = vlib_buffer_get_current (b);
4088 lisp_pending_map_request_lock (lcm);
4090 /* Check pending requests table and nonce */
4091 nonce = MREP_NONCE (mrep_hdr);
4092 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, nonce);
4095 clib_warning ("No pending map-request entry with nonce %lu!", nonce);
4098 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
4100 vlib_buffer_pull (b, sizeof (*mrep_hdr));
4102 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
4104 h = vlib_buffer_get_current (b);
4105 ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
4106 action = MAP_REC_ACTION (h);
4107 authoritative = MAP_REC_AUTH (h);
4109 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
4112 clib_warning ("Failed to parse mapping record!");
4113 vec_foreach (loc, locators)
4117 vec_free (locators);
4121 /* insert/update mappings cache */
4122 vnet_lisp_add_del_mapping (&deid, locators, action, authoritative, ttl,
4123 1, 0 /* is_static */ , &dst_map_index);
4125 /* try to program forwarding only if mapping saved or updated */
4126 if ((u32) ~ 0 != dst_map_index)
4127 lisp_add_del_adjacency (lcm, &pmr->src, &deid, 1);
4129 vec_free (locators);
4132 /* remove pending map request entry */
4135 clib_fifo_foreach (noncep, pmr->nonces, ({
4136 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
4140 clib_fifo_free (pmr->nonces);
4141 pool_put (lcm->pending_map_requests_pool, pmr);
4144 lisp_pending_map_request_unlock (lcm);
4149 is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
4150 lisp_key_type_t key_id, u8 * key)
4156 auth_data_len = auth_data_len_by_key_id (key_id);
4157 if ((u16) ~ 0 == auth_data_len)
4159 clib_warning ("invalid length for key_id %d!", key_id);
4163 /* save auth data */
4164 vec_validate (auth_data, auth_data_len - 1);
4165 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
4167 /* clear auth data */
4168 memset (MNOTIFY_DATA (h), 0, auth_data_len);
4170 /* get hash of the message */
4171 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
4172 (unsigned char *) h, msg_len, NULL, NULL);
4174 result = memcmp (code, auth_data, auth_data_len);
4176 vec_free (auth_data);
4182 process_map_notify (vlib_main_t * vm, lisp_cp_main_t * lcm, vlib_buffer_t * b)
4184 lisp_key_type_t key_id;
4188 map_notify_hdr_t *mnotif_hdr;
4191 locator_t *locators = 0, *loc;
4192 u16 auth_data_len = 0;
4194 mnotif_hdr = vlib_buffer_get_current (b);
4195 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
4196 memset (&deid, 0, sizeof (deid));
4198 nonce = MNOTIFY_NONCE (mnotif_hdr);
4199 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
4200 auth_data_len = auth_data_len_by_key_id (key_id);
4202 pmr_index = hash_get (lcm->map_register_messages_by_nonce, nonce);
4205 clib_warning ("No pending map-register entry with nonce %lu!", nonce);
4209 /* advance buffer by authentication data */
4210 vlib_buffer_pull (b, auth_data_len);
4212 /* parse record eid */
4213 for (i = 0; i < MNOTIFY_REC_COUNT (mnotif_hdr); i++)
4215 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
4218 clib_warning ("Failed to parse mapping record!");
4219 vec_foreach (loc, locators)
4223 vec_free (locators);
4227 vec_free (locators);
4230 /* lookup eid in map-cache */
4231 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &deid);
4234 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
4239 m = pool_elt_at_index (lcm->mapping_pool, mi);
4242 clib_warning ("There is no secret key assigned to %U!",
4243 format_gid_address, &deid);
4247 /* verify authentication data */
4248 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
4249 - (u8 *) mnotif_hdr, m->key_id, m->key))
4251 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
4257 hash_unset (lcm->map_register_messages_by_nonce, nonce);
4258 gid_address_free (&deid);
4261 static vlib_buffer_t *
4262 build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
4263 ip_address_t * dst, u64 nonce, u8 probe_bit,
4264 mapping_t * records, u16 dst_port, u32 * bi_res)
4268 vlib_main_t *vm = lcm->vlib_main;
4270 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
4272 clib_warning ("Can't allocate buffer for Map-Register!");
4276 b = vlib_get_buffer (vm, bi);
4278 /* leave some space for the encap headers */
4279 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
4281 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
4283 /* push outer ip header */
4284 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
4291 send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
4292 u8 probe_bit, u64 nonce, u16 dst_port,
4293 ip_address_t * probed_loc)
4299 u32 next_index, *to_next;
4300 mapping_t *records = 0, *m;
4302 m = pool_elt_at_index (lcm->mapping_pool, mi);
4306 vec_add1 (records, m[0]);
4307 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
4308 memset (&src, 0, sizeof (src));
4310 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
4312 clib_warning ("can't find inteface address for %U", format_ip_address,
4317 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
4321 free_map_register_records (records);
4323 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
4324 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
4325 ip4_lookup_node.index : ip6_lookup_node.index;
4327 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
4329 /* Enqueue the packet */
4330 to_next = vlib_frame_vector_args (f);
4333 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
4338 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm,
4341 u8 *ip_hdr = 0, *udp_hdr;
4344 ip_address_t *dst_loc = 0, probed_loc, src_loc;
4346 map_request_hdr_t *mreq_hdr;
4347 gid_address_t src, dst;
4350 gid_address_t *itr_rlocs = 0, *rloc;
4352 mreq_hdr = vlib_buffer_get_current (b);
4354 // TODO ugly workaround to find out whether LISP is carried by ip4 or 6
4355 // and needs to be fixed
4356 udp_hdr = (u8 *) vlib_buffer_get_current (b) - sizeof (udp_header_t);
4357 ip4 = (ip4_header_t *) (udp_hdr - sizeof (ip4_header_t));
4358 ip6 = (ip6_header_t *) (udp_hdr - sizeof (ip6_header_t));
4360 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
4361 ip_hdr = (u8 *) ip4;
4364 u32 flags = clib_net_to_host_u32
4365 (ip6->ip_version_traffic_class_and_flow_label);
4366 if ((flags & 0xF0000000) == 0x60000000)
4367 ip_hdr = (u8 *) ip6;
4370 clib_warning ("internal error: cannot determine whether packet "
4376 vlib_buffer_pull (b, sizeof (*mreq_hdr));
4378 nonce = MREQ_NONCE (mreq_hdr);
4380 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
4383 ("Only SMR Map-Requests and RLOC probe supported for now!");
4388 len = lisp_msg_parse_addr (b, &src);
4392 /* for now we don't do anything with the itr's rlocs */
4394 lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
4395 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
4399 /* parse eid records and send SMR-invoked map-requests */
4400 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
4402 memset (&dst, 0, sizeof (dst));
4403 len = lisp_msg_parse_eid_rec (b, &dst);
4406 clib_warning ("Can't parse map-request EID-record");
4410 if (MREQ_SMR (mreq_hdr))
4412 /* send SMR-invoked map-requests */
4413 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
4415 else if (MREQ_RLOC_PROBE (mreq_hdr))
4417 memset (&m, 0, sizeof (m));
4418 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
4420 // TODO: select best locator; for now use the first one
4421 dst_loc = &gid_address_ip (&itr_rlocs[0]);
4423 /* get src/dst IP addresses */
4424 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
4426 // TODO get source port from buffer
4427 u16 src_port = LISP_CONTROL_PORT;
4429 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
4430 src_port, &probed_loc);
4435 vec_foreach (rloc, itr_rlocs)
4437 gid_address_free (rloc);
4442 queue_map_reply (vlib_buffer_t * b)
4444 vlib_buffer_t *a = clib_mem_alloc (sizeof (a[0]) + b->current_length);
4446 clib_memcpy (a->data, b->data + b->current_data, b->current_length);
4447 a->current_length = b->current_length;
4448 a->current_data = 0;
4450 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (a[0])
4451 + a->current_length);
4456 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
4457 vlib_frame_t * from_frame)
4459 u32 n_left_from, *from, *to_next_drop;
4460 lisp_msg_type_e type;
4461 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4463 from = vlib_frame_vector_args (from_frame);
4464 n_left_from = from_frame->n_vectors;
4467 while (n_left_from > 0)
4469 u32 n_left_to_next_drop;
4471 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4472 to_next_drop, n_left_to_next_drop);
4473 while (n_left_from > 0 && n_left_to_next_drop > 0)
4481 to_next_drop[0] = bi0;
4483 n_left_to_next_drop -= 1;
4485 b0 = vlib_get_buffer (vm, bi0);
4487 type = lisp_msg_type (vlib_buffer_get_current (b0));
4490 case LISP_MAP_REPLY:
4491 queue_map_reply (b0);
4493 case LISP_MAP_REQUEST:
4494 process_map_request (vm, lcm, b0);
4496 case LISP_MAP_NOTIFY:
4497 process_map_notify (vm, lcm, b0);
4500 clib_warning ("Unsupported LISP message type %d", type);
4504 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
4506 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
4512 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4513 n_left_to_next_drop);
4515 return from_frame->n_vectors;
4519 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
4520 .function = lisp_cp_input,
4521 .name = "lisp-cp-input",
4522 .vector_size = sizeof (u32),
4523 .format_trace = format_lisp_cp_input_trace,
4524 .type = VLIB_NODE_TYPE_INTERNAL,
4526 .n_errors = LISP_CP_INPUT_N_ERROR,
4527 .error_strings = lisp_cp_input_error_strings,
4529 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
4532 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
4538 lisp_cp_init (vlib_main_t * vm)
4540 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4541 clib_error_t *error = 0;
4543 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
4546 lcm->im4 = &ip4_main;
4547 lcm->im6 = &ip6_main;
4548 lcm->vlib_main = vm;
4549 lcm->vnet_main = vnet_get_main ();
4550 lcm->mreq_itr_rlocs = ~0;
4552 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
4554 lcm->pending_map_request_lock =
4555 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
4557 lcm->pending_map_request_lock[0] = 0;
4558 gid_dictionary_init (&lcm->mapping_index_by_gid);
4559 lcm->do_map_resolver_election = 1;
4560 lcm->map_request_mode = MR_MODE_DST_ONLY;
4562 /* default vrf mapped to vni 0 */
4563 hash_set (lcm->table_id_by_vni, 0, 0);
4564 hash_set (lcm->vni_by_table_id, 0, 0);
4566 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
4567 lisp_cp_input_node.index, 1 /* is_ip4 */ );
4568 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
4569 lisp_cp_input_node.index, 0 /* is_ip4 */ );
4575 send_map_request_thread_fn (void *arg)
4577 map_request_args_t *a = arg;
4578 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4581 /* if resending, we already have the lock */
4582 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4585 /* get lock before sending map-request */
4586 lisp_pending_map_request_lock (lcm);
4587 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4588 lisp_pending_map_request_unlock (lcm);
4596 queue_map_request (gid_address_t * seid, gid_address_t * deid,
4597 u8 smr_invoked, u8 is_resend)
4599 map_request_args_t a;
4601 a.is_resend = is_resend;
4602 gid_address_copy (&a.seid, seid);
4603 gid_address_copy (&a.deid, deid);
4604 a.smr_invoked = smr_invoked;
4606 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
4607 (u8 *) & a, sizeof (a));
4612 * Take an action with a pending map request depending on expiration time
4613 * and re-try counters.
4616 update_pending_request (pending_map_request_t * r, f64 dt)
4618 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4621 if (r->time_to_expire - dt < 0)
4622 /* it's time to decide what to do with this pending request */
4624 if (r->retries_num >= NUMBER_OF_RETRIES)
4625 /* too many retries -> assume current map resolver is not available */
4627 mr = get_map_resolver (&lcm->active_map_resolver);
4630 clib_warning ("Map resolver %U not found - probably deleted "
4631 "by the user recently.", format_ip_address,
4632 &lcm->active_map_resolver);
4636 clib_warning ("map resolver %U is unreachable, ignoring",
4637 format_ip_address, &lcm->active_map_resolver);
4639 /* mark current map resolver unavailable so it won't be
4640 * selected next time */
4642 mr->last_update = vlib_time_now (lcm->vlib_main);
4645 reset_pending_mr_counters (r);
4646 elect_map_resolver (lcm);
4648 /* try to find a next eligible map resolver and re-send */
4649 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4655 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4658 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
4662 r->time_to_expire -= dt;
4666 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4669 pending_map_request_t *pmr;
4670 u32 *to_be_removed = 0, *pmr_index;
4672 ASSERT (*lcm->pending_map_request_lock);
4675 pool_foreach (pmr, lcm->pending_map_requests_pool,
4677 if (pmr->to_be_removed)
4679 clib_fifo_foreach (nonce, pmr->nonces, ({
4680 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4683 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4688 vec_foreach (pmr_index, to_be_removed)
4689 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
4691 vec_free (to_be_removed);
4695 update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4697 static f64 time_left = RLOC_PROBING_INTERVAL;
4699 if (!lcm->is_enabled || !lcm->rloc_probing)
4705 time_left = RLOC_PROBING_INTERVAL;
4706 send_rloc_probes (lcm);
4711 update_map_register (lisp_cp_main_t * lcm, f64 dt)
4713 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4714 static u64 mreg_sent_counter = 0;
4716 if (!lcm->is_enabled || !lcm->map_registering)
4722 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4723 time_left = MAP_REGISTER_INTERVAL;
4726 mreg_sent_counter++;
4727 time_left = QUICK_MAP_REGISTER_INTERVAL;
4729 send_map_register (lcm, 1 /* want map notify */ );
4734 send_map_resolver_service (vlib_main_t * vm,
4735 vlib_node_runtime_t * rt, vlib_frame_t * f)
4738 pending_map_request_t *pmr;
4739 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4743 vlib_process_wait_for_event_or_clock (vm, period);
4745 /* currently no signals are expected - just wait for clock */
4746 (void) vlib_process_get_events (vm, 0);
4748 lisp_pending_map_request_lock (lcm);
4751 pool_foreach (pmr, lcm->pending_map_requests_pool,
4753 if (!pmr->to_be_removed)
4754 update_pending_request (pmr, period);
4758 remove_dead_pending_map_requests (lcm);
4759 lisp_pending_map_request_unlock (lcm);
4761 update_map_register (lcm, period);
4762 update_rloc_probing (lcm, period);
4770 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
4771 .function = send_map_resolver_service,
4772 .type = VLIB_NODE_TYPE_PROCESS,
4773 .name = "lisp-retry-service",
4774 .process_log2_n_stack_bytes = 16,
4778 VLIB_INIT_FUNCTION (lisp_cp_init);
4781 * fd.io coding-style-patch-verification: ON
4784 * eval: (c-set-style "gnu")