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_ip_address,
52 return VNET_API_ERROR_VALUE_EXIST;
55 pool_get(lcm->mapping_pool, m);
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_ip_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 pool_put_index (lcm->mapping_pool, mi);
132 /* Stores mapping in map-cache and programs data plane for local mappings. */
134 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
135 u32 * map_index_result)
137 uword * table_id, * refc;
139 vnet_lisp_gpe_add_del_iface_args_t _ai, *ai = &_ai;
140 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
142 vni = gid_address_vni(&a->deid);
144 /* store/remove mapping from map-cache */
145 rv = vnet_lisp_add_del_mapping (a, map_index_result);
149 table_id = hash_get(lcm->table_id_by_vni, vni);
153 clib_warning ("vni %d not associated to a vrf!", vni);
154 return VNET_API_ERROR_INVALID_VALUE;
157 refc = hash_get(lcm->dp_if_refcount_by_vni, vni);
159 /* enable/disable data-plane interface */
162 /* create interface or update refcount */
167 ai->table_id = table_id[0];
168 vnet_lisp_gpe_add_del_iface (ai, 0);
170 /* counts the number of eids in a vni that use the interface */
171 hash_set(lcm->dp_if_refcount_by_vni, vni, 1);
180 /* since this is a remove for an existing eid, the iface should exist */
184 /* remove iface if needed */
189 ai->table_id = table_id[0];
190 vnet_lisp_gpe_add_del_iface (ai, 0);
191 hash_unset (lcm->dp_if_refcount_by_vni, vni);
198 static clib_error_t *
199 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
200 vlib_cli_command_t * cmd)
202 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
203 unformat_input_t _line_input, * line_input = &_line_input;
206 ip_prefix_t * prefp = &gid_address_ippref(&eid);
207 gid_address_t * eids = 0;
208 clib_error_t * error = 0;
209 u8 * locator_set_name = 0;
210 u32 locator_set_index = 0, map_index = 0;
212 vnet_lisp_add_del_mapping_args_t _a, * a = &_a;
214 gid_address_type (&eid) = GID_ADDR_IP_PREFIX;
216 /* Get a line of input. */
217 if (! unformat_user (input, unformat_line_input, line_input))
220 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
222 if (unformat (line_input, "add"))
224 else if (unformat (line_input, "del"))
226 else if (unformat (line_input, "eid %U", unformat_ip_prefix, prefp))
230 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
232 p = hash_get_mem(lcm->locator_set_index_by_name, locator_set_name);
235 error = clib_error_return(0, "locator-set %s doesn't exist",
239 locator_set_index = p[0];
243 error = unformat_parse_error(line_input);
248 /* XXX treat batch configuration */
251 a->locator_set_index = locator_set_index;
254 vnet_lisp_add_del_local_mapping (a, &map_index);
257 if (locator_set_name)
258 vec_free (locator_set_name);
262 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
263 .path = "lisp eid-table",
264 .short_help = "lisp eid-table add/del eid <eid> locator-set <locator-set>",
265 .function = lisp_add_del_local_eid_command_fn,
269 lisp_add_del_negative_static_mapping (gid_address_t * deid,
270 vnet_lisp_add_del_locator_set_args_t * ls, u8 action, u8 is_add)
275 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
277 vnet_lisp_add_del_mapping_args_t _dm_args, * dm_args = &_dm_args;
279 u32 ls_index = 0, dst_map_index;
280 vnet_lisp_gpe_add_del_iface_args_t _ai, *ai = &_ai;
282 memset (dm_args, 0, sizeof (dm_args[0]));
283 u32 vni = gid_address_vni (deid);
284 refc = hash_get (lcm->dp_if_refcount_by_vni, vni);
286 p = hash_get (lcm->table_id_by_vni, vni);
289 clib_warning ("vni %d not associated to a vrf!", vni);
290 return VNET_API_ERROR_INVALID_VALUE;
295 vnet_lisp_add_del_locator_set (ls, &ls_index);
297 gid_address_copy (&dm_args->deid, deid);
299 dm_args->action = action;
300 dm_args->locator_set_index = ls_index;
302 /* create interface or update refcount */
305 vnet_lisp_gpe_add_del_iface_args_t _ai, *ai = &_ai;
309 vnet_lisp_gpe_add_del_iface (ai, 0);
311 /* counts the number of eids in a vni that use the interface */
312 hash_set (lcm->dp_if_refcount_by_vni, vni, 1);
317 rv = vnet_lisp_add_del_local_mapping (dm_args, &dst_map_index);
319 add_fwd_entry (lcm, lcm->pitr_map_index, dst_map_index);
323 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, deid);
326 clib_warning ("eid %U marked for removal, but not found in "
327 "map-cache!", unformat_gid_address, deid);
328 return VNET_API_ERROR_INVALID_VALUE;
331 /* delete forwarding entry */
332 del_fwd_entry (lcm, 0, mi);
335 gid_address_copy (&dm_args->deid, deid);
336 map = pool_elt_at_index (lcm->mapping_pool, mi);
337 dm_args->locator_set_index = map->locator_set_index;
339 /* delete mapping associated to fwd entry */
340 vnet_lisp_add_del_mapping (dm_args, 0);
342 refc = hash_get (lcm->dp_if_refcount_by_vni, vni);
346 /* remove iface if needed */
352 vnet_lisp_gpe_add_del_iface (ai, 0);
353 hash_unset (lcm->dp_if_refcount_by_vni, vni);
360 * Adds/removes/updates static remote mapping.
362 * This function also modifies forwarding entries if needed.
364 * @param deid destination EID
365 * @param seid source EID
366 * @param rlocs vector of remote locators
367 * @param action action for negative map-reply
368 * @param is_add add mapping if non-zero, delete otherwise
369 * @param del_all if set, delete all remote mappings
370 * @return return code
373 vnet_lisp_add_del_remote_mapping (gid_address_t * deid, gid_address_t * seid,
374 ip_address_t * rlocs, u8 action, u8 is_add,
377 vnet_lisp_add_del_mapping_args_t _dm_args, * dm_args = &_dm_args;
378 vnet_lisp_add_del_mapping_args_t _sm_args, * sm_args = &_sm_args;
379 vnet_lisp_add_del_locator_set_args_t _ls, * ls = &_ls;
380 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
381 u32 mi, ls_index = 0, dst_map_index, src_map_index;
387 return vnet_lisp_clear_all_remote_mappings ();
389 memset (sm_args, 0, sizeof (sm_args[0]));
390 memset (dm_args, 0, sizeof (dm_args[0]));
391 memset (ls, 0, sizeof (ls[0]));
393 /* prepare remote locator set */
394 vec_foreach (dl, rlocs)
396 memset (&loc, 0, sizeof (loc));
397 gid_address_ip (&loc.address) = dl[0];
398 vec_add1 (ls->locators, loc);
400 src_map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
402 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, deid);
409 /* process a negative mapping */
410 if (0 == vec_len (rlocs))
411 return lisp_add_del_negative_static_mapping (deid, ls,
414 if ((u32)~0 == src_map_index)
416 clib_warning ("seid %U not found!", format_gid_address, seid);
422 clib_warning ("deid %U marked for removal but not "
423 "found!", format_gid_address, deid);
427 vnet_lisp_add_del_locator_set (ls, &ls_index);
430 gid_address_copy (&dm_args->deid, deid);
432 dm_args->action = action;
433 dm_args->locator_set_index = ls_index;
434 vnet_lisp_add_del_mapping (dm_args, &dst_map_index);
437 add_fwd_entry (lcm, src_map_index, dst_map_index);
444 /* delete forwarding entry */
445 del_fwd_entry (lcm, 0, mi);
447 gid_address_copy (&dm_args->deid, deid);
448 mapping_t * map = pool_elt_at_index (lcm->mapping_pool, mi);
449 dm_args->locator_set_index = map->locator_set_index;
451 /* delete mapping associated to fwd entry */
452 vnet_lisp_add_del_mapping (dm_args, 0);
455 ls->index = map->locator_set_index;
456 /* delete locator set */
457 vnet_lisp_add_del_locator_set (ls, 0);
459 /* update existing locator set */
462 if ((u32)~0 == src_map_index)
464 clib_warning ("seid %U not found!", format_gid_address, seid);
469 locator_set_t * old_ls;
470 old_map = pool_elt_at_index (lcm->mapping_pool, mi);
472 /* update mapping attributes */
473 old_map->action = action;
475 old_ls = pool_elt_at_index(lcm->locator_set_pool,
476 old_map->locator_set_index);
477 if (compare_locators (lcm, old_ls->locator_indices,
480 /* set locator-set index to overwrite */
482 ls->index = old_map->locator_set_index;
483 vnet_lisp_add_del_locator_set (ls, 0);
484 add_fwd_entry (lcm, src_map_index, mi);
491 vec_free (ls->locators);
496 vnet_lisp_clear_all_remote_mappings (void)
499 u32 mi, * map_indices = 0, * map_indexp;
500 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
501 vnet_lisp_add_del_mapping_args_t _dm_args, * dm_args = &_dm_args;
502 vnet_lisp_add_del_locator_set_args_t _ls, * ls = &_ls;
504 pool_foreach_index (mi, lcm->mapping_pool,
506 vec_add1 (map_indices, mi);
509 vec_foreach (map_indexp, map_indices)
511 mapping_t * map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
514 del_fwd_entry (lcm, 0, map_indexp[0]);
517 gid_address_copy (&dm_args->deid, &map->eid);
518 dm_args->locator_set_index = map->locator_set_index;
520 /* delete mapping associated to fwd entry */
521 vnet_lisp_add_del_mapping (dm_args, 0);
525 ls->index = map->locator_set_index;
526 /* delete locator set */
527 rv = vnet_lisp_add_del_locator_set (ls, 0);
535 vec_free (map_indices);
540 * Handler for add/del remote mapping CLI.
542 * @param vm vlib context
543 * @param input input from user
545 * @return pointer to clib error structure
547 static clib_error_t *
548 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
549 unformat_input_t * input,
550 vlib_cli_command_t * cmd)
552 clib_error_t * error = 0;
553 unformat_input_t _line_input, * line_input = &_line_input;
554 u8 is_add = 1, del_all = 0;
555 ip_address_t rloc, * rlocs = 0;
556 ip_prefix_t * deid_ippref, * seid_ippref;
557 gid_address_t seid, deid;
560 u32 vni, action = ~0;
562 /* Get a line of input. */
563 if (! unformat_user (input, unformat_line_input, line_input))
566 memset (&deid, 0, sizeof (deid));
567 memset (&seid, 0, sizeof (seid));
569 seid_ippref = &gid_address_ippref(&seid);
570 deid_ippref = &gid_address_ippref(&deid);
572 gid_address_type (&deid) = GID_ADDR_IP_PREFIX;
573 gid_address_type (&seid) = GID_ADDR_IP_PREFIX;
575 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
577 if (unformat (line_input, "del-all"))
579 else if (unformat (line_input, "del"))
581 else if (unformat (line_input, "add"))
583 else if (unformat (line_input, "deid %U",
584 unformat_ip_prefix, deid_ippref))
588 else if (unformat (line_input, "vni %u", &vni))
590 gid_address_set_vni (&seid, vni);
591 gid_address_set_vni (&deid, vni);
593 else if (unformat (line_input, "seid %U",
594 unformat_ip_prefix, seid_ippref))
596 else if (unformat (line_input, "rloc %U", unformat_ip_address, &rloc))
597 vec_add1 (rlocs, rloc);
598 else if (unformat (line_input, "action %s", &s))
600 if (!strcmp ((char *)s, "no-action"))
601 action = ACTION_NONE;
602 if (!strcmp ((char *)s, "natively-forward"))
603 action = ACTION_NATIVELY_FORWARDED;
604 if (!strcmp ((char *)s, "send-map-request"))
605 action = ACTION_SEND_MAP_REQUEST;
606 else if (!strcmp ((char *)s, "drop"))
607 action = ACTION_DROP;
610 clib_warning ("invalid action: '%s'", s);
616 clib_warning ("parse error");
625 clib_warning ("missing deid!");
630 && (ip_prefix_version (deid_ippref)
631 != ip_prefix_version (seid_ippref)))
633 clib_warning ("source and destination EIDs are not"
634 " in the same IP family!");
638 if (is_add && (~0 == action)
639 && 0 == vec_len (rlocs))
641 clib_warning ("no action set for negative map-reply!");
646 int rv = vnet_lisp_add_del_remote_mapping (&deid, &seid, rlocs,
647 action, is_add, del_all);
649 clib_warning ("failed to %s remote mapping!",
650 is_add ? "add" : "delete");
653 unformat_free (line_input);
659 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) = {
660 .path = "lisp remote-mapping",
661 .short_help = "lisp remote-mapping add|del [del-all] vni <vni>"
662 "deid <dest-eid> seid <src-eid> [action <no-action|natively-forward|"
663 "send-map-request|drop>] rloc <dst-locator> [rloc <dst-locator> ... ]",
664 .function = lisp_add_del_remote_mapping_command_fn,
667 static clib_error_t *
668 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
669 unformat_input_t * input,
670 vlib_cli_command_t * cmd)
673 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
675 vec_foreach (addr, lcm->map_resolvers)
677 vlib_cli_output (vm, "%U", format_ip_address, addr);
682 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
683 .path = "show lisp map-resolvers",
684 .short_help = "show lisp map-resolvers",
685 .function = lisp_show_map_resolvers_command_fn,
689 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
691 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
692 u32 locator_set_index = ~0;
696 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
699 clib_warning ("locator-set %v doesn't exist", locator_set_name);
702 locator_set_index = p[0];
706 pool_get (lcm->mapping_pool, m);
707 m->locator_set_index = locator_set_index;
709 lcm->pitr_map_index = m - lcm->mapping_pool;
711 /* enable pitr mode */
716 /* remove pitr mapping */
717 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
719 /* disable pitr mode */
725 static clib_error_t *
726 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
727 unformat_input_t * input,
728 vlib_cli_command_t * cmd)
730 u8 locator_name_set = 0;
731 u8 * locator_set_name = 0;
733 unformat_input_t _line_input, * line_input = &_line_input;
735 /* Get a line of input. */
736 if (! unformat_user (input, unformat_line_input, line_input))
739 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
741 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
742 locator_name_set = 1;
743 else if (unformat (line_input, "disable"))
746 return clib_error_return (0, "parse error");
749 if (!locator_name_set)
751 clib_warning ("No locator set specified!");
754 vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
757 if (locator_set_name)
758 vec_free (locator_set_name);
762 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
764 .short_help = "lisp pitr [disable] ls <locator-set-name>",
765 .function = lisp_pitr_set_locator_set_command_fn,
768 static clib_error_t *
769 lisp_show_local_eid_table_command_fn (vlib_main_t * vm,
770 unformat_input_t * input,
771 vlib_cli_command_t * cmd)
773 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
776 vlib_cli_output (vm, "%=20s%=16s", "EID", "Locator");
777 pool_foreach (mapit, lcm->mapping_pool,
780 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
781 mapit->locator_set_index);
782 vlib_cli_output (vm, "%-16U%16v", format_gid_address, &mapit->eid,
790 VLIB_CLI_COMMAND (lisp_cp_show_local_eid_table_command) = {
791 .path = "show lisp eid-table",
792 .short_help = "Shows local EID table",
793 .function = lisp_show_local_eid_table_command_fn,
796 /* cleans locator to locator-set data and removes locators not part of
799 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
801 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
802 locator_set_t * ls = pool_elt_at_index(lcm->locator_set_pool, lsi);
803 for (i = 0; i < vec_len(ls->locator_indices); i++)
805 loc_indexp = vec_elt_at_index(ls->locator_indices, i);
806 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
808 for (j = 0; j < vec_len(ls_indexes[0]); j++)
810 ls_indexp = vec_elt_at_index(ls_indexes[0], j);
811 if (ls_indexp[0] == lsi)
815 /* delete index for removed locator-set*/
816 vec_del1(ls_indexes[0], j);
818 /* delete locator if it's part of no locator-set */
819 if (vec_len (ls_indexes[0]) == 0)
821 pool_put_index (lcm->locator_pool, loc_indexp[0]);
822 vec_add1 (to_be_deleted, i);
828 for (i = 0; i < vec_len (to_be_deleted); i++)
830 loc_indexp = vec_elt_at_index (to_be_deleted, i);
831 vec_del1 (ls->locator_indices, loc_indexp[0]);
833 vec_free (to_be_deleted);
838 uword *get_locator_set_index(vnet_lisp_add_del_locator_set_args_t * a,
841 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
846 /* find locator-set */
849 p = hash_get_mem(lcm->locator_set_index_by_name, a->name);
860 int is_locator_in_locator_set(lisp_cp_main_t * lcm, locator_set_t * ls,
869 vec_foreach(locit, ls->locator_indices)
871 itloc = pool_elt_at_index(lcm->locator_pool, locit[0]);
872 if (itloc->sw_if_index == loc->sw_if_index ||
873 !gid_address_cmp(&itloc->address, &loc->address))
875 clib_warning("Duplicate locator");
876 return VNET_API_ERROR_VALUE_EXIST;
884 void remove_locator_from_locator_set(locator_set_t * ls, u32 * locit,
885 u32 ls_index, u32 loc_id)
887 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
888 u32 ** ls_indexes = NULL;
891 ASSERT(locit != NULL);
893 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
895 pool_put_index(lcm->locator_pool, locit[0]);
896 vec_del1(ls->locator_indices, loc_id);
897 vec_del1(ls_indexes[0], ls_index);
901 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
902 locator_set_t * ls, u32 * ls_result)
904 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
905 locator_t * loc = NULL, *itloc = NULL;
906 uword _p = (u32)~0, * p = &_p;
907 u32 loc_index = ~0, ls_index = ~0, * locit = NULL, ** ls_indexes = NULL;
913 p = get_locator_set_index(a, p);
916 clib_warning("locator-set %v doesn't exist", a->name);
917 return VNET_API_ERROR_INVALID_ARGUMENT;
922 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
925 clib_warning("locator-set %d to be overwritten doesn't exist!",
927 return VNET_API_ERROR_INVALID_ARGUMENT;
937 /* allocate locators */
938 vec_foreach (itloc, a->locators)
940 ret = is_locator_in_locator_set(lcm, ls, itloc);
946 pool_get(lcm->locator_pool, loc);
948 loc_index = loc - lcm->locator_pool;
950 vec_add1(ls->locator_indices, loc_index);
952 vec_validate (lcm->locator_to_locator_sets, loc_index);
953 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
955 vec_add1(ls_indexes[0], ls_index);
964 vec_foreach (locit, ls->locator_indices)
966 loc = pool_elt_at_index(lcm->locator_pool, locit[0]);
968 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
970 remove_locator_from_locator_set(ls, locit,
973 if (0 == loc->local &&
974 !gid_address_cmp(&loc->address, &itloc->address))
976 remove_locator_from_locator_set(ls, locit,
988 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
991 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
993 uword _p = (u32)~0, * p = &_p;
1000 p = get_locator_set_index(a, p);
1003 if (p && p[0] != (u32)~0)
1005 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1008 clib_warning("locator-set %d to be overwritten doesn't exist!",
1013 /* clean locator to locator-set vectors and remove locators if
1014 * they're not part of another locator-set */
1015 clean_locator_to_locator_set (lcm, p[0]);
1017 /* remove locator indices from locator set */
1018 vec_free(ls->locator_indices);
1023 ls_result[0] = p[0];
1025 /* new locator-set */
1028 pool_get(lcm->locator_set_pool, ls);
1029 memset(ls, 0, sizeof(*ls));
1030 ls_index = ls - lcm->locator_set_pool;
1034 ls->name = vec_dup(a->name);
1036 if (!lcm->locator_set_index_by_name)
1037 lcm->locator_set_index_by_name = hash_create_vec(
1038 /* size */0, sizeof(ls->name[0]), sizeof(uword));
1039 hash_set_mem(lcm->locator_set_index_by_name, ls->name, ls_index);
1041 /* mark as local locator-set */
1042 vec_add1(lcm->local_locator_set_indexes, ls_index);
1044 ls->local = a->local;
1046 ls_result[0] = ls_index;
1049 ret = vnet_lisp_add_del_locator(a, ls, NULL);
1057 p = get_locator_set_index(a, p);
1060 clib_warning("locator-set %v doesn't exists", a->name);
1064 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1067 clib_warning("locator-set with index %d doesn't exists", p[0]);
1071 if (vec_len(lcm->locator_set_to_eids) != 0)
1073 eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids, p[0]);
1074 if (vec_len(eid_indexes[0]) != 0)
1076 clib_warning ("Can't delete a locator that supports a mapping!");
1081 /* clean locator to locator-sets data */
1082 clean_locator_to_locator_set (lcm, p[0]);
1088 vec_foreach_index(it, lcm->local_locator_set_indexes)
1090 lsi = vec_elt(lcm->local_locator_set_indexes, it);
1093 vec_del1(lcm->local_locator_set_indexes, it);
1097 hash_unset_mem(lcm->locator_set_index_by_name, ls->name);
1100 vec_free(ls->locator_indices);
1101 pool_put(lcm->locator_set_pool, ls);
1107 vnet_lisp_enable_disable (u8 is_enabled)
1109 vnet_lisp_gpe_add_del_iface_args_t _ai, * ai= &_ai;
1110 uword * table_id, * refc;
1112 clib_error_t * error = 0;
1113 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1114 vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
1116 a->is_en = is_enabled;
1117 error = vnet_lisp_gpe_enable_disable (a);
1120 return clib_error_return (0, "failed to %s data-plane!",
1121 a->is_en ? "enable" : "disable");
1126 /* enable all ifaces */
1127 for (i = 0; i < vec_len (lcm->local_mappings_indexes); i++)
1129 mapping_t * m = vec_elt_at_index (lcm->mapping_pool, i);
1131 ai->vni = gid_address_vni (&m->eid);
1133 refc = hash_get (lcm->dp_if_refcount_by_vni, ai->vni);
1136 table_id = hash_get (lcm->table_id_by_vni, ai->vni);
1139 ai->table_id = table_id[0];
1140 /* enables interface and adds defaults */
1141 vnet_lisp_gpe_add_del_iface (ai, 0);
1144 return clib_error_return (0, "no table_id found for vni %u!",
1147 hash_set (lcm->dp_if_refcount_by_vni, ai->vni, 1);
1157 /* clear refcount table */
1158 hash_free (lcm->dp_if_refcount_by_vni);
1159 hash_free (lcm->fwd_entry_by_mapping_index);
1160 pool_free (lcm->fwd_entry_pool);
1163 /* update global flag */
1164 lcm->is_enabled = is_enabled;
1169 static clib_error_t *
1170 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
1171 vlib_cli_command_t * cmd)
1173 unformat_input_t _line_input, * line_input = &_line_input;
1177 /* Get a line of input. */
1178 if (! unformat_user (input, unformat_line_input, line_input))
1181 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1183 if (unformat (line_input, "enable"))
1188 else if (unformat (line_input, "disable"))
1192 return clib_error_return (0, "parse error: '%U'",
1193 format_unformat_error, line_input);
1198 return clib_error_return (0, "state not set");
1200 return vnet_lisp_enable_disable (is_enabled);
1203 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
1205 .short_help = "lisp [enable|disable]",
1206 .function = lisp_enable_disable_command_fn,
1210 vnet_lisp_enable_disable_status (void)
1212 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1213 return lcm->is_enabled;
1217 format_lisp_status (u8 * s, va_list * args)
1219 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1220 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
1223 static clib_error_t *
1224 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
1225 vlib_cli_command_t * cmd)
1228 msg = format (msg, "feature: %U\ngpe: %U\n",
1229 format_lisp_status, format_vnet_lisp_gpe_status);
1230 vlib_cli_output (vm, "%v", msg);
1235 VLIB_CLI_COMMAND (lisp_show_status_command) = {
1236 .path = "show lisp status",
1237 .short_help = "show lisp status",
1238 .function = lisp_show_status_command_fn,
1240 static clib_error_t *
1241 lisp_add_del_locator_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
1242 vlib_cli_command_t * cmd)
1244 lisp_gpe_main_t * lgm = &lisp_gpe_main;
1245 vnet_main_t * vnm = lgm->vnet_main;
1246 unformat_input_t _line_input, * line_input = &_line_input;
1248 clib_error_t * error = 0;
1249 u8 * locator_set_name = 0;
1250 locator_t locator, * locators = 0;
1251 vnet_lisp_add_del_locator_set_args_t _a, * a = &_a;
1254 memset(&locator, 0, sizeof(locator));
1255 memset(a, 0, sizeof(a[0]));
1257 /* Get a line of input. */
1258 if (! unformat_user (input, unformat_line_input, line_input))
1261 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1263 if (unformat (line_input, "add %_%v%_", &locator_set_name))
1265 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
1267 else if (unformat (line_input, "iface %U p %d w %d",
1268 unformat_vnet_sw_interface, vnm, &locator.sw_if_index,
1269 &locator.priority, &locator.weight))
1272 vec_add1(locators, locator);
1276 error = unformat_parse_error(line_input);
1281 a->name = locator_set_name;
1282 a->locators = locators;
1286 vnet_lisp_add_del_locator_set(a, &ls_index);
1290 if (locator_set_name)
1291 vec_free (locator_set_name);
1295 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
1296 .path = "lisp locator-set",
1297 .short_help = "lisp locator-set add/del <name> iface <iface-name> "
1298 "p <priority> w <weight>",
1299 .function = lisp_add_del_locator_set_command_fn,
1302 static clib_error_t *
1303 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
1304 unformat_input_t * input,
1305 vlib_cli_command_t * cmd)
1307 locator_set_t * lsit;
1310 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1312 vlib_cli_output (vm, "%=20s%=16s%=16s%=16s", "Locator-set", "Locator",
1313 "Priority", "Weight");
1314 pool_foreach (lsit, lcm->locator_set_pool,
1317 msg = format (msg, "%-16v", lsit->name);
1318 vec_foreach (locit, lsit->locator_indices)
1320 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1322 msg = format (msg, "%16d%16d%16d", loc->sw_if_index, loc->priority,
1325 msg = format (msg, "%16U%16d%16d", format_gid_address, &loc->address,
1326 loc->priority, loc->weight);
1328 vlib_cli_output (vm, "%v", msg);
1334 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
1335 .path = "show lisp locator-set",
1336 .short_help = "Shows locator-sets",
1337 .function = lisp_cp_show_locator_sets_command_fn,
1341 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
1343 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1344 ip_address_t * addr;
1349 vec_foreach(addr, lcm->map_resolvers)
1351 if (!ip_address_cmp (addr, &a->address))
1353 clib_warning("map-resolver %U already exists!", format_ip_address,
1358 vec_add1(lcm->map_resolvers, a->address);
1362 for (i = 0; i < vec_len(lcm->map_resolvers); i++)
1364 addr = vec_elt_at_index(lcm->map_resolvers, i);
1365 if (!ip_address_cmp (addr, &a->address))
1367 vec_delete(lcm->map_resolvers, 1, i);
1375 static clib_error_t *
1376 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
1377 unformat_input_t * input,
1378 vlib_cli_command_t * cmd)
1380 unformat_input_t _line_input, * line_input = &_line_input;
1382 ip_address_t ip_addr;
1383 clib_error_t * error = 0;
1384 vnet_lisp_add_del_map_resolver_args_t _a, * a = &_a;
1386 /* Get a line of input. */
1387 if (! unformat_user (input, unformat_line_input, line_input))
1390 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1392 if (unformat (line_input, "add"))
1394 else if (unformat (line_input, "del"))
1396 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
1400 error = unformat_parse_error(line_input);
1405 a->address = ip_addr;
1406 vnet_lisp_add_del_map_resolver (a);
1412 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
1413 .path = "lisp map-resolver",
1414 .short_help = "lisp map-resolver add/del <ip_address>",
1415 .function = lisp_add_del_map_resolver_command_fn,
1418 /* Statistics (not really errors) */
1419 #define foreach_lisp_cp_lookup_error \
1421 _(MAP_REQUESTS_SENT, "map-request sent")
1423 static char * lisp_cp_lookup_error_strings[] = {
1424 #define _(sym,string) string,
1425 foreach_lisp_cp_lookup_error
1431 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
1432 foreach_lisp_cp_lookup_error
1434 LISP_CP_LOOKUP_N_ERROR,
1435 } lisp_cp_lookup_error_t;
1439 LISP_CP_LOOKUP_NEXT_DROP,
1440 LISP_CP_LOOKUP_NEXT_IP4_LOOKUP,
1441 LISP_CP_LOOKUP_NEXT_IP6_LOOKUP,
1442 LISP_CP_LOOKUP_N_NEXT,
1443 } lisp_cp_lookup_next_t;
1447 gid_address_t dst_eid;
1448 ip_address_t map_resolver_ip;
1449 } lisp_cp_lookup_trace_t;
1452 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
1454 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1455 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1456 lisp_cp_lookup_trace_t * t = va_arg (*args, lisp_cp_lookup_trace_t *);
1458 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
1459 format_ip_address, &t->map_resolver_ip, format_gid_address,
1465 ip_fib_lookup_with_table (lisp_cp_main_t * lcm, u32 fib_index,
1468 if (ip_addr_version (dst) == IP4)
1469 return ip4_fib_lookup_with_table (lcm->im4, fib_index, &ip_addr_v4(dst),
1472 return ip6_fib_lookup_with_table (lcm->im6, fib_index, &ip_addr_v6(dst));
1476 get_mr_and_local_iface_ip (lisp_cp_main_t *lcm, ip_address_t * mr_ip,
1477 ip_address_t * sloc)
1480 ip_adjacency_t * adj;
1481 ip_interface_address_t * ia = 0;
1482 ip_lookup_main_t * lm;
1483 ip4_address_t * l4 = 0;
1484 ip6_address_t * l6 = 0;
1485 ip_address_t * mrit;
1487 if (vec_len(lcm->map_resolvers) == 0)
1489 clib_warning("No map-resolver configured");
1493 /* find the first mr ip we have a route to and the ip of the
1494 * iface that has a route to it */
1495 vec_foreach(mrit, lcm->map_resolvers)
1497 lm = ip_addr_version (mrit) == IP4 ?
1498 &lcm->im4->lookup_main : &lcm->im6->lookup_main;
1500 adj_index = ip_fib_lookup_with_table (lcm, 0, mrit);
1501 adj = ip_get_adjacency (lm, adj_index);
1506 if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP)
1508 ia = pool_elt_at_index(lm->if_address_pool, adj->if_address_index);
1509 if (ip_addr_version(mrit) == IP4)
1511 l4 = ip_interface_address_get_address (lm, ia);
1515 l6 = ip_interface_address_get_address (lm, ia);
1518 else if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE)
1520 /* find sw_if_index in rewrite header */
1521 u32 sw_if_index = adj->rewrite_header.sw_if_index;
1523 /* find suitable address */
1524 if (ip_addr_version(mrit) == IP4)
1526 /* find the first ip address */
1527 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
1528 sw_if_index, 1 /* unnumbered */,
1530 l4 = ip_interface_address_get_address (&lcm->im4->lookup_main,
1537 /* find the first ip address */
1538 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
1539 sw_if_index, 1 /* unnumbered */,
1541 l6 = ip_interface_address_get_address (&lcm->im6->lookup_main,
1550 ip_addr_v4(sloc).as_u32 = l4->as_u32;
1551 ip_addr_version(sloc) = IP4;
1552 ip_address_copy(mr_ip, mrit);
1557 clib_memcpy (&ip_addr_v6(sloc), l6, sizeof(*l6));
1558 ip_addr_version(sloc) = IP6;
1559 ip_address_copy(mr_ip, mrit);
1564 clib_warning("Can't find map-resolver and local interface ip!");
1568 static gid_address_t *
1569 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
1576 ip_interface_address_t * ia = 0;
1577 gid_address_t gid_data, * gid = &gid_data;
1578 gid_address_t * rlocs = 0;
1579 ip_prefix_t * ippref = &gid_address_ippref (gid);
1580 ip_address_t * rloc = &ip_prefix_addr (ippref);
1582 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
1583 for (i = 0; i < vec_len(loc_set->locator_indices); i++)
1585 loc_indexp = vec_elt_at_index(loc_set->locator_indices, i);
1586 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
1588 ip_addr_version(rloc) = IP4;
1589 /* Add ipv4 locators first TODO sort them */
1590 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
1591 loc->sw_if_index, 1 /* unnumbered */,
1593 l4 = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
1594 ip_addr_v4 (rloc) = l4[0];
1595 ip_prefix_len (ippref) = 32;
1596 vec_add1 (rlocs, gid[0]);
1599 ip_addr_version(rloc) = IP6;
1600 /* Add ipv6 locators */
1601 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
1602 loc->sw_if_index, 1 /* unnumbered */,
1604 l6 = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
1605 ip_addr_v6 (rloc) = l6[0];
1606 ip_prefix_len (ippref) = 128;
1607 vec_add1 (rlocs, gid[0]);
1613 static vlib_buffer_t *
1614 build_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
1615 gid_address_t * seid, gid_address_t * deid,
1616 locator_set_t * loc_set, ip_address_t * mr_ip,
1617 ip_address_t * sloc, u8 is_smr_invoked,
1618 u64 *nonce_res, u32 * bi_res)
1622 gid_address_t * rlocs = 0;
1624 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
1626 clib_warning ("Can't allocate buffer for Map-Request!");
1630 b = vlib_get_buffer (vm, bi);
1632 /* leave some space for the encap headers */
1633 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
1636 rlocs = build_itr_rloc_list (lcm, loc_set);
1639 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked, nonce_res);
1641 /* push ecm: udp-ip-lisp */
1642 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
1644 /* push outer ip header */
1645 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
1656 send_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
1657 gid_address_t * seid, gid_address_t * deid,
1660 u32 next_index, bi = 0, * to_next, map_index;
1664 locator_set_t * loc_set;
1666 pending_map_request_t * pmr;
1667 ip_address_t mr_ip, sloc;
1669 /* get locator-set for seid */
1670 if (!lcm->lisp_pitr)
1672 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
1673 if (map_index == ~0)
1675 clib_warning("No local mapping found in eid-table for %U!",
1676 format_gid_address, seid);
1680 map = pool_elt_at_index (lcm->mapping_pool, map_index);
1684 clib_warning("Mapping found for src eid %U is not marked as local!",
1685 format_gid_address, seid);
1691 map_index = lcm->pitr_map_index;
1692 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1695 loc_set = pool_elt_at_index (lcm->locator_set_pool, map->locator_set_index);
1697 /* get local iface ip to use in map-request XXX fib 0 for now*/
1698 get_mr_and_local_iface_ip (lcm, &mr_ip, &sloc);
1700 /* build the encapsulated map request */
1701 b = build_encapsulated_map_request (vm, lcm, seid, deid, loc_set, &mr_ip,
1702 &sloc, is_smr_invoked, &nonce, &bi);
1707 /* set fib index and lookup node */
1708 vnet_buffer(b)->sw_if_index[VLIB_TX] = ~0;
1709 next_index = (ip_addr_version(&mr_ip) == IP4) ?
1710 ip4_lookup_node.index : ip6_lookup_node.index;
1712 f = vlib_get_frame_to_node (vm, next_index);
1714 /* Enqueue the packet */
1715 to_next = vlib_frame_vector_args (f);
1718 vlib_put_frame_to_node (vm, next_index, f);
1720 /* add map-request to pending requests table */
1721 pool_get(lcm->pending_map_requests_pool, pmr);
1722 gid_address_copy (&pmr->src, seid);
1723 gid_address_copy (&pmr->dst, deid);
1724 pmr->src_mapping_index = map_index;
1725 hash_set(lcm->pending_map_requests_by_nonce, nonce,
1726 pmr - lcm->pending_map_requests_pool);
1730 get_src_and_dst (void *hdr, ip_address_t * src, ip_address_t *dst)
1732 ip4_header_t * ip4 = hdr;
1735 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
1737 ip_addr_v4(src).as_u32 = ip4->src_address.as_u32;
1738 ip_addr_version(src) = IP4;
1739 ip_addr_v4(dst).as_u32 = ip4->dst_address.as_u32;
1740 ip_addr_version(dst) = IP4;
1745 clib_memcpy (&ip_addr_v6(src), &ip6->src_address, sizeof(ip6->src_address));
1746 ip_addr_version(src) = IP6;
1747 clib_memcpy (&ip_addr_v6(dst), &ip6->dst_address, sizeof(ip6->dst_address));
1748 ip_addr_version(dst) = IP6;
1753 lisp_cp_lookup (vlib_main_t * vm, vlib_node_runtime_t * node,
1754 vlib_frame_t * from_frame)
1756 u32 * from, * to_next_drop, di, si;
1757 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main();
1758 u32 pkts_mapped = 0;
1759 uword n_left_from, n_left_to_next_drop;
1761 from = vlib_frame_vector_args (from_frame);
1762 n_left_from = from_frame->n_vectors;
1764 while (n_left_from > 0)
1766 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
1767 to_next_drop, n_left_to_next_drop);
1769 while (n_left_from > 0 && n_left_to_next_drop > 0)
1774 gid_address_t src, dst;
1775 ip_prefix_t * spref, * dpref;
1777 gid_address_type (&src) = GID_ADDR_IP_PREFIX;
1778 spref = &gid_address_ippref(&src);
1779 gid_address_type (&dst) = GID_ADDR_IP_PREFIX;
1780 dpref = &gid_address_ippref(&dst);
1785 to_next_drop[0] = pi0;
1787 n_left_to_next_drop -= 1;
1789 p0 = vlib_get_buffer (vm, pi0);
1790 p0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
1792 /* src/dst eid pair */
1793 ip0 = vlib_buffer_get_current (p0);
1794 get_src_and_dst (ip0, &ip_prefix_addr(spref), &ip_prefix_addr(dpref));
1795 ip_prefix_len(spref) = ip_address_max_len (ip_prefix_version(spref));
1796 ip_prefix_len(dpref) = ip_address_max_len (ip_prefix_version(dpref));
1798 /* if we have remote mapping for destination already in map-chache
1799 add forwarding tunnel directly. If not send a map-request */
1800 di = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
1803 mapping_t * m = vec_elt_at_index (lcm->mapping_pool, di);
1804 /* send a map-request also in case of negative mapping entry
1805 with corresponding action */
1806 if (m->action == ACTION_SEND_MAP_REQUEST)
1808 /* send map-request */
1809 send_encapsulated_map_request (vm, lcm, &src, &dst, 0);
1814 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
1818 add_fwd_entry (lcm, si, di);
1824 /* send map-request */
1825 send_encapsulated_map_request (vm, lcm, &src, &dst, 0);
1829 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
1831 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, p0,
1834 memset(tr, 0, sizeof(*tr));
1835 gid_address_copy (&tr->dst_eid, &dst);
1836 if (vec_len(lcm->map_resolvers) > 0)
1838 clib_memcpy (&tr->map_resolver_ip,
1839 vec_elt_at_index(lcm->map_resolvers, 0),
1840 sizeof(ip_address_t));
1845 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP, n_left_to_next_drop);
1847 vlib_node_increment_counter (vm, node->node_index,
1848 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
1850 return from_frame->n_vectors;
1853 VLIB_REGISTER_NODE (lisp_cp_lookup_node) = {
1854 .function = lisp_cp_lookup,
1855 .name = "lisp-cp-lookup",
1856 .vector_size = sizeof (u32),
1857 .format_trace = format_lisp_cp_lookup_trace,
1858 .type = VLIB_NODE_TYPE_INTERNAL,
1860 .n_errors = LISP_CP_LOOKUP_N_ERROR,
1861 .error_strings = lisp_cp_lookup_error_strings,
1863 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
1866 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
1867 [LISP_CP_LOOKUP_NEXT_IP4_LOOKUP] = "ip4-lookup",
1868 [LISP_CP_LOOKUP_NEXT_IP6_LOOKUP] = "ip6-lookup",
1872 /* lisp_cp_input statistics */
1873 #define foreach_lisp_cp_input_error \
1875 _(MAP_REPLIES_RECEIVED, "map-replies received")
1877 static char * lisp_cp_input_error_strings[] = {
1878 #define _(sym,string) string,
1879 foreach_lisp_cp_input_error
1885 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
1886 foreach_lisp_cp_input_error
1888 LISP_CP_INPUT_N_ERROR,
1889 } lisp_cp_input_error_t;
1893 LISP_CP_INPUT_NEXT_DROP,
1894 LISP_CP_INPUT_N_NEXT,
1895 } lisp_cp_input_next_t;
1899 gid_address_t dst_eid;
1900 ip4_address_t map_resolver_ip;
1901 } lisp_cp_input_trace_t;
1904 format_lisp_cp_input_trace (u8 * s, va_list * args)
1906 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1907 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1908 CLIB_UNUSED(lisp_cp_input_trace_t * t) = va_arg (*args, lisp_cp_input_trace_t *);
1910 s = format (s, "LISP-CP-INPUT: TODO");
1914 ip_interface_address_t *
1915 ip_interface_get_first_interface_address (ip_lookup_main_t *lm, u32 sw_if_index,
1918 vnet_main_t *vnm = vnet_get_main ();
1919 vnet_sw_interface_t * swif = vnet_get_sw_interface (vnm, sw_if_index);
1920 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
1921 sw_if_index = swif->unnumbered_sw_if_index;
1923 (vec_len((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
1924 vec_elt((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
1926 return pool_elt_at_index((lm)->if_address_pool, ia);
1930 ip_interface_get_first_ip_addres (ip_lookup_main_t *lm, u32 sw_if_index,
1933 ip_interface_address_t * ia = ip_interface_get_first_interface_address (
1934 lm, sw_if_index, loop);
1935 return ip_interface_address_get_address (lm, ia);
1939 del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index,
1942 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
1943 fwd_entry_t * fe = 0;
1945 memset(a, 0, sizeof(*a));
1947 feip = hash_get(lcm->fwd_entry_by_mapping_index, dst_map_index);
1951 fe = pool_elt_at_index(lcm->fwd_entry_pool, feip[0]);
1953 /* delete dp fwd entry */
1956 a->dlocator = fe->dst_loc;
1957 a->slocator = fe->src_loc;
1958 a->vni = gid_address_vni(&a->deid);
1959 gid_address_copy(&a->deid, &fe->deid);
1961 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
1963 /* delete entry in fwd table */
1964 hash_unset(lcm->fwd_entry_by_mapping_index, dst_map_index);
1965 pool_put(lcm->fwd_entry_pool, fe);
1969 add_fwd_entry (lisp_cp_main_t* lcm, u32 src_map_index, u32 dst_map_index)
1971 mapping_t * src_map, * dst_map;
1972 locator_set_t * dst_ls, * src_ls;
1973 u32 i, minp = ~0, sw_if_index;
1975 uword * feip = 0, * tidp;
1977 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
1979 memset (a, 0, sizeof(*a));
1981 /* remove entry if it already exists */
1982 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
1984 del_fwd_entry (lcm, src_map_index, dst_map_index);
1986 src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
1987 dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
1989 gid_address_copy (&a->deid, &dst_map->eid);
1990 a->vni = gid_address_vni(&a->deid);
1992 tidp = hash_get(lcm->table_id_by_vni, a->vni);
1995 clib_warning("vni %d not associated to a vrf!", a->vni);
1998 a->table_id = tidp[0];
2000 /* XXX simple forwarding policy: first lowest (value) priority locator */
2001 dst_ls = pool_elt_at_index (lcm->locator_set_pool,
2002 dst_map->locator_set_index);
2003 for (i = 0; i < vec_len (dst_ls->locator_indices); i++)
2005 u32 li = vec_elt (dst_ls->locator_indices, i);
2006 locator_t * l = pool_elt_at_index (lcm->locator_pool, li);
2007 if (l->priority < minp && gid_address_type(&l->address)
2008 == GID_ADDR_IP_PREFIX)
2016 src_ls = pool_elt_at_index(lcm->locator_set_pool,
2017 src_map->locator_set_index);
2018 for (i = 0; i < vec_len(src_ls->locator_indices); i++)
2020 u32 li = vec_elt (src_ls->locator_indices, i);
2021 locator_t * sl = pool_elt_at_index (lcm->locator_pool, li);
2023 if (ip_addr_version(&gid_address_ip(&dl->address)) == IP4)
2026 l4 = ip_interface_get_first_ip_addres (&lcm->im4->lookup_main,
2028 1 /* unnumbered */);
2029 ip_addr_v4(&a->slocator) = *l4;
2030 ip_addr_version(&a->slocator) = IP4;
2035 l6 = ip_interface_get_first_ip_addres (&lcm->im6->lookup_main,
2037 1 /* unnumbered */);
2038 ip_addr_v6(&a->slocator) = *l6;
2039 ip_addr_version(&a->slocator) = IP6;
2044 /* insert data plane forwarding entry */
2047 a->dlocator = gid_address_ip(&dl->address);
2051 a->action = dst_map->action;
2055 u8 ipver = ip_prefix_version(&gid_address_ippref(&a->deid));
2056 a->decap_next_index = (ipver == IP4) ?
2057 LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
2059 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
2061 /* add tunnel to fwd entry table XXX check return value from DP insertion */
2062 pool_get (lcm->fwd_entry_pool, fe);
2063 fe->dst_loc = a->dlocator;
2064 fe->src_loc = a->slocator;
2065 gid_address_copy (&fe->deid, &a->deid);
2066 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
2067 fe - lcm->fwd_entry_pool);
2070 /* return 0 if the two locator sets are identical 1 otherwise */
2072 compare_locators (lisp_cp_main_t *lcm, u32 * old_ls_indexes,
2073 locator_t * new_locators)
2076 locator_t * old_loc, * new_loc;
2078 if (vec_len (old_ls_indexes) != vec_len(new_locators))
2081 for (i = 0; i < vec_len(new_locators); i++)
2083 old_li = vec_elt(old_ls_indexes, i);
2084 old_loc = pool_elt_at_index(lcm->locator_pool, old_li);
2086 new_loc = vec_elt_at_index(new_locators, i);
2088 if (locator_cmp (old_loc, new_loc))
2095 process_map_reply (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2097 mapping_t * old_map;
2099 u32 len = 0, i, ls_index = 0;
2101 vnet_lisp_add_del_locator_set_args_t _ls_arg, * ls_arg = &_ls_arg;
2102 vnet_lisp_add_del_mapping_args_t _m_args, * m_args = &_m_args;
2103 pending_map_request_t * pmr;
2105 map_reply_hdr_t * mrep_hdr;
2107 u32 dst_map_index, mi;
2110 mrep_hdr = vlib_buffer_get_current (b);
2112 /* Check pending requests table and nonce */
2113 nonce = MREP_NONCE(mrep_hdr);
2114 pmr_index = hash_get(lcm->pending_map_requests_by_nonce, nonce);
2117 clib_warning("No pending map-request entry with nonce %lu!", nonce);
2120 pmr = pool_elt_at_index(lcm->pending_map_requests_pool, pmr_index[0]);
2122 vlib_buffer_pull (b, sizeof(*mrep_hdr));
2124 for (i = 0; i < MREP_REC_COUNT(mrep_hdr); i++)
2126 memset (ls_arg, 0, sizeof(*ls_arg));
2127 memset (m_args, 0, sizeof(*m_args));
2129 h = vlib_buffer_get_current (b);
2130 m_args->ttl = clib_net_to_host_u32 (MAP_REC_TTL(h));
2131 m_args->action = MAP_REC_ACTION(h);
2132 m_args->authoritative = MAP_REC_AUTH(h);
2134 len = lisp_msg_parse_mapping_record (b, &m_args->deid, &ls_arg->locators,
2138 clib_warning ("Failed to parse mapping record!");
2139 vec_foreach (loc, ls_arg->locators)
2143 vec_free(ls_arg->locators);
2147 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &m_args->deid);
2148 old_map = mi != ~0 ? pool_elt_at_index(lcm->mapping_pool, mi) : 0;
2150 /* if mapping already exists, decide if locators (and forwarding) should
2151 * be updated and be done */
2152 if (old_map != 0 && !gid_address_cmp (&old_map->eid, &m_args->deid))
2154 locator_set_t * old_ls;
2156 /* update mapping attributes */
2157 old_map->action = m_args->action;
2158 old_map->authoritative = m_args->authoritative;
2159 old_map->ttl = m_args->ttl;
2161 old_ls = pool_elt_at_index(lcm->locator_set_pool,
2162 old_map->locator_set_index);
2163 /* if the two locators are not equal, update them and forwarding
2164 * otherwise there's nothing to be done */
2165 if (compare_locators (lcm, old_ls->locator_indices, ls_arg->locators))
2167 /* set locator-set index to overwrite */
2169 ls_arg->index = old_map->locator_set_index;
2170 vnet_lisp_add_del_locator_set (ls_arg, 0);
2171 add_fwd_entry (lcm, pmr->src_mapping_index, mi);
2177 /* add locator-set */
2180 vnet_lisp_add_del_locator_set (ls_arg, &ls_index);
2184 m_args->locator_set_index = ls_index;
2185 vnet_lisp_add_del_mapping (m_args, &dst_map_index);
2187 /* add forwarding tunnel */
2188 add_fwd_entry (lcm, pmr->src_mapping_index, dst_map_index);
2190 vec_free(ls_arg->locators);
2193 /* remove pending map request entry */
2194 hash_unset(lcm->pending_map_requests_by_nonce, nonce);
2195 pool_put(lcm->pending_map_requests_pool, pmr);
2199 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm, vlib_buffer_t * b)
2201 map_request_hdr_t * mreq_hdr;
2202 gid_address_t src, dst;
2205 gid_address_t * itr_rlocs = 0, * rloc;
2207 mreq_hdr = vlib_buffer_get_current (b);
2208 vlib_buffer_pull (b, sizeof(*mreq_hdr));
2210 // nonce = MREQ_NONCE(mreq_hdr);
2212 if (!MREQ_SMR(mreq_hdr)) {
2213 clib_warning("Only SMR Map-Requests supported for now!");
2218 len = lisp_msg_parse_addr (b, &src);
2222 /* for now we don't do anything with the itr's rlocs */
2223 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs, MREQ_ITR_RLOC_COUNT(mreq_hdr) + 1);
2227 /* TODO: RLOCs are currently unused, so free them for now */
2228 vec_foreach (rloc, itr_rlocs)
2230 gid_address_free (rloc);
2233 /* parse eid records and send SMR-invoked map-requests */
2234 for (i = 0; i < MREQ_REC_COUNT(mreq_hdr); i++)
2236 memset(&dst, 0, sizeof(dst));
2237 len = lisp_msg_parse_eid_rec (b, &dst);
2240 clib_warning("Can't parse map-request EID-record");
2243 /* send SMR-invoked map-requests */
2244 send_encapsulated_map_request (vm, lcm, &dst, &src, /* invoked */ 1);
2249 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2250 vlib_frame_t * from_frame)
2252 u32 n_left_from, * from, * to_next_drop;
2253 lisp_msg_type_e type;
2254 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2256 from = vlib_frame_vector_args (from_frame);
2257 n_left_from = from_frame->n_vectors;
2260 while (n_left_from > 0)
2262 u32 n_left_to_next_drop;
2264 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
2265 to_next_drop, n_left_to_next_drop);
2266 while (n_left_from > 0 && n_left_to_next_drop > 0)
2274 to_next_drop[0] = bi0;
2276 n_left_to_next_drop -= 1;
2278 b0 = vlib_get_buffer (vm, bi0);
2280 type = lisp_msg_type(vlib_buffer_get_current (b0));
2283 case LISP_MAP_REPLY:
2284 process_map_reply (lcm, b0);
2286 case LISP_MAP_REQUEST:
2287 process_map_request(vm, lcm, b0);
2290 clib_warning("Unsupported LISP message type %d", type);
2294 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
2296 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
2302 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP, n_left_to_next_drop);
2304 return from_frame->n_vectors;
2307 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
2308 .function = lisp_cp_input,
2309 .name = "lisp-cp-input",
2310 .vector_size = sizeof (u32),
2311 .format_trace = format_lisp_cp_input_trace,
2312 .type = VLIB_NODE_TYPE_INTERNAL,
2314 .n_errors = LISP_CP_INPUT_N_ERROR,
2315 .error_strings = lisp_cp_input_error_strings,
2317 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2320 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2325 lisp_cp_init (vlib_main_t *vm)
2327 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2328 clib_error_t * error = 0;
2330 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
2333 lcm->im4 = &ip4_main;
2334 lcm->im6 = &ip6_main;
2335 lcm->vlib_main = vm;
2336 lcm->vnet_main = vnet_get_main();
2338 gid_dictionary_init (&lcm->mapping_index_by_gid);
2340 /* default vrf mapped to vni 0 */
2341 hash_set(lcm->table_id_by_vni, 0, 0);
2343 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
2344 lisp_cp_input_node.index, 1 /* is_ip4 */);
2345 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
2346 lisp_cp_input_node.index, 0 /* is_ip4 */);
2351 VLIB_INIT_FUNCTION(lisp_cp_init);