2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vlibmemory/api.h>
17 #include <vnet/lisp-cp/control.h>
18 #include <vnet/lisp-cp/packets.h>
19 #include <vnet/lisp-cp/lisp_msg_serdes.h>
20 #include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
21 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
22 #include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
23 #include <vnet/fib/fib_entry.h>
24 #include <vnet/fib/fib_table.h>
25 #include <vnet/ethernet/arp_packet.h>
26 #include <vnet/ethernet/packet.h>
28 #include <openssl/evp.h>
29 #include <openssl/hmac.h>
31 lisp_cp_main_t lisp_control_main;
33 u8 *format_lisp_cp_input_trace (u8 * s, va_list * args);
37 LISP_CP_INPUT_NEXT_DROP,
39 } lisp_cp_input_next_t;
50 vnet_lisp_get_map_request_mode (void)
52 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
53 return lcm->map_request_mode;
57 auth_data_len_by_key_id (lisp_key_type_t key_id)
62 return SHA1_AUTH_DATA_LEN;
63 case HMAC_SHA_256_128:
64 return SHA256_AUTH_DATA_LEN;
66 clib_warning ("unsupported key type: %d!", key_id);
73 get_encrypt_fcn (lisp_key_type_t key_id)
79 case HMAC_SHA_256_128:
82 clib_warning ("unsupported encryption key type: %d!", key_id);
89 queue_map_request (gid_address_t * seid, gid_address_t * deid,
90 u8 smr_invoked, u8 is_resend);
92 ip_interface_address_t *
93 ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
94 u32 sw_if_index, u8 loop)
96 vnet_main_t *vnm = vnet_get_main ();
97 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
98 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
99 sw_if_index = swif->unnumbered_sw_if_index;
101 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
102 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
104 return pool_elt_at_index ((lm)->if_address_pool, ia);
108 ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
111 ip_interface_address_t *ia;
113 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
116 return ip_interface_address_get_address (lm, ia);
120 ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
121 u8 version, ip_address_t * result)
123 ip_lookup_main_t *lm;
126 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
127 addr = ip_interface_get_first_address (lm, sw_if_index, version);
131 ip_address_set (result, addr, version);
136 * convert from a LISP address to a FIB prefix
139 ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
141 if (addr->version == IP4)
144 prefix->fp_proto = FIB_PROTOCOL_IP4;
145 memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
146 memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
150 prefix->fp_len = 128;
151 prefix->fp_proto = FIB_PROTOCOL_IP6;
152 memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
157 * convert from a LISP to a FIB prefix
160 ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
161 fib_prefix_t * fib_prefix)
163 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
164 fib_prefix->fp_len = ip_prefix->len;
168 * Find the sw_if_index of the interface that would be used to egress towards
172 ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
174 fib_node_index_t fei;
177 ip_address_to_fib_prefix (dst, &prefix);
179 fei = fib_table_lookup (0, &prefix);
181 return (fib_entry_get_resolving_interface (fei));
185 * Find first IP of the interface that would be used to egress towards dst.
186 * Returns 1 if the address is found 0 otherwise.
189 ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
190 ip_address_t * result)
193 ip_lookup_main_t *lm;
197 ASSERT (result != 0);
199 ipver = ip_addr_version (dst);
201 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
202 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
207 /* find the first ip address */
208 addr = ip_interface_get_first_address (lm, si, ipver);
212 ip_address_set (result, addr, ipver);
217 dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
223 dp_table = hash_get (lcm->table_id_by_vni, vni);
227 clib_warning ("vni %d not associated to a vrf!", vni);
228 return VNET_API_ERROR_INVALID_VALUE;
233 dp_table = hash_get (lcm->bd_id_by_vni, vni);
236 clib_warning ("vni %d not associated to a bridge domain!", vni);
237 return VNET_API_ERROR_INVALID_VALUE;
241 /* enable/disable data-plane interface */
245 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
247 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
252 lisp_gpe_tenant_l2_iface_unlock (vni);
254 lisp_gpe_tenant_l3_iface_unlock (vni);
261 dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
263 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
266 memset (a, 0, sizeof (*a));
268 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
272 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
274 /* delete dp fwd entry */
277 a->locator_pairs = fe->locator_pairs;
278 a->vni = gid_address_vni (&fe->reid);
279 gid_address_copy (&a->rmt_eid, &fe->reid);
281 gid_address_copy (&a->lcl_eid, &fe->leid);
283 vnet_lisp_gpe_del_fwd_counters (a, feip[0]);
284 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
286 /* delete entry in fwd table */
287 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
288 vec_free (fe->locator_pairs);
289 pool_put (lcm->fwd_entry_pool, fe);
293 * Finds first remote locator with best (lowest) priority that has a local
294 * peer locator with an underlying route to it.
298 get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
299 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
301 u32 i, limitp = 0, li, found = 0, esi;
302 locator_set_t *rmt_ls, *lcl_ls;
303 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
304 locator_t *lp, *rmt = 0;
309 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
311 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
313 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
320 /* find unvisited remote locator with best priority */
321 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
323 if (0 != hash_get (checked, i))
326 li = vec_elt (rmt_ls->locator_indices, i);
327 lp = pool_elt_at_index (lcm->locator_pool, li);
329 /* we don't support non-IP locators for now */
330 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
333 if ((found && lp->priority == limitp)
334 || (!found && lp->priority >= limitp))
338 /* don't search for locators with lower priority and don't
339 * check this locator again*/
340 limitp = lp->priority;
341 hash_set (checked, i, 1);
345 /* check if a local locator with a route to remote locator exists */
348 /* find egress sw_if_index for rmt locator */
350 ip_fib_get_egress_iface_for_dst (lcm,
351 &gid_address_ip (&rmt->address));
352 if ((u32) ~ 0 == esi)
355 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
357 li = vec_elt (lcl_ls->locator_indices, i);
358 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
360 /* found local locator with the needed sw_if_index */
361 if (sl->sw_if_index == esi)
363 /* and it has an address */
364 if (0 == ip_interface_get_first_ip_address (lcm,
366 gid_address_ip_version
371 memset (&pair, 0, sizeof (pair));
372 ip_address_copy (&pair.rmt_loc,
373 &gid_address_ip (&rmt->address));
374 ip_address_copy (&pair.lcl_loc, lcl_addr);
375 pair.weight = rmt->weight;
376 pair.priority = rmt->priority;
377 vec_add1 (locator_pairs[0], pair);
391 gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
394 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
398 switch (fid_addr_type (fid))
400 case FID_ADDR_IP_PREF:
401 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
402 gid_address_ippref (dst) = fid_addr_ippref (fid);
405 gid_address_type (dst) = GID_ADDR_MAC;
406 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
409 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
415 vnet_lisp_map_register_state_get (void)
417 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
418 return lcm->map_registering;
422 vnet_lisp_rloc_probe_state_get (void)
424 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
425 return lcm->rloc_probing;
429 dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
431 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
432 gid_address_t *rmt_eid, *lcl_eid;
433 mapping_t *lcl_map, *rmt_map;
435 uword *feip = 0, *dpid;
437 u8 type, is_src_dst = 0;
440 memset (a, 0, sizeof (*a));
442 /* remove entry if it already exists */
443 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
445 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
448 * Determine local mapping and eid
451 lcl_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
453 lcl_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
454 lcl_eid = &lcl_map->eid;
457 * Determine remote mapping and eid
459 rmt_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
460 rmt_eid = &rmt_map->eid;
463 * Build and insert data plane forwarding entry
467 if (MR_MODE_SRC_DST == lcm->map_request_mode)
469 if (GID_ADDR_SRC_DST == gid_address_type (rmt_eid))
471 gid_address_sd_to_flat (&a->rmt_eid, rmt_eid,
472 &gid_address_sd_dst (rmt_eid));
473 gid_address_sd_to_flat (&a->lcl_eid, rmt_eid,
474 &gid_address_sd_src (rmt_eid));
478 gid_address_copy (&a->rmt_eid, rmt_eid);
479 gid_address_copy (&a->lcl_eid, lcl_eid);
484 gid_address_copy (&a->rmt_eid, rmt_eid);
486 a->vni = gid_address_vni (&a->rmt_eid);
487 a->is_src_dst = is_src_dst;
489 /* get vrf or bd_index associated to vni */
490 type = gid_address_type (&a->rmt_eid);
491 if (GID_ADDR_IP_PREFIX == type)
493 dpid = hash_get (lcm->table_id_by_vni, a->vni);
496 clib_warning ("vni %d not associated to a vrf!", a->vni);
499 a->table_id = dpid[0];
501 else if (GID_ADDR_MAC == type)
503 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
506 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
512 /* find best locator pair that 1) verifies LISP policy 2) are connected */
513 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
515 /* Either rmt mapping is negative or we can't find underlay path.
516 * Try again with petr if configured */
517 if (rv == 0 && (lcm->flags & LISP_FLAG_USE_PETR))
519 rmt_map = lisp_get_petr_mapping (lcm);
520 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
527 a->action = rmt_map->action;
530 rv = vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
533 if (a->locator_pairs)
534 vec_free (a->locator_pairs);
538 /* add tunnel to fwd entry table */
539 pool_get (lcm->fwd_entry_pool, fe);
540 vnet_lisp_gpe_add_fwd_counters (a, fe - lcm->fwd_entry_pool);
542 fe->locator_pairs = a->locator_pairs;
543 gid_address_copy (&fe->reid, &a->rmt_eid);
546 gid_address_copy (&fe->leid, &a->lcl_eid);
548 gid_address_copy (&fe->leid, lcl_eid);
550 fe->is_src_dst = is_src_dst;
551 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
552 fe - lcm->fwd_entry_pool);
559 } fwd_entry_mt_arg_t;
562 dp_add_fwd_entry_thread_fn (void *arg)
564 fwd_entry_mt_arg_t *a = arg;
565 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
566 dp_add_fwd_entry (lcm, a->si, a->di);
571 dp_add_fwd_entry_from_mt (u32 si, u32 di)
573 fwd_entry_mt_arg_t a;
575 memset (&a, 0, sizeof (a));
579 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
580 (u8 *) & a, sizeof (a));
585 * Returns vector of adjacencies.
587 * The caller must free the vector returned by this function.
589 * @param vni virtual network identifier
590 * @return vector of adjacencies
593 vnet_lisp_adjacencies_get_by_vni (u32 vni)
595 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
597 lisp_adjacency_t *adjs = 0, adj;
600 pool_foreach(fwd, lcm->fwd_entry_pool,
602 if (gid_address_vni (&fwd->reid) != vni)
605 gid_address_copy (&adj.reid, &fwd->reid);
606 gid_address_copy (&adj.leid, &fwd->leid);
607 vec_add1 (adjs, adj);
615 get_map_server (ip_address_t * a)
617 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
620 vec_foreach (m, lcm->map_servers)
622 if (!ip_address_cmp (&m->address, a))
631 get_map_resolver (ip_address_t * a)
633 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
636 vec_foreach (m, lcm->map_resolvers)
638 if (!ip_address_cmp (&m->address, a))
647 vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
650 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
651 lisp_msmr_t _ms, *ms = &_ms;
653 if (vnet_lisp_enable_disable_status () == 0)
655 clib_warning ("LISP is disabled!");
656 return VNET_API_ERROR_LISP_DISABLED;
661 if (get_map_server (addr))
663 clib_warning ("map-server %U already exists!", format_ip_address,
668 memset (ms, 0, sizeof (*ms));
669 ip_address_copy (&ms->address, addr);
670 vec_add1 (lcm->map_servers, ms[0]);
674 for (i = 0; i < vec_len (lcm->map_servers); i++)
676 ms = vec_elt_at_index (lcm->map_servers, i);
677 if (!ip_address_cmp (&ms->address, addr))
679 vec_del1 (lcm->map_servers, i);
689 * Add/remove mapping to/from map-cache. Overwriting not allowed.
692 vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
693 u32 * map_index_result)
695 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
696 u32 mi, *map_indexp, map_index, i;
697 mapping_t *m, *old_map;
700 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
701 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
704 /* TODO check if overwriting and take appropriate actions */
705 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
707 clib_warning ("eid %U found in the eid-table", format_gid_address,
709 return VNET_API_ERROR_VALUE_EXIST;
712 pool_get (lcm->mapping_pool, m);
713 gid_address_copy (&m->eid, &a->eid);
714 m->locator_set_index = a->locator_set_index;
716 m->action = a->action;
718 m->is_static = a->is_static;
719 m->key = vec_dup (a->key);
720 m->key_id = a->key_id;
722 map_index = m - lcm->mapping_pool;
723 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
726 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
728 clib_warning ("Locator set with index %d doesn't exist",
729 a->locator_set_index);
730 return VNET_API_ERROR_INVALID_VALUE;
733 /* add eid to list of eids supported by locator-set */
734 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
735 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
736 a->locator_set_index);
737 vec_add1 (eid_indexes[0], map_index);
742 vec_add1 (lcm->local_mappings_indexes, map_index);
744 map_index_result[0] = map_index;
748 if (mi == GID_LOOKUP_MISS)
750 clib_warning ("eid %U not found in the eid-table",
751 format_gid_address, &a->eid);
752 return VNET_API_ERROR_INVALID_VALUE;
755 /* clear locator-set to eids binding */
756 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
757 a->locator_set_index);
758 for (i = 0; i < vec_len (eid_indexes[0]); i++)
760 map_indexp = vec_elt_at_index (eid_indexes[0], i);
761 if (map_indexp[0] == mi)
764 vec_del1 (eid_indexes[0], i);
766 /* remove local mark if needed */
767 m = pool_elt_at_index (lcm->mapping_pool, mi);
771 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
773 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
774 if (lm_indexp[0] == mi)
777 vec_del1 (lcm->local_mappings_indexes, k);
780 /* remove mapping from dictionary */
781 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
782 gid_address_free (&m->eid);
783 pool_put_index (lcm->mapping_pool, mi);
790 * Add/update/delete mapping to/in/from map-cache.
793 vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
794 u32 * map_index_result)
800 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
802 if (vnet_lisp_enable_disable_status () == 0)
804 clib_warning ("LISP is disabled!");
805 return VNET_API_ERROR_LISP_DISABLED;
808 vni = gid_address_vni (&a->eid);
809 type = gid_address_type (&a->eid);
810 if (GID_ADDR_IP_PREFIX == type)
811 dp_table = hash_get (lcm->table_id_by_vni, vni);
812 else if (GID_ADDR_MAC == type)
813 dp_table = hash_get (lcm->bd_id_by_vni, vni);
817 clib_warning ("vni %d not associated to a %s!", vni,
818 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
819 return VNET_API_ERROR_INVALID_VALUE;
822 /* store/remove mapping from map-cache */
823 return vnet_lisp_map_cache_add_del (a, map_index_result);
827 add_l2_arp_bd (BVT (clib_bihash_kv) * kvp, void *arg)
830 u32 bd = (u32) kvp->key[0];
831 hash_set (ht[0], bd, 0);
835 vnet_lisp_l2_arp_bds_get (void)
837 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
840 gid_dict_foreach_l2_arp_entry (&lcm->mapping_index_by_gid,
841 add_l2_arp_bd, &bds);
849 } lisp_add_l2_arp_args_t;
852 add_l2_arp_entry (BVT (clib_bihash_kv) * kvp, void *arg)
854 lisp_add_l2_arp_args_t *a = arg;
855 lisp_api_l2_arp_entry_t **vector = a->vector, e;
857 if ((u32) kvp->key[0] == a->bd)
859 mac_copy (e.mac, (void *) &kvp->value);
860 e.ip4 = (u32) kvp->key[1];
861 vec_add1 (vector[0], e);
865 lisp_api_l2_arp_entry_t *
866 vnet_lisp_l2_arp_entries_get_by_bd (u32 bd)
868 lisp_api_l2_arp_entry_t *entries = 0;
869 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
870 lisp_add_l2_arp_args_t a;
875 gid_dict_foreach_l2_arp_entry (&lcm->mapping_index_by_gid,
876 add_l2_arp_entry, &a);
881 vnet_lisp_add_del_l2_arp_entry (gid_address_t * key, u8 * mac, u8 is_add)
883 if (vnet_lisp_enable_disable_status () == 0)
885 clib_warning ("LISP is disabled!");
886 return VNET_API_ERROR_LISP_DISABLED;
889 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
892 u64 res = gid_dictionary_lookup (&lcm->mapping_index_by_gid, key);
895 if (res != GID_LOOKUP_MISS_L2)
897 clib_warning ("Entry %U exists in DB!", format_gid_address, key);
898 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
900 u64 val = mac_to_u64 (mac);
901 gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, val,
906 if (res == GID_LOOKUP_MISS_L2)
908 clib_warning ("ONE ARP entry %U not found - cannot delete!",
909 format_gid_address, key);
912 gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, 0,
920 vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
922 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
923 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
925 if (vnet_lisp_enable_disable_status () == 0)
927 clib_warning ("LISP is disabled!");
928 return VNET_API_ERROR_LISP_DISABLED;
931 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
932 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
934 if (!is_l2 && (vni == 0 || dp_id == 0))
936 clib_warning ("can't add/del default vni-vrf mapping!");
940 dp_idp = hash_get (dp_table_by_vni[0], vni);
941 vnip = hash_get (vni_by_dp_table[0], dp_id);
947 clib_warning ("vni %d or vrf %d already used in vrf/vni "
948 "mapping!", vni, dp_id);
951 hash_set (dp_table_by_vni[0], vni, dp_id);
952 hash_set (vni_by_dp_table[0], dp_id, vni);
954 /* create dp iface */
955 dp_add_del_iface (lcm, vni, is_l2, 1);
959 if (!dp_idp || !vnip)
961 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
962 "mapping!", vni, dp_id);
965 /* remove dp iface */
966 dp_add_del_iface (lcm, vni, is_l2, 0);
968 hash_unset (dp_table_by_vni[0], vni);
969 hash_unset (vni_by_dp_table[0], dp_id);
975 /* return 0 if the two locator sets are identical 1 otherwise */
977 compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
978 locator_t * new_locators)
981 locator_t *old_loc, *new_loc;
983 if (vec_len (old_ls_indexes) != vec_len (new_locators))
986 for (i = 0; i < vec_len (new_locators); i++)
988 old_li = vec_elt (old_ls_indexes, i);
989 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
991 new_loc = vec_elt_at_index (new_locators, i);
993 if (locator_cmp (old_loc, new_loc))
1003 gid_address_t *eids_to_be_deleted;
1004 } remove_mapping_args_t;
1007 * Callback invoked when a sub-prefix is found
1010 remove_mapping_if_needed (u32 mi, void *arg)
1013 remove_mapping_args_t *a = arg;
1014 lisp_cp_main_t *lcm = a->lcm;
1018 m = pool_elt_at_index (lcm->mapping_pool, mi);
1022 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1026 if (0 != vec_len (ls->locator_indices))
1031 if (0 == vec_len (ls->locator_indices))
1036 vec_add1 (a->eids_to_be_deleted, m->eid);
1040 * This function searches map cache and looks for IP prefixes that are subset
1041 * of the provided one. If such prefix is found depending on 'is_negative'
1044 * 1) if is_negative is true and found prefix points to positive mapping,
1045 * then the mapping is removed
1046 * 2) if is_negative is false and found prefix points to negative mapping,
1047 * then the mapping is removed
1050 remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1054 remove_mapping_args_t a;
1056 memset (&a, 0, sizeof (a));
1058 /* do this only in src/dst mode ... */
1059 if (MR_MODE_SRC_DST != lcm->map_request_mode)
1062 /* ... and only for IP prefix */
1063 if (GID_ADDR_SRC_DST != gid_address_type (eid)
1064 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1067 a.is_negative = is_negative;
1070 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1071 remove_mapping_if_needed, &a);
1073 vec_foreach (e, a.eids_to_be_deleted)
1075 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
1077 memset (adj_args, 0, sizeof (adj_args[0]));
1078 gid_address_copy (&adj_args->reid, e);
1079 adj_args->is_add = 0;
1080 if (vnet_lisp_add_del_adjacency (adj_args))
1081 clib_warning ("failed to del adjacency!");
1083 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
1086 vec_free (a.eids_to_be_deleted);
1090 mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1092 timing_wheel_delete (&lcm->wheel, mi);
1096 is_local_ip (lisp_cp_main_t * lcm, ip_address_t * addr)
1098 fib_node_index_t fei;
1099 fib_prefix_t prefix;
1100 fib_entry_flag_t flags;
1102 ip_address_to_fib_prefix (addr, &prefix);
1104 fei = fib_table_lookup (0, &prefix);
1105 flags = fib_entry_get_flags (fei);
1106 return (FIB_ENTRY_FLAG_LOCAL & flags);
1110 * Adds/removes/updates mapping. Does not program forwarding.
1112 * @param eid end-host identifier
1113 * @param rlocs vector of remote locators
1114 * @param action action for negative map-reply
1115 * @param is_add add mapping if non-zero, delete otherwise
1116 * @param res_map_index the map-index that was created/updated/removed. It is
1117 * set to ~0 if no action is taken.
1118 * @param is_static used for distinguishing between statically learned
1119 remote mappings and mappings obtained from MR
1120 * @return return code
1123 vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
1124 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
1125 u32 * res_map_index)
1127 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1128 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1129 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1130 u32 mi, ls_index = 0, dst_map_index;
1134 if (vnet_lisp_enable_disable_status () == 0)
1136 clib_warning ("LISP is disabled!");
1137 return VNET_API_ERROR_LISP_DISABLED;
1141 res_map_index[0] = ~0;
1143 memset (m_args, 0, sizeof (m_args[0]));
1144 memset (ls_args, 0, sizeof (ls_args[0]));
1146 ls_args->locators = rlocs;
1148 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
1149 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
1153 /* check if none of the locators match localy configured address */
1154 vec_foreach (loc, rlocs)
1156 ip_prefix_t *p = &gid_address_ippref (&loc->address);
1157 if (is_local_ip (lcm, &ip_prefix_addr (p)))
1159 clib_warning ("RLOC %U matches a local address!",
1160 format_gid_address, &loc->address);
1161 return VNET_API_ERROR_LISP_RLOC_LOCAL;
1165 /* overwrite: if mapping already exists, decide if locators should be
1166 * updated and be done */
1167 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
1169 if (!is_static && (old_map->is_static || old_map->local))
1171 /* do not overwrite local or static remote mappings */
1172 clib_warning ("mapping %U rejected due to collision with local "
1173 "or static remote mapping!", format_gid_address,
1178 locator_set_t *old_ls;
1180 /* update mapping attributes */
1181 old_map->action = action;
1182 old_map->authoritative = authoritative;
1185 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1186 old_map->locator_set_index);
1187 if (compare_locators (lcm, old_ls->locator_indices,
1190 /* set locator-set index to overwrite */
1191 ls_args->is_add = 1;
1192 ls_args->index = old_map->locator_set_index;
1193 vnet_lisp_add_del_locator_set (ls_args, 0);
1195 res_map_index[0] = mi;
1201 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1203 ls_args->is_add = 1;
1204 ls_args->index = ~0;
1206 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1209 gid_address_copy (&m_args->eid, eid);
1211 m_args->action = action;
1212 m_args->locator_set_index = ls_index;
1213 m_args->is_static = is_static;
1215 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
1218 res_map_index[0] = dst_map_index;
1223 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
1225 clib_warning ("cannot delete mapping for eid %U",
1226 format_gid_address, eid);
1231 gid_address_copy (&m_args->eid, eid);
1232 m_args->locator_set_index = old_map->locator_set_index;
1234 /* delete mapping associated from map-cache */
1235 vnet_lisp_map_cache_add_del (m_args, 0);
1237 ls_args->is_add = 0;
1238 ls_args->index = old_map->locator_set_index;
1239 /* delete locator set */
1240 vnet_lisp_add_del_locator_set (ls_args, 0);
1242 /* delete timer associated to the mapping if any */
1243 if (old_map->timer_set)
1244 mapping_delete_timer (lcm, mi);
1246 /* return old mapping index */
1248 res_map_index[0] = mi;
1256 vnet_lisp_clear_all_remote_adjacencies (void)
1259 u32 mi, *map_indices = 0, *map_indexp;
1260 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1261 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1262 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
1265 pool_foreach_index (mi, lcm->mapping_pool,
1267 vec_add1 (map_indices, mi);
1271 vec_foreach (map_indexp, map_indices)
1273 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1276 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
1278 dm_args->is_add = 0;
1279 gid_address_copy (&dm_args->eid, &map->eid);
1280 dm_args->locator_set_index = map->locator_set_index;
1282 /* delete mapping associated to fwd entry */
1283 vnet_lisp_map_cache_add_del (dm_args, 0);
1287 ls->index = map->locator_set_index;
1288 /* delete locator set */
1289 rv = vnet_lisp_add_del_locator_set (ls, 0);
1297 vec_free (map_indices);
1302 * Adds adjacency or removes forwarding entry associated to remote mapping.
1303 * Note that adjacencies are not stored, they only result in forwarding entries
1307 vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1309 lisp_cp_main_t *lcm = &lisp_control_main;
1310 u32 local_mi, remote_mi = ~0;
1312 if (vnet_lisp_enable_disable_status () == 0)
1314 clib_warning ("LISP is disabled!");
1315 return VNET_API_ERROR_LISP_DISABLED;
1318 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1319 &a->reid, &a->leid);
1320 if (GID_LOOKUP_MISS == remote_mi)
1322 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1323 format_gid_address, &a->reid);
1330 /* check if source eid has an associated mapping. If pitr mode is on,
1331 * just use the pitr's mapping */
1332 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
1333 gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->leid);
1335 if (GID_LOOKUP_MISS == local_mi)
1337 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1338 format_gid_address, &a->leid);
1343 /* update forwarding */
1344 dp_add_fwd_entry (lcm, local_mi, remote_mi);
1347 dp_del_fwd_entry (lcm, 0, remote_mi);
1353 vnet_lisp_set_map_request_mode (u8 mode)
1355 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1357 if (vnet_lisp_enable_disable_status () == 0)
1359 clib_warning ("LISP is disabled!");
1360 return VNET_API_ERROR_LISP_DISABLED;
1363 if (mode >= _MR_MODE_MAX)
1365 clib_warning ("Invalid LISP map request mode %d!", mode);
1366 return VNET_API_ERROR_INVALID_ARGUMENT;
1369 lcm->map_request_mode = mode;
1374 vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1376 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1377 u32 locator_set_index = ~0;
1381 if (vnet_lisp_enable_disable_status () == 0)
1383 clib_warning ("LISP is disabled!");
1384 return VNET_API_ERROR_LISP_DISABLED;
1387 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1390 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1393 locator_set_index = p[0];
1397 pool_get (lcm->mapping_pool, m);
1398 m->locator_set_index = locator_set_index;
1401 lcm->pitr_map_index = m - lcm->mapping_pool;
1403 /* enable pitr mode */
1408 /* remove pitr mapping */
1409 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1411 /* disable pitr mode */
1418 * Configure Proxy-ETR
1420 * @param ip PETR's IP address
1421 * @param is_add Flag that indicates if this is an addition or removal
1423 * return 0 on success
1426 vnet_lisp_use_petr (ip_address_t * ip, u8 is_add)
1428 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1431 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1434 if (vnet_lisp_enable_disable_status () == 0)
1436 clib_warning ("LISP is disabled!");
1437 return VNET_API_ERROR_LISP_DISABLED;
1440 memset (ls_args, 0, sizeof (*ls_args));
1444 /* Create dummy petr locator-set */
1445 memset (&loc, 0, sizeof (loc));
1446 gid_address_from_ip (&loc.address, ip);
1448 loc.state = loc.weight = 1;
1451 ls_args->is_add = 1;
1452 ls_args->index = ~0;
1453 vec_add1 (ls_args->locators, loc);
1454 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1456 /* Add petr mapping */
1457 pool_get (lcm->mapping_pool, m);
1458 m->locator_set_index = ls_index;
1459 lcm->petr_map_index = m - lcm->mapping_pool;
1461 /* Enable use-petr */
1462 lcm->flags |= LISP_FLAG_USE_PETR;
1466 m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1468 /* Remove petr locator */
1469 ls_args->is_add = 0;
1470 ls_args->index = m->locator_set_index;
1471 vnet_lisp_add_del_locator_set (ls_args, 0);
1473 /* Remove petr mapping */
1474 pool_put_index (lcm->mapping_pool, lcm->petr_map_index);
1476 /* Disable use-petr */
1477 lcm->flags &= ~LISP_FLAG_USE_PETR;
1482 /* cleans locator to locator-set data and removes locators not part of
1483 * any locator-set */
1485 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1487 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
1488 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1489 for (i = 0; i < vec_len (ls->locator_indices); i++)
1491 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1492 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1494 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1496 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1497 if (ls_indexp[0] == lsi)
1501 /* delete index for removed locator-set */
1502 vec_del1 (ls_indexes[0], j);
1504 /* delete locator if it's part of no locator-set */
1505 if (vec_len (ls_indexes[0]) == 0)
1507 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1508 vec_add1 (to_be_deleted, i);
1514 for (i = 0; i < vec_len (to_be_deleted); i++)
1516 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1517 vec_del1 (ls->locator_indices, loc_indexp[0]);
1519 vec_free (to_be_deleted);
1523 static inline uword *
1524 get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
1526 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1531 /* find locator-set */
1534 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
1545 is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
1551 ASSERT (ls != NULL);
1552 ASSERT (loc != NULL);
1554 vec_foreach (locit, ls->locator_indices)
1556 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1557 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1558 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1560 clib_warning ("Duplicate locator");
1561 return VNET_API_ERROR_VALUE_EXIST;
1569 update_adjacencies_by_map_index (lisp_cp_main_t * lcm, u8 is_local,
1570 u32 mapping_index, u8 remove_only)
1574 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1576 map = pool_elt_at_index (lcm->mapping_pool, mapping_index);
1579 pool_foreach(fwd, lcm->fwd_entry_pool,
1581 if ((is_local && 0 == gid_address_cmp (&map->eid, &fwd->leid)) ||
1582 (!is_local && 0 == gid_address_cmp (&map->eid, &fwd->reid)))
1585 gid_address_copy (&a->leid, &fwd->leid);
1586 gid_address_copy (&a->reid, &fwd->reid);
1588 vnet_lisp_add_del_adjacency (a);
1593 vnet_lisp_add_del_adjacency (a);
1601 update_fwd_entries_by_locator_set (lisp_cp_main_t * lcm, u8 is_local,
1602 u32 ls_index, u8 remove_only)
1607 if (vec_len (lcm->locator_set_to_eids) <= ls_index)
1610 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, ls_index);
1612 for (i = 0; i < vec_len (eid_indexes[0]); i++)
1614 map_indexp = vec_elt_at_index (eid_indexes[0], i);
1615 update_adjacencies_by_map_index (lcm, is_local, map_indexp[0],
1621 remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1622 u32 ls_index, u32 loc_id)
1624 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1625 u32 **ls_indexes = NULL;
1627 ASSERT (ls != NULL);
1628 ASSERT (locit != NULL);
1630 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1631 pool_put_index (lcm->locator_pool, locit[0]);
1632 vec_del1 (ls->locator_indices, loc_id);
1633 vec_del1 (ls_indexes[0], ls_index);
1637 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
1638 locator_set_t * ls, u32 * ls_result)
1640 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1641 locator_t *loc = NULL, *itloc = NULL;
1642 uword _p = (u32) ~ 0, *p = &_p;
1643 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
1649 if (vnet_lisp_enable_disable_status () == 0)
1651 clib_warning ("LISP is disabled!");
1652 return VNET_API_ERROR_LISP_DISABLED;
1655 p = get_locator_set_index (a, p);
1658 clib_warning ("locator-set %v doesn't exist", a->name);
1659 return VNET_API_ERROR_INVALID_ARGUMENT;
1664 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1667 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1669 return VNET_API_ERROR_INVALID_ARGUMENT;
1676 ls_result[0] = p[0];
1678 /* allocate locators */
1679 vec_foreach (itloc, a->locators)
1681 ret = is_locator_in_locator_set (lcm, ls, itloc);
1687 pool_get (lcm->locator_pool, loc);
1689 loc_index = loc - lcm->locator_pool;
1691 vec_add1 (ls->locator_indices, loc_index);
1693 vec_validate (lcm->locator_to_locator_sets, loc_index);
1694 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1696 vec_add1 (ls_indexes[0], p[0]);
1704 vec_foreach (itloc, a->locators)
1708 vec_foreach (locit, ls->locator_indices)
1710 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1712 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1715 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1717 if (0 == loc->local &&
1718 !gid_address_cmp (&loc->address, &itloc->address))
1721 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1726 /* update fwd entries using this locator in DP */
1727 update_fwd_entries_by_locator_set (lcm, loc->local, ls_index,
1728 vec_len (ls->locator_indices)
1741 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
1744 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1746 uword _p = (u32) ~ 0, *p = &_p;
1751 if (vnet_lisp_enable_disable_status () == 0)
1753 clib_warning ("LISP is disabled!");
1754 return VNET_API_ERROR_LISP_DISABLED;
1759 p = get_locator_set_index (a, p);
1762 if (p && p[0] != (u32) ~ 0)
1764 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1767 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1772 /* clean locator to locator-set vectors and remove locators if
1773 * they're not part of another locator-set */
1774 clean_locator_to_locator_set (lcm, p[0]);
1776 /* remove locator indices from locator set */
1777 vec_free (ls->locator_indices);
1782 ls_result[0] = p[0];
1784 /* new locator-set */
1787 pool_get (lcm->locator_set_pool, ls);
1788 memset (ls, 0, sizeof (*ls));
1789 ls_index = ls - lcm->locator_set_pool;
1793 ls->name = vec_dup (a->name);
1795 if (!lcm->locator_set_index_by_name)
1796 lcm->locator_set_index_by_name = hash_create_vec (
1804 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
1807 /* mark as local locator-set */
1808 vec_add1 (lcm->local_locator_set_indexes, ls_index);
1810 ls->local = a->local;
1812 ls_result[0] = ls_index;
1815 ret = vnet_lisp_add_del_locator (a, ls, NULL);
1823 p = get_locator_set_index (a, p);
1826 clib_warning ("locator-set %v doesn't exists", a->name);
1830 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1833 clib_warning ("locator-set with index %d doesn't exists", p[0]);
1837 if (lcm->mreq_itr_rlocs == p[0])
1839 clib_warning ("Can't delete the locator-set used to constrain "
1840 "the itr-rlocs in map-requests!");
1844 if (vec_len (lcm->locator_set_to_eids) != 0)
1846 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
1847 if (vec_len (eid_indexes[0]) != 0)
1850 ("Can't delete a locator that supports a mapping!");
1855 /* clean locator to locator-sets data */
1856 clean_locator_to_locator_set (lcm, p[0]);
1862 vec_foreach_index (it, lcm->local_locator_set_indexes)
1864 lsi = vec_elt (lcm->local_locator_set_indexes, it);
1867 vec_del1 (lcm->local_locator_set_indexes, it);
1871 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
1873 vec_free (ls->name);
1874 vec_free (ls->locator_indices);
1875 pool_put (lcm->locator_set_pool, ls);
1881 vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
1883 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1885 lcm->rloc_probing = is_enable;
1890 vnet_lisp_map_register_enable_disable (u8 is_enable)
1892 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1894 lcm->map_registering = is_enable;
1899 vnet_lisp_enable_disable (u8 is_enable)
1902 clib_error_t *error = 0;
1903 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1904 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
1906 a->is_en = is_enable;
1907 error = vnet_lisp_gpe_enable_disable (a);
1910 return clib_error_return (0, "failed to %s data-plane!",
1911 a->is_en ? "enable" : "disable");
1916 /* enable all l2 and l3 ifaces */
1919 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
1920 dp_add_del_iface(lcm, vni, 0, 1);
1922 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
1923 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
1929 /* clear interface table */
1930 hash_free (lcm->fwd_entry_by_mapping_index);
1931 pool_free (lcm->fwd_entry_pool);
1934 /* update global flag */
1935 lcm->is_enabled = is_enable;
1941 vnet_lisp_enable_disable_status (void)
1943 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1944 return lcm->is_enabled;
1948 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
1950 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1952 lisp_msmr_t _mr, *mr = &_mr;
1954 if (vnet_lisp_enable_disable_status () == 0)
1956 clib_warning ("LISP is disabled!");
1957 return VNET_API_ERROR_LISP_DISABLED;
1963 if (get_map_resolver (&a->address))
1965 clib_warning ("map-resolver %U already exists!", format_ip_address,
1970 memset (mr, 0, sizeof (*mr));
1971 ip_address_copy (&mr->address, &a->address);
1972 vec_add1 (lcm->map_resolvers, *mr);
1974 if (vec_len (lcm->map_resolvers) == 1)
1975 lcm->do_map_resolver_election = 1;
1979 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
1981 mr = vec_elt_at_index (lcm->map_resolvers, i);
1982 if (!ip_address_cmp (&mr->address, &a->address))
1984 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
1985 lcm->do_map_resolver_election = 1;
1987 vec_del1 (lcm->map_resolvers, i);
1996 vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
1998 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2001 if (vnet_lisp_enable_disable_status () == 0)
2003 clib_warning ("LISP is disabled!");
2004 return VNET_API_ERROR_LISP_DISABLED;
2009 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
2012 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
2013 return VNET_API_ERROR_INVALID_ARGUMENT;
2016 lcm->mreq_itr_rlocs = p[0];
2020 lcm->mreq_itr_rlocs = ~0;
2026 /* Statistics (not really errors) */
2027 #define foreach_lisp_cp_lookup_error \
2029 _(MAP_REQUESTS_SENT, "map-request sent") \
2030 _(ARP_REPLY_TX, "ARP replies sent")
2032 static char *lisp_cp_lookup_error_strings[] = {
2033 #define _(sym,string) string,
2034 foreach_lisp_cp_lookup_error
2040 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
2041 foreach_lisp_cp_lookup_error
2043 LISP_CP_LOOKUP_N_ERROR,
2044 } lisp_cp_lookup_error_t;
2048 LISP_CP_LOOKUP_NEXT_DROP,
2049 LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX,
2050 LISP_CP_LOOKUP_N_NEXT,
2051 } lisp_cp_lookup_next_t;
2055 gid_address_t dst_eid;
2056 ip_address_t map_resolver_ip;
2057 } lisp_cp_lookup_trace_t;
2060 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
2062 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2063 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2064 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
2066 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
2067 format_ip_address, &t->map_resolver_ip, format_gid_address,
2073 get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
2074 ip_address_t * sloc)
2079 if (vec_len (lcm->map_resolvers) == 0)
2081 clib_warning ("No map-resolver configured");
2085 /* find the first mr ip we have a route to and the ip of the
2086 * iface that has a route to it */
2087 vec_foreach (mrit, lcm->map_resolvers)
2090 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
2092 ip_address_copy (mr_ip, a);
2094 /* also update globals */
2099 clib_warning ("Can't find map-resolver and local interface ip!");
2103 static gid_address_t *
2104 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2110 ip_interface_address_t *ia = 0;
2111 gid_address_t gid_data, *gid = &gid_data;
2112 gid_address_t *rlocs = 0;
2113 ip_prefix_t *ippref = &gid_address_ippref (gid);
2114 ip_address_t *rloc = &ip_prefix_addr (ippref);
2116 memset (gid, 0, sizeof (gid[0]));
2117 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
2118 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
2120 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
2121 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2123 /* Add ipv4 locators first TODO sort them */
2126 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2127 loc->sw_if_index, 1 /* unnumbered */,
2129 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2130 ip_address_set (rloc, addr, IP4);
2131 ip_prefix_len (ippref) = 32;
2132 ip_prefix_normalize (ippref);
2133 vec_add1 (rlocs, gid[0]);
2136 /* Add ipv6 locators */
2137 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2138 loc->sw_if_index, 1 /* unnumbered */,
2140 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2141 ip_address_set (rloc, addr, IP6);
2142 ip_prefix_len (ippref) = 128;
2143 ip_prefix_normalize (ippref);
2144 vec_add1 (rlocs, gid[0]);
2152 static vlib_buffer_t *
2153 build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
2154 ip_address_t * sloc, ip_address_t * rloc,
2155 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
2159 vlib_main_t *vm = lcm->vlib_main;
2161 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2163 clib_warning ("Can't allocate buffer for Map-Request!");
2167 b = vlib_get_buffer (vm, bi);
2169 /* leave some space for the encap headers */
2170 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2173 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
2174 1 /* rloc probe */ , nonce_res);
2176 /* push outer ip header */
2177 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2185 static vlib_buffer_t *
2186 build_encapsulated_map_request (lisp_cp_main_t * lcm,
2187 gid_address_t * seid, gid_address_t * deid,
2188 locator_set_t * loc_set, ip_address_t * mr_ip,
2189 ip_address_t * sloc, u8 is_smr_invoked,
2190 u64 * nonce_res, u32 * bi_res)
2194 gid_address_t *rlocs = 0;
2195 vlib_main_t *vm = lcm->vlib_main;
2197 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2199 clib_warning ("Can't allocate buffer for Map-Request!");
2203 b = vlib_get_buffer (vm, bi);
2205 /* leave some space for the encap headers */
2206 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2209 rlocs = build_itr_rloc_list (lcm, loc_set);
2211 if (MR_MODE_SRC_DST == lcm->map_request_mode
2212 && GID_ADDR_SRC_DST != gid_address_type (deid))
2215 memset (&sd, 0, sizeof (sd));
2216 build_src_dst (&sd, seid, deid);
2217 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
2218 0 /* rloc probe */ , nonce_res);
2223 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
2224 0 /* rloc probe */ , nonce_res);
2227 /* push ecm: udp-ip-lisp */
2228 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2230 /* push outer ip header */
2231 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2241 reset_pending_mr_counters (pending_map_request_t * r)
2243 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2248 elect_map_resolver (lisp_cp_main_t * lcm)
2252 vec_foreach (mr, lcm->map_resolvers)
2256 ip_address_copy (&lcm->active_map_resolver, &mr->address);
2257 lcm->do_map_resolver_election = 0;
2265 free_map_register_records (mapping_t * maps)
2268 vec_foreach (map, maps) vec_free (map->locators);
2274 add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
2275 ip_address_t * probed_loc)
2278 locator_t *loc, new;
2279 ip_interface_address_t *ia = 0;
2281 ip_address_t *new_ip = &gid_address_ip (&new.address);
2284 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2286 vec_foreach (li, ls->locator_indices)
2288 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
2293 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2294 loc->sw_if_index, 1 /* unnumbered */,
2296 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
2298 ip_address_set (new_ip, addr, IP4);
2301 /* Add ipv6 locators */
2302 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2303 loc->sw_if_index, 1 /* unnumbered */,
2305 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
2307 ip_address_set (new_ip, addr, IP6);
2311 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
2314 vec_add1 (m->locators, new);
2319 build_map_register_record_list (lisp_cp_main_t * lcm)
2321 mapping_t *recs = 0, rec, *m;
2324 pool_foreach(m, lcm->mapping_pool,
2326 /* for now build only local mappings */
2331 add_locators (lcm, &rec, m->locator_set_index, NULL);
2332 vec_add1 (recs, rec);
2340 update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
2341 lisp_key_type_t key_id, u8 * key,
2342 u16 auth_data_len, u32 msg_len)
2344 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
2345 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
2347 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
2348 (unsigned char *) map_reg_hdr, msg_len, NULL,
2350 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
2355 static vlib_buffer_t *
2356 build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
2357 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
2358 mapping_t * records, lisp_key_type_t key_id, u8 * key,
2363 u32 bi, auth_data_len = 0, msg_len = 0;
2364 vlib_main_t *vm = lcm->vlib_main;
2366 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2368 clib_warning ("Can't allocate buffer for Map-Register!");
2372 b = vlib_get_buffer (vm, bi);
2374 /* leave some space for the encap headers */
2375 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2377 auth_data_len = auth_data_len_by_key_id (key_id);
2378 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
2379 auth_data_len, nonce_res,
2382 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
2385 /* push outer ip header */
2386 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2394 get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
2397 while (lcm->do_map_resolver_election
2398 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
2399 &lcm->active_map_resolver,
2402 if (0 == elect_map_resolver (lcm))
2403 /* all map resolvers are down */
2405 /* restart MR checking by marking all of them up */
2406 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
2413 /* CP output statistics */
2414 #define foreach_lisp_cp_output_error \
2415 _(MAP_REGISTERS_SENT, "map-registers sent") \
2416 _(RLOC_PROBES_SENT, "rloc-probes sent")
2418 static char *lisp_cp_output_error_strings[] = {
2419 #define _(sym,string) string,
2420 foreach_lisp_cp_output_error
2426 #define _(sym,str) LISP_CP_OUTPUT_ERROR_##sym,
2427 foreach_lisp_cp_output_error
2429 LISP_CP_OUTPUT_N_ERROR,
2430 } lisp_cp_output_error_t;
2433 lisp_cp_output (vlib_main_t * vm, vlib_node_runtime_t * node,
2434 vlib_frame_t * from_frame)
2439 /* dummy node used only for statistics */
2441 VLIB_REGISTER_NODE (lisp_cp_output_node) = {
2442 .function = lisp_cp_output,
2443 .name = "lisp-cp-output",
2444 .vector_size = sizeof (u32),
2445 .format_trace = format_lisp_cp_input_trace,
2446 .type = VLIB_NODE_TYPE_INTERNAL,
2448 .n_errors = LISP_CP_OUTPUT_N_ERROR,
2449 .error_strings = lisp_cp_output_error_strings,
2451 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2454 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2460 send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
2461 u32 local_locator_set_index, ip_address_t * sloc,
2462 ip_address_t * rloc)
2469 u32 next_index, *to_next;
2470 gid_address_t *itr_rlocs;
2472 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
2473 itr_rlocs = build_itr_rloc_list (lcm, ls);
2475 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
2476 vec_free (itr_rlocs);
2480 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2482 next_index = (ip_addr_version (rloc) == IP4) ?
2483 ip4_lookup_node.index : ip6_lookup_node.index;
2485 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2487 /* Enqueue the packet */
2488 to_next = vlib_frame_vector_args (f);
2491 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2493 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2498 send_rloc_probes (lisp_cp_main_t * lcm)
2504 u32 si, rloc_probes_sent = 0;
2507 pool_foreach (e, lcm->fwd_entry_pool,
2509 if (vec_len (e->locator_pairs) == 0)
2512 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
2515 clib_warning ("internal error: cannot find local eid %U in "
2516 "map-cache!", format_gid_address, &e->leid);
2519 lm = pool_elt_at_index (lcm->mapping_pool, si);
2521 /* get the best (lowest) priority */
2522 lprio = e->locator_pairs[0].priority;
2524 /* send rloc-probe for pair(s) with the best remote locator priority */
2525 vec_foreach (lp, e->locator_pairs)
2527 if (lp->priority != lprio)
2530 /* get first remote locator */
2531 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
2538 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2539 LISP_CP_OUTPUT_ERROR_RLOC_PROBES_SENT,
2545 send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
2547 u32 bi, map_registers_sent = 0;
2552 u32 next_index, *to_next;
2553 ip_address_t *ms = 0;
2554 mapping_t *records, *r, *group, *k;
2556 // TODO: support multiple map servers and do election
2557 if (0 == vec_len (lcm->map_servers))
2560 ms = &lcm->map_servers[0].address;
2562 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
2564 clib_warning ("no eligible interface address found for %U!",
2565 format_ip_address, &lcm->map_servers[0]);
2569 records = build_map_register_record_list (lcm);
2573 vec_foreach (r, records)
2576 u8 key_id = r->key_id;
2579 continue; /* no secret key -> map-register cannot be sent */
2582 vec_add1 (group, r[0]);
2584 /* group mappings that share common key */
2585 for (k = r + 1; k < vec_end (records); k++)
2587 if (k->key_id != r->key_id)
2590 if (vec_is_equal (k->key, r->key))
2592 vec_add1 (group, k[0]);
2593 k->key = 0; /* don't process this mapping again */
2597 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, group,
2603 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2605 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2606 ip4_lookup_node.index : ip6_lookup_node.index;
2608 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2610 /* Enqueue the packet */
2611 to_next = vlib_frame_vector_args (f);
2614 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2615 map_registers_sent++;
2617 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2619 free_map_register_records (records);
2621 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2622 LISP_CP_OUTPUT_ERROR_MAP_REGISTERS_SENT,
2623 map_registers_sent);
2628 #define send_encapsulated_map_request(lcm, seid, deid, smr) \
2629 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2631 #define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2632 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2635 _send_encapsulated_map_request (lisp_cp_main_t * lcm,
2636 gid_address_t * seid, gid_address_t * deid,
2637 u8 is_smr_invoked, u8 is_resend)
2639 u32 next_index, bi = 0, *to_next, map_index;
2643 locator_set_t *loc_set;
2645 pending_map_request_t *pmr, *duplicate_pmr = 0;
2649 /* if there is already a pending request remember it */
2652 pool_foreach(pmr, lcm->pending_map_requests_pool,
2654 if (!gid_address_cmp (&pmr->src, seid)
2655 && !gid_address_cmp (&pmr->dst, deid))
2657 duplicate_pmr = pmr;
2663 if (!is_resend && duplicate_pmr)
2665 /* don't send the request if there is a pending map request already */
2669 /* get locator-set for seid */
2670 if (!lcm->lisp_pitr)
2672 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2673 if (map_index == ~0)
2675 clib_warning ("No local mapping found in eid-table for %U!",
2676 format_gid_address, seid);
2680 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2685 ("Mapping found for src eid %U is not marked as local!",
2686 format_gid_address, seid);
2689 ls_index = map->locator_set_index;
2693 map_index = lcm->pitr_map_index;
2694 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
2695 ls_index = map->locator_set_index;
2698 /* overwrite locator set if map-request itr-rlocs configured */
2699 if (~0 != lcm->mreq_itr_rlocs)
2701 ls_index = lcm->mreq_itr_rlocs;
2704 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
2706 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
2709 duplicate_pmr->to_be_removed = 1;
2713 /* build the encapsulated map request */
2714 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
2715 &lcm->active_map_resolver,
2716 &sloc, is_smr_invoked, &nonce, &bi);
2721 /* set fib index to default and lookup node */
2722 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2723 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2724 ip4_lookup_node.index : ip6_lookup_node.index;
2726 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2728 /* Enqueue the packet */
2729 to_next = vlib_frame_vector_args (f);
2732 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2735 /* if there is a pending request already update it */
2737 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
2739 /* remove the oldest nonce */
2740 u64 CLIB_UNUSED (tmp), *nonce_del;
2741 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
2742 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
2743 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
2746 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
2747 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2748 duplicate_pmr - lcm->pending_map_requests_pool);
2752 /* add map-request to pending requests table */
2753 pool_get (lcm->pending_map_requests_pool, pmr);
2754 memset (pmr, 0, sizeof (*pmr));
2755 gid_address_copy (&pmr->src, seid);
2756 gid_address_copy (&pmr->dst, deid);
2757 clib_fifo_add1 (pmr->nonces, nonce);
2758 pmr->is_smr_invoked = is_smr_invoked;
2759 reset_pending_mr_counters (pmr);
2760 hash_set (lcm->pending_map_requests_by_nonce, nonce,
2761 pmr - lcm->pending_map_requests_pool);
2768 get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
2770 ip4_header_t *ip4 = hdr;
2773 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2775 ip_address_set (src, &ip4->src_address, IP4);
2776 ip_address_set (dst, &ip4->dst_address, IP4);
2781 ip_address_set (src, &ip6->src_address, IP6);
2782 ip_address_set (dst, &ip6->dst_address, IP6);
2787 lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2791 u32 vni = ~0, table_id = ~0;
2793 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
2794 IP4 ? FIB_PROTOCOL_IP4 :
2800 vnip = hash_get (lcm->vni_by_table_id, table_id);
2804 clib_warning ("vrf %d is not mapped to any vni!", table_id);
2810 lisp_get_bd_from_buffer_eth (vlib_buffer_t * b)
2814 l2input_main_t *l2im = &l2input_main;
2815 l2_input_config_t *config;
2816 l2_bridge_domain_t *bd_config;
2818 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
2819 config = vec_elt_at_index (l2im->configs, sw_if_index0);
2820 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
2822 return bd_config->bd_id;
2826 lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2830 u32 bd = lisp_get_bd_from_buffer_eth (b);
2832 vnip = hash_get (lcm->vni_by_bd_id, bd);
2836 clib_warning ("bridge domain %d is not mapped to any vni!", bd);
2842 get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
2843 gid_address_t * src, gid_address_t * dst,
2848 memset (src, 0, sizeof (*src));
2849 memset (dst, 0, sizeof (*dst));
2851 gid_address_type (dst) = GID_ADDR_NO_ADDRESS;
2852 gid_address_type (src) = GID_ADDR_NO_ADDRESS;
2854 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
2857 u8 version, preflen;
2859 gid_address_type (src) = GID_ADDR_IP_PREFIX;
2860 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
2862 ip = vlib_buffer_get_current (b);
2863 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
2865 version = gid_address_ip_version (src);
2866 preflen = ip_address_max_len (version);
2867 gid_address_ippref_len (src) = preflen;
2868 gid_address_ippref_len (dst) = preflen;
2870 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
2871 gid_address_vni (dst) = vni;
2872 gid_address_vni (src) = vni;
2874 else if (LISP_AFI_MAC == type)
2876 ethernet_header_t *eh;
2877 ethernet_arp_header_t *ah;
2879 eh = vlib_buffer_get_current (b);
2881 if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_ARP)
2883 ah = (ethernet_arp_header_t *) (((u8 *) eh) + sizeof (*eh));
2884 if (clib_net_to_host_u16 (ah->opcode)
2885 != ETHERNET_ARP_OPCODE_request)
2888 gid_address_type (dst) = GID_ADDR_ARP;
2889 gid_address_arp_bd (dst) = lisp_get_bd_from_buffer_eth (b);
2890 clib_memcpy (&gid_address_arp_ip4 (dst),
2891 &ah->ip4_over_ethernet[1].ip4, 4);
2895 gid_address_type (src) = GID_ADDR_MAC;
2896 gid_address_type (dst) = GID_ADDR_MAC;
2897 mac_copy (&gid_address_mac (src), eh->src_address);
2898 mac_copy (&gid_address_mac (dst), eh->dst_address);
2901 vni = lisp_get_vni_from_buffer_eth (lcm, b);
2903 gid_address_vni (dst) = vni;
2904 gid_address_vni (src) = vni;
2907 else if (LISP_AFI_LCAF == type)
2909 /* Eventually extend this to support NSH and other */
2915 lisp_cp_lookup_inline (vlib_main_t * vm,
2916 vlib_node_runtime_t * node,
2917 vlib_frame_t * from_frame, int overlay)
2919 u32 *from, *to_next, di, si;
2920 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2921 u32 pkts_mapped = 0, next_index;
2922 uword n_left_from, n_left_to_next;
2923 vnet_main_t *vnm = vnet_get_main ();
2925 from = vlib_frame_vector_args (from_frame);
2926 n_left_from = from_frame->n_vectors;
2927 next_index = node->cached_next_index;
2929 while (n_left_from > 0)
2931 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2933 while (n_left_from > 0 && n_left_to_next > 0)
2935 u32 pi0, sw_if_index0, next0;
2938 gid_address_t src, dst;
2939 ethernet_arp_header_t *arp0;
2940 ethernet_header_t *eth0;
2941 vnet_hw_interface_t *hw_if0;
2948 n_left_to_next -= 1;
2950 b0 = vlib_get_buffer (vm, pi0);
2952 /* src/dst eid pair */
2953 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst, overlay);
2955 if (gid_address_type (&dst) == GID_ADDR_ARP)
2957 mac0 = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
2958 if (GID_LOOKUP_MISS_L2 != mac0)
2960 /* send ARP reply */
2962 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2963 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
2965 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
2967 eth0 = vlib_buffer_get_current (b0);
2968 arp0 = (ethernet_arp_header_t *) (((u8 *) eth0)
2971 clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply);
2972 arp0->ip4_over_ethernet[1] = arp0->ip4_over_ethernet[0];
2973 clib_memcpy (arp0->ip4_over_ethernet[0].ethernet,
2975 clib_memcpy (&arp0->ip4_over_ethernet[0].ip4,
2976 &gid_address_arp_ip4 (&dst), 4);
2978 /* Hardware must be ethernet-like. */
2979 ASSERT (vec_len (hw_if0->hw_address) == 6);
2981 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
2982 clib_memcpy (eth0->src_address, hw_if0->hw_address, 6);
2984 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_ARP_REPLY_TX];
2985 next0 = LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX;
2986 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2988 n_left_to_next, pi0,
2994 /* if we have remote mapping for destination already in map-chache
2995 add forwarding tunnel directly. If not send a map-request */
2996 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3000 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3001 /* send a map-request also in case of negative mapping entry
3002 with corresponding action */
3003 if (m->action == LISP_SEND_MAP_REQUEST)
3005 /* send map-request */
3006 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3007 0 /* is_resend */ );
3012 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3016 dp_add_fwd_entry_from_mt (si, di);
3022 /* send map-request */
3023 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3024 0 /* is_resend */ );
3028 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
3029 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3031 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3034 memset (tr, 0, sizeof (*tr));
3035 gid_address_copy (&tr->dst_eid, &dst);
3036 ip_address_copy (&tr->map_resolver_ip,
3037 &lcm->active_map_resolver);
3039 gid_address_free (&dst);
3040 gid_address_free (&src);
3041 next0 = LISP_CP_LOOKUP_NEXT_DROP;
3042 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3044 n_left_to_next, pi0, next0);
3047 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3049 vlib_node_increment_counter (vm, node->node_index,
3050 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
3052 return from_frame->n_vectors;
3056 lisp_cp_lookup_ip4 (vlib_main_t * vm,
3057 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3059 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3063 lisp_cp_lookup_ip6 (vlib_main_t * vm,
3064 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3066 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
3070 lisp_cp_lookup_l2 (vlib_main_t * vm,
3071 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3073 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
3077 lisp_cp_lookup_nsh (vlib_main_t * vm,
3078 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3080 /* TODO decide if NSH should be propagated as LCAF or not */
3081 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_LCAF));
3085 VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
3086 .function = lisp_cp_lookup_ip4,
3087 .name = "lisp-cp-lookup-ip4",
3088 .vector_size = sizeof (u32),
3089 .format_trace = format_lisp_cp_lookup_trace,
3090 .type = VLIB_NODE_TYPE_INTERNAL,
3092 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3093 .error_strings = lisp_cp_lookup_error_strings,
3095 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3098 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3099 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
3105 VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
3106 .function = lisp_cp_lookup_ip6,
3107 .name = "lisp-cp-lookup-ip6",
3108 .vector_size = sizeof (u32),
3109 .format_trace = format_lisp_cp_lookup_trace,
3110 .type = VLIB_NODE_TYPE_INTERNAL,
3112 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3113 .error_strings = lisp_cp_lookup_error_strings,
3115 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3118 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3119 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
3125 VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
3126 .function = lisp_cp_lookup_l2,
3127 .name = "lisp-cp-lookup-l2",
3128 .vector_size = sizeof (u32),
3129 .format_trace = format_lisp_cp_lookup_trace,
3130 .type = VLIB_NODE_TYPE_INTERNAL,
3132 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3133 .error_strings = lisp_cp_lookup_error_strings,
3135 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3138 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3139 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
3145 VLIB_REGISTER_NODE (lisp_cp_lookup_nsh_node) = {
3146 .function = lisp_cp_lookup_nsh,
3147 .name = "lisp-cp-lookup-nsh",
3148 .vector_size = sizeof (u32),
3149 .format_trace = format_lisp_cp_lookup_trace,
3150 .type = VLIB_NODE_TYPE_INTERNAL,
3152 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3153 .error_strings = lisp_cp_lookup_error_strings,
3155 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3158 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
3159 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
3164 /* lisp_cp_input statistics */
3165 #define foreach_lisp_cp_input_error \
3167 _(RLOC_PROBE_REQ_RECEIVED, "rloc-probe requests received") \
3168 _(RLOC_PROBE_REP_RECEIVED, "rloc-probe replies received") \
3169 _(MAP_NOTIFIES_RECEIVED, "map-notifies received") \
3170 _(MAP_REPLIES_RECEIVED, "map-replies received")
3172 static char *lisp_cp_input_error_strings[] = {
3173 #define _(sym,string) string,
3174 foreach_lisp_cp_input_error
3180 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
3181 foreach_lisp_cp_input_error
3183 LISP_CP_INPUT_N_ERROR,
3184 } lisp_cp_input_error_t;
3188 gid_address_t dst_eid;
3189 ip4_address_t map_resolver_ip;
3190 } lisp_cp_input_trace_t;
3193 format_lisp_cp_input_trace (u8 * s, va_list * args)
3195 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3196 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
3197 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
3198 va_arg (*args, lisp_cp_input_trace_t *);
3200 s = format (s, "LISP-CP-INPUT: TODO");
3205 remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
3208 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3209 memset (adj_args, 0, sizeof (adj_args[0]));
3211 m = pool_elt_at_index (lcm->mapping_pool, mi);
3213 gid_address_copy (&adj_args->reid, &m->eid);
3214 adj_args->is_add = 0;
3215 if (vnet_lisp_add_del_adjacency (adj_args))
3216 clib_warning ("failed to del adjacency!");
3218 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
3219 0 /* is_static */ , 0);
3220 mapping_delete_timer (lcm, mi);
3224 mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
3225 f64 expiration_time)
3228 u64 now = clib_cpu_time_now ();
3229 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
3230 u64 exp_clock_time = now + expiration_time * cpu_cps;
3232 m = pool_elt_at_index (lcm->mapping_pool, mi);
3235 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
3239 map_records_arg_free (map_records_arg_t * a)
3242 vec_foreach (m, a->mappings)
3244 vec_free (m->locators);
3245 gid_address_free (&m->eid);
3252 process_map_reply (map_records_arg_t * a)
3255 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3256 u32 dst_map_index = 0;
3257 pending_map_request_t *pmr;
3261 if (a->is_rloc_probe)
3264 /* Check pending requests table and nonce */
3265 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
3268 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
3271 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
3273 vec_foreach (m, a->mappings)
3275 /* insert/update mappings cache */
3276 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
3277 m->authoritative, m->ttl,
3278 1, 0 /* is_static */ , &dst_map_index);
3280 if (dst_map_index == (u32) ~ 0)
3283 /* try to program forwarding only if mapping saved or updated */
3284 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3285 memset (adj_args, 0, sizeof (adj_args[0]));
3287 gid_address_copy (&adj_args->leid, &pmr->src);
3288 gid_address_copy (&adj_args->reid, &m->eid);
3289 adj_args->is_add = 1;
3290 if (vnet_lisp_add_del_adjacency (adj_args))
3291 clib_warning ("failed to add adjacency!");
3293 if ((u32) ~ 0 != m->ttl)
3294 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
3297 /* remove pending map request entry */
3300 clib_fifo_foreach (noncep, pmr->nonces, ({
3301 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
3305 clib_fifo_free (pmr->nonces);
3306 pool_put (lcm->pending_map_requests_pool, pmr);
3309 map_records_arg_free (a);
3314 is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
3315 lisp_key_type_t key_id, u8 * key)
3321 auth_data_len = auth_data_len_by_key_id (key_id);
3322 if ((u16) ~ 0 == auth_data_len)
3324 clib_warning ("invalid length for key_id %d!", key_id);
3328 /* save auth data */
3329 vec_validate (auth_data, auth_data_len - 1);
3330 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
3332 /* clear auth data */
3333 memset (MNOTIFY_DATA (h), 0, auth_data_len);
3335 /* get hash of the message */
3336 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3337 (unsigned char *) h, msg_len, NULL, NULL);
3339 result = memcmp (code, auth_data, auth_data_len);
3341 vec_free (auth_data);
3347 process_map_notify (map_records_arg_t * a)
3349 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3352 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
3355 clib_warning ("No pending map-register entry with nonce %lu!",
3360 map_records_arg_free (a);
3361 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
3365 get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
3369 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
3372 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
3376 return pool_elt_at_index (lcm->mapping_pool, mi);
3380 * When map-notify is received it is necessary that all EIDs in the record
3381 * list share common key. The key is then used to verify authentication
3382 * data in map-notify message.
3385 map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
3386 u32 key_id, u8 ** key_out)
3388 u32 i, len = vec_len (maps);
3391 /* get key of the first mapping */
3392 m = get_mapping (lcm, &maps[0].eid);
3396 key_out[0] = m->key;
3398 for (i = 1; i < len; i++)
3400 m = get_mapping (lcm, &maps[i].eid);
3404 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
3406 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
3414 parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
3416 locator_t *locators = 0;
3422 memset (&m, 0, sizeof (m));
3424 /* parse record eid */
3425 for (i = 0; i < count; i++)
3428 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
3431 clib_warning ("Failed to parse mapping record!");
3432 vec_foreach (loc, locators) locator_free (loc);
3433 vec_free (locators);
3437 m.locators = locators;
3438 gid_address_copy (&m.eid, &deid);
3439 vec_add1 (a->mappings, m);
3445 static map_records_arg_t *
3446 parse_map_notify (vlib_buffer_t * b)
3449 map_notify_hdr_t *mnotif_hdr;
3450 lisp_key_type_t key_id;
3451 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3454 u16 auth_data_len = 0;
3456 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3458 memset (a, 0, sizeof (*a));
3459 mnotif_hdr = vlib_buffer_get_current (b);
3460 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
3461 memset (&deid, 0, sizeof (deid));
3463 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
3464 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
3465 auth_data_len = auth_data_len_by_key_id (key_id);
3467 /* advance buffer by authentication data */
3468 vlib_buffer_pull (b, auth_data_len);
3470 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
3471 rc = parse_map_records (b, a, record_count);
3474 map_records_arg_free (a);
3478 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
3481 map_records_arg_free (a);
3485 /* verify authentication data */
3486 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
3487 - (u8 *) mnotif_hdr, key_id, key))
3489 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
3491 map_records_arg_free (a);
3497 static vlib_buffer_t *
3498 build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
3499 ip_address_t * dst, u64 nonce, u8 probe_bit,
3500 mapping_t * records, u16 dst_port, u32 * bi_res)
3504 vlib_main_t *vm = lcm->vlib_main;
3506 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3508 clib_warning ("Can't allocate buffer for Map-Register!");
3512 b = vlib_get_buffer (vm, bi);
3514 /* leave some space for the encap headers */
3515 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3517 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
3519 /* push outer ip header */
3520 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
3527 send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
3528 u8 probe_bit, u64 nonce, u16 dst_port,
3529 ip_address_t * probed_loc)
3535 u32 next_index, *to_next;
3536 mapping_t *records = 0, *m;
3538 m = pool_elt_at_index (lcm->mapping_pool, mi);
3542 vec_add1 (records, m[0]);
3543 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
3544 memset (&src, 0, sizeof (src));
3546 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
3548 clib_warning ("can't find inteface address for %U", format_ip_address,
3553 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
3557 free_map_register_records (records);
3559 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3560 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3561 ip4_lookup_node.index : ip6_lookup_node.index;
3563 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3565 /* Enqueue the packet */
3566 to_next = vlib_frame_vector_args (f);
3569 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3574 find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
3576 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
3577 if (start < 0 && start < -sizeof (b->pre_data))
3583 *ip_hdr = b->data + start;
3584 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
3589 process_map_request (vlib_main_t * vm, vlib_node_runtime_t * node,
3590 lisp_cp_main_t * lcm, vlib_buffer_t * b)
3593 ip_address_t *dst_loc = 0, probed_loc, src_loc;
3595 map_request_hdr_t *mreq_hdr;
3596 gid_address_t src, dst;
3598 u32 i, len = 0, rloc_probe_recv = 0;
3599 gid_address_t *itr_rlocs = 0;
3601 mreq_hdr = vlib_buffer_get_current (b);
3602 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
3605 ("Only SMR Map-Requests and RLOC probe supported for now!");
3609 vlib_buffer_pull (b, sizeof (*mreq_hdr));
3610 nonce = MREQ_NONCE (mreq_hdr);
3613 len = lisp_msg_parse_addr (b, &src);
3617 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3618 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
3622 /* parse eid records and send SMR-invoked map-requests */
3623 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
3625 memset (&dst, 0, sizeof (dst));
3626 len = lisp_msg_parse_eid_rec (b, &dst);
3629 clib_warning ("Can't parse map-request EID-record");
3633 if (MREQ_SMR (mreq_hdr))
3635 /* send SMR-invoked map-requests */
3636 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3638 else if (MREQ_RLOC_PROBE (mreq_hdr))
3640 find_ip_header (b, &ip_hdr);
3643 clib_warning ("Cannot find the IP header!");
3647 memset (&m, 0, sizeof (m));
3648 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3650 // TODO: select best locator; for now use the first one
3651 dst_loc = &gid_address_ip (&itr_rlocs[0]);
3653 /* get src/dst IP addresses */
3654 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
3656 // TODO get source port from buffer
3657 u16 src_port = LISP_CONTROL_PORT;
3659 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
3660 src_port, &probed_loc);
3665 vlib_node_increment_counter (vm, node->node_index,
3666 LISP_CP_INPUT_ERROR_RLOC_PROBE_REQ_RECEIVED,
3668 vec_free (itr_rlocs);
3672 parse_map_reply (vlib_buffer_t * b)
3679 map_reply_hdr_t *mrep_hdr;
3680 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3681 memset (a, 0, sizeof (*a));
3682 locator_t *locators;
3684 mrep_hdr = vlib_buffer_get_current (b);
3685 a->nonce = MREP_NONCE (mrep_hdr);
3686 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
3687 if (!vlib_buffer_has_space (b, sizeof (*mrep_hdr)))
3692 vlib_buffer_pull (b, sizeof (*mrep_hdr));
3694 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3696 memset (&m, 0, sizeof (m));
3698 h = vlib_buffer_get_current (b);
3700 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3701 m.action = MAP_REC_ACTION (h);
3702 m.authoritative = MAP_REC_AUTH (h);
3704 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3707 clib_warning ("Failed to parse mapping record!");
3708 map_records_arg_free (a);
3712 m.locators = locators;
3713 gid_address_copy (&m.eid, &deid);
3714 vec_add1 (a->mappings, m);
3720 queue_map_reply_for_processing (map_records_arg_t * a)
3722 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
3726 queue_map_notify_for_processing (map_records_arg_t * a)
3728 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
3732 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
3733 vlib_frame_t * from_frame)
3735 u32 n_left_from, *from, *to_next_drop, rloc_probe_rep_recv = 0,
3736 map_notifies_recv = 0;
3737 lisp_msg_type_e type;
3738 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3739 map_records_arg_t *a;
3741 from = vlib_frame_vector_args (from_frame);
3742 n_left_from = from_frame->n_vectors;
3745 while (n_left_from > 0)
3747 u32 n_left_to_next_drop;
3749 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3750 to_next_drop, n_left_to_next_drop);
3751 while (n_left_from > 0 && n_left_to_next_drop > 0)
3759 to_next_drop[0] = bi0;
3761 n_left_to_next_drop -= 1;
3763 b0 = vlib_get_buffer (vm, bi0);
3765 type = lisp_msg_type (vlib_buffer_get_current (b0));
3768 case LISP_MAP_REPLY:
3769 a = parse_map_reply (b0);
3772 if (a->is_rloc_probe)
3773 rloc_probe_rep_recv++;
3774 queue_map_reply_for_processing (a);
3777 case LISP_MAP_REQUEST:
3778 process_map_request (vm, node, lcm, b0);
3780 case LISP_MAP_NOTIFY:
3781 a = parse_map_notify (b0);
3784 map_notifies_recv++;
3785 queue_map_notify_for_processing (a);
3789 clib_warning ("Unsupported LISP message type %d", type);
3793 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
3795 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3801 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3802 n_left_to_next_drop);
3804 vlib_node_increment_counter (vm, node->node_index,
3805 LISP_CP_INPUT_ERROR_RLOC_PROBE_REP_RECEIVED,
3806 rloc_probe_rep_recv);
3807 vlib_node_increment_counter (vm, node->node_index,
3808 LISP_CP_INPUT_ERROR_MAP_NOTIFIES_RECEIVED,
3810 return from_frame->n_vectors;
3814 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3815 .function = lisp_cp_input,
3816 .name = "lisp-cp-input",
3817 .vector_size = sizeof (u32),
3818 .format_trace = format_lisp_cp_input_trace,
3819 .type = VLIB_NODE_TYPE_INTERNAL,
3821 .n_errors = LISP_CP_INPUT_N_ERROR,
3822 .error_strings = lisp_cp_input_error_strings,
3824 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3827 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3833 lisp_cp_init (vlib_main_t * vm)
3835 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3836 clib_error_t *error = 0;
3838 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3841 lcm->im4 = &ip4_main;
3842 lcm->im6 = &ip6_main;
3843 lcm->vlib_main = vm;
3844 lcm->vnet_main = vnet_get_main ();
3845 lcm->mreq_itr_rlocs = ~0;
3848 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
3850 gid_dictionary_init (&lcm->mapping_index_by_gid);
3851 lcm->do_map_resolver_election = 1;
3852 lcm->map_request_mode = MR_MODE_DST_ONLY;
3854 /* default vrf mapped to vni 0 */
3855 hash_set (lcm->table_id_by_vni, 0, 0);
3856 hash_set (lcm->vni_by_table_id, 0, 0);
3858 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
3859 lisp_cp_input_node.index, 1 /* is_ip4 */ );
3860 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
3861 lisp_cp_input_node.index, 0 /* is_ip4 */ );
3863 u64 now = clib_cpu_time_now ();
3864 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
3869 lisp_stats_api_fill (lisp_cp_main_t * lcm, lisp_gpe_main_t * lgm,
3870 lisp_api_stats_t * stat, lisp_stats_key_t * key,
3874 vlib_combined_counter_main_t *cm = &lgm->counters;
3875 lisp_gpe_fwd_entry_key_t fwd_key;
3876 const lisp_gpe_tunnel_t *lgt;
3879 memset (stat, 0, sizeof (*stat));
3880 memset (&fwd_key, 0, sizeof (fwd_key));
3882 fe = pool_elt_at_index (lcm->fwd_entry_pool, key->fwd_entry_index);
3885 gid_to_dp_address (&fe->reid, &stat->deid);
3886 gid_to_dp_address (&fe->leid, &stat->seid);
3887 stat->vni = gid_address_vni (&fe->reid);
3889 lgt = lisp_gpe_tunnel_get (key->tunnel_index);
3890 stat->loc_rloc = lgt->key->lcl;
3891 stat->rmt_rloc = lgt->key->rmt;
3893 vlib_get_combined_counter (cm, stats_index, &v);
3899 vnet_lisp_get_stats (void)
3901 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
3902 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3903 lisp_api_stats_t *stats = 0, stat;
3904 lisp_stats_key_t *key;
3908 hash_foreach_mem (key, index, lgm->lisp_stats_index_by_key,
3910 if (lisp_stats_api_fill (lcm, lgm, &stat, key, index))
3911 vec_add1 (stats, stat);
3919 send_map_request_thread_fn (void *arg)
3921 map_request_args_t *a = arg;
3922 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3925 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3927 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3933 queue_map_request (gid_address_t * seid, gid_address_t * deid,
3934 u8 smr_invoked, u8 is_resend)
3936 map_request_args_t a;
3938 a.is_resend = is_resend;
3939 gid_address_copy (&a.seid, seid);
3940 gid_address_copy (&a.deid, deid);
3941 a.smr_invoked = smr_invoked;
3943 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
3944 (u8 *) & a, sizeof (a));
3949 * Take an action with a pending map request depending on expiration time
3950 * and re-try counters.
3953 update_pending_request (pending_map_request_t * r, f64 dt)
3955 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3958 if (r->time_to_expire - dt < 0)
3959 /* it's time to decide what to do with this pending request */
3961 if (r->retries_num >= NUMBER_OF_RETRIES)
3962 /* too many retries -> assume current map resolver is not available */
3964 mr = get_map_resolver (&lcm->active_map_resolver);
3967 clib_warning ("Map resolver %U not found - probably deleted "
3968 "by the user recently.", format_ip_address,
3969 &lcm->active_map_resolver);
3973 clib_warning ("map resolver %U is unreachable, ignoring",
3974 format_ip_address, &lcm->active_map_resolver);
3976 /* mark current map resolver unavailable so it won't be
3977 * selected next time */
3979 mr->last_update = vlib_time_now (lcm->vlib_main);
3982 reset_pending_mr_counters (r);
3983 elect_map_resolver (lcm);
3985 /* try to find a next eligible map resolver and re-send */
3986 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3992 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3995 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3999 r->time_to_expire -= dt;
4003 remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4006 pending_map_request_t *pmr;
4007 u32 *to_be_removed = 0, *pmr_index;
4010 pool_foreach (pmr, lcm->pending_map_requests_pool,
4012 if (pmr->to_be_removed)
4014 clib_fifo_foreach (nonce, pmr->nonces, ({
4015 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4018 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4023 vec_foreach (pmr_index, to_be_removed)
4024 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
4026 vec_free (to_be_removed);
4030 update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4032 static f64 time_left = RLOC_PROBING_INTERVAL;
4034 if (!lcm->is_enabled || !lcm->rloc_probing)
4040 time_left = RLOC_PROBING_INTERVAL;
4041 send_rloc_probes (lcm);
4046 update_map_register (lisp_cp_main_t * lcm, f64 dt)
4048 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4049 static u64 mreg_sent_counter = 0;
4051 if (!lcm->is_enabled || !lcm->map_registering)
4057 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4058 time_left = MAP_REGISTER_INTERVAL;
4061 mreg_sent_counter++;
4062 time_left = QUICK_MAP_REGISTER_INTERVAL;
4064 send_map_register (lcm, 1 /* want map notify */ );
4069 send_map_resolver_service (vlib_main_t * vm,
4070 vlib_node_runtime_t * rt, vlib_frame_t * f)
4074 pending_map_request_t *pmr;
4075 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4079 vlib_process_wait_for_event_or_clock (vm, period);
4081 /* currently no signals are expected - just wait for clock */
4082 (void) vlib_process_get_events (vm, 0);
4085 pool_foreach (pmr, lcm->pending_map_requests_pool,
4087 if (!pmr->to_be_removed)
4088 update_pending_request (pmr, period);
4092 remove_dead_pending_map_requests (lcm);
4094 update_map_register (lcm, period);
4095 update_rloc_probing (lcm, period);
4097 u64 now = clib_cpu_time_now ();
4099 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
4100 if (vec_len (expired) > 0)
4103 vec_foreach (mi, expired)
4105 remove_expired_mapping (lcm, mi[0]);
4107 _vec_len (expired) = 0;
4116 vnet_lisp_stats_enable_disable (u8 enable)
4118 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4120 if (vnet_lisp_enable_disable_status () == 0)
4121 return VNET_API_ERROR_LISP_DISABLED;
4124 lcm->flags |= LISP_FLAG_STATS_ENABLED;
4126 lcm->flags &= ~LISP_FLAG_STATS_ENABLED;
4132 vnet_lisp_stats_enable_disable_state (void)
4134 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4136 if (vnet_lisp_enable_disable_status () == 0)
4137 return VNET_API_ERROR_LISP_DISABLED;
4139 return lcm->flags & LISP_FLAG_STATS_ENABLED;
4143 VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
4144 .function = send_map_resolver_service,
4145 .type = VLIB_NODE_TYPE_PROCESS,
4146 .name = "lisp-retry-service",
4147 .process_log2_n_stack_bytes = 16,
4151 VLIB_INIT_FUNCTION (lisp_cp_init);
4154 * fd.io coding-style-patch-verification: ON
4157 * eval: (c-set-style "gnu")