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 <vnet/lisp-cp/control.h>
17 #include <vnet/lisp-cp/packets.h>
18 #include <vnet/lisp-cp/lisp_msg_serdes.h>
19 #include <vnet/lisp-gpe/lisp_gpe.h>
22 add_fwd_entry (lisp_cp_main_t* lcm, u32 src_map_index, u32 dst_map_index);
25 del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index);
28 compare_locators (lisp_cp_main_t *lcm, u32 * old_ls_indexes,
29 locator_t * new_locators);
31 /* Stores mapping in map-cache. It does NOT program data plane forwarding for
32 * remote/learned mappings. */
34 vnet_lisp_add_del_mapping (vnet_lisp_add_del_mapping_args_t * a,
35 u32 * map_index_result)
37 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
38 u32 mi, * map_indexp, map_index, i;
39 mapping_t * m, * old_map;
42 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->deid);
43 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
46 /* TODO check if overwriting and take appropriate actions */
47 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid,
50 clib_warning ("eid %U found in the eid-table", format_gid_address,
52 return VNET_API_ERROR_VALUE_EXIST;
55 pool_get(lcm->mapping_pool, m);
56 gid_address_copy (&m->eid, &a->deid);
57 m->locator_set_index = a->locator_set_index;
59 m->action = a->action;
62 map_index = m - lcm->mapping_pool;
63 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->deid, map_index,
66 if (pool_is_free_index(lcm->locator_set_pool, a->locator_set_index))
68 clib_warning("Locator set with index %d doesn't exist",
69 a->locator_set_index);
70 return VNET_API_ERROR_INVALID_VALUE;
73 /* add eid to list of eids supported by locator-set */
74 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
75 eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids,
76 a->locator_set_index);
77 vec_add1(eid_indexes[0], map_index);
82 vec_add1(lcm->local_mappings_indexes, map_index);
84 map_index_result[0] = map_index;
88 if (mi == GID_LOOKUP_MISS)
90 clib_warning("eid %U not found in the eid-table", format_gid_address,
92 return VNET_API_ERROR_INVALID_VALUE;
95 /* clear locator-set to eids binding */
96 eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids,
97 a->locator_set_index);
98 for (i = 0; i < vec_len(eid_indexes[0]); i++)
100 map_indexp = vec_elt_at_index(eid_indexes[0], i);
101 if (map_indexp[0] == mi)
104 vec_del1(eid_indexes[0], i);
106 /* remove local mark if needed */
107 m = pool_elt_at_index(lcm->mapping_pool, mi);
111 for (k = 0; k < vec_len(lcm->local_mappings_indexes); k++)
113 lm_indexp = vec_elt_at_index(lcm->local_mappings_indexes, k);
114 if (lm_indexp[0] == mi)
117 vec_del1(lcm->local_mappings_indexes, k);
121 /* remove tunnel ??? */
124 /* remove mapping from dictionary */
125 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->deid, 0, 0);
126 gid_address_free (&m->eid);
127 pool_put_index (lcm->mapping_pool, mi);
133 /* Stores mapping in map-cache and programs data plane for local mappings. */
135 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
136 u32 * map_index_result)
138 uword * table_id, * refc;
140 vnet_lisp_gpe_add_del_iface_args_t _ai, *ai = &_ai;
141 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
143 if (vnet_lisp_enable_disable_status () == 0)
145 clib_warning ("LISP is disabled!");
146 return VNET_API_ERROR_LISP_DISABLED;
149 vni = gid_address_vni(&a->deid);
151 /* store/remove mapping from map-cache */
152 rv = vnet_lisp_add_del_mapping (a, map_index_result);
156 table_id = hash_get(lcm->table_id_by_vni, vni);
160 clib_warning ("vni %d not associated to a vrf!", vni);
161 return VNET_API_ERROR_INVALID_VALUE;
164 refc = hash_get(lcm->dp_if_refcount_by_vni, vni);
166 /* enable/disable data-plane interface */
169 /* create interface or update refcount */
174 ai->table_id = table_id[0];
175 vnet_lisp_gpe_add_del_iface (ai, 0);
177 /* counts the number of eids in a vni that use the interface */
178 hash_set(lcm->dp_if_refcount_by_vni, vni, 1);
187 /* since this is a remove for an existing eid, the iface should exist */
191 /* remove iface if needed */
196 ai->table_id = table_id[0];
197 vnet_lisp_gpe_add_del_iface (ai, 0);
198 hash_unset (lcm->dp_if_refcount_by_vni, vni);
205 static clib_error_t *
206 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
207 vlib_cli_command_t * cmd)
209 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
210 unformat_input_t _line_input, * line_input = &_line_input;
213 ip_prefix_t * prefp = &gid_address_ippref(&eid);
214 gid_address_t * eids = 0;
215 clib_error_t * error = 0;
216 u8 * locator_set_name = 0;
217 u32 locator_set_index = 0, map_index = 0;
219 vnet_lisp_add_del_mapping_args_t _a, * a = &_a;
223 memset (&eid, 0, sizeof (eid));
224 /* Get a line of input. */
225 if (! unformat_user (input, unformat_line_input, line_input))
228 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
230 if (unformat (line_input, "add"))
232 else if (unformat (line_input, "del"))
234 else if (unformat (line_input, "vni %d", &vni))
235 gid_address_vni (&eid) = vni;
236 else if (unformat (line_input, "eid %U", unformat_ip_prefix, prefp))
240 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
242 p = hash_get_mem(lcm->locator_set_index_by_name, locator_set_name);
245 error = clib_error_return(0, "locator-set %s doesn't exist",
249 locator_set_index = p[0];
253 error = unformat_parse_error(line_input);
257 /* XXX treat batch configuration */
259 gid_address_type (&eid) = GID_ADDR_IP_PREFIX;
262 a->locator_set_index = locator_set_index;
265 rv = vnet_lisp_add_del_local_mapping (a, &map_index);
268 error = clib_error_return(0, "failed to %s eid-table!",
269 is_add ? "add" : "delete");
273 if (locator_set_name)
274 vec_free (locator_set_name);
275 gid_address_free (&a->deid);
279 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
280 .path = "lisp eid-table",
281 .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
282 "locator-set <locator-set>",
283 .function = lisp_add_del_local_eid_command_fn,
287 vnet_lisp_eid_table_map (u32 vni, u32 vrf, u8 is_add)
289 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
290 uword * table_id, * vnip;
292 if (vnet_lisp_enable_disable_status () == 0)
294 clib_warning ("LISP is disabled!");
298 if (vni == 0 || vrf == 0)
300 clib_warning ("can't add/del default vni-vrf mapping!");
304 table_id = hash_get (lcm->table_id_by_vni, vni);
305 vnip = hash_get (lcm->vni_by_table_id, vrf);
309 if (table_id || vnip)
311 clib_warning ("vni %d or vrf %d already used in any vrf/vni "
312 "mapping!", vni, vrf);
315 hash_set (lcm->table_id_by_vni, vni, vrf);
316 hash_set (lcm->vni_by_table_id, vrf, vni);
320 if (!table_id || !vnip)
322 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
323 "mapping!", vni, vrf);
326 hash_unset (lcm->table_id_by_vni, vni);
327 hash_unset (lcm->vni_by_table_id, vrf);
332 static clib_error_t *
333 lisp_eid_table_map_command_fn (vlib_main_t * vm,
334 unformat_input_t * input,
335 vlib_cli_command_t * cmd)
338 u32 vni = 0, vrf = 0;
339 unformat_input_t _line_input, * line_input = &_line_input;
341 /* Get a line of input. */
342 if (! unformat_user (input, unformat_line_input, line_input))
345 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
347 if (unformat (line_input, "del"))
349 else if (unformat (line_input, "vni %d", &vni))
351 else if (unformat (line_input, "vrf %d", &vrf))
355 return unformat_parse_error (line_input);
358 vnet_lisp_eid_table_map (vni, vrf, is_add);
362 VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
363 .path = "lisp eid-table map",
364 .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf>",
365 .function = lisp_eid_table_map_command_fn,
369 lisp_add_del_negative_static_mapping (gid_address_t * deid,
370 vnet_lisp_add_del_locator_set_args_t * ls, u8 action, u8 is_add)
375 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
377 vnet_lisp_add_del_mapping_args_t _dm_args, * dm_args = &_dm_args;
379 u32 ls_index = 0, dst_map_index;
380 vnet_lisp_gpe_add_del_iface_args_t _ai, *ai = &_ai;
382 memset (dm_args, 0, sizeof (dm_args[0]));
383 u32 vni = gid_address_vni (deid);
384 refc = hash_get (lcm->dp_if_refcount_by_vni, vni);
386 p = hash_get (lcm->table_id_by_vni, vni);
389 clib_warning ("vni %d not associated to a vrf!", vni);
390 return VNET_API_ERROR_INVALID_VALUE;
395 vnet_lisp_add_del_locator_set (ls, &ls_index);
397 gid_address_copy (&dm_args->deid, deid);
399 dm_args->action = action;
400 dm_args->locator_set_index = ls_index;
402 /* create interface or update refcount */
405 vnet_lisp_gpe_add_del_iface_args_t _ai, *ai = &_ai;
409 vnet_lisp_gpe_add_del_iface (ai, 0);
411 /* counts the number of eids in a vni that use the interface */
412 hash_set (lcm->dp_if_refcount_by_vni, vni, 1);
417 rv = vnet_lisp_add_del_local_mapping (dm_args, &dst_map_index);
419 add_fwd_entry (lcm, lcm->pitr_map_index, dst_map_index);
423 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, deid);
426 clib_warning ("eid %U marked for removal, but not found in "
427 "map-cache!", unformat_gid_address, deid);
428 return VNET_API_ERROR_INVALID_VALUE;
431 /* delete forwarding entry */
432 del_fwd_entry (lcm, 0, mi);
435 gid_address_copy (&dm_args->deid, deid);
436 map = pool_elt_at_index (lcm->mapping_pool, mi);
437 dm_args->locator_set_index = map->locator_set_index;
439 /* delete mapping associated to fwd entry */
440 vnet_lisp_add_del_mapping (dm_args, 0);
442 refc = hash_get (lcm->dp_if_refcount_by_vni, vni);
446 /* remove iface if needed */
452 vnet_lisp_gpe_add_del_iface (ai, 0);
453 hash_unset (lcm->dp_if_refcount_by_vni, vni);
460 * Adds/removes/updates static remote mapping.
462 * This function also modifies forwarding entries if needed.
464 * @param deid destination EID
465 * @param seid source EID
466 * @param rlocs vector of remote locators
467 * @param action action for negative map-reply
468 * @param is_add add mapping if non-zero, delete otherwise
469 * @param del_all if set, delete all remote mappings
470 * @return return code
473 vnet_lisp_add_del_remote_mapping (gid_address_t * deid, gid_address_t * seid,
474 ip_address_t * rlocs, u8 action, u8 is_add,
477 vnet_lisp_add_del_mapping_args_t _dm_args, * dm_args = &_dm_args;
478 vnet_lisp_add_del_mapping_args_t _sm_args, * sm_args = &_sm_args;
479 vnet_lisp_add_del_locator_set_args_t _ls, * ls = &_ls;
480 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
481 u32 mi, ls_index = 0, dst_map_index, src_map_index;
486 if (vnet_lisp_enable_disable_status() == 0)
488 clib_warning ("LISP is disabled!");
489 return VNET_API_ERROR_LISP_DISABLED;
493 return vnet_lisp_clear_all_remote_mappings ();
495 memset (sm_args, 0, sizeof (sm_args[0]));
496 memset (dm_args, 0, sizeof (dm_args[0]));
497 memset (ls, 0, sizeof (ls[0]));
499 /* prepare remote locator set */
500 vec_foreach (dl, rlocs)
502 memset (&loc, 0, sizeof (loc));
503 gid_address_ip (&loc.address) = dl[0];
504 vec_add1 (ls->locators, loc);
506 src_map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
508 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, deid);
515 /* process a negative mapping */
516 if (0 == vec_len (rlocs))
517 return lisp_add_del_negative_static_mapping (deid, ls,
520 if ((u32)~0 == src_map_index)
522 clib_warning ("seid %U not found!", format_gid_address, seid);
528 clib_warning ("deid %U marked for removal but not "
529 "found!", format_gid_address, deid);
533 vnet_lisp_add_del_locator_set (ls, &ls_index);
536 gid_address_copy (&dm_args->deid, deid);
538 dm_args->action = action;
539 dm_args->locator_set_index = ls_index;
540 vnet_lisp_add_del_mapping (dm_args, &dst_map_index);
543 add_fwd_entry (lcm, src_map_index, dst_map_index);
550 /* delete forwarding entry */
551 del_fwd_entry (lcm, 0, mi);
553 gid_address_copy (&dm_args->deid, deid);
554 mapping_t * map = pool_elt_at_index (lcm->mapping_pool, mi);
555 dm_args->locator_set_index = map->locator_set_index;
557 /* delete mapping associated to fwd entry */
558 vnet_lisp_add_del_mapping (dm_args, 0);
561 ls->index = map->locator_set_index;
562 /* delete locator set */
563 vnet_lisp_add_del_locator_set (ls, 0);
565 /* update existing locator set */
568 if ((u32)~0 == src_map_index)
570 clib_warning ("seid %U not found!", format_gid_address, seid);
575 locator_set_t * old_ls;
576 old_map = pool_elt_at_index (lcm->mapping_pool, mi);
578 /* update mapping attributes */
579 old_map->action = action;
581 old_ls = pool_elt_at_index(lcm->locator_set_pool,
582 old_map->locator_set_index);
583 if (compare_locators (lcm, old_ls->locator_indices,
586 /* set locator-set index to overwrite */
588 ls->index = old_map->locator_set_index;
589 vnet_lisp_add_del_locator_set (ls, 0);
590 add_fwd_entry (lcm, src_map_index, mi);
597 vec_free (ls->locators);
602 vnet_lisp_clear_all_remote_mappings (void)
605 u32 mi, * map_indices = 0, * map_indexp;
606 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
607 vnet_lisp_add_del_mapping_args_t _dm_args, * dm_args = &_dm_args;
608 vnet_lisp_add_del_locator_set_args_t _ls, * ls = &_ls;
610 pool_foreach_index (mi, lcm->mapping_pool,
612 vec_add1 (map_indices, mi);
615 vec_foreach (map_indexp, map_indices)
617 mapping_t * map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
620 del_fwd_entry (lcm, 0, map_indexp[0]);
623 gid_address_copy (&dm_args->deid, &map->eid);
624 dm_args->locator_set_index = map->locator_set_index;
626 /* delete mapping associated to fwd entry */
627 vnet_lisp_add_del_mapping (dm_args, 0);
631 ls->index = map->locator_set_index;
632 /* delete locator set */
633 rv = vnet_lisp_add_del_locator_set (ls, 0);
641 vec_free (map_indices);
646 * Handler for add/del remote mapping CLI.
648 * @param vm vlib context
649 * @param input input from user
651 * @return pointer to clib error structure
653 static clib_error_t *
654 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
655 unformat_input_t * input,
656 vlib_cli_command_t * cmd)
658 clib_error_t * error = 0;
659 unformat_input_t _line_input, * line_input = &_line_input;
660 u8 is_add = 1, del_all = 0;
661 ip_address_t rloc, * rlocs = 0;
662 ip_prefix_t * deid_ippref, * seid_ippref;
663 gid_address_t seid, deid;
664 u8 deid_set = 0, seid_set = 0;
666 u32 vni, action = ~0;
668 /* Get a line of input. */
669 if (! unformat_user (input, unformat_line_input, line_input))
672 memset (&deid, 0, sizeof (deid));
673 memset (&seid, 0, sizeof (seid));
675 seid_ippref = &gid_address_ippref(&seid);
676 deid_ippref = &gid_address_ippref(&deid);
678 gid_address_type (&deid) = GID_ADDR_IP_PREFIX;
679 gid_address_type (&seid) = GID_ADDR_IP_PREFIX;
681 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
683 if (unformat (line_input, "del-all"))
685 else if (unformat (line_input, "del"))
687 else if (unformat (line_input, "add"))
689 else if (unformat (line_input, "deid %U",
690 unformat_ip_prefix, deid_ippref))
694 else if (unformat (line_input, "vni %u", &vni))
696 gid_address_vni (&seid) = vni;
697 gid_address_vni (&deid) = vni;
699 else if (unformat (line_input, "seid %U",
700 unformat_ip_prefix, seid_ippref))
704 else if (unformat (line_input, "rloc %U", unformat_ip_address, &rloc))
705 vec_add1 (rlocs, rloc);
706 else if (unformat (line_input, "action %s", &s))
708 if (!strcmp ((char *)s, "no-action"))
709 action = ACTION_NONE;
710 if (!strcmp ((char *)s, "natively-forward"))
711 action = ACTION_NATIVELY_FORWARDED;
712 if (!strcmp ((char *)s, "send-map-request"))
713 action = ACTION_SEND_MAP_REQUEST;
714 else if (!strcmp ((char *)s, "drop"))
715 action = ACTION_DROP;
718 clib_warning ("invalid action: '%s'", s);
724 clib_warning ("parse error");
733 clib_warning ("missing deid!");
737 /* if seid not set, make sure the ip version is the same as that of the
738 * deid. This ensures the seid to be configured will be either 0/0 or
741 ip_prefix_version(seid_ippref) = ip_prefix_version(deid_ippref);
744 (ip_prefix_version (deid_ippref) != ip_prefix_version(seid_ippref)))
746 clib_warning ("source and destination EIDs are not"
747 " in the same IP family!");
751 if (is_add && (~0 == action)
752 && 0 == vec_len (rlocs))
754 clib_warning ("no action set for negative map-reply!");
759 int rv = vnet_lisp_add_del_remote_mapping (&deid, &seid, rlocs,
760 action, is_add, del_all);
762 clib_warning ("failed to %s remote mapping!",
763 is_add ? "add" : "delete");
766 unformat_free (line_input);
772 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) = {
773 .path = "lisp remote-mapping",
774 .short_help = "lisp remote-mapping add|del [del-all] vni <vni>"
775 "deid <dest-eid> seid <src-eid> [action <no-action|natively-forward|"
776 "send-map-request|drop>] rloc <dst-locator> [rloc <dst-locator> ... ]",
777 .function = lisp_add_del_remote_mapping_command_fn,
780 static clib_error_t *
781 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
782 unformat_input_t * input,
783 vlib_cli_command_t * cmd)
786 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
788 vec_foreach (addr, lcm->map_resolvers)
790 vlib_cli_output (vm, "%U", format_ip_address, addr);
795 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
796 .path = "show lisp map-resolvers",
797 .short_help = "show lisp map-resolvers",
798 .function = lisp_show_map_resolvers_command_fn,
802 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
804 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
805 u32 locator_set_index = ~0;
809 if (vnet_lisp_enable_disable_status () == 0)
811 clib_warning ("LISP is disabled!");
812 return VNET_API_ERROR_LISP_DISABLED;
815 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
818 clib_warning ("locator-set %v doesn't exist", locator_set_name);
821 locator_set_index = p[0];
825 pool_get (lcm->mapping_pool, m);
826 m->locator_set_index = locator_set_index;
828 lcm->pitr_map_index = m - lcm->mapping_pool;
830 /* enable pitr mode */
835 /* remove pitr mapping */
836 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
838 /* disable pitr mode */
844 static clib_error_t *
845 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
846 unformat_input_t * input,
847 vlib_cli_command_t * cmd)
849 u8 locator_name_set = 0;
850 u8 * locator_set_name = 0;
852 unformat_input_t _line_input, * line_input = &_line_input;
853 clib_error_t * error = 0;
856 /* Get a line of input. */
857 if (! unformat_user (input, unformat_line_input, line_input))
860 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
862 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
863 locator_name_set = 1;
864 else if (unformat (line_input, "disable"))
867 return clib_error_return (0, "parse error");
870 if (!locator_name_set)
872 clib_warning ("No locator set specified!");
875 rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
878 error = clib_error_return(0, "failed to %s pitr!",
879 is_add ? "add" : "delete");
883 if (locator_set_name)
884 vec_free (locator_set_name);
888 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
890 .short_help = "lisp pitr [disable] ls <locator-set-name>",
891 .function = lisp_pitr_set_locator_set_command_fn,
894 static clib_error_t *
895 lisp_show_local_eid_table_command_fn (vlib_main_t * vm,
896 unformat_input_t * input,
897 vlib_cli_command_t * cmd)
899 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
902 vlib_cli_output (vm, "%=30s%=16s", "EID", "Locator");
903 pool_foreach (mapit, lcm->mapping_pool,
906 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
907 mapit->locator_set_index);
908 vlib_cli_output (vm, "%-30U%16v", format_gid_address, &mapit->eid,
916 VLIB_CLI_COMMAND (lisp_cp_show_local_eid_table_command) = {
917 .path = "show lisp eid-table",
918 .short_help = "Shows local EID table",
919 .function = lisp_show_local_eid_table_command_fn,
922 /* cleans locator to locator-set data and removes locators not part of
925 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
927 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
928 locator_set_t * ls = pool_elt_at_index(lcm->locator_set_pool, lsi);
929 for (i = 0; i < vec_len(ls->locator_indices); i++)
931 loc_indexp = vec_elt_at_index(ls->locator_indices, i);
932 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
934 for (j = 0; j < vec_len(ls_indexes[0]); j++)
936 ls_indexp = vec_elt_at_index(ls_indexes[0], j);
937 if (ls_indexp[0] == lsi)
941 /* delete index for removed locator-set*/
942 vec_del1(ls_indexes[0], j);
944 /* delete locator if it's part of no locator-set */
945 if (vec_len (ls_indexes[0]) == 0)
947 pool_put_index (lcm->locator_pool, loc_indexp[0]);
948 vec_add1 (to_be_deleted, i);
954 for (i = 0; i < vec_len (to_be_deleted); i++)
956 loc_indexp = vec_elt_at_index (to_be_deleted, i);
957 vec_del1 (ls->locator_indices, loc_indexp[0]);
959 vec_free (to_be_deleted);
964 uword *get_locator_set_index(vnet_lisp_add_del_locator_set_args_t * a,
967 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
972 /* find locator-set */
975 p = hash_get_mem(lcm->locator_set_index_by_name, a->name);
986 int is_locator_in_locator_set(lisp_cp_main_t * lcm, locator_set_t * ls,
995 vec_foreach(locit, ls->locator_indices)
997 itloc = pool_elt_at_index(lcm->locator_pool, locit[0]);
998 if (itloc->sw_if_index == loc->sw_if_index ||
999 !gid_address_cmp(&itloc->address, &loc->address))
1001 clib_warning("Duplicate locator");
1002 return VNET_API_ERROR_VALUE_EXIST;
1010 void remove_locator_from_locator_set(locator_set_t * ls, u32 * locit,
1011 u32 ls_index, u32 loc_id)
1013 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1014 u32 ** ls_indexes = NULL;
1017 ASSERT(locit != NULL);
1019 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
1021 pool_put_index(lcm->locator_pool, locit[0]);
1022 vec_del1(ls->locator_indices, loc_id);
1023 vec_del1(ls_indexes[0], ls_index);
1027 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1028 locator_set_t * ls, u32 * ls_result)
1030 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1031 locator_t * loc = NULL, *itloc = NULL;
1032 uword _p = (u32)~0, * p = &_p;
1033 u32 loc_index = ~0, ls_index = ~0, * locit = NULL, ** ls_indexes = NULL;
1039 if (vnet_lisp_enable_disable_status () == 0)
1041 clib_warning ("LISP is disabled!");
1042 return VNET_API_ERROR_LISP_DISABLED;
1045 p = get_locator_set_index(a, p);
1048 clib_warning("locator-set %v doesn't exist", a->name);
1049 return VNET_API_ERROR_INVALID_ARGUMENT;
1054 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1057 clib_warning("locator-set %d to be overwritten doesn't exist!",
1059 return VNET_API_ERROR_INVALID_ARGUMENT;
1067 ls_result[0] = p[0];
1069 /* allocate locators */
1070 vec_foreach (itloc, a->locators)
1072 ret = is_locator_in_locator_set(lcm, ls, itloc);
1078 pool_get(lcm->locator_pool, loc);
1080 loc_index = loc - lcm->locator_pool;
1082 vec_add1(ls->locator_indices, loc_index);
1084 vec_validate (lcm->locator_to_locator_sets, loc_index);
1085 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
1087 vec_add1(ls_indexes[0], ls_index);
1094 itloc = a->locators;
1096 vec_foreach (locit, ls->locator_indices)
1098 loc = pool_elt_at_index(lcm->locator_pool, locit[0]);
1100 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1102 remove_locator_from_locator_set(ls, locit,
1105 if (0 == loc->local &&
1106 !gid_address_cmp(&loc->address, &itloc->address))
1108 remove_locator_from_locator_set(ls, locit,
1120 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
1123 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1125 uword _p = (u32)~0, * p = &_p;
1130 if (vnet_lisp_enable_disable_status () == 0)
1132 clib_warning ("LISP is disabled!");
1133 return VNET_API_ERROR_LISP_DISABLED;
1138 p = get_locator_set_index(a, p);
1141 if (p && p[0] != (u32)~0)
1143 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1146 clib_warning("locator-set %d to be overwritten doesn't exist!",
1151 /* clean locator to locator-set vectors and remove locators if
1152 * they're not part of another locator-set */
1153 clean_locator_to_locator_set (lcm, p[0]);
1155 /* remove locator indices from locator set */
1156 vec_free(ls->locator_indices);
1161 ls_result[0] = p[0];
1163 /* new locator-set */
1166 pool_get(lcm->locator_set_pool, ls);
1167 memset(ls, 0, sizeof(*ls));
1168 ls_index = ls - lcm->locator_set_pool;
1172 ls->name = vec_dup(a->name);
1174 if (!lcm->locator_set_index_by_name)
1175 lcm->locator_set_index_by_name = hash_create_vec(
1176 /* size */0, sizeof(ls->name[0]), sizeof(uword));
1177 hash_set_mem(lcm->locator_set_index_by_name, ls->name, ls_index);
1179 /* mark as local locator-set */
1180 vec_add1(lcm->local_locator_set_indexes, ls_index);
1182 ls->local = a->local;
1184 ls_result[0] = ls_index;
1187 ret = vnet_lisp_add_del_locator(a, ls, NULL);
1195 p = get_locator_set_index(a, p);
1198 clib_warning("locator-set %v doesn't exists", a->name);
1202 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1205 clib_warning("locator-set with index %d doesn't exists", p[0]);
1209 if (lcm->mreq_itr_rlocs == p[0])
1211 clib_warning ("Can't delete the locator-set used to constrain "
1212 "the itr-rlocs in map-requests!");
1216 if (vec_len(lcm->locator_set_to_eids) != 0)
1218 eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids, p[0]);
1219 if (vec_len(eid_indexes[0]) != 0)
1221 clib_warning ("Can't delete a locator that supports a mapping!");
1226 /* clean locator to locator-sets data */
1227 clean_locator_to_locator_set (lcm, p[0]);
1233 vec_foreach_index(it, lcm->local_locator_set_indexes)
1235 lsi = vec_elt(lcm->local_locator_set_indexes, it);
1238 vec_del1(lcm->local_locator_set_indexes, it);
1242 hash_unset_mem(lcm->locator_set_index_by_name, ls->name);
1245 vec_free(ls->locator_indices);
1246 pool_put(lcm->locator_set_pool, ls);
1252 vnet_lisp_enable_disable (u8 is_enabled)
1254 vnet_lisp_gpe_add_del_iface_args_t _ai, * ai= &_ai;
1255 uword * table_id, * refc;
1257 clib_error_t * error = 0;
1258 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1259 vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
1261 a->is_en = is_enabled;
1262 error = vnet_lisp_gpe_enable_disable (a);
1265 return clib_error_return (0, "failed to %s data-plane!",
1266 a->is_en ? "enable" : "disable");
1271 /* enable all ifaces */
1272 for (i = 0; i < vec_len (lcm->local_mappings_indexes); i++)
1274 mapping_t * m = vec_elt_at_index (lcm->mapping_pool, i);
1276 ai->vni = gid_address_vni (&m->eid);
1278 refc = hash_get (lcm->dp_if_refcount_by_vni, ai->vni);
1281 table_id = hash_get (lcm->table_id_by_vni, ai->vni);
1284 ai->table_id = table_id[0];
1285 /* enables interface and adds defaults */
1286 vnet_lisp_gpe_add_del_iface (ai, 0);
1289 return clib_error_return (0, "no table_id found for vni %u!",
1292 hash_set (lcm->dp_if_refcount_by_vni, ai->vni, 1);
1302 /* clear refcount table */
1303 hash_free (lcm->dp_if_refcount_by_vni);
1304 hash_free (lcm->fwd_entry_by_mapping_index);
1305 pool_free (lcm->fwd_entry_pool);
1308 /* update global flag */
1309 lcm->is_enabled = is_enabled;
1314 static clib_error_t *
1315 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
1316 vlib_cli_command_t * cmd)
1318 unformat_input_t _line_input, * line_input = &_line_input;
1322 /* Get a line of input. */
1323 if (! unformat_user (input, unformat_line_input, line_input))
1326 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1328 if (unformat (line_input, "enable"))
1333 else if (unformat (line_input, "disable"))
1337 return clib_error_return (0, "parse error: '%U'",
1338 format_unformat_error, line_input);
1343 return clib_error_return (0, "state not set");
1345 return vnet_lisp_enable_disable (is_enabled);
1348 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
1350 .short_help = "lisp [enable|disable]",
1351 .function = lisp_enable_disable_command_fn,
1355 vnet_lisp_enable_disable_status (void)
1357 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1358 return lcm->is_enabled;
1362 format_lisp_status (u8 * s, va_list * args)
1364 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1365 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
1368 static clib_error_t *
1369 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
1370 vlib_cli_command_t * cmd)
1373 msg = format (msg, "feature: %U\ngpe: %U\n",
1374 format_lisp_status, format_vnet_lisp_gpe_status);
1375 vlib_cli_output (vm, "%v", msg);
1380 VLIB_CLI_COMMAND (lisp_show_status_command) = {
1381 .path = "show lisp status",
1382 .short_help = "show lisp status",
1383 .function = lisp_show_status_command_fn,
1386 static clib_error_t *
1387 lisp_show_eid_table_map_command_fn (vlib_main_t * vm, unformat_input_t * input,
1388 vlib_cli_command_t * cmd)
1391 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1393 vlib_cli_output (vm, "%=10s%=10s", "VNI", "VRF");
1394 hash_foreach_pair (p, lcm->table_id_by_vni,
1396 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
1401 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
1402 .path = "show lisp eid-table map",
1403 .short_help = "show lisp eid-table vni to vrf mappings",
1404 .function = lisp_show_eid_table_map_command_fn,
1407 static clib_error_t *
1408 lisp_add_del_locator_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
1409 vlib_cli_command_t * cmd)
1411 lisp_gpe_main_t * lgm = &lisp_gpe_main;
1412 vnet_main_t * vnm = lgm->vnet_main;
1413 unformat_input_t _line_input, * line_input = &_line_input;
1415 clib_error_t * error = 0;
1416 u8 * locator_set_name = 0;
1417 locator_t locator, * locators = 0;
1418 vnet_lisp_add_del_locator_set_args_t _a, * a = &_a;
1422 memset(&locator, 0, sizeof(locator));
1423 memset(a, 0, sizeof(a[0]));
1425 /* Get a line of input. */
1426 if (! unformat_user (input, unformat_line_input, line_input))
1429 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1431 if (unformat (line_input, "add %_%v%_", &locator_set_name))
1433 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
1435 else if (unformat (line_input, "iface %U p %d w %d",
1436 unformat_vnet_sw_interface, vnm, &locator.sw_if_index,
1437 &locator.priority, &locator.weight))
1440 vec_add1(locators, locator);
1444 error = unformat_parse_error(line_input);
1449 a->name = locator_set_name;
1450 a->locators = locators;
1454 rv = vnet_lisp_add_del_locator_set(a, &ls_index);
1457 error = clib_error_return(0, "failed to %s locator-set!",
1458 is_add ? "add" : "delete");
1463 if (locator_set_name)
1464 vec_free (locator_set_name);
1468 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
1469 .path = "lisp locator-set",
1470 .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
1471 "p <priority> w <weight>]",
1472 .function = lisp_add_del_locator_set_command_fn,
1475 static clib_error_t *
1476 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
1477 vlib_cli_command_t * cmd)
1479 lisp_gpe_main_t * lgm = &lisp_gpe_main;
1480 vnet_main_t * vnm = lgm->vnet_main;
1481 unformat_input_t _line_input, * line_input = &_line_input;
1483 clib_error_t * error = 0;
1484 u8 * locator_set_name = 0;
1485 u8 locator_set_name_set = 0;
1486 locator_t locator, * locators = 0;
1487 vnet_lisp_add_del_locator_set_args_t _a, * a = &_a;
1490 memset(&locator, 0, sizeof(locator));
1491 memset(a, 0, sizeof(a[0]));
1493 /* Get a line of input. */
1494 if (! unformat_user (input, unformat_line_input, line_input))
1497 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1499 if (unformat (line_input, "add"))
1501 else if (unformat (line_input, "del"))
1503 else if (unformat(line_input, "locator-set %_%v%_", &locator_set_name))
1504 locator_set_name_set = 1;
1505 else if (unformat (line_input, "iface %U p %d w %d",
1506 unformat_vnet_sw_interface, vnm, &locator.sw_if_index,
1507 &locator.priority, &locator.weight))
1510 vec_add1(locators, locator);
1514 error = unformat_parse_error(line_input);
1519 if (!locator_set_name_set)
1521 error = clib_error_return(0, "locator_set name not set!");
1525 a->name = locator_set_name;
1526 a->locators = locators;
1530 vnet_lisp_add_del_locator(a, 0, &ls_index);
1534 vec_free (locator_set_name);
1538 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
1539 .path = "lisp locator",
1540 .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
1541 "p <priority> w <weight>",
1542 .function = lisp_add_del_locator_in_set_command_fn,
1545 static clib_error_t *
1546 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
1547 unformat_input_t * input,
1548 vlib_cli_command_t * cmd)
1550 locator_set_t * lsit;
1553 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1555 vlib_cli_output (vm, "%=20s%=16s%=16s%=16s", "Locator-set", "Locator",
1556 "Priority", "Weight");
1557 pool_foreach (lsit, lcm->locator_set_pool,
1561 msg = format (msg, "%=16v", lsit->name);
1562 vec_foreach (locit, lsit->locator_indices)
1566 msg = format (msg, "%16s", " ");
1568 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1570 msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
1573 msg = format (msg, "%16U%16d%16d\n", format_ip_address,
1574 gid_address_ip(&loc->address), loc->priority,
1578 vlib_cli_output (vm, "%v", msg);
1584 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
1585 .path = "show lisp locator-set",
1586 .short_help = "Shows locator-sets",
1587 .function = lisp_cp_show_locator_sets_command_fn,
1591 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
1593 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1594 ip_address_t * addr;
1597 if (vnet_lisp_enable_disable_status () == 0)
1599 clib_warning ("LISP is disabled!");
1600 return VNET_API_ERROR_LISP_DISABLED;
1605 vec_foreach(addr, lcm->map_resolvers)
1607 if (!ip_address_cmp (addr, &a->address))
1609 clib_warning("map-resolver %U already exists!", format_ip_address,
1614 vec_add1(lcm->map_resolvers, a->address);
1618 for (i = 0; i < vec_len(lcm->map_resolvers); i++)
1620 addr = vec_elt_at_index(lcm->map_resolvers, i);
1621 if (!ip_address_cmp (addr, &a->address))
1623 vec_delete(lcm->map_resolvers, 1, i);
1631 static clib_error_t *
1632 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
1633 unformat_input_t * input,
1634 vlib_cli_command_t * cmd)
1636 unformat_input_t _line_input, * line_input = &_line_input;
1638 ip_address_t ip_addr;
1639 clib_error_t * error = 0;
1641 vnet_lisp_add_del_map_resolver_args_t _a, * a = &_a;
1643 /* Get a line of input. */
1644 if (! unformat_user (input, unformat_line_input, line_input))
1647 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1649 if (unformat (line_input, "add"))
1651 else if (unformat (line_input, "del"))
1653 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
1657 error = unformat_parse_error(line_input);
1662 a->address = ip_addr;
1663 rv = vnet_lisp_add_del_map_resolver (a);
1666 error = clib_error_return(0, "failed to %s map-resolver!",
1667 is_add ? "add" : "delete");
1674 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
1675 .path = "lisp map-resolver",
1676 .short_help = "lisp map-resolver add/del <ip_address>",
1677 .function = lisp_add_del_map_resolver_command_fn,
1681 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
1683 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1686 //TODO: Wait for merge https://gerrit.fd.io/r/#/c/1427/
1687 // if (vnet_lisp_enable_disable_status () == 0)
1689 // clib_warning ("LISP is disabled!");
1690 // return VNET_API_ERROR_LISP_DISABLED;
1695 p = hash_get_mem(lcm->locator_set_index_by_name, a->locator_set_name);
1698 clib_warning("locator-set %v doesn't exist", a->locator_set_name);
1699 return VNET_API_ERROR_INVALID_ARGUMENT;
1702 lcm->mreq_itr_rlocs = p[0];
1706 lcm->mreq_itr_rlocs = ~0;
1712 static clib_error_t *
1713 lisp_add_del_mreq_itr_rlocs_command_fn(vlib_main_t * vm,
1714 unformat_input_t * input,
1715 vlib_cli_command_t * cmd)
1717 unformat_input_t _line_input, * line_input = &_line_input;
1719 u8 * locator_set_name = 0;
1720 clib_error_t * error = 0;
1722 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, * a = &_a;
1724 /* Get a line of input. */
1725 if (! unformat_user (input, unformat_line_input, line_input))
1728 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1730 if (unformat (line_input, "del"))
1732 else if (unformat (line_input, "add %s", &locator_set_name))
1736 error = unformat_parse_error(line_input);
1742 a->locator_set_name = locator_set_name;
1743 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
1746 error = clib_error_return(0, "failed to %s map-request itr-rlocs!",
1747 is_add ? "add" : "delete");
1750 vec_free(locator_set_name);
1757 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
1758 .path = "lisp map-request itr-rlocs",
1759 .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
1760 .function = lisp_add_del_mreq_itr_rlocs_command_fn,
1763 static clib_error_t *
1764 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
1765 unformat_input_t * input,
1766 vlib_cli_command_t * cmd)
1768 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1769 locator_set_t * loc_set;
1771 vlib_cli_output (vm, "%=20s", "itr-rlocs");
1773 if (~0 == lcm->mreq_itr_rlocs)
1778 loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
1780 vlib_cli_output (vm, "%=20s", loc_set->name);
1785 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
1786 .path = "show lisp map-request itr-rlocs",
1787 .short_help = "Shows map-request itr-rlocs",
1788 .function = lisp_show_mreq_itr_rlocs_command_fn,
1791 /* Statistics (not really errors) */
1792 #define foreach_lisp_cp_lookup_error \
1794 _(MAP_REQUESTS_SENT, "map-request sent")
1796 static char * lisp_cp_lookup_error_strings[] = {
1797 #define _(sym,string) string,
1798 foreach_lisp_cp_lookup_error
1804 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
1805 foreach_lisp_cp_lookup_error
1807 LISP_CP_LOOKUP_N_ERROR,
1808 } lisp_cp_lookup_error_t;
1812 LISP_CP_LOOKUP_NEXT_DROP,
1813 LISP_CP_LOOKUP_NEXT_IP4_LOOKUP,
1814 LISP_CP_LOOKUP_NEXT_IP6_LOOKUP,
1815 LISP_CP_LOOKUP_N_NEXT,
1816 } lisp_cp_lookup_next_t;
1820 gid_address_t dst_eid;
1821 ip_address_t map_resolver_ip;
1822 } lisp_cp_lookup_trace_t;
1825 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
1827 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1828 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1829 lisp_cp_lookup_trace_t * t = va_arg (*args, lisp_cp_lookup_trace_t *);
1831 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
1832 format_ip_address, &t->map_resolver_ip, format_gid_address,
1837 ip_interface_address_t *
1838 ip_interface_get_first_interface_address (ip_lookup_main_t *lm, u32 sw_if_index,
1841 vnet_main_t *vnm = vnet_get_main ();
1842 vnet_sw_interface_t * swif = vnet_get_sw_interface (vnm, sw_if_index);
1843 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
1844 sw_if_index = swif->unnumbered_sw_if_index;
1846 (vec_len((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
1847 vec_elt((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
1849 return pool_elt_at_index((lm)->if_address_pool, ia);
1853 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
1856 ip_interface_address_t * ia;
1858 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
1861 return ip_interface_address_get_address (lm, ia);
1865 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
1866 u8 version, ip_address_t * result)
1868 ip_lookup_main_t * lm;
1871 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
1872 addr = ip_interface_get_first_address (lm, sw_if_index, version);
1876 ip_address_set (result, addr, version);
1881 ip_fib_lookup_with_table (lisp_cp_main_t * lcm, u32 fib_index,
1884 if (ip_addr_version (dst) == IP4)
1885 return ip4_fib_lookup_with_table (lcm->im4, fib_index, &ip_addr_v4(dst),
1888 return ip6_fib_lookup_with_table (lcm->im6, fib_index, &ip_addr_v6(dst));
1892 ip_fib_get_egress_iface_for_dst_with_lm (lisp_cp_main_t * lcm,
1894 ip_lookup_main_t * lm)
1897 ip_adjacency_t * adj;
1899 adj_index = ip_fib_lookup_with_table (lcm, 0, dst);
1900 adj = ip_get_adjacency (lm, adj_index);
1905 /* we only want outgoing routes */
1906 if (adj->lookup_next_index != IP_LOOKUP_NEXT_ARP
1907 && adj->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
1910 return adj->rewrite_header.sw_if_index;
1914 * Find the sw_if_index of the interface that would be used to egress towards
1918 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
1920 ip_lookup_main_t * lm;
1922 lm = ip_addr_version (dst) == IP4 ?
1923 &lcm->im4->lookup_main : &lcm->im6->lookup_main;
1925 return ip_fib_get_egress_iface_for_dst_with_lm (lcm, dst, lm);
1929 * Find first IP of the interface that would be used to egress towards dst.
1930 * Returns 1 if the address is found 0 otherwise.
1933 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
1934 ip_address_t * result)
1937 ip_lookup_main_t * lm;
1941 ASSERT(result != 0);
1943 ipver = ip_addr_version(dst);
1945 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
1946 si = ip_fib_get_egress_iface_for_dst_with_lm (lcm, dst, lm);
1951 /* find the first ip address */
1952 addr = ip_interface_get_first_address (lm, si, ipver);
1956 ip_address_set (result, addr, ipver);
1961 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
1962 ip_address_t * sloc)
1964 ip_address_t * mrit;
1966 if (vec_len(lcm->map_resolvers) == 0)
1968 clib_warning("No map-resolver configured");
1972 /* find the first mr ip we have a route to and the ip of the
1973 * iface that has a route to it */
1974 vec_foreach(mrit, lcm->map_resolvers)
1976 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, mrit, sloc)) {
1977 ip_address_copy(mr_ip, mrit);
1982 clib_warning("Can't find map-resolver and local interface ip!");
1986 static gid_address_t *
1987 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
1993 ip_interface_address_t * ia = 0;
1994 gid_address_t gid_data, * gid = &gid_data;
1995 gid_address_t * rlocs = 0;
1996 ip_prefix_t * ippref = &gid_address_ippref (gid);
1997 ip_address_t * rloc = &ip_prefix_addr (ippref);
1999 memset (gid, 0, sizeof (gid[0]));
2000 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
2001 for (i = 0; i < vec_len(loc_set->locator_indices); i++)
2003 loc_indexp = vec_elt_at_index(loc_set->locator_indices, i);
2004 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2006 /* Add ipv4 locators first TODO sort them */
2007 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2008 loc->sw_if_index, 1 /* unnumbered */,
2010 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2011 ip_address_set (rloc, addr, IP4);
2012 ip_prefix_len (ippref) = 32;
2013 vec_add1 (rlocs, gid[0]);
2016 /* Add ipv6 locators */
2017 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2018 loc->sw_if_index, 1 /* unnumbered */,
2020 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2021 ip_address_set (rloc, addr, IP6);
2022 ip_prefix_len (ippref) = 128;
2023 vec_add1 (rlocs, gid[0]);
2029 static vlib_buffer_t *
2030 build_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
2031 gid_address_t * seid, gid_address_t * deid,
2032 locator_set_t * loc_set, ip_address_t * mr_ip,
2033 ip_address_t * sloc, u8 is_smr_invoked,
2034 u64 *nonce_res, u32 * bi_res)
2038 gid_address_t * rlocs = 0;
2040 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2042 clib_warning ("Can't allocate buffer for Map-Request!");
2046 b = vlib_get_buffer (vm, bi);
2048 /* leave some space for the encap headers */
2049 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2052 rlocs = build_itr_rloc_list (lcm, loc_set);
2055 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked, nonce_res);
2057 /* push ecm: udp-ip-lisp */
2058 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2060 /* push outer ip header */
2061 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2071 send_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
2072 gid_address_t * seid, gid_address_t * deid,
2075 u32 next_index, bi = 0, * to_next, map_index;
2079 locator_set_t * loc_set;
2081 pending_map_request_t * pmr;
2082 ip_address_t mr_ip, sloc;
2085 /* get locator-set for seid */
2086 if (!lcm->lisp_pitr)
2088 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2089 if (map_index == ~0)
2091 clib_warning("No local mapping found in eid-table for %U!",
2092 format_gid_address, seid);
2096 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2100 clib_warning("Mapping found for src eid %U is not marked as local!",
2101 format_gid_address, seid);
2104 ls_index = map->locator_set_index;
2108 map_index = lcm->pitr_map_index;
2109 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
2110 ls_index = map->locator_set_index;
2113 /* overwrite locator set if map-request itr-rlocs configured */
2114 if (~0 != lcm->mreq_itr_rlocs)
2116 ls_index = lcm->mreq_itr_rlocs;
2119 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
2121 /* get local iface ip to use in map-request */
2122 if (0 == get_mr_and_local_iface_ip (lcm, &mr_ip, &sloc))
2125 /* build the encapsulated map request */
2126 b = build_encapsulated_map_request (vm, lcm, seid, deid, loc_set, &mr_ip,
2127 &sloc, is_smr_invoked, &nonce, &bi);
2132 /* set fib index and lookup node */
2133 vnet_buffer(b)->sw_if_index[VLIB_TX] = ~0;
2134 next_index = (ip_addr_version(&mr_ip) == IP4) ?
2135 ip4_lookup_node.index : ip6_lookup_node.index;
2137 f = vlib_get_frame_to_node (vm, next_index);
2139 /* Enqueue the packet */
2140 to_next = vlib_frame_vector_args (f);
2143 vlib_put_frame_to_node (vm, next_index, f);
2145 /* add map-request to pending requests table */
2146 pool_get(lcm->pending_map_requests_pool, pmr);
2147 gid_address_copy (&pmr->src, seid);
2148 gid_address_copy (&pmr->dst, deid);
2149 pmr->src_mapping_index = map_index;
2150 hash_set(lcm->pending_map_requests_by_nonce, nonce,
2151 pmr - lcm->pending_map_requests_pool);
2155 get_src_and_dst (void *hdr, ip_address_t * src, ip_address_t *dst)
2157 ip4_header_t * ip4 = hdr;
2160 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2162 ip_address_set(src, &ip4->src_address, IP4);
2163 ip_address_set(dst, &ip4->dst_address, IP4);
2168 ip_address_set(src, &ip6->src_address, IP6);
2169 ip_address_set(dst, &ip6->dst_address, IP6);
2174 lisp_get_vni_from_buffer (vlib_buffer_t * b, u8 version)
2177 u32 vni = ~0, table_id = ~0, fib_index;
2178 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
2183 ip4_main_t * im4 = &ip4_main;
2184 fib_index = vec_elt (im4->fib_index_by_sw_if_index,
2185 vnet_buffer (b)->sw_if_index[VLIB_RX]);
2186 fib = find_ip4_fib_by_table_index_or_id (im4, fib_index,
2187 IP4_ROUTE_FLAG_FIB_INDEX);
2188 table_id = fib->table_id;
2193 ip6_main_t * im6 = &ip6_main;
2194 fib_index = vec_elt (im6->fib_index_by_sw_if_index,
2195 vnet_buffer (b)->sw_if_index[VLIB_RX]);
2196 fib = find_ip6_fib_by_table_index_or_id (im6, fib_index,
2197 IP6_ROUTE_FLAG_FIB_INDEX);
2198 table_id = fib->table_id;
2201 vnip = hash_get (lcm->vni_by_table_id, table_id);
2205 clib_warning ("vrf %d is not mapped to any vni!", table_id);
2211 lisp_cp_lookup (vlib_main_t * vm, vlib_node_runtime_t * node,
2212 vlib_frame_t * from_frame)
2214 u32 * from, * to_next_drop, di, si;
2215 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main();
2216 u32 pkts_mapped = 0;
2217 uword n_left_from, n_left_to_next_drop;
2219 from = vlib_frame_vector_args (from_frame);
2220 n_left_from = from_frame->n_vectors;
2222 while (n_left_from > 0)
2224 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2225 to_next_drop, n_left_to_next_drop);
2227 while (n_left_from > 0 && n_left_to_next_drop > 0)
2232 gid_address_t src, dst;
2233 ip_prefix_t * spref, * dpref;
2235 gid_address_type (&src) = GID_ADDR_IP_PREFIX;
2236 spref = &gid_address_ippref(&src);
2237 gid_address_type (&dst) = GID_ADDR_IP_PREFIX;
2238 dpref = &gid_address_ippref(&dst);
2243 to_next_drop[0] = pi0;
2245 n_left_to_next_drop -= 1;
2247 p0 = vlib_get_buffer (vm, pi0);
2248 p0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
2250 /* src/dst eid pair */
2251 ip0 = vlib_buffer_get_current (p0);
2252 get_src_and_dst (ip0, &ip_prefix_addr(spref), &ip_prefix_addr(dpref));
2253 ip_prefix_len(spref) = ip_address_max_len (ip_prefix_version(spref));
2254 ip_prefix_len(dpref) = ip_address_max_len (ip_prefix_version(dpref));
2256 vni = lisp_get_vni_from_buffer (p0, ip_prefix_version (spref));
2257 gid_address_vni (&dst) = vni;
2258 gid_address_vni (&src) = vni;
2260 /* if we have remote mapping for destination already in map-chache
2261 add forwarding tunnel directly. If not send a map-request */
2262 di = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
2265 mapping_t * m = vec_elt_at_index (lcm->mapping_pool, di);
2266 /* send a map-request also in case of negative mapping entry
2267 with corresponding action */
2268 if (m->action == ACTION_SEND_MAP_REQUEST)
2270 /* send map-request */
2271 send_encapsulated_map_request (vm, lcm, &src, &dst, 0);
2276 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
2280 add_fwd_entry (lcm, si, di);
2286 /* send map-request */
2287 send_encapsulated_map_request (vm, lcm, &src, &dst, 0);
2291 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
2293 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, p0,
2296 memset(tr, 0, sizeof(*tr));
2297 gid_address_copy (&tr->dst_eid, &dst);
2298 if (vec_len(lcm->map_resolvers) > 0)
2300 clib_memcpy (&tr->map_resolver_ip,
2301 vec_elt_at_index(lcm->map_resolvers, 0),
2302 sizeof(ip_address_t));
2305 gid_address_free (&dst);
2306 gid_address_free (&src);
2309 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP, n_left_to_next_drop);
2311 vlib_node_increment_counter (vm, node->node_index,
2312 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
2314 return from_frame->n_vectors;
2317 VLIB_REGISTER_NODE (lisp_cp_lookup_node) = {
2318 .function = lisp_cp_lookup,
2319 .name = "lisp-cp-lookup",
2320 .vector_size = sizeof (u32),
2321 .format_trace = format_lisp_cp_lookup_trace,
2322 .type = VLIB_NODE_TYPE_INTERNAL,
2324 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2325 .error_strings = lisp_cp_lookup_error_strings,
2327 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2330 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2331 [LISP_CP_LOOKUP_NEXT_IP4_LOOKUP] = "ip4-lookup",
2332 [LISP_CP_LOOKUP_NEXT_IP6_LOOKUP] = "ip6-lookup",
2336 /* lisp_cp_input statistics */
2337 #define foreach_lisp_cp_input_error \
2339 _(MAP_REPLIES_RECEIVED, "map-replies received")
2341 static char * lisp_cp_input_error_strings[] = {
2342 #define _(sym,string) string,
2343 foreach_lisp_cp_input_error
2349 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
2350 foreach_lisp_cp_input_error
2352 LISP_CP_INPUT_N_ERROR,
2353 } lisp_cp_input_error_t;
2357 LISP_CP_INPUT_NEXT_DROP,
2358 LISP_CP_INPUT_N_NEXT,
2359 } lisp_cp_input_next_t;
2363 gid_address_t dst_eid;
2364 ip4_address_t map_resolver_ip;
2365 } lisp_cp_input_trace_t;
2368 format_lisp_cp_input_trace (u8 * s, va_list * args)
2370 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2371 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2372 CLIB_UNUSED(lisp_cp_input_trace_t * t) = va_arg (*args, lisp_cp_input_trace_t *);
2374 s = format (s, "LISP-CP-INPUT: TODO");
2379 del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index,
2382 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
2383 fwd_entry_t * fe = 0;
2385 memset(a, 0, sizeof(*a));
2387 feip = hash_get(lcm->fwd_entry_by_mapping_index, dst_map_index);
2391 fe = pool_elt_at_index(lcm->fwd_entry_pool, feip[0]);
2393 /* delete dp fwd entry */
2396 a->dlocator = fe->dst_loc;
2397 a->slocator = fe->src_loc;
2398 a->vni = gid_address_vni(&a->deid);
2399 gid_address_copy(&a->deid, &fe->deid);
2401 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
2403 /* delete entry in fwd table */
2404 hash_unset(lcm->fwd_entry_by_mapping_index, dst_map_index);
2405 pool_put(lcm->fwd_entry_pool, fe);
2409 * Finds first remote locator with best (lowest) priority that has a local
2410 * peer locator with an underlying route to it.
2414 get_locator_pair (lisp_cp_main_t* lcm, mapping_t * lcl_map, mapping_t * rmt_map,
2415 ip_address_t * lcl_loc, ip_address_t * rmt_loc)
2417 u32 i, minp = ~0, limitp = 0, li, check_index = 0, done = 0, esi;
2418 locator_set_t * rmt_ls, * lcl_ls;
2419 ip_address_t _lcl, * lcl = &_lcl;
2420 locator_t * l, * rmt = 0;
2421 uword * checked = 0;
2423 rmt_ls = pool_elt_at_index(lcm->locator_set_pool, rmt_map->locator_set_index);
2424 lcl_ls = pool_elt_at_index(lcm->locator_set_pool, lcl_map->locator_set_index);
2426 if (!rmt_ls || vec_len(rmt_ls->locator_indices) == 0)
2431 /* find unvisited remote locator with best priority */
2432 for (i = 0; i < vec_len(rmt_ls->locator_indices); i++)
2434 if (0 != hash_get(checked, i))
2437 li = vec_elt(rmt_ls->locator_indices, i);
2438 l = pool_elt_at_index(lcm->locator_pool, li);
2440 /* we don't support non-IP locators for now */
2441 if (gid_address_type(&l->address) != GID_ADDR_IP_PREFIX)
2444 if (l->priority < minp && l->priority >= limitp)
2451 /* check if a local locator with a route to remote locator exists */
2454 esi = ip_fib_get_egress_iface_for_dst (
2455 lcm, &gid_address_ip(&rmt->address));
2456 if ((u32) ~0 == esi)
2459 for (i = 0; i < vec_len(lcl_ls->locator_indices); i++)
2461 li = vec_elt (lcl_ls->locator_indices, i);
2462 locator_t * sl = pool_elt_at_index (lcm->locator_pool, li);
2464 /* found local locator */
2465 if (sl->sw_if_index == esi)
2467 if (0 == ip_interface_get_first_ip_address (lcm,
2469 gid_address_ip_version(&rmt->address), lcl))
2472 ip_address_copy(rmt_loc, &gid_address_ip(&rmt->address));
2473 ip_address_copy(lcl_loc, lcl);
2478 /* skip this remote locator in next searches */
2480 hash_set(checked, check_index, 1);
2486 return (done == 2) ? 1 : 0;
2490 add_fwd_entry (lisp_cp_main_t* lcm, u32 src_map_index, u32 dst_map_index)
2492 mapping_t * src_map, * dst_map;
2494 uword * feip = 0, * tidp;
2496 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
2498 memset (a, 0, sizeof(*a));
2500 /* remove entry if it already exists */
2501 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
2503 del_fwd_entry (lcm, src_map_index, dst_map_index);
2505 src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
2506 dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
2508 gid_address_copy (&a->deid, &dst_map->eid);
2509 a->vni = gid_address_vni(&a->deid);
2511 tidp = hash_get(lcm->table_id_by_vni, a->vni);
2514 clib_warning("vni %d not associated to a vrf!", a->vni);
2517 a->table_id = tidp[0];
2519 /* insert data plane forwarding entry */
2522 /* find best locator pair that 1) verifies LISP policy 2) are connected */
2523 if (0 == get_locator_pair (lcm, src_map, dst_map, &a->slocator, &a->dlocator))
2525 /* negative entry */
2527 a->action = dst_map->action;
2531 u8 ipver = ip_prefix_version(&gid_address_ippref(&a->deid));
2532 a->decap_next_index = (ipver == IP4) ?
2533 LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
2535 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
2537 /* add tunnel to fwd entry table XXX check return value from DP insertion */
2538 pool_get (lcm->fwd_entry_pool, fe);
2539 fe->dst_loc = a->dlocator;
2540 fe->src_loc = a->slocator;
2541 gid_address_copy (&fe->deid, &a->deid);
2542 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
2543 fe - lcm->fwd_entry_pool);
2546 /* return 0 if the two locator sets are identical 1 otherwise */
2548 compare_locators (lisp_cp_main_t *lcm, u32 * old_ls_indexes,
2549 locator_t * new_locators)
2552 locator_t * old_loc, * new_loc;
2554 if (vec_len (old_ls_indexes) != vec_len(new_locators))
2557 for (i = 0; i < vec_len(new_locators); i++)
2559 old_li = vec_elt(old_ls_indexes, i);
2560 old_loc = pool_elt_at_index(lcm->locator_pool, old_li);
2562 new_loc = vec_elt_at_index(new_locators, i);
2564 if (locator_cmp (old_loc, new_loc))
2571 process_map_reply (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2573 mapping_t * old_map;
2575 u32 len = 0, i, ls_index = 0;
2577 vnet_lisp_add_del_locator_set_args_t _ls_arg, * ls_arg = &_ls_arg;
2578 vnet_lisp_add_del_mapping_args_t _m_args, * m_args = &_m_args;
2579 pending_map_request_t * pmr;
2581 map_reply_hdr_t * mrep_hdr;
2583 u32 dst_map_index, mi;
2586 mrep_hdr = vlib_buffer_get_current (b);
2588 /* Check pending requests table and nonce */
2589 nonce = MREP_NONCE(mrep_hdr);
2590 pmr_index = hash_get(lcm->pending_map_requests_by_nonce, nonce);
2593 clib_warning("No pending map-request entry with nonce %lu!", nonce);
2596 pmr = pool_elt_at_index(lcm->pending_map_requests_pool, pmr_index[0]);
2598 vlib_buffer_pull (b, sizeof(*mrep_hdr));
2600 for (i = 0; i < MREP_REC_COUNT(mrep_hdr); i++)
2602 memset (ls_arg, 0, sizeof(*ls_arg));
2603 memset (m_args, 0, sizeof(*m_args));
2605 h = vlib_buffer_get_current (b);
2606 m_args->ttl = clib_net_to_host_u32 (MAP_REC_TTL(h));
2607 m_args->action = MAP_REC_ACTION(h);
2608 m_args->authoritative = MAP_REC_AUTH(h);
2610 len = lisp_msg_parse_mapping_record (b, &m_args->deid, &ls_arg->locators,
2614 clib_warning ("Failed to parse mapping record!");
2615 vec_foreach (loc, ls_arg->locators)
2619 vec_free(ls_arg->locators);
2623 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &m_args->deid);
2624 old_map = mi != ~0 ? pool_elt_at_index(lcm->mapping_pool, mi) : 0;
2626 /* if mapping already exists, decide if locators (and forwarding) should
2627 * be updated and be done */
2628 if (old_map != 0 && !gid_address_cmp (&old_map->eid, &m_args->deid))
2630 locator_set_t * old_ls;
2632 /* update mapping attributes */
2633 old_map->action = m_args->action;
2634 old_map->authoritative = m_args->authoritative;
2635 old_map->ttl = m_args->ttl;
2637 old_ls = pool_elt_at_index(lcm->locator_set_pool,
2638 old_map->locator_set_index);
2639 /* if the two locators are not equal, update them and forwarding
2640 * otherwise there's nothing to be done */
2641 if (compare_locators (lcm, old_ls->locator_indices, ls_arg->locators))
2643 /* set locator-set index to overwrite */
2645 ls_arg->index = old_map->locator_set_index;
2646 vnet_lisp_add_del_locator_set (ls_arg, 0);
2647 add_fwd_entry (lcm, pmr->src_mapping_index, mi);
2653 /* add locator-set */
2656 vnet_lisp_add_del_locator_set (ls_arg, &ls_index);
2660 m_args->locator_set_index = ls_index;
2661 vnet_lisp_add_del_mapping (m_args, &dst_map_index);
2663 /* add forwarding tunnel */
2664 add_fwd_entry (lcm, pmr->src_mapping_index, dst_map_index);
2666 vec_free(ls_arg->locators);
2669 /* remove pending map request entry */
2670 hash_unset(lcm->pending_map_requests_by_nonce, nonce);
2671 pool_put(lcm->pending_map_requests_pool, pmr);
2675 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm, vlib_buffer_t * b)
2677 map_request_hdr_t * mreq_hdr;
2678 gid_address_t src, dst;
2681 gid_address_t * itr_rlocs = 0, * rloc;
2683 mreq_hdr = vlib_buffer_get_current (b);
2684 vlib_buffer_pull (b, sizeof(*mreq_hdr));
2686 // nonce = MREQ_NONCE(mreq_hdr);
2688 if (!MREQ_SMR(mreq_hdr)) {
2689 clib_warning("Only SMR Map-Requests supported for now!");
2694 len = lisp_msg_parse_addr (b, &src);
2698 /* for now we don't do anything with the itr's rlocs */
2699 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs, MREQ_ITR_RLOC_COUNT(mreq_hdr) + 1);
2703 /* TODO: RLOCs are currently unused, so free them for now */
2704 vec_foreach (rloc, itr_rlocs)
2706 gid_address_free (rloc);
2709 /* parse eid records and send SMR-invoked map-requests */
2710 for (i = 0; i < MREQ_REC_COUNT(mreq_hdr); i++)
2712 memset(&dst, 0, sizeof(dst));
2713 len = lisp_msg_parse_eid_rec (b, &dst);
2716 clib_warning("Can't parse map-request EID-record");
2719 /* send SMR-invoked map-requests */
2720 send_encapsulated_map_request (vm, lcm, &dst, &src, /* invoked */ 1);
2725 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2726 vlib_frame_t * from_frame)
2728 u32 n_left_from, * from, * to_next_drop;
2729 lisp_msg_type_e type;
2730 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2732 from = vlib_frame_vector_args (from_frame);
2733 n_left_from = from_frame->n_vectors;
2736 while (n_left_from > 0)
2738 u32 n_left_to_next_drop;
2740 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
2741 to_next_drop, n_left_to_next_drop);
2742 while (n_left_from > 0 && n_left_to_next_drop > 0)
2750 to_next_drop[0] = bi0;
2752 n_left_to_next_drop -= 1;
2754 b0 = vlib_get_buffer (vm, bi0);
2756 type = lisp_msg_type(vlib_buffer_get_current (b0));
2759 case LISP_MAP_REPLY:
2760 process_map_reply (lcm, b0);
2762 case LISP_MAP_REQUEST:
2763 process_map_request(vm, lcm, b0);
2766 clib_warning("Unsupported LISP message type %d", type);
2770 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
2772 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
2778 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP, n_left_to_next_drop);
2780 return from_frame->n_vectors;
2783 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
2784 .function = lisp_cp_input,
2785 .name = "lisp-cp-input",
2786 .vector_size = sizeof (u32),
2787 .format_trace = format_lisp_cp_input_trace,
2788 .type = VLIB_NODE_TYPE_INTERNAL,
2790 .n_errors = LISP_CP_INPUT_N_ERROR,
2791 .error_strings = lisp_cp_input_error_strings,
2793 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2796 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2801 lisp_cp_init (vlib_main_t *vm)
2803 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2804 clib_error_t * error = 0;
2806 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
2809 lcm->im4 = &ip4_main;
2810 lcm->im6 = &ip6_main;
2811 lcm->vlib_main = vm;
2812 lcm->vnet_main = vnet_get_main();
2813 lcm->mreq_itr_rlocs = ~0;
2815 gid_dictionary_init (&lcm->mapping_index_by_gid);
2817 /* default vrf mapped to vni 0 */
2818 hash_set(lcm->table_id_by_vni, 0, 0);
2819 hash_set(lcm->vni_by_table_id, 0, 0);
2821 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
2822 lisp_cp_input_node.index, 1 /* is_ip4 */);
2823 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
2824 lisp_cp_input_node.index, 0 /* is_ip4 */);
2829 VLIB_INIT_FUNCTION(lisp_cp_init);