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/fib/fib_entry.h>
23 #include <vnet/fib/fib_table.h>
25 #include <openssl/evp.h>
26 #include <openssl/hmac.h>
28 lisp_cp_main_t lisp_control_main;
30 u8 *format_lisp_cp_input_trace (u8 * s, va_list * args);
34 LISP_CP_INPUT_NEXT_DROP,
36 } lisp_cp_input_next_t;
54 vnet_lisp_get_map_request_mode (void)
56 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
57 return lcm->map_request_mode;
61 auth_data_len_by_key_id (lisp_key_type_t key_id)
66 return SHA1_AUTH_DATA_LEN;
67 case HMAC_SHA_256_128:
68 return SHA256_AUTH_DATA_LEN;
70 clib_warning ("unsupported key type: %d!", key_id);
77 get_encrypt_fcn (lisp_key_type_t key_id)
83 case HMAC_SHA_256_128:
86 clib_warning ("unsupported encryption key type: %d!", key_id);
93 queue_map_request (gid_address_t * seid, gid_address_t * deid,
94 u8 smr_invoked, u8 is_resend);
96 ip_interface_address_t *
97 ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
98 u32 sw_if_index, u8 loop)
100 vnet_main_t *vnm = vnet_get_main ();
101 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
102 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
103 sw_if_index = swif->unnumbered_sw_if_index;
105 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
106 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
108 return pool_elt_at_index ((lm)->if_address_pool, ia);
112 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
115 ip_interface_address_t *ia;
117 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
120 return ip_interface_address_get_address (lm, ia);
124 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
125 u8 version, ip_address_t * result)
127 ip_lookup_main_t *lm;
130 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
131 addr = ip_interface_get_first_address (lm, sw_if_index, version);
135 ip_address_set (result, addr, version);
140 * convert from a LISP address to a FIB prefix
143 ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
145 if (addr->version == IP4)
148 prefix->fp_proto = FIB_PROTOCOL_IP4;
149 memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
150 memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
154 prefix->fp_len = 128;
155 prefix->fp_proto = FIB_PROTOCOL_IP6;
156 memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
161 * convert from a LISP to a FIB prefix
164 ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
165 fib_prefix_t * fib_prefix)
167 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
168 fib_prefix->fp_len = ip_prefix->len;
172 * Find the sw_if_index of the interface that would be used to egress towards
176 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
178 fib_node_index_t fei;
181 ip_address_to_fib_prefix (dst, &prefix);
183 fei = fib_table_lookup (0, &prefix);
185 return (fib_entry_get_resolving_interface (fei));
189 * Find first IP of the interface that would be used to egress towards dst.
190 * Returns 1 if the address is found 0 otherwise.
193 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
194 ip_address_t * result)
197 ip_lookup_main_t *lm;
201 ASSERT (result != 0);
203 ipver = ip_addr_version (dst);
205 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
206 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
211 /* find the first ip address */
212 addr = ip_interface_get_first_address (lm, si, ipver);
216 ip_address_set (result, addr, ipver);
221 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
227 dp_table = hash_get (lcm->table_id_by_vni, vni);
231 clib_warning ("vni %d not associated to a vrf!", vni);
232 return VNET_API_ERROR_INVALID_VALUE;
237 dp_table = hash_get (lcm->bd_id_by_vni, vni);
240 clib_warning ("vni %d not associated to a bridge domain!", vni);
241 return VNET_API_ERROR_INVALID_VALUE;
245 /* enable/disable data-plane interface */
249 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
251 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
256 lisp_gpe_tenant_l2_iface_unlock (vni);
258 lisp_gpe_tenant_l3_iface_unlock (vni);
265 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
267 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
270 memset (a, 0, sizeof (*a));
272 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
276 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
278 /* delete dp fwd entry */
281 a->locator_pairs = fe->locator_pairs;
282 a->vni = gid_address_vni (&fe->reid);
283 gid_address_copy (&a->rmt_eid, &fe->reid);
285 gid_address_copy (&a->lcl_eid, &fe->leid);
287 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
289 /* delete entry in fwd table */
290 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
291 vec_free (fe->locator_pairs);
292 pool_put (lcm->fwd_entry_pool, fe);
296 * Finds first remote locator with best (lowest) priority that has a local
297 * peer locator with an underlying route to it.
301 get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
302 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
304 u32 i, limitp = 0, li, found = 0, esi;
305 locator_set_t *rmt_ls, *lcl_ls;
306 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
307 locator_t *lp, *rmt = 0;
312 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
314 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
316 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
323 /* find unvisited remote locator with best priority */
324 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
326 if (0 != hash_get (checked, i))
329 li = vec_elt (rmt_ls->locator_indices, i);
330 lp = pool_elt_at_index (lcm->locator_pool, li);
332 /* we don't support non-IP locators for now */
333 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
336 if ((found && lp->priority == limitp)
337 || (!found && lp->priority >= limitp))
341 /* don't search for locators with lower priority and don't
342 * check this locator again*/
343 limitp = lp->priority;
344 hash_set (checked, i, 1);
348 /* check if a local locator with a route to remote locator exists */
351 /* find egress sw_if_index for rmt locator */
353 ip_fib_get_egress_iface_for_dst (lcm,
354 &gid_address_ip (&rmt->address));
355 if ((u32) ~ 0 == esi)
358 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
360 li = vec_elt (lcl_ls->locator_indices, i);
361 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
363 /* found local locator with the needed sw_if_index */
364 if (sl->sw_if_index == esi)
366 /* and it has an address */
367 if (0 == ip_interface_get_first_ip_address (lcm,
369 gid_address_ip_version
374 memset (&pair, 0, sizeof (pair));
375 ip_address_copy (&pair.rmt_loc,
376 &gid_address_ip (&rmt->address));
377 ip_address_copy (&pair.lcl_loc, lcl_addr);
378 pair.weight = rmt->weight;
379 pair.priority = rmt->priority;
380 vec_add1 (locator_pairs[0], pair);
394 gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
397 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
401 switch (fid_addr_type (fid))
403 case FID_ADDR_IP_PREF:
404 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
405 gid_address_ippref (dst) = fid_addr_ippref (fid);
408 gid_address_type (dst) = GID_ADDR_MAC;
409 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
412 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
418 vnet_lisp_map_register_state_get (void)
420 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
421 return lcm->map_registering;
425 vnet_lisp_rloc_probe_state_get (void)
427 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
428 return lcm->rloc_probing;
432 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
434 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
435 gid_address_t *rmt_eid, *lcl_eid;
436 mapping_t *lcl_map, *rmt_map;
438 uword *feip = 0, *dpid;
440 u8 type, is_src_dst = 0;
443 memset (a, 0, sizeof (*a));
445 /* remove entry if it already exists */
446 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
448 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
451 * Determine local mapping and eid
454 lcl_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
456 lcl_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
457 lcl_eid = &lcl_map->eid;
460 * Determine remote mapping and eid
462 rmt_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
463 rmt_eid = &rmt_map->eid;
466 * Build and insert data plane forwarding entry
470 if (MR_MODE_SRC_DST == lcm->map_request_mode)
472 if (GID_ADDR_SRC_DST == gid_address_type (rmt_eid))
474 gid_address_sd_to_flat (&a->rmt_eid, rmt_eid,
475 &gid_address_sd_dst (rmt_eid));
476 gid_address_sd_to_flat (&a->lcl_eid, rmt_eid,
477 &gid_address_sd_src (rmt_eid));
481 gid_address_copy (&a->rmt_eid, rmt_eid);
482 gid_address_copy (&a->lcl_eid, lcl_eid);
487 gid_address_copy (&a->rmt_eid, rmt_eid);
489 a->vni = gid_address_vni (&a->rmt_eid);
491 /* get vrf or bd_index associated to vni */
492 type = gid_address_type (&a->rmt_eid);
493 if (GID_ADDR_IP_PREFIX == type)
495 dpid = hash_get (lcm->table_id_by_vni, a->vni);
498 clib_warning ("vni %d not associated to a vrf!", a->vni);
501 a->table_id = dpid[0];
503 else if (GID_ADDR_MAC == type)
505 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
508 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
514 /* find best locator pair that 1) verifies LISP policy 2) are connected */
515 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
517 /* Either rmt mapping is negative or we can't find underlay path.
518 * Try again with petr if configured */
519 if (rv == 0 && (lcm->flags & LISP_FLAG_USE_PETR))
521 rmt_map = lisp_get_petr_mapping (lcm);
522 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
529 a->action = rmt_map->action;
532 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
534 /* add tunnel to fwd entry table XXX check return value from DP insertion */
535 pool_get (lcm->fwd_entry_pool, fe);
536 fe->locator_pairs = a->locator_pairs;
537 gid_address_copy (&fe->reid, &a->rmt_eid);
540 gid_address_copy (&fe->leid, &a->lcl_eid);
542 gid_address_copy (&fe->leid, lcl_eid);
544 fe->is_src_dst = is_src_dst;
545 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
546 fe - lcm->fwd_entry_pool);
553 } fwd_entry_mt_arg_t;
556 dp_add_fwd_entry_thread_fn (void *arg)
558 fwd_entry_mt_arg_t *a = arg;
559 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
560 dp_add_fwd_entry (lcm, a->si, a->di);
565 dp_add_fwd_entry_from_mt (u32 si, u32 di)
567 fwd_entry_mt_arg_t a;
569 memset (&a, 0, sizeof (a));
573 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
574 (u8 *) & a, sizeof (a));
579 * Returns vector of adjacencies.
581 * The caller must free the vector returned by this function.
583 * @param vni virtual network identifier
584 * @return vector of adjacencies
587 vnet_lisp_adjacencies_get_by_vni (u32 vni)
589 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
591 lisp_adjacency_t *adjs = 0, adj;
594 pool_foreach(fwd, lcm->fwd_entry_pool,
596 if (gid_address_vni (&fwd->reid) != vni)
599 gid_address_copy (&adj.reid, &fwd->reid);
600 gid_address_copy (&adj.leid, &fwd->leid);
601 vec_add1 (adjs, adj);
609 get_map_server (ip_address_t * a)
611 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
614 vec_foreach (m, lcm->map_servers)
616 if (!ip_address_cmp (&m->address, a))
625 get_map_resolver (ip_address_t * a)
627 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
630 vec_foreach (m, lcm->map_resolvers)
632 if (!ip_address_cmp (&m->address, a))
641 vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
644 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
645 lisp_msmr_t _ms, *ms = &_ms;
647 if (vnet_lisp_enable_disable_status () == 0)
649 clib_warning ("LISP is disabled!");
650 return VNET_API_ERROR_LISP_DISABLED;
655 if (get_map_server (addr))
657 clib_warning ("map-server %U already exists!", format_ip_address,
662 memset (ms, 0, sizeof (*ms));
663 ip_address_copy (&ms->address, addr);
664 vec_add1 (lcm->map_servers, ms[0]);
668 for (i = 0; i < vec_len (lcm->map_servers); i++)
670 ms = vec_elt_at_index (lcm->map_servers, i);
671 if (!ip_address_cmp (&ms->address, addr))
673 vec_del1 (lcm->map_servers, i);
683 * Add/remove mapping to/from map-cache. Overwriting not allowed.
686 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
687 u32 * map_index_result)
689 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
690 u32 mi, *map_indexp, map_index, i;
691 mapping_t *m, *old_map;
694 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
695 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
698 /* TODO check if overwriting and take appropriate actions */
699 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
701 clib_warning ("eid %U found in the eid-table", format_gid_address,
703 return VNET_API_ERROR_VALUE_EXIST;
706 pool_get (lcm->mapping_pool, m);
707 gid_address_copy (&m->eid, &a->eid);
708 m->locator_set_index = a->locator_set_index;
710 m->action = a->action;
712 m->is_static = a->is_static;
713 m->key = vec_dup (a->key);
714 m->key_id = a->key_id;
716 map_index = m - lcm->mapping_pool;
717 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
720 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
722 clib_warning ("Locator set with index %d doesn't exist",
723 a->locator_set_index);
724 return VNET_API_ERROR_INVALID_VALUE;
727 /* add eid to list of eids supported by locator-set */
728 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
729 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
730 a->locator_set_index);
731 vec_add1 (eid_indexes[0], map_index);
736 vec_add1 (lcm->local_mappings_indexes, map_index);
738 map_index_result[0] = map_index;
742 if (mi == GID_LOOKUP_MISS)
744 clib_warning ("eid %U not found in the eid-table",
745 format_gid_address, &a->eid);
746 return VNET_API_ERROR_INVALID_VALUE;
749 /* clear locator-set to eids binding */
750 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
751 a->locator_set_index);
752 for (i = 0; i < vec_len (eid_indexes[0]); i++)
754 map_indexp = vec_elt_at_index (eid_indexes[0], i);
755 if (map_indexp[0] == mi)
758 vec_del1 (eid_indexes[0], i);
760 /* remove local mark if needed */
761 m = pool_elt_at_index (lcm->mapping_pool, mi);
765 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
767 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
768 if (lm_indexp[0] == mi)
771 vec_del1 (lcm->local_mappings_indexes, k);
774 /* remove mapping from dictionary */
775 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
776 gid_address_free (&m->eid);
777 pool_put_index (lcm->mapping_pool, mi);
784 * Add/update/delete mapping to/in/from map-cache.
787 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
788 u32 * map_index_result)
794 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
796 if (vnet_lisp_enable_disable_status () == 0)
798 clib_warning ("LISP is disabled!");
799 return VNET_API_ERROR_LISP_DISABLED;
802 vni = gid_address_vni (&a->eid);
803 type = gid_address_type (&a->eid);
804 if (GID_ADDR_IP_PREFIX == type)
805 dp_table = hash_get (lcm->table_id_by_vni, vni);
806 else if (GID_ADDR_MAC == type)
807 dp_table = hash_get (lcm->bd_id_by_vni, vni);
811 clib_warning ("vni %d not associated to a %s!", vni,
812 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
813 return VNET_API_ERROR_INVALID_VALUE;
816 /* store/remove mapping from map-cache */
817 return vnet_lisp_map_cache_add_del (a, map_index_result);
821 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
823 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
824 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
826 if (vnet_lisp_enable_disable_status () == 0)
828 clib_warning ("LISP is disabled!");
832 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
833 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
835 if (!is_l2 && (vni == 0 || dp_id == 0))
837 clib_warning ("can't add/del default vni-vrf mapping!");
841 dp_idp = hash_get (dp_table_by_vni[0], vni);
842 vnip = hash_get (vni_by_dp_table[0], dp_id);
848 clib_warning ("vni %d or vrf %d already used in vrf/vni "
849 "mapping!", vni, dp_id);
852 hash_set (dp_table_by_vni[0], vni, dp_id);
853 hash_set (vni_by_dp_table[0], dp_id, vni);
855 /* create dp iface */
856 dp_add_del_iface (lcm, vni, is_l2, 1);
860 if (!dp_idp || !vnip)
862 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
863 "mapping!", vni, dp_id);
866 hash_unset (dp_table_by_vni[0], vni);
867 hash_unset (vni_by_dp_table[0], dp_id);
869 /* remove dp iface */
870 dp_add_del_iface (lcm, vni, is_l2, 0);
876 /* return 0 if the two locator sets are identical 1 otherwise */
878 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
879 locator_t * new_locators)
882 locator_t *old_loc, *new_loc;
884 if (vec_len (old_ls_indexes) != vec_len (new_locators))
887 for (i = 0; i < vec_len (new_locators); i++)
889 old_li = vec_elt (old_ls_indexes, i);
890 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
892 new_loc = vec_elt_at_index (new_locators, i);
894 if (locator_cmp (old_loc, new_loc))
904 gid_address_t *eids_to_be_deleted;
905 } remove_mapping_args_t;
908 * Callback invoked when a sub-prefix is found
911 remove_mapping_if_needed (u32 mi, void *arg)
914 remove_mapping_args_t *a = arg;
915 lisp_cp_main_t *lcm = a->lcm;
919 m = pool_elt_at_index (lcm->mapping_pool, mi);
923 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
927 if (0 != vec_len (ls->locator_indices))
932 if (0 == vec_len (ls->locator_indices))
937 vec_add1 (a->eids_to_be_deleted, m->eid);
941 * This function searches map cache and looks for IP prefixes that are subset
942 * of the provided one. If such prefix is found depending on 'is_negative'
945 * 1) if is_negative is true and found prefix points to positive mapping,
946 * then the mapping is removed
947 * 2) if is_negative is false and found prefix points to negative mapping,
948 * then the mapping is removed
951 remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
955 remove_mapping_args_t a;
957 memset (&a, 0, sizeof (a));
959 /* do this only in src/dst mode ... */
960 if (MR_MODE_SRC_DST != lcm->map_request_mode)
963 /* ... and only for IP prefix */
964 if (GID_ADDR_SRC_DST != gid_address_type (eid)
965 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
968 a.is_negative = is_negative;
971 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
972 remove_mapping_if_needed, &a);
974 vec_foreach (e, a.eids_to_be_deleted)
976 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
978 memset (adj_args, 0, sizeof (adj_args[0]));
979 gid_address_copy (&adj_args->reid, e);
980 adj_args->is_add = 0;
981 if (vnet_lisp_add_del_adjacency (adj_args))
982 clib_warning ("failed to del adjacency!");
984 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
987 vec_free (a.eids_to_be_deleted);
991 mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
993 timing_wheel_delete (&lcm->wheel, mi);
997 is_local_ip (lisp_cp_main_t * lcm, ip_address_t * addr)
999 fib_node_index_t fei;
1000 fib_prefix_t prefix;
1001 fib_entry_flag_t flags;
1003 ip_address_to_fib_prefix (addr, &prefix);
1005 fei = fib_table_lookup (0, &prefix);
1006 flags = fib_entry_get_flags (fei);
1007 return (FIB_ENTRY_FLAG_LOCAL & flags);
1011 * Adds/removes/updates mapping. Does not program forwarding.
1013 * @param eid end-host identifier
1014 * @param rlocs vector of remote locators
1015 * @param action action for negative map-reply
1016 * @param is_add add mapping if non-zero, delete otherwise
1017 * @param res_map_index the map-index that was created/updated/removed. It is
1018 * set to ~0 if no action is taken.
1019 * @param is_static used for distinguishing between statically learned
1020 remote mappings and mappings obtained from MR
1021 * @return return code
1024 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
1025 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
1026 u32 * res_map_index)
1028 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1029 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1030 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1031 u32 mi, ls_index = 0, dst_map_index;
1035 if (vnet_lisp_enable_disable_status () == 0)
1037 clib_warning ("LISP is disabled!");
1038 return VNET_API_ERROR_LISP_DISABLED;
1042 res_map_index[0] = ~0;
1044 memset (m_args, 0, sizeof (m_args[0]));
1045 memset (ls_args, 0, sizeof (ls_args[0]));
1047 ls_args->locators = rlocs;
1049 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
1050 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1054 /* check if none of the locators match localy configured address */
1055 vec_foreach (loc, rlocs)
1057 ip_prefix_t *p = &gid_address_ippref (&loc->address);
1058 if (is_local_ip (lcm, &ip_prefix_addr (p)))
1060 clib_warning ("RLOC %U matches a local address!",
1061 format_gid_address, &loc->address);
1062 return VNET_API_ERROR_LISP_RLOC_LOCAL;
1066 /* overwrite: if mapping already exists, decide if locators should be
1067 * updated and be done */
1068 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
1070 if (!is_static && (old_map->is_static || old_map->local))
1072 /* do not overwrite local or static remote mappings */
1073 clib_warning ("mapping %U rejected due to collision with local "
1074 "or static remote mapping!", format_gid_address,
1079 locator_set_t *old_ls;
1081 /* update mapping attributes */
1082 old_map->action = action;
1083 old_map->authoritative = authoritative;
1086 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1087 old_map->locator_set_index);
1088 if (compare_locators (lcm, old_ls->locator_indices,
1091 /* set locator-set index to overwrite */
1092 ls_args->is_add = 1;
1093 ls_args->index = old_map->locator_set_index;
1094 vnet_lisp_add_del_locator_set (ls_args, 0);
1096 res_map_index[0] = mi;
1102 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1104 ls_args->is_add = 1;
1105 ls_args->index = ~0;
1107 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1110 gid_address_copy (&m_args->eid, eid);
1112 m_args->action = action;
1113 m_args->locator_set_index = ls_index;
1114 m_args->is_static = is_static;
1116 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
1119 res_map_index[0] = dst_map_index;
1124 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
1126 clib_warning ("cannot delete mapping for eid %U",
1127 format_gid_address, eid);
1132 gid_address_copy (&m_args->eid, eid);
1133 m_args->locator_set_index = old_map->locator_set_index;
1135 /* delete mapping associated from map-cache */
1136 vnet_lisp_map_cache_add_del (m_args, 0);
1138 ls_args->is_add = 0;
1139 ls_args->index = old_map->locator_set_index;
1140 /* delete locator set */
1141 vnet_lisp_add_del_locator_set (ls_args, 0);
1143 /* delete timer associated to the mapping if any */
1144 if (old_map->timer_set)
1145 mapping_delete_timer (lcm, mi);
1147 /* return old mapping index */
1149 res_map_index[0] = mi;
1157 vnet_lisp_clear_all_remote_adjacencies (void)
1160 u32 mi, *map_indices = 0, *map_indexp;
1161 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1162 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1163 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1166 pool_foreach_index (mi, lcm->mapping_pool,
1168 vec_add1 (map_indices, mi);
1172 vec_foreach (map_indexp, map_indices)
1174 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1177 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
1179 dm_args->is_add = 0;
1180 gid_address_copy (&dm_args->eid, &map->eid);
1181 dm_args->locator_set_index = map->locator_set_index;
1183 /* delete mapping associated to fwd entry */
1184 vnet_lisp_map_cache_add_del (dm_args, 0);
1188 ls->index = map->locator_set_index;
1189 /* delete locator set */
1190 rv = vnet_lisp_add_del_locator_set (ls, 0);
1198 vec_free (map_indices);
1203 * Adds adjacency or removes forwarding entry associated to remote mapping.
1204 * Note that adjacencies are not stored, they only result in forwarding entries
1208 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1210 lisp_cp_main_t *lcm = &lisp_control_main;
1211 u32 local_mi, remote_mi = ~0;
1213 if (vnet_lisp_enable_disable_status () == 0)
1215 clib_warning ("LISP is disabled!");
1216 return VNET_API_ERROR_LISP_DISABLED;
1219 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1220 &a->reid, &a->leid);
1221 if (GID_LOOKUP_MISS == remote_mi)
1223 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1224 format_gid_address, &a->reid);
1231 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1234 /* check if source eid has an associated mapping. If pitr mode is on,
1235 * just use the pitr's mapping */
1236 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
1237 gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->leid);
1239 if (GID_LOOKUP_MISS == local_mi)
1241 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1242 format_gid_address, &a->leid);
1247 /* update forwarding */
1248 dp_add_fwd_entry (lcm, local_mi, remote_mi);
1251 dp_del_fwd_entry (lcm, 0, remote_mi);
1257 vnet_lisp_set_map_request_mode (u8 mode)
1259 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1261 if (vnet_lisp_enable_disable_status () == 0)
1263 clib_warning ("LISP is disabled!");
1264 return VNET_API_ERROR_LISP_DISABLED;
1267 if (mode >= _MR_MODE_MAX)
1269 clib_warning ("Invalid LISP map request mode %d!", mode);
1270 return VNET_API_ERROR_INVALID_ARGUMENT;
1273 lcm->map_request_mode = mode;
1278 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1280 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1281 u32 locator_set_index = ~0;
1285 if (vnet_lisp_enable_disable_status () == 0)
1287 clib_warning ("LISP is disabled!");
1288 return VNET_API_ERROR_LISP_DISABLED;
1291 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1294 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1297 locator_set_index = p[0];
1301 pool_get (lcm->mapping_pool, m);
1302 m->locator_set_index = locator_set_index;
1304 lcm->pitr_map_index = m - lcm->mapping_pool;
1306 /* enable pitr mode */
1311 /* remove pitr mapping */
1312 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1314 /* disable pitr mode */
1321 * Configure Proxy-ETR
1323 * @param ip PETR's IP address
1324 * @param is_add Flag that indicates if this is an addition or removal
1326 * return 0 on success
1329 vnet_lisp_use_petr (ip_address_t * ip, u8 is_add)
1331 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1334 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1337 if (vnet_lisp_enable_disable_status () == 0)
1339 clib_warning ("LISP is disabled!");
1340 return VNET_API_ERROR_LISP_DISABLED;
1343 memset (ls_args, 0, sizeof (*ls_args));
1347 /* Create dummy petr locator-set */
1348 gid_address_from_ip (&loc.address, ip);
1350 loc.state = loc.weight = 1;
1353 ls_args->is_add = 1;
1354 ls_args->index = ~0;
1355 vec_add1 (ls_args->locators, loc);
1356 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1358 /* Add petr mapping */
1359 pool_get (lcm->mapping_pool, m);
1360 m->locator_set_index = ls_index;
1361 lcm->petr_map_index = m - lcm->mapping_pool;
1363 /* Enable use-petr */
1364 lcm->flags |= LISP_FLAG_USE_PETR;
1368 m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1370 /* Remove petr locator */
1371 ls_args->is_add = 0;
1372 ls_args->index = m->locator_set_index;
1373 vnet_lisp_add_del_locator_set (ls_args, 0);
1375 /* Remove petr mapping */
1376 pool_put_index (lcm->mapping_pool, lcm->petr_map_index);
1378 /* Disable use-petr */
1379 lcm->flags &= ~LISP_FLAG_USE_PETR;
1384 /* cleans locator to locator-set data and removes locators not part of
1385 * any locator-set */
1387 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1389 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
1390 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1391 for (i = 0; i < vec_len (ls->locator_indices); i++)
1393 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1394 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1396 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1398 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1399 if (ls_indexp[0] == lsi)
1403 /* delete index for removed locator-set */
1404 vec_del1 (ls_indexes[0], j);
1406 /* delete locator if it's part of no locator-set */
1407 if (vec_len (ls_indexes[0]) == 0)
1409 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1410 vec_add1 (to_be_deleted, i);
1416 for (i = 0; i < vec_len (to_be_deleted); i++)
1418 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1419 vec_del1 (ls->locator_indices, loc_indexp[0]);
1421 vec_free (to_be_deleted);
1425 static inline uword *
1426 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
1428 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1433 /* find locator-set */
1436 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
1447 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
1453 ASSERT (ls != NULL);
1454 ASSERT (loc != NULL);
1456 vec_foreach (locit, ls->locator_indices)
1458 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1459 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1460 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1462 clib_warning ("Duplicate locator");
1463 return VNET_API_ERROR_VALUE_EXIST;
1471 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1472 u32 ls_index, u32 loc_id)
1474 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1475 u32 **ls_indexes = NULL;
1477 ASSERT (ls != NULL);
1478 ASSERT (locit != NULL);
1480 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1481 pool_put_index (lcm->locator_pool, locit[0]);
1482 vec_del1 (ls->locator_indices, loc_id);
1483 vec_del1 (ls_indexes[0], ls_index);
1487 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1488 locator_set_t * ls, u32 * ls_result)
1490 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1491 locator_t *loc = NULL, *itloc = NULL;
1492 uword _p = (u32) ~ 0, *p = &_p;
1493 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
1499 if (vnet_lisp_enable_disable_status () == 0)
1501 clib_warning ("LISP is disabled!");
1502 return VNET_API_ERROR_LISP_DISABLED;
1505 p = get_locator_set_index (a, p);
1508 clib_warning ("locator-set %v doesn't exist", a->name);
1509 return VNET_API_ERROR_INVALID_ARGUMENT;
1514 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1517 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1519 return VNET_API_ERROR_INVALID_ARGUMENT;
1526 ls_result[0] = p[0];
1528 /* allocate locators */
1529 vec_foreach (itloc, a->locators)
1531 ret = is_locator_in_locator_set (lcm, ls, itloc);
1537 pool_get (lcm->locator_pool, loc);
1539 loc_index = loc - lcm->locator_pool;
1541 vec_add1 (ls->locator_indices, loc_index);
1543 vec_validate (lcm->locator_to_locator_sets, loc_index);
1544 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1546 vec_add1 (ls_indexes[0], p[0]);
1553 itloc = a->locators;
1555 vec_foreach (locit, ls->locator_indices)
1557 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1559 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1561 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1563 if (0 == loc->local &&
1564 !gid_address_cmp (&loc->address, &itloc->address))
1566 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1577 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
1580 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1582 uword _p = (u32) ~ 0, *p = &_p;
1587 if (vnet_lisp_enable_disable_status () == 0)
1589 clib_warning ("LISP is disabled!");
1590 return VNET_API_ERROR_LISP_DISABLED;
1595 p = get_locator_set_index (a, p);
1598 if (p && p[0] != (u32) ~ 0)
1600 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1603 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1608 /* clean locator to locator-set vectors and remove locators if
1609 * they're not part of another locator-set */
1610 clean_locator_to_locator_set (lcm, p[0]);
1612 /* remove locator indices from locator set */
1613 vec_free (ls->locator_indices);
1618 ls_result[0] = p[0];
1620 /* new locator-set */
1623 pool_get (lcm->locator_set_pool, ls);
1624 memset (ls, 0, sizeof (*ls));
1625 ls_index = ls - lcm->locator_set_pool;
1629 ls->name = vec_dup (a->name);
1631 if (!lcm->locator_set_index_by_name)
1632 lcm->locator_set_index_by_name = hash_create_vec (
1640 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
1643 /* mark as local locator-set */
1644 vec_add1 (lcm->local_locator_set_indexes, ls_index);
1646 ls->local = a->local;
1648 ls_result[0] = ls_index;
1651 ret = vnet_lisp_add_del_locator (a, ls, NULL);
1659 p = get_locator_set_index (a, p);
1662 clib_warning ("locator-set %v doesn't exists", a->name);
1666 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1669 clib_warning ("locator-set with index %d doesn't exists", p[0]);
1673 if (lcm->mreq_itr_rlocs == p[0])
1675 clib_warning ("Can't delete the locator-set used to constrain "
1676 "the itr-rlocs in map-requests!");
1680 if (vec_len (lcm->locator_set_to_eids) != 0)
1682 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
1683 if (vec_len (eid_indexes[0]) != 0)
1686 ("Can't delete a locator that supports a mapping!");
1691 /* clean locator to locator-sets data */
1692 clean_locator_to_locator_set (lcm, p[0]);
1698 vec_foreach_index (it, lcm->local_locator_set_indexes)
1700 lsi = vec_elt (lcm->local_locator_set_indexes, it);
1703 vec_del1 (lcm->local_locator_set_indexes, it);
1707 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
1709 vec_free (ls->name);
1710 vec_free (ls->locator_indices);
1711 pool_put (lcm->locator_set_pool, ls);
1717 vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
1719 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1721 lcm->rloc_probing = is_enable;
1726 vnet_lisp_map_register_enable_disable (u8 is_enable)
1728 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1730 lcm->map_registering = is_enable;
1735 vnet_lisp_enable_disable (u8 is_enable)
1738 clib_error_t *error = 0;
1739 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1740 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
1742 a->is_en = is_enable;
1743 error = vnet_lisp_gpe_enable_disable (a);
1746 return clib_error_return (0, "failed to %s data-plane!",
1747 a->is_en ? "enable" : "disable");
1752 /* enable all l2 and l3 ifaces */
1755 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
1756 dp_add_del_iface(lcm, vni, 0, 1);
1758 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
1759 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
1765 /* clear interface table */
1766 hash_free (lcm->fwd_entry_by_mapping_index);
1767 pool_free (lcm->fwd_entry_pool);
1770 /* update global flag */
1771 lcm->is_enabled = is_enable;
1777 vnet_lisp_enable_disable_status (void)
1779 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1780 return lcm->is_enabled;
1784 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
1786 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1788 lisp_msmr_t _mr, *mr = &_mr;
1790 if (vnet_lisp_enable_disable_status () == 0)
1792 clib_warning ("LISP is disabled!");
1793 return VNET_API_ERROR_LISP_DISABLED;
1799 if (get_map_resolver (&a->address))
1801 clib_warning ("map-resolver %U already exists!", format_ip_address,
1806 memset (mr, 0, sizeof (*mr));
1807 ip_address_copy (&mr->address, &a->address);
1808 vec_add1 (lcm->map_resolvers, *mr);
1810 if (vec_len (lcm->map_resolvers) == 1)
1811 lcm->do_map_resolver_election = 1;
1815 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
1817 mr = vec_elt_at_index (lcm->map_resolvers, i);
1818 if (!ip_address_cmp (&mr->address, &a->address))
1820 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
1821 lcm->do_map_resolver_election = 1;
1823 vec_del1 (lcm->map_resolvers, i);
1832 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
1834 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1837 if (vnet_lisp_enable_disable_status () == 0)
1839 clib_warning ("LISP is disabled!");
1840 return VNET_API_ERROR_LISP_DISABLED;
1845 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
1848 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
1849 return VNET_API_ERROR_INVALID_ARGUMENT;
1852 lcm->mreq_itr_rlocs = p[0];
1856 lcm->mreq_itr_rlocs = ~0;
1862 /* Statistics (not really errors) */
1863 #define foreach_lisp_cp_lookup_error \
1865 _(MAP_REQUESTS_SENT, "map-request sent")
1867 static char *lisp_cp_lookup_error_strings[] = {
1868 #define _(sym,string) string,
1869 foreach_lisp_cp_lookup_error
1875 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
1876 foreach_lisp_cp_lookup_error
1878 LISP_CP_LOOKUP_N_ERROR,
1879 } lisp_cp_lookup_error_t;
1883 LISP_CP_LOOKUP_NEXT_DROP,
1884 LISP_CP_LOOKUP_N_NEXT,
1885 } lisp_cp_lookup_next_t;
1889 gid_address_t dst_eid;
1890 ip_address_t map_resolver_ip;
1891 } lisp_cp_lookup_trace_t;
1894 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
1896 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1897 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1898 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
1900 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
1901 format_ip_address, &t->map_resolver_ip, format_gid_address,
1907 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
1908 ip_address_t * sloc)
1913 if (vec_len (lcm->map_resolvers) == 0)
1915 clib_warning ("No map-resolver configured");
1919 /* find the first mr ip we have a route to and the ip of the
1920 * iface that has a route to it */
1921 vec_foreach (mrit, lcm->map_resolvers)
1924 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
1926 ip_address_copy (mr_ip, a);
1928 /* also update globals */
1933 clib_warning ("Can't find map-resolver and local interface ip!");
1937 static gid_address_t *
1938 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
1944 ip_interface_address_t *ia = 0;
1945 gid_address_t gid_data, *gid = &gid_data;
1946 gid_address_t *rlocs = 0;
1947 ip_prefix_t *ippref = &gid_address_ippref (gid);
1948 ip_address_t *rloc = &ip_prefix_addr (ippref);
1950 memset (gid, 0, sizeof (gid[0]));
1951 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
1952 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
1954 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
1955 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
1957 /* Add ipv4 locators first TODO sort them */
1960 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
1961 loc->sw_if_index, 1 /* unnumbered */,
1963 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
1964 ip_address_set (rloc, addr, IP4);
1965 ip_prefix_len (ippref) = 32;
1966 ip_prefix_normalize (ippref);
1967 vec_add1 (rlocs, gid[0]);
1970 /* Add ipv6 locators */
1971 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
1972 loc->sw_if_index, 1 /* unnumbered */,
1974 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
1975 ip_address_set (rloc, addr, IP6);
1976 ip_prefix_len (ippref) = 128;
1977 ip_prefix_normalize (ippref);
1978 vec_add1 (rlocs, gid[0]);
1986 static vlib_buffer_t *
1987 build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
1988 ip_address_t * sloc, ip_address_t * rloc,
1989 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
1993 vlib_main_t *vm = lcm->vlib_main;
1995 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
1997 clib_warning ("Can't allocate buffer for Map-Request!");
2001 b = vlib_get_buffer (vm, bi);
2003 /* leave some space for the encap headers */
2004 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2007 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
2008 1 /* rloc probe */ , nonce_res);
2010 /* push outer ip header */
2011 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2019 static vlib_buffer_t *
2020 build_encapsulated_map_request (lisp_cp_main_t * lcm,
2021 gid_address_t * seid, gid_address_t * deid,
2022 locator_set_t * loc_set, ip_address_t * mr_ip,
2023 ip_address_t * sloc, u8 is_smr_invoked,
2024 u64 * nonce_res, u32 * bi_res)
2028 gid_address_t *rlocs = 0;
2029 vlib_main_t *vm = lcm->vlib_main;
2031 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2033 clib_warning ("Can't allocate buffer for Map-Request!");
2037 b = vlib_get_buffer (vm, bi);
2039 /* leave some space for the encap headers */
2040 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2043 rlocs = build_itr_rloc_list (lcm, loc_set);
2045 if (MR_MODE_SRC_DST == lcm->map_request_mode
2046 && GID_ADDR_SRC_DST != gid_address_type (deid))
2049 memset (&sd, 0, sizeof (sd));
2050 build_src_dst (&sd, seid, deid);
2051 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
2052 0 /* rloc probe */ , nonce_res);
2057 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
2058 0 /* rloc probe */ , nonce_res);
2061 /* push ecm: udp-ip-lisp */
2062 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2064 /* push outer ip header */
2065 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2075 reset_pending_mr_counters (pending_map_request_t * r)
2077 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2082 elect_map_resolver (lisp_cp_main_t * lcm)
2086 vec_foreach (mr, lcm->map_resolvers)
2090 ip_address_copy (&lcm->active_map_resolver, &mr->address);
2091 lcm->do_map_resolver_election = 0;
2099 free_map_register_records (mapping_t * maps)
2102 vec_foreach (map, maps) vec_free (map->locators);
2108 add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
2109 ip_address_t * probed_loc)
2112 locator_t *loc, new;
2113 ip_interface_address_t *ia = 0;
2115 ip_address_t *new_ip = &gid_address_ip (&new.address);
2118 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2120 vec_foreach (li, ls->locator_indices)
2122 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
2127 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2128 loc->sw_if_index, 1 /* unnumbered */,
2130 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
2132 ip_address_set (new_ip, addr, IP4);
2135 /* Add ipv6 locators */
2136 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2137 loc->sw_if_index, 1 /* unnumbered */,
2139 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
2141 ip_address_set (new_ip, addr, IP6);
2145 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
2148 vec_add1 (m->locators, new);
2153 build_map_register_record_list (lisp_cp_main_t * lcm)
2155 mapping_t *recs = 0, rec, *m;
2158 pool_foreach(m, lcm->mapping_pool,
2160 /* for now build only local mappings */
2165 add_locators (lcm, &rec, m->locator_set_index, NULL);
2166 vec_add1 (recs, rec);
2174 update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
2175 lisp_key_type_t key_id, u8 * key,
2176 u16 auth_data_len, u32 msg_len)
2178 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
2179 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
2181 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
2182 (unsigned char *) map_reg_hdr, msg_len, NULL,
2184 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
2189 static vlib_buffer_t *
2190 build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
2191 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
2192 mapping_t * records, lisp_key_type_t key_id, u8 * key,
2197 u32 bi, auth_data_len = 0, msg_len = 0;
2198 vlib_main_t *vm = lcm->vlib_main;
2200 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2202 clib_warning ("Can't allocate buffer for Map-Register!");
2206 b = vlib_get_buffer (vm, bi);
2208 /* leave some space for the encap headers */
2209 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2211 auth_data_len = auth_data_len_by_key_id (key_id);
2212 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
2213 auth_data_len, nonce_res,
2216 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
2219 /* push outer ip header */
2220 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2228 get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
2231 while (lcm->do_map_resolver_election
2232 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
2233 &lcm->active_map_resolver,
2236 if (0 == elect_map_resolver (lcm))
2237 /* all map resolvers are down */
2239 /* restart MR checking by marking all of them up */
2240 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
2247 /* CP output statistics */
2248 #define foreach_lisp_cp_output_error \
2249 _(MAP_REGISTERS_SENT, "map-registers sent") \
2250 _(RLOC_PROBES_SENT, "rloc-probes sent")
2252 static char *lisp_cp_output_error_strings[] = {
2253 #define _(sym,string) string,
2254 foreach_lisp_cp_output_error
2260 #define _(sym,str) LISP_CP_OUTPUT_ERROR_##sym,
2261 foreach_lisp_cp_output_error
2263 LISP_CP_OUTPUT_N_ERROR,
2264 } lisp_cp_output_error_t;
2267 lisp_cp_output (vlib_main_t * vm, vlib_node_runtime_t * node,
2268 vlib_frame_t * from_frame)
2273 /* dummy node used only for statistics */
2275 VLIB_REGISTER_NODE (lisp_cp_output_node) = {
2276 .function = lisp_cp_output,
2277 .name = "lisp-cp-output",
2278 .vector_size = sizeof (u32),
2279 .format_trace = format_lisp_cp_input_trace,
2280 .type = VLIB_NODE_TYPE_INTERNAL,
2282 .n_errors = LISP_CP_OUTPUT_N_ERROR,
2283 .error_strings = lisp_cp_output_error_strings,
2285 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2288 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2294 send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
2295 u32 local_locator_set_index, ip_address_t * sloc,
2296 ip_address_t * rloc)
2303 u32 next_index, *to_next;
2304 gid_address_t *itr_rlocs;
2306 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
2307 itr_rlocs = build_itr_rloc_list (lcm, ls);
2309 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
2310 vec_free (itr_rlocs);
2314 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2316 next_index = (ip_addr_version (rloc) == IP4) ?
2317 ip4_lookup_node.index : ip6_lookup_node.index;
2319 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2321 /* Enqueue the packet */
2322 to_next = vlib_frame_vector_args (f);
2325 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2327 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2332 send_rloc_probes (lisp_cp_main_t * lcm)
2338 u32 si, rloc_probes_sent = 0;
2341 pool_foreach (e, lcm->fwd_entry_pool,
2343 if (vec_len (e->locator_pairs) == 0)
2346 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
2349 clib_warning ("internal error: cannot find local eid %U in "
2350 "map-cache!", format_gid_address, &e->leid);
2353 lm = pool_elt_at_index (lcm->mapping_pool, si);
2355 /* get the best (lowest) priority */
2356 lprio = e->locator_pairs[0].priority;
2358 /* send rloc-probe for pair(s) with the best remote locator priority */
2359 vec_foreach (lp, e->locator_pairs)
2361 if (lp->priority != lprio)
2364 /* get first remote locator */
2365 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
2372 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2373 LISP_CP_OUTPUT_ERROR_RLOC_PROBES_SENT,
2379 send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
2381 u32 bi, map_registers_sent = 0;
2386 u32 next_index, *to_next;
2387 ip_address_t *ms = 0;
2388 mapping_t *records, *r, *g;
2390 // TODO: support multiple map servers and do election
2391 if (0 == vec_len (lcm->map_servers))
2394 ms = &lcm->map_servers[0].address;
2396 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
2398 clib_warning ("no eligible interface address found for %U!",
2399 format_ip_address, &lcm->map_servers[0]);
2403 records = build_map_register_record_list (lcm);
2407 vec_foreach (r, records)
2410 u8 key_id = r->key_id;
2413 continue; /* no secret key -> map-register cannot be sent */
2416 // TODO: group mappings that share common key
2418 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, g,
2424 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2426 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2427 ip4_lookup_node.index : ip6_lookup_node.index;
2429 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2431 /* Enqueue the packet */
2432 to_next = vlib_frame_vector_args (f);
2435 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2436 map_registers_sent++;
2438 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2440 free_map_register_records (records);
2442 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2443 LISP_CP_OUTPUT_ERROR_MAP_REGISTERS_SENT,
2444 map_registers_sent);
2449 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
2450 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2452 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2453 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2456 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
2457 gid_address_t * seid, gid_address_t * deid,
2458 u8 is_smr_invoked, u8 is_resend)
2460 u32 next_index, bi = 0, *to_next, map_index;
2464 locator_set_t *loc_set;
2466 pending_map_request_t *pmr, *duplicate_pmr = 0;
2470 /* if there is already a pending request remember it */
2473 pool_foreach(pmr, lcm->pending_map_requests_pool,
2475 if (!gid_address_cmp (&pmr->src, seid)
2476 && !gid_address_cmp (&pmr->dst, deid))
2478 duplicate_pmr = pmr;
2484 if (!is_resend && duplicate_pmr)
2486 /* don't send the request if there is a pending map request already */
2490 /* get locator-set for seid */
2491 if (!lcm->lisp_pitr)
2493 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2494 if (map_index == ~0)
2496 clib_warning ("No local mapping found in eid-table for %U!",
2497 format_gid_address, seid);
2501 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2506 ("Mapping found for src eid %U is not marked as local!",
2507 format_gid_address, seid);
2510 ls_index = map->locator_set_index;
2514 map_index = lcm->pitr_map_index;
2515 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
2516 ls_index = map->locator_set_index;
2519 /* overwrite locator set if map-request itr-rlocs configured */
2520 if (~0 != lcm->mreq_itr_rlocs)
2522 ls_index = lcm->mreq_itr_rlocs;
2525 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
2527 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
2530 duplicate_pmr->to_be_removed = 1;
2534 /* build the encapsulated map request */
2535 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
2536 &lcm->active_map_resolver,
2537 &sloc, is_smr_invoked, &nonce, &bi);
2542 /* set fib index to default and lookup node */
2543 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2544 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2545 ip4_lookup_node.index : ip6_lookup_node.index;
2547 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2549 /* Enqueue the packet */
2550 to_next = vlib_frame_vector_args (f);
2553 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2556 /* if there is a pending request already update it */
2558 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
2560 /* remove the oldest nonce */
2561 u64 CLIB_UNUSED (tmp), *nonce_del;
2562 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
2563 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
2564 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
2567 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
2568 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2569 duplicate_pmr - lcm->pending_map_requests_pool);
2573 /* add map-request to pending requests table */
2574 pool_get (lcm->pending_map_requests_pool, pmr);
2575 memset (pmr, 0, sizeof (*pmr));
2576 gid_address_copy (&pmr->src, seid);
2577 gid_address_copy (&pmr->dst, deid);
2578 clib_fifo_add1 (pmr->nonces, nonce);
2579 pmr->is_smr_invoked = is_smr_invoked;
2580 reset_pending_mr_counters (pmr);
2581 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2582 pmr - lcm->pending_map_requests_pool);
2589 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
2591 ip4_header_t *ip4 = hdr;
2594 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2596 ip_address_set (src, &ip4->src_address, IP4);
2597 ip_address_set (dst, &ip4->dst_address, IP4);
2602 ip_address_set (src, &ip6->src_address, IP6);
2603 ip_address_set (dst, &ip6->dst_address, IP6);
2608 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2612 u32 vni = ~0, table_id = ~0;
2614 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
2615 IP4 ? FIB_PROTOCOL_IP4 :
2621 vnip = hash_get (lcm->vni_by_table_id, table_id);
2625 clib_warning ("vrf %d is not mapped to any vni!", table_id);
2631 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2637 l2input_main_t *l2im = &l2input_main;
2638 l2_input_config_t *config;
2639 l2_bridge_domain_t *bd_config;
2641 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
2642 config = vec_elt_at_index (l2im->configs, sw_if_index0);
2643 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
2645 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
2649 clib_warning ("bridge domain %d is not mapped to any vni!",
2656 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2657 gid_address_t * src, gid_address_t * dst)
2662 memset (src, 0, sizeof (*src));
2663 memset (dst, 0, sizeof (*dst));
2664 type = vnet_buffer (b)->lisp.overlay_afi;
2666 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
2669 u8 version, preflen;
2671 gid_address_type (src) = GID_ADDR_IP_PREFIX;
2672 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
2674 ip = vlib_buffer_get_current (b);
2675 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
2677 version = gid_address_ip_version (src);
2678 preflen = ip_address_max_len (version);
2679 gid_address_ippref_len (src) = preflen;
2680 gid_address_ippref_len (dst) = preflen;
2682 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
2683 gid_address_vni (dst) = vni;
2684 gid_address_vni (src) = vni;
2686 else if (LISP_AFI_MAC == type)
2688 ethernet_header_t *eh;
2690 eh = vlib_buffer_get_current (b);
2692 gid_address_type (src) = GID_ADDR_MAC;
2693 gid_address_type (dst) = GID_ADDR_MAC;
2694 mac_copy (&gid_address_mac (src), eh->src_address);
2695 mac_copy (&gid_address_mac (dst), eh->dst_address);
2698 vni = lisp_get_vni_from_buffer_eth (lcm, b);
2700 gid_address_vni (dst) = vni;
2701 gid_address_vni (src) = vni;
2703 else if (LISP_AFI_LCAF == type)
2705 /* Eventually extend this to support NSH and other */
2711 lisp_cp_lookup_inline (vlib_main_t * vm,
2712 vlib_node_runtime_t * node,
2713 vlib_frame_t * from_frame, int overlay)
2715 u32 *from, *to_next_drop, di, si;
2716 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2717 u32 pkts_mapped = 0;
2718 uword n_left_from, n_left_to_next_drop;
2720 from = vlib_frame_vector_args (from_frame);
2721 n_left_from = from_frame->n_vectors;
2723 while (n_left_from > 0)
2725 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2726 to_next_drop, n_left_to_next_drop);
2728 while (n_left_from > 0 && n_left_to_next_drop > 0)
2732 gid_address_t src, dst;
2737 to_next_drop[0] = pi0;
2739 n_left_to_next_drop -= 1;
2741 b0 = vlib_get_buffer (vm, pi0);
2742 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
2743 vnet_buffer (b0)->lisp.overlay_afi = overlay;
2745 /* src/dst eid pair */
2746 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
2748 /* if we have remote mapping for destination already in map-chache
2749 add forwarding tunnel directly. If not send a map-request */
2750 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
2754 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
2755 /* send a map-request also in case of negative mapping entry
2756 with corresponding action */
2757 if (m->action == LISP_SEND_MAP_REQUEST)
2759 /* send map-request */
2760 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2761 0 /* is_resend */ );
2766 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
2770 dp_add_fwd_entry_from_mt (si, di);
2776 /* send map-request */
2777 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
2778 0 /* is_resend */ );
2782 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2784 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
2787 memset (tr, 0, sizeof (*tr));
2788 gid_address_copy (&tr->dst_eid, &dst);
2789 ip_address_copy (&tr->map_resolver_ip,
2790 &lcm->active_map_resolver);
2792 gid_address_free (&dst);
2793 gid_address_free (&src);
2796 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
2797 n_left_to_next_drop);
2799 vlib_node_increment_counter (vm, node->node_index,
2800 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
2802 return from_frame->n_vectors;
2806 lisp_cp_lookup_ip4 (vlib_main_t * vm,
2807 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2809 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
2813 lisp_cp_lookup_ip6 (vlib_main_t * vm,
2814 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2816 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
2820 lisp_cp_lookup_l2 (vlib_main_t * vm,
2821 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2823 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
2827 lisp_cp_lookup_nsh (vlib_main_t * vm,
2828 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
2830 /* TODO decide if NSH should be propagated as LCAF or not */
2831 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_LCAF));
2835 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
2836 .function = lisp_cp_lookup_ip4,
2837 .name = "lisp-cp-lookup-ip4",
2838 .vector_size = sizeof (u32),
2839 .format_trace = format_lisp_cp_lookup_trace,
2840 .type = VLIB_NODE_TYPE_INTERNAL,
2842 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2843 .error_strings = lisp_cp_lookup_error_strings,
2845 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2848 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2854 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
2855 .function = lisp_cp_lookup_ip6,
2856 .name = "lisp-cp-lookup-ip6",
2857 .vector_size = sizeof (u32),
2858 .format_trace = format_lisp_cp_lookup_trace,
2859 .type = VLIB_NODE_TYPE_INTERNAL,
2861 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2862 .error_strings = lisp_cp_lookup_error_strings,
2864 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2867 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2873 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
2874 .function = lisp_cp_lookup_l2,
2875 .name = "lisp-cp-lookup-l2",
2876 .vector_size = sizeof (u32),
2877 .format_trace = format_lisp_cp_lookup_trace,
2878 .type = VLIB_NODE_TYPE_INTERNAL,
2880 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2881 .error_strings = lisp_cp_lookup_error_strings,
2883 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2886 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2892 VLIB_REGISTER_NODE (lisp_cp_lookup_nsh_node) = {
2893 .function = lisp_cp_lookup_nsh,
2894 .name = "lisp-cp-lookup-nsh",
2895 .vector_size = sizeof (u32),
2896 .format_trace = format_lisp_cp_lookup_trace,
2897 .type = VLIB_NODE_TYPE_INTERNAL,
2899 .n_errors = LISP_CP_LOOKUP_N_ERROR,
2900 .error_strings = lisp_cp_lookup_error_strings,
2902 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
2905 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
2910 /* lisp_cp_input statistics */
2911 #define foreach_lisp_cp_input_error \
2913 _(RLOC_PROBE_REQ_RECEIVED, "rloc-probe requests received") \
2914 _(RLOC_PROBE_REP_RECEIVED, "rloc-probe replies received") \
2915 _(MAP_NOTIFIES_RECEIVED, "map-notifies received") \
2916 _(MAP_REPLIES_RECEIVED, "map-replies received")
2918 static char *lisp_cp_input_error_strings[] = {
2919 #define _(sym,string) string,
2920 foreach_lisp_cp_input_error
2926 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
2927 foreach_lisp_cp_input_error
2929 LISP_CP_INPUT_N_ERROR,
2930 } lisp_cp_input_error_t;
2934 gid_address_t dst_eid;
2935 ip4_address_t map_resolver_ip;
2936 } lisp_cp_input_trace_t;
2939 format_lisp_cp_input_trace (u8 * s, va_list * args)
2941 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2942 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2943 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
2944 va_arg (*args, lisp_cp_input_trace_t *);
2946 s = format (s, "LISP-CP-INPUT: TODO");
2951 remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
2954 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
2955 memset (adj_args, 0, sizeof (adj_args[0]));
2957 m = pool_elt_at_index (lcm->mapping_pool, mi);
2959 gid_address_copy (&adj_args->reid, &m->eid);
2960 adj_args->is_add = 0;
2961 if (vnet_lisp_add_del_adjacency (adj_args))
2962 clib_warning ("failed to del adjacency!");
2964 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
2965 0 /* is_static */ , 0);
2966 mapping_delete_timer (lcm, mi);
2970 mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
2971 f64 expiration_time)
2974 u64 now = clib_cpu_time_now ();
2975 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
2976 u64 exp_clock_time = now + expiration_time * cpu_cps;
2978 m = pool_elt_at_index (lcm->mapping_pool, mi);
2981 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
2985 map_records_arg_free (map_records_arg_t * a)
2988 vec_foreach (m, a->mappings)
2990 vec_free (m->locators);
2991 gid_address_free (&m->eid);
2998 process_map_reply (map_records_arg_t * a)
3001 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3002 u32 dst_map_index = 0;
3003 pending_map_request_t *pmr;
3007 if (a->is_rloc_probe)
3010 /* Check pending requests table and nonce */
3011 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
3014 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
3017 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
3019 vec_foreach (m, a->mappings)
3021 /* insert/update mappings cache */
3022 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
3023 m->authoritative, m->ttl,
3024 1, 0 /* is_static */ , &dst_map_index);
3026 if (dst_map_index == (u32) ~ 0)
3029 /* try to program forwarding only if mapping saved or updated */
3030 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3031 memset (adj_args, 0, sizeof (adj_args[0]));
3033 gid_address_copy (&adj_args->leid, &pmr->src);
3034 gid_address_copy (&adj_args->reid, &m->eid);
3035 adj_args->is_add = 1;
3036 if (vnet_lisp_add_del_adjacency (adj_args))
3037 clib_warning ("failed to add adjacency!");
3039 if ((u32) ~ 0 != m->ttl)
3040 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
3043 /* remove pending map request entry */
3046 clib_fifo_foreach (noncep, pmr->nonces, ({
3047 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
3051 clib_fifo_free (pmr->nonces);
3052 pool_put (lcm->pending_map_requests_pool, pmr);
3055 map_records_arg_free (a);
3060 is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
3061 lisp_key_type_t key_id, u8 * key)
3067 auth_data_len = auth_data_len_by_key_id (key_id);
3068 if ((u16) ~ 0 == auth_data_len)
3070 clib_warning ("invalid length for key_id %d!", key_id);
3074 /* save auth data */
3075 vec_validate (auth_data, auth_data_len - 1);
3076 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
3078 /* clear auth data */
3079 memset (MNOTIFY_DATA (h), 0, auth_data_len);
3081 /* get hash of the message */
3082 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3083 (unsigned char *) h, msg_len, NULL, NULL);
3085 result = memcmp (code, auth_data, auth_data_len);
3087 vec_free (auth_data);
3093 process_map_notify (map_records_arg_t * a)
3095 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3098 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
3101 clib_warning ("No pending map-register entry with nonce %lu!",
3106 map_records_arg_free (a);
3107 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
3111 get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
3115 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
3118 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
3122 return pool_elt_at_index (lcm->mapping_pool, mi);
3126 * When map-notify is received it is necessary that all EIDs in the record
3127 * list share common key. The key is then used to verify authentication
3128 * data in map-notify message.
3131 map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
3132 u32 key_id, u8 ** key_out)
3134 u32 i, len = vec_len (maps);
3137 /* get key of the first mapping */
3138 m = get_mapping (lcm, &maps[0].eid);
3142 key_out[0] = m->key;
3144 for (i = 1; i < len; i++)
3146 m = get_mapping (lcm, &maps[i].eid);
3150 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
3152 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
3160 parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
3162 locator_t *locators = 0;
3168 /* parse record eid */
3169 for (i = 0; i < count; i++)
3171 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
3174 clib_warning ("Failed to parse mapping record!");
3175 vec_foreach (loc, locators) locator_free (loc);
3176 vec_free (locators);
3180 m.locators = locators;
3181 gid_address_copy (&m.eid, &deid);
3182 vec_add1 (a->mappings, m);
3188 static map_records_arg_t *
3189 parse_map_notify (vlib_buffer_t * b)
3192 map_notify_hdr_t *mnotif_hdr;
3193 lisp_key_type_t key_id;
3194 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3197 u16 auth_data_len = 0;
3199 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3201 memset (a, 0, sizeof (*a));
3202 mnotif_hdr = vlib_buffer_get_current (b);
3203 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
3204 memset (&deid, 0, sizeof (deid));
3206 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
3207 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
3208 auth_data_len = auth_data_len_by_key_id (key_id);
3210 /* advance buffer by authentication data */
3211 vlib_buffer_pull (b, auth_data_len);
3213 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
3214 rc = parse_map_records (b, a, record_count);
3217 map_records_arg_free (a);
3221 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
3224 map_records_arg_free (a);
3228 /* verify authentication data */
3229 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
3230 - (u8 *) mnotif_hdr, key_id, key))
3232 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
3234 map_records_arg_free (a);
3240 static vlib_buffer_t *
3241 build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
3242 ip_address_t * dst, u64 nonce, u8 probe_bit,
3243 mapping_t * records, u16 dst_port, u32 * bi_res)
3247 vlib_main_t *vm = lcm->vlib_main;
3249 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3251 clib_warning ("Can't allocate buffer for Map-Register!");
3255 b = vlib_get_buffer (vm, bi);
3257 /* leave some space for the encap headers */
3258 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3260 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
3262 /* push outer ip header */
3263 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
3270 send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
3271 u8 probe_bit, u64 nonce, u16 dst_port,
3272 ip_address_t * probed_loc)
3278 u32 next_index, *to_next;
3279 mapping_t *records = 0, *m;
3281 m = pool_elt_at_index (lcm->mapping_pool, mi);
3285 vec_add1 (records, m[0]);
3286 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
3287 memset (&src, 0, sizeof (src));
3289 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
3291 clib_warning ("can't find inteface address for %U", format_ip_address,
3296 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
3300 free_map_register_records (records);
3302 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3303 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3304 ip4_lookup_node.index : ip6_lookup_node.index;
3306 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3308 /* Enqueue the packet */
3309 to_next = vlib_frame_vector_args (f);
3312 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3317 find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
3319 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
3320 if (start < 0 && start < -sizeof (b->pre_data))
3326 *ip_hdr = b->data + start;
3327 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
3332 process_map_request (vlib_main_t * vm, vlib_node_runtime_t * node,
3333 lisp_cp_main_t * lcm, vlib_buffer_t * b)
3336 ip_address_t *dst_loc = 0, probed_loc, src_loc;
3338 map_request_hdr_t *mreq_hdr;
3339 gid_address_t src, dst;
3341 u32 i, len = 0, rloc_probe_recv = 0;
3342 gid_address_t *itr_rlocs = 0;
3344 mreq_hdr = vlib_buffer_get_current (b);
3345 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
3348 ("Only SMR Map-Requests and RLOC probe supported for now!");
3352 vlib_buffer_pull (b, sizeof (*mreq_hdr));
3353 nonce = MREQ_NONCE (mreq_hdr);
3356 len = lisp_msg_parse_addr (b, &src);
3360 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3361 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
3365 /* parse eid records and send SMR-invoked map-requests */
3366 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
3368 memset (&dst, 0, sizeof (dst));
3369 len = lisp_msg_parse_eid_rec (b, &dst);
3372 clib_warning ("Can't parse map-request EID-record");
3376 if (MREQ_SMR (mreq_hdr))
3378 /* send SMR-invoked map-requests */
3379 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3381 else if (MREQ_RLOC_PROBE (mreq_hdr))
3383 find_ip_header (b, &ip_hdr);
3386 clib_warning ("Cannot find the IP header!");
3390 memset (&m, 0, sizeof (m));
3391 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3393 // TODO: select best locator; for now use the first one
3394 dst_loc = &gid_address_ip (&itr_rlocs[0]);
3396 /* get src/dst IP addresses */
3397 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
3399 // TODO get source port from buffer
3400 u16 src_port = LISP_CONTROL_PORT;
3402 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
3403 src_port, &probed_loc);
3408 vlib_node_increment_counter (vm, node->node_index,
3409 LISP_CP_INPUT_ERROR_RLOC_PROBE_REQ_RECEIVED,
3411 vec_free (itr_rlocs);
3414 static map_records_arg_t *
3415 parse_map_reply (vlib_buffer_t * b)
3422 map_reply_hdr_t *mrep_hdr;
3423 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3424 memset (a, 0, sizeof (*a));
3425 locator_t *locators;
3427 mrep_hdr = vlib_buffer_get_current (b);
3428 a->nonce = MREP_NONCE (mrep_hdr);
3429 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
3430 vlib_buffer_pull (b, sizeof (*mrep_hdr));
3432 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3434 memset (&m, 0, sizeof (m));
3436 h = vlib_buffer_get_current (b);
3438 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3439 m.action = MAP_REC_ACTION (h);
3440 m.authoritative = MAP_REC_AUTH (h);
3442 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3445 clib_warning ("Failed to parse mapping record!");
3446 map_records_arg_free (a);
3450 m.locators = locators;
3451 gid_address_copy (&m.eid, &deid);
3452 vec_add1 (a->mappings, m);
3458 queue_map_reply_for_processing (map_records_arg_t * a)
3460 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
3464 queue_map_notify_for_processing (map_records_arg_t * a)
3466 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
3470 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
3471 vlib_frame_t * from_frame)
3473 u32 n_left_from, *from, *to_next_drop, rloc_probe_rep_recv = 0,
3474 map_notifies_recv = 0;
3475 lisp_msg_type_e type;
3476 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3477 map_records_arg_t *a;
3479 from = vlib_frame_vector_args (from_frame);
3480 n_left_from = from_frame->n_vectors;
3483 while (n_left_from > 0)
3485 u32 n_left_to_next_drop;
3487 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3488 to_next_drop, n_left_to_next_drop);
3489 while (n_left_from > 0 && n_left_to_next_drop > 0)
3497 to_next_drop[0] = bi0;
3499 n_left_to_next_drop -= 1;
3501 b0 = vlib_get_buffer (vm, bi0);
3503 type = lisp_msg_type (vlib_buffer_get_current (b0));
3506 case LISP_MAP_REPLY:
3507 a = parse_map_reply (b0);
3510 if (a->is_rloc_probe)
3511 rloc_probe_rep_recv++;
3512 queue_map_reply_for_processing (a);
3515 case LISP_MAP_REQUEST:
3516 process_map_request (vm, node, lcm, b0);
3518 case LISP_MAP_NOTIFY:
3519 a = parse_map_notify (b0);
3522 map_notifies_recv++;
3523 queue_map_notify_for_processing (a);
3527 clib_warning ("Unsupported LISP message type %d", type);
3531 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
3533 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3539 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3540 n_left_to_next_drop);
3542 vlib_node_increment_counter (vm, node->node_index,
3543 LISP_CP_INPUT_ERROR_RLOC_PROBE_REP_RECEIVED,
3544 rloc_probe_rep_recv);
3545 vlib_node_increment_counter (vm, node->node_index,
3546 LISP_CP_INPUT_ERROR_MAP_NOTIFIES_RECEIVED,
3548 return from_frame->n_vectors;
3552 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3553 .function = lisp_cp_input,
3554 .name = "lisp-cp-input",
3555 .vector_size = sizeof (u32),
3556 .format_trace = format_lisp_cp_input_trace,
3557 .type = VLIB_NODE_TYPE_INTERNAL,
3559 .n_errors = LISP_CP_INPUT_N_ERROR,
3560 .error_strings = lisp_cp_input_error_strings,
3562 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3565 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3571 lisp_cp_init (vlib_main_t * vm)
3573 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3574 clib_error_t *error = 0;
3576 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3579 lcm->im4 = &ip4_main;
3580 lcm->im6 = &ip6_main;
3581 lcm->vlib_main = vm;
3582 lcm->vnet_main = vnet_get_main ();
3583 lcm->mreq_itr_rlocs = ~0;
3586 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
3588 gid_dictionary_init (&lcm->mapping_index_by_gid);
3589 lcm->do_map_resolver_election = 1;
3590 lcm->map_request_mode = MR_MODE_DST_ONLY;
3592 /* default vrf mapped to vni 0 */
3593 hash_set (lcm->table_id_by_vni, 0, 0);
3594 hash_set (lcm->vni_by_table_id, 0, 0);
3596 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
3597 lisp_cp_input_node.index, 1 /* is_ip4 */ );
3598 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
3599 lisp_cp_input_node.index, 0 /* is_ip4 */ );
3601 u64 now = clib_cpu_time_now ();
3602 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
3607 send_map_request_thread_fn (void *arg)
3609 map_request_args_t *a = arg;
3610 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3613 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3615 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3621 queue_map_request (gid_address_t * seid, gid_address_t * deid,
3622 u8 smr_invoked, u8 is_resend)
3624 map_request_args_t a;
3626 a.is_resend = is_resend;
3627 gid_address_copy (&a.seid, seid);
3628 gid_address_copy (&a.deid, deid);
3629 a.smr_invoked = smr_invoked;
3631 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
3632 (u8 *) & a, sizeof (a));
3637 * Take an action with a pending map request depending on expiration time
3638 * and re-try counters.
3641 update_pending_request (pending_map_request_t * r, f64 dt)
3643 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3646 if (r->time_to_expire - dt < 0)
3647 /* it's time to decide what to do with this pending request */
3649 if (r->retries_num >= NUMBER_OF_RETRIES)
3650 /* too many retries -> assume current map resolver is not available */
3652 mr = get_map_resolver (&lcm->active_map_resolver);
3655 clib_warning ("Map resolver %U not found - probably deleted "
3656 "by the user recently.", format_ip_address,
3657 &lcm->active_map_resolver);
3661 clib_warning ("map resolver %U is unreachable, ignoring",
3662 format_ip_address, &lcm->active_map_resolver);
3664 /* mark current map resolver unavailable so it won't be
3665 * selected next time */
3667 mr->last_update = vlib_time_now (lcm->vlib_main);
3670 reset_pending_mr_counters (r);
3671 elect_map_resolver (lcm);
3673 /* try to find a next eligible map resolver and re-send */
3674 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3680 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3683 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3687 r->time_to_expire -= dt;
3691 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
3694 pending_map_request_t *pmr;
3695 u32 *to_be_removed = 0, *pmr_index;
3698 pool_foreach (pmr, lcm->pending_map_requests_pool,
3700 if (pmr->to_be_removed)
3702 clib_fifo_foreach (nonce, pmr->nonces, ({
3703 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
3706 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
3711 vec_foreach (pmr_index, to_be_removed)
3712 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
3714 vec_free (to_be_removed);
3718 update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
3720 static f64 time_left = RLOC_PROBING_INTERVAL;
3722 if (!lcm->is_enabled || !lcm->rloc_probing)
3728 time_left = RLOC_PROBING_INTERVAL;
3729 send_rloc_probes (lcm);
3734 update_map_register (lisp_cp_main_t * lcm, f64 dt)
3736 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
3737 static u64 mreg_sent_counter = 0;
3739 if (!lcm->is_enabled || !lcm->map_registering)
3745 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
3746 time_left = MAP_REGISTER_INTERVAL;
3749 mreg_sent_counter++;
3750 time_left = QUICK_MAP_REGISTER_INTERVAL;
3752 send_map_register (lcm, 1 /* want map notify */ );
3757 send_map_resolver_service (vlib_main_t * vm,
3758 vlib_node_runtime_t * rt, vlib_frame_t * f)
3762 pending_map_request_t *pmr;
3763 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3767 vlib_process_wait_for_event_or_clock (vm, period);
3769 /* currently no signals are expected - just wait for clock */
3770 (void) vlib_process_get_events (vm, 0);
3773 pool_foreach (pmr, lcm->pending_map_requests_pool,
3775 if (!pmr->to_be_removed)
3776 update_pending_request (pmr, period);
3780 remove_dead_pending_map_requests (lcm);
3782 update_map_register (lcm, period);
3783 update_rloc_probing (lcm, period);
3785 u64 now = clib_cpu_time_now ();
3787 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
3788 if (vec_len (expired) > 0)
3791 vec_foreach (mi, expired)
3793 remove_expired_mapping (lcm, mi[0]);
3795 _vec_len (expired) = 0;
3804 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
3805 .function = send_map_resolver_service,
3806 .type = VLIB_NODE_TYPE_PROCESS,
3807 .name = "lisp-retry-service",
3808 .process_log2_n_stack_bytes = 16,
3812 VLIB_INIT_FUNCTION (lisp_cp_init);
3815 * fd.io coding-style-patch-verification: ON
3818 * eval: (c-set-style "gnu")