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>
45 lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
46 gid_address_t * remote_eid, u8 is_add);
49 vnet_lisp_get_map_request_mode (void)
51 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
52 return lcm->map_request_mode;
56 auth_data_len_by_key_id (lisp_key_type_t key_id)
61 return SHA1_AUTH_DATA_LEN;
62 case HMAC_SHA_256_128:
63 return SHA256_AUTH_DATA_LEN;
65 clib_warning ("unsupported key type: %d!", key_id);
72 get_encrypt_fcn (lisp_key_type_t key_id)
78 case HMAC_SHA_256_128:
81 clib_warning ("unsupported encryption key type: %d!", key_id);
88 queue_map_request (gid_address_t * seid, gid_address_t * deid,
89 u8 smr_invoked, u8 is_resend);
91 ip_interface_address_t *
92 ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
93 u32 sw_if_index, u8 loop)
95 vnet_main_t *vnm = vnet_get_main ();
96 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
97 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
98 sw_if_index = swif->unnumbered_sw_if_index;
100 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
101 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
103 return pool_elt_at_index ((lm)->if_address_pool, ia);
107 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
110 ip_interface_address_t *ia;
112 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
115 return ip_interface_address_get_address (lm, ia);
119 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
120 u8 version, ip_address_t * result)
122 ip_lookup_main_t *lm;
125 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
126 addr = ip_interface_get_first_address (lm, sw_if_index, version);
130 ip_address_set (result, addr, version);
135 * convert from a LISP address to a FIB prefix
138 ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
140 if (addr->version == IP4)
143 prefix->fp_proto = FIB_PROTOCOL_IP4;
144 memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
145 memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
149 prefix->fp_len = 128;
150 prefix->fp_proto = FIB_PROTOCOL_IP6;
151 memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
156 * convert from a LISP to a FIB prefix
159 ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
160 fib_prefix_t * fib_prefix)
162 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
163 fib_prefix->fp_len = ip_prefix->len;
167 * Find the sw_if_index of the interface that would be used to egress towards
171 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
173 fib_node_index_t fei;
176 ip_address_to_fib_prefix (dst, &prefix);
178 fei = fib_table_lookup (0, &prefix);
180 return (fib_entry_get_resolving_interface (fei));
184 * Find first IP of the interface that would be used to egress towards dst.
185 * Returns 1 if the address is found 0 otherwise.
188 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
189 ip_address_t * result)
192 ip_lookup_main_t *lm;
196 ASSERT (result != 0);
198 ipver = ip_addr_version (dst);
200 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
201 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
206 /* find the first ip address */
207 addr = ip_interface_get_first_address (lm, si, ipver);
211 ip_address_set (result, addr, ipver);
216 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
222 dp_table = hash_get (lcm->table_id_by_vni, vni);
226 clib_warning ("vni %d not associated to a vrf!", vni);
227 return VNET_API_ERROR_INVALID_VALUE;
232 dp_table = hash_get (lcm->bd_id_by_vni, vni);
235 clib_warning ("vni %d not associated to a bridge domain!", vni);
236 return VNET_API_ERROR_INVALID_VALUE;
240 /* enable/disable data-plane interface */
244 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
246 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
251 lisp_gpe_tenant_l2_iface_unlock (vni);
253 lisp_gpe_tenant_l3_iface_unlock (vni);
260 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
262 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
265 memset (a, 0, sizeof (*a));
267 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
271 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
273 /* delete dp fwd entry */
276 a->locator_pairs = fe->locator_pairs;
277 a->vni = gid_address_vni (&fe->reid);
278 gid_address_copy (&a->rmt_eid, &fe->reid);
280 gid_address_copy (&a->lcl_eid, &fe->leid);
282 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
284 /* delete entry in fwd table */
285 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
286 vec_free (fe->locator_pairs);
287 pool_put (lcm->fwd_entry_pool, fe);
291 * Finds first remote locator with best (lowest) priority that has a local
292 * peer locator with an underlying route to it.
296 get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
297 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
299 u32 i, limitp = 0, li, found = 0, esi;
300 locator_set_t *rmt_ls, *lcl_ls;
301 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
302 locator_t *lp, *rmt = 0;
307 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
309 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
311 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
318 /* find unvisited remote locator with best priority */
319 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
321 if (0 != hash_get (checked, i))
324 li = vec_elt (rmt_ls->locator_indices, i);
325 lp = pool_elt_at_index (lcm->locator_pool, li);
327 /* we don't support non-IP locators for now */
328 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
331 if ((found && lp->priority == limitp)
332 || (!found && lp->priority >= limitp))
336 /* don't search for locators with lower priority and don't
337 * check this locator again*/
338 limitp = lp->priority;
339 hash_set (checked, i, 1);
343 /* check if a local locator with a route to remote locator exists */
346 /* find egress sw_if_index for rmt locator */
348 ip_fib_get_egress_iface_for_dst (lcm,
349 &gid_address_ip (&rmt->address));
350 if ((u32) ~ 0 == esi)
353 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
355 li = vec_elt (lcl_ls->locator_indices, i);
356 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
358 /* found local locator with the needed sw_if_index */
359 if (sl->sw_if_index == esi)
361 /* and it has an address */
362 if (0 == ip_interface_get_first_ip_address (lcm,
364 gid_address_ip_version
369 memset (&pair, 0, sizeof (pair));
370 ip_address_copy (&pair.rmt_loc,
371 &gid_address_ip (&rmt->address));
372 ip_address_copy (&pair.lcl_loc, lcl_addr);
373 pair.weight = rmt->weight;
374 pair.priority = rmt->priority;
375 vec_add1 (locator_pairs[0], pair);
389 gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
392 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
396 switch (fid_addr_type (fid))
398 case FID_ADDR_IP_PREF:
399 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
400 gid_address_ippref (dst) = fid_addr_ippref (fid);
403 gid_address_type (dst) = GID_ADDR_MAC;
404 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
407 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
413 vnet_lisp_map_register_state_get (void)
415 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
416 return lcm->map_registering;
420 vnet_lisp_rloc_probe_state_get (void)
422 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
423 return lcm->rloc_probing;
427 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
429 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
430 mapping_t *src_map, *dst_map;
432 uword *feip = 0, *dpid;
434 u8 type, is_src_dst = 0;
436 memset (a, 0, sizeof (*a));
438 /* remove entry if it already exists */
439 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
441 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
444 src_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
446 src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
447 dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
449 /* insert data plane forwarding entry */
452 if (MR_MODE_SRC_DST == lcm->map_request_mode)
454 if (GID_ADDR_SRC_DST == gid_address_type (&dst_map->eid))
456 gid_address_sd_to_flat (&a->rmt_eid, &dst_map->eid,
457 &gid_address_sd_dst (&dst_map->eid));
458 gid_address_sd_to_flat (&a->lcl_eid, &dst_map->eid,
459 &gid_address_sd_src (&dst_map->eid));
463 gid_address_copy (&a->rmt_eid, &dst_map->eid);
464 gid_address_copy (&a->lcl_eid, &src_map->eid);
469 gid_address_copy (&a->rmt_eid, &dst_map->eid);
471 a->vni = gid_address_vni (&a->rmt_eid);
473 /* get vrf or bd_index associated to vni */
474 type = gid_address_type (&a->rmt_eid);
475 if (GID_ADDR_IP_PREFIX == type)
477 dpid = hash_get (lcm->table_id_by_vni, a->vni);
480 clib_warning ("vni %d not associated to a vrf!", a->vni);
483 a->table_id = dpid[0];
485 else if (GID_ADDR_MAC == type)
487 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
490 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
496 /* find best locator pair that 1) verifies LISP policy 2) are connected */
497 if (0 == get_locator_pairs (lcm, src_map, dst_map, &a->locator_pairs))
501 a->action = dst_map->action;
505 u8 ipver = ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
506 a->decap_next_index = (ipver == IP4) ?
507 LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
509 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
511 /* add tunnel to fwd entry table XXX check return value from DP insertion */
512 pool_get (lcm->fwd_entry_pool, fe);
513 fe->locator_pairs = a->locator_pairs;
514 gid_address_copy (&fe->reid, &a->rmt_eid);
515 gid_address_copy (&fe->leid, &src_map->eid);
516 fe->is_src_dst = is_src_dst;
517 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
518 fe - lcm->fwd_entry_pool);
522 * Returns vector of adjacencies.
524 * The caller must free the vector returned by this function.
526 * @param vni virtual network identifier
527 * @return vector of adjacencies
530 vnet_lisp_adjacencies_get_by_vni (u32 vni)
532 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
534 lisp_adjacency_t *adjs = 0, adj;
537 pool_foreach(fwd, lcm->fwd_entry_pool,
539 if (gid_address_vni (&fwd->reid) != vni)
542 gid_address_copy (&adj.reid, &fwd->reid);
543 gid_address_copy (&adj.leid, &fwd->leid);
544 vec_add1 (adjs, adj);
551 static clib_error_t *
552 lisp_show_adjacencies_command_fn (vlib_main_t * vm,
553 unformat_input_t * input,
554 vlib_cli_command_t * cmd)
556 lisp_adjacency_t *adjs, *adj;
557 vlib_cli_output (vm, "%s %40s\n", "leid", "reid");
558 unformat_input_t _line_input, *line_input = &_line_input;
561 /* Get a line of input. */
562 if (!unformat_user (input, unformat_line_input, line_input))
565 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
567 if (unformat (line_input, "vni %d", &vni))
571 vlib_cli_output (vm, "parse error: '%U'",
572 format_unformat_error, line_input);
579 vlib_cli_output (vm, "error: no vni specified!");
583 adjs = vnet_lisp_adjacencies_get_by_vni (vni);
585 vec_foreach (adj, adjs)
587 vlib_cli_output (vm, "%U %40U\n", format_gid_address, &adj->leid,
588 format_gid_address, &adj->reid);
596 VLIB_CLI_COMMAND (lisp_show_adjacencies_command) = {
597 .path = "show lisp adjacencies",
598 .short_help = "show lisp adjacencies",
599 .function = lisp_show_adjacencies_command_fn,
604 get_map_server (ip_address_t * a)
606 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
609 vec_foreach (m, lcm->map_servers)
611 if (!ip_address_cmp (&m->address, a))
620 get_map_resolver (ip_address_t * a)
622 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
625 vec_foreach (m, lcm->map_resolvers)
627 if (!ip_address_cmp (&m->address, a))
636 vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
639 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
640 lisp_msmr_t _ms, *ms = &_ms;
642 if (vnet_lisp_enable_disable_status () == 0)
644 clib_warning ("LISP is disabled!");
645 return VNET_API_ERROR_LISP_DISABLED;
650 if (get_map_server (addr))
652 clib_warning ("map-server %U already exists!", format_ip_address,
657 memset (ms, 0, sizeof (*ms));
658 ip_address_copy (&ms->address, addr);
659 vec_add1 (lcm->map_servers, ms[0]);
663 for (i = 0; i < vec_len (lcm->map_servers); i++)
665 ms = vec_elt_at_index (lcm->map_servers, i);
666 if (!ip_address_cmp (&ms->address, addr))
668 vec_del1 (lcm->map_servers, i);
677 static clib_error_t *
678 lisp_add_del_map_server_command_fn (vlib_main_t * vm,
679 unformat_input_t * input,
680 vlib_cli_command_t * cmd)
683 u8 is_add = 1, ip_set = 0;
685 unformat_input_t _line_input, *line_input = &_line_input;
687 /* Get a line of input. */
688 if (!unformat_user (input, unformat_line_input, line_input))
691 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
693 if (unformat (line_input, "add"))
695 else if (unformat (line_input, "del"))
697 else if (unformat (line_input, "%U", unformat_ip_address, &ip))
701 vlib_cli_output (vm, "parse error: '%U'",
702 format_unformat_error, line_input);
709 vlib_cli_output (vm, "map-server ip address not set!");
713 rv = vnet_lisp_add_del_map_server (&ip, is_add);
715 vlib_cli_output (vm, "failed to %s map-server!",
716 is_add ? "add" : "delete");
722 VLIB_CLI_COMMAND (lisp_add_del_map_server_command) = {
723 .path = "lisp map-server",
724 .short_help = "lisp map-server add|del <ip>",
725 .function = lisp_add_del_map_server_command_fn,
730 * Add/remove mapping to/from map-cache. Overwriting not allowed.
733 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
734 u32 * map_index_result)
736 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
737 u32 mi, *map_indexp, map_index, i;
738 mapping_t *m, *old_map;
741 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
742 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
745 /* TODO check if overwriting and take appropriate actions */
746 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
748 clib_warning ("eid %U found in the eid-table", format_gid_address,
750 return VNET_API_ERROR_VALUE_EXIST;
753 pool_get (lcm->mapping_pool, m);
754 gid_address_copy (&m->eid, &a->eid);
755 m->locator_set_index = a->locator_set_index;
757 m->action = a->action;
759 m->is_static = a->is_static;
760 m->key = vec_dup (a->key);
761 m->key_id = a->key_id;
763 map_index = m - lcm->mapping_pool;
764 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
767 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
769 clib_warning ("Locator set with index %d doesn't exist",
770 a->locator_set_index);
771 return VNET_API_ERROR_INVALID_VALUE;
774 /* add eid to list of eids supported by locator-set */
775 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
776 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
777 a->locator_set_index);
778 vec_add1 (eid_indexes[0], map_index);
783 vec_add1 (lcm->local_mappings_indexes, map_index);
785 map_index_result[0] = map_index;
789 if (mi == GID_LOOKUP_MISS)
791 clib_warning ("eid %U not found in the eid-table",
792 format_gid_address, &a->eid);
793 return VNET_API_ERROR_INVALID_VALUE;
796 /* clear locator-set to eids binding */
797 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
798 a->locator_set_index);
799 for (i = 0; i < vec_len (eid_indexes[0]); i++)
801 map_indexp = vec_elt_at_index (eid_indexes[0], i);
802 if (map_indexp[0] == mi)
805 vec_del1 (eid_indexes[0], i);
807 /* remove local mark if needed */
808 m = pool_elt_at_index (lcm->mapping_pool, mi);
812 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
814 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
815 if (lm_indexp[0] == mi)
818 vec_del1 (lcm->local_mappings_indexes, k);
821 /* remove mapping from dictionary */
822 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
823 gid_address_free (&m->eid);
824 pool_put_index (lcm->mapping_pool, mi);
831 * Add/update/delete mapping to/in/from map-cache.
834 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
835 u32 * map_index_result)
841 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
843 if (vnet_lisp_enable_disable_status () == 0)
845 clib_warning ("LISP is disabled!");
846 return VNET_API_ERROR_LISP_DISABLED;
849 vni = gid_address_vni (&a->eid);
850 type = gid_address_type (&a->eid);
851 if (GID_ADDR_IP_PREFIX == type)
852 dp_table = hash_get (lcm->table_id_by_vni, vni);
853 else if (GID_ADDR_MAC == type)
854 dp_table = hash_get (lcm->bd_id_by_vni, vni);
858 clib_warning ("vni %d not associated to a %s!", vni,
859 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
860 return VNET_API_ERROR_INVALID_VALUE;
863 /* store/remove mapping from map-cache */
864 return vnet_lisp_map_cache_add_del (a, map_index_result);
867 static clib_error_t *
868 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
869 vlib_cli_command_t * cmd)
871 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
872 unformat_input_t _line_input, *line_input = &_line_input;
875 gid_address_t *eids = 0;
876 clib_error_t *error = 0;
877 u8 *locator_set_name = 0;
878 u32 locator_set_index = 0, map_index = 0;
880 vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
886 memset (&eid, 0, sizeof (eid));
887 memset (a, 0, sizeof (*a));
889 /* Get a line of input. */
890 if (!unformat_user (input, unformat_line_input, line_input))
893 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
895 if (unformat (line_input, "add"))
897 else if (unformat (line_input, "del"))
899 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
901 else if (unformat (line_input, "vni %d", &vni))
902 gid_address_vni (&eid) = vni;
903 else if (unformat (line_input, "secret-key %_%v%_", &key))
905 else if (unformat (line_input, "key-id %U", unformat_hmac_key_id,
908 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
910 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
913 error = clib_error_return (0, "locator-set %s doesn't exist",
917 locator_set_index = p[0];
921 error = unformat_parse_error (line_input);
925 /* XXX treat batch configuration */
927 if (GID_ADDR_SRC_DST == gid_address_type (&eid))
930 clib_error_return (0, "src/dst is not supported for local EIDs!");
934 if (key && (0 == key_id))
936 vlib_cli_output (vm, "invalid key_id!");
940 gid_address_copy (&a->eid, &eid);
942 a->locator_set_index = locator_set_index;
947 rv = vnet_lisp_add_del_local_mapping (a, &map_index);
950 error = clib_error_return (0, "failed to %s local mapping!",
951 is_add ? "add" : "delete");
955 if (locator_set_name)
956 vec_free (locator_set_name);
957 gid_address_free (&a->eid);
963 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
964 .path = "lisp eid-table",
965 .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
966 "locator-set <locator-set> [key <secret-key> key-id sha1|sha256 ]",
967 .function = lisp_add_del_local_eid_command_fn,
972 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
974 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
975 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
977 if (vnet_lisp_enable_disable_status () == 0)
979 clib_warning ("LISP is disabled!");
983 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
984 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
986 if (!is_l2 && (vni == 0 || dp_id == 0))
988 clib_warning ("can't add/del default vni-vrf mapping!");
992 dp_idp = hash_get (dp_table_by_vni[0], vni);
993 vnip = hash_get (vni_by_dp_table[0], dp_id);
999 clib_warning ("vni %d or vrf %d already used in vrf/vni "
1000 "mapping!", vni, dp_id);
1003 hash_set (dp_table_by_vni[0], vni, dp_id);
1004 hash_set (vni_by_dp_table[0], dp_id, vni);
1006 /* create dp iface */
1007 dp_add_del_iface (lcm, vni, is_l2, 1);
1011 if (!dp_idp || !vnip)
1013 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
1014 "mapping!", vni, dp_id);
1017 hash_unset (dp_table_by_vni[0], vni);
1018 hash_unset (vni_by_dp_table[0], dp_id);
1020 /* remove dp iface */
1021 dp_add_del_iface (lcm, vni, is_l2, 0);
1027 static clib_error_t *
1028 lisp_eid_table_map_command_fn (vlib_main_t * vm,
1029 unformat_input_t * input,
1030 vlib_cli_command_t * cmd)
1032 u8 is_add = 1, is_l2 = 0;
1033 u32 vni = 0, dp_id = 0;
1034 unformat_input_t _line_input, *line_input = &_line_input;
1036 /* Get a line of input. */
1037 if (!unformat_user (input, unformat_line_input, line_input))
1040 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1042 if (unformat (line_input, "del"))
1044 else if (unformat (line_input, "vni %d", &vni))
1046 else if (unformat (line_input, "vrf %d", &dp_id))
1048 else if (unformat (line_input, "bd %d", &dp_id))
1052 return unformat_parse_error (line_input);
1055 vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
1060 VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
1061 .path = "lisp eid-table map",
1062 .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
1063 .function = lisp_eid_table_map_command_fn,
1067 /* return 0 if the two locator sets are identical 1 otherwise */
1069 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
1070 locator_t * new_locators)
1073 locator_t *old_loc, *new_loc;
1075 if (vec_len (old_ls_indexes) != vec_len (new_locators))
1078 for (i = 0; i < vec_len (new_locators); i++)
1080 old_li = vec_elt (old_ls_indexes, i);
1081 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
1083 new_loc = vec_elt_at_index (new_locators, i);
1085 if (locator_cmp (old_loc, new_loc))
1095 gid_address_t *eids_to_be_deleted;
1096 } remove_mapping_args_t;
1099 * Callback invoked when a sub-prefix is found
1102 remove_mapping_if_needed (u32 mi, void *arg)
1105 remove_mapping_args_t *a = arg;
1106 lisp_cp_main_t *lcm = a->lcm;
1110 m = pool_elt_at_index (lcm->mapping_pool, mi);
1114 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1118 if (0 != vec_len (ls->locator_indices))
1123 if (0 == vec_len (ls->locator_indices))
1128 vec_add1 (a->eids_to_be_deleted, m->eid);
1132 * This function searches map cache and looks for IP prefixes that are subset
1133 * of the provided one. If such prefix is found depending on 'is_negative'
1136 * 1) if is_negative is true and found prefix points to positive mapping,
1137 * then the mapping is removed
1138 * 2) if is_negative is false and found prefix points to negative mapping,
1139 * then the mapping is removed
1142 remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1146 remove_mapping_args_t a;
1147 memset (&a, 0, sizeof (a));
1149 /* do this only in src/dst mode ... */
1150 if (MR_MODE_SRC_DST != lcm->map_request_mode)
1153 /* ... and only for IP prefix */
1154 if (GID_ADDR_SRC_DST != gid_address_type (eid)
1155 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1158 a.is_negative = is_negative;
1161 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1162 remove_mapping_if_needed, &a);
1164 vec_foreach (e, a.eids_to_be_deleted)
1166 lisp_add_del_adjacency (lcm, 0, e, 0 /* is_add */ );
1167 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
1170 vec_free (a.eids_to_be_deleted);
1174 mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1176 timing_wheel_delete (&lcm->wheel, mi);
1180 * Adds/removes/updates mapping. Does not program forwarding.
1182 * @param eid end-host identifier
1183 * @param rlocs vector of remote locators
1184 * @param action action for negative map-reply
1185 * @param is_add add mapping if non-zero, delete otherwise
1186 * @param res_map_index the map-index that was created/updated/removed. It is
1187 * set to ~0 if no action is taken.
1188 * @param is_static used for distinguishing between statically learned
1189 remote mappings and mappings obtained from MR
1190 * @return return code
1193 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
1194 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
1195 u32 * res_map_index)
1197 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1198 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1199 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1200 u32 mi, ls_index = 0, dst_map_index;
1203 if (vnet_lisp_enable_disable_status () == 0)
1205 clib_warning ("LISP is disabled!");
1206 return VNET_API_ERROR_LISP_DISABLED;
1210 res_map_index[0] = ~0;
1212 memset (m_args, 0, sizeof (m_args[0]));
1213 memset (ls_args, 0, sizeof (ls_args[0]));
1215 ls_args->locators = rlocs;
1217 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
1218 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1222 /* overwrite: if mapping already exists, decide if locators should be
1223 * updated and be done */
1224 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
1226 if (!is_static && (old_map->is_static || old_map->local))
1228 /* do not overwrite local or static remote mappings */
1229 clib_warning ("mapping %U rejected due to collision with local "
1230 "or static remote mapping!", format_gid_address,
1235 locator_set_t *old_ls;
1237 /* update mapping attributes */
1238 old_map->action = action;
1239 old_map->authoritative = authoritative;
1242 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1243 old_map->locator_set_index);
1244 if (compare_locators (lcm, old_ls->locator_indices,
1247 /* set locator-set index to overwrite */
1248 ls_args->is_add = 1;
1249 ls_args->index = old_map->locator_set_index;
1250 vnet_lisp_add_del_locator_set (ls_args, 0);
1252 res_map_index[0] = mi;
1258 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1260 ls_args->is_add = 1;
1261 ls_args->index = ~0;
1263 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1266 gid_address_copy (&m_args->eid, eid);
1268 m_args->action = action;
1269 m_args->locator_set_index = ls_index;
1270 m_args->is_static = is_static;
1272 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
1275 res_map_index[0] = dst_map_index;
1280 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
1282 clib_warning ("cannot delete mapping for eid %U",
1283 format_gid_address, eid);
1288 gid_address_copy (&m_args->eid, eid);
1289 m_args->locator_set_index = old_map->locator_set_index;
1291 /* delete mapping associated from map-cache */
1292 vnet_lisp_map_cache_add_del (m_args, 0);
1294 ls_args->is_add = 0;
1295 ls_args->index = old_map->locator_set_index;
1296 /* delete locator set */
1297 vnet_lisp_add_del_locator_set (ls_args, 0);
1299 /* delete timer associated to the mapping if any */
1300 if (old_map->timer_set)
1301 mapping_delete_timer (lcm, mi);
1303 /* return old mapping index */
1305 res_map_index[0] = mi;
1313 vnet_lisp_clear_all_remote_adjacencies (void)
1316 u32 mi, *map_indices = 0, *map_indexp;
1317 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1318 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1319 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1322 pool_foreach_index (mi, lcm->mapping_pool,
1324 vec_add1 (map_indices, mi);
1328 vec_foreach (map_indexp, map_indices)
1330 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1333 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
1335 dm_args->is_add = 0;
1336 gid_address_copy (&dm_args->eid, &map->eid);
1337 dm_args->locator_set_index = map->locator_set_index;
1339 /* delete mapping associated to fwd entry */
1340 vnet_lisp_map_cache_add_del (dm_args, 0);
1344 ls->index = map->locator_set_index;
1345 /* delete locator set */
1346 rv = vnet_lisp_add_del_locator_set (ls, 0);
1354 vec_free (map_indices);
1359 * Adds adjacency or removes forwarding entry associated to remote mapping.
1360 * Note that adjacencies are not stored, they only result in forwarding entries
1364 lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
1365 gid_address_t * remote_eid, u8 is_add)
1367 u32 local_mi, remote_mi = ~0;
1369 if (vnet_lisp_enable_disable_status () == 0)
1371 clib_warning ("LISP is disabled!");
1372 return VNET_API_ERROR_LISP_DISABLED;
1375 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1376 remote_eid, local_eid);
1377 if (GID_LOOKUP_MISS == remote_mi)
1379 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1380 format_gid_address, remote_eid);
1387 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1390 /* check if source eid has an associated mapping. If pitr mode is on,
1391 * just use the pitr's mapping */
1392 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
1393 gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
1396 if (GID_LOOKUP_MISS == local_mi)
1398 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1399 format_gid_address, local_eid);
1404 /* update forwarding */
1405 dp_add_fwd_entry (lcm, local_mi, remote_mi);
1408 dp_del_fwd_entry (lcm, 0, remote_mi);
1414 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1416 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1417 return lisp_add_del_adjacency (lcm, &a->leid, &a->reid, a->is_add);
1421 * Handler for add/del remote mapping CLI.
1423 * @param vm vlib context
1424 * @param input input from user
1426 * @return pointer to clib error structure
1428 static clib_error_t *
1429 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
1430 unformat_input_t * input,
1431 vlib_cli_command_t * cmd)
1433 clib_error_t *error = 0;
1434 unformat_input_t _line_input, *line_input = &_line_input;
1435 u8 is_add = 1, del_all = 0;
1436 locator_t rloc, *rlocs = 0, *curr_rloc = 0;
1439 u32 vni, action = ~0, p, w;
1442 /* Get a line of input. */
1443 if (!unformat_user (input, unformat_line_input, line_input))
1446 memset (&eid, 0, sizeof (eid));
1447 memset (&rloc, 0, sizeof (rloc));
1449 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1451 if (unformat (line_input, "del-all"))
1453 else if (unformat (line_input, "del"))
1455 else if (unformat (line_input, "add"))
1457 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1459 else if (unformat (line_input, "vni %u", &vni))
1461 gid_address_vni (&eid) = vni;
1463 else if (unformat (line_input, "p %d w %d", &p, &w))
1468 ("No RLOC configured for setting priority/weight!");
1471 curr_rloc->priority = p;
1472 curr_rloc->weight = w;
1474 else if (unformat (line_input, "rloc %U", unformat_ip_address,
1475 &gid_address_ip (&rloc.address)))
1477 /* since rloc is stored in ip prefix we need to set prefix length */
1478 ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
1480 u8 version = gid_address_ip_version (&rloc.address);
1481 ip_prefix_len (pref) = ip_address_max_len (version);
1483 vec_add1 (rlocs, rloc);
1484 curr_rloc = &rlocs[vec_len (rlocs) - 1];
1486 else if (unformat (line_input, "action %U",
1487 unformat_negative_mapping_action, &action))
1491 clib_warning ("parse error");
1498 clib_warning ("missing eid!");
1504 if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1506 clib_warning ("no action set for negative map-reply!");
1512 vnet_lisp_clear_all_remote_adjacencies ();
1516 /* TODO build src/dst with seid */
1518 /* if it's a delete, clean forwarding */
1521 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1522 rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1529 /* add as static remote mapping, i.e., not authoritative and infinite
1531 rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
1532 1 /* is_static */ , 0);
1535 clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
1539 unformat_free (line_input);
1543 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) =
1545 .path = "lisp remote-mapping",.short_help =
1546 "lisp remote-mapping add|del [del-all] vni <vni> "
1547 "eid <est-eid> [action <no-action|natively-forward|"
1548 "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
1549 "[rloc <dst-locator> ... ]",.function =
1550 lisp_add_del_remote_mapping_command_fn,};
1553 * Handler for add/del adjacency CLI.
1555 static clib_error_t *
1556 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
1557 vlib_cli_command_t * cmd)
1559 clib_error_t *error = 0;
1560 unformat_input_t _line_input, *line_input = &_line_input;
1561 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1563 ip_prefix_t *reid_ippref, *leid_ippref;
1564 gid_address_t leid, reid;
1565 u8 *dmac = gid_address_mac (&reid);
1566 u8 *smac = gid_address_mac (&leid);
1567 u8 reid_set = 0, leid_set = 0;
1571 /* Get a line of input. */
1572 if (!unformat_user (input, unformat_line_input, line_input))
1575 memset (&reid, 0, sizeof (reid));
1576 memset (&leid, 0, sizeof (leid));
1578 leid_ippref = &gid_address_ippref (&leid);
1579 reid_ippref = &gid_address_ippref (&reid);
1581 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1583 if (unformat (line_input, "del"))
1585 else if (unformat (line_input, "add"))
1587 else if (unformat (line_input, "reid %U",
1588 unformat_ip_prefix, reid_ippref))
1590 gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
1593 else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
1595 gid_address_type (&reid) = GID_ADDR_MAC;
1598 else if (unformat (line_input, "vni %u", &vni))
1600 gid_address_vni (&leid) = vni;
1601 gid_address_vni (&reid) = vni;
1603 else if (unformat (line_input, "leid %U",
1604 unformat_ip_prefix, leid_ippref))
1606 gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
1609 else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
1611 gid_address_type (&leid) = GID_ADDR_MAC;
1616 clib_warning ("parse error");
1621 if (!reid_set || !leid_set)
1623 clib_warning ("missing remote or local eid!");
1627 if ((gid_address_type (&leid) != gid_address_type (&reid))
1628 || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
1629 && ip_prefix_version (reid_ippref)
1630 != ip_prefix_version (leid_ippref)))
1632 clib_warning ("remote and local EIDs are of different types!");
1636 memset (a, 0, sizeof (a[0]));
1637 gid_address_copy (&a->leid, &leid);
1638 gid_address_copy (&a->reid, &reid);
1641 rv = vnet_lisp_add_del_adjacency (a);
1644 clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
1647 unformat_free (line_input);
1652 VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1653 .path = "lisp adjacency",
1654 .short_help = "lisp adjacency add|del vni <vni> reid <remote-eid> "
1656 .function = lisp_add_del_adjacency_command_fn,
1661 vnet_lisp_set_map_request_mode (u8 mode)
1663 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1665 if (vnet_lisp_enable_disable_status () == 0)
1667 clib_warning ("LISP is disabled!");
1668 return VNET_API_ERROR_LISP_DISABLED;
1671 if (mode >= _MR_MODE_MAX)
1673 clib_warning ("Invalid LISP map request mode %d!", mode);
1674 return VNET_API_ERROR_INVALID_ARGUMENT;
1677 lcm->map_request_mode = mode;
1681 static clib_error_t *
1682 lisp_map_request_mode_command_fn (vlib_main_t * vm,
1683 unformat_input_t * input,
1684 vlib_cli_command_t * cmd)
1686 unformat_input_t _i, *i = &_i;
1687 map_request_mode_t mr_mode = _MR_MODE_MAX;
1689 /* Get a line of input. */
1690 if (!unformat_user (input, unformat_line_input, i))
1693 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1695 if (unformat (i, "dst-only"))
1696 mr_mode = MR_MODE_DST_ONLY;
1697 else if (unformat (i, "src-dst"))
1698 mr_mode = MR_MODE_SRC_DST;
1701 clib_warning ("parse error '%U'", format_unformat_error, i);
1706 if (_MR_MODE_MAX == mr_mode)
1708 clib_warning ("No LISP map request mode entered!");
1712 vnet_lisp_set_map_request_mode (mr_mode);
1718 VLIB_CLI_COMMAND (lisp_map_request_mode_command) = {
1719 .path = "lisp map-request mode",
1720 .short_help = "lisp map-request mode dst-only|src-dst",
1721 .function = lisp_map_request_mode_command_fn,
1726 format_lisp_map_request_mode (u8 * s, va_list * args)
1728 u32 mode = va_arg (*args, u32);
1733 return format (0, "dst-only");
1735 return format (0, "src-dst");
1740 static clib_error_t *
1741 lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
1742 unformat_input_t * input,
1743 vlib_cli_command_t * cmd)
1745 vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
1746 vnet_lisp_get_map_request_mode ());
1751 VLIB_CLI_COMMAND (lisp_show_map_request_mode_command) = {
1752 .path = "show lisp map-request mode",
1753 .short_help = "show lisp map-request mode",
1754 .function = lisp_show_map_request_mode_command_fn,
1758 static clib_error_t *
1759 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
1760 unformat_input_t * input,
1761 vlib_cli_command_t * cmd)
1764 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1766 vec_foreach (mr, lcm->map_resolvers)
1768 vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
1774 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1775 .path = "show lisp map-resolvers",
1776 .short_help = "show lisp map-resolvers",
1777 .function = lisp_show_map_resolvers_command_fn,
1782 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1784 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1785 u32 locator_set_index = ~0;
1789 if (vnet_lisp_enable_disable_status () == 0)
1791 clib_warning ("LISP is disabled!");
1792 return VNET_API_ERROR_LISP_DISABLED;
1795 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1798 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1801 locator_set_index = p[0];
1805 pool_get (lcm->mapping_pool, m);
1806 m->locator_set_index = locator_set_index;
1808 lcm->pitr_map_index = m - lcm->mapping_pool;
1810 /* enable pitr mode */
1815 /* remove pitr mapping */
1816 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1818 /* disable pitr mode */
1824 static clib_error_t *
1825 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
1826 unformat_input_t * input,
1827 vlib_cli_command_t * cmd)
1829 u8 locator_name_set = 0;
1830 u8 *locator_set_name = 0;
1832 unformat_input_t _line_input, *line_input = &_line_input;
1833 clib_error_t *error = 0;
1836 /* Get a line of input. */
1837 if (!unformat_user (input, unformat_line_input, line_input))
1840 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1842 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
1843 locator_name_set = 1;
1844 else if (unformat (line_input, "disable"))
1847 return clib_error_return (0, "parse error");
1850 if (!locator_name_set)
1852 clib_warning ("No locator set specified!");
1855 rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1858 error = clib_error_return (0, "failed to %s pitr!",
1859 is_add ? "add" : "delete");
1863 if (locator_set_name)
1864 vec_free (locator_set_name);
1869 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1870 .path = "lisp pitr",
1871 .short_help = "lisp pitr [disable] ls <locator-set-name>",
1872 .function = lisp_pitr_set_locator_set_command_fn,
1876 static clib_error_t *
1877 lisp_show_pitr_command_fn (vlib_main_t * vm,
1878 unformat_input_t * input, vlib_cli_command_t * cmd)
1880 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1885 vlib_cli_output (vm, "%=20s%=16s",
1886 "pitr", lcm->lisp_pitr ? "locator-set" : "");
1888 if (!lcm->lisp_pitr)
1890 vlib_cli_output (vm, "%=20s", "disable");
1894 if (~0 == lcm->pitr_map_index)
1896 tmp_str = format (0, "N/A");
1900 m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1901 if (~0 != m->locator_set_index)
1904 pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1905 tmp_str = format (0, "%s", ls->name);
1909 tmp_str = format (0, "N/A");
1912 vec_add1 (tmp_str, 0);
1914 vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1922 VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1923 .path = "show lisp pitr",
1924 .short_help = "Show pitr",
1925 .function = lisp_show_pitr_command_fn,
1930 format_eid_entry (u8 * s, va_list * args)
1932 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
1933 lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
1934 mapping_t *mapit = va_arg (*args, mapping_t *);
1935 locator_set_t *ls = va_arg (*args, locator_set_t *);
1936 gid_address_t *gid = &mapit->eid;
1937 u32 ttl = mapit->ttl;
1938 u8 aut = mapit->authoritative;
1943 u8 *type = ls->local ? format (0, "local(%s)", ls->name)
1944 : format (0, "remote");
1946 if (vec_len (ls->locator_indices) == 0)
1948 s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
1953 vec_foreach (loc_index, ls->locator_indices)
1955 locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1957 loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1960 loc = format (0, "%U", format_ip_address,
1961 &gid_address_ip (&l->address));
1965 s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
1966 gid, type, loc, ttl, aut);
1970 s = format (s, "%55s%v\n", "", loc);
1976 static clib_error_t *
1977 lisp_show_eid_table_command_fn (vlib_main_t * vm,
1978 unformat_input_t * input,
1979 vlib_cli_command_t * cmd)
1981 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1983 unformat_input_t _line_input, *line_input = &_line_input;
1989 memset (&eid, 0, sizeof (eid));
1991 /* Get a line of input. */
1992 if (!unformat_user (input, unformat_line_input, line_input))
1995 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1997 if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1999 else if (unformat (line_input, "local"))
2001 else if (unformat (line_input, "remote"))
2004 return clib_error_return (0, "parse error: '%U'",
2005 format_unformat_error, line_input);
2008 vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
2009 "EID", "type", "locators", "ttl", "autoritative");
2014 pool_foreach (mapit, lcm->mapping_pool,
2016 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
2017 mapit->locator_set_index);
2018 if (filter && !((1 == filter && ls->local) ||
2019 (2 == filter && !ls->local)))
2023 vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
2030 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
2031 if ((u32) ~ 0 == mi)
2034 mapit = pool_elt_at_index (lcm->mapping_pool, mi);
2035 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2036 mapit->locator_set_index);
2038 if (filter && !((1 == filter && ls->local) ||
2039 (2 == filter && !ls->local)))
2044 vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
2052 VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
2053 .path = "show lisp eid-table",
2054 .short_help = "Shows EID table",
2055 .function = lisp_show_eid_table_command_fn,
2059 /* cleans locator to locator-set data and removes locators not part of
2060 * any locator-set */
2062 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
2064 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
2065 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
2066 for (i = 0; i < vec_len (ls->locator_indices); i++)
2068 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
2069 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2071 for (j = 0; j < vec_len (ls_indexes[0]); j++)
2073 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
2074 if (ls_indexp[0] == lsi)
2078 /* delete index for removed locator-set */
2079 vec_del1 (ls_indexes[0], j);
2081 /* delete locator if it's part of no locator-set */
2082 if (vec_len (ls_indexes[0]) == 0)
2084 pool_put_index (lcm->locator_pool, loc_indexp[0]);
2085 vec_add1 (to_be_deleted, i);
2091 for (i = 0; i < vec_len (to_be_deleted); i++)
2093 loc_indexp = vec_elt_at_index (to_be_deleted, i);
2094 vec_del1 (ls->locator_indices, loc_indexp[0]);
2096 vec_free (to_be_deleted);
2100 static inline uword *
2101 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
2103 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2108 /* find locator-set */
2111 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
2122 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
2128 ASSERT (ls != NULL);
2129 ASSERT (loc != NULL);
2131 vec_foreach (locit, ls->locator_indices)
2133 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2134 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
2135 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
2137 clib_warning ("Duplicate locator");
2138 return VNET_API_ERROR_VALUE_EXIST;
2146 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
2147 u32 ls_index, u32 loc_id)
2149 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2150 u32 **ls_indexes = NULL;
2152 ASSERT (ls != NULL);
2153 ASSERT (locit != NULL);
2155 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
2156 pool_put_index (lcm->locator_pool, locit[0]);
2157 vec_del1 (ls->locator_indices, loc_id);
2158 vec_del1 (ls_indexes[0], ls_index);
2162 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
2163 locator_set_t * ls, u32 * ls_result)
2165 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2166 locator_t *loc = NULL, *itloc = NULL;
2167 uword _p = (u32) ~ 0, *p = &_p;
2168 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
2174 if (vnet_lisp_enable_disable_status () == 0)
2176 clib_warning ("LISP is disabled!");
2177 return VNET_API_ERROR_LISP_DISABLED;
2180 p = get_locator_set_index (a, p);
2183 clib_warning ("locator-set %v doesn't exist", a->name);
2184 return VNET_API_ERROR_INVALID_ARGUMENT;
2189 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2192 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2194 return VNET_API_ERROR_INVALID_ARGUMENT;
2201 ls_result[0] = p[0];
2203 /* allocate locators */
2204 vec_foreach (itloc, a->locators)
2206 ret = is_locator_in_locator_set (lcm, ls, itloc);
2212 pool_get (lcm->locator_pool, loc);
2214 loc_index = loc - lcm->locator_pool;
2216 vec_add1 (ls->locator_indices, loc_index);
2218 vec_validate (lcm->locator_to_locator_sets, loc_index);
2219 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2221 vec_add1 (ls_indexes[0], p[0]);
2228 itloc = a->locators;
2230 vec_foreach (locit, ls->locator_indices)
2232 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2234 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
2236 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2238 if (0 == loc->local &&
2239 !gid_address_cmp (&loc->address, &itloc->address))
2241 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2252 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
2255 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2257 uword _p = (u32) ~ 0, *p = &_p;
2262 if (vnet_lisp_enable_disable_status () == 0)
2264 clib_warning ("LISP is disabled!");
2265 return VNET_API_ERROR_LISP_DISABLED;
2270 p = get_locator_set_index (a, p);
2273 if (p && p[0] != (u32) ~ 0)
2275 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2278 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2283 /* clean locator to locator-set vectors and remove locators if
2284 * they're not part of another locator-set */
2285 clean_locator_to_locator_set (lcm, p[0]);
2287 /* remove locator indices from locator set */
2288 vec_free (ls->locator_indices);
2293 ls_result[0] = p[0];
2295 /* new locator-set */
2298 pool_get (lcm->locator_set_pool, ls);
2299 memset (ls, 0, sizeof (*ls));
2300 ls_index = ls - lcm->locator_set_pool;
2304 ls->name = vec_dup (a->name);
2306 if (!lcm->locator_set_index_by_name)
2307 lcm->locator_set_index_by_name = hash_create_vec (
2315 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
2318 /* mark as local locator-set */
2319 vec_add1 (lcm->local_locator_set_indexes, ls_index);
2321 ls->local = a->local;
2323 ls_result[0] = ls_index;
2326 ret = vnet_lisp_add_del_locator (a, ls, NULL);
2334 p = get_locator_set_index (a, p);
2337 clib_warning ("locator-set %v doesn't exists", a->name);
2341 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2344 clib_warning ("locator-set with index %d doesn't exists", p[0]);
2348 if (lcm->mreq_itr_rlocs == p[0])
2350 clib_warning ("Can't delete the locator-set used to constrain "
2351 "the itr-rlocs in map-requests!");
2355 if (vec_len (lcm->locator_set_to_eids) != 0)
2357 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
2358 if (vec_len (eid_indexes[0]) != 0)
2361 ("Can't delete a locator that supports a mapping!");
2366 /* clean locator to locator-sets data */
2367 clean_locator_to_locator_set (lcm, p[0]);
2373 vec_foreach_index (it, lcm->local_locator_set_indexes)
2375 lsi = vec_elt (lcm->local_locator_set_indexes, it);
2378 vec_del1 (lcm->local_locator_set_indexes, it);
2382 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2384 vec_free (ls->name);
2385 vec_free (ls->locator_indices);
2386 pool_put (lcm->locator_set_pool, ls);
2392 vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
2394 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2396 lcm->rloc_probing = is_enable;
2401 vnet_lisp_map_register_enable_disable (u8 is_enable)
2403 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2405 lcm->map_registering = is_enable;
2410 vnet_lisp_enable_disable (u8 is_enable)
2413 clib_error_t *error = 0;
2414 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2415 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
2417 a->is_en = is_enable;
2418 error = vnet_lisp_gpe_enable_disable (a);
2421 return clib_error_return (0, "failed to %s data-plane!",
2422 a->is_en ? "enable" : "disable");
2427 /* enable all l2 and l3 ifaces */
2430 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2431 dp_add_del_iface(lcm, vni, 0, 1);
2433 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2434 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
2440 /* clear interface table */
2441 hash_free (lcm->fwd_entry_by_mapping_index);
2442 pool_free (lcm->fwd_entry_pool);
2445 /* update global flag */
2446 lcm->is_enabled = is_enable;
2451 static clib_error_t *
2452 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
2453 vlib_cli_command_t * cmd)
2455 unformat_input_t _line_input, *line_input = &_line_input;
2459 /* Get a line of input. */
2460 if (!unformat_user (input, unformat_line_input, line_input))
2463 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2465 if (unformat (line_input, "enable"))
2470 else if (unformat (line_input, "disable"))
2474 return clib_error_return (0, "parse error: '%U'",
2475 format_unformat_error, line_input);
2480 return clib_error_return (0, "state not set");
2482 vnet_lisp_enable_disable (is_enabled);
2487 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
2489 .short_help = "lisp [enable|disable]",
2490 .function = lisp_enable_disable_command_fn,
2494 static clib_error_t *
2495 lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
2496 unformat_input_t * input,
2497 vlib_cli_command_t * cmd)
2499 unformat_input_t _line_input, *line_input = &_line_input;
2503 /* Get a line of input. */
2504 if (!unformat_user (input, unformat_line_input, line_input))
2507 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2509 if (unformat (line_input, "enable"))
2514 else if (unformat (line_input, "disable"))
2518 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2526 vlib_cli_output (vm, "state not set!");
2530 vnet_lisp_map_register_enable_disable (is_enabled);
2535 VLIB_CLI_COMMAND (lisp_map_register_enable_disable_command) = {
2536 .path = "lisp map-register",
2537 .short_help = "lisp map-register [enable|disable]",
2538 .function = lisp_map_register_enable_disable_command_fn,
2542 static clib_error_t *
2543 lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
2544 unformat_input_t * input,
2545 vlib_cli_command_t * cmd)
2547 unformat_input_t _line_input, *line_input = &_line_input;
2551 /* Get a line of input. */
2552 if (!unformat_user (input, unformat_line_input, line_input))
2555 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2557 if (unformat (line_input, "enable"))
2562 else if (unformat (line_input, "disable"))
2566 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2574 vlib_cli_output (vm, "state not set!");
2578 vnet_lisp_rloc_probe_enable_disable (is_enabled);
2583 VLIB_CLI_COMMAND (lisp_rloc_probe_enable_disable_command) = {
2584 .path = "lisp rloc-probe",
2585 .short_help = "lisp rloc-probe [enable|disable]",
2586 .function = lisp_rloc_probe_enable_disable_command_fn,
2591 vnet_lisp_enable_disable_status (void)
2593 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2594 return lcm->is_enabled;
2598 format_lisp_status (u8 * s, va_list * args)
2600 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2601 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
2604 static clib_error_t *
2605 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
2606 vlib_cli_command_t * cmd)
2609 msg = format (msg, "feature: %U\ngpe: %U\n",
2610 format_lisp_status, format_vnet_lisp_gpe_status);
2611 vlib_cli_output (vm, "%v", msg);
2617 VLIB_CLI_COMMAND (lisp_show_status_command) = {
2618 .path = "show lisp status",
2619 .short_help = "show lisp status",
2620 .function = lisp_show_status_command_fn,
2624 static clib_error_t *
2625 lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
2626 unformat_input_t * input,
2627 vlib_cli_command_t * cmd)
2630 unformat_input_t _line_input, *line_input = &_line_input;
2631 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2632 uword *vni_table = 0;
2635 /* Get a line of input. */
2636 if (!unformat_user (input, unformat_line_input, line_input))
2639 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2641 if (unformat (line_input, "l2"))
2643 vni_table = lcm->bd_id_by_vni;
2646 else if (unformat (line_input, "l3"))
2648 vni_table = lcm->table_id_by_vni;
2652 return clib_error_return (0, "parse error: '%U'",
2653 format_unformat_error, line_input);
2658 vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
2662 vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
2665 hash_foreach_pair (p, vni_table,
2667 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
2675 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
2676 .path = "show lisp eid-table map",
2677 .short_help = "show lisp eid-table l2|l3",
2678 .function = lisp_show_eid_table_map_command_fn,
2682 static clib_error_t *
2683 lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
2684 unformat_input_t * input,
2685 vlib_cli_command_t * cmd)
2687 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2688 vnet_main_t *vnm = lgm->vnet_main;
2689 unformat_input_t _line_input, *line_input = &_line_input;
2691 clib_error_t *error = 0;
2692 u8 *locator_set_name = 0;
2693 locator_t locator, *locators = 0;
2694 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2698 memset (&locator, 0, sizeof (locator));
2699 memset (a, 0, sizeof (a[0]));
2701 /* Get a line of input. */
2702 if (!unformat_user (input, unformat_line_input, line_input))
2705 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2707 if (unformat (line_input, "add %_%v%_", &locator_set_name))
2709 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
2711 else if (unformat (line_input, "iface %U p %d w %d",
2712 unformat_vnet_sw_interface, vnm,
2713 &locator.sw_if_index, &locator.priority,
2717 vec_add1 (locators, locator);
2721 error = unformat_parse_error (line_input);
2726 a->name = locator_set_name;
2727 a->locators = locators;
2731 rv = vnet_lisp_add_del_locator_set (a, &ls_index);
2734 error = clib_error_return (0, "failed to %s locator-set!",
2735 is_add ? "add" : "delete");
2739 vec_free (locators);
2740 if (locator_set_name)
2741 vec_free (locator_set_name);
2746 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2747 .path = "lisp locator-set",
2748 .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2749 "p <priority> w <weight>]",
2750 .function = lisp_add_del_locator_set_command_fn,
2754 static clib_error_t *
2755 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
2756 unformat_input_t * input,
2757 vlib_cli_command_t * cmd)
2759 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2760 vnet_main_t *vnm = lgm->vnet_main;
2761 unformat_input_t _line_input, *line_input = &_line_input;
2763 clib_error_t *error = 0;
2764 u8 *locator_set_name = 0;
2765 u8 locator_set_name_set = 0;
2766 locator_t locator, *locators = 0;
2767 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2770 memset (&locator, 0, sizeof (locator));
2771 memset (a, 0, sizeof (a[0]));
2773 /* Get a line of input. */
2774 if (!unformat_user (input, unformat_line_input, line_input))
2777 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2779 if (unformat (line_input, "add"))
2781 else if (unformat (line_input, "del"))
2783 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
2784 locator_set_name_set = 1;
2785 else if (unformat (line_input, "iface %U p %d w %d",
2786 unformat_vnet_sw_interface, vnm,
2787 &locator.sw_if_index, &locator.priority,
2791 vec_add1 (locators, locator);
2795 error = unformat_parse_error (line_input);
2800 if (!locator_set_name_set)
2802 error = clib_error_return (0, "locator_set name not set!");
2806 a->name = locator_set_name;
2807 a->locators = locators;
2811 vnet_lisp_add_del_locator (a, 0, &ls_index);
2814 vec_free (locators);
2815 vec_free (locator_set_name);
2820 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2821 .path = "lisp locator",
2822 .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2823 "p <priority> w <weight>",
2824 .function = lisp_add_del_locator_in_set_command_fn,
2828 static clib_error_t *
2829 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
2830 unformat_input_t * input,
2831 vlib_cli_command_t * cmd)
2833 locator_set_t *lsit;
2836 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2838 vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
2839 "Priority", "Weight");
2842 pool_foreach (lsit, lcm->locator_set_pool,
2848 msg = format (msg, "%v", lsit->name);
2852 msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
2854 vec_foreach (locit, lsit->locator_indices)
2858 msg = format (msg, "%16s", " ");
2860 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2862 msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
2865 msg = format (msg, "%16U%16d%16d\n", format_ip_address,
2866 &gid_address_ip(&loc->address), loc->priority,
2870 vlib_cli_output (vm, "%v", msg);
2878 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2879 .path = "show lisp locator-set",
2880 .short_help = "Shows locator-sets",
2881 .function = lisp_cp_show_locator_sets_command_fn,
2886 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2888 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2890 lisp_msmr_t _mr, *mr = &_mr;
2892 if (vnet_lisp_enable_disable_status () == 0)
2894 clib_warning ("LISP is disabled!");
2895 return VNET_API_ERROR_LISP_DISABLED;
2901 if (get_map_resolver (&a->address))
2903 clib_warning ("map-resolver %U already exists!", format_ip_address,
2908 memset (mr, 0, sizeof (*mr));
2909 ip_address_copy (&mr->address, &a->address);
2910 vec_add1 (lcm->map_resolvers, *mr);
2912 if (vec_len (lcm->map_resolvers) == 1)
2913 lcm->do_map_resolver_election = 1;
2917 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2919 mr = vec_elt_at_index (lcm->map_resolvers, i);
2920 if (!ip_address_cmp (&mr->address, &a->address))
2922 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2923 lcm->do_map_resolver_election = 1;
2925 vec_del1 (lcm->map_resolvers, i);
2933 static clib_error_t *
2934 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
2935 unformat_input_t * input,
2936 vlib_cli_command_t * cmd)
2938 unformat_input_t _line_input, *line_input = &_line_input;
2939 u8 is_add = 1, addr_set = 0;
2940 ip_address_t ip_addr;
2941 clib_error_t *error = 0;
2943 vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
2945 /* Get a line of input. */
2946 if (!unformat_user (input, unformat_line_input, line_input))
2949 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2951 if (unformat (line_input, "add"))
2953 else if (unformat (line_input, "del"))
2955 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
2959 error = unformat_parse_error (line_input);
2966 error = clib_error_return (0, "Map-resolver address must be set!");
2971 a->address = ip_addr;
2972 rv = vnet_lisp_add_del_map_resolver (a);
2975 error = clib_error_return (0, "failed to %s map-resolver!",
2976 is_add ? "add" : "delete");
2984 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
2985 .path = "lisp map-resolver",
2986 .short_help = "lisp map-resolver add/del <ip_address>",
2987 .function = lisp_add_del_map_resolver_command_fn,
2992 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2994 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2997 if (vnet_lisp_enable_disable_status () == 0)
2999 clib_warning ("LISP is disabled!");
3000 return VNET_API_ERROR_LISP_DISABLED;
3005 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
3008 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
3009 return VNET_API_ERROR_INVALID_ARGUMENT;
3012 lcm->mreq_itr_rlocs = p[0];
3016 lcm->mreq_itr_rlocs = ~0;
3022 static clib_error_t *
3023 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
3024 unformat_input_t * input,
3025 vlib_cli_command_t * cmd)
3027 unformat_input_t _line_input, *line_input = &_line_input;
3029 u8 *locator_set_name = 0;
3030 clib_error_t *error = 0;
3032 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
3034 /* Get a line of input. */
3035 if (!unformat_user (input, unformat_line_input, line_input))
3038 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3040 if (unformat (line_input, "del"))
3042 else if (unformat (line_input, "add %_%v%_", &locator_set_name))
3046 error = unformat_parse_error (line_input);
3052 a->locator_set_name = locator_set_name;
3053 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
3056 error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
3057 is_add ? "add" : "delete");
3060 vec_free (locator_set_name);
3068 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
3069 .path = "lisp map-request itr-rlocs",
3070 .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
3071 .function = lisp_add_del_mreq_itr_rlocs_command_fn,
3075 static clib_error_t *
3076 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
3077 unformat_input_t * input,
3078 vlib_cli_command_t * cmd)
3080 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3081 locator_set_t *loc_set;
3083 vlib_cli_output (vm, "%=20s", "itr-rlocs");
3085 if (~0 == lcm->mreq_itr_rlocs)
3090 loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
3092 vlib_cli_output (vm, "%=20s", loc_set->name);
3098 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
3099 .path = "show lisp map-request itr-rlocs",
3100 .short_help = "Shows map-request itr-rlocs",
3101 .function = lisp_show_mreq_itr_rlocs_command_fn,
3105 /* Statistics (not really errors) */
3106 #define foreach_lisp_cp_lookup_error \
3108 _(MAP_REQUESTS_SENT, "map-request sent")
3110 static char *lisp_cp_lookup_error_strings[] = {
3111 #define _(sym,string) string,
3112 foreach_lisp_cp_lookup_error
3118 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
3119 foreach_lisp_cp_lookup_error
3121 LISP_CP_LOOKUP_N_ERROR,
3122 } lisp_cp_lookup_error_t;
3126 LISP_CP_LOOKUP_NEXT_DROP,
3127 LISP_CP_LOOKUP_N_NEXT,
3128 } lisp_cp_lookup_next_t;
3132 gid_address_t dst_eid;
3133 ip_address_t map_resolver_ip;
3134 } lisp_cp_lookup_trace_t;
3137 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
3139 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3140 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3141 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
3143 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
3144 format_ip_address, &t->map_resolver_ip, format_gid_address,
3150 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
3151 ip_address_t * sloc)
3156 if (vec_len (lcm->map_resolvers) == 0)
3158 clib_warning ("No map-resolver configured");
3162 /* find the first mr ip we have a route to and the ip of the
3163 * iface that has a route to it */
3164 vec_foreach (mrit, lcm->map_resolvers)
3167 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
3169 ip_address_copy (mr_ip, a);
3171 /* also update globals */
3176 clib_warning ("Can't find map-resolver and local interface ip!");
3180 static gid_address_t *
3181 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
3187 ip_interface_address_t *ia = 0;
3188 gid_address_t gid_data, *gid = &gid_data;
3189 gid_address_t *rlocs = 0;
3190 ip_prefix_t *ippref = &gid_address_ippref (gid);
3191 ip_address_t *rloc = &ip_prefix_addr (ippref);
3193 memset (gid, 0, sizeof (gid[0]));
3194 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
3195 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
3197 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
3198 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
3200 /* Add ipv4 locators first TODO sort them */
3203 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3204 loc->sw_if_index, 1 /* unnumbered */,
3206 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
3207 ip_address_set (rloc, addr, IP4);
3208 ip_prefix_len (ippref) = 32;
3209 ip_prefix_normalize (ippref);
3210 vec_add1 (rlocs, gid[0]);
3213 /* Add ipv6 locators */
3214 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3215 loc->sw_if_index, 1 /* unnumbered */,
3217 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
3218 ip_address_set (rloc, addr, IP6);
3219 ip_prefix_len (ippref) = 128;
3220 ip_prefix_normalize (ippref);
3221 vec_add1 (rlocs, gid[0]);
3229 static vlib_buffer_t *
3230 build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
3231 ip_address_t * sloc, ip_address_t * rloc,
3232 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
3236 vlib_main_t *vm = lcm->vlib_main;
3238 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3240 clib_warning ("Can't allocate buffer for Map-Request!");
3244 b = vlib_get_buffer (vm, bi);
3246 /* leave some space for the encap headers */
3247 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3250 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
3251 1 /* rloc probe */ , nonce_res);
3253 /* push outer ip header */
3254 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3262 static vlib_buffer_t *
3263 build_encapsulated_map_request (lisp_cp_main_t * lcm,
3264 gid_address_t * seid, gid_address_t * deid,
3265 locator_set_t * loc_set, ip_address_t * mr_ip,
3266 ip_address_t * sloc, u8 is_smr_invoked,
3267 u64 * nonce_res, u32 * bi_res)
3271 gid_address_t *rlocs = 0;
3272 vlib_main_t *vm = lcm->vlib_main;
3274 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3276 clib_warning ("Can't allocate buffer for Map-Request!");
3280 b = vlib_get_buffer (vm, bi);
3282 /* leave some space for the encap headers */
3283 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3286 rlocs = build_itr_rloc_list (lcm, loc_set);
3288 if (MR_MODE_SRC_DST == lcm->map_request_mode
3289 && GID_ADDR_SRC_DST != gid_address_type (deid))
3292 memset (&sd, 0, sizeof (sd));
3293 build_src_dst (&sd, seid, deid);
3294 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
3295 0 /* rloc probe */ , nonce_res);
3300 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
3301 0 /* rloc probe */ , nonce_res);
3304 /* push ecm: udp-ip-lisp */
3305 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
3307 /* push outer ip header */
3308 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3318 reset_pending_mr_counters (pending_map_request_t * r)
3320 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3325 elect_map_resolver (lisp_cp_main_t * lcm)
3329 vec_foreach (mr, lcm->map_resolvers)
3333 ip_address_copy (&lcm->active_map_resolver, &mr->address);
3334 lcm->do_map_resolver_election = 0;
3342 free_map_register_records (mapping_t * maps)
3345 vec_foreach (map, maps) vec_free (map->locators);
3351 add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
3352 ip_address_t * probed_loc)
3355 locator_t *loc, new;
3356 ip_interface_address_t *ia = 0;
3358 ip_address_t *new_ip = &gid_address_ip (&new.address);
3361 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
3363 vec_foreach (li, ls->locator_indices)
3365 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
3370 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3371 loc->sw_if_index, 1 /* unnumbered */,
3373 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
3375 ip_address_set (new_ip, addr, IP4);
3378 /* Add ipv6 locators */
3379 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3380 loc->sw_if_index, 1 /* unnumbered */,
3382 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
3384 ip_address_set (new_ip, addr, IP6);
3388 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
3391 vec_add1 (m->locators, new);
3396 build_map_register_record_list (lisp_cp_main_t * lcm)
3398 mapping_t *recs = 0, rec, *m;
3401 pool_foreach(m, lcm->mapping_pool,
3403 /* for now build only local mappings */
3408 add_locators (lcm, &rec, m->locator_set_index, NULL);
3409 vec_add1 (recs, rec);
3417 update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
3418 lisp_key_type_t key_id, u8 * key,
3419 u16 auth_data_len, u32 msg_len)
3421 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
3422 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
3424 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3425 (unsigned char *) map_reg_hdr, msg_len, NULL,
3427 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
3432 static vlib_buffer_t *
3433 build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
3434 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
3435 mapping_t * records, lisp_key_type_t key_id, u8 * key,
3440 u32 bi, auth_data_len = 0, msg_len = 0;
3441 vlib_main_t *vm = lcm->vlib_main;
3443 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3445 clib_warning ("Can't allocate buffer for Map-Register!");
3449 b = vlib_get_buffer (vm, bi);
3451 /* leave some space for the encap headers */
3452 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3454 auth_data_len = auth_data_len_by_key_id (key_id);
3455 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
3456 auth_data_len, nonce_res,
3459 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
3462 /* push outer ip header */
3463 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3471 get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
3474 while (lcm->do_map_resolver_election
3475 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
3476 &lcm->active_map_resolver,
3479 if (0 == elect_map_resolver (lcm))
3480 /* all map resolvers are down */
3482 /* restart MR checking by marking all of them up */
3483 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
3491 send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
3492 u32 local_locator_set_index, ip_address_t * sloc,
3493 ip_address_t * rloc)
3500 u32 next_index, *to_next;
3501 gid_address_t *itr_rlocs;
3503 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
3504 itr_rlocs = build_itr_rloc_list (lcm, ls);
3506 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
3507 vec_free (itr_rlocs);
3511 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3513 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3514 ip4_lookup_node.index : ip6_lookup_node.index;
3516 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3518 /* Enqueue the packet */
3519 to_next = vlib_frame_vector_args (f);
3522 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3524 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3529 send_rloc_probes (lisp_cp_main_t * lcm)
3538 pool_foreach (e, lcm->fwd_entry_pool,
3540 if (vec_len (e->locator_pairs) == 0)
3543 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
3546 clib_warning ("internal error: cannot find local eid %U in "
3547 "map-cache!", format_gid_address, &e->leid);
3550 lm = pool_elt_at_index (lcm->mapping_pool, si);
3552 /* get the best (lowest) priority */
3553 lprio = e->locator_pairs[0].priority;
3555 /* send rloc-probe for pair(s) with the best remote locator priority */
3556 vec_foreach (lp, e->locator_pairs)
3558 if (lp->priority != lprio)
3561 /* get first remote locator */
3562 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
3572 send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
3579 u32 next_index, *to_next;
3580 ip_address_t *ms = 0;
3581 mapping_t *records, *r, *g;
3583 // TODO: support multiple map servers and do election
3584 if (0 == vec_len (lcm->map_servers))
3587 ms = &lcm->map_servers[0].address;
3589 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
3591 clib_warning ("no eligible interface address found for %U!",
3592 format_ip_address, &lcm->map_servers[0]);
3596 records = build_map_register_record_list (lcm);
3600 vec_foreach (r, records)
3603 u8 key_id = r->key_id;
3606 continue; /* no secret key -> map-register cannot be sent */
3609 // TODO: group mappings that share common key
3611 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, g,
3617 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3619 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3620 ip4_lookup_node.index : ip6_lookup_node.index;
3622 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3624 /* Enqueue the packet */
3625 to_next = vlib_frame_vector_args (f);
3628 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3630 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3632 free_map_register_records (records);
3637 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
3638 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
3640 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
3641 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
3644 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
3645 gid_address_t * seid, gid_address_t * deid,
3646 u8 is_smr_invoked, u8 is_resend)
3648 u32 next_index, bi = 0, *to_next, map_index;
3652 locator_set_t *loc_set;
3654 pending_map_request_t *pmr, *duplicate_pmr = 0;
3658 /* if there is already a pending request remember it */
3661 pool_foreach(pmr, lcm->pending_map_requests_pool,
3663 if (!gid_address_cmp (&pmr->src, seid)
3664 && !gid_address_cmp (&pmr->dst, deid))
3666 duplicate_pmr = pmr;
3672 if (!is_resend && duplicate_pmr)
3674 /* don't send the request if there is a pending map request already */
3678 /* get locator-set for seid */
3679 if (!lcm->lisp_pitr)
3681 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
3682 if (map_index == ~0)
3684 clib_warning ("No local mapping found in eid-table for %U!",
3685 format_gid_address, seid);
3689 map = pool_elt_at_index (lcm->mapping_pool, map_index);
3694 ("Mapping found for src eid %U is not marked as local!",
3695 format_gid_address, seid);
3698 ls_index = map->locator_set_index;
3702 map_index = lcm->pitr_map_index;
3703 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
3704 ls_index = map->locator_set_index;
3707 /* overwrite locator set if map-request itr-rlocs configured */
3708 if (~0 != lcm->mreq_itr_rlocs)
3710 ls_index = lcm->mreq_itr_rlocs;
3713 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
3715 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
3718 duplicate_pmr->to_be_removed = 1;
3722 /* build the encapsulated map request */
3723 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
3724 &lcm->active_map_resolver,
3725 &sloc, is_smr_invoked, &nonce, &bi);
3730 /* set fib index to default and lookup node */
3731 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3732 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3733 ip4_lookup_node.index : ip6_lookup_node.index;
3735 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3737 /* Enqueue the packet */
3738 to_next = vlib_frame_vector_args (f);
3741 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3744 /* if there is a pending request already update it */
3746 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
3748 /* remove the oldest nonce */
3749 u64 CLIB_UNUSED (tmp), *nonce_del;
3750 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
3751 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
3752 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
3755 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
3756 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3757 duplicate_pmr - lcm->pending_map_requests_pool);
3761 /* add map-request to pending requests table */
3762 pool_get (lcm->pending_map_requests_pool, pmr);
3763 memset (pmr, 0, sizeof (*pmr));
3764 gid_address_copy (&pmr->src, seid);
3765 gid_address_copy (&pmr->dst, deid);
3766 clib_fifo_add1 (pmr->nonces, nonce);
3767 pmr->is_smr_invoked = is_smr_invoked;
3768 reset_pending_mr_counters (pmr);
3769 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3770 pmr - lcm->pending_map_requests_pool);
3777 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
3779 ip4_header_t *ip4 = hdr;
3782 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3784 ip_address_set (src, &ip4->src_address, IP4);
3785 ip_address_set (dst, &ip4->dst_address, IP4);
3790 ip_address_set (src, &ip6->src_address, IP6);
3791 ip_address_set (dst, &ip6->dst_address, IP6);
3796 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3800 u32 vni = ~0, table_id = ~0;
3802 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
3803 IP4 ? FIB_PROTOCOL_IP4 :
3809 vnip = hash_get (lcm->vni_by_table_id, table_id);
3813 clib_warning ("vrf %d is not mapped to any vni!", table_id);
3819 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3825 l2input_main_t *l2im = &l2input_main;
3826 l2_input_config_t *config;
3827 l2_bridge_domain_t *bd_config;
3829 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3830 config = vec_elt_at_index (l2im->configs, sw_if_index0);
3831 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3833 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
3837 clib_warning ("bridge domain %d is not mapped to any vni!",
3844 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3845 gid_address_t * src, gid_address_t * dst)
3850 memset (src, 0, sizeof (*src));
3851 memset (dst, 0, sizeof (*dst));
3852 type = vnet_buffer (b)->lisp.overlay_afi;
3854 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3857 u8 version, preflen;
3859 gid_address_type (src) = GID_ADDR_IP_PREFIX;
3860 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
3862 ip = vlib_buffer_get_current (b);
3863 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
3865 version = gid_address_ip_version (src);
3866 preflen = ip_address_max_len (version);
3867 gid_address_ippref_len (src) = preflen;
3868 gid_address_ippref_len (dst) = preflen;
3870 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3871 gid_address_vni (dst) = vni;
3872 gid_address_vni (src) = vni;
3874 else if (LISP_AFI_MAC == type)
3876 ethernet_header_t *eh;
3878 eh = vlib_buffer_get_current (b);
3880 gid_address_type (src) = GID_ADDR_MAC;
3881 gid_address_type (dst) = GID_ADDR_MAC;
3882 mac_copy (&gid_address_mac (src), eh->src_address);
3883 mac_copy (&gid_address_mac (dst), eh->dst_address);
3886 vni = lisp_get_vni_from_buffer_eth (lcm, b);
3888 gid_address_vni (dst) = vni;
3889 gid_address_vni (src) = vni;
3894 lisp_cp_lookup_inline (vlib_main_t * vm,
3895 vlib_node_runtime_t * node,
3896 vlib_frame_t * from_frame, int overlay)
3898 u32 *from, *to_next_drop, di, si;
3899 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3900 u32 pkts_mapped = 0;
3901 uword n_left_from, n_left_to_next_drop;
3903 from = vlib_frame_vector_args (from_frame);
3904 n_left_from = from_frame->n_vectors;
3906 while (n_left_from > 0)
3908 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3909 to_next_drop, n_left_to_next_drop);
3911 while (n_left_from > 0 && n_left_to_next_drop > 0)
3915 gid_address_t src, dst;
3920 to_next_drop[0] = pi0;
3922 n_left_to_next_drop -= 1;
3924 b0 = vlib_get_buffer (vm, pi0);
3925 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
3926 vnet_buffer (b0)->lisp.overlay_afi = overlay;
3928 /* src/dst eid pair */
3929 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
3931 /* if we have remote mapping for destination already in map-chache
3932 add forwarding tunnel directly. If not send a map-request */
3933 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3937 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3938 /* send a map-request also in case of negative mapping entry
3939 with corresponding action */
3940 if (m->action == LISP_SEND_MAP_REQUEST)
3942 /* send map-request */
3943 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3944 0 /* is_resend */ );
3949 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3953 dp_add_fwd_entry (lcm, si, di);
3959 /* send map-request */
3960 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3961 0 /* is_resend */ );
3965 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3967 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3970 memset (tr, 0, sizeof (*tr));
3971 gid_address_copy (&tr->dst_eid, &dst);
3972 ip_address_copy (&tr->map_resolver_ip,
3973 &lcm->active_map_resolver);
3975 gid_address_free (&dst);
3976 gid_address_free (&src);
3979 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3980 n_left_to_next_drop);
3982 vlib_node_increment_counter (vm, node->node_index,
3983 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
3985 return from_frame->n_vectors;
3989 lisp_cp_lookup_ip4 (vlib_main_t * vm,
3990 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3992 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3996 lisp_cp_lookup_ip6 (vlib_main_t * vm,
3997 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3999 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
4003 lisp_cp_lookup_l2 (vlib_main_t * vm,
4004 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4006 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
4010 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
4011 .function = lisp_cp_lookup_ip4,
4012 .name = "lisp-cp-lookup-ip4",
4013 .vector_size = sizeof (u32),
4014 .format_trace = format_lisp_cp_lookup_trace,
4015 .type = VLIB_NODE_TYPE_INTERNAL,
4017 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4018 .error_strings = lisp_cp_lookup_error_strings,
4020 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4023 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4029 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
4030 .function = lisp_cp_lookup_ip6,
4031 .name = "lisp-cp-lookup-ip6",
4032 .vector_size = sizeof (u32),
4033 .format_trace = format_lisp_cp_lookup_trace,
4034 .type = VLIB_NODE_TYPE_INTERNAL,
4036 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4037 .error_strings = lisp_cp_lookup_error_strings,
4039 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4042 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4048 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
4049 .function = lisp_cp_lookup_l2,
4050 .name = "lisp-cp-lookup-l2",
4051 .vector_size = sizeof (u32),
4052 .format_trace = format_lisp_cp_lookup_trace,
4053 .type = VLIB_NODE_TYPE_INTERNAL,
4055 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4056 .error_strings = lisp_cp_lookup_error_strings,
4058 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4061 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4066 /* lisp_cp_input statistics */
4067 #define foreach_lisp_cp_input_error \
4069 _(MAP_REPLIES_RECEIVED, "map-replies received")
4071 static char *lisp_cp_input_error_strings[] = {
4072 #define _(sym,string) string,
4073 foreach_lisp_cp_input_error
4079 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
4080 foreach_lisp_cp_input_error
4082 LISP_CP_INPUT_N_ERROR,
4083 } lisp_cp_input_error_t;
4087 LISP_CP_INPUT_NEXT_DROP,
4088 LISP_CP_INPUT_N_NEXT,
4089 } lisp_cp_input_next_t;
4093 gid_address_t dst_eid;
4094 ip4_address_t map_resolver_ip;
4095 } lisp_cp_input_trace_t;
4098 format_lisp_cp_input_trace (u8 * s, va_list * args)
4100 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
4101 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
4102 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
4103 va_arg (*args, lisp_cp_input_trace_t *);
4105 s = format (s, "LISP-CP-INPUT: TODO");
4110 remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
4114 m = pool_elt_at_index (lcm->mapping_pool, mi);
4115 lisp_add_del_adjacency (lcm, 0, &m->eid, 0 /* is_add */ );
4116 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
4117 0 /* is_static */ , 0);
4118 mapping_delete_timer (lcm, mi);
4122 mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
4123 f64 expiration_time)
4126 u64 now = clib_cpu_time_now ();
4127 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
4128 u64 exp_clock_time = now + expiration_time * cpu_cps;
4130 m = pool_elt_at_index (lcm->mapping_pool, mi);
4133 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
4137 map_records_arg_free (map_records_arg_t * a)
4140 vec_foreach (m, a->mappings)
4142 vec_free (m->locators);
4143 gid_address_free (&m->eid);
4150 process_map_reply (map_records_arg_t * a)
4153 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4154 u32 dst_map_index = 0;
4155 pending_map_request_t *pmr;
4159 if (a->is_rloc_probe)
4162 /* Check pending requests table and nonce */
4163 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
4166 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
4169 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
4171 vec_foreach (m, a->mappings)
4173 /* insert/update mappings cache */
4174 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
4175 m->authoritative, m->ttl,
4176 1, 0 /* is_static */ , &dst_map_index);
4178 /* try to program forwarding only if mapping saved or updated */
4179 if ((u32) ~ 0 != dst_map_index)
4181 lisp_add_del_adjacency (lcm, &pmr->src, &m->eid, 1);
4182 if ((u32) ~ 0 != m->ttl)
4183 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
4187 /* remove pending map request entry */
4190 clib_fifo_foreach (noncep, pmr->nonces, ({
4191 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
4195 clib_fifo_free (pmr->nonces);
4196 pool_put (lcm->pending_map_requests_pool, pmr);
4199 map_records_arg_free (a);
4204 is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
4205 lisp_key_type_t key_id, u8 * key)
4211 auth_data_len = auth_data_len_by_key_id (key_id);
4212 if ((u16) ~ 0 == auth_data_len)
4214 clib_warning ("invalid length for key_id %d!", key_id);
4218 /* save auth data */
4219 vec_validate (auth_data, auth_data_len - 1);
4220 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
4222 /* clear auth data */
4223 memset (MNOTIFY_DATA (h), 0, auth_data_len);
4225 /* get hash of the message */
4226 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
4227 (unsigned char *) h, msg_len, NULL, NULL);
4229 result = memcmp (code, auth_data, auth_data_len);
4231 vec_free (auth_data);
4237 process_map_notify (map_records_arg_t * a)
4239 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4242 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
4245 clib_warning ("No pending map-register entry with nonce %lu!",
4250 map_records_arg_free (a);
4251 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
4255 get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
4259 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
4262 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
4266 return pool_elt_at_index (lcm->mapping_pool, mi);
4270 * When map-notify is received it is necessary that all EIDs in the record
4271 * list share common key. The key is then used to verify authentication
4272 * data in map-notify message.
4275 map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
4276 u32 key_id, u8 ** key_out)
4278 u32 i, len = vec_len (maps);
4281 /* get key of the first mapping */
4282 m = get_mapping (lcm, &maps[0].eid);
4286 key_out[0] = m->key;
4288 for (i = 1; i < len; i++)
4290 m = get_mapping (lcm, &maps[i].eid);
4294 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
4296 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
4304 parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
4306 locator_t *locators = 0;
4312 /* parse record eid */
4313 for (i = 0; i < count; i++)
4315 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
4318 clib_warning ("Failed to parse mapping record!");
4319 vec_foreach (loc, locators) locator_free (loc);
4320 vec_free (locators);
4324 m.locators = locators;
4325 gid_address_copy (&m.eid, &deid);
4326 vec_add1 (a->mappings, m);
4332 static map_records_arg_t *
4333 parse_map_notify (vlib_buffer_t * b)
4336 map_notify_hdr_t *mnotif_hdr;
4337 lisp_key_type_t key_id;
4338 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4341 u16 auth_data_len = 0;
4343 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
4345 memset (a, 0, sizeof (*a));
4346 mnotif_hdr = vlib_buffer_get_current (b);
4347 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
4348 memset (&deid, 0, sizeof (deid));
4350 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
4351 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
4352 auth_data_len = auth_data_len_by_key_id (key_id);
4354 /* advance buffer by authentication data */
4355 vlib_buffer_pull (b, auth_data_len);
4357 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
4358 rc = parse_map_records (b, a, record_count);
4361 map_records_arg_free (a);
4365 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
4368 map_records_arg_free (a);
4372 /* verify authentication data */
4373 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
4374 - (u8 *) mnotif_hdr, key_id, key))
4376 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
4378 map_records_arg_free (a);
4384 static vlib_buffer_t *
4385 build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
4386 ip_address_t * dst, u64 nonce, u8 probe_bit,
4387 mapping_t * records, u16 dst_port, u32 * bi_res)
4391 vlib_main_t *vm = lcm->vlib_main;
4393 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
4395 clib_warning ("Can't allocate buffer for Map-Register!");
4399 b = vlib_get_buffer (vm, bi);
4401 /* leave some space for the encap headers */
4402 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
4404 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
4406 /* push outer ip header */
4407 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
4414 send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
4415 u8 probe_bit, u64 nonce, u16 dst_port,
4416 ip_address_t * probed_loc)
4422 u32 next_index, *to_next;
4423 mapping_t *records = 0, *m;
4425 m = pool_elt_at_index (lcm->mapping_pool, mi);
4429 vec_add1 (records, m[0]);
4430 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
4431 memset (&src, 0, sizeof (src));
4433 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
4435 clib_warning ("can't find inteface address for %U", format_ip_address,
4440 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
4444 free_map_register_records (records);
4446 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
4447 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
4448 ip4_lookup_node.index : ip6_lookup_node.index;
4450 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
4452 /* Enqueue the packet */
4453 to_next = vlib_frame_vector_args (f);
4456 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
4461 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm,
4464 u8 *ip_hdr = 0, *udp_hdr;
4467 ip_address_t *dst_loc = 0, probed_loc, src_loc;
4469 map_request_hdr_t *mreq_hdr;
4470 gid_address_t src, dst;
4473 gid_address_t *itr_rlocs = 0;
4475 mreq_hdr = vlib_buffer_get_current (b);
4477 // TODO ugly workaround to find out whether LISP is carried by ip4 or 6
4478 // and needs to be fixed
4479 udp_hdr = (u8 *) vlib_buffer_get_current (b) - sizeof (udp_header_t);
4480 ip4 = (ip4_header_t *) (udp_hdr - sizeof (ip4_header_t));
4481 ip6 = (ip6_header_t *) (udp_hdr - sizeof (ip6_header_t));
4483 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
4484 ip_hdr = (u8 *) ip4;
4487 u32 flags = clib_net_to_host_u32
4488 (ip6->ip_version_traffic_class_and_flow_label);
4489 if ((flags & 0xF0000000) == 0x60000000)
4490 ip_hdr = (u8 *) ip6;
4493 clib_warning ("internal error: cannot determine whether packet "
4499 vlib_buffer_pull (b, sizeof (*mreq_hdr));
4501 nonce = MREQ_NONCE (mreq_hdr);
4503 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
4506 ("Only SMR Map-Requests and RLOC probe supported for now!");
4511 len = lisp_msg_parse_addr (b, &src);
4515 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
4516 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
4520 /* parse eid records and send SMR-invoked map-requests */
4521 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
4523 memset (&dst, 0, sizeof (dst));
4524 len = lisp_msg_parse_eid_rec (b, &dst);
4527 clib_warning ("Can't parse map-request EID-record");
4531 if (MREQ_SMR (mreq_hdr))
4533 /* send SMR-invoked map-requests */
4534 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
4536 else if (MREQ_RLOC_PROBE (mreq_hdr))
4538 memset (&m, 0, sizeof (m));
4539 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
4541 // TODO: select best locator; for now use the first one
4542 dst_loc = &gid_address_ip (&itr_rlocs[0]);
4544 /* get src/dst IP addresses */
4545 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
4547 // TODO get source port from buffer
4548 u16 src_port = LISP_CONTROL_PORT;
4550 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
4551 src_port, &probed_loc);
4556 vec_free (itr_rlocs);
4559 static map_records_arg_t *
4560 parse_map_reply (vlib_buffer_t * b)
4567 map_reply_hdr_t *mrep_hdr;
4568 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
4569 memset (a, 0, sizeof (*a));
4570 locator_t *locators;
4572 mrep_hdr = vlib_buffer_get_current (b);
4573 a->nonce = MREP_NONCE (mrep_hdr);
4574 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
4575 vlib_buffer_pull (b, sizeof (*mrep_hdr));
4577 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
4579 memset (&m, 0, sizeof (m));
4581 h = vlib_buffer_get_current (b);
4583 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
4584 m.action = MAP_REC_ACTION (h);
4585 m.authoritative = MAP_REC_AUTH (h);
4587 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
4590 clib_warning ("Failed to parse mapping record!");
4591 map_records_arg_free (a);
4595 m.locators = locators;
4596 gid_address_copy (&m.eid, &deid);
4597 vec_add1 (a->mappings, m);
4603 queue_map_reply_for_processing (map_records_arg_t * a)
4605 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (a));
4609 queue_map_notify_for_processing (map_records_arg_t * a)
4611 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
4615 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
4616 vlib_frame_t * from_frame)
4618 u32 n_left_from, *from, *to_next_drop;
4619 lisp_msg_type_e type;
4620 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4621 map_records_arg_t *a;
4623 from = vlib_frame_vector_args (from_frame);
4624 n_left_from = from_frame->n_vectors;
4627 while (n_left_from > 0)
4629 u32 n_left_to_next_drop;
4631 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4632 to_next_drop, n_left_to_next_drop);
4633 while (n_left_from > 0 && n_left_to_next_drop > 0)
4641 to_next_drop[0] = bi0;
4643 n_left_to_next_drop -= 1;
4645 b0 = vlib_get_buffer (vm, bi0);
4647 type = lisp_msg_type (vlib_buffer_get_current (b0));
4650 case LISP_MAP_REPLY:
4651 a = parse_map_reply (b0);
4653 queue_map_reply_for_processing (a);
4655 case LISP_MAP_REQUEST:
4656 process_map_request (vm, lcm, b0);
4658 case LISP_MAP_NOTIFY:
4659 a = parse_map_notify (b0);
4661 queue_map_notify_for_processing (a);
4664 clib_warning ("Unsupported LISP message type %d", type);
4668 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
4670 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
4676 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4677 n_left_to_next_drop);
4679 return from_frame->n_vectors;
4683 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
4684 .function = lisp_cp_input,
4685 .name = "lisp-cp-input",
4686 .vector_size = sizeof (u32),
4687 .format_trace = format_lisp_cp_input_trace,
4688 .type = VLIB_NODE_TYPE_INTERNAL,
4690 .n_errors = LISP_CP_INPUT_N_ERROR,
4691 .error_strings = lisp_cp_input_error_strings,
4693 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
4696 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
4702 lisp_cp_init (vlib_main_t * vm)
4704 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4705 clib_error_t *error = 0;
4707 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
4710 lcm->im4 = &ip4_main;
4711 lcm->im6 = &ip6_main;
4712 lcm->vlib_main = vm;
4713 lcm->vnet_main = vnet_get_main ();
4714 lcm->mreq_itr_rlocs = ~0;
4716 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
4718 gid_dictionary_init (&lcm->mapping_index_by_gid);
4719 lcm->do_map_resolver_election = 1;
4720 lcm->map_request_mode = MR_MODE_DST_ONLY;
4722 /* default vrf mapped to vni 0 */
4723 hash_set (lcm->table_id_by_vni, 0, 0);
4724 hash_set (lcm->vni_by_table_id, 0, 0);
4726 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
4727 lisp_cp_input_node.index, 1 /* is_ip4 */ );
4728 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
4729 lisp_cp_input_node.index, 0 /* is_ip4 */ );
4731 u64 now = clib_cpu_time_now ();
4732 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
4737 send_map_request_thread_fn (void *arg)
4739 map_request_args_t *a = arg;
4740 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4743 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4745 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4751 queue_map_request (gid_address_t * seid, gid_address_t * deid,
4752 u8 smr_invoked, u8 is_resend)
4754 map_request_args_t a;
4756 a.is_resend = is_resend;
4757 gid_address_copy (&a.seid, seid);
4758 gid_address_copy (&a.deid, deid);
4759 a.smr_invoked = smr_invoked;
4761 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
4762 (u8 *) & a, sizeof (a));
4767 * Take an action with a pending map request depending on expiration time
4768 * and re-try counters.
4771 update_pending_request (pending_map_request_t * r, f64 dt)
4773 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4776 if (r->time_to_expire - dt < 0)
4777 /* it's time to decide what to do with this pending request */
4779 if (r->retries_num >= NUMBER_OF_RETRIES)
4780 /* too many retries -> assume current map resolver is not available */
4782 mr = get_map_resolver (&lcm->active_map_resolver);
4785 clib_warning ("Map resolver %U not found - probably deleted "
4786 "by the user recently.", format_ip_address,
4787 &lcm->active_map_resolver);
4791 clib_warning ("map resolver %U is unreachable, ignoring",
4792 format_ip_address, &lcm->active_map_resolver);
4794 /* mark current map resolver unavailable so it won't be
4795 * selected next time */
4797 mr->last_update = vlib_time_now (lcm->vlib_main);
4800 reset_pending_mr_counters (r);
4801 elect_map_resolver (lcm);
4803 /* try to find a next eligible map resolver and re-send */
4804 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4810 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4813 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
4817 r->time_to_expire -= dt;
4821 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4824 pending_map_request_t *pmr;
4825 u32 *to_be_removed = 0, *pmr_index;
4828 pool_foreach (pmr, lcm->pending_map_requests_pool,
4830 if (pmr->to_be_removed)
4832 clib_fifo_foreach (nonce, pmr->nonces, ({
4833 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4836 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4841 vec_foreach (pmr_index, to_be_removed)
4842 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
4844 vec_free (to_be_removed);
4848 update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4850 static f64 time_left = RLOC_PROBING_INTERVAL;
4852 if (!lcm->is_enabled || !lcm->rloc_probing)
4858 time_left = RLOC_PROBING_INTERVAL;
4859 send_rloc_probes (lcm);
4864 update_map_register (lisp_cp_main_t * lcm, f64 dt)
4866 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4867 static u64 mreg_sent_counter = 0;
4869 if (!lcm->is_enabled || !lcm->map_registering)
4875 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4876 time_left = MAP_REGISTER_INTERVAL;
4879 mreg_sent_counter++;
4880 time_left = QUICK_MAP_REGISTER_INTERVAL;
4882 send_map_register (lcm, 1 /* want map notify */ );
4887 send_map_resolver_service (vlib_main_t * vm,
4888 vlib_node_runtime_t * rt, vlib_frame_t * f)
4892 pending_map_request_t *pmr;
4893 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4897 vlib_process_wait_for_event_or_clock (vm, period);
4899 /* currently no signals are expected - just wait for clock */
4900 (void) vlib_process_get_events (vm, 0);
4903 pool_foreach (pmr, lcm->pending_map_requests_pool,
4905 if (!pmr->to_be_removed)
4906 update_pending_request (pmr, period);
4910 remove_dead_pending_map_requests (lcm);
4912 update_map_register (lcm, period);
4913 update_rloc_probing (lcm, period);
4915 u64 now = clib_cpu_time_now ();
4917 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
4918 if (vec_len (expired) > 0)
4921 vec_foreach (mi, expired)
4923 remove_expired_mapping (lcm, mi[0]);
4925 _vec_len (expired) = 0;
4934 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
4935 .function = send_map_resolver_service,
4936 .type = VLIB_NODE_TYPE_PROCESS,
4937 .name = "lisp-retry-service",
4938 .process_log2_n_stack_bytes = 16,
4942 VLIB_INIT_FUNCTION (lisp_cp_init);
4945 * fd.io coding-style-patch-verification: ON
4948 * eval: (c-set-style "gnu")