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 * @return return code
372 vnet_lisp_add_del_remote_mapping (gid_address_t * deid, gid_address_t * seid,
373 ip_address_t * rlocs, u8 action, u8 is_add)
375 vnet_lisp_add_del_mapping_args_t _dm_args, * dm_args = &_dm_args;
376 vnet_lisp_add_del_mapping_args_t _sm_args, * sm_args = &_sm_args;
377 vnet_lisp_add_del_locator_set_args_t _ls, * ls = &_ls;
378 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
379 u32 mi, ls_index = 0, dst_map_index, src_map_index;
384 memset (sm_args, 0, sizeof (sm_args[0]));
385 memset (dm_args, 0, sizeof (dm_args[0]));
386 memset (ls, 0, sizeof (ls[0]));
388 /* prepare remote locator set */
389 vec_foreach (dl, rlocs)
391 memset (&loc, 0, sizeof (loc));
392 gid_address_ip (&loc.address) = dl[0];
393 vec_add1 (ls->locators, loc);
395 src_map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
397 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, deid);
404 /* process a negative mapping */
405 if (0 == vec_len (rlocs))
406 return lisp_add_del_negative_static_mapping (deid, ls,
409 if ((u32)~0 == src_map_index)
411 clib_warning ("seid %U not found!", format_gid_address, seid);
417 clib_warning ("deid %U marked for removal but not "
418 "found!", format_gid_address, deid);
422 vnet_lisp_add_del_locator_set (ls, &ls_index);
425 gid_address_copy (&dm_args->deid, deid);
427 dm_args->action = action;
428 dm_args->locator_set_index = ls_index;
429 vnet_lisp_add_del_mapping (dm_args, &dst_map_index);
432 add_fwd_entry (lcm, src_map_index, dst_map_index);
439 /* delete forwarding entry */
440 del_fwd_entry (lcm, 0, mi);
442 gid_address_copy (&dm_args->deid, deid);
443 mapping_t * map = pool_elt_at_index (lcm->mapping_pool, mi);
444 dm_args->locator_set_index = map->locator_set_index;
446 /* delete mapping associated to fwd entry */
447 vnet_lisp_add_del_mapping (dm_args, 0);
450 ls->index = map->locator_set_index;
451 /* delete locator set */
452 vnet_lisp_add_del_locator_set (ls, 0);
454 /* update existing locator set */
457 if ((u32)~0 == src_map_index)
459 clib_warning ("seid %U not found!", format_gid_address, seid);
464 locator_set_t * old_ls;
465 old_map = pool_elt_at_index (lcm->mapping_pool, mi);
467 /* update mapping attributes */
468 old_map->action = action;
470 old_ls = pool_elt_at_index(lcm->locator_set_pool,
471 old_map->locator_set_index);
472 if (compare_locators (lcm, old_ls->locator_indices,
475 /* set locator-set index to overwrite */
477 ls->index = old_map->locator_set_index;
478 vnet_lisp_add_del_locator_set (ls, 0);
479 add_fwd_entry (lcm, src_map_index, mi);
486 vec_free (ls->locators);
491 * Handler for add/del remote mapping CLI.
493 * @param vm vlib context
494 * @param input input from user
496 * @return pointer to clib error structure
498 static clib_error_t *
499 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
500 unformat_input_t * input,
501 vlib_cli_command_t * cmd)
503 clib_error_t * error = 0;
504 unformat_input_t _line_input, * line_input = &_line_input;
506 ip_address_t rloc, * rlocs = 0;
507 ip_prefix_t * deid_ippref, * seid_ippref;
508 gid_address_t seid, deid;
511 u32 vni, action = ~0;
513 /* Get a line of input. */
514 if (! unformat_user (input, unformat_line_input, line_input))
517 memset (&deid, 0, sizeof (deid));
518 memset (&seid, 0, sizeof (seid));
520 seid_ippref = &gid_address_ippref(&seid);
521 deid_ippref = &gid_address_ippref(&deid);
523 gid_address_type (&deid) = GID_ADDR_IP_PREFIX;
524 gid_address_type (&seid) = GID_ADDR_IP_PREFIX;
526 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
528 if (unformat (line_input, "del"))
530 if (unformat (line_input, "add"))
532 else if (unformat (line_input, "deid %U",
533 unformat_ip_prefix, deid_ippref))
537 else if (unformat (line_input, "vni %u", &vni))
539 gid_address_set_vni (&seid, vni);
540 gid_address_set_vni (&deid, vni);
542 else if (unformat (line_input, "seid %U",
543 unformat_ip_prefix, seid_ippref))
545 else if (unformat (line_input, "rloc %U", unformat_ip_address, &rloc))
546 vec_add1 (rlocs, rloc);
547 else if (unformat (line_input, "action %s", &s))
549 if (!strcmp ((char *)s, "no-action"))
550 action = ACTION_NONE;
551 if (!strcmp ((char *)s, "natively-forward"))
552 action = ACTION_NATIVELY_FORWARDED;
553 if (!strcmp ((char *)s, "send-map-request"))
554 action = ACTION_SEND_MAP_REQUEST;
555 else if (!strcmp ((char *)s, "drop"))
556 action = ACTION_DROP;
559 clib_warning ("invalid action: '%s'", s);
565 clib_warning ("parse error");
572 clib_warning ("missing deid!");
577 && (ip_prefix_version (deid_ippref)
578 != ip_prefix_version (seid_ippref)))
580 clib_warning ("source and destination EIDs are not"
581 " in the same IP family!");
585 if (is_add && (~0 == action)
586 && 0 == vec_len (rlocs))
588 clib_warning ("no action set for negative map-reply!");
592 int rv = vnet_lisp_add_del_remote_mapping (&deid, &seid, rlocs,
595 clib_warning ("failed to %s remote mapping!",
596 is_add ? "add" : "delete");
599 unformat_free (line_input);
605 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) = {
606 .path = "lisp remote-mapping",
607 .short_help = "lisp remote-mapping add|del vni <vni>"
608 "deid <dest-eid> seid <src-eid> [action <no-action|natively-forward|"
609 "send-map-request|drop>] rloc <dst-locator> [rloc <dst-locator> ... ]",
610 .function = lisp_add_del_remote_mapping_command_fn,
613 static clib_error_t *
614 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
615 unformat_input_t * input,
616 vlib_cli_command_t * cmd)
619 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
621 vec_foreach (addr, lcm->map_resolvers)
623 vlib_cli_output (vm, "%U", format_ip_address, addr);
628 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
629 .path = "show lisp map-resolvers",
630 .short_help = "show lisp map-resolvers",
631 .function = lisp_show_map_resolvers_command_fn,
635 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
637 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
638 u32 locator_set_index = ~0;
642 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
645 clib_warning ("locator-set %v doesn't exist", locator_set_name);
648 locator_set_index = p[0];
652 pool_get (lcm->mapping_pool, m);
653 m->locator_set_index = locator_set_index;
655 lcm->pitr_map_index = m - lcm->mapping_pool;
657 /* enable pitr mode */
662 /* remove pitr mapping */
663 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
665 /* disable pitr mode */
671 static clib_error_t *
672 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
673 unformat_input_t * input,
674 vlib_cli_command_t * cmd)
676 u8 locator_name_set = 0;
677 u8 * locator_set_name = 0;
679 unformat_input_t _line_input, * line_input = &_line_input;
681 /* Get a line of input. */
682 if (! unformat_user (input, unformat_line_input, line_input))
685 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
687 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
688 locator_name_set = 1;
689 else if (unformat (line_input, "disable"))
692 return clib_error_return (0, "parse error");
695 if (!locator_name_set)
697 clib_warning ("No locator set specified!");
700 vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
703 if (locator_set_name)
704 vec_free (locator_set_name);
708 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
710 .short_help = "lisp pitr [disable] ls <locator-set-name>",
711 .function = lisp_pitr_set_locator_set_command_fn,
714 static clib_error_t *
715 lisp_show_local_eid_table_command_fn (vlib_main_t * vm,
716 unformat_input_t * input,
717 vlib_cli_command_t * cmd)
719 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
722 vlib_cli_output (vm, "%=20s%=16s", "EID", "Locator");
723 pool_foreach (mapit, lcm->mapping_pool,
726 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
727 mapit->locator_set_index);
728 vlib_cli_output (vm, "%-16U%16v", format_gid_address, &mapit->eid,
736 VLIB_CLI_COMMAND (lisp_cp_show_local_eid_table_command) = {
737 .path = "show lisp eid-table",
738 .short_help = "Shows local EID table",
739 .function = lisp_show_local_eid_table_command_fn,
742 /* cleans locator to locator-set data and removes locators not part of
745 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
747 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes;
748 locator_set_t * ls = pool_elt_at_index(lcm->locator_set_pool, lsi);
749 for (i = 0; i < vec_len(ls->locator_indices); i++)
751 loc_indexp = vec_elt_at_index(ls->locator_indices, i);
752 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
754 for (j = 0; j < vec_len(ls_indexes[0]); j++)
756 ls_indexp = vec_elt_at_index(ls_indexes[0], j);
757 if (ls_indexp[0] == lsi)
761 /* delete index for removed locator-set*/
762 vec_del1(ls_indexes[0], j);
764 /* delete locator if it's part of no locator-set */
765 if (vec_len (ls_indexes[0]) == 0)
766 pool_put_index(lcm->locator_pool, loc_indexp[0]);
770 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
773 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
775 locator_t * loc, * itloc;
776 uword _p = (u32)~0, * p = &_p;
777 u32 loc_index, ls_index, ** ls_indexes;
782 /* check if overwrite */
784 p = hash_get_mem(lcm->locator_set_index_by_name, a->name);
789 if (p && p[0] != (u32)~0)
791 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
794 clib_warning("locator-set %d to be overwritten doesn't exist!",
799 /* clean locator to locator-set vectors and remove locators if
800 * they're not part of another locator-set */
801 clean_locator_to_locator_set (lcm, p[0]);
803 /* remove locator indices from locator set */
804 vec_free(ls->locator_indices);
811 /* new locator-set */
814 pool_get(lcm->locator_set_pool, ls);
815 ls_index = ls - lcm->locator_set_pool;
819 ls->name = vec_dup(a->name);
821 if (!lcm->locator_set_index_by_name)
822 lcm->locator_set_index_by_name = hash_create_vec(
823 /* size */0, sizeof(ls->name[0]), sizeof(uword));
824 hash_set_mem(lcm->locator_set_index_by_name, ls->name, ls_index);
826 /* mark as local locator-set */
827 vec_add1(lcm->local_locator_set_indexes, ls_index);
829 ls->local = a->local;
831 ls_result[0] = ls_index;
834 /* allocate locators */
835 vec_foreach (itloc, a->locators)
837 pool_get(lcm->locator_pool, loc);
839 loc_index = loc - lcm->locator_pool;
841 vec_add1(ls->locator_indices, loc_index);
843 vec_validate (lcm->locator_to_locator_sets, loc_index);
844 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
846 vec_add1(ls_indexes[0], ls_index);
851 /* find locator-set */
854 p = hash_get_mem(lcm->locator_set_index_by_name, a->name);
857 clib_warning("locator-set %v doesn't exists", a->name);
864 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
867 clib_warning("locator-set with index %d doesn't exists", p[0]);
870 // /* XXX what happens when a mapping is configured to use the loc-set ? */
871 // if (vec_len (vec_elt_at_index(lcm->locator_set_to_eids, p[0])) != 0)
873 // clib_warning ("Can't delete a locator that supports a mapping!");
877 if (vec_len(lcm->locator_set_to_eids) != 0)
879 eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids, p[0]);
880 if (vec_len(eid_indexes[0]) != 0)
882 clib_warning ("Can't delete a locator that supports a mapping!");
887 /* clean locator to locator-sets data */
888 clean_locator_to_locator_set (lcm, p[0]);
894 vec_foreach_index(it, lcm->local_locator_set_indexes)
896 lsi = vec_elt(lcm->local_locator_set_indexes, it);
899 vec_del1(lcm->local_locator_set_indexes, it);
903 hash_unset_mem(lcm->locator_set_index_by_name, ls->name);
906 pool_put(lcm->locator_set_pool, ls);
912 uword *vnet_lisp_get_locator(vnet_lisp_add_del_locator_set_args_t * a,
915 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
920 /* find locator-set */
923 p = hash_get_mem(lcm->locator_set_index_by_name, a->name);
934 vnet_lisp_add_del_locator_set_name (vnet_lisp_add_del_locator_set_args_t * a,
937 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
939 uword _p = (u32)~0, * p = &_p;
941 u32 **eid_indexes = NULL;
944 ASSERT(ls_result != NULL);
946 p = vnet_lisp_get_locator(a, p);
951 if (p && p[0] != (u32)~0)
953 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
956 clib_warning("locator-set %d to be overwritten doesn't exist!",
958 return VNET_API_ERROR_UNSPECIFIED;
961 /* clean locator to locator-set vectors and remove locators if
962 * they're not part of another locator-set */
963 clean_locator_to_locator_set (lcm, p[0]);
965 /* remove locator indices from locator set */
966 vec_free(ls->locator_indices);
973 /* new locator-set */
976 pool_get(lcm->locator_set_pool, ls);
977 ls_index = ls - lcm->locator_set_pool;
981 ls->name = vec_dup(a->name);
983 if (!lcm->locator_set_index_by_name)
984 lcm->locator_set_index_by_name = hash_create_vec(
985 /* size */0, sizeof(ls->name[0]), sizeof(uword));
986 hash_set_mem(lcm->locator_set_index_by_name, ls->name, ls_index);
988 /* mark as local locator-set */
989 vec_add1(lcm->local_locator_set_indexes, ls_index);
991 ls->local = a->local;
992 ls->locator_indices = NULL;
994 ls_result[0] = ls_index;
1001 clib_warning("locator-set %v doesn't exists", a->name);
1002 return VNET_API_ERROR_INVALID_ARGUMENT;
1005 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1008 clib_warning("locator-set with index %d doesn't exists", p[0]);
1009 return VNET_API_ERROR_INVALID_ARGUMENT;
1012 if (vec_len(lcm->locator_set_to_eids) != 0)
1014 eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids, p[0]);
1015 if (vec_len(eid_indexes[0]) != 0)
1017 clib_warning ("Can't delete a locator that supports a mapping!");
1022 /* clean locator to locator-sets data */
1023 clean_locator_to_locator_set (lcm, p[0]);
1029 vec_foreach_index(it, lcm->local_locator_set_indexes)
1031 lsi = vec_elt(lcm->local_locator_set_indexes, it);
1034 vec_del1(lcm->local_locator_set_indexes, it);
1038 hash_unset_mem(lcm->locator_set_index_by_name, ls->name);
1041 pool_put(lcm->locator_set_pool, ls);
1047 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t *a,
1050 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1051 locator_set_t *ls = NULL;
1052 locator_t *loc = NULL, *itloc = NULL;
1053 uword _p = (u32)~0, * p = &_p;
1054 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
1058 ASSERT(ls_result != NULL);
1060 p = vnet_lisp_get_locator(a, p);
1062 clib_warning("locator-set %v doesn't exists", a->name);
1063 return VNET_API_ERROR_INVALID_ARGUMENT;
1070 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1073 clib_warning("locator-set %d to be overwritten doesn't exist!",
1075 return VNET_API_ERROR_INVALID_ARGUMENT;
1079 ls_result[0] = p[0];
1081 /* allocate locators */
1082 itloc = a->locators;
1083 pool_get(lcm->locator_pool, loc);
1085 loc_index = loc - lcm->locator_pool;
1087 vec_add1(ls->locator_indices, loc_index);
1089 vec_validate (lcm->locator_to_locator_sets, loc_index);
1090 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
1092 vec_add1(ls_indexes[0], ls_index);
1096 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1099 clib_warning("locator-set with index %d doesn't exists", p[0]);
1100 return VNET_API_ERROR_INVALID_ARGUMENT;
1105 itloc = a->locators;
1107 vec_foreach (locit, ls->locator_indices)
1109 loc = pool_elt_at_index(lcm->locator_pool, locit[0]);
1110 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1112 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
1114 pool_put_index(lcm->locator_pool, locit[0]);
1115 vec_del1(ls->locator_indices, i);
1116 vec_del1(ls_indexes[0], ls_index);
1126 vnet_lisp_enable_disable (u8 is_enabled)
1128 vnet_lisp_gpe_add_del_iface_args_t _ai, * ai= &_ai;
1129 uword * table_id, * refc;
1131 clib_error_t * error = 0;
1132 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1133 vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
1135 a->is_en = is_enabled;
1136 error = vnet_lisp_gpe_enable_disable (a);
1139 return clib_error_return (0, "failed to %s data-plane!",
1140 a->is_en ? "enable" : "disable");
1145 /* enable all ifaces */
1146 for (i = 0; i < vec_len (lcm->local_mappings_indexes); i++)
1148 mapping_t * m = vec_elt_at_index (lcm->mapping_pool, i);
1150 ai->vni = gid_address_vni (&m->eid);
1152 refc = hash_get (lcm->dp_if_refcount_by_vni, ai->vni);
1155 table_id = hash_get (lcm->table_id_by_vni, ai->vni);
1158 ai->table_id = table_id[0];
1159 /* enables interface and adds defaults */
1160 vnet_lisp_gpe_add_del_iface (ai, 0);
1163 return clib_error_return (0, "no table_id found for vni %u!",
1166 hash_set (lcm->dp_if_refcount_by_vni, ai->vni, 1);
1176 /* clear refcount table */
1177 hash_free (lcm->dp_if_refcount_by_vni);
1178 hash_free (lcm->fwd_entry_by_mapping_index);
1179 pool_free (lcm->fwd_entry_pool);
1182 /* update global flag */
1183 lcm->is_enabled = is_enabled;
1188 static clib_error_t *
1189 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
1190 vlib_cli_command_t * cmd)
1192 unformat_input_t _line_input, * line_input = &_line_input;
1196 /* Get a line of input. */
1197 if (! unformat_user (input, unformat_line_input, line_input))
1200 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1202 if (unformat (line_input, "enable"))
1207 else if (unformat (line_input, "disable"))
1211 return clib_error_return (0, "parse error: '%U'",
1212 format_unformat_error, line_input);
1217 return clib_error_return (0, "state not set");
1219 return vnet_lisp_enable_disable (is_enabled);
1222 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
1224 .short_help = "lisp [enable|disable]",
1225 .function = lisp_enable_disable_command_fn,
1229 vnet_lisp_enable_disable_status (void)
1231 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1232 return lcm->is_enabled;
1236 format_lisp_status (u8 * s, va_list * args)
1238 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1239 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
1242 static clib_error_t *
1243 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
1244 vlib_cli_command_t * cmd)
1247 msg = format (msg, "feature: %U\ngpe: %U\n",
1248 format_lisp_status, format_vnet_lisp_gpe_status);
1249 vlib_cli_output (vm, "%v", msg);
1254 VLIB_CLI_COMMAND (lisp_show_status_command) = {
1255 .path = "show lisp status",
1256 .short_help = "show lisp status",
1257 .function = lisp_show_status_command_fn,
1259 static clib_error_t *
1260 lisp_add_del_locator_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
1261 vlib_cli_command_t * cmd)
1263 lisp_gpe_main_t * lgm = &lisp_gpe_main;
1264 vnet_main_t * vnm = lgm->vnet_main;
1265 unformat_input_t _line_input, * line_input = &_line_input;
1267 clib_error_t * error = 0;
1268 u8 * locator_set_name = 0;
1269 locator_t locator, * locators = 0;
1270 vnet_lisp_add_del_locator_set_args_t _a, * a = &_a;
1273 memset(&locator, 0, sizeof(locator));
1274 memset(a, 0, sizeof(a[0]));
1276 /* Get a line of input. */
1277 if (! unformat_user (input, unformat_line_input, line_input))
1280 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1282 if (unformat (line_input, "add %_%v%_", &locator_set_name))
1284 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
1286 else if (unformat (line_input, "iface %U p %d w %d",
1287 unformat_vnet_sw_interface, vnm, &locator.sw_if_index,
1288 &locator.priority, &locator.weight))
1291 vec_add1(locators, locator);
1295 error = unformat_parse_error(line_input);
1300 a->name = locator_set_name;
1301 a->locators = locators;
1305 vnet_lisp_add_del_locator_set(a, &ls_index);
1309 if (locator_set_name)
1310 vec_free (locator_set_name);
1314 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
1315 .path = "lisp locator-set",
1316 .short_help = "lisp locator-set add/del <name> iface <iface-name> "
1317 "p <priority> w <weight>",
1318 .function = lisp_add_del_locator_set_command_fn,
1321 static clib_error_t *
1322 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
1323 unformat_input_t * input,
1324 vlib_cli_command_t * cmd)
1326 locator_set_t * lsit;
1329 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1331 vlib_cli_output (vm, "%=20s%=16s%=16s%=16s", "Locator-set", "Locator",
1332 "Priority", "Weight");
1333 pool_foreach (lsit, lcm->locator_set_pool,
1336 msg = format (msg, "%-16v", lsit->name);
1337 vec_foreach (locit, lsit->locator_indices)
1339 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1341 msg = format (msg, "%16d%16d%16d", loc->sw_if_index, loc->priority,
1344 msg = format (msg, "%16U%16d%16d", format_gid_address, &loc->address,
1345 loc->priority, loc->weight);
1347 vlib_cli_output (vm, "%v", msg);
1353 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
1354 .path = "show lisp locator-set",
1355 .short_help = "Shows locator-sets",
1356 .function = lisp_cp_show_locator_sets_command_fn,
1360 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
1362 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1363 ip_address_t * addr;
1368 vec_foreach(addr, lcm->map_resolvers)
1370 if (!ip_address_cmp (addr, &a->address))
1372 clib_warning("map-resolver %U already exists!", format_ip_address,
1377 vec_add1(lcm->map_resolvers, a->address);
1381 for (i = 0; i < vec_len(lcm->map_resolvers); i++)
1383 addr = vec_elt_at_index(lcm->map_resolvers, i);
1384 if (!ip_address_cmp (addr, &a->address))
1386 vec_delete(lcm->map_resolvers, 1, i);
1394 static clib_error_t *
1395 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
1396 unformat_input_t * input,
1397 vlib_cli_command_t * cmd)
1399 unformat_input_t _line_input, * line_input = &_line_input;
1401 ip_address_t ip_addr;
1402 clib_error_t * error = 0;
1403 vnet_lisp_add_del_map_resolver_args_t _a, * a = &_a;
1405 /* Get a line of input. */
1406 if (! unformat_user (input, unformat_line_input, line_input))
1409 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1411 if (unformat (line_input, "add"))
1413 else if (unformat (line_input, "del"))
1415 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
1419 error = unformat_parse_error(line_input);
1424 a->address = ip_addr;
1425 vnet_lisp_add_del_map_resolver (a);
1431 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
1432 .path = "lisp map-resolver",
1433 .short_help = "lisp map-resolver add/del <ip_address>",
1434 .function = lisp_add_del_map_resolver_command_fn,
1437 /* Statistics (not really errors) */
1438 #define foreach_lisp_cp_lookup_error \
1440 _(MAP_REQUESTS_SENT, "map-request sent")
1442 static char * lisp_cp_lookup_error_strings[] = {
1443 #define _(sym,string) string,
1444 foreach_lisp_cp_lookup_error
1450 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
1451 foreach_lisp_cp_lookup_error
1453 LISP_CP_LOOKUP_N_ERROR,
1454 } lisp_cp_lookup_error_t;
1458 LISP_CP_LOOKUP_NEXT_DROP,
1459 LISP_CP_LOOKUP_NEXT_IP4_LOOKUP,
1460 LISP_CP_LOOKUP_NEXT_IP6_LOOKUP,
1461 LISP_CP_LOOKUP_N_NEXT,
1462 } lisp_cp_lookup_next_t;
1466 gid_address_t dst_eid;
1467 ip_address_t map_resolver_ip;
1468 } lisp_cp_lookup_trace_t;
1471 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
1473 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1474 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1475 lisp_cp_lookup_trace_t * t = va_arg (*args, lisp_cp_lookup_trace_t *);
1477 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
1478 format_ip_address, &t->map_resolver_ip, format_gid_address,
1484 ip_fib_lookup_with_table (lisp_cp_main_t * lcm, u32 fib_index,
1487 if (ip_addr_version (dst) == IP4)
1488 return ip4_fib_lookup_with_table (lcm->im4, fib_index, &ip_addr_v4(dst),
1491 return ip6_fib_lookup_with_table (lcm->im6, fib_index, &ip_addr_v6(dst));
1495 get_mr_and_local_iface_ip (lisp_cp_main_t *lcm, ip_address_t * mr_ip,
1496 ip_address_t * sloc)
1499 ip_adjacency_t * adj;
1500 ip_interface_address_t * ia = 0;
1501 ip_lookup_main_t * lm;
1502 ip4_address_t * l4 = 0;
1503 ip6_address_t * l6 = 0;
1504 ip_address_t * mrit;
1506 if (vec_len(lcm->map_resolvers) == 0)
1508 clib_warning("No map-resolver configured");
1512 /* find the first mr ip we have a route to and the ip of the
1513 * iface that has a route to it */
1514 vec_foreach(mrit, lcm->map_resolvers)
1516 lm = ip_addr_version (mrit) == IP4 ?
1517 &lcm->im4->lookup_main : &lcm->im6->lookup_main;
1519 adj_index = ip_fib_lookup_with_table (lcm, 0, mrit);
1520 adj = ip_get_adjacency (lm, adj_index);
1525 if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP)
1527 ia = pool_elt_at_index(lm->if_address_pool, adj->if_address_index);
1528 if (ip_addr_version(mrit) == IP4)
1530 l4 = ip_interface_address_get_address (lm, ia);
1534 l6 = ip_interface_address_get_address (lm, ia);
1537 else if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE)
1539 /* find sw_if_index in rewrite header */
1540 u32 sw_if_index = adj->rewrite_header.sw_if_index;
1542 /* find suitable address */
1543 if (ip_addr_version(mrit) == IP4)
1545 /* find the first ip address */
1546 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
1547 sw_if_index, 1 /* unnumbered */,
1549 l4 = ip_interface_address_get_address (&lcm->im4->lookup_main,
1556 /* find the first ip address */
1557 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
1558 sw_if_index, 1 /* unnumbered */,
1560 l6 = ip_interface_address_get_address (&lcm->im6->lookup_main,
1569 ip_addr_v4(sloc).as_u32 = l4->as_u32;
1570 ip_addr_version(sloc) = IP4;
1571 ip_address_copy(mr_ip, mrit);
1576 clib_memcpy (&ip_addr_v6(sloc), l6, sizeof(*l6));
1577 ip_addr_version(sloc) = IP6;
1578 ip_address_copy(mr_ip, mrit);
1583 clib_warning("Can't find map-resolver and local interface ip!");
1587 static gid_address_t *
1588 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
1595 ip_interface_address_t * ia = 0;
1596 gid_address_t gid_data, * gid = &gid_data;
1597 gid_address_t * rlocs = 0;
1598 ip_prefix_t * ippref = &gid_address_ippref (gid);
1599 ip_address_t * rloc = &ip_prefix_addr (ippref);
1601 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
1602 for (i = 0; i < vec_len(loc_set->locator_indices); i++)
1604 loc_indexp = vec_elt_at_index(loc_set->locator_indices, i);
1605 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
1607 ip_addr_version(rloc) = IP4;
1608 /* Add ipv4 locators first TODO sort them */
1609 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
1610 loc->sw_if_index, 1 /* unnumbered */,
1612 l4 = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
1613 ip_addr_v4 (rloc) = l4[0];
1614 ip_prefix_len (ippref) = 32;
1615 vec_add1 (rlocs, gid[0]);
1618 ip_addr_version(rloc) = IP6;
1619 /* Add ipv6 locators */
1620 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
1621 loc->sw_if_index, 1 /* unnumbered */,
1623 l6 = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
1624 ip_addr_v6 (rloc) = l6[0];
1625 ip_prefix_len (ippref) = 128;
1626 vec_add1 (rlocs, gid[0]);
1632 static vlib_buffer_t *
1633 build_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
1634 gid_address_t * seid, gid_address_t * deid,
1635 locator_set_t * loc_set, ip_address_t * mr_ip,
1636 ip_address_t * sloc, u8 is_smr_invoked,
1637 u64 *nonce_res, u32 * bi_res)
1641 gid_address_t * rlocs = 0;
1643 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
1645 clib_warning ("Can't allocate buffer for Map-Request!");
1649 b = vlib_get_buffer (vm, bi);
1651 /* leave some space for the encap headers */
1652 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
1655 rlocs = build_itr_rloc_list (lcm, loc_set);
1658 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked, nonce_res);
1660 /* push ecm: udp-ip-lisp */
1661 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
1663 /* push outer ip header */
1664 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
1675 send_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
1676 gid_address_t * seid, gid_address_t * deid,
1679 u32 next_index, bi = 0, * to_next, map_index;
1683 locator_set_t * loc_set;
1685 pending_map_request_t * pmr;
1686 ip_address_t mr_ip, sloc;
1688 /* get locator-set for seid */
1689 if (!lcm->lisp_pitr)
1691 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
1692 if (map_index == ~0)
1694 clib_warning("No local mapping found in eid-table for %U!",
1695 format_gid_address, seid);
1699 map = pool_elt_at_index (lcm->mapping_pool, map_index);
1703 clib_warning("Mapping found for src eid %U is not marked as local!",
1704 format_gid_address, seid);
1710 map_index = lcm->pitr_map_index;
1711 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1714 loc_set = pool_elt_at_index (lcm->locator_set_pool, map->locator_set_index);
1716 /* get local iface ip to use in map-request XXX fib 0 for now*/
1717 get_mr_and_local_iface_ip (lcm, &mr_ip, &sloc);
1719 /* build the encapsulated map request */
1720 b = build_encapsulated_map_request (vm, lcm, seid, deid, loc_set, &mr_ip,
1721 &sloc, is_smr_invoked, &nonce, &bi);
1726 /* set fib index and lookup node */
1727 vnet_buffer(b)->sw_if_index[VLIB_TX] = ~0;
1728 next_index = (ip_addr_version(&mr_ip) == IP4) ?
1729 ip4_lookup_node.index : ip6_lookup_node.index;
1731 f = vlib_get_frame_to_node (vm, next_index);
1733 /* Enqueue the packet */
1734 to_next = vlib_frame_vector_args (f);
1737 vlib_put_frame_to_node (vm, next_index, f);
1739 /* add map-request to pending requests table */
1740 pool_get(lcm->pending_map_requests_pool, pmr);
1741 gid_address_copy (&pmr->src, seid);
1742 gid_address_copy (&pmr->dst, deid);
1743 pmr->src_mapping_index = map_index;
1744 hash_set(lcm->pending_map_requests_by_nonce, nonce,
1745 pmr - lcm->pending_map_requests_pool);
1749 get_src_and_dst (void *hdr, ip_address_t * src, ip_address_t *dst)
1751 ip4_header_t * ip4 = hdr;
1754 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
1756 ip_addr_v4(src).as_u32 = ip4->src_address.as_u32;
1757 ip_addr_version(src) = IP4;
1758 ip_addr_v4(dst).as_u32 = ip4->dst_address.as_u32;
1759 ip_addr_version(dst) = IP4;
1764 clib_memcpy (&ip_addr_v6(src), &ip6->src_address, sizeof(ip6->src_address));
1765 ip_addr_version(src) = IP6;
1766 clib_memcpy (&ip_addr_v6(dst), &ip6->dst_address, sizeof(ip6->dst_address));
1767 ip_addr_version(dst) = IP6;
1772 lisp_cp_lookup (vlib_main_t * vm, vlib_node_runtime_t * node,
1773 vlib_frame_t * from_frame)
1775 u32 * from, * to_next_drop, di, si;
1776 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main();
1777 u32 pkts_mapped = 0;
1778 uword n_left_from, n_left_to_next_drop;
1780 from = vlib_frame_vector_args (from_frame);
1781 n_left_from = from_frame->n_vectors;
1783 while (n_left_from > 0)
1785 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
1786 to_next_drop, n_left_to_next_drop);
1788 while (n_left_from > 0 && n_left_to_next_drop > 0)
1793 gid_address_t src, dst;
1794 ip_prefix_t * spref, * dpref;
1796 gid_address_type (&src) = GID_ADDR_IP_PREFIX;
1797 spref = &gid_address_ippref(&src);
1798 gid_address_type (&dst) = GID_ADDR_IP_PREFIX;
1799 dpref = &gid_address_ippref(&dst);
1804 to_next_drop[0] = pi0;
1806 n_left_to_next_drop -= 1;
1808 p0 = vlib_get_buffer (vm, pi0);
1809 p0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
1811 /* src/dst eid pair */
1812 ip0 = vlib_buffer_get_current (p0);
1813 get_src_and_dst (ip0, &ip_prefix_addr(spref), &ip_prefix_addr(dpref));
1814 ip_prefix_len(spref) = ip_address_max_len (ip_prefix_version(spref));
1815 ip_prefix_len(dpref) = ip_address_max_len (ip_prefix_version(dpref));
1817 /* if we have remote mapping for destination already in map-chache
1818 add forwarding tunnel directly. If not send a map-request */
1819 di = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
1822 mapping_t * m = vec_elt_at_index (lcm->mapping_pool, di);
1823 /* send a map-request also in case of negative mapping entry
1824 with corresponding action */
1825 if (m->action == ACTION_SEND_MAP_REQUEST)
1827 /* send map-request */
1828 send_encapsulated_map_request (vm, lcm, &src, &dst, 0);
1833 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
1837 add_fwd_entry (lcm, si, di);
1843 /* send map-request */
1844 send_encapsulated_map_request (vm, lcm, &src, &dst, 0);
1848 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
1850 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, p0,
1853 memset(tr, 0, sizeof(*tr));
1854 gid_address_copy (&tr->dst_eid, &dst);
1855 if (vec_len(lcm->map_resolvers) > 0)
1857 clib_memcpy (&tr->map_resolver_ip,
1858 vec_elt_at_index(lcm->map_resolvers, 0),
1859 sizeof(ip_address_t));
1864 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP, n_left_to_next_drop);
1866 vlib_node_increment_counter (vm, node->node_index,
1867 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
1869 return from_frame->n_vectors;
1872 VLIB_REGISTER_NODE (lisp_cp_lookup_node) = {
1873 .function = lisp_cp_lookup,
1874 .name = "lisp-cp-lookup",
1875 .vector_size = sizeof (u32),
1876 .format_trace = format_lisp_cp_lookup_trace,
1877 .type = VLIB_NODE_TYPE_INTERNAL,
1879 .n_errors = LISP_CP_LOOKUP_N_ERROR,
1880 .error_strings = lisp_cp_lookup_error_strings,
1882 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
1885 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
1886 [LISP_CP_LOOKUP_NEXT_IP4_LOOKUP] = "ip4-lookup",
1887 [LISP_CP_LOOKUP_NEXT_IP6_LOOKUP] = "ip6-lookup",
1891 /* lisp_cp_input statistics */
1892 #define foreach_lisp_cp_input_error \
1894 _(MAP_REPLIES_RECEIVED, "map-replies received")
1896 static char * lisp_cp_input_error_strings[] = {
1897 #define _(sym,string) string,
1898 foreach_lisp_cp_input_error
1904 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
1905 foreach_lisp_cp_input_error
1907 LISP_CP_INPUT_N_ERROR,
1908 } lisp_cp_input_error_t;
1912 LISP_CP_INPUT_NEXT_DROP,
1913 LISP_CP_INPUT_N_NEXT,
1914 } lisp_cp_input_next_t;
1918 gid_address_t dst_eid;
1919 ip4_address_t map_resolver_ip;
1920 } lisp_cp_input_trace_t;
1923 format_lisp_cp_input_trace (u8 * s, va_list * args)
1925 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1926 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1927 CLIB_UNUSED(lisp_cp_input_trace_t * t) = va_arg (*args, lisp_cp_input_trace_t *);
1929 s = format (s, "LISP-CP-INPUT: TODO");
1933 ip_interface_address_t *
1934 ip_interface_get_first_interface_address (ip_lookup_main_t *lm, u32 sw_if_index,
1937 vnet_main_t *vnm = vnet_get_main ();
1938 vnet_sw_interface_t * swif = vnet_get_sw_interface (vnm, sw_if_index);
1939 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
1940 sw_if_index = swif->unnumbered_sw_if_index;
1942 (vec_len((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
1943 vec_elt((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
1945 return pool_elt_at_index((lm)->if_address_pool, ia);
1949 ip_interface_get_first_ip_addres (ip_lookup_main_t *lm, u32 sw_if_index,
1952 ip_interface_address_t * ia = ip_interface_get_first_interface_address (
1953 lm, sw_if_index, loop);
1954 return ip_interface_address_get_address (lm, ia);
1958 del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index,
1961 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
1962 fwd_entry_t * fe = 0;
1964 memset(a, 0, sizeof(*a));
1966 feip = hash_get(lcm->fwd_entry_by_mapping_index, dst_map_index);
1970 fe = pool_elt_at_index(lcm->fwd_entry_pool, feip[0]);
1972 /* delete dp fwd entry */
1975 a->dlocator = fe->dst_loc;
1976 a->slocator = fe->src_loc;
1977 a->vni = gid_address_vni(&a->deid);
1978 gid_address_copy(&a->deid, &fe->deid);
1980 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
1982 /* delete entry in fwd table */
1983 hash_unset(lcm->fwd_entry_by_mapping_index, dst_map_index);
1984 pool_put(lcm->fwd_entry_pool, fe);
1988 add_fwd_entry (lisp_cp_main_t* lcm, u32 src_map_index, u32 dst_map_index)
1990 mapping_t * src_map, * dst_map;
1991 locator_set_t * dst_ls, * src_ls;
1992 u32 i, minp = ~0, sw_if_index;
1994 uword * feip = 0, * tidp;
1996 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
1998 memset (a, 0, sizeof(*a));
2000 /* remove entry if it already exists */
2001 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
2003 del_fwd_entry (lcm, src_map_index, dst_map_index);
2005 src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
2006 dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
2008 gid_address_copy (&a->deid, &dst_map->eid);
2009 a->vni = gid_address_vni(&a->deid);
2011 tidp = hash_get(lcm->table_id_by_vni, a->vni);
2014 clib_warning("vni %d not associated to a vrf!", a->vni);
2017 a->table_id = tidp[0];
2019 /* XXX simple forwarding policy: first lowest (value) priority locator */
2020 dst_ls = pool_elt_at_index (lcm->locator_set_pool,
2021 dst_map->locator_set_index);
2022 for (i = 0; i < vec_len (dst_ls->locator_indices); i++)
2024 u32 li = vec_elt (dst_ls->locator_indices, i);
2025 locator_t * l = pool_elt_at_index (lcm->locator_pool, li);
2026 if (l->priority < minp && gid_address_type(&l->address)
2027 == GID_ADDR_IP_PREFIX)
2035 src_ls = pool_elt_at_index(lcm->locator_set_pool,
2036 src_map->locator_set_index);
2037 for (i = 0; i < vec_len(src_ls->locator_indices); i++)
2039 u32 li = vec_elt (src_ls->locator_indices, i);
2040 locator_t * sl = pool_elt_at_index (lcm->locator_pool, li);
2042 if (ip_addr_version(&gid_address_ip(&dl->address)) == IP4)
2045 l4 = ip_interface_get_first_ip_addres (&lcm->im4->lookup_main,
2047 1 /* unnumbered */);
2048 ip_addr_v4(&a->slocator) = *l4;
2049 ip_addr_version(&a->slocator) = IP4;
2054 l6 = ip_interface_get_first_ip_addres (&lcm->im6->lookup_main,
2056 1 /* unnumbered */);
2057 ip_addr_v6(&a->slocator) = *l6;
2058 ip_addr_version(&a->slocator) = IP6;
2063 /* insert data plane forwarding entry */
2066 a->dlocator = gid_address_ip(&dl->address);
2070 a->action = dst_map->action;
2074 u8 ipver = ip_prefix_version(&gid_address_ippref(&a->deid));
2075 a->decap_next_index = (ipver == IP4) ?
2076 LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
2078 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
2080 /* add tunnel to fwd entry table XXX check return value from DP insertion */
2081 pool_get (lcm->fwd_entry_pool, fe);
2082 fe->dst_loc = a->dlocator;
2083 fe->src_loc = a->slocator;
2084 gid_address_copy (&fe->deid, &a->deid);
2085 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
2086 fe - lcm->fwd_entry_pool);
2089 /* return 0 if the two locator sets are identical 1 otherwise */
2091 compare_locators (lisp_cp_main_t *lcm, u32 * old_ls_indexes,
2092 locator_t * new_locators)
2095 locator_t * old_loc, * new_loc;
2097 if (vec_len (old_ls_indexes) != vec_len(new_locators))
2100 for (i = 0; i < vec_len(new_locators); i++)
2102 old_li = vec_elt(old_ls_indexes, i);
2103 old_loc = pool_elt_at_index(lcm->locator_pool, old_li);
2105 new_loc = vec_elt_at_index(new_locators, i);
2107 if (locator_cmp (old_loc, new_loc))
2114 process_map_reply (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2116 mapping_t * old_map;
2118 u32 len = 0, i, ls_index = 0;
2120 vnet_lisp_add_del_locator_set_args_t _ls_arg, * ls_arg = &_ls_arg;
2121 vnet_lisp_add_del_mapping_args_t _m_args, * m_args = &_m_args;
2122 pending_map_request_t * pmr;
2124 map_reply_hdr_t * mrep_hdr;
2126 u32 dst_map_index, mi;
2129 mrep_hdr = vlib_buffer_get_current (b);
2131 /* Check pending requests table and nonce */
2132 nonce = MREP_NONCE(mrep_hdr);
2133 pmr_index = hash_get(lcm->pending_map_requests_by_nonce, nonce);
2136 clib_warning("No pending map-request entry with nonce %lu!", nonce);
2139 pmr = pool_elt_at_index(lcm->pending_map_requests_pool, pmr_index[0]);
2141 vlib_buffer_pull (b, sizeof(*mrep_hdr));
2143 for (i = 0; i < MREP_REC_COUNT(mrep_hdr); i++)
2145 memset (ls_arg, 0, sizeof(*ls_arg));
2146 memset (m_args, 0, sizeof(*m_args));
2148 h = vlib_buffer_get_current (b);
2149 m_args->ttl = clib_net_to_host_u32 (MAP_REC_TTL(h));
2150 m_args->action = MAP_REC_ACTION(h);
2151 m_args->authoritative = MAP_REC_AUTH(h);
2153 len = lisp_msg_parse_mapping_record (b, &m_args->deid, &ls_arg->locators,
2157 clib_warning ("Failed to parse mapping record!");
2158 vec_foreach (loc, ls_arg->locators)
2162 vec_free(ls_arg->locators);
2166 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &m_args->deid);
2167 old_map = mi != ~0 ? pool_elt_at_index(lcm->mapping_pool, mi) : 0;
2169 /* if mapping already exists, decide if locators (and forwarding) should
2170 * be updated and be done */
2171 if (old_map != 0 && !gid_address_cmp (&old_map->eid, &m_args->deid))
2173 locator_set_t * old_ls;
2175 /* update mapping attributes */
2176 old_map->action = m_args->action;
2177 old_map->authoritative = m_args->authoritative;
2178 old_map->ttl = m_args->ttl;
2180 old_ls = pool_elt_at_index(lcm->locator_set_pool,
2181 old_map->locator_set_index);
2182 /* if the two locators are not equal, update them and forwarding
2183 * otherwise there's nothing to be done */
2184 if (compare_locators (lcm, old_ls->locator_indices, ls_arg->locators))
2186 /* set locator-set index to overwrite */
2188 ls_arg->index = old_map->locator_set_index;
2189 vnet_lisp_add_del_locator_set (ls_arg, 0);
2190 add_fwd_entry (lcm, pmr->src_mapping_index, mi);
2196 /* add locator-set */
2199 vnet_lisp_add_del_locator_set (ls_arg, &ls_index);
2203 m_args->locator_set_index = ls_index;
2204 vnet_lisp_add_del_mapping (m_args, &dst_map_index);
2206 /* add forwarding tunnel */
2207 add_fwd_entry (lcm, pmr->src_mapping_index, dst_map_index);
2209 vec_free(ls_arg->locators);
2212 /* remove pending map request entry */
2213 hash_unset(lcm->pending_map_requests_by_nonce, nonce);
2214 pool_put(lcm->pending_map_requests_pool, pmr);
2218 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm, vlib_buffer_t * b)
2220 map_request_hdr_t * mreq_hdr;
2221 gid_address_t src, dst;
2224 gid_address_t * itr_rlocs = 0, * rloc;
2226 mreq_hdr = vlib_buffer_get_current (b);
2227 vlib_buffer_pull (b, sizeof(*mreq_hdr));
2229 // nonce = MREQ_NONCE(mreq_hdr);
2231 if (!MREQ_SMR(mreq_hdr)) {
2232 clib_warning("Only SMR Map-Requests supported for now!");
2237 len = lisp_msg_parse_addr (b, &src);
2241 /* for now we don't do anything with the itr's rlocs */
2242 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs, MREQ_ITR_RLOC_COUNT(mreq_hdr) + 1);
2246 /* TODO: RLOCs are currently unused, so free them for now */
2247 vec_foreach (rloc, itr_rlocs)
2249 gid_address_free (rloc);
2252 /* parse eid records and send SMR-invoked map-requests */
2253 for (i = 0; i < MREQ_REC_COUNT(mreq_hdr); i++)
2255 memset(&dst, 0, sizeof(dst));
2256 len = lisp_msg_parse_eid_rec (b, &dst);
2259 clib_warning("Can't parse map-request EID-record");
2262 /* send SMR-invoked map-requests */
2263 send_encapsulated_map_request (vm, lcm, &dst, &src, /* invoked */ 1);
2268 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2269 vlib_frame_t * from_frame)
2271 u32 n_left_from, * from, * to_next_drop;
2272 lisp_msg_type_e type;
2273 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2275 from = vlib_frame_vector_args (from_frame);
2276 n_left_from = from_frame->n_vectors;
2279 while (n_left_from > 0)
2281 u32 n_left_to_next_drop;
2283 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
2284 to_next_drop, n_left_to_next_drop);
2285 while (n_left_from > 0 && n_left_to_next_drop > 0)
2293 to_next_drop[0] = bi0;
2295 n_left_to_next_drop -= 1;
2297 b0 = vlib_get_buffer (vm, bi0);
2299 type = lisp_msg_type(vlib_buffer_get_current (b0));
2302 case LISP_MAP_REPLY:
2303 process_map_reply (lcm, b0);
2305 case LISP_MAP_REQUEST:
2306 process_map_request(vm, lcm, b0);
2309 clib_warning("Unsupported LISP message type %d", type);
2313 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
2315 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
2321 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP, n_left_to_next_drop);
2323 return from_frame->n_vectors;
2326 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
2327 .function = lisp_cp_input,
2328 .name = "lisp-cp-input",
2329 .vector_size = sizeof (u32),
2330 .format_trace = format_lisp_cp_input_trace,
2331 .type = VLIB_NODE_TYPE_INTERNAL,
2333 .n_errors = LISP_CP_INPUT_N_ERROR,
2334 .error_strings = lisp_cp_input_error_strings,
2336 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2339 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2344 lisp_cp_init (vlib_main_t *vm)
2346 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2347 clib_error_t * error = 0;
2349 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
2352 lcm->im4 = &ip4_main;
2353 lcm->im6 = &ip6_main;
2354 lcm->vlib_main = vm;
2355 lcm->vnet_main = vnet_get_main();
2357 gid_dictionary_init (&lcm->mapping_index_by_gid);
2359 /* default vrf mapped to vni 0 */
2360 hash_set(lcm->table_id_by_vni, 0, 0);
2362 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
2363 lisp_cp_input_node.index, 1 /* is_ip4 */);
2364 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
2365 lisp_cp_input_node.index, 0 /* is_ip4 */);
2370 VLIB_INIT_FUNCTION(lisp_cp_init);