blob: b67d0e9b18cd83ce9f79afb9b3ed48e7d1ae64b6 [file] [log] [blame]
Florin Corase127a7e2016-02-18 22:20:01 +01001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
Filip Tehlara5abdeb2016-07-18 17:35:40 +020016#include <vlibmemory/api.h>
Florin Corase127a7e2016-02-18 22:20:01 +010017#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.h>
Neale Ranns5e575b12016-10-03 09:40:25 +010021#include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
22#include <vnet/lisp-gpe/lisp_gpe_tenant.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010023#include <vnet/fib/fib_entry.h>
24#include <vnet/fib/fib_table.h>
Florin Corase127a7e2016-02-18 22:20:01 +010025
Filip Tehlar397fd7d2016-10-26 14:31:24 +020026#include <openssl/evp.h>
27#include <openssl/hmac.h>
28
Filip Tehlara5abdeb2016-07-18 17:35:40 +020029typedef struct
30{
31 u8 is_resend;
32 gid_address_t seid;
33 gid_address_t deid;
34 u8 smr_invoked;
35} map_request_args_t;
36
Filip Tehlarcdab4bd2016-12-06 10:31:57 +010037typedef struct
38{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +010039 u64 nonce;
Filip Tehlarfb9931f2016-12-09 13:52:38 +010040 u8 is_rloc_probe;
41 mapping_t *mappings;
42} map_records_arg_t;
43
44static int
45lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
46 gid_address_t * remote_eid, u8 is_add);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +010047
Florin Corasdca88042016-09-14 16:01:38 +020048u8
49vnet_lisp_get_map_request_mode (void)
50{
51 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
52 return lcm->map_request_mode;
53}
54
Filip Tehlar397fd7d2016-10-26 14:31:24 +020055static u16
56auth_data_len_by_key_id (lisp_key_type_t key_id)
57{
58 switch (key_id)
59 {
60 case HMAC_SHA_1_96:
61 return SHA1_AUTH_DATA_LEN;
62 case HMAC_SHA_256_128:
63 return SHA256_AUTH_DATA_LEN;
64 default:
65 clib_warning ("unsupported key type: %d!", key_id);
66 return (u16) ~ 0;
67 }
68 return (u16) ~ 0;
69}
70
71static const EVP_MD *
72get_encrypt_fcn (lisp_key_type_t key_id)
73{
74 switch (key_id)
75 {
76 case HMAC_SHA_1_96:
77 return EVP_sha1 ();
78 case HMAC_SHA_256_128:
79 return EVP_sha256 ();
80 default:
81 clib_warning ("unsupported encryption key type: %d!", key_id);
82 break;
83 }
84 return 0;
85}
86
Filip Tehlara5abdeb2016-07-18 17:35:40 +020087static int
88queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +020089 u8 smr_invoked, u8 is_resend);
Filip Tehlara5abdeb2016-07-18 17:35:40 +020090
Florin Corasf727db92016-06-23 15:01:58 +020091ip_interface_address_t *
Florin Corasa2157cf2016-08-16 21:09:14 +020092ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
93 u32 sw_if_index, u8 loop)
Florin Corasf727db92016-06-23 15:01:58 +020094{
95 vnet_main_t *vnm = vnet_get_main ();
Florin Corasa2157cf2016-08-16 21:09:14 +020096 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
Florin Corasf727db92016-06-23 15:01:58 +020097 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
98 sw_if_index = swif->unnumbered_sw_if_index;
99 u32 ia =
Florin Corasa2157cf2016-08-16 21:09:14 +0200100 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
101 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
102 (u32) ~ 0;
103 return pool_elt_at_index ((lm)->if_address_pool, ia);
Florin Corasf727db92016-06-23 15:01:58 +0200104}
Filip Tehlar215104e2016-05-10 16:58:29 +0200105
Florin Corasf727db92016-06-23 15:01:58 +0200106void *
107ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200108 u8 version)
Florin Corasf727db92016-06-23 15:01:58 +0200109{
Florin Corasa2157cf2016-08-16 21:09:14 +0200110 ip_interface_address_t *ia;
Filip Tehlar195bcee2016-05-13 17:37:35 +0200111
Florin Corasf727db92016-06-23 15:01:58 +0200112 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
113 if (!ia)
114 return 0;
115 return ip_interface_address_get_address (lm, ia);
116}
Filip Tehlar195bcee2016-05-13 17:37:35 +0200117
Florin Corase127a7e2016-02-18 22:20:01 +0100118int
Florin Corasf727db92016-06-23 15:01:58 +0200119ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200120 u8 version, ip_address_t * result)
Florin Corasf727db92016-06-23 15:01:58 +0200121{
Florin Corasa2157cf2016-08-16 21:09:14 +0200122 ip_lookup_main_t *lm;
123 void *addr;
Florin Corasf727db92016-06-23 15:01:58 +0200124
125 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
126 addr = ip_interface_get_first_address (lm, sw_if_index, version);
127 if (!addr)
128 return 0;
129
130 ip_address_set (result, addr, version);
131 return 1;
132}
133
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100134/**
135 * convert from a LISP address to a FIB prefix
136 */
137void
138ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
Florin Corasf727db92016-06-23 15:01:58 +0200139{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100140 if (addr->version == IP4)
141 {
142 prefix->fp_len = 32;
143 prefix->fp_proto = FIB_PROTOCOL_IP4;
144 memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
145 memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
146 }
Florin Corasf727db92016-06-23 15:01:58 +0200147 else
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100148 {
149 prefix->fp_len = 128;
150 prefix->fp_proto = FIB_PROTOCOL_IP6;
151 memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
152 }
Florin Corasf727db92016-06-23 15:01:58 +0200153}
154
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100155/**
156 * convert from a LISP to a FIB prefix
157 */
158void
159ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
160 fib_prefix_t * fib_prefix)
Florin Corasf727db92016-06-23 15:01:58 +0200161{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100162 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
163 fib_prefix->fp_len = ip_prefix->len;
Florin Corasf727db92016-06-23 15:01:58 +0200164}
165
166/**
167 * Find the sw_if_index of the interface that would be used to egress towards
168 * dst.
169 */
170u32
171ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
172{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100173 fib_node_index_t fei;
174 fib_prefix_t prefix;
Florin Corasf727db92016-06-23 15:01:58 +0200175
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100176 ip_address_to_fib_prefix (dst, &prefix);
Florin Corasf727db92016-06-23 15:01:58 +0200177
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100178 fei = fib_table_lookup (0, &prefix);
179
180 return (fib_entry_get_resolving_interface (fei));
Florin Corasf727db92016-06-23 15:01:58 +0200181}
182
183/**
184 * Find first IP of the interface that would be used to egress towards dst.
185 * Returns 1 if the address is found 0 otherwise.
186 */
187int
188ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
Florin Corasa2157cf2016-08-16 21:09:14 +0200189 ip_address_t * result)
Florin Corasf727db92016-06-23 15:01:58 +0200190{
191 u32 si;
Florin Corasa2157cf2016-08-16 21:09:14 +0200192 ip_lookup_main_t *lm;
193 void *addr = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200194 u8 ipver;
195
Florin Corasa2157cf2016-08-16 21:09:14 +0200196 ASSERT (result != 0);
Florin Corasf727db92016-06-23 15:01:58 +0200197
Florin Corasa2157cf2016-08-16 21:09:14 +0200198 ipver = ip_addr_version (dst);
Florin Corasf727db92016-06-23 15:01:58 +0200199
200 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100201 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
Florin Corasf727db92016-06-23 15:01:58 +0200202
Florin Corasa2157cf2016-08-16 21:09:14 +0200203 if ((u32) ~ 0 == si)
Florin Corasf727db92016-06-23 15:01:58 +0200204 return 0;
205
206 /* find the first ip address */
207 addr = ip_interface_get_first_address (lm, si, ipver);
208 if (0 == addr)
209 return 0;
210
211 ip_address_set (result, addr, ipver);
212 return 1;
213}
214
215static int
Florin Coras1a1adc72016-07-22 01:45:30 +0200216dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
Florin Corasf727db92016-06-23 15:01:58 +0200217{
Neale Ranns5e575b12016-10-03 09:40:25 +0100218 uword *dp_table;
Florin Corasf727db92016-06-23 15:01:58 +0200219
Florin Coras1a1adc72016-07-22 01:45:30 +0200220 if (!is_l2)
Florin Corasf727db92016-06-23 15:01:58 +0200221 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200222 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200223
224 if (!dp_table)
Florin Corasa2157cf2016-08-16 21:09:14 +0200225 {
226 clib_warning ("vni %d not associated to a vrf!", vni);
227 return VNET_API_ERROR_INVALID_VALUE;
228 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200229 }
230 else
231 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200232 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200233 if (!dp_table)
Florin Corasa2157cf2016-08-16 21:09:14 +0200234 {
235 clib_warning ("vni %d not associated to a bridge domain!", vni);
236 return VNET_API_ERROR_INVALID_VALUE;
237 }
Florin Corasf727db92016-06-23 15:01:58 +0200238 }
239
Florin Corasf727db92016-06-23 15:01:58 +0200240 /* enable/disable data-plane interface */
241 if (is_add)
242 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100243 if (is_l2)
244 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
245 else
246 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200247 }
248 else
249 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100250 if (is_l2)
251 lisp_gpe_tenant_l2_iface_unlock (vni);
252 else
253 lisp_gpe_tenant_l3_iface_unlock (vni);
Florin Corasf727db92016-06-23 15:01:58 +0200254 }
255
256 return 0;
257}
258
259static void
260dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
261{
Florin Corasa2157cf2016-08-16 21:09:14 +0200262 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
263 fwd_entry_t *fe = 0;
264 uword *feip = 0;
265 memset (a, 0, sizeof (*a));
Florin Corasf727db92016-06-23 15:01:58 +0200266
Florin Corasa2157cf2016-08-16 21:09:14 +0200267 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +0200268 if (!feip)
269 return;
270
Florin Corasa2157cf2016-08-16 21:09:14 +0200271 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200272
273 /* delete dp fwd entry */
274 u32 sw_if_index;
275 a->is_add = 0;
Florin Corasbb5c22f2016-08-02 02:31:03 +0200276 a->locator_pairs = fe->locator_pairs;
Filip Tehlar2fdaece2016-09-28 14:27:59 +0200277 a->vni = gid_address_vni (&fe->reid);
278 gid_address_copy (&a->rmt_eid, &fe->reid);
Filip Tehlard5fcc462016-10-17 16:20:18 +0200279 if (fe->is_src_dst)
280 gid_address_copy (&a->lcl_eid, &fe->leid);
Florin Corasf727db92016-06-23 15:01:58 +0200281
282 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
283
284 /* delete entry in fwd table */
Florin Corasa2157cf2016-08-16 21:09:14 +0200285 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
286 vec_free (fe->locator_pairs);
287 pool_put (lcm->fwd_entry_pool, fe);
Florin Corasf727db92016-06-23 15:01:58 +0200288}
289
290/**
291 * Finds first remote locator with best (lowest) priority that has a local
292 * peer locator with an underlying route to it.
293 *
294 */
295static u32
Florin Corasa2157cf2016-08-16 21:09:14 +0200296get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
297 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
Florin Corasf727db92016-06-23 15:01:58 +0200298{
Florin Coras3590ac52016-08-08 16:04:26 +0200299 u32 i, limitp = 0, li, found = 0, esi;
Florin Corasa2157cf2016-08-16 21:09:14 +0200300 locator_set_t *rmt_ls, *lcl_ls;
301 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
302 locator_t *lp, *rmt = 0;
303 uword *checked = 0;
Florin Corasbb5c22f2016-08-02 02:31:03 +0200304 locator_pair_t pair;
Florin Corasf727db92016-06-23 15:01:58 +0200305
Florin Corasa2157cf2016-08-16 21:09:14 +0200306 rmt_ls =
307 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
308 lcl_ls =
309 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
Florin Corasf727db92016-06-23 15:01:58 +0200310
Florin Corasa2157cf2016-08-16 21:09:14 +0200311 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
Florin Corasf727db92016-06-23 15:01:58 +0200312 return 0;
313
Florin Coras3590ac52016-08-08 16:04:26 +0200314 while (1)
Florin Corasf727db92016-06-23 15:01:58 +0200315 {
316 rmt = 0;
317
318 /* find unvisited remote locator with best priority */
Florin Corasa2157cf2016-08-16 21:09:14 +0200319 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
320 {
321 if (0 != hash_get (checked, i))
322 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200323
Florin Corasa2157cf2016-08-16 21:09:14 +0200324 li = vec_elt (rmt_ls->locator_indices, i);
325 lp = pool_elt_at_index (lcm->locator_pool, li);
Florin Corasf727db92016-06-23 15:01:58 +0200326
Florin Corasa2157cf2016-08-16 21:09:14 +0200327 /* we don't support non-IP locators for now */
328 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
329 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200330
Florin Corasa2157cf2016-08-16 21:09:14 +0200331 if ((found && lp->priority == limitp)
332 || (!found && lp->priority >= limitp))
333 {
334 rmt = lp;
Florin Coras3590ac52016-08-08 16:04:26 +0200335
Florin Corasa2157cf2016-08-16 21:09:14 +0200336 /* don't search for locators with lower priority and don't
337 * check this locator again*/
338 limitp = lp->priority;
339 hash_set (checked, i, 1);
340 break;
341 }
342 }
Florin Corasf727db92016-06-23 15:01:58 +0200343 /* check if a local locator with a route to remote locator exists */
344 if (rmt != 0)
Florin Corasa2157cf2016-08-16 21:09:14 +0200345 {
346 /* find egress sw_if_index for rmt locator */
347 esi =
348 ip_fib_get_egress_iface_for_dst (lcm,
349 &gid_address_ip (&rmt->address));
350 if ((u32) ~ 0 == esi)
351 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200352
Florin Corasa2157cf2016-08-16 21:09:14 +0200353 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
354 {
355 li = vec_elt (lcl_ls->locator_indices, i);
356 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
Florin Corasf727db92016-06-23 15:01:58 +0200357
Florin Corasa2157cf2016-08-16 21:09:14 +0200358 /* found local locator with the needed sw_if_index */
359 if (sl->sw_if_index == esi)
360 {
361 /* and it has an address */
362 if (0 == ip_interface_get_first_ip_address (lcm,
363 sl->sw_if_index,
364 gid_address_ip_version
365 (&rmt->address),
366 lcl_addr))
367 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200368
Florin Corasa2157cf2016-08-16 21:09:14 +0200369 memset (&pair, 0, sizeof (pair));
370 ip_address_copy (&pair.rmt_loc,
371 &gid_address_ip (&rmt->address));
372 ip_address_copy (&pair.lcl_loc, lcl_addr);
373 pair.weight = rmt->weight;
Filip Tehlarfb9931f2016-12-09 13:52:38 +0100374 pair.priority = rmt->priority;
Florin Corasa2157cf2016-08-16 21:09:14 +0200375 vec_add1 (locator_pairs[0], pair);
376 found = 1;
377 }
378 }
379 }
Florin Corasf727db92016-06-23 15:01:58 +0200380 else
Florin Corasa2157cf2016-08-16 21:09:14 +0200381 break;
Florin Corasf727db92016-06-23 15:01:58 +0200382 }
Florin Coras3590ac52016-08-08 16:04:26 +0200383
Florin Corasa2157cf2016-08-16 21:09:14 +0200384 hash_free (checked);
Florin Coras3590ac52016-08-08 16:04:26 +0200385 return found;
Florin Corasf727db92016-06-23 15:01:58 +0200386}
387
388static void
Florin Corasdca88042016-09-14 16:01:38 +0200389gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
390 fid_address_t * fid)
391{
392 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
393
394 dst[0] = src[0];
395
396 switch (fid_addr_type (fid))
397 {
398 case FID_ADDR_IP_PREF:
399 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
400 gid_address_ippref (dst) = fid_addr_ippref (fid);
401 break;
402 case FID_ADDR_MAC:
403 gid_address_type (dst) = GID_ADDR_MAC;
404 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
405 break;
406 default:
407 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
408 break;
409 }
410}
411
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200412u8
413vnet_lisp_map_register_state_get (void)
414{
415 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
416 return lcm->map_registering;
417}
418
419u8
420vnet_lisp_rloc_probe_state_get (void)
421{
422 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
423 return lcm->rloc_probing;
424}
425
Florin Corasdca88042016-09-14 16:01:38 +0200426static void
Florin Corasa2157cf2016-08-16 21:09:14 +0200427dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
Florin Corasf727db92016-06-23 15:01:58 +0200428{
Florin Corasa2157cf2016-08-16 21:09:14 +0200429 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
430 mapping_t *src_map, *dst_map;
Florin Corasf727db92016-06-23 15:01:58 +0200431 u32 sw_if_index;
Florin Corasa2157cf2016-08-16 21:09:14 +0200432 uword *feip = 0, *dpid;
433 fwd_entry_t *fe;
Filip Tehlar69a9b762016-09-23 10:00:52 +0200434 u8 type, is_src_dst = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200435
Florin Corasa2157cf2016-08-16 21:09:14 +0200436 memset (a, 0, sizeof (*a));
Florin Corasf727db92016-06-23 15:01:58 +0200437
438 /* remove entry if it already exists */
439 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
440 if (feip)
441 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
442
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200443 if (lcm->lisp_pitr)
444 src_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
445 else
446 src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
Florin Corasf727db92016-06-23 15:01:58 +0200447 dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
448
Florin Corasf727db92016-06-23 15:01:58 +0200449 /* insert data plane forwarding entry */
450 a->is_add = 1;
451
Filip Tehlard5fcc462016-10-17 16:20:18 +0200452 if (MR_MODE_SRC_DST == lcm->map_request_mode)
Florin Corasdca88042016-09-14 16:01:38 +0200453 {
Filip Tehlard5fcc462016-10-17 16:20:18 +0200454 if (GID_ADDR_SRC_DST == gid_address_type (&dst_map->eid))
455 {
456 gid_address_sd_to_flat (&a->rmt_eid, &dst_map->eid,
457 &gid_address_sd_dst (&dst_map->eid));
458 gid_address_sd_to_flat (&a->lcl_eid, &dst_map->eid,
459 &gid_address_sd_src (&dst_map->eid));
460 }
461 else
462 {
463 gid_address_copy (&a->rmt_eid, &dst_map->eid);
464 gid_address_copy (&a->lcl_eid, &src_map->eid);
465 }
Filip Tehlar69a9b762016-09-23 10:00:52 +0200466 is_src_dst = 1;
Florin Corasdca88042016-09-14 16:01:38 +0200467 }
468 else
469 gid_address_copy (&a->rmt_eid, &dst_map->eid);
470
Florin Corasa2157cf2016-08-16 21:09:14 +0200471 a->vni = gid_address_vni (&a->rmt_eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200472
473 /* get vrf or bd_index associated to vni */
Florin Corasdca88042016-09-14 16:01:38 +0200474 type = gid_address_type (&a->rmt_eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200475 if (GID_ADDR_IP_PREFIX == type)
476 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200477 dpid = hash_get (lcm->table_id_by_vni, a->vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200478 if (!dpid)
Florin Corasa2157cf2016-08-16 21:09:14 +0200479 {
480 clib_warning ("vni %d not associated to a vrf!", a->vni);
481 return;
482 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200483 a->table_id = dpid[0];
484 }
485 else if (GID_ADDR_MAC == type)
486 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200487 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200488 if (!dpid)
Florin Corasa2157cf2016-08-16 21:09:14 +0200489 {
490 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
491 return;
492 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200493 a->bd_id = dpid[0];
494 }
495
Florin Corasf727db92016-06-23 15:01:58 +0200496 /* find best locator pair that 1) verifies LISP policy 2) are connected */
Florin Coras3590ac52016-08-08 16:04:26 +0200497 if (0 == get_locator_pairs (lcm, src_map, dst_map, &a->locator_pairs))
Florin Corasf727db92016-06-23 15:01:58 +0200498 {
499 /* negative entry */
500 a->is_negative = 1;
501 a->action = dst_map->action;
502 }
503
504 /* TODO remove */
Florin Corasa2157cf2016-08-16 21:09:14 +0200505 u8 ipver = ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
Florin Corasf727db92016-06-23 15:01:58 +0200506 a->decap_next_index = (ipver == IP4) ?
Florin Corasa2157cf2016-08-16 21:09:14 +0200507 LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
Florin Corasf727db92016-06-23 15:01:58 +0200508
509 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
510
511 /* add tunnel to fwd entry table XXX check return value from DP insertion */
512 pool_get (lcm->fwd_entry_pool, fe);
Florin Corasbb5c22f2016-08-02 02:31:03 +0200513 fe->locator_pairs = a->locator_pairs;
Filip Tehlar2fdaece2016-09-28 14:27:59 +0200514 gid_address_copy (&fe->reid, &a->rmt_eid);
Filip Tehlarb601f222017-01-02 10:22:56 +0100515
516 if (is_src_dst)
517 gid_address_copy (&fe->leid, &a->lcl_eid);
518 else
519 gid_address_copy (&fe->leid, &src_map->eid);
520
Filip Tehlar69a9b762016-09-23 10:00:52 +0200521 fe->is_src_dst = is_src_dst;
Florin Corasf727db92016-06-23 15:01:58 +0200522 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200523 fe - lcm->fwd_entry_pool);
Florin Corasf727db92016-06-23 15:01:58 +0200524}
525
Filip Tehlarce1aae42017-01-04 10:42:25 +0100526typedef struct
527{
528 u32 si;
529 u32 di;
530} fwd_entry_mt_arg_t;
531
532static void *
533dp_add_fwd_entry_thread_fn (void *arg)
534{
535 fwd_entry_mt_arg_t *a = arg;
536 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
537 dp_add_fwd_entry (lcm, a->si, a->di);
538 return 0;
539}
540
541static int
542dp_add_fwd_entry_from_mt (u32 si, u32 di)
543{
544 fwd_entry_mt_arg_t a;
545
546 memset (&a, 0, sizeof (a));
547 a.si = si;
548 a.di = di;
549
550 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
551 (u8 *) & a, sizeof (a));
552 return 0;
553}
554
Florin Corasf727db92016-06-23 15:01:58 +0200555/**
Filip Tehlar69a9b762016-09-23 10:00:52 +0200556 * Returns vector of adjacencies.
557 *
558 * The caller must free the vector returned by this function.
559 *
560 * @param vni virtual network identifier
561 * @return vector of adjacencies
562 */
563lisp_adjacency_t *
564vnet_lisp_adjacencies_get_by_vni (u32 vni)
565{
566 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
567 fwd_entry_t *fwd;
568 lisp_adjacency_t *adjs = 0, adj;
569
570 /* *INDENT-OFF* */
571 pool_foreach(fwd, lcm->fwd_entry_pool,
572 ({
573 if (gid_address_vni (&fwd->reid) != vni)
574 continue;
575
576 gid_address_copy (&adj.reid, &fwd->reid);
577 gid_address_copy (&adj.leid, &fwd->leid);
578 vec_add1 (adjs, adj);
579 }));
580 /* *INDENT-ON* */
581
582 return adjs;
583}
584
585static clib_error_t *
586lisp_show_adjacencies_command_fn (vlib_main_t * vm,
587 unformat_input_t * input,
588 vlib_cli_command_t * cmd)
589{
590 lisp_adjacency_t *adjs, *adj;
591 vlib_cli_output (vm, "%s %40s\n", "leid", "reid");
592 unformat_input_t _line_input, *line_input = &_line_input;
593 u32 vni = ~0;
594
595 /* Get a line of input. */
596 if (!unformat_user (input, unformat_line_input, line_input))
597 return 0;
598
599 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
600 {
601 if (unformat (line_input, "vni %d", &vni))
602 ;
603 else
604 {
605 vlib_cli_output (vm, "parse error: '%U'",
606 format_unformat_error, line_input);
607 return 0;
608 }
609 }
610
611 if (~0 == vni)
612 {
613 vlib_cli_output (vm, "error: no vni specified!");
614 return 0;
615 }
616
617 adjs = vnet_lisp_adjacencies_get_by_vni (vni);
618
619 vec_foreach (adj, adjs)
620 {
621 vlib_cli_output (vm, "%U %40U\n", format_gid_address, &adj->leid,
622 format_gid_address, &adj->reid);
623 }
624 vec_free (adjs);
625
626 return 0;
627}
628
629/* *INDENT-OFF* */
630VLIB_CLI_COMMAND (lisp_show_adjacencies_command) = {
631 .path = "show lisp adjacencies",
632 .short_help = "show lisp adjacencies",
633 .function = lisp_show_adjacencies_command_fn,
634};
635/* *INDENT-ON* */
636
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200637static lisp_msmr_t *
638get_map_server (ip_address_t * a)
639{
640 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
641 lisp_msmr_t *m;
642
643 vec_foreach (m, lcm->map_servers)
644 {
645 if (!ip_address_cmp (&m->address, a))
646 {
647 return m;
648 }
649 }
650 return 0;
651}
652
653static lisp_msmr_t *
654get_map_resolver (ip_address_t * a)
655{
656 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
657 lisp_msmr_t *m;
658
659 vec_foreach (m, lcm->map_resolvers)
660 {
661 if (!ip_address_cmp (&m->address, a))
662 {
663 return m;
664 }
665 }
666 return 0;
667}
668
669int
670vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
671{
672 u32 i;
673 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
674 lisp_msmr_t _ms, *ms = &_ms;
675
676 if (vnet_lisp_enable_disable_status () == 0)
677 {
678 clib_warning ("LISP is disabled!");
679 return VNET_API_ERROR_LISP_DISABLED;
680 }
681
682 if (is_add)
683 {
684 if (get_map_server (addr))
685 {
686 clib_warning ("map-server %U already exists!", format_ip_address,
687 addr);
688 return -1;
689 }
690
691 memset (ms, 0, sizeof (*ms));
692 ip_address_copy (&ms->address, addr);
693 vec_add1 (lcm->map_servers, ms[0]);
694 }
695 else
696 {
697 for (i = 0; i < vec_len (lcm->map_servers); i++)
698 {
699 ms = vec_elt_at_index (lcm->map_servers, i);
700 if (!ip_address_cmp (&ms->address, addr))
701 {
702 vec_del1 (lcm->map_servers, i);
703 break;
704 }
705 }
706 }
707
708 return 0;
709}
710
711static clib_error_t *
712lisp_add_del_map_server_command_fn (vlib_main_t * vm,
713 unformat_input_t * input,
714 vlib_cli_command_t * cmd)
715{
716 int rv = 0;
717 u8 is_add = 1, ip_set = 0;
718 ip_address_t ip;
719 unformat_input_t _line_input, *line_input = &_line_input;
720
721 /* Get a line of input. */
722 if (!unformat_user (input, unformat_line_input, line_input))
723 return 0;
724
725 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
726 {
727 if (unformat (line_input, "add"))
728 is_add = 1;
729 else if (unformat (line_input, "del"))
730 is_add = 0;
731 else if (unformat (line_input, "%U", unformat_ip_address, &ip))
732 ip_set = 1;
733 else
734 {
735 vlib_cli_output (vm, "parse error: '%U'",
736 format_unformat_error, line_input);
737 return 0;
738 }
739 }
740
741 if (!ip_set)
742 {
743 vlib_cli_output (vm, "map-server ip address not set!");
744 return 0;
745 }
746
747 rv = vnet_lisp_add_del_map_server (&ip, is_add);
748 if (!rv)
749 vlib_cli_output (vm, "failed to %s map-server!",
750 is_add ? "add" : "delete");
751
752 return 0;
753}
754
755/* *INDENT-OFF* */
756VLIB_CLI_COMMAND (lisp_add_del_map_server_command) = {
757 .path = "lisp map-server",
758 .short_help = "lisp map-server add|del <ip>",
759 .function = lisp_add_del_map_server_command_fn,
760};
761/* *INDENT-ON* */
762
Filip Tehlar69a9b762016-09-23 10:00:52 +0200763/**
Florin Corasf727db92016-06-23 15:01:58 +0200764 * Add/remove mapping to/from map-cache. Overwriting not allowed.
765 */
766int
767vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200768 u32 * map_index_result)
Florin Corase127a7e2016-02-18 22:20:01 +0100769{
Florin Corasa2157cf2016-08-16 21:09:14 +0200770 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
771 u32 mi, *map_indexp, map_index, i;
772 mapping_t *m, *old_map;
773 u32 **eid_indexes;
Florin Corase127a7e2016-02-18 22:20:01 +0100774
Florin Corasf727db92016-06-23 15:01:58 +0200775 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
Filip Tehlar53f09e32016-05-19 14:25:44 +0200776 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100777 if (a->is_add)
778 {
779 /* TODO check if overwriting and take appropriate actions */
Florin Corasa2157cf2016-08-16 21:09:14 +0200780 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
781 {
782 clib_warning ("eid %U found in the eid-table", format_gid_address,
783 &a->eid);
784 return VNET_API_ERROR_VALUE_EXIST;
785 }
Florin Corase127a7e2016-02-18 22:20:01 +0100786
Florin Corasa2157cf2016-08-16 21:09:14 +0200787 pool_get (lcm->mapping_pool, m);
Florin Corasf727db92016-06-23 15:01:58 +0200788 gid_address_copy (&m->eid, &a->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100789 m->locator_set_index = a->locator_set_index;
790 m->ttl = a->ttl;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200791 m->action = a->action;
Florin Corase127a7e2016-02-18 22:20:01 +0100792 m->local = a->local;
Filip Tehlar3cd9e732016-08-23 10:52:44 +0200793 m->is_static = a->is_static;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200794 m->key = vec_dup (a->key);
795 m->key_id = a->key_id;
Florin Corase127a7e2016-02-18 22:20:01 +0100796
797 map_index = m - lcm->mapping_pool;
Florin Corasf727db92016-06-23 15:01:58 +0200798 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200799 1);
Florin Corase127a7e2016-02-18 22:20:01 +0100800
Florin Corasa2157cf2016-08-16 21:09:14 +0200801 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
802 {
803 clib_warning ("Locator set with index %d doesn't exist",
804 a->locator_set_index);
805 return VNET_API_ERROR_INVALID_VALUE;
806 }
Florin Corase127a7e2016-02-18 22:20:01 +0100807
808 /* add eid to list of eids supported by locator-set */
809 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
Florin Corasa2157cf2016-08-16 21:09:14 +0200810 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
811 a->locator_set_index);
812 vec_add1 (eid_indexes[0], map_index);
Florin Corase127a7e2016-02-18 22:20:01 +0100813
814 if (a->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200815 {
816 /* mark as local */
817 vec_add1 (lcm->local_mappings_indexes, map_index);
818 }
Florin Corase127a7e2016-02-18 22:20:01 +0100819 map_index_result[0] = map_index;
820 }
821 else
822 {
Florin Coras577c3552016-04-21 00:45:40 +0200823 if (mi == GID_LOOKUP_MISS)
Florin Corasa2157cf2016-08-16 21:09:14 +0200824 {
825 clib_warning ("eid %U not found in the eid-table",
826 format_gid_address, &a->eid);
827 return VNET_API_ERROR_INVALID_VALUE;
828 }
Florin Corase127a7e2016-02-18 22:20:01 +0100829
830 /* clear locator-set to eids binding */
Florin Corasa2157cf2016-08-16 21:09:14 +0200831 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
832 a->locator_set_index);
833 for (i = 0; i < vec_len (eid_indexes[0]); i++)
834 {
835 map_indexp = vec_elt_at_index (eid_indexes[0], i);
836 if (map_indexp[0] == mi)
837 break;
838 }
839 vec_del1 (eid_indexes[0], i);
Florin Corase127a7e2016-02-18 22:20:01 +0100840
841 /* remove local mark if needed */
Florin Corasa2157cf2016-08-16 21:09:14 +0200842 m = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corase127a7e2016-02-18 22:20:01 +0100843 if (m->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200844 {
845 u32 k, *lm_indexp;
846 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
847 {
848 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
849 if (lm_indexp[0] == mi)
850 break;
851 }
852 vec_del1 (lcm->local_mappings_indexes, k);
853 }
Florin Corase127a7e2016-02-18 22:20:01 +0100854
855 /* remove mapping from dictionary */
Florin Corasf727db92016-06-23 15:01:58 +0200856 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
Filip Tehlar324112f2016-06-02 16:07:38 +0200857 gid_address_free (&m->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100858 pool_put_index (lcm->mapping_pool, mi);
859 }
860
861 return 0;
862}
863
Florin Corasf727db92016-06-23 15:01:58 +0200864/**
865 * Add/update/delete mapping to/in/from map-cache.
866 */
Florin Coras577c3552016-04-21 00:45:40 +0200867int
868vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200869 u32 * map_index_result)
Florin Coras577c3552016-04-21 00:45:40 +0200870{
Florin Corasa2157cf2016-08-16 21:09:14 +0200871 uword *dp_table = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200872 u32 vni;
Florin Coras1a1adc72016-07-22 01:45:30 +0200873 u8 type;
Florin Corasf727db92016-06-23 15:01:58 +0200874
Florin Corasa2157cf2016-08-16 21:09:14 +0200875 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Coras577c3552016-04-21 00:45:40 +0200876
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200877 if (vnet_lisp_enable_disable_status () == 0)
878 {
879 clib_warning ("LISP is disabled!");
880 return VNET_API_ERROR_LISP_DISABLED;
881 }
882
Florin Corasa2157cf2016-08-16 21:09:14 +0200883 vni = gid_address_vni (&a->eid);
884 type = gid_address_type (&a->eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200885 if (GID_ADDR_IP_PREFIX == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200886 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200887 else if (GID_ADDR_MAC == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200888 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras577c3552016-04-21 00:45:40 +0200889
Florin Coras1a1adc72016-07-22 01:45:30 +0200890 if (!dp_table)
Florin Coras577c3552016-04-21 00:45:40 +0200891 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200892 clib_warning ("vni %d not associated to a %s!", vni,
893 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
Florin Coras577c3552016-04-21 00:45:40 +0200894 return VNET_API_ERROR_INVALID_VALUE;
895 }
896
Florin Corasf727db92016-06-23 15:01:58 +0200897 /* store/remove mapping from map-cache */
898 return vnet_lisp_map_cache_add_del (a, map_index_result);
Florin Coras577c3552016-04-21 00:45:40 +0200899}
900
Florin Corase127a7e2016-02-18 22:20:01 +0100901static clib_error_t *
902lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
Florin Corasa2157cf2016-08-16 21:09:14 +0200903 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +0100904{
Florin Corasa2157cf2016-08-16 21:09:14 +0200905 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
906 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corase127a7e2016-02-18 22:20:01 +0100907 u8 is_add = 1;
908 gid_address_t eid;
Florin Corasa2157cf2016-08-16 21:09:14 +0200909 gid_address_t *eids = 0;
910 clib_error_t *error = 0;
911 u8 *locator_set_name = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100912 u32 locator_set_index = 0, map_index = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +0200913 uword *p;
914 vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200915 int rv = 0;
Filip Tehlar324112f2016-06-02 16:07:38 +0200916 u32 vni = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200917 u8 *key = 0;
918 u32 key_id = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100919
Filip Tehlar324112f2016-06-02 16:07:38 +0200920 memset (&eid, 0, sizeof (eid));
Filip Tehlar5ca5dc82016-08-02 19:32:25 +0200921 memset (a, 0, sizeof (*a));
922
Florin Corase127a7e2016-02-18 22:20:01 +0100923 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +0200924 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corase127a7e2016-02-18 22:20:01 +0100925 return 0;
926
927 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
928 {
929 if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +0200930 is_add = 1;
Florin Corase127a7e2016-02-18 22:20:01 +0100931 else if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +0200932 is_add = 0;
Filip Tehlar5ca5dc82016-08-02 19:32:25 +0200933 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
Florin Corasa2157cf2016-08-16 21:09:14 +0200934 ;
Filip Tehlar324112f2016-06-02 16:07:38 +0200935 else if (unformat (line_input, "vni %d", &vni))
Florin Corasa2157cf2016-08-16 21:09:14 +0200936 gid_address_vni (&eid) = vni;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200937 else if (unformat (line_input, "secret-key %_%v%_", &key))
938 ;
939 else if (unformat (line_input, "key-id %U", unformat_hmac_key_id,
940 &key_id))
941 ;
Florin Corase127a7e2016-02-18 22:20:01 +0100942 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +0200943 {
944 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
945 if (!p)
946 {
947 error = clib_error_return (0, "locator-set %s doesn't exist",
948 locator_set_name);
949 goto done;
950 }
951 locator_set_index = p[0];
952 }
Florin Corase127a7e2016-02-18 22:20:01 +0100953 else
Florin Corasa2157cf2016-08-16 21:09:14 +0200954 {
955 error = unformat_parse_error (line_input);
956 goto done;
957 }
Florin Corase127a7e2016-02-18 22:20:01 +0100958 }
Florin Corase127a7e2016-02-18 22:20:01 +0100959 /* XXX treat batch configuration */
Filip Tehlar324112f2016-06-02 16:07:38 +0200960
Florin Corasa2157cf2016-08-16 21:09:14 +0200961 if (GID_ADDR_SRC_DST == gid_address_type (&eid))
Filip Tehlar5ca5dc82016-08-02 19:32:25 +0200962 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200963 error =
964 clib_error_return (0, "src/dst is not supported for local EIDs!");
Filip Tehlar5ca5dc82016-08-02 19:32:25 +0200965 goto done;
966 }
967
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200968 if (key && (0 == key_id))
969 {
970 vlib_cli_output (vm, "invalid key_id!");
971 return 0;
972 }
973
Florin Corasa2157cf2016-08-16 21:09:14 +0200974 gid_address_copy (&a->eid, &eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100975 a->is_add = is_add;
976 a->locator_set_index = locator_set_index;
977 a->local = 1;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200978 a->key = key;
979 a->key_id = key_id;
Florin Corase127a7e2016-02-18 22:20:01 +0100980
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200981 rv = vnet_lisp_add_del_local_mapping (a, &map_index);
982 if (0 != rv)
Florin Corasa2157cf2016-08-16 21:09:14 +0200983 {
984 error = clib_error_return (0, "failed to %s local mapping!",
985 is_add ? "add" : "delete");
986 }
987done:
988 vec_free (eids);
Filip Tehlar53f09e32016-05-19 14:25:44 +0200989 if (locator_set_name)
990 vec_free (locator_set_name);
Florin Corasf727db92016-06-23 15:01:58 +0200991 gid_address_free (&a->eid);
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200992 vec_free (a->key);
Florin Corase127a7e2016-02-18 22:20:01 +0100993 return error;
994}
995
Florin Corasa2157cf2016-08-16 21:09:14 +0200996/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +0100997VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
998 .path = "lisp eid-table",
Filip Tehlar324112f2016-06-02 16:07:38 +0200999 .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001000 "locator-set <locator-set> [key <secret-key> key-id sha1|sha256 ]",
Florin Corase127a7e2016-02-18 22:20:01 +01001001 .function = lisp_add_del_local_eid_command_fn,
1002};
Florin Corasa2157cf2016-08-16 21:09:14 +02001003/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01001004
Filip Tehlar324112f2016-06-02 16:07:38 +02001005int
Florin Coras1a1adc72016-07-22 01:45:30 +02001006vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
Filip Tehlar324112f2016-06-02 16:07:38 +02001007{
Florin Corasa2157cf2016-08-16 21:09:14 +02001008 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1009 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
Filip Tehlar324112f2016-06-02 16:07:38 +02001010
1011 if (vnet_lisp_enable_disable_status () == 0)
1012 {
1013 clib_warning ("LISP is disabled!");
1014 return -1;
1015 }
1016
Florin Coras1a1adc72016-07-22 01:45:30 +02001017 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
1018 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
1019
1020 if (!is_l2 && (vni == 0 || dp_id == 0))
Filip Tehlar324112f2016-06-02 16:07:38 +02001021 {
1022 clib_warning ("can't add/del default vni-vrf mapping!");
1023 return -1;
1024 }
1025
Florin Coras1a1adc72016-07-22 01:45:30 +02001026 dp_idp = hash_get (dp_table_by_vni[0], vni);
1027 vnip = hash_get (vni_by_dp_table[0], dp_id);
Filip Tehlar324112f2016-06-02 16:07:38 +02001028
1029 if (is_add)
1030 {
Florin Coras1a1adc72016-07-22 01:45:30 +02001031 if (dp_idp || vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +02001032 {
1033 clib_warning ("vni %d or vrf %d already used in vrf/vni "
1034 "mapping!", vni, dp_id);
1035 return -1;
1036 }
Florin Coras1a1adc72016-07-22 01:45:30 +02001037 hash_set (dp_table_by_vni[0], vni, dp_id);
1038 hash_set (vni_by_dp_table[0], dp_id, vni);
Florin Corasf727db92016-06-23 15:01:58 +02001039
1040 /* create dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +02001041 dp_add_del_iface (lcm, vni, is_l2, 1);
Filip Tehlar324112f2016-06-02 16:07:38 +02001042 }
1043 else
1044 {
Florin Coras1a1adc72016-07-22 01:45:30 +02001045 if (!dp_idp || !vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +02001046 {
1047 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
1048 "mapping!", vni, dp_id);
1049 return -1;
1050 }
Florin Coras1a1adc72016-07-22 01:45:30 +02001051 hash_unset (dp_table_by_vni[0], vni);
1052 hash_unset (vni_by_dp_table[0], dp_id);
Florin Corasf727db92016-06-23 15:01:58 +02001053
1054 /* remove dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +02001055 dp_add_del_iface (lcm, vni, is_l2, 0);
Filip Tehlar324112f2016-06-02 16:07:38 +02001056 }
1057 return 0;
Florin Coras1a1adc72016-07-22 01:45:30 +02001058
Filip Tehlar324112f2016-06-02 16:07:38 +02001059}
1060
1061static clib_error_t *
1062lisp_eid_table_map_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001063 unformat_input_t * input,
1064 vlib_cli_command_t * cmd)
Filip Tehlar324112f2016-06-02 16:07:38 +02001065{
Florin Coras1a1adc72016-07-22 01:45:30 +02001066 u8 is_add = 1, is_l2 = 0;
1067 u32 vni = 0, dp_id = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001068 unformat_input_t _line_input, *line_input = &_line_input;
Filip Tehlar324112f2016-06-02 16:07:38 +02001069
1070 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02001071 if (!unformat_user (input, unformat_line_input, line_input))
Filip Tehlar324112f2016-06-02 16:07:38 +02001072 return 0;
1073
1074 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1075 {
1076 if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001077 is_add = 0;
Filip Tehlar324112f2016-06-02 16:07:38 +02001078 else if (unformat (line_input, "vni %d", &vni))
Florin Corasa2157cf2016-08-16 21:09:14 +02001079 ;
Florin Coras1a1adc72016-07-22 01:45:30 +02001080 else if (unformat (line_input, "vrf %d", &dp_id))
Florin Corasa2157cf2016-08-16 21:09:14 +02001081 ;
Florin Coras1a1adc72016-07-22 01:45:30 +02001082 else if (unformat (line_input, "bd %d", &dp_id))
Florin Corasa2157cf2016-08-16 21:09:14 +02001083 is_l2 = 1;
Filip Tehlar324112f2016-06-02 16:07:38 +02001084 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001085 {
1086 return unformat_parse_error (line_input);
1087 }
Filip Tehlar324112f2016-06-02 16:07:38 +02001088 }
Florin Coras1a1adc72016-07-22 01:45:30 +02001089 vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
Filip Tehlar324112f2016-06-02 16:07:38 +02001090 return 0;
1091}
1092
Florin Corasa2157cf2016-08-16 21:09:14 +02001093/* *INDENT-OFF* */
Filip Tehlar324112f2016-06-02 16:07:38 +02001094VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
1095 .path = "lisp eid-table map",
Florin Coras1a1adc72016-07-22 01:45:30 +02001096 .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
Filip Tehlar324112f2016-06-02 16:07:38 +02001097 .function = lisp_eid_table_map_command_fn,
1098};
Florin Corasa2157cf2016-08-16 21:09:14 +02001099/* *INDENT-ON* */
Florin Corasf727db92016-06-23 15:01:58 +02001100
1101/* return 0 if the two locator sets are identical 1 otherwise */
1102static u8
Florin Corasa2157cf2016-08-16 21:09:14 +02001103compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
1104 locator_t * new_locators)
Filip Tehlar53f09e32016-05-19 14:25:44 +02001105{
Florin Corasf727db92016-06-23 15:01:58 +02001106 u32 i, old_li;
Florin Corasa2157cf2016-08-16 21:09:14 +02001107 locator_t *old_loc, *new_loc;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001108
Florin Corasa2157cf2016-08-16 21:09:14 +02001109 if (vec_len (old_ls_indexes) != vec_len (new_locators))
Florin Corasf727db92016-06-23 15:01:58 +02001110 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001111
Florin Corasa2157cf2016-08-16 21:09:14 +02001112 for (i = 0; i < vec_len (new_locators); i++)
Filip Tehlar53f09e32016-05-19 14:25:44 +02001113 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001114 old_li = vec_elt (old_ls_indexes, i);
1115 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
Florin Corasf727db92016-06-23 15:01:58 +02001116
Florin Corasa2157cf2016-08-16 21:09:14 +02001117 new_loc = vec_elt_at_index (new_locators, i);
Florin Corasf727db92016-06-23 15:01:58 +02001118
1119 if (locator_cmp (old_loc, new_loc))
Florin Corasa2157cf2016-08-16 21:09:14 +02001120 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001121 }
Florin Corasf727db92016-06-23 15:01:58 +02001122 return 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001123}
1124
Filip Tehlard5fcc462016-10-17 16:20:18 +02001125typedef struct
1126{
1127 u8 is_negative;
1128 void *lcm;
1129 gid_address_t *eids_to_be_deleted;
1130} remove_mapping_args_t;
1131
1132/**
1133 * Callback invoked when a sub-prefix is found
1134 */
1135static void
1136remove_mapping_if_needed (u32 mi, void *arg)
1137{
1138 u8 delete = 0;
1139 remove_mapping_args_t *a = arg;
1140 lisp_cp_main_t *lcm = a->lcm;
1141 mapping_t *m;
1142 locator_set_t *ls;
1143
1144 m = pool_elt_at_index (lcm->mapping_pool, mi);
1145 if (!m)
1146 return;
1147
1148 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1149
1150 if (a->is_negative)
1151 {
1152 if (0 != vec_len (ls->locator_indices))
1153 delete = 1;
1154 }
1155 else
1156 {
1157 if (0 == vec_len (ls->locator_indices))
1158 delete = 1;
1159 }
1160
1161 if (delete)
1162 vec_add1 (a->eids_to_be_deleted, m->eid);
1163}
1164
1165/**
1166 * This function searches map cache and looks for IP prefixes that are subset
1167 * of the provided one. If such prefix is found depending on 'is_negative'
1168 * it does follows:
1169 *
1170 * 1) if is_negative is true and found prefix points to positive mapping,
1171 * then the mapping is removed
1172 * 2) if is_negative is false and found prefix points to negative mapping,
1173 * then the mapping is removed
1174 */
1175static void
1176remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1177 u8 is_negative)
1178{
1179 gid_address_t *e;
1180 remove_mapping_args_t a;
1181 memset (&a, 0, sizeof (a));
1182
1183 /* do this only in src/dst mode ... */
1184 if (MR_MODE_SRC_DST != lcm->map_request_mode)
1185 return;
1186
1187 /* ... and only for IP prefix */
1188 if (GID_ADDR_SRC_DST != gid_address_type (eid)
1189 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1190 return;
1191
1192 a.is_negative = is_negative;
1193 a.lcm = lcm;
1194
1195 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1196 remove_mapping_if_needed, &a);
1197
1198 vec_foreach (e, a.eids_to_be_deleted)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001199 {
1200 lisp_add_del_adjacency (lcm, 0, e, 0 /* is_add */ );
Filip Tehlard5fcc462016-10-17 16:20:18 +02001201 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001202 }
Filip Tehlard5fcc462016-10-17 16:20:18 +02001203
1204 vec_free (a.eids_to_be_deleted);
1205}
1206
Filip Tehlar9677a942016-11-28 10:23:31 +01001207static void
1208mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1209{
1210 timing_wheel_delete (&lcm->wheel, mi);
1211}
1212
Filip Tehlar195bcee2016-05-13 17:37:35 +02001213/**
Florin Corasf727db92016-06-23 15:01:58 +02001214 * Adds/removes/updates mapping. Does not program forwarding.
Filip Tehlar195bcee2016-05-13 17:37:35 +02001215 *
Florin Coras71893ac2016-07-10 20:09:32 +02001216 * @param eid end-host identifier
Filip Tehlar195bcee2016-05-13 17:37:35 +02001217 * @param rlocs vector of remote locators
1218 * @param action action for negative map-reply
1219 * @param is_add add mapping if non-zero, delete otherwise
Florin Coras71893ac2016-07-10 20:09:32 +02001220 * @param res_map_index the map-index that was created/updated/removed. It is
1221 * set to ~0 if no action is taken.
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001222 * @param is_static used for distinguishing between statically learned
1223 remote mappings and mappings obtained from MR
Filip Tehlar195bcee2016-05-13 17:37:35 +02001224 * @return return code
1225 */
1226int
Florin Coras71893ac2016-07-10 20:09:32 +02001227vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001228 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
Florin Corasa2157cf2016-08-16 21:09:14 +02001229 u32 * res_map_index)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001230{
Florin Corasa2157cf2016-08-16 21:09:14 +02001231 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1232 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1233 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corasf727db92016-06-23 15:01:58 +02001234 u32 mi, ls_index = 0, dst_map_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02001235 mapping_t *old_map;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001236
Florin Corasa2157cf2016-08-16 21:09:14 +02001237 if (vnet_lisp_enable_disable_status () == 0)
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001238 {
1239 clib_warning ("LISP is disabled!");
1240 return VNET_API_ERROR_LISP_DISABLED;
1241 }
1242
Florin Corasf727db92016-06-23 15:01:58 +02001243 if (res_map_index)
1244 res_map_index[0] = ~0;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001245
Florin Corasf727db92016-06-23 15:01:58 +02001246 memset (m_args, 0, sizeof (m_args[0]));
1247 memset (ls_args, 0, sizeof (ls_args[0]));
Filip Tehlar195bcee2016-05-13 17:37:35 +02001248
Florin Corasf727db92016-06-23 15:01:58 +02001249 ls_args->locators = rlocs;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001250
Florin Coras71893ac2016-07-10 20:09:32 +02001251 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
Florin Corasa2157cf2016-08-16 21:09:14 +02001252 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corasf727db92016-06-23 15:01:58 +02001253
1254 if (is_add)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001255 {
Florin Corasf727db92016-06-23 15:01:58 +02001256 /* overwrite: if mapping already exists, decide if locators should be
1257 * updated and be done */
Florin Coras71893ac2016-07-10 20:09:32 +02001258 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001259 {
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001260 if (!is_static && (old_map->is_static || old_map->local))
1261 {
1262 /* do not overwrite local or static remote mappings */
1263 clib_warning ("mapping %U rejected due to collision with local "
1264 "or static remote mapping!", format_gid_address,
Filip Tehlard5fcc462016-10-17 16:20:18 +02001265 eid);
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001266 return 0;
1267 }
1268
Florin Corasa2157cf2016-08-16 21:09:14 +02001269 locator_set_t *old_ls;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001270
Florin Corasa2157cf2016-08-16 21:09:14 +02001271 /* update mapping attributes */
1272 old_map->action = action;
1273 old_map->authoritative = authoritative;
1274 old_map->ttl = ttl;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001275
Florin Corasa2157cf2016-08-16 21:09:14 +02001276 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1277 old_map->locator_set_index);
1278 if (compare_locators (lcm, old_ls->locator_indices,
1279 ls_args->locators))
1280 {
1281 /* set locator-set index to overwrite */
1282 ls_args->is_add = 1;
1283 ls_args->index = old_map->locator_set_index;
1284 vnet_lisp_add_del_locator_set (ls_args, 0);
1285 if (res_map_index)
1286 res_map_index[0] = mi;
1287 }
1288 }
Florin Corasf727db92016-06-23 15:01:58 +02001289 /* new mapping */
1290 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001291 {
Filip Tehlard5fcc462016-10-17 16:20:18 +02001292 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1293
Florin Corasa2157cf2016-08-16 21:09:14 +02001294 ls_args->is_add = 1;
1295 ls_args->index = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001296
Florin Corasa2157cf2016-08-16 21:09:14 +02001297 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
Florin Corasf727db92016-06-23 15:01:58 +02001298
Florin Corasa2157cf2016-08-16 21:09:14 +02001299 /* add mapping */
1300 gid_address_copy (&m_args->eid, eid);
1301 m_args->is_add = 1;
1302 m_args->action = action;
1303 m_args->locator_set_index = ls_index;
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001304 m_args->is_static = is_static;
Filip Tehlar9677a942016-11-28 10:23:31 +01001305 m_args->ttl = ttl;
Florin Corasa2157cf2016-08-16 21:09:14 +02001306 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +02001307
Florin Corasa2157cf2016-08-16 21:09:14 +02001308 if (res_map_index)
1309 res_map_index[0] = dst_map_index;
1310 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001311 }
Florin Corasf727db92016-06-23 15:01:58 +02001312 else
1313 {
Florin Coras71893ac2016-07-10 20:09:32 +02001314 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001315 {
1316 clib_warning ("cannot delete mapping for eid %U",
1317 format_gid_address, eid);
1318 return -1;
1319 }
Florin Corasf727db92016-06-23 15:01:58 +02001320
1321 m_args->is_add = 0;
Florin Coras71893ac2016-07-10 20:09:32 +02001322 gid_address_copy (&m_args->eid, eid);
Florin Corasf727db92016-06-23 15:01:58 +02001323 m_args->locator_set_index = old_map->locator_set_index;
1324
1325 /* delete mapping associated from map-cache */
1326 vnet_lisp_map_cache_add_del (m_args, 0);
1327
1328 ls_args->is_add = 0;
1329 ls_args->index = old_map->locator_set_index;
1330 /* delete locator set */
1331 vnet_lisp_add_del_locator_set (ls_args, 0);
Filip Tehlarb93222f2016-07-07 09:58:08 +02001332
Filip Tehlar9677a942016-11-28 10:23:31 +01001333 /* delete timer associated to the mapping if any */
1334 if (old_map->timer_set)
1335 mapping_delete_timer (lcm, mi);
1336
Filip Tehlarb93222f2016-07-07 09:58:08 +02001337 /* return old mapping index */
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001338 if (res_map_index)
Florin Corasa2157cf2016-08-16 21:09:14 +02001339 res_map_index[0] = mi;
Florin Corasf727db92016-06-23 15:01:58 +02001340 }
1341
Filip Tehlar195bcee2016-05-13 17:37:35 +02001342 /* success */
Florin Corasf727db92016-06-23 15:01:58 +02001343 return 0;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001344}
1345
Filip Tehlar58f886a2016-05-30 15:57:40 +02001346int
Florin Corasf727db92016-06-23 15:01:58 +02001347vnet_lisp_clear_all_remote_adjacencies (void)
Filip Tehlar58f886a2016-05-30 15:57:40 +02001348{
1349 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001350 u32 mi, *map_indices = 0, *map_indexp;
1351 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1352 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1353 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001354
Florin Corasa2157cf2016-08-16 21:09:14 +02001355 /* *INDENT-OFF* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001356 pool_foreach_index (mi, lcm->mapping_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02001357 ({
1358 vec_add1 (map_indices, mi);
1359 }));
1360 /* *INDENT-ON* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001361
1362 vec_foreach (map_indexp, map_indices)
Florin Corasa2157cf2016-08-16 21:09:14 +02001363 {
1364 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1365 if (!map->local)
1366 {
1367 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001368
Florin Corasa2157cf2016-08-16 21:09:14 +02001369 dm_args->is_add = 0;
1370 gid_address_copy (&dm_args->eid, &map->eid);
1371 dm_args->locator_set_index = map->locator_set_index;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001372
Florin Corasa2157cf2016-08-16 21:09:14 +02001373 /* delete mapping associated to fwd entry */
1374 vnet_lisp_map_cache_add_del (dm_args, 0);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001375
Florin Corasa2157cf2016-08-16 21:09:14 +02001376 ls->is_add = 0;
1377 ls->local = 0;
1378 ls->index = map->locator_set_index;
1379 /* delete locator set */
1380 rv = vnet_lisp_add_del_locator_set (ls, 0);
1381 if (rv != 0)
1382 goto cleanup;
1383 }
1384 }
Filip Tehlar58f886a2016-05-30 15:57:40 +02001385
1386cleanup:
1387 if (map_indices)
1388 vec_free (map_indices);
1389 return rv;
1390}
1391
Filip Tehlar195bcee2016-05-13 17:37:35 +02001392/**
Florin Coras71893ac2016-07-10 20:09:32 +02001393 * Adds adjacency or removes forwarding entry associated to remote mapping.
1394 * Note that adjacencies are not stored, they only result in forwarding entries
1395 * being created.
Florin Corasf727db92016-06-23 15:01:58 +02001396 */
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001397static int
Florin Coras71893ac2016-07-10 20:09:32 +02001398lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
Florin Corasa2157cf2016-08-16 21:09:14 +02001399 gid_address_t * remote_eid, u8 is_add)
Florin Corasf727db92016-06-23 15:01:58 +02001400{
Florin Coras71893ac2016-07-10 20:09:32 +02001401 u32 local_mi, remote_mi = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001402
1403 if (vnet_lisp_enable_disable_status () == 0)
1404 {
1405 clib_warning ("LISP is disabled!");
1406 return VNET_API_ERROR_LISP_DISABLED;
1407 }
1408
Filip Tehlar69a9b762016-09-23 10:00:52 +02001409 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1410 remote_eid, local_eid);
Florin Coras71893ac2016-07-10 20:09:32 +02001411 if (GID_LOOKUP_MISS == remote_mi)
1412 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001413 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1414 format_gid_address, remote_eid);
Florin Corasf727db92016-06-23 15:01:58 +02001415
Florin Coras71893ac2016-07-10 20:09:32 +02001416 return -1;
1417 }
1418
1419 if (is_add)
Florin Corasf727db92016-06-23 15:01:58 +02001420 {
1421 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1422 * delete*/
1423
1424 /* check if source eid has an associated mapping. If pitr mode is on,
1425 * just use the pitr's mapping */
Florin Coras71893ac2016-07-10 20:09:32 +02001426 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
Florin Corasa2157cf2016-08-16 21:09:14 +02001427 gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
Florin Corasf727db92016-06-23 15:01:58 +02001428
1429
Florin Coras71893ac2016-07-10 20:09:32 +02001430 if (GID_LOOKUP_MISS == local_mi)
Florin Corasa2157cf2016-08-16 21:09:14 +02001431 {
1432 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1433 format_gid_address, local_eid);
Florin Corasf727db92016-06-23 15:01:58 +02001434
Florin Corasa2157cf2016-08-16 21:09:14 +02001435 return -1;
1436 }
Florin Corasf727db92016-06-23 15:01:58 +02001437
Florin Coras71893ac2016-07-10 20:09:32 +02001438 /* update forwarding */
1439 dp_add_fwd_entry (lcm, local_mi, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001440 }
1441 else
Florin Coras71893ac2016-07-10 20:09:32 +02001442 dp_del_fwd_entry (lcm, 0, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001443
1444 return 0;
1445}
1446
1447int
1448vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1449{
Florin Corasa2157cf2016-08-16 21:09:14 +02001450 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001451 return lisp_add_del_adjacency (lcm, &a->leid, &a->reid, a->is_add);
Florin Corasf727db92016-06-23 15:01:58 +02001452}
1453
1454/**
Filip Tehlar195bcee2016-05-13 17:37:35 +02001455 * Handler for add/del remote mapping CLI.
1456 *
1457 * @param vm vlib context
1458 * @param input input from user
1459 * @param cmd cmd
1460 * @return pointer to clib error structure
1461 */
1462static clib_error_t *
1463lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001464 unformat_input_t * input,
1465 vlib_cli_command_t * cmd)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001466{
Florin Corasa2157cf2016-08-16 21:09:14 +02001467 clib_error_t *error = 0;
1468 unformat_input_t _line_input, *line_input = &_line_input;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001469 u8 is_add = 1, del_all = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001470 locator_t rloc, *rlocs = 0, *curr_rloc = 0;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001471 gid_address_t eid;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001472 u8 eid_set = 0;
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02001473 u32 vni, action = ~0, p, w;
Florin Corasf727db92016-06-23 15:01:58 +02001474 int rv;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001475
1476 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02001477 if (!unformat_user (input, unformat_line_input, line_input))
Filip Tehlar195bcee2016-05-13 17:37:35 +02001478 return 0;
1479
Florin Corasa2157cf2016-08-16 21:09:14 +02001480 memset (&eid, 0, sizeof (eid));
1481 memset (&rloc, 0, sizeof (rloc));
Filip Tehlar195bcee2016-05-13 17:37:35 +02001482
Filip Tehlar195bcee2016-05-13 17:37:35 +02001483 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1484 {
Filip Tehlar58f886a2016-05-30 15:57:40 +02001485 if (unformat (line_input, "del-all"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001486 del_all = 1;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001487 else if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001488 is_add = 0;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001489 else if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001490 ;
Florin Corasbb5c22f2016-08-02 02:31:03 +02001491 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
Florin Corasa2157cf2016-08-16 21:09:14 +02001492 eid_set = 1;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001493 else if (unformat (line_input, "vni %u", &vni))
Florin Corasa2157cf2016-08-16 21:09:14 +02001494 {
1495 gid_address_vni (&eid) = vni;
1496 }
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02001497 else if (unformat (line_input, "p %d w %d", &p, &w))
Florin Corasa2157cf2016-08-16 21:09:14 +02001498 {
1499 if (!curr_rloc)
1500 {
1501 clib_warning
1502 ("No RLOC configured for setting priority/weight!");
1503 goto done;
1504 }
1505 curr_rloc->priority = p;
1506 curr_rloc->weight = w;
1507 }
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02001508 else if (unformat (line_input, "rloc %U", unformat_ip_address,
Florin Corasa2157cf2016-08-16 21:09:14 +02001509 &gid_address_ip (&rloc.address)))
1510 {
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02001511 /* since rloc is stored in ip prefix we need to set prefix length */
1512 ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
1513
1514 u8 version = gid_address_ip_version (&rloc.address);
1515 ip_prefix_len (pref) = ip_address_max_len (version);
1516
Florin Corasa2157cf2016-08-16 21:09:14 +02001517 vec_add1 (rlocs, rloc);
1518 curr_rloc = &rlocs[vec_len (rlocs) - 1];
1519 }
Florin Corasf7643fd2016-08-01 15:35:20 +02001520 else if (unformat (line_input, "action %U",
Florin Corasa2157cf2016-08-16 21:09:14 +02001521 unformat_negative_mapping_action, &action))
1522 ;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001523 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001524 {
1525 clib_warning ("parse error");
1526 goto done;
1527 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001528 }
1529
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001530 if (!eid_set)
1531 {
1532 clib_warning ("missing eid!");
1533 goto done;
1534 }
1535
Filip Tehlar58f886a2016-05-30 15:57:40 +02001536 if (!del_all)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001537 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001538 if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1539 {
1540 clib_warning ("no action set for negative map-reply!");
1541 goto done;
1542 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001543 }
Florin Corasf727db92016-06-23 15:01:58 +02001544 else
1545 {
1546 vnet_lisp_clear_all_remote_adjacencies ();
1547 goto done;
1548 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001549
Florin Corasa2157cf2016-08-16 21:09:14 +02001550 /* TODO build src/dst with seid */
Florin Corasf727db92016-06-23 15:01:58 +02001551
1552 /* if it's a delete, clean forwarding */
1553 if (!is_add)
1554 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001555 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001556 rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1557 if (rv)
Florin Corasa2157cf2016-08-16 21:09:14 +02001558 {
1559 goto done;
1560 }
Florin Corasf727db92016-06-23 15:01:58 +02001561 }
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001562
1563 /* add as static remote mapping, i.e., not authoritative and infinite
1564 * ttl */
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001565 rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
1566 1 /* is_static */ , 0);
Florin Corasf727db92016-06-23 15:01:58 +02001567
Filip Tehlar195bcee2016-05-13 17:37:35 +02001568 if (rv)
Florin Corasa2157cf2016-08-16 21:09:14 +02001569 clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
Filip Tehlar195bcee2016-05-13 17:37:35 +02001570
1571done:
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02001572 vec_free (rlocs);
Filip Tehlar195bcee2016-05-13 17:37:35 +02001573 unformat_free (line_input);
1574 return error;
1575}
1576
Florin Corasa2157cf2016-08-16 21:09:14 +02001577VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) =
1578{
1579.path = "lisp remote-mapping",.short_help =
1580 "lisp remote-mapping add|del [del-all] vni <vni> "
1581 "eid <est-eid> [action <no-action|natively-forward|"
1582 "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
1583 "[rloc <dst-locator> ... ]",.function =
1584 lisp_add_del_remote_mapping_command_fn,};
Filip Tehlar195bcee2016-05-13 17:37:35 +02001585
Florin Corasf727db92016-06-23 15:01:58 +02001586/**
1587 * Handler for add/del adjacency CLI.
1588 */
1589static clib_error_t *
1590lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
Florin Corasa2157cf2016-08-16 21:09:14 +02001591 vlib_cli_command_t * cmd)
Florin Corasf727db92016-06-23 15:01:58 +02001592{
Florin Corasa2157cf2016-08-16 21:09:14 +02001593 clib_error_t *error = 0;
1594 unformat_input_t _line_input, *line_input = &_line_input;
1595 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
Florin Corasf727db92016-06-23 15:01:58 +02001596 u8 is_add = 1;
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001597 ip_prefix_t *reid_ippref, *leid_ippref;
1598 gid_address_t leid, reid;
1599 u8 *dmac = gid_address_mac (&reid);
1600 u8 *smac = gid_address_mac (&leid);
1601 u8 reid_set = 0, leid_set = 0;
1602 u32 vni;
Florin Corasf727db92016-06-23 15:01:58 +02001603 int rv;
1604
1605 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02001606 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corasf727db92016-06-23 15:01:58 +02001607 return 0;
1608
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001609 memset (&reid, 0, sizeof (reid));
1610 memset (&leid, 0, sizeof (leid));
Florin Corasf727db92016-06-23 15:01:58 +02001611
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001612 leid_ippref = &gid_address_ippref (&leid);
1613 reid_ippref = &gid_address_ippref (&reid);
Florin Corasf727db92016-06-23 15:01:58 +02001614
1615 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1616 {
1617 if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001618 is_add = 0;
Florin Corasf727db92016-06-23 15:01:58 +02001619 else if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001620 ;
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001621 else if (unformat (line_input, "reid %U",
1622 unformat_ip_prefix, reid_ippref))
Florin Corasa2157cf2016-08-16 21:09:14 +02001623 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001624 gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
1625 reid_set = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001626 }
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001627 else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
Florin Corasa2157cf2016-08-16 21:09:14 +02001628 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001629 gid_address_type (&reid) = GID_ADDR_MAC;
1630 reid_set = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001631 }
Florin Corasf727db92016-06-23 15:01:58 +02001632 else if (unformat (line_input, "vni %u", &vni))
Florin Corasa2157cf2016-08-16 21:09:14 +02001633 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001634 gid_address_vni (&leid) = vni;
1635 gid_address_vni (&reid) = vni;
Florin Corasa2157cf2016-08-16 21:09:14 +02001636 }
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001637 else if (unformat (line_input, "leid %U",
1638 unformat_ip_prefix, leid_ippref))
Florin Corasa2157cf2016-08-16 21:09:14 +02001639 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001640 gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
1641 leid_set = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001642 }
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001643 else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
Florin Corasa2157cf2016-08-16 21:09:14 +02001644 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001645 gid_address_type (&leid) = GID_ADDR_MAC;
1646 leid_set = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001647 }
Florin Corasf727db92016-06-23 15:01:58 +02001648 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001649 {
1650 clib_warning ("parse error");
1651 goto done;
1652 }
Florin Corasf727db92016-06-23 15:01:58 +02001653 }
1654
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001655 if (!reid_set || !leid_set)
Florin Corasf727db92016-06-23 15:01:58 +02001656 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001657 clib_warning ("missing remote or local eid!");
Florin Corasf727db92016-06-23 15:01:58 +02001658 goto done;
1659 }
1660
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001661 if ((gid_address_type (&leid) != gid_address_type (&reid))
1662 || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
1663 && ip_prefix_version (reid_ippref)
1664 != ip_prefix_version (leid_ippref)))
Florin Corasf727db92016-06-23 15:01:58 +02001665 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001666 clib_warning ("remote and local EIDs are of different types!");
1667 return error;
Florin Corasf727db92016-06-23 15:01:58 +02001668 }
1669
Florin Corasa2157cf2016-08-16 21:09:14 +02001670 memset (a, 0, sizeof (a[0]));
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001671 gid_address_copy (&a->leid, &leid);
1672 gid_address_copy (&a->reid, &reid);
Florin Coras71893ac2016-07-10 20:09:32 +02001673
Florin Corasf727db92016-06-23 15:01:58 +02001674 a->is_add = is_add;
Florin Corasf727db92016-06-23 15:01:58 +02001675 rv = vnet_lisp_add_del_adjacency (a);
1676
1677 if (rv)
Florin Corasa2157cf2016-08-16 21:09:14 +02001678 clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
Florin Corasf727db92016-06-23 15:01:58 +02001679
1680done:
1681 unformat_free (line_input);
Florin Corasf727db92016-06-23 15:01:58 +02001682 return error;
1683}
1684
Florin Corasa2157cf2016-08-16 21:09:14 +02001685/* *INDENT-OFF* */
Florin Corasf727db92016-06-23 15:01:58 +02001686VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1687 .path = "lisp adjacency",
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001688 .short_help = "lisp adjacency add|del vni <vni> reid <remote-eid> "
1689 "leid <local-eid>",
Florin Corasf727db92016-06-23 15:01:58 +02001690 .function = lisp_add_del_adjacency_command_fn,
1691};
Florin Corasa2157cf2016-08-16 21:09:14 +02001692/* *INDENT-ON* */
Florin Corasf727db92016-06-23 15:01:58 +02001693
Florin Corasdca88042016-09-14 16:01:38 +02001694int
1695vnet_lisp_set_map_request_mode (u8 mode)
1696{
1697 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1698
1699 if (vnet_lisp_enable_disable_status () == 0)
1700 {
1701 clib_warning ("LISP is disabled!");
1702 return VNET_API_ERROR_LISP_DISABLED;
1703 }
1704
1705 if (mode >= _MR_MODE_MAX)
1706 {
1707 clib_warning ("Invalid LISP map request mode %d!", mode);
1708 return VNET_API_ERROR_INVALID_ARGUMENT;
1709 }
1710
1711 lcm->map_request_mode = mode;
1712 return 0;
1713}
1714
1715static clib_error_t *
1716lisp_map_request_mode_command_fn (vlib_main_t * vm,
1717 unformat_input_t * input,
1718 vlib_cli_command_t * cmd)
1719{
1720 unformat_input_t _i, *i = &_i;
1721 map_request_mode_t mr_mode = _MR_MODE_MAX;
1722
1723 /* Get a line of input. */
1724 if (!unformat_user (input, unformat_line_input, i))
1725 return 0;
1726
1727 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1728 {
1729 if (unformat (i, "dst-only"))
1730 mr_mode = MR_MODE_DST_ONLY;
1731 else if (unformat (i, "src-dst"))
1732 mr_mode = MR_MODE_SRC_DST;
1733 else
1734 {
1735 clib_warning ("parse error '%U'", format_unformat_error, i);
1736 goto done;
1737 }
1738 }
1739
1740 if (_MR_MODE_MAX == mr_mode)
1741 {
1742 clib_warning ("No LISP map request mode entered!");
1743 return 0;
1744 }
1745
1746 vnet_lisp_set_map_request_mode (mr_mode);
1747done:
1748 return 0;
1749}
1750
1751/* *INDENT-OFF* */
1752VLIB_CLI_COMMAND (lisp_map_request_mode_command) = {
1753 .path = "lisp map-request mode",
1754 .short_help = "lisp map-request mode dst-only|src-dst",
1755 .function = lisp_map_request_mode_command_fn,
1756};
1757/* *INDENT-ON* */
1758
1759static u8 *
1760format_lisp_map_request_mode (u8 * s, va_list * args)
1761{
1762 u32 mode = va_arg (*args, u32);
1763
1764 switch (mode)
1765 {
1766 case 0:
1767 return format (0, "dst-only");
1768 case 1:
1769 return format (0, "src-dst");
1770 }
1771 return 0;
1772}
1773
1774static clib_error_t *
1775lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
1776 unformat_input_t * input,
1777 vlib_cli_command_t * cmd)
1778{
1779 vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
1780 vnet_lisp_get_map_request_mode ());
1781 return 0;
1782}
1783
1784/* *INDENT-OFF* */
1785VLIB_CLI_COMMAND (lisp_show_map_request_mode_command) = {
1786 .path = "show lisp map-request mode",
1787 .short_help = "show lisp map-request mode",
1788 .function = lisp_show_map_request_mode_command_fn,
1789};
1790/* *INDENT-ON* */
1791
Filip Tehlarf747cd82016-05-26 16:47:11 +02001792static clib_error_t *
1793lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001794 unformat_input_t * input,
1795 vlib_cli_command_t * cmd)
Filip Tehlarf747cd82016-05-26 16:47:11 +02001796{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001797 lisp_msmr_t *mr;
Florin Corasa2157cf2016-08-16 21:09:14 +02001798 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarf747cd82016-05-26 16:47:11 +02001799
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001800 vec_foreach (mr, lcm->map_resolvers)
Florin Corasa2157cf2016-08-16 21:09:14 +02001801 {
1802 vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
1803 }
Filip Tehlarf747cd82016-05-26 16:47:11 +02001804 return 0;
1805}
1806
Florin Corasa2157cf2016-08-16 21:09:14 +02001807/* *INDENT-OFF* */
Filip Tehlarf747cd82016-05-26 16:47:11 +02001808VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1809 .path = "show lisp map-resolvers",
1810 .short_help = "show lisp map-resolvers",
1811 .function = lisp_show_map_resolvers_command_fn,
1812};
Florin Corasa2157cf2016-08-16 21:09:14 +02001813/* *INDENT-ON* */
Filip Tehlarf747cd82016-05-26 16:47:11 +02001814
Filip Tehlar53f09e32016-05-19 14:25:44 +02001815int
1816vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1817{
Florin Corasa2157cf2016-08-16 21:09:14 +02001818 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar53f09e32016-05-19 14:25:44 +02001819 u32 locator_set_index = ~0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001820 mapping_t *m;
1821 uword *p;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001822
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001823 if (vnet_lisp_enable_disable_status () == 0)
1824 {
1825 clib_warning ("LISP is disabled!");
1826 return VNET_API_ERROR_LISP_DISABLED;
1827 }
1828
Filip Tehlar53f09e32016-05-19 14:25:44 +02001829 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1830 if (!p)
1831 {
1832 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1833 return -1;
1834 }
1835 locator_set_index = p[0];
1836
1837 if (is_add)
1838 {
1839 pool_get (lcm->mapping_pool, m);
1840 m->locator_set_index = locator_set_index;
1841 m->local = 1;
1842 lcm->pitr_map_index = m - lcm->mapping_pool;
1843
1844 /* enable pitr mode */
1845 lcm->lisp_pitr = 1;
1846 }
1847 else
1848 {
1849 /* remove pitr mapping */
1850 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1851
1852 /* disable pitr mode */
1853 lcm->lisp_pitr = 0;
1854 }
1855 return 0;
1856}
1857
1858static clib_error_t *
1859lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001860 unformat_input_t * input,
1861 vlib_cli_command_t * cmd)
Filip Tehlar53f09e32016-05-19 14:25:44 +02001862{
1863 u8 locator_name_set = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001864 u8 *locator_set_name = 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001865 u8 is_add = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001866 unformat_input_t _line_input, *line_input = &_line_input;
1867 clib_error_t *error = 0;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001868 int rv = 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001869
1870 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02001871 if (!unformat_user (input, unformat_line_input, line_input))
Filip Tehlar53f09e32016-05-19 14:25:44 +02001872 return 0;
1873
1874 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1875 {
1876 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +02001877 locator_name_set = 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001878 else if (unformat (line_input, "disable"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001879 is_add = 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001880 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001881 return clib_error_return (0, "parse error");
Filip Tehlar53f09e32016-05-19 14:25:44 +02001882 }
1883
1884 if (!locator_name_set)
1885 {
1886 clib_warning ("No locator set specified!");
1887 goto done;
1888 }
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001889 rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1890 if (0 != rv)
1891 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001892 error = clib_error_return (0, "failed to %s pitr!",
1893 is_add ? "add" : "delete");
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001894 }
Filip Tehlar53f09e32016-05-19 14:25:44 +02001895
1896done:
1897 if (locator_set_name)
1898 vec_free (locator_set_name);
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001899 return error;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001900}
1901
Florin Corasa2157cf2016-08-16 21:09:14 +02001902/* *INDENT-OFF* */
Filip Tehlar53f09e32016-05-19 14:25:44 +02001903VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1904 .path = "lisp pitr",
1905 .short_help = "lisp pitr [disable] ls <locator-set-name>",
1906 .function = lisp_pitr_set_locator_set_command_fn,
1907};
Florin Corasa2157cf2016-08-16 21:09:14 +02001908/* *INDENT-ON* */
Filip Tehlar53f09e32016-05-19 14:25:44 +02001909
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001910static clib_error_t *
1911lisp_show_pitr_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001912 unformat_input_t * input, vlib_cli_command_t * cmd)
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001913{
Florin Corasa2157cf2016-08-16 21:09:14 +02001914 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1915 mapping_t *m;
1916 locator_set_t *ls;
1917 u8 *tmp_str = 0;
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001918
1919 vlib_cli_output (vm, "%=20s%=16s",
Florin Corasa2157cf2016-08-16 21:09:14 +02001920 "pitr", lcm->lisp_pitr ? "locator-set" : "");
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001921
Florin Corasa2157cf2016-08-16 21:09:14 +02001922 if (!lcm->lisp_pitr)
1923 {
1924 vlib_cli_output (vm, "%=20s", "disable");
1925 return 0;
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001926 }
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001927
Florin Corasa2157cf2016-08-16 21:09:14 +02001928 if (~0 == lcm->pitr_map_index)
1929 {
1930 tmp_str = format (0, "N/A");
1931 }
1932 else
1933 {
1934 m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1935 if (~0 != m->locator_set_index)
1936 {
1937 ls =
1938 pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1939 tmp_str = format (0, "%s", ls->name);
1940 }
1941 else
1942 {
1943 tmp_str = format (0, "N/A");
1944 }
1945 }
1946 vec_add1 (tmp_str, 0);
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001947
Florin Corasa2157cf2016-08-16 21:09:14 +02001948 vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1949
1950 vec_free (tmp_str);
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001951
1952 return 0;
1953}
1954
Florin Corasa2157cf2016-08-16 21:09:14 +02001955/* *INDENT-OFF* */
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001956VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1957 .path = "show lisp pitr",
1958 .short_help = "Show pitr",
1959 .function = lisp_show_pitr_command_fn,
1960};
Florin Corasa2157cf2016-08-16 21:09:14 +02001961/* *INDENT-ON* */
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001962
1963static u8 *
1964format_eid_entry (u8 * s, va_list * args)
1965{
Florin Corasa2157cf2016-08-16 21:09:14 +02001966 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
1967 lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
1968 mapping_t *mapit = va_arg (*args, mapping_t *);
1969 locator_set_t *ls = va_arg (*args, locator_set_t *);
1970 gid_address_t *gid = &mapit->eid;
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02001971 u32 ttl = mapit->ttl;
1972 u8 aut = mapit->authoritative;
Florin Corasa2157cf2016-08-16 21:09:14 +02001973 u32 *loc_index;
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001974 u8 first_line = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001975 u8 *loc;
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001976
Florin Corasa2157cf2016-08-16 21:09:14 +02001977 u8 *type = ls->local ? format (0, "local(%s)", ls->name)
1978 : format (0, "remote");
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001979
1980 if (vec_len (ls->locator_indices) == 0)
1981 {
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02001982 s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
Florin Corasa2157cf2016-08-16 21:09:14 +02001983 type, ttl, aut);
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001984 }
1985 else
1986 {
1987 vec_foreach (loc_index, ls->locator_indices)
Florin Corasa2157cf2016-08-16 21:09:14 +02001988 {
1989 locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1990 if (l->local)
1991 loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1992 l->sw_if_index);
1993 else
1994 loc = format (0, "%U", format_ip_address,
1995 &gid_address_ip (&l->address));
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001996
Florin Corasa2157cf2016-08-16 21:09:14 +02001997 if (first_line)
1998 {
1999 s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
2000 gid, type, loc, ttl, aut);
2001 first_line = 0;
2002 }
2003 else
2004 s = format (s, "%55s%v\n", "", loc);
2005 }
Filip Tehlarb0ccf032016-06-22 09:18:12 +02002006 }
2007 return s;
2008}
2009
Florin Corase127a7e2016-02-18 22:20:01 +01002010static clib_error_t *
Filip Tehlarb0ccf032016-06-22 09:18:12 +02002011lisp_show_eid_table_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02002012 unformat_input_t * input,
2013 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +01002014{
Florin Corasa2157cf2016-08-16 21:09:14 +02002015 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2016 mapping_t *mapit;
2017 unformat_input_t _line_input, *line_input = &_line_input;
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002018 u32 mi;
2019 gid_address_t eid;
2020 u8 print_all = 1;
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002021 u8 filter = 0;
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002022
Florin Corasa2157cf2016-08-16 21:09:14 +02002023 memset (&eid, 0, sizeof (eid));
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002024
2025 /* Get a line of input. */
2026 if (!unformat_user (input, unformat_line_input, line_input))
2027 return 0;
2028
2029 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2030 {
2031 if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
Florin Corasa2157cf2016-08-16 21:09:14 +02002032 print_all = 0;
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002033 else if (unformat (line_input, "local"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002034 filter = 1;
2035 else if (unformat (line_input, "remote"))
2036 filter = 2;
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002037 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002038 return clib_error_return (0, "parse error: '%U'",
2039 format_unformat_error, line_input);
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002040 }
Florin Corase127a7e2016-02-18 22:20:01 +01002041
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002042 vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
Florin Corasa2157cf2016-08-16 21:09:14 +02002043 "EID", "type", "locators", "ttl", "autoritative");
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002044
2045 if (print_all)
2046 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002047 /* *INDENT-OFF* */
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002048 pool_foreach (mapit, lcm->mapping_pool,
2049 ({
2050 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
2051 mapit->locator_set_index);
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002052 if (filter && !((1 == filter && ls->local) ||
2053 (2 == filter && !ls->local)))
2054 {
2055 continue;
2056 }
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002057 vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002058 lcm, mapit, ls);
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002059 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002060 /* *INDENT-ON* */
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002061 }
2062 else
2063 {
2064 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
Florin Corasa2157cf2016-08-16 21:09:14 +02002065 if ((u32) ~ 0 == mi)
2066 return 0;
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002067
2068 mapit = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corasa2157cf2016-08-16 21:09:14 +02002069 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2070 mapit->locator_set_index);
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002071
2072 if (filter && !((1 == filter && ls->local) ||
Florin Corasa2157cf2016-08-16 21:09:14 +02002073 (2 == filter && !ls->local)))
2074 {
2075 return 0;
2076 }
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002077
2078 vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
Florin Corasa2157cf2016-08-16 21:09:14 +02002079 lcm, mapit, ls);
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002080 }
Florin Corase127a7e2016-02-18 22:20:01 +01002081
2082 return 0;
2083}
2084
Florin Corasa2157cf2016-08-16 21:09:14 +02002085/* *INDENT-OFF* */
Filip Tehlarb0ccf032016-06-22 09:18:12 +02002086VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
Florin Corase127a7e2016-02-18 22:20:01 +01002087 .path = "show lisp eid-table",
Filip Tehlarb0ccf032016-06-22 09:18:12 +02002088 .short_help = "Shows EID table",
2089 .function = lisp_show_eid_table_command_fn,
Florin Corase127a7e2016-02-18 22:20:01 +01002090};
Florin Corasa2157cf2016-08-16 21:09:14 +02002091/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002092
2093/* cleans locator to locator-set data and removes locators not part of
2094 * any locator-set */
2095static void
2096clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
2097{
Filip Tehlard1c5cc32016-05-30 10:39:20 +02002098 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002099 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
2100 for (i = 0; i < vec_len (ls->locator_indices); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01002101 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002102 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
2103 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2104 loc_indexp[0]);
2105 for (j = 0; j < vec_len (ls_indexes[0]); j++)
2106 {
2107 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
2108 if (ls_indexp[0] == lsi)
2109 break;
2110 }
Florin Corase127a7e2016-02-18 22:20:01 +01002111
Florin Corasa2157cf2016-08-16 21:09:14 +02002112 /* delete index for removed locator-set */
2113 vec_del1 (ls_indexes[0], j);
Florin Corase127a7e2016-02-18 22:20:01 +01002114
2115 /* delete locator if it's part of no locator-set */
2116 if (vec_len (ls_indexes[0]) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02002117 {
2118 pool_put_index (lcm->locator_pool, loc_indexp[0]);
2119 vec_add1 (to_be_deleted, i);
2120 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02002121 }
2122
2123 if (to_be_deleted)
2124 {
2125 for (i = 0; i < vec_len (to_be_deleted); i++)
Florin Corasa2157cf2016-08-16 21:09:14 +02002126 {
2127 loc_indexp = vec_elt_at_index (to_be_deleted, i);
2128 vec_del1 (ls->locator_indices, loc_indexp[0]);
2129 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02002130 vec_free (to_be_deleted);
Florin Corase127a7e2016-02-18 22:20:01 +01002131 }
2132}
Filip Tehlard1c5cc32016-05-30 10:39:20 +02002133
Florin Corasf727db92016-06-23 15:01:58 +02002134static inline uword *
2135get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002136{
Florin Corasa2157cf2016-08-16 21:09:14 +02002137 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002138
Florin Corasa2157cf2016-08-16 21:09:14 +02002139 ASSERT (a != NULL);
2140 ASSERT (p != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002141
2142 /* find locator-set */
2143 if (a->local)
2144 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002145 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002146 }
2147 else
2148 {
2149 *p = a->index;
2150 }
2151
2152 return p;
2153}
2154
Florin Corasf727db92016-06-23 15:01:58 +02002155static inline int
2156is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
Florin Corasa2157cf2016-08-16 21:09:14 +02002157 locator_t * loc)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002158{
Florin Corasa2157cf2016-08-16 21:09:14 +02002159 locator_t *itloc;
2160 u32 *locit;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002161
Florin Corasa2157cf2016-08-16 21:09:14 +02002162 ASSERT (ls != NULL);
2163 ASSERT (loc != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002164
Florin Corasa2157cf2016-08-16 21:09:14 +02002165 vec_foreach (locit, ls->locator_indices)
2166 {
2167 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2168 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
2169 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
2170 {
2171 clib_warning ("Duplicate locator");
2172 return VNET_API_ERROR_VALUE_EXIST;
2173 }
2174 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002175
2176 return 0;
2177}
2178
Florin Corasf727db92016-06-23 15:01:58 +02002179static inline void
Florin Corasa2157cf2016-08-16 21:09:14 +02002180remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
2181 u32 ls_index, u32 loc_id)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002182{
Florin Corasa2157cf2016-08-16 21:09:14 +02002183 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2184 u32 **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002185
Florin Corasa2157cf2016-08-16 21:09:14 +02002186 ASSERT (ls != NULL);
2187 ASSERT (locit != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002188
Florin Corasa2157cf2016-08-16 21:09:14 +02002189 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
2190 pool_put_index (lcm->locator_pool, locit[0]);
2191 vec_del1 (ls->locator_indices, loc_id);
2192 vec_del1 (ls_indexes[0], ls_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002193}
2194
2195int
2196vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02002197 locator_set_t * ls, u32 * ls_result)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002198{
Florin Corasa2157cf2016-08-16 21:09:14 +02002199 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2200 locator_t *loc = NULL, *itloc = NULL;
2201 uword _p = (u32) ~ 0, *p = &_p;
2202 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002203 u32 loc_id = ~0;
2204 int ret = 0;
2205
Florin Corasa2157cf2016-08-16 21:09:14 +02002206 ASSERT (a != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002207
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002208 if (vnet_lisp_enable_disable_status () == 0)
2209 {
2210 clib_warning ("LISP is disabled!");
2211 return VNET_API_ERROR_LISP_DISABLED;
2212 }
2213
Florin Corasa2157cf2016-08-16 21:09:14 +02002214 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002215 if (!p)
2216 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002217 clib_warning ("locator-set %v doesn't exist", a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002218 return VNET_API_ERROR_INVALID_ARGUMENT;
2219 }
2220
2221 if (ls == 0)
2222 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002223 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002224 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02002225 {
2226 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2227 p[0]);
2228 return VNET_API_ERROR_INVALID_ARGUMENT;
2229 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002230 }
2231
2232 if (a->is_add)
2233 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002234 if (ls_result)
2235 ls_result[0] = p[0];
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002236
Florin Corasa2157cf2016-08-16 21:09:14 +02002237 /* allocate locators */
2238 vec_foreach (itloc, a->locators)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002239 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002240 ret = is_locator_in_locator_set (lcm, ls, itloc);
2241 if (0 != ret)
2242 {
2243 return ret;
2244 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002245
Florin Corasa2157cf2016-08-16 21:09:14 +02002246 pool_get (lcm->locator_pool, loc);
2247 loc[0] = itloc[0];
2248 loc_index = loc - lcm->locator_pool;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002249
Florin Corasa2157cf2016-08-16 21:09:14 +02002250 vec_add1 (ls->locator_indices, loc_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002251
Florin Corasa2157cf2016-08-16 21:09:14 +02002252 vec_validate (lcm->locator_to_locator_sets, loc_index);
2253 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2254 loc_index);
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02002255 vec_add1 (ls_indexes[0], p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002256 }
Florin Corasa2157cf2016-08-16 21:09:14 +02002257 }
2258 else
2259 {
2260 ls_index = p[0];
2261
2262 itloc = a->locators;
2263 loc_id = 0;
2264 vec_foreach (locit, ls->locator_indices)
2265 {
2266 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2267
2268 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
2269 {
2270 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2271 }
2272 if (0 == loc->local &&
2273 !gid_address_cmp (&loc->address, &itloc->address))
2274 {
2275 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2276 }
2277
2278 loc_id++;
2279 }
2280 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002281
2282 return 0;
2283}
2284
Florin Corase127a7e2016-02-18 22:20:01 +01002285int
2286vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02002287 u32 * ls_result)
Florin Corase127a7e2016-02-18 22:20:01 +01002288{
Florin Corasa2157cf2016-08-16 21:09:14 +02002289 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2290 locator_set_t *ls;
2291 uword _p = (u32) ~ 0, *p = &_p;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002292 u32 ls_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02002293 u32 **eid_indexes;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002294 int ret = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002295
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002296 if (vnet_lisp_enable_disable_status () == 0)
2297 {
2298 clib_warning ("LISP is disabled!");
2299 return VNET_API_ERROR_LISP_DISABLED;
2300 }
2301
Florin Corase127a7e2016-02-18 22:20:01 +01002302 if (a->is_add)
2303 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002304 p = get_locator_set_index (a, p);
Florin Corase127a7e2016-02-18 22:20:01 +01002305
2306 /* overwrite */
Florin Corasa2157cf2016-08-16 21:09:14 +02002307 if (p && p[0] != (u32) ~ 0)
2308 {
2309 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2310 if (!ls)
2311 {
2312 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2313 p[0]);
2314 return -1;
2315 }
Florin Corase127a7e2016-02-18 22:20:01 +01002316
Florin Corasa2157cf2016-08-16 21:09:14 +02002317 /* clean locator to locator-set vectors and remove locators if
2318 * they're not part of another locator-set */
2319 clean_locator_to_locator_set (lcm, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01002320
Florin Corasa2157cf2016-08-16 21:09:14 +02002321 /* remove locator indices from locator set */
2322 vec_free (ls->locator_indices);
Florin Corase127a7e2016-02-18 22:20:01 +01002323
Florin Corasa2157cf2016-08-16 21:09:14 +02002324 ls_index = p[0];
Florin Corase127a7e2016-02-18 22:20:01 +01002325
Florin Corasa2157cf2016-08-16 21:09:14 +02002326 if (ls_result)
2327 ls_result[0] = p[0];
2328 }
Florin Corase127a7e2016-02-18 22:20:01 +01002329 /* new locator-set */
2330 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002331 {
2332 pool_get (lcm->locator_set_pool, ls);
2333 memset (ls, 0, sizeof (*ls));
2334 ls_index = ls - lcm->locator_set_pool;
Florin Corase127a7e2016-02-18 22:20:01 +01002335
Florin Corasa2157cf2016-08-16 21:09:14 +02002336 if (a->local)
2337 {
2338 ls->name = vec_dup (a->name);
Florin Corase127a7e2016-02-18 22:20:01 +01002339
Florin Corasa2157cf2016-08-16 21:09:14 +02002340 if (!lcm->locator_set_index_by_name)
2341 lcm->locator_set_index_by_name = hash_create_vec (
2342 /* size */
2343 0,
2344 sizeof
2345 (ls->name
2346 [0]),
2347 sizeof
2348 (uword));
2349 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
2350 ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01002351
Florin Corasa2157cf2016-08-16 21:09:14 +02002352 /* mark as local locator-set */
2353 vec_add1 (lcm->local_locator_set_indexes, ls_index);
2354 }
2355 ls->local = a->local;
2356 if (ls_result)
2357 ls_result[0] = ls_index;
2358 }
Florin Corase127a7e2016-02-18 22:20:01 +01002359
Florin Corasa2157cf2016-08-16 21:09:14 +02002360 ret = vnet_lisp_add_del_locator (a, ls, NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002361 if (0 != ret)
Florin Corasa2157cf2016-08-16 21:09:14 +02002362 {
2363 return ret;
2364 }
Florin Corase127a7e2016-02-18 22:20:01 +01002365 }
2366 else
2367 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002368 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002369 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02002370 {
2371 clib_warning ("locator-set %v doesn't exists", a->name);
2372 return -1;
2373 }
Florin Corase127a7e2016-02-18 22:20:01 +01002374
Florin Corasa2157cf2016-08-16 21:09:14 +02002375 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01002376 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02002377 {
2378 clib_warning ("locator-set with index %d doesn't exists", p[0]);
2379 return -1;
2380 }
Florin Corase127a7e2016-02-18 22:20:01 +01002381
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002382 if (lcm->mreq_itr_rlocs == p[0])
Florin Corasa2157cf2016-08-16 21:09:14 +02002383 {
2384 clib_warning ("Can't delete the locator-set used to constrain "
2385 "the itr-rlocs in map-requests!");
2386 return -1;
2387 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002388
Florin Corasa2157cf2016-08-16 21:09:14 +02002389 if (vec_len (lcm->locator_set_to_eids) != 0)
2390 {
2391 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
2392 if (vec_len (eid_indexes[0]) != 0)
2393 {
2394 clib_warning
2395 ("Can't delete a locator that supports a mapping!");
2396 return -1;
2397 }
2398 }
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02002399
2400 /* clean locator to locator-sets data */
2401 clean_locator_to_locator_set (lcm, p[0]);
2402
2403 if (ls->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02002404 {
2405 u32 it, lsi;
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02002406
Florin Corasa2157cf2016-08-16 21:09:14 +02002407 vec_foreach_index (it, lcm->local_locator_set_indexes)
2408 {
2409 lsi = vec_elt (lcm->local_locator_set_indexes, it);
2410 if (lsi == p[0])
2411 {
2412 vec_del1 (lcm->local_locator_set_indexes, it);
2413 break;
2414 }
2415 }
2416 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2417 }
2418 vec_free (ls->name);
2419 vec_free (ls->locator_indices);
2420 pool_put (lcm->locator_set_pool, ls);
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02002421 }
2422 return 0;
2423}
2424
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002425int
2426vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
2427{
2428 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2429
2430 lcm->rloc_probing = is_enable;
2431 return 0;
2432}
2433
2434int
2435vnet_lisp_map_register_enable_disable (u8 is_enable)
2436{
2437 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2438
2439 lcm->map_registering = is_enable;
2440 return 0;
2441}
2442
Filip Tehlar46d4e362016-05-09 09:39:26 +02002443clib_error_t *
Florin Corasf727db92016-06-23 15:01:58 +02002444vnet_lisp_enable_disable (u8 is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002445{
Florin Coras1a1adc72016-07-22 01:45:30 +02002446 u32 vni, dp_table;
Florin Corasa2157cf2016-08-16 21:09:14 +02002447 clib_error_t *error = 0;
2448 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2449 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002450
Florin Corasf727db92016-06-23 15:01:58 +02002451 a->is_en = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002452 error = vnet_lisp_gpe_enable_disable (a);
2453 if (error)
2454 {
2455 return clib_error_return (0, "failed to %s data-plane!",
Florin Corasa2157cf2016-08-16 21:09:14 +02002456 a->is_en ? "enable" : "disable");
Filip Tehlar46d4e362016-05-09 09:39:26 +02002457 }
2458
Florin Corasf727db92016-06-23 15:01:58 +02002459 if (is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002460 {
Florin Coras1a1adc72016-07-22 01:45:30 +02002461 /* enable all l2 and l3 ifaces */
Florin Corasa2157cf2016-08-16 21:09:14 +02002462
2463 /* *INDENT-OFF* */
Florin Coras1a1adc72016-07-22 01:45:30 +02002464 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2465 dp_add_del_iface(lcm, vni, 0, 1);
2466 }));
Florin Coras1a1adc72016-07-22 01:45:30 +02002467 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2468 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
Florin Corasf727db92016-06-23 15:01:58 +02002469 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002470 /* *INDENT-ON* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02002471 }
2472 else
2473 {
Florin Corasf727db92016-06-23 15:01:58 +02002474 /* clear interface table */
Florin Corasa2157cf2016-08-16 21:09:14 +02002475 hash_free (lcm->fwd_entry_by_mapping_index);
2476 pool_free (lcm->fwd_entry_pool);
Filip Tehlar46d4e362016-05-09 09:39:26 +02002477 }
2478
2479 /* update global flag */
Florin Corasf727db92016-06-23 15:01:58 +02002480 lcm->is_enabled = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002481
2482 return 0;
2483}
2484
2485static clib_error_t *
2486lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
Florin Corasa2157cf2016-08-16 21:09:14 +02002487 vlib_cli_command_t * cmd)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002488{
Florin Corasa2157cf2016-08-16 21:09:14 +02002489 unformat_input_t _line_input, *line_input = &_line_input;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002490 u8 is_enabled = 0;
2491 u8 is_set = 0;
2492
2493 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02002494 if (!unformat_user (input, unformat_line_input, line_input))
Filip Tehlar46d4e362016-05-09 09:39:26 +02002495 return 0;
2496
2497 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2498 {
2499 if (unformat (line_input, "enable"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002500 {
2501 is_set = 1;
2502 is_enabled = 1;
2503 }
Filip Tehlar46d4e362016-05-09 09:39:26 +02002504 else if (unformat (line_input, "disable"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002505 is_set = 1;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002506 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002507 {
2508 return clib_error_return (0, "parse error: '%U'",
2509 format_unformat_error, line_input);
2510 }
Filip Tehlar46d4e362016-05-09 09:39:26 +02002511 }
2512
2513 if (!is_set)
Florin Corasa2157cf2016-08-16 21:09:14 +02002514 return clib_error_return (0, "state not set");
Filip Tehlar46d4e362016-05-09 09:39:26 +02002515
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002516 vnet_lisp_enable_disable (is_enabled);
2517 return 0;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002518}
2519
Florin Corasa2157cf2016-08-16 21:09:14 +02002520/* *INDENT-OFF* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02002521VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
2522 .path = "lisp",
2523 .short_help = "lisp [enable|disable]",
2524 .function = lisp_enable_disable_command_fn,
2525};
Florin Corasa2157cf2016-08-16 21:09:14 +02002526/* *INDENT-ON* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02002527
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002528static clib_error_t *
2529lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
2530 unformat_input_t * input,
2531 vlib_cli_command_t * cmd)
2532{
2533 unformat_input_t _line_input, *line_input = &_line_input;
2534 u8 is_enabled = 0;
2535 u8 is_set = 0;
2536
2537 /* Get a line of input. */
2538 if (!unformat_user (input, unformat_line_input, line_input))
2539 return 0;
2540
2541 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2542 {
2543 if (unformat (line_input, "enable"))
2544 {
2545 is_set = 1;
2546 is_enabled = 1;
2547 }
2548 else if (unformat (line_input, "disable"))
2549 is_set = 1;
2550 else
2551 {
2552 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2553 line_input);
2554 return 0;
2555 }
2556 }
2557
2558 if (!is_set)
2559 {
2560 vlib_cli_output (vm, "state not set!");
2561 return 0;
2562 }
2563
2564 vnet_lisp_map_register_enable_disable (is_enabled);
2565 return 0;
2566}
2567
2568/* *INDENT-OFF* */
2569VLIB_CLI_COMMAND (lisp_map_register_enable_disable_command) = {
2570 .path = "lisp map-register",
2571 .short_help = "lisp map-register [enable|disable]",
2572 .function = lisp_map_register_enable_disable_command_fn,
2573};
2574/* *INDENT-ON* */
2575
2576static clib_error_t *
2577lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
2578 unformat_input_t * input,
2579 vlib_cli_command_t * cmd)
2580{
2581 unformat_input_t _line_input, *line_input = &_line_input;
2582 u8 is_enabled = 0;
2583 u8 is_set = 0;
2584
2585 /* Get a line of input. */
2586 if (!unformat_user (input, unformat_line_input, line_input))
2587 return 0;
2588
2589 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2590 {
2591 if (unformat (line_input, "enable"))
2592 {
2593 is_set = 1;
2594 is_enabled = 1;
2595 }
2596 else if (unformat (line_input, "disable"))
2597 is_set = 1;
2598 else
2599 {
2600 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2601 line_input);
2602 return 0;
2603 }
2604 }
2605
2606 if (!is_set)
2607 {
2608 vlib_cli_output (vm, "state not set!");
2609 return 0;
2610 }
2611
2612 vnet_lisp_rloc_probe_enable_disable (is_enabled);
2613 return 0;
2614}
2615
2616/* *INDENT-OFF* */
2617VLIB_CLI_COMMAND (lisp_rloc_probe_enable_disable_command) = {
2618 .path = "lisp rloc-probe",
2619 .short_help = "lisp rloc-probe [enable|disable]",
2620 .function = lisp_rloc_probe_enable_disable_command_fn,
2621};
2622/* *INDENT-ON* */
2623
Filip Tehlar46d4e362016-05-09 09:39:26 +02002624u8
2625vnet_lisp_enable_disable_status (void)
2626{
Florin Corasa2157cf2016-08-16 21:09:14 +02002627 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar46d4e362016-05-09 09:39:26 +02002628 return lcm->is_enabled;
2629}
2630
2631static u8 *
2632format_lisp_status (u8 * s, va_list * args)
2633{
Florin Corasa2157cf2016-08-16 21:09:14 +02002634 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar46d4e362016-05-09 09:39:26 +02002635 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
2636}
2637
2638static clib_error_t *
2639lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
Florin Corasa2157cf2016-08-16 21:09:14 +02002640 vlib_cli_command_t * cmd)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002641{
Florin Corasa2157cf2016-08-16 21:09:14 +02002642 u8 *msg = 0;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002643 msg = format (msg, "feature: %U\ngpe: %U\n",
Florin Corasa2157cf2016-08-16 21:09:14 +02002644 format_lisp_status, format_vnet_lisp_gpe_status);
Filip Tehlar46d4e362016-05-09 09:39:26 +02002645 vlib_cli_output (vm, "%v", msg);
2646 vec_free (msg);
2647 return 0;
2648}
2649
Florin Corasa2157cf2016-08-16 21:09:14 +02002650/* *INDENT-OFF* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02002651VLIB_CLI_COMMAND (lisp_show_status_command) = {
2652 .path = "show lisp status",
2653 .short_help = "show lisp status",
2654 .function = lisp_show_status_command_fn,
2655};
Florin Corasa2157cf2016-08-16 21:09:14 +02002656/* *INDENT-ON* */
Filip Tehlar324112f2016-06-02 16:07:38 +02002657
2658static clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002659lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
2660 unformat_input_t * input,
2661 vlib_cli_command_t * cmd)
Filip Tehlar324112f2016-06-02 16:07:38 +02002662{
Florin Corasa2157cf2016-08-16 21:09:14 +02002663 hash_pair_t *p;
Filip Tehlarc0681792016-08-24 14:11:07 +02002664 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corasa2157cf2016-08-16 21:09:14 +02002665 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarc0681792016-08-24 14:11:07 +02002666 uword *vni_table = 0;
2667 u8 is_l2 = 0;
Filip Tehlar324112f2016-06-02 16:07:38 +02002668
Filip Tehlarc0681792016-08-24 14:11:07 +02002669 /* Get a line of input. */
2670 if (!unformat_user (input, unformat_line_input, line_input))
2671 return 0;
2672
2673 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2674 {
2675 if (unformat (line_input, "l2"))
2676 {
2677 vni_table = lcm->bd_id_by_vni;
2678 is_l2 = 1;
2679 }
2680 else if (unformat (line_input, "l3"))
2681 {
2682 vni_table = lcm->table_id_by_vni;
2683 is_l2 = 0;
2684 }
2685 else
2686 return clib_error_return (0, "parse error: '%U'",
2687 format_unformat_error, line_input);
2688 }
2689
2690 if (!vni_table)
2691 {
2692 vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
2693 return 0;
2694 }
2695
2696 vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
Florin Corasa2157cf2016-08-16 21:09:14 +02002697
2698 /* *INDENT-OFF* */
Filip Tehlarc0681792016-08-24 14:11:07 +02002699 hash_foreach_pair (p, vni_table,
Florin Corasa2157cf2016-08-16 21:09:14 +02002700 ({
2701 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
2702 }));
2703 /* *INDENT-ON* */
2704
Filip Tehlar324112f2016-06-02 16:07:38 +02002705 return 0;
2706}
2707
Florin Corasa2157cf2016-08-16 21:09:14 +02002708/* *INDENT-OFF* */
Filip Tehlar324112f2016-06-02 16:07:38 +02002709VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
2710 .path = "show lisp eid-table map",
Filip Tehlarc0681792016-08-24 14:11:07 +02002711 .short_help = "show lisp eid-table l2|l3",
Filip Tehlar324112f2016-06-02 16:07:38 +02002712 .function = lisp_show_eid_table_map_command_fn,
2713};
Florin Corasa2157cf2016-08-16 21:09:14 +02002714/* *INDENT-ON* */
Filip Tehlar324112f2016-06-02 16:07:38 +02002715
Florin Corase127a7e2016-02-18 22:20:01 +01002716static clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002717lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
2718 unformat_input_t * input,
2719 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +01002720{
Florin Corasa2157cf2016-08-16 21:09:14 +02002721 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2722 vnet_main_t *vnm = lgm->vnet_main;
2723 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corase127a7e2016-02-18 22:20:01 +01002724 u8 is_add = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02002725 clib_error_t *error = 0;
2726 u8 *locator_set_name = 0;
2727 locator_t locator, *locators = 0;
2728 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
Florin Corase127a7e2016-02-18 22:20:01 +01002729 u32 ls_index = 0;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002730 int rv = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002731
Florin Corasa2157cf2016-08-16 21:09:14 +02002732 memset (&locator, 0, sizeof (locator));
2733 memset (a, 0, sizeof (a[0]));
Florin Corase127a7e2016-02-18 22:20:01 +01002734
2735 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02002736 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corase127a7e2016-02-18 22:20:01 +01002737 return 0;
2738
2739 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2740 {
2741 if (unformat (line_input, "add %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +02002742 is_add = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002743 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +02002744 is_add = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002745 else if (unformat (line_input, "iface %U p %d w %d",
Florin Corasa2157cf2016-08-16 21:09:14 +02002746 unformat_vnet_sw_interface, vnm,
2747 &locator.sw_if_index, &locator.priority,
2748 &locator.weight))
2749 {
2750 locator.local = 1;
2751 vec_add1 (locators, locator);
2752 }
Florin Corase127a7e2016-02-18 22:20:01 +01002753 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002754 {
2755 error = unformat_parse_error (line_input);
2756 goto done;
2757 }
Florin Corase127a7e2016-02-18 22:20:01 +01002758 }
2759
2760 a->name = locator_set_name;
2761 a->locators = locators;
2762 a->is_add = is_add;
2763 a->local = 1;
2764
Florin Corasa2157cf2016-08-16 21:09:14 +02002765 rv = vnet_lisp_add_del_locator_set (a, &ls_index);
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002766 if (0 != rv)
2767 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002768 error = clib_error_return (0, "failed to %s locator-set!",
2769 is_add ? "add" : "delete");
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002770 }
Florin Corase127a7e2016-02-18 22:20:01 +01002771
Florin Corasa2157cf2016-08-16 21:09:14 +02002772done:
2773 vec_free (locators);
Filip Tehlar53f09e32016-05-19 14:25:44 +02002774 if (locator_set_name)
2775 vec_free (locator_set_name);
Florin Corase127a7e2016-02-18 22:20:01 +01002776 return error;
2777}
2778
Florin Corasa2157cf2016-08-16 21:09:14 +02002779/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01002780VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2781 .path = "lisp locator-set",
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002782 .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2783 "p <priority> w <weight>]",
Florin Corase127a7e2016-02-18 22:20:01 +01002784 .function = lisp_add_del_locator_set_command_fn,
2785};
Florin Corasa2157cf2016-08-16 21:09:14 +02002786/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002787
2788static clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002789lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
2790 unformat_input_t * input,
2791 vlib_cli_command_t * cmd)
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002792{
Florin Corasa2157cf2016-08-16 21:09:14 +02002793 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2794 vnet_main_t *vnm = lgm->vnet_main;
2795 unformat_input_t _line_input, *line_input = &_line_input;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002796 u8 is_add = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02002797 clib_error_t *error = 0;
2798 u8 *locator_set_name = 0;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002799 u8 locator_set_name_set = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002800 locator_t locator, *locators = 0;
2801 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002802 u32 ls_index = 0;
2803
Florin Corasa2157cf2016-08-16 21:09:14 +02002804 memset (&locator, 0, sizeof (locator));
2805 memset (a, 0, sizeof (a[0]));
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002806
2807 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02002808 if (!unformat_user (input, unformat_line_input, line_input))
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002809 return 0;
2810
2811 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2812 {
2813 if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002814 is_add = 1;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002815 else if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002816 is_add = 0;
2817 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
2818 locator_set_name_set = 1;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002819 else if (unformat (line_input, "iface %U p %d w %d",
Florin Corasa2157cf2016-08-16 21:09:14 +02002820 unformat_vnet_sw_interface, vnm,
2821 &locator.sw_if_index, &locator.priority,
2822 &locator.weight))
2823 {
2824 locator.local = 1;
2825 vec_add1 (locators, locator);
2826 }
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002827 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002828 {
2829 error = unformat_parse_error (line_input);
2830 goto done;
2831 }
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002832 }
2833
2834 if (!locator_set_name_set)
2835 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002836 error = clib_error_return (0, "locator_set name not set!");
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002837 goto done;
Florin Corasa2157cf2016-08-16 21:09:14 +02002838 }
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002839
2840 a->name = locator_set_name;
2841 a->locators = locators;
2842 a->is_add = is_add;
2843 a->local = 1;
2844
Florin Corasa2157cf2016-08-16 21:09:14 +02002845 vnet_lisp_add_del_locator (a, 0, &ls_index);
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002846
Florin Corasa2157cf2016-08-16 21:09:14 +02002847done:
2848 vec_free (locators);
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002849 vec_free (locator_set_name);
2850 return error;
2851}
2852
Florin Corasa2157cf2016-08-16 21:09:14 +02002853/* *INDENT-OFF* */
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002854VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2855 .path = "lisp locator",
2856 .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2857 "p <priority> w <weight>",
2858 .function = lisp_add_del_locator_in_set_command_fn,
2859};
Florin Corasa2157cf2016-08-16 21:09:14 +02002860/* *INDENT-ON* */
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002861
2862static clib_error_t *
Florin Corase127a7e2016-02-18 22:20:01 +01002863lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02002864 unformat_input_t * input,
2865 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +01002866{
Florin Corasa2157cf2016-08-16 21:09:14 +02002867 locator_set_t *lsit;
2868 locator_t *loc;
2869 u32 *locit;
2870 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01002871
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02002872 vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
Florin Corasa2157cf2016-08-16 21:09:14 +02002873 "Priority", "Weight");
2874
2875 /* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01002876 pool_foreach (lsit, lcm->locator_set_pool,
2877 ({
2878 u8 * msg = 0;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002879 int next_line = 0;
Andrej Kozemcaka8691752016-07-27 10:33:38 +02002880 if (lsit->local)
2881 {
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02002882 msg = format (msg, "%v", lsit->name);
Andrej Kozemcaka8691752016-07-27 10:33:38 +02002883 }
2884 else
2885 {
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02002886 msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
Andrej Kozemcaka8691752016-07-27 10:33:38 +02002887 }
Florin Corase127a7e2016-02-18 22:20:01 +01002888 vec_foreach (locit, lsit->locator_indices)
2889 {
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002890 if (next_line)
2891 {
2892 msg = format (msg, "%16s", " ");
2893 }
Florin Corase127a7e2016-02-18 22:20:01 +01002894 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2895 if (loc->local)
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002896 msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
Florin Corase127a7e2016-02-18 22:20:01 +01002897 loc->weight);
2898 else
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002899 msg = format (msg, "%16U%16d%16d\n", format_ip_address,
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02002900 &gid_address_ip(&loc->address), loc->priority,
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002901 loc->weight);
2902 next_line = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002903 }
2904 vlib_cli_output (vm, "%v", msg);
2905 vec_free (msg);
2906 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002907 /* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002908 return 0;
2909}
2910
Florin Corasa2157cf2016-08-16 21:09:14 +02002911/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01002912VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2913 .path = "show lisp locator-set",
2914 .short_help = "Shows locator-sets",
2915 .function = lisp_cp_show_locator_sets_command_fn,
2916};
Florin Corasa2157cf2016-08-16 21:09:14 +02002917/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002918
2919int
2920vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2921{
Florin Corasa2157cf2016-08-16 21:09:14 +02002922 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01002923 u32 i;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002924 lisp_msmr_t _mr, *mr = &_mr;
Florin Corase127a7e2016-02-18 22:20:01 +01002925
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002926 if (vnet_lisp_enable_disable_status () == 0)
2927 {
2928 clib_warning ("LISP is disabled!");
2929 return VNET_API_ERROR_LISP_DISABLED;
2930 }
2931
Florin Corase127a7e2016-02-18 22:20:01 +01002932 if (a->is_add)
2933 {
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002934
2935 if (get_map_resolver (&a->address))
Florin Corasa2157cf2016-08-16 21:09:14 +02002936 {
2937 clib_warning ("map-resolver %U already exists!", format_ip_address,
2938 &a->address);
2939 return -1;
2940 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002941
2942 memset (mr, 0, sizeof (*mr));
Florin Corasa2157cf2016-08-16 21:09:14 +02002943 ip_address_copy (&mr->address, &a->address);
2944 vec_add1 (lcm->map_resolvers, *mr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002945
2946 if (vec_len (lcm->map_resolvers) == 1)
Florin Corasa2157cf2016-08-16 21:09:14 +02002947 lcm->do_map_resolver_election = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002948 }
2949 else
2950 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002951 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2952 {
2953 mr = vec_elt_at_index (lcm->map_resolvers, i);
2954 if (!ip_address_cmp (&mr->address, &a->address))
2955 {
2956 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2957 lcm->do_map_resolver_election = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002958
Florin Corasa2157cf2016-08-16 21:09:14 +02002959 vec_del1 (lcm->map_resolvers, i);
2960 break;
2961 }
2962 }
Florin Corase127a7e2016-02-18 22:20:01 +01002963 }
2964 return 0;
2965}
2966
2967static clib_error_t *
2968lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02002969 unformat_input_t * input,
2970 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +01002971{
Florin Corasa2157cf2016-08-16 21:09:14 +02002972 unformat_input_t _line_input, *line_input = &_line_input;
Florin Coras0f1c29c2016-07-26 12:12:17 +02002973 u8 is_add = 1, addr_set = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002974 ip_address_t ip_addr;
Florin Corasa2157cf2016-08-16 21:09:14 +02002975 clib_error_t *error = 0;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002976 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002977 vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
Florin Corase127a7e2016-02-18 22:20:01 +01002978
2979 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02002980 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corase127a7e2016-02-18 22:20:01 +01002981 return 0;
2982
2983 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2984 {
2985 if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002986 is_add = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002987 else if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002988 is_add = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002989 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
Florin Corasa2157cf2016-08-16 21:09:14 +02002990 addr_set = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002991 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002992 {
2993 error = unformat_parse_error (line_input);
2994 goto done;
2995 }
Florin Corase127a7e2016-02-18 22:20:01 +01002996 }
Florin Coras0f1c29c2016-07-26 12:12:17 +02002997
2998 if (!addr_set)
2999 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003000 error = clib_error_return (0, "Map-resolver address must be set!");
Florin Coras0f1c29c2016-07-26 12:12:17 +02003001 goto done;
3002 }
3003
Florin Corase127a7e2016-02-18 22:20:01 +01003004 a->is_add = is_add;
3005 a->address = ip_addr;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02003006 rv = vnet_lisp_add_del_map_resolver (a);
3007 if (0 != rv)
3008 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003009 error = clib_error_return (0, "failed to %s map-resolver!",
3010 is_add ? "add" : "delete");
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02003011 }
Florin Corase127a7e2016-02-18 22:20:01 +01003012
Florin Corasa2157cf2016-08-16 21:09:14 +02003013done:
Florin Corase127a7e2016-02-18 22:20:01 +01003014 return error;
3015}
3016
Florin Corasa2157cf2016-08-16 21:09:14 +02003017/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01003018VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
3019 .path = "lisp map-resolver",
3020 .short_help = "lisp map-resolver add/del <ip_address>",
3021 .function = lisp_add_del_map_resolver_command_fn,
3022};
Florin Corasa2157cf2016-08-16 21:09:14 +02003023/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01003024
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003025int
3026vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
3027{
Florin Corasa2157cf2016-08-16 21:09:14 +02003028 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3029 uword *p = 0;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003030
Florin Corasf727db92016-06-23 15:01:58 +02003031 if (vnet_lisp_enable_disable_status () == 0)
3032 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003033 clib_warning ("LISP is disabled!");
Florin Corasf727db92016-06-23 15:01:58 +02003034 return VNET_API_ERROR_LISP_DISABLED;
3035 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003036
3037 if (a->is_add)
3038 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003039 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003040 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02003041 {
3042 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
3043 return VNET_API_ERROR_INVALID_ARGUMENT;
3044 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003045
3046 lcm->mreq_itr_rlocs = p[0];
3047 }
3048 else
3049 {
3050 lcm->mreq_itr_rlocs = ~0;
3051 }
3052
3053 return 0;
3054}
3055
3056static clib_error_t *
Florin Corasf727db92016-06-23 15:01:58 +02003057lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02003058 unformat_input_t * input,
3059 vlib_cli_command_t * cmd)
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003060{
Florin Corasa2157cf2016-08-16 21:09:14 +02003061 unformat_input_t _line_input, *line_input = &_line_input;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003062 u8 is_add = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02003063 u8 *locator_set_name = 0;
3064 clib_error_t *error = 0;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003065 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02003066 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003067
3068 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02003069 if (!unformat_user (input, unformat_line_input, line_input))
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003070 return 0;
3071
3072 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3073 {
3074 if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02003075 is_add = 0;
Filip Tehlar240977f2016-10-03 14:08:59 +02003076 else if (unformat (line_input, "add %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +02003077 is_add = 1;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003078 else
Florin Corasa2157cf2016-08-16 21:09:14 +02003079 {
3080 error = unformat_parse_error (line_input);
3081 goto done;
3082 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003083 }
3084
3085 a->is_add = is_add;
3086 a->locator_set_name = locator_set_name;
3087 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
3088 if (0 != rv)
3089 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003090 error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
3091 is_add ? "add" : "delete");
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003092 }
3093
Florin Corasa2157cf2016-08-16 21:09:14 +02003094 vec_free (locator_set_name);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003095
Florin Corasa2157cf2016-08-16 21:09:14 +02003096done:
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003097 return error;
3098
3099}
3100
Florin Corasa2157cf2016-08-16 21:09:14 +02003101/* *INDENT-OFF* */
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003102VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
3103 .path = "lisp map-request itr-rlocs",
3104 .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
3105 .function = lisp_add_del_mreq_itr_rlocs_command_fn,
3106};
Florin Corasa2157cf2016-08-16 21:09:14 +02003107/* *INDENT-ON* */
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003108
3109static clib_error_t *
3110lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02003111 unformat_input_t * input,
3112 vlib_cli_command_t * cmd)
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003113{
Florin Corasa2157cf2016-08-16 21:09:14 +02003114 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3115 locator_set_t *loc_set;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003116
3117 vlib_cli_output (vm, "%=20s", "itr-rlocs");
3118
3119 if (~0 == lcm->mreq_itr_rlocs)
3120 {
3121 return 0;
3122 }
3123
3124 loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
3125
3126 vlib_cli_output (vm, "%=20s", loc_set->name);
3127
3128 return 0;
3129}
3130
Florin Corasa2157cf2016-08-16 21:09:14 +02003131/* *INDENT-OFF* */
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003132VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
3133 .path = "show lisp map-request itr-rlocs",
3134 .short_help = "Shows map-request itr-rlocs",
3135 .function = lisp_show_mreq_itr_rlocs_command_fn,
3136};
Florin Corasa2157cf2016-08-16 21:09:14 +02003137/* *INDENT-ON* */
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003138
Florin Corase127a7e2016-02-18 22:20:01 +01003139/* Statistics (not really errors) */
3140#define foreach_lisp_cp_lookup_error \
3141_(DROP, "drop") \
3142_(MAP_REQUESTS_SENT, "map-request sent")
3143
Florin Corasa2157cf2016-08-16 21:09:14 +02003144static char *lisp_cp_lookup_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01003145#define _(sym,string) string,
3146 foreach_lisp_cp_lookup_error
3147#undef _
3148};
3149
3150typedef enum
3151{
3152#define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02003153 foreach_lisp_cp_lookup_error
Florin Corase127a7e2016-02-18 22:20:01 +01003154#undef _
3155 LISP_CP_LOOKUP_N_ERROR,
3156} lisp_cp_lookup_error_t;
3157
3158typedef enum
3159{
3160 LISP_CP_LOOKUP_NEXT_DROP,
Florin Corase127a7e2016-02-18 22:20:01 +01003161 LISP_CP_LOOKUP_N_NEXT,
3162} lisp_cp_lookup_next_t;
3163
3164typedef struct
3165{
3166 gid_address_t dst_eid;
Andrej Kozemcak94e34762016-05-25 12:43:21 +02003167 ip_address_t map_resolver_ip;
Florin Corase127a7e2016-02-18 22:20:01 +01003168} lisp_cp_lookup_trace_t;
3169
3170u8 *
3171format_lisp_cp_lookup_trace (u8 * s, va_list * args)
3172{
3173 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3174 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02003175 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01003176
3177 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
Florin Corasa2157cf2016-08-16 21:09:14 +02003178 format_ip_address, &t->map_resolver_ip, format_gid_address,
3179 &t->dst_eid);
Florin Corase127a7e2016-02-18 22:20:01 +01003180 return s;
3181}
3182
Florin Coras1c494842016-06-16 21:08:56 +02003183int
3184get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
Florin Corasa2157cf2016-08-16 21:09:14 +02003185 ip_address_t * sloc)
Florin Corasc2c90082016-06-13 18:37:15 +02003186{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003187 lisp_msmr_t *mrit;
Florin Corasa2157cf2016-08-16 21:09:14 +02003188 ip_address_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01003189
Florin Corasa2157cf2016-08-16 21:09:14 +02003190 if (vec_len (lcm->map_resolvers) == 0)
Florin Corase127a7e2016-02-18 22:20:01 +01003191 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003192 clib_warning ("No map-resolver configured");
Florin Coras1c494842016-06-16 21:08:56 +02003193 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003194 }
3195
Florin Corasddfafb82016-05-13 18:09:56 +02003196 /* find the first mr ip we have a route to and the ip of the
3197 * iface that has a route to it */
Florin Corasa2157cf2016-08-16 21:09:14 +02003198 vec_foreach (mrit, lcm->map_resolvers)
3199 {
3200 a = &mrit->address;
3201 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
3202 {
3203 ip_address_copy (mr_ip, a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003204
Florin Corasa2157cf2016-08-16 21:09:14 +02003205 /* also update globals */
3206 return 1;
3207 }
3208 }
Florin Corasddfafb82016-05-13 18:09:56 +02003209
Florin Corasa2157cf2016-08-16 21:09:14 +02003210 clib_warning ("Can't find map-resolver and local interface ip!");
Florin Coras1c494842016-06-16 21:08:56 +02003211 return 0;
Florin Corasddfafb82016-05-13 18:09:56 +02003212}
Filip Tehlar254b0362016-04-07 10:04:34 +02003213
Filip Tehlarbeceab92016-04-20 17:21:55 +02003214static gid_address_t *
Filip Tehlar254b0362016-04-07 10:04:34 +02003215build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
3216{
Florin Corasa2157cf2016-08-16 21:09:14 +02003217 void *addr;
Filip Tehlar254b0362016-04-07 10:04:34 +02003218 u32 i;
Florin Corasa2157cf2016-08-16 21:09:14 +02003219 locator_t *loc;
3220 u32 *loc_indexp;
3221 ip_interface_address_t *ia = 0;
3222 gid_address_t gid_data, *gid = &gid_data;
3223 gid_address_t *rlocs = 0;
3224 ip_prefix_t *ippref = &gid_address_ippref (gid);
3225 ip_address_t *rloc = &ip_prefix_addr (ippref);
Filip Tehlar254b0362016-04-07 10:04:34 +02003226
Filip Tehlar324112f2016-06-02 16:07:38 +02003227 memset (gid, 0, sizeof (gid[0]));
Filip Tehlarbeceab92016-04-20 17:21:55 +02003228 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
Florin Corasa2157cf2016-08-16 21:09:14 +02003229 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
Filip Tehlar254b0362016-04-07 10:04:34 +02003230 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003231 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
Filip Tehlar254b0362016-04-07 10:04:34 +02003232 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
3233
Filip Tehlar254b0362016-04-07 10:04:34 +02003234 /* Add ipv4 locators first TODO sort them */
Florin Corasa2157cf2016-08-16 21:09:14 +02003235
3236 /* *INDENT-OFF* */
Filip Tehlar254b0362016-04-07 10:04:34 +02003237 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3238 loc->sw_if_index, 1 /* unnumbered */,
3239 ({
Florin Coras1c494842016-06-16 21:08:56 +02003240 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
3241 ip_address_set (rloc, addr, IP4);
Florin Corasddfafb82016-05-13 18:09:56 +02003242 ip_prefix_len (ippref) = 32;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02003243 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02003244 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02003245 }));
3246
Filip Tehlar254b0362016-04-07 10:04:34 +02003247 /* Add ipv6 locators */
3248 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3249 loc->sw_if_index, 1 /* unnumbered */,
3250 ({
Florin Coras1c494842016-06-16 21:08:56 +02003251 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
3252 ip_address_set (rloc, addr, IP6);
Florin Corasddfafb82016-05-13 18:09:56 +02003253 ip_prefix_len (ippref) = 128;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02003254 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02003255 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02003256 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02003257 /* *INDENT-ON* */
3258
Filip Tehlar254b0362016-04-07 10:04:34 +02003259 }
3260 return rlocs;
3261}
3262
Florin Corase127a7e2016-02-18 22:20:01 +01003263static vlib_buffer_t *
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003264build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
3265 ip_address_t * sloc, ip_address_t * rloc,
3266 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
3267{
3268 vlib_buffer_t *b;
3269 u32 bi;
3270 vlib_main_t *vm = lcm->vlib_main;
3271
3272 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3273 {
3274 clib_warning ("Can't allocate buffer for Map-Request!");
3275 return 0;
3276 }
3277
3278 b = vlib_get_buffer (vm, bi);
3279
3280 /* leave some space for the encap headers */
3281 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3282
3283 /* put lisp msg */
3284 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
3285 1 /* rloc probe */ , nonce_res);
3286
3287 /* push outer ip header */
3288 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3289 rloc);
3290
3291 bi_res[0] = bi;
3292
3293 return b;
3294}
3295
3296static vlib_buffer_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02003297build_encapsulated_map_request (lisp_cp_main_t * lcm,
3298 gid_address_t * seid, gid_address_t * deid,
3299 locator_set_t * loc_set, ip_address_t * mr_ip,
3300 ip_address_t * sloc, u8 is_smr_invoked,
3301 u64 * nonce_res, u32 * bi_res)
Florin Corase127a7e2016-02-18 22:20:01 +01003302{
Florin Corasa2157cf2016-08-16 21:09:14 +02003303 vlib_buffer_t *b;
Florin Corase127a7e2016-02-18 22:20:01 +01003304 u32 bi;
Florin Corasa2157cf2016-08-16 21:09:14 +02003305 gid_address_t *rlocs = 0;
3306 vlib_main_t *vm = lcm->vlib_main;
Florin Corase127a7e2016-02-18 22:20:01 +01003307
3308 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3309 {
3310 clib_warning ("Can't allocate buffer for Map-Request!");
3311 return 0;
3312 }
3313
3314 b = vlib_get_buffer (vm, bi);
3315
3316 /* leave some space for the encap headers */
3317 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3318
Filip Tehlar254b0362016-04-07 10:04:34 +02003319 /* get rlocs */
3320 rlocs = build_itr_rloc_list (lcm, loc_set);
3321
Filip Tehlard5fcc462016-10-17 16:20:18 +02003322 if (MR_MODE_SRC_DST == lcm->map_request_mode
3323 && GID_ADDR_SRC_DST != gid_address_type (deid))
Florin Corasdca88042016-09-14 16:01:38 +02003324 {
3325 gid_address_t sd;
3326 memset (&sd, 0, sizeof (sd));
3327 build_src_dst (&sd, seid, deid);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003328 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
3329 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02003330 }
3331 else
3332 {
3333 /* put lisp msg */
3334 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003335 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02003336 }
Florin Corase127a7e2016-02-18 22:20:01 +01003337
3338 /* push ecm: udp-ip-lisp */
3339 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
3340
Florin Corase127a7e2016-02-18 22:20:01 +01003341 /* push outer ip header */
Florin Corasddfafb82016-05-13 18:09:56 +02003342 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
Florin Corasa2157cf2016-08-16 21:09:14 +02003343 mr_ip);
Florin Corase127a7e2016-02-18 22:20:01 +01003344
3345 bi_res[0] = bi;
Filip Tehlar254b0362016-04-07 10:04:34 +02003346
Florin Corasa2157cf2016-08-16 21:09:14 +02003347 vec_free (rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01003348 return b;
3349}
3350
3351static void
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003352reset_pending_mr_counters (pending_map_request_t * r)
Florin Corase127a7e2016-02-18 22:20:01 +01003353{
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003354 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3355 r->retries_num = 0;
3356}
3357
3358static int
3359elect_map_resolver (lisp_cp_main_t * lcm)
3360{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003361 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003362
3363 vec_foreach (mr, lcm->map_resolvers)
Florin Corasa2157cf2016-08-16 21:09:14 +02003364 {
3365 if (!mr->is_down)
3366 {
3367 ip_address_copy (&lcm->active_map_resolver, &mr->address);
3368 lcm->do_map_resolver_election = 0;
3369 return 1;
3370 }
3371 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003372 return 0;
3373}
3374
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003375static void
3376free_map_register_records (mapping_t * maps)
3377{
3378 mapping_t *map;
3379 vec_foreach (map, maps) vec_free (map->locators);
3380
3381 vec_free (maps);
3382}
3383
3384static void
3385add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
3386 ip_address_t * probed_loc)
3387{
3388 u32 *li;
3389 locator_t *loc, new;
3390 ip_interface_address_t *ia = 0;
3391 void *addr;
3392 ip_address_t *new_ip = &gid_address_ip (&new.address);
3393
3394 m->locators = 0;
3395 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
3396 locator_set_index);
3397 vec_foreach (li, ls->locator_indices)
3398 {
3399 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
3400 new = loc[0];
3401 if (loc->local)
3402 {
3403 /* *INDENT-OFF* */
3404 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3405 loc->sw_if_index, 1 /* unnumbered */,
3406 ({
3407 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
3408 ia);
3409 ip_address_set (new_ip, addr, IP4);
3410 }));
3411
3412 /* Add ipv6 locators */
3413 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3414 loc->sw_if_index, 1 /* unnumbered */,
3415 ({
3416 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
3417 ia);
3418 ip_address_set (new_ip, addr, IP6);
3419 }));
3420 /* *INDENT-ON* */
3421
3422 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
3423 new.probed = 1;
3424 }
3425 vec_add1 (m->locators, new);
3426 }
3427}
3428
3429static mapping_t *
3430build_map_register_record_list (lisp_cp_main_t * lcm)
3431{
3432 mapping_t *recs = 0, rec, *m;
3433
3434 /* *INDENT-OFF* */
3435 pool_foreach(m, lcm->mapping_pool,
3436 {
3437 /* for now build only local mappings */
3438 if (!m->local)
3439 continue;
3440
3441 rec = m[0];
3442 add_locators (lcm, &rec, m->locator_set_index, NULL);
3443 vec_add1 (recs, rec);
3444 });
3445 /* *INDENT-ON* */
3446
3447 return recs;
3448}
3449
3450static int
3451update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
3452 lisp_key_type_t key_id, u8 * key,
3453 u16 auth_data_len, u32 msg_len)
3454{
3455 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
3456 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
3457
3458 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3459 (unsigned char *) map_reg_hdr, msg_len, NULL,
3460 NULL);
3461 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
3462
3463 return 0;
3464}
3465
3466static vlib_buffer_t *
3467build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
3468 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
3469 mapping_t * records, lisp_key_type_t key_id, u8 * key,
3470 u32 * bi_res)
3471{
3472 void *map_reg_hdr;
3473 vlib_buffer_t *b;
3474 u32 bi, auth_data_len = 0, msg_len = 0;
3475 vlib_main_t *vm = lcm->vlib_main;
3476
3477 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3478 {
3479 clib_warning ("Can't allocate buffer for Map-Register!");
3480 return 0;
3481 }
3482
3483 b = vlib_get_buffer (vm, bi);
3484
3485 /* leave some space for the encap headers */
3486 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3487
3488 auth_data_len = auth_data_len_by_key_id (key_id);
3489 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
3490 auth_data_len, nonce_res,
3491 &msg_len);
3492
3493 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
3494 msg_len);
3495
3496 /* push outer ip header */
3497 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3498 ms_ip);
3499
3500 bi_res[0] = bi;
3501 return b;
3502}
3503
3504static int
3505get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
3506{
3507 lisp_msmr_t *mr;
3508 while (lcm->do_map_resolver_election
3509 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
3510 &lcm->active_map_resolver,
3511 ip)))
3512 {
3513 if (0 == elect_map_resolver (lcm))
3514 /* all map resolvers are down */
3515 {
3516 /* restart MR checking by marking all of them up */
3517 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
3518 return -1;
3519 }
3520 }
3521 return 0;
3522}
3523
3524static int
3525send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
3526 u32 local_locator_set_index, ip_address_t * sloc,
3527 ip_address_t * rloc)
3528{
3529 locator_set_t *ls;
3530 u32 bi;
3531 vlib_buffer_t *b;
3532 vlib_frame_t *f;
3533 u64 nonce = 0;
3534 u32 next_index, *to_next;
3535 gid_address_t *itr_rlocs;
3536
3537 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
3538 itr_rlocs = build_itr_rloc_list (lcm, ls);
3539
3540 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
3541 vec_free (itr_rlocs);
3542 if (!b)
3543 return -1;
3544
3545 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3546
3547 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3548 ip4_lookup_node.index : ip6_lookup_node.index;
3549
3550 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3551
3552 /* Enqueue the packet */
3553 to_next = vlib_frame_vector_args (f);
3554 to_next[0] = bi;
3555 f->n_vectors = 1;
3556 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3557
3558 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3559 return 0;
3560}
3561
3562static int
3563send_rloc_probes (lisp_cp_main_t * lcm)
3564{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003565 u8 lprio = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003566 mapping_t *lm;
3567 fwd_entry_t *e;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003568 locator_pair_t *lp;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003569 u32 si;
3570
3571 /* *INDENT-OFF* */
3572 pool_foreach (e, lcm->fwd_entry_pool,
3573 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003574 if (vec_len (e->locator_pairs) == 0)
3575 continue;
3576
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003577 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
3578 if (~0 == si)
3579 {
3580 clib_warning ("internal error: cannot find local eid %U in "
3581 "map-cache!", format_gid_address, &e->leid);
3582 continue;
3583 }
3584 lm = pool_elt_at_index (lcm->mapping_pool, si);
3585
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003586 /* get the best (lowest) priority */
3587 lprio = e->locator_pairs[0].priority;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003588
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003589 /* send rloc-probe for pair(s) with the best remote locator priority */
3590 vec_foreach (lp, e->locator_pairs)
3591 {
3592 if (lp->priority != lprio)
3593 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003594
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003595 /* get first remote locator */
3596 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
3597 &lp->rmt_loc);
3598 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003599 });
3600 /* *INDENT-ON* */
3601
3602 return 0;
3603}
3604
3605static int
3606send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
3607{
3608 u32 bi;
3609 vlib_buffer_t *b;
3610 ip_address_t sloc;
3611 vlib_frame_t *f;
3612 u64 nonce = 0;
3613 u32 next_index, *to_next;
3614 ip_address_t *ms = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003615 mapping_t *records, *r, *g;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003616
3617 // TODO: support multiple map servers and do election
3618 if (0 == vec_len (lcm->map_servers))
3619 return -1;
3620
3621 ms = &lcm->map_servers[0].address;
3622
3623 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
3624 {
3625 clib_warning ("no eligible interface address found for %U!",
3626 format_ip_address, &lcm->map_servers[0]);
3627 return -1;
3628 }
3629
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003630 records = build_map_register_record_list (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003631 if (!records)
3632 return -1;
3633
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003634 vec_foreach (r, records)
3635 {
3636 u8 *key = r->key;
3637 u8 key_id = r->key_id;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003638
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003639 if (!key)
3640 continue; /* no secret key -> map-register cannot be sent */
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003641
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003642 g = 0;
3643 // TODO: group mappings that share common key
3644 vec_add1 (g, r[0]);
3645 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, g,
3646 key_id, key, &bi);
3647 vec_free (g);
3648 if (!b)
3649 continue;
3650
3651 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3652
3653 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3654 ip4_lookup_node.index : ip6_lookup_node.index;
3655
3656 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3657
3658 /* Enqueue the packet */
3659 to_next = vlib_frame_vector_args (f);
3660 to_next[0] = bi;
3661 f->n_vectors = 1;
3662 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3663
3664 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3665 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003666 free_map_register_records (records);
3667
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003668 return 0;
3669}
3670
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003671#define send_encapsulated_map_request(lcm, seid, deid, smr) \
3672 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
3673
3674#define resend_encapsulated_map_request(lcm, seid, deid, smr) \
3675 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
3676
3677static int
Florin Corasa2157cf2016-08-16 21:09:14 +02003678_send_encapsulated_map_request (lisp_cp_main_t * lcm,
3679 gid_address_t * seid, gid_address_t * deid,
3680 u8 is_smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003681{
Florin Corasa2157cf2016-08-16 21:09:14 +02003682 u32 next_index, bi = 0, *to_next, map_index;
3683 vlib_buffer_t *b;
3684 vlib_frame_t *f;
Florin Corase127a7e2016-02-18 22:20:01 +01003685 u64 nonce = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02003686 locator_set_t *loc_set;
3687 mapping_t *map;
3688 pending_map_request_t *pmr, *duplicate_pmr = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003689 ip_address_t sloc;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003690 u32 ls_index;
Florin Corase127a7e2016-02-18 22:20:01 +01003691
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003692 /* if there is already a pending request remember it */
Florin Corasa2157cf2016-08-16 21:09:14 +02003693
3694 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003695 pool_foreach(pmr, lcm->pending_map_requests_pool,
3696 ({
3697 if (!gid_address_cmp (&pmr->src, seid)
3698 && !gid_address_cmp (&pmr->dst, deid))
3699 {
3700 duplicate_pmr = pmr;
3701 break;
3702 }
3703 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02003704 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003705
3706 if (!is_resend && duplicate_pmr)
3707 {
3708 /* don't send the request if there is a pending map request already */
3709 return 0;
3710 }
3711
Florin Corase127a7e2016-02-18 22:20:01 +01003712 /* get locator-set for seid */
Filip Tehlar53f09e32016-05-19 14:25:44 +02003713 if (!lcm->lisp_pitr)
Florin Corase127a7e2016-02-18 22:20:01 +01003714 {
Filip Tehlar53f09e32016-05-19 14:25:44 +02003715 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
3716 if (map_index == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003717 {
3718 clib_warning ("No local mapping found in eid-table for %U!",
3719 format_gid_address, seid);
3720 return -1;
3721 }
Filip Tehlar53f09e32016-05-19 14:25:44 +02003722
3723 map = pool_elt_at_index (lcm->mapping_pool, map_index);
3724
3725 if (!map->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02003726 {
3727 clib_warning
3728 ("Mapping found for src eid %U is not marked as local!",
3729 format_gid_address, seid);
3730 return -1;
3731 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003732 ls_index = map->locator_set_index;
Filip Tehlar53f09e32016-05-19 14:25:44 +02003733 }
3734 else
3735 {
3736 map_index = lcm->pitr_map_index;
3737 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003738 ls_index = map->locator_set_index;
Florin Corase127a7e2016-02-18 22:20:01 +01003739 }
3740
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003741 /* overwrite locator set if map-request itr-rlocs configured */
3742 if (~0 != lcm->mreq_itr_rlocs)
3743 {
3744 ls_index = lcm->mreq_itr_rlocs;
3745 }
3746
3747 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01003748
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003749 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003750 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003751 if (duplicate_pmr)
3752 duplicate_pmr->to_be_removed = 1;
3753 return -1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003754 }
Florin Corasddfafb82016-05-13 18:09:56 +02003755
Florin Corase127a7e2016-02-18 22:20:01 +01003756 /* build the encapsulated map request */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003757 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
Florin Corasa2157cf2016-08-16 21:09:14 +02003758 &lcm->active_map_resolver,
3759 &sloc, is_smr_invoked, &nonce, &bi);
Florin Corase127a7e2016-02-18 22:20:01 +01003760
3761 if (!b)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003762 return -1;
Florin Corase127a7e2016-02-18 22:20:01 +01003763
Florin Corasf727db92016-06-23 15:01:58 +02003764 /* set fib index to default and lookup node */
Florin Corasa2157cf2016-08-16 21:09:14 +02003765 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3766 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3767 ip4_lookup_node.index : ip6_lookup_node.index;
Florin Corase127a7e2016-02-18 22:20:01 +01003768
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003769 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
Florin Corase127a7e2016-02-18 22:20:01 +01003770
3771 /* Enqueue the packet */
3772 to_next = vlib_frame_vector_args (f);
3773 to_next[0] = bi;
3774 f->n_vectors = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003775 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
Florin Corase127a7e2016-02-18 22:20:01 +01003776
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003777 if (duplicate_pmr)
3778 /* if there is a pending request already update it */
3779 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003780 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
3781 {
3782 /* remove the oldest nonce */
3783 u64 CLIB_UNUSED (tmp), *nonce_del;
3784 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
3785 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
3786 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
3787 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003788
Florin Corasf4691cd2016-08-15 19:16:32 +02003789 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003790 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02003791 duplicate_pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003792 }
3793 else
3794 {
3795 /* add map-request to pending requests table */
Florin Corasa2157cf2016-08-16 21:09:14 +02003796 pool_get (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003797 memset (pmr, 0, sizeof (*pmr));
3798 gid_address_copy (&pmr->src, seid);
3799 gid_address_copy (&pmr->dst, deid);
Florin Corasf4691cd2016-08-15 19:16:32 +02003800 clib_fifo_add1 (pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003801 pmr->is_smr_invoked = is_smr_invoked;
3802 reset_pending_mr_counters (pmr);
3803 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02003804 pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003805 }
3806
3807 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003808}
3809
3810static void
Florin Corasa2157cf2016-08-16 21:09:14 +02003811get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
Florin Corase127a7e2016-02-18 22:20:01 +01003812{
Florin Corasa2157cf2016-08-16 21:09:14 +02003813 ip4_header_t *ip4 = hdr;
3814 ip6_header_t *ip6;
Florin Corase127a7e2016-02-18 22:20:01 +01003815
3816 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3817 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003818 ip_address_set (src, &ip4->src_address, IP4);
3819 ip_address_set (dst, &ip4->dst_address, IP4);
Florin Corase127a7e2016-02-18 22:20:01 +01003820 }
3821 else
3822 {
3823 ip6 = hdr;
Florin Corasa2157cf2016-08-16 21:09:14 +02003824 ip_address_set (src, &ip6->src_address, IP6);
3825 ip_address_set (dst, &ip6->dst_address, IP6);
Florin Corase127a7e2016-02-18 22:20:01 +01003826 }
3827}
3828
Filip Tehlar324112f2016-06-02 16:07:38 +02003829static u32
Florin Coras1a1adc72016-07-22 01:45:30 +02003830lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
Florin Corasa2157cf2016-08-16 21:09:14 +02003831 u8 version)
Filip Tehlar324112f2016-06-02 16:07:38 +02003832{
Florin Corasa2157cf2016-08-16 21:09:14 +02003833 uword *vnip;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003834 u32 vni = ~0, table_id = ~0;
Filip Tehlar324112f2016-06-02 16:07:38 +02003835
Florin Coras87e40692016-09-22 18:02:17 +02003836 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
3837 IP4 ? FIB_PROTOCOL_IP4 :
3838 FIB_PROTOCOL_IP6),
3839 vnet_buffer
3840 (b)->sw_if_index
3841 [VLIB_RX]);
Filip Tehlar324112f2016-06-02 16:07:38 +02003842
3843 vnip = hash_get (lcm->vni_by_table_id, table_id);
3844 if (vnip)
3845 vni = vnip[0];
3846 else
3847 clib_warning ("vrf %d is not mapped to any vni!", table_id);
3848
3849 return vni;
3850}
3851
Florin Coras1a1adc72016-07-22 01:45:30 +02003852always_inline u32
3853lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3854{
Florin Corasa2157cf2016-08-16 21:09:14 +02003855 uword *vnip;
Florin Coras1a1adc72016-07-22 01:45:30 +02003856 u32 vni = ~0;
3857 u32 sw_if_index0;
3858
Florin Corasa2157cf2016-08-16 21:09:14 +02003859 l2input_main_t *l2im = &l2input_main;
3860 l2_input_config_t *config;
3861 l2_bridge_domain_t *bd_config;
Florin Coras1a1adc72016-07-22 01:45:30 +02003862
Florin Corasa2157cf2016-08-16 21:09:14 +02003863 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3864 config = vec_elt_at_index (l2im->configs, sw_if_index0);
Florin Coras1a1adc72016-07-22 01:45:30 +02003865 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3866
3867 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
3868 if (vnip)
3869 vni = vnip[0];
3870 else
Florin Corasa2157cf2016-08-16 21:09:14 +02003871 clib_warning ("bridge domain %d is not mapped to any vni!",
3872 config->bd_index);
Florin Coras1a1adc72016-07-22 01:45:30 +02003873
3874 return vni;
3875}
3876
3877always_inline void
Florin Corasa2157cf2016-08-16 21:09:14 +02003878get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3879 gid_address_t * src, gid_address_t * dst)
Florin Coras1a1adc72016-07-22 01:45:30 +02003880{
3881 u32 vni = 0;
3882 u16 type;
3883
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003884 memset (src, 0, sizeof (*src));
3885 memset (dst, 0, sizeof (*dst));
Florin Corasa2157cf2016-08-16 21:09:14 +02003886 type = vnet_buffer (b)->lisp.overlay_afi;
Florin Coras1a1adc72016-07-22 01:45:30 +02003887
3888 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3889 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003890 ip4_header_t *ip;
Florin Coras1a1adc72016-07-22 01:45:30 +02003891 u8 version, preflen;
3892
Florin Corasa2157cf2016-08-16 21:09:14 +02003893 gid_address_type (src) = GID_ADDR_IP_PREFIX;
3894 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
Florin Coras1a1adc72016-07-22 01:45:30 +02003895
3896 ip = vlib_buffer_get_current (b);
Florin Corasa2157cf2016-08-16 21:09:14 +02003897 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
Florin Coras1a1adc72016-07-22 01:45:30 +02003898
Florin Corasa2157cf2016-08-16 21:09:14 +02003899 version = gid_address_ip_version (src);
Florin Coras1a1adc72016-07-22 01:45:30 +02003900 preflen = ip_address_max_len (version);
Florin Corasa2157cf2016-08-16 21:09:14 +02003901 gid_address_ippref_len (src) = preflen;
3902 gid_address_ippref_len (dst) = preflen;
Florin Coras1a1adc72016-07-22 01:45:30 +02003903
3904 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3905 gid_address_vni (dst) = vni;
3906 gid_address_vni (src) = vni;
3907 }
3908 else if (LISP_AFI_MAC == type)
3909 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003910 ethernet_header_t *eh;
Florin Coras1a1adc72016-07-22 01:45:30 +02003911
3912 eh = vlib_buffer_get_current (b);
3913
Florin Corasa2157cf2016-08-16 21:09:14 +02003914 gid_address_type (src) = GID_ADDR_MAC;
3915 gid_address_type (dst) = GID_ADDR_MAC;
3916 mac_copy (&gid_address_mac (src), eh->src_address);
3917 mac_copy (&gid_address_mac (dst), eh->dst_address);
Florin Coras1a1adc72016-07-22 01:45:30 +02003918
3919 /* get vni */
3920 vni = lisp_get_vni_from_buffer_eth (lcm, b);
3921
3922 gid_address_vni (dst) = vni;
3923 gid_address_vni (src) = vni;
3924 }
3925}
3926
Florin Corase127a7e2016-02-18 22:20:01 +01003927static uword
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003928lisp_cp_lookup_inline (vlib_main_t * vm,
3929 vlib_node_runtime_t * node,
3930 vlib_frame_t * from_frame, int overlay)
Florin Corase127a7e2016-02-18 22:20:01 +01003931{
Florin Corasa2157cf2016-08-16 21:09:14 +02003932 u32 *from, *to_next_drop, di, si;
3933 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01003934 u32 pkts_mapped = 0;
3935 uword n_left_from, n_left_to_next_drop;
3936
3937 from = vlib_frame_vector_args (from_frame);
3938 n_left_from = from_frame->n_vectors;
3939
3940 while (n_left_from > 0)
3941 {
3942 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
Florin Corasa2157cf2016-08-16 21:09:14 +02003943 to_next_drop, n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01003944
3945 while (n_left_from > 0 && n_left_to_next_drop > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003946 {
3947 u32 pi0;
3948 vlib_buffer_t *b0;
3949 gid_address_t src, dst;
Florin Corase127a7e2016-02-18 22:20:01 +01003950
Florin Corasa2157cf2016-08-16 21:09:14 +02003951 pi0 = from[0];
3952 from += 1;
3953 n_left_from -= 1;
3954 to_next_drop[0] = pi0;
3955 to_next_drop += 1;
3956 n_left_to_next_drop -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01003957
Florin Corasa2157cf2016-08-16 21:09:14 +02003958 b0 = vlib_get_buffer (vm, pi0);
3959 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003960 vnet_buffer (b0)->lisp.overlay_afi = overlay;
Florin Corase127a7e2016-02-18 22:20:01 +01003961
Florin Corasa2157cf2016-08-16 21:09:14 +02003962 /* src/dst eid pair */
3963 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
Filip Tehlar324112f2016-06-02 16:07:38 +02003964
Florin Corasa2157cf2016-08-16 21:09:14 +02003965 /* if we have remote mapping for destination already in map-chache
3966 add forwarding tunnel directly. If not send a map-request */
Florin Corasdca88042016-09-14 16:01:38 +02003967 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3968 &src);
Florin Corasa2157cf2016-08-16 21:09:14 +02003969 if (~0 != di)
3970 {
3971 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3972 /* send a map-request also in case of negative mapping entry
3973 with corresponding action */
3974 if (m->action == LISP_SEND_MAP_REQUEST)
3975 {
3976 /* send map-request */
3977 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3978 0 /* is_resend */ );
3979 pkts_mapped++;
3980 }
3981 else
3982 {
3983 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3984 &src);
3985 if (~0 != si)
3986 {
Filip Tehlarce1aae42017-01-04 10:42:25 +01003987 dp_add_fwd_entry_from_mt (si, di);
Florin Corasa2157cf2016-08-16 21:09:14 +02003988 }
3989 }
3990 }
3991 else
3992 {
3993 /* send map-request */
3994 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3995 0 /* is_resend */ );
3996 pkts_mapped++;
3997 }
Florin Corase127a7e2016-02-18 22:20:01 +01003998
Florin Corasa2157cf2016-08-16 21:09:14 +02003999 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
4000 {
4001 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
4002 sizeof (*tr));
Andrej Kozemcak94e34762016-05-25 12:43:21 +02004003
Florin Corasa2157cf2016-08-16 21:09:14 +02004004 memset (tr, 0, sizeof (*tr));
4005 gid_address_copy (&tr->dst_eid, &dst);
Florin Coras5a1c11b2016-09-06 16:29:34 +02004006 ip_address_copy (&tr->map_resolver_ip,
4007 &lcm->active_map_resolver);
Florin Corasa2157cf2016-08-16 21:09:14 +02004008 }
4009 gid_address_free (&dst);
4010 gid_address_free (&src);
4011 }
Florin Corase127a7e2016-02-18 22:20:01 +01004012
Florin Corasa2157cf2016-08-16 21:09:14 +02004013 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
4014 n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01004015 }
4016 vlib_node_increment_counter (vm, node->node_index,
Florin Corasa2157cf2016-08-16 21:09:14 +02004017 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
4018 pkts_mapped);
Florin Corase127a7e2016-02-18 22:20:01 +01004019 return from_frame->n_vectors;
4020}
4021
Neale Ranns0bfe5d82016-08-25 15:29:12 +01004022static uword
4023lisp_cp_lookup_ip4 (vlib_main_t * vm,
4024 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4025{
4026 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
4027}
4028
4029static uword
4030lisp_cp_lookup_ip6 (vlib_main_t * vm,
4031 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4032{
4033 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
4034}
4035
Neale Ranns5e575b12016-10-03 09:40:25 +01004036static uword
4037lisp_cp_lookup_l2 (vlib_main_t * vm,
4038 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4039{
4040 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
4041}
4042
Florin Corasa2157cf2016-08-16 21:09:14 +02004043/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01004044VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
4045 .function = lisp_cp_lookup_ip4,
4046 .name = "lisp-cp-lookup-ip4",
4047 .vector_size = sizeof (u32),
4048 .format_trace = format_lisp_cp_lookup_trace,
4049 .type = VLIB_NODE_TYPE_INTERNAL,
4050
4051 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4052 .error_strings = lisp_cp_lookup_error_strings,
4053
4054 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4055
4056 .next_nodes = {
4057 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Neale Ranns0bfe5d82016-08-25 15:29:12 +01004058 },
4059};
4060/* *INDENT-ON* */
4061
4062/* *INDENT-OFF* */
4063VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
4064 .function = lisp_cp_lookup_ip6,
4065 .name = "lisp-cp-lookup-ip6",
Florin Corase127a7e2016-02-18 22:20:01 +01004066 .vector_size = sizeof (u32),
4067 .format_trace = format_lisp_cp_lookup_trace,
4068 .type = VLIB_NODE_TYPE_INTERNAL,
4069
4070 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4071 .error_strings = lisp_cp_lookup_error_strings,
4072
4073 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4074
4075 .next_nodes = {
4076 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Neale Ranns5e575b12016-10-03 09:40:25 +01004077 },
4078};
4079/* *INDENT-ON* */
4080
4081/* *INDENT-OFF* */
4082VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
4083 .function = lisp_cp_lookup_l2,
4084 .name = "lisp-cp-lookup-l2",
4085 .vector_size = sizeof (u32),
4086 .format_trace = format_lisp_cp_lookup_trace,
4087 .type = VLIB_NODE_TYPE_INTERNAL,
4088
4089 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4090 .error_strings = lisp_cp_lookup_error_strings,
4091
4092 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4093
4094 .next_nodes = {
4095 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Florin Corase127a7e2016-02-18 22:20:01 +01004096 },
4097};
Florin Corasa2157cf2016-08-16 21:09:14 +02004098/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01004099
4100/* lisp_cp_input statistics */
Florin Corasf727db92016-06-23 15:01:58 +02004101#define foreach_lisp_cp_input_error \
4102_(DROP, "drop") \
Florin Corase127a7e2016-02-18 22:20:01 +01004103_(MAP_REPLIES_RECEIVED, "map-replies received")
4104
Florin Corasa2157cf2016-08-16 21:09:14 +02004105static char *lisp_cp_input_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01004106#define _(sym,string) string,
4107 foreach_lisp_cp_input_error
4108#undef _
4109};
4110
4111typedef enum
4112{
4113#define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02004114 foreach_lisp_cp_input_error
Florin Corase127a7e2016-02-18 22:20:01 +01004115#undef _
4116 LISP_CP_INPUT_N_ERROR,
4117} lisp_cp_input_error_t;
4118
4119typedef enum
4120{
4121 LISP_CP_INPUT_NEXT_DROP,
4122 LISP_CP_INPUT_N_NEXT,
4123} lisp_cp_input_next_t;
4124
4125typedef struct
4126{
4127 gid_address_t dst_eid;
4128 ip4_address_t map_resolver_ip;
4129} lisp_cp_input_trace_t;
4130
4131u8 *
4132format_lisp_cp_input_trace (u8 * s, va_list * args)
4133{
4134 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
4135 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02004136 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
4137 va_arg (*args, lisp_cp_input_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01004138
4139 s = format (s, "LISP-CP-INPUT: TODO");
4140 return s;
4141}
4142
Filip Tehlar9677a942016-11-28 10:23:31 +01004143static void
4144remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
4145{
4146 mapping_t *m;
4147
4148 m = pool_elt_at_index (lcm->mapping_pool, mi);
4149 lisp_add_del_adjacency (lcm, 0, &m->eid, 0 /* is_add */ );
4150 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
4151 0 /* is_static */ , 0);
4152 mapping_delete_timer (lcm, mi);
4153}
4154
4155static void
4156mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
4157 f64 expiration_time)
4158{
4159 mapping_t *m;
4160 u64 now = clib_cpu_time_now ();
4161 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
4162 u64 exp_clock_time = now + expiration_time * cpu_cps;
4163
4164 m = pool_elt_at_index (lcm->mapping_pool, mi);
4165
4166 m->timer_set = 1;
4167 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
4168}
4169
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004170static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004171map_records_arg_free (map_records_arg_t * a)
Florin Corase127a7e2016-02-18 22:20:01 +01004172{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004173 mapping_t *m;
4174 vec_foreach (m, a->mappings)
4175 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004176 vec_free (m->locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004177 gid_address_free (&m->eid);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004178 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004179
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004180 clib_mem_free (a);
4181}
4182
4183void *
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004184process_map_reply (map_records_arg_t * a)
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004185{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004186 mapping_t *m;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004187 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4188 u32 dst_map_index = 0;
4189 pending_map_request_t *pmr;
4190 u64 *noncep;
4191 uword *pmr_index;
4192
4193 if (a->is_rloc_probe)
4194 goto done;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004195
Florin Corase127a7e2016-02-18 22:20:01 +01004196 /* Check pending requests table and nonce */
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004197 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
Florin Corase127a7e2016-02-18 22:20:01 +01004198 if (!pmr_index)
4199 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004200 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004201 goto done;
Florin Corase127a7e2016-02-18 22:20:01 +01004202 }
Florin Corasa2157cf2016-08-16 21:09:14 +02004203 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01004204
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004205 vec_foreach (m, a->mappings)
4206 {
4207 /* insert/update mappings cache */
4208 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
4209 m->authoritative, m->ttl,
4210 1, 0 /* is_static */ , &dst_map_index);
Florin Corase127a7e2016-02-18 22:20:01 +01004211
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004212 /* try to program forwarding only if mapping saved or updated */
4213 if ((u32) ~ 0 != dst_map_index)
4214 {
4215 lisp_add_del_adjacency (lcm, &pmr->src, &m->eid, 1);
4216 if ((u32) ~ 0 != m->ttl)
4217 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
4218 }
4219 }
Florin Corase127a7e2016-02-18 22:20:01 +01004220
4221 /* remove pending map request entry */
Florin Corasa2157cf2016-08-16 21:09:14 +02004222
4223 /* *INDENT-OFF* */
Florin Corasf4691cd2016-08-15 19:16:32 +02004224 clib_fifo_foreach (noncep, pmr->nonces, ({
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004225 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
Florin Corasf4691cd2016-08-15 19:16:32 +02004226 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02004227 /* *INDENT-ON* */
4228
4229 clib_fifo_free (pmr->nonces);
4230 pool_put (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004231
4232done:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004233 map_records_arg_free (a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004234 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01004235}
4236
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004237static int
4238is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
4239 lisp_key_type_t key_id, u8 * key)
4240{
4241 u8 *auth_data = 0;
4242 u16 auth_data_len;
4243 int result;
4244
4245 auth_data_len = auth_data_len_by_key_id (key_id);
4246 if ((u16) ~ 0 == auth_data_len)
4247 {
4248 clib_warning ("invalid length for key_id %d!", key_id);
4249 return 0;
4250 }
4251
4252 /* save auth data */
4253 vec_validate (auth_data, auth_data_len - 1);
4254 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
4255
4256 /* clear auth data */
4257 memset (MNOTIFY_DATA (h), 0, auth_data_len);
4258
4259 /* get hash of the message */
4260 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
4261 (unsigned char *) h, msg_len, NULL, NULL);
4262
4263 result = memcmp (code, auth_data, auth_data_len);
4264
4265 vec_free (auth_data);
4266
4267 return !result;
4268}
4269
4270static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004271process_map_notify (map_records_arg_t * a)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004272{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004273 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004274 uword *pmr_index;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004275
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004276 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004277 if (!pmr_index)
4278 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004279 clib_warning ("No pending map-register entry with nonce %lu!",
4280 a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004281 return;
4282 }
4283
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004284 map_records_arg_free (a);
4285 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
4286}
4287
4288static mapping_t *
4289get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
4290{
4291 u32 mi;
4292
4293 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
4294 if (~0 == mi)
4295 {
4296 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
4297 e);
4298 return 0;
4299 }
4300 return pool_elt_at_index (lcm->mapping_pool, mi);
4301}
4302
4303/**
4304 * When map-notify is received it is necessary that all EIDs in the record
4305 * list share common key. The key is then used to verify authentication
4306 * data in map-notify message.
4307 */
4308static int
4309map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
4310 u32 key_id, u8 ** key_out)
4311{
4312 u32 i, len = vec_len (maps);
4313 mapping_t *m;
4314
4315 /* get key of the first mapping */
4316 m = get_mapping (lcm, &maps[0].eid);
4317 if (!m || !m->key)
4318 return -1;
4319
4320 key_out[0] = m->key;
4321
4322 for (i = 1; i < len; i++)
4323 {
4324 m = get_mapping (lcm, &maps[i].eid);
4325 if (!m || !m->key)
4326 return -1;
4327
4328 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
4329 {
4330 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
4331 return -1;
4332 }
4333 }
4334 return 0;
4335}
4336
4337static int
4338parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
4339{
4340 locator_t *locators = 0;
4341 u32 i, len;
4342 gid_address_t deid;
4343 mapping_t m;
4344 locator_t *loc;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004345
4346 /* parse record eid */
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004347 for (i = 0; i < count; i++)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004348 {
4349 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
4350 if (len == ~0)
4351 {
4352 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004353 vec_foreach (loc, locators) locator_free (loc);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004354 vec_free (locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004355 return -1;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004356 }
4357
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004358 m.locators = locators;
4359 gid_address_copy (&m.eid, &deid);
4360 vec_add1 (a->mappings, m);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004361 }
4362
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004363 return 0;
4364}
4365
4366static map_records_arg_t *
4367parse_map_notify (vlib_buffer_t * b)
4368{
4369 int rc = 0;
4370 map_notify_hdr_t *mnotif_hdr;
4371 lisp_key_type_t key_id;
4372 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4373 u8 *key = 0;
4374 gid_address_t deid;
4375 u16 auth_data_len = 0;
4376 u8 record_count;
4377 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
4378
4379 memset (a, 0, sizeof (*a));
4380 mnotif_hdr = vlib_buffer_get_current (b);
4381 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
4382 memset (&deid, 0, sizeof (deid));
4383
4384 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
4385 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
4386 auth_data_len = auth_data_len_by_key_id (key_id);
4387
4388 /* advance buffer by authentication data */
4389 vlib_buffer_pull (b, auth_data_len);
4390
4391 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
4392 rc = parse_map_records (b, a, record_count);
4393 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004394 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004395 map_records_arg_free (a);
4396 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004397 }
4398
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004399 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
4400 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004401 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004402 map_records_arg_free (a);
4403 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004404 }
4405
4406 /* verify authentication data */
4407 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004408 - (u8 *) mnotif_hdr, key_id, key))
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004409 {
4410 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004411 a->nonce);
4412 map_records_arg_free (a);
4413 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004414 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004415 return a;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004416}
4417
4418static vlib_buffer_t *
4419build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
4420 ip_address_t * dst, u64 nonce, u8 probe_bit,
4421 mapping_t * records, u16 dst_port, u32 * bi_res)
4422{
4423 vlib_buffer_t *b;
4424 u32 bi;
4425 vlib_main_t *vm = lcm->vlib_main;
4426
4427 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
4428 {
4429 clib_warning ("Can't allocate buffer for Map-Register!");
4430 return 0;
4431 }
4432
4433 b = vlib_get_buffer (vm, bi);
4434
4435 /* leave some space for the encap headers */
4436 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
4437
4438 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
4439
4440 /* push outer ip header */
4441 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
4442
4443 bi_res[0] = bi;
4444 return b;
4445}
4446
4447static int
4448send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
4449 u8 probe_bit, u64 nonce, u16 dst_port,
4450 ip_address_t * probed_loc)
4451{
4452 ip_address_t src;
4453 u32 bi;
4454 vlib_buffer_t *b;
4455 vlib_frame_t *f;
4456 u32 next_index, *to_next;
4457 mapping_t *records = 0, *m;
4458
4459 m = pool_elt_at_index (lcm->mapping_pool, mi);
4460 if (!m)
4461 return -1;
4462
4463 vec_add1 (records, m[0]);
4464 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
4465 memset (&src, 0, sizeof (src));
4466
4467 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
4468 {
4469 clib_warning ("can't find inteface address for %U", format_ip_address,
4470 dst);
4471 return -1;
4472 }
4473
4474 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
4475 &bi);
4476 if (!b)
4477 return -1;
4478 free_map_register_records (records);
4479
4480 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
4481 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
4482 ip4_lookup_node.index : ip6_lookup_node.index;
4483
4484 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
4485
4486 /* Enqueue the packet */
4487 to_next = vlib_frame_vector_args (f);
4488 to_next[0] = bi;
4489 f->n_vectors = 1;
4490 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
4491 return 0;
4492}
4493
Filip Tehlarb601f222017-01-02 10:22:56 +01004494static void
4495find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
4496{
4497 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
4498 if (start < 0 && start < -sizeof (b->pre_data))
4499 {
4500 *ip_hdr = 0;
4501 return;
4502 }
4503
4504 *ip_hdr = b->data + start;
4505 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
4506 *ip_hdr = 0;
4507}
4508
Florin Corase127a7e2016-02-18 22:20:01 +01004509void
Florin Corasa2157cf2016-08-16 21:09:14 +02004510process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm,
4511 vlib_buffer_t * b)
Florin Corase127a7e2016-02-18 22:20:01 +01004512{
Filip Tehlarb601f222017-01-02 10:22:56 +01004513 u8 *ip_hdr = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004514 ip_address_t *dst_loc = 0, probed_loc, src_loc;
4515 mapping_t m;
Florin Corasa2157cf2016-08-16 21:09:14 +02004516 map_request_hdr_t *mreq_hdr;
Florin Corase127a7e2016-02-18 22:20:01 +01004517 gid_address_t src, dst;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004518 u64 nonce;
Florin Corase127a7e2016-02-18 22:20:01 +01004519 u32 i, len = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004520 gid_address_t *itr_rlocs = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01004521
4522 mreq_hdr = vlib_buffer_get_current (b);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004523 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
Florin Corasa2157cf2016-08-16 21:09:14 +02004524 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004525 clib_warning
4526 ("Only SMR Map-Requests and RLOC probe supported for now!");
Florin Corase127a7e2016-02-18 22:20:01 +01004527 return;
Florin Corasa2157cf2016-08-16 21:09:14 +02004528 }
Florin Corase127a7e2016-02-18 22:20:01 +01004529
Filip Tehlarb601f222017-01-02 10:22:56 +01004530 vlib_buffer_pull (b, sizeof (*mreq_hdr));
4531 nonce = MREQ_NONCE (mreq_hdr);
4532
Florin Corase127a7e2016-02-18 22:20:01 +01004533 /* parse src eid */
4534 len = lisp_msg_parse_addr (b, &src);
4535 if (len == ~0)
4536 return;
4537
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004538 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
4539 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
Florin Corase127a7e2016-02-18 22:20:01 +01004540 if (len == ~0)
4541 return;
4542
4543 /* parse eid records and send SMR-invoked map-requests */
Florin Corasa2157cf2016-08-16 21:09:14 +02004544 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01004545 {
Florin Corasa2157cf2016-08-16 21:09:14 +02004546 memset (&dst, 0, sizeof (dst));
Florin Corase127a7e2016-02-18 22:20:01 +01004547 len = lisp_msg_parse_eid_rec (b, &dst);
4548 if (len == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02004549 {
4550 clib_warning ("Can't parse map-request EID-record");
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004551 goto done;
Florin Corasa2157cf2016-08-16 21:09:14 +02004552 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004553
4554 if (MREQ_SMR (mreq_hdr))
4555 {
4556 /* send SMR-invoked map-requests */
4557 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
4558 }
4559 else if (MREQ_RLOC_PROBE (mreq_hdr))
4560 {
Filip Tehlarb601f222017-01-02 10:22:56 +01004561 find_ip_header (b, &ip_hdr);
4562 if (!ip_hdr)
4563 {
4564 clib_warning ("Cannot find the IP header!");
4565 return;
4566 }
4567
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004568 memset (&m, 0, sizeof (m));
4569 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
4570
4571 // TODO: select best locator; for now use the first one
4572 dst_loc = &gid_address_ip (&itr_rlocs[0]);
4573
4574 /* get src/dst IP addresses */
4575 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
4576
4577 // TODO get source port from buffer
4578 u16 src_port = LISP_CONTROL_PORT;
4579
4580 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
4581 src_port, &probed_loc);
4582 }
Florin Corase127a7e2016-02-18 22:20:01 +01004583 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004584
4585done:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004586 vec_free (itr_rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01004587}
4588
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004589static map_records_arg_t *
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004590parse_map_reply (vlib_buffer_t * b)
4591{
4592 locator_t probed;
4593 gid_address_t deid;
4594 void *h;
4595 u32 i, len = 0;
4596 mapping_t m;
4597 map_reply_hdr_t *mrep_hdr;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004598 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004599 memset (a, 0, sizeof (*a));
4600 locator_t *locators;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004601
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004602 mrep_hdr = vlib_buffer_get_current (b);
4603 a->nonce = MREP_NONCE (mrep_hdr);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004604 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004605 vlib_buffer_pull (b, sizeof (*mrep_hdr));
4606
4607 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
4608 {
4609 memset (&m, 0, sizeof (m));
4610 locators = 0;
4611 h = vlib_buffer_get_current (b);
4612
4613 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
4614 m.action = MAP_REC_ACTION (h);
4615 m.authoritative = MAP_REC_AUTH (h);
4616
4617 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
4618 if (len == ~0)
4619 {
4620 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004621 map_records_arg_free (a);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004622 return 0;
4623 }
4624
4625 m.locators = locators;
4626 gid_address_copy (&m.eid, &deid);
4627 vec_add1 (a->mappings, m);
4628 }
4629 return a;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004630}
4631
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004632static void
4633queue_map_reply_for_processing (map_records_arg_t * a)
4634{
Florin Coras655fcc42017-01-10 08:57:54 -08004635 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004636}
4637
4638static void
4639queue_map_notify_for_processing (map_records_arg_t * a)
4640{
4641 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
4642}
4643
Florin Corase127a7e2016-02-18 22:20:01 +01004644static uword
4645lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Corasa2157cf2016-08-16 21:09:14 +02004646 vlib_frame_t * from_frame)
Florin Corase127a7e2016-02-18 22:20:01 +01004647{
Florin Corasa2157cf2016-08-16 21:09:14 +02004648 u32 n_left_from, *from, *to_next_drop;
Florin Corase127a7e2016-02-18 22:20:01 +01004649 lisp_msg_type_e type;
Florin Corasa2157cf2016-08-16 21:09:14 +02004650 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004651 map_records_arg_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01004652
4653 from = vlib_frame_vector_args (from_frame);
4654 n_left_from = from_frame->n_vectors;
4655
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004656
Florin Corase127a7e2016-02-18 22:20:01 +01004657 while (n_left_from > 0)
4658 {
4659 u32 n_left_to_next_drop;
4660
4661 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
Florin Corasa2157cf2016-08-16 21:09:14 +02004662 to_next_drop, n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01004663 while (n_left_from > 0 && n_left_to_next_drop > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02004664 {
4665 u32 bi0;
4666 vlib_buffer_t *b0;
Florin Corase127a7e2016-02-18 22:20:01 +01004667
Florin Corasa2157cf2016-08-16 21:09:14 +02004668 bi0 = from[0];
4669 from += 1;
4670 n_left_from -= 1;
4671 to_next_drop[0] = bi0;
4672 to_next_drop += 1;
4673 n_left_to_next_drop -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01004674
Florin Corasa2157cf2016-08-16 21:09:14 +02004675 b0 = vlib_get_buffer (vm, bi0);
Florin Corase127a7e2016-02-18 22:20:01 +01004676
Florin Corasa2157cf2016-08-16 21:09:14 +02004677 type = lisp_msg_type (vlib_buffer_get_current (b0));
4678 switch (type)
4679 {
4680 case LISP_MAP_REPLY:
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004681 a = parse_map_reply (b0);
4682 if (a)
4683 queue_map_reply_for_processing (a);
Florin Corasa2157cf2016-08-16 21:09:14 +02004684 break;
4685 case LISP_MAP_REQUEST:
4686 process_map_request (vm, lcm, b0);
4687 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004688 case LISP_MAP_NOTIFY:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004689 a = parse_map_notify (b0);
4690 if (a)
4691 queue_map_notify_for_processing (a);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004692 break;
Florin Corasa2157cf2016-08-16 21:09:14 +02004693 default:
4694 clib_warning ("Unsupported LISP message type %d", type);
4695 break;
4696 }
Florin Corase127a7e2016-02-18 22:20:01 +01004697
Florin Corasa2157cf2016-08-16 21:09:14 +02004698 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
Florin Corase127a7e2016-02-18 22:20:01 +01004699
Florin Corasa2157cf2016-08-16 21:09:14 +02004700 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
4701 {
Florin Corase127a7e2016-02-18 22:20:01 +01004702
Florin Corasa2157cf2016-08-16 21:09:14 +02004703 }
4704 }
Florin Corase127a7e2016-02-18 22:20:01 +01004705
Florin Corasa2157cf2016-08-16 21:09:14 +02004706 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4707 n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01004708 }
4709 return from_frame->n_vectors;
4710}
4711
Florin Corasa2157cf2016-08-16 21:09:14 +02004712/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01004713VLIB_REGISTER_NODE (lisp_cp_input_node) = {
4714 .function = lisp_cp_input,
4715 .name = "lisp-cp-input",
4716 .vector_size = sizeof (u32),
4717 .format_trace = format_lisp_cp_input_trace,
4718 .type = VLIB_NODE_TYPE_INTERNAL,
4719
4720 .n_errors = LISP_CP_INPUT_N_ERROR,
4721 .error_strings = lisp_cp_input_error_strings,
4722
4723 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
4724
4725 .next_nodes = {
4726 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
4727 },
4728};
Florin Corasa2157cf2016-08-16 21:09:14 +02004729/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01004730
4731clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02004732lisp_cp_init (vlib_main_t * vm)
Florin Corase127a7e2016-02-18 22:20:01 +01004733{
Florin Corasa2157cf2016-08-16 21:09:14 +02004734 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4735 clib_error_t *error = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01004736
4737 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
4738 return error;
4739
4740 lcm->im4 = &ip4_main;
4741 lcm->im6 = &ip6_main;
4742 lcm->vlib_main = vm;
Florin Corasa2157cf2016-08-16 21:09:14 +02004743 lcm->vnet_main = vnet_get_main ();
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02004744 lcm->mreq_itr_rlocs = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02004745 lcm->lisp_pitr = 0;
Florin Coras5a1c11b2016-09-06 16:29:34 +02004746 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
Florin Corase127a7e2016-02-18 22:20:01 +01004747
4748 gid_dictionary_init (&lcm->mapping_index_by_gid);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004749 lcm->do_map_resolver_election = 1;
Florin Corasdca88042016-09-14 16:01:38 +02004750 lcm->map_request_mode = MR_MODE_DST_ONLY;
Florin Corase127a7e2016-02-18 22:20:01 +01004751
Florin Coras577c3552016-04-21 00:45:40 +02004752 /* default vrf mapped to vni 0 */
Florin Corasa2157cf2016-08-16 21:09:14 +02004753 hash_set (lcm->table_id_by_vni, 0, 0);
4754 hash_set (lcm->vni_by_table_id, 0, 0);
Florin Coras577c3552016-04-21 00:45:40 +02004755
Florin Corase127a7e2016-02-18 22:20:01 +01004756 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
Florin Corasa2157cf2016-08-16 21:09:14 +02004757 lisp_cp_input_node.index, 1 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01004758 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
Florin Corasa2157cf2016-08-16 21:09:14 +02004759 lisp_cp_input_node.index, 0 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01004760
Filip Tehlar9677a942016-11-28 10:23:31 +01004761 u64 now = clib_cpu_time_now ();
4762 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
Florin Corase127a7e2016-02-18 22:20:01 +01004763 return 0;
4764}
4765
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004766static void *
Florin Corasa2157cf2016-08-16 21:09:14 +02004767send_map_request_thread_fn (void *arg)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004768{
Florin Corasa2157cf2016-08-16 21:09:14 +02004769 map_request_args_t *a = arg;
4770 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004771
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004772 if (a->is_resend)
4773 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4774 else
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004775 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004776
4777 return 0;
4778}
4779
4780static int
4781queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +02004782 u8 smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004783{
4784 map_request_args_t a;
4785
4786 a.is_resend = is_resend;
4787 gid_address_copy (&a.seid, seid);
4788 gid_address_copy (&a.deid, deid);
4789 a.smr_invoked = smr_invoked;
4790
4791 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
Florin Corasa2157cf2016-08-16 21:09:14 +02004792 (u8 *) & a, sizeof (a));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004793 return 0;
4794}
4795
4796/**
4797 * Take an action with a pending map request depending on expiration time
4798 * and re-try counters.
4799 */
4800static void
4801update_pending_request (pending_map_request_t * r, f64 dt)
4802{
Florin Corasa2157cf2016-08-16 21:09:14 +02004803 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004804 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004805
4806 if (r->time_to_expire - dt < 0)
4807 /* it's time to decide what to do with this pending request */
4808 {
4809 if (r->retries_num >= NUMBER_OF_RETRIES)
Florin Corasa2157cf2016-08-16 21:09:14 +02004810 /* too many retries -> assume current map resolver is not available */
4811 {
4812 mr = get_map_resolver (&lcm->active_map_resolver);
4813 if (!mr)
4814 {
4815 clib_warning ("Map resolver %U not found - probably deleted "
4816 "by the user recently.", format_ip_address,
4817 &lcm->active_map_resolver);
4818 }
4819 else
4820 {
4821 clib_warning ("map resolver %U is unreachable, ignoring",
4822 format_ip_address, &lcm->active_map_resolver);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004823
Florin Corasa2157cf2016-08-16 21:09:14 +02004824 /* mark current map resolver unavailable so it won't be
4825 * selected next time */
4826 mr->is_down = 1;
4827 mr->last_update = vlib_time_now (lcm->vlib_main);
4828 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004829
Florin Corasa2157cf2016-08-16 21:09:14 +02004830 reset_pending_mr_counters (r);
4831 elect_map_resolver (lcm);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004832
Florin Corasa2157cf2016-08-16 21:09:14 +02004833 /* try to find a next eligible map resolver and re-send */
4834 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4835 1 /* resend */ );
4836 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004837 else
Florin Corasa2157cf2016-08-16 21:09:14 +02004838 {
4839 /* try again */
4840 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4841 1 /* resend */ );
4842 r->retries_num++;
4843 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
4844 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004845 }
4846 else
4847 r->time_to_expire -= dt;
4848}
4849
4850static void
4851remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4852{
Florin Corasa2157cf2016-08-16 21:09:14 +02004853 u64 *nonce;
4854 pending_map_request_t *pmr;
4855 u32 *to_be_removed = 0, *pmr_index;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004856
Florin Corasa2157cf2016-08-16 21:09:14 +02004857 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004858 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02004859 ({
4860 if (pmr->to_be_removed)
4861 {
4862 clib_fifo_foreach (nonce, pmr->nonces, ({
4863 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4864 }));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004865
Florin Corasa2157cf2016-08-16 21:09:14 +02004866 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4867 }
4868 }));
4869 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004870
4871 vec_foreach (pmr_index, to_be_removed)
4872 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
4873
4874 vec_free (to_be_removed);
4875}
4876
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004877static void
4878update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4879{
4880 static f64 time_left = RLOC_PROBING_INTERVAL;
4881
4882 if (!lcm->is_enabled || !lcm->rloc_probing)
4883 return;
4884
4885 time_left -= dt;
4886 if (time_left <= 0)
4887 {
4888 time_left = RLOC_PROBING_INTERVAL;
4889 send_rloc_probes (lcm);
4890 }
4891}
4892
4893static void
4894update_map_register (lisp_cp_main_t * lcm, f64 dt)
4895{
4896 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4897 static u64 mreg_sent_counter = 0;
4898
4899 if (!lcm->is_enabled || !lcm->map_registering)
4900 return;
4901
4902 time_left -= dt;
4903 if (time_left <= 0)
4904 {
4905 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4906 time_left = MAP_REGISTER_INTERVAL;
4907 else
4908 {
4909 mreg_sent_counter++;
4910 time_left = QUICK_MAP_REGISTER_INTERVAL;
4911 }
4912 send_map_register (lcm, 1 /* want map notify */ );
4913 }
4914}
4915
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004916static uword
4917send_map_resolver_service (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02004918 vlib_node_runtime_t * rt, vlib_frame_t * f)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004919{
Filip Tehlar9677a942016-11-28 10:23:31 +01004920 u32 *expired = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004921 f64 period = 2.0;
Florin Corasa2157cf2016-08-16 21:09:14 +02004922 pending_map_request_t *pmr;
4923 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004924
4925 while (1)
4926 {
4927 vlib_process_wait_for_event_or_clock (vm, period);
4928
4929 /* currently no signals are expected - just wait for clock */
4930 (void) vlib_process_get_events (vm, 0);
4931
Florin Corasa2157cf2016-08-16 21:09:14 +02004932 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004933 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02004934 ({
4935 if (!pmr->to_be_removed)
4936 update_pending_request (pmr, period);
4937 }));
4938 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004939
4940 remove_dead_pending_map_requests (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004941
4942 update_map_register (lcm, period);
4943 update_rloc_probing (lcm, period);
Filip Tehlar9677a942016-11-28 10:23:31 +01004944
4945 u64 now = clib_cpu_time_now ();
4946
4947 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
4948 if (vec_len (expired) > 0)
4949 {
4950 u32 *mi = 0;
4951 vec_foreach (mi, expired)
4952 {
4953 remove_expired_mapping (lcm, mi[0]);
4954 }
4955 _vec_len (expired) = 0;
4956 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004957 }
4958
4959 /* unreachable */
4960 return 0;
4961}
4962
Florin Corasa2157cf2016-08-16 21:09:14 +02004963/* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004964VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
4965 .function = send_map_resolver_service,
4966 .type = VLIB_NODE_TYPE_PROCESS,
4967 .name = "lisp-retry-service",
4968 .process_log2_n_stack_bytes = 16,
4969};
Florin Corasa2157cf2016-08-16 21:09:14 +02004970/* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004971
Florin Corasa2157cf2016-08-16 21:09:14 +02004972VLIB_INIT_FUNCTION (lisp_cp_init);
4973
4974/*
4975 * fd.io coding-style-patch-verification: ON
4976 *
4977 * Local Variables:
4978 * eval: (c-set-style "gnu")
4979 * End:
4980 */