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, &a->lcl_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);
525 } fwd_entry_mt_arg_t;
528 dp_add_fwd_entry_thread_fn (void *arg)
530 fwd_entry_mt_arg_t *a = arg;
531 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
532 dp_add_fwd_entry (lcm, a->si, a->di);
537 dp_add_fwd_entry_from_mt (u32 si, u32 di)
539 fwd_entry_mt_arg_t a;
541 memset (&a, 0, sizeof (a));
545 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
546 (u8 *) & a, sizeof (a));
551 * Returns vector of adjacencies.
553 * The caller must free the vector returned by this function.
555 * @param vni virtual network identifier
556 * @return vector of adjacencies
559 vnet_lisp_adjacencies_get_by_vni (u32 vni)
561 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
563 lisp_adjacency_t *adjs = 0, adj;
566 pool_foreach(fwd, lcm->fwd_entry_pool,
568 if (gid_address_vni (&fwd->reid) != vni)
571 gid_address_copy (&adj.reid, &fwd->reid);
572 gid_address_copy (&adj.leid, &fwd->leid);
573 vec_add1 (adjs, adj);
580 static clib_error_t *
581 lisp_show_adjacencies_command_fn (vlib_main_t * vm,
582 unformat_input_t * input,
583 vlib_cli_command_t * cmd)
585 lisp_adjacency_t *adjs, *adj;
586 vlib_cli_output (vm, "%s %40s\n", "leid", "reid");
587 unformat_input_t _line_input, *line_input = &_line_input;
590 /* Get a line of input. */
591 if (!unformat_user (input, unformat_line_input, line_input))
594 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
596 if (unformat (line_input, "vni %d", &vni))
600 vlib_cli_output (vm, "parse error: '%U'",
601 format_unformat_error, line_input);
608 vlib_cli_output (vm, "error: no vni specified!");
612 adjs = vnet_lisp_adjacencies_get_by_vni (vni);
614 vec_foreach (adj, adjs)
616 vlib_cli_output (vm, "%U %40U\n", format_gid_address, &adj->leid,
617 format_gid_address, &adj->reid);
625 VLIB_CLI_COMMAND (lisp_show_adjacencies_command) = {
626 .path = "show lisp adjacencies",
627 .short_help = "show lisp adjacencies",
628 .function = lisp_show_adjacencies_command_fn,
633 get_map_server (ip_address_t * a)
635 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
638 vec_foreach (m, lcm->map_servers)
640 if (!ip_address_cmp (&m->address, a))
649 get_map_resolver (ip_address_t * a)
651 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
654 vec_foreach (m, lcm->map_resolvers)
656 if (!ip_address_cmp (&m->address, a))
665 vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
668 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
669 lisp_msmr_t _ms, *ms = &_ms;
671 if (vnet_lisp_enable_disable_status () == 0)
673 clib_warning ("LISP is disabled!");
674 return VNET_API_ERROR_LISP_DISABLED;
679 if (get_map_server (addr))
681 clib_warning ("map-server %U already exists!", format_ip_address,
686 memset (ms, 0, sizeof (*ms));
687 ip_address_copy (&ms->address, addr);
688 vec_add1 (lcm->map_servers, ms[0]);
692 for (i = 0; i < vec_len (lcm->map_servers); i++)
694 ms = vec_elt_at_index (lcm->map_servers, i);
695 if (!ip_address_cmp (&ms->address, addr))
697 vec_del1 (lcm->map_servers, i);
706 static clib_error_t *
707 lisp_add_del_map_server_command_fn (vlib_main_t * vm,
708 unformat_input_t * input,
709 vlib_cli_command_t * cmd)
712 u8 is_add = 1, ip_set = 0;
714 unformat_input_t _line_input, *line_input = &_line_input;
716 /* Get a line of input. */
717 if (!unformat_user (input, unformat_line_input, line_input))
720 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
722 if (unformat (line_input, "add"))
724 else if (unformat (line_input, "del"))
726 else if (unformat (line_input, "%U", unformat_ip_address, &ip))
730 vlib_cli_output (vm, "parse error: '%U'",
731 format_unformat_error, line_input);
738 vlib_cli_output (vm, "map-server ip address not set!");
742 rv = vnet_lisp_add_del_map_server (&ip, is_add);
744 vlib_cli_output (vm, "failed to %s map-server!",
745 is_add ? "add" : "delete");
751 VLIB_CLI_COMMAND (lisp_add_del_map_server_command) = {
752 .path = "lisp map-server",
753 .short_help = "lisp map-server add|del <ip>",
754 .function = lisp_add_del_map_server_command_fn,
759 * Add/remove mapping to/from map-cache. Overwriting not allowed.
762 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
763 u32 * map_index_result)
765 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
766 u32 mi, *map_indexp, map_index, i;
767 mapping_t *m, *old_map;
770 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
771 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
774 /* TODO check if overwriting and take appropriate actions */
775 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
777 clib_warning ("eid %U found in the eid-table", format_gid_address,
779 return VNET_API_ERROR_VALUE_EXIST;
782 pool_get (lcm->mapping_pool, m);
783 gid_address_copy (&m->eid, &a->eid);
784 m->locator_set_index = a->locator_set_index;
786 m->action = a->action;
788 m->is_static = a->is_static;
789 m->key = vec_dup (a->key);
790 m->key_id = a->key_id;
792 map_index = m - lcm->mapping_pool;
793 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
796 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
798 clib_warning ("Locator set with index %d doesn't exist",
799 a->locator_set_index);
800 return VNET_API_ERROR_INVALID_VALUE;
803 /* add eid to list of eids supported by locator-set */
804 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
805 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
806 a->locator_set_index);
807 vec_add1 (eid_indexes[0], map_index);
812 vec_add1 (lcm->local_mappings_indexes, map_index);
814 map_index_result[0] = map_index;
818 if (mi == GID_LOOKUP_MISS)
820 clib_warning ("eid %U not found in the eid-table",
821 format_gid_address, &a->eid);
822 return VNET_API_ERROR_INVALID_VALUE;
825 /* clear locator-set to eids binding */
826 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
827 a->locator_set_index);
828 for (i = 0; i < vec_len (eid_indexes[0]); i++)
830 map_indexp = vec_elt_at_index (eid_indexes[0], i);
831 if (map_indexp[0] == mi)
834 vec_del1 (eid_indexes[0], i);
836 /* remove local mark if needed */
837 m = pool_elt_at_index (lcm->mapping_pool, mi);
841 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
843 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
844 if (lm_indexp[0] == mi)
847 vec_del1 (lcm->local_mappings_indexes, k);
850 /* remove mapping from dictionary */
851 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
852 gid_address_free (&m->eid);
853 pool_put_index (lcm->mapping_pool, mi);
860 * Add/update/delete mapping to/in/from map-cache.
863 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
864 u32 * map_index_result)
870 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
872 if (vnet_lisp_enable_disable_status () == 0)
874 clib_warning ("LISP is disabled!");
875 return VNET_API_ERROR_LISP_DISABLED;
878 vni = gid_address_vni (&a->eid);
879 type = gid_address_type (&a->eid);
880 if (GID_ADDR_IP_PREFIX == type)
881 dp_table = hash_get (lcm->table_id_by_vni, vni);
882 else if (GID_ADDR_MAC == type)
883 dp_table = hash_get (lcm->bd_id_by_vni, vni);
887 clib_warning ("vni %d not associated to a %s!", vni,
888 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
889 return VNET_API_ERROR_INVALID_VALUE;
892 /* store/remove mapping from map-cache */
893 return vnet_lisp_map_cache_add_del (a, map_index_result);
896 static clib_error_t *
897 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
898 vlib_cli_command_t * cmd)
900 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
901 unformat_input_t _line_input, *line_input = &_line_input;
904 gid_address_t *eids = 0;
905 clib_error_t *error = 0;
906 u8 *locator_set_name = 0;
907 u32 locator_set_index = 0, map_index = 0;
909 vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
915 memset (&eid, 0, sizeof (eid));
916 memset (a, 0, sizeof (*a));
918 /* Get a line of input. */
919 if (!unformat_user (input, unformat_line_input, line_input))
922 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
924 if (unformat (line_input, "add"))
926 else if (unformat (line_input, "del"))
928 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
930 else if (unformat (line_input, "vni %d", &vni))
931 gid_address_vni (&eid) = vni;
932 else if (unformat (line_input, "secret-key %_%v%_", &key))
934 else if (unformat (line_input, "key-id %U", unformat_hmac_key_id,
937 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
939 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
942 error = clib_error_return (0, "locator-set %s doesn't exist",
946 locator_set_index = p[0];
950 error = unformat_parse_error (line_input);
954 /* XXX treat batch configuration */
956 if (GID_ADDR_SRC_DST == gid_address_type (&eid))
959 clib_error_return (0, "src/dst is not supported for local EIDs!");
963 if (key && (0 == key_id))
965 vlib_cli_output (vm, "invalid key_id!");
969 gid_address_copy (&a->eid, &eid);
971 a->locator_set_index = locator_set_index;
976 rv = vnet_lisp_add_del_local_mapping (a, &map_index);
979 error = clib_error_return (0, "failed to %s local mapping!",
980 is_add ? "add" : "delete");
984 if (locator_set_name)
985 vec_free (locator_set_name);
986 gid_address_free (&a->eid);
992 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
993 .path = "lisp eid-table",
994 .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
995 "locator-set <locator-set> [key <secret-key> key-id sha1|sha256 ]",
996 .function = lisp_add_del_local_eid_command_fn,
1001 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
1003 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1004 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
1006 if (vnet_lisp_enable_disable_status () == 0)
1008 clib_warning ("LISP is disabled!");
1012 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
1013 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
1015 if (!is_l2 && (vni == 0 || dp_id == 0))
1017 clib_warning ("can't add/del default vni-vrf mapping!");
1021 dp_idp = hash_get (dp_table_by_vni[0], vni);
1022 vnip = hash_get (vni_by_dp_table[0], dp_id);
1028 clib_warning ("vni %d or vrf %d already used in vrf/vni "
1029 "mapping!", vni, dp_id);
1032 hash_set (dp_table_by_vni[0], vni, dp_id);
1033 hash_set (vni_by_dp_table[0], dp_id, vni);
1035 /* create dp iface */
1036 dp_add_del_iface (lcm, vni, is_l2, 1);
1040 if (!dp_idp || !vnip)
1042 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
1043 "mapping!", vni, dp_id);
1046 hash_unset (dp_table_by_vni[0], vni);
1047 hash_unset (vni_by_dp_table[0], dp_id);
1049 /* remove dp iface */
1050 dp_add_del_iface (lcm, vni, is_l2, 0);
1056 static clib_error_t *
1057 lisp_eid_table_map_command_fn (vlib_main_t * vm,
1058 unformat_input_t * input,
1059 vlib_cli_command_t * cmd)
1061 u8 is_add = 1, is_l2 = 0;
1062 u32 vni = 0, dp_id = 0;
1063 unformat_input_t _line_input, *line_input = &_line_input;
1065 /* Get a line of input. */
1066 if (!unformat_user (input, unformat_line_input, line_input))
1069 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1071 if (unformat (line_input, "del"))
1073 else if (unformat (line_input, "vni %d", &vni))
1075 else if (unformat (line_input, "vrf %d", &dp_id))
1077 else if (unformat (line_input, "bd %d", &dp_id))
1081 return unformat_parse_error (line_input);
1084 vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
1089 VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
1090 .path = "lisp eid-table map",
1091 .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
1092 .function = lisp_eid_table_map_command_fn,
1096 /* return 0 if the two locator sets are identical 1 otherwise */
1098 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
1099 locator_t * new_locators)
1102 locator_t *old_loc, *new_loc;
1104 if (vec_len (old_ls_indexes) != vec_len (new_locators))
1107 for (i = 0; i < vec_len (new_locators); i++)
1109 old_li = vec_elt (old_ls_indexes, i);
1110 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
1112 new_loc = vec_elt_at_index (new_locators, i);
1114 if (locator_cmp (old_loc, new_loc))
1124 gid_address_t *eids_to_be_deleted;
1125 } remove_mapping_args_t;
1128 * Callback invoked when a sub-prefix is found
1131 remove_mapping_if_needed (u32 mi, void *arg)
1134 remove_mapping_args_t *a = arg;
1135 lisp_cp_main_t *lcm = a->lcm;
1139 m = pool_elt_at_index (lcm->mapping_pool, mi);
1143 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1147 if (0 != vec_len (ls->locator_indices))
1152 if (0 == vec_len (ls->locator_indices))
1157 vec_add1 (a->eids_to_be_deleted, m->eid);
1161 * This function searches map cache and looks for IP prefixes that are subset
1162 * of the provided one. If such prefix is found depending on 'is_negative'
1165 * 1) if is_negative is true and found prefix points to positive mapping,
1166 * then the mapping is removed
1167 * 2) if is_negative is false and found prefix points to negative mapping,
1168 * then the mapping is removed
1171 remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1175 remove_mapping_args_t a;
1176 memset (&a, 0, sizeof (a));
1178 /* do this only in src/dst mode ... */
1179 if (MR_MODE_SRC_DST != lcm->map_request_mode)
1182 /* ... and only for IP prefix */
1183 if (GID_ADDR_SRC_DST != gid_address_type (eid)
1184 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1187 a.is_negative = is_negative;
1190 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1191 remove_mapping_if_needed, &a);
1193 vec_foreach (e, a.eids_to_be_deleted)
1195 lisp_add_del_adjacency (lcm, 0, e, 0 /* is_add */ );
1196 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
1199 vec_free (a.eids_to_be_deleted);
1203 mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1205 timing_wheel_delete (&lcm->wheel, mi);
1209 * Adds/removes/updates mapping. Does not program forwarding.
1211 * @param eid end-host identifier
1212 * @param rlocs vector of remote locators
1213 * @param action action for negative map-reply
1214 * @param is_add add mapping if non-zero, delete otherwise
1215 * @param res_map_index the map-index that was created/updated/removed. It is
1216 * set to ~0 if no action is taken.
1217 * @param is_static used for distinguishing between statically learned
1218 remote mappings and mappings obtained from MR
1219 * @return return code
1222 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
1223 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
1224 u32 * res_map_index)
1226 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1227 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1228 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1229 u32 mi, ls_index = 0, dst_map_index;
1232 if (vnet_lisp_enable_disable_status () == 0)
1234 clib_warning ("LISP is disabled!");
1235 return VNET_API_ERROR_LISP_DISABLED;
1239 res_map_index[0] = ~0;
1241 memset (m_args, 0, sizeof (m_args[0]));
1242 memset (ls_args, 0, sizeof (ls_args[0]));
1244 ls_args->locators = rlocs;
1246 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
1247 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1251 /* overwrite: if mapping already exists, decide if locators should be
1252 * updated and be done */
1253 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
1255 if (!is_static && (old_map->is_static || old_map->local))
1257 /* do not overwrite local or static remote mappings */
1258 clib_warning ("mapping %U rejected due to collision with local "
1259 "or static remote mapping!", format_gid_address,
1264 locator_set_t *old_ls;
1266 /* update mapping attributes */
1267 old_map->action = action;
1268 old_map->authoritative = authoritative;
1271 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1272 old_map->locator_set_index);
1273 if (compare_locators (lcm, old_ls->locator_indices,
1276 /* set locator-set index to overwrite */
1277 ls_args->is_add = 1;
1278 ls_args->index = old_map->locator_set_index;
1279 vnet_lisp_add_del_locator_set (ls_args, 0);
1281 res_map_index[0] = mi;
1287 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1289 ls_args->is_add = 1;
1290 ls_args->index = ~0;
1292 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1295 gid_address_copy (&m_args->eid, eid);
1297 m_args->action = action;
1298 m_args->locator_set_index = ls_index;
1299 m_args->is_static = is_static;
1301 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
1304 res_map_index[0] = dst_map_index;
1309 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
1311 clib_warning ("cannot delete mapping for eid %U",
1312 format_gid_address, eid);
1317 gid_address_copy (&m_args->eid, eid);
1318 m_args->locator_set_index = old_map->locator_set_index;
1320 /* delete mapping associated from map-cache */
1321 vnet_lisp_map_cache_add_del (m_args, 0);
1323 ls_args->is_add = 0;
1324 ls_args->index = old_map->locator_set_index;
1325 /* delete locator set */
1326 vnet_lisp_add_del_locator_set (ls_args, 0);
1328 /* delete timer associated to the mapping if any */
1329 if (old_map->timer_set)
1330 mapping_delete_timer (lcm, mi);
1332 /* return old mapping index */
1334 res_map_index[0] = mi;
1342 vnet_lisp_clear_all_remote_adjacencies (void)
1345 u32 mi, *map_indices = 0, *map_indexp;
1346 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1347 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1348 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1351 pool_foreach_index (mi, lcm->mapping_pool,
1353 vec_add1 (map_indices, mi);
1357 vec_foreach (map_indexp, map_indices)
1359 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1362 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
1364 dm_args->is_add = 0;
1365 gid_address_copy (&dm_args->eid, &map->eid);
1366 dm_args->locator_set_index = map->locator_set_index;
1368 /* delete mapping associated to fwd entry */
1369 vnet_lisp_map_cache_add_del (dm_args, 0);
1373 ls->index = map->locator_set_index;
1374 /* delete locator set */
1375 rv = vnet_lisp_add_del_locator_set (ls, 0);
1383 vec_free (map_indices);
1388 * Adds adjacency or removes forwarding entry associated to remote mapping.
1389 * Note that adjacencies are not stored, they only result in forwarding entries
1393 lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
1394 gid_address_t * remote_eid, u8 is_add)
1396 u32 local_mi, remote_mi = ~0;
1398 if (vnet_lisp_enable_disable_status () == 0)
1400 clib_warning ("LISP is disabled!");
1401 return VNET_API_ERROR_LISP_DISABLED;
1404 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1405 remote_eid, local_eid);
1406 if (GID_LOOKUP_MISS == remote_mi)
1408 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1409 format_gid_address, remote_eid);
1416 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1419 /* check if source eid has an associated mapping. If pitr mode is on,
1420 * just use the pitr's mapping */
1421 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
1422 gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
1425 if (GID_LOOKUP_MISS == local_mi)
1427 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1428 format_gid_address, local_eid);
1433 /* update forwarding */
1434 dp_add_fwd_entry (lcm, local_mi, remote_mi);
1437 dp_del_fwd_entry (lcm, 0, remote_mi);
1443 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1445 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1446 return lisp_add_del_adjacency (lcm, &a->leid, &a->reid, a->is_add);
1450 * Handler for add/del remote mapping CLI.
1452 * @param vm vlib context
1453 * @param input input from user
1455 * @return pointer to clib error structure
1457 static clib_error_t *
1458 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
1459 unformat_input_t * input,
1460 vlib_cli_command_t * cmd)
1462 clib_error_t *error = 0;
1463 unformat_input_t _line_input, *line_input = &_line_input;
1464 u8 is_add = 1, del_all = 0;
1465 locator_t rloc, *rlocs = 0, *curr_rloc = 0;
1468 u32 vni, action = ~0, p, w;
1471 /* Get a line of input. */
1472 if (!unformat_user (input, unformat_line_input, line_input))
1475 memset (&eid, 0, sizeof (eid));
1476 memset (&rloc, 0, sizeof (rloc));
1478 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1480 if (unformat (line_input, "del-all"))
1482 else if (unformat (line_input, "del"))
1484 else if (unformat (line_input, "add"))
1486 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1488 else if (unformat (line_input, "vni %u", &vni))
1490 gid_address_vni (&eid) = vni;
1492 else if (unformat (line_input, "p %d w %d", &p, &w))
1497 ("No RLOC configured for setting priority/weight!");
1500 curr_rloc->priority = p;
1501 curr_rloc->weight = w;
1503 else if (unformat (line_input, "rloc %U", unformat_ip_address,
1504 &gid_address_ip (&rloc.address)))
1506 /* since rloc is stored in ip prefix we need to set prefix length */
1507 ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
1509 u8 version = gid_address_ip_version (&rloc.address);
1510 ip_prefix_len (pref) = ip_address_max_len (version);
1512 vec_add1 (rlocs, rloc);
1513 curr_rloc = &rlocs[vec_len (rlocs) - 1];
1515 else if (unformat (line_input, "action %U",
1516 unformat_negative_mapping_action, &action))
1520 clib_warning ("parse error");
1527 clib_warning ("missing eid!");
1533 if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1535 clib_warning ("no action set for negative map-reply!");
1541 vnet_lisp_clear_all_remote_adjacencies ();
1545 /* TODO build src/dst with seid */
1547 /* if it's a delete, clean forwarding */
1550 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1551 rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1558 /* add as static remote mapping, i.e., not authoritative and infinite
1560 rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
1561 1 /* is_static */ , 0);
1564 clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
1568 unformat_free (line_input);
1572 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) =
1574 .path = "lisp remote-mapping",.short_help =
1575 "lisp remote-mapping add|del [del-all] vni <vni> "
1576 "eid <est-eid> [action <no-action|natively-forward|"
1577 "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
1578 "[rloc <dst-locator> ... ]",.function =
1579 lisp_add_del_remote_mapping_command_fn,};
1582 * Handler for add/del adjacency CLI.
1584 static clib_error_t *
1585 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
1586 vlib_cli_command_t * cmd)
1588 clib_error_t *error = 0;
1589 unformat_input_t _line_input, *line_input = &_line_input;
1590 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1592 ip_prefix_t *reid_ippref, *leid_ippref;
1593 gid_address_t leid, reid;
1594 u8 *dmac = gid_address_mac (&reid);
1595 u8 *smac = gid_address_mac (&leid);
1596 u8 reid_set = 0, leid_set = 0;
1600 /* Get a line of input. */
1601 if (!unformat_user (input, unformat_line_input, line_input))
1604 memset (&reid, 0, sizeof (reid));
1605 memset (&leid, 0, sizeof (leid));
1607 leid_ippref = &gid_address_ippref (&leid);
1608 reid_ippref = &gid_address_ippref (&reid);
1610 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1612 if (unformat (line_input, "del"))
1614 else if (unformat (line_input, "add"))
1616 else if (unformat (line_input, "reid %U",
1617 unformat_ip_prefix, reid_ippref))
1619 gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
1622 else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
1624 gid_address_type (&reid) = GID_ADDR_MAC;
1627 else if (unformat (line_input, "vni %u", &vni))
1629 gid_address_vni (&leid) = vni;
1630 gid_address_vni (&reid) = vni;
1632 else if (unformat (line_input, "leid %U",
1633 unformat_ip_prefix, leid_ippref))
1635 gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
1638 else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
1640 gid_address_type (&leid) = GID_ADDR_MAC;
1645 clib_warning ("parse error");
1650 if (!reid_set || !leid_set)
1652 clib_warning ("missing remote or local eid!");
1656 if ((gid_address_type (&leid) != gid_address_type (&reid))
1657 || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
1658 && ip_prefix_version (reid_ippref)
1659 != ip_prefix_version (leid_ippref)))
1661 clib_warning ("remote and local EIDs are of different types!");
1665 memset (a, 0, sizeof (a[0]));
1666 gid_address_copy (&a->leid, &leid);
1667 gid_address_copy (&a->reid, &reid);
1670 rv = vnet_lisp_add_del_adjacency (a);
1673 clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
1676 unformat_free (line_input);
1681 VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1682 .path = "lisp adjacency",
1683 .short_help = "lisp adjacency add|del vni <vni> reid <remote-eid> "
1685 .function = lisp_add_del_adjacency_command_fn,
1690 vnet_lisp_set_map_request_mode (u8 mode)
1692 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1694 if (vnet_lisp_enable_disable_status () == 0)
1696 clib_warning ("LISP is disabled!");
1697 return VNET_API_ERROR_LISP_DISABLED;
1700 if (mode >= _MR_MODE_MAX)
1702 clib_warning ("Invalid LISP map request mode %d!", mode);
1703 return VNET_API_ERROR_INVALID_ARGUMENT;
1706 lcm->map_request_mode = mode;
1710 static clib_error_t *
1711 lisp_map_request_mode_command_fn (vlib_main_t * vm,
1712 unformat_input_t * input,
1713 vlib_cli_command_t * cmd)
1715 unformat_input_t _i, *i = &_i;
1716 map_request_mode_t mr_mode = _MR_MODE_MAX;
1718 /* Get a line of input. */
1719 if (!unformat_user (input, unformat_line_input, i))
1722 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1724 if (unformat (i, "dst-only"))
1725 mr_mode = MR_MODE_DST_ONLY;
1726 else if (unformat (i, "src-dst"))
1727 mr_mode = MR_MODE_SRC_DST;
1730 clib_warning ("parse error '%U'", format_unformat_error, i);
1735 if (_MR_MODE_MAX == mr_mode)
1737 clib_warning ("No LISP map request mode entered!");
1741 vnet_lisp_set_map_request_mode (mr_mode);
1747 VLIB_CLI_COMMAND (lisp_map_request_mode_command) = {
1748 .path = "lisp map-request mode",
1749 .short_help = "lisp map-request mode dst-only|src-dst",
1750 .function = lisp_map_request_mode_command_fn,
1755 format_lisp_map_request_mode (u8 * s, va_list * args)
1757 u32 mode = va_arg (*args, u32);
1762 return format (0, "dst-only");
1764 return format (0, "src-dst");
1769 static clib_error_t *
1770 lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
1771 unformat_input_t * input,
1772 vlib_cli_command_t * cmd)
1774 vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
1775 vnet_lisp_get_map_request_mode ());
1780 VLIB_CLI_COMMAND (lisp_show_map_request_mode_command) = {
1781 .path = "show lisp map-request mode",
1782 .short_help = "show lisp map-request mode",
1783 .function = lisp_show_map_request_mode_command_fn,
1787 static clib_error_t *
1788 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
1789 unformat_input_t * input,
1790 vlib_cli_command_t * cmd)
1793 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1795 vec_foreach (mr, lcm->map_resolvers)
1797 vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
1803 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1804 .path = "show lisp map-resolvers",
1805 .short_help = "show lisp map-resolvers",
1806 .function = lisp_show_map_resolvers_command_fn,
1811 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1813 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1814 u32 locator_set_index = ~0;
1818 if (vnet_lisp_enable_disable_status () == 0)
1820 clib_warning ("LISP is disabled!");
1821 return VNET_API_ERROR_LISP_DISABLED;
1824 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1827 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1830 locator_set_index = p[0];
1834 pool_get (lcm->mapping_pool, m);
1835 m->locator_set_index = locator_set_index;
1837 lcm->pitr_map_index = m - lcm->mapping_pool;
1839 /* enable pitr mode */
1844 /* remove pitr mapping */
1845 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1847 /* disable pitr mode */
1853 static clib_error_t *
1854 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
1855 unformat_input_t * input,
1856 vlib_cli_command_t * cmd)
1858 u8 locator_name_set = 0;
1859 u8 *locator_set_name = 0;
1861 unformat_input_t _line_input, *line_input = &_line_input;
1862 clib_error_t *error = 0;
1865 /* Get a line of input. */
1866 if (!unformat_user (input, unformat_line_input, line_input))
1869 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1871 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
1872 locator_name_set = 1;
1873 else if (unformat (line_input, "disable"))
1876 return clib_error_return (0, "parse error");
1879 if (!locator_name_set)
1881 clib_warning ("No locator set specified!");
1884 rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1887 error = clib_error_return (0, "failed to %s pitr!",
1888 is_add ? "add" : "delete");
1892 if (locator_set_name)
1893 vec_free (locator_set_name);
1898 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1899 .path = "lisp pitr",
1900 .short_help = "lisp pitr [disable] ls <locator-set-name>",
1901 .function = lisp_pitr_set_locator_set_command_fn,
1905 static clib_error_t *
1906 lisp_show_pitr_command_fn (vlib_main_t * vm,
1907 unformat_input_t * input, vlib_cli_command_t * cmd)
1909 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1914 vlib_cli_output (vm, "%=20s%=16s",
1915 "pitr", lcm->lisp_pitr ? "locator-set" : "");
1917 if (!lcm->lisp_pitr)
1919 vlib_cli_output (vm, "%=20s", "disable");
1923 if (~0 == lcm->pitr_map_index)
1925 tmp_str = format (0, "N/A");
1929 m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1930 if (~0 != m->locator_set_index)
1933 pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1934 tmp_str = format (0, "%s", ls->name);
1938 tmp_str = format (0, "N/A");
1941 vec_add1 (tmp_str, 0);
1943 vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1951 VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1952 .path = "show lisp pitr",
1953 .short_help = "Show pitr",
1954 .function = lisp_show_pitr_command_fn,
1959 format_eid_entry (u8 * s, va_list * args)
1961 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
1962 lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
1963 mapping_t *mapit = va_arg (*args, mapping_t *);
1964 locator_set_t *ls = va_arg (*args, locator_set_t *);
1965 gid_address_t *gid = &mapit->eid;
1966 u32 ttl = mapit->ttl;
1967 u8 aut = mapit->authoritative;
1972 u8 *type = ls->local ? format (0, "local(%s)", ls->name)
1973 : format (0, "remote");
1975 if (vec_len (ls->locator_indices) == 0)
1977 s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
1982 vec_foreach (loc_index, ls->locator_indices)
1984 locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1986 loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1989 loc = format (0, "%U", format_ip_address,
1990 &gid_address_ip (&l->address));
1994 s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
1995 gid, type, loc, ttl, aut);
1999 s = format (s, "%55s%v\n", "", loc);
2005 static clib_error_t *
2006 lisp_show_eid_table_command_fn (vlib_main_t * vm,
2007 unformat_input_t * input,
2008 vlib_cli_command_t * cmd)
2010 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2012 unformat_input_t _line_input, *line_input = &_line_input;
2018 memset (&eid, 0, sizeof (eid));
2020 /* Get a line of input. */
2021 if (!unformat_user (input, unformat_line_input, line_input))
2024 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2026 if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
2028 else if (unformat (line_input, "local"))
2030 else if (unformat (line_input, "remote"))
2033 return clib_error_return (0, "parse error: '%U'",
2034 format_unformat_error, line_input);
2037 vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
2038 "EID", "type", "locators", "ttl", "autoritative");
2043 pool_foreach (mapit, lcm->mapping_pool,
2045 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
2046 mapit->locator_set_index);
2047 if (filter && !((1 == filter && ls->local) ||
2048 (2 == filter && !ls->local)))
2052 vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
2059 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
2060 if ((u32) ~ 0 == mi)
2063 mapit = pool_elt_at_index (lcm->mapping_pool, mi);
2064 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2065 mapit->locator_set_index);
2067 if (filter && !((1 == filter && ls->local) ||
2068 (2 == filter && !ls->local)))
2073 vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
2081 VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
2082 .path = "show lisp eid-table",
2083 .short_help = "Shows EID table",
2084 .function = lisp_show_eid_table_command_fn,
2088 /* cleans locator to locator-set data and removes locators not part of
2089 * any locator-set */
2091 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
2093 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
2094 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
2095 for (i = 0; i < vec_len (ls->locator_indices); i++)
2097 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
2098 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2100 for (j = 0; j < vec_len (ls_indexes[0]); j++)
2102 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
2103 if (ls_indexp[0] == lsi)
2107 /* delete index for removed locator-set */
2108 vec_del1 (ls_indexes[0], j);
2110 /* delete locator if it's part of no locator-set */
2111 if (vec_len (ls_indexes[0]) == 0)
2113 pool_put_index (lcm->locator_pool, loc_indexp[0]);
2114 vec_add1 (to_be_deleted, i);
2120 for (i = 0; i < vec_len (to_be_deleted); i++)
2122 loc_indexp = vec_elt_at_index (to_be_deleted, i);
2123 vec_del1 (ls->locator_indices, loc_indexp[0]);
2125 vec_free (to_be_deleted);
2129 static inline uword *
2130 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
2132 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2137 /* find locator-set */
2140 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
2151 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
2157 ASSERT (ls != NULL);
2158 ASSERT (loc != NULL);
2160 vec_foreach (locit, ls->locator_indices)
2162 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2163 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
2164 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
2166 clib_warning ("Duplicate locator");
2167 return VNET_API_ERROR_VALUE_EXIST;
2175 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
2176 u32 ls_index, u32 loc_id)
2178 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2179 u32 **ls_indexes = NULL;
2181 ASSERT (ls != NULL);
2182 ASSERT (locit != NULL);
2184 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
2185 pool_put_index (lcm->locator_pool, locit[0]);
2186 vec_del1 (ls->locator_indices, loc_id);
2187 vec_del1 (ls_indexes[0], ls_index);
2191 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
2192 locator_set_t * ls, u32 * ls_result)
2194 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2195 locator_t *loc = NULL, *itloc = NULL;
2196 uword _p = (u32) ~ 0, *p = &_p;
2197 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
2203 if (vnet_lisp_enable_disable_status () == 0)
2205 clib_warning ("LISP is disabled!");
2206 return VNET_API_ERROR_LISP_DISABLED;
2209 p = get_locator_set_index (a, p);
2212 clib_warning ("locator-set %v doesn't exist", a->name);
2213 return VNET_API_ERROR_INVALID_ARGUMENT;
2218 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2221 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2223 return VNET_API_ERROR_INVALID_ARGUMENT;
2230 ls_result[0] = p[0];
2232 /* allocate locators */
2233 vec_foreach (itloc, a->locators)
2235 ret = is_locator_in_locator_set (lcm, ls, itloc);
2241 pool_get (lcm->locator_pool, loc);
2243 loc_index = loc - lcm->locator_pool;
2245 vec_add1 (ls->locator_indices, loc_index);
2247 vec_validate (lcm->locator_to_locator_sets, loc_index);
2248 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2250 vec_add1 (ls_indexes[0], p[0]);
2257 itloc = a->locators;
2259 vec_foreach (locit, ls->locator_indices)
2261 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2263 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
2265 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2267 if (0 == loc->local &&
2268 !gid_address_cmp (&loc->address, &itloc->address))
2270 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2281 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
2284 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2286 uword _p = (u32) ~ 0, *p = &_p;
2291 if (vnet_lisp_enable_disable_status () == 0)
2293 clib_warning ("LISP is disabled!");
2294 return VNET_API_ERROR_LISP_DISABLED;
2299 p = get_locator_set_index (a, p);
2302 if (p && p[0] != (u32) ~ 0)
2304 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2307 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2312 /* clean locator to locator-set vectors and remove locators if
2313 * they're not part of another locator-set */
2314 clean_locator_to_locator_set (lcm, p[0]);
2316 /* remove locator indices from locator set */
2317 vec_free (ls->locator_indices);
2322 ls_result[0] = p[0];
2324 /* new locator-set */
2327 pool_get (lcm->locator_set_pool, ls);
2328 memset (ls, 0, sizeof (*ls));
2329 ls_index = ls - lcm->locator_set_pool;
2333 ls->name = vec_dup (a->name);
2335 if (!lcm->locator_set_index_by_name)
2336 lcm->locator_set_index_by_name = hash_create_vec (
2344 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
2347 /* mark as local locator-set */
2348 vec_add1 (lcm->local_locator_set_indexes, ls_index);
2350 ls->local = a->local;
2352 ls_result[0] = ls_index;
2355 ret = vnet_lisp_add_del_locator (a, ls, NULL);
2363 p = get_locator_set_index (a, p);
2366 clib_warning ("locator-set %v doesn't exists", a->name);
2370 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2373 clib_warning ("locator-set with index %d doesn't exists", p[0]);
2377 if (lcm->mreq_itr_rlocs == p[0])
2379 clib_warning ("Can't delete the locator-set used to constrain "
2380 "the itr-rlocs in map-requests!");
2384 if (vec_len (lcm->locator_set_to_eids) != 0)
2386 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
2387 if (vec_len (eid_indexes[0]) != 0)
2390 ("Can't delete a locator that supports a mapping!");
2395 /* clean locator to locator-sets data */
2396 clean_locator_to_locator_set (lcm, p[0]);
2402 vec_foreach_index (it, lcm->local_locator_set_indexes)
2404 lsi = vec_elt (lcm->local_locator_set_indexes, it);
2407 vec_del1 (lcm->local_locator_set_indexes, it);
2411 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2413 vec_free (ls->name);
2414 vec_free (ls->locator_indices);
2415 pool_put (lcm->locator_set_pool, ls);
2421 vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
2423 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2425 lcm->rloc_probing = is_enable;
2430 vnet_lisp_map_register_enable_disable (u8 is_enable)
2432 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2434 lcm->map_registering = is_enable;
2439 vnet_lisp_enable_disable (u8 is_enable)
2442 clib_error_t *error = 0;
2443 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2444 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
2446 a->is_en = is_enable;
2447 error = vnet_lisp_gpe_enable_disable (a);
2450 return clib_error_return (0, "failed to %s data-plane!",
2451 a->is_en ? "enable" : "disable");
2456 /* enable all l2 and l3 ifaces */
2459 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2460 dp_add_del_iface(lcm, vni, 0, 1);
2462 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2463 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
2469 /* clear interface table */
2470 hash_free (lcm->fwd_entry_by_mapping_index);
2471 pool_free (lcm->fwd_entry_pool);
2474 /* update global flag */
2475 lcm->is_enabled = is_enable;
2480 static clib_error_t *
2481 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
2482 vlib_cli_command_t * cmd)
2484 unformat_input_t _line_input, *line_input = &_line_input;
2488 /* Get a line of input. */
2489 if (!unformat_user (input, unformat_line_input, line_input))
2492 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2494 if (unformat (line_input, "enable"))
2499 else if (unformat (line_input, "disable"))
2503 return clib_error_return (0, "parse error: '%U'",
2504 format_unformat_error, line_input);
2509 return clib_error_return (0, "state not set");
2511 vnet_lisp_enable_disable (is_enabled);
2516 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
2518 .short_help = "lisp [enable|disable]",
2519 .function = lisp_enable_disable_command_fn,
2523 static clib_error_t *
2524 lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
2525 unformat_input_t * input,
2526 vlib_cli_command_t * cmd)
2528 unformat_input_t _line_input, *line_input = &_line_input;
2532 /* Get a line of input. */
2533 if (!unformat_user (input, unformat_line_input, line_input))
2536 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2538 if (unformat (line_input, "enable"))
2543 else if (unformat (line_input, "disable"))
2547 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2555 vlib_cli_output (vm, "state not set!");
2559 vnet_lisp_map_register_enable_disable (is_enabled);
2564 VLIB_CLI_COMMAND (lisp_map_register_enable_disable_command) = {
2565 .path = "lisp map-register",
2566 .short_help = "lisp map-register [enable|disable]",
2567 .function = lisp_map_register_enable_disable_command_fn,
2571 static clib_error_t *
2572 lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
2573 unformat_input_t * input,
2574 vlib_cli_command_t * cmd)
2576 unformat_input_t _line_input, *line_input = &_line_input;
2580 /* Get a line of input. */
2581 if (!unformat_user (input, unformat_line_input, line_input))
2584 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2586 if (unformat (line_input, "enable"))
2591 else if (unformat (line_input, "disable"))
2595 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2603 vlib_cli_output (vm, "state not set!");
2607 vnet_lisp_rloc_probe_enable_disable (is_enabled);
2612 VLIB_CLI_COMMAND (lisp_rloc_probe_enable_disable_command) = {
2613 .path = "lisp rloc-probe",
2614 .short_help = "lisp rloc-probe [enable|disable]",
2615 .function = lisp_rloc_probe_enable_disable_command_fn,
2620 vnet_lisp_enable_disable_status (void)
2622 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2623 return lcm->is_enabled;
2627 format_lisp_status (u8 * s, va_list * args)
2629 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2630 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
2633 static clib_error_t *
2634 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
2635 vlib_cli_command_t * cmd)
2638 msg = format (msg, "feature: %U\ngpe: %U\n",
2639 format_lisp_status, format_vnet_lisp_gpe_status);
2640 vlib_cli_output (vm, "%v", msg);
2646 VLIB_CLI_COMMAND (lisp_show_status_command) = {
2647 .path = "show lisp status",
2648 .short_help = "show lisp status",
2649 .function = lisp_show_status_command_fn,
2653 static clib_error_t *
2654 lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
2655 unformat_input_t * input,
2656 vlib_cli_command_t * cmd)
2659 unformat_input_t _line_input, *line_input = &_line_input;
2660 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2661 uword *vni_table = 0;
2664 /* Get a line of input. */
2665 if (!unformat_user (input, unformat_line_input, line_input))
2668 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2670 if (unformat (line_input, "l2"))
2672 vni_table = lcm->bd_id_by_vni;
2675 else if (unformat (line_input, "l3"))
2677 vni_table = lcm->table_id_by_vni;
2681 return clib_error_return (0, "parse error: '%U'",
2682 format_unformat_error, line_input);
2687 vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
2691 vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
2694 hash_foreach_pair (p, vni_table,
2696 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
2704 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
2705 .path = "show lisp eid-table map",
2706 .short_help = "show lisp eid-table l2|l3",
2707 .function = lisp_show_eid_table_map_command_fn,
2711 static clib_error_t *
2712 lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
2713 unformat_input_t * input,
2714 vlib_cli_command_t * cmd)
2716 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2717 vnet_main_t *vnm = lgm->vnet_main;
2718 unformat_input_t _line_input, *line_input = &_line_input;
2720 clib_error_t *error = 0;
2721 u8 *locator_set_name = 0;
2722 locator_t locator, *locators = 0;
2723 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2727 memset (&locator, 0, sizeof (locator));
2728 memset (a, 0, sizeof (a[0]));
2730 /* Get a line of input. */
2731 if (!unformat_user (input, unformat_line_input, line_input))
2734 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2736 if (unformat (line_input, "add %_%v%_", &locator_set_name))
2738 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
2740 else if (unformat (line_input, "iface %U p %d w %d",
2741 unformat_vnet_sw_interface, vnm,
2742 &locator.sw_if_index, &locator.priority,
2746 vec_add1 (locators, locator);
2750 error = unformat_parse_error (line_input);
2755 a->name = locator_set_name;
2756 a->locators = locators;
2760 rv = vnet_lisp_add_del_locator_set (a, &ls_index);
2763 error = clib_error_return (0, "failed to %s locator-set!",
2764 is_add ? "add" : "delete");
2768 vec_free (locators);
2769 if (locator_set_name)
2770 vec_free (locator_set_name);
2775 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2776 .path = "lisp locator-set",
2777 .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2778 "p <priority> w <weight>]",
2779 .function = lisp_add_del_locator_set_command_fn,
2783 static clib_error_t *
2784 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
2785 unformat_input_t * input,
2786 vlib_cli_command_t * cmd)
2788 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2789 vnet_main_t *vnm = lgm->vnet_main;
2790 unformat_input_t _line_input, *line_input = &_line_input;
2792 clib_error_t *error = 0;
2793 u8 *locator_set_name = 0;
2794 u8 locator_set_name_set = 0;
2795 locator_t locator, *locators = 0;
2796 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2799 memset (&locator, 0, sizeof (locator));
2800 memset (a, 0, sizeof (a[0]));
2802 /* Get a line of input. */
2803 if (!unformat_user (input, unformat_line_input, line_input))
2806 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2808 if (unformat (line_input, "add"))
2810 else if (unformat (line_input, "del"))
2812 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
2813 locator_set_name_set = 1;
2814 else if (unformat (line_input, "iface %U p %d w %d",
2815 unformat_vnet_sw_interface, vnm,
2816 &locator.sw_if_index, &locator.priority,
2820 vec_add1 (locators, locator);
2824 error = unformat_parse_error (line_input);
2829 if (!locator_set_name_set)
2831 error = clib_error_return (0, "locator_set name not set!");
2835 a->name = locator_set_name;
2836 a->locators = locators;
2840 vnet_lisp_add_del_locator (a, 0, &ls_index);
2843 vec_free (locators);
2844 vec_free (locator_set_name);
2849 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2850 .path = "lisp locator",
2851 .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2852 "p <priority> w <weight>",
2853 .function = lisp_add_del_locator_in_set_command_fn,
2857 static clib_error_t *
2858 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
2859 unformat_input_t * input,
2860 vlib_cli_command_t * cmd)
2862 locator_set_t *lsit;
2865 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2867 vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
2868 "Priority", "Weight");
2871 pool_foreach (lsit, lcm->locator_set_pool,
2877 msg = format (msg, "%v", lsit->name);
2881 msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
2883 vec_foreach (locit, lsit->locator_indices)
2887 msg = format (msg, "%16s", " ");
2889 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2891 msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
2894 msg = format (msg, "%16U%16d%16d\n", format_ip_address,
2895 &gid_address_ip(&loc->address), loc->priority,
2899 vlib_cli_output (vm, "%v", msg);
2907 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2908 .path = "show lisp locator-set",
2909 .short_help = "Shows locator-sets",
2910 .function = lisp_cp_show_locator_sets_command_fn,
2915 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2917 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2919 lisp_msmr_t _mr, *mr = &_mr;
2921 if (vnet_lisp_enable_disable_status () == 0)
2923 clib_warning ("LISP is disabled!");
2924 return VNET_API_ERROR_LISP_DISABLED;
2930 if (get_map_resolver (&a->address))
2932 clib_warning ("map-resolver %U already exists!", format_ip_address,
2937 memset (mr, 0, sizeof (*mr));
2938 ip_address_copy (&mr->address, &a->address);
2939 vec_add1 (lcm->map_resolvers, *mr);
2941 if (vec_len (lcm->map_resolvers) == 1)
2942 lcm->do_map_resolver_election = 1;
2946 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2948 mr = vec_elt_at_index (lcm->map_resolvers, i);
2949 if (!ip_address_cmp (&mr->address, &a->address))
2951 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2952 lcm->do_map_resolver_election = 1;
2954 vec_del1 (lcm->map_resolvers, i);
2962 static clib_error_t *
2963 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
2964 unformat_input_t * input,
2965 vlib_cli_command_t * cmd)
2967 unformat_input_t _line_input, *line_input = &_line_input;
2968 u8 is_add = 1, addr_set = 0;
2969 ip_address_t ip_addr;
2970 clib_error_t *error = 0;
2972 vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
2974 /* Get a line of input. */
2975 if (!unformat_user (input, unformat_line_input, line_input))
2978 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2980 if (unformat (line_input, "add"))
2982 else if (unformat (line_input, "del"))
2984 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
2988 error = unformat_parse_error (line_input);
2995 error = clib_error_return (0, "Map-resolver address must be set!");
3000 a->address = ip_addr;
3001 rv = vnet_lisp_add_del_map_resolver (a);
3004 error = clib_error_return (0, "failed to %s map-resolver!",
3005 is_add ? "add" : "delete");
3013 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
3014 .path = "lisp map-resolver",
3015 .short_help = "lisp map-resolver add/del <ip_address>",
3016 .function = lisp_add_del_map_resolver_command_fn,
3021 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
3023 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3026 if (vnet_lisp_enable_disable_status () == 0)
3028 clib_warning ("LISP is disabled!");
3029 return VNET_API_ERROR_LISP_DISABLED;
3034 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
3037 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
3038 return VNET_API_ERROR_INVALID_ARGUMENT;
3041 lcm->mreq_itr_rlocs = p[0];
3045 lcm->mreq_itr_rlocs = ~0;
3051 static clib_error_t *
3052 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
3053 unformat_input_t * input,
3054 vlib_cli_command_t * cmd)
3056 unformat_input_t _line_input, *line_input = &_line_input;
3058 u8 *locator_set_name = 0;
3059 clib_error_t *error = 0;
3061 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
3063 /* Get a line of input. */
3064 if (!unformat_user (input, unformat_line_input, line_input))
3067 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3069 if (unformat (line_input, "del"))
3071 else if (unformat (line_input, "add %_%v%_", &locator_set_name))
3075 error = unformat_parse_error (line_input);
3081 a->locator_set_name = locator_set_name;
3082 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
3085 error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
3086 is_add ? "add" : "delete");
3089 vec_free (locator_set_name);
3097 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
3098 .path = "lisp map-request itr-rlocs",
3099 .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
3100 .function = lisp_add_del_mreq_itr_rlocs_command_fn,
3104 static clib_error_t *
3105 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
3106 unformat_input_t * input,
3107 vlib_cli_command_t * cmd)
3109 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3110 locator_set_t *loc_set;
3112 vlib_cli_output (vm, "%=20s", "itr-rlocs");
3114 if (~0 == lcm->mreq_itr_rlocs)
3119 loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
3121 vlib_cli_output (vm, "%=20s", loc_set->name);
3127 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
3128 .path = "show lisp map-request itr-rlocs",
3129 .short_help = "Shows map-request itr-rlocs",
3130 .function = lisp_show_mreq_itr_rlocs_command_fn,
3134 /* Statistics (not really errors) */
3135 #define foreach_lisp_cp_lookup_error \
3137 _(MAP_REQUESTS_SENT, "map-request sent")
3139 static char *lisp_cp_lookup_error_strings[] = {
3140 #define _(sym,string) string,
3141 foreach_lisp_cp_lookup_error
3147 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
3148 foreach_lisp_cp_lookup_error
3150 LISP_CP_LOOKUP_N_ERROR,
3151 } lisp_cp_lookup_error_t;
3155 LISP_CP_LOOKUP_NEXT_DROP,
3156 LISP_CP_LOOKUP_N_NEXT,
3157 } lisp_cp_lookup_next_t;
3161 gid_address_t dst_eid;
3162 ip_address_t map_resolver_ip;
3163 } lisp_cp_lookup_trace_t;
3166 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
3168 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3169 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3170 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
3172 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
3173 format_ip_address, &t->map_resolver_ip, format_gid_address,
3179 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
3180 ip_address_t * sloc)
3185 if (vec_len (lcm->map_resolvers) == 0)
3187 clib_warning ("No map-resolver configured");
3191 /* find the first mr ip we have a route to and the ip of the
3192 * iface that has a route to it */
3193 vec_foreach (mrit, lcm->map_resolvers)
3196 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
3198 ip_address_copy (mr_ip, a);
3200 /* also update globals */
3205 clib_warning ("Can't find map-resolver and local interface ip!");
3209 static gid_address_t *
3210 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
3216 ip_interface_address_t *ia = 0;
3217 gid_address_t gid_data, *gid = &gid_data;
3218 gid_address_t *rlocs = 0;
3219 ip_prefix_t *ippref = &gid_address_ippref (gid);
3220 ip_address_t *rloc = &ip_prefix_addr (ippref);
3222 memset (gid, 0, sizeof (gid[0]));
3223 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
3224 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
3226 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
3227 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
3229 /* Add ipv4 locators first TODO sort them */
3232 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3233 loc->sw_if_index, 1 /* unnumbered */,
3235 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
3236 ip_address_set (rloc, addr, IP4);
3237 ip_prefix_len (ippref) = 32;
3238 ip_prefix_normalize (ippref);
3239 vec_add1 (rlocs, gid[0]);
3242 /* Add ipv6 locators */
3243 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3244 loc->sw_if_index, 1 /* unnumbered */,
3246 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
3247 ip_address_set (rloc, addr, IP6);
3248 ip_prefix_len (ippref) = 128;
3249 ip_prefix_normalize (ippref);
3250 vec_add1 (rlocs, gid[0]);
3258 static vlib_buffer_t *
3259 build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
3260 ip_address_t * sloc, ip_address_t * rloc,
3261 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
3265 vlib_main_t *vm = lcm->vlib_main;
3267 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3269 clib_warning ("Can't allocate buffer for Map-Request!");
3273 b = vlib_get_buffer (vm, bi);
3275 /* leave some space for the encap headers */
3276 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3279 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
3280 1 /* rloc probe */ , nonce_res);
3282 /* push outer ip header */
3283 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3291 static vlib_buffer_t *
3292 build_encapsulated_map_request (lisp_cp_main_t * lcm,
3293 gid_address_t * seid, gid_address_t * deid,
3294 locator_set_t * loc_set, ip_address_t * mr_ip,
3295 ip_address_t * sloc, u8 is_smr_invoked,
3296 u64 * nonce_res, u32 * bi_res)
3300 gid_address_t *rlocs = 0;
3301 vlib_main_t *vm = lcm->vlib_main;
3303 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3305 clib_warning ("Can't allocate buffer for Map-Request!");
3309 b = vlib_get_buffer (vm, bi);
3311 /* leave some space for the encap headers */
3312 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3315 rlocs = build_itr_rloc_list (lcm, loc_set);
3317 if (MR_MODE_SRC_DST == lcm->map_request_mode
3318 && GID_ADDR_SRC_DST != gid_address_type (deid))
3321 memset (&sd, 0, sizeof (sd));
3322 build_src_dst (&sd, seid, deid);
3323 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
3324 0 /* rloc probe */ , nonce_res);
3329 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
3330 0 /* rloc probe */ , nonce_res);
3333 /* push ecm: udp-ip-lisp */
3334 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
3336 /* push outer ip header */
3337 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3347 reset_pending_mr_counters (pending_map_request_t * r)
3349 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3354 elect_map_resolver (lisp_cp_main_t * lcm)
3358 vec_foreach (mr, lcm->map_resolvers)
3362 ip_address_copy (&lcm->active_map_resolver, &mr->address);
3363 lcm->do_map_resolver_election = 0;
3371 free_map_register_records (mapping_t * maps)
3374 vec_foreach (map, maps) vec_free (map->locators);
3380 add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
3381 ip_address_t * probed_loc)
3384 locator_t *loc, new;
3385 ip_interface_address_t *ia = 0;
3387 ip_address_t *new_ip = &gid_address_ip (&new.address);
3390 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
3392 vec_foreach (li, ls->locator_indices)
3394 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
3399 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3400 loc->sw_if_index, 1 /* unnumbered */,
3402 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
3404 ip_address_set (new_ip, addr, IP4);
3407 /* Add ipv6 locators */
3408 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3409 loc->sw_if_index, 1 /* unnumbered */,
3411 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
3413 ip_address_set (new_ip, addr, IP6);
3417 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
3420 vec_add1 (m->locators, new);
3425 build_map_register_record_list (lisp_cp_main_t * lcm)
3427 mapping_t *recs = 0, rec, *m;
3430 pool_foreach(m, lcm->mapping_pool,
3432 /* for now build only local mappings */
3437 add_locators (lcm, &rec, m->locator_set_index, NULL);
3438 vec_add1 (recs, rec);
3446 update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
3447 lisp_key_type_t key_id, u8 * key,
3448 u16 auth_data_len, u32 msg_len)
3450 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
3451 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
3453 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3454 (unsigned char *) map_reg_hdr, msg_len, NULL,
3456 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
3461 static vlib_buffer_t *
3462 build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
3463 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
3464 mapping_t * records, lisp_key_type_t key_id, u8 * key,
3469 u32 bi, auth_data_len = 0, msg_len = 0;
3470 vlib_main_t *vm = lcm->vlib_main;
3472 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3474 clib_warning ("Can't allocate buffer for Map-Register!");
3478 b = vlib_get_buffer (vm, bi);
3480 /* leave some space for the encap headers */
3481 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3483 auth_data_len = auth_data_len_by_key_id (key_id);
3484 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
3485 auth_data_len, nonce_res,
3488 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
3491 /* push outer ip header */
3492 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3500 get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
3503 while (lcm->do_map_resolver_election
3504 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
3505 &lcm->active_map_resolver,
3508 if (0 == elect_map_resolver (lcm))
3509 /* all map resolvers are down */
3511 /* restart MR checking by marking all of them up */
3512 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
3520 send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
3521 u32 local_locator_set_index, ip_address_t * sloc,
3522 ip_address_t * rloc)
3529 u32 next_index, *to_next;
3530 gid_address_t *itr_rlocs;
3532 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
3533 itr_rlocs = build_itr_rloc_list (lcm, ls);
3535 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
3536 vec_free (itr_rlocs);
3540 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3542 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3543 ip4_lookup_node.index : ip6_lookup_node.index;
3545 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3547 /* Enqueue the packet */
3548 to_next = vlib_frame_vector_args (f);
3551 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3553 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3558 send_rloc_probes (lisp_cp_main_t * lcm)
3567 pool_foreach (e, lcm->fwd_entry_pool,
3569 if (vec_len (e->locator_pairs) == 0)
3572 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
3575 clib_warning ("internal error: cannot find local eid %U in "
3576 "map-cache!", format_gid_address, &e->leid);
3579 lm = pool_elt_at_index (lcm->mapping_pool, si);
3581 /* get the best (lowest) priority */
3582 lprio = e->locator_pairs[0].priority;
3584 /* send rloc-probe for pair(s) with the best remote locator priority */
3585 vec_foreach (lp, e->locator_pairs)
3587 if (lp->priority != lprio)
3590 /* get first remote locator */
3591 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
3601 send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
3608 u32 next_index, *to_next;
3609 ip_address_t *ms = 0;
3610 mapping_t *records, *r, *g;
3612 // TODO: support multiple map servers and do election
3613 if (0 == vec_len (lcm->map_servers))
3616 ms = &lcm->map_servers[0].address;
3618 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
3620 clib_warning ("no eligible interface address found for %U!",
3621 format_ip_address, &lcm->map_servers[0]);
3625 records = build_map_register_record_list (lcm);
3629 vec_foreach (r, records)
3632 u8 key_id = r->key_id;
3635 continue; /* no secret key -> map-register cannot be sent */
3638 // TODO: group mappings that share common key
3640 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, g,
3646 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3648 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3649 ip4_lookup_node.index : ip6_lookup_node.index;
3651 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3653 /* Enqueue the packet */
3654 to_next = vlib_frame_vector_args (f);
3657 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3659 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3661 free_map_register_records (records);
3666 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
3667 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
3669 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
3670 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
3673 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
3674 gid_address_t * seid, gid_address_t * deid,
3675 u8 is_smr_invoked, u8 is_resend)
3677 u32 next_index, bi = 0, *to_next, map_index;
3681 locator_set_t *loc_set;
3683 pending_map_request_t *pmr, *duplicate_pmr = 0;
3687 /* if there is already a pending request remember it */
3690 pool_foreach(pmr, lcm->pending_map_requests_pool,
3692 if (!gid_address_cmp (&pmr->src, seid)
3693 && !gid_address_cmp (&pmr->dst, deid))
3695 duplicate_pmr = pmr;
3701 if (!is_resend && duplicate_pmr)
3703 /* don't send the request if there is a pending map request already */
3707 /* get locator-set for seid */
3708 if (!lcm->lisp_pitr)
3710 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
3711 if (map_index == ~0)
3713 clib_warning ("No local mapping found in eid-table for %U!",
3714 format_gid_address, seid);
3718 map = pool_elt_at_index (lcm->mapping_pool, map_index);
3723 ("Mapping found for src eid %U is not marked as local!",
3724 format_gid_address, seid);
3727 ls_index = map->locator_set_index;
3731 map_index = lcm->pitr_map_index;
3732 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
3733 ls_index = map->locator_set_index;
3736 /* overwrite locator set if map-request itr-rlocs configured */
3737 if (~0 != lcm->mreq_itr_rlocs)
3739 ls_index = lcm->mreq_itr_rlocs;
3742 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
3744 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
3747 duplicate_pmr->to_be_removed = 1;
3751 /* build the encapsulated map request */
3752 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
3753 &lcm->active_map_resolver,
3754 &sloc, is_smr_invoked, &nonce, &bi);
3759 /* set fib index to default and lookup node */
3760 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3761 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3762 ip4_lookup_node.index : ip6_lookup_node.index;
3764 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3766 /* Enqueue the packet */
3767 to_next = vlib_frame_vector_args (f);
3770 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3773 /* if there is a pending request already update it */
3775 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
3777 /* remove the oldest nonce */
3778 u64 CLIB_UNUSED (tmp), *nonce_del;
3779 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
3780 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
3781 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
3784 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
3785 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3786 duplicate_pmr - lcm->pending_map_requests_pool);
3790 /* add map-request to pending requests table */
3791 pool_get (lcm->pending_map_requests_pool, pmr);
3792 memset (pmr, 0, sizeof (*pmr));
3793 gid_address_copy (&pmr->src, seid);
3794 gid_address_copy (&pmr->dst, deid);
3795 clib_fifo_add1 (pmr->nonces, nonce);
3796 pmr->is_smr_invoked = is_smr_invoked;
3797 reset_pending_mr_counters (pmr);
3798 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3799 pmr - lcm->pending_map_requests_pool);
3806 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
3808 ip4_header_t *ip4 = hdr;
3811 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3813 ip_address_set (src, &ip4->src_address, IP4);
3814 ip_address_set (dst, &ip4->dst_address, IP4);
3819 ip_address_set (src, &ip6->src_address, IP6);
3820 ip_address_set (dst, &ip6->dst_address, IP6);
3825 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3829 u32 vni = ~0, table_id = ~0;
3831 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
3832 IP4 ? FIB_PROTOCOL_IP4 :
3838 vnip = hash_get (lcm->vni_by_table_id, table_id);
3842 clib_warning ("vrf %d is not mapped to any vni!", table_id);
3848 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3854 l2input_main_t *l2im = &l2input_main;
3855 l2_input_config_t *config;
3856 l2_bridge_domain_t *bd_config;
3858 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3859 config = vec_elt_at_index (l2im->configs, sw_if_index0);
3860 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3862 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
3866 clib_warning ("bridge domain %d is not mapped to any vni!",
3873 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3874 gid_address_t * src, gid_address_t * dst)
3879 memset (src, 0, sizeof (*src));
3880 memset (dst, 0, sizeof (*dst));
3881 type = vnet_buffer (b)->lisp.overlay_afi;
3883 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3886 u8 version, preflen;
3888 gid_address_type (src) = GID_ADDR_IP_PREFIX;
3889 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
3891 ip = vlib_buffer_get_current (b);
3892 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
3894 version = gid_address_ip_version (src);
3895 preflen = ip_address_max_len (version);
3896 gid_address_ippref_len (src) = preflen;
3897 gid_address_ippref_len (dst) = preflen;
3899 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3900 gid_address_vni (dst) = vni;
3901 gid_address_vni (src) = vni;
3903 else if (LISP_AFI_MAC == type)
3905 ethernet_header_t *eh;
3907 eh = vlib_buffer_get_current (b);
3909 gid_address_type (src) = GID_ADDR_MAC;
3910 gid_address_type (dst) = GID_ADDR_MAC;
3911 mac_copy (&gid_address_mac (src), eh->src_address);
3912 mac_copy (&gid_address_mac (dst), eh->dst_address);
3915 vni = lisp_get_vni_from_buffer_eth (lcm, b);
3917 gid_address_vni (dst) = vni;
3918 gid_address_vni (src) = vni;
3923 lisp_cp_lookup_inline (vlib_main_t * vm,
3924 vlib_node_runtime_t * node,
3925 vlib_frame_t * from_frame, int overlay)
3927 u32 *from, *to_next_drop, di, si;
3928 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3929 u32 pkts_mapped = 0;
3930 uword n_left_from, n_left_to_next_drop;
3932 from = vlib_frame_vector_args (from_frame);
3933 n_left_from = from_frame->n_vectors;
3935 while (n_left_from > 0)
3937 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3938 to_next_drop, n_left_to_next_drop);
3940 while (n_left_from > 0 && n_left_to_next_drop > 0)
3944 gid_address_t src, dst;
3949 to_next_drop[0] = pi0;
3951 n_left_to_next_drop -= 1;
3953 b0 = vlib_get_buffer (vm, pi0);
3954 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
3955 vnet_buffer (b0)->lisp.overlay_afi = overlay;
3957 /* src/dst eid pair */
3958 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
3960 /* if we have remote mapping for destination already in map-chache
3961 add forwarding tunnel directly. If not send a map-request */
3962 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3966 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3967 /* send a map-request also in case of negative mapping entry
3968 with corresponding action */
3969 if (m->action == LISP_SEND_MAP_REQUEST)
3971 /* send map-request */
3972 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3973 0 /* is_resend */ );
3978 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3982 dp_add_fwd_entry_from_mt (si, di);
3988 /* send map-request */
3989 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3990 0 /* is_resend */ );
3994 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3996 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3999 memset (tr, 0, sizeof (*tr));
4000 gid_address_copy (&tr->dst_eid, &dst);
4001 ip_address_copy (&tr->map_resolver_ip,
4002 &lcm->active_map_resolver);
4004 gid_address_free (&dst);
4005 gid_address_free (&src);
4008 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
4009 n_left_to_next_drop);
4011 vlib_node_increment_counter (vm, node->node_index,
4012 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
4014 return from_frame->n_vectors;
4018 lisp_cp_lookup_ip4 (vlib_main_t * vm,
4019 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4021 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
4025 lisp_cp_lookup_ip6 (vlib_main_t * vm,
4026 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4028 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
4032 lisp_cp_lookup_l2 (vlib_main_t * vm,
4033 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4035 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
4039 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
4040 .function = lisp_cp_lookup_ip4,
4041 .name = "lisp-cp-lookup-ip4",
4042 .vector_size = sizeof (u32),
4043 .format_trace = format_lisp_cp_lookup_trace,
4044 .type = VLIB_NODE_TYPE_INTERNAL,
4046 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4047 .error_strings = lisp_cp_lookup_error_strings,
4049 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4052 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4058 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
4059 .function = lisp_cp_lookup_ip6,
4060 .name = "lisp-cp-lookup-ip6",
4061 .vector_size = sizeof (u32),
4062 .format_trace = format_lisp_cp_lookup_trace,
4063 .type = VLIB_NODE_TYPE_INTERNAL,
4065 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4066 .error_strings = lisp_cp_lookup_error_strings,
4068 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4071 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4077 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
4078 .function = lisp_cp_lookup_l2,
4079 .name = "lisp-cp-lookup-l2",
4080 .vector_size = sizeof (u32),
4081 .format_trace = format_lisp_cp_lookup_trace,
4082 .type = VLIB_NODE_TYPE_INTERNAL,
4084 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4085 .error_strings = lisp_cp_lookup_error_strings,
4087 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4090 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4095 /* lisp_cp_input statistics */
4096 #define foreach_lisp_cp_input_error \
4098 _(MAP_REPLIES_RECEIVED, "map-replies received")
4100 static char *lisp_cp_input_error_strings[] = {
4101 #define _(sym,string) string,
4102 foreach_lisp_cp_input_error
4108 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
4109 foreach_lisp_cp_input_error
4111 LISP_CP_INPUT_N_ERROR,
4112 } lisp_cp_input_error_t;
4116 LISP_CP_INPUT_NEXT_DROP,
4117 LISP_CP_INPUT_N_NEXT,
4118 } lisp_cp_input_next_t;
4122 gid_address_t dst_eid;
4123 ip4_address_t map_resolver_ip;
4124 } lisp_cp_input_trace_t;
4127 format_lisp_cp_input_trace (u8 * s, va_list * args)
4129 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
4130 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
4131 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
4132 va_arg (*args, lisp_cp_input_trace_t *);
4134 s = format (s, "LISP-CP-INPUT: TODO");
4139 remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
4143 m = pool_elt_at_index (lcm->mapping_pool, mi);
4144 lisp_add_del_adjacency (lcm, 0, &m->eid, 0 /* is_add */ );
4145 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
4146 0 /* is_static */ , 0);
4147 mapping_delete_timer (lcm, mi);
4151 mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
4152 f64 expiration_time)
4155 u64 now = clib_cpu_time_now ();
4156 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
4157 u64 exp_clock_time = now + expiration_time * cpu_cps;
4159 m = pool_elt_at_index (lcm->mapping_pool, mi);
4162 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
4166 map_records_arg_free (map_records_arg_t * a)
4169 vec_foreach (m, a->mappings)
4171 vec_free (m->locators);
4172 gid_address_free (&m->eid);
4179 process_map_reply (map_records_arg_t * a)
4182 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4183 u32 dst_map_index = 0;
4184 pending_map_request_t *pmr;
4188 if (a->is_rloc_probe)
4191 /* Check pending requests table and nonce */
4192 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
4195 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
4198 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
4200 vec_foreach (m, a->mappings)
4202 /* insert/update mappings cache */
4203 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
4204 m->authoritative, m->ttl,
4205 1, 0 /* is_static */ , &dst_map_index);
4207 /* try to program forwarding only if mapping saved or updated */
4208 if ((u32) ~ 0 != dst_map_index)
4210 lisp_add_del_adjacency (lcm, &pmr->src, &m->eid, 1);
4211 if ((u32) ~ 0 != m->ttl)
4212 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
4216 /* remove pending map request entry */
4219 clib_fifo_foreach (noncep, pmr->nonces, ({
4220 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
4224 clib_fifo_free (pmr->nonces);
4225 pool_put (lcm->pending_map_requests_pool, pmr);
4228 map_records_arg_free (a);
4233 is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
4234 lisp_key_type_t key_id, u8 * key)
4240 auth_data_len = auth_data_len_by_key_id (key_id);
4241 if ((u16) ~ 0 == auth_data_len)
4243 clib_warning ("invalid length for key_id %d!", key_id);
4247 /* save auth data */
4248 vec_validate (auth_data, auth_data_len - 1);
4249 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
4251 /* clear auth data */
4252 memset (MNOTIFY_DATA (h), 0, auth_data_len);
4254 /* get hash of the message */
4255 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
4256 (unsigned char *) h, msg_len, NULL, NULL);
4258 result = memcmp (code, auth_data, auth_data_len);
4260 vec_free (auth_data);
4266 process_map_notify (map_records_arg_t * a)
4268 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4271 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
4274 clib_warning ("No pending map-register entry with nonce %lu!",
4279 map_records_arg_free (a);
4280 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
4284 get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
4288 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
4291 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
4295 return pool_elt_at_index (lcm->mapping_pool, mi);
4299 * When map-notify is received it is necessary that all EIDs in the record
4300 * list share common key. The key is then used to verify authentication
4301 * data in map-notify message.
4304 map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
4305 u32 key_id, u8 ** key_out)
4307 u32 i, len = vec_len (maps);
4310 /* get key of the first mapping */
4311 m = get_mapping (lcm, &maps[0].eid);
4315 key_out[0] = m->key;
4317 for (i = 1; i < len; i++)
4319 m = get_mapping (lcm, &maps[i].eid);
4323 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
4325 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
4333 parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
4335 locator_t *locators = 0;
4341 /* parse record eid */
4342 for (i = 0; i < count; i++)
4344 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
4347 clib_warning ("Failed to parse mapping record!");
4348 vec_foreach (loc, locators) locator_free (loc);
4349 vec_free (locators);
4353 m.locators = locators;
4354 gid_address_copy (&m.eid, &deid);
4355 vec_add1 (a->mappings, m);
4361 static map_records_arg_t *
4362 parse_map_notify (vlib_buffer_t * b)
4365 map_notify_hdr_t *mnotif_hdr;
4366 lisp_key_type_t key_id;
4367 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4370 u16 auth_data_len = 0;
4372 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
4374 memset (a, 0, sizeof (*a));
4375 mnotif_hdr = vlib_buffer_get_current (b);
4376 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
4377 memset (&deid, 0, sizeof (deid));
4379 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
4380 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
4381 auth_data_len = auth_data_len_by_key_id (key_id);
4383 /* advance buffer by authentication data */
4384 vlib_buffer_pull (b, auth_data_len);
4386 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
4387 rc = parse_map_records (b, a, record_count);
4390 map_records_arg_free (a);
4394 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
4397 map_records_arg_free (a);
4401 /* verify authentication data */
4402 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
4403 - (u8 *) mnotif_hdr, key_id, key))
4405 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
4407 map_records_arg_free (a);
4413 static vlib_buffer_t *
4414 build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
4415 ip_address_t * dst, u64 nonce, u8 probe_bit,
4416 mapping_t * records, u16 dst_port, u32 * bi_res)
4420 vlib_main_t *vm = lcm->vlib_main;
4422 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
4424 clib_warning ("Can't allocate buffer for Map-Register!");
4428 b = vlib_get_buffer (vm, bi);
4430 /* leave some space for the encap headers */
4431 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
4433 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
4435 /* push outer ip header */
4436 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
4443 send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
4444 u8 probe_bit, u64 nonce, u16 dst_port,
4445 ip_address_t * probed_loc)
4451 u32 next_index, *to_next;
4452 mapping_t *records = 0, *m;
4454 m = pool_elt_at_index (lcm->mapping_pool, mi);
4458 vec_add1 (records, m[0]);
4459 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
4460 memset (&src, 0, sizeof (src));
4462 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
4464 clib_warning ("can't find inteface address for %U", format_ip_address,
4469 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
4473 free_map_register_records (records);
4475 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
4476 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
4477 ip4_lookup_node.index : ip6_lookup_node.index;
4479 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
4481 /* Enqueue the packet */
4482 to_next = vlib_frame_vector_args (f);
4485 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
4490 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm,
4493 u8 *ip_hdr = 0, *udp_hdr;
4496 ip_address_t *dst_loc = 0, probed_loc, src_loc;
4498 map_request_hdr_t *mreq_hdr;
4499 gid_address_t src, dst;
4502 gid_address_t *itr_rlocs = 0;
4504 mreq_hdr = vlib_buffer_get_current (b);
4506 // TODO ugly workaround to find out whether LISP is carried by ip4 or 6
4507 // and needs to be fixed
4508 udp_hdr = (u8 *) vlib_buffer_get_current (b) - sizeof (udp_header_t);
4509 ip4 = (ip4_header_t *) (udp_hdr - sizeof (ip4_header_t));
4510 ip6 = (ip6_header_t *) (udp_hdr - sizeof (ip6_header_t));
4512 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
4513 ip_hdr = (u8 *) ip4;
4516 u32 flags = clib_net_to_host_u32
4517 (ip6->ip_version_traffic_class_and_flow_label);
4518 if ((flags & 0xF0000000) == 0x60000000)
4519 ip_hdr = (u8 *) ip6;
4522 clib_warning ("internal error: cannot determine whether packet "
4528 vlib_buffer_pull (b, sizeof (*mreq_hdr));
4530 nonce = MREQ_NONCE (mreq_hdr);
4532 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
4535 ("Only SMR Map-Requests and RLOC probe supported for now!");
4540 len = lisp_msg_parse_addr (b, &src);
4544 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
4545 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
4549 /* parse eid records and send SMR-invoked map-requests */
4550 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
4552 memset (&dst, 0, sizeof (dst));
4553 len = lisp_msg_parse_eid_rec (b, &dst);
4556 clib_warning ("Can't parse map-request EID-record");
4560 if (MREQ_SMR (mreq_hdr))
4562 /* send SMR-invoked map-requests */
4563 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
4565 else if (MREQ_RLOC_PROBE (mreq_hdr))
4567 memset (&m, 0, sizeof (m));
4568 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
4570 // TODO: select best locator; for now use the first one
4571 dst_loc = &gid_address_ip (&itr_rlocs[0]);
4573 /* get src/dst IP addresses */
4574 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
4576 // TODO get source port from buffer
4577 u16 src_port = LISP_CONTROL_PORT;
4579 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
4580 src_port, &probed_loc);
4585 vec_free (itr_rlocs);
4588 static map_records_arg_t *
4589 parse_map_reply (vlib_buffer_t * b)
4596 map_reply_hdr_t *mrep_hdr;
4597 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
4598 memset (a, 0, sizeof (*a));
4599 locator_t *locators;
4601 mrep_hdr = vlib_buffer_get_current (b);
4602 a->nonce = MREP_NONCE (mrep_hdr);
4603 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
4604 vlib_buffer_pull (b, sizeof (*mrep_hdr));
4606 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
4608 memset (&m, 0, sizeof (m));
4610 h = vlib_buffer_get_current (b);
4612 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
4613 m.action = MAP_REC_ACTION (h);
4614 m.authoritative = MAP_REC_AUTH (h);
4616 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
4619 clib_warning ("Failed to parse mapping record!");
4620 map_records_arg_free (a);
4624 m.locators = locators;
4625 gid_address_copy (&m.eid, &deid);
4626 vec_add1 (a->mappings, m);
4632 queue_map_reply_for_processing (map_records_arg_t * a)
4634 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
4638 queue_map_notify_for_processing (map_records_arg_t * a)
4640 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
4644 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
4645 vlib_frame_t * from_frame)
4647 u32 n_left_from, *from, *to_next_drop;
4648 lisp_msg_type_e type;
4649 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4650 map_records_arg_t *a;
4652 from = vlib_frame_vector_args (from_frame);
4653 n_left_from = from_frame->n_vectors;
4656 while (n_left_from > 0)
4658 u32 n_left_to_next_drop;
4660 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4661 to_next_drop, n_left_to_next_drop);
4662 while (n_left_from > 0 && n_left_to_next_drop > 0)
4670 to_next_drop[0] = bi0;
4672 n_left_to_next_drop -= 1;
4674 b0 = vlib_get_buffer (vm, bi0);
4676 type = lisp_msg_type (vlib_buffer_get_current (b0));
4679 case LISP_MAP_REPLY:
4680 a = parse_map_reply (b0);
4682 queue_map_reply_for_processing (a);
4684 case LISP_MAP_REQUEST:
4685 process_map_request (vm, lcm, b0);
4687 case LISP_MAP_NOTIFY:
4688 a = parse_map_notify (b0);
4690 queue_map_notify_for_processing (a);
4693 clib_warning ("Unsupported LISP message type %d", type);
4697 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
4699 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
4705 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4706 n_left_to_next_drop);
4708 return from_frame->n_vectors;
4712 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
4713 .function = lisp_cp_input,
4714 .name = "lisp-cp-input",
4715 .vector_size = sizeof (u32),
4716 .format_trace = format_lisp_cp_input_trace,
4717 .type = VLIB_NODE_TYPE_INTERNAL,
4719 .n_errors = LISP_CP_INPUT_N_ERROR,
4720 .error_strings = lisp_cp_input_error_strings,
4722 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
4725 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
4731 lisp_cp_init (vlib_main_t * vm)
4733 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4734 clib_error_t *error = 0;
4736 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
4739 lcm->im4 = &ip4_main;
4740 lcm->im6 = &ip6_main;
4741 lcm->vlib_main = vm;
4742 lcm->vnet_main = vnet_get_main ();
4743 lcm->mreq_itr_rlocs = ~0;
4745 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
4747 gid_dictionary_init (&lcm->mapping_index_by_gid);
4748 lcm->do_map_resolver_election = 1;
4749 lcm->map_request_mode = MR_MODE_DST_ONLY;
4751 /* default vrf mapped to vni 0 */
4752 hash_set (lcm->table_id_by_vni, 0, 0);
4753 hash_set (lcm->vni_by_table_id, 0, 0);
4755 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
4756 lisp_cp_input_node.index, 1 /* is_ip4 */ );
4757 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
4758 lisp_cp_input_node.index, 0 /* is_ip4 */ );
4760 u64 now = clib_cpu_time_now ();
4761 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
4766 send_map_request_thread_fn (void *arg)
4768 map_request_args_t *a = arg;
4769 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4772 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4774 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4780 queue_map_request (gid_address_t * seid, gid_address_t * deid,
4781 u8 smr_invoked, u8 is_resend)
4783 map_request_args_t a;
4785 a.is_resend = is_resend;
4786 gid_address_copy (&a.seid, seid);
4787 gid_address_copy (&a.deid, deid);
4788 a.smr_invoked = smr_invoked;
4790 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
4791 (u8 *) & a, sizeof (a));
4796 * Take an action with a pending map request depending on expiration time
4797 * and re-try counters.
4800 update_pending_request (pending_map_request_t * r, f64 dt)
4802 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4805 if (r->time_to_expire - dt < 0)
4806 /* it's time to decide what to do with this pending request */
4808 if (r->retries_num >= NUMBER_OF_RETRIES)
4809 /* too many retries -> assume current map resolver is not available */
4811 mr = get_map_resolver (&lcm->active_map_resolver);
4814 clib_warning ("Map resolver %U not found - probably deleted "
4815 "by the user recently.", format_ip_address,
4816 &lcm->active_map_resolver);
4820 clib_warning ("map resolver %U is unreachable, ignoring",
4821 format_ip_address, &lcm->active_map_resolver);
4823 /* mark current map resolver unavailable so it won't be
4824 * selected next time */
4826 mr->last_update = vlib_time_now (lcm->vlib_main);
4829 reset_pending_mr_counters (r);
4830 elect_map_resolver (lcm);
4832 /* try to find a next eligible map resolver and re-send */
4833 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4839 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4842 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
4846 r->time_to_expire -= dt;
4850 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4853 pending_map_request_t *pmr;
4854 u32 *to_be_removed = 0, *pmr_index;
4857 pool_foreach (pmr, lcm->pending_map_requests_pool,
4859 if (pmr->to_be_removed)
4861 clib_fifo_foreach (nonce, pmr->nonces, ({
4862 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4865 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4870 vec_foreach (pmr_index, to_be_removed)
4871 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
4873 vec_free (to_be_removed);
4877 update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4879 static f64 time_left = RLOC_PROBING_INTERVAL;
4881 if (!lcm->is_enabled || !lcm->rloc_probing)
4887 time_left = RLOC_PROBING_INTERVAL;
4888 send_rloc_probes (lcm);
4893 update_map_register (lisp_cp_main_t * lcm, f64 dt)
4895 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4896 static u64 mreg_sent_counter = 0;
4898 if (!lcm->is_enabled || !lcm->map_registering)
4904 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4905 time_left = MAP_REGISTER_INTERVAL;
4908 mreg_sent_counter++;
4909 time_left = QUICK_MAP_REGISTER_INTERVAL;
4911 send_map_register (lcm, 1 /* want map notify */ );
4916 send_map_resolver_service (vlib_main_t * vm,
4917 vlib_node_runtime_t * rt, vlib_frame_t * f)
4921 pending_map_request_t *pmr;
4922 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4926 vlib_process_wait_for_event_or_clock (vm, period);
4928 /* currently no signals are expected - just wait for clock */
4929 (void) vlib_process_get_events (vm, 0);
4932 pool_foreach (pmr, lcm->pending_map_requests_pool,
4934 if (!pmr->to_be_removed)
4935 update_pending_request (pmr, period);
4939 remove_dead_pending_map_requests (lcm);
4941 update_map_register (lcm, period);
4942 update_rloc_probing (lcm, period);
4944 u64 now = clib_cpu_time_now ();
4946 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
4947 if (vec_len (expired) > 0)
4950 vec_foreach (mi, expired)
4952 remove_expired_mapping (lcm, mi[0]);
4954 _vec_len (expired) = 0;
4963 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
4964 .function = send_map_resolver_service,
4965 .type = VLIB_NODE_TYPE_PROCESS,
4966 .name = "lisp-retry-service",
4967 .process_log2_n_stack_bytes = 16,
4971 VLIB_INIT_FUNCTION (lisp_cp_init);
4974 * fd.io coding-style-patch-verification: ON
4977 * eval: (c-set-style "gnu")