2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vlibmemory/api.h>
17 #include <vnet/lisp-cp/control.h>
18 #include <vnet/lisp-cp/packets.h>
19 #include <vnet/lisp-cp/lisp_msg_serdes.h>
20 #include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
21 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
22 #include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
23 #include <vnet/fib/fib_entry.h>
24 #include <vnet/fib/fib_table.h>
25 #include <vnet/ethernet/arp_packet.h>
26 #include <vnet/ethernet/packet.h>
28 #include <openssl/evp.h>
29 #include <openssl/hmac.h>
31 #define MAX_VALUE_U24 0xffffff
33 lisp_cp_main_t lisp_control_main;
35 u8 *format_lisp_cp_input_trace (u8 * s, va_list * args);
39 LISP_CP_INPUT_NEXT_DROP,
41 } lisp_cp_input_next_t;
52 vnet_lisp_get_map_request_mode (void)
54 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
55 return lcm->map_request_mode;
59 auth_data_len_by_key_id (lisp_key_type_t key_id)
64 return SHA1_AUTH_DATA_LEN;
65 case HMAC_SHA_256_128:
66 return SHA256_AUTH_DATA_LEN;
68 clib_warning ("unsupported key type: %d!", key_id);
75 get_encrypt_fcn (lisp_key_type_t key_id)
81 case HMAC_SHA_256_128:
84 clib_warning ("unsupported encryption key type: %d!", key_id);
91 queue_map_request (gid_address_t * seid, gid_address_t * deid,
92 u8 smr_invoked, u8 is_resend);
94 ip_interface_address_t *
95 ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
96 u32 sw_if_index, u8 loop)
98 vnet_main_t *vnm = vnet_get_main ();
99 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
100 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
101 sw_if_index = swif->unnumbered_sw_if_index;
103 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
104 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
106 return pool_elt_at_index ((lm)->if_address_pool, ia);
110 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
113 ip_interface_address_t *ia;
115 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
118 return ip_interface_address_get_address (lm, ia);
122 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
123 u8 version, ip_address_t * result)
125 ip_lookup_main_t *lm;
128 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
129 addr = ip_interface_get_first_address (lm, sw_if_index, version);
133 ip_address_set (result, addr, version);
138 * convert from a LISP address to a FIB prefix
141 ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
143 if (addr->version == IP4)
146 prefix->fp_proto = FIB_PROTOCOL_IP4;
147 memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
148 memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
152 prefix->fp_len = 128;
153 prefix->fp_proto = FIB_PROTOCOL_IP6;
154 memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
159 * convert from a LISP to a FIB prefix
162 ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
163 fib_prefix_t * fib_prefix)
165 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
166 fib_prefix->fp_len = ip_prefix->len;
170 * Find the sw_if_index of the interface that would be used to egress towards
174 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
176 fib_node_index_t fei;
179 ip_address_to_fib_prefix (dst, &prefix);
181 fei = fib_table_lookup (0, &prefix);
183 return (fib_entry_get_resolving_interface (fei));
187 * Find first IP of the interface that would be used to egress towards dst.
188 * Returns 1 if the address is found 0 otherwise.
191 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
192 ip_address_t * result)
195 ip_lookup_main_t *lm;
199 ASSERT (result != 0);
201 ipver = ip_addr_version (dst);
203 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
204 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
209 /* find the first ip address */
210 addr = ip_interface_get_first_address (lm, si, ipver);
214 ip_address_set (result, addr, ipver);
219 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
225 dp_table = hash_get (lcm->table_id_by_vni, vni);
229 clib_warning ("vni %d not associated to a vrf!", vni);
230 return VNET_API_ERROR_INVALID_VALUE;
235 dp_table = hash_get (lcm->bd_id_by_vni, vni);
238 clib_warning ("vni %d not associated to a bridge domain!", vni);
239 return VNET_API_ERROR_INVALID_VALUE;
243 /* enable/disable data-plane interface */
247 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
249 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
254 lisp_gpe_tenant_l2_iface_unlock (vni);
256 lisp_gpe_tenant_l3_iface_unlock (vni);
263 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
265 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
268 memset (a, 0, sizeof (*a));
270 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
274 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
276 /* delete dp fwd entry */
279 a->locator_pairs = fe->locator_pairs;
280 a->vni = gid_address_vni (&fe->reid);
281 gid_address_copy (&a->rmt_eid, &fe->reid);
283 gid_address_copy (&a->lcl_eid, &fe->leid);
285 vnet_lisp_gpe_del_fwd_counters (a, feip[0]);
286 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
288 /* delete entry in fwd table */
289 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
290 vec_free (fe->locator_pairs);
291 pool_put (lcm->fwd_entry_pool, fe);
295 * Finds first remote locator with best (lowest) priority that has a local
296 * peer locator with an underlying route to it.
300 get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
301 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
303 u32 i, limitp = 0, li, found = 0, esi;
304 locator_set_t *rmt_ls, *lcl_ls;
305 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
306 locator_t *lp, *rmt = 0;
311 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
313 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
315 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
322 /* find unvisited remote locator with best priority */
323 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
325 if (0 != hash_get (checked, i))
328 li = vec_elt (rmt_ls->locator_indices, i);
329 lp = pool_elt_at_index (lcm->locator_pool, li);
331 /* we don't support non-IP locators for now */
332 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
335 if ((found && lp->priority == limitp)
336 || (!found && lp->priority >= limitp))
340 /* don't search for locators with lower priority and don't
341 * check this locator again*/
342 limitp = lp->priority;
343 hash_set (checked, i, 1);
347 /* check if a local locator with a route to remote locator exists */
350 /* find egress sw_if_index for rmt locator */
352 ip_fib_get_egress_iface_for_dst (lcm,
353 &gid_address_ip (&rmt->address));
354 if ((u32) ~ 0 == esi)
357 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
359 li = vec_elt (lcl_ls->locator_indices, i);
360 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
362 /* found local locator with the needed sw_if_index */
363 if (sl->sw_if_index == esi)
365 /* and it has an address */
366 if (0 == ip_interface_get_first_ip_address (lcm,
368 gid_address_ip_version
373 memset (&pair, 0, sizeof (pair));
374 ip_address_copy (&pair.rmt_loc,
375 &gid_address_ip (&rmt->address));
376 ip_address_copy (&pair.lcl_loc, lcl_addr);
377 pair.weight = rmt->weight;
378 pair.priority = rmt->priority;
379 vec_add1 (locator_pairs[0], pair);
393 gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
396 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
400 switch (fid_addr_type (fid))
402 case FID_ADDR_IP_PREF:
403 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
404 gid_address_ippref (dst) = fid_addr_ippref (fid);
407 gid_address_type (dst) = GID_ADDR_MAC;
408 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
411 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
417 vnet_lisp_map_register_state_get (void)
419 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
420 return lcm->map_registering;
424 vnet_lisp_rloc_probe_state_get (void)
426 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
427 return lcm->rloc_probing;
431 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
433 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
434 gid_address_t *rmt_eid, *lcl_eid;
435 mapping_t *lcl_map, *rmt_map;
437 uword *feip = 0, *dpid;
439 u8 type, is_src_dst = 0;
442 memset (a, 0, sizeof (*a));
444 /* remove entry if it already exists */
445 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
447 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
450 * Determine local mapping and eid
453 lcl_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
455 lcl_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
456 lcl_eid = &lcl_map->eid;
459 * Determine remote mapping and eid
461 rmt_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
462 rmt_eid = &rmt_map->eid;
465 * Build and insert data plane forwarding entry
469 if (MR_MODE_SRC_DST == lcm->map_request_mode)
471 if (GID_ADDR_SRC_DST == gid_address_type (rmt_eid))
473 gid_address_sd_to_flat (&a->rmt_eid, rmt_eid,
474 &gid_address_sd_dst (rmt_eid));
475 gid_address_sd_to_flat (&a->lcl_eid, rmt_eid,
476 &gid_address_sd_src (rmt_eid));
480 gid_address_copy (&a->rmt_eid, rmt_eid);
481 gid_address_copy (&a->lcl_eid, lcl_eid);
486 gid_address_copy (&a->rmt_eid, rmt_eid);
488 a->vni = gid_address_vni (&a->rmt_eid);
489 a->is_src_dst = is_src_dst;
491 /* get vrf or bd_index associated to vni */
492 type = gid_address_type (&a->rmt_eid);
493 if (GID_ADDR_IP_PREFIX == type)
495 dpid = hash_get (lcm->table_id_by_vni, a->vni);
498 clib_warning ("vni %d not associated to a vrf!", a->vni);
501 a->table_id = dpid[0];
503 else if (GID_ADDR_MAC == type)
505 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
508 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
514 /* find best locator pair that 1) verifies LISP policy 2) are connected */
515 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
517 /* Either rmt mapping is negative or we can't find underlay path.
518 * Try again with petr if configured */
519 if (rv == 0 && (lcm->flags & LISP_FLAG_USE_PETR))
521 rmt_map = lisp_get_petr_mapping (lcm);
522 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
529 a->action = rmt_map->action;
532 rv = vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
535 if (a->locator_pairs)
536 vec_free (a->locator_pairs);
540 /* add tunnel to fwd entry table */
541 pool_get (lcm->fwd_entry_pool, fe);
542 vnet_lisp_gpe_add_fwd_counters (a, fe - lcm->fwd_entry_pool);
544 fe->locator_pairs = a->locator_pairs;
545 gid_address_copy (&fe->reid, &a->rmt_eid);
548 gid_address_copy (&fe->leid, &a->lcl_eid);
550 gid_address_copy (&fe->leid, lcl_eid);
552 fe->is_src_dst = is_src_dst;
553 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
554 fe - lcm->fwd_entry_pool);
561 } fwd_entry_mt_arg_t;
564 dp_add_fwd_entry_thread_fn (void *arg)
566 fwd_entry_mt_arg_t *a = arg;
567 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
568 dp_add_fwd_entry (lcm, a->si, a->di);
573 dp_add_fwd_entry_from_mt (u32 si, u32 di)
575 fwd_entry_mt_arg_t a;
577 memset (&a, 0, sizeof (a));
581 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
582 (u8 *) & a, sizeof (a));
587 * Returns vector of adjacencies.
589 * The caller must free the vector returned by this function.
591 * @param vni virtual network identifier
592 * @return vector of adjacencies
595 vnet_lisp_adjacencies_get_by_vni (u32 vni)
597 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
599 lisp_adjacency_t *adjs = 0, adj;
602 pool_foreach(fwd, lcm->fwd_entry_pool,
604 if (gid_address_vni (&fwd->reid) != vni)
607 gid_address_copy (&adj.reid, &fwd->reid);
608 gid_address_copy (&adj.leid, &fwd->leid);
609 vec_add1 (adjs, adj);
617 get_map_server (ip_address_t * a)
619 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
622 vec_foreach (m, lcm->map_servers)
624 if (!ip_address_cmp (&m->address, a))
633 get_map_resolver (ip_address_t * a)
635 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
638 vec_foreach (m, lcm->map_resolvers)
640 if (!ip_address_cmp (&m->address, a))
649 vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
652 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
653 lisp_msmr_t _ms, *ms = &_ms;
655 if (vnet_lisp_enable_disable_status () == 0)
657 clib_warning ("LISP is disabled!");
658 return VNET_API_ERROR_LISP_DISABLED;
663 if (get_map_server (addr))
665 clib_warning ("map-server %U already exists!", format_ip_address,
670 memset (ms, 0, sizeof (*ms));
671 ip_address_copy (&ms->address, addr);
672 vec_add1 (lcm->map_servers, ms[0]);
676 for (i = 0; i < vec_len (lcm->map_servers); i++)
678 ms = vec_elt_at_index (lcm->map_servers, i);
679 if (!ip_address_cmp (&ms->address, addr))
681 vec_del1 (lcm->map_servers, i);
691 * Add/remove mapping to/from map-cache. Overwriting not allowed.
694 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
695 u32 * map_index_result)
697 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
698 u32 mi, *map_indexp, map_index, i;
699 mapping_t *m, *old_map;
702 if (gid_address_type (&a->eid) == GID_ADDR_NSH)
704 if (gid_address_vni (&a->eid) != 0)
706 clib_warning ("Supported only default VNI for NSH!");
707 return VNET_API_ERROR_INVALID_ARGUMENT;
709 if (gid_address_nsh_spi (&a->eid) > MAX_VALUE_U24)
711 clib_warning ("SPI is greater than 24bit!");
712 return VNET_API_ERROR_INVALID_ARGUMENT;
716 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
717 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
720 /* TODO check if overwriting and take appropriate actions */
721 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
723 clib_warning ("eid %U found in the eid-table", format_gid_address,
725 return VNET_API_ERROR_VALUE_EXIST;
728 pool_get (lcm->mapping_pool, m);
729 gid_address_copy (&m->eid, &a->eid);
730 m->locator_set_index = a->locator_set_index;
732 m->action = a->action;
734 m->is_static = a->is_static;
735 m->key = vec_dup (a->key);
736 m->key_id = a->key_id;
738 map_index = m - lcm->mapping_pool;
739 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
742 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
744 clib_warning ("Locator set with index %d doesn't exist",
745 a->locator_set_index);
746 return VNET_API_ERROR_INVALID_VALUE;
749 /* add eid to list of eids supported by locator-set */
750 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
751 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
752 a->locator_set_index);
753 vec_add1 (eid_indexes[0], map_index);
758 vec_add1 (lcm->local_mappings_indexes, map_index);
760 map_index_result[0] = map_index;
764 if (mi == GID_LOOKUP_MISS)
766 clib_warning ("eid %U not found in the eid-table",
767 format_gid_address, &a->eid);
768 return VNET_API_ERROR_INVALID_VALUE;
771 /* clear locator-set to eids binding */
772 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
773 a->locator_set_index);
774 for (i = 0; i < vec_len (eid_indexes[0]); i++)
776 map_indexp = vec_elt_at_index (eid_indexes[0], i);
777 if (map_indexp[0] == mi)
780 vec_del1 (eid_indexes[0], i);
782 /* remove local mark if needed */
783 m = pool_elt_at_index (lcm->mapping_pool, mi);
787 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
789 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
790 if (lm_indexp[0] == mi)
793 vec_del1 (lcm->local_mappings_indexes, k);
796 /* remove mapping from dictionary */
797 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
798 gid_address_free (&m->eid);
799 pool_put_index (lcm->mapping_pool, mi);
806 * Add/update/delete mapping to/in/from map-cache.
809 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
810 u32 * map_index_result)
816 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
818 if (vnet_lisp_enable_disable_status () == 0)
820 clib_warning ("LISP is disabled!");
821 return VNET_API_ERROR_LISP_DISABLED;
824 vni = gid_address_vni (&a->eid);
825 type = gid_address_type (&a->eid);
826 if (GID_ADDR_IP_PREFIX == type)
827 dp_table = hash_get (lcm->table_id_by_vni, vni);
828 else if (GID_ADDR_MAC == type)
829 dp_table = hash_get (lcm->bd_id_by_vni, vni);
831 if (!dp_table && GID_ADDR_NSH != type)
833 clib_warning ("vni %d not associated to a %s!", vni,
834 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
835 return VNET_API_ERROR_INVALID_VALUE;
838 /* store/remove mapping from map-cache */
839 return vnet_lisp_map_cache_add_del (a, map_index_result);
843 add_l2_arp_bd (BVT (clib_bihash_kv) * kvp, void *arg)
846 u32 bd = (u32) kvp->key[0];
847 hash_set (ht[0], bd, 0);
851 vnet_lisp_l2_arp_bds_get (void)
853 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
856 gid_dict_foreach_l2_arp_entry (&lcm->mapping_index_by_gid,
857 add_l2_arp_bd, &bds);
865 } lisp_add_l2_arp_args_t;
868 add_l2_arp_entry (BVT (clib_bihash_kv) * kvp, void *arg)
870 lisp_add_l2_arp_args_t *a = arg;
871 lisp_api_l2_arp_entry_t **vector = a->vector, e;
873 if ((u32) kvp->key[0] == a->bd)
875 mac_copy (e.mac, (void *) &kvp->value);
876 e.ip4 = (u32) kvp->key[1];
877 vec_add1 (vector[0], e);
881 lisp_api_l2_arp_entry_t *
882 vnet_lisp_l2_arp_entries_get_by_bd (u32 bd)
884 lisp_api_l2_arp_entry_t *entries = 0;
885 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
886 lisp_add_l2_arp_args_t a;
891 gid_dict_foreach_l2_arp_entry (&lcm->mapping_index_by_gid,
892 add_l2_arp_entry, &a);
897 vnet_lisp_add_del_l2_arp_entry (gid_address_t * key, u8 * mac, u8 is_add)
899 if (vnet_lisp_enable_disable_status () == 0)
901 clib_warning ("LISP is disabled!");
902 return VNET_API_ERROR_LISP_DISABLED;
905 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
908 u64 res = gid_dictionary_lookup (&lcm->mapping_index_by_gid, key);
911 if (res != GID_LOOKUP_MISS_L2)
913 clib_warning ("Entry %U exists in DB!", format_gid_address, key);
914 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
916 u64 val = mac_to_u64 (mac);
917 gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, val,
922 if (res == GID_LOOKUP_MISS_L2)
924 clib_warning ("ONE ARP entry %U not found - cannot delete!",
925 format_gid_address, key);
928 gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, 0,
936 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
938 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
939 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
941 if (vnet_lisp_enable_disable_status () == 0)
943 clib_warning ("LISP is disabled!");
944 return VNET_API_ERROR_LISP_DISABLED;
947 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
948 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
950 if (!is_l2 && (vni == 0 || dp_id == 0))
952 clib_warning ("can't add/del default vni-vrf mapping!");
956 dp_idp = hash_get (dp_table_by_vni[0], vni);
957 vnip = hash_get (vni_by_dp_table[0], dp_id);
963 clib_warning ("vni %d or vrf %d already used in vrf/vni "
964 "mapping!", vni, dp_id);
967 hash_set (dp_table_by_vni[0], vni, dp_id);
968 hash_set (vni_by_dp_table[0], dp_id, vni);
970 /* create dp iface */
971 dp_add_del_iface (lcm, vni, is_l2, 1);
975 if (!dp_idp || !vnip)
977 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
978 "mapping!", vni, dp_id);
981 /* remove dp iface */
982 dp_add_del_iface (lcm, vni, is_l2, 0);
984 hash_unset (dp_table_by_vni[0], vni);
985 hash_unset (vni_by_dp_table[0], dp_id);
991 /* return 0 if the two locator sets are identical 1 otherwise */
993 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
994 locator_t * new_locators)
997 locator_t *old_loc, *new_loc;
999 if (vec_len (old_ls_indexes) != vec_len (new_locators))
1002 for (i = 0; i < vec_len (new_locators); i++)
1004 old_li = vec_elt (old_ls_indexes, i);
1005 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
1007 new_loc = vec_elt_at_index (new_locators, i);
1009 if (locator_cmp (old_loc, new_loc))
1019 gid_address_t *eids_to_be_deleted;
1020 } remove_mapping_args_t;
1023 * Callback invoked when a sub-prefix is found
1026 remove_mapping_if_needed (u32 mi, void *arg)
1029 remove_mapping_args_t *a = arg;
1030 lisp_cp_main_t *lcm = a->lcm;
1034 m = pool_elt_at_index (lcm->mapping_pool, mi);
1038 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1042 if (0 != vec_len (ls->locator_indices))
1047 if (0 == vec_len (ls->locator_indices))
1052 vec_add1 (a->eids_to_be_deleted, m->eid);
1056 * This function searches map cache and looks for IP prefixes that are subset
1057 * of the provided one. If such prefix is found depending on 'is_negative'
1060 * 1) if is_negative is true and found prefix points to positive mapping,
1061 * then the mapping is removed
1062 * 2) if is_negative is false and found prefix points to negative mapping,
1063 * then the mapping is removed
1066 remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1070 remove_mapping_args_t a;
1072 memset (&a, 0, sizeof (a));
1074 /* do this only in src/dst mode ... */
1075 if (MR_MODE_SRC_DST != lcm->map_request_mode)
1078 /* ... and only for IP prefix */
1079 if (GID_ADDR_SRC_DST != gid_address_type (eid)
1080 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1083 a.is_negative = is_negative;
1086 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1087 remove_mapping_if_needed, &a);
1089 vec_foreach (e, a.eids_to_be_deleted)
1091 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
1093 memset (adj_args, 0, sizeof (adj_args[0]));
1094 gid_address_copy (&adj_args->reid, e);
1095 adj_args->is_add = 0;
1096 if (vnet_lisp_add_del_adjacency (adj_args))
1097 clib_warning ("failed to del adjacency!");
1099 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
1102 vec_free (a.eids_to_be_deleted);
1106 mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1108 timing_wheel_delete (&lcm->wheel, mi);
1112 is_local_ip (lisp_cp_main_t * lcm, ip_address_t * addr)
1114 fib_node_index_t fei;
1115 fib_prefix_t prefix;
1116 fib_entry_flag_t flags;
1118 ip_address_to_fib_prefix (addr, &prefix);
1120 fei = fib_table_lookup (0, &prefix);
1121 flags = fib_entry_get_flags (fei);
1122 return (FIB_ENTRY_FLAG_LOCAL & flags);
1126 * Adds/removes/updates mapping. Does not program forwarding.
1128 * @param eid end-host identifier
1129 * @param rlocs vector of remote locators
1130 * @param action action for negative map-reply
1131 * @param is_add add mapping if non-zero, delete otherwise
1132 * @param res_map_index the map-index that was created/updated/removed. It is
1133 * set to ~0 if no action is taken.
1134 * @param is_static used for distinguishing between statically learned
1135 remote mappings and mappings obtained from MR
1136 * @return return code
1139 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
1140 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
1141 u32 * res_map_index)
1143 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1144 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1145 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1146 u32 mi, ls_index = 0, dst_map_index;
1150 if (vnet_lisp_enable_disable_status () == 0)
1152 clib_warning ("LISP is disabled!");
1153 return VNET_API_ERROR_LISP_DISABLED;
1157 res_map_index[0] = ~0;
1159 memset (m_args, 0, sizeof (m_args[0]));
1160 memset (ls_args, 0, sizeof (ls_args[0]));
1162 ls_args->locators = rlocs;
1164 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
1165 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1169 /* check if none of the locators match localy configured address */
1170 vec_foreach (loc, rlocs)
1172 ip_prefix_t *p = &gid_address_ippref (&loc->address);
1173 if (is_local_ip (lcm, &ip_prefix_addr (p)))
1175 clib_warning ("RLOC %U matches a local address!",
1176 format_gid_address, &loc->address);
1177 return VNET_API_ERROR_LISP_RLOC_LOCAL;
1181 /* overwrite: if mapping already exists, decide if locators should be
1182 * updated and be done */
1183 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
1185 if (!is_static && (old_map->is_static || old_map->local))
1187 /* do not overwrite local or static remote mappings */
1188 clib_warning ("mapping %U rejected due to collision with local "
1189 "or static remote mapping!", format_gid_address,
1194 locator_set_t *old_ls;
1196 /* update mapping attributes */
1197 old_map->action = action;
1198 old_map->authoritative = authoritative;
1201 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1202 old_map->locator_set_index);
1203 if (compare_locators (lcm, old_ls->locator_indices,
1206 /* set locator-set index to overwrite */
1207 ls_args->is_add = 1;
1208 ls_args->index = old_map->locator_set_index;
1209 vnet_lisp_add_del_locator_set (ls_args, 0);
1211 res_map_index[0] = mi;
1217 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1219 ls_args->is_add = 1;
1220 ls_args->index = ~0;
1222 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1225 gid_address_copy (&m_args->eid, eid);
1227 m_args->action = action;
1228 m_args->locator_set_index = ls_index;
1229 m_args->is_static = is_static;
1231 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
1234 res_map_index[0] = dst_map_index;
1239 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
1241 clib_warning ("cannot delete mapping for eid %U",
1242 format_gid_address, eid);
1247 gid_address_copy (&m_args->eid, eid);
1248 m_args->locator_set_index = old_map->locator_set_index;
1250 /* delete mapping associated from map-cache */
1251 vnet_lisp_map_cache_add_del (m_args, 0);
1253 ls_args->is_add = 0;
1254 ls_args->index = old_map->locator_set_index;
1255 /* delete locator set */
1256 vnet_lisp_add_del_locator_set (ls_args, 0);
1258 /* delete timer associated to the mapping if any */
1259 if (old_map->timer_set)
1260 mapping_delete_timer (lcm, mi);
1262 /* return old mapping index */
1264 res_map_index[0] = mi;
1272 vnet_lisp_clear_all_remote_adjacencies (void)
1275 u32 mi, *map_indices = 0, *map_indexp;
1276 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1277 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1278 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1281 pool_foreach_index (mi, lcm->mapping_pool,
1283 vec_add1 (map_indices, mi);
1287 vec_foreach (map_indexp, map_indices)
1289 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1292 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
1294 dm_args->is_add = 0;
1295 gid_address_copy (&dm_args->eid, &map->eid);
1296 dm_args->locator_set_index = map->locator_set_index;
1298 /* delete mapping associated to fwd entry */
1299 vnet_lisp_map_cache_add_del (dm_args, 0);
1303 ls->index = map->locator_set_index;
1304 /* delete locator set */
1305 rv = vnet_lisp_add_del_locator_set (ls, 0);
1313 vec_free (map_indices);
1318 * Adds adjacency or removes forwarding entry associated to remote mapping.
1319 * Note that adjacencies are not stored, they only result in forwarding entries
1323 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1325 lisp_cp_main_t *lcm = &lisp_control_main;
1326 u32 local_mi, remote_mi = ~0;
1328 if (vnet_lisp_enable_disable_status () == 0)
1330 clib_warning ("LISP is disabled!");
1331 return VNET_API_ERROR_LISP_DISABLED;
1334 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1335 &a->reid, &a->leid);
1336 if (GID_LOOKUP_MISS == remote_mi)
1338 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1339 format_gid_address, &a->reid);
1346 /* check if source eid has an associated mapping. If pitr mode is on,
1347 * just use the pitr's mapping */
1349 local_mi = lcm->pitr_map_index;
1352 if (gid_address_type (&a->reid) == GID_ADDR_NSH)
1354 if (lcm->nsh_map_index == ~0)
1355 local_mi = GID_LOOKUP_MISS;
1357 local_mi = lcm->nsh_map_index;
1361 local_mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
1366 if (GID_LOOKUP_MISS == local_mi)
1368 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1369 format_gid_address, &a->leid);
1374 /* update forwarding */
1375 dp_add_fwd_entry (lcm, local_mi, remote_mi);
1378 dp_del_fwd_entry (lcm, 0, remote_mi);
1384 vnet_lisp_set_map_request_mode (u8 mode)
1386 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1388 if (vnet_lisp_enable_disable_status () == 0)
1390 clib_warning ("LISP is disabled!");
1391 return VNET_API_ERROR_LISP_DISABLED;
1394 if (mode >= _MR_MODE_MAX)
1396 clib_warning ("Invalid LISP map request mode %d!", mode);
1397 return VNET_API_ERROR_INVALID_ARGUMENT;
1400 lcm->map_request_mode = mode;
1405 vnet_lisp_nsh_set_locator_set (u8 * locator_set_name, u8 is_add)
1407 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1408 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1409 u32 locator_set_index = ~0;
1413 if (vnet_lisp_enable_disable_status () == 0)
1415 clib_warning ("LISP is disabled!");
1416 return VNET_API_ERROR_LISP_DISABLED;
1421 if (lcm->nsh_map_index == (u32) ~ 0)
1423 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1426 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1429 locator_set_index = p[0];
1431 pool_get (lcm->mapping_pool, m);
1432 memset (m, 0, sizeof *m);
1433 m->locator_set_index = locator_set_index;
1436 lcm->nsh_map_index = m - lcm->mapping_pool;
1438 if (~0 == vnet_lisp_gpe_add_nsh_iface (lgm))
1444 if (lcm->nsh_map_index != (u32) ~ 0)
1446 /* remove NSH mapping */
1447 pool_put_index (lcm->mapping_pool, lcm->nsh_map_index);
1448 lcm->nsh_map_index = ~0;
1449 vnet_lisp_gpe_del_nsh_iface (lgm);
1456 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1458 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1459 u32 locator_set_index = ~0;
1463 if (vnet_lisp_enable_disable_status () == 0)
1465 clib_warning ("LISP is disabled!");
1466 return VNET_API_ERROR_LISP_DISABLED;
1469 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1472 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1475 locator_set_index = p[0];
1479 pool_get (lcm->mapping_pool, m);
1480 m->locator_set_index = locator_set_index;
1483 lcm->pitr_map_index = m - lcm->mapping_pool;
1485 /* enable pitr mode */
1490 /* remove pitr mapping */
1491 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1493 /* disable pitr mode */
1500 * Configure Proxy-ETR
1502 * @param ip PETR's IP address
1503 * @param is_add Flag that indicates if this is an addition or removal
1505 * return 0 on success
1508 vnet_lisp_use_petr (ip_address_t * ip, u8 is_add)
1510 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1513 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1516 if (vnet_lisp_enable_disable_status () == 0)
1518 clib_warning ("LISP is disabled!");
1519 return VNET_API_ERROR_LISP_DISABLED;
1522 memset (ls_args, 0, sizeof (*ls_args));
1526 /* Create dummy petr locator-set */
1527 memset (&loc, 0, sizeof (loc));
1528 gid_address_from_ip (&loc.address, ip);
1530 loc.state = loc.weight = 1;
1533 ls_args->is_add = 1;
1534 ls_args->index = ~0;
1535 vec_add1 (ls_args->locators, loc);
1536 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1538 /* Add petr mapping */
1539 pool_get (lcm->mapping_pool, m);
1540 m->locator_set_index = ls_index;
1541 lcm->petr_map_index = m - lcm->mapping_pool;
1543 /* Enable use-petr */
1544 lcm->flags |= LISP_FLAG_USE_PETR;
1548 m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1550 /* Remove petr locator */
1551 ls_args->is_add = 0;
1552 ls_args->index = m->locator_set_index;
1553 vnet_lisp_add_del_locator_set (ls_args, 0);
1555 /* Remove petr mapping */
1556 pool_put_index (lcm->mapping_pool, lcm->petr_map_index);
1558 /* Disable use-petr */
1559 lcm->flags &= ~LISP_FLAG_USE_PETR;
1564 /* cleans locator to locator-set data and removes locators not part of
1565 * any locator-set */
1567 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1569 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
1570 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1571 for (i = 0; i < vec_len (ls->locator_indices); i++)
1573 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1574 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1576 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1578 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1579 if (ls_indexp[0] == lsi)
1583 /* delete index for removed locator-set */
1584 vec_del1 (ls_indexes[0], j);
1586 /* delete locator if it's part of no locator-set */
1587 if (vec_len (ls_indexes[0]) == 0)
1589 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1590 vec_add1 (to_be_deleted, i);
1596 for (i = 0; i < vec_len (to_be_deleted); i++)
1598 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1599 vec_del1 (ls->locator_indices, loc_indexp[0]);
1601 vec_free (to_be_deleted);
1605 static inline uword *
1606 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
1608 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1613 /* find locator-set */
1616 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
1627 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
1633 ASSERT (ls != NULL);
1634 ASSERT (loc != NULL);
1636 vec_foreach (locit, ls->locator_indices)
1638 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1639 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1640 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1642 clib_warning ("Duplicate locator");
1643 return VNET_API_ERROR_VALUE_EXIST;
1651 update_adjacencies_by_map_index (lisp_cp_main_t * lcm, u8 is_local,
1652 u32 mapping_index, u8 remove_only)
1656 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1658 map = pool_elt_at_index (lcm->mapping_pool, mapping_index);
1661 pool_foreach(fwd, lcm->fwd_entry_pool,
1663 if ((is_local && 0 == gid_address_cmp (&map->eid, &fwd->leid)) ||
1664 (!is_local && 0 == gid_address_cmp (&map->eid, &fwd->reid)))
1667 gid_address_copy (&a->leid, &fwd->leid);
1668 gid_address_copy (&a->reid, &fwd->reid);
1670 vnet_lisp_add_del_adjacency (a);
1675 vnet_lisp_add_del_adjacency (a);
1683 update_fwd_entries_by_locator_set (lisp_cp_main_t * lcm, u8 is_local,
1684 u32 ls_index, u8 remove_only)
1689 if (vec_len (lcm->locator_set_to_eids) <= ls_index)
1692 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, ls_index);
1694 for (i = 0; i < vec_len (eid_indexes[0]); i++)
1696 map_indexp = vec_elt_at_index (eid_indexes[0], i);
1697 update_adjacencies_by_map_index (lcm, is_local, map_indexp[0],
1703 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1704 u32 ls_index, u32 loc_id)
1706 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1707 u32 **ls_indexes = NULL;
1709 ASSERT (ls != NULL);
1710 ASSERT (locit != NULL);
1712 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1713 pool_put_index (lcm->locator_pool, locit[0]);
1714 vec_del1 (ls->locator_indices, loc_id);
1715 vec_del1 (ls_indexes[0], ls_index);
1719 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1720 locator_set_t * ls, u32 * ls_result)
1722 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1723 locator_t *loc = NULL, *itloc = NULL;
1724 uword _p = (u32) ~ 0, *p = &_p;
1725 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
1731 if (vnet_lisp_enable_disable_status () == 0)
1733 clib_warning ("LISP is disabled!");
1734 return VNET_API_ERROR_LISP_DISABLED;
1737 p = get_locator_set_index (a, p);
1740 clib_warning ("locator-set %v doesn't exist", a->name);
1741 return VNET_API_ERROR_INVALID_ARGUMENT;
1746 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1749 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1751 return VNET_API_ERROR_INVALID_ARGUMENT;
1758 ls_result[0] = p[0];
1760 /* allocate locators */
1761 vec_foreach (itloc, a->locators)
1763 ret = is_locator_in_locator_set (lcm, ls, itloc);
1769 pool_get (lcm->locator_pool, loc);
1771 loc_index = loc - lcm->locator_pool;
1773 vec_add1 (ls->locator_indices, loc_index);
1775 vec_validate (lcm->locator_to_locator_sets, loc_index);
1776 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1778 vec_add1 (ls_indexes[0], p[0]);
1786 vec_foreach (itloc, a->locators)
1790 vec_foreach (locit, ls->locator_indices)
1792 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1794 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1797 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1799 if (0 == loc->local &&
1800 !gid_address_cmp (&loc->address, &itloc->address))
1803 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1808 /* update fwd entries using this locator in DP */
1809 update_fwd_entries_by_locator_set (lcm, loc->local, ls_index,
1810 vec_len (ls->locator_indices)
1823 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
1826 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1828 uword _p = (u32) ~ 0, *p = &_p;
1833 if (vnet_lisp_enable_disable_status () == 0)
1835 clib_warning ("LISP is disabled!");
1836 return VNET_API_ERROR_LISP_DISABLED;
1841 p = get_locator_set_index (a, p);
1844 if (p && p[0] != (u32) ~ 0)
1846 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1849 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1854 /* clean locator to locator-set vectors and remove locators if
1855 * they're not part of another locator-set */
1856 clean_locator_to_locator_set (lcm, p[0]);
1858 /* remove locator indices from locator set */
1859 vec_free (ls->locator_indices);
1864 ls_result[0] = p[0];
1866 /* new locator-set */
1869 pool_get (lcm->locator_set_pool, ls);
1870 memset (ls, 0, sizeof (*ls));
1871 ls_index = ls - lcm->locator_set_pool;
1875 ls->name = vec_dup (a->name);
1877 if (!lcm->locator_set_index_by_name)
1878 lcm->locator_set_index_by_name = hash_create_vec (
1886 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
1889 /* mark as local locator-set */
1890 vec_add1 (lcm->local_locator_set_indexes, ls_index);
1892 ls->local = a->local;
1894 ls_result[0] = ls_index;
1897 ret = vnet_lisp_add_del_locator (a, ls, NULL);
1905 p = get_locator_set_index (a, p);
1908 clib_warning ("locator-set %v doesn't exists", a->name);
1912 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1915 clib_warning ("locator-set with index %d doesn't exists", p[0]);
1919 if (lcm->mreq_itr_rlocs == p[0])
1921 clib_warning ("Can't delete the locator-set used to constrain "
1922 "the itr-rlocs in map-requests!");
1926 if (vec_len (lcm->locator_set_to_eids) != 0)
1928 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
1929 if (vec_len (eid_indexes[0]) != 0)
1932 ("Can't delete a locator that supports a mapping!");
1937 /* clean locator to locator-sets data */
1938 clean_locator_to_locator_set (lcm, p[0]);
1944 vec_foreach_index (it, lcm->local_locator_set_indexes)
1946 lsi = vec_elt (lcm->local_locator_set_indexes, it);
1949 vec_del1 (lcm->local_locator_set_indexes, it);
1953 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
1955 vec_free (ls->name);
1956 vec_free (ls->locator_indices);
1957 pool_put (lcm->locator_set_pool, ls);
1963 vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
1965 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1967 lcm->rloc_probing = is_enable;
1972 vnet_lisp_map_register_enable_disable (u8 is_enable)
1974 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1976 lcm->map_registering = is_enable;
1981 vnet_lisp_enable_disable (u8 is_enable)
1984 clib_error_t *error = 0;
1985 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1986 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
1988 a->is_en = is_enable;
1989 error = vnet_lisp_gpe_enable_disable (a);
1992 return clib_error_return (0, "failed to %s data-plane!",
1993 a->is_en ? "enable" : "disable");
1998 /* enable all l2 and l3 ifaces */
2001 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2002 dp_add_del_iface(lcm, vni, 0, 1);
2004 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2005 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
2011 /* clear interface table */
2012 hash_free (lcm->fwd_entry_by_mapping_index);
2013 pool_free (lcm->fwd_entry_pool);
2016 /* update global flag */
2017 lcm->is_enabled = is_enable;
2023 vnet_lisp_enable_disable_status (void)
2025 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2026 return lcm->is_enabled;
2030 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2032 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2034 lisp_msmr_t _mr, *mr = &_mr;
2036 if (vnet_lisp_enable_disable_status () == 0)
2038 clib_warning ("LISP is disabled!");
2039 return VNET_API_ERROR_LISP_DISABLED;
2045 if (get_map_resolver (&a->address))
2047 clib_warning ("map-resolver %U already exists!", format_ip_address,
2052 memset (mr, 0, sizeof (*mr));
2053 ip_address_copy (&mr->address, &a->address);
2054 vec_add1 (lcm->map_resolvers, *mr);
2056 if (vec_len (lcm->map_resolvers) == 1)
2057 lcm->do_map_resolver_election = 1;
2061 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2063 mr = vec_elt_at_index (lcm->map_resolvers, i);
2064 if (!ip_address_cmp (&mr->address, &a->address))
2066 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2067 lcm->do_map_resolver_election = 1;
2069 vec_del1 (lcm->map_resolvers, i);
2078 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2080 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2083 if (vnet_lisp_enable_disable_status () == 0)
2085 clib_warning ("LISP is disabled!");
2086 return VNET_API_ERROR_LISP_DISABLED;
2091 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
2094 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
2095 return VNET_API_ERROR_INVALID_ARGUMENT;
2098 lcm->mreq_itr_rlocs = p[0];
2102 lcm->mreq_itr_rlocs = ~0;
2108 /* Statistics (not really errors) */
2109 #define foreach_lisp_cp_lookup_error \
2111 _(MAP_REQUESTS_SENT, "map-request sent") \
2112 _(ARP_REPLY_TX, "ARP replies sent")
2114 static char *lisp_cp_lookup_error_strings[] = {
2115 #define _(sym,string) string,
2116 foreach_lisp_cp_lookup_error
2122 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
2123 foreach_lisp_cp_lookup_error
2125 LISP_CP_LOOKUP_N_ERROR,
2126 } lisp_cp_lookup_error_t;
2130 LISP_CP_LOOKUP_NEXT_DROP,
2131 LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX,
2132 LISP_CP_LOOKUP_N_NEXT,
2133 } lisp_cp_lookup_next_t;
2137 gid_address_t dst_eid;
2138 ip_address_t map_resolver_ip;
2139 } lisp_cp_lookup_trace_t;
2142 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
2144 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2145 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2146 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
2148 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
2149 format_ip_address, &t->map_resolver_ip, format_gid_address,
2155 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
2156 ip_address_t * sloc)
2161 if (vec_len (lcm->map_resolvers) == 0)
2163 clib_warning ("No map-resolver configured");
2167 /* find the first mr ip we have a route to and the ip of the
2168 * iface that has a route to it */
2169 vec_foreach (mrit, lcm->map_resolvers)
2172 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
2174 ip_address_copy (mr_ip, a);
2176 /* also update globals */
2181 clib_warning ("Can't find map-resolver and local interface ip!");
2185 static gid_address_t *
2186 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2192 ip_interface_address_t *ia = 0;
2193 gid_address_t gid_data, *gid = &gid_data;
2194 gid_address_t *rlocs = 0;
2195 ip_prefix_t *ippref = &gid_address_ippref (gid);
2196 ip_address_t *rloc = &ip_prefix_addr (ippref);
2198 memset (gid, 0, sizeof (gid[0]));
2199 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
2200 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
2202 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
2203 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2205 /* Add ipv4 locators first TODO sort them */
2208 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2209 loc->sw_if_index, 1 /* unnumbered */,
2211 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2212 ip_address_set (rloc, addr, IP4);
2213 ip_prefix_len (ippref) = 32;
2214 ip_prefix_normalize (ippref);
2215 vec_add1 (rlocs, gid[0]);
2218 /* Add ipv6 locators */
2219 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2220 loc->sw_if_index, 1 /* unnumbered */,
2222 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2223 ip_address_set (rloc, addr, IP6);
2224 ip_prefix_len (ippref) = 128;
2225 ip_prefix_normalize (ippref);
2226 vec_add1 (rlocs, gid[0]);
2234 static vlib_buffer_t *
2235 build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
2236 ip_address_t * sloc, ip_address_t * rloc,
2237 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
2241 vlib_main_t *vm = lcm->vlib_main;
2243 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2245 clib_warning ("Can't allocate buffer for Map-Request!");
2249 b = vlib_get_buffer (vm, bi);
2251 /* leave some space for the encap headers */
2252 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2255 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
2256 1 /* rloc probe */ , nonce_res);
2258 /* push outer ip header */
2259 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2267 static vlib_buffer_t *
2268 build_encapsulated_map_request (lisp_cp_main_t * lcm,
2269 gid_address_t * seid, gid_address_t * deid,
2270 locator_set_t * loc_set, ip_address_t * mr_ip,
2271 ip_address_t * sloc, u8 is_smr_invoked,
2272 u64 * nonce_res, u32 * bi_res)
2276 gid_address_t *rlocs = 0;
2277 vlib_main_t *vm = lcm->vlib_main;
2279 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2281 clib_warning ("Can't allocate buffer for Map-Request!");
2285 b = vlib_get_buffer (vm, bi);
2287 /* leave some space for the encap headers */
2288 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2291 rlocs = build_itr_rloc_list (lcm, loc_set);
2293 if (MR_MODE_SRC_DST == lcm->map_request_mode
2294 && GID_ADDR_SRC_DST != gid_address_type (deid))
2297 memset (&sd, 0, sizeof (sd));
2298 build_src_dst (&sd, seid, deid);
2299 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
2300 0 /* rloc probe */ , nonce_res);
2305 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
2306 0 /* rloc probe */ , nonce_res);
2309 /* push ecm: udp-ip-lisp */
2310 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2312 /* push outer ip header */
2313 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2323 reset_pending_mr_counters (pending_map_request_t * r)
2325 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2330 elect_map_resolver (lisp_cp_main_t * lcm)
2334 vec_foreach (mr, lcm->map_resolvers)
2338 ip_address_copy (&lcm->active_map_resolver, &mr->address);
2339 lcm->do_map_resolver_election = 0;
2347 free_map_register_records (mapping_t * maps)
2350 vec_foreach (map, maps) vec_free (map->locators);
2356 add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
2357 ip_address_t * probed_loc)
2360 locator_t *loc, new;
2361 ip_interface_address_t *ia = 0;
2363 ip_address_t *new_ip = &gid_address_ip (&new.address);
2366 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2368 vec_foreach (li, ls->locator_indices)
2370 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
2375 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2376 loc->sw_if_index, 1 /* unnumbered */,
2378 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
2380 ip_address_set (new_ip, addr, IP4);
2383 /* Add ipv6 locators */
2384 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2385 loc->sw_if_index, 1 /* unnumbered */,
2387 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
2389 ip_address_set (new_ip, addr, IP6);
2393 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
2396 vec_add1 (m->locators, new);
2401 build_map_register_record_list (lisp_cp_main_t * lcm)
2403 mapping_t *recs = 0, rec, *m;
2406 pool_foreach(m, lcm->mapping_pool,
2408 /* for now build only local mappings */
2413 add_locators (lcm, &rec, m->locator_set_index, NULL);
2414 vec_add1 (recs, rec);
2422 update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
2423 lisp_key_type_t key_id, u8 * key,
2424 u16 auth_data_len, u32 msg_len)
2426 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
2427 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
2429 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
2430 (unsigned char *) map_reg_hdr, msg_len, NULL,
2432 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
2437 static vlib_buffer_t *
2438 build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
2439 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
2440 mapping_t * records, lisp_key_type_t key_id, u8 * key,
2445 u32 bi, auth_data_len = 0, msg_len = 0;
2446 vlib_main_t *vm = lcm->vlib_main;
2448 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2450 clib_warning ("Can't allocate buffer for Map-Register!");
2454 b = vlib_get_buffer (vm, bi);
2456 /* leave some space for the encap headers */
2457 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2459 auth_data_len = auth_data_len_by_key_id (key_id);
2460 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
2461 auth_data_len, nonce_res,
2464 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
2467 /* push outer ip header */
2468 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2476 get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
2479 while (lcm->do_map_resolver_election
2480 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
2481 &lcm->active_map_resolver,
2484 if (0 == elect_map_resolver (lcm))
2485 /* all map resolvers are down */
2487 /* restart MR checking by marking all of them up */
2488 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
2495 /* CP output statistics */
2496 #define foreach_lisp_cp_output_error \
2497 _(MAP_REGISTERS_SENT, "map-registers sent") \
2498 _(RLOC_PROBES_SENT, "rloc-probes sent")
2500 static char *lisp_cp_output_error_strings[] = {
2501 #define _(sym,string) string,
2502 foreach_lisp_cp_output_error
2508 #define _(sym,str) LISP_CP_OUTPUT_ERROR_##sym,
2509 foreach_lisp_cp_output_error
2511 LISP_CP_OUTPUT_N_ERROR,
2512 } lisp_cp_output_error_t;
2515 lisp_cp_output (vlib_main_t * vm, vlib_node_runtime_t * node,
2516 vlib_frame_t * from_frame)
2521 /* dummy node used only for statistics */
2523 VLIB_REGISTER_NODE (lisp_cp_output_node) = {
2524 .function = lisp_cp_output,
2525 .name = "lisp-cp-output",
2526 .vector_size = sizeof (u32),
2527 .format_trace = format_lisp_cp_input_trace,
2528 .type = VLIB_NODE_TYPE_INTERNAL,
2530 .n_errors = LISP_CP_OUTPUT_N_ERROR,
2531 .error_strings = lisp_cp_output_error_strings,
2533 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2536 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2542 send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
2543 u32 local_locator_set_index, ip_address_t * sloc,
2544 ip_address_t * rloc)
2551 u32 next_index, *to_next;
2552 gid_address_t *itr_rlocs;
2554 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
2555 itr_rlocs = build_itr_rloc_list (lcm, ls);
2557 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
2558 vec_free (itr_rlocs);
2562 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2564 next_index = (ip_addr_version (rloc) == IP4) ?
2565 ip4_lookup_node.index : ip6_lookup_node.index;
2567 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2569 /* Enqueue the packet */
2570 to_next = vlib_frame_vector_args (f);
2573 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2575 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2580 send_rloc_probes (lisp_cp_main_t * lcm)
2586 u32 si, rloc_probes_sent = 0;
2589 pool_foreach (e, lcm->fwd_entry_pool,
2591 if (vec_len (e->locator_pairs) == 0)
2594 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
2597 clib_warning ("internal error: cannot find local eid %U in "
2598 "map-cache!", format_gid_address, &e->leid);
2601 lm = pool_elt_at_index (lcm->mapping_pool, si);
2603 /* get the best (lowest) priority */
2604 lprio = e->locator_pairs[0].priority;
2606 /* send rloc-probe for pair(s) with the best remote locator priority */
2607 vec_foreach (lp, e->locator_pairs)
2609 if (lp->priority != lprio)
2612 /* get first remote locator */
2613 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
2620 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2621 LISP_CP_OUTPUT_ERROR_RLOC_PROBES_SENT,
2627 send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
2629 u32 bi, map_registers_sent = 0;
2634 u32 next_index, *to_next;
2635 ip_address_t *ms = 0;
2636 mapping_t *records, *r, *group, *k;
2638 // TODO: support multiple map servers and do election
2639 if (0 == vec_len (lcm->map_servers))
2642 ms = &lcm->map_servers[0].address;
2644 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
2646 clib_warning ("no eligible interface address found for %U!",
2647 format_ip_address, &lcm->map_servers[0]);
2651 records = build_map_register_record_list (lcm);
2655 vec_foreach (r, records)
2658 u8 key_id = r->key_id;
2661 continue; /* no secret key -> map-register cannot be sent */
2664 vec_add1 (group, r[0]);
2666 /* group mappings that share common key */
2667 for (k = r + 1; k < vec_end (records); k++)
2669 if (k->key_id != r->key_id)
2672 if (vec_is_equal (k->key, r->key))
2674 vec_add1 (group, k[0]);
2675 k->key = 0; /* don't process this mapping again */
2679 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, group,
2685 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2687 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2688 ip4_lookup_node.index : ip6_lookup_node.index;
2690 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2692 /* Enqueue the packet */
2693 to_next = vlib_frame_vector_args (f);
2696 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2697 map_registers_sent++;
2699 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2701 free_map_register_records (records);
2703 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2704 LISP_CP_OUTPUT_ERROR_MAP_REGISTERS_SENT,
2705 map_registers_sent);
2710 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
2711 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2713 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2714 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2717 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
2718 gid_address_t * seid, gid_address_t * deid,
2719 u8 is_smr_invoked, u8 is_resend)
2721 u32 next_index, bi = 0, *to_next, map_index;
2725 locator_set_t *loc_set;
2727 pending_map_request_t *pmr, *duplicate_pmr = 0;
2731 /* if there is already a pending request remember it */
2734 pool_foreach(pmr, lcm->pending_map_requests_pool,
2736 if (!gid_address_cmp (&pmr->src, seid)
2737 && !gid_address_cmp (&pmr->dst, deid))
2739 duplicate_pmr = pmr;
2745 if (!is_resend && duplicate_pmr)
2747 /* don't send the request if there is a pending map request already */
2751 /* get locator-set for seid */
2752 if (!lcm->lisp_pitr && gid_address_type (deid) != GID_ADDR_NSH)
2754 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2755 if (map_index == ~0)
2757 clib_warning ("No local mapping found in eid-table for %U!",
2758 format_gid_address, seid);
2762 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2767 ("Mapping found for src eid %U is not marked as local!",
2768 format_gid_address, seid);
2771 ls_index = map->locator_set_index;
2777 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
2778 ls_index = map->locator_set_index;
2782 if (lcm->nsh_map_index == (u32) ~ 0)
2784 clib_warning ("No locator-set defined for NSH!");
2789 map = pool_elt_at_index (lcm->mapping_pool, lcm->nsh_map_index);
2790 ls_index = map->locator_set_index;
2795 /* overwrite locator set if map-request itr-rlocs configured */
2796 if (~0 != lcm->mreq_itr_rlocs)
2798 ls_index = lcm->mreq_itr_rlocs;
2801 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
2803 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
2806 duplicate_pmr->to_be_removed = 1;
2810 /* build the encapsulated map request */
2811 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
2812 &lcm->active_map_resolver,
2813 &sloc, is_smr_invoked, &nonce, &bi);
2818 /* set fib index to default and lookup node */
2819 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2820 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2821 ip4_lookup_node.index : ip6_lookup_node.index;
2823 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2825 /* Enqueue the packet */
2826 to_next = vlib_frame_vector_args (f);
2829 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2832 /* if there is a pending request already update it */
2834 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
2836 /* remove the oldest nonce */
2837 u64 CLIB_UNUSED (tmp), *nonce_del;
2838 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
2839 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
2840 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
2843 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
2844 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2845 duplicate_pmr - lcm->pending_map_requests_pool);
2849 /* add map-request to pending requests table */
2850 pool_get (lcm->pending_map_requests_pool, pmr);
2851 memset (pmr, 0, sizeof (*pmr));
2852 gid_address_copy (&pmr->src, seid);
2853 gid_address_copy (&pmr->dst, deid);
2854 clib_fifo_add1 (pmr->nonces, nonce);
2855 pmr->is_smr_invoked = is_smr_invoked;
2856 reset_pending_mr_counters (pmr);
2857 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2858 pmr - lcm->pending_map_requests_pool);
2865 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
2867 ip4_header_t *ip4 = hdr;
2870 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2872 ip_address_set (src, &ip4->src_address, IP4);
2873 ip_address_set (dst, &ip4->dst_address, IP4);
2878 ip_address_set (src, &ip6->src_address, IP6);
2879 ip_address_set (dst, &ip6->dst_address, IP6);
2884 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2888 u32 vni = ~0, table_id = ~0;
2890 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
2891 IP4 ? FIB_PROTOCOL_IP4 :
2897 vnip = hash_get (lcm->vni_by_table_id, table_id);
2901 clib_warning ("vrf %d is not mapped to any vni!", table_id);
2907 lisp_get_bd_from_buffer_eth (vlib_buffer_t * b)
2911 l2input_main_t *l2im = &l2input_main;
2912 l2_input_config_t *config;
2913 l2_bridge_domain_t *bd_config;
2915 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
2916 config = vec_elt_at_index (l2im->configs, sw_if_index0);
2917 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
2919 return bd_config->bd_id;
2923 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2927 u32 bd = lisp_get_bd_from_buffer_eth (b);
2929 vnip = hash_get (lcm->vni_by_bd_id, bd);
2933 clib_warning ("bridge domain %d is not mapped to any vni!", bd);
2939 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2940 gid_address_t * src, gid_address_t * dst,
2943 ethernet_header_t *eh;
2946 memset (src, 0, sizeof (*src));
2947 memset (dst, 0, sizeof (*dst));
2949 gid_address_type (dst) = GID_ADDR_NO_ADDRESS;
2950 gid_address_type (src) = GID_ADDR_NO_ADDRESS;
2952 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
2955 u8 version, preflen;
2957 gid_address_type (src) = GID_ADDR_IP_PREFIX;
2958 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
2960 ip = vlib_buffer_get_current (b);
2961 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
2963 version = gid_address_ip_version (src);
2964 preflen = ip_address_max_len (version);
2965 gid_address_ippref_len (src) = preflen;
2966 gid_address_ippref_len (dst) = preflen;
2968 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
2969 gid_address_vni (dst) = vni;
2970 gid_address_vni (src) = vni;
2972 else if (LISP_AFI_MAC == type)
2974 ethernet_arp_header_t *ah;
2976 eh = vlib_buffer_get_current (b);
2978 if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_ARP)
2980 ah = (ethernet_arp_header_t *) (((u8 *) eh) + sizeof (*eh));
2981 if (clib_net_to_host_u16 (ah->opcode)
2982 != ETHERNET_ARP_OPCODE_request)
2985 gid_address_type (dst) = GID_ADDR_ARP;
2986 gid_address_arp_bd (dst) = lisp_get_bd_from_buffer_eth (b);
2987 clib_memcpy (&gid_address_arp_ip4 (dst),
2988 &ah->ip4_over_ethernet[1].ip4, 4);
2992 gid_address_type (src) = GID_ADDR_MAC;
2993 gid_address_type (dst) = GID_ADDR_MAC;
2994 mac_copy (&gid_address_mac (src), eh->src_address);
2995 mac_copy (&gid_address_mac (dst), eh->dst_address);
2998 vni = lisp_get_vni_from_buffer_eth (lcm, b);
3000 gid_address_vni (dst) = vni;
3001 gid_address_vni (src) = vni;
3004 else if (LISP_AFI_LCAF == type)
3007 eh = vlib_buffer_get_current (b);
3009 if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_NSH)
3011 nh = (lisp_nsh_hdr_t *) (((u8 *) eh) + sizeof (*eh));
3012 u32 spi = clib_net_to_host_u32 (nh->spi_si << 8);
3013 u8 si = (u8) clib_net_to_host_u32 (nh->spi_si);
3014 gid_address_nsh_spi (dst) = spi;
3015 gid_address_nsh_si (dst) = si;
3017 gid_address_type (dst) = GID_ADDR_NSH;
3023 lisp_cp_lookup_inline (vlib_main_t * vm,
3024 vlib_node_runtime_t * node,
3025 vlib_frame_t * from_frame, int overlay)
3027 u32 *from, *to_next, di, si;
3028 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3029 u32 pkts_mapped = 0, next_index;
3030 uword n_left_from, n_left_to_next;
3031 vnet_main_t *vnm = vnet_get_main ();
3033 from = vlib_frame_vector_args (from_frame);
3034 n_left_from = from_frame->n_vectors;
3035 next_index = node->cached_next_index;
3037 while (n_left_from > 0)
3039 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
3041 while (n_left_from > 0 && n_left_to_next > 0)
3043 u32 pi0, sw_if_index0, next0;
3046 gid_address_t src, dst;
3047 ethernet_arp_header_t *arp0;
3048 ethernet_header_t *eth0;
3049 vnet_hw_interface_t *hw_if0;
3056 n_left_to_next -= 1;
3058 b0 = vlib_get_buffer (vm, pi0);
3060 /* src/dst eid pair */
3061 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst, overlay);
3063 if (gid_address_type (&dst) == GID_ADDR_ARP)
3065 mac0 = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3066 if (GID_LOOKUP_MISS_L2 != mac0)
3068 /* send ARP reply */
3070 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3071 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
3073 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3075 eth0 = vlib_buffer_get_current (b0);
3076 arp0 = (ethernet_arp_header_t *) (((u8 *) eth0)
3079 clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply);
3080 arp0->ip4_over_ethernet[1] = arp0->ip4_over_ethernet[0];
3081 clib_memcpy (arp0->ip4_over_ethernet[0].ethernet,
3083 clib_memcpy (&arp0->ip4_over_ethernet[0].ip4,
3084 &gid_address_arp_ip4 (&dst), 4);
3086 /* Hardware must be ethernet-like. */
3087 ASSERT (vec_len (hw_if0->hw_address) == 6);
3089 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
3090 clib_memcpy (eth0->src_address, hw_if0->hw_address, 6);
3092 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_ARP_REPLY_TX];
3093 next0 = LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX;
3094 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3096 n_left_to_next, pi0,
3102 /* if we have remote mapping for destination already in map-chache
3103 add forwarding tunnel directly. If not send a map-request */
3104 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3108 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3109 /* send a map-request also in case of negative mapping entry
3110 with corresponding action */
3111 if (m->action == LISP_SEND_MAP_REQUEST)
3113 /* send map-request */
3114 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3115 0 /* is_resend */ );
3120 if (GID_ADDR_NSH != gid_address_type (&dst))
3122 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3126 si = lcm->nsh_map_index;
3130 dp_add_fwd_entry_from_mt (si, di);
3136 /* send map-request */
3137 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3138 0 /* is_resend */ );
3142 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
3143 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3145 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3148 memset (tr, 0, sizeof (*tr));
3149 gid_address_copy (&tr->dst_eid, &dst);
3150 ip_address_copy (&tr->map_resolver_ip,
3151 &lcm->active_map_resolver);
3153 gid_address_free (&dst);
3154 gid_address_free (&src);
3155 next0 = LISP_CP_LOOKUP_NEXT_DROP;
3156 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3158 n_left_to_next, pi0, next0);
3161 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3163 vlib_node_increment_counter (vm, node->node_index,
3164 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
3166 return from_frame->n_vectors;
3170 lisp_cp_lookup_ip4 (vlib_main_t * vm,
3171 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3173 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3177 lisp_cp_lookup_ip6 (vlib_main_t * vm,
3178 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3180 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
3184 lisp_cp_lookup_l2 (vlib_main_t * vm,
3185 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3187 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
3191 lisp_cp_lookup_nsh (vlib_main_t * vm,
3192 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3194 /* TODO decide if NSH should be propagated as LCAF or not */
3195 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_LCAF));
3199 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
3200 .function = lisp_cp_lookup_ip4,
3201 .name = "lisp-cp-lookup-ip4",
3202 .vector_size = sizeof (u32),
3203 .format_trace = format_lisp_cp_lookup_trace,
3204 .type = VLIB_NODE_TYPE_INTERNAL,
3206 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3207 .error_strings = lisp_cp_lookup_error_strings,
3209 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3212 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3213 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
3219 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
3220 .function = lisp_cp_lookup_ip6,
3221 .name = "lisp-cp-lookup-ip6",
3222 .vector_size = sizeof (u32),
3223 .format_trace = format_lisp_cp_lookup_trace,
3224 .type = VLIB_NODE_TYPE_INTERNAL,
3226 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3227 .error_strings = lisp_cp_lookup_error_strings,
3229 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3232 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3233 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
3239 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
3240 .function = lisp_cp_lookup_l2,
3241 .name = "lisp-cp-lookup-l2",
3242 .vector_size = sizeof (u32),
3243 .format_trace = format_lisp_cp_lookup_trace,
3244 .type = VLIB_NODE_TYPE_INTERNAL,
3246 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3247 .error_strings = lisp_cp_lookup_error_strings,
3249 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3252 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3253 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
3259 VLIB_REGISTER_NODE (lisp_cp_lookup_nsh_node) = {
3260 .function = lisp_cp_lookup_nsh,
3261 .name = "lisp-cp-lookup-nsh",
3262 .vector_size = sizeof (u32),
3263 .format_trace = format_lisp_cp_lookup_trace,
3264 .type = VLIB_NODE_TYPE_INTERNAL,
3266 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3267 .error_strings = lisp_cp_lookup_error_strings,
3269 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3272 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3273 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
3278 /* lisp_cp_input statistics */
3279 #define foreach_lisp_cp_input_error \
3281 _(RLOC_PROBE_REQ_RECEIVED, "rloc-probe requests received") \
3282 _(RLOC_PROBE_REP_RECEIVED, "rloc-probe replies received") \
3283 _(MAP_NOTIFIES_RECEIVED, "map-notifies received") \
3284 _(MAP_REPLIES_RECEIVED, "map-replies received")
3286 static char *lisp_cp_input_error_strings[] = {
3287 #define _(sym,string) string,
3288 foreach_lisp_cp_input_error
3294 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
3295 foreach_lisp_cp_input_error
3297 LISP_CP_INPUT_N_ERROR,
3298 } lisp_cp_input_error_t;
3302 gid_address_t dst_eid;
3303 ip4_address_t map_resolver_ip;
3304 } lisp_cp_input_trace_t;
3307 format_lisp_cp_input_trace (u8 * s, va_list * args)
3309 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3310 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3311 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
3312 va_arg (*args, lisp_cp_input_trace_t *);
3314 s = format (s, "LISP-CP-INPUT: TODO");
3319 remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
3322 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3323 memset (adj_args, 0, sizeof (adj_args[0]));
3325 m = pool_elt_at_index (lcm->mapping_pool, mi);
3327 gid_address_copy (&adj_args->reid, &m->eid);
3328 adj_args->is_add = 0;
3329 if (vnet_lisp_add_del_adjacency (adj_args))
3330 clib_warning ("failed to del adjacency!");
3332 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
3333 0 /* is_static */ , 0);
3334 mapping_delete_timer (lcm, mi);
3338 mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
3339 f64 expiration_time)
3342 u64 now = clib_cpu_time_now ();
3343 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
3344 u64 exp_clock_time = now + expiration_time * cpu_cps;
3346 m = pool_elt_at_index (lcm->mapping_pool, mi);
3349 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
3353 map_records_arg_free (map_records_arg_t * a)
3355 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3357 vec_foreach (m, a->mappings)
3359 vec_free (m->locators);
3360 gid_address_free (&m->eid);
3362 pool_put (lcm->map_records_args_pool[vlib_get_thread_index ()], a);
3366 process_map_reply (map_records_arg_t * a)
3369 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3370 u32 dst_map_index = 0;
3371 pending_map_request_t *pmr;
3375 if (a->is_rloc_probe)
3378 /* Check pending requests table and nonce */
3379 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
3382 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
3385 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
3387 vec_foreach (m, a->mappings)
3389 /* insert/update mappings cache */
3390 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
3391 m->authoritative, m->ttl,
3392 1, 0 /* is_static */ , &dst_map_index);
3394 if (dst_map_index == (u32) ~ 0)
3397 /* try to program forwarding only if mapping saved or updated */
3398 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3399 memset (adj_args, 0, sizeof (adj_args[0]));
3401 gid_address_copy (&adj_args->leid, &pmr->src);
3402 gid_address_copy (&adj_args->reid, &m->eid);
3403 adj_args->is_add = 1;
3404 if (vnet_lisp_add_del_adjacency (adj_args))
3405 clib_warning ("failed to add adjacency!");
3407 if ((u32) ~ 0 != m->ttl)
3408 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
3411 /* remove pending map request entry */
3414 clib_fifo_foreach (noncep, pmr->nonces, ({
3415 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
3419 clib_fifo_free (pmr->nonces);
3420 pool_put (lcm->pending_map_requests_pool, pmr);
3428 is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
3429 lisp_key_type_t key_id, u8 * key)
3435 auth_data_len = auth_data_len_by_key_id (key_id);
3436 if ((u16) ~ 0 == auth_data_len)
3438 clib_warning ("invalid length for key_id %d!", key_id);
3442 /* save auth data */
3443 vec_validate (auth_data, auth_data_len - 1);
3444 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
3446 /* clear auth data */
3447 memset (MNOTIFY_DATA (h), 0, auth_data_len);
3449 /* get hash of the message */
3450 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3451 (unsigned char *) h, msg_len, NULL, NULL);
3453 result = memcmp (code, auth_data, auth_data_len);
3455 vec_free (auth_data);
3461 process_map_notify (map_records_arg_t * a)
3463 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3466 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
3469 clib_warning ("No pending map-register entry with nonce %lu!",
3475 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
3479 get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
3483 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
3486 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
3490 return pool_elt_at_index (lcm->mapping_pool, mi);
3494 * When map-notify is received it is necessary that all EIDs in the record
3495 * list share common key. The key is then used to verify authentication
3496 * data in map-notify message.
3499 map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
3500 u32 key_id, u8 ** key_out)
3502 u32 i, len = vec_len (maps);
3505 /* get key of the first mapping */
3506 m = get_mapping (lcm, &maps[0].eid);
3510 key_out[0] = m->key;
3512 for (i = 1; i < len; i++)
3514 m = get_mapping (lcm, &maps[i].eid);
3518 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
3520 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
3528 parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
3530 locator_t *locators = 0;
3536 memset (&m, 0, sizeof (m));
3538 /* parse record eid */
3539 for (i = 0; i < count; i++)
3542 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
3545 clib_warning ("Failed to parse mapping record!");
3546 vec_foreach (loc, locators) locator_free (loc);
3547 vec_free (locators);
3551 m.locators = locators;
3552 gid_address_copy (&m.eid, &deid);
3553 vec_add1 (a->mappings, m);
3559 static map_records_arg_t *
3560 map_record_args_get ()
3562 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3563 map_records_arg_t *rec;
3567 pool_foreach (rec, lcm->map_records_args_pool[vlib_get_thread_index()], ({
3569 map_records_arg_free (rec);
3573 pool_get (lcm->map_records_args_pool[vlib_get_thread_index ()], rec);
3577 static map_records_arg_t *
3578 parse_map_notify (vlib_buffer_t * b)
3581 map_notify_hdr_t *mnotif_hdr;
3582 lisp_key_type_t key_id;
3583 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3586 u16 auth_data_len = 0;
3588 map_records_arg_t *a;
3590 a = map_record_args_get ();
3591 memset (a, 0, sizeof (*a));
3592 mnotif_hdr = vlib_buffer_get_current (b);
3593 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
3594 memset (&deid, 0, sizeof (deid));
3596 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
3597 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
3598 auth_data_len = auth_data_len_by_key_id (key_id);
3600 /* advance buffer by authentication data */
3601 vlib_buffer_pull (b, auth_data_len);
3603 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
3604 rc = parse_map_records (b, a, record_count);
3607 map_records_arg_free (a);
3611 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
3614 map_records_arg_free (a);
3618 /* verify authentication data */
3619 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
3620 - (u8 *) mnotif_hdr, key_id, key))
3622 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
3624 map_records_arg_free (a);
3630 static vlib_buffer_t *
3631 build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
3632 ip_address_t * dst, u64 nonce, u8 probe_bit,
3633 mapping_t * records, u16 dst_port, u32 * bi_res)
3637 vlib_main_t *vm = lcm->vlib_main;
3639 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3641 clib_warning ("Can't allocate buffer for Map-Register!");
3645 b = vlib_get_buffer (vm, bi);
3647 /* leave some space for the encap headers */
3648 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3650 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
3652 /* push outer ip header */
3653 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
3660 send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
3661 u8 probe_bit, u64 nonce, u16 dst_port,
3662 ip_address_t * probed_loc)
3668 u32 next_index, *to_next;
3669 mapping_t *records = 0, *m;
3671 m = pool_elt_at_index (lcm->mapping_pool, mi);
3675 vec_add1 (records, m[0]);
3676 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
3677 memset (&src, 0, sizeof (src));
3679 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
3681 clib_warning ("can't find inteface address for %U", format_ip_address,
3686 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
3690 free_map_register_records (records);
3692 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3693 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3694 ip4_lookup_node.index : ip6_lookup_node.index;
3696 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3698 /* Enqueue the packet */
3699 to_next = vlib_frame_vector_args (f);
3702 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3707 find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
3709 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
3710 if (start < 0 && start < -sizeof (b->pre_data))
3716 *ip_hdr = b->data + start;
3717 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
3722 process_map_request (vlib_main_t * vm, vlib_node_runtime_t * node,
3723 lisp_cp_main_t * lcm, vlib_buffer_t * b)
3726 ip_address_t *dst_loc = 0, probed_loc, src_loc;
3728 map_request_hdr_t *mreq_hdr;
3729 gid_address_t src, dst;
3731 u32 i, len = 0, rloc_probe_recv = 0;
3732 gid_address_t *itr_rlocs = 0;
3734 mreq_hdr = vlib_buffer_get_current (b);
3735 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
3738 ("Only SMR Map-Requests and RLOC probe supported for now!");
3742 vlib_buffer_pull (b, sizeof (*mreq_hdr));
3743 nonce = MREQ_NONCE (mreq_hdr);
3746 len = lisp_msg_parse_addr (b, &src);
3750 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3751 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
3755 /* parse eid records and send SMR-invoked map-requests */
3756 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
3758 memset (&dst, 0, sizeof (dst));
3759 len = lisp_msg_parse_eid_rec (b, &dst);
3762 clib_warning ("Can't parse map-request EID-record");
3766 if (MREQ_SMR (mreq_hdr))
3768 /* send SMR-invoked map-requests */
3769 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3771 else if (MREQ_RLOC_PROBE (mreq_hdr))
3773 find_ip_header (b, &ip_hdr);
3776 clib_warning ("Cannot find the IP header!");
3780 memset (&m, 0, sizeof (m));
3781 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3783 // TODO: select best locator; for now use the first one
3784 dst_loc = &gid_address_ip (&itr_rlocs[0]);
3786 /* get src/dst IP addresses */
3787 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
3789 // TODO get source port from buffer
3790 u16 src_port = LISP_CONTROL_PORT;
3792 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
3793 src_port, &probed_loc);
3798 vlib_node_increment_counter (vm, node->node_index,
3799 LISP_CP_INPUT_ERROR_RLOC_PROBE_REQ_RECEIVED,
3801 vec_free (itr_rlocs);
3805 parse_map_reply (vlib_buffer_t * b)
3812 map_reply_hdr_t *mrep_hdr;
3813 map_records_arg_t *a;
3815 a = map_record_args_get ();
3816 memset (a, 0, sizeof (*a));
3818 locator_t *locators;
3820 mrep_hdr = vlib_buffer_get_current (b);
3821 a->nonce = MREP_NONCE (mrep_hdr);
3822 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
3823 if (!vlib_buffer_has_space (b, sizeof (*mrep_hdr)))
3828 vlib_buffer_pull (b, sizeof (*mrep_hdr));
3830 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3832 memset (&m, 0, sizeof (m));
3834 h = vlib_buffer_get_current (b);
3836 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3837 m.action = MAP_REC_ACTION (h);
3838 m.authoritative = MAP_REC_AUTH (h);
3840 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3843 clib_warning ("Failed to parse mapping record!");
3844 map_records_arg_free (a);
3848 m.locators = locators;
3849 gid_address_copy (&m.eid, &deid);
3850 vec_add1 (a->mappings, m);
3856 queue_map_reply_for_processing (map_records_arg_t * a)
3858 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
3862 queue_map_notify_for_processing (map_records_arg_t * a)
3864 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
3868 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
3869 vlib_frame_t * from_frame)
3871 u32 n_left_from, *from, *to_next_drop, rloc_probe_rep_recv = 0,
3872 map_notifies_recv = 0;
3873 lisp_msg_type_e type;
3874 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3875 map_records_arg_t *a;
3877 from = vlib_frame_vector_args (from_frame);
3878 n_left_from = from_frame->n_vectors;
3881 while (n_left_from > 0)
3883 u32 n_left_to_next_drop;
3885 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3886 to_next_drop, n_left_to_next_drop);
3887 while (n_left_from > 0 && n_left_to_next_drop > 0)
3895 to_next_drop[0] = bi0;
3897 n_left_to_next_drop -= 1;
3899 b0 = vlib_get_buffer (vm, bi0);
3901 type = lisp_msg_type (vlib_buffer_get_current (b0));
3904 case LISP_MAP_REPLY:
3905 a = parse_map_reply (b0);
3908 if (a->is_rloc_probe)
3909 rloc_probe_rep_recv++;
3910 queue_map_reply_for_processing (a);
3913 case LISP_MAP_REQUEST:
3914 process_map_request (vm, node, lcm, b0);
3916 case LISP_MAP_NOTIFY:
3917 a = parse_map_notify (b0);
3920 map_notifies_recv++;
3921 queue_map_notify_for_processing (a);
3925 clib_warning ("Unsupported LISP message type %d", type);
3929 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
3931 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3937 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3938 n_left_to_next_drop);
3940 vlib_node_increment_counter (vm, node->node_index,
3941 LISP_CP_INPUT_ERROR_RLOC_PROBE_REP_RECEIVED,
3942 rloc_probe_rep_recv);
3943 vlib_node_increment_counter (vm, node->node_index,
3944 LISP_CP_INPUT_ERROR_MAP_NOTIFIES_RECEIVED,
3946 return from_frame->n_vectors;
3950 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3951 .function = lisp_cp_input,
3952 .name = "lisp-cp-input",
3953 .vector_size = sizeof (u32),
3954 .format_trace = format_lisp_cp_input_trace,
3955 .type = VLIB_NODE_TYPE_INTERNAL,
3957 .n_errors = LISP_CP_INPUT_N_ERROR,
3958 .error_strings = lisp_cp_input_error_strings,
3960 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3963 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3969 lisp_cp_init (vlib_main_t * vm)
3971 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3972 clib_error_t *error = 0;
3973 vlib_thread_main_t *vtm = vlib_get_thread_main ();
3976 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3979 lcm->im4 = &ip4_main;
3980 lcm->im6 = &ip6_main;
3981 lcm->vlib_main = vm;
3982 lcm->vnet_main = vnet_get_main ();
3983 lcm->mreq_itr_rlocs = ~0;
3986 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
3988 gid_dictionary_init (&lcm->mapping_index_by_gid);
3989 lcm->do_map_resolver_election = 1;
3990 lcm->map_request_mode = MR_MODE_DST_ONLY;
3992 num_threads = 1 /* main thread */ + vtm->n_threads;
3993 vec_validate (lcm->map_records_args_pool, num_threads - 1);
3995 /* default vrf mapped to vni 0 */
3996 hash_set (lcm->table_id_by_vni, 0, 0);
3997 hash_set (lcm->vni_by_table_id, 0, 0);
3999 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
4000 lisp_cp_input_node.index, 1 /* is_ip4 */ );
4001 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
4002 lisp_cp_input_node.index, 0 /* is_ip4 */ );
4004 u64 now = clib_cpu_time_now ();
4005 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
4006 lcm->nsh_map_index = ~0;
4011 lisp_stats_api_fill (lisp_cp_main_t * lcm, lisp_gpe_main_t * lgm,
4012 lisp_api_stats_t * stat, lisp_stats_key_t * key,
4016 vlib_combined_counter_main_t *cm = &lgm->counters;
4017 lisp_gpe_fwd_entry_key_t fwd_key;
4018 const lisp_gpe_tunnel_t *lgt;
4021 memset (stat, 0, sizeof (*stat));
4022 memset (&fwd_key, 0, sizeof (fwd_key));
4024 fe = pool_elt_at_index (lcm->fwd_entry_pool, key->fwd_entry_index);
4027 gid_to_dp_address (&fe->reid, &stat->deid);
4028 gid_to_dp_address (&fe->leid, &stat->seid);
4029 stat->vni = gid_address_vni (&fe->reid);
4031 lgt = lisp_gpe_tunnel_get (key->tunnel_index);
4032 stat->loc_rloc = lgt->key->lcl;
4033 stat->rmt_rloc = lgt->key->rmt;
4035 vlib_get_combined_counter (cm, stats_index, &v);
4041 vnet_lisp_get_stats (void)
4043 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
4044 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4045 lisp_api_stats_t *stats = 0, stat;
4046 lisp_stats_key_t *key;
4050 hash_foreach_mem (key, index, lgm->lisp_stats_index_by_key,
4052 if (lisp_stats_api_fill (lcm, lgm, &stat, key, index))
4053 vec_add1 (stats, stat);
4061 send_map_request_thread_fn (void *arg)
4063 map_request_args_t *a = arg;
4064 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4067 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4069 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4075 queue_map_request (gid_address_t * seid, gid_address_t * deid,
4076 u8 smr_invoked, u8 is_resend)
4078 map_request_args_t a;
4080 a.is_resend = is_resend;
4081 gid_address_copy (&a.seid, seid);
4082 gid_address_copy (&a.deid, deid);
4083 a.smr_invoked = smr_invoked;
4085 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
4086 (u8 *) & a, sizeof (a));
4091 * Take an action with a pending map request depending on expiration time
4092 * and re-try counters.
4095 update_pending_request (pending_map_request_t * r, f64 dt)
4097 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4100 if (r->time_to_expire - dt < 0)
4101 /* it's time to decide what to do with this pending request */
4103 if (r->retries_num >= NUMBER_OF_RETRIES)
4104 /* too many retries -> assume current map resolver is not available */
4106 mr = get_map_resolver (&lcm->active_map_resolver);
4109 clib_warning ("Map resolver %U not found - probably deleted "
4110 "by the user recently.", format_ip_address,
4111 &lcm->active_map_resolver);
4115 clib_warning ("map resolver %U is unreachable, ignoring",
4116 format_ip_address, &lcm->active_map_resolver);
4118 /* mark current map resolver unavailable so it won't be
4119 * selected next time */
4121 mr->last_update = vlib_time_now (lcm->vlib_main);
4124 reset_pending_mr_counters (r);
4125 elect_map_resolver (lcm);
4127 /* try to find a next eligible map resolver and re-send */
4128 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4134 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4137 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
4141 r->time_to_expire -= dt;
4145 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4148 pending_map_request_t *pmr;
4149 u32 *to_be_removed = 0, *pmr_index;
4152 pool_foreach (pmr, lcm->pending_map_requests_pool,
4154 if (pmr->to_be_removed)
4156 clib_fifo_foreach (nonce, pmr->nonces, ({
4157 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4160 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4165 vec_foreach (pmr_index, to_be_removed)
4166 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
4168 vec_free (to_be_removed);
4172 update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4174 static f64 time_left = RLOC_PROBING_INTERVAL;
4176 if (!lcm->is_enabled || !lcm->rloc_probing)
4182 time_left = RLOC_PROBING_INTERVAL;
4183 send_rloc_probes (lcm);
4188 update_map_register (lisp_cp_main_t * lcm, f64 dt)
4190 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4191 static u64 mreg_sent_counter = 0;
4193 if (!lcm->is_enabled || !lcm->map_registering)
4199 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4200 time_left = MAP_REGISTER_INTERVAL;
4203 mreg_sent_counter++;
4204 time_left = QUICK_MAP_REGISTER_INTERVAL;
4206 send_map_register (lcm, 1 /* want map notify */ );
4211 send_map_resolver_service (vlib_main_t * vm,
4212 vlib_node_runtime_t * rt, vlib_frame_t * f)
4216 pending_map_request_t *pmr;
4217 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4221 vlib_process_wait_for_event_or_clock (vm, period);
4223 /* currently no signals are expected - just wait for clock */
4224 (void) vlib_process_get_events (vm, 0);
4227 pool_foreach (pmr, lcm->pending_map_requests_pool,
4229 if (!pmr->to_be_removed)
4230 update_pending_request (pmr, period);
4234 remove_dead_pending_map_requests (lcm);
4236 update_map_register (lcm, period);
4237 update_rloc_probing (lcm, period);
4239 u64 now = clib_cpu_time_now ();
4241 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
4242 if (vec_len (expired) > 0)
4245 vec_foreach (mi, expired)
4247 remove_expired_mapping (lcm, mi[0]);
4249 _vec_len (expired) = 0;
4258 vnet_lisp_stats_enable_disable (u8 enable)
4260 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4262 if (vnet_lisp_enable_disable_status () == 0)
4263 return VNET_API_ERROR_LISP_DISABLED;
4266 lcm->flags |= LISP_FLAG_STATS_ENABLED;
4268 lcm->flags &= ~LISP_FLAG_STATS_ENABLED;
4274 vnet_lisp_stats_enable_disable_state (void)
4276 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4278 if (vnet_lisp_enable_disable_status () == 0)
4279 return VNET_API_ERROR_LISP_DISABLED;
4281 return lcm->flags & LISP_FLAG_STATS_ENABLED;
4285 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
4286 .function = send_map_resolver_service,
4287 .type = VLIB_NODE_TYPE_PROCESS,
4288 .name = "lisp-retry-service",
4289 .process_log2_n_stack_bytes = 16,
4293 VLIB_INIT_FUNCTION (lisp_cp_init);
4296 * fd.io coding-style-patch-verification: ON
4299 * eval: (c-set-style "gnu")