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;
48 vnet_lisp_get_map_request_mode (void)
50 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
51 return lcm->map_request_mode;
55 auth_data_len_by_key_id (lisp_key_type_t key_id)
60 return SHA1_AUTH_DATA_LEN;
61 case HMAC_SHA_256_128:
62 return SHA256_AUTH_DATA_LEN;
64 clib_warning ("unsupported key type: %d!", key_id);
71 get_encrypt_fcn (lisp_key_type_t key_id)
77 case HMAC_SHA_256_128:
80 clib_warning ("unsupported encryption key type: %d!", key_id);
87 queue_map_request (gid_address_t * seid, gid_address_t * deid,
88 u8 smr_invoked, u8 is_resend);
90 ip_interface_address_t *
91 ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
92 u32 sw_if_index, u8 loop)
94 vnet_main_t *vnm = vnet_get_main ();
95 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
96 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
97 sw_if_index = swif->unnumbered_sw_if_index;
99 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
100 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
102 return pool_elt_at_index ((lm)->if_address_pool, ia);
106 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
109 ip_interface_address_t *ia;
111 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
114 return ip_interface_address_get_address (lm, ia);
118 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
119 u8 version, ip_address_t * result)
121 ip_lookup_main_t *lm;
124 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
125 addr = ip_interface_get_first_address (lm, sw_if_index, version);
129 ip_address_set (result, addr, version);
134 * convert from a LISP address to a FIB prefix
137 ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
139 if (addr->version == IP4)
142 prefix->fp_proto = FIB_PROTOCOL_IP4;
143 memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
144 memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
148 prefix->fp_len = 128;
149 prefix->fp_proto = FIB_PROTOCOL_IP6;
150 memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
155 * convert from a LISP to a FIB prefix
158 ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
159 fib_prefix_t * fib_prefix)
161 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
162 fib_prefix->fp_len = ip_prefix->len;
166 * Find the sw_if_index of the interface that would be used to egress towards
170 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
172 fib_node_index_t fei;
175 ip_address_to_fib_prefix (dst, &prefix);
177 fei = fib_table_lookup (0, &prefix);
179 return (fib_entry_get_resolving_interface (fei));
183 * Find first IP of the interface that would be used to egress towards dst.
184 * Returns 1 if the address is found 0 otherwise.
187 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
188 ip_address_t * result)
191 ip_lookup_main_t *lm;
195 ASSERT (result != 0);
197 ipver = ip_addr_version (dst);
199 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
200 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
205 /* find the first ip address */
206 addr = ip_interface_get_first_address (lm, si, ipver);
210 ip_address_set (result, addr, ipver);
215 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
221 dp_table = hash_get (lcm->table_id_by_vni, vni);
225 clib_warning ("vni %d not associated to a vrf!", vni);
226 return VNET_API_ERROR_INVALID_VALUE;
231 dp_table = hash_get (lcm->bd_id_by_vni, vni);
234 clib_warning ("vni %d not associated to a bridge domain!", vni);
235 return VNET_API_ERROR_INVALID_VALUE;
239 /* enable/disable data-plane interface */
243 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
245 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
250 lisp_gpe_tenant_l2_iface_unlock (vni);
252 lisp_gpe_tenant_l3_iface_unlock (vni);
259 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
261 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
264 memset (a, 0, sizeof (*a));
266 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
270 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
272 /* delete dp fwd entry */
275 a->locator_pairs = fe->locator_pairs;
276 a->vni = gid_address_vni (&fe->reid);
277 gid_address_copy (&a->rmt_eid, &fe->reid);
279 gid_address_copy (&a->lcl_eid, &fe->leid);
281 vnet_lisp_gpe_del_fwd_counters (a, feip[0]);
282 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
284 /* delete entry in fwd table */
285 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
286 vec_free (fe->locator_pairs);
287 pool_put (lcm->fwd_entry_pool, fe);
291 * Finds first remote locator with best (lowest) priority that has a local
292 * peer locator with an underlying route to it.
296 get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
297 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
299 u32 i, limitp = 0, li, found = 0, esi;
300 locator_set_t *rmt_ls, *lcl_ls;
301 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
302 locator_t *lp, *rmt = 0;
307 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
309 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
311 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
318 /* find unvisited remote locator with best priority */
319 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
321 if (0 != hash_get (checked, i))
324 li = vec_elt (rmt_ls->locator_indices, i);
325 lp = pool_elt_at_index (lcm->locator_pool, li);
327 /* we don't support non-IP locators for now */
328 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
331 if ((found && lp->priority == limitp)
332 || (!found && lp->priority >= limitp))
336 /* don't search for locators with lower priority and don't
337 * check this locator again*/
338 limitp = lp->priority;
339 hash_set (checked, i, 1);
343 /* check if a local locator with a route to remote locator exists */
346 /* find egress sw_if_index for rmt locator */
348 ip_fib_get_egress_iface_for_dst (lcm,
349 &gid_address_ip (&rmt->address));
350 if ((u32) ~ 0 == esi)
353 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
355 li = vec_elt (lcl_ls->locator_indices, i);
356 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
358 /* found local locator with the needed sw_if_index */
359 if (sl->sw_if_index == esi)
361 /* and it has an address */
362 if (0 == ip_interface_get_first_ip_address (lcm,
364 gid_address_ip_version
369 memset (&pair, 0, sizeof (pair));
370 ip_address_copy (&pair.rmt_loc,
371 &gid_address_ip (&rmt->address));
372 ip_address_copy (&pair.lcl_loc, lcl_addr);
373 pair.weight = rmt->weight;
374 pair.priority = rmt->priority;
375 vec_add1 (locator_pairs[0], pair);
389 gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
392 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
396 switch (fid_addr_type (fid))
398 case FID_ADDR_IP_PREF:
399 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
400 gid_address_ippref (dst) = fid_addr_ippref (fid);
403 gid_address_type (dst) = GID_ADDR_MAC;
404 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
407 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
413 vnet_lisp_map_register_state_get (void)
415 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
416 return lcm->map_registering;
420 vnet_lisp_rloc_probe_state_get (void)
422 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
423 return lcm->rloc_probing;
427 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
429 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
430 gid_address_t *rmt_eid, *lcl_eid;
431 mapping_t *lcl_map, *rmt_map;
433 uword *feip = 0, *dpid;
435 u8 type, is_src_dst = 0;
438 memset (a, 0, sizeof (*a));
440 /* remove entry if it already exists */
441 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
443 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
446 * Determine local mapping and eid
449 lcl_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
451 lcl_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
452 lcl_eid = &lcl_map->eid;
455 * Determine remote mapping and eid
457 rmt_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
458 rmt_eid = &rmt_map->eid;
461 * Build and insert data plane forwarding entry
465 if (MR_MODE_SRC_DST == lcm->map_request_mode)
467 if (GID_ADDR_SRC_DST == gid_address_type (rmt_eid))
469 gid_address_sd_to_flat (&a->rmt_eid, rmt_eid,
470 &gid_address_sd_dst (rmt_eid));
471 gid_address_sd_to_flat (&a->lcl_eid, rmt_eid,
472 &gid_address_sd_src (rmt_eid));
476 gid_address_copy (&a->rmt_eid, rmt_eid);
477 gid_address_copy (&a->lcl_eid, lcl_eid);
482 gid_address_copy (&a->rmt_eid, rmt_eid);
484 a->vni = gid_address_vni (&a->rmt_eid);
485 a->is_src_dst = is_src_dst;
487 /* get vrf or bd_index associated to vni */
488 type = gid_address_type (&a->rmt_eid);
489 if (GID_ADDR_IP_PREFIX == type)
491 dpid = hash_get (lcm->table_id_by_vni, a->vni);
494 clib_warning ("vni %d not associated to a vrf!", a->vni);
497 a->table_id = dpid[0];
499 else if (GID_ADDR_MAC == type)
501 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
504 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
510 /* find best locator pair that 1) verifies LISP policy 2) are connected */
511 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
513 /* Either rmt mapping is negative or we can't find underlay path.
514 * Try again with petr if configured */
515 if (rv == 0 && (lcm->flags & LISP_FLAG_USE_PETR))
517 rmt_map = lisp_get_petr_mapping (lcm);
518 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
525 a->action = rmt_map->action;
528 rv = vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
531 if (a->locator_pairs)
532 vec_free (a->locator_pairs);
536 /* add tunnel to fwd entry table */
537 pool_get (lcm->fwd_entry_pool, fe);
538 vnet_lisp_gpe_add_fwd_counters (a, fe - lcm->fwd_entry_pool);
540 fe->locator_pairs = a->locator_pairs;
541 gid_address_copy (&fe->reid, &a->rmt_eid);
544 gid_address_copy (&fe->leid, &a->lcl_eid);
546 gid_address_copy (&fe->leid, lcl_eid);
548 fe->is_src_dst = is_src_dst;
549 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
550 fe - lcm->fwd_entry_pool);
557 } fwd_entry_mt_arg_t;
560 dp_add_fwd_entry_thread_fn (void *arg)
562 fwd_entry_mt_arg_t *a = arg;
563 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
564 dp_add_fwd_entry (lcm, a->si, a->di);
569 dp_add_fwd_entry_from_mt (u32 si, u32 di)
571 fwd_entry_mt_arg_t a;
573 memset (&a, 0, sizeof (a));
577 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
578 (u8 *) & a, sizeof (a));
583 * Returns vector of adjacencies.
585 * The caller must free the vector returned by this function.
587 * @param vni virtual network identifier
588 * @return vector of adjacencies
591 vnet_lisp_adjacencies_get_by_vni (u32 vni)
593 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
595 lisp_adjacency_t *adjs = 0, adj;
598 pool_foreach(fwd, lcm->fwd_entry_pool,
600 if (gid_address_vni (&fwd->reid) != vni)
603 gid_address_copy (&adj.reid, &fwd->reid);
604 gid_address_copy (&adj.leid, &fwd->leid);
605 vec_add1 (adjs, adj);
613 get_map_server (ip_address_t * a)
615 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
618 vec_foreach (m, lcm->map_servers)
620 if (!ip_address_cmp (&m->address, a))
629 get_map_resolver (ip_address_t * a)
631 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
634 vec_foreach (m, lcm->map_resolvers)
636 if (!ip_address_cmp (&m->address, a))
645 vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
648 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
649 lisp_msmr_t _ms, *ms = &_ms;
651 if (vnet_lisp_enable_disable_status () == 0)
653 clib_warning ("LISP is disabled!");
654 return VNET_API_ERROR_LISP_DISABLED;
659 if (get_map_server (addr))
661 clib_warning ("map-server %U already exists!", format_ip_address,
666 memset (ms, 0, sizeof (*ms));
667 ip_address_copy (&ms->address, addr);
668 vec_add1 (lcm->map_servers, ms[0]);
672 for (i = 0; i < vec_len (lcm->map_servers); i++)
674 ms = vec_elt_at_index (lcm->map_servers, i);
675 if (!ip_address_cmp (&ms->address, addr))
677 vec_del1 (lcm->map_servers, i);
687 * Add/remove mapping to/from map-cache. Overwriting not allowed.
690 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
691 u32 * map_index_result)
693 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
694 u32 mi, *map_indexp, map_index, i;
695 mapping_t *m, *old_map;
698 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
699 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
702 /* TODO check if overwriting and take appropriate actions */
703 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
705 clib_warning ("eid %U found in the eid-table", format_gid_address,
707 return VNET_API_ERROR_VALUE_EXIST;
710 pool_get (lcm->mapping_pool, m);
711 gid_address_copy (&m->eid, &a->eid);
712 m->locator_set_index = a->locator_set_index;
714 m->action = a->action;
716 m->is_static = a->is_static;
717 m->key = vec_dup (a->key);
718 m->key_id = a->key_id;
720 map_index = m - lcm->mapping_pool;
721 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
724 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
726 clib_warning ("Locator set with index %d doesn't exist",
727 a->locator_set_index);
728 return VNET_API_ERROR_INVALID_VALUE;
731 /* add eid to list of eids supported by locator-set */
732 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
733 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
734 a->locator_set_index);
735 vec_add1 (eid_indexes[0], map_index);
740 vec_add1 (lcm->local_mappings_indexes, map_index);
742 map_index_result[0] = map_index;
746 if (mi == GID_LOOKUP_MISS)
748 clib_warning ("eid %U not found in the eid-table",
749 format_gid_address, &a->eid);
750 return VNET_API_ERROR_INVALID_VALUE;
753 /* clear locator-set to eids binding */
754 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
755 a->locator_set_index);
756 for (i = 0; i < vec_len (eid_indexes[0]); i++)
758 map_indexp = vec_elt_at_index (eid_indexes[0], i);
759 if (map_indexp[0] == mi)
762 vec_del1 (eid_indexes[0], i);
764 /* remove local mark if needed */
765 m = pool_elt_at_index (lcm->mapping_pool, mi);
769 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
771 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
772 if (lm_indexp[0] == mi)
775 vec_del1 (lcm->local_mappings_indexes, k);
778 /* remove mapping from dictionary */
779 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
780 gid_address_free (&m->eid);
781 pool_put_index (lcm->mapping_pool, mi);
788 * Add/update/delete mapping to/in/from map-cache.
791 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
792 u32 * map_index_result)
798 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
800 if (vnet_lisp_enable_disable_status () == 0)
802 clib_warning ("LISP is disabled!");
803 return VNET_API_ERROR_LISP_DISABLED;
806 vni = gid_address_vni (&a->eid);
807 type = gid_address_type (&a->eid);
808 if (GID_ADDR_IP_PREFIX == type)
809 dp_table = hash_get (lcm->table_id_by_vni, vni);
810 else if (GID_ADDR_MAC == type)
811 dp_table = hash_get (lcm->bd_id_by_vni, vni);
815 clib_warning ("vni %d not associated to a %s!", vni,
816 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
817 return VNET_API_ERROR_INVALID_VALUE;
820 /* store/remove mapping from map-cache */
821 return vnet_lisp_map_cache_add_del (a, map_index_result);
825 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
827 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
828 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
830 if (vnet_lisp_enable_disable_status () == 0)
832 clib_warning ("LISP is disabled!");
836 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
837 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
839 if (!is_l2 && (vni == 0 || dp_id == 0))
841 clib_warning ("can't add/del default vni-vrf mapping!");
845 dp_idp = hash_get (dp_table_by_vni[0], vni);
846 vnip = hash_get (vni_by_dp_table[0], dp_id);
852 clib_warning ("vni %d or vrf %d already used in vrf/vni "
853 "mapping!", vni, dp_id);
856 hash_set (dp_table_by_vni[0], vni, dp_id);
857 hash_set (vni_by_dp_table[0], dp_id, vni);
859 /* create dp iface */
860 dp_add_del_iface (lcm, vni, is_l2, 1);
864 if (!dp_idp || !vnip)
866 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
867 "mapping!", vni, dp_id);
870 /* remove dp iface */
871 dp_add_del_iface (lcm, vni, is_l2, 0);
873 hash_unset (dp_table_by_vni[0], vni);
874 hash_unset (vni_by_dp_table[0], dp_id);
880 /* return 0 if the two locator sets are identical 1 otherwise */
882 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
883 locator_t * new_locators)
886 locator_t *old_loc, *new_loc;
888 if (vec_len (old_ls_indexes) != vec_len (new_locators))
891 for (i = 0; i < vec_len (new_locators); i++)
893 old_li = vec_elt (old_ls_indexes, i);
894 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
896 new_loc = vec_elt_at_index (new_locators, i);
898 if (locator_cmp (old_loc, new_loc))
908 gid_address_t *eids_to_be_deleted;
909 } remove_mapping_args_t;
912 * Callback invoked when a sub-prefix is found
915 remove_mapping_if_needed (u32 mi, void *arg)
918 remove_mapping_args_t *a = arg;
919 lisp_cp_main_t *lcm = a->lcm;
923 m = pool_elt_at_index (lcm->mapping_pool, mi);
927 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
931 if (0 != vec_len (ls->locator_indices))
936 if (0 == vec_len (ls->locator_indices))
941 vec_add1 (a->eids_to_be_deleted, m->eid);
945 * This function searches map cache and looks for IP prefixes that are subset
946 * of the provided one. If such prefix is found depending on 'is_negative'
949 * 1) if is_negative is true and found prefix points to positive mapping,
950 * then the mapping is removed
951 * 2) if is_negative is false and found prefix points to negative mapping,
952 * then the mapping is removed
955 remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
959 remove_mapping_args_t a;
961 memset (&a, 0, sizeof (a));
963 /* do this only in src/dst mode ... */
964 if (MR_MODE_SRC_DST != lcm->map_request_mode)
967 /* ... and only for IP prefix */
968 if (GID_ADDR_SRC_DST != gid_address_type (eid)
969 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
972 a.is_negative = is_negative;
975 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
976 remove_mapping_if_needed, &a);
978 vec_foreach (e, a.eids_to_be_deleted)
980 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
982 memset (adj_args, 0, sizeof (adj_args[0]));
983 gid_address_copy (&adj_args->reid, e);
984 adj_args->is_add = 0;
985 if (vnet_lisp_add_del_adjacency (adj_args))
986 clib_warning ("failed to del adjacency!");
988 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
991 vec_free (a.eids_to_be_deleted);
995 mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
997 timing_wheel_delete (&lcm->wheel, mi);
1001 is_local_ip (lisp_cp_main_t * lcm, ip_address_t * addr)
1003 fib_node_index_t fei;
1004 fib_prefix_t prefix;
1005 fib_entry_flag_t flags;
1007 ip_address_to_fib_prefix (addr, &prefix);
1009 fei = fib_table_lookup (0, &prefix);
1010 flags = fib_entry_get_flags (fei);
1011 return (FIB_ENTRY_FLAG_LOCAL & flags);
1015 * Adds/removes/updates mapping. Does not program forwarding.
1017 * @param eid end-host identifier
1018 * @param rlocs vector of remote locators
1019 * @param action action for negative map-reply
1020 * @param is_add add mapping if non-zero, delete otherwise
1021 * @param res_map_index the map-index that was created/updated/removed. It is
1022 * set to ~0 if no action is taken.
1023 * @param is_static used for distinguishing between statically learned
1024 remote mappings and mappings obtained from MR
1025 * @return return code
1028 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
1029 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
1030 u32 * res_map_index)
1032 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1033 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1034 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1035 u32 mi, ls_index = 0, dst_map_index;
1039 if (vnet_lisp_enable_disable_status () == 0)
1041 clib_warning ("LISP is disabled!");
1042 return VNET_API_ERROR_LISP_DISABLED;
1046 res_map_index[0] = ~0;
1048 memset (m_args, 0, sizeof (m_args[0]));
1049 memset (ls_args, 0, sizeof (ls_args[0]));
1051 ls_args->locators = rlocs;
1053 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
1054 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1058 /* check if none of the locators match localy configured address */
1059 vec_foreach (loc, rlocs)
1061 ip_prefix_t *p = &gid_address_ippref (&loc->address);
1062 if (is_local_ip (lcm, &ip_prefix_addr (p)))
1064 clib_warning ("RLOC %U matches a local address!",
1065 format_gid_address, &loc->address);
1066 return VNET_API_ERROR_LISP_RLOC_LOCAL;
1070 /* overwrite: if mapping already exists, decide if locators should be
1071 * updated and be done */
1072 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
1074 if (!is_static && (old_map->is_static || old_map->local))
1076 /* do not overwrite local or static remote mappings */
1077 clib_warning ("mapping %U rejected due to collision with local "
1078 "or static remote mapping!", format_gid_address,
1083 locator_set_t *old_ls;
1085 /* update mapping attributes */
1086 old_map->action = action;
1087 old_map->authoritative = authoritative;
1090 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1091 old_map->locator_set_index);
1092 if (compare_locators (lcm, old_ls->locator_indices,
1095 /* set locator-set index to overwrite */
1096 ls_args->is_add = 1;
1097 ls_args->index = old_map->locator_set_index;
1098 vnet_lisp_add_del_locator_set (ls_args, 0);
1100 res_map_index[0] = mi;
1106 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1108 ls_args->is_add = 1;
1109 ls_args->index = ~0;
1111 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1114 gid_address_copy (&m_args->eid, eid);
1116 m_args->action = action;
1117 m_args->locator_set_index = ls_index;
1118 m_args->is_static = is_static;
1120 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
1123 res_map_index[0] = dst_map_index;
1128 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
1130 clib_warning ("cannot delete mapping for eid %U",
1131 format_gid_address, eid);
1136 gid_address_copy (&m_args->eid, eid);
1137 m_args->locator_set_index = old_map->locator_set_index;
1139 /* delete mapping associated from map-cache */
1140 vnet_lisp_map_cache_add_del (m_args, 0);
1142 ls_args->is_add = 0;
1143 ls_args->index = old_map->locator_set_index;
1144 /* delete locator set */
1145 vnet_lisp_add_del_locator_set (ls_args, 0);
1147 /* delete timer associated to the mapping if any */
1148 if (old_map->timer_set)
1149 mapping_delete_timer (lcm, mi);
1151 /* return old mapping index */
1153 res_map_index[0] = mi;
1161 vnet_lisp_clear_all_remote_adjacencies (void)
1164 u32 mi, *map_indices = 0, *map_indexp;
1165 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1166 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1167 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1170 pool_foreach_index (mi, lcm->mapping_pool,
1172 vec_add1 (map_indices, mi);
1176 vec_foreach (map_indexp, map_indices)
1178 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1181 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
1183 dm_args->is_add = 0;
1184 gid_address_copy (&dm_args->eid, &map->eid);
1185 dm_args->locator_set_index = map->locator_set_index;
1187 /* delete mapping associated to fwd entry */
1188 vnet_lisp_map_cache_add_del (dm_args, 0);
1192 ls->index = map->locator_set_index;
1193 /* delete locator set */
1194 rv = vnet_lisp_add_del_locator_set (ls, 0);
1202 vec_free (map_indices);
1207 * Adds adjacency or removes forwarding entry associated to remote mapping.
1208 * Note that adjacencies are not stored, they only result in forwarding entries
1212 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1214 lisp_cp_main_t *lcm = &lisp_control_main;
1215 u32 local_mi, remote_mi = ~0;
1217 if (vnet_lisp_enable_disable_status () == 0)
1219 clib_warning ("LISP is disabled!");
1220 return VNET_API_ERROR_LISP_DISABLED;
1223 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1224 &a->reid, &a->leid);
1225 if (GID_LOOKUP_MISS == remote_mi)
1227 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1228 format_gid_address, &a->reid);
1235 /* check if source eid has an associated mapping. If pitr mode is on,
1236 * just use the pitr's mapping */
1237 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
1238 gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->leid);
1240 if (GID_LOOKUP_MISS == local_mi)
1242 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1243 format_gid_address, &a->leid);
1248 /* update forwarding */
1249 dp_add_fwd_entry (lcm, local_mi, remote_mi);
1252 dp_del_fwd_entry (lcm, 0, remote_mi);
1258 vnet_lisp_set_map_request_mode (u8 mode)
1260 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1262 if (vnet_lisp_enable_disable_status () == 0)
1264 clib_warning ("LISP is disabled!");
1265 return VNET_API_ERROR_LISP_DISABLED;
1268 if (mode >= _MR_MODE_MAX)
1270 clib_warning ("Invalid LISP map request mode %d!", mode);
1271 return VNET_API_ERROR_INVALID_ARGUMENT;
1274 lcm->map_request_mode = mode;
1279 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1281 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1282 u32 locator_set_index = ~0;
1286 if (vnet_lisp_enable_disable_status () == 0)
1288 clib_warning ("LISP is disabled!");
1289 return VNET_API_ERROR_LISP_DISABLED;
1292 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1295 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1298 locator_set_index = p[0];
1302 pool_get (lcm->mapping_pool, m);
1303 m->locator_set_index = locator_set_index;
1306 lcm->pitr_map_index = m - lcm->mapping_pool;
1308 /* enable pitr mode */
1313 /* remove pitr mapping */
1314 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1316 /* disable pitr mode */
1323 * Configure Proxy-ETR
1325 * @param ip PETR's IP address
1326 * @param is_add Flag that indicates if this is an addition or removal
1328 * return 0 on success
1331 vnet_lisp_use_petr (ip_address_t * ip, u8 is_add)
1333 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1336 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1339 if (vnet_lisp_enable_disable_status () == 0)
1341 clib_warning ("LISP is disabled!");
1342 return VNET_API_ERROR_LISP_DISABLED;
1345 memset (ls_args, 0, sizeof (*ls_args));
1349 /* Create dummy petr locator-set */
1350 memset (&loc, 0, sizeof (loc));
1351 gid_address_from_ip (&loc.address, ip);
1353 loc.state = loc.weight = 1;
1356 ls_args->is_add = 1;
1357 ls_args->index = ~0;
1358 vec_add1 (ls_args->locators, loc);
1359 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1361 /* Add petr mapping */
1362 pool_get (lcm->mapping_pool, m);
1363 m->locator_set_index = ls_index;
1364 lcm->petr_map_index = m - lcm->mapping_pool;
1366 /* Enable use-petr */
1367 lcm->flags |= LISP_FLAG_USE_PETR;
1371 m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1373 /* Remove petr locator */
1374 ls_args->is_add = 0;
1375 ls_args->index = m->locator_set_index;
1376 vnet_lisp_add_del_locator_set (ls_args, 0);
1378 /* Remove petr mapping */
1379 pool_put_index (lcm->mapping_pool, lcm->petr_map_index);
1381 /* Disable use-petr */
1382 lcm->flags &= ~LISP_FLAG_USE_PETR;
1387 /* cleans locator to locator-set data and removes locators not part of
1388 * any locator-set */
1390 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1392 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
1393 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1394 for (i = 0; i < vec_len (ls->locator_indices); i++)
1396 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1397 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1399 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1401 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1402 if (ls_indexp[0] == lsi)
1406 /* delete index for removed locator-set */
1407 vec_del1 (ls_indexes[0], j);
1409 /* delete locator if it's part of no locator-set */
1410 if (vec_len (ls_indexes[0]) == 0)
1412 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1413 vec_add1 (to_be_deleted, i);
1419 for (i = 0; i < vec_len (to_be_deleted); i++)
1421 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1422 vec_del1 (ls->locator_indices, loc_indexp[0]);
1424 vec_free (to_be_deleted);
1428 static inline uword *
1429 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
1431 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1436 /* find locator-set */
1439 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
1450 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
1456 ASSERT (ls != NULL);
1457 ASSERT (loc != NULL);
1459 vec_foreach (locit, ls->locator_indices)
1461 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1462 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1463 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1465 clib_warning ("Duplicate locator");
1466 return VNET_API_ERROR_VALUE_EXIST;
1474 update_adjacencies_by_map_index (lisp_cp_main_t * lcm, u8 is_local,
1475 u32 mapping_index, u8 remove_only)
1479 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1481 map = pool_elt_at_index (lcm->mapping_pool, mapping_index);
1484 pool_foreach(fwd, lcm->fwd_entry_pool,
1486 if ((is_local && 0 == gid_address_cmp (&map->eid, &fwd->leid)) ||
1487 (!is_local && 0 == gid_address_cmp (&map->eid, &fwd->reid)))
1490 gid_address_copy (&a->leid, &fwd->leid);
1491 gid_address_copy (&a->reid, &fwd->reid);
1493 vnet_lisp_add_del_adjacency (a);
1498 vnet_lisp_add_del_adjacency (a);
1506 update_fwd_entries_by_locator_set (lisp_cp_main_t * lcm, u8 is_local,
1507 u32 ls_index, u8 remove_only)
1512 if (vec_len (lcm->locator_set_to_eids) <= ls_index)
1515 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, ls_index);
1517 for (i = 0; i < vec_len (eid_indexes[0]); i++)
1519 map_indexp = vec_elt_at_index (eid_indexes[0], i);
1520 update_adjacencies_by_map_index (lcm, is_local, map_indexp[0],
1526 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1527 u32 ls_index, u32 loc_id)
1529 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1530 u32 **ls_indexes = NULL;
1532 ASSERT (ls != NULL);
1533 ASSERT (locit != NULL);
1535 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1536 pool_put_index (lcm->locator_pool, locit[0]);
1537 vec_del1 (ls->locator_indices, loc_id);
1538 vec_del1 (ls_indexes[0], ls_index);
1542 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1543 locator_set_t * ls, u32 * ls_result)
1545 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1546 locator_t *loc = NULL, *itloc = NULL;
1547 uword _p = (u32) ~ 0, *p = &_p;
1548 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
1554 if (vnet_lisp_enable_disable_status () == 0)
1556 clib_warning ("LISP is disabled!");
1557 return VNET_API_ERROR_LISP_DISABLED;
1560 p = get_locator_set_index (a, p);
1563 clib_warning ("locator-set %v doesn't exist", a->name);
1564 return VNET_API_ERROR_INVALID_ARGUMENT;
1569 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1572 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1574 return VNET_API_ERROR_INVALID_ARGUMENT;
1581 ls_result[0] = p[0];
1583 /* allocate locators */
1584 vec_foreach (itloc, a->locators)
1586 ret = is_locator_in_locator_set (lcm, ls, itloc);
1592 pool_get (lcm->locator_pool, loc);
1594 loc_index = loc - lcm->locator_pool;
1596 vec_add1 (ls->locator_indices, loc_index);
1598 vec_validate (lcm->locator_to_locator_sets, loc_index);
1599 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1601 vec_add1 (ls_indexes[0], p[0]);
1609 vec_foreach (itloc, a->locators)
1613 vec_foreach (locit, ls->locator_indices)
1615 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1617 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1620 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1622 if (0 == loc->local &&
1623 !gid_address_cmp (&loc->address, &itloc->address))
1626 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1631 /* update fwd entries using this locator in DP */
1632 update_fwd_entries_by_locator_set (lcm, loc->local, ls_index,
1633 vec_len (ls->locator_indices)
1646 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
1649 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1651 uword _p = (u32) ~ 0, *p = &_p;
1656 if (vnet_lisp_enable_disable_status () == 0)
1658 clib_warning ("LISP is disabled!");
1659 return VNET_API_ERROR_LISP_DISABLED;
1664 p = get_locator_set_index (a, p);
1667 if (p && p[0] != (u32) ~ 0)
1669 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1672 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1677 /* clean locator to locator-set vectors and remove locators if
1678 * they're not part of another locator-set */
1679 clean_locator_to_locator_set (lcm, p[0]);
1681 /* remove locator indices from locator set */
1682 vec_free (ls->locator_indices);
1687 ls_result[0] = p[0];
1689 /* new locator-set */
1692 pool_get (lcm->locator_set_pool, ls);
1693 memset (ls, 0, sizeof (*ls));
1694 ls_index = ls - lcm->locator_set_pool;
1698 ls->name = vec_dup (a->name);
1700 if (!lcm->locator_set_index_by_name)
1701 lcm->locator_set_index_by_name = hash_create_vec (
1709 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
1712 /* mark as local locator-set */
1713 vec_add1 (lcm->local_locator_set_indexes, ls_index);
1715 ls->local = a->local;
1717 ls_result[0] = ls_index;
1720 ret = vnet_lisp_add_del_locator (a, ls, NULL);
1728 p = get_locator_set_index (a, p);
1731 clib_warning ("locator-set %v doesn't exists", a->name);
1735 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1738 clib_warning ("locator-set with index %d doesn't exists", p[0]);
1742 if (lcm->mreq_itr_rlocs == p[0])
1744 clib_warning ("Can't delete the locator-set used to constrain "
1745 "the itr-rlocs in map-requests!");
1749 if (vec_len (lcm->locator_set_to_eids) != 0)
1751 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
1752 if (vec_len (eid_indexes[0]) != 0)
1755 ("Can't delete a locator that supports a mapping!");
1760 /* clean locator to locator-sets data */
1761 clean_locator_to_locator_set (lcm, p[0]);
1767 vec_foreach_index (it, lcm->local_locator_set_indexes)
1769 lsi = vec_elt (lcm->local_locator_set_indexes, it);
1772 vec_del1 (lcm->local_locator_set_indexes, it);
1776 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
1778 vec_free (ls->name);
1779 vec_free (ls->locator_indices);
1780 pool_put (lcm->locator_set_pool, ls);
1786 vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
1788 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1790 lcm->rloc_probing = is_enable;
1795 vnet_lisp_map_register_enable_disable (u8 is_enable)
1797 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1799 lcm->map_registering = is_enable;
1804 vnet_lisp_enable_disable (u8 is_enable)
1807 clib_error_t *error = 0;
1808 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1809 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
1811 a->is_en = is_enable;
1812 error = vnet_lisp_gpe_enable_disable (a);
1815 return clib_error_return (0, "failed to %s data-plane!",
1816 a->is_en ? "enable" : "disable");
1821 /* enable all l2 and l3 ifaces */
1824 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
1825 dp_add_del_iface(lcm, vni, 0, 1);
1827 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
1828 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
1834 /* clear interface table */
1835 hash_free (lcm->fwd_entry_by_mapping_index);
1836 pool_free (lcm->fwd_entry_pool);
1839 /* update global flag */
1840 lcm->is_enabled = is_enable;
1846 vnet_lisp_enable_disable_status (void)
1848 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1849 return lcm->is_enabled;
1853 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
1855 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1857 lisp_msmr_t _mr, *mr = &_mr;
1859 if (vnet_lisp_enable_disable_status () == 0)
1861 clib_warning ("LISP is disabled!");
1862 return VNET_API_ERROR_LISP_DISABLED;
1868 if (get_map_resolver (&a->address))
1870 clib_warning ("map-resolver %U already exists!", format_ip_address,
1875 memset (mr, 0, sizeof (*mr));
1876 ip_address_copy (&mr->address, &a->address);
1877 vec_add1 (lcm->map_resolvers, *mr);
1879 if (vec_len (lcm->map_resolvers) == 1)
1880 lcm->do_map_resolver_election = 1;
1884 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
1886 mr = vec_elt_at_index (lcm->map_resolvers, i);
1887 if (!ip_address_cmp (&mr->address, &a->address))
1889 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
1890 lcm->do_map_resolver_election = 1;
1892 vec_del1 (lcm->map_resolvers, i);
1901 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
1903 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1906 if (vnet_lisp_enable_disable_status () == 0)
1908 clib_warning ("LISP is disabled!");
1909 return VNET_API_ERROR_LISP_DISABLED;
1914 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
1917 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
1918 return VNET_API_ERROR_INVALID_ARGUMENT;
1921 lcm->mreq_itr_rlocs = p[0];
1925 lcm->mreq_itr_rlocs = ~0;
1931 /* Statistics (not really errors) */
1932 #define foreach_lisp_cp_lookup_error \
1934 _(MAP_REQUESTS_SENT, "map-request sent")
1936 static char *lisp_cp_lookup_error_strings[] = {
1937 #define _(sym,string) string,
1938 foreach_lisp_cp_lookup_error
1944 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
1945 foreach_lisp_cp_lookup_error
1947 LISP_CP_LOOKUP_N_ERROR,
1948 } lisp_cp_lookup_error_t;
1952 LISP_CP_LOOKUP_NEXT_DROP,
1953 LISP_CP_LOOKUP_N_NEXT,
1954 } lisp_cp_lookup_next_t;
1958 gid_address_t dst_eid;
1959 ip_address_t map_resolver_ip;
1960 } lisp_cp_lookup_trace_t;
1963 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
1965 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1966 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1967 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
1969 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
1970 format_ip_address, &t->map_resolver_ip, format_gid_address,
1976 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
1977 ip_address_t * sloc)
1982 if (vec_len (lcm->map_resolvers) == 0)
1984 clib_warning ("No map-resolver configured");
1988 /* find the first mr ip we have a route to and the ip of the
1989 * iface that has a route to it */
1990 vec_foreach (mrit, lcm->map_resolvers)
1993 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
1995 ip_address_copy (mr_ip, a);
1997 /* also update globals */
2002 clib_warning ("Can't find map-resolver and local interface ip!");
2006 static gid_address_t *
2007 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2013 ip_interface_address_t *ia = 0;
2014 gid_address_t gid_data, *gid = &gid_data;
2015 gid_address_t *rlocs = 0;
2016 ip_prefix_t *ippref = &gid_address_ippref (gid);
2017 ip_address_t *rloc = &ip_prefix_addr (ippref);
2019 memset (gid, 0, sizeof (gid[0]));
2020 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
2021 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
2023 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
2024 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2026 /* Add ipv4 locators first TODO sort them */
2029 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2030 loc->sw_if_index, 1 /* unnumbered */,
2032 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2033 ip_address_set (rloc, addr, IP4);
2034 ip_prefix_len (ippref) = 32;
2035 ip_prefix_normalize (ippref);
2036 vec_add1 (rlocs, gid[0]);
2039 /* Add ipv6 locators */
2040 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2041 loc->sw_if_index, 1 /* unnumbered */,
2043 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2044 ip_address_set (rloc, addr, IP6);
2045 ip_prefix_len (ippref) = 128;
2046 ip_prefix_normalize (ippref);
2047 vec_add1 (rlocs, gid[0]);
2055 static vlib_buffer_t *
2056 build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
2057 ip_address_t * sloc, ip_address_t * rloc,
2058 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
2062 vlib_main_t *vm = lcm->vlib_main;
2064 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2066 clib_warning ("Can't allocate buffer for Map-Request!");
2070 b = vlib_get_buffer (vm, bi);
2072 /* leave some space for the encap headers */
2073 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2076 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
2077 1 /* rloc probe */ , nonce_res);
2079 /* push outer ip header */
2080 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2088 static vlib_buffer_t *
2089 build_encapsulated_map_request (lisp_cp_main_t * lcm,
2090 gid_address_t * seid, gid_address_t * deid,
2091 locator_set_t * loc_set, ip_address_t * mr_ip,
2092 ip_address_t * sloc, u8 is_smr_invoked,
2093 u64 * nonce_res, u32 * bi_res)
2097 gid_address_t *rlocs = 0;
2098 vlib_main_t *vm = lcm->vlib_main;
2100 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2102 clib_warning ("Can't allocate buffer for Map-Request!");
2106 b = vlib_get_buffer (vm, bi);
2108 /* leave some space for the encap headers */
2109 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2112 rlocs = build_itr_rloc_list (lcm, loc_set);
2114 if (MR_MODE_SRC_DST == lcm->map_request_mode
2115 && GID_ADDR_SRC_DST != gid_address_type (deid))
2118 memset (&sd, 0, sizeof (sd));
2119 build_src_dst (&sd, seid, deid);
2120 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
2121 0 /* rloc probe */ , nonce_res);
2126 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
2127 0 /* rloc probe */ , nonce_res);
2130 /* push ecm: udp-ip-lisp */
2131 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2133 /* push outer ip header */
2134 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2144 reset_pending_mr_counters (pending_map_request_t * r)
2146 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2151 elect_map_resolver (lisp_cp_main_t * lcm)
2155 vec_foreach (mr, lcm->map_resolvers)
2159 ip_address_copy (&lcm->active_map_resolver, &mr->address);
2160 lcm->do_map_resolver_election = 0;
2168 free_map_register_records (mapping_t * maps)
2171 vec_foreach (map, maps) vec_free (map->locators);
2177 add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
2178 ip_address_t * probed_loc)
2181 locator_t *loc, new;
2182 ip_interface_address_t *ia = 0;
2184 ip_address_t *new_ip = &gid_address_ip (&new.address);
2187 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2189 vec_foreach (li, ls->locator_indices)
2191 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
2196 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2197 loc->sw_if_index, 1 /* unnumbered */,
2199 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
2201 ip_address_set (new_ip, addr, IP4);
2204 /* Add ipv6 locators */
2205 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2206 loc->sw_if_index, 1 /* unnumbered */,
2208 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
2210 ip_address_set (new_ip, addr, IP6);
2214 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
2217 vec_add1 (m->locators, new);
2222 build_map_register_record_list (lisp_cp_main_t * lcm)
2224 mapping_t *recs = 0, rec, *m;
2227 pool_foreach(m, lcm->mapping_pool,
2229 /* for now build only local mappings */
2234 add_locators (lcm, &rec, m->locator_set_index, NULL);
2235 vec_add1 (recs, rec);
2243 update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
2244 lisp_key_type_t key_id, u8 * key,
2245 u16 auth_data_len, u32 msg_len)
2247 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
2248 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
2250 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
2251 (unsigned char *) map_reg_hdr, msg_len, NULL,
2253 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
2258 static vlib_buffer_t *
2259 build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
2260 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
2261 mapping_t * records, lisp_key_type_t key_id, u8 * key,
2266 u32 bi, auth_data_len = 0, msg_len = 0;
2267 vlib_main_t *vm = lcm->vlib_main;
2269 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2271 clib_warning ("Can't allocate buffer for Map-Register!");
2275 b = vlib_get_buffer (vm, bi);
2277 /* leave some space for the encap headers */
2278 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2280 auth_data_len = auth_data_len_by_key_id (key_id);
2281 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
2282 auth_data_len, nonce_res,
2285 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
2288 /* push outer ip header */
2289 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2297 get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
2300 while (lcm->do_map_resolver_election
2301 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
2302 &lcm->active_map_resolver,
2305 if (0 == elect_map_resolver (lcm))
2306 /* all map resolvers are down */
2308 /* restart MR checking by marking all of them up */
2309 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
2316 /* CP output statistics */
2317 #define foreach_lisp_cp_output_error \
2318 _(MAP_REGISTERS_SENT, "map-registers sent") \
2319 _(RLOC_PROBES_SENT, "rloc-probes sent")
2321 static char *lisp_cp_output_error_strings[] = {
2322 #define _(sym,string) string,
2323 foreach_lisp_cp_output_error
2329 #define _(sym,str) LISP_CP_OUTPUT_ERROR_##sym,
2330 foreach_lisp_cp_output_error
2332 LISP_CP_OUTPUT_N_ERROR,
2333 } lisp_cp_output_error_t;
2336 lisp_cp_output (vlib_main_t * vm, vlib_node_runtime_t * node,
2337 vlib_frame_t * from_frame)
2342 /* dummy node used only for statistics */
2344 VLIB_REGISTER_NODE (lisp_cp_output_node) = {
2345 .function = lisp_cp_output,
2346 .name = "lisp-cp-output",
2347 .vector_size = sizeof (u32),
2348 .format_trace = format_lisp_cp_input_trace,
2349 .type = VLIB_NODE_TYPE_INTERNAL,
2351 .n_errors = LISP_CP_OUTPUT_N_ERROR,
2352 .error_strings = lisp_cp_output_error_strings,
2354 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2357 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2363 send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
2364 u32 local_locator_set_index, ip_address_t * sloc,
2365 ip_address_t * rloc)
2372 u32 next_index, *to_next;
2373 gid_address_t *itr_rlocs;
2375 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
2376 itr_rlocs = build_itr_rloc_list (lcm, ls);
2378 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
2379 vec_free (itr_rlocs);
2383 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2385 next_index = (ip_addr_version (rloc) == IP4) ?
2386 ip4_lookup_node.index : ip6_lookup_node.index;
2388 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2390 /* Enqueue the packet */
2391 to_next = vlib_frame_vector_args (f);
2394 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2396 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2401 send_rloc_probes (lisp_cp_main_t * lcm)
2407 u32 si, rloc_probes_sent = 0;
2410 pool_foreach (e, lcm->fwd_entry_pool,
2412 if (vec_len (e->locator_pairs) == 0)
2415 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
2418 clib_warning ("internal error: cannot find local eid %U in "
2419 "map-cache!", format_gid_address, &e->leid);
2422 lm = pool_elt_at_index (lcm->mapping_pool, si);
2424 /* get the best (lowest) priority */
2425 lprio = e->locator_pairs[0].priority;
2427 /* send rloc-probe for pair(s) with the best remote locator priority */
2428 vec_foreach (lp, e->locator_pairs)
2430 if (lp->priority != lprio)
2433 /* get first remote locator */
2434 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
2441 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2442 LISP_CP_OUTPUT_ERROR_RLOC_PROBES_SENT,
2448 send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
2450 u32 bi, map_registers_sent = 0;
2455 u32 next_index, *to_next;
2456 ip_address_t *ms = 0;
2457 mapping_t *records, *r, *group, *k;
2459 // TODO: support multiple map servers and do election
2460 if (0 == vec_len (lcm->map_servers))
2463 ms = &lcm->map_servers[0].address;
2465 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
2467 clib_warning ("no eligible interface address found for %U!",
2468 format_ip_address, &lcm->map_servers[0]);
2472 records = build_map_register_record_list (lcm);
2476 vec_foreach (r, records)
2479 u8 key_id = r->key_id;
2482 continue; /* no secret key -> map-register cannot be sent */
2485 vec_add1 (group, r[0]);
2487 /* group mappings that share common key */
2488 for (k = r + 1; k < vec_end (records); k++)
2490 if (k->key_id != r->key_id)
2493 if (vec_is_equal (k->key, r->key))
2495 vec_add1 (group, k[0]);
2496 k->key = 0; /* don't process this mapping again */
2500 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, group,
2506 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2508 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2509 ip4_lookup_node.index : ip6_lookup_node.index;
2511 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2513 /* Enqueue the packet */
2514 to_next = vlib_frame_vector_args (f);
2517 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2518 map_registers_sent++;
2520 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2522 free_map_register_records (records);
2524 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2525 LISP_CP_OUTPUT_ERROR_MAP_REGISTERS_SENT,
2526 map_registers_sent);
2531 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
2532 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2534 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2535 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2538 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
2539 gid_address_t * seid, gid_address_t * deid,
2540 u8 is_smr_invoked, u8 is_resend)
2542 u32 next_index, bi = 0, *to_next, map_index;
2546 locator_set_t *loc_set;
2548 pending_map_request_t *pmr, *duplicate_pmr = 0;
2552 /* if there is already a pending request remember it */
2555 pool_foreach(pmr, lcm->pending_map_requests_pool,
2557 if (!gid_address_cmp (&pmr->src, seid)
2558 && !gid_address_cmp (&pmr->dst, deid))
2560 duplicate_pmr = pmr;
2566 if (!is_resend && duplicate_pmr)
2568 /* don't send the request if there is a pending map request already */
2572 /* get locator-set for seid */
2573 if (!lcm->lisp_pitr)
2575 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2576 if (map_index == ~0)
2578 clib_warning ("No local mapping found in eid-table for %U!",
2579 format_gid_address, seid);
2583 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2588 ("Mapping found for src eid %U is not marked as local!",
2589 format_gid_address, seid);
2592 ls_index = map->locator_set_index;
2596 map_index = lcm->pitr_map_index;
2597 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
2598 ls_index = map->locator_set_index;
2601 /* overwrite locator set if map-request itr-rlocs configured */
2602 if (~0 != lcm->mreq_itr_rlocs)
2604 ls_index = lcm->mreq_itr_rlocs;
2607 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
2609 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
2612 duplicate_pmr->to_be_removed = 1;
2616 /* build the encapsulated map request */
2617 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
2618 &lcm->active_map_resolver,
2619 &sloc, is_smr_invoked, &nonce, &bi);
2624 /* set fib index to default and lookup node */
2625 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2626 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2627 ip4_lookup_node.index : ip6_lookup_node.index;
2629 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2631 /* Enqueue the packet */
2632 to_next = vlib_frame_vector_args (f);
2635 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2638 /* if there is a pending request already update it */
2640 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
2642 /* remove the oldest nonce */
2643 u64 CLIB_UNUSED (tmp), *nonce_del;
2644 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
2645 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
2646 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
2649 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
2650 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2651 duplicate_pmr - lcm->pending_map_requests_pool);
2655 /* add map-request to pending requests table */
2656 pool_get (lcm->pending_map_requests_pool, pmr);
2657 memset (pmr, 0, sizeof (*pmr));
2658 gid_address_copy (&pmr->src, seid);
2659 gid_address_copy (&pmr->dst, deid);
2660 clib_fifo_add1 (pmr->nonces, nonce);
2661 pmr->is_smr_invoked = is_smr_invoked;
2662 reset_pending_mr_counters (pmr);
2663 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2664 pmr - lcm->pending_map_requests_pool);
2671 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
2673 ip4_header_t *ip4 = hdr;
2676 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2678 ip_address_set (src, &ip4->src_address, IP4);
2679 ip_address_set (dst, &ip4->dst_address, IP4);
2684 ip_address_set (src, &ip6->src_address, IP6);
2685 ip_address_set (dst, &ip6->dst_address, IP6);
2690 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2694 u32 vni = ~0, table_id = ~0;
2696 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
2697 IP4 ? FIB_PROTOCOL_IP4 :
2703 vnip = hash_get (lcm->vni_by_table_id, table_id);
2707 clib_warning ("vrf %d is not mapped to any vni!", table_id);
2713 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2719 l2input_main_t *l2im = &l2input_main;
2720 l2_input_config_t *config;
2721 l2_bridge_domain_t *bd_config;
2723 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
2724 config = vec_elt_at_index (l2im->configs, sw_if_index0);
2725 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
2727 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
2731 clib_warning ("bridge domain %d is not mapped to any vni!",
2738 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2739 gid_address_t * src, gid_address_t * dst,
2744 memset (src, 0, sizeof (*src));
2745 memset (dst, 0, sizeof (*dst));
2747 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
2750 u8 version, preflen;
2752 gid_address_type (src) = GID_ADDR_IP_PREFIX;
2753 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
2755 ip = vlib_buffer_get_current (b);
2756 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
2758 version = gid_address_ip_version (src);
2759 preflen = ip_address_max_len (version);
2760 gid_address_ippref_len (src) = preflen;
2761 gid_address_ippref_len (dst) = preflen;
2763 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
2764 gid_address_vni (dst) = vni;
2765 gid_address_vni (src) = vni;
2767 else if (LISP_AFI_MAC == type)
2769 ethernet_header_t *eh;
2771 eh = vlib_buffer_get_current (b);
2773 gid_address_type (src) = GID_ADDR_MAC;
2774 gid_address_type (dst) = GID_ADDR_MAC;
2775 mac_copy (&gid_address_mac (src), eh->src_address);
2776 mac_copy (&gid_address_mac (dst), eh->dst_address);
2779 vni = lisp_get_vni_from_buffer_eth (lcm, b);
2781 gid_address_vni (dst) = vni;
2782 gid_address_vni (src) = vni;
2784 else if (LISP_AFI_LCAF == type)
2786 /* Eventually extend this to support NSH and other */
2792 lisp_cp_lookup_inline (vlib_main_t * vm,
2793 vlib_node_runtime_t * node,
2794 vlib_frame_t * from_frame, int overlay)
2796 u32 *from, *to_next_drop, di, si;
2797 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2798 u32 pkts_mapped = 0;
2799 uword n_left_from, n_left_to_next_drop;
2801 from = vlib_frame_vector_args (from_frame);
2802 n_left_from = from_frame->n_vectors;
2804 while (n_left_from > 0)
2806 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2807 to_next_drop, n_left_to_next_drop);
2809 while (n_left_from > 0 && n_left_to_next_drop > 0)
2813 gid_address_t src, dst;
2818 to_next_drop[0] = pi0;
2820 n_left_to_next_drop -= 1;
2822 b0 = vlib_get_buffer (vm, pi0);
2823 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
2825 /* src/dst eid pair */
2826 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst, overlay);
2828 /* if we have remote mapping for destination already in map-chache
2829 add forwarding tunnel directly. If not send a map-request */
2830 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
2834 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
2835 /* send a map-request also in case of negative mapping entry
2836 with corresponding action */
2837 if (m->action == LISP_SEND_MAP_REQUEST)
2839 /* send map-request */
2840 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2841 0 /* is_resend */ );
2846 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
2850 dp_add_fwd_entry_from_mt (si, di);
2856 /* send map-request */
2857 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2858 0 /* is_resend */ );
2862 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2864 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
2867 memset (tr, 0, sizeof (*tr));
2868 gid_address_copy (&tr->dst_eid, &dst);
2869 ip_address_copy (&tr->map_resolver_ip,
2870 &lcm->active_map_resolver);
2872 gid_address_free (&dst);
2873 gid_address_free (&src);
2876 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2877 n_left_to_next_drop);
2879 vlib_node_increment_counter (vm, node->node_index,
2880 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
2882 return from_frame->n_vectors;
2886 lisp_cp_lookup_ip4 (vlib_main_t * vm,
2887 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2889 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
2893 lisp_cp_lookup_ip6 (vlib_main_t * vm,
2894 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2896 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
2900 lisp_cp_lookup_l2 (vlib_main_t * vm,
2901 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2903 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
2907 lisp_cp_lookup_nsh (vlib_main_t * vm,
2908 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2910 /* TODO decide if NSH should be propagated as LCAF or not */
2911 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_LCAF));
2915 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
2916 .function = lisp_cp_lookup_ip4,
2917 .name = "lisp-cp-lookup-ip4",
2918 .vector_size = sizeof (u32),
2919 .format_trace = format_lisp_cp_lookup_trace,
2920 .type = VLIB_NODE_TYPE_INTERNAL,
2922 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2923 .error_strings = lisp_cp_lookup_error_strings,
2925 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2928 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2934 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
2935 .function = lisp_cp_lookup_ip6,
2936 .name = "lisp-cp-lookup-ip6",
2937 .vector_size = sizeof (u32),
2938 .format_trace = format_lisp_cp_lookup_trace,
2939 .type = VLIB_NODE_TYPE_INTERNAL,
2941 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2942 .error_strings = lisp_cp_lookup_error_strings,
2944 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2947 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2953 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
2954 .function = lisp_cp_lookup_l2,
2955 .name = "lisp-cp-lookup-l2",
2956 .vector_size = sizeof (u32),
2957 .format_trace = format_lisp_cp_lookup_trace,
2958 .type = VLIB_NODE_TYPE_INTERNAL,
2960 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2961 .error_strings = lisp_cp_lookup_error_strings,
2963 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2966 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2972 VLIB_REGISTER_NODE (lisp_cp_lookup_nsh_node) = {
2973 .function = lisp_cp_lookup_nsh,
2974 .name = "lisp-cp-lookup-nsh",
2975 .vector_size = sizeof (u32),
2976 .format_trace = format_lisp_cp_lookup_trace,
2977 .type = VLIB_NODE_TYPE_INTERNAL,
2979 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2980 .error_strings = lisp_cp_lookup_error_strings,
2982 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2985 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2990 /* lisp_cp_input statistics */
2991 #define foreach_lisp_cp_input_error \
2993 _(RLOC_PROBE_REQ_RECEIVED, "rloc-probe requests received") \
2994 _(RLOC_PROBE_REP_RECEIVED, "rloc-probe replies received") \
2995 _(MAP_NOTIFIES_RECEIVED, "map-notifies received") \
2996 _(MAP_REPLIES_RECEIVED, "map-replies received")
2998 static char *lisp_cp_input_error_strings[] = {
2999 #define _(sym,string) string,
3000 foreach_lisp_cp_input_error
3006 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
3007 foreach_lisp_cp_input_error
3009 LISP_CP_INPUT_N_ERROR,
3010 } lisp_cp_input_error_t;
3014 gid_address_t dst_eid;
3015 ip4_address_t map_resolver_ip;
3016 } lisp_cp_input_trace_t;
3019 format_lisp_cp_input_trace (u8 * s, va_list * args)
3021 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3022 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3023 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
3024 va_arg (*args, lisp_cp_input_trace_t *);
3026 s = format (s, "LISP-CP-INPUT: TODO");
3031 remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
3034 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3035 memset (adj_args, 0, sizeof (adj_args[0]));
3037 m = pool_elt_at_index (lcm->mapping_pool, mi);
3039 gid_address_copy (&adj_args->reid, &m->eid);
3040 adj_args->is_add = 0;
3041 if (vnet_lisp_add_del_adjacency (adj_args))
3042 clib_warning ("failed to del adjacency!");
3044 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
3045 0 /* is_static */ , 0);
3046 mapping_delete_timer (lcm, mi);
3050 mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
3051 f64 expiration_time)
3054 u64 now = clib_cpu_time_now ();
3055 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
3056 u64 exp_clock_time = now + expiration_time * cpu_cps;
3058 m = pool_elt_at_index (lcm->mapping_pool, mi);
3061 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
3065 map_records_arg_free (map_records_arg_t * a)
3068 vec_foreach (m, a->mappings)
3070 vec_free (m->locators);
3071 gid_address_free (&m->eid);
3078 process_map_reply (map_records_arg_t * a)
3081 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3082 u32 dst_map_index = 0;
3083 pending_map_request_t *pmr;
3087 if (a->is_rloc_probe)
3090 /* Check pending requests table and nonce */
3091 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
3094 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
3097 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
3099 vec_foreach (m, a->mappings)
3101 /* insert/update mappings cache */
3102 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
3103 m->authoritative, m->ttl,
3104 1, 0 /* is_static */ , &dst_map_index);
3106 if (dst_map_index == (u32) ~ 0)
3109 /* try to program forwarding only if mapping saved or updated */
3110 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3111 memset (adj_args, 0, sizeof (adj_args[0]));
3113 gid_address_copy (&adj_args->leid, &pmr->src);
3114 gid_address_copy (&adj_args->reid, &m->eid);
3115 adj_args->is_add = 1;
3116 if (vnet_lisp_add_del_adjacency (adj_args))
3117 clib_warning ("failed to add adjacency!");
3119 if ((u32) ~ 0 != m->ttl)
3120 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
3123 /* remove pending map request entry */
3126 clib_fifo_foreach (noncep, pmr->nonces, ({
3127 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
3131 clib_fifo_free (pmr->nonces);
3132 pool_put (lcm->pending_map_requests_pool, pmr);
3135 map_records_arg_free (a);
3140 is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
3141 lisp_key_type_t key_id, u8 * key)
3147 auth_data_len = auth_data_len_by_key_id (key_id);
3148 if ((u16) ~ 0 == auth_data_len)
3150 clib_warning ("invalid length for key_id %d!", key_id);
3154 /* save auth data */
3155 vec_validate (auth_data, auth_data_len - 1);
3156 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
3158 /* clear auth data */
3159 memset (MNOTIFY_DATA (h), 0, auth_data_len);
3161 /* get hash of the message */
3162 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3163 (unsigned char *) h, msg_len, NULL, NULL);
3165 result = memcmp (code, auth_data, auth_data_len);
3167 vec_free (auth_data);
3173 process_map_notify (map_records_arg_t * a)
3175 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3178 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
3181 clib_warning ("No pending map-register entry with nonce %lu!",
3186 map_records_arg_free (a);
3187 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
3191 get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
3195 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
3198 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
3202 return pool_elt_at_index (lcm->mapping_pool, mi);
3206 * When map-notify is received it is necessary that all EIDs in the record
3207 * list share common key. The key is then used to verify authentication
3208 * data in map-notify message.
3211 map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
3212 u32 key_id, u8 ** key_out)
3214 u32 i, len = vec_len (maps);
3217 /* get key of the first mapping */
3218 m = get_mapping (lcm, &maps[0].eid);
3222 key_out[0] = m->key;
3224 for (i = 1; i < len; i++)
3226 m = get_mapping (lcm, &maps[i].eid);
3230 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
3232 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
3240 parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
3242 locator_t *locators = 0;
3248 /* parse record eid */
3249 for (i = 0; i < count; i++)
3251 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
3254 clib_warning ("Failed to parse mapping record!");
3255 vec_foreach (loc, locators) locator_free (loc);
3256 vec_free (locators);
3260 m.locators = locators;
3261 gid_address_copy (&m.eid, &deid);
3262 vec_add1 (a->mappings, m);
3268 static map_records_arg_t *
3269 parse_map_notify (vlib_buffer_t * b)
3272 map_notify_hdr_t *mnotif_hdr;
3273 lisp_key_type_t key_id;
3274 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3277 u16 auth_data_len = 0;
3279 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3281 memset (a, 0, sizeof (*a));
3282 mnotif_hdr = vlib_buffer_get_current (b);
3283 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
3284 memset (&deid, 0, sizeof (deid));
3286 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
3287 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
3288 auth_data_len = auth_data_len_by_key_id (key_id);
3290 /* advance buffer by authentication data */
3291 vlib_buffer_pull (b, auth_data_len);
3293 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
3294 rc = parse_map_records (b, a, record_count);
3297 map_records_arg_free (a);
3301 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
3304 map_records_arg_free (a);
3308 /* verify authentication data */
3309 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
3310 - (u8 *) mnotif_hdr, key_id, key))
3312 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
3314 map_records_arg_free (a);
3320 static vlib_buffer_t *
3321 build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
3322 ip_address_t * dst, u64 nonce, u8 probe_bit,
3323 mapping_t * records, u16 dst_port, u32 * bi_res)
3327 vlib_main_t *vm = lcm->vlib_main;
3329 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3331 clib_warning ("Can't allocate buffer for Map-Register!");
3335 b = vlib_get_buffer (vm, bi);
3337 /* leave some space for the encap headers */
3338 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3340 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
3342 /* push outer ip header */
3343 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
3350 send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
3351 u8 probe_bit, u64 nonce, u16 dst_port,
3352 ip_address_t * probed_loc)
3358 u32 next_index, *to_next;
3359 mapping_t *records = 0, *m;
3361 m = pool_elt_at_index (lcm->mapping_pool, mi);
3365 vec_add1 (records, m[0]);
3366 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
3367 memset (&src, 0, sizeof (src));
3369 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
3371 clib_warning ("can't find inteface address for %U", format_ip_address,
3376 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
3380 free_map_register_records (records);
3382 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3383 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3384 ip4_lookup_node.index : ip6_lookup_node.index;
3386 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3388 /* Enqueue the packet */
3389 to_next = vlib_frame_vector_args (f);
3392 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3397 find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
3399 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
3400 if (start < 0 && start < -sizeof (b->pre_data))
3406 *ip_hdr = b->data + start;
3407 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
3412 process_map_request (vlib_main_t * vm, vlib_node_runtime_t * node,
3413 lisp_cp_main_t * lcm, vlib_buffer_t * b)
3416 ip_address_t *dst_loc = 0, probed_loc, src_loc;
3418 map_request_hdr_t *mreq_hdr;
3419 gid_address_t src, dst;
3421 u32 i, len = 0, rloc_probe_recv = 0;
3422 gid_address_t *itr_rlocs = 0;
3424 mreq_hdr = vlib_buffer_get_current (b);
3425 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
3428 ("Only SMR Map-Requests and RLOC probe supported for now!");
3432 vlib_buffer_pull (b, sizeof (*mreq_hdr));
3433 nonce = MREQ_NONCE (mreq_hdr);
3436 len = lisp_msg_parse_addr (b, &src);
3440 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3441 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
3445 /* parse eid records and send SMR-invoked map-requests */
3446 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
3448 memset (&dst, 0, sizeof (dst));
3449 len = lisp_msg_parse_eid_rec (b, &dst);
3452 clib_warning ("Can't parse map-request EID-record");
3456 if (MREQ_SMR (mreq_hdr))
3458 /* send SMR-invoked map-requests */
3459 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3461 else if (MREQ_RLOC_PROBE (mreq_hdr))
3463 find_ip_header (b, &ip_hdr);
3466 clib_warning ("Cannot find the IP header!");
3470 memset (&m, 0, sizeof (m));
3471 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3473 // TODO: select best locator; for now use the first one
3474 dst_loc = &gid_address_ip (&itr_rlocs[0]);
3476 /* get src/dst IP addresses */
3477 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
3479 // TODO get source port from buffer
3480 u16 src_port = LISP_CONTROL_PORT;
3482 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
3483 src_port, &probed_loc);
3488 vlib_node_increment_counter (vm, node->node_index,
3489 LISP_CP_INPUT_ERROR_RLOC_PROBE_REQ_RECEIVED,
3491 vec_free (itr_rlocs);
3495 parse_map_reply (vlib_buffer_t * b)
3502 map_reply_hdr_t *mrep_hdr;
3503 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3504 memset (a, 0, sizeof (*a));
3505 locator_t *locators;
3507 mrep_hdr = vlib_buffer_get_current (b);
3508 a->nonce = MREP_NONCE (mrep_hdr);
3509 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
3510 if (!vlib_buffer_has_space (b, sizeof (*mrep_hdr)))
3515 vlib_buffer_pull (b, sizeof (*mrep_hdr));
3517 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3519 memset (&m, 0, sizeof (m));
3521 h = vlib_buffer_get_current (b);
3523 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3524 m.action = MAP_REC_ACTION (h);
3525 m.authoritative = MAP_REC_AUTH (h);
3527 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3530 clib_warning ("Failed to parse mapping record!");
3531 map_records_arg_free (a);
3535 m.locators = locators;
3536 gid_address_copy (&m.eid, &deid);
3537 vec_add1 (a->mappings, m);
3543 queue_map_reply_for_processing (map_records_arg_t * a)
3545 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
3549 queue_map_notify_for_processing (map_records_arg_t * a)
3551 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
3555 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
3556 vlib_frame_t * from_frame)
3558 u32 n_left_from, *from, *to_next_drop, rloc_probe_rep_recv = 0,
3559 map_notifies_recv = 0;
3560 lisp_msg_type_e type;
3561 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3562 map_records_arg_t *a;
3564 from = vlib_frame_vector_args (from_frame);
3565 n_left_from = from_frame->n_vectors;
3568 while (n_left_from > 0)
3570 u32 n_left_to_next_drop;
3572 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3573 to_next_drop, n_left_to_next_drop);
3574 while (n_left_from > 0 && n_left_to_next_drop > 0)
3582 to_next_drop[0] = bi0;
3584 n_left_to_next_drop -= 1;
3586 b0 = vlib_get_buffer (vm, bi0);
3588 type = lisp_msg_type (vlib_buffer_get_current (b0));
3591 case LISP_MAP_REPLY:
3592 a = parse_map_reply (b0);
3595 if (a->is_rloc_probe)
3596 rloc_probe_rep_recv++;
3597 queue_map_reply_for_processing (a);
3600 case LISP_MAP_REQUEST:
3601 process_map_request (vm, node, lcm, b0);
3603 case LISP_MAP_NOTIFY:
3604 a = parse_map_notify (b0);
3607 map_notifies_recv++;
3608 queue_map_notify_for_processing (a);
3612 clib_warning ("Unsupported LISP message type %d", type);
3616 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
3618 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3624 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3625 n_left_to_next_drop);
3627 vlib_node_increment_counter (vm, node->node_index,
3628 LISP_CP_INPUT_ERROR_RLOC_PROBE_REP_RECEIVED,
3629 rloc_probe_rep_recv);
3630 vlib_node_increment_counter (vm, node->node_index,
3631 LISP_CP_INPUT_ERROR_MAP_NOTIFIES_RECEIVED,
3633 return from_frame->n_vectors;
3637 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3638 .function = lisp_cp_input,
3639 .name = "lisp-cp-input",
3640 .vector_size = sizeof (u32),
3641 .format_trace = format_lisp_cp_input_trace,
3642 .type = VLIB_NODE_TYPE_INTERNAL,
3644 .n_errors = LISP_CP_INPUT_N_ERROR,
3645 .error_strings = lisp_cp_input_error_strings,
3647 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3650 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3656 lisp_cp_init (vlib_main_t * vm)
3658 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3659 clib_error_t *error = 0;
3661 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3664 lcm->im4 = &ip4_main;
3665 lcm->im6 = &ip6_main;
3666 lcm->vlib_main = vm;
3667 lcm->vnet_main = vnet_get_main ();
3668 lcm->mreq_itr_rlocs = ~0;
3671 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
3673 gid_dictionary_init (&lcm->mapping_index_by_gid);
3674 lcm->do_map_resolver_election = 1;
3675 lcm->map_request_mode = MR_MODE_DST_ONLY;
3677 /* default vrf mapped to vni 0 */
3678 hash_set (lcm->table_id_by_vni, 0, 0);
3679 hash_set (lcm->vni_by_table_id, 0, 0);
3681 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
3682 lisp_cp_input_node.index, 1 /* is_ip4 */ );
3683 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
3684 lisp_cp_input_node.index, 0 /* is_ip4 */ );
3686 u64 now = clib_cpu_time_now ();
3687 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
3692 lisp_stats_api_fill (lisp_cp_main_t * lcm, lisp_gpe_main_t * lgm,
3693 lisp_api_stats_t * stat, lisp_stats_key_t * key,
3697 vlib_combined_counter_main_t *cm = &lgm->counters;
3698 lisp_gpe_fwd_entry_key_t fwd_key;
3699 const lisp_gpe_tunnel_t *lgt;
3702 memset (stat, 0, sizeof (*stat));
3703 memset (&fwd_key, 0, sizeof (fwd_key));
3705 fe = pool_elt_at_index (lcm->fwd_entry_pool, key->fwd_entry_index);
3708 gid_to_dp_address (&fe->reid, &stat->deid);
3709 gid_to_dp_address (&fe->leid, &stat->seid);
3710 stat->vni = gid_address_vni (&fe->reid);
3712 lgt = lisp_gpe_tunnel_get (key->tunnel_index);
3713 stat->loc_rloc = lgt->key->lcl;
3714 stat->rmt_rloc = lgt->key->rmt;
3716 vlib_get_combined_counter (cm, stats_index, &v);
3722 vnet_lisp_get_stats (void)
3724 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
3725 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3726 lisp_api_stats_t *stats = 0, stat;
3727 lisp_stats_key_t *key;
3731 hash_foreach_mem (key, index, lgm->lisp_stats_index_by_key,
3733 if (lisp_stats_api_fill (lcm, lgm, &stat, key, index))
3734 vec_add1 (stats, stat);
3742 send_map_request_thread_fn (void *arg)
3744 map_request_args_t *a = arg;
3745 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3748 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3750 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3756 queue_map_request (gid_address_t * seid, gid_address_t * deid,
3757 u8 smr_invoked, u8 is_resend)
3759 map_request_args_t a;
3761 a.is_resend = is_resend;
3762 gid_address_copy (&a.seid, seid);
3763 gid_address_copy (&a.deid, deid);
3764 a.smr_invoked = smr_invoked;
3766 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
3767 (u8 *) & a, sizeof (a));
3772 * Take an action with a pending map request depending on expiration time
3773 * and re-try counters.
3776 update_pending_request (pending_map_request_t * r, f64 dt)
3778 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3781 if (r->time_to_expire - dt < 0)
3782 /* it's time to decide what to do with this pending request */
3784 if (r->retries_num >= NUMBER_OF_RETRIES)
3785 /* too many retries -> assume current map resolver is not available */
3787 mr = get_map_resolver (&lcm->active_map_resolver);
3790 clib_warning ("Map resolver %U not found - probably deleted "
3791 "by the user recently.", format_ip_address,
3792 &lcm->active_map_resolver);
3796 clib_warning ("map resolver %U is unreachable, ignoring",
3797 format_ip_address, &lcm->active_map_resolver);
3799 /* mark current map resolver unavailable so it won't be
3800 * selected next time */
3802 mr->last_update = vlib_time_now (lcm->vlib_main);
3805 reset_pending_mr_counters (r);
3806 elect_map_resolver (lcm);
3808 /* try to find a next eligible map resolver and re-send */
3809 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3815 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3818 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3822 r->time_to_expire -= dt;
3826 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
3829 pending_map_request_t *pmr;
3830 u32 *to_be_removed = 0, *pmr_index;
3833 pool_foreach (pmr, lcm->pending_map_requests_pool,
3835 if (pmr->to_be_removed)
3837 clib_fifo_foreach (nonce, pmr->nonces, ({
3838 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
3841 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
3846 vec_foreach (pmr_index, to_be_removed)
3847 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
3849 vec_free (to_be_removed);
3853 update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
3855 static f64 time_left = RLOC_PROBING_INTERVAL;
3857 if (!lcm->is_enabled || !lcm->rloc_probing)
3863 time_left = RLOC_PROBING_INTERVAL;
3864 send_rloc_probes (lcm);
3869 update_map_register (lisp_cp_main_t * lcm, f64 dt)
3871 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
3872 static u64 mreg_sent_counter = 0;
3874 if (!lcm->is_enabled || !lcm->map_registering)
3880 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
3881 time_left = MAP_REGISTER_INTERVAL;
3884 mreg_sent_counter++;
3885 time_left = QUICK_MAP_REGISTER_INTERVAL;
3887 send_map_register (lcm, 1 /* want map notify */ );
3892 send_map_resolver_service (vlib_main_t * vm,
3893 vlib_node_runtime_t * rt, vlib_frame_t * f)
3897 pending_map_request_t *pmr;
3898 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3902 vlib_process_wait_for_event_or_clock (vm, period);
3904 /* currently no signals are expected - just wait for clock */
3905 (void) vlib_process_get_events (vm, 0);
3908 pool_foreach (pmr, lcm->pending_map_requests_pool,
3910 if (!pmr->to_be_removed)
3911 update_pending_request (pmr, period);
3915 remove_dead_pending_map_requests (lcm);
3917 update_map_register (lcm, period);
3918 update_rloc_probing (lcm, period);
3920 u64 now = clib_cpu_time_now ();
3922 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
3923 if (vec_len (expired) > 0)
3926 vec_foreach (mi, expired)
3928 remove_expired_mapping (lcm, mi[0]);
3930 _vec_len (expired) = 0;
3939 vnet_lisp_stats_enable_disable (u8 enable)
3941 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3943 if (vnet_lisp_enable_disable_status () == 0)
3944 return VNET_API_ERROR_LISP_DISABLED;
3947 lcm->flags |= LISP_FLAG_STATS_ENABLED;
3949 lcm->flags &= ~LISP_FLAG_STATS_ENABLED;
3955 vnet_lisp_stats_enable_disable_state (void)
3957 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3959 if (vnet_lisp_enable_disable_status () == 0)
3960 return VNET_API_ERROR_LISP_DISABLED;
3962 return lcm->flags & LISP_FLAG_STATS_ENABLED;
3966 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
3967 .function = send_map_resolver_service,
3968 .type = VLIB_NODE_TYPE_PROCESS,
3969 .name = "lisp-retry-service",
3970 .process_log2_n_stack_bytes = 16,
3974 VLIB_INIT_FUNCTION (lisp_cp_init);
3977 * fd.io coding-style-patch-verification: ON
3980 * eval: (c-set-style "gnu")