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>
21 ip_interface_address_t *
22 ip_interface_get_first_interface_address (ip_lookup_main_t *lm, u32 sw_if_index,
25 vnet_main_t *vnm = vnet_get_main ();
26 vnet_sw_interface_t * swif = vnet_get_sw_interface (vnm, sw_if_index);
27 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
28 sw_if_index = swif->unnumbered_sw_if_index;
30 (vec_len((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
31 vec_elt((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
33 return pool_elt_at_index((lm)->if_address_pool, ia);
37 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
40 ip_interface_address_t * ia;
42 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
45 return ip_interface_address_get_address (lm, ia);
49 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
50 u8 version, ip_address_t * result)
52 ip_lookup_main_t * lm;
55 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
56 addr = ip_interface_get_first_address (lm, sw_if_index, version);
60 ip_address_set (result, addr, version);
65 ip_fib_lookup_with_table (lisp_cp_main_t * lcm, u32 fib_index,
68 if (ip_addr_version (dst) == IP4)
69 return ip4_fib_lookup_with_table (lcm->im4, fib_index, &ip_addr_v4(dst),
72 return ip6_fib_lookup_with_table (lcm->im6, fib_index, &ip_addr_v6(dst));
76 ip_fib_get_egress_iface_for_dst_with_lm (lisp_cp_main_t * lcm,
78 ip_lookup_main_t * lm)
83 adj_index = ip_fib_lookup_with_table (lcm, 0, dst);
84 adj = ip_get_adjacency (lm, adj_index);
89 /* we only want outgoing routes */
90 if (adj->lookup_next_index != IP_LOOKUP_NEXT_ARP
91 && adj->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
94 return adj->rewrite_header.sw_if_index;
98 * Find the sw_if_index of the interface that would be used to egress towards
102 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
104 ip_lookup_main_t * lm;
106 lm = ip_addr_version (dst) == IP4 ?
107 &lcm->im4->lookup_main : &lcm->im6->lookup_main;
109 return ip_fib_get_egress_iface_for_dst_with_lm (lcm, dst, lm);
113 * Find first IP of the interface that would be used to egress towards dst.
114 * Returns 1 if the address is found 0 otherwise.
117 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
118 ip_address_t * result)
121 ip_lookup_main_t * lm;
127 ipver = ip_addr_version(dst);
129 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
130 si = ip_fib_get_egress_iface_for_dst_with_lm (lcm, dst, lm);
135 /* find the first ip address */
136 addr = ip_interface_get_first_address (lm, si, ipver);
140 ip_address_set (result, addr, ipver);
145 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
147 uword * dp_table, * intf;
148 vnet_lisp_gpe_add_del_iface_args_t _ai, *ai = &_ai;
152 dp_table = hash_get(lcm->table_id_by_vni, vni);
156 clib_warning("vni %d not associated to a vrf!", vni);
157 return VNET_API_ERROR_INVALID_VALUE;
162 dp_table = hash_get(lcm->bd_id_by_vni, vni);
165 clib_warning("vni %d not associated to a bridge domain!", vni);
166 return VNET_API_ERROR_INVALID_VALUE;
170 intf = hash_get(is_l2 ? lcm->l2_dp_intf_by_vni :lcm->dp_intf_by_vni, vni);
172 /* enable/disable data-plane interface */
175 /* create interface */
181 ai->dp_table = dp_table[0];
183 vnet_lisp_gpe_add_del_iface (ai, 0);
185 /* keep track of vnis for which interfaces have been created */
186 hash_set(lcm->dp_intf_by_vni, vni, 1);
193 clib_warning("interface for vni %d doesn't exist!", vni);
194 return VNET_API_ERROR_INVALID_VALUE;
199 ai->dp_table = dp_table[0];
200 vnet_lisp_gpe_add_del_iface (ai, 0);
201 hash_unset(lcm->dp_intf_by_vni, vni);
208 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
210 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
211 fwd_entry_t * fe = 0;
213 memset(a, 0, sizeof(*a));
215 feip = hash_get(lcm->fwd_entry_by_mapping_index, dst_map_index);
219 fe = pool_elt_at_index(lcm->fwd_entry_pool, feip[0]);
221 /* delete dp fwd entry */
224 a->rmt_loc = fe->dst_loc;
225 a->lcl_loc = fe->src_loc;
226 a->vni = gid_address_vni(&a->rmt_eid);
227 gid_address_copy(&a->rmt_eid, &fe->deid);
229 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
231 /* delete entry in fwd table */
232 hash_unset(lcm->fwd_entry_by_mapping_index, dst_map_index);
233 pool_put(lcm->fwd_entry_pool, fe);
237 * Finds first remote locator with best (lowest) priority that has a local
238 * peer locator with an underlying route to it.
242 get_locator_pair (lisp_cp_main_t* lcm, mapping_t * lcl_map, mapping_t * rmt_map,
243 ip_address_t * lcl_loc, ip_address_t * rmt_loc)
245 u32 i, minp = ~0, limitp = 0, li, check_index = 0, done = 0, esi;
246 locator_set_t * rmt_ls, * lcl_ls;
247 ip_address_t _lcl, * lcl = &_lcl;
248 locator_t * l, * rmt = 0;
251 rmt_ls = pool_elt_at_index(lcm->locator_set_pool, rmt_map->locator_set_index);
252 lcl_ls = pool_elt_at_index(lcm->locator_set_pool, lcl_map->locator_set_index);
254 if (!rmt_ls || vec_len(rmt_ls->locator_indices) == 0)
261 /* find unvisited remote locator with best priority */
262 for (i = 0; i < vec_len(rmt_ls->locator_indices); i++)
264 if (0 != hash_get(checked, i))
267 li = vec_elt(rmt_ls->locator_indices, i);
268 l = pool_elt_at_index(lcm->locator_pool, li);
270 /* we don't support non-IP locators for now */
271 if (gid_address_type(&l->address) != GID_ADDR_IP_PREFIX)
274 if (l->priority < minp && l->priority >= limitp)
281 /* check if a local locator with a route to remote locator exists */
284 esi = ip_fib_get_egress_iface_for_dst (
285 lcm, &gid_address_ip(&rmt->address));
289 for (i = 0; i < vec_len(lcl_ls->locator_indices); i++)
291 li = vec_elt (lcl_ls->locator_indices, i);
292 locator_t * sl = pool_elt_at_index (lcm->locator_pool, li);
294 /* found local locator */
295 if (sl->sw_if_index == esi)
297 if (0 == ip_interface_get_first_ip_address (lcm,
299 gid_address_ip_version(&rmt->address), lcl))
302 ip_address_copy(rmt_loc, &gid_address_ip(&rmt->address));
303 ip_address_copy(lcl_loc, lcl);
308 /* skip this remote locator in next searches */
310 hash_set(checked, check_index, 1);
316 return (done == 2) ? 1 : 0;
320 dp_add_fwd_entry (lisp_cp_main_t* lcm, u32 src_map_index, u32 dst_map_index)
322 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
323 mapping_t * src_map, * dst_map;
325 uword * feip = 0, * dpid;
329 memset (a, 0, sizeof(*a));
331 /* remove entry if it already exists */
332 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
334 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
336 src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
337 dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
339 /* insert data plane forwarding entry */
342 gid_address_copy (&a->rmt_eid, &dst_map->eid);
343 a->vni = gid_address_vni(&a->rmt_eid);
345 /* get vrf or bd_index associated to vni */
346 type = gid_address_type(&dst_map->eid);
347 if (GID_ADDR_IP_PREFIX == type)
349 dpid = hash_get(lcm->table_id_by_vni, a->vni);
352 clib_warning("vni %d not associated to a vrf!", a->vni);
355 a->table_id = dpid[0];
357 else if (GID_ADDR_MAC == type)
359 dpid = hash_get(lcm->bd_id_by_vni, a->vni);
362 clib_warning("vni %d not associated to a bridge domain !", a->vni);
368 /* find best locator pair that 1) verifies LISP policy 2) are connected */
369 if (0 == get_locator_pair (lcm, src_map, dst_map, &a->lcl_loc, &a->rmt_loc))
373 a->action = dst_map->action;
377 u8 ipver = ip_prefix_version(&gid_address_ippref(&a->rmt_eid));
378 a->decap_next_index = (ipver == IP4) ?
379 LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
381 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
383 /* add tunnel to fwd entry table XXX check return value from DP insertion */
384 pool_get (lcm->fwd_entry_pool, fe);
385 fe->dst_loc = a->rmt_loc;
386 fe->src_loc = a->lcl_loc;
387 gid_address_copy (&fe->deid, &a->rmt_eid);
388 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
389 fe - lcm->fwd_entry_pool);
393 * Add/remove mapping to/from map-cache. Overwriting not allowed.
396 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
397 u32 * map_index_result)
399 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
400 u32 mi, * map_indexp, map_index, i;
401 mapping_t * m, * old_map;
404 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
405 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
408 /* TODO check if overwriting and take appropriate actions */
409 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid,
412 clib_warning ("eid %U found in the eid-table", format_gid_address,
414 return VNET_API_ERROR_VALUE_EXIST;
417 pool_get(lcm->mapping_pool, m);
418 gid_address_copy (&m->eid, &a->eid);
419 m->locator_set_index = a->locator_set_index;
421 m->action = a->action;
424 map_index = m - lcm->mapping_pool;
425 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
428 if (pool_is_free_index(lcm->locator_set_pool, a->locator_set_index))
430 clib_warning("Locator set with index %d doesn't exist",
431 a->locator_set_index);
432 return VNET_API_ERROR_INVALID_VALUE;
435 /* add eid to list of eids supported by locator-set */
436 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
437 eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids,
438 a->locator_set_index);
439 vec_add1(eid_indexes[0], map_index);
444 vec_add1(lcm->local_mappings_indexes, map_index);
446 map_index_result[0] = map_index;
450 if (mi == GID_LOOKUP_MISS)
452 clib_warning("eid %U not found in the eid-table", format_gid_address,
454 return VNET_API_ERROR_INVALID_VALUE;
457 /* clear locator-set to eids binding */
458 eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids,
459 a->locator_set_index);
460 for (i = 0; i < vec_len(eid_indexes[0]); i++)
462 map_indexp = vec_elt_at_index(eid_indexes[0], i);
463 if (map_indexp[0] == mi)
466 vec_del1(eid_indexes[0], i);
468 /* remove local mark if needed */
469 m = pool_elt_at_index(lcm->mapping_pool, mi);
473 for (k = 0; k < vec_len(lcm->local_mappings_indexes); k++)
475 lm_indexp = vec_elt_at_index(lcm->local_mappings_indexes, k);
476 if (lm_indexp[0] == mi)
479 vec_del1(lcm->local_mappings_indexes, k);
482 /* remove mapping from dictionary */
483 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
484 gid_address_free (&m->eid);
485 pool_put_index (lcm->mapping_pool, mi);
492 * Add/update/delete mapping to/in/from map-cache.
495 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
496 u32 * map_index_result)
498 uword * dp_table = 0;
502 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
504 if (vnet_lisp_enable_disable_status () == 0)
506 clib_warning ("LISP is disabled!");
507 return VNET_API_ERROR_LISP_DISABLED;
510 vni = gid_address_vni(&a->eid);
511 type = gid_address_type(&a->eid);
512 if (GID_ADDR_IP_PREFIX == type)
513 dp_table = hash_get(lcm->table_id_by_vni, vni);
514 else if (GID_ADDR_MAC == type)
515 dp_table = hash_get(lcm->bd_id_by_vni, vni);
519 clib_warning("vni %d not associated to a %s!", vni,
520 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
521 return VNET_API_ERROR_INVALID_VALUE;
524 /* store/remove mapping from map-cache */
525 return vnet_lisp_map_cache_add_del (a, map_index_result);
528 static clib_error_t *
529 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
530 vlib_cli_command_t * cmd)
532 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
533 unformat_input_t _line_input, * line_input = &_line_input;
536 ip_prefix_t * prefp = &gid_address_ippref(&eid);
537 u8 * mac = gid_address_mac(&eid);
538 gid_address_t * eids = 0;
539 clib_error_t * error = 0;
540 u8 * locator_set_name = 0;
541 u32 locator_set_index = 0, map_index = 0;
543 vnet_lisp_add_del_mapping_args_t _a, * a = &_a;
547 memset (&eid, 0, sizeof (eid));
548 /* Get a line of input. */
549 if (! unformat_user (input, unformat_line_input, line_input))
552 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
554 if (unformat (line_input, "add"))
556 else if (unformat (line_input, "del"))
558 else if (unformat (line_input, "vni %d", &vni))
559 gid_address_vni (&eid) = vni;
560 else if (unformat (line_input, "eid %U", unformat_ip_prefix, prefp))
562 gid_address_type (&eid) = GID_ADDR_IP_PREFIX;
565 else if (unformat (line_input, "eid %U", unformat_mac_address, mac))
567 gid_address_type (&eid) = GID_ADDR_MAC;
570 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
572 p = hash_get_mem(lcm->locator_set_index_by_name, locator_set_name);
575 error = clib_error_return(0, "locator-set %s doesn't exist",
579 locator_set_index = p[0];
583 error = unformat_parse_error(line_input);
587 /* XXX treat batch configuration */
591 a->locator_set_index = locator_set_index;
594 rv = vnet_lisp_add_del_local_mapping (a, &map_index);
597 error = clib_error_return(0, "failed to %s local mapping!",
598 is_add ? "add" : "delete");
602 if (locator_set_name)
603 vec_free (locator_set_name);
604 gid_address_free (&a->eid);
608 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
609 .path = "lisp eid-table",
610 .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
611 "locator-set <locator-set>",
612 .function = lisp_add_del_local_eid_command_fn,
616 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
618 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
619 uword * dp_idp, * vnip, ** dp_table_by_vni, ** vni_by_dp_table;
621 if (vnet_lisp_enable_disable_status () == 0)
623 clib_warning ("LISP is disabled!");
627 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
628 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
630 if (!is_l2 && (vni == 0 || dp_id == 0))
632 clib_warning ("can't add/del default vni-vrf mapping!");
636 dp_idp = hash_get (dp_table_by_vni[0], vni);
637 vnip = hash_get (vni_by_dp_table[0], dp_id);
643 clib_warning ("vni %d or vrf %d already used in vrf/vni "
644 "mapping!", vni, dp_id);
647 hash_set (dp_table_by_vni[0], vni, dp_id);
648 hash_set (vni_by_dp_table[0], dp_id, vni);
650 /* create dp iface */
651 dp_add_del_iface (lcm, vni, is_l2, 1);
655 if (!dp_idp || !vnip)
657 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
658 "mapping!", vni, dp_id);
661 hash_unset (dp_table_by_vni[0], vni);
662 hash_unset (vni_by_dp_table[0], dp_id);
664 /* remove dp iface */
665 dp_add_del_iface (lcm, vni, is_l2, 0);
671 static clib_error_t *
672 lisp_eid_table_map_command_fn (vlib_main_t * vm,
673 unformat_input_t * input,
674 vlib_cli_command_t * cmd)
676 u8 is_add = 1, is_l2 = 0;
677 u32 vni = 0, dp_id = 0;
678 unformat_input_t _line_input, * line_input = &_line_input;
680 /* Get a line of input. */
681 if (! unformat_user (input, unformat_line_input, line_input))
684 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
686 if (unformat (line_input, "del"))
688 else if (unformat (line_input, "vni %d", &vni))
690 else if (unformat (line_input, "vrf %d", &dp_id))
692 else if (unformat (line_input, "bd %d", &dp_id))
696 return unformat_parse_error (line_input);
699 vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
703 VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
704 .path = "lisp eid-table map",
705 .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
706 .function = lisp_eid_table_map_command_fn,
710 /* return 0 if the two locator sets are identical 1 otherwise */
712 compare_locators (lisp_cp_main_t *lcm, u32 * old_ls_indexes,
713 locator_t * new_locators)
716 locator_t * old_loc, * new_loc;
718 if (vec_len (old_ls_indexes) != vec_len(new_locators))
721 for (i = 0; i < vec_len(new_locators); i++)
723 old_li = vec_elt(old_ls_indexes, i);
724 old_loc = pool_elt_at_index(lcm->locator_pool, old_li);
726 new_loc = vec_elt_at_index(new_locators, i);
728 if (locator_cmp (old_loc, new_loc))
735 * Adds/removes/updates mapping. Does not program forwarding.
737 * @param eid end-host identifier
738 * @param rlocs vector of remote locators
739 * @param action action for negative map-reply
740 * @param is_add add mapping if non-zero, delete otherwise
741 * @param res_map_index the map-index that was created/updated/removed. It is
742 * set to ~0 if no action is taken.
743 * @return return code
746 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
747 u8 authoritative, u32 ttl, u8 is_add,
750 vnet_lisp_add_del_mapping_args_t _m_args, * m_args = &_m_args;
751 vnet_lisp_add_del_locator_set_args_t _ls_args, * ls_args = &_ls_args;
752 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
753 u32 mi, ls_index = 0, dst_map_index;
756 if (vnet_lisp_enable_disable_status() == 0)
758 clib_warning ("LISP is disabled!");
759 return VNET_API_ERROR_LISP_DISABLED;
763 res_map_index[0] = ~0;
765 memset (m_args, 0, sizeof (m_args[0]));
766 memset (ls_args, 0, sizeof (ls_args[0]));
768 ls_args->locators = rlocs;
770 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
771 old_map = ((u32) ~0 != mi) ? pool_elt_at_index(lcm->mapping_pool, mi) : 0;
775 /* overwrite: if mapping already exists, decide if locators should be
776 * updated and be done */
777 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
779 locator_set_t * old_ls;
781 /* update mapping attributes */
782 old_map->action = action;
783 old_map->authoritative = authoritative;
786 old_ls = pool_elt_at_index(lcm->locator_set_pool,
787 old_map->locator_set_index);
788 if (compare_locators (lcm, old_ls->locator_indices,
791 /* set locator-set index to overwrite */
793 ls_args->index = old_map->locator_set_index;
794 vnet_lisp_add_del_locator_set (ls_args, 0);
796 res_map_index[0] = mi;
805 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
808 gid_address_copy (&m_args->eid, eid);
810 m_args->action = action;
811 m_args->locator_set_index = ls_index;
812 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
815 res_map_index[0] = dst_map_index;
820 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
822 clib_warning("cannot delete mapping for eid %U", format_gid_address,
828 gid_address_copy (&m_args->eid, eid);
829 m_args->locator_set_index = old_map->locator_set_index;
831 /* delete mapping associated from map-cache */
832 vnet_lisp_map_cache_add_del (m_args, 0);
835 ls_args->index = old_map->locator_set_index;
836 /* delete locator set */
837 vnet_lisp_add_del_locator_set (ls_args, 0);
839 /* return old mapping index */
841 res_map_index[0] = mi;
849 vnet_lisp_clear_all_remote_adjacencies (void)
852 u32 mi, * map_indices = 0, * map_indexp;
853 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
854 vnet_lisp_add_del_mapping_args_t _dm_args, * dm_args = &_dm_args;
855 vnet_lisp_add_del_locator_set_args_t _ls, * ls = &_ls;
857 pool_foreach_index (mi, lcm->mapping_pool,
859 vec_add1 (map_indices, mi);
862 vec_foreach (map_indexp, map_indices)
864 mapping_t * map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
867 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
870 gid_address_copy (&dm_args->eid, &map->eid);
871 dm_args->locator_set_index = map->locator_set_index;
873 /* delete mapping associated to fwd entry */
874 vnet_lisp_map_cache_add_del (dm_args, 0);
878 ls->index = map->locator_set_index;
879 /* delete locator set */
880 rv = vnet_lisp_add_del_locator_set (ls, 0);
888 vec_free (map_indices);
893 * Adds adjacency or removes forwarding entry associated to remote mapping.
894 * Note that adjacencies are not stored, they only result in forwarding entries
898 lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
899 gid_address_t * remote_eid, u8 is_add)
901 u32 local_mi, remote_mi = ~0;
903 if (vnet_lisp_enable_disable_status () == 0)
905 clib_warning ("LISP is disabled!");
906 return VNET_API_ERROR_LISP_DISABLED;
909 remote_mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, remote_eid);
910 if (GID_LOOKUP_MISS == remote_mi)
912 clib_warning("Remote eid %U not found. Cannot add adjacency!",
913 format_gid_address, remote_eid);
920 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
923 /* check if source eid has an associated mapping. If pitr mode is on,
924 * just use the pitr's mapping */
925 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
926 gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
929 if (GID_LOOKUP_MISS == local_mi)
931 clib_warning("Local eid %U not found. Cannot add adjacency!",
932 format_gid_address, local_eid);
937 /* update forwarding */
938 dp_add_fwd_entry (lcm, local_mi, remote_mi);
941 dp_del_fwd_entry (lcm, 0, remote_mi);
947 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
949 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
950 return lisp_add_del_adjacency(lcm, &a->seid, &a->deid, a->is_add);
954 * Handler for add/del remote mapping CLI.
956 * @param vm vlib context
957 * @param input input from user
959 * @return pointer to clib error structure
961 static clib_error_t *
962 lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
963 unformat_input_t * input,
964 vlib_cli_command_t * cmd)
966 clib_error_t * error = 0;
967 unformat_input_t _line_input, * line_input = &_line_input;
968 u8 is_add = 1, del_all = 0;
969 locator_t rloc, * rlocs = 0, * curr_rloc = 0;
970 ip_prefix_t * eid_ippref;
972 u8 * dmac = gid_address_mac (&eid);
974 u32 vni, action = ~0, p, w;
977 /* Get a line of input. */
978 if (! unformat_user (input, unformat_line_input, line_input))
981 memset(&eid, 0, sizeof(eid));
982 memset(&rloc, 0, sizeof(rloc));
984 eid_ippref = &gid_address_ippref(&eid);
986 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
988 if (unformat (line_input, "del-all"))
990 else if (unformat (line_input, "del"))
992 else if (unformat (line_input, "add"))
994 else if (unformat (line_input, "eid %U",
995 unformat_ip_prefix, eid_ippref))
997 gid_address_type (&eid) = GID_ADDR_IP_PREFIX;
1000 else if (unformat (line_input, "eid %U",
1001 unformat_mac_address, dmac))
1003 gid_address_type (&eid) = GID_ADDR_MAC;
1006 else if (unformat (line_input, "vni %u", &vni))
1008 gid_address_vni (&eid) = vni;
1010 else if (unformat (line_input, "p %d w %d", &p, &w))
1014 clib_warning ("No RLOC configured for setting priority/weight!");
1017 curr_rloc->priority = p;
1018 curr_rloc->weight = w;
1020 else if (unformat (line_input, "rloc %U", unformat_ip_address,
1023 vec_add1 (rlocs, rloc);
1024 curr_rloc = &rlocs[vec_len (rlocs) - 1];
1026 else if (unformat (line_input, "action %U",
1027 unformat_negative_mapping_action, &action))
1031 clib_warning ("parse error");
1038 clib_warning ("missing eid!");
1046 if (is_add && (~0 == action)
1047 && 0 == vec_len (rlocs))
1049 clib_warning ("no action set for negative map-reply!");
1055 vnet_lisp_clear_all_remote_adjacencies ();
1059 /* TODO build src/dst with seid*/
1061 /* if it's a delete, clean forwarding */
1064 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1065 rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1072 /* add as static remote mapping, i.e., not authoritative and infinite
1074 rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add, 0);
1077 clib_warning("failed to %s remote mapping!", is_add ? "add" : "delete");
1081 unformat_free (line_input);
1085 VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) = {
1086 .path = "lisp remote-mapping",
1087 .short_help = "lisp remote-mapping add|del [del-all] vni <vni> "
1088 "eid <est-eid> [action <no-action|natively-forward|"
1089 "send-map-request|drop>] rloc <dst-locator> [rloc <dst-locator> ... ]",
1090 .function = lisp_add_del_remote_mapping_command_fn,
1094 * Handler for add/del adjacency CLI.
1096 static clib_error_t *
1097 lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
1098 vlib_cli_command_t * cmd)
1100 clib_error_t * error = 0;
1101 unformat_input_t _line_input, * line_input = &_line_input;
1102 vnet_lisp_add_del_adjacency_args_t _a, * a = &_a;
1104 locator_t rloc, * rlocs = 0;
1105 ip_prefix_t * deid_ippref, * seid_ippref;
1106 gid_address_t seid, deid;
1107 u8 * dmac = gid_address_mac (&deid);
1108 u8 * smac = gid_address_mac (&seid);
1109 u8 deid_set = 0, seid_set = 0;
1111 u32 vni, action = ~0;
1114 /* Get a line of input. */
1115 if (! unformat_user (input, unformat_line_input, line_input))
1118 memset(&deid, 0, sizeof(deid));
1119 memset(&seid, 0, sizeof(seid));
1120 memset(&rloc, 0, sizeof(rloc));
1122 seid_ippref = &gid_address_ippref(&seid);
1123 deid_ippref = &gid_address_ippref(&deid);
1125 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1127 if (unformat (line_input, "del"))
1129 else if (unformat (line_input, "add"))
1131 else if (unformat (line_input, "deid %U",
1132 unformat_ip_prefix, deid_ippref))
1134 gid_address_type (&deid) = GID_ADDR_IP_PREFIX;
1137 else if (unformat (line_input, "deid %U",
1138 unformat_mac_address, dmac))
1140 gid_address_type (&deid) = GID_ADDR_MAC;
1143 else if (unformat (line_input, "vni %u", &vni))
1145 gid_address_vni (&seid) = vni;
1146 gid_address_vni (&deid) = vni;
1148 else if (unformat (line_input, "seid %U",
1149 unformat_ip_prefix, seid_ippref))
1151 gid_address_type (&seid) = GID_ADDR_IP_PREFIX;
1154 else if (unformat (line_input, "seid %U",
1155 unformat_mac_address, smac))
1157 gid_address_type (&seid) = GID_ADDR_MAC;
1162 clib_warning ("parse error");
1169 clib_warning ("missing deid!");
1173 if (GID_ADDR_IP_PREFIX == gid_address_type (&deid))
1175 /* if seid not set, make sure the ip version is the same as that
1176 * of the deid. This ensures the seid to be configured will be
1177 * either 0/0 or ::/0 */
1179 ip_prefix_version(seid_ippref) = ip_prefix_version(deid_ippref);
1182 (ip_prefix_version (deid_ippref)
1183 != ip_prefix_version(seid_ippref)))
1185 clib_warning ("source and destination EIDs are not"
1186 " in the same IP family!");
1191 if (is_add && (~0 == action)
1192 && 0 == vec_len (rlocs))
1194 clib_warning ("no action set for negative map-reply!");
1198 memset(a, 0, sizeof(a[0]));
1199 gid_address_copy (&a->seid, &deid);
1200 gid_address_copy (&a->deid, &seid);
1203 rv = vnet_lisp_add_del_adjacency (a);
1206 clib_warning("failed to %s adjacency!", is_add ? "add" : "delete");
1209 unformat_free (line_input);
1215 VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1216 .path = "lisp adjacency",
1217 .short_help = "lisp adjacency add|del vni <vni>"
1218 "deid <dest-eid> seid <src-eid> [action <no-action|natively-forward|"
1219 "send-map-request|drop>] rloc <dst-locator> [rloc <dst-locator> ... ]",
1220 .function = lisp_add_del_adjacency_command_fn,
1224 static clib_error_t *
1225 lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
1226 unformat_input_t * input,
1227 vlib_cli_command_t * cmd)
1229 ip_address_t * addr;
1230 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1232 vec_foreach (addr, lcm->map_resolvers)
1234 vlib_cli_output (vm, "%U", format_ip_address, addr);
1239 VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1240 .path = "show lisp map-resolvers",
1241 .short_help = "show lisp map-resolvers",
1242 .function = lisp_show_map_resolvers_command_fn,
1246 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1248 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1249 u32 locator_set_index = ~0;
1253 if (vnet_lisp_enable_disable_status () == 0)
1255 clib_warning ("LISP is disabled!");
1256 return VNET_API_ERROR_LISP_DISABLED;
1259 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1262 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1265 locator_set_index = p[0];
1269 pool_get (lcm->mapping_pool, m);
1270 m->locator_set_index = locator_set_index;
1272 lcm->pitr_map_index = m - lcm->mapping_pool;
1274 /* enable pitr mode */
1279 /* remove pitr mapping */
1280 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1282 /* disable pitr mode */
1288 static clib_error_t *
1289 lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
1290 unformat_input_t * input,
1291 vlib_cli_command_t * cmd)
1293 u8 locator_name_set = 0;
1294 u8 * locator_set_name = 0;
1296 unformat_input_t _line_input, * line_input = &_line_input;
1297 clib_error_t * error = 0;
1300 /* Get a line of input. */
1301 if (! unformat_user (input, unformat_line_input, line_input))
1304 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1306 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
1307 locator_name_set = 1;
1308 else if (unformat (line_input, "disable"))
1311 return clib_error_return (0, "parse error");
1314 if (!locator_name_set)
1316 clib_warning ("No locator set specified!");
1319 rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1322 error = clib_error_return(0, "failed to %s pitr!",
1323 is_add ? "add" : "delete");
1327 if (locator_set_name)
1328 vec_free (locator_set_name);
1332 VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1333 .path = "lisp pitr",
1334 .short_help = "lisp pitr [disable] ls <locator-set-name>",
1335 .function = lisp_pitr_set_locator_set_command_fn,
1338 static clib_error_t *
1339 lisp_show_pitr_command_fn (vlib_main_t * vm,
1340 unformat_input_t * input,
1341 vlib_cli_command_t * cmd)
1343 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1348 vlib_cli_output (vm, "%=20s%=16s",
1349 "pitr", lcm->lisp_pitr ? "locator-set" : "");
1351 if (!lcm->lisp_pitr) {
1352 vlib_cli_output (vm, "%=20s", "disable");
1356 if (~0 == lcm->pitr_map_index) {
1357 tmp_str = format(0, "N/A");
1359 m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1360 if (~0 != m->locator_set_index) {
1361 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1362 tmp_str = format(0, "%s", ls->name);
1364 tmp_str = format(0, "N/A");
1367 vec_add1(tmp_str, 0);
1369 vlib_cli_output (vm, "%=20s%=16s",
1377 VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1378 .path = "show lisp pitr",
1379 .short_help = "Show pitr",
1380 .function = lisp_show_pitr_command_fn,
1384 format_eid_entry (u8 * s, va_list * args)
1386 vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
1387 lisp_cp_main_t * lcm = va_arg (*args, lisp_cp_main_t *);
1388 mapping_t * mapit = va_arg (*args, mapping_t *);
1389 locator_set_t * ls = va_arg (*args, locator_set_t *);
1390 gid_address_t * gid = &mapit->eid;
1391 u32 ttl = mapit->ttl;
1392 u8 aut = mapit->authoritative;
1397 u8 * type = ls->local ? format(0, "local(%s)", ls->name)
1398 : format(0, "remote");
1400 if (vec_len (ls->locator_indices) == 0)
1402 s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
1407 vec_foreach (loc_index, ls->locator_indices)
1409 locator_t * l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1411 loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1414 loc = format (0, "%U", format_ip_address,
1415 &gid_address_ip (&l->address));
1419 s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
1420 gid, type, loc, ttl, aut);
1424 s = format (s, "%55s%v\n", "", loc);
1430 static clib_error_t *
1431 lisp_show_eid_table_command_fn (vlib_main_t * vm,
1432 unformat_input_t * input,
1433 vlib_cli_command_t * cmd)
1435 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1437 unformat_input_t _line_input, * line_input = &_line_input;
1443 memset (&eid, 0, sizeof(eid));
1445 /* Get a line of input. */
1446 if (!unformat_user (input, unformat_line_input, line_input))
1449 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1451 if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
1453 else if (unformat (line_input, "local"))
1455 else if (unformat(line_input, "remote"))
1458 return clib_error_return (0, "parse error: '%U'",
1459 format_unformat_error, line_input);
1462 vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
1463 "EID", "type", "locators", "ttl", "autoritative");
1467 pool_foreach (mapit, lcm->mapping_pool,
1469 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
1470 mapit->locator_set_index);
1471 if (filter && !((1 == filter && ls->local) ||
1472 (2 == filter && !ls->local)))
1476 vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
1482 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
1486 mapit = pool_elt_at_index (lcm->mapping_pool, mi);
1487 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
1488 mapit->locator_set_index);
1490 if (filter && !((1 == filter && ls->local) ||
1491 (2 == filter && !ls->local)))
1496 vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
1503 VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
1504 .path = "show lisp eid-table",
1505 .short_help = "Shows EID table",
1506 .function = lisp_show_eid_table_command_fn,
1509 /* cleans locator to locator-set data and removes locators not part of
1510 * any locator-set */
1512 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1514 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
1515 locator_set_t * ls = pool_elt_at_index(lcm->locator_set_pool, lsi);
1516 for (i = 0; i < vec_len(ls->locator_indices); i++)
1518 loc_indexp = vec_elt_at_index(ls->locator_indices, i);
1519 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
1521 for (j = 0; j < vec_len(ls_indexes[0]); j++)
1523 ls_indexp = vec_elt_at_index(ls_indexes[0], j);
1524 if (ls_indexp[0] == lsi)
1528 /* delete index for removed locator-set*/
1529 vec_del1(ls_indexes[0], j);
1531 /* delete locator if it's part of no locator-set */
1532 if (vec_len (ls_indexes[0]) == 0)
1534 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1535 vec_add1 (to_be_deleted, i);
1541 for (i = 0; i < vec_len (to_be_deleted); i++)
1543 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1544 vec_del1 (ls->locator_indices, loc_indexp[0]);
1546 vec_free (to_be_deleted);
1550 static inline uword *
1551 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
1553 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1558 /* find locator-set */
1561 p = hash_get_mem(lcm->locator_set_index_by_name, a->name);
1572 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
1579 ASSERT(loc != NULL);
1581 vec_foreach(locit, ls->locator_indices)
1583 itloc = pool_elt_at_index(lcm->locator_pool, locit[0]);
1584 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1585 (!ls->local && !gid_address_cmp(&itloc->address, &loc->address)))
1587 clib_warning("Duplicate locator");
1588 return VNET_API_ERROR_VALUE_EXIST;
1596 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit, u32 ls_index,
1599 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1600 u32 ** ls_indexes = NULL;
1603 ASSERT(locit != NULL);
1605 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
1607 pool_put_index(lcm->locator_pool, locit[0]);
1608 vec_del1(ls->locator_indices, loc_id);
1609 vec_del1(ls_indexes[0], ls_index);
1613 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1614 locator_set_t * ls, u32 * ls_result)
1616 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1617 locator_t * loc = NULL, *itloc = NULL;
1618 uword _p = (u32)~0, * p = &_p;
1619 u32 loc_index = ~0, ls_index = ~0, * locit = NULL, ** ls_indexes = NULL;
1625 if (vnet_lisp_enable_disable_status () == 0)
1627 clib_warning ("LISP is disabled!");
1628 return VNET_API_ERROR_LISP_DISABLED;
1631 p = get_locator_set_index(a, p);
1634 clib_warning("locator-set %v doesn't exist", a->name);
1635 return VNET_API_ERROR_INVALID_ARGUMENT;
1640 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1643 clib_warning("locator-set %d to be overwritten doesn't exist!",
1645 return VNET_API_ERROR_INVALID_ARGUMENT;
1653 ls_result[0] = p[0];
1655 /* allocate locators */
1656 vec_foreach (itloc, a->locators)
1658 ret = is_locator_in_locator_set(lcm, ls, itloc);
1664 pool_get(lcm->locator_pool, loc);
1666 loc_index = loc - lcm->locator_pool;
1668 vec_add1(ls->locator_indices, loc_index);
1670 vec_validate (lcm->locator_to_locator_sets, loc_index);
1671 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
1673 vec_add1(ls_indexes[0], ls_index);
1680 itloc = a->locators;
1682 vec_foreach (locit, ls->locator_indices)
1684 loc = pool_elt_at_index(lcm->locator_pool, locit[0]);
1686 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1688 remove_locator_from_locator_set(ls, locit,
1691 if (0 == loc->local &&
1692 !gid_address_cmp(&loc->address, &itloc->address))
1694 remove_locator_from_locator_set(ls, locit,
1706 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
1709 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1711 uword _p = (u32)~0, * p = &_p;
1716 if (vnet_lisp_enable_disable_status () == 0)
1718 clib_warning ("LISP is disabled!");
1719 return VNET_API_ERROR_LISP_DISABLED;
1724 p = get_locator_set_index(a, p);
1727 if (p && p[0] != (u32)~0)
1729 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1732 clib_warning("locator-set %d to be overwritten doesn't exist!",
1737 /* clean locator to locator-set vectors and remove locators if
1738 * they're not part of another locator-set */
1739 clean_locator_to_locator_set (lcm, p[0]);
1741 /* remove locator indices from locator set */
1742 vec_free(ls->locator_indices);
1747 ls_result[0] = p[0];
1749 /* new locator-set */
1752 pool_get(lcm->locator_set_pool, ls);
1753 memset(ls, 0, sizeof(*ls));
1754 ls_index = ls - lcm->locator_set_pool;
1758 ls->name = vec_dup(a->name);
1760 if (!lcm->locator_set_index_by_name)
1761 lcm->locator_set_index_by_name = hash_create_vec(
1762 /* size */0, sizeof(ls->name[0]), sizeof(uword));
1763 hash_set_mem(lcm->locator_set_index_by_name, ls->name, ls_index);
1765 /* mark as local locator-set */
1766 vec_add1(lcm->local_locator_set_indexes, ls_index);
1768 ls->local = a->local;
1770 ls_result[0] = ls_index;
1773 ret = vnet_lisp_add_del_locator(a, ls, NULL);
1781 p = get_locator_set_index(a, p);
1784 clib_warning("locator-set %v doesn't exists", a->name);
1788 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
1791 clib_warning("locator-set with index %d doesn't exists", p[0]);
1795 if (lcm->mreq_itr_rlocs == p[0])
1797 clib_warning ("Can't delete the locator-set used to constrain "
1798 "the itr-rlocs in map-requests!");
1802 if (vec_len(lcm->locator_set_to_eids) != 0)
1804 eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids, p[0]);
1805 if (vec_len(eid_indexes[0]) != 0)
1807 clib_warning ("Can't delete a locator that supports a mapping!");
1812 /* clean locator to locator-sets data */
1813 clean_locator_to_locator_set (lcm, p[0]);
1819 vec_foreach_index(it, lcm->local_locator_set_indexes)
1821 lsi = vec_elt(lcm->local_locator_set_indexes, it);
1824 vec_del1(lcm->local_locator_set_indexes, it);
1828 hash_unset_mem(lcm->locator_set_index_by_name, ls->name);
1831 vec_free(ls->locator_indices);
1832 pool_put(lcm->locator_set_pool, ls);
1838 vnet_lisp_enable_disable (u8 is_enable)
1841 clib_error_t * error = 0;
1842 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1843 vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
1845 a->is_en = is_enable;
1846 error = vnet_lisp_gpe_enable_disable (a);
1849 return clib_error_return (0, "failed to %s data-plane!",
1850 a->is_en ? "enable" : "disable");
1855 /* enable all l2 and l3 ifaces */
1856 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
1857 dp_add_del_iface(lcm, vni, 0, 1);
1860 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
1861 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
1866 /* clear interface table */
1867 hash_free(lcm->dp_intf_by_vni);
1868 hash_free(lcm->fwd_entry_by_mapping_index);
1869 pool_free(lcm->fwd_entry_pool);
1872 /* update global flag */
1873 lcm->is_enabled = is_enable;
1878 static clib_error_t *
1879 lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
1880 vlib_cli_command_t * cmd)
1882 unformat_input_t _line_input, * line_input = &_line_input;
1886 /* Get a line of input. */
1887 if (! unformat_user (input, unformat_line_input, line_input))
1890 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1892 if (unformat (line_input, "enable"))
1897 else if (unformat (line_input, "disable"))
1901 return clib_error_return (0, "parse error: '%U'",
1902 format_unformat_error, line_input);
1907 return clib_error_return (0, "state not set");
1909 return vnet_lisp_enable_disable (is_enabled);
1912 VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
1914 .short_help = "lisp [enable|disable]",
1915 .function = lisp_enable_disable_command_fn,
1919 vnet_lisp_enable_disable_status (void)
1921 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1922 return lcm->is_enabled;
1926 format_lisp_status (u8 * s, va_list * args)
1928 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1929 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
1932 static clib_error_t *
1933 lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
1934 vlib_cli_command_t * cmd)
1937 msg = format (msg, "feature: %U\ngpe: %U\n",
1938 format_lisp_status, format_vnet_lisp_gpe_status);
1939 vlib_cli_output (vm, "%v", msg);
1944 VLIB_CLI_COMMAND (lisp_show_status_command) = {
1945 .path = "show lisp status",
1946 .short_help = "show lisp status",
1947 .function = lisp_show_status_command_fn,
1950 static clib_error_t *
1951 lisp_show_eid_table_map_command_fn (vlib_main_t * vm, unformat_input_t * input,
1952 vlib_cli_command_t * cmd)
1955 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main ();
1957 vlib_cli_output (vm, "%=10s%=10s", "VNI", "VRF");
1958 hash_foreach_pair (p, lcm->table_id_by_vni,
1960 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
1965 VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
1966 .path = "show lisp eid-table map",
1967 .short_help = "show lisp eid-table vni to vrf mappings",
1968 .function = lisp_show_eid_table_map_command_fn,
1971 static clib_error_t *
1972 lisp_add_del_locator_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
1973 vlib_cli_command_t * cmd)
1975 lisp_gpe_main_t * lgm = &lisp_gpe_main;
1976 vnet_main_t * vnm = lgm->vnet_main;
1977 unformat_input_t _line_input, * line_input = &_line_input;
1979 clib_error_t * error = 0;
1980 u8 * locator_set_name = 0;
1981 locator_t locator, * locators = 0;
1982 vnet_lisp_add_del_locator_set_args_t _a, * a = &_a;
1986 memset(&locator, 0, sizeof(locator));
1987 memset(a, 0, sizeof(a[0]));
1989 /* Get a line of input. */
1990 if (! unformat_user (input, unformat_line_input, line_input))
1993 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1995 if (unformat (line_input, "add %_%v%_", &locator_set_name))
1997 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
1999 else if (unformat (line_input, "iface %U p %d w %d",
2000 unformat_vnet_sw_interface, vnm, &locator.sw_if_index,
2001 &locator.priority, &locator.weight))
2004 vec_add1(locators, locator);
2008 error = unformat_parse_error(line_input);
2013 a->name = locator_set_name;
2014 a->locators = locators;
2018 rv = vnet_lisp_add_del_locator_set(a, &ls_index);
2021 error = clib_error_return(0, "failed to %s locator-set!",
2022 is_add ? "add" : "delete");
2027 if (locator_set_name)
2028 vec_free (locator_set_name);
2032 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2033 .path = "lisp locator-set",
2034 .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2035 "p <priority> w <weight>]",
2036 .function = lisp_add_del_locator_set_command_fn,
2039 static clib_error_t *
2040 lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
2041 vlib_cli_command_t * cmd)
2043 lisp_gpe_main_t * lgm = &lisp_gpe_main;
2044 vnet_main_t * vnm = lgm->vnet_main;
2045 unformat_input_t _line_input, * line_input = &_line_input;
2047 clib_error_t * error = 0;
2048 u8 * locator_set_name = 0;
2049 u8 locator_set_name_set = 0;
2050 locator_t locator, * locators = 0;
2051 vnet_lisp_add_del_locator_set_args_t _a, * a = &_a;
2054 memset(&locator, 0, sizeof(locator));
2055 memset(a, 0, sizeof(a[0]));
2057 /* Get a line of input. */
2058 if (! unformat_user (input, unformat_line_input, line_input))
2061 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2063 if (unformat (line_input, "add"))
2065 else if (unformat (line_input, "del"))
2067 else if (unformat(line_input, "locator-set %_%v%_", &locator_set_name))
2068 locator_set_name_set = 1;
2069 else if (unformat (line_input, "iface %U p %d w %d",
2070 unformat_vnet_sw_interface, vnm, &locator.sw_if_index,
2071 &locator.priority, &locator.weight))
2074 vec_add1(locators, locator);
2078 error = unformat_parse_error(line_input);
2083 if (!locator_set_name_set)
2085 error = clib_error_return(0, "locator_set name not set!");
2089 a->name = locator_set_name;
2090 a->locators = locators;
2094 vnet_lisp_add_del_locator(a, 0, &ls_index);
2098 vec_free (locator_set_name);
2102 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2103 .path = "lisp locator",
2104 .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2105 "p <priority> w <weight>",
2106 .function = lisp_add_del_locator_in_set_command_fn,
2109 static clib_error_t *
2110 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
2111 unformat_input_t * input,
2112 vlib_cli_command_t * cmd)
2114 locator_set_t * lsit;
2117 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2119 vlib_cli_output (vm, "%=20s%=16s%=16s%=16s", "Locator-set", "Locator",
2120 "Priority", "Weight");
2121 pool_foreach (lsit, lcm->locator_set_pool,
2127 msg = format (msg, "%=16v", lsit->name);
2131 msg = format (msg, "%=16s", "remote");
2133 vec_foreach (locit, lsit->locator_indices)
2137 msg = format (msg, "%16s", " ");
2139 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2141 msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
2144 msg = format (msg, "%16U%16d%16d\n", format_ip_address,
2145 &gid_address_ip(&loc->address), loc->priority,
2149 vlib_cli_output (vm, "%v", msg);
2155 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2156 .path = "show lisp locator-set",
2157 .short_help = "Shows locator-sets",
2158 .function = lisp_cp_show_locator_sets_command_fn,
2162 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2164 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2165 ip_address_t * addr;
2168 if (vnet_lisp_enable_disable_status () == 0)
2170 clib_warning ("LISP is disabled!");
2171 return VNET_API_ERROR_LISP_DISABLED;
2176 vec_foreach(addr, lcm->map_resolvers)
2178 if (!ip_address_cmp (addr, &a->address))
2180 clib_warning("map-resolver %U already exists!", format_ip_address,
2185 vec_add1(lcm->map_resolvers, a->address);
2189 for (i = 0; i < vec_len(lcm->map_resolvers); i++)
2191 addr = vec_elt_at_index(lcm->map_resolvers, i);
2192 if (!ip_address_cmp (addr, &a->address))
2194 vec_delete(lcm->map_resolvers, 1, i);
2202 static clib_error_t *
2203 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
2204 unformat_input_t * input,
2205 vlib_cli_command_t * cmd)
2207 unformat_input_t _line_input, * line_input = &_line_input;
2208 u8 is_add = 1, addr_set = 0;
2209 ip_address_t ip_addr;
2210 clib_error_t * error = 0;
2212 vnet_lisp_add_del_map_resolver_args_t _a, * a = &_a;
2214 /* Get a line of input. */
2215 if (! unformat_user (input, unformat_line_input, line_input))
2218 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2220 if (unformat (line_input, "add"))
2222 else if (unformat (line_input, "del"))
2224 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
2228 error = unformat_parse_error(line_input);
2235 error = clib_error_return(0, "Map-resolver address must be set!");
2240 a->address = ip_addr;
2241 rv = vnet_lisp_add_del_map_resolver (a);
2244 error = clib_error_return(0, "failed to %s map-resolver!",
2245 is_add ? "add" : "delete");
2252 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
2253 .path = "lisp map-resolver",
2254 .short_help = "lisp map-resolver add/del <ip_address>",
2255 .function = lisp_add_del_map_resolver_command_fn,
2259 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2261 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2264 if (vnet_lisp_enable_disable_status () == 0)
2266 clib_warning("LISP is disabled!");
2267 return VNET_API_ERROR_LISP_DISABLED;
2272 p = hash_get_mem(lcm->locator_set_index_by_name, a->locator_set_name);
2275 clib_warning("locator-set %v doesn't exist", a->locator_set_name);
2276 return VNET_API_ERROR_INVALID_ARGUMENT;
2279 lcm->mreq_itr_rlocs = p[0];
2283 lcm->mreq_itr_rlocs = ~0;
2289 static clib_error_t *
2290 lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
2291 unformat_input_t * input,
2292 vlib_cli_command_t * cmd)
2294 unformat_input_t _line_input, * line_input = &_line_input;
2296 u8 * locator_set_name = 0;
2297 clib_error_t * error = 0;
2299 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, * a = &_a;
2301 /* Get a line of input. */
2302 if (! unformat_user (input, unformat_line_input, line_input))
2305 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2307 if (unformat (line_input, "del"))
2309 else if (unformat (line_input, "add %s", &locator_set_name))
2313 error = unformat_parse_error(line_input);
2319 a->locator_set_name = locator_set_name;
2320 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
2323 error = clib_error_return(0, "failed to %s map-request itr-rlocs!",
2324 is_add ? "add" : "delete");
2327 vec_free(locator_set_name);
2334 VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
2335 .path = "lisp map-request itr-rlocs",
2336 .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
2337 .function = lisp_add_del_mreq_itr_rlocs_command_fn,
2340 static clib_error_t *
2341 lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
2342 unformat_input_t * input,
2343 vlib_cli_command_t * cmd)
2345 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
2346 locator_set_t * loc_set;
2348 vlib_cli_output (vm, "%=20s", "itr-rlocs");
2350 if (~0 == lcm->mreq_itr_rlocs)
2355 loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
2357 vlib_cli_output (vm, "%=20s", loc_set->name);
2362 VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
2363 .path = "show lisp map-request itr-rlocs",
2364 .short_help = "Shows map-request itr-rlocs",
2365 .function = lisp_show_mreq_itr_rlocs_command_fn,
2368 /* Statistics (not really errors) */
2369 #define foreach_lisp_cp_lookup_error \
2371 _(MAP_REQUESTS_SENT, "map-request sent")
2373 static char * lisp_cp_lookup_error_strings[] = {
2374 #define _(sym,string) string,
2375 foreach_lisp_cp_lookup_error
2381 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
2382 foreach_lisp_cp_lookup_error
2384 LISP_CP_LOOKUP_N_ERROR,
2385 } lisp_cp_lookup_error_t;
2389 LISP_CP_LOOKUP_NEXT_DROP,
2390 LISP_CP_LOOKUP_NEXT_IP4_LOOKUP,
2391 LISP_CP_LOOKUP_NEXT_IP6_LOOKUP,
2392 LISP_CP_LOOKUP_N_NEXT,
2393 } lisp_cp_lookup_next_t;
2397 gid_address_t dst_eid;
2398 ip_address_t map_resolver_ip;
2399 } lisp_cp_lookup_trace_t;
2402 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
2404 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2405 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2406 lisp_cp_lookup_trace_t * t = va_arg (*args, lisp_cp_lookup_trace_t *);
2408 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
2409 format_ip_address, &t->map_resolver_ip, format_gid_address,
2415 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
2416 ip_address_t * sloc)
2418 ip_address_t * mrit;
2420 if (vec_len(lcm->map_resolvers) == 0)
2422 clib_warning("No map-resolver configured");
2426 /* find the first mr ip we have a route to and the ip of the
2427 * iface that has a route to it */
2428 vec_foreach(mrit, lcm->map_resolvers)
2430 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, mrit, sloc)) {
2431 ip_address_copy(mr_ip, mrit);
2436 clib_warning("Can't find map-resolver and local interface ip!");
2440 static gid_address_t *
2441 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2447 ip_interface_address_t * ia = 0;
2448 gid_address_t gid_data, * gid = &gid_data;
2449 gid_address_t * rlocs = 0;
2450 ip_prefix_t * ippref = &gid_address_ippref (gid);
2451 ip_address_t * rloc = &ip_prefix_addr (ippref);
2453 memset (gid, 0, sizeof (gid[0]));
2454 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
2455 for (i = 0; i < vec_len(loc_set->locator_indices); i++)
2457 loc_indexp = vec_elt_at_index(loc_set->locator_indices, i);
2458 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2460 /* Add ipv4 locators first TODO sort them */
2461 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2462 loc->sw_if_index, 1 /* unnumbered */,
2464 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2465 ip_address_set (rloc, addr, IP4);
2466 ip_prefix_len (ippref) = 32;
2467 ip_prefix_normalize (ippref);
2468 vec_add1 (rlocs, gid[0]);
2471 /* Add ipv6 locators */
2472 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2473 loc->sw_if_index, 1 /* unnumbered */,
2475 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2476 ip_address_set (rloc, addr, IP6);
2477 ip_prefix_len (ippref) = 128;
2478 ip_prefix_normalize (ippref);
2479 vec_add1 (rlocs, gid[0]);
2485 static vlib_buffer_t *
2486 build_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
2487 gid_address_t * seid, gid_address_t * deid,
2488 locator_set_t * loc_set, ip_address_t * mr_ip,
2489 ip_address_t * sloc, u8 is_smr_invoked,
2490 u64 *nonce_res, u32 * bi_res)
2494 gid_address_t * rlocs = 0;
2496 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2498 clib_warning ("Can't allocate buffer for Map-Request!");
2502 b = vlib_get_buffer (vm, bi);
2504 /* leave some space for the encap headers */
2505 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2508 rlocs = build_itr_rloc_list (lcm, loc_set);
2511 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked, nonce_res);
2513 /* push ecm: udp-ip-lisp */
2514 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2516 /* push outer ip header */
2517 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2527 send_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
2528 gid_address_t * seid, gid_address_t * deid,
2531 u32 next_index, bi = 0, * to_next, map_index;
2535 locator_set_t * loc_set;
2537 pending_map_request_t * pmr;
2538 ip_address_t mr_ip, sloc;
2541 /* get locator-set for seid */
2542 if (!lcm->lisp_pitr)
2544 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2545 if (map_index == ~0)
2547 clib_warning("No local mapping found in eid-table for %U!",
2548 format_gid_address, seid);
2552 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2556 clib_warning("Mapping found for src eid %U is not marked as local!",
2557 format_gid_address, seid);
2560 ls_index = map->locator_set_index;
2564 map_index = lcm->pitr_map_index;
2565 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
2566 ls_index = map->locator_set_index;
2569 /* overwrite locator set if map-request itr-rlocs configured */
2570 if (~0 != lcm->mreq_itr_rlocs)
2572 ls_index = lcm->mreq_itr_rlocs;
2575 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
2577 /* get local iface ip to use in map-request */
2578 if (0 == get_mr_and_local_iface_ip (lcm, &mr_ip, &sloc))
2581 /* build the encapsulated map request */
2582 b = build_encapsulated_map_request (vm, lcm, seid, deid, loc_set, &mr_ip,
2583 &sloc, is_smr_invoked, &nonce, &bi);
2588 /* set fib index to default and lookup node */
2589 vnet_buffer(b)->sw_if_index[VLIB_TX] = 0;
2590 next_index = (ip_addr_version(&mr_ip) == IP4) ?
2591 ip4_lookup_node.index : ip6_lookup_node.index;
2593 f = vlib_get_frame_to_node (vm, next_index);
2595 /* Enqueue the packet */
2596 to_next = vlib_frame_vector_args (f);
2599 vlib_put_frame_to_node (vm, next_index, f);
2601 /* add map-request to pending requests table */
2602 pool_get(lcm->pending_map_requests_pool, pmr);
2603 gid_address_copy (&pmr->src, seid);
2604 gid_address_copy (&pmr->dst, deid);
2605 hash_set(lcm->pending_map_requests_by_nonce, nonce,
2606 pmr - lcm->pending_map_requests_pool);
2610 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t *dst)
2612 ip4_header_t * ip4 = hdr;
2615 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2617 ip_address_set(src, &ip4->src_address, IP4);
2618 ip_address_set(dst, &ip4->dst_address, IP4);
2623 ip_address_set(src, &ip6->src_address, IP6);
2624 ip_address_set(dst, &ip6->dst_address, IP6);
2629 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2633 u32 vni = ~0, table_id = ~0, fib_index;
2638 ip4_main_t * im4 = &ip4_main;
2639 fib_index = vec_elt (im4->fib_index_by_sw_if_index,
2640 vnet_buffer (b)->sw_if_index[VLIB_RX]);
2641 fib = find_ip4_fib_by_table_index_or_id (im4, fib_index,
2642 IP4_ROUTE_FLAG_FIB_INDEX);
2643 table_id = fib->table_id;
2648 ip6_main_t * im6 = &ip6_main;
2649 fib_index = vec_elt (im6->fib_index_by_sw_if_index,
2650 vnet_buffer (b)->sw_if_index[VLIB_RX]);
2651 fib = find_ip6_fib_by_table_index_or_id (im6, fib_index,
2652 IP6_ROUTE_FLAG_FIB_INDEX);
2653 table_id = fib->table_id;
2656 vnip = hash_get (lcm->vni_by_table_id, table_id);
2660 clib_warning ("vrf %d is not mapped to any vni!", table_id);
2666 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2672 l2input_main_t * l2im = &l2input_main;
2673 l2_input_config_t * config;
2674 l2_bridge_domain_t * bd_config;
2676 sw_if_index0 = vnet_buffer(b)->sw_if_index[VLIB_RX];
2677 config = vec_elt_at_index(l2im->configs, sw_if_index0);
2678 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
2680 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
2684 clib_warning("bridge domain %d is not mapped to any vni!",
2691 get_src_and_dst_eids_from_buffer (lisp_cp_main_t *lcm, vlib_buffer_t * b,
2692 gid_address_t * src, gid_address_t * dst)
2697 type = vnet_buffer(b)->lisp.overlay_afi;
2699 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
2702 u8 version, preflen;
2704 gid_address_type(src) = GID_ADDR_IP_PREFIX;
2705 gid_address_type(dst) = GID_ADDR_IP_PREFIX;
2707 ip = vlib_buffer_get_current (b);
2708 get_src_and_dst_ip (ip, &gid_address_ip(src), &gid_address_ip(dst));
2710 version = gid_address_ip_version(src);
2711 preflen = ip_address_max_len (version);
2712 gid_address_ippref_len(src) = preflen;
2713 gid_address_ippref_len(dst) = preflen;
2715 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
2716 gid_address_vni (dst) = vni;
2717 gid_address_vni (src) = vni;
2719 else if (LISP_AFI_MAC == type)
2721 ethernet_header_t * eh;
2723 eh = vlib_buffer_get_current (b);
2725 gid_address_type(src) = GID_ADDR_MAC;
2726 gid_address_type(dst) = GID_ADDR_MAC;
2727 mac_copy(&gid_address_mac(src), eh->src_address);
2728 mac_copy(&gid_address_mac(dst), eh->dst_address);
2731 vni = lisp_get_vni_from_buffer_eth (lcm, b);
2733 gid_address_vni (dst) = vni;
2734 gid_address_vni (src) = vni;
2739 lisp_cp_lookup (vlib_main_t * vm, vlib_node_runtime_t * node,
2740 vlib_frame_t * from_frame)
2742 u32 * from, * to_next_drop, di, si;
2743 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main();
2744 u32 pkts_mapped = 0;
2745 uword n_left_from, n_left_to_next_drop;
2747 from = vlib_frame_vector_args (from_frame);
2748 n_left_from = from_frame->n_vectors;
2750 while (n_left_from > 0)
2752 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2753 to_next_drop, n_left_to_next_drop);
2755 while (n_left_from > 0 && n_left_to_next_drop > 0)
2759 gid_address_t src, dst;
2764 to_next_drop[0] = pi0;
2766 n_left_to_next_drop -= 1;
2768 b0 = vlib_get_buffer (vm, pi0);
2769 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
2771 /* src/dst eid pair */
2772 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
2774 /* if we have remote mapping for destination already in map-chache
2775 add forwarding tunnel directly. If not send a map-request */
2776 di = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
2779 mapping_t * m = vec_elt_at_index (lcm->mapping_pool, di);
2780 /* send a map-request also in case of negative mapping entry
2781 with corresponding action */
2782 if (m->action == LISP_SEND_MAP_REQUEST)
2784 /* send map-request */
2785 send_encapsulated_map_request (vm, lcm, &src, &dst, 0);
2790 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
2794 dp_add_fwd_entry (lcm, si, di);
2800 /* send map-request */
2801 send_encapsulated_map_request (vm, lcm, &src, &dst, 0);
2805 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
2807 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
2810 memset(tr, 0, sizeof(*tr));
2811 gid_address_copy (&tr->dst_eid, &dst);
2812 if (vec_len(lcm->map_resolvers) > 0)
2814 clib_memcpy (&tr->map_resolver_ip,
2815 vec_elt_at_index(lcm->map_resolvers, 0),
2816 sizeof(ip_address_t));
2819 gid_address_free (&dst);
2820 gid_address_free (&src);
2823 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP, n_left_to_next_drop);
2825 vlib_node_increment_counter (vm, node->node_index,
2826 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
2828 return from_frame->n_vectors;
2831 VLIB_REGISTER_NODE (lisp_cp_lookup_node) = {
2832 .function = lisp_cp_lookup,
2833 .name = "lisp-cp-lookup",
2834 .vector_size = sizeof (u32),
2835 .format_trace = format_lisp_cp_lookup_trace,
2836 .type = VLIB_NODE_TYPE_INTERNAL,
2838 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2839 .error_strings = lisp_cp_lookup_error_strings,
2841 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2844 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2845 [LISP_CP_LOOKUP_NEXT_IP4_LOOKUP] = "ip4-lookup",
2846 [LISP_CP_LOOKUP_NEXT_IP6_LOOKUP] = "ip6-lookup",
2850 /* lisp_cp_input statistics */
2851 #define foreach_lisp_cp_input_error \
2853 _(MAP_REPLIES_RECEIVED, "map-replies received")
2855 static char * lisp_cp_input_error_strings[] = {
2856 #define _(sym,string) string,
2857 foreach_lisp_cp_input_error
2863 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
2864 foreach_lisp_cp_input_error
2866 LISP_CP_INPUT_N_ERROR,
2867 } lisp_cp_input_error_t;
2871 LISP_CP_INPUT_NEXT_DROP,
2872 LISP_CP_INPUT_N_NEXT,
2873 } lisp_cp_input_next_t;
2877 gid_address_t dst_eid;
2878 ip4_address_t map_resolver_ip;
2879 } lisp_cp_input_trace_t;
2882 format_lisp_cp_input_trace (u8 * s, va_list * args)
2884 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2885 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2886 CLIB_UNUSED(lisp_cp_input_trace_t * t) = va_arg (*args, lisp_cp_input_trace_t *);
2888 s = format (s, "LISP-CP-INPUT: TODO");
2893 process_map_reply (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2895 u32 len = 0, i, ttl, dst_map_index = 0;
2897 pending_map_request_t * pmr;
2899 map_reply_hdr_t * mrep_hdr;
2903 u8 authoritative, action;
2904 locator_t * locators = 0, * loc;
2906 mrep_hdr = vlib_buffer_get_current (b);
2908 /* Check pending requests table and nonce */
2909 nonce = MREP_NONCE(mrep_hdr);
2910 pmr_index = hash_get(lcm->pending_map_requests_by_nonce, nonce);
2913 clib_warning("No pending map-request entry with nonce %lu!", nonce);
2916 pmr = pool_elt_at_index(lcm->pending_map_requests_pool, pmr_index[0]);
2918 vlib_buffer_pull (b, sizeof(*mrep_hdr));
2920 for (i = 0; i < MREP_REC_COUNT(mrep_hdr); i++)
2923 h = vlib_buffer_get_current (b);
2924 ttl = clib_net_to_host_u32 (MAP_REC_TTL(h));
2925 action = MAP_REC_ACTION(h);
2926 authoritative = MAP_REC_AUTH(h);
2928 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
2931 clib_warning ("Failed to parse mapping record!");
2932 vec_foreach (loc, locators)
2940 /* insert/update mappings cache */
2941 vnet_lisp_add_del_mapping (&deid, locators, action, authoritative, ttl, 1,
2944 /* try to program forwarding only if mapping saved or updated*/
2945 if ((u32) ~0 != dst_map_index)
2946 lisp_add_del_adjacency (lcm, &pmr->src, &deid, 1);
2951 /* remove pending map request entry */
2952 hash_unset(lcm->pending_map_requests_by_nonce, nonce);
2953 pool_put(lcm->pending_map_requests_pool, pmr);
2957 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm, vlib_buffer_t * b)
2959 map_request_hdr_t * mreq_hdr;
2960 gid_address_t src, dst;
2963 gid_address_t * itr_rlocs = 0, * rloc;
2965 mreq_hdr = vlib_buffer_get_current (b);
2966 vlib_buffer_pull (b, sizeof(*mreq_hdr));
2968 // nonce = MREQ_NONCE(mreq_hdr);
2970 if (!MREQ_SMR(mreq_hdr)) {
2971 clib_warning("Only SMR Map-Requests supported for now!");
2976 len = lisp_msg_parse_addr (b, &src);
2980 /* for now we don't do anything with the itr's rlocs */
2981 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs, MREQ_ITR_RLOC_COUNT(mreq_hdr) + 1);
2985 /* TODO: RLOCs are currently unused, so free them for now */
2986 vec_foreach (rloc, itr_rlocs)
2988 gid_address_free (rloc);
2991 /* parse eid records and send SMR-invoked map-requests */
2992 for (i = 0; i < MREQ_REC_COUNT(mreq_hdr); i++)
2994 memset(&dst, 0, sizeof(dst));
2995 len = lisp_msg_parse_eid_rec (b, &dst);
2998 clib_warning("Can't parse map-request EID-record");
3001 /* send SMR-invoked map-requests */
3002 send_encapsulated_map_request (vm, lcm, &dst, &src, /* invoked */ 1);
3007 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
3008 vlib_frame_t * from_frame)
3010 u32 n_left_from, * from, * to_next_drop;
3011 lisp_msg_type_e type;
3012 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
3014 from = vlib_frame_vector_args (from_frame);
3015 n_left_from = from_frame->n_vectors;
3018 while (n_left_from > 0)
3020 u32 n_left_to_next_drop;
3022 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3023 to_next_drop, n_left_to_next_drop);
3024 while (n_left_from > 0 && n_left_to_next_drop > 0)
3032 to_next_drop[0] = bi0;
3034 n_left_to_next_drop -= 1;
3036 b0 = vlib_get_buffer (vm, bi0);
3038 type = lisp_msg_type(vlib_buffer_get_current (b0));
3041 case LISP_MAP_REPLY:
3042 process_map_reply (lcm, b0);
3044 case LISP_MAP_REQUEST:
3045 process_map_request(vm, lcm, b0);
3048 clib_warning("Unsupported LISP message type %d", type);
3052 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
3054 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
3060 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP, n_left_to_next_drop);
3062 return from_frame->n_vectors;
3065 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3066 .function = lisp_cp_input,
3067 .name = "lisp-cp-input",
3068 .vector_size = sizeof (u32),
3069 .format_trace = format_lisp_cp_input_trace,
3070 .type = VLIB_NODE_TYPE_INTERNAL,
3072 .n_errors = LISP_CP_INPUT_N_ERROR,
3073 .error_strings = lisp_cp_input_error_strings,
3075 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3078 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3083 lisp_cp_init (vlib_main_t *vm)
3085 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
3086 clib_error_t * error = 0;
3088 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3091 lcm->im4 = &ip4_main;
3092 lcm->im6 = &ip6_main;
3093 lcm->vlib_main = vm;
3094 lcm->vnet_main = vnet_get_main();
3095 lcm->mreq_itr_rlocs = ~0;
3098 gid_dictionary_init (&lcm->mapping_index_by_gid);
3100 /* default vrf mapped to vni 0 */
3101 hash_set(lcm->table_id_by_vni, 0, 0);
3102 hash_set(lcm->vni_by_table_id, 0, 0);
3104 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
3105 lisp_cp_input_node.index, 1 /* is_ip4 */);
3106 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
3107 lisp_cp_input_node.index, 0 /* is_ip4 */);
3112 VLIB_INIT_FUNCTION(lisp_cp_init);