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 mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1161 timing_wheel_delete (&lcm->wheel, mi);
1165 * Adds/removes/updates mapping. Does not program forwarding.
1167 * @param eid end-host identifier
1168 * @param rlocs vector of remote locators
1169 * @param action action for negative map-reply
1170 * @param is_add add mapping if non-zero, delete otherwise
1171 * @param res_map_index the map-index that was created/updated/removed. It is
1172 * set to ~0 if no action is taken.
1173 * @param is_static used for distinguishing between statically learned
1174 remote mappings and mappings obtained from MR
1175 * @return return code
1178 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
1179 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
1180 u32 * res_map_index)
1182 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1183 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1184 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1185 u32 mi, ls_index = 0, dst_map_index;
1188 if (vnet_lisp_enable_disable_status () == 0)
1190 clib_warning ("LISP is disabled!");
1191 return VNET_API_ERROR_LISP_DISABLED;
1195 res_map_index[0] = ~0;
1197 memset (m_args, 0, sizeof (m_args[0]));
1198 memset (ls_args, 0, sizeof (ls_args[0]));
1200 ls_args->locators = rlocs;
1202 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
1203 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1207 /* overwrite: if mapping already exists, decide if locators should be
1208 * updated and be done */
1209 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
1211 if (!is_static && (old_map->is_static || old_map->local))
1213 /* do not overwrite local or static remote mappings */
1214 clib_warning ("mapping %U rejected due to collision with local "
1215 "or static remote mapping!", format_gid_address,
1220 locator_set_t *old_ls;
1222 /* update mapping attributes */
1223 old_map->action = action;
1224 old_map->authoritative = authoritative;
1227 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1228 old_map->locator_set_index);
1229 if (compare_locators (lcm, old_ls->locator_indices,
1232 /* set locator-set index to overwrite */
1233 ls_args->is_add = 1;
1234 ls_args->index = old_map->locator_set_index;
1235 vnet_lisp_add_del_locator_set (ls_args, 0);
1237 res_map_index[0] = mi;
1243 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1245 ls_args->is_add = 1;
1246 ls_args->index = ~0;
1248 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1251 gid_address_copy (&m_args->eid, eid);
1253 m_args->action = action;
1254 m_args->locator_set_index = ls_index;
1255 m_args->is_static = is_static;
1257 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
1260 res_map_index[0] = dst_map_index;
1265 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
1267 clib_warning ("cannot delete mapping for eid %U",
1268 format_gid_address, eid);
1273 gid_address_copy (&m_args->eid, eid);
1274 m_args->locator_set_index = old_map->locator_set_index;
1276 /* delete mapping associated from map-cache */
1277 vnet_lisp_map_cache_add_del (m_args, 0);
1279 ls_args->is_add = 0;
1280 ls_args->index = old_map->locator_set_index;
1281 /* delete locator set */
1282 vnet_lisp_add_del_locator_set (ls_args, 0);
1284 /* delete timer associated to the mapping if any */
1285 if (old_map->timer_set)
1286 mapping_delete_timer (lcm, mi);
1288 /* return old mapping index */
1290 res_map_index[0] = mi;
1298 vnet_lisp_clear_all_remote_adjacencies (void)
1301 u32 mi, *map_indices = 0, *map_indexp;
1302 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1303 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1304 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1307 pool_foreach_index (mi, lcm->mapping_pool,
1309 vec_add1 (map_indices, mi);
1313 vec_foreach (map_indexp, map_indices)
1315 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1318 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
1320 dm_args->is_add = 0;
1321 gid_address_copy (&dm_args->eid, &map->eid);
1322 dm_args->locator_set_index = map->locator_set_index;
1324 /* delete mapping associated to fwd entry */
1325 vnet_lisp_map_cache_add_del (dm_args, 0);
1329 ls->index = map->locator_set_index;
1330 /* delete locator set */
1331 rv = vnet_lisp_add_del_locator_set (ls, 0);
1339 vec_free (map_indices);
1344 * Adds adjacency or removes forwarding entry associated to remote mapping.
1345 * Note that adjacencies are not stored, they only result in forwarding entries
1349 lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
1350 gid_address_t * remote_eid, u8 is_add)
1352 u32 local_mi, remote_mi = ~0;
1354 if (vnet_lisp_enable_disable_status () == 0)
1356 clib_warning ("LISP is disabled!");
1357 return VNET_API_ERROR_LISP_DISABLED;
1360 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1361 remote_eid, local_eid);
1362 if (GID_LOOKUP_MISS == remote_mi)
1364 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1365 format_gid_address, remote_eid);
1372 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1375 /* check if source eid has an associated mapping. If pitr mode is on,
1376 * just use the pitr's mapping */
1377 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
1378 gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
1381 if (GID_LOOKUP_MISS == local_mi)
1383 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1384 format_gid_address, local_eid);
1389 /* update forwarding */
1390 dp_add_fwd_entry (lcm, local_mi, remote_mi);
1393 dp_del_fwd_entry (lcm, 0, remote_mi);
1399 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1401 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1402 return lisp_add_del_adjacency (lcm, &a->leid, &a->reid, a->is_add);
1406 * Handler for add/del remote mapping CLI.
1408 * @param vm vlib context
1409 * @param input input from user
1411 * @return pointer to clib error structure
1413 static clib_error_t *
1414 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
1415 unformat_input_t * input,
1416 vlib_cli_command_t * cmd)
1418 clib_error_t *error = 0;
1419 unformat_input_t _line_input, *line_input = &_line_input;
1420 u8 is_add = 1, del_all = 0;
1421 locator_t rloc, *rlocs = 0, *curr_rloc = 0;
1424 u32 vni, action = ~0, p, w;
1427 /* Get a line of input. */
1428 if (!unformat_user (input, unformat_line_input, line_input))
1431 memset (&eid, 0, sizeof (eid));
1432 memset (&rloc, 0, sizeof (rloc));
1434 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1436 if (unformat (line_input, "del-all"))
1438 else if (unformat (line_input, "del"))
1440 else if (unformat (line_input, "add"))
1442 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1444 else if (unformat (line_input, "vni %u", &vni))
1446 gid_address_vni (&eid) = vni;
1448 else if (unformat (line_input, "p %d w %d", &p, &w))
1453 ("No RLOC configured for setting priority/weight!");
1456 curr_rloc->priority = p;
1457 curr_rloc->weight = w;
1459 else if (unformat (line_input, "rloc %U", unformat_ip_address,
1460 &gid_address_ip (&rloc.address)))
1462 /* since rloc is stored in ip prefix we need to set prefix length */
1463 ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
1465 u8 version = gid_address_ip_version (&rloc.address);
1466 ip_prefix_len (pref) = ip_address_max_len (version);
1468 vec_add1 (rlocs, rloc);
1469 curr_rloc = &rlocs[vec_len (rlocs) - 1];
1471 else if (unformat (line_input, "action %U",
1472 unformat_negative_mapping_action, &action))
1476 clib_warning ("parse error");
1483 clib_warning ("missing eid!");
1489 if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1491 clib_warning ("no action set for negative map-reply!");
1497 vnet_lisp_clear_all_remote_adjacencies ();
1501 /* TODO build src/dst with seid */
1503 /* if it's a delete, clean forwarding */
1506 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1507 rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1514 /* add as static remote mapping, i.e., not authoritative and infinite
1516 rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
1517 1 /* is_static */ , 0);
1520 clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
1524 unformat_free (line_input);
1528 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) =
1530 .path = "lisp remote-mapping",.short_help =
1531 "lisp remote-mapping add|del [del-all] vni <vni> "
1532 "eid <est-eid> [action <no-action|natively-forward|"
1533 "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
1534 "[rloc <dst-locator> ... ]",.function =
1535 lisp_add_del_remote_mapping_command_fn,};
1538 * Handler for add/del adjacency CLI.
1540 static clib_error_t *
1541 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
1542 vlib_cli_command_t * cmd)
1544 clib_error_t *error = 0;
1545 unformat_input_t _line_input, *line_input = &_line_input;
1546 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1548 ip_prefix_t *reid_ippref, *leid_ippref;
1549 gid_address_t leid, reid;
1550 u8 *dmac = gid_address_mac (&reid);
1551 u8 *smac = gid_address_mac (&leid);
1552 u8 reid_set = 0, leid_set = 0;
1556 /* Get a line of input. */
1557 if (!unformat_user (input, unformat_line_input, line_input))
1560 memset (&reid, 0, sizeof (reid));
1561 memset (&leid, 0, sizeof (leid));
1563 leid_ippref = &gid_address_ippref (&leid);
1564 reid_ippref = &gid_address_ippref (&reid);
1566 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1568 if (unformat (line_input, "del"))
1570 else if (unformat (line_input, "add"))
1572 else if (unformat (line_input, "reid %U",
1573 unformat_ip_prefix, reid_ippref))
1575 gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
1578 else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
1580 gid_address_type (&reid) = GID_ADDR_MAC;
1583 else if (unformat (line_input, "vni %u", &vni))
1585 gid_address_vni (&leid) = vni;
1586 gid_address_vni (&reid) = vni;
1588 else if (unformat (line_input, "leid %U",
1589 unformat_ip_prefix, leid_ippref))
1591 gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
1594 else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
1596 gid_address_type (&leid) = GID_ADDR_MAC;
1601 clib_warning ("parse error");
1606 if (!reid_set || !leid_set)
1608 clib_warning ("missing remote or local eid!");
1612 if ((gid_address_type (&leid) != gid_address_type (&reid))
1613 || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
1614 && ip_prefix_version (reid_ippref)
1615 != ip_prefix_version (leid_ippref)))
1617 clib_warning ("remote and local EIDs are of different types!");
1621 memset (a, 0, sizeof (a[0]));
1622 gid_address_copy (&a->leid, &leid);
1623 gid_address_copy (&a->reid, &reid);
1626 rv = vnet_lisp_add_del_adjacency (a);
1629 clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
1632 unformat_free (line_input);
1637 VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1638 .path = "lisp adjacency",
1639 .short_help = "lisp adjacency add|del vni <vni> reid <remote-eid> "
1641 .function = lisp_add_del_adjacency_command_fn,
1646 vnet_lisp_set_map_request_mode (u8 mode)
1648 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1650 if (vnet_lisp_enable_disable_status () == 0)
1652 clib_warning ("LISP is disabled!");
1653 return VNET_API_ERROR_LISP_DISABLED;
1656 if (mode >= _MR_MODE_MAX)
1658 clib_warning ("Invalid LISP map request mode %d!", mode);
1659 return VNET_API_ERROR_INVALID_ARGUMENT;
1662 lcm->map_request_mode = mode;
1666 static clib_error_t *
1667 lisp_map_request_mode_command_fn (vlib_main_t * vm,
1668 unformat_input_t * input,
1669 vlib_cli_command_t * cmd)
1671 unformat_input_t _i, *i = &_i;
1672 map_request_mode_t mr_mode = _MR_MODE_MAX;
1674 /* Get a line of input. */
1675 if (!unformat_user (input, unformat_line_input, i))
1678 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1680 if (unformat (i, "dst-only"))
1681 mr_mode = MR_MODE_DST_ONLY;
1682 else if (unformat (i, "src-dst"))
1683 mr_mode = MR_MODE_SRC_DST;
1686 clib_warning ("parse error '%U'", format_unformat_error, i);
1691 if (_MR_MODE_MAX == mr_mode)
1693 clib_warning ("No LISP map request mode entered!");
1697 vnet_lisp_set_map_request_mode (mr_mode);
1703 VLIB_CLI_COMMAND (lisp_map_request_mode_command) = {
1704 .path = "lisp map-request mode",
1705 .short_help = "lisp map-request mode dst-only|src-dst",
1706 .function = lisp_map_request_mode_command_fn,
1711 format_lisp_map_request_mode (u8 * s, va_list * args)
1713 u32 mode = va_arg (*args, u32);
1718 return format (0, "dst-only");
1720 return format (0, "src-dst");
1725 static clib_error_t *
1726 lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
1727 unformat_input_t * input,
1728 vlib_cli_command_t * cmd)
1730 vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
1731 vnet_lisp_get_map_request_mode ());
1736 VLIB_CLI_COMMAND (lisp_show_map_request_mode_command) = {
1737 .path = "show lisp map-request mode",
1738 .short_help = "show lisp map-request mode",
1739 .function = lisp_show_map_request_mode_command_fn,
1743 static clib_error_t *
1744 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
1745 unformat_input_t * input,
1746 vlib_cli_command_t * cmd)
1749 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1751 vec_foreach (mr, lcm->map_resolvers)
1753 vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
1759 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1760 .path = "show lisp map-resolvers",
1761 .short_help = "show lisp map-resolvers",
1762 .function = lisp_show_map_resolvers_command_fn,
1767 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1769 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1770 u32 locator_set_index = ~0;
1774 if (vnet_lisp_enable_disable_status () == 0)
1776 clib_warning ("LISP is disabled!");
1777 return VNET_API_ERROR_LISP_DISABLED;
1780 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1783 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1786 locator_set_index = p[0];
1790 pool_get (lcm->mapping_pool, m);
1791 m->locator_set_index = locator_set_index;
1793 lcm->pitr_map_index = m - lcm->mapping_pool;
1795 /* enable pitr mode */
1800 /* remove pitr mapping */
1801 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1803 /* disable pitr mode */
1809 static clib_error_t *
1810 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
1811 unformat_input_t * input,
1812 vlib_cli_command_t * cmd)
1814 u8 locator_name_set = 0;
1815 u8 *locator_set_name = 0;
1817 unformat_input_t _line_input, *line_input = &_line_input;
1818 clib_error_t *error = 0;
1821 /* Get a line of input. */
1822 if (!unformat_user (input, unformat_line_input, line_input))
1825 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1827 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
1828 locator_name_set = 1;
1829 else if (unformat (line_input, "disable"))
1832 return clib_error_return (0, "parse error");
1835 if (!locator_name_set)
1837 clib_warning ("No locator set specified!");
1840 rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1843 error = clib_error_return (0, "failed to %s pitr!",
1844 is_add ? "add" : "delete");
1848 if (locator_set_name)
1849 vec_free (locator_set_name);
1854 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1855 .path = "lisp pitr",
1856 .short_help = "lisp pitr [disable] ls <locator-set-name>",
1857 .function = lisp_pitr_set_locator_set_command_fn,
1861 static clib_error_t *
1862 lisp_show_pitr_command_fn (vlib_main_t * vm,
1863 unformat_input_t * input, vlib_cli_command_t * cmd)
1865 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1870 vlib_cli_output (vm, "%=20s%=16s",
1871 "pitr", lcm->lisp_pitr ? "locator-set" : "");
1873 if (!lcm->lisp_pitr)
1875 vlib_cli_output (vm, "%=20s", "disable");
1879 if (~0 == lcm->pitr_map_index)
1881 tmp_str = format (0, "N/A");
1885 m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1886 if (~0 != m->locator_set_index)
1889 pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1890 tmp_str = format (0, "%s", ls->name);
1894 tmp_str = format (0, "N/A");
1897 vec_add1 (tmp_str, 0);
1899 vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1907 VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1908 .path = "show lisp pitr",
1909 .short_help = "Show pitr",
1910 .function = lisp_show_pitr_command_fn,
1915 format_eid_entry (u8 * s, va_list * args)
1917 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
1918 lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
1919 mapping_t *mapit = va_arg (*args, mapping_t *);
1920 locator_set_t *ls = va_arg (*args, locator_set_t *);
1921 gid_address_t *gid = &mapit->eid;
1922 u32 ttl = mapit->ttl;
1923 u8 aut = mapit->authoritative;
1928 u8 *type = ls->local ? format (0, "local(%s)", ls->name)
1929 : format (0, "remote");
1931 if (vec_len (ls->locator_indices) == 0)
1933 s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
1938 vec_foreach (loc_index, ls->locator_indices)
1940 locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1942 loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1945 loc = format (0, "%U", format_ip_address,
1946 &gid_address_ip (&l->address));
1950 s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
1951 gid, type, loc, ttl, aut);
1955 s = format (s, "%55s%v\n", "", loc);
1961 static clib_error_t *
1962 lisp_show_eid_table_command_fn (vlib_main_t * vm,
1963 unformat_input_t * input,
1964 vlib_cli_command_t * cmd)
1966 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1968 unformat_input_t _line_input, *line_input = &_line_input;
1974 memset (&eid, 0, sizeof (eid));
1976 /* Get a line of input. */
1977 if (!unformat_user (input, unformat_line_input, line_input))
1980 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1982 if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1984 else if (unformat (line_input, "local"))
1986 else if (unformat (line_input, "remote"))
1989 return clib_error_return (0, "parse error: '%U'",
1990 format_unformat_error, line_input);
1993 vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
1994 "EID", "type", "locators", "ttl", "autoritative");
1999 pool_foreach (mapit, lcm->mapping_pool,
2001 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
2002 mapit->locator_set_index);
2003 if (filter && !((1 == filter && ls->local) ||
2004 (2 == filter && !ls->local)))
2008 vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
2015 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
2016 if ((u32) ~ 0 == mi)
2019 mapit = pool_elt_at_index (lcm->mapping_pool, mi);
2020 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2021 mapit->locator_set_index);
2023 if (filter && !((1 == filter && ls->local) ||
2024 (2 == filter && !ls->local)))
2029 vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
2037 VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
2038 .path = "show lisp eid-table",
2039 .short_help = "Shows EID table",
2040 .function = lisp_show_eid_table_command_fn,
2044 /* cleans locator to locator-set data and removes locators not part of
2045 * any locator-set */
2047 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
2049 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
2050 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
2051 for (i = 0; i < vec_len (ls->locator_indices); i++)
2053 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
2054 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2056 for (j = 0; j < vec_len (ls_indexes[0]); j++)
2058 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
2059 if (ls_indexp[0] == lsi)
2063 /* delete index for removed locator-set */
2064 vec_del1 (ls_indexes[0], j);
2066 /* delete locator if it's part of no locator-set */
2067 if (vec_len (ls_indexes[0]) == 0)
2069 pool_put_index (lcm->locator_pool, loc_indexp[0]);
2070 vec_add1 (to_be_deleted, i);
2076 for (i = 0; i < vec_len (to_be_deleted); i++)
2078 loc_indexp = vec_elt_at_index (to_be_deleted, i);
2079 vec_del1 (ls->locator_indices, loc_indexp[0]);
2081 vec_free (to_be_deleted);
2085 static inline uword *
2086 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
2088 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2093 /* find locator-set */
2096 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
2107 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
2113 ASSERT (ls != NULL);
2114 ASSERT (loc != NULL);
2116 vec_foreach (locit, ls->locator_indices)
2118 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2119 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
2120 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
2122 clib_warning ("Duplicate locator");
2123 return VNET_API_ERROR_VALUE_EXIST;
2131 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
2132 u32 ls_index, u32 loc_id)
2134 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2135 u32 **ls_indexes = NULL;
2137 ASSERT (ls != NULL);
2138 ASSERT (locit != NULL);
2140 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
2141 pool_put_index (lcm->locator_pool, locit[0]);
2142 vec_del1 (ls->locator_indices, loc_id);
2143 vec_del1 (ls_indexes[0], ls_index);
2147 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
2148 locator_set_t * ls, u32 * ls_result)
2150 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2151 locator_t *loc = NULL, *itloc = NULL;
2152 uword _p = (u32) ~ 0, *p = &_p;
2153 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
2159 if (vnet_lisp_enable_disable_status () == 0)
2161 clib_warning ("LISP is disabled!");
2162 return VNET_API_ERROR_LISP_DISABLED;
2165 p = get_locator_set_index (a, p);
2168 clib_warning ("locator-set %v doesn't exist", a->name);
2169 return VNET_API_ERROR_INVALID_ARGUMENT;
2174 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2177 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2179 return VNET_API_ERROR_INVALID_ARGUMENT;
2186 ls_result[0] = p[0];
2188 /* allocate locators */
2189 vec_foreach (itloc, a->locators)
2191 ret = is_locator_in_locator_set (lcm, ls, itloc);
2197 pool_get (lcm->locator_pool, loc);
2199 loc_index = loc - lcm->locator_pool;
2201 vec_add1 (ls->locator_indices, loc_index);
2203 vec_validate (lcm->locator_to_locator_sets, loc_index);
2204 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2206 vec_add1 (ls_indexes[0], p[0]);
2213 itloc = a->locators;
2215 vec_foreach (locit, ls->locator_indices)
2217 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2219 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
2221 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2223 if (0 == loc->local &&
2224 !gid_address_cmp (&loc->address, &itloc->address))
2226 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2237 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
2240 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2242 uword _p = (u32) ~ 0, *p = &_p;
2247 if (vnet_lisp_enable_disable_status () == 0)
2249 clib_warning ("LISP is disabled!");
2250 return VNET_API_ERROR_LISP_DISABLED;
2255 p = get_locator_set_index (a, p);
2258 if (p && p[0] != (u32) ~ 0)
2260 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2263 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2268 /* clean locator to locator-set vectors and remove locators if
2269 * they're not part of another locator-set */
2270 clean_locator_to_locator_set (lcm, p[0]);
2272 /* remove locator indices from locator set */
2273 vec_free (ls->locator_indices);
2278 ls_result[0] = p[0];
2280 /* new locator-set */
2283 pool_get (lcm->locator_set_pool, ls);
2284 memset (ls, 0, sizeof (*ls));
2285 ls_index = ls - lcm->locator_set_pool;
2289 ls->name = vec_dup (a->name);
2291 if (!lcm->locator_set_index_by_name)
2292 lcm->locator_set_index_by_name = hash_create_vec (
2300 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
2303 /* mark as local locator-set */
2304 vec_add1 (lcm->local_locator_set_indexes, ls_index);
2306 ls->local = a->local;
2308 ls_result[0] = ls_index;
2311 ret = vnet_lisp_add_del_locator (a, ls, NULL);
2319 p = get_locator_set_index (a, p);
2322 clib_warning ("locator-set %v doesn't exists", a->name);
2326 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2329 clib_warning ("locator-set with index %d doesn't exists", p[0]);
2333 if (lcm->mreq_itr_rlocs == p[0])
2335 clib_warning ("Can't delete the locator-set used to constrain "
2336 "the itr-rlocs in map-requests!");
2340 if (vec_len (lcm->locator_set_to_eids) != 0)
2342 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
2343 if (vec_len (eid_indexes[0]) != 0)
2346 ("Can't delete a locator that supports a mapping!");
2351 /* clean locator to locator-sets data */
2352 clean_locator_to_locator_set (lcm, p[0]);
2358 vec_foreach_index (it, lcm->local_locator_set_indexes)
2360 lsi = vec_elt (lcm->local_locator_set_indexes, it);
2363 vec_del1 (lcm->local_locator_set_indexes, it);
2367 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2369 vec_free (ls->name);
2370 vec_free (ls->locator_indices);
2371 pool_put (lcm->locator_set_pool, ls);
2377 vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
2379 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2381 lcm->rloc_probing = is_enable;
2386 vnet_lisp_map_register_enable_disable (u8 is_enable)
2388 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2390 lcm->map_registering = is_enable;
2395 vnet_lisp_enable_disable (u8 is_enable)
2398 clib_error_t *error = 0;
2399 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2400 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
2402 a->is_en = is_enable;
2403 error = vnet_lisp_gpe_enable_disable (a);
2406 return clib_error_return (0, "failed to %s data-plane!",
2407 a->is_en ? "enable" : "disable");
2412 /* enable all l2 and l3 ifaces */
2415 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2416 dp_add_del_iface(lcm, vni, 0, 1);
2418 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2419 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
2425 /* clear interface table */
2426 hash_free (lcm->fwd_entry_by_mapping_index);
2427 pool_free (lcm->fwd_entry_pool);
2430 /* update global flag */
2431 lcm->is_enabled = is_enable;
2436 static clib_error_t *
2437 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
2438 vlib_cli_command_t * cmd)
2440 unformat_input_t _line_input, *line_input = &_line_input;
2444 /* Get a line of input. */
2445 if (!unformat_user (input, unformat_line_input, line_input))
2448 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2450 if (unformat (line_input, "enable"))
2455 else if (unformat (line_input, "disable"))
2459 return clib_error_return (0, "parse error: '%U'",
2460 format_unformat_error, line_input);
2465 return clib_error_return (0, "state not set");
2467 vnet_lisp_enable_disable (is_enabled);
2472 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
2474 .short_help = "lisp [enable|disable]",
2475 .function = lisp_enable_disable_command_fn,
2479 static clib_error_t *
2480 lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
2481 unformat_input_t * input,
2482 vlib_cli_command_t * cmd)
2484 unformat_input_t _line_input, *line_input = &_line_input;
2488 /* Get a line of input. */
2489 if (!unformat_user (input, unformat_line_input, line_input))
2492 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2494 if (unformat (line_input, "enable"))
2499 else if (unformat (line_input, "disable"))
2503 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2511 vlib_cli_output (vm, "state not set!");
2515 vnet_lisp_map_register_enable_disable (is_enabled);
2520 VLIB_CLI_COMMAND (lisp_map_register_enable_disable_command) = {
2521 .path = "lisp map-register",
2522 .short_help = "lisp map-register [enable|disable]",
2523 .function = lisp_map_register_enable_disable_command_fn,
2527 static clib_error_t *
2528 lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
2529 unformat_input_t * input,
2530 vlib_cli_command_t * cmd)
2532 unformat_input_t _line_input, *line_input = &_line_input;
2536 /* Get a line of input. */
2537 if (!unformat_user (input, unformat_line_input, line_input))
2540 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2542 if (unformat (line_input, "enable"))
2547 else if (unformat (line_input, "disable"))
2551 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2559 vlib_cli_output (vm, "state not set!");
2563 vnet_lisp_rloc_probe_enable_disable (is_enabled);
2568 VLIB_CLI_COMMAND (lisp_rloc_probe_enable_disable_command) = {
2569 .path = "lisp rloc-probe",
2570 .short_help = "lisp rloc-probe [enable|disable]",
2571 .function = lisp_rloc_probe_enable_disable_command_fn,
2576 vnet_lisp_enable_disable_status (void)
2578 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2579 return lcm->is_enabled;
2583 format_lisp_status (u8 * s, va_list * args)
2585 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2586 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
2589 static clib_error_t *
2590 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
2591 vlib_cli_command_t * cmd)
2594 msg = format (msg, "feature: %U\ngpe: %U\n",
2595 format_lisp_status, format_vnet_lisp_gpe_status);
2596 vlib_cli_output (vm, "%v", msg);
2602 VLIB_CLI_COMMAND (lisp_show_status_command) = {
2603 .path = "show lisp status",
2604 .short_help = "show lisp status",
2605 .function = lisp_show_status_command_fn,
2609 static clib_error_t *
2610 lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
2611 unformat_input_t * input,
2612 vlib_cli_command_t * cmd)
2615 unformat_input_t _line_input, *line_input = &_line_input;
2616 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2617 uword *vni_table = 0;
2620 /* Get a line of input. */
2621 if (!unformat_user (input, unformat_line_input, line_input))
2624 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2626 if (unformat (line_input, "l2"))
2628 vni_table = lcm->bd_id_by_vni;
2631 else if (unformat (line_input, "l3"))
2633 vni_table = lcm->table_id_by_vni;
2637 return clib_error_return (0, "parse error: '%U'",
2638 format_unformat_error, line_input);
2643 vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
2647 vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
2650 hash_foreach_pair (p, vni_table,
2652 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
2660 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
2661 .path = "show lisp eid-table map",
2662 .short_help = "show lisp eid-table l2|l3",
2663 .function = lisp_show_eid_table_map_command_fn,
2667 static clib_error_t *
2668 lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
2669 unformat_input_t * input,
2670 vlib_cli_command_t * cmd)
2672 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2673 vnet_main_t *vnm = lgm->vnet_main;
2674 unformat_input_t _line_input, *line_input = &_line_input;
2676 clib_error_t *error = 0;
2677 u8 *locator_set_name = 0;
2678 locator_t locator, *locators = 0;
2679 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2683 memset (&locator, 0, sizeof (locator));
2684 memset (a, 0, sizeof (a[0]));
2686 /* Get a line of input. */
2687 if (!unformat_user (input, unformat_line_input, line_input))
2690 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2692 if (unformat (line_input, "add %_%v%_", &locator_set_name))
2694 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
2696 else if (unformat (line_input, "iface %U p %d w %d",
2697 unformat_vnet_sw_interface, vnm,
2698 &locator.sw_if_index, &locator.priority,
2702 vec_add1 (locators, locator);
2706 error = unformat_parse_error (line_input);
2711 a->name = locator_set_name;
2712 a->locators = locators;
2716 rv = vnet_lisp_add_del_locator_set (a, &ls_index);
2719 error = clib_error_return (0, "failed to %s locator-set!",
2720 is_add ? "add" : "delete");
2724 vec_free (locators);
2725 if (locator_set_name)
2726 vec_free (locator_set_name);
2731 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2732 .path = "lisp locator-set",
2733 .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2734 "p <priority> w <weight>]",
2735 .function = lisp_add_del_locator_set_command_fn,
2739 static clib_error_t *
2740 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
2741 unformat_input_t * input,
2742 vlib_cli_command_t * cmd)
2744 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2745 vnet_main_t *vnm = lgm->vnet_main;
2746 unformat_input_t _line_input, *line_input = &_line_input;
2748 clib_error_t *error = 0;
2749 u8 *locator_set_name = 0;
2750 u8 locator_set_name_set = 0;
2751 locator_t locator, *locators = 0;
2752 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2755 memset (&locator, 0, sizeof (locator));
2756 memset (a, 0, sizeof (a[0]));
2758 /* Get a line of input. */
2759 if (!unformat_user (input, unformat_line_input, line_input))
2762 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2764 if (unformat (line_input, "add"))
2766 else if (unformat (line_input, "del"))
2768 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
2769 locator_set_name_set = 1;
2770 else if (unformat (line_input, "iface %U p %d w %d",
2771 unformat_vnet_sw_interface, vnm,
2772 &locator.sw_if_index, &locator.priority,
2776 vec_add1 (locators, locator);
2780 error = unformat_parse_error (line_input);
2785 if (!locator_set_name_set)
2787 error = clib_error_return (0, "locator_set name not set!");
2791 a->name = locator_set_name;
2792 a->locators = locators;
2796 vnet_lisp_add_del_locator (a, 0, &ls_index);
2799 vec_free (locators);
2800 vec_free (locator_set_name);
2805 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2806 .path = "lisp locator",
2807 .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2808 "p <priority> w <weight>",
2809 .function = lisp_add_del_locator_in_set_command_fn,
2813 static clib_error_t *
2814 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
2815 unformat_input_t * input,
2816 vlib_cli_command_t * cmd)
2818 locator_set_t *lsit;
2821 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2823 vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
2824 "Priority", "Weight");
2827 pool_foreach (lsit, lcm->locator_set_pool,
2833 msg = format (msg, "%v", lsit->name);
2837 msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
2839 vec_foreach (locit, lsit->locator_indices)
2843 msg = format (msg, "%16s", " ");
2845 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2847 msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
2850 msg = format (msg, "%16U%16d%16d\n", format_ip_address,
2851 &gid_address_ip(&loc->address), loc->priority,
2855 vlib_cli_output (vm, "%v", msg);
2863 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2864 .path = "show lisp locator-set",
2865 .short_help = "Shows locator-sets",
2866 .function = lisp_cp_show_locator_sets_command_fn,
2871 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2873 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2875 lisp_msmr_t _mr, *mr = &_mr;
2877 if (vnet_lisp_enable_disable_status () == 0)
2879 clib_warning ("LISP is disabled!");
2880 return VNET_API_ERROR_LISP_DISABLED;
2886 if (get_map_resolver (&a->address))
2888 clib_warning ("map-resolver %U already exists!", format_ip_address,
2893 memset (mr, 0, sizeof (*mr));
2894 ip_address_copy (&mr->address, &a->address);
2895 vec_add1 (lcm->map_resolvers, *mr);
2897 if (vec_len (lcm->map_resolvers) == 1)
2898 lcm->do_map_resolver_election = 1;
2902 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2904 mr = vec_elt_at_index (lcm->map_resolvers, i);
2905 if (!ip_address_cmp (&mr->address, &a->address))
2907 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2908 lcm->do_map_resolver_election = 1;
2910 vec_del1 (lcm->map_resolvers, i);
2918 static clib_error_t *
2919 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
2920 unformat_input_t * input,
2921 vlib_cli_command_t * cmd)
2923 unformat_input_t _line_input, *line_input = &_line_input;
2924 u8 is_add = 1, addr_set = 0;
2925 ip_address_t ip_addr;
2926 clib_error_t *error = 0;
2928 vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
2930 /* Get a line of input. */
2931 if (!unformat_user (input, unformat_line_input, line_input))
2934 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2936 if (unformat (line_input, "add"))
2938 else if (unformat (line_input, "del"))
2940 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
2944 error = unformat_parse_error (line_input);
2951 error = clib_error_return (0, "Map-resolver address must be set!");
2956 a->address = ip_addr;
2957 rv = vnet_lisp_add_del_map_resolver (a);
2960 error = clib_error_return (0, "failed to %s map-resolver!",
2961 is_add ? "add" : "delete");
2969 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
2970 .path = "lisp map-resolver",
2971 .short_help = "lisp map-resolver add/del <ip_address>",
2972 .function = lisp_add_del_map_resolver_command_fn,
2977 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2979 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2982 if (vnet_lisp_enable_disable_status () == 0)
2984 clib_warning ("LISP is disabled!");
2985 return VNET_API_ERROR_LISP_DISABLED;
2990 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
2993 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
2994 return VNET_API_ERROR_INVALID_ARGUMENT;
2997 lcm->mreq_itr_rlocs = p[0];
3001 lcm->mreq_itr_rlocs = ~0;
3007 static clib_error_t *
3008 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
3009 unformat_input_t * input,
3010 vlib_cli_command_t * cmd)
3012 unformat_input_t _line_input, *line_input = &_line_input;
3014 u8 *locator_set_name = 0;
3015 clib_error_t *error = 0;
3017 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
3019 /* Get a line of input. */
3020 if (!unformat_user (input, unformat_line_input, line_input))
3023 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3025 if (unformat (line_input, "del"))
3027 else if (unformat (line_input, "add %_%v%_", &locator_set_name))
3031 error = unformat_parse_error (line_input);
3037 a->locator_set_name = locator_set_name;
3038 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
3041 error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
3042 is_add ? "add" : "delete");
3045 vec_free (locator_set_name);
3053 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
3054 .path = "lisp map-request itr-rlocs",
3055 .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
3056 .function = lisp_add_del_mreq_itr_rlocs_command_fn,
3060 static clib_error_t *
3061 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
3062 unformat_input_t * input,
3063 vlib_cli_command_t * cmd)
3065 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3066 locator_set_t *loc_set;
3068 vlib_cli_output (vm, "%=20s", "itr-rlocs");
3070 if (~0 == lcm->mreq_itr_rlocs)
3075 loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
3077 vlib_cli_output (vm, "%=20s", loc_set->name);
3083 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
3084 .path = "show lisp map-request itr-rlocs",
3085 .short_help = "Shows map-request itr-rlocs",
3086 .function = lisp_show_mreq_itr_rlocs_command_fn,
3090 /* Statistics (not really errors) */
3091 #define foreach_lisp_cp_lookup_error \
3093 _(MAP_REQUESTS_SENT, "map-request sent")
3095 static char *lisp_cp_lookup_error_strings[] = {
3096 #define _(sym,string) string,
3097 foreach_lisp_cp_lookup_error
3103 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
3104 foreach_lisp_cp_lookup_error
3106 LISP_CP_LOOKUP_N_ERROR,
3107 } lisp_cp_lookup_error_t;
3111 LISP_CP_LOOKUP_NEXT_DROP,
3112 LISP_CP_LOOKUP_N_NEXT,
3113 } lisp_cp_lookup_next_t;
3117 gid_address_t dst_eid;
3118 ip_address_t map_resolver_ip;
3119 } lisp_cp_lookup_trace_t;
3122 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
3124 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3125 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3126 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
3128 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
3129 format_ip_address, &t->map_resolver_ip, format_gid_address,
3135 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
3136 ip_address_t * sloc)
3141 if (vec_len (lcm->map_resolvers) == 0)
3143 clib_warning ("No map-resolver configured");
3147 /* find the first mr ip we have a route to and the ip of the
3148 * iface that has a route to it */
3149 vec_foreach (mrit, lcm->map_resolvers)
3152 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
3154 ip_address_copy (mr_ip, a);
3156 /* also update globals */
3161 clib_warning ("Can't find map-resolver and local interface ip!");
3165 static gid_address_t *
3166 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
3172 ip_interface_address_t *ia = 0;
3173 gid_address_t gid_data, *gid = &gid_data;
3174 gid_address_t *rlocs = 0;
3175 ip_prefix_t *ippref = &gid_address_ippref (gid);
3176 ip_address_t *rloc = &ip_prefix_addr (ippref);
3178 memset (gid, 0, sizeof (gid[0]));
3179 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
3180 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
3182 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
3183 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
3185 /* Add ipv4 locators first TODO sort them */
3188 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3189 loc->sw_if_index, 1 /* unnumbered */,
3191 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
3192 ip_address_set (rloc, addr, IP4);
3193 ip_prefix_len (ippref) = 32;
3194 ip_prefix_normalize (ippref);
3195 vec_add1 (rlocs, gid[0]);
3198 /* Add ipv6 locators */
3199 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3200 loc->sw_if_index, 1 /* unnumbered */,
3202 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
3203 ip_address_set (rloc, addr, IP6);
3204 ip_prefix_len (ippref) = 128;
3205 ip_prefix_normalize (ippref);
3206 vec_add1 (rlocs, gid[0]);
3214 static vlib_buffer_t *
3215 build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
3216 ip_address_t * sloc, ip_address_t * rloc,
3217 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
3221 vlib_main_t *vm = lcm->vlib_main;
3223 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3225 clib_warning ("Can't allocate buffer for Map-Request!");
3229 b = vlib_get_buffer (vm, bi);
3231 /* leave some space for the encap headers */
3232 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3235 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
3236 1 /* rloc probe */ , nonce_res);
3238 /* push outer ip header */
3239 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3247 static vlib_buffer_t *
3248 build_encapsulated_map_request (lisp_cp_main_t * lcm,
3249 gid_address_t * seid, gid_address_t * deid,
3250 locator_set_t * loc_set, ip_address_t * mr_ip,
3251 ip_address_t * sloc, u8 is_smr_invoked,
3252 u64 * nonce_res, u32 * bi_res)
3256 gid_address_t *rlocs = 0;
3257 vlib_main_t *vm = lcm->vlib_main;
3259 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3261 clib_warning ("Can't allocate buffer for Map-Request!");
3265 b = vlib_get_buffer (vm, bi);
3267 /* leave some space for the encap headers */
3268 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3271 rlocs = build_itr_rloc_list (lcm, loc_set);
3273 if (MR_MODE_SRC_DST == lcm->map_request_mode
3274 && GID_ADDR_SRC_DST != gid_address_type (deid))
3277 memset (&sd, 0, sizeof (sd));
3278 build_src_dst (&sd, seid, deid);
3279 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
3280 0 /* rloc probe */ , nonce_res);
3285 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
3286 0 /* rloc probe */ , nonce_res);
3289 /* push ecm: udp-ip-lisp */
3290 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
3292 /* push outer ip header */
3293 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3303 reset_pending_mr_counters (pending_map_request_t * r)
3305 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3310 elect_map_resolver (lisp_cp_main_t * lcm)
3314 vec_foreach (mr, lcm->map_resolvers)
3318 ip_address_copy (&lcm->active_map_resolver, &mr->address);
3319 lcm->do_map_resolver_election = 0;
3327 free_map_register_records (mapping_t * maps)
3330 vec_foreach (map, maps) vec_free (map->locators);
3336 add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
3337 ip_address_t * probed_loc)
3340 locator_t *loc, new;
3341 ip_interface_address_t *ia = 0;
3343 ip_address_t *new_ip = &gid_address_ip (&new.address);
3346 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
3348 vec_foreach (li, ls->locator_indices)
3350 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
3355 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3356 loc->sw_if_index, 1 /* unnumbered */,
3358 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
3360 ip_address_set (new_ip, addr, IP4);
3363 /* Add ipv6 locators */
3364 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3365 loc->sw_if_index, 1 /* unnumbered */,
3367 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
3369 ip_address_set (new_ip, addr, IP6);
3373 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
3376 vec_add1 (m->locators, new);
3381 build_map_register_record_list (lisp_cp_main_t * lcm)
3383 mapping_t *recs = 0, rec, *m;
3386 pool_foreach(m, lcm->mapping_pool,
3388 /* for now build only local mappings */
3393 add_locators (lcm, &rec, m->locator_set_index, NULL);
3394 vec_add1 (recs, rec);
3402 update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
3403 lisp_key_type_t key_id, u8 * key,
3404 u16 auth_data_len, u32 msg_len)
3406 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
3407 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
3409 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3410 (unsigned char *) map_reg_hdr, msg_len, NULL,
3412 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
3417 static vlib_buffer_t *
3418 build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
3419 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
3420 mapping_t * records, lisp_key_type_t key_id, u8 * key,
3425 u32 bi, auth_data_len = 0, msg_len = 0;
3426 vlib_main_t *vm = lcm->vlib_main;
3428 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3430 clib_warning ("Can't allocate buffer for Map-Register!");
3434 b = vlib_get_buffer (vm, bi);
3436 /* leave some space for the encap headers */
3437 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3439 auth_data_len = auth_data_len_by_key_id (key_id);
3440 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
3441 auth_data_len, nonce_res,
3444 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
3447 /* push outer ip header */
3448 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3456 get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
3459 while (lcm->do_map_resolver_election
3460 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
3461 &lcm->active_map_resolver,
3464 if (0 == elect_map_resolver (lcm))
3465 /* all map resolvers are down */
3467 /* restart MR checking by marking all of them up */
3468 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
3476 send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
3477 u32 local_locator_set_index, ip_address_t * sloc,
3478 ip_address_t * rloc)
3485 u32 next_index, *to_next;
3486 gid_address_t *itr_rlocs;
3488 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
3489 itr_rlocs = build_itr_rloc_list (lcm, ls);
3491 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
3492 vec_free (itr_rlocs);
3496 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3498 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3499 ip4_lookup_node.index : ip6_lookup_node.index;
3501 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3503 /* Enqueue the packet */
3504 to_next = vlib_frame_vector_args (f);
3507 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3509 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3514 send_rloc_probes (lisp_cp_main_t * lcm)
3521 pool_foreach (e, lcm->fwd_entry_pool,
3523 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
3526 clib_warning ("internal error: cannot find local eid %U in "
3527 "map-cache!", format_gid_address, &e->leid);
3530 lm = pool_elt_at_index (lcm->mapping_pool, si);
3532 if (vec_len (e->locator_pairs) == 0)
3535 /* TODO send rloc-probe for each pair? for now assume there is only one
3537 locator_pair_t *lp = &e->locator_pairs[0];
3539 /* get first remote locator */
3540 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
3549 send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
3556 u32 next_index, *to_next;
3557 ip_address_t *ms = 0;
3559 // TODO: support multiple map servers and do election
3560 if (0 == vec_len (lcm->map_servers))
3563 ms = &lcm->map_servers[0].address;
3565 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
3567 clib_warning ("no eligible interface address found for %U!",
3568 format_ip_address, &lcm->map_servers[0]);
3572 /* TODO build mapping records grouped by secret key and send one map-register
3573 per group. Currently only one secret key per vpp is supported */
3574 mapping_t *records = build_map_register_record_list (lcm);
3578 u8 *key = records[0].key;
3579 u8 key_id = records[0].key_id;
3582 return 0; /* no secret key -> map-register cannot be sent */
3584 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, records,
3588 free_map_register_records (records);
3590 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3592 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3593 ip4_lookup_node.index : ip6_lookup_node.index;
3595 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3597 /* Enqueue the packet */
3598 to_next = vlib_frame_vector_args (f);
3601 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3603 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3607 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
3608 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
3610 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
3611 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
3614 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
3615 gid_address_t * seid, gid_address_t * deid,
3616 u8 is_smr_invoked, u8 is_resend)
3618 u32 next_index, bi = 0, *to_next, map_index;
3622 locator_set_t *loc_set;
3624 pending_map_request_t *pmr, *duplicate_pmr = 0;
3628 ASSERT (*lcm->pending_map_request_lock);
3630 /* if there is already a pending request remember it */
3633 pool_foreach(pmr, lcm->pending_map_requests_pool,
3635 if (!gid_address_cmp (&pmr->src, seid)
3636 && !gid_address_cmp (&pmr->dst, deid))
3638 duplicate_pmr = pmr;
3644 if (!is_resend && duplicate_pmr)
3646 /* don't send the request if there is a pending map request already */
3650 /* get locator-set for seid */
3651 if (!lcm->lisp_pitr)
3653 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
3654 if (map_index == ~0)
3656 clib_warning ("No local mapping found in eid-table for %U!",
3657 format_gid_address, seid);
3661 map = pool_elt_at_index (lcm->mapping_pool, map_index);
3666 ("Mapping found for src eid %U is not marked as local!",
3667 format_gid_address, seid);
3670 ls_index = map->locator_set_index;
3674 map_index = lcm->pitr_map_index;
3675 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
3676 ls_index = map->locator_set_index;
3679 /* overwrite locator set if map-request itr-rlocs configured */
3680 if (~0 != lcm->mreq_itr_rlocs)
3682 ls_index = lcm->mreq_itr_rlocs;
3685 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
3687 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
3690 duplicate_pmr->to_be_removed = 1;
3694 /* build the encapsulated map request */
3695 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
3696 &lcm->active_map_resolver,
3697 &sloc, is_smr_invoked, &nonce, &bi);
3702 /* set fib index to default and lookup node */
3703 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3704 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3705 ip4_lookup_node.index : ip6_lookup_node.index;
3707 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3709 /* Enqueue the packet */
3710 to_next = vlib_frame_vector_args (f);
3713 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3716 /* if there is a pending request already update it */
3718 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
3720 /* remove the oldest nonce */
3721 u64 CLIB_UNUSED (tmp), *nonce_del;
3722 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
3723 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
3724 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
3727 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
3728 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3729 duplicate_pmr - lcm->pending_map_requests_pool);
3733 /* add map-request to pending requests table */
3734 pool_get (lcm->pending_map_requests_pool, pmr);
3735 memset (pmr, 0, sizeof (*pmr));
3736 gid_address_copy (&pmr->src, seid);
3737 gid_address_copy (&pmr->dst, deid);
3738 clib_fifo_add1 (pmr->nonces, nonce);
3739 pmr->is_smr_invoked = is_smr_invoked;
3740 reset_pending_mr_counters (pmr);
3741 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3742 pmr - lcm->pending_map_requests_pool);
3749 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
3751 ip4_header_t *ip4 = hdr;
3754 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3756 ip_address_set (src, &ip4->src_address, IP4);
3757 ip_address_set (dst, &ip4->dst_address, IP4);
3762 ip_address_set (src, &ip6->src_address, IP6);
3763 ip_address_set (dst, &ip6->dst_address, IP6);
3768 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3772 u32 vni = ~0, table_id = ~0;
3774 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
3775 IP4 ? FIB_PROTOCOL_IP4 :
3781 vnip = hash_get (lcm->vni_by_table_id, table_id);
3785 clib_warning ("vrf %d is not mapped to any vni!", table_id);
3791 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3797 l2input_main_t *l2im = &l2input_main;
3798 l2_input_config_t *config;
3799 l2_bridge_domain_t *bd_config;
3801 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3802 config = vec_elt_at_index (l2im->configs, sw_if_index0);
3803 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3805 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
3809 clib_warning ("bridge domain %d is not mapped to any vni!",
3816 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3817 gid_address_t * src, gid_address_t * dst)
3822 memset (src, 0, sizeof (*src));
3823 memset (dst, 0, sizeof (*dst));
3824 type = vnet_buffer (b)->lisp.overlay_afi;
3826 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3829 u8 version, preflen;
3831 gid_address_type (src) = GID_ADDR_IP_PREFIX;
3832 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
3834 ip = vlib_buffer_get_current (b);
3835 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
3837 version = gid_address_ip_version (src);
3838 preflen = ip_address_max_len (version);
3839 gid_address_ippref_len (src) = preflen;
3840 gid_address_ippref_len (dst) = preflen;
3842 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3843 gid_address_vni (dst) = vni;
3844 gid_address_vni (src) = vni;
3846 else if (LISP_AFI_MAC == type)
3848 ethernet_header_t *eh;
3850 eh = vlib_buffer_get_current (b);
3852 gid_address_type (src) = GID_ADDR_MAC;
3853 gid_address_type (dst) = GID_ADDR_MAC;
3854 mac_copy (&gid_address_mac (src), eh->src_address);
3855 mac_copy (&gid_address_mac (dst), eh->dst_address);
3858 vni = lisp_get_vni_from_buffer_eth (lcm, b);
3860 gid_address_vni (dst) = vni;
3861 gid_address_vni (src) = vni;
3866 lisp_cp_lookup_inline (vlib_main_t * vm,
3867 vlib_node_runtime_t * node,
3868 vlib_frame_t * from_frame, int overlay)
3870 u32 *from, *to_next_drop, di, si;
3871 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3872 u32 pkts_mapped = 0;
3873 uword n_left_from, n_left_to_next_drop;
3875 from = vlib_frame_vector_args (from_frame);
3876 n_left_from = from_frame->n_vectors;
3878 while (n_left_from > 0)
3880 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3881 to_next_drop, n_left_to_next_drop);
3883 while (n_left_from > 0 && n_left_to_next_drop > 0)
3887 gid_address_t src, dst;
3892 to_next_drop[0] = pi0;
3894 n_left_to_next_drop -= 1;
3896 b0 = vlib_get_buffer (vm, pi0);
3897 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
3898 vnet_buffer (b0)->lisp.overlay_afi = overlay;
3900 /* src/dst eid pair */
3901 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
3903 /* if we have remote mapping for destination already in map-chache
3904 add forwarding tunnel directly. If not send a map-request */
3905 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3909 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3910 /* send a map-request also in case of negative mapping entry
3911 with corresponding action */
3912 if (m->action == LISP_SEND_MAP_REQUEST)
3914 /* send map-request */
3915 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3916 0 /* is_resend */ );
3921 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3925 dp_add_fwd_entry (lcm, si, di);
3931 /* send map-request */
3932 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3933 0 /* is_resend */ );
3937 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3939 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3942 memset (tr, 0, sizeof (*tr));
3943 gid_address_copy (&tr->dst_eid, &dst);
3944 ip_address_copy (&tr->map_resolver_ip,
3945 &lcm->active_map_resolver);
3947 gid_address_free (&dst);
3948 gid_address_free (&src);
3951 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3952 n_left_to_next_drop);
3954 vlib_node_increment_counter (vm, node->node_index,
3955 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
3957 return from_frame->n_vectors;
3961 lisp_cp_lookup_ip4 (vlib_main_t * vm,
3962 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3964 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3968 lisp_cp_lookup_ip6 (vlib_main_t * vm,
3969 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3971 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
3975 lisp_cp_lookup_l2 (vlib_main_t * vm,
3976 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3978 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
3982 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
3983 .function = lisp_cp_lookup_ip4,
3984 .name = "lisp-cp-lookup-ip4",
3985 .vector_size = sizeof (u32),
3986 .format_trace = format_lisp_cp_lookup_trace,
3987 .type = VLIB_NODE_TYPE_INTERNAL,
3989 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3990 .error_strings = lisp_cp_lookup_error_strings,
3992 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3995 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4001 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
4002 .function = lisp_cp_lookup_ip6,
4003 .name = "lisp-cp-lookup-ip6",
4004 .vector_size = sizeof (u32),
4005 .format_trace = format_lisp_cp_lookup_trace,
4006 .type = VLIB_NODE_TYPE_INTERNAL,
4008 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4009 .error_strings = lisp_cp_lookup_error_strings,
4011 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4014 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4020 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
4021 .function = lisp_cp_lookup_l2,
4022 .name = "lisp-cp-lookup-l2",
4023 .vector_size = sizeof (u32),
4024 .format_trace = format_lisp_cp_lookup_trace,
4025 .type = VLIB_NODE_TYPE_INTERNAL,
4027 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4028 .error_strings = lisp_cp_lookup_error_strings,
4030 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4033 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4038 /* lisp_cp_input statistics */
4039 #define foreach_lisp_cp_input_error \
4041 _(MAP_REPLIES_RECEIVED, "map-replies received")
4043 static char *lisp_cp_input_error_strings[] = {
4044 #define _(sym,string) string,
4045 foreach_lisp_cp_input_error
4051 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
4052 foreach_lisp_cp_input_error
4054 LISP_CP_INPUT_N_ERROR,
4055 } lisp_cp_input_error_t;
4059 LISP_CP_INPUT_NEXT_DROP,
4060 LISP_CP_INPUT_N_NEXT,
4061 } lisp_cp_input_next_t;
4065 gid_address_t dst_eid;
4066 ip4_address_t map_resolver_ip;
4067 } lisp_cp_input_trace_t;
4070 format_lisp_cp_input_trace (u8 * s, va_list * args)
4072 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
4073 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
4074 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
4075 va_arg (*args, lisp_cp_input_trace_t *);
4077 s = format (s, "LISP-CP-INPUT: TODO");
4082 remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
4086 m = pool_elt_at_index (lcm->mapping_pool, mi);
4087 lisp_add_del_adjacency (lcm, 0, &m->eid, 0 /* is_add */ );
4088 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
4089 0 /* is_static */ , 0);
4090 mapping_delete_timer (lcm, mi);
4094 mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
4095 f64 expiration_time)
4098 u64 now = clib_cpu_time_now ();
4099 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
4100 u64 exp_clock_time = now + expiration_time * cpu_cps;
4102 m = pool_elt_at_index (lcm->mapping_pool, mi);
4105 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
4109 process_map_reply (void *arg)
4111 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4112 vlib_buffer_t *b = arg;
4113 u32 len = 0, i, ttl, dst_map_index = 0;
4115 pending_map_request_t *pmr;
4117 map_reply_hdr_t *mrep_hdr;
4121 u8 authoritative, action;
4122 locator_t *locators = 0, *loc;
4124 mrep_hdr = vlib_buffer_get_current (b);
4126 lisp_pending_map_request_lock (lcm);
4128 /* Check pending requests table and nonce */
4129 nonce = MREP_NONCE (mrep_hdr);
4130 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, nonce);
4133 clib_warning ("No pending map-request entry with nonce %lu!", nonce);
4136 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
4138 vlib_buffer_pull (b, sizeof (*mrep_hdr));
4140 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
4142 h = vlib_buffer_get_current (b);
4143 ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
4144 action = MAP_REC_ACTION (h);
4145 authoritative = MAP_REC_AUTH (h);
4147 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
4150 clib_warning ("Failed to parse mapping record!");
4151 vec_foreach (loc, locators)
4155 vec_free (locators);
4159 /* insert/update mappings cache */
4160 vnet_lisp_add_del_mapping (&deid, locators, action, authoritative, ttl,
4161 1, 0 /* is_static */ , &dst_map_index);
4163 /* try to program forwarding only if mapping saved or updated */
4164 if ((u32) ~ 0 != dst_map_index)
4166 lisp_add_del_adjacency (lcm, &pmr->src, &deid, 1);
4167 if ((u32) ~ 0 != ttl)
4168 mapping_start_expiration_timer (lcm, dst_map_index, ttl * 60);
4171 vec_free (locators);
4174 /* remove pending map request entry */
4177 clib_fifo_foreach (noncep, pmr->nonces, ({
4178 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
4182 clib_fifo_free (pmr->nonces);
4183 pool_put (lcm->pending_map_requests_pool, pmr);
4186 lisp_pending_map_request_unlock (lcm);
4191 is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
4192 lisp_key_type_t key_id, u8 * key)
4198 auth_data_len = auth_data_len_by_key_id (key_id);
4199 if ((u16) ~ 0 == auth_data_len)
4201 clib_warning ("invalid length for key_id %d!", key_id);
4205 /* save auth data */
4206 vec_validate (auth_data, auth_data_len - 1);
4207 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
4209 /* clear auth data */
4210 memset (MNOTIFY_DATA (h), 0, auth_data_len);
4212 /* get hash of the message */
4213 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
4214 (unsigned char *) h, msg_len, NULL, NULL);
4216 result = memcmp (code, auth_data, auth_data_len);
4218 vec_free (auth_data);
4224 process_map_notify (vlib_main_t * vm, lisp_cp_main_t * lcm, vlib_buffer_t * b)
4226 lisp_key_type_t key_id;
4230 map_notify_hdr_t *mnotif_hdr;
4233 locator_t *locators = 0, *loc;
4234 u16 auth_data_len = 0;
4236 mnotif_hdr = vlib_buffer_get_current (b);
4237 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
4238 memset (&deid, 0, sizeof (deid));
4240 nonce = MNOTIFY_NONCE (mnotif_hdr);
4241 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
4242 auth_data_len = auth_data_len_by_key_id (key_id);
4244 pmr_index = hash_get (lcm->map_register_messages_by_nonce, nonce);
4247 clib_warning ("No pending map-register entry with nonce %lu!", nonce);
4251 /* advance buffer by authentication data */
4252 vlib_buffer_pull (b, auth_data_len);
4254 /* parse record eid */
4255 for (i = 0; i < MNOTIFY_REC_COUNT (mnotif_hdr); i++)
4257 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
4260 clib_warning ("Failed to parse mapping record!");
4261 vec_foreach (loc, locators)
4265 vec_free (locators);
4269 vec_free (locators);
4272 /* lookup eid in map-cache */
4273 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &deid);
4276 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
4281 m = pool_elt_at_index (lcm->mapping_pool, mi);
4284 clib_warning ("There is no secret key assigned to %U!",
4285 format_gid_address, &deid);
4289 /* verify authentication data */
4290 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
4291 - (u8 *) mnotif_hdr, m->key_id, m->key))
4293 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
4299 hash_unset (lcm->map_register_messages_by_nonce, nonce);
4300 gid_address_free (&deid);
4303 static vlib_buffer_t *
4304 build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
4305 ip_address_t * dst, u64 nonce, u8 probe_bit,
4306 mapping_t * records, u16 dst_port, u32 * bi_res)
4310 vlib_main_t *vm = lcm->vlib_main;
4312 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
4314 clib_warning ("Can't allocate buffer for Map-Register!");
4318 b = vlib_get_buffer (vm, bi);
4320 /* leave some space for the encap headers */
4321 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
4323 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
4325 /* push outer ip header */
4326 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
4333 send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
4334 u8 probe_bit, u64 nonce, u16 dst_port,
4335 ip_address_t * probed_loc)
4341 u32 next_index, *to_next;
4342 mapping_t *records = 0, *m;
4344 m = pool_elt_at_index (lcm->mapping_pool, mi);
4348 vec_add1 (records, m[0]);
4349 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
4350 memset (&src, 0, sizeof (src));
4352 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
4354 clib_warning ("can't find inteface address for %U", format_ip_address,
4359 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
4363 free_map_register_records (records);
4365 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
4366 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
4367 ip4_lookup_node.index : ip6_lookup_node.index;
4369 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
4371 /* Enqueue the packet */
4372 to_next = vlib_frame_vector_args (f);
4375 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
4380 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm,
4383 u8 *ip_hdr = 0, *udp_hdr;
4386 ip_address_t *dst_loc = 0, probed_loc, src_loc;
4388 map_request_hdr_t *mreq_hdr;
4389 gid_address_t src, dst;
4392 gid_address_t *itr_rlocs = 0, *rloc;
4394 mreq_hdr = vlib_buffer_get_current (b);
4396 // TODO ugly workaround to find out whether LISP is carried by ip4 or 6
4397 // and needs to be fixed
4398 udp_hdr = (u8 *) vlib_buffer_get_current (b) - sizeof (udp_header_t);
4399 ip4 = (ip4_header_t *) (udp_hdr - sizeof (ip4_header_t));
4400 ip6 = (ip6_header_t *) (udp_hdr - sizeof (ip6_header_t));
4402 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
4403 ip_hdr = (u8 *) ip4;
4406 u32 flags = clib_net_to_host_u32
4407 (ip6->ip_version_traffic_class_and_flow_label);
4408 if ((flags & 0xF0000000) == 0x60000000)
4409 ip_hdr = (u8 *) ip6;
4412 clib_warning ("internal error: cannot determine whether packet "
4418 vlib_buffer_pull (b, sizeof (*mreq_hdr));
4420 nonce = MREQ_NONCE (mreq_hdr);
4422 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
4425 ("Only SMR Map-Requests and RLOC probe supported for now!");
4430 len = lisp_msg_parse_addr (b, &src);
4434 /* for now we don't do anything with the itr's rlocs */
4436 lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
4437 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
4441 /* parse eid records and send SMR-invoked map-requests */
4442 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
4444 memset (&dst, 0, sizeof (dst));
4445 len = lisp_msg_parse_eid_rec (b, &dst);
4448 clib_warning ("Can't parse map-request EID-record");
4452 if (MREQ_SMR (mreq_hdr))
4454 /* send SMR-invoked map-requests */
4455 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
4457 else if (MREQ_RLOC_PROBE (mreq_hdr))
4459 memset (&m, 0, sizeof (m));
4460 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
4462 // TODO: select best locator; for now use the first one
4463 dst_loc = &gid_address_ip (&itr_rlocs[0]);
4465 /* get src/dst IP addresses */
4466 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
4468 // TODO get source port from buffer
4469 u16 src_port = LISP_CONTROL_PORT;
4471 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
4472 src_port, &probed_loc);
4477 vec_foreach (rloc, itr_rlocs)
4479 gid_address_free (rloc);
4484 queue_map_reply (vlib_buffer_t * b)
4486 vlib_buffer_t *a = clib_mem_alloc (sizeof (a[0]) + b->current_length);
4488 clib_memcpy (a->data, b->data + b->current_data, b->current_length);
4489 a->current_length = b->current_length;
4490 a->current_data = 0;
4492 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (a[0])
4493 + a->current_length);
4498 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
4499 vlib_frame_t * from_frame)
4501 u32 n_left_from, *from, *to_next_drop;
4502 lisp_msg_type_e type;
4503 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4505 from = vlib_frame_vector_args (from_frame);
4506 n_left_from = from_frame->n_vectors;
4509 while (n_left_from > 0)
4511 u32 n_left_to_next_drop;
4513 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4514 to_next_drop, n_left_to_next_drop);
4515 while (n_left_from > 0 && n_left_to_next_drop > 0)
4523 to_next_drop[0] = bi0;
4525 n_left_to_next_drop -= 1;
4527 b0 = vlib_get_buffer (vm, bi0);
4529 type = lisp_msg_type (vlib_buffer_get_current (b0));
4532 case LISP_MAP_REPLY:
4533 queue_map_reply (b0);
4535 case LISP_MAP_REQUEST:
4536 process_map_request (vm, lcm, b0);
4538 case LISP_MAP_NOTIFY:
4539 process_map_notify (vm, lcm, b0);
4542 clib_warning ("Unsupported LISP message type %d", type);
4546 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
4548 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
4554 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4555 n_left_to_next_drop);
4557 return from_frame->n_vectors;
4561 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
4562 .function = lisp_cp_input,
4563 .name = "lisp-cp-input",
4564 .vector_size = sizeof (u32),
4565 .format_trace = format_lisp_cp_input_trace,
4566 .type = VLIB_NODE_TYPE_INTERNAL,
4568 .n_errors = LISP_CP_INPUT_N_ERROR,
4569 .error_strings = lisp_cp_input_error_strings,
4571 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
4574 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
4580 lisp_cp_init (vlib_main_t * vm)
4582 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4583 clib_error_t *error = 0;
4585 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
4588 lcm->im4 = &ip4_main;
4589 lcm->im6 = &ip6_main;
4590 lcm->vlib_main = vm;
4591 lcm->vnet_main = vnet_get_main ();
4592 lcm->mreq_itr_rlocs = ~0;
4594 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
4596 lcm->pending_map_request_lock =
4597 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
4599 lcm->pending_map_request_lock[0] = 0;
4600 gid_dictionary_init (&lcm->mapping_index_by_gid);
4601 lcm->do_map_resolver_election = 1;
4602 lcm->map_request_mode = MR_MODE_DST_ONLY;
4604 /* default vrf mapped to vni 0 */
4605 hash_set (lcm->table_id_by_vni, 0, 0);
4606 hash_set (lcm->vni_by_table_id, 0, 0);
4608 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
4609 lisp_cp_input_node.index, 1 /* is_ip4 */ );
4610 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
4611 lisp_cp_input_node.index, 0 /* is_ip4 */ );
4613 u64 now = clib_cpu_time_now ();
4614 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
4619 send_map_request_thread_fn (void *arg)
4621 map_request_args_t *a = arg;
4622 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4625 /* if resending, we already have the lock */
4626 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4629 /* get lock before sending map-request */
4630 lisp_pending_map_request_lock (lcm);
4631 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4632 lisp_pending_map_request_unlock (lcm);
4640 queue_map_request (gid_address_t * seid, gid_address_t * deid,
4641 u8 smr_invoked, u8 is_resend)
4643 map_request_args_t a;
4645 a.is_resend = is_resend;
4646 gid_address_copy (&a.seid, seid);
4647 gid_address_copy (&a.deid, deid);
4648 a.smr_invoked = smr_invoked;
4650 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
4651 (u8 *) & a, sizeof (a));
4656 * Take an action with a pending map request depending on expiration time
4657 * and re-try counters.
4660 update_pending_request (pending_map_request_t * r, f64 dt)
4662 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4665 if (r->time_to_expire - dt < 0)
4666 /* it's time to decide what to do with this pending request */
4668 if (r->retries_num >= NUMBER_OF_RETRIES)
4669 /* too many retries -> assume current map resolver is not available */
4671 mr = get_map_resolver (&lcm->active_map_resolver);
4674 clib_warning ("Map resolver %U not found - probably deleted "
4675 "by the user recently.", format_ip_address,
4676 &lcm->active_map_resolver);
4680 clib_warning ("map resolver %U is unreachable, ignoring",
4681 format_ip_address, &lcm->active_map_resolver);
4683 /* mark current map resolver unavailable so it won't be
4684 * selected next time */
4686 mr->last_update = vlib_time_now (lcm->vlib_main);
4689 reset_pending_mr_counters (r);
4690 elect_map_resolver (lcm);
4692 /* try to find a next eligible map resolver and re-send */
4693 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4699 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4702 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
4706 r->time_to_expire -= dt;
4710 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4713 pending_map_request_t *pmr;
4714 u32 *to_be_removed = 0, *pmr_index;
4716 ASSERT (*lcm->pending_map_request_lock);
4719 pool_foreach (pmr, lcm->pending_map_requests_pool,
4721 if (pmr->to_be_removed)
4723 clib_fifo_foreach (nonce, pmr->nonces, ({
4724 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4727 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4732 vec_foreach (pmr_index, to_be_removed)
4733 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
4735 vec_free (to_be_removed);
4739 update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4741 static f64 time_left = RLOC_PROBING_INTERVAL;
4743 if (!lcm->is_enabled || !lcm->rloc_probing)
4749 time_left = RLOC_PROBING_INTERVAL;
4750 send_rloc_probes (lcm);
4755 update_map_register (lisp_cp_main_t * lcm, f64 dt)
4757 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4758 static u64 mreg_sent_counter = 0;
4760 if (!lcm->is_enabled || !lcm->map_registering)
4766 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4767 time_left = MAP_REGISTER_INTERVAL;
4770 mreg_sent_counter++;
4771 time_left = QUICK_MAP_REGISTER_INTERVAL;
4773 send_map_register (lcm, 1 /* want map notify */ );
4778 send_map_resolver_service (vlib_main_t * vm,
4779 vlib_node_runtime_t * rt, vlib_frame_t * f)
4783 pending_map_request_t *pmr;
4784 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4788 vlib_process_wait_for_event_or_clock (vm, period);
4790 /* currently no signals are expected - just wait for clock */
4791 (void) vlib_process_get_events (vm, 0);
4793 lisp_pending_map_request_lock (lcm);
4796 pool_foreach (pmr, lcm->pending_map_requests_pool,
4798 if (!pmr->to_be_removed)
4799 update_pending_request (pmr, period);
4803 remove_dead_pending_map_requests (lcm);
4804 lisp_pending_map_request_unlock (lcm);
4806 update_map_register (lcm, period);
4807 update_rloc_probing (lcm, period);
4809 u64 now = clib_cpu_time_now ();
4811 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
4812 if (vec_len (expired) > 0)
4815 vec_foreach (mi, expired)
4817 remove_expired_mapping (lcm, mi[0]);
4819 _vec_len (expired) = 0;
4828 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
4829 .function = send_map_resolver_service,
4830 .type = VLIB_NODE_TYPE_PROCESS,
4831 .name = "lisp-retry-service",
4832 .process_log2_n_stack_bytes = 16,
4836 VLIB_INIT_FUNCTION (lisp_cp_init);
4839 * fd.io coding-style-patch-verification: ON
4842 * eval: (c-set-style "gnu")