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>
26 #include <openssl/evp.h>
27 #include <openssl/hmac.h>
29 lisp_cp_main_t lisp_control_main;
31 u8 *format_lisp_cp_input_trace (u8 * s, va_list * args);
35 LISP_CP_INPUT_NEXT_DROP,
37 } lisp_cp_input_next_t;
55 vnet_lisp_get_map_request_mode (void)
57 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
58 return lcm->map_request_mode;
62 auth_data_len_by_key_id (lisp_key_type_t key_id)
67 return SHA1_AUTH_DATA_LEN;
68 case HMAC_SHA_256_128:
69 return SHA256_AUTH_DATA_LEN;
71 clib_warning ("unsupported key type: %d!", key_id);
78 get_encrypt_fcn (lisp_key_type_t key_id)
84 case HMAC_SHA_256_128:
87 clib_warning ("unsupported encryption key type: %d!", key_id);
94 queue_map_request (gid_address_t * seid, gid_address_t * deid,
95 u8 smr_invoked, u8 is_resend);
97 ip_interface_address_t *
98 ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
99 u32 sw_if_index, u8 loop)
101 vnet_main_t *vnm = vnet_get_main ();
102 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
103 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
104 sw_if_index = swif->unnumbered_sw_if_index;
106 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
107 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
109 return pool_elt_at_index ((lm)->if_address_pool, ia);
113 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
116 ip_interface_address_t *ia;
118 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
121 return ip_interface_address_get_address (lm, ia);
125 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
126 u8 version, ip_address_t * result)
128 ip_lookup_main_t *lm;
131 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
132 addr = ip_interface_get_first_address (lm, sw_if_index, version);
136 ip_address_set (result, addr, version);
141 * convert from a LISP address to a FIB prefix
144 ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
146 if (addr->version == IP4)
149 prefix->fp_proto = FIB_PROTOCOL_IP4;
150 memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
151 memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
155 prefix->fp_len = 128;
156 prefix->fp_proto = FIB_PROTOCOL_IP6;
157 memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
162 * convert from a LISP to a FIB prefix
165 ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
166 fib_prefix_t * fib_prefix)
168 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
169 fib_prefix->fp_len = ip_prefix->len;
173 * Find the sw_if_index of the interface that would be used to egress towards
177 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
179 fib_node_index_t fei;
182 ip_address_to_fib_prefix (dst, &prefix);
184 fei = fib_table_lookup (0, &prefix);
186 return (fib_entry_get_resolving_interface (fei));
190 * Find first IP of the interface that would be used to egress towards dst.
191 * Returns 1 if the address is found 0 otherwise.
194 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
195 ip_address_t * result)
198 ip_lookup_main_t *lm;
202 ASSERT (result != 0);
204 ipver = ip_addr_version (dst);
206 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
207 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
212 /* find the first ip address */
213 addr = ip_interface_get_first_address (lm, si, ipver);
217 ip_address_set (result, addr, ipver);
222 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
228 dp_table = hash_get (lcm->table_id_by_vni, vni);
232 clib_warning ("vni %d not associated to a vrf!", vni);
233 return VNET_API_ERROR_INVALID_VALUE;
238 dp_table = hash_get (lcm->bd_id_by_vni, vni);
241 clib_warning ("vni %d not associated to a bridge domain!", vni);
242 return VNET_API_ERROR_INVALID_VALUE;
246 /* enable/disable data-plane interface */
250 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
252 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
257 lisp_gpe_tenant_l2_iface_unlock (vni);
259 lisp_gpe_tenant_l3_iface_unlock (vni);
266 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
268 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
271 memset (a, 0, sizeof (*a));
273 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
277 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
279 /* delete dp fwd entry */
282 a->locator_pairs = fe->locator_pairs;
283 a->vni = gid_address_vni (&fe->reid);
284 gid_address_copy (&a->rmt_eid, &fe->reid);
286 gid_address_copy (&a->lcl_eid, &fe->leid);
288 vnet_lisp_gpe_del_fwd_counters (a, feip[0]);
289 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
291 /* delete entry in fwd table */
292 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
293 vec_free (fe->locator_pairs);
294 pool_put (lcm->fwd_entry_pool, fe);
298 * Finds first remote locator with best (lowest) priority that has a local
299 * peer locator with an underlying route to it.
303 get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
304 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
306 u32 i, limitp = 0, li, found = 0, esi;
307 locator_set_t *rmt_ls, *lcl_ls;
308 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
309 locator_t *lp, *rmt = 0;
314 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
316 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
318 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
325 /* find unvisited remote locator with best priority */
326 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
328 if (0 != hash_get (checked, i))
331 li = vec_elt (rmt_ls->locator_indices, i);
332 lp = pool_elt_at_index (lcm->locator_pool, li);
334 /* we don't support non-IP locators for now */
335 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
338 if ((found && lp->priority == limitp)
339 || (!found && lp->priority >= limitp))
343 /* don't search for locators with lower priority and don't
344 * check this locator again*/
345 limitp = lp->priority;
346 hash_set (checked, i, 1);
350 /* check if a local locator with a route to remote locator exists */
353 /* find egress sw_if_index for rmt locator */
355 ip_fib_get_egress_iface_for_dst (lcm,
356 &gid_address_ip (&rmt->address));
357 if ((u32) ~ 0 == esi)
360 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
362 li = vec_elt (lcl_ls->locator_indices, i);
363 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
365 /* found local locator with the needed sw_if_index */
366 if (sl->sw_if_index == esi)
368 /* and it has an address */
369 if (0 == ip_interface_get_first_ip_address (lcm,
371 gid_address_ip_version
376 memset (&pair, 0, sizeof (pair));
377 ip_address_copy (&pair.rmt_loc,
378 &gid_address_ip (&rmt->address));
379 ip_address_copy (&pair.lcl_loc, lcl_addr);
380 pair.weight = rmt->weight;
381 pair.priority = rmt->priority;
382 vec_add1 (locator_pairs[0], pair);
396 gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
399 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
403 switch (fid_addr_type (fid))
405 case FID_ADDR_IP_PREF:
406 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
407 gid_address_ippref (dst) = fid_addr_ippref (fid);
410 gid_address_type (dst) = GID_ADDR_MAC;
411 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
414 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
420 vnet_lisp_map_register_state_get (void)
422 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
423 return lcm->map_registering;
427 vnet_lisp_rloc_probe_state_get (void)
429 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
430 return lcm->rloc_probing;
434 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
436 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
437 gid_address_t *rmt_eid, *lcl_eid;
438 mapping_t *lcl_map, *rmt_map;
440 uword *feip = 0, *dpid;
442 u8 type, is_src_dst = 0;
445 memset (a, 0, sizeof (*a));
447 /* remove entry if it already exists */
448 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
450 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
453 * Determine local mapping and eid
456 lcl_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
458 lcl_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
459 lcl_eid = &lcl_map->eid;
462 * Determine remote mapping and eid
464 rmt_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
465 rmt_eid = &rmt_map->eid;
468 * Build and insert data plane forwarding entry
472 if (MR_MODE_SRC_DST == lcm->map_request_mode)
474 if (GID_ADDR_SRC_DST == gid_address_type (rmt_eid))
476 gid_address_sd_to_flat (&a->rmt_eid, rmt_eid,
477 &gid_address_sd_dst (rmt_eid));
478 gid_address_sd_to_flat (&a->lcl_eid, rmt_eid,
479 &gid_address_sd_src (rmt_eid));
483 gid_address_copy (&a->rmt_eid, rmt_eid);
484 gid_address_copy (&a->lcl_eid, lcl_eid);
489 gid_address_copy (&a->rmt_eid, rmt_eid);
491 a->vni = gid_address_vni (&a->rmt_eid);
492 a->is_src_dst = is_src_dst;
494 /* get vrf or bd_index associated to vni */
495 type = gid_address_type (&a->rmt_eid);
496 if (GID_ADDR_IP_PREFIX == type)
498 dpid = hash_get (lcm->table_id_by_vni, a->vni);
501 clib_warning ("vni %d not associated to a vrf!", a->vni);
504 a->table_id = dpid[0];
506 else if (GID_ADDR_MAC == type)
508 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
511 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
517 /* find best locator pair that 1) verifies LISP policy 2) are connected */
518 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
520 /* Either rmt mapping is negative or we can't find underlay path.
521 * Try again with petr if configured */
522 if (rv == 0 && (lcm->flags & LISP_FLAG_USE_PETR))
524 rmt_map = lisp_get_petr_mapping (lcm);
525 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
532 a->action = rmt_map->action;
535 rv = vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
538 if (a->locator_pairs)
539 vec_free (a->locator_pairs);
543 /* add tunnel to fwd entry table */
544 pool_get (lcm->fwd_entry_pool, fe);
545 vnet_lisp_gpe_add_fwd_counters (a, fe - lcm->fwd_entry_pool);
547 fe->locator_pairs = a->locator_pairs;
548 gid_address_copy (&fe->reid, &a->rmt_eid);
551 gid_address_copy (&fe->leid, &a->lcl_eid);
553 gid_address_copy (&fe->leid, lcl_eid);
555 fe->is_src_dst = is_src_dst;
556 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
557 fe - lcm->fwd_entry_pool);
564 } fwd_entry_mt_arg_t;
567 dp_add_fwd_entry_thread_fn (void *arg)
569 fwd_entry_mt_arg_t *a = arg;
570 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
571 dp_add_fwd_entry (lcm, a->si, a->di);
576 dp_add_fwd_entry_from_mt (u32 si, u32 di)
578 fwd_entry_mt_arg_t a;
580 memset (&a, 0, sizeof (a));
584 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
585 (u8 *) & a, sizeof (a));
590 * Returns vector of adjacencies.
592 * The caller must free the vector returned by this function.
594 * @param vni virtual network identifier
595 * @return vector of adjacencies
598 vnet_lisp_adjacencies_get_by_vni (u32 vni)
600 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
602 lisp_adjacency_t *adjs = 0, adj;
605 pool_foreach(fwd, lcm->fwd_entry_pool,
607 if (gid_address_vni (&fwd->reid) != vni)
610 gid_address_copy (&adj.reid, &fwd->reid);
611 gid_address_copy (&adj.leid, &fwd->leid);
612 vec_add1 (adjs, adj);
620 get_map_server (ip_address_t * a)
622 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
625 vec_foreach (m, lcm->map_servers)
627 if (!ip_address_cmp (&m->address, a))
636 get_map_resolver (ip_address_t * a)
638 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
641 vec_foreach (m, lcm->map_resolvers)
643 if (!ip_address_cmp (&m->address, a))
652 vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
655 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
656 lisp_msmr_t _ms, *ms = &_ms;
658 if (vnet_lisp_enable_disable_status () == 0)
660 clib_warning ("LISP is disabled!");
661 return VNET_API_ERROR_LISP_DISABLED;
666 if (get_map_server (addr))
668 clib_warning ("map-server %U already exists!", format_ip_address,
673 memset (ms, 0, sizeof (*ms));
674 ip_address_copy (&ms->address, addr);
675 vec_add1 (lcm->map_servers, ms[0]);
679 for (i = 0; i < vec_len (lcm->map_servers); i++)
681 ms = vec_elt_at_index (lcm->map_servers, i);
682 if (!ip_address_cmp (&ms->address, addr))
684 vec_del1 (lcm->map_servers, i);
694 * Add/remove mapping to/from map-cache. Overwriting not allowed.
697 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
698 u32 * map_index_result)
700 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
701 u32 mi, *map_indexp, map_index, i;
702 mapping_t *m, *old_map;
705 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
706 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
709 /* TODO check if overwriting and take appropriate actions */
710 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
712 clib_warning ("eid %U found in the eid-table", format_gid_address,
714 return VNET_API_ERROR_VALUE_EXIST;
717 pool_get (lcm->mapping_pool, m);
718 gid_address_copy (&m->eid, &a->eid);
719 m->locator_set_index = a->locator_set_index;
721 m->action = a->action;
723 m->is_static = a->is_static;
724 m->key = vec_dup (a->key);
725 m->key_id = a->key_id;
727 map_index = m - lcm->mapping_pool;
728 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
731 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
733 clib_warning ("Locator set with index %d doesn't exist",
734 a->locator_set_index);
735 return VNET_API_ERROR_INVALID_VALUE;
738 /* add eid to list of eids supported by locator-set */
739 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
740 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
741 a->locator_set_index);
742 vec_add1 (eid_indexes[0], map_index);
747 vec_add1 (lcm->local_mappings_indexes, map_index);
749 map_index_result[0] = map_index;
753 if (mi == GID_LOOKUP_MISS)
755 clib_warning ("eid %U not found in the eid-table",
756 format_gid_address, &a->eid);
757 return VNET_API_ERROR_INVALID_VALUE;
760 /* clear locator-set to eids binding */
761 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
762 a->locator_set_index);
763 for (i = 0; i < vec_len (eid_indexes[0]); i++)
765 map_indexp = vec_elt_at_index (eid_indexes[0], i);
766 if (map_indexp[0] == mi)
769 vec_del1 (eid_indexes[0], i);
771 /* remove local mark if needed */
772 m = pool_elt_at_index (lcm->mapping_pool, mi);
776 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
778 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
779 if (lm_indexp[0] == mi)
782 vec_del1 (lcm->local_mappings_indexes, k);
785 /* remove mapping from dictionary */
786 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
787 gid_address_free (&m->eid);
788 pool_put_index (lcm->mapping_pool, mi);
795 * Add/update/delete mapping to/in/from map-cache.
798 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
799 u32 * map_index_result)
805 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
807 if (vnet_lisp_enable_disable_status () == 0)
809 clib_warning ("LISP is disabled!");
810 return VNET_API_ERROR_LISP_DISABLED;
813 vni = gid_address_vni (&a->eid);
814 type = gid_address_type (&a->eid);
815 if (GID_ADDR_IP_PREFIX == type)
816 dp_table = hash_get (lcm->table_id_by_vni, vni);
817 else if (GID_ADDR_MAC == type)
818 dp_table = hash_get (lcm->bd_id_by_vni, vni);
822 clib_warning ("vni %d not associated to a %s!", vni,
823 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
824 return VNET_API_ERROR_INVALID_VALUE;
827 /* store/remove mapping from map-cache */
828 return vnet_lisp_map_cache_add_del (a, map_index_result);
832 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
834 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
835 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
837 if (vnet_lisp_enable_disable_status () == 0)
839 clib_warning ("LISP is disabled!");
843 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
844 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
846 if (!is_l2 && (vni == 0 || dp_id == 0))
848 clib_warning ("can't add/del default vni-vrf mapping!");
852 dp_idp = hash_get (dp_table_by_vni[0], vni);
853 vnip = hash_get (vni_by_dp_table[0], dp_id);
859 clib_warning ("vni %d or vrf %d already used in vrf/vni "
860 "mapping!", vni, dp_id);
863 hash_set (dp_table_by_vni[0], vni, dp_id);
864 hash_set (vni_by_dp_table[0], dp_id, vni);
866 /* create dp iface */
867 dp_add_del_iface (lcm, vni, is_l2, 1);
871 if (!dp_idp || !vnip)
873 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
874 "mapping!", vni, dp_id);
877 /* remove dp iface */
878 dp_add_del_iface (lcm, vni, is_l2, 0);
880 hash_unset (dp_table_by_vni[0], vni);
881 hash_unset (vni_by_dp_table[0], dp_id);
887 /* return 0 if the two locator sets are identical 1 otherwise */
889 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
890 locator_t * new_locators)
893 locator_t *old_loc, *new_loc;
895 if (vec_len (old_ls_indexes) != vec_len (new_locators))
898 for (i = 0; i < vec_len (new_locators); i++)
900 old_li = vec_elt (old_ls_indexes, i);
901 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
903 new_loc = vec_elt_at_index (new_locators, i);
905 if (locator_cmp (old_loc, new_loc))
915 gid_address_t *eids_to_be_deleted;
916 } remove_mapping_args_t;
919 * Callback invoked when a sub-prefix is found
922 remove_mapping_if_needed (u32 mi, void *arg)
925 remove_mapping_args_t *a = arg;
926 lisp_cp_main_t *lcm = a->lcm;
930 m = pool_elt_at_index (lcm->mapping_pool, mi);
934 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
938 if (0 != vec_len (ls->locator_indices))
943 if (0 == vec_len (ls->locator_indices))
948 vec_add1 (a->eids_to_be_deleted, m->eid);
952 * This function searches map cache and looks for IP prefixes that are subset
953 * of the provided one. If such prefix is found depending on 'is_negative'
956 * 1) if is_negative is true and found prefix points to positive mapping,
957 * then the mapping is removed
958 * 2) if is_negative is false and found prefix points to negative mapping,
959 * then the mapping is removed
962 remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
966 remove_mapping_args_t a;
968 memset (&a, 0, sizeof (a));
970 /* do this only in src/dst mode ... */
971 if (MR_MODE_SRC_DST != lcm->map_request_mode)
974 /* ... and only for IP prefix */
975 if (GID_ADDR_SRC_DST != gid_address_type (eid)
976 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
979 a.is_negative = is_negative;
982 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
983 remove_mapping_if_needed, &a);
985 vec_foreach (e, a.eids_to_be_deleted)
987 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
989 memset (adj_args, 0, sizeof (adj_args[0]));
990 gid_address_copy (&adj_args->reid, e);
991 adj_args->is_add = 0;
992 if (vnet_lisp_add_del_adjacency (adj_args))
993 clib_warning ("failed to del adjacency!");
995 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
998 vec_free (a.eids_to_be_deleted);
1002 mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1004 timing_wheel_delete (&lcm->wheel, mi);
1008 is_local_ip (lisp_cp_main_t * lcm, ip_address_t * addr)
1010 fib_node_index_t fei;
1011 fib_prefix_t prefix;
1012 fib_entry_flag_t flags;
1014 ip_address_to_fib_prefix (addr, &prefix);
1016 fei = fib_table_lookup (0, &prefix);
1017 flags = fib_entry_get_flags (fei);
1018 return (FIB_ENTRY_FLAG_LOCAL & flags);
1022 * Adds/removes/updates mapping. Does not program forwarding.
1024 * @param eid end-host identifier
1025 * @param rlocs vector of remote locators
1026 * @param action action for negative map-reply
1027 * @param is_add add mapping if non-zero, delete otherwise
1028 * @param res_map_index the map-index that was created/updated/removed. It is
1029 * set to ~0 if no action is taken.
1030 * @param is_static used for distinguishing between statically learned
1031 remote mappings and mappings obtained from MR
1032 * @return return code
1035 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
1036 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
1037 u32 * res_map_index)
1039 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1040 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1041 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1042 u32 mi, ls_index = 0, dst_map_index;
1046 if (vnet_lisp_enable_disable_status () == 0)
1048 clib_warning ("LISP is disabled!");
1049 return VNET_API_ERROR_LISP_DISABLED;
1053 res_map_index[0] = ~0;
1055 memset (m_args, 0, sizeof (m_args[0]));
1056 memset (ls_args, 0, sizeof (ls_args[0]));
1058 ls_args->locators = rlocs;
1060 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
1061 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1065 /* check if none of the locators match localy configured address */
1066 vec_foreach (loc, rlocs)
1068 ip_prefix_t *p = &gid_address_ippref (&loc->address);
1069 if (is_local_ip (lcm, &ip_prefix_addr (p)))
1071 clib_warning ("RLOC %U matches a local address!",
1072 format_gid_address, &loc->address);
1073 return VNET_API_ERROR_LISP_RLOC_LOCAL;
1077 /* overwrite: if mapping already exists, decide if locators should be
1078 * updated and be done */
1079 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
1081 if (!is_static && (old_map->is_static || old_map->local))
1083 /* do not overwrite local or static remote mappings */
1084 clib_warning ("mapping %U rejected due to collision with local "
1085 "or static remote mapping!", format_gid_address,
1090 locator_set_t *old_ls;
1092 /* update mapping attributes */
1093 old_map->action = action;
1094 old_map->authoritative = authoritative;
1097 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1098 old_map->locator_set_index);
1099 if (compare_locators (lcm, old_ls->locator_indices,
1102 /* set locator-set index to overwrite */
1103 ls_args->is_add = 1;
1104 ls_args->index = old_map->locator_set_index;
1105 vnet_lisp_add_del_locator_set (ls_args, 0);
1107 res_map_index[0] = mi;
1113 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1115 ls_args->is_add = 1;
1116 ls_args->index = ~0;
1118 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1121 gid_address_copy (&m_args->eid, eid);
1123 m_args->action = action;
1124 m_args->locator_set_index = ls_index;
1125 m_args->is_static = is_static;
1127 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
1130 res_map_index[0] = dst_map_index;
1135 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
1137 clib_warning ("cannot delete mapping for eid %U",
1138 format_gid_address, eid);
1143 gid_address_copy (&m_args->eid, eid);
1144 m_args->locator_set_index = old_map->locator_set_index;
1146 /* delete mapping associated from map-cache */
1147 vnet_lisp_map_cache_add_del (m_args, 0);
1149 ls_args->is_add = 0;
1150 ls_args->index = old_map->locator_set_index;
1151 /* delete locator set */
1152 vnet_lisp_add_del_locator_set (ls_args, 0);
1154 /* delete timer associated to the mapping if any */
1155 if (old_map->timer_set)
1156 mapping_delete_timer (lcm, mi);
1158 /* return old mapping index */
1160 res_map_index[0] = mi;
1168 vnet_lisp_clear_all_remote_adjacencies (void)
1171 u32 mi, *map_indices = 0, *map_indexp;
1172 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1173 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1174 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1177 pool_foreach_index (mi, lcm->mapping_pool,
1179 vec_add1 (map_indices, mi);
1183 vec_foreach (map_indexp, map_indices)
1185 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1188 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
1190 dm_args->is_add = 0;
1191 gid_address_copy (&dm_args->eid, &map->eid);
1192 dm_args->locator_set_index = map->locator_set_index;
1194 /* delete mapping associated to fwd entry */
1195 vnet_lisp_map_cache_add_del (dm_args, 0);
1199 ls->index = map->locator_set_index;
1200 /* delete locator set */
1201 rv = vnet_lisp_add_del_locator_set (ls, 0);
1209 vec_free (map_indices);
1214 * Adds adjacency or removes forwarding entry associated to remote mapping.
1215 * Note that adjacencies are not stored, they only result in forwarding entries
1219 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1221 lisp_cp_main_t *lcm = &lisp_control_main;
1222 u32 local_mi, remote_mi = ~0;
1224 if (vnet_lisp_enable_disable_status () == 0)
1226 clib_warning ("LISP is disabled!");
1227 return VNET_API_ERROR_LISP_DISABLED;
1230 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1231 &a->reid, &a->leid);
1232 if (GID_LOOKUP_MISS == remote_mi)
1234 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1235 format_gid_address, &a->reid);
1242 /* check if source eid has an associated mapping. If pitr mode is on,
1243 * just use the pitr's mapping */
1244 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
1245 gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->leid);
1247 if (GID_LOOKUP_MISS == local_mi)
1249 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1250 format_gid_address, &a->leid);
1255 /* update forwarding */
1256 dp_add_fwd_entry (lcm, local_mi, remote_mi);
1259 dp_del_fwd_entry (lcm, 0, remote_mi);
1265 vnet_lisp_set_map_request_mode (u8 mode)
1267 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1269 if (vnet_lisp_enable_disable_status () == 0)
1271 clib_warning ("LISP is disabled!");
1272 return VNET_API_ERROR_LISP_DISABLED;
1275 if (mode >= _MR_MODE_MAX)
1277 clib_warning ("Invalid LISP map request mode %d!", mode);
1278 return VNET_API_ERROR_INVALID_ARGUMENT;
1281 lcm->map_request_mode = mode;
1286 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1288 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1289 u32 locator_set_index = ~0;
1293 if (vnet_lisp_enable_disable_status () == 0)
1295 clib_warning ("LISP is disabled!");
1296 return VNET_API_ERROR_LISP_DISABLED;
1299 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1302 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1305 locator_set_index = p[0];
1309 pool_get (lcm->mapping_pool, m);
1310 m->locator_set_index = locator_set_index;
1313 lcm->pitr_map_index = m - lcm->mapping_pool;
1315 /* enable pitr mode */
1320 /* remove pitr mapping */
1321 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1323 /* disable pitr mode */
1330 * Configure Proxy-ETR
1332 * @param ip PETR's IP address
1333 * @param is_add Flag that indicates if this is an addition or removal
1335 * return 0 on success
1338 vnet_lisp_use_petr (ip_address_t * ip, u8 is_add)
1340 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1343 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1346 if (vnet_lisp_enable_disable_status () == 0)
1348 clib_warning ("LISP is disabled!");
1349 return VNET_API_ERROR_LISP_DISABLED;
1352 memset (ls_args, 0, sizeof (*ls_args));
1356 /* Create dummy petr locator-set */
1357 memset (&loc, 0, sizeof (loc));
1358 gid_address_from_ip (&loc.address, ip);
1360 loc.state = loc.weight = 1;
1363 ls_args->is_add = 1;
1364 ls_args->index = ~0;
1365 vec_add1 (ls_args->locators, loc);
1366 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1368 /* Add petr mapping */
1369 pool_get (lcm->mapping_pool, m);
1370 m->locator_set_index = ls_index;
1371 lcm->petr_map_index = m - lcm->mapping_pool;
1373 /* Enable use-petr */
1374 lcm->flags |= LISP_FLAG_USE_PETR;
1378 m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1380 /* Remove petr locator */
1381 ls_args->is_add = 0;
1382 ls_args->index = m->locator_set_index;
1383 vnet_lisp_add_del_locator_set (ls_args, 0);
1385 /* Remove petr mapping */
1386 pool_put_index (lcm->mapping_pool, lcm->petr_map_index);
1388 /* Disable use-petr */
1389 lcm->flags &= ~LISP_FLAG_USE_PETR;
1394 /* cleans locator to locator-set data and removes locators not part of
1395 * any locator-set */
1397 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1399 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
1400 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1401 for (i = 0; i < vec_len (ls->locator_indices); i++)
1403 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1404 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1406 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1408 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1409 if (ls_indexp[0] == lsi)
1413 /* delete index for removed locator-set */
1414 vec_del1 (ls_indexes[0], j);
1416 /* delete locator if it's part of no locator-set */
1417 if (vec_len (ls_indexes[0]) == 0)
1419 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1420 vec_add1 (to_be_deleted, i);
1426 for (i = 0; i < vec_len (to_be_deleted); i++)
1428 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1429 vec_del1 (ls->locator_indices, loc_indexp[0]);
1431 vec_free (to_be_deleted);
1435 static inline uword *
1436 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
1438 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1443 /* find locator-set */
1446 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
1457 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
1463 ASSERT (ls != NULL);
1464 ASSERT (loc != NULL);
1466 vec_foreach (locit, ls->locator_indices)
1468 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1469 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1470 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1472 clib_warning ("Duplicate locator");
1473 return VNET_API_ERROR_VALUE_EXIST;
1481 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1482 u32 ls_index, u32 loc_id)
1484 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1485 u32 **ls_indexes = NULL;
1487 ASSERT (ls != NULL);
1488 ASSERT (locit != NULL);
1490 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1491 pool_put_index (lcm->locator_pool, locit[0]);
1492 vec_del1 (ls->locator_indices, loc_id);
1493 vec_del1 (ls_indexes[0], ls_index);
1497 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1498 locator_set_t * ls, u32 * ls_result)
1500 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1501 locator_t *loc = NULL, *itloc = NULL;
1502 uword _p = (u32) ~ 0, *p = &_p;
1503 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
1509 if (vnet_lisp_enable_disable_status () == 0)
1511 clib_warning ("LISP is disabled!");
1512 return VNET_API_ERROR_LISP_DISABLED;
1515 p = get_locator_set_index (a, p);
1518 clib_warning ("locator-set %v doesn't exist", a->name);
1519 return VNET_API_ERROR_INVALID_ARGUMENT;
1524 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1527 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1529 return VNET_API_ERROR_INVALID_ARGUMENT;
1536 ls_result[0] = p[0];
1538 /* allocate locators */
1539 vec_foreach (itloc, a->locators)
1541 ret = is_locator_in_locator_set (lcm, ls, itloc);
1547 pool_get (lcm->locator_pool, loc);
1549 loc_index = loc - lcm->locator_pool;
1551 vec_add1 (ls->locator_indices, loc_index);
1553 vec_validate (lcm->locator_to_locator_sets, loc_index);
1554 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1556 vec_add1 (ls_indexes[0], p[0]);
1563 itloc = a->locators;
1565 vec_foreach (locit, ls->locator_indices)
1567 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1569 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1571 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1573 if (0 == loc->local &&
1574 !gid_address_cmp (&loc->address, &itloc->address))
1576 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1587 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
1590 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1592 uword _p = (u32) ~ 0, *p = &_p;
1597 if (vnet_lisp_enable_disable_status () == 0)
1599 clib_warning ("LISP is disabled!");
1600 return VNET_API_ERROR_LISP_DISABLED;
1605 p = get_locator_set_index (a, p);
1608 if (p && p[0] != (u32) ~ 0)
1610 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1613 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1618 /* clean locator to locator-set vectors and remove locators if
1619 * they're not part of another locator-set */
1620 clean_locator_to_locator_set (lcm, p[0]);
1622 /* remove locator indices from locator set */
1623 vec_free (ls->locator_indices);
1628 ls_result[0] = p[0];
1630 /* new locator-set */
1633 pool_get (lcm->locator_set_pool, ls);
1634 memset (ls, 0, sizeof (*ls));
1635 ls_index = ls - lcm->locator_set_pool;
1639 ls->name = vec_dup (a->name);
1641 if (!lcm->locator_set_index_by_name)
1642 lcm->locator_set_index_by_name = hash_create_vec (
1650 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
1653 /* mark as local locator-set */
1654 vec_add1 (lcm->local_locator_set_indexes, ls_index);
1656 ls->local = a->local;
1658 ls_result[0] = ls_index;
1661 ret = vnet_lisp_add_del_locator (a, ls, NULL);
1669 p = get_locator_set_index (a, p);
1672 clib_warning ("locator-set %v doesn't exists", a->name);
1676 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1679 clib_warning ("locator-set with index %d doesn't exists", p[0]);
1683 if (lcm->mreq_itr_rlocs == p[0])
1685 clib_warning ("Can't delete the locator-set used to constrain "
1686 "the itr-rlocs in map-requests!");
1690 if (vec_len (lcm->locator_set_to_eids) != 0)
1692 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
1693 if (vec_len (eid_indexes[0]) != 0)
1696 ("Can't delete a locator that supports a mapping!");
1701 /* clean locator to locator-sets data */
1702 clean_locator_to_locator_set (lcm, p[0]);
1708 vec_foreach_index (it, lcm->local_locator_set_indexes)
1710 lsi = vec_elt (lcm->local_locator_set_indexes, it);
1713 vec_del1 (lcm->local_locator_set_indexes, it);
1717 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
1719 vec_free (ls->name);
1720 vec_free (ls->locator_indices);
1721 pool_put (lcm->locator_set_pool, ls);
1727 vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
1729 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1731 lcm->rloc_probing = is_enable;
1736 vnet_lisp_map_register_enable_disable (u8 is_enable)
1738 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1740 lcm->map_registering = is_enable;
1745 vnet_lisp_enable_disable (u8 is_enable)
1748 clib_error_t *error = 0;
1749 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1750 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
1752 a->is_en = is_enable;
1753 error = vnet_lisp_gpe_enable_disable (a);
1756 return clib_error_return (0, "failed to %s data-plane!",
1757 a->is_en ? "enable" : "disable");
1762 /* enable all l2 and l3 ifaces */
1765 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
1766 dp_add_del_iface(lcm, vni, 0, 1);
1768 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
1769 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
1775 /* clear interface table */
1776 hash_free (lcm->fwd_entry_by_mapping_index);
1777 pool_free (lcm->fwd_entry_pool);
1780 /* update global flag */
1781 lcm->is_enabled = is_enable;
1787 vnet_lisp_enable_disable_status (void)
1789 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1790 return lcm->is_enabled;
1794 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
1796 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1798 lisp_msmr_t _mr, *mr = &_mr;
1800 if (vnet_lisp_enable_disable_status () == 0)
1802 clib_warning ("LISP is disabled!");
1803 return VNET_API_ERROR_LISP_DISABLED;
1809 if (get_map_resolver (&a->address))
1811 clib_warning ("map-resolver %U already exists!", format_ip_address,
1816 memset (mr, 0, sizeof (*mr));
1817 ip_address_copy (&mr->address, &a->address);
1818 vec_add1 (lcm->map_resolvers, *mr);
1820 if (vec_len (lcm->map_resolvers) == 1)
1821 lcm->do_map_resolver_election = 1;
1825 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
1827 mr = vec_elt_at_index (lcm->map_resolvers, i);
1828 if (!ip_address_cmp (&mr->address, &a->address))
1830 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
1831 lcm->do_map_resolver_election = 1;
1833 vec_del1 (lcm->map_resolvers, i);
1842 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
1844 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1847 if (vnet_lisp_enable_disable_status () == 0)
1849 clib_warning ("LISP is disabled!");
1850 return VNET_API_ERROR_LISP_DISABLED;
1855 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
1858 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
1859 return VNET_API_ERROR_INVALID_ARGUMENT;
1862 lcm->mreq_itr_rlocs = p[0];
1866 lcm->mreq_itr_rlocs = ~0;
1872 /* Statistics (not really errors) */
1873 #define foreach_lisp_cp_lookup_error \
1875 _(MAP_REQUESTS_SENT, "map-request sent")
1877 static char *lisp_cp_lookup_error_strings[] = {
1878 #define _(sym,string) string,
1879 foreach_lisp_cp_lookup_error
1885 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
1886 foreach_lisp_cp_lookup_error
1888 LISP_CP_LOOKUP_N_ERROR,
1889 } lisp_cp_lookup_error_t;
1893 LISP_CP_LOOKUP_NEXT_DROP,
1894 LISP_CP_LOOKUP_N_NEXT,
1895 } lisp_cp_lookup_next_t;
1899 gid_address_t dst_eid;
1900 ip_address_t map_resolver_ip;
1901 } lisp_cp_lookup_trace_t;
1904 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
1906 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1907 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1908 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
1910 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
1911 format_ip_address, &t->map_resolver_ip, format_gid_address,
1917 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
1918 ip_address_t * sloc)
1923 if (vec_len (lcm->map_resolvers) == 0)
1925 clib_warning ("No map-resolver configured");
1929 /* find the first mr ip we have a route to and the ip of the
1930 * iface that has a route to it */
1931 vec_foreach (mrit, lcm->map_resolvers)
1934 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
1936 ip_address_copy (mr_ip, a);
1938 /* also update globals */
1943 clib_warning ("Can't find map-resolver and local interface ip!");
1947 static gid_address_t *
1948 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
1954 ip_interface_address_t *ia = 0;
1955 gid_address_t gid_data, *gid = &gid_data;
1956 gid_address_t *rlocs = 0;
1957 ip_prefix_t *ippref = &gid_address_ippref (gid);
1958 ip_address_t *rloc = &ip_prefix_addr (ippref);
1960 memset (gid, 0, sizeof (gid[0]));
1961 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
1962 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
1964 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
1965 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
1967 /* Add ipv4 locators first TODO sort them */
1970 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
1971 loc->sw_if_index, 1 /* unnumbered */,
1973 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
1974 ip_address_set (rloc, addr, IP4);
1975 ip_prefix_len (ippref) = 32;
1976 ip_prefix_normalize (ippref);
1977 vec_add1 (rlocs, gid[0]);
1980 /* Add ipv6 locators */
1981 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
1982 loc->sw_if_index, 1 /* unnumbered */,
1984 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
1985 ip_address_set (rloc, addr, IP6);
1986 ip_prefix_len (ippref) = 128;
1987 ip_prefix_normalize (ippref);
1988 vec_add1 (rlocs, gid[0]);
1996 static vlib_buffer_t *
1997 build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
1998 ip_address_t * sloc, ip_address_t * rloc,
1999 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
2003 vlib_main_t *vm = lcm->vlib_main;
2005 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2007 clib_warning ("Can't allocate buffer for Map-Request!");
2011 b = vlib_get_buffer (vm, bi);
2013 /* leave some space for the encap headers */
2014 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2017 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
2018 1 /* rloc probe */ , nonce_res);
2020 /* push outer ip header */
2021 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2029 static vlib_buffer_t *
2030 build_encapsulated_map_request (lisp_cp_main_t * lcm,
2031 gid_address_t * seid, gid_address_t * deid,
2032 locator_set_t * loc_set, ip_address_t * mr_ip,
2033 ip_address_t * sloc, u8 is_smr_invoked,
2034 u64 * nonce_res, u32 * bi_res)
2038 gid_address_t *rlocs = 0;
2039 vlib_main_t *vm = lcm->vlib_main;
2041 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2043 clib_warning ("Can't allocate buffer for Map-Request!");
2047 b = vlib_get_buffer (vm, bi);
2049 /* leave some space for the encap headers */
2050 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2053 rlocs = build_itr_rloc_list (lcm, loc_set);
2055 if (MR_MODE_SRC_DST == lcm->map_request_mode
2056 && GID_ADDR_SRC_DST != gid_address_type (deid))
2059 memset (&sd, 0, sizeof (sd));
2060 build_src_dst (&sd, seid, deid);
2061 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
2062 0 /* rloc probe */ , nonce_res);
2067 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
2068 0 /* rloc probe */ , nonce_res);
2071 /* push ecm: udp-ip-lisp */
2072 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2074 /* push outer ip header */
2075 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2085 reset_pending_mr_counters (pending_map_request_t * r)
2087 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2092 elect_map_resolver (lisp_cp_main_t * lcm)
2096 vec_foreach (mr, lcm->map_resolvers)
2100 ip_address_copy (&lcm->active_map_resolver, &mr->address);
2101 lcm->do_map_resolver_election = 0;
2109 free_map_register_records (mapping_t * maps)
2112 vec_foreach (map, maps) vec_free (map->locators);
2118 add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
2119 ip_address_t * probed_loc)
2122 locator_t *loc, new;
2123 ip_interface_address_t *ia = 0;
2125 ip_address_t *new_ip = &gid_address_ip (&new.address);
2128 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2130 vec_foreach (li, ls->locator_indices)
2132 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
2137 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2138 loc->sw_if_index, 1 /* unnumbered */,
2140 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
2142 ip_address_set (new_ip, addr, IP4);
2145 /* Add ipv6 locators */
2146 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2147 loc->sw_if_index, 1 /* unnumbered */,
2149 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
2151 ip_address_set (new_ip, addr, IP6);
2155 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
2158 vec_add1 (m->locators, new);
2163 build_map_register_record_list (lisp_cp_main_t * lcm)
2165 mapping_t *recs = 0, rec, *m;
2168 pool_foreach(m, lcm->mapping_pool,
2170 /* for now build only local mappings */
2175 add_locators (lcm, &rec, m->locator_set_index, NULL);
2176 vec_add1 (recs, rec);
2184 update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
2185 lisp_key_type_t key_id, u8 * key,
2186 u16 auth_data_len, u32 msg_len)
2188 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
2189 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
2191 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
2192 (unsigned char *) map_reg_hdr, msg_len, NULL,
2194 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
2199 static vlib_buffer_t *
2200 build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
2201 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
2202 mapping_t * records, lisp_key_type_t key_id, u8 * key,
2207 u32 bi, auth_data_len = 0, msg_len = 0;
2208 vlib_main_t *vm = lcm->vlib_main;
2210 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2212 clib_warning ("Can't allocate buffer for Map-Register!");
2216 b = vlib_get_buffer (vm, bi);
2218 /* leave some space for the encap headers */
2219 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2221 auth_data_len = auth_data_len_by_key_id (key_id);
2222 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
2223 auth_data_len, nonce_res,
2226 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
2229 /* push outer ip header */
2230 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2238 get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
2241 while (lcm->do_map_resolver_election
2242 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
2243 &lcm->active_map_resolver,
2246 if (0 == elect_map_resolver (lcm))
2247 /* all map resolvers are down */
2249 /* restart MR checking by marking all of them up */
2250 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
2257 /* CP output statistics */
2258 #define foreach_lisp_cp_output_error \
2259 _(MAP_REGISTERS_SENT, "map-registers sent") \
2260 _(RLOC_PROBES_SENT, "rloc-probes sent")
2262 static char *lisp_cp_output_error_strings[] = {
2263 #define _(sym,string) string,
2264 foreach_lisp_cp_output_error
2270 #define _(sym,str) LISP_CP_OUTPUT_ERROR_##sym,
2271 foreach_lisp_cp_output_error
2273 LISP_CP_OUTPUT_N_ERROR,
2274 } lisp_cp_output_error_t;
2277 lisp_cp_output (vlib_main_t * vm, vlib_node_runtime_t * node,
2278 vlib_frame_t * from_frame)
2283 /* dummy node used only for statistics */
2285 VLIB_REGISTER_NODE (lisp_cp_output_node) = {
2286 .function = lisp_cp_output,
2287 .name = "lisp-cp-output",
2288 .vector_size = sizeof (u32),
2289 .format_trace = format_lisp_cp_input_trace,
2290 .type = VLIB_NODE_TYPE_INTERNAL,
2292 .n_errors = LISP_CP_OUTPUT_N_ERROR,
2293 .error_strings = lisp_cp_output_error_strings,
2295 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2298 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2304 send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
2305 u32 local_locator_set_index, ip_address_t * sloc,
2306 ip_address_t * rloc)
2313 u32 next_index, *to_next;
2314 gid_address_t *itr_rlocs;
2316 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
2317 itr_rlocs = build_itr_rloc_list (lcm, ls);
2319 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
2320 vec_free (itr_rlocs);
2324 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2326 next_index = (ip_addr_version (rloc) == IP4) ?
2327 ip4_lookup_node.index : ip6_lookup_node.index;
2329 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2331 /* Enqueue the packet */
2332 to_next = vlib_frame_vector_args (f);
2335 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2337 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2342 send_rloc_probes (lisp_cp_main_t * lcm)
2348 u32 si, rloc_probes_sent = 0;
2351 pool_foreach (e, lcm->fwd_entry_pool,
2353 if (vec_len (e->locator_pairs) == 0)
2356 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
2359 clib_warning ("internal error: cannot find local eid %U in "
2360 "map-cache!", format_gid_address, &e->leid);
2363 lm = pool_elt_at_index (lcm->mapping_pool, si);
2365 /* get the best (lowest) priority */
2366 lprio = e->locator_pairs[0].priority;
2368 /* send rloc-probe for pair(s) with the best remote locator priority */
2369 vec_foreach (lp, e->locator_pairs)
2371 if (lp->priority != lprio)
2374 /* get first remote locator */
2375 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
2382 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2383 LISP_CP_OUTPUT_ERROR_RLOC_PROBES_SENT,
2389 send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
2391 u32 bi, map_registers_sent = 0;
2396 u32 next_index, *to_next;
2397 ip_address_t *ms = 0;
2398 mapping_t *records, *r, *g;
2400 // TODO: support multiple map servers and do election
2401 if (0 == vec_len (lcm->map_servers))
2404 ms = &lcm->map_servers[0].address;
2406 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
2408 clib_warning ("no eligible interface address found for %U!",
2409 format_ip_address, &lcm->map_servers[0]);
2413 records = build_map_register_record_list (lcm);
2417 vec_foreach (r, records)
2420 u8 key_id = r->key_id;
2423 continue; /* no secret key -> map-register cannot be sent */
2426 // TODO: group mappings that share common key
2428 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, g,
2434 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2436 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2437 ip4_lookup_node.index : ip6_lookup_node.index;
2439 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2441 /* Enqueue the packet */
2442 to_next = vlib_frame_vector_args (f);
2445 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2446 map_registers_sent++;
2448 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2450 free_map_register_records (records);
2452 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2453 LISP_CP_OUTPUT_ERROR_MAP_REGISTERS_SENT,
2454 map_registers_sent);
2459 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
2460 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2462 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2463 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2466 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
2467 gid_address_t * seid, gid_address_t * deid,
2468 u8 is_smr_invoked, u8 is_resend)
2470 u32 next_index, bi = 0, *to_next, map_index;
2474 locator_set_t *loc_set;
2476 pending_map_request_t *pmr, *duplicate_pmr = 0;
2480 /* if there is already a pending request remember it */
2483 pool_foreach(pmr, lcm->pending_map_requests_pool,
2485 if (!gid_address_cmp (&pmr->src, seid)
2486 && !gid_address_cmp (&pmr->dst, deid))
2488 duplicate_pmr = pmr;
2494 if (!is_resend && duplicate_pmr)
2496 /* don't send the request if there is a pending map request already */
2500 /* get locator-set for seid */
2501 if (!lcm->lisp_pitr)
2503 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2504 if (map_index == ~0)
2506 clib_warning ("No local mapping found in eid-table for %U!",
2507 format_gid_address, seid);
2511 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2516 ("Mapping found for src eid %U is not marked as local!",
2517 format_gid_address, seid);
2520 ls_index = map->locator_set_index;
2524 map_index = lcm->pitr_map_index;
2525 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
2526 ls_index = map->locator_set_index;
2529 /* overwrite locator set if map-request itr-rlocs configured */
2530 if (~0 != lcm->mreq_itr_rlocs)
2532 ls_index = lcm->mreq_itr_rlocs;
2535 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
2537 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
2540 duplicate_pmr->to_be_removed = 1;
2544 /* build the encapsulated map request */
2545 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
2546 &lcm->active_map_resolver,
2547 &sloc, is_smr_invoked, &nonce, &bi);
2552 /* set fib index to default and lookup node */
2553 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2554 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2555 ip4_lookup_node.index : ip6_lookup_node.index;
2557 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2559 /* Enqueue the packet */
2560 to_next = vlib_frame_vector_args (f);
2563 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2566 /* if there is a pending request already update it */
2568 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
2570 /* remove the oldest nonce */
2571 u64 CLIB_UNUSED (tmp), *nonce_del;
2572 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
2573 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
2574 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
2577 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
2578 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2579 duplicate_pmr - lcm->pending_map_requests_pool);
2583 /* add map-request to pending requests table */
2584 pool_get (lcm->pending_map_requests_pool, pmr);
2585 memset (pmr, 0, sizeof (*pmr));
2586 gid_address_copy (&pmr->src, seid);
2587 gid_address_copy (&pmr->dst, deid);
2588 clib_fifo_add1 (pmr->nonces, nonce);
2589 pmr->is_smr_invoked = is_smr_invoked;
2590 reset_pending_mr_counters (pmr);
2591 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2592 pmr - lcm->pending_map_requests_pool);
2599 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
2601 ip4_header_t *ip4 = hdr;
2604 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2606 ip_address_set (src, &ip4->src_address, IP4);
2607 ip_address_set (dst, &ip4->dst_address, IP4);
2612 ip_address_set (src, &ip6->src_address, IP6);
2613 ip_address_set (dst, &ip6->dst_address, IP6);
2618 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2622 u32 vni = ~0, table_id = ~0;
2624 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
2625 IP4 ? FIB_PROTOCOL_IP4 :
2631 vnip = hash_get (lcm->vni_by_table_id, table_id);
2635 clib_warning ("vrf %d is not mapped to any vni!", table_id);
2641 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2647 l2input_main_t *l2im = &l2input_main;
2648 l2_input_config_t *config;
2649 l2_bridge_domain_t *bd_config;
2651 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
2652 config = vec_elt_at_index (l2im->configs, sw_if_index0);
2653 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
2655 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
2659 clib_warning ("bridge domain %d is not mapped to any vni!",
2666 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2667 gid_address_t * src, gid_address_t * dst,
2672 memset (src, 0, sizeof (*src));
2673 memset (dst, 0, sizeof (*dst));
2675 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
2678 u8 version, preflen;
2680 gid_address_type (src) = GID_ADDR_IP_PREFIX;
2681 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
2683 ip = vlib_buffer_get_current (b);
2684 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
2686 version = gid_address_ip_version (src);
2687 preflen = ip_address_max_len (version);
2688 gid_address_ippref_len (src) = preflen;
2689 gid_address_ippref_len (dst) = preflen;
2691 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
2692 gid_address_vni (dst) = vni;
2693 gid_address_vni (src) = vni;
2695 else if (LISP_AFI_MAC == type)
2697 ethernet_header_t *eh;
2699 eh = vlib_buffer_get_current (b);
2701 gid_address_type (src) = GID_ADDR_MAC;
2702 gid_address_type (dst) = GID_ADDR_MAC;
2703 mac_copy (&gid_address_mac (src), eh->src_address);
2704 mac_copy (&gid_address_mac (dst), eh->dst_address);
2707 vni = lisp_get_vni_from_buffer_eth (lcm, b);
2709 gid_address_vni (dst) = vni;
2710 gid_address_vni (src) = vni;
2712 else if (LISP_AFI_LCAF == type)
2714 /* Eventually extend this to support NSH and other */
2720 lisp_cp_lookup_inline (vlib_main_t * vm,
2721 vlib_node_runtime_t * node,
2722 vlib_frame_t * from_frame, int overlay)
2724 u32 *from, *to_next_drop, di, si;
2725 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2726 u32 pkts_mapped = 0;
2727 uword n_left_from, n_left_to_next_drop;
2729 from = vlib_frame_vector_args (from_frame);
2730 n_left_from = from_frame->n_vectors;
2732 while (n_left_from > 0)
2734 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2735 to_next_drop, n_left_to_next_drop);
2737 while (n_left_from > 0 && n_left_to_next_drop > 0)
2741 gid_address_t src, dst;
2746 to_next_drop[0] = pi0;
2748 n_left_to_next_drop -= 1;
2750 b0 = vlib_get_buffer (vm, pi0);
2751 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
2753 /* src/dst eid pair */
2754 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst, overlay);
2756 /* if we have remote mapping for destination already in map-chache
2757 add forwarding tunnel directly. If not send a map-request */
2758 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
2762 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
2763 /* send a map-request also in case of negative mapping entry
2764 with corresponding action */
2765 if (m->action == LISP_SEND_MAP_REQUEST)
2767 /* send map-request */
2768 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2769 0 /* is_resend */ );
2774 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
2778 dp_add_fwd_entry_from_mt (si, di);
2784 /* send map-request */
2785 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2786 0 /* is_resend */ );
2790 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2792 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
2795 memset (tr, 0, sizeof (*tr));
2796 gid_address_copy (&tr->dst_eid, &dst);
2797 ip_address_copy (&tr->map_resolver_ip,
2798 &lcm->active_map_resolver);
2800 gid_address_free (&dst);
2801 gid_address_free (&src);
2804 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2805 n_left_to_next_drop);
2807 vlib_node_increment_counter (vm, node->node_index,
2808 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
2810 return from_frame->n_vectors;
2814 lisp_cp_lookup_ip4 (vlib_main_t * vm,
2815 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2817 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
2821 lisp_cp_lookup_ip6 (vlib_main_t * vm,
2822 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2824 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
2828 lisp_cp_lookup_l2 (vlib_main_t * vm,
2829 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2831 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
2835 lisp_cp_lookup_nsh (vlib_main_t * vm,
2836 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2838 /* TODO decide if NSH should be propagated as LCAF or not */
2839 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_LCAF));
2843 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
2844 .function = lisp_cp_lookup_ip4,
2845 .name = "lisp-cp-lookup-ip4",
2846 .vector_size = sizeof (u32),
2847 .format_trace = format_lisp_cp_lookup_trace,
2848 .type = VLIB_NODE_TYPE_INTERNAL,
2850 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2851 .error_strings = lisp_cp_lookup_error_strings,
2853 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2856 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2862 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
2863 .function = lisp_cp_lookup_ip6,
2864 .name = "lisp-cp-lookup-ip6",
2865 .vector_size = sizeof (u32),
2866 .format_trace = format_lisp_cp_lookup_trace,
2867 .type = VLIB_NODE_TYPE_INTERNAL,
2869 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2870 .error_strings = lisp_cp_lookup_error_strings,
2872 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2875 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2881 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
2882 .function = lisp_cp_lookup_l2,
2883 .name = "lisp-cp-lookup-l2",
2884 .vector_size = sizeof (u32),
2885 .format_trace = format_lisp_cp_lookup_trace,
2886 .type = VLIB_NODE_TYPE_INTERNAL,
2888 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2889 .error_strings = lisp_cp_lookup_error_strings,
2891 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2894 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2900 VLIB_REGISTER_NODE (lisp_cp_lookup_nsh_node) = {
2901 .function = lisp_cp_lookup_nsh,
2902 .name = "lisp-cp-lookup-nsh",
2903 .vector_size = sizeof (u32),
2904 .format_trace = format_lisp_cp_lookup_trace,
2905 .type = VLIB_NODE_TYPE_INTERNAL,
2907 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2908 .error_strings = lisp_cp_lookup_error_strings,
2910 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2913 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2918 /* lisp_cp_input statistics */
2919 #define foreach_lisp_cp_input_error \
2921 _(RLOC_PROBE_REQ_RECEIVED, "rloc-probe requests received") \
2922 _(RLOC_PROBE_REP_RECEIVED, "rloc-probe replies received") \
2923 _(MAP_NOTIFIES_RECEIVED, "map-notifies received") \
2924 _(MAP_REPLIES_RECEIVED, "map-replies received")
2926 static char *lisp_cp_input_error_strings[] = {
2927 #define _(sym,string) string,
2928 foreach_lisp_cp_input_error
2934 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
2935 foreach_lisp_cp_input_error
2937 LISP_CP_INPUT_N_ERROR,
2938 } lisp_cp_input_error_t;
2942 gid_address_t dst_eid;
2943 ip4_address_t map_resolver_ip;
2944 } lisp_cp_input_trace_t;
2947 format_lisp_cp_input_trace (u8 * s, va_list * args)
2949 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2950 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2951 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
2952 va_arg (*args, lisp_cp_input_trace_t *);
2954 s = format (s, "LISP-CP-INPUT: TODO");
2959 remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
2962 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
2963 memset (adj_args, 0, sizeof (adj_args[0]));
2965 m = pool_elt_at_index (lcm->mapping_pool, mi);
2967 gid_address_copy (&adj_args->reid, &m->eid);
2968 adj_args->is_add = 0;
2969 if (vnet_lisp_add_del_adjacency (adj_args))
2970 clib_warning ("failed to del adjacency!");
2972 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
2973 0 /* is_static */ , 0);
2974 mapping_delete_timer (lcm, mi);
2978 mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
2979 f64 expiration_time)
2982 u64 now = clib_cpu_time_now ();
2983 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
2984 u64 exp_clock_time = now + expiration_time * cpu_cps;
2986 m = pool_elt_at_index (lcm->mapping_pool, mi);
2989 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
2993 map_records_arg_free (map_records_arg_t * a)
2996 vec_foreach (m, a->mappings)
2998 vec_free (m->locators);
2999 gid_address_free (&m->eid);
3006 process_map_reply (map_records_arg_t * a)
3009 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3010 u32 dst_map_index = 0;
3011 pending_map_request_t *pmr;
3015 if (a->is_rloc_probe)
3018 /* Check pending requests table and nonce */
3019 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
3022 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
3025 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
3027 vec_foreach (m, a->mappings)
3029 /* insert/update mappings cache */
3030 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
3031 m->authoritative, m->ttl,
3032 1, 0 /* is_static */ , &dst_map_index);
3034 if (dst_map_index == (u32) ~ 0)
3037 /* try to program forwarding only if mapping saved or updated */
3038 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3039 memset (adj_args, 0, sizeof (adj_args[0]));
3041 gid_address_copy (&adj_args->leid, &pmr->src);
3042 gid_address_copy (&adj_args->reid, &m->eid);
3043 adj_args->is_add = 1;
3044 if (vnet_lisp_add_del_adjacency (adj_args))
3045 clib_warning ("failed to add adjacency!");
3047 if ((u32) ~ 0 != m->ttl)
3048 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
3051 /* remove pending map request entry */
3054 clib_fifo_foreach (noncep, pmr->nonces, ({
3055 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
3059 clib_fifo_free (pmr->nonces);
3060 pool_put (lcm->pending_map_requests_pool, pmr);
3063 map_records_arg_free (a);
3068 is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
3069 lisp_key_type_t key_id, u8 * key)
3075 auth_data_len = auth_data_len_by_key_id (key_id);
3076 if ((u16) ~ 0 == auth_data_len)
3078 clib_warning ("invalid length for key_id %d!", key_id);
3082 /* save auth data */
3083 vec_validate (auth_data, auth_data_len - 1);
3084 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
3086 /* clear auth data */
3087 memset (MNOTIFY_DATA (h), 0, auth_data_len);
3089 /* get hash of the message */
3090 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3091 (unsigned char *) h, msg_len, NULL, NULL);
3093 result = memcmp (code, auth_data, auth_data_len);
3095 vec_free (auth_data);
3101 process_map_notify (map_records_arg_t * a)
3103 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3106 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
3109 clib_warning ("No pending map-register entry with nonce %lu!",
3114 map_records_arg_free (a);
3115 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
3119 get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
3123 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
3126 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
3130 return pool_elt_at_index (lcm->mapping_pool, mi);
3134 * When map-notify is received it is necessary that all EIDs in the record
3135 * list share common key. The key is then used to verify authentication
3136 * data in map-notify message.
3139 map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
3140 u32 key_id, u8 ** key_out)
3142 u32 i, len = vec_len (maps);
3145 /* get key of the first mapping */
3146 m = get_mapping (lcm, &maps[0].eid);
3150 key_out[0] = m->key;
3152 for (i = 1; i < len; i++)
3154 m = get_mapping (lcm, &maps[i].eid);
3158 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
3160 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
3168 parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
3170 locator_t *locators = 0;
3176 /* parse record eid */
3177 for (i = 0; i < count; i++)
3179 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
3182 clib_warning ("Failed to parse mapping record!");
3183 vec_foreach (loc, locators) locator_free (loc);
3184 vec_free (locators);
3188 m.locators = locators;
3189 gid_address_copy (&m.eid, &deid);
3190 vec_add1 (a->mappings, m);
3196 static map_records_arg_t *
3197 parse_map_notify (vlib_buffer_t * b)
3200 map_notify_hdr_t *mnotif_hdr;
3201 lisp_key_type_t key_id;
3202 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3205 u16 auth_data_len = 0;
3207 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3209 memset (a, 0, sizeof (*a));
3210 mnotif_hdr = vlib_buffer_get_current (b);
3211 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
3212 memset (&deid, 0, sizeof (deid));
3214 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
3215 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
3216 auth_data_len = auth_data_len_by_key_id (key_id);
3218 /* advance buffer by authentication data */
3219 vlib_buffer_pull (b, auth_data_len);
3221 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
3222 rc = parse_map_records (b, a, record_count);
3225 map_records_arg_free (a);
3229 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
3232 map_records_arg_free (a);
3236 /* verify authentication data */
3237 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
3238 - (u8 *) mnotif_hdr, key_id, key))
3240 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
3242 map_records_arg_free (a);
3248 static vlib_buffer_t *
3249 build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
3250 ip_address_t * dst, u64 nonce, u8 probe_bit,
3251 mapping_t * records, u16 dst_port, u32 * bi_res)
3255 vlib_main_t *vm = lcm->vlib_main;
3257 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3259 clib_warning ("Can't allocate buffer for Map-Register!");
3263 b = vlib_get_buffer (vm, bi);
3265 /* leave some space for the encap headers */
3266 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3268 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
3270 /* push outer ip header */
3271 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
3278 send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
3279 u8 probe_bit, u64 nonce, u16 dst_port,
3280 ip_address_t * probed_loc)
3286 u32 next_index, *to_next;
3287 mapping_t *records = 0, *m;
3289 m = pool_elt_at_index (lcm->mapping_pool, mi);
3293 vec_add1 (records, m[0]);
3294 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
3295 memset (&src, 0, sizeof (src));
3297 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
3299 clib_warning ("can't find inteface address for %U", format_ip_address,
3304 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
3308 free_map_register_records (records);
3310 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3311 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3312 ip4_lookup_node.index : ip6_lookup_node.index;
3314 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3316 /* Enqueue the packet */
3317 to_next = vlib_frame_vector_args (f);
3320 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3325 find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
3327 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
3328 if (start < 0 && start < -sizeof (b->pre_data))
3334 *ip_hdr = b->data + start;
3335 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
3340 process_map_request (vlib_main_t * vm, vlib_node_runtime_t * node,
3341 lisp_cp_main_t * lcm, vlib_buffer_t * b)
3344 ip_address_t *dst_loc = 0, probed_loc, src_loc;
3346 map_request_hdr_t *mreq_hdr;
3347 gid_address_t src, dst;
3349 u32 i, len = 0, rloc_probe_recv = 0;
3350 gid_address_t *itr_rlocs = 0;
3352 mreq_hdr = vlib_buffer_get_current (b);
3353 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
3356 ("Only SMR Map-Requests and RLOC probe supported for now!");
3360 vlib_buffer_pull (b, sizeof (*mreq_hdr));
3361 nonce = MREQ_NONCE (mreq_hdr);
3364 len = lisp_msg_parse_addr (b, &src);
3368 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3369 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
3373 /* parse eid records and send SMR-invoked map-requests */
3374 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
3376 memset (&dst, 0, sizeof (dst));
3377 len = lisp_msg_parse_eid_rec (b, &dst);
3380 clib_warning ("Can't parse map-request EID-record");
3384 if (MREQ_SMR (mreq_hdr))
3386 /* send SMR-invoked map-requests */
3387 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3389 else if (MREQ_RLOC_PROBE (mreq_hdr))
3391 find_ip_header (b, &ip_hdr);
3394 clib_warning ("Cannot find the IP header!");
3398 memset (&m, 0, sizeof (m));
3399 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3401 // TODO: select best locator; for now use the first one
3402 dst_loc = &gid_address_ip (&itr_rlocs[0]);
3404 /* get src/dst IP addresses */
3405 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
3407 // TODO get source port from buffer
3408 u16 src_port = LISP_CONTROL_PORT;
3410 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
3411 src_port, &probed_loc);
3416 vlib_node_increment_counter (vm, node->node_index,
3417 LISP_CP_INPUT_ERROR_RLOC_PROBE_REQ_RECEIVED,
3419 vec_free (itr_rlocs);
3422 static map_records_arg_t *
3423 parse_map_reply (vlib_buffer_t * b)
3430 map_reply_hdr_t *mrep_hdr;
3431 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3432 memset (a, 0, sizeof (*a));
3433 locator_t *locators;
3435 mrep_hdr = vlib_buffer_get_current (b);
3436 a->nonce = MREP_NONCE (mrep_hdr);
3437 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
3438 vlib_buffer_pull (b, sizeof (*mrep_hdr));
3440 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3442 memset (&m, 0, sizeof (m));
3444 h = vlib_buffer_get_current (b);
3446 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3447 m.action = MAP_REC_ACTION (h);
3448 m.authoritative = MAP_REC_AUTH (h);
3450 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3453 clib_warning ("Failed to parse mapping record!");
3454 map_records_arg_free (a);
3458 m.locators = locators;
3459 gid_address_copy (&m.eid, &deid);
3460 vec_add1 (a->mappings, m);
3466 queue_map_reply_for_processing (map_records_arg_t * a)
3468 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
3472 queue_map_notify_for_processing (map_records_arg_t * a)
3474 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
3478 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
3479 vlib_frame_t * from_frame)
3481 u32 n_left_from, *from, *to_next_drop, rloc_probe_rep_recv = 0,
3482 map_notifies_recv = 0;
3483 lisp_msg_type_e type;
3484 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3485 map_records_arg_t *a;
3487 from = vlib_frame_vector_args (from_frame);
3488 n_left_from = from_frame->n_vectors;
3491 while (n_left_from > 0)
3493 u32 n_left_to_next_drop;
3495 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3496 to_next_drop, n_left_to_next_drop);
3497 while (n_left_from > 0 && n_left_to_next_drop > 0)
3505 to_next_drop[0] = bi0;
3507 n_left_to_next_drop -= 1;
3509 b0 = vlib_get_buffer (vm, bi0);
3511 type = lisp_msg_type (vlib_buffer_get_current (b0));
3514 case LISP_MAP_REPLY:
3515 a = parse_map_reply (b0);
3518 if (a->is_rloc_probe)
3519 rloc_probe_rep_recv++;
3520 queue_map_reply_for_processing (a);
3523 case LISP_MAP_REQUEST:
3524 process_map_request (vm, node, lcm, b0);
3526 case LISP_MAP_NOTIFY:
3527 a = parse_map_notify (b0);
3530 map_notifies_recv++;
3531 queue_map_notify_for_processing (a);
3535 clib_warning ("Unsupported LISP message type %d", type);
3539 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
3541 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3547 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3548 n_left_to_next_drop);
3550 vlib_node_increment_counter (vm, node->node_index,
3551 LISP_CP_INPUT_ERROR_RLOC_PROBE_REP_RECEIVED,
3552 rloc_probe_rep_recv);
3553 vlib_node_increment_counter (vm, node->node_index,
3554 LISP_CP_INPUT_ERROR_MAP_NOTIFIES_RECEIVED,
3556 return from_frame->n_vectors;
3560 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3561 .function = lisp_cp_input,
3562 .name = "lisp-cp-input",
3563 .vector_size = sizeof (u32),
3564 .format_trace = format_lisp_cp_input_trace,
3565 .type = VLIB_NODE_TYPE_INTERNAL,
3567 .n_errors = LISP_CP_INPUT_N_ERROR,
3568 .error_strings = lisp_cp_input_error_strings,
3570 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3573 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3579 lisp_cp_init (vlib_main_t * vm)
3581 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3582 clib_error_t *error = 0;
3584 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3587 lcm->im4 = &ip4_main;
3588 lcm->im6 = &ip6_main;
3589 lcm->vlib_main = vm;
3590 lcm->vnet_main = vnet_get_main ();
3591 lcm->mreq_itr_rlocs = ~0;
3594 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
3596 gid_dictionary_init (&lcm->mapping_index_by_gid);
3597 lcm->do_map_resolver_election = 1;
3598 lcm->map_request_mode = MR_MODE_DST_ONLY;
3600 /* default vrf mapped to vni 0 */
3601 hash_set (lcm->table_id_by_vni, 0, 0);
3602 hash_set (lcm->vni_by_table_id, 0, 0);
3604 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
3605 lisp_cp_input_node.index, 1 /* is_ip4 */ );
3606 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
3607 lisp_cp_input_node.index, 0 /* is_ip4 */ );
3609 u64 now = clib_cpu_time_now ();
3610 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
3615 lisp_stats_api_fill (lisp_cp_main_t * lcm, lisp_gpe_main_t * lgm,
3616 lisp_api_stats_t * stat, lisp_stats_key_t * key,
3620 vlib_combined_counter_main_t *cm = &lgm->counters;
3621 lisp_gpe_fwd_entry_key_t fwd_key;
3622 const lisp_gpe_tunnel_t *lgt;
3625 memset (stat, 0, sizeof (*stat));
3626 memset (&fwd_key, 0, sizeof (fwd_key));
3628 fe = pool_elt_at_index (lcm->fwd_entry_pool, key->fwd_entry_index);
3631 gid_to_dp_address (&fe->reid, &stat->deid);
3632 gid_to_dp_address (&fe->leid, &stat->seid);
3633 stat->vni = gid_address_vni (&fe->reid);
3635 lgt = lisp_gpe_tunnel_get (key->tunnel_index);
3636 stat->loc_rloc = lgt->key->lcl;
3637 stat->rmt_rloc = lgt->key->rmt;
3639 vlib_get_combined_counter (cm, stats_index, &v);
3645 vnet_lisp_get_stats (void)
3647 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
3648 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3649 lisp_api_stats_t *stats = 0, stat;
3650 lisp_stats_key_t *key;
3654 hash_foreach_mem (key, index, lgm->lisp_stats_index_by_key,
3656 if (lisp_stats_api_fill (lcm, lgm, &stat, key, index))
3657 vec_add1 (stats, stat);
3665 send_map_request_thread_fn (void *arg)
3667 map_request_args_t *a = arg;
3668 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3671 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3673 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3679 queue_map_request (gid_address_t * seid, gid_address_t * deid,
3680 u8 smr_invoked, u8 is_resend)
3682 map_request_args_t a;
3684 a.is_resend = is_resend;
3685 gid_address_copy (&a.seid, seid);
3686 gid_address_copy (&a.deid, deid);
3687 a.smr_invoked = smr_invoked;
3689 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
3690 (u8 *) & a, sizeof (a));
3695 * Take an action with a pending map request depending on expiration time
3696 * and re-try counters.
3699 update_pending_request (pending_map_request_t * r, f64 dt)
3701 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3704 if (r->time_to_expire - dt < 0)
3705 /* it's time to decide what to do with this pending request */
3707 if (r->retries_num >= NUMBER_OF_RETRIES)
3708 /* too many retries -> assume current map resolver is not available */
3710 mr = get_map_resolver (&lcm->active_map_resolver);
3713 clib_warning ("Map resolver %U not found - probably deleted "
3714 "by the user recently.", format_ip_address,
3715 &lcm->active_map_resolver);
3719 clib_warning ("map resolver %U is unreachable, ignoring",
3720 format_ip_address, &lcm->active_map_resolver);
3722 /* mark current map resolver unavailable so it won't be
3723 * selected next time */
3725 mr->last_update = vlib_time_now (lcm->vlib_main);
3728 reset_pending_mr_counters (r);
3729 elect_map_resolver (lcm);
3731 /* try to find a next eligible map resolver and re-send */
3732 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3738 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3741 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3745 r->time_to_expire -= dt;
3749 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
3752 pending_map_request_t *pmr;
3753 u32 *to_be_removed = 0, *pmr_index;
3756 pool_foreach (pmr, lcm->pending_map_requests_pool,
3758 if (pmr->to_be_removed)
3760 clib_fifo_foreach (nonce, pmr->nonces, ({
3761 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
3764 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
3769 vec_foreach (pmr_index, to_be_removed)
3770 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
3772 vec_free (to_be_removed);
3776 update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
3778 static f64 time_left = RLOC_PROBING_INTERVAL;
3780 if (!lcm->is_enabled || !lcm->rloc_probing)
3786 time_left = RLOC_PROBING_INTERVAL;
3787 send_rloc_probes (lcm);
3792 update_map_register (lisp_cp_main_t * lcm, f64 dt)
3794 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
3795 static u64 mreg_sent_counter = 0;
3797 if (!lcm->is_enabled || !lcm->map_registering)
3803 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
3804 time_left = MAP_REGISTER_INTERVAL;
3807 mreg_sent_counter++;
3808 time_left = QUICK_MAP_REGISTER_INTERVAL;
3810 send_map_register (lcm, 1 /* want map notify */ );
3815 send_map_resolver_service (vlib_main_t * vm,
3816 vlib_node_runtime_t * rt, vlib_frame_t * f)
3820 pending_map_request_t *pmr;
3821 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3825 vlib_process_wait_for_event_or_clock (vm, period);
3827 /* currently no signals are expected - just wait for clock */
3828 (void) vlib_process_get_events (vm, 0);
3831 pool_foreach (pmr, lcm->pending_map_requests_pool,
3833 if (!pmr->to_be_removed)
3834 update_pending_request (pmr, period);
3838 remove_dead_pending_map_requests (lcm);
3840 update_map_register (lcm, period);
3841 update_rloc_probing (lcm, period);
3843 u64 now = clib_cpu_time_now ();
3845 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
3846 if (vec_len (expired) > 0)
3849 vec_foreach (mi, expired)
3851 remove_expired_mapping (lcm, mi[0]);
3853 _vec_len (expired) = 0;
3862 vnet_lisp_stats_enable_disable (u8 enable)
3864 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3866 if (vnet_lisp_enable_disable_status () == 0)
3867 return VNET_API_ERROR_LISP_DISABLED;
3870 lcm->flags |= LISP_FLAG_STATS_ENABLED;
3872 lcm->flags &= ~LISP_FLAG_STATS_ENABLED;
3878 vnet_lisp_stats_enable_disable_state (void)
3880 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3882 if (vnet_lisp_enable_disable_status () == 0)
3883 return VNET_API_ERROR_LISP_DISABLED;
3885 return lcm->flags & LISP_FLAG_STATS_ENABLED;
3889 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
3890 .function = send_map_resolver_service,
3891 .type = VLIB_NODE_TYPE_PROCESS,
3892 .name = "lisp-retry-service",
3893 .process_log2_n_stack_bytes = 16,
3897 VLIB_INIT_FUNCTION (lisp_cp_init);
3900 * fd.io coding-style-patch-verification: ON
3903 * eval: (c-set-style "gnu")