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);
517 gid_address_copy (&fe->leid, &a->lcl_eid);
519 gid_address_copy (&fe->leid, &src_map->eid);
521 fe->is_src_dst = is_src_dst;
522 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
523 fe - lcm->fwd_entry_pool);
530 } fwd_entry_mt_arg_t;
533 dp_add_fwd_entry_thread_fn (void *arg)
535 fwd_entry_mt_arg_t *a = arg;
536 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
537 dp_add_fwd_entry (lcm, a->si, a->di);
542 dp_add_fwd_entry_from_mt (u32 si, u32 di)
544 fwd_entry_mt_arg_t a;
546 memset (&a, 0, sizeof (a));
550 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
551 (u8 *) & a, sizeof (a));
556 * Returns vector of adjacencies.
558 * The caller must free the vector returned by this function.
560 * @param vni virtual network identifier
561 * @return vector of adjacencies
564 vnet_lisp_adjacencies_get_by_vni (u32 vni)
566 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
568 lisp_adjacency_t *adjs = 0, adj;
571 pool_foreach(fwd, lcm->fwd_entry_pool,
573 if (gid_address_vni (&fwd->reid) != vni)
576 gid_address_copy (&adj.reid, &fwd->reid);
577 gid_address_copy (&adj.leid, &fwd->leid);
578 vec_add1 (adjs, adj);
585 static clib_error_t *
586 lisp_show_adjacencies_command_fn (vlib_main_t * vm,
587 unformat_input_t * input,
588 vlib_cli_command_t * cmd)
590 lisp_adjacency_t *adjs, *adj;
591 vlib_cli_output (vm, "%s %40s\n", "leid", "reid");
592 unformat_input_t _line_input, *line_input = &_line_input;
595 /* Get a line of input. */
596 if (!unformat_user (input, unformat_line_input, line_input))
599 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
601 if (unformat (line_input, "vni %d", &vni))
605 vlib_cli_output (vm, "parse error: '%U'",
606 format_unformat_error, line_input);
613 vlib_cli_output (vm, "error: no vni specified!");
617 adjs = vnet_lisp_adjacencies_get_by_vni (vni);
619 vec_foreach (adj, adjs)
621 vlib_cli_output (vm, "%U %40U\n", format_gid_address, &adj->leid,
622 format_gid_address, &adj->reid);
630 VLIB_CLI_COMMAND (lisp_show_adjacencies_command) = {
631 .path = "show lisp adjacencies",
632 .short_help = "show lisp adjacencies",
633 .function = lisp_show_adjacencies_command_fn,
638 get_map_server (ip_address_t * a)
640 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
643 vec_foreach (m, lcm->map_servers)
645 if (!ip_address_cmp (&m->address, a))
654 get_map_resolver (ip_address_t * a)
656 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
659 vec_foreach (m, lcm->map_resolvers)
661 if (!ip_address_cmp (&m->address, a))
670 vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
673 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
674 lisp_msmr_t _ms, *ms = &_ms;
676 if (vnet_lisp_enable_disable_status () == 0)
678 clib_warning ("LISP is disabled!");
679 return VNET_API_ERROR_LISP_DISABLED;
684 if (get_map_server (addr))
686 clib_warning ("map-server %U already exists!", format_ip_address,
691 memset (ms, 0, sizeof (*ms));
692 ip_address_copy (&ms->address, addr);
693 vec_add1 (lcm->map_servers, ms[0]);
697 for (i = 0; i < vec_len (lcm->map_servers); i++)
699 ms = vec_elt_at_index (lcm->map_servers, i);
700 if (!ip_address_cmp (&ms->address, addr))
702 vec_del1 (lcm->map_servers, i);
711 static clib_error_t *
712 lisp_add_del_map_server_command_fn (vlib_main_t * vm,
713 unformat_input_t * input,
714 vlib_cli_command_t * cmd)
717 u8 is_add = 1, ip_set = 0;
719 unformat_input_t _line_input, *line_input = &_line_input;
721 /* Get a line of input. */
722 if (!unformat_user (input, unformat_line_input, line_input))
725 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
727 if (unformat (line_input, "add"))
729 else if (unformat (line_input, "del"))
731 else if (unformat (line_input, "%U", unformat_ip_address, &ip))
735 vlib_cli_output (vm, "parse error: '%U'",
736 format_unformat_error, line_input);
743 vlib_cli_output (vm, "map-server ip address not set!");
747 rv = vnet_lisp_add_del_map_server (&ip, is_add);
749 vlib_cli_output (vm, "failed to %s map-server!",
750 is_add ? "add" : "delete");
756 VLIB_CLI_COMMAND (lisp_add_del_map_server_command) = {
757 .path = "lisp map-server",
758 .short_help = "lisp map-server add|del <ip>",
759 .function = lisp_add_del_map_server_command_fn,
764 * Add/remove mapping to/from map-cache. Overwriting not allowed.
767 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
768 u32 * map_index_result)
770 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
771 u32 mi, *map_indexp, map_index, i;
772 mapping_t *m, *old_map;
775 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
776 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
779 /* TODO check if overwriting and take appropriate actions */
780 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
782 clib_warning ("eid %U found in the eid-table", format_gid_address,
784 return VNET_API_ERROR_VALUE_EXIST;
787 pool_get (lcm->mapping_pool, m);
788 gid_address_copy (&m->eid, &a->eid);
789 m->locator_set_index = a->locator_set_index;
791 m->action = a->action;
793 m->is_static = a->is_static;
794 m->key = vec_dup (a->key);
795 m->key_id = a->key_id;
797 map_index = m - lcm->mapping_pool;
798 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
801 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
803 clib_warning ("Locator set with index %d doesn't exist",
804 a->locator_set_index);
805 return VNET_API_ERROR_INVALID_VALUE;
808 /* add eid to list of eids supported by locator-set */
809 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
810 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
811 a->locator_set_index);
812 vec_add1 (eid_indexes[0], map_index);
817 vec_add1 (lcm->local_mappings_indexes, map_index);
819 map_index_result[0] = map_index;
823 if (mi == GID_LOOKUP_MISS)
825 clib_warning ("eid %U not found in the eid-table",
826 format_gid_address, &a->eid);
827 return VNET_API_ERROR_INVALID_VALUE;
830 /* clear locator-set to eids binding */
831 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
832 a->locator_set_index);
833 for (i = 0; i < vec_len (eid_indexes[0]); i++)
835 map_indexp = vec_elt_at_index (eid_indexes[0], i);
836 if (map_indexp[0] == mi)
839 vec_del1 (eid_indexes[0], i);
841 /* remove local mark if needed */
842 m = pool_elt_at_index (lcm->mapping_pool, mi);
846 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
848 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
849 if (lm_indexp[0] == mi)
852 vec_del1 (lcm->local_mappings_indexes, k);
855 /* remove mapping from dictionary */
856 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
857 gid_address_free (&m->eid);
858 pool_put_index (lcm->mapping_pool, mi);
865 * Add/update/delete mapping to/in/from map-cache.
868 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
869 u32 * map_index_result)
875 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
877 if (vnet_lisp_enable_disable_status () == 0)
879 clib_warning ("LISP is disabled!");
880 return VNET_API_ERROR_LISP_DISABLED;
883 vni = gid_address_vni (&a->eid);
884 type = gid_address_type (&a->eid);
885 if (GID_ADDR_IP_PREFIX == type)
886 dp_table = hash_get (lcm->table_id_by_vni, vni);
887 else if (GID_ADDR_MAC == type)
888 dp_table = hash_get (lcm->bd_id_by_vni, vni);
892 clib_warning ("vni %d not associated to a %s!", vni,
893 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
894 return VNET_API_ERROR_INVALID_VALUE;
897 /* store/remove mapping from map-cache */
898 return vnet_lisp_map_cache_add_del (a, map_index_result);
901 static clib_error_t *
902 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
903 vlib_cli_command_t * cmd)
905 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
906 unformat_input_t _line_input, *line_input = &_line_input;
909 gid_address_t *eids = 0;
910 clib_error_t *error = 0;
911 u8 *locator_set_name = 0;
912 u32 locator_set_index = 0, map_index = 0;
914 vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
920 memset (&eid, 0, sizeof (eid));
921 memset (a, 0, sizeof (*a));
923 /* Get a line of input. */
924 if (!unformat_user (input, unformat_line_input, line_input))
927 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
929 if (unformat (line_input, "add"))
931 else if (unformat (line_input, "del"))
933 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
935 else if (unformat (line_input, "vni %d", &vni))
936 gid_address_vni (&eid) = vni;
937 else if (unformat (line_input, "secret-key %_%v%_", &key))
939 else if (unformat (line_input, "key-id %U", unformat_hmac_key_id,
942 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
944 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
947 error = clib_error_return (0, "locator-set %s doesn't exist",
951 locator_set_index = p[0];
955 error = unformat_parse_error (line_input);
959 /* XXX treat batch configuration */
961 if (GID_ADDR_SRC_DST == gid_address_type (&eid))
964 clib_error_return (0, "src/dst is not supported for local EIDs!");
968 if (key && (0 == key_id))
970 vlib_cli_output (vm, "invalid key_id!");
974 gid_address_copy (&a->eid, &eid);
976 a->locator_set_index = locator_set_index;
981 rv = vnet_lisp_add_del_local_mapping (a, &map_index);
984 error = clib_error_return (0, "failed to %s local mapping!",
985 is_add ? "add" : "delete");
989 if (locator_set_name)
990 vec_free (locator_set_name);
991 gid_address_free (&a->eid);
997 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
998 .path = "lisp eid-table",
999 .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
1000 "locator-set <locator-set> [key <secret-key> key-id sha1|sha256 ]",
1001 .function = lisp_add_del_local_eid_command_fn,
1006 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
1008 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1009 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
1011 if (vnet_lisp_enable_disable_status () == 0)
1013 clib_warning ("LISP is disabled!");
1017 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
1018 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
1020 if (!is_l2 && (vni == 0 || dp_id == 0))
1022 clib_warning ("can't add/del default vni-vrf mapping!");
1026 dp_idp = hash_get (dp_table_by_vni[0], vni);
1027 vnip = hash_get (vni_by_dp_table[0], dp_id);
1033 clib_warning ("vni %d or vrf %d already used in vrf/vni "
1034 "mapping!", vni, dp_id);
1037 hash_set (dp_table_by_vni[0], vni, dp_id);
1038 hash_set (vni_by_dp_table[0], dp_id, vni);
1040 /* create dp iface */
1041 dp_add_del_iface (lcm, vni, is_l2, 1);
1045 if (!dp_idp || !vnip)
1047 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
1048 "mapping!", vni, dp_id);
1051 hash_unset (dp_table_by_vni[0], vni);
1052 hash_unset (vni_by_dp_table[0], dp_id);
1054 /* remove dp iface */
1055 dp_add_del_iface (lcm, vni, is_l2, 0);
1061 static clib_error_t *
1062 lisp_eid_table_map_command_fn (vlib_main_t * vm,
1063 unformat_input_t * input,
1064 vlib_cli_command_t * cmd)
1066 u8 is_add = 1, is_l2 = 0;
1067 u32 vni = 0, dp_id = 0;
1068 unformat_input_t _line_input, *line_input = &_line_input;
1070 /* Get a line of input. */
1071 if (!unformat_user (input, unformat_line_input, line_input))
1074 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1076 if (unformat (line_input, "del"))
1078 else if (unformat (line_input, "vni %d", &vni))
1080 else if (unformat (line_input, "vrf %d", &dp_id))
1082 else if (unformat (line_input, "bd %d", &dp_id))
1086 return unformat_parse_error (line_input);
1089 vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
1094 VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
1095 .path = "lisp eid-table map",
1096 .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
1097 .function = lisp_eid_table_map_command_fn,
1101 /* return 0 if the two locator sets are identical 1 otherwise */
1103 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
1104 locator_t * new_locators)
1107 locator_t *old_loc, *new_loc;
1109 if (vec_len (old_ls_indexes) != vec_len (new_locators))
1112 for (i = 0; i < vec_len (new_locators); i++)
1114 old_li = vec_elt (old_ls_indexes, i);
1115 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
1117 new_loc = vec_elt_at_index (new_locators, i);
1119 if (locator_cmp (old_loc, new_loc))
1129 gid_address_t *eids_to_be_deleted;
1130 } remove_mapping_args_t;
1133 * Callback invoked when a sub-prefix is found
1136 remove_mapping_if_needed (u32 mi, void *arg)
1139 remove_mapping_args_t *a = arg;
1140 lisp_cp_main_t *lcm = a->lcm;
1144 m = pool_elt_at_index (lcm->mapping_pool, mi);
1148 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1152 if (0 != vec_len (ls->locator_indices))
1157 if (0 == vec_len (ls->locator_indices))
1162 vec_add1 (a->eids_to_be_deleted, m->eid);
1166 * This function searches map cache and looks for IP prefixes that are subset
1167 * of the provided one. If such prefix is found depending on 'is_negative'
1170 * 1) if is_negative is true and found prefix points to positive mapping,
1171 * then the mapping is removed
1172 * 2) if is_negative is false and found prefix points to negative mapping,
1173 * then the mapping is removed
1176 remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1180 remove_mapping_args_t a;
1181 memset (&a, 0, sizeof (a));
1183 /* do this only in src/dst mode ... */
1184 if (MR_MODE_SRC_DST != lcm->map_request_mode)
1187 /* ... and only for IP prefix */
1188 if (GID_ADDR_SRC_DST != gid_address_type (eid)
1189 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1192 a.is_negative = is_negative;
1195 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1196 remove_mapping_if_needed, &a);
1198 vec_foreach (e, a.eids_to_be_deleted)
1200 lisp_add_del_adjacency (lcm, 0, e, 0 /* is_add */ );
1201 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
1204 vec_free (a.eids_to_be_deleted);
1208 mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1210 timing_wheel_delete (&lcm->wheel, mi);
1214 * Adds/removes/updates mapping. Does not program forwarding.
1216 * @param eid end-host identifier
1217 * @param rlocs vector of remote locators
1218 * @param action action for negative map-reply
1219 * @param is_add add mapping if non-zero, delete otherwise
1220 * @param res_map_index the map-index that was created/updated/removed. It is
1221 * set to ~0 if no action is taken.
1222 * @param is_static used for distinguishing between statically learned
1223 remote mappings and mappings obtained from MR
1224 * @return return code
1227 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
1228 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
1229 u32 * res_map_index)
1231 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1232 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1233 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1234 u32 mi, ls_index = 0, dst_map_index;
1237 if (vnet_lisp_enable_disable_status () == 0)
1239 clib_warning ("LISP is disabled!");
1240 return VNET_API_ERROR_LISP_DISABLED;
1244 res_map_index[0] = ~0;
1246 memset (m_args, 0, sizeof (m_args[0]));
1247 memset (ls_args, 0, sizeof (ls_args[0]));
1249 ls_args->locators = rlocs;
1251 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
1252 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1256 /* overwrite: if mapping already exists, decide if locators should be
1257 * updated and be done */
1258 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
1260 if (!is_static && (old_map->is_static || old_map->local))
1262 /* do not overwrite local or static remote mappings */
1263 clib_warning ("mapping %U rejected due to collision with local "
1264 "or static remote mapping!", format_gid_address,
1269 locator_set_t *old_ls;
1271 /* update mapping attributes */
1272 old_map->action = action;
1273 old_map->authoritative = authoritative;
1276 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1277 old_map->locator_set_index);
1278 if (compare_locators (lcm, old_ls->locator_indices,
1281 /* set locator-set index to overwrite */
1282 ls_args->is_add = 1;
1283 ls_args->index = old_map->locator_set_index;
1284 vnet_lisp_add_del_locator_set (ls_args, 0);
1286 res_map_index[0] = mi;
1292 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1294 ls_args->is_add = 1;
1295 ls_args->index = ~0;
1297 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1300 gid_address_copy (&m_args->eid, eid);
1302 m_args->action = action;
1303 m_args->locator_set_index = ls_index;
1304 m_args->is_static = is_static;
1306 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
1309 res_map_index[0] = dst_map_index;
1314 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
1316 clib_warning ("cannot delete mapping for eid %U",
1317 format_gid_address, eid);
1322 gid_address_copy (&m_args->eid, eid);
1323 m_args->locator_set_index = old_map->locator_set_index;
1325 /* delete mapping associated from map-cache */
1326 vnet_lisp_map_cache_add_del (m_args, 0);
1328 ls_args->is_add = 0;
1329 ls_args->index = old_map->locator_set_index;
1330 /* delete locator set */
1331 vnet_lisp_add_del_locator_set (ls_args, 0);
1333 /* delete timer associated to the mapping if any */
1334 if (old_map->timer_set)
1335 mapping_delete_timer (lcm, mi);
1337 /* return old mapping index */
1339 res_map_index[0] = mi;
1347 vnet_lisp_clear_all_remote_adjacencies (void)
1350 u32 mi, *map_indices = 0, *map_indexp;
1351 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1352 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1353 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1356 pool_foreach_index (mi, lcm->mapping_pool,
1358 vec_add1 (map_indices, mi);
1362 vec_foreach (map_indexp, map_indices)
1364 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1367 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
1369 dm_args->is_add = 0;
1370 gid_address_copy (&dm_args->eid, &map->eid);
1371 dm_args->locator_set_index = map->locator_set_index;
1373 /* delete mapping associated to fwd entry */
1374 vnet_lisp_map_cache_add_del (dm_args, 0);
1378 ls->index = map->locator_set_index;
1379 /* delete locator set */
1380 rv = vnet_lisp_add_del_locator_set (ls, 0);
1388 vec_free (map_indices);
1393 * Adds adjacency or removes forwarding entry associated to remote mapping.
1394 * Note that adjacencies are not stored, they only result in forwarding entries
1398 lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
1399 gid_address_t * remote_eid, u8 is_add)
1401 u32 local_mi, remote_mi = ~0;
1403 if (vnet_lisp_enable_disable_status () == 0)
1405 clib_warning ("LISP is disabled!");
1406 return VNET_API_ERROR_LISP_DISABLED;
1409 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1410 remote_eid, local_eid);
1411 if (GID_LOOKUP_MISS == remote_mi)
1413 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1414 format_gid_address, remote_eid);
1421 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1424 /* check if source eid has an associated mapping. If pitr mode is on,
1425 * just use the pitr's mapping */
1426 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
1427 gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
1430 if (GID_LOOKUP_MISS == local_mi)
1432 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1433 format_gid_address, local_eid);
1438 /* update forwarding */
1439 dp_add_fwd_entry (lcm, local_mi, remote_mi);
1442 dp_del_fwd_entry (lcm, 0, remote_mi);
1448 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1450 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1451 return lisp_add_del_adjacency (lcm, &a->leid, &a->reid, a->is_add);
1455 * Handler for add/del remote mapping CLI.
1457 * @param vm vlib context
1458 * @param input input from user
1460 * @return pointer to clib error structure
1462 static clib_error_t *
1463 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
1464 unformat_input_t * input,
1465 vlib_cli_command_t * cmd)
1467 clib_error_t *error = 0;
1468 unformat_input_t _line_input, *line_input = &_line_input;
1469 u8 is_add = 1, del_all = 0;
1470 locator_t rloc, *rlocs = 0, *curr_rloc = 0;
1473 u32 vni, action = ~0, p, w;
1476 /* Get a line of input. */
1477 if (!unformat_user (input, unformat_line_input, line_input))
1480 memset (&eid, 0, sizeof (eid));
1481 memset (&rloc, 0, sizeof (rloc));
1483 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1485 if (unformat (line_input, "del-all"))
1487 else if (unformat (line_input, "del"))
1489 else if (unformat (line_input, "add"))
1491 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1493 else if (unformat (line_input, "vni %u", &vni))
1495 gid_address_vni (&eid) = vni;
1497 else if (unformat (line_input, "p %d w %d", &p, &w))
1502 ("No RLOC configured for setting priority/weight!");
1505 curr_rloc->priority = p;
1506 curr_rloc->weight = w;
1508 else if (unformat (line_input, "rloc %U", unformat_ip_address,
1509 &gid_address_ip (&rloc.address)))
1511 /* since rloc is stored in ip prefix we need to set prefix length */
1512 ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
1514 u8 version = gid_address_ip_version (&rloc.address);
1515 ip_prefix_len (pref) = ip_address_max_len (version);
1517 vec_add1 (rlocs, rloc);
1518 curr_rloc = &rlocs[vec_len (rlocs) - 1];
1520 else if (unformat (line_input, "action %U",
1521 unformat_negative_mapping_action, &action))
1525 clib_warning ("parse error");
1532 clib_warning ("missing eid!");
1538 if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1540 clib_warning ("no action set for negative map-reply!");
1546 vnet_lisp_clear_all_remote_adjacencies ();
1550 /* TODO build src/dst with seid */
1552 /* if it's a delete, clean forwarding */
1555 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1556 rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1563 /* add as static remote mapping, i.e., not authoritative and infinite
1565 rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
1566 1 /* is_static */ , 0);
1569 clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
1573 unformat_free (line_input);
1577 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) =
1579 .path = "lisp remote-mapping",.short_help =
1580 "lisp remote-mapping add|del [del-all] vni <vni> "
1581 "eid <est-eid> [action <no-action|natively-forward|"
1582 "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
1583 "[rloc <dst-locator> ... ]",.function =
1584 lisp_add_del_remote_mapping_command_fn,};
1587 * Handler for add/del adjacency CLI.
1589 static clib_error_t *
1590 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
1591 vlib_cli_command_t * cmd)
1593 clib_error_t *error = 0;
1594 unformat_input_t _line_input, *line_input = &_line_input;
1595 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1597 ip_prefix_t *reid_ippref, *leid_ippref;
1598 gid_address_t leid, reid;
1599 u8 *dmac = gid_address_mac (&reid);
1600 u8 *smac = gid_address_mac (&leid);
1601 u8 reid_set = 0, leid_set = 0;
1605 /* Get a line of input. */
1606 if (!unformat_user (input, unformat_line_input, line_input))
1609 memset (&reid, 0, sizeof (reid));
1610 memset (&leid, 0, sizeof (leid));
1612 leid_ippref = &gid_address_ippref (&leid);
1613 reid_ippref = &gid_address_ippref (&reid);
1615 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1617 if (unformat (line_input, "del"))
1619 else if (unformat (line_input, "add"))
1621 else if (unformat (line_input, "reid %U",
1622 unformat_ip_prefix, reid_ippref))
1624 gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
1627 else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
1629 gid_address_type (&reid) = GID_ADDR_MAC;
1632 else if (unformat (line_input, "vni %u", &vni))
1634 gid_address_vni (&leid) = vni;
1635 gid_address_vni (&reid) = vni;
1637 else if (unformat (line_input, "leid %U",
1638 unformat_ip_prefix, leid_ippref))
1640 gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
1643 else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
1645 gid_address_type (&leid) = GID_ADDR_MAC;
1650 clib_warning ("parse error");
1655 if (!reid_set || !leid_set)
1657 clib_warning ("missing remote or local eid!");
1661 if ((gid_address_type (&leid) != gid_address_type (&reid))
1662 || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
1663 && ip_prefix_version (reid_ippref)
1664 != ip_prefix_version (leid_ippref)))
1666 clib_warning ("remote and local EIDs are of different types!");
1670 memset (a, 0, sizeof (a[0]));
1671 gid_address_copy (&a->leid, &leid);
1672 gid_address_copy (&a->reid, &reid);
1675 rv = vnet_lisp_add_del_adjacency (a);
1678 clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
1681 unformat_free (line_input);
1686 VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1687 .path = "lisp adjacency",
1688 .short_help = "lisp adjacency add|del vni <vni> reid <remote-eid> "
1690 .function = lisp_add_del_adjacency_command_fn,
1695 vnet_lisp_set_map_request_mode (u8 mode)
1697 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1699 if (vnet_lisp_enable_disable_status () == 0)
1701 clib_warning ("LISP is disabled!");
1702 return VNET_API_ERROR_LISP_DISABLED;
1705 if (mode >= _MR_MODE_MAX)
1707 clib_warning ("Invalid LISP map request mode %d!", mode);
1708 return VNET_API_ERROR_INVALID_ARGUMENT;
1711 lcm->map_request_mode = mode;
1715 static clib_error_t *
1716 lisp_map_request_mode_command_fn (vlib_main_t * vm,
1717 unformat_input_t * input,
1718 vlib_cli_command_t * cmd)
1720 unformat_input_t _i, *i = &_i;
1721 map_request_mode_t mr_mode = _MR_MODE_MAX;
1723 /* Get a line of input. */
1724 if (!unformat_user (input, unformat_line_input, i))
1727 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1729 if (unformat (i, "dst-only"))
1730 mr_mode = MR_MODE_DST_ONLY;
1731 else if (unformat (i, "src-dst"))
1732 mr_mode = MR_MODE_SRC_DST;
1735 clib_warning ("parse error '%U'", format_unformat_error, i);
1740 if (_MR_MODE_MAX == mr_mode)
1742 clib_warning ("No LISP map request mode entered!");
1746 vnet_lisp_set_map_request_mode (mr_mode);
1752 VLIB_CLI_COMMAND (lisp_map_request_mode_command) = {
1753 .path = "lisp map-request mode",
1754 .short_help = "lisp map-request mode dst-only|src-dst",
1755 .function = lisp_map_request_mode_command_fn,
1760 format_lisp_map_request_mode (u8 * s, va_list * args)
1762 u32 mode = va_arg (*args, u32);
1767 return format (0, "dst-only");
1769 return format (0, "src-dst");
1774 static clib_error_t *
1775 lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
1776 unformat_input_t * input,
1777 vlib_cli_command_t * cmd)
1779 vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
1780 vnet_lisp_get_map_request_mode ());
1785 VLIB_CLI_COMMAND (lisp_show_map_request_mode_command) = {
1786 .path = "show lisp map-request mode",
1787 .short_help = "show lisp map-request mode",
1788 .function = lisp_show_map_request_mode_command_fn,
1792 static clib_error_t *
1793 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
1794 unformat_input_t * input,
1795 vlib_cli_command_t * cmd)
1798 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1800 vec_foreach (mr, lcm->map_resolvers)
1802 vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
1808 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1809 .path = "show lisp map-resolvers",
1810 .short_help = "show lisp map-resolvers",
1811 .function = lisp_show_map_resolvers_command_fn,
1816 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1818 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1819 u32 locator_set_index = ~0;
1823 if (vnet_lisp_enable_disable_status () == 0)
1825 clib_warning ("LISP is disabled!");
1826 return VNET_API_ERROR_LISP_DISABLED;
1829 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1832 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1835 locator_set_index = p[0];
1839 pool_get (lcm->mapping_pool, m);
1840 m->locator_set_index = locator_set_index;
1842 lcm->pitr_map_index = m - lcm->mapping_pool;
1844 /* enable pitr mode */
1849 /* remove pitr mapping */
1850 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1852 /* disable pitr mode */
1858 static clib_error_t *
1859 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
1860 unformat_input_t * input,
1861 vlib_cli_command_t * cmd)
1863 u8 locator_name_set = 0;
1864 u8 *locator_set_name = 0;
1866 unformat_input_t _line_input, *line_input = &_line_input;
1867 clib_error_t *error = 0;
1870 /* Get a line of input. */
1871 if (!unformat_user (input, unformat_line_input, line_input))
1874 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1876 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
1877 locator_name_set = 1;
1878 else if (unformat (line_input, "disable"))
1881 return clib_error_return (0, "parse error");
1884 if (!locator_name_set)
1886 clib_warning ("No locator set specified!");
1889 rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1892 error = clib_error_return (0, "failed to %s pitr!",
1893 is_add ? "add" : "delete");
1897 if (locator_set_name)
1898 vec_free (locator_set_name);
1903 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1904 .path = "lisp pitr",
1905 .short_help = "lisp pitr [disable] ls <locator-set-name>",
1906 .function = lisp_pitr_set_locator_set_command_fn,
1910 static clib_error_t *
1911 lisp_show_pitr_command_fn (vlib_main_t * vm,
1912 unformat_input_t * input, vlib_cli_command_t * cmd)
1914 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1919 vlib_cli_output (vm, "%=20s%=16s",
1920 "pitr", lcm->lisp_pitr ? "locator-set" : "");
1922 if (!lcm->lisp_pitr)
1924 vlib_cli_output (vm, "%=20s", "disable");
1928 if (~0 == lcm->pitr_map_index)
1930 tmp_str = format (0, "N/A");
1934 m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1935 if (~0 != m->locator_set_index)
1938 pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1939 tmp_str = format (0, "%s", ls->name);
1943 tmp_str = format (0, "N/A");
1946 vec_add1 (tmp_str, 0);
1948 vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1956 VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1957 .path = "show lisp pitr",
1958 .short_help = "Show pitr",
1959 .function = lisp_show_pitr_command_fn,
1964 format_eid_entry (u8 * s, va_list * args)
1966 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
1967 lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
1968 mapping_t *mapit = va_arg (*args, mapping_t *);
1969 locator_set_t *ls = va_arg (*args, locator_set_t *);
1970 gid_address_t *gid = &mapit->eid;
1971 u32 ttl = mapit->ttl;
1972 u8 aut = mapit->authoritative;
1977 u8 *type = ls->local ? format (0, "local(%s)", ls->name)
1978 : format (0, "remote");
1980 if (vec_len (ls->locator_indices) == 0)
1982 s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
1987 vec_foreach (loc_index, ls->locator_indices)
1989 locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1991 loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1994 loc = format (0, "%U", format_ip_address,
1995 &gid_address_ip (&l->address));
1999 s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
2000 gid, type, loc, ttl, aut);
2004 s = format (s, "%55s%v\n", "", loc);
2010 static clib_error_t *
2011 lisp_show_eid_table_command_fn (vlib_main_t * vm,
2012 unformat_input_t * input,
2013 vlib_cli_command_t * cmd)
2015 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2017 unformat_input_t _line_input, *line_input = &_line_input;
2023 memset (&eid, 0, sizeof (eid));
2025 /* Get a line of input. */
2026 if (!unformat_user (input, unformat_line_input, line_input))
2029 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2031 if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
2033 else if (unformat (line_input, "local"))
2035 else if (unformat (line_input, "remote"))
2038 return clib_error_return (0, "parse error: '%U'",
2039 format_unformat_error, line_input);
2042 vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
2043 "EID", "type", "locators", "ttl", "autoritative");
2048 pool_foreach (mapit, lcm->mapping_pool,
2050 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
2051 mapit->locator_set_index);
2052 if (filter && !((1 == filter && ls->local) ||
2053 (2 == filter && !ls->local)))
2057 vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
2064 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
2065 if ((u32) ~ 0 == mi)
2068 mapit = pool_elt_at_index (lcm->mapping_pool, mi);
2069 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2070 mapit->locator_set_index);
2072 if (filter && !((1 == filter && ls->local) ||
2073 (2 == filter && !ls->local)))
2078 vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
2086 VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
2087 .path = "show lisp eid-table",
2088 .short_help = "Shows EID table",
2089 .function = lisp_show_eid_table_command_fn,
2093 /* cleans locator to locator-set data and removes locators not part of
2094 * any locator-set */
2096 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
2098 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
2099 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
2100 for (i = 0; i < vec_len (ls->locator_indices); i++)
2102 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
2103 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2105 for (j = 0; j < vec_len (ls_indexes[0]); j++)
2107 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
2108 if (ls_indexp[0] == lsi)
2112 /* delete index for removed locator-set */
2113 vec_del1 (ls_indexes[0], j);
2115 /* delete locator if it's part of no locator-set */
2116 if (vec_len (ls_indexes[0]) == 0)
2118 pool_put_index (lcm->locator_pool, loc_indexp[0]);
2119 vec_add1 (to_be_deleted, i);
2125 for (i = 0; i < vec_len (to_be_deleted); i++)
2127 loc_indexp = vec_elt_at_index (to_be_deleted, i);
2128 vec_del1 (ls->locator_indices, loc_indexp[0]);
2130 vec_free (to_be_deleted);
2134 static inline uword *
2135 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
2137 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2142 /* find locator-set */
2145 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
2156 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
2162 ASSERT (ls != NULL);
2163 ASSERT (loc != NULL);
2165 vec_foreach (locit, ls->locator_indices)
2167 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2168 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
2169 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
2171 clib_warning ("Duplicate locator");
2172 return VNET_API_ERROR_VALUE_EXIST;
2180 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
2181 u32 ls_index, u32 loc_id)
2183 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2184 u32 **ls_indexes = NULL;
2186 ASSERT (ls != NULL);
2187 ASSERT (locit != NULL);
2189 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
2190 pool_put_index (lcm->locator_pool, locit[0]);
2191 vec_del1 (ls->locator_indices, loc_id);
2192 vec_del1 (ls_indexes[0], ls_index);
2196 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
2197 locator_set_t * ls, u32 * ls_result)
2199 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2200 locator_t *loc = NULL, *itloc = NULL;
2201 uword _p = (u32) ~ 0, *p = &_p;
2202 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
2208 if (vnet_lisp_enable_disable_status () == 0)
2210 clib_warning ("LISP is disabled!");
2211 return VNET_API_ERROR_LISP_DISABLED;
2214 p = get_locator_set_index (a, p);
2217 clib_warning ("locator-set %v doesn't exist", a->name);
2218 return VNET_API_ERROR_INVALID_ARGUMENT;
2223 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2226 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2228 return VNET_API_ERROR_INVALID_ARGUMENT;
2235 ls_result[0] = p[0];
2237 /* allocate locators */
2238 vec_foreach (itloc, a->locators)
2240 ret = is_locator_in_locator_set (lcm, ls, itloc);
2246 pool_get (lcm->locator_pool, loc);
2248 loc_index = loc - lcm->locator_pool;
2250 vec_add1 (ls->locator_indices, loc_index);
2252 vec_validate (lcm->locator_to_locator_sets, loc_index);
2253 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2255 vec_add1 (ls_indexes[0], p[0]);
2262 itloc = a->locators;
2264 vec_foreach (locit, ls->locator_indices)
2266 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2268 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
2270 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2272 if (0 == loc->local &&
2273 !gid_address_cmp (&loc->address, &itloc->address))
2275 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2286 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
2289 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2291 uword _p = (u32) ~ 0, *p = &_p;
2296 if (vnet_lisp_enable_disable_status () == 0)
2298 clib_warning ("LISP is disabled!");
2299 return VNET_API_ERROR_LISP_DISABLED;
2304 p = get_locator_set_index (a, p);
2307 if (p && p[0] != (u32) ~ 0)
2309 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2312 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2317 /* clean locator to locator-set vectors and remove locators if
2318 * they're not part of another locator-set */
2319 clean_locator_to_locator_set (lcm, p[0]);
2321 /* remove locator indices from locator set */
2322 vec_free (ls->locator_indices);
2327 ls_result[0] = p[0];
2329 /* new locator-set */
2332 pool_get (lcm->locator_set_pool, ls);
2333 memset (ls, 0, sizeof (*ls));
2334 ls_index = ls - lcm->locator_set_pool;
2338 ls->name = vec_dup (a->name);
2340 if (!lcm->locator_set_index_by_name)
2341 lcm->locator_set_index_by_name = hash_create_vec (
2349 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
2352 /* mark as local locator-set */
2353 vec_add1 (lcm->local_locator_set_indexes, ls_index);
2355 ls->local = a->local;
2357 ls_result[0] = ls_index;
2360 ret = vnet_lisp_add_del_locator (a, ls, NULL);
2368 p = get_locator_set_index (a, p);
2371 clib_warning ("locator-set %v doesn't exists", a->name);
2375 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2378 clib_warning ("locator-set with index %d doesn't exists", p[0]);
2382 if (lcm->mreq_itr_rlocs == p[0])
2384 clib_warning ("Can't delete the locator-set used to constrain "
2385 "the itr-rlocs in map-requests!");
2389 if (vec_len (lcm->locator_set_to_eids) != 0)
2391 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
2392 if (vec_len (eid_indexes[0]) != 0)
2395 ("Can't delete a locator that supports a mapping!");
2400 /* clean locator to locator-sets data */
2401 clean_locator_to_locator_set (lcm, p[0]);
2407 vec_foreach_index (it, lcm->local_locator_set_indexes)
2409 lsi = vec_elt (lcm->local_locator_set_indexes, it);
2412 vec_del1 (lcm->local_locator_set_indexes, it);
2416 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2418 vec_free (ls->name);
2419 vec_free (ls->locator_indices);
2420 pool_put (lcm->locator_set_pool, ls);
2426 vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
2428 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2430 lcm->rloc_probing = is_enable;
2435 vnet_lisp_map_register_enable_disable (u8 is_enable)
2437 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2439 lcm->map_registering = is_enable;
2444 vnet_lisp_enable_disable (u8 is_enable)
2447 clib_error_t *error = 0;
2448 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2449 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
2451 a->is_en = is_enable;
2452 error = vnet_lisp_gpe_enable_disable (a);
2455 return clib_error_return (0, "failed to %s data-plane!",
2456 a->is_en ? "enable" : "disable");
2461 /* enable all l2 and l3 ifaces */
2464 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2465 dp_add_del_iface(lcm, vni, 0, 1);
2467 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2468 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
2474 /* clear interface table */
2475 hash_free (lcm->fwd_entry_by_mapping_index);
2476 pool_free (lcm->fwd_entry_pool);
2479 /* update global flag */
2480 lcm->is_enabled = is_enable;
2485 static clib_error_t *
2486 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
2487 vlib_cli_command_t * cmd)
2489 unformat_input_t _line_input, *line_input = &_line_input;
2493 /* Get a line of input. */
2494 if (!unformat_user (input, unformat_line_input, line_input))
2497 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2499 if (unformat (line_input, "enable"))
2504 else if (unformat (line_input, "disable"))
2508 return clib_error_return (0, "parse error: '%U'",
2509 format_unformat_error, line_input);
2514 return clib_error_return (0, "state not set");
2516 vnet_lisp_enable_disable (is_enabled);
2521 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
2523 .short_help = "lisp [enable|disable]",
2524 .function = lisp_enable_disable_command_fn,
2528 static clib_error_t *
2529 lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
2530 unformat_input_t * input,
2531 vlib_cli_command_t * cmd)
2533 unformat_input_t _line_input, *line_input = &_line_input;
2537 /* Get a line of input. */
2538 if (!unformat_user (input, unformat_line_input, line_input))
2541 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2543 if (unformat (line_input, "enable"))
2548 else if (unformat (line_input, "disable"))
2552 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2560 vlib_cli_output (vm, "state not set!");
2564 vnet_lisp_map_register_enable_disable (is_enabled);
2569 VLIB_CLI_COMMAND (lisp_map_register_enable_disable_command) = {
2570 .path = "lisp map-register",
2571 .short_help = "lisp map-register [enable|disable]",
2572 .function = lisp_map_register_enable_disable_command_fn,
2576 static clib_error_t *
2577 lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
2578 unformat_input_t * input,
2579 vlib_cli_command_t * cmd)
2581 unformat_input_t _line_input, *line_input = &_line_input;
2585 /* Get a line of input. */
2586 if (!unformat_user (input, unformat_line_input, line_input))
2589 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2591 if (unformat (line_input, "enable"))
2596 else if (unformat (line_input, "disable"))
2600 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2608 vlib_cli_output (vm, "state not set!");
2612 vnet_lisp_rloc_probe_enable_disable (is_enabled);
2617 VLIB_CLI_COMMAND (lisp_rloc_probe_enable_disable_command) = {
2618 .path = "lisp rloc-probe",
2619 .short_help = "lisp rloc-probe [enable|disable]",
2620 .function = lisp_rloc_probe_enable_disable_command_fn,
2625 vnet_lisp_enable_disable_status (void)
2627 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2628 return lcm->is_enabled;
2632 format_lisp_status (u8 * s, va_list * args)
2634 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2635 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
2638 static clib_error_t *
2639 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
2640 vlib_cli_command_t * cmd)
2643 msg = format (msg, "feature: %U\ngpe: %U\n",
2644 format_lisp_status, format_vnet_lisp_gpe_status);
2645 vlib_cli_output (vm, "%v", msg);
2651 VLIB_CLI_COMMAND (lisp_show_status_command) = {
2652 .path = "show lisp status",
2653 .short_help = "show lisp status",
2654 .function = lisp_show_status_command_fn,
2658 static clib_error_t *
2659 lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
2660 unformat_input_t * input,
2661 vlib_cli_command_t * cmd)
2664 unformat_input_t _line_input, *line_input = &_line_input;
2665 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2666 uword *vni_table = 0;
2669 /* Get a line of input. */
2670 if (!unformat_user (input, unformat_line_input, line_input))
2673 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2675 if (unformat (line_input, "l2"))
2677 vni_table = lcm->bd_id_by_vni;
2680 else if (unformat (line_input, "l3"))
2682 vni_table = lcm->table_id_by_vni;
2686 return clib_error_return (0, "parse error: '%U'",
2687 format_unformat_error, line_input);
2692 vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
2696 vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
2699 hash_foreach_pair (p, vni_table,
2701 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
2709 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
2710 .path = "show lisp eid-table map",
2711 .short_help = "show lisp eid-table l2|l3",
2712 .function = lisp_show_eid_table_map_command_fn,
2716 static clib_error_t *
2717 lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
2718 unformat_input_t * input,
2719 vlib_cli_command_t * cmd)
2721 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2722 vnet_main_t *vnm = lgm->vnet_main;
2723 unformat_input_t _line_input, *line_input = &_line_input;
2725 clib_error_t *error = 0;
2726 u8 *locator_set_name = 0;
2727 locator_t locator, *locators = 0;
2728 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2732 memset (&locator, 0, sizeof (locator));
2733 memset (a, 0, sizeof (a[0]));
2735 /* Get a line of input. */
2736 if (!unformat_user (input, unformat_line_input, line_input))
2739 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2741 if (unformat (line_input, "add %_%v%_", &locator_set_name))
2743 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
2745 else if (unformat (line_input, "iface %U p %d w %d",
2746 unformat_vnet_sw_interface, vnm,
2747 &locator.sw_if_index, &locator.priority,
2751 vec_add1 (locators, locator);
2755 error = unformat_parse_error (line_input);
2760 a->name = locator_set_name;
2761 a->locators = locators;
2765 rv = vnet_lisp_add_del_locator_set (a, &ls_index);
2768 error = clib_error_return (0, "failed to %s locator-set!",
2769 is_add ? "add" : "delete");
2773 vec_free (locators);
2774 if (locator_set_name)
2775 vec_free (locator_set_name);
2780 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2781 .path = "lisp locator-set",
2782 .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2783 "p <priority> w <weight>]",
2784 .function = lisp_add_del_locator_set_command_fn,
2788 static clib_error_t *
2789 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
2790 unformat_input_t * input,
2791 vlib_cli_command_t * cmd)
2793 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2794 vnet_main_t *vnm = lgm->vnet_main;
2795 unformat_input_t _line_input, *line_input = &_line_input;
2797 clib_error_t *error = 0;
2798 u8 *locator_set_name = 0;
2799 u8 locator_set_name_set = 0;
2800 locator_t locator, *locators = 0;
2801 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
2804 memset (&locator, 0, sizeof (locator));
2805 memset (a, 0, sizeof (a[0]));
2807 /* Get a line of input. */
2808 if (!unformat_user (input, unformat_line_input, line_input))
2811 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2813 if (unformat (line_input, "add"))
2815 else if (unformat (line_input, "del"))
2817 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
2818 locator_set_name_set = 1;
2819 else if (unformat (line_input, "iface %U p %d w %d",
2820 unformat_vnet_sw_interface, vnm,
2821 &locator.sw_if_index, &locator.priority,
2825 vec_add1 (locators, locator);
2829 error = unformat_parse_error (line_input);
2834 if (!locator_set_name_set)
2836 error = clib_error_return (0, "locator_set name not set!");
2840 a->name = locator_set_name;
2841 a->locators = locators;
2845 vnet_lisp_add_del_locator (a, 0, &ls_index);
2848 vec_free (locators);
2849 vec_free (locator_set_name);
2854 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2855 .path = "lisp locator",
2856 .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2857 "p <priority> w <weight>",
2858 .function = lisp_add_del_locator_in_set_command_fn,
2862 static clib_error_t *
2863 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
2864 unformat_input_t * input,
2865 vlib_cli_command_t * cmd)
2867 locator_set_t *lsit;
2870 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2872 vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
2873 "Priority", "Weight");
2876 pool_foreach (lsit, lcm->locator_set_pool,
2882 msg = format (msg, "%v", lsit->name);
2886 msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
2888 vec_foreach (locit, lsit->locator_indices)
2892 msg = format (msg, "%16s", " ");
2894 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2896 msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
2899 msg = format (msg, "%16U%16d%16d\n", format_ip_address,
2900 &gid_address_ip(&loc->address), loc->priority,
2904 vlib_cli_output (vm, "%v", msg);
2912 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2913 .path = "show lisp locator-set",
2914 .short_help = "Shows locator-sets",
2915 .function = lisp_cp_show_locator_sets_command_fn,
2920 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2922 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2924 lisp_msmr_t _mr, *mr = &_mr;
2926 if (vnet_lisp_enable_disable_status () == 0)
2928 clib_warning ("LISP is disabled!");
2929 return VNET_API_ERROR_LISP_DISABLED;
2935 if (get_map_resolver (&a->address))
2937 clib_warning ("map-resolver %U already exists!", format_ip_address,
2942 memset (mr, 0, sizeof (*mr));
2943 ip_address_copy (&mr->address, &a->address);
2944 vec_add1 (lcm->map_resolvers, *mr);
2946 if (vec_len (lcm->map_resolvers) == 1)
2947 lcm->do_map_resolver_election = 1;
2951 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2953 mr = vec_elt_at_index (lcm->map_resolvers, i);
2954 if (!ip_address_cmp (&mr->address, &a->address))
2956 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2957 lcm->do_map_resolver_election = 1;
2959 vec_del1 (lcm->map_resolvers, i);
2967 static clib_error_t *
2968 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
2969 unformat_input_t * input,
2970 vlib_cli_command_t * cmd)
2972 unformat_input_t _line_input, *line_input = &_line_input;
2973 u8 is_add = 1, addr_set = 0;
2974 ip_address_t ip_addr;
2975 clib_error_t *error = 0;
2977 vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
2979 /* Get a line of input. */
2980 if (!unformat_user (input, unformat_line_input, line_input))
2983 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2985 if (unformat (line_input, "add"))
2987 else if (unformat (line_input, "del"))
2989 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
2993 error = unformat_parse_error (line_input);
3000 error = clib_error_return (0, "Map-resolver address must be set!");
3005 a->address = ip_addr;
3006 rv = vnet_lisp_add_del_map_resolver (a);
3009 error = clib_error_return (0, "failed to %s map-resolver!",
3010 is_add ? "add" : "delete");
3018 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
3019 .path = "lisp map-resolver",
3020 .short_help = "lisp map-resolver add/del <ip_address>",
3021 .function = lisp_add_del_map_resolver_command_fn,
3026 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
3028 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3031 if (vnet_lisp_enable_disable_status () == 0)
3033 clib_warning ("LISP is disabled!");
3034 return VNET_API_ERROR_LISP_DISABLED;
3039 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
3042 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
3043 return VNET_API_ERROR_INVALID_ARGUMENT;
3046 lcm->mreq_itr_rlocs = p[0];
3050 lcm->mreq_itr_rlocs = ~0;
3056 static clib_error_t *
3057 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
3058 unformat_input_t * input,
3059 vlib_cli_command_t * cmd)
3061 unformat_input_t _line_input, *line_input = &_line_input;
3063 u8 *locator_set_name = 0;
3064 clib_error_t *error = 0;
3066 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
3068 /* Get a line of input. */
3069 if (!unformat_user (input, unformat_line_input, line_input))
3072 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3074 if (unformat (line_input, "del"))
3076 else if (unformat (line_input, "add %_%v%_", &locator_set_name))
3080 error = unformat_parse_error (line_input);
3086 a->locator_set_name = locator_set_name;
3087 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
3090 error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
3091 is_add ? "add" : "delete");
3094 vec_free (locator_set_name);
3102 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
3103 .path = "lisp map-request itr-rlocs",
3104 .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
3105 .function = lisp_add_del_mreq_itr_rlocs_command_fn,
3109 static clib_error_t *
3110 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
3111 unformat_input_t * input,
3112 vlib_cli_command_t * cmd)
3114 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3115 locator_set_t *loc_set;
3117 vlib_cli_output (vm, "%=20s", "itr-rlocs");
3119 if (~0 == lcm->mreq_itr_rlocs)
3124 loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
3126 vlib_cli_output (vm, "%=20s", loc_set->name);
3132 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
3133 .path = "show lisp map-request itr-rlocs",
3134 .short_help = "Shows map-request itr-rlocs",
3135 .function = lisp_show_mreq_itr_rlocs_command_fn,
3139 /* Statistics (not really errors) */
3140 #define foreach_lisp_cp_lookup_error \
3142 _(MAP_REQUESTS_SENT, "map-request sent")
3144 static char *lisp_cp_lookup_error_strings[] = {
3145 #define _(sym,string) string,
3146 foreach_lisp_cp_lookup_error
3152 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
3153 foreach_lisp_cp_lookup_error
3155 LISP_CP_LOOKUP_N_ERROR,
3156 } lisp_cp_lookup_error_t;
3160 LISP_CP_LOOKUP_NEXT_DROP,
3161 LISP_CP_LOOKUP_N_NEXT,
3162 } lisp_cp_lookup_next_t;
3166 gid_address_t dst_eid;
3167 ip_address_t map_resolver_ip;
3168 } lisp_cp_lookup_trace_t;
3171 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
3173 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3174 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3175 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
3177 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
3178 format_ip_address, &t->map_resolver_ip, format_gid_address,
3184 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
3185 ip_address_t * sloc)
3190 if (vec_len (lcm->map_resolvers) == 0)
3192 clib_warning ("No map-resolver configured");
3196 /* find the first mr ip we have a route to and the ip of the
3197 * iface that has a route to it */
3198 vec_foreach (mrit, lcm->map_resolvers)
3201 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
3203 ip_address_copy (mr_ip, a);
3205 /* also update globals */
3210 clib_warning ("Can't find map-resolver and local interface ip!");
3214 static gid_address_t *
3215 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
3221 ip_interface_address_t *ia = 0;
3222 gid_address_t gid_data, *gid = &gid_data;
3223 gid_address_t *rlocs = 0;
3224 ip_prefix_t *ippref = &gid_address_ippref (gid);
3225 ip_address_t *rloc = &ip_prefix_addr (ippref);
3227 memset (gid, 0, sizeof (gid[0]));
3228 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
3229 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
3231 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
3232 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
3234 /* Add ipv4 locators first TODO sort them */
3237 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3238 loc->sw_if_index, 1 /* unnumbered */,
3240 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
3241 ip_address_set (rloc, addr, IP4);
3242 ip_prefix_len (ippref) = 32;
3243 ip_prefix_normalize (ippref);
3244 vec_add1 (rlocs, gid[0]);
3247 /* Add ipv6 locators */
3248 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3249 loc->sw_if_index, 1 /* unnumbered */,
3251 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
3252 ip_address_set (rloc, addr, IP6);
3253 ip_prefix_len (ippref) = 128;
3254 ip_prefix_normalize (ippref);
3255 vec_add1 (rlocs, gid[0]);
3263 static vlib_buffer_t *
3264 build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
3265 ip_address_t * sloc, ip_address_t * rloc,
3266 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
3270 vlib_main_t *vm = lcm->vlib_main;
3272 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3274 clib_warning ("Can't allocate buffer for Map-Request!");
3278 b = vlib_get_buffer (vm, bi);
3280 /* leave some space for the encap headers */
3281 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3284 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
3285 1 /* rloc probe */ , nonce_res);
3287 /* push outer ip header */
3288 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3296 static vlib_buffer_t *
3297 build_encapsulated_map_request (lisp_cp_main_t * lcm,
3298 gid_address_t * seid, gid_address_t * deid,
3299 locator_set_t * loc_set, ip_address_t * mr_ip,
3300 ip_address_t * sloc, u8 is_smr_invoked,
3301 u64 * nonce_res, u32 * bi_res)
3305 gid_address_t *rlocs = 0;
3306 vlib_main_t *vm = lcm->vlib_main;
3308 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3310 clib_warning ("Can't allocate buffer for Map-Request!");
3314 b = vlib_get_buffer (vm, bi);
3316 /* leave some space for the encap headers */
3317 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3320 rlocs = build_itr_rloc_list (lcm, loc_set);
3322 if (MR_MODE_SRC_DST == lcm->map_request_mode
3323 && GID_ADDR_SRC_DST != gid_address_type (deid))
3326 memset (&sd, 0, sizeof (sd));
3327 build_src_dst (&sd, seid, deid);
3328 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
3329 0 /* rloc probe */ , nonce_res);
3334 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
3335 0 /* rloc probe */ , nonce_res);
3338 /* push ecm: udp-ip-lisp */
3339 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
3341 /* push outer ip header */
3342 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3352 reset_pending_mr_counters (pending_map_request_t * r)
3354 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3359 elect_map_resolver (lisp_cp_main_t * lcm)
3363 vec_foreach (mr, lcm->map_resolvers)
3367 ip_address_copy (&lcm->active_map_resolver, &mr->address);
3368 lcm->do_map_resolver_election = 0;
3376 free_map_register_records (mapping_t * maps)
3379 vec_foreach (map, maps) vec_free (map->locators);
3385 add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
3386 ip_address_t * probed_loc)
3389 locator_t *loc, new;
3390 ip_interface_address_t *ia = 0;
3392 ip_address_t *new_ip = &gid_address_ip (&new.address);
3395 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
3397 vec_foreach (li, ls->locator_indices)
3399 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
3404 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3405 loc->sw_if_index, 1 /* unnumbered */,
3407 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
3409 ip_address_set (new_ip, addr, IP4);
3412 /* Add ipv6 locators */
3413 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3414 loc->sw_if_index, 1 /* unnumbered */,
3416 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
3418 ip_address_set (new_ip, addr, IP6);
3422 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
3425 vec_add1 (m->locators, new);
3430 build_map_register_record_list (lisp_cp_main_t * lcm)
3432 mapping_t *recs = 0, rec, *m;
3435 pool_foreach(m, lcm->mapping_pool,
3437 /* for now build only local mappings */
3442 add_locators (lcm, &rec, m->locator_set_index, NULL);
3443 vec_add1 (recs, rec);
3451 update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
3452 lisp_key_type_t key_id, u8 * key,
3453 u16 auth_data_len, u32 msg_len)
3455 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
3456 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
3458 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3459 (unsigned char *) map_reg_hdr, msg_len, NULL,
3461 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
3466 static vlib_buffer_t *
3467 build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
3468 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
3469 mapping_t * records, lisp_key_type_t key_id, u8 * key,
3474 u32 bi, auth_data_len = 0, msg_len = 0;
3475 vlib_main_t *vm = lcm->vlib_main;
3477 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3479 clib_warning ("Can't allocate buffer for Map-Register!");
3483 b = vlib_get_buffer (vm, bi);
3485 /* leave some space for the encap headers */
3486 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3488 auth_data_len = auth_data_len_by_key_id (key_id);
3489 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
3490 auth_data_len, nonce_res,
3493 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
3496 /* push outer ip header */
3497 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3505 get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
3508 while (lcm->do_map_resolver_election
3509 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
3510 &lcm->active_map_resolver,
3513 if (0 == elect_map_resolver (lcm))
3514 /* all map resolvers are down */
3516 /* restart MR checking by marking all of them up */
3517 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
3525 send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
3526 u32 local_locator_set_index, ip_address_t * sloc,
3527 ip_address_t * rloc)
3534 u32 next_index, *to_next;
3535 gid_address_t *itr_rlocs;
3537 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
3538 itr_rlocs = build_itr_rloc_list (lcm, ls);
3540 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
3541 vec_free (itr_rlocs);
3545 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3547 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3548 ip4_lookup_node.index : ip6_lookup_node.index;
3550 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3552 /* Enqueue the packet */
3553 to_next = vlib_frame_vector_args (f);
3556 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3558 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3563 send_rloc_probes (lisp_cp_main_t * lcm)
3572 pool_foreach (e, lcm->fwd_entry_pool,
3574 if (vec_len (e->locator_pairs) == 0)
3577 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
3580 clib_warning ("internal error: cannot find local eid %U in "
3581 "map-cache!", format_gid_address, &e->leid);
3584 lm = pool_elt_at_index (lcm->mapping_pool, si);
3586 /* get the best (lowest) priority */
3587 lprio = e->locator_pairs[0].priority;
3589 /* send rloc-probe for pair(s) with the best remote locator priority */
3590 vec_foreach (lp, e->locator_pairs)
3592 if (lp->priority != lprio)
3595 /* get first remote locator */
3596 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
3606 send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
3613 u32 next_index, *to_next;
3614 ip_address_t *ms = 0;
3615 mapping_t *records, *r, *g;
3617 // TODO: support multiple map servers and do election
3618 if (0 == vec_len (lcm->map_servers))
3621 ms = &lcm->map_servers[0].address;
3623 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
3625 clib_warning ("no eligible interface address found for %U!",
3626 format_ip_address, &lcm->map_servers[0]);
3630 records = build_map_register_record_list (lcm);
3634 vec_foreach (r, records)
3637 u8 key_id = r->key_id;
3640 continue; /* no secret key -> map-register cannot be sent */
3643 // TODO: group mappings that share common key
3645 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, g,
3651 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3653 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3654 ip4_lookup_node.index : ip6_lookup_node.index;
3656 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3658 /* Enqueue the packet */
3659 to_next = vlib_frame_vector_args (f);
3662 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3664 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3666 free_map_register_records (records);
3671 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
3672 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
3674 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
3675 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
3678 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
3679 gid_address_t * seid, gid_address_t * deid,
3680 u8 is_smr_invoked, u8 is_resend)
3682 u32 next_index, bi = 0, *to_next, map_index;
3686 locator_set_t *loc_set;
3688 pending_map_request_t *pmr, *duplicate_pmr = 0;
3692 /* if there is already a pending request remember it */
3695 pool_foreach(pmr, lcm->pending_map_requests_pool,
3697 if (!gid_address_cmp (&pmr->src, seid)
3698 && !gid_address_cmp (&pmr->dst, deid))
3700 duplicate_pmr = pmr;
3706 if (!is_resend && duplicate_pmr)
3708 /* don't send the request if there is a pending map request already */
3712 /* get locator-set for seid */
3713 if (!lcm->lisp_pitr)
3715 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
3716 if (map_index == ~0)
3718 clib_warning ("No local mapping found in eid-table for %U!",
3719 format_gid_address, seid);
3723 map = pool_elt_at_index (lcm->mapping_pool, map_index);
3728 ("Mapping found for src eid %U is not marked as local!",
3729 format_gid_address, seid);
3732 ls_index = map->locator_set_index;
3736 map_index = lcm->pitr_map_index;
3737 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
3738 ls_index = map->locator_set_index;
3741 /* overwrite locator set if map-request itr-rlocs configured */
3742 if (~0 != lcm->mreq_itr_rlocs)
3744 ls_index = lcm->mreq_itr_rlocs;
3747 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
3749 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
3752 duplicate_pmr->to_be_removed = 1;
3756 /* build the encapsulated map request */
3757 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
3758 &lcm->active_map_resolver,
3759 &sloc, is_smr_invoked, &nonce, &bi);
3764 /* set fib index to default and lookup node */
3765 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3766 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3767 ip4_lookup_node.index : ip6_lookup_node.index;
3769 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3771 /* Enqueue the packet */
3772 to_next = vlib_frame_vector_args (f);
3775 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3778 /* if there is a pending request already update it */
3780 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
3782 /* remove the oldest nonce */
3783 u64 CLIB_UNUSED (tmp), *nonce_del;
3784 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
3785 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
3786 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
3789 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
3790 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3791 duplicate_pmr - lcm->pending_map_requests_pool);
3795 /* add map-request to pending requests table */
3796 pool_get (lcm->pending_map_requests_pool, pmr);
3797 memset (pmr, 0, sizeof (*pmr));
3798 gid_address_copy (&pmr->src, seid);
3799 gid_address_copy (&pmr->dst, deid);
3800 clib_fifo_add1 (pmr->nonces, nonce);
3801 pmr->is_smr_invoked = is_smr_invoked;
3802 reset_pending_mr_counters (pmr);
3803 hash_set (lcm->pending_map_requests_by_nonce, nonce,
3804 pmr - lcm->pending_map_requests_pool);
3811 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
3813 ip4_header_t *ip4 = hdr;
3816 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3818 ip_address_set (src, &ip4->src_address, IP4);
3819 ip_address_set (dst, &ip4->dst_address, IP4);
3824 ip_address_set (src, &ip6->src_address, IP6);
3825 ip_address_set (dst, &ip6->dst_address, IP6);
3830 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3834 u32 vni = ~0, table_id = ~0;
3836 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
3837 IP4 ? FIB_PROTOCOL_IP4 :
3843 vnip = hash_get (lcm->vni_by_table_id, table_id);
3847 clib_warning ("vrf %d is not mapped to any vni!", table_id);
3853 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3859 l2input_main_t *l2im = &l2input_main;
3860 l2_input_config_t *config;
3861 l2_bridge_domain_t *bd_config;
3863 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3864 config = vec_elt_at_index (l2im->configs, sw_if_index0);
3865 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3867 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
3871 clib_warning ("bridge domain %d is not mapped to any vni!",
3878 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3879 gid_address_t * src, gid_address_t * dst)
3884 memset (src, 0, sizeof (*src));
3885 memset (dst, 0, sizeof (*dst));
3886 type = vnet_buffer (b)->lisp.overlay_afi;
3888 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3891 u8 version, preflen;
3893 gid_address_type (src) = GID_ADDR_IP_PREFIX;
3894 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
3896 ip = vlib_buffer_get_current (b);
3897 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
3899 version = gid_address_ip_version (src);
3900 preflen = ip_address_max_len (version);
3901 gid_address_ippref_len (src) = preflen;
3902 gid_address_ippref_len (dst) = preflen;
3904 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3905 gid_address_vni (dst) = vni;
3906 gid_address_vni (src) = vni;
3908 else if (LISP_AFI_MAC == type)
3910 ethernet_header_t *eh;
3912 eh = vlib_buffer_get_current (b);
3914 gid_address_type (src) = GID_ADDR_MAC;
3915 gid_address_type (dst) = GID_ADDR_MAC;
3916 mac_copy (&gid_address_mac (src), eh->src_address);
3917 mac_copy (&gid_address_mac (dst), eh->dst_address);
3920 vni = lisp_get_vni_from_buffer_eth (lcm, b);
3922 gid_address_vni (dst) = vni;
3923 gid_address_vni (src) = vni;
3928 lisp_cp_lookup_inline (vlib_main_t * vm,
3929 vlib_node_runtime_t * node,
3930 vlib_frame_t * from_frame, int overlay)
3932 u32 *from, *to_next_drop, di, si;
3933 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3934 u32 pkts_mapped = 0;
3935 uword n_left_from, n_left_to_next_drop;
3937 from = vlib_frame_vector_args (from_frame);
3938 n_left_from = from_frame->n_vectors;
3940 while (n_left_from > 0)
3942 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3943 to_next_drop, n_left_to_next_drop);
3945 while (n_left_from > 0 && n_left_to_next_drop > 0)
3949 gid_address_t src, dst;
3954 to_next_drop[0] = pi0;
3956 n_left_to_next_drop -= 1;
3958 b0 = vlib_get_buffer (vm, pi0);
3959 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
3960 vnet_buffer (b0)->lisp.overlay_afi = overlay;
3962 /* src/dst eid pair */
3963 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
3965 /* if we have remote mapping for destination already in map-chache
3966 add forwarding tunnel directly. If not send a map-request */
3967 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3971 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3972 /* send a map-request also in case of negative mapping entry
3973 with corresponding action */
3974 if (m->action == LISP_SEND_MAP_REQUEST)
3976 /* send map-request */
3977 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3978 0 /* is_resend */ );
3983 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3987 dp_add_fwd_entry_from_mt (si, di);
3993 /* send map-request */
3994 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3995 0 /* is_resend */ );
3999 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
4001 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
4004 memset (tr, 0, sizeof (*tr));
4005 gid_address_copy (&tr->dst_eid, &dst);
4006 ip_address_copy (&tr->map_resolver_ip,
4007 &lcm->active_map_resolver);
4009 gid_address_free (&dst);
4010 gid_address_free (&src);
4013 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
4014 n_left_to_next_drop);
4016 vlib_node_increment_counter (vm, node->node_index,
4017 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
4019 return from_frame->n_vectors;
4023 lisp_cp_lookup_ip4 (vlib_main_t * vm,
4024 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4026 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
4030 lisp_cp_lookup_ip6 (vlib_main_t * vm,
4031 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4033 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
4037 lisp_cp_lookup_l2 (vlib_main_t * vm,
4038 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4040 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
4044 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
4045 .function = lisp_cp_lookup_ip4,
4046 .name = "lisp-cp-lookup-ip4",
4047 .vector_size = sizeof (u32),
4048 .format_trace = format_lisp_cp_lookup_trace,
4049 .type = VLIB_NODE_TYPE_INTERNAL,
4051 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4052 .error_strings = lisp_cp_lookup_error_strings,
4054 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4057 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4063 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
4064 .function = lisp_cp_lookup_ip6,
4065 .name = "lisp-cp-lookup-ip6",
4066 .vector_size = sizeof (u32),
4067 .format_trace = format_lisp_cp_lookup_trace,
4068 .type = VLIB_NODE_TYPE_INTERNAL,
4070 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4071 .error_strings = lisp_cp_lookup_error_strings,
4073 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4076 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4082 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
4083 .function = lisp_cp_lookup_l2,
4084 .name = "lisp-cp-lookup-l2",
4085 .vector_size = sizeof (u32),
4086 .format_trace = format_lisp_cp_lookup_trace,
4087 .type = VLIB_NODE_TYPE_INTERNAL,
4089 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4090 .error_strings = lisp_cp_lookup_error_strings,
4092 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4095 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
4100 /* lisp_cp_input statistics */
4101 #define foreach_lisp_cp_input_error \
4103 _(MAP_REPLIES_RECEIVED, "map-replies received")
4105 static char *lisp_cp_input_error_strings[] = {
4106 #define _(sym,string) string,
4107 foreach_lisp_cp_input_error
4113 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
4114 foreach_lisp_cp_input_error
4116 LISP_CP_INPUT_N_ERROR,
4117 } lisp_cp_input_error_t;
4121 LISP_CP_INPUT_NEXT_DROP,
4122 LISP_CP_INPUT_N_NEXT,
4123 } lisp_cp_input_next_t;
4127 gid_address_t dst_eid;
4128 ip4_address_t map_resolver_ip;
4129 } lisp_cp_input_trace_t;
4132 format_lisp_cp_input_trace (u8 * s, va_list * args)
4134 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
4135 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
4136 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
4137 va_arg (*args, lisp_cp_input_trace_t *);
4139 s = format (s, "LISP-CP-INPUT: TODO");
4144 remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
4148 m = pool_elt_at_index (lcm->mapping_pool, mi);
4149 lisp_add_del_adjacency (lcm, 0, &m->eid, 0 /* is_add */ );
4150 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
4151 0 /* is_static */ , 0);
4152 mapping_delete_timer (lcm, mi);
4156 mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
4157 f64 expiration_time)
4160 u64 now = clib_cpu_time_now ();
4161 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
4162 u64 exp_clock_time = now + expiration_time * cpu_cps;
4164 m = pool_elt_at_index (lcm->mapping_pool, mi);
4167 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
4171 map_records_arg_free (map_records_arg_t * a)
4174 vec_foreach (m, a->mappings)
4176 vec_free (m->locators);
4177 gid_address_free (&m->eid);
4184 process_map_reply (map_records_arg_t * a)
4187 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4188 u32 dst_map_index = 0;
4189 pending_map_request_t *pmr;
4193 if (a->is_rloc_probe)
4196 /* Check pending requests table and nonce */
4197 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
4200 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
4203 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
4205 vec_foreach (m, a->mappings)
4207 /* insert/update mappings cache */
4208 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
4209 m->authoritative, m->ttl,
4210 1, 0 /* is_static */ , &dst_map_index);
4212 /* try to program forwarding only if mapping saved or updated */
4213 if ((u32) ~ 0 != dst_map_index)
4215 lisp_add_del_adjacency (lcm, &pmr->src, &m->eid, 1);
4216 if ((u32) ~ 0 != m->ttl)
4217 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
4221 /* remove pending map request entry */
4224 clib_fifo_foreach (noncep, pmr->nonces, ({
4225 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
4229 clib_fifo_free (pmr->nonces);
4230 pool_put (lcm->pending_map_requests_pool, pmr);
4233 map_records_arg_free (a);
4238 is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
4239 lisp_key_type_t key_id, u8 * key)
4245 auth_data_len = auth_data_len_by_key_id (key_id);
4246 if ((u16) ~ 0 == auth_data_len)
4248 clib_warning ("invalid length for key_id %d!", key_id);
4252 /* save auth data */
4253 vec_validate (auth_data, auth_data_len - 1);
4254 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
4256 /* clear auth data */
4257 memset (MNOTIFY_DATA (h), 0, auth_data_len);
4259 /* get hash of the message */
4260 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
4261 (unsigned char *) h, msg_len, NULL, NULL);
4263 result = memcmp (code, auth_data, auth_data_len);
4265 vec_free (auth_data);
4271 process_map_notify (map_records_arg_t * a)
4273 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4276 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
4279 clib_warning ("No pending map-register entry with nonce %lu!",
4284 map_records_arg_free (a);
4285 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
4289 get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
4293 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
4296 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
4300 return pool_elt_at_index (lcm->mapping_pool, mi);
4304 * When map-notify is received it is necessary that all EIDs in the record
4305 * list share common key. The key is then used to verify authentication
4306 * data in map-notify message.
4309 map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
4310 u32 key_id, u8 ** key_out)
4312 u32 i, len = vec_len (maps);
4315 /* get key of the first mapping */
4316 m = get_mapping (lcm, &maps[0].eid);
4320 key_out[0] = m->key;
4322 for (i = 1; i < len; i++)
4324 m = get_mapping (lcm, &maps[i].eid);
4328 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
4330 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
4338 parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
4340 locator_t *locators = 0;
4346 /* parse record eid */
4347 for (i = 0; i < count; i++)
4349 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
4352 clib_warning ("Failed to parse mapping record!");
4353 vec_foreach (loc, locators) locator_free (loc);
4354 vec_free (locators);
4358 m.locators = locators;
4359 gid_address_copy (&m.eid, &deid);
4360 vec_add1 (a->mappings, m);
4366 static map_records_arg_t *
4367 parse_map_notify (vlib_buffer_t * b)
4370 map_notify_hdr_t *mnotif_hdr;
4371 lisp_key_type_t key_id;
4372 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4375 u16 auth_data_len = 0;
4377 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
4379 memset (a, 0, sizeof (*a));
4380 mnotif_hdr = vlib_buffer_get_current (b);
4381 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
4382 memset (&deid, 0, sizeof (deid));
4384 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
4385 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
4386 auth_data_len = auth_data_len_by_key_id (key_id);
4388 /* advance buffer by authentication data */
4389 vlib_buffer_pull (b, auth_data_len);
4391 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
4392 rc = parse_map_records (b, a, record_count);
4395 map_records_arg_free (a);
4399 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
4402 map_records_arg_free (a);
4406 /* verify authentication data */
4407 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
4408 - (u8 *) mnotif_hdr, key_id, key))
4410 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
4412 map_records_arg_free (a);
4418 static vlib_buffer_t *
4419 build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
4420 ip_address_t * dst, u64 nonce, u8 probe_bit,
4421 mapping_t * records, u16 dst_port, u32 * bi_res)
4425 vlib_main_t *vm = lcm->vlib_main;
4427 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
4429 clib_warning ("Can't allocate buffer for Map-Register!");
4433 b = vlib_get_buffer (vm, bi);
4435 /* leave some space for the encap headers */
4436 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
4438 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
4440 /* push outer ip header */
4441 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
4448 send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
4449 u8 probe_bit, u64 nonce, u16 dst_port,
4450 ip_address_t * probed_loc)
4456 u32 next_index, *to_next;
4457 mapping_t *records = 0, *m;
4459 m = pool_elt_at_index (lcm->mapping_pool, mi);
4463 vec_add1 (records, m[0]);
4464 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
4465 memset (&src, 0, sizeof (src));
4467 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
4469 clib_warning ("can't find inteface address for %U", format_ip_address,
4474 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
4478 free_map_register_records (records);
4480 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
4481 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
4482 ip4_lookup_node.index : ip6_lookup_node.index;
4484 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
4486 /* Enqueue the packet */
4487 to_next = vlib_frame_vector_args (f);
4490 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
4495 find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
4497 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
4498 if (start < 0 && start < -sizeof (b->pre_data))
4504 *ip_hdr = b->data + start;
4505 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
4510 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm,
4514 ip_address_t *dst_loc = 0, probed_loc, src_loc;
4516 map_request_hdr_t *mreq_hdr;
4517 gid_address_t src, dst;
4520 gid_address_t *itr_rlocs = 0;
4522 mreq_hdr = vlib_buffer_get_current (b);
4523 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
4526 ("Only SMR Map-Requests and RLOC probe supported for now!");
4530 vlib_buffer_pull (b, sizeof (*mreq_hdr));
4531 nonce = MREQ_NONCE (mreq_hdr);
4534 len = lisp_msg_parse_addr (b, &src);
4538 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
4539 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
4543 /* parse eid records and send SMR-invoked map-requests */
4544 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
4546 memset (&dst, 0, sizeof (dst));
4547 len = lisp_msg_parse_eid_rec (b, &dst);
4550 clib_warning ("Can't parse map-request EID-record");
4554 if (MREQ_SMR (mreq_hdr))
4556 /* send SMR-invoked map-requests */
4557 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
4559 else if (MREQ_RLOC_PROBE (mreq_hdr))
4561 find_ip_header (b, &ip_hdr);
4564 clib_warning ("Cannot find the IP header!");
4568 memset (&m, 0, sizeof (m));
4569 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
4571 // TODO: select best locator; for now use the first one
4572 dst_loc = &gid_address_ip (&itr_rlocs[0]);
4574 /* get src/dst IP addresses */
4575 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
4577 // TODO get source port from buffer
4578 u16 src_port = LISP_CONTROL_PORT;
4580 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
4581 src_port, &probed_loc);
4586 vec_free (itr_rlocs);
4589 static map_records_arg_t *
4590 parse_map_reply (vlib_buffer_t * b)
4597 map_reply_hdr_t *mrep_hdr;
4598 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
4599 memset (a, 0, sizeof (*a));
4600 locator_t *locators;
4602 mrep_hdr = vlib_buffer_get_current (b);
4603 a->nonce = MREP_NONCE (mrep_hdr);
4604 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
4605 vlib_buffer_pull (b, sizeof (*mrep_hdr));
4607 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
4609 memset (&m, 0, sizeof (m));
4611 h = vlib_buffer_get_current (b);
4613 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
4614 m.action = MAP_REC_ACTION (h);
4615 m.authoritative = MAP_REC_AUTH (h);
4617 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
4620 clib_warning ("Failed to parse mapping record!");
4621 map_records_arg_free (a);
4625 m.locators = locators;
4626 gid_address_copy (&m.eid, &deid);
4627 vec_add1 (a->mappings, m);
4633 queue_map_reply_for_processing (map_records_arg_t * a)
4635 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
4639 queue_map_notify_for_processing (map_records_arg_t * a)
4641 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
4645 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
4646 vlib_frame_t * from_frame)
4648 u32 n_left_from, *from, *to_next_drop;
4649 lisp_msg_type_e type;
4650 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4651 map_records_arg_t *a;
4653 from = vlib_frame_vector_args (from_frame);
4654 n_left_from = from_frame->n_vectors;
4657 while (n_left_from > 0)
4659 u32 n_left_to_next_drop;
4661 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4662 to_next_drop, n_left_to_next_drop);
4663 while (n_left_from > 0 && n_left_to_next_drop > 0)
4671 to_next_drop[0] = bi0;
4673 n_left_to_next_drop -= 1;
4675 b0 = vlib_get_buffer (vm, bi0);
4677 type = lisp_msg_type (vlib_buffer_get_current (b0));
4680 case LISP_MAP_REPLY:
4681 a = parse_map_reply (b0);
4683 queue_map_reply_for_processing (a);
4685 case LISP_MAP_REQUEST:
4686 process_map_request (vm, lcm, b0);
4688 case LISP_MAP_NOTIFY:
4689 a = parse_map_notify (b0);
4691 queue_map_notify_for_processing (a);
4694 clib_warning ("Unsupported LISP message type %d", type);
4698 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
4700 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
4706 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4707 n_left_to_next_drop);
4709 return from_frame->n_vectors;
4713 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
4714 .function = lisp_cp_input,
4715 .name = "lisp-cp-input",
4716 .vector_size = sizeof (u32),
4717 .format_trace = format_lisp_cp_input_trace,
4718 .type = VLIB_NODE_TYPE_INTERNAL,
4720 .n_errors = LISP_CP_INPUT_N_ERROR,
4721 .error_strings = lisp_cp_input_error_strings,
4723 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
4726 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
4732 lisp_cp_init (vlib_main_t * vm)
4734 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4735 clib_error_t *error = 0;
4737 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
4740 lcm->im4 = &ip4_main;
4741 lcm->im6 = &ip6_main;
4742 lcm->vlib_main = vm;
4743 lcm->vnet_main = vnet_get_main ();
4744 lcm->mreq_itr_rlocs = ~0;
4746 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
4748 gid_dictionary_init (&lcm->mapping_index_by_gid);
4749 lcm->do_map_resolver_election = 1;
4750 lcm->map_request_mode = MR_MODE_DST_ONLY;
4752 /* default vrf mapped to vni 0 */
4753 hash_set (lcm->table_id_by_vni, 0, 0);
4754 hash_set (lcm->vni_by_table_id, 0, 0);
4756 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
4757 lisp_cp_input_node.index, 1 /* is_ip4 */ );
4758 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
4759 lisp_cp_input_node.index, 0 /* is_ip4 */ );
4761 u64 now = clib_cpu_time_now ();
4762 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
4767 send_map_request_thread_fn (void *arg)
4769 map_request_args_t *a = arg;
4770 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4773 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4775 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4781 queue_map_request (gid_address_t * seid, gid_address_t * deid,
4782 u8 smr_invoked, u8 is_resend)
4784 map_request_args_t a;
4786 a.is_resend = is_resend;
4787 gid_address_copy (&a.seid, seid);
4788 gid_address_copy (&a.deid, deid);
4789 a.smr_invoked = smr_invoked;
4791 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
4792 (u8 *) & a, sizeof (a));
4797 * Take an action with a pending map request depending on expiration time
4798 * and re-try counters.
4801 update_pending_request (pending_map_request_t * r, f64 dt)
4803 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4806 if (r->time_to_expire - dt < 0)
4807 /* it's time to decide what to do with this pending request */
4809 if (r->retries_num >= NUMBER_OF_RETRIES)
4810 /* too many retries -> assume current map resolver is not available */
4812 mr = get_map_resolver (&lcm->active_map_resolver);
4815 clib_warning ("Map resolver %U not found - probably deleted "
4816 "by the user recently.", format_ip_address,
4817 &lcm->active_map_resolver);
4821 clib_warning ("map resolver %U is unreachable, ignoring",
4822 format_ip_address, &lcm->active_map_resolver);
4824 /* mark current map resolver unavailable so it won't be
4825 * selected next time */
4827 mr->last_update = vlib_time_now (lcm->vlib_main);
4830 reset_pending_mr_counters (r);
4831 elect_map_resolver (lcm);
4833 /* try to find a next eligible map resolver and re-send */
4834 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4840 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4843 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
4847 r->time_to_expire -= dt;
4851 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4854 pending_map_request_t *pmr;
4855 u32 *to_be_removed = 0, *pmr_index;
4858 pool_foreach (pmr, lcm->pending_map_requests_pool,
4860 if (pmr->to_be_removed)
4862 clib_fifo_foreach (nonce, pmr->nonces, ({
4863 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4866 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4871 vec_foreach (pmr_index, to_be_removed)
4872 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
4874 vec_free (to_be_removed);
4878 update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4880 static f64 time_left = RLOC_PROBING_INTERVAL;
4882 if (!lcm->is_enabled || !lcm->rloc_probing)
4888 time_left = RLOC_PROBING_INTERVAL;
4889 send_rloc_probes (lcm);
4894 update_map_register (lisp_cp_main_t * lcm, f64 dt)
4896 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4897 static u64 mreg_sent_counter = 0;
4899 if (!lcm->is_enabled || !lcm->map_registering)
4905 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4906 time_left = MAP_REGISTER_INTERVAL;
4909 mreg_sent_counter++;
4910 time_left = QUICK_MAP_REGISTER_INTERVAL;
4912 send_map_register (lcm, 1 /* want map notify */ );
4917 send_map_resolver_service (vlib_main_t * vm,
4918 vlib_node_runtime_t * rt, vlib_frame_t * f)
4922 pending_map_request_t *pmr;
4923 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4927 vlib_process_wait_for_event_or_clock (vm, period);
4929 /* currently no signals are expected - just wait for clock */
4930 (void) vlib_process_get_events (vm, 0);
4933 pool_foreach (pmr, lcm->pending_map_requests_pool,
4935 if (!pmr->to_be_removed)
4936 update_pending_request (pmr, period);
4940 remove_dead_pending_map_requests (lcm);
4942 update_map_register (lcm, period);
4943 update_rloc_probing (lcm, period);
4945 u64 now = clib_cpu_time_now ();
4947 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
4948 if (vec_len (expired) > 0)
4951 vec_foreach (mi, expired)
4953 remove_expired_mapping (lcm, mi[0]);
4955 _vec_len (expired) = 0;
4964 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
4965 .function = send_map_resolver_service,
4966 .type = VLIB_NODE_TYPE_PROCESS,
4967 .name = "lisp-retry-service",
4968 .process_log2_n_stack_bytes = 16,
4972 VLIB_INIT_FUNCTION (lisp_cp_init);
4975 * fd.io coding-style-patch-verification: ON
4978 * eval: (c-set-style "gnu")