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 update_adjacencies_by_map_index (lisp_cp_main_t * lcm, u8 is_local,
1482 u32 mapping_index, u8 remove_only)
1486 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1488 map = pool_elt_at_index (lcm->mapping_pool, mapping_index);
1491 pool_foreach(fwd, lcm->fwd_entry_pool,
1493 if ((is_local && 0 == gid_address_cmp (&map->eid, &fwd->leid)) ||
1494 (!is_local && 0 == gid_address_cmp (&map->eid, &fwd->reid)))
1497 gid_address_copy (&a->leid, &fwd->leid);
1498 gid_address_copy (&a->reid, &fwd->reid);
1500 vnet_lisp_add_del_adjacency (a);
1505 vnet_lisp_add_del_adjacency (a);
1513 update_fwd_entries_by_locator_set (lisp_cp_main_t * lcm, u8 is_local,
1514 u32 ls_index, u8 remove_only)
1518 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, ls_index);
1520 for (i = 0; i < vec_len (eid_indexes[0]); i++)
1522 map_indexp = vec_elt_at_index (eid_indexes[0], i);
1523 update_adjacencies_by_map_index (lcm, is_local, map_indexp[0],
1529 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1530 u32 ls_index, u32 loc_id)
1532 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1533 u32 **ls_indexes = NULL;
1535 ASSERT (ls != NULL);
1536 ASSERT (locit != NULL);
1538 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1539 pool_put_index (lcm->locator_pool, locit[0]);
1540 vec_del1 (ls->locator_indices, loc_id);
1541 vec_del1 (ls_indexes[0], ls_index);
1545 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1546 locator_set_t * ls, u32 * ls_result)
1548 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1549 locator_t *loc = NULL, *itloc = NULL;
1550 uword _p = (u32) ~ 0, *p = &_p;
1551 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
1557 if (vnet_lisp_enable_disable_status () == 0)
1559 clib_warning ("LISP is disabled!");
1560 return VNET_API_ERROR_LISP_DISABLED;
1563 p = get_locator_set_index (a, p);
1566 clib_warning ("locator-set %v doesn't exist", a->name);
1567 return VNET_API_ERROR_INVALID_ARGUMENT;
1572 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1575 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1577 return VNET_API_ERROR_INVALID_ARGUMENT;
1584 ls_result[0] = p[0];
1586 /* allocate locators */
1587 vec_foreach (itloc, a->locators)
1589 ret = is_locator_in_locator_set (lcm, ls, itloc);
1595 pool_get (lcm->locator_pool, loc);
1597 loc_index = loc - lcm->locator_pool;
1599 vec_add1 (ls->locator_indices, loc_index);
1601 vec_validate (lcm->locator_to_locator_sets, loc_index);
1602 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1604 vec_add1 (ls_indexes[0], p[0]);
1612 vec_foreach (itloc, a->locators)
1616 vec_foreach (locit, ls->locator_indices)
1618 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1620 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1623 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1625 if (0 == loc->local &&
1626 !gid_address_cmp (&loc->address, &itloc->address))
1629 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1634 /* update fwd entries using this locator in DP */
1635 update_fwd_entries_by_locator_set (lcm, loc->local, ls_index,
1636 vec_len (ls->locator_indices)
1649 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
1652 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1654 uword _p = (u32) ~ 0, *p = &_p;
1659 if (vnet_lisp_enable_disable_status () == 0)
1661 clib_warning ("LISP is disabled!");
1662 return VNET_API_ERROR_LISP_DISABLED;
1667 p = get_locator_set_index (a, p);
1670 if (p && p[0] != (u32) ~ 0)
1672 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1675 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1680 /* clean locator to locator-set vectors and remove locators if
1681 * they're not part of another locator-set */
1682 clean_locator_to_locator_set (lcm, p[0]);
1684 /* remove locator indices from locator set */
1685 vec_free (ls->locator_indices);
1690 ls_result[0] = p[0];
1692 /* new locator-set */
1695 pool_get (lcm->locator_set_pool, ls);
1696 memset (ls, 0, sizeof (*ls));
1697 ls_index = ls - lcm->locator_set_pool;
1701 ls->name = vec_dup (a->name);
1703 if (!lcm->locator_set_index_by_name)
1704 lcm->locator_set_index_by_name = hash_create_vec (
1712 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
1715 /* mark as local locator-set */
1716 vec_add1 (lcm->local_locator_set_indexes, ls_index);
1718 ls->local = a->local;
1720 ls_result[0] = ls_index;
1723 ret = vnet_lisp_add_del_locator (a, ls, NULL);
1731 p = get_locator_set_index (a, p);
1734 clib_warning ("locator-set %v doesn't exists", a->name);
1738 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1741 clib_warning ("locator-set with index %d doesn't exists", p[0]);
1745 if (lcm->mreq_itr_rlocs == p[0])
1747 clib_warning ("Can't delete the locator-set used to constrain "
1748 "the itr-rlocs in map-requests!");
1752 if (vec_len (lcm->locator_set_to_eids) != 0)
1754 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
1755 if (vec_len (eid_indexes[0]) != 0)
1758 ("Can't delete a locator that supports a mapping!");
1763 /* clean locator to locator-sets data */
1764 clean_locator_to_locator_set (lcm, p[0]);
1770 vec_foreach_index (it, lcm->local_locator_set_indexes)
1772 lsi = vec_elt (lcm->local_locator_set_indexes, it);
1775 vec_del1 (lcm->local_locator_set_indexes, it);
1779 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
1781 vec_free (ls->name);
1782 vec_free (ls->locator_indices);
1783 pool_put (lcm->locator_set_pool, ls);
1789 vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
1791 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1793 lcm->rloc_probing = is_enable;
1798 vnet_lisp_map_register_enable_disable (u8 is_enable)
1800 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1802 lcm->map_registering = is_enable;
1807 vnet_lisp_enable_disable (u8 is_enable)
1810 clib_error_t *error = 0;
1811 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1812 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
1814 a->is_en = is_enable;
1815 error = vnet_lisp_gpe_enable_disable (a);
1818 return clib_error_return (0, "failed to %s data-plane!",
1819 a->is_en ? "enable" : "disable");
1824 /* enable all l2 and l3 ifaces */
1827 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
1828 dp_add_del_iface(lcm, vni, 0, 1);
1830 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
1831 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
1837 /* clear interface table */
1838 hash_free (lcm->fwd_entry_by_mapping_index);
1839 pool_free (lcm->fwd_entry_pool);
1842 /* update global flag */
1843 lcm->is_enabled = is_enable;
1849 vnet_lisp_enable_disable_status (void)
1851 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1852 return lcm->is_enabled;
1856 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
1858 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1860 lisp_msmr_t _mr, *mr = &_mr;
1862 if (vnet_lisp_enable_disable_status () == 0)
1864 clib_warning ("LISP is disabled!");
1865 return VNET_API_ERROR_LISP_DISABLED;
1871 if (get_map_resolver (&a->address))
1873 clib_warning ("map-resolver %U already exists!", format_ip_address,
1878 memset (mr, 0, sizeof (*mr));
1879 ip_address_copy (&mr->address, &a->address);
1880 vec_add1 (lcm->map_resolvers, *mr);
1882 if (vec_len (lcm->map_resolvers) == 1)
1883 lcm->do_map_resolver_election = 1;
1887 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
1889 mr = vec_elt_at_index (lcm->map_resolvers, i);
1890 if (!ip_address_cmp (&mr->address, &a->address))
1892 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
1893 lcm->do_map_resolver_election = 1;
1895 vec_del1 (lcm->map_resolvers, i);
1904 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
1906 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1909 if (vnet_lisp_enable_disable_status () == 0)
1911 clib_warning ("LISP is disabled!");
1912 return VNET_API_ERROR_LISP_DISABLED;
1917 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
1920 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
1921 return VNET_API_ERROR_INVALID_ARGUMENT;
1924 lcm->mreq_itr_rlocs = p[0];
1928 lcm->mreq_itr_rlocs = ~0;
1934 /* Statistics (not really errors) */
1935 #define foreach_lisp_cp_lookup_error \
1937 _(MAP_REQUESTS_SENT, "map-request sent")
1939 static char *lisp_cp_lookup_error_strings[] = {
1940 #define _(sym,string) string,
1941 foreach_lisp_cp_lookup_error
1947 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
1948 foreach_lisp_cp_lookup_error
1950 LISP_CP_LOOKUP_N_ERROR,
1951 } lisp_cp_lookup_error_t;
1955 LISP_CP_LOOKUP_NEXT_DROP,
1956 LISP_CP_LOOKUP_N_NEXT,
1957 } lisp_cp_lookup_next_t;
1961 gid_address_t dst_eid;
1962 ip_address_t map_resolver_ip;
1963 } lisp_cp_lookup_trace_t;
1966 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
1968 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1969 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1970 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
1972 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
1973 format_ip_address, &t->map_resolver_ip, format_gid_address,
1979 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
1980 ip_address_t * sloc)
1985 if (vec_len (lcm->map_resolvers) == 0)
1987 clib_warning ("No map-resolver configured");
1991 /* find the first mr ip we have a route to and the ip of the
1992 * iface that has a route to it */
1993 vec_foreach (mrit, lcm->map_resolvers)
1996 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
1998 ip_address_copy (mr_ip, a);
2000 /* also update globals */
2005 clib_warning ("Can't find map-resolver and local interface ip!");
2009 static gid_address_t *
2010 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2016 ip_interface_address_t *ia = 0;
2017 gid_address_t gid_data, *gid = &gid_data;
2018 gid_address_t *rlocs = 0;
2019 ip_prefix_t *ippref = &gid_address_ippref (gid);
2020 ip_address_t *rloc = &ip_prefix_addr (ippref);
2022 memset (gid, 0, sizeof (gid[0]));
2023 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
2024 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
2026 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
2027 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2029 /* Add ipv4 locators first TODO sort them */
2032 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2033 loc->sw_if_index, 1 /* unnumbered */,
2035 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2036 ip_address_set (rloc, addr, IP4);
2037 ip_prefix_len (ippref) = 32;
2038 ip_prefix_normalize (ippref);
2039 vec_add1 (rlocs, gid[0]);
2042 /* Add ipv6 locators */
2043 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2044 loc->sw_if_index, 1 /* unnumbered */,
2046 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2047 ip_address_set (rloc, addr, IP6);
2048 ip_prefix_len (ippref) = 128;
2049 ip_prefix_normalize (ippref);
2050 vec_add1 (rlocs, gid[0]);
2058 static vlib_buffer_t *
2059 build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
2060 ip_address_t * sloc, ip_address_t * rloc,
2061 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
2065 vlib_main_t *vm = lcm->vlib_main;
2067 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2069 clib_warning ("Can't allocate buffer for Map-Request!");
2073 b = vlib_get_buffer (vm, bi);
2075 /* leave some space for the encap headers */
2076 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2079 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
2080 1 /* rloc probe */ , nonce_res);
2082 /* push outer ip header */
2083 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2091 static vlib_buffer_t *
2092 build_encapsulated_map_request (lisp_cp_main_t * lcm,
2093 gid_address_t * seid, gid_address_t * deid,
2094 locator_set_t * loc_set, ip_address_t * mr_ip,
2095 ip_address_t * sloc, u8 is_smr_invoked,
2096 u64 * nonce_res, u32 * bi_res)
2100 gid_address_t *rlocs = 0;
2101 vlib_main_t *vm = lcm->vlib_main;
2103 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2105 clib_warning ("Can't allocate buffer for Map-Request!");
2109 b = vlib_get_buffer (vm, bi);
2111 /* leave some space for the encap headers */
2112 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2115 rlocs = build_itr_rloc_list (lcm, loc_set);
2117 if (MR_MODE_SRC_DST == lcm->map_request_mode
2118 && GID_ADDR_SRC_DST != gid_address_type (deid))
2121 memset (&sd, 0, sizeof (sd));
2122 build_src_dst (&sd, seid, deid);
2123 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
2124 0 /* rloc probe */ , nonce_res);
2129 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
2130 0 /* rloc probe */ , nonce_res);
2133 /* push ecm: udp-ip-lisp */
2134 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2136 /* push outer ip header */
2137 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2147 reset_pending_mr_counters (pending_map_request_t * r)
2149 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2154 elect_map_resolver (lisp_cp_main_t * lcm)
2158 vec_foreach (mr, lcm->map_resolvers)
2162 ip_address_copy (&lcm->active_map_resolver, &mr->address);
2163 lcm->do_map_resolver_election = 0;
2171 free_map_register_records (mapping_t * maps)
2174 vec_foreach (map, maps) vec_free (map->locators);
2180 add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
2181 ip_address_t * probed_loc)
2184 locator_t *loc, new;
2185 ip_interface_address_t *ia = 0;
2187 ip_address_t *new_ip = &gid_address_ip (&new.address);
2190 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2192 vec_foreach (li, ls->locator_indices)
2194 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
2199 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2200 loc->sw_if_index, 1 /* unnumbered */,
2202 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
2204 ip_address_set (new_ip, addr, IP4);
2207 /* Add ipv6 locators */
2208 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2209 loc->sw_if_index, 1 /* unnumbered */,
2211 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
2213 ip_address_set (new_ip, addr, IP6);
2217 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
2220 vec_add1 (m->locators, new);
2225 build_map_register_record_list (lisp_cp_main_t * lcm)
2227 mapping_t *recs = 0, rec, *m;
2230 pool_foreach(m, lcm->mapping_pool,
2232 /* for now build only local mappings */
2237 add_locators (lcm, &rec, m->locator_set_index, NULL);
2238 vec_add1 (recs, rec);
2246 update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
2247 lisp_key_type_t key_id, u8 * key,
2248 u16 auth_data_len, u32 msg_len)
2250 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
2251 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
2253 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
2254 (unsigned char *) map_reg_hdr, msg_len, NULL,
2256 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
2261 static vlib_buffer_t *
2262 build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
2263 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
2264 mapping_t * records, lisp_key_type_t key_id, u8 * key,
2269 u32 bi, auth_data_len = 0, msg_len = 0;
2270 vlib_main_t *vm = lcm->vlib_main;
2272 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2274 clib_warning ("Can't allocate buffer for Map-Register!");
2278 b = vlib_get_buffer (vm, bi);
2280 /* leave some space for the encap headers */
2281 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2283 auth_data_len = auth_data_len_by_key_id (key_id);
2284 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
2285 auth_data_len, nonce_res,
2288 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
2291 /* push outer ip header */
2292 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2300 get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
2303 while (lcm->do_map_resolver_election
2304 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
2305 &lcm->active_map_resolver,
2308 if (0 == elect_map_resolver (lcm))
2309 /* all map resolvers are down */
2311 /* restart MR checking by marking all of them up */
2312 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
2319 /* CP output statistics */
2320 #define foreach_lisp_cp_output_error \
2321 _(MAP_REGISTERS_SENT, "map-registers sent") \
2322 _(RLOC_PROBES_SENT, "rloc-probes sent")
2324 static char *lisp_cp_output_error_strings[] = {
2325 #define _(sym,string) string,
2326 foreach_lisp_cp_output_error
2332 #define _(sym,str) LISP_CP_OUTPUT_ERROR_##sym,
2333 foreach_lisp_cp_output_error
2335 LISP_CP_OUTPUT_N_ERROR,
2336 } lisp_cp_output_error_t;
2339 lisp_cp_output (vlib_main_t * vm, vlib_node_runtime_t * node,
2340 vlib_frame_t * from_frame)
2345 /* dummy node used only for statistics */
2347 VLIB_REGISTER_NODE (lisp_cp_output_node) = {
2348 .function = lisp_cp_output,
2349 .name = "lisp-cp-output",
2350 .vector_size = sizeof (u32),
2351 .format_trace = format_lisp_cp_input_trace,
2352 .type = VLIB_NODE_TYPE_INTERNAL,
2354 .n_errors = LISP_CP_OUTPUT_N_ERROR,
2355 .error_strings = lisp_cp_output_error_strings,
2357 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2360 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2366 send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
2367 u32 local_locator_set_index, ip_address_t * sloc,
2368 ip_address_t * rloc)
2375 u32 next_index, *to_next;
2376 gid_address_t *itr_rlocs;
2378 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
2379 itr_rlocs = build_itr_rloc_list (lcm, ls);
2381 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
2382 vec_free (itr_rlocs);
2386 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2388 next_index = (ip_addr_version (rloc) == IP4) ?
2389 ip4_lookup_node.index : ip6_lookup_node.index;
2391 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2393 /* Enqueue the packet */
2394 to_next = vlib_frame_vector_args (f);
2397 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2399 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2404 send_rloc_probes (lisp_cp_main_t * lcm)
2410 u32 si, rloc_probes_sent = 0;
2413 pool_foreach (e, lcm->fwd_entry_pool,
2415 if (vec_len (e->locator_pairs) == 0)
2418 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
2421 clib_warning ("internal error: cannot find local eid %U in "
2422 "map-cache!", format_gid_address, &e->leid);
2425 lm = pool_elt_at_index (lcm->mapping_pool, si);
2427 /* get the best (lowest) priority */
2428 lprio = e->locator_pairs[0].priority;
2430 /* send rloc-probe for pair(s) with the best remote locator priority */
2431 vec_foreach (lp, e->locator_pairs)
2433 if (lp->priority != lprio)
2436 /* get first remote locator */
2437 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
2444 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2445 LISP_CP_OUTPUT_ERROR_RLOC_PROBES_SENT,
2451 send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
2453 u32 bi, map_registers_sent = 0;
2458 u32 next_index, *to_next;
2459 ip_address_t *ms = 0;
2460 mapping_t *records, *r, *g;
2462 // TODO: support multiple map servers and do election
2463 if (0 == vec_len (lcm->map_servers))
2466 ms = &lcm->map_servers[0].address;
2468 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
2470 clib_warning ("no eligible interface address found for %U!",
2471 format_ip_address, &lcm->map_servers[0]);
2475 records = build_map_register_record_list (lcm);
2479 vec_foreach (r, records)
2482 u8 key_id = r->key_id;
2485 continue; /* no secret key -> map-register cannot be sent */
2488 // TODO: group mappings that share common key
2490 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, g,
2496 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2498 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2499 ip4_lookup_node.index : ip6_lookup_node.index;
2501 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2503 /* Enqueue the packet */
2504 to_next = vlib_frame_vector_args (f);
2507 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2508 map_registers_sent++;
2510 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2512 free_map_register_records (records);
2514 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2515 LISP_CP_OUTPUT_ERROR_MAP_REGISTERS_SENT,
2516 map_registers_sent);
2521 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
2522 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2524 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2525 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2528 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
2529 gid_address_t * seid, gid_address_t * deid,
2530 u8 is_smr_invoked, u8 is_resend)
2532 u32 next_index, bi = 0, *to_next, map_index;
2536 locator_set_t *loc_set;
2538 pending_map_request_t *pmr, *duplicate_pmr = 0;
2542 /* if there is already a pending request remember it */
2545 pool_foreach(pmr, lcm->pending_map_requests_pool,
2547 if (!gid_address_cmp (&pmr->src, seid)
2548 && !gid_address_cmp (&pmr->dst, deid))
2550 duplicate_pmr = pmr;
2556 if (!is_resend && duplicate_pmr)
2558 /* don't send the request if there is a pending map request already */
2562 /* get locator-set for seid */
2563 if (!lcm->lisp_pitr)
2565 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2566 if (map_index == ~0)
2568 clib_warning ("No local mapping found in eid-table for %U!",
2569 format_gid_address, seid);
2573 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2578 ("Mapping found for src eid %U is not marked as local!",
2579 format_gid_address, seid);
2582 ls_index = map->locator_set_index;
2586 map_index = lcm->pitr_map_index;
2587 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
2588 ls_index = map->locator_set_index;
2591 /* overwrite locator set if map-request itr-rlocs configured */
2592 if (~0 != lcm->mreq_itr_rlocs)
2594 ls_index = lcm->mreq_itr_rlocs;
2597 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
2599 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
2602 duplicate_pmr->to_be_removed = 1;
2606 /* build the encapsulated map request */
2607 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
2608 &lcm->active_map_resolver,
2609 &sloc, is_smr_invoked, &nonce, &bi);
2614 /* set fib index to default and lookup node */
2615 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2616 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2617 ip4_lookup_node.index : ip6_lookup_node.index;
2619 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2621 /* Enqueue the packet */
2622 to_next = vlib_frame_vector_args (f);
2625 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2628 /* if there is a pending request already update it */
2630 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
2632 /* remove the oldest nonce */
2633 u64 CLIB_UNUSED (tmp), *nonce_del;
2634 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
2635 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
2636 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
2639 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
2640 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2641 duplicate_pmr - lcm->pending_map_requests_pool);
2645 /* add map-request to pending requests table */
2646 pool_get (lcm->pending_map_requests_pool, pmr);
2647 memset (pmr, 0, sizeof (*pmr));
2648 gid_address_copy (&pmr->src, seid);
2649 gid_address_copy (&pmr->dst, deid);
2650 clib_fifo_add1 (pmr->nonces, nonce);
2651 pmr->is_smr_invoked = is_smr_invoked;
2652 reset_pending_mr_counters (pmr);
2653 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2654 pmr - lcm->pending_map_requests_pool);
2661 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
2663 ip4_header_t *ip4 = hdr;
2666 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2668 ip_address_set (src, &ip4->src_address, IP4);
2669 ip_address_set (dst, &ip4->dst_address, IP4);
2674 ip_address_set (src, &ip6->src_address, IP6);
2675 ip_address_set (dst, &ip6->dst_address, IP6);
2680 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2684 u32 vni = ~0, table_id = ~0;
2686 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
2687 IP4 ? FIB_PROTOCOL_IP4 :
2693 vnip = hash_get (lcm->vni_by_table_id, table_id);
2697 clib_warning ("vrf %d is not mapped to any vni!", table_id);
2703 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2709 l2input_main_t *l2im = &l2input_main;
2710 l2_input_config_t *config;
2711 l2_bridge_domain_t *bd_config;
2713 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
2714 config = vec_elt_at_index (l2im->configs, sw_if_index0);
2715 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
2717 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
2721 clib_warning ("bridge domain %d is not mapped to any vni!",
2728 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2729 gid_address_t * src, gid_address_t * dst,
2734 memset (src, 0, sizeof (*src));
2735 memset (dst, 0, sizeof (*dst));
2737 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
2740 u8 version, preflen;
2742 gid_address_type (src) = GID_ADDR_IP_PREFIX;
2743 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
2745 ip = vlib_buffer_get_current (b);
2746 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
2748 version = gid_address_ip_version (src);
2749 preflen = ip_address_max_len (version);
2750 gid_address_ippref_len (src) = preflen;
2751 gid_address_ippref_len (dst) = preflen;
2753 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
2754 gid_address_vni (dst) = vni;
2755 gid_address_vni (src) = vni;
2757 else if (LISP_AFI_MAC == type)
2759 ethernet_header_t *eh;
2761 eh = vlib_buffer_get_current (b);
2763 gid_address_type (src) = GID_ADDR_MAC;
2764 gid_address_type (dst) = GID_ADDR_MAC;
2765 mac_copy (&gid_address_mac (src), eh->src_address);
2766 mac_copy (&gid_address_mac (dst), eh->dst_address);
2769 vni = lisp_get_vni_from_buffer_eth (lcm, b);
2771 gid_address_vni (dst) = vni;
2772 gid_address_vni (src) = vni;
2774 else if (LISP_AFI_LCAF == type)
2776 /* Eventually extend this to support NSH and other */
2782 lisp_cp_lookup_inline (vlib_main_t * vm,
2783 vlib_node_runtime_t * node,
2784 vlib_frame_t * from_frame, int overlay)
2786 u32 *from, *to_next_drop, di, si;
2787 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2788 u32 pkts_mapped = 0;
2789 uword n_left_from, n_left_to_next_drop;
2791 from = vlib_frame_vector_args (from_frame);
2792 n_left_from = from_frame->n_vectors;
2794 while (n_left_from > 0)
2796 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2797 to_next_drop, n_left_to_next_drop);
2799 while (n_left_from > 0 && n_left_to_next_drop > 0)
2803 gid_address_t src, dst;
2808 to_next_drop[0] = pi0;
2810 n_left_to_next_drop -= 1;
2812 b0 = vlib_get_buffer (vm, pi0);
2813 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
2815 /* src/dst eid pair */
2816 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst, overlay);
2818 /* if we have remote mapping for destination already in map-chache
2819 add forwarding tunnel directly. If not send a map-request */
2820 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
2824 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
2825 /* send a map-request also in case of negative mapping entry
2826 with corresponding action */
2827 if (m->action == LISP_SEND_MAP_REQUEST)
2829 /* send map-request */
2830 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2831 0 /* is_resend */ );
2836 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
2840 dp_add_fwd_entry_from_mt (si, di);
2846 /* send map-request */
2847 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2848 0 /* is_resend */ );
2852 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2854 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
2857 memset (tr, 0, sizeof (*tr));
2858 gid_address_copy (&tr->dst_eid, &dst);
2859 ip_address_copy (&tr->map_resolver_ip,
2860 &lcm->active_map_resolver);
2862 gid_address_free (&dst);
2863 gid_address_free (&src);
2866 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2867 n_left_to_next_drop);
2869 vlib_node_increment_counter (vm, node->node_index,
2870 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
2872 return from_frame->n_vectors;
2876 lisp_cp_lookup_ip4 (vlib_main_t * vm,
2877 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2879 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
2883 lisp_cp_lookup_ip6 (vlib_main_t * vm,
2884 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2886 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
2890 lisp_cp_lookup_l2 (vlib_main_t * vm,
2891 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2893 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
2897 lisp_cp_lookup_nsh (vlib_main_t * vm,
2898 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2900 /* TODO decide if NSH should be propagated as LCAF or not */
2901 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_LCAF));
2905 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
2906 .function = lisp_cp_lookup_ip4,
2907 .name = "lisp-cp-lookup-ip4",
2908 .vector_size = sizeof (u32),
2909 .format_trace = format_lisp_cp_lookup_trace,
2910 .type = VLIB_NODE_TYPE_INTERNAL,
2912 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2913 .error_strings = lisp_cp_lookup_error_strings,
2915 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2918 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2924 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
2925 .function = lisp_cp_lookup_ip6,
2926 .name = "lisp-cp-lookup-ip6",
2927 .vector_size = sizeof (u32),
2928 .format_trace = format_lisp_cp_lookup_trace,
2929 .type = VLIB_NODE_TYPE_INTERNAL,
2931 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2932 .error_strings = lisp_cp_lookup_error_strings,
2934 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2937 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2943 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
2944 .function = lisp_cp_lookup_l2,
2945 .name = "lisp-cp-lookup-l2",
2946 .vector_size = sizeof (u32),
2947 .format_trace = format_lisp_cp_lookup_trace,
2948 .type = VLIB_NODE_TYPE_INTERNAL,
2950 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2951 .error_strings = lisp_cp_lookup_error_strings,
2953 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2956 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2962 VLIB_REGISTER_NODE (lisp_cp_lookup_nsh_node) = {
2963 .function = lisp_cp_lookup_nsh,
2964 .name = "lisp-cp-lookup-nsh",
2965 .vector_size = sizeof (u32),
2966 .format_trace = format_lisp_cp_lookup_trace,
2967 .type = VLIB_NODE_TYPE_INTERNAL,
2969 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2970 .error_strings = lisp_cp_lookup_error_strings,
2972 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2975 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2980 /* lisp_cp_input statistics */
2981 #define foreach_lisp_cp_input_error \
2983 _(RLOC_PROBE_REQ_RECEIVED, "rloc-probe requests received") \
2984 _(RLOC_PROBE_REP_RECEIVED, "rloc-probe replies received") \
2985 _(MAP_NOTIFIES_RECEIVED, "map-notifies received") \
2986 _(MAP_REPLIES_RECEIVED, "map-replies received")
2988 static char *lisp_cp_input_error_strings[] = {
2989 #define _(sym,string) string,
2990 foreach_lisp_cp_input_error
2996 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
2997 foreach_lisp_cp_input_error
2999 LISP_CP_INPUT_N_ERROR,
3000 } lisp_cp_input_error_t;
3004 gid_address_t dst_eid;
3005 ip4_address_t map_resolver_ip;
3006 } lisp_cp_input_trace_t;
3009 format_lisp_cp_input_trace (u8 * s, va_list * args)
3011 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3012 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3013 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
3014 va_arg (*args, lisp_cp_input_trace_t *);
3016 s = format (s, "LISP-CP-INPUT: TODO");
3021 remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
3024 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3025 memset (adj_args, 0, sizeof (adj_args[0]));
3027 m = pool_elt_at_index (lcm->mapping_pool, mi);
3029 gid_address_copy (&adj_args->reid, &m->eid);
3030 adj_args->is_add = 0;
3031 if (vnet_lisp_add_del_adjacency (adj_args))
3032 clib_warning ("failed to del adjacency!");
3034 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
3035 0 /* is_static */ , 0);
3036 mapping_delete_timer (lcm, mi);
3040 mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
3041 f64 expiration_time)
3044 u64 now = clib_cpu_time_now ();
3045 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
3046 u64 exp_clock_time = now + expiration_time * cpu_cps;
3048 m = pool_elt_at_index (lcm->mapping_pool, mi);
3051 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
3055 map_records_arg_free (map_records_arg_t * a)
3058 vec_foreach (m, a->mappings)
3060 vec_free (m->locators);
3061 gid_address_free (&m->eid);
3068 process_map_reply (map_records_arg_t * a)
3071 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3072 u32 dst_map_index = 0;
3073 pending_map_request_t *pmr;
3077 if (a->is_rloc_probe)
3080 /* Check pending requests table and nonce */
3081 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
3084 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
3087 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
3089 vec_foreach (m, a->mappings)
3091 /* insert/update mappings cache */
3092 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
3093 m->authoritative, m->ttl,
3094 1, 0 /* is_static */ , &dst_map_index);
3096 if (dst_map_index == (u32) ~ 0)
3099 /* try to program forwarding only if mapping saved or updated */
3100 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3101 memset (adj_args, 0, sizeof (adj_args[0]));
3103 gid_address_copy (&adj_args->leid, &pmr->src);
3104 gid_address_copy (&adj_args->reid, &m->eid);
3105 adj_args->is_add = 1;
3106 if (vnet_lisp_add_del_adjacency (adj_args))
3107 clib_warning ("failed to add adjacency!");
3109 if ((u32) ~ 0 != m->ttl)
3110 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
3113 /* remove pending map request entry */
3116 clib_fifo_foreach (noncep, pmr->nonces, ({
3117 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
3121 clib_fifo_free (pmr->nonces);
3122 pool_put (lcm->pending_map_requests_pool, pmr);
3125 map_records_arg_free (a);
3130 is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
3131 lisp_key_type_t key_id, u8 * key)
3137 auth_data_len = auth_data_len_by_key_id (key_id);
3138 if ((u16) ~ 0 == auth_data_len)
3140 clib_warning ("invalid length for key_id %d!", key_id);
3144 /* save auth data */
3145 vec_validate (auth_data, auth_data_len - 1);
3146 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
3148 /* clear auth data */
3149 memset (MNOTIFY_DATA (h), 0, auth_data_len);
3151 /* get hash of the message */
3152 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3153 (unsigned char *) h, msg_len, NULL, NULL);
3155 result = memcmp (code, auth_data, auth_data_len);
3157 vec_free (auth_data);
3163 process_map_notify (map_records_arg_t * a)
3165 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3168 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
3171 clib_warning ("No pending map-register entry with nonce %lu!",
3176 map_records_arg_free (a);
3177 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
3181 get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
3185 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
3188 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
3192 return pool_elt_at_index (lcm->mapping_pool, mi);
3196 * When map-notify is received it is necessary that all EIDs in the record
3197 * list share common key. The key is then used to verify authentication
3198 * data in map-notify message.
3201 map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
3202 u32 key_id, u8 ** key_out)
3204 u32 i, len = vec_len (maps);
3207 /* get key of the first mapping */
3208 m = get_mapping (lcm, &maps[0].eid);
3212 key_out[0] = m->key;
3214 for (i = 1; i < len; i++)
3216 m = get_mapping (lcm, &maps[i].eid);
3220 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
3222 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
3230 parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
3232 locator_t *locators = 0;
3238 /* parse record eid */
3239 for (i = 0; i < count; i++)
3241 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
3244 clib_warning ("Failed to parse mapping record!");
3245 vec_foreach (loc, locators) locator_free (loc);
3246 vec_free (locators);
3250 m.locators = locators;
3251 gid_address_copy (&m.eid, &deid);
3252 vec_add1 (a->mappings, m);
3258 static map_records_arg_t *
3259 parse_map_notify (vlib_buffer_t * b)
3262 map_notify_hdr_t *mnotif_hdr;
3263 lisp_key_type_t key_id;
3264 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3267 u16 auth_data_len = 0;
3269 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3271 memset (a, 0, sizeof (*a));
3272 mnotif_hdr = vlib_buffer_get_current (b);
3273 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
3274 memset (&deid, 0, sizeof (deid));
3276 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
3277 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
3278 auth_data_len = auth_data_len_by_key_id (key_id);
3280 /* advance buffer by authentication data */
3281 vlib_buffer_pull (b, auth_data_len);
3283 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
3284 rc = parse_map_records (b, a, record_count);
3287 map_records_arg_free (a);
3291 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
3294 map_records_arg_free (a);
3298 /* verify authentication data */
3299 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
3300 - (u8 *) mnotif_hdr, key_id, key))
3302 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
3304 map_records_arg_free (a);
3310 static vlib_buffer_t *
3311 build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
3312 ip_address_t * dst, u64 nonce, u8 probe_bit,
3313 mapping_t * records, u16 dst_port, u32 * bi_res)
3317 vlib_main_t *vm = lcm->vlib_main;
3319 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3321 clib_warning ("Can't allocate buffer for Map-Register!");
3325 b = vlib_get_buffer (vm, bi);
3327 /* leave some space for the encap headers */
3328 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3330 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
3332 /* push outer ip header */
3333 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
3340 send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
3341 u8 probe_bit, u64 nonce, u16 dst_port,
3342 ip_address_t * probed_loc)
3348 u32 next_index, *to_next;
3349 mapping_t *records = 0, *m;
3351 m = pool_elt_at_index (lcm->mapping_pool, mi);
3355 vec_add1 (records, m[0]);
3356 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
3357 memset (&src, 0, sizeof (src));
3359 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
3361 clib_warning ("can't find inteface address for %U", format_ip_address,
3366 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
3370 free_map_register_records (records);
3372 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3373 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3374 ip4_lookup_node.index : ip6_lookup_node.index;
3376 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3378 /* Enqueue the packet */
3379 to_next = vlib_frame_vector_args (f);
3382 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3387 find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
3389 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
3390 if (start < 0 && start < -sizeof (b->pre_data))
3396 *ip_hdr = b->data + start;
3397 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
3402 process_map_request (vlib_main_t * vm, vlib_node_runtime_t * node,
3403 lisp_cp_main_t * lcm, vlib_buffer_t * b)
3406 ip_address_t *dst_loc = 0, probed_loc, src_loc;
3408 map_request_hdr_t *mreq_hdr;
3409 gid_address_t src, dst;
3411 u32 i, len = 0, rloc_probe_recv = 0;
3412 gid_address_t *itr_rlocs = 0;
3414 mreq_hdr = vlib_buffer_get_current (b);
3415 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
3418 ("Only SMR Map-Requests and RLOC probe supported for now!");
3422 vlib_buffer_pull (b, sizeof (*mreq_hdr));
3423 nonce = MREQ_NONCE (mreq_hdr);
3426 len = lisp_msg_parse_addr (b, &src);
3430 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3431 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
3435 /* parse eid records and send SMR-invoked map-requests */
3436 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
3438 memset (&dst, 0, sizeof (dst));
3439 len = lisp_msg_parse_eid_rec (b, &dst);
3442 clib_warning ("Can't parse map-request EID-record");
3446 if (MREQ_SMR (mreq_hdr))
3448 /* send SMR-invoked map-requests */
3449 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3451 else if (MREQ_RLOC_PROBE (mreq_hdr))
3453 find_ip_header (b, &ip_hdr);
3456 clib_warning ("Cannot find the IP header!");
3460 memset (&m, 0, sizeof (m));
3461 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3463 // TODO: select best locator; for now use the first one
3464 dst_loc = &gid_address_ip (&itr_rlocs[0]);
3466 /* get src/dst IP addresses */
3467 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
3469 // TODO get source port from buffer
3470 u16 src_port = LISP_CONTROL_PORT;
3472 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
3473 src_port, &probed_loc);
3478 vlib_node_increment_counter (vm, node->node_index,
3479 LISP_CP_INPUT_ERROR_RLOC_PROBE_REQ_RECEIVED,
3481 vec_free (itr_rlocs);
3484 static map_records_arg_t *
3485 parse_map_reply (vlib_buffer_t * b)
3492 map_reply_hdr_t *mrep_hdr;
3493 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3494 memset (a, 0, sizeof (*a));
3495 locator_t *locators;
3497 mrep_hdr = vlib_buffer_get_current (b);
3498 a->nonce = MREP_NONCE (mrep_hdr);
3499 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
3500 vlib_buffer_pull (b, sizeof (*mrep_hdr));
3502 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3504 memset (&m, 0, sizeof (m));
3506 h = vlib_buffer_get_current (b);
3508 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3509 m.action = MAP_REC_ACTION (h);
3510 m.authoritative = MAP_REC_AUTH (h);
3512 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3515 clib_warning ("Failed to parse mapping record!");
3516 map_records_arg_free (a);
3520 m.locators = locators;
3521 gid_address_copy (&m.eid, &deid);
3522 vec_add1 (a->mappings, m);
3528 queue_map_reply_for_processing (map_records_arg_t * a)
3530 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
3534 queue_map_notify_for_processing (map_records_arg_t * a)
3536 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
3540 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
3541 vlib_frame_t * from_frame)
3543 u32 n_left_from, *from, *to_next_drop, rloc_probe_rep_recv = 0,
3544 map_notifies_recv = 0;
3545 lisp_msg_type_e type;
3546 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3547 map_records_arg_t *a;
3549 from = vlib_frame_vector_args (from_frame);
3550 n_left_from = from_frame->n_vectors;
3553 while (n_left_from > 0)
3555 u32 n_left_to_next_drop;
3557 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3558 to_next_drop, n_left_to_next_drop);
3559 while (n_left_from > 0 && n_left_to_next_drop > 0)
3567 to_next_drop[0] = bi0;
3569 n_left_to_next_drop -= 1;
3571 b0 = vlib_get_buffer (vm, bi0);
3573 type = lisp_msg_type (vlib_buffer_get_current (b0));
3576 case LISP_MAP_REPLY:
3577 a = parse_map_reply (b0);
3580 if (a->is_rloc_probe)
3581 rloc_probe_rep_recv++;
3582 queue_map_reply_for_processing (a);
3585 case LISP_MAP_REQUEST:
3586 process_map_request (vm, node, lcm, b0);
3588 case LISP_MAP_NOTIFY:
3589 a = parse_map_notify (b0);
3592 map_notifies_recv++;
3593 queue_map_notify_for_processing (a);
3597 clib_warning ("Unsupported LISP message type %d", type);
3601 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
3603 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3609 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3610 n_left_to_next_drop);
3612 vlib_node_increment_counter (vm, node->node_index,
3613 LISP_CP_INPUT_ERROR_RLOC_PROBE_REP_RECEIVED,
3614 rloc_probe_rep_recv);
3615 vlib_node_increment_counter (vm, node->node_index,
3616 LISP_CP_INPUT_ERROR_MAP_NOTIFIES_RECEIVED,
3618 return from_frame->n_vectors;
3622 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3623 .function = lisp_cp_input,
3624 .name = "lisp-cp-input",
3625 .vector_size = sizeof (u32),
3626 .format_trace = format_lisp_cp_input_trace,
3627 .type = VLIB_NODE_TYPE_INTERNAL,
3629 .n_errors = LISP_CP_INPUT_N_ERROR,
3630 .error_strings = lisp_cp_input_error_strings,
3632 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3635 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3641 lisp_cp_init (vlib_main_t * vm)
3643 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3644 clib_error_t *error = 0;
3646 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3649 lcm->im4 = &ip4_main;
3650 lcm->im6 = &ip6_main;
3651 lcm->vlib_main = vm;
3652 lcm->vnet_main = vnet_get_main ();
3653 lcm->mreq_itr_rlocs = ~0;
3656 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
3658 gid_dictionary_init (&lcm->mapping_index_by_gid);
3659 lcm->do_map_resolver_election = 1;
3660 lcm->map_request_mode = MR_MODE_DST_ONLY;
3662 /* default vrf mapped to vni 0 */
3663 hash_set (lcm->table_id_by_vni, 0, 0);
3664 hash_set (lcm->vni_by_table_id, 0, 0);
3666 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
3667 lisp_cp_input_node.index, 1 /* is_ip4 */ );
3668 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
3669 lisp_cp_input_node.index, 0 /* is_ip4 */ );
3671 u64 now = clib_cpu_time_now ();
3672 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
3677 lisp_stats_api_fill (lisp_cp_main_t * lcm, lisp_gpe_main_t * lgm,
3678 lisp_api_stats_t * stat, lisp_stats_key_t * key,
3682 vlib_combined_counter_main_t *cm = &lgm->counters;
3683 lisp_gpe_fwd_entry_key_t fwd_key;
3684 const lisp_gpe_tunnel_t *lgt;
3687 memset (stat, 0, sizeof (*stat));
3688 memset (&fwd_key, 0, sizeof (fwd_key));
3690 fe = pool_elt_at_index (lcm->fwd_entry_pool, key->fwd_entry_index);
3693 gid_to_dp_address (&fe->reid, &stat->deid);
3694 gid_to_dp_address (&fe->leid, &stat->seid);
3695 stat->vni = gid_address_vni (&fe->reid);
3697 lgt = lisp_gpe_tunnel_get (key->tunnel_index);
3698 stat->loc_rloc = lgt->key->lcl;
3699 stat->rmt_rloc = lgt->key->rmt;
3701 vlib_get_combined_counter (cm, stats_index, &v);
3707 vnet_lisp_get_stats (void)
3709 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
3710 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3711 lisp_api_stats_t *stats = 0, stat;
3712 lisp_stats_key_t *key;
3716 hash_foreach_mem (key, index, lgm->lisp_stats_index_by_key,
3718 if (lisp_stats_api_fill (lcm, lgm, &stat, key, index))
3719 vec_add1 (stats, stat);
3727 send_map_request_thread_fn (void *arg)
3729 map_request_args_t *a = arg;
3730 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3733 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3735 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3741 queue_map_request (gid_address_t * seid, gid_address_t * deid,
3742 u8 smr_invoked, u8 is_resend)
3744 map_request_args_t a;
3746 a.is_resend = is_resend;
3747 gid_address_copy (&a.seid, seid);
3748 gid_address_copy (&a.deid, deid);
3749 a.smr_invoked = smr_invoked;
3751 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
3752 (u8 *) & a, sizeof (a));
3757 * Take an action with a pending map request depending on expiration time
3758 * and re-try counters.
3761 update_pending_request (pending_map_request_t * r, f64 dt)
3763 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3766 if (r->time_to_expire - dt < 0)
3767 /* it's time to decide what to do with this pending request */
3769 if (r->retries_num >= NUMBER_OF_RETRIES)
3770 /* too many retries -> assume current map resolver is not available */
3772 mr = get_map_resolver (&lcm->active_map_resolver);
3775 clib_warning ("Map resolver %U not found - probably deleted "
3776 "by the user recently.", format_ip_address,
3777 &lcm->active_map_resolver);
3781 clib_warning ("map resolver %U is unreachable, ignoring",
3782 format_ip_address, &lcm->active_map_resolver);
3784 /* mark current map resolver unavailable so it won't be
3785 * selected next time */
3787 mr->last_update = vlib_time_now (lcm->vlib_main);
3790 reset_pending_mr_counters (r);
3791 elect_map_resolver (lcm);
3793 /* try to find a next eligible map resolver and re-send */
3794 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3800 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3803 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3807 r->time_to_expire -= dt;
3811 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
3814 pending_map_request_t *pmr;
3815 u32 *to_be_removed = 0, *pmr_index;
3818 pool_foreach (pmr, lcm->pending_map_requests_pool,
3820 if (pmr->to_be_removed)
3822 clib_fifo_foreach (nonce, pmr->nonces, ({
3823 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
3826 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
3831 vec_foreach (pmr_index, to_be_removed)
3832 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
3834 vec_free (to_be_removed);
3838 update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
3840 static f64 time_left = RLOC_PROBING_INTERVAL;
3842 if (!lcm->is_enabled || !lcm->rloc_probing)
3848 time_left = RLOC_PROBING_INTERVAL;
3849 send_rloc_probes (lcm);
3854 update_map_register (lisp_cp_main_t * lcm, f64 dt)
3856 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
3857 static u64 mreg_sent_counter = 0;
3859 if (!lcm->is_enabled || !lcm->map_registering)
3865 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
3866 time_left = MAP_REGISTER_INTERVAL;
3869 mreg_sent_counter++;
3870 time_left = QUICK_MAP_REGISTER_INTERVAL;
3872 send_map_register (lcm, 1 /* want map notify */ );
3877 send_map_resolver_service (vlib_main_t * vm,
3878 vlib_node_runtime_t * rt, vlib_frame_t * f)
3882 pending_map_request_t *pmr;
3883 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3887 vlib_process_wait_for_event_or_clock (vm, period);
3889 /* currently no signals are expected - just wait for clock */
3890 (void) vlib_process_get_events (vm, 0);
3893 pool_foreach (pmr, lcm->pending_map_requests_pool,
3895 if (!pmr->to_be_removed)
3896 update_pending_request (pmr, period);
3900 remove_dead_pending_map_requests (lcm);
3902 update_map_register (lcm, period);
3903 update_rloc_probing (lcm, period);
3905 u64 now = clib_cpu_time_now ();
3907 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
3908 if (vec_len (expired) > 0)
3911 vec_foreach (mi, expired)
3913 remove_expired_mapping (lcm, mi[0]);
3915 _vec_len (expired) = 0;
3924 vnet_lisp_stats_enable_disable (u8 enable)
3926 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3928 if (vnet_lisp_enable_disable_status () == 0)
3929 return VNET_API_ERROR_LISP_DISABLED;
3932 lcm->flags |= LISP_FLAG_STATS_ENABLED;
3934 lcm->flags &= ~LISP_FLAG_STATS_ENABLED;
3940 vnet_lisp_stats_enable_disable_state (void)
3942 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3944 if (vnet_lisp_enable_disable_status () == 0)
3945 return VNET_API_ERROR_LISP_DISABLED;
3947 return lcm->flags & LISP_FLAG_STATS_ENABLED;
3951 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
3952 .function = send_map_resolver_service,
3953 .type = VLIB_NODE_TYPE_PROCESS,
3954 .name = "lisp-retry-service",
3955 .process_log2_n_stack_bytes = 16,
3959 VLIB_INIT_FUNCTION (lisp_cp_init);
3962 * fd.io coding-style-patch-verification: ON
3965 * eval: (c-set-style "gnu")