blob: f74166acfe2a919984232a9b1440e4bdc6bea066 [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 Tehlarb05116e2016-12-15 14:04:02 +0100515 gid_address_copy (&fe->leid, &a->lcl_eid);
Filip Tehlar69a9b762016-09-23 10:00:52 +0200516 fe->is_src_dst = is_src_dst;
Florin Corasf727db92016-06-23 15:01:58 +0200517 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200518 fe - lcm->fwd_entry_pool);
Florin Corasf727db92016-06-23 15:01:58 +0200519}
520
Filip Tehlarce1aae42017-01-04 10:42:25 +0100521typedef struct
522{
523 u32 si;
524 u32 di;
525} fwd_entry_mt_arg_t;
526
527static void *
528dp_add_fwd_entry_thread_fn (void *arg)
529{
530 fwd_entry_mt_arg_t *a = arg;
531 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
532 dp_add_fwd_entry (lcm, a->si, a->di);
533 return 0;
534}
535
536static int
537dp_add_fwd_entry_from_mt (u32 si, u32 di)
538{
539 fwd_entry_mt_arg_t a;
540
541 memset (&a, 0, sizeof (a));
542 a.si = si;
543 a.di = di;
544
545 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
546 (u8 *) & a, sizeof (a));
547 return 0;
548}
549
Florin Corasf727db92016-06-23 15:01:58 +0200550/**
Filip Tehlar69a9b762016-09-23 10:00:52 +0200551 * Returns vector of adjacencies.
552 *
553 * The caller must free the vector returned by this function.
554 *
555 * @param vni virtual network identifier
556 * @return vector of adjacencies
557 */
558lisp_adjacency_t *
559vnet_lisp_adjacencies_get_by_vni (u32 vni)
560{
561 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
562 fwd_entry_t *fwd;
563 lisp_adjacency_t *adjs = 0, adj;
564
565 /* *INDENT-OFF* */
566 pool_foreach(fwd, lcm->fwd_entry_pool,
567 ({
568 if (gid_address_vni (&fwd->reid) != vni)
569 continue;
570
571 gid_address_copy (&adj.reid, &fwd->reid);
572 gid_address_copy (&adj.leid, &fwd->leid);
573 vec_add1 (adjs, adj);
574 }));
575 /* *INDENT-ON* */
576
577 return adjs;
578}
579
580static clib_error_t *
581lisp_show_adjacencies_command_fn (vlib_main_t * vm,
582 unformat_input_t * input,
583 vlib_cli_command_t * cmd)
584{
585 lisp_adjacency_t *adjs, *adj;
586 vlib_cli_output (vm, "%s %40s\n", "leid", "reid");
587 unformat_input_t _line_input, *line_input = &_line_input;
588 u32 vni = ~0;
589
590 /* Get a line of input. */
591 if (!unformat_user (input, unformat_line_input, line_input))
592 return 0;
593
594 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
595 {
596 if (unformat (line_input, "vni %d", &vni))
597 ;
598 else
599 {
600 vlib_cli_output (vm, "parse error: '%U'",
601 format_unformat_error, line_input);
602 return 0;
603 }
604 }
605
606 if (~0 == vni)
607 {
608 vlib_cli_output (vm, "error: no vni specified!");
609 return 0;
610 }
611
612 adjs = vnet_lisp_adjacencies_get_by_vni (vni);
613
614 vec_foreach (adj, adjs)
615 {
616 vlib_cli_output (vm, "%U %40U\n", format_gid_address, &adj->leid,
617 format_gid_address, &adj->reid);
618 }
619 vec_free (adjs);
620
621 return 0;
622}
623
624/* *INDENT-OFF* */
625VLIB_CLI_COMMAND (lisp_show_adjacencies_command) = {
626 .path = "show lisp adjacencies",
627 .short_help = "show lisp adjacencies",
628 .function = lisp_show_adjacencies_command_fn,
629};
630/* *INDENT-ON* */
631
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200632static lisp_msmr_t *
633get_map_server (ip_address_t * a)
634{
635 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
636 lisp_msmr_t *m;
637
638 vec_foreach (m, lcm->map_servers)
639 {
640 if (!ip_address_cmp (&m->address, a))
641 {
642 return m;
643 }
644 }
645 return 0;
646}
647
648static lisp_msmr_t *
649get_map_resolver (ip_address_t * a)
650{
651 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
652 lisp_msmr_t *m;
653
654 vec_foreach (m, lcm->map_resolvers)
655 {
656 if (!ip_address_cmp (&m->address, a))
657 {
658 return m;
659 }
660 }
661 return 0;
662}
663
664int
665vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
666{
667 u32 i;
668 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
669 lisp_msmr_t _ms, *ms = &_ms;
670
671 if (vnet_lisp_enable_disable_status () == 0)
672 {
673 clib_warning ("LISP is disabled!");
674 return VNET_API_ERROR_LISP_DISABLED;
675 }
676
677 if (is_add)
678 {
679 if (get_map_server (addr))
680 {
681 clib_warning ("map-server %U already exists!", format_ip_address,
682 addr);
683 return -1;
684 }
685
686 memset (ms, 0, sizeof (*ms));
687 ip_address_copy (&ms->address, addr);
688 vec_add1 (lcm->map_servers, ms[0]);
689 }
690 else
691 {
692 for (i = 0; i < vec_len (lcm->map_servers); i++)
693 {
694 ms = vec_elt_at_index (lcm->map_servers, i);
695 if (!ip_address_cmp (&ms->address, addr))
696 {
697 vec_del1 (lcm->map_servers, i);
698 break;
699 }
700 }
701 }
702
703 return 0;
704}
705
706static clib_error_t *
707lisp_add_del_map_server_command_fn (vlib_main_t * vm,
708 unformat_input_t * input,
709 vlib_cli_command_t * cmd)
710{
711 int rv = 0;
712 u8 is_add = 1, ip_set = 0;
713 ip_address_t ip;
714 unformat_input_t _line_input, *line_input = &_line_input;
715
716 /* Get a line of input. */
717 if (!unformat_user (input, unformat_line_input, line_input))
718 return 0;
719
720 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
721 {
722 if (unformat (line_input, "add"))
723 is_add = 1;
724 else if (unformat (line_input, "del"))
725 is_add = 0;
726 else if (unformat (line_input, "%U", unformat_ip_address, &ip))
727 ip_set = 1;
728 else
729 {
730 vlib_cli_output (vm, "parse error: '%U'",
731 format_unformat_error, line_input);
732 return 0;
733 }
734 }
735
736 if (!ip_set)
737 {
738 vlib_cli_output (vm, "map-server ip address not set!");
739 return 0;
740 }
741
742 rv = vnet_lisp_add_del_map_server (&ip, is_add);
743 if (!rv)
744 vlib_cli_output (vm, "failed to %s map-server!",
745 is_add ? "add" : "delete");
746
747 return 0;
748}
749
750/* *INDENT-OFF* */
751VLIB_CLI_COMMAND (lisp_add_del_map_server_command) = {
752 .path = "lisp map-server",
753 .short_help = "lisp map-server add|del <ip>",
754 .function = lisp_add_del_map_server_command_fn,
755};
756/* *INDENT-ON* */
757
Filip Tehlar69a9b762016-09-23 10:00:52 +0200758/**
Florin Corasf727db92016-06-23 15:01:58 +0200759 * Add/remove mapping to/from map-cache. Overwriting not allowed.
760 */
761int
762vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200763 u32 * map_index_result)
Florin Corase127a7e2016-02-18 22:20:01 +0100764{
Florin Corasa2157cf2016-08-16 21:09:14 +0200765 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
766 u32 mi, *map_indexp, map_index, i;
767 mapping_t *m, *old_map;
768 u32 **eid_indexes;
Florin Corase127a7e2016-02-18 22:20:01 +0100769
Florin Corasf727db92016-06-23 15:01:58 +0200770 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
Filip Tehlar53f09e32016-05-19 14:25:44 +0200771 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100772 if (a->is_add)
773 {
774 /* TODO check if overwriting and take appropriate actions */
Florin Corasa2157cf2016-08-16 21:09:14 +0200775 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
776 {
777 clib_warning ("eid %U found in the eid-table", format_gid_address,
778 &a->eid);
779 return VNET_API_ERROR_VALUE_EXIST;
780 }
Florin Corase127a7e2016-02-18 22:20:01 +0100781
Florin Corasa2157cf2016-08-16 21:09:14 +0200782 pool_get (lcm->mapping_pool, m);
Florin Corasf727db92016-06-23 15:01:58 +0200783 gid_address_copy (&m->eid, &a->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100784 m->locator_set_index = a->locator_set_index;
785 m->ttl = a->ttl;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200786 m->action = a->action;
Florin Corase127a7e2016-02-18 22:20:01 +0100787 m->local = a->local;
Filip Tehlar3cd9e732016-08-23 10:52:44 +0200788 m->is_static = a->is_static;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200789 m->key = vec_dup (a->key);
790 m->key_id = a->key_id;
Florin Corase127a7e2016-02-18 22:20:01 +0100791
792 map_index = m - lcm->mapping_pool;
Florin Corasf727db92016-06-23 15:01:58 +0200793 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200794 1);
Florin Corase127a7e2016-02-18 22:20:01 +0100795
Florin Corasa2157cf2016-08-16 21:09:14 +0200796 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
797 {
798 clib_warning ("Locator set with index %d doesn't exist",
799 a->locator_set_index);
800 return VNET_API_ERROR_INVALID_VALUE;
801 }
Florin Corase127a7e2016-02-18 22:20:01 +0100802
803 /* add eid to list of eids supported by locator-set */
804 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
Florin Corasa2157cf2016-08-16 21:09:14 +0200805 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
806 a->locator_set_index);
807 vec_add1 (eid_indexes[0], map_index);
Florin Corase127a7e2016-02-18 22:20:01 +0100808
809 if (a->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200810 {
811 /* mark as local */
812 vec_add1 (lcm->local_mappings_indexes, map_index);
813 }
Florin Corase127a7e2016-02-18 22:20:01 +0100814 map_index_result[0] = map_index;
815 }
816 else
817 {
Florin Coras577c3552016-04-21 00:45:40 +0200818 if (mi == GID_LOOKUP_MISS)
Florin Corasa2157cf2016-08-16 21:09:14 +0200819 {
820 clib_warning ("eid %U not found in the eid-table",
821 format_gid_address, &a->eid);
822 return VNET_API_ERROR_INVALID_VALUE;
823 }
Florin Corase127a7e2016-02-18 22:20:01 +0100824
825 /* clear locator-set to eids binding */
Florin Corasa2157cf2016-08-16 21:09:14 +0200826 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
827 a->locator_set_index);
828 for (i = 0; i < vec_len (eid_indexes[0]); i++)
829 {
830 map_indexp = vec_elt_at_index (eid_indexes[0], i);
831 if (map_indexp[0] == mi)
832 break;
833 }
834 vec_del1 (eid_indexes[0], i);
Florin Corase127a7e2016-02-18 22:20:01 +0100835
836 /* remove local mark if needed */
Florin Corasa2157cf2016-08-16 21:09:14 +0200837 m = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corase127a7e2016-02-18 22:20:01 +0100838 if (m->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200839 {
840 u32 k, *lm_indexp;
841 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
842 {
843 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
844 if (lm_indexp[0] == mi)
845 break;
846 }
847 vec_del1 (lcm->local_mappings_indexes, k);
848 }
Florin Corase127a7e2016-02-18 22:20:01 +0100849
850 /* remove mapping from dictionary */
Florin Corasf727db92016-06-23 15:01:58 +0200851 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
Filip Tehlar324112f2016-06-02 16:07:38 +0200852 gid_address_free (&m->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100853 pool_put_index (lcm->mapping_pool, mi);
854 }
855
856 return 0;
857}
858
Florin Corasf727db92016-06-23 15:01:58 +0200859/**
860 * Add/update/delete mapping to/in/from map-cache.
861 */
Florin Coras577c3552016-04-21 00:45:40 +0200862int
863vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200864 u32 * map_index_result)
Florin Coras577c3552016-04-21 00:45:40 +0200865{
Florin Corasa2157cf2016-08-16 21:09:14 +0200866 uword *dp_table = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200867 u32 vni;
Florin Coras1a1adc72016-07-22 01:45:30 +0200868 u8 type;
Florin Corasf727db92016-06-23 15:01:58 +0200869
Florin Corasa2157cf2016-08-16 21:09:14 +0200870 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Coras577c3552016-04-21 00:45:40 +0200871
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200872 if (vnet_lisp_enable_disable_status () == 0)
873 {
874 clib_warning ("LISP is disabled!");
875 return VNET_API_ERROR_LISP_DISABLED;
876 }
877
Florin Corasa2157cf2016-08-16 21:09:14 +0200878 vni = gid_address_vni (&a->eid);
879 type = gid_address_type (&a->eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200880 if (GID_ADDR_IP_PREFIX == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200881 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200882 else if (GID_ADDR_MAC == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200883 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras577c3552016-04-21 00:45:40 +0200884
Florin Coras1a1adc72016-07-22 01:45:30 +0200885 if (!dp_table)
Florin Coras577c3552016-04-21 00:45:40 +0200886 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200887 clib_warning ("vni %d not associated to a %s!", vni,
888 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
Florin Coras577c3552016-04-21 00:45:40 +0200889 return VNET_API_ERROR_INVALID_VALUE;
890 }
891
Florin Corasf727db92016-06-23 15:01:58 +0200892 /* store/remove mapping from map-cache */
893 return vnet_lisp_map_cache_add_del (a, map_index_result);
Florin Coras577c3552016-04-21 00:45:40 +0200894}
895
Florin Corase127a7e2016-02-18 22:20:01 +0100896static clib_error_t *
897lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
Florin Corasa2157cf2016-08-16 21:09:14 +0200898 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +0100899{
Florin Corasa2157cf2016-08-16 21:09:14 +0200900 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
901 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corase127a7e2016-02-18 22:20:01 +0100902 u8 is_add = 1;
903 gid_address_t eid;
Florin Corasa2157cf2016-08-16 21:09:14 +0200904 gid_address_t *eids = 0;
905 clib_error_t *error = 0;
906 u8 *locator_set_name = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100907 u32 locator_set_index = 0, map_index = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +0200908 uword *p;
909 vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200910 int rv = 0;
Filip Tehlar324112f2016-06-02 16:07:38 +0200911 u32 vni = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200912 u8 *key = 0;
913 u32 key_id = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100914
Filip Tehlar324112f2016-06-02 16:07:38 +0200915 memset (&eid, 0, sizeof (eid));
Filip Tehlar5ca5dc82016-08-02 19:32:25 +0200916 memset (a, 0, sizeof (*a));
917
Florin Corase127a7e2016-02-18 22:20:01 +0100918 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +0200919 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corase127a7e2016-02-18 22:20:01 +0100920 return 0;
921
922 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
923 {
924 if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +0200925 is_add = 1;
Florin Corase127a7e2016-02-18 22:20:01 +0100926 else if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +0200927 is_add = 0;
Filip Tehlar5ca5dc82016-08-02 19:32:25 +0200928 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
Florin Corasa2157cf2016-08-16 21:09:14 +0200929 ;
Filip Tehlar324112f2016-06-02 16:07:38 +0200930 else if (unformat (line_input, "vni %d", &vni))
Florin Corasa2157cf2016-08-16 21:09:14 +0200931 gid_address_vni (&eid) = vni;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200932 else if (unformat (line_input, "secret-key %_%v%_", &key))
933 ;
934 else if (unformat (line_input, "key-id %U", unformat_hmac_key_id,
935 &key_id))
936 ;
Florin Corase127a7e2016-02-18 22:20:01 +0100937 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +0200938 {
939 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
940 if (!p)
941 {
942 error = clib_error_return (0, "locator-set %s doesn't exist",
943 locator_set_name);
944 goto done;
945 }
946 locator_set_index = p[0];
947 }
Florin Corase127a7e2016-02-18 22:20:01 +0100948 else
Florin Corasa2157cf2016-08-16 21:09:14 +0200949 {
950 error = unformat_parse_error (line_input);
951 goto done;
952 }
Florin Corase127a7e2016-02-18 22:20:01 +0100953 }
Florin Corase127a7e2016-02-18 22:20:01 +0100954 /* XXX treat batch configuration */
Filip Tehlar324112f2016-06-02 16:07:38 +0200955
Florin Corasa2157cf2016-08-16 21:09:14 +0200956 if (GID_ADDR_SRC_DST == gid_address_type (&eid))
Filip Tehlar5ca5dc82016-08-02 19:32:25 +0200957 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200958 error =
959 clib_error_return (0, "src/dst is not supported for local EIDs!");
Filip Tehlar5ca5dc82016-08-02 19:32:25 +0200960 goto done;
961 }
962
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200963 if (key && (0 == key_id))
964 {
965 vlib_cli_output (vm, "invalid key_id!");
966 return 0;
967 }
968
Florin Corasa2157cf2016-08-16 21:09:14 +0200969 gid_address_copy (&a->eid, &eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100970 a->is_add = is_add;
971 a->locator_set_index = locator_set_index;
972 a->local = 1;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200973 a->key = key;
974 a->key_id = key_id;
Florin Corase127a7e2016-02-18 22:20:01 +0100975
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200976 rv = vnet_lisp_add_del_local_mapping (a, &map_index);
977 if (0 != rv)
Florin Corasa2157cf2016-08-16 21:09:14 +0200978 {
979 error = clib_error_return (0, "failed to %s local mapping!",
980 is_add ? "add" : "delete");
981 }
982done:
983 vec_free (eids);
Filip Tehlar53f09e32016-05-19 14:25:44 +0200984 if (locator_set_name)
985 vec_free (locator_set_name);
Florin Corasf727db92016-06-23 15:01:58 +0200986 gid_address_free (&a->eid);
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200987 vec_free (a->key);
Florin Corase127a7e2016-02-18 22:20:01 +0100988 return error;
989}
990
Florin Corasa2157cf2016-08-16 21:09:14 +0200991/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +0100992VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
993 .path = "lisp eid-table",
Filip Tehlar324112f2016-06-02 16:07:38 +0200994 .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200995 "locator-set <locator-set> [key <secret-key> key-id sha1|sha256 ]",
Florin Corase127a7e2016-02-18 22:20:01 +0100996 .function = lisp_add_del_local_eid_command_fn,
997};
Florin Corasa2157cf2016-08-16 21:09:14 +0200998/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +0100999
Filip Tehlar324112f2016-06-02 16:07:38 +02001000int
Florin Coras1a1adc72016-07-22 01:45:30 +02001001vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
Filip Tehlar324112f2016-06-02 16:07:38 +02001002{
Florin Corasa2157cf2016-08-16 21:09:14 +02001003 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1004 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
Filip Tehlar324112f2016-06-02 16:07:38 +02001005
1006 if (vnet_lisp_enable_disable_status () == 0)
1007 {
1008 clib_warning ("LISP is disabled!");
1009 return -1;
1010 }
1011
Florin Coras1a1adc72016-07-22 01:45:30 +02001012 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
1013 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
1014
1015 if (!is_l2 && (vni == 0 || dp_id == 0))
Filip Tehlar324112f2016-06-02 16:07:38 +02001016 {
1017 clib_warning ("can't add/del default vni-vrf mapping!");
1018 return -1;
1019 }
1020
Florin Coras1a1adc72016-07-22 01:45:30 +02001021 dp_idp = hash_get (dp_table_by_vni[0], vni);
1022 vnip = hash_get (vni_by_dp_table[0], dp_id);
Filip Tehlar324112f2016-06-02 16:07:38 +02001023
1024 if (is_add)
1025 {
Florin Coras1a1adc72016-07-22 01:45:30 +02001026 if (dp_idp || vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +02001027 {
1028 clib_warning ("vni %d or vrf %d already used in vrf/vni "
1029 "mapping!", vni, dp_id);
1030 return -1;
1031 }
Florin Coras1a1adc72016-07-22 01:45:30 +02001032 hash_set (dp_table_by_vni[0], vni, dp_id);
1033 hash_set (vni_by_dp_table[0], dp_id, vni);
Florin Corasf727db92016-06-23 15:01:58 +02001034
1035 /* create dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +02001036 dp_add_del_iface (lcm, vni, is_l2, 1);
Filip Tehlar324112f2016-06-02 16:07:38 +02001037 }
1038 else
1039 {
Florin Coras1a1adc72016-07-22 01:45:30 +02001040 if (!dp_idp || !vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +02001041 {
1042 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
1043 "mapping!", vni, dp_id);
1044 return -1;
1045 }
Florin Coras1a1adc72016-07-22 01:45:30 +02001046 hash_unset (dp_table_by_vni[0], vni);
1047 hash_unset (vni_by_dp_table[0], dp_id);
Florin Corasf727db92016-06-23 15:01:58 +02001048
1049 /* remove dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +02001050 dp_add_del_iface (lcm, vni, is_l2, 0);
Filip Tehlar324112f2016-06-02 16:07:38 +02001051 }
1052 return 0;
Florin Coras1a1adc72016-07-22 01:45:30 +02001053
Filip Tehlar324112f2016-06-02 16:07:38 +02001054}
1055
1056static clib_error_t *
1057lisp_eid_table_map_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001058 unformat_input_t * input,
1059 vlib_cli_command_t * cmd)
Filip Tehlar324112f2016-06-02 16:07:38 +02001060{
Florin Coras1a1adc72016-07-22 01:45:30 +02001061 u8 is_add = 1, is_l2 = 0;
1062 u32 vni = 0, dp_id = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001063 unformat_input_t _line_input, *line_input = &_line_input;
Filip Tehlar324112f2016-06-02 16:07:38 +02001064
1065 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02001066 if (!unformat_user (input, unformat_line_input, line_input))
Filip Tehlar324112f2016-06-02 16:07:38 +02001067 return 0;
1068
1069 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1070 {
1071 if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001072 is_add = 0;
Filip Tehlar324112f2016-06-02 16:07:38 +02001073 else if (unformat (line_input, "vni %d", &vni))
Florin Corasa2157cf2016-08-16 21:09:14 +02001074 ;
Florin Coras1a1adc72016-07-22 01:45:30 +02001075 else if (unformat (line_input, "vrf %d", &dp_id))
Florin Corasa2157cf2016-08-16 21:09:14 +02001076 ;
Florin Coras1a1adc72016-07-22 01:45:30 +02001077 else if (unformat (line_input, "bd %d", &dp_id))
Florin Corasa2157cf2016-08-16 21:09:14 +02001078 is_l2 = 1;
Filip Tehlar324112f2016-06-02 16:07:38 +02001079 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001080 {
1081 return unformat_parse_error (line_input);
1082 }
Filip Tehlar324112f2016-06-02 16:07:38 +02001083 }
Florin Coras1a1adc72016-07-22 01:45:30 +02001084 vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
Filip Tehlar324112f2016-06-02 16:07:38 +02001085 return 0;
1086}
1087
Florin Corasa2157cf2016-08-16 21:09:14 +02001088/* *INDENT-OFF* */
Filip Tehlar324112f2016-06-02 16:07:38 +02001089VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
1090 .path = "lisp eid-table map",
Florin Coras1a1adc72016-07-22 01:45:30 +02001091 .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
Filip Tehlar324112f2016-06-02 16:07:38 +02001092 .function = lisp_eid_table_map_command_fn,
1093};
Florin Corasa2157cf2016-08-16 21:09:14 +02001094/* *INDENT-ON* */
Florin Corasf727db92016-06-23 15:01:58 +02001095
1096/* return 0 if the two locator sets are identical 1 otherwise */
1097static u8
Florin Corasa2157cf2016-08-16 21:09:14 +02001098compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
1099 locator_t * new_locators)
Filip Tehlar53f09e32016-05-19 14:25:44 +02001100{
Florin Corasf727db92016-06-23 15:01:58 +02001101 u32 i, old_li;
Florin Corasa2157cf2016-08-16 21:09:14 +02001102 locator_t *old_loc, *new_loc;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001103
Florin Corasa2157cf2016-08-16 21:09:14 +02001104 if (vec_len (old_ls_indexes) != vec_len (new_locators))
Florin Corasf727db92016-06-23 15:01:58 +02001105 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001106
Florin Corasa2157cf2016-08-16 21:09:14 +02001107 for (i = 0; i < vec_len (new_locators); i++)
Filip Tehlar53f09e32016-05-19 14:25:44 +02001108 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001109 old_li = vec_elt (old_ls_indexes, i);
1110 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
Florin Corasf727db92016-06-23 15:01:58 +02001111
Florin Corasa2157cf2016-08-16 21:09:14 +02001112 new_loc = vec_elt_at_index (new_locators, i);
Florin Corasf727db92016-06-23 15:01:58 +02001113
1114 if (locator_cmp (old_loc, new_loc))
Florin Corasa2157cf2016-08-16 21:09:14 +02001115 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001116 }
Florin Corasf727db92016-06-23 15:01:58 +02001117 return 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001118}
1119
Filip Tehlard5fcc462016-10-17 16:20:18 +02001120typedef struct
1121{
1122 u8 is_negative;
1123 void *lcm;
1124 gid_address_t *eids_to_be_deleted;
1125} remove_mapping_args_t;
1126
1127/**
1128 * Callback invoked when a sub-prefix is found
1129 */
1130static void
1131remove_mapping_if_needed (u32 mi, void *arg)
1132{
1133 u8 delete = 0;
1134 remove_mapping_args_t *a = arg;
1135 lisp_cp_main_t *lcm = a->lcm;
1136 mapping_t *m;
1137 locator_set_t *ls;
1138
1139 m = pool_elt_at_index (lcm->mapping_pool, mi);
1140 if (!m)
1141 return;
1142
1143 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1144
1145 if (a->is_negative)
1146 {
1147 if (0 != vec_len (ls->locator_indices))
1148 delete = 1;
1149 }
1150 else
1151 {
1152 if (0 == vec_len (ls->locator_indices))
1153 delete = 1;
1154 }
1155
1156 if (delete)
1157 vec_add1 (a->eids_to_be_deleted, m->eid);
1158}
1159
1160/**
1161 * This function searches map cache and looks for IP prefixes that are subset
1162 * of the provided one. If such prefix is found depending on 'is_negative'
1163 * it does follows:
1164 *
1165 * 1) if is_negative is true and found prefix points to positive mapping,
1166 * then the mapping is removed
1167 * 2) if is_negative is false and found prefix points to negative mapping,
1168 * then the mapping is removed
1169 */
1170static void
1171remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1172 u8 is_negative)
1173{
1174 gid_address_t *e;
1175 remove_mapping_args_t a;
1176 memset (&a, 0, sizeof (a));
1177
1178 /* do this only in src/dst mode ... */
1179 if (MR_MODE_SRC_DST != lcm->map_request_mode)
1180 return;
1181
1182 /* ... and only for IP prefix */
1183 if (GID_ADDR_SRC_DST != gid_address_type (eid)
1184 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1185 return;
1186
1187 a.is_negative = is_negative;
1188 a.lcm = lcm;
1189
1190 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1191 remove_mapping_if_needed, &a);
1192
1193 vec_foreach (e, a.eids_to_be_deleted)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001194 {
1195 lisp_add_del_adjacency (lcm, 0, e, 0 /* is_add */ );
Filip Tehlard5fcc462016-10-17 16:20:18 +02001196 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001197 }
Filip Tehlard5fcc462016-10-17 16:20:18 +02001198
1199 vec_free (a.eids_to_be_deleted);
1200}
1201
Filip Tehlar9677a942016-11-28 10:23:31 +01001202static void
1203mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1204{
1205 timing_wheel_delete (&lcm->wheel, mi);
1206}
1207
Filip Tehlar195bcee2016-05-13 17:37:35 +02001208/**
Florin Corasf727db92016-06-23 15:01:58 +02001209 * Adds/removes/updates mapping. Does not program forwarding.
Filip Tehlar195bcee2016-05-13 17:37:35 +02001210 *
Florin Coras71893ac2016-07-10 20:09:32 +02001211 * @param eid end-host identifier
Filip Tehlar195bcee2016-05-13 17:37:35 +02001212 * @param rlocs vector of remote locators
1213 * @param action action for negative map-reply
1214 * @param is_add add mapping if non-zero, delete otherwise
Florin Coras71893ac2016-07-10 20:09:32 +02001215 * @param res_map_index the map-index that was created/updated/removed. It is
1216 * set to ~0 if no action is taken.
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001217 * @param is_static used for distinguishing between statically learned
1218 remote mappings and mappings obtained from MR
Filip Tehlar195bcee2016-05-13 17:37:35 +02001219 * @return return code
1220 */
1221int
Florin Coras71893ac2016-07-10 20:09:32 +02001222vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001223 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
Florin Corasa2157cf2016-08-16 21:09:14 +02001224 u32 * res_map_index)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001225{
Florin Corasa2157cf2016-08-16 21:09:14 +02001226 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1227 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1228 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corasf727db92016-06-23 15:01:58 +02001229 u32 mi, ls_index = 0, dst_map_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02001230 mapping_t *old_map;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001231
Florin Corasa2157cf2016-08-16 21:09:14 +02001232 if (vnet_lisp_enable_disable_status () == 0)
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001233 {
1234 clib_warning ("LISP is disabled!");
1235 return VNET_API_ERROR_LISP_DISABLED;
1236 }
1237
Florin Corasf727db92016-06-23 15:01:58 +02001238 if (res_map_index)
1239 res_map_index[0] = ~0;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001240
Florin Corasf727db92016-06-23 15:01:58 +02001241 memset (m_args, 0, sizeof (m_args[0]));
1242 memset (ls_args, 0, sizeof (ls_args[0]));
Filip Tehlar195bcee2016-05-13 17:37:35 +02001243
Florin Corasf727db92016-06-23 15:01:58 +02001244 ls_args->locators = rlocs;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001245
Florin Coras71893ac2016-07-10 20:09:32 +02001246 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
Florin Corasa2157cf2016-08-16 21:09:14 +02001247 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corasf727db92016-06-23 15:01:58 +02001248
1249 if (is_add)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001250 {
Florin Corasf727db92016-06-23 15:01:58 +02001251 /* overwrite: if mapping already exists, decide if locators should be
1252 * updated and be done */
Florin Coras71893ac2016-07-10 20:09:32 +02001253 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001254 {
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001255 if (!is_static && (old_map->is_static || old_map->local))
1256 {
1257 /* do not overwrite local or static remote mappings */
1258 clib_warning ("mapping %U rejected due to collision with local "
1259 "or static remote mapping!", format_gid_address,
Filip Tehlard5fcc462016-10-17 16:20:18 +02001260 eid);
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001261 return 0;
1262 }
1263
Florin Corasa2157cf2016-08-16 21:09:14 +02001264 locator_set_t *old_ls;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001265
Florin Corasa2157cf2016-08-16 21:09:14 +02001266 /* update mapping attributes */
1267 old_map->action = action;
1268 old_map->authoritative = authoritative;
1269 old_map->ttl = ttl;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001270
Florin Corasa2157cf2016-08-16 21:09:14 +02001271 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1272 old_map->locator_set_index);
1273 if (compare_locators (lcm, old_ls->locator_indices,
1274 ls_args->locators))
1275 {
1276 /* set locator-set index to overwrite */
1277 ls_args->is_add = 1;
1278 ls_args->index = old_map->locator_set_index;
1279 vnet_lisp_add_del_locator_set (ls_args, 0);
1280 if (res_map_index)
1281 res_map_index[0] = mi;
1282 }
1283 }
Florin Corasf727db92016-06-23 15:01:58 +02001284 /* new mapping */
1285 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001286 {
Filip Tehlard5fcc462016-10-17 16:20:18 +02001287 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1288
Florin Corasa2157cf2016-08-16 21:09:14 +02001289 ls_args->is_add = 1;
1290 ls_args->index = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001291
Florin Corasa2157cf2016-08-16 21:09:14 +02001292 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
Florin Corasf727db92016-06-23 15:01:58 +02001293
Florin Corasa2157cf2016-08-16 21:09:14 +02001294 /* add mapping */
1295 gid_address_copy (&m_args->eid, eid);
1296 m_args->is_add = 1;
1297 m_args->action = action;
1298 m_args->locator_set_index = ls_index;
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001299 m_args->is_static = is_static;
Filip Tehlar9677a942016-11-28 10:23:31 +01001300 m_args->ttl = ttl;
Florin Corasa2157cf2016-08-16 21:09:14 +02001301 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +02001302
Florin Corasa2157cf2016-08-16 21:09:14 +02001303 if (res_map_index)
1304 res_map_index[0] = dst_map_index;
1305 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001306 }
Florin Corasf727db92016-06-23 15:01:58 +02001307 else
1308 {
Florin Coras71893ac2016-07-10 20:09:32 +02001309 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001310 {
1311 clib_warning ("cannot delete mapping for eid %U",
1312 format_gid_address, eid);
1313 return -1;
1314 }
Florin Corasf727db92016-06-23 15:01:58 +02001315
1316 m_args->is_add = 0;
Florin Coras71893ac2016-07-10 20:09:32 +02001317 gid_address_copy (&m_args->eid, eid);
Florin Corasf727db92016-06-23 15:01:58 +02001318 m_args->locator_set_index = old_map->locator_set_index;
1319
1320 /* delete mapping associated from map-cache */
1321 vnet_lisp_map_cache_add_del (m_args, 0);
1322
1323 ls_args->is_add = 0;
1324 ls_args->index = old_map->locator_set_index;
1325 /* delete locator set */
1326 vnet_lisp_add_del_locator_set (ls_args, 0);
Filip Tehlarb93222f2016-07-07 09:58:08 +02001327
Filip Tehlar9677a942016-11-28 10:23:31 +01001328 /* delete timer associated to the mapping if any */
1329 if (old_map->timer_set)
1330 mapping_delete_timer (lcm, mi);
1331
Filip Tehlarb93222f2016-07-07 09:58:08 +02001332 /* return old mapping index */
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001333 if (res_map_index)
Florin Corasa2157cf2016-08-16 21:09:14 +02001334 res_map_index[0] = mi;
Florin Corasf727db92016-06-23 15:01:58 +02001335 }
1336
Filip Tehlar195bcee2016-05-13 17:37:35 +02001337 /* success */
Florin Corasf727db92016-06-23 15:01:58 +02001338 return 0;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001339}
1340
Filip Tehlar58f886a2016-05-30 15:57:40 +02001341int
Florin Corasf727db92016-06-23 15:01:58 +02001342vnet_lisp_clear_all_remote_adjacencies (void)
Filip Tehlar58f886a2016-05-30 15:57:40 +02001343{
1344 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001345 u32 mi, *map_indices = 0, *map_indexp;
1346 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1347 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1348 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001349
Florin Corasa2157cf2016-08-16 21:09:14 +02001350 /* *INDENT-OFF* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001351 pool_foreach_index (mi, lcm->mapping_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02001352 ({
1353 vec_add1 (map_indices, mi);
1354 }));
1355 /* *INDENT-ON* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001356
1357 vec_foreach (map_indexp, map_indices)
Florin Corasa2157cf2016-08-16 21:09:14 +02001358 {
1359 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1360 if (!map->local)
1361 {
1362 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001363
Florin Corasa2157cf2016-08-16 21:09:14 +02001364 dm_args->is_add = 0;
1365 gid_address_copy (&dm_args->eid, &map->eid);
1366 dm_args->locator_set_index = map->locator_set_index;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001367
Florin Corasa2157cf2016-08-16 21:09:14 +02001368 /* delete mapping associated to fwd entry */
1369 vnet_lisp_map_cache_add_del (dm_args, 0);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001370
Florin Corasa2157cf2016-08-16 21:09:14 +02001371 ls->is_add = 0;
1372 ls->local = 0;
1373 ls->index = map->locator_set_index;
1374 /* delete locator set */
1375 rv = vnet_lisp_add_del_locator_set (ls, 0);
1376 if (rv != 0)
1377 goto cleanup;
1378 }
1379 }
Filip Tehlar58f886a2016-05-30 15:57:40 +02001380
1381cleanup:
1382 if (map_indices)
1383 vec_free (map_indices);
1384 return rv;
1385}
1386
Filip Tehlar195bcee2016-05-13 17:37:35 +02001387/**
Florin Coras71893ac2016-07-10 20:09:32 +02001388 * Adds adjacency or removes forwarding entry associated to remote mapping.
1389 * Note that adjacencies are not stored, they only result in forwarding entries
1390 * being created.
Florin Corasf727db92016-06-23 15:01:58 +02001391 */
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001392static int
Florin Coras71893ac2016-07-10 20:09:32 +02001393lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
Florin Corasa2157cf2016-08-16 21:09:14 +02001394 gid_address_t * remote_eid, u8 is_add)
Florin Corasf727db92016-06-23 15:01:58 +02001395{
Florin Coras71893ac2016-07-10 20:09:32 +02001396 u32 local_mi, remote_mi = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001397
1398 if (vnet_lisp_enable_disable_status () == 0)
1399 {
1400 clib_warning ("LISP is disabled!");
1401 return VNET_API_ERROR_LISP_DISABLED;
1402 }
1403
Filip Tehlar69a9b762016-09-23 10:00:52 +02001404 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1405 remote_eid, local_eid);
Florin Coras71893ac2016-07-10 20:09:32 +02001406 if (GID_LOOKUP_MISS == remote_mi)
1407 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001408 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1409 format_gid_address, remote_eid);
Florin Corasf727db92016-06-23 15:01:58 +02001410
Florin Coras71893ac2016-07-10 20:09:32 +02001411 return -1;
1412 }
1413
1414 if (is_add)
Florin Corasf727db92016-06-23 15:01:58 +02001415 {
1416 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1417 * delete*/
1418
1419 /* check if source eid has an associated mapping. If pitr mode is on,
1420 * just use the pitr's mapping */
Florin Coras71893ac2016-07-10 20:09:32 +02001421 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
Florin Corasa2157cf2016-08-16 21:09:14 +02001422 gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
Florin Corasf727db92016-06-23 15:01:58 +02001423
1424
Florin Coras71893ac2016-07-10 20:09:32 +02001425 if (GID_LOOKUP_MISS == local_mi)
Florin Corasa2157cf2016-08-16 21:09:14 +02001426 {
1427 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1428 format_gid_address, local_eid);
Florin Corasf727db92016-06-23 15:01:58 +02001429
Florin Corasa2157cf2016-08-16 21:09:14 +02001430 return -1;
1431 }
Florin Corasf727db92016-06-23 15:01:58 +02001432
Florin Coras71893ac2016-07-10 20:09:32 +02001433 /* update forwarding */
1434 dp_add_fwd_entry (lcm, local_mi, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001435 }
1436 else
Florin Coras71893ac2016-07-10 20:09:32 +02001437 dp_del_fwd_entry (lcm, 0, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001438
1439 return 0;
1440}
1441
1442int
1443vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1444{
Florin Corasa2157cf2016-08-16 21:09:14 +02001445 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001446 return lisp_add_del_adjacency (lcm, &a->leid, &a->reid, a->is_add);
Florin Corasf727db92016-06-23 15:01:58 +02001447}
1448
1449/**
Filip Tehlar195bcee2016-05-13 17:37:35 +02001450 * Handler for add/del remote mapping CLI.
1451 *
1452 * @param vm vlib context
1453 * @param input input from user
1454 * @param cmd cmd
1455 * @return pointer to clib error structure
1456 */
1457static clib_error_t *
1458lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001459 unformat_input_t * input,
1460 vlib_cli_command_t * cmd)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001461{
Florin Corasa2157cf2016-08-16 21:09:14 +02001462 clib_error_t *error = 0;
1463 unformat_input_t _line_input, *line_input = &_line_input;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001464 u8 is_add = 1, del_all = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001465 locator_t rloc, *rlocs = 0, *curr_rloc = 0;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001466 gid_address_t eid;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001467 u8 eid_set = 0;
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02001468 u32 vni, action = ~0, p, w;
Florin Corasf727db92016-06-23 15:01:58 +02001469 int rv;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001470
1471 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02001472 if (!unformat_user (input, unformat_line_input, line_input))
Filip Tehlar195bcee2016-05-13 17:37:35 +02001473 return 0;
1474
Florin Corasa2157cf2016-08-16 21:09:14 +02001475 memset (&eid, 0, sizeof (eid));
1476 memset (&rloc, 0, sizeof (rloc));
Filip Tehlar195bcee2016-05-13 17:37:35 +02001477
Filip Tehlar195bcee2016-05-13 17:37:35 +02001478 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1479 {
Filip Tehlar58f886a2016-05-30 15:57:40 +02001480 if (unformat (line_input, "del-all"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001481 del_all = 1;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001482 else if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001483 is_add = 0;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001484 else if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001485 ;
Florin Corasbb5c22f2016-08-02 02:31:03 +02001486 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
Florin Corasa2157cf2016-08-16 21:09:14 +02001487 eid_set = 1;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001488 else if (unformat (line_input, "vni %u", &vni))
Florin Corasa2157cf2016-08-16 21:09:14 +02001489 {
1490 gid_address_vni (&eid) = vni;
1491 }
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02001492 else if (unformat (line_input, "p %d w %d", &p, &w))
Florin Corasa2157cf2016-08-16 21:09:14 +02001493 {
1494 if (!curr_rloc)
1495 {
1496 clib_warning
1497 ("No RLOC configured for setting priority/weight!");
1498 goto done;
1499 }
1500 curr_rloc->priority = p;
1501 curr_rloc->weight = w;
1502 }
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02001503 else if (unformat (line_input, "rloc %U", unformat_ip_address,
Florin Corasa2157cf2016-08-16 21:09:14 +02001504 &gid_address_ip (&rloc.address)))
1505 {
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02001506 /* since rloc is stored in ip prefix we need to set prefix length */
1507 ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
1508
1509 u8 version = gid_address_ip_version (&rloc.address);
1510 ip_prefix_len (pref) = ip_address_max_len (version);
1511
Florin Corasa2157cf2016-08-16 21:09:14 +02001512 vec_add1 (rlocs, rloc);
1513 curr_rloc = &rlocs[vec_len (rlocs) - 1];
1514 }
Florin Corasf7643fd2016-08-01 15:35:20 +02001515 else if (unformat (line_input, "action %U",
Florin Corasa2157cf2016-08-16 21:09:14 +02001516 unformat_negative_mapping_action, &action))
1517 ;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001518 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001519 {
1520 clib_warning ("parse error");
1521 goto done;
1522 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001523 }
1524
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001525 if (!eid_set)
1526 {
1527 clib_warning ("missing eid!");
1528 goto done;
1529 }
1530
Filip Tehlar58f886a2016-05-30 15:57:40 +02001531 if (!del_all)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001532 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001533 if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1534 {
1535 clib_warning ("no action set for negative map-reply!");
1536 goto done;
1537 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001538 }
Florin Corasf727db92016-06-23 15:01:58 +02001539 else
1540 {
1541 vnet_lisp_clear_all_remote_adjacencies ();
1542 goto done;
1543 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001544
Florin Corasa2157cf2016-08-16 21:09:14 +02001545 /* TODO build src/dst with seid */
Florin Corasf727db92016-06-23 15:01:58 +02001546
1547 /* if it's a delete, clean forwarding */
1548 if (!is_add)
1549 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001550 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001551 rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1552 if (rv)
Florin Corasa2157cf2016-08-16 21:09:14 +02001553 {
1554 goto done;
1555 }
Florin Corasf727db92016-06-23 15:01:58 +02001556 }
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001557
1558 /* add as static remote mapping, i.e., not authoritative and infinite
1559 * ttl */
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001560 rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
1561 1 /* is_static */ , 0);
Florin Corasf727db92016-06-23 15:01:58 +02001562
Filip Tehlar195bcee2016-05-13 17:37:35 +02001563 if (rv)
Florin Corasa2157cf2016-08-16 21:09:14 +02001564 clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
Filip Tehlar195bcee2016-05-13 17:37:35 +02001565
1566done:
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02001567 vec_free (rlocs);
Filip Tehlar195bcee2016-05-13 17:37:35 +02001568 unformat_free (line_input);
1569 return error;
1570}
1571
Florin Corasa2157cf2016-08-16 21:09:14 +02001572VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) =
1573{
1574.path = "lisp remote-mapping",.short_help =
1575 "lisp remote-mapping add|del [del-all] vni <vni> "
1576 "eid <est-eid> [action <no-action|natively-forward|"
1577 "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
1578 "[rloc <dst-locator> ... ]",.function =
1579 lisp_add_del_remote_mapping_command_fn,};
Filip Tehlar195bcee2016-05-13 17:37:35 +02001580
Florin Corasf727db92016-06-23 15:01:58 +02001581/**
1582 * Handler for add/del adjacency CLI.
1583 */
1584static clib_error_t *
1585lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
Florin Corasa2157cf2016-08-16 21:09:14 +02001586 vlib_cli_command_t * cmd)
Florin Corasf727db92016-06-23 15:01:58 +02001587{
Florin Corasa2157cf2016-08-16 21:09:14 +02001588 clib_error_t *error = 0;
1589 unformat_input_t _line_input, *line_input = &_line_input;
1590 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
Florin Corasf727db92016-06-23 15:01:58 +02001591 u8 is_add = 1;
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001592 ip_prefix_t *reid_ippref, *leid_ippref;
1593 gid_address_t leid, reid;
1594 u8 *dmac = gid_address_mac (&reid);
1595 u8 *smac = gid_address_mac (&leid);
1596 u8 reid_set = 0, leid_set = 0;
1597 u32 vni;
Florin Corasf727db92016-06-23 15:01:58 +02001598 int rv;
1599
1600 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02001601 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corasf727db92016-06-23 15:01:58 +02001602 return 0;
1603
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001604 memset (&reid, 0, sizeof (reid));
1605 memset (&leid, 0, sizeof (leid));
Florin Corasf727db92016-06-23 15:01:58 +02001606
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001607 leid_ippref = &gid_address_ippref (&leid);
1608 reid_ippref = &gid_address_ippref (&reid);
Florin Corasf727db92016-06-23 15:01:58 +02001609
1610 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1611 {
1612 if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001613 is_add = 0;
Florin Corasf727db92016-06-23 15:01:58 +02001614 else if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001615 ;
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001616 else if (unformat (line_input, "reid %U",
1617 unformat_ip_prefix, reid_ippref))
Florin Corasa2157cf2016-08-16 21:09:14 +02001618 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001619 gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
1620 reid_set = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001621 }
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001622 else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
Florin Corasa2157cf2016-08-16 21:09:14 +02001623 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001624 gid_address_type (&reid) = GID_ADDR_MAC;
1625 reid_set = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001626 }
Florin Corasf727db92016-06-23 15:01:58 +02001627 else if (unformat (line_input, "vni %u", &vni))
Florin Corasa2157cf2016-08-16 21:09:14 +02001628 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001629 gid_address_vni (&leid) = vni;
1630 gid_address_vni (&reid) = vni;
Florin Corasa2157cf2016-08-16 21:09:14 +02001631 }
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001632 else if (unformat (line_input, "leid %U",
1633 unformat_ip_prefix, leid_ippref))
Florin Corasa2157cf2016-08-16 21:09:14 +02001634 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001635 gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
1636 leid_set = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001637 }
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001638 else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
Florin Corasa2157cf2016-08-16 21:09:14 +02001639 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001640 gid_address_type (&leid) = GID_ADDR_MAC;
1641 leid_set = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001642 }
Florin Corasf727db92016-06-23 15:01:58 +02001643 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001644 {
1645 clib_warning ("parse error");
1646 goto done;
1647 }
Florin Corasf727db92016-06-23 15:01:58 +02001648 }
1649
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001650 if (!reid_set || !leid_set)
Florin Corasf727db92016-06-23 15:01:58 +02001651 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001652 clib_warning ("missing remote or local eid!");
Florin Corasf727db92016-06-23 15:01:58 +02001653 goto done;
1654 }
1655
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001656 if ((gid_address_type (&leid) != gid_address_type (&reid))
1657 || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
1658 && ip_prefix_version (reid_ippref)
1659 != ip_prefix_version (leid_ippref)))
Florin Corasf727db92016-06-23 15:01:58 +02001660 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001661 clib_warning ("remote and local EIDs are of different types!");
1662 return error;
Florin Corasf727db92016-06-23 15:01:58 +02001663 }
1664
Florin Corasa2157cf2016-08-16 21:09:14 +02001665 memset (a, 0, sizeof (a[0]));
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001666 gid_address_copy (&a->leid, &leid);
1667 gid_address_copy (&a->reid, &reid);
Florin Coras71893ac2016-07-10 20:09:32 +02001668
Florin Corasf727db92016-06-23 15:01:58 +02001669 a->is_add = is_add;
Florin Corasf727db92016-06-23 15:01:58 +02001670 rv = vnet_lisp_add_del_adjacency (a);
1671
1672 if (rv)
Florin Corasa2157cf2016-08-16 21:09:14 +02001673 clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
Florin Corasf727db92016-06-23 15:01:58 +02001674
1675done:
1676 unformat_free (line_input);
Florin Corasf727db92016-06-23 15:01:58 +02001677 return error;
1678}
1679
Florin Corasa2157cf2016-08-16 21:09:14 +02001680/* *INDENT-OFF* */
Florin Corasf727db92016-06-23 15:01:58 +02001681VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1682 .path = "lisp adjacency",
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001683 .short_help = "lisp adjacency add|del vni <vni> reid <remote-eid> "
1684 "leid <local-eid>",
Florin Corasf727db92016-06-23 15:01:58 +02001685 .function = lisp_add_del_adjacency_command_fn,
1686};
Florin Corasa2157cf2016-08-16 21:09:14 +02001687/* *INDENT-ON* */
Florin Corasf727db92016-06-23 15:01:58 +02001688
Florin Corasdca88042016-09-14 16:01:38 +02001689int
1690vnet_lisp_set_map_request_mode (u8 mode)
1691{
1692 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1693
1694 if (vnet_lisp_enable_disable_status () == 0)
1695 {
1696 clib_warning ("LISP is disabled!");
1697 return VNET_API_ERROR_LISP_DISABLED;
1698 }
1699
1700 if (mode >= _MR_MODE_MAX)
1701 {
1702 clib_warning ("Invalid LISP map request mode %d!", mode);
1703 return VNET_API_ERROR_INVALID_ARGUMENT;
1704 }
1705
1706 lcm->map_request_mode = mode;
1707 return 0;
1708}
1709
1710static clib_error_t *
1711lisp_map_request_mode_command_fn (vlib_main_t * vm,
1712 unformat_input_t * input,
1713 vlib_cli_command_t * cmd)
1714{
1715 unformat_input_t _i, *i = &_i;
1716 map_request_mode_t mr_mode = _MR_MODE_MAX;
1717
1718 /* Get a line of input. */
1719 if (!unformat_user (input, unformat_line_input, i))
1720 return 0;
1721
1722 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1723 {
1724 if (unformat (i, "dst-only"))
1725 mr_mode = MR_MODE_DST_ONLY;
1726 else if (unformat (i, "src-dst"))
1727 mr_mode = MR_MODE_SRC_DST;
1728 else
1729 {
1730 clib_warning ("parse error '%U'", format_unformat_error, i);
1731 goto done;
1732 }
1733 }
1734
1735 if (_MR_MODE_MAX == mr_mode)
1736 {
1737 clib_warning ("No LISP map request mode entered!");
1738 return 0;
1739 }
1740
1741 vnet_lisp_set_map_request_mode (mr_mode);
1742done:
1743 return 0;
1744}
1745
1746/* *INDENT-OFF* */
1747VLIB_CLI_COMMAND (lisp_map_request_mode_command) = {
1748 .path = "lisp map-request mode",
1749 .short_help = "lisp map-request mode dst-only|src-dst",
1750 .function = lisp_map_request_mode_command_fn,
1751};
1752/* *INDENT-ON* */
1753
1754static u8 *
1755format_lisp_map_request_mode (u8 * s, va_list * args)
1756{
1757 u32 mode = va_arg (*args, u32);
1758
1759 switch (mode)
1760 {
1761 case 0:
1762 return format (0, "dst-only");
1763 case 1:
1764 return format (0, "src-dst");
1765 }
1766 return 0;
1767}
1768
1769static clib_error_t *
1770lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
1771 unformat_input_t * input,
1772 vlib_cli_command_t * cmd)
1773{
1774 vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
1775 vnet_lisp_get_map_request_mode ());
1776 return 0;
1777}
1778
1779/* *INDENT-OFF* */
1780VLIB_CLI_COMMAND (lisp_show_map_request_mode_command) = {
1781 .path = "show lisp map-request mode",
1782 .short_help = "show lisp map-request mode",
1783 .function = lisp_show_map_request_mode_command_fn,
1784};
1785/* *INDENT-ON* */
1786
Filip Tehlarf747cd82016-05-26 16:47:11 +02001787static clib_error_t *
1788lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001789 unformat_input_t * input,
1790 vlib_cli_command_t * cmd)
Filip Tehlarf747cd82016-05-26 16:47:11 +02001791{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001792 lisp_msmr_t *mr;
Florin Corasa2157cf2016-08-16 21:09:14 +02001793 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarf747cd82016-05-26 16:47:11 +02001794
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001795 vec_foreach (mr, lcm->map_resolvers)
Florin Corasa2157cf2016-08-16 21:09:14 +02001796 {
1797 vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
1798 }
Filip Tehlarf747cd82016-05-26 16:47:11 +02001799 return 0;
1800}
1801
Florin Corasa2157cf2016-08-16 21:09:14 +02001802/* *INDENT-OFF* */
Filip Tehlarf747cd82016-05-26 16:47:11 +02001803VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1804 .path = "show lisp map-resolvers",
1805 .short_help = "show lisp map-resolvers",
1806 .function = lisp_show_map_resolvers_command_fn,
1807};
Florin Corasa2157cf2016-08-16 21:09:14 +02001808/* *INDENT-ON* */
Filip Tehlarf747cd82016-05-26 16:47:11 +02001809
Filip Tehlar53f09e32016-05-19 14:25:44 +02001810int
1811vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1812{
Florin Corasa2157cf2016-08-16 21:09:14 +02001813 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar53f09e32016-05-19 14:25:44 +02001814 u32 locator_set_index = ~0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001815 mapping_t *m;
1816 uword *p;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001817
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001818 if (vnet_lisp_enable_disable_status () == 0)
1819 {
1820 clib_warning ("LISP is disabled!");
1821 return VNET_API_ERROR_LISP_DISABLED;
1822 }
1823
Filip Tehlar53f09e32016-05-19 14:25:44 +02001824 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1825 if (!p)
1826 {
1827 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1828 return -1;
1829 }
1830 locator_set_index = p[0];
1831
1832 if (is_add)
1833 {
1834 pool_get (lcm->mapping_pool, m);
1835 m->locator_set_index = locator_set_index;
1836 m->local = 1;
1837 lcm->pitr_map_index = m - lcm->mapping_pool;
1838
1839 /* enable pitr mode */
1840 lcm->lisp_pitr = 1;
1841 }
1842 else
1843 {
1844 /* remove pitr mapping */
1845 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1846
1847 /* disable pitr mode */
1848 lcm->lisp_pitr = 0;
1849 }
1850 return 0;
1851}
1852
1853static clib_error_t *
1854lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001855 unformat_input_t * input,
1856 vlib_cli_command_t * cmd)
Filip Tehlar53f09e32016-05-19 14:25:44 +02001857{
1858 u8 locator_name_set = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001859 u8 *locator_set_name = 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001860 u8 is_add = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001861 unformat_input_t _line_input, *line_input = &_line_input;
1862 clib_error_t *error = 0;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001863 int rv = 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001864
1865 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02001866 if (!unformat_user (input, unformat_line_input, line_input))
Filip Tehlar53f09e32016-05-19 14:25:44 +02001867 return 0;
1868
1869 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1870 {
1871 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +02001872 locator_name_set = 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001873 else if (unformat (line_input, "disable"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001874 is_add = 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001875 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001876 return clib_error_return (0, "parse error");
Filip Tehlar53f09e32016-05-19 14:25:44 +02001877 }
1878
1879 if (!locator_name_set)
1880 {
1881 clib_warning ("No locator set specified!");
1882 goto done;
1883 }
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001884 rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1885 if (0 != rv)
1886 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001887 error = clib_error_return (0, "failed to %s pitr!",
1888 is_add ? "add" : "delete");
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001889 }
Filip Tehlar53f09e32016-05-19 14:25:44 +02001890
1891done:
1892 if (locator_set_name)
1893 vec_free (locator_set_name);
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001894 return error;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001895}
1896
Florin Corasa2157cf2016-08-16 21:09:14 +02001897/* *INDENT-OFF* */
Filip Tehlar53f09e32016-05-19 14:25:44 +02001898VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1899 .path = "lisp pitr",
1900 .short_help = "lisp pitr [disable] ls <locator-set-name>",
1901 .function = lisp_pitr_set_locator_set_command_fn,
1902};
Florin Corasa2157cf2016-08-16 21:09:14 +02001903/* *INDENT-ON* */
Filip Tehlar53f09e32016-05-19 14:25:44 +02001904
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001905static clib_error_t *
1906lisp_show_pitr_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001907 unformat_input_t * input, vlib_cli_command_t * cmd)
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001908{
Florin Corasa2157cf2016-08-16 21:09:14 +02001909 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1910 mapping_t *m;
1911 locator_set_t *ls;
1912 u8 *tmp_str = 0;
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001913
1914 vlib_cli_output (vm, "%=20s%=16s",
Florin Corasa2157cf2016-08-16 21:09:14 +02001915 "pitr", lcm->lisp_pitr ? "locator-set" : "");
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001916
Florin Corasa2157cf2016-08-16 21:09:14 +02001917 if (!lcm->lisp_pitr)
1918 {
1919 vlib_cli_output (vm, "%=20s", "disable");
1920 return 0;
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001921 }
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001922
Florin Corasa2157cf2016-08-16 21:09:14 +02001923 if (~0 == lcm->pitr_map_index)
1924 {
1925 tmp_str = format (0, "N/A");
1926 }
1927 else
1928 {
1929 m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1930 if (~0 != m->locator_set_index)
1931 {
1932 ls =
1933 pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1934 tmp_str = format (0, "%s", ls->name);
1935 }
1936 else
1937 {
1938 tmp_str = format (0, "N/A");
1939 }
1940 }
1941 vec_add1 (tmp_str, 0);
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001942
Florin Corasa2157cf2016-08-16 21:09:14 +02001943 vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1944
1945 vec_free (tmp_str);
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001946
1947 return 0;
1948}
1949
Florin Corasa2157cf2016-08-16 21:09:14 +02001950/* *INDENT-OFF* */
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001951VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1952 .path = "show lisp pitr",
1953 .short_help = "Show pitr",
1954 .function = lisp_show_pitr_command_fn,
1955};
Florin Corasa2157cf2016-08-16 21:09:14 +02001956/* *INDENT-ON* */
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001957
1958static u8 *
1959format_eid_entry (u8 * s, va_list * args)
1960{
Florin Corasa2157cf2016-08-16 21:09:14 +02001961 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
1962 lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
1963 mapping_t *mapit = va_arg (*args, mapping_t *);
1964 locator_set_t *ls = va_arg (*args, locator_set_t *);
1965 gid_address_t *gid = &mapit->eid;
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02001966 u32 ttl = mapit->ttl;
1967 u8 aut = mapit->authoritative;
Florin Corasa2157cf2016-08-16 21:09:14 +02001968 u32 *loc_index;
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001969 u8 first_line = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001970 u8 *loc;
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001971
Florin Corasa2157cf2016-08-16 21:09:14 +02001972 u8 *type = ls->local ? format (0, "local(%s)", ls->name)
1973 : format (0, "remote");
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001974
1975 if (vec_len (ls->locator_indices) == 0)
1976 {
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02001977 s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
Florin Corasa2157cf2016-08-16 21:09:14 +02001978 type, ttl, aut);
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001979 }
1980 else
1981 {
1982 vec_foreach (loc_index, ls->locator_indices)
Florin Corasa2157cf2016-08-16 21:09:14 +02001983 {
1984 locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1985 if (l->local)
1986 loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1987 l->sw_if_index);
1988 else
1989 loc = format (0, "%U", format_ip_address,
1990 &gid_address_ip (&l->address));
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001991
Florin Corasa2157cf2016-08-16 21:09:14 +02001992 if (first_line)
1993 {
1994 s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
1995 gid, type, loc, ttl, aut);
1996 first_line = 0;
1997 }
1998 else
1999 s = format (s, "%55s%v\n", "", loc);
2000 }
Filip Tehlarb0ccf032016-06-22 09:18:12 +02002001 }
2002 return s;
2003}
2004
Florin Corase127a7e2016-02-18 22:20:01 +01002005static clib_error_t *
Filip Tehlarb0ccf032016-06-22 09:18:12 +02002006lisp_show_eid_table_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02002007 unformat_input_t * input,
2008 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +01002009{
Florin Corasa2157cf2016-08-16 21:09:14 +02002010 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2011 mapping_t *mapit;
2012 unformat_input_t _line_input, *line_input = &_line_input;
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002013 u32 mi;
2014 gid_address_t eid;
2015 u8 print_all = 1;
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002016 u8 filter = 0;
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002017
Florin Corasa2157cf2016-08-16 21:09:14 +02002018 memset (&eid, 0, sizeof (eid));
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002019
2020 /* Get a line of input. */
2021 if (!unformat_user (input, unformat_line_input, line_input))
2022 return 0;
2023
2024 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2025 {
2026 if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
Florin Corasa2157cf2016-08-16 21:09:14 +02002027 print_all = 0;
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002028 else if (unformat (line_input, "local"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002029 filter = 1;
2030 else if (unformat (line_input, "remote"))
2031 filter = 2;
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002032 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002033 return clib_error_return (0, "parse error: '%U'",
2034 format_unformat_error, line_input);
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002035 }
Florin Corase127a7e2016-02-18 22:20:01 +01002036
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002037 vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
Florin Corasa2157cf2016-08-16 21:09:14 +02002038 "EID", "type", "locators", "ttl", "autoritative");
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002039
2040 if (print_all)
2041 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002042 /* *INDENT-OFF* */
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002043 pool_foreach (mapit, lcm->mapping_pool,
2044 ({
2045 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
2046 mapit->locator_set_index);
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002047 if (filter && !((1 == filter && ls->local) ||
2048 (2 == filter && !ls->local)))
2049 {
2050 continue;
2051 }
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002052 vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002053 lcm, mapit, ls);
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002054 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002055 /* *INDENT-ON* */
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002056 }
2057 else
2058 {
2059 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
Florin Corasa2157cf2016-08-16 21:09:14 +02002060 if ((u32) ~ 0 == mi)
2061 return 0;
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002062
2063 mapit = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corasa2157cf2016-08-16 21:09:14 +02002064 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2065 mapit->locator_set_index);
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002066
2067 if (filter && !((1 == filter && ls->local) ||
Florin Corasa2157cf2016-08-16 21:09:14 +02002068 (2 == filter && !ls->local)))
2069 {
2070 return 0;
2071 }
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002072
2073 vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
Florin Corasa2157cf2016-08-16 21:09:14 +02002074 lcm, mapit, ls);
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002075 }
Florin Corase127a7e2016-02-18 22:20:01 +01002076
2077 return 0;
2078}
2079
Florin Corasa2157cf2016-08-16 21:09:14 +02002080/* *INDENT-OFF* */
Filip Tehlarb0ccf032016-06-22 09:18:12 +02002081VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
Florin Corase127a7e2016-02-18 22:20:01 +01002082 .path = "show lisp eid-table",
Filip Tehlarb0ccf032016-06-22 09:18:12 +02002083 .short_help = "Shows EID table",
2084 .function = lisp_show_eid_table_command_fn,
Florin Corase127a7e2016-02-18 22:20:01 +01002085};
Florin Corasa2157cf2016-08-16 21:09:14 +02002086/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002087
2088/* cleans locator to locator-set data and removes locators not part of
2089 * any locator-set */
2090static void
2091clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
2092{
Filip Tehlard1c5cc32016-05-30 10:39:20 +02002093 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002094 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
2095 for (i = 0; i < vec_len (ls->locator_indices); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01002096 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002097 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
2098 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2099 loc_indexp[0]);
2100 for (j = 0; j < vec_len (ls_indexes[0]); j++)
2101 {
2102 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
2103 if (ls_indexp[0] == lsi)
2104 break;
2105 }
Florin Corase127a7e2016-02-18 22:20:01 +01002106
Florin Corasa2157cf2016-08-16 21:09:14 +02002107 /* delete index for removed locator-set */
2108 vec_del1 (ls_indexes[0], j);
Florin Corase127a7e2016-02-18 22:20:01 +01002109
2110 /* delete locator if it's part of no locator-set */
2111 if (vec_len (ls_indexes[0]) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02002112 {
2113 pool_put_index (lcm->locator_pool, loc_indexp[0]);
2114 vec_add1 (to_be_deleted, i);
2115 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02002116 }
2117
2118 if (to_be_deleted)
2119 {
2120 for (i = 0; i < vec_len (to_be_deleted); i++)
Florin Corasa2157cf2016-08-16 21:09:14 +02002121 {
2122 loc_indexp = vec_elt_at_index (to_be_deleted, i);
2123 vec_del1 (ls->locator_indices, loc_indexp[0]);
2124 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02002125 vec_free (to_be_deleted);
Florin Corase127a7e2016-02-18 22:20:01 +01002126 }
2127}
Filip Tehlard1c5cc32016-05-30 10:39:20 +02002128
Florin Corasf727db92016-06-23 15:01:58 +02002129static inline uword *
2130get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002131{
Florin Corasa2157cf2016-08-16 21:09:14 +02002132 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002133
Florin Corasa2157cf2016-08-16 21:09:14 +02002134 ASSERT (a != NULL);
2135 ASSERT (p != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002136
2137 /* find locator-set */
2138 if (a->local)
2139 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002140 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002141 }
2142 else
2143 {
2144 *p = a->index;
2145 }
2146
2147 return p;
2148}
2149
Florin Corasf727db92016-06-23 15:01:58 +02002150static inline int
2151is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
Florin Corasa2157cf2016-08-16 21:09:14 +02002152 locator_t * loc)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002153{
Florin Corasa2157cf2016-08-16 21:09:14 +02002154 locator_t *itloc;
2155 u32 *locit;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002156
Florin Corasa2157cf2016-08-16 21:09:14 +02002157 ASSERT (ls != NULL);
2158 ASSERT (loc != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002159
Florin Corasa2157cf2016-08-16 21:09:14 +02002160 vec_foreach (locit, ls->locator_indices)
2161 {
2162 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2163 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
2164 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
2165 {
2166 clib_warning ("Duplicate locator");
2167 return VNET_API_ERROR_VALUE_EXIST;
2168 }
2169 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002170
2171 return 0;
2172}
2173
Florin Corasf727db92016-06-23 15:01:58 +02002174static inline void
Florin Corasa2157cf2016-08-16 21:09:14 +02002175remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
2176 u32 ls_index, u32 loc_id)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002177{
Florin Corasa2157cf2016-08-16 21:09:14 +02002178 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2179 u32 **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002180
Florin Corasa2157cf2016-08-16 21:09:14 +02002181 ASSERT (ls != NULL);
2182 ASSERT (locit != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002183
Florin Corasa2157cf2016-08-16 21:09:14 +02002184 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
2185 pool_put_index (lcm->locator_pool, locit[0]);
2186 vec_del1 (ls->locator_indices, loc_id);
2187 vec_del1 (ls_indexes[0], ls_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002188}
2189
2190int
2191vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02002192 locator_set_t * ls, u32 * ls_result)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002193{
Florin Corasa2157cf2016-08-16 21:09:14 +02002194 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2195 locator_t *loc = NULL, *itloc = NULL;
2196 uword _p = (u32) ~ 0, *p = &_p;
2197 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002198 u32 loc_id = ~0;
2199 int ret = 0;
2200
Florin Corasa2157cf2016-08-16 21:09:14 +02002201 ASSERT (a != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002202
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002203 if (vnet_lisp_enable_disable_status () == 0)
2204 {
2205 clib_warning ("LISP is disabled!");
2206 return VNET_API_ERROR_LISP_DISABLED;
2207 }
2208
Florin Corasa2157cf2016-08-16 21:09:14 +02002209 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002210 if (!p)
2211 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002212 clib_warning ("locator-set %v doesn't exist", a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002213 return VNET_API_ERROR_INVALID_ARGUMENT;
2214 }
2215
2216 if (ls == 0)
2217 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002218 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002219 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02002220 {
2221 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2222 p[0]);
2223 return VNET_API_ERROR_INVALID_ARGUMENT;
2224 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002225 }
2226
2227 if (a->is_add)
2228 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002229 if (ls_result)
2230 ls_result[0] = p[0];
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002231
Florin Corasa2157cf2016-08-16 21:09:14 +02002232 /* allocate locators */
2233 vec_foreach (itloc, a->locators)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002234 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002235 ret = is_locator_in_locator_set (lcm, ls, itloc);
2236 if (0 != ret)
2237 {
2238 return ret;
2239 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002240
Florin Corasa2157cf2016-08-16 21:09:14 +02002241 pool_get (lcm->locator_pool, loc);
2242 loc[0] = itloc[0];
2243 loc_index = loc - lcm->locator_pool;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002244
Florin Corasa2157cf2016-08-16 21:09:14 +02002245 vec_add1 (ls->locator_indices, loc_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002246
Florin Corasa2157cf2016-08-16 21:09:14 +02002247 vec_validate (lcm->locator_to_locator_sets, loc_index);
2248 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2249 loc_index);
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02002250 vec_add1 (ls_indexes[0], p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002251 }
Florin Corasa2157cf2016-08-16 21:09:14 +02002252 }
2253 else
2254 {
2255 ls_index = p[0];
2256
2257 itloc = a->locators;
2258 loc_id = 0;
2259 vec_foreach (locit, ls->locator_indices)
2260 {
2261 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2262
2263 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
2264 {
2265 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2266 }
2267 if (0 == loc->local &&
2268 !gid_address_cmp (&loc->address, &itloc->address))
2269 {
2270 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2271 }
2272
2273 loc_id++;
2274 }
2275 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002276
2277 return 0;
2278}
2279
Florin Corase127a7e2016-02-18 22:20:01 +01002280int
2281vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02002282 u32 * ls_result)
Florin Corase127a7e2016-02-18 22:20:01 +01002283{
Florin Corasa2157cf2016-08-16 21:09:14 +02002284 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2285 locator_set_t *ls;
2286 uword _p = (u32) ~ 0, *p = &_p;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002287 u32 ls_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02002288 u32 **eid_indexes;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002289 int ret = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002290
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002291 if (vnet_lisp_enable_disable_status () == 0)
2292 {
2293 clib_warning ("LISP is disabled!");
2294 return VNET_API_ERROR_LISP_DISABLED;
2295 }
2296
Florin Corase127a7e2016-02-18 22:20:01 +01002297 if (a->is_add)
2298 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002299 p = get_locator_set_index (a, p);
Florin Corase127a7e2016-02-18 22:20:01 +01002300
2301 /* overwrite */
Florin Corasa2157cf2016-08-16 21:09:14 +02002302 if (p && p[0] != (u32) ~ 0)
2303 {
2304 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2305 if (!ls)
2306 {
2307 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2308 p[0]);
2309 return -1;
2310 }
Florin Corase127a7e2016-02-18 22:20:01 +01002311
Florin Corasa2157cf2016-08-16 21:09:14 +02002312 /* clean locator to locator-set vectors and remove locators if
2313 * they're not part of another locator-set */
2314 clean_locator_to_locator_set (lcm, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01002315
Florin Corasa2157cf2016-08-16 21:09:14 +02002316 /* remove locator indices from locator set */
2317 vec_free (ls->locator_indices);
Florin Corase127a7e2016-02-18 22:20:01 +01002318
Florin Corasa2157cf2016-08-16 21:09:14 +02002319 ls_index = p[0];
Florin Corase127a7e2016-02-18 22:20:01 +01002320
Florin Corasa2157cf2016-08-16 21:09:14 +02002321 if (ls_result)
2322 ls_result[0] = p[0];
2323 }
Florin Corase127a7e2016-02-18 22:20:01 +01002324 /* new locator-set */
2325 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002326 {
2327 pool_get (lcm->locator_set_pool, ls);
2328 memset (ls, 0, sizeof (*ls));
2329 ls_index = ls - lcm->locator_set_pool;
Florin Corase127a7e2016-02-18 22:20:01 +01002330
Florin Corasa2157cf2016-08-16 21:09:14 +02002331 if (a->local)
2332 {
2333 ls->name = vec_dup (a->name);
Florin Corase127a7e2016-02-18 22:20:01 +01002334
Florin Corasa2157cf2016-08-16 21:09:14 +02002335 if (!lcm->locator_set_index_by_name)
2336 lcm->locator_set_index_by_name = hash_create_vec (
2337 /* size */
2338 0,
2339 sizeof
2340 (ls->name
2341 [0]),
2342 sizeof
2343 (uword));
2344 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
2345 ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01002346
Florin Corasa2157cf2016-08-16 21:09:14 +02002347 /* mark as local locator-set */
2348 vec_add1 (lcm->local_locator_set_indexes, ls_index);
2349 }
2350 ls->local = a->local;
2351 if (ls_result)
2352 ls_result[0] = ls_index;
2353 }
Florin Corase127a7e2016-02-18 22:20:01 +01002354
Florin Corasa2157cf2016-08-16 21:09:14 +02002355 ret = vnet_lisp_add_del_locator (a, ls, NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002356 if (0 != ret)
Florin Corasa2157cf2016-08-16 21:09:14 +02002357 {
2358 return ret;
2359 }
Florin Corase127a7e2016-02-18 22:20:01 +01002360 }
2361 else
2362 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002363 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002364 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02002365 {
2366 clib_warning ("locator-set %v doesn't exists", a->name);
2367 return -1;
2368 }
Florin Corase127a7e2016-02-18 22:20:01 +01002369
Florin Corasa2157cf2016-08-16 21:09:14 +02002370 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01002371 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02002372 {
2373 clib_warning ("locator-set with index %d doesn't exists", p[0]);
2374 return -1;
2375 }
Florin Corase127a7e2016-02-18 22:20:01 +01002376
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002377 if (lcm->mreq_itr_rlocs == p[0])
Florin Corasa2157cf2016-08-16 21:09:14 +02002378 {
2379 clib_warning ("Can't delete the locator-set used to constrain "
2380 "the itr-rlocs in map-requests!");
2381 return -1;
2382 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002383
Florin Corasa2157cf2016-08-16 21:09:14 +02002384 if (vec_len (lcm->locator_set_to_eids) != 0)
2385 {
2386 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
2387 if (vec_len (eid_indexes[0]) != 0)
2388 {
2389 clib_warning
2390 ("Can't delete a locator that supports a mapping!");
2391 return -1;
2392 }
2393 }
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02002394
2395 /* clean locator to locator-sets data */
2396 clean_locator_to_locator_set (lcm, p[0]);
2397
2398 if (ls->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02002399 {
2400 u32 it, lsi;
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02002401
Florin Corasa2157cf2016-08-16 21:09:14 +02002402 vec_foreach_index (it, lcm->local_locator_set_indexes)
2403 {
2404 lsi = vec_elt (lcm->local_locator_set_indexes, it);
2405 if (lsi == p[0])
2406 {
2407 vec_del1 (lcm->local_locator_set_indexes, it);
2408 break;
2409 }
2410 }
2411 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2412 }
2413 vec_free (ls->name);
2414 vec_free (ls->locator_indices);
2415 pool_put (lcm->locator_set_pool, ls);
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02002416 }
2417 return 0;
2418}
2419
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002420int
2421vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
2422{
2423 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2424
2425 lcm->rloc_probing = is_enable;
2426 return 0;
2427}
2428
2429int
2430vnet_lisp_map_register_enable_disable (u8 is_enable)
2431{
2432 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2433
2434 lcm->map_registering = is_enable;
2435 return 0;
2436}
2437
Filip Tehlar46d4e362016-05-09 09:39:26 +02002438clib_error_t *
Florin Corasf727db92016-06-23 15:01:58 +02002439vnet_lisp_enable_disable (u8 is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002440{
Florin Coras1a1adc72016-07-22 01:45:30 +02002441 u32 vni, dp_table;
Florin Corasa2157cf2016-08-16 21:09:14 +02002442 clib_error_t *error = 0;
2443 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2444 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002445
Florin Corasf727db92016-06-23 15:01:58 +02002446 a->is_en = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002447 error = vnet_lisp_gpe_enable_disable (a);
2448 if (error)
2449 {
2450 return clib_error_return (0, "failed to %s data-plane!",
Florin Corasa2157cf2016-08-16 21:09:14 +02002451 a->is_en ? "enable" : "disable");
Filip Tehlar46d4e362016-05-09 09:39:26 +02002452 }
2453
Florin Corasf727db92016-06-23 15:01:58 +02002454 if (is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002455 {
Florin Coras1a1adc72016-07-22 01:45:30 +02002456 /* enable all l2 and l3 ifaces */
Florin Corasa2157cf2016-08-16 21:09:14 +02002457
2458 /* *INDENT-OFF* */
Florin Coras1a1adc72016-07-22 01:45:30 +02002459 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2460 dp_add_del_iface(lcm, vni, 0, 1);
2461 }));
Florin Coras1a1adc72016-07-22 01:45:30 +02002462 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2463 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
Florin Corasf727db92016-06-23 15:01:58 +02002464 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002465 /* *INDENT-ON* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02002466 }
2467 else
2468 {
Florin Corasf727db92016-06-23 15:01:58 +02002469 /* clear interface table */
Florin Corasa2157cf2016-08-16 21:09:14 +02002470 hash_free (lcm->fwd_entry_by_mapping_index);
2471 pool_free (lcm->fwd_entry_pool);
Filip Tehlar46d4e362016-05-09 09:39:26 +02002472 }
2473
2474 /* update global flag */
Florin Corasf727db92016-06-23 15:01:58 +02002475 lcm->is_enabled = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002476
2477 return 0;
2478}
2479
2480static clib_error_t *
2481lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
Florin Corasa2157cf2016-08-16 21:09:14 +02002482 vlib_cli_command_t * cmd)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002483{
Florin Corasa2157cf2016-08-16 21:09:14 +02002484 unformat_input_t _line_input, *line_input = &_line_input;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002485 u8 is_enabled = 0;
2486 u8 is_set = 0;
2487
2488 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02002489 if (!unformat_user (input, unformat_line_input, line_input))
Filip Tehlar46d4e362016-05-09 09:39:26 +02002490 return 0;
2491
2492 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2493 {
2494 if (unformat (line_input, "enable"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002495 {
2496 is_set = 1;
2497 is_enabled = 1;
2498 }
Filip Tehlar46d4e362016-05-09 09:39:26 +02002499 else if (unformat (line_input, "disable"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002500 is_set = 1;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002501 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002502 {
2503 return clib_error_return (0, "parse error: '%U'",
2504 format_unformat_error, line_input);
2505 }
Filip Tehlar46d4e362016-05-09 09:39:26 +02002506 }
2507
2508 if (!is_set)
Florin Corasa2157cf2016-08-16 21:09:14 +02002509 return clib_error_return (0, "state not set");
Filip Tehlar46d4e362016-05-09 09:39:26 +02002510
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002511 vnet_lisp_enable_disable (is_enabled);
2512 return 0;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002513}
2514
Florin Corasa2157cf2016-08-16 21:09:14 +02002515/* *INDENT-OFF* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02002516VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
2517 .path = "lisp",
2518 .short_help = "lisp [enable|disable]",
2519 .function = lisp_enable_disable_command_fn,
2520};
Florin Corasa2157cf2016-08-16 21:09:14 +02002521/* *INDENT-ON* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02002522
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002523static clib_error_t *
2524lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
2525 unformat_input_t * input,
2526 vlib_cli_command_t * cmd)
2527{
2528 unformat_input_t _line_input, *line_input = &_line_input;
2529 u8 is_enabled = 0;
2530 u8 is_set = 0;
2531
2532 /* Get a line of input. */
2533 if (!unformat_user (input, unformat_line_input, line_input))
2534 return 0;
2535
2536 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2537 {
2538 if (unformat (line_input, "enable"))
2539 {
2540 is_set = 1;
2541 is_enabled = 1;
2542 }
2543 else if (unformat (line_input, "disable"))
2544 is_set = 1;
2545 else
2546 {
2547 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2548 line_input);
2549 return 0;
2550 }
2551 }
2552
2553 if (!is_set)
2554 {
2555 vlib_cli_output (vm, "state not set!");
2556 return 0;
2557 }
2558
2559 vnet_lisp_map_register_enable_disable (is_enabled);
2560 return 0;
2561}
2562
2563/* *INDENT-OFF* */
2564VLIB_CLI_COMMAND (lisp_map_register_enable_disable_command) = {
2565 .path = "lisp map-register",
2566 .short_help = "lisp map-register [enable|disable]",
2567 .function = lisp_map_register_enable_disable_command_fn,
2568};
2569/* *INDENT-ON* */
2570
2571static clib_error_t *
2572lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
2573 unformat_input_t * input,
2574 vlib_cli_command_t * cmd)
2575{
2576 unformat_input_t _line_input, *line_input = &_line_input;
2577 u8 is_enabled = 0;
2578 u8 is_set = 0;
2579
2580 /* Get a line of input. */
2581 if (!unformat_user (input, unformat_line_input, line_input))
2582 return 0;
2583
2584 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2585 {
2586 if (unformat (line_input, "enable"))
2587 {
2588 is_set = 1;
2589 is_enabled = 1;
2590 }
2591 else if (unformat (line_input, "disable"))
2592 is_set = 1;
2593 else
2594 {
2595 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2596 line_input);
2597 return 0;
2598 }
2599 }
2600
2601 if (!is_set)
2602 {
2603 vlib_cli_output (vm, "state not set!");
2604 return 0;
2605 }
2606
2607 vnet_lisp_rloc_probe_enable_disable (is_enabled);
2608 return 0;
2609}
2610
2611/* *INDENT-OFF* */
2612VLIB_CLI_COMMAND (lisp_rloc_probe_enable_disable_command) = {
2613 .path = "lisp rloc-probe",
2614 .short_help = "lisp rloc-probe [enable|disable]",
2615 .function = lisp_rloc_probe_enable_disable_command_fn,
2616};
2617/* *INDENT-ON* */
2618
Filip Tehlar46d4e362016-05-09 09:39:26 +02002619u8
2620vnet_lisp_enable_disable_status (void)
2621{
Florin Corasa2157cf2016-08-16 21:09:14 +02002622 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar46d4e362016-05-09 09:39:26 +02002623 return lcm->is_enabled;
2624}
2625
2626static u8 *
2627format_lisp_status (u8 * s, va_list * args)
2628{
Florin Corasa2157cf2016-08-16 21:09:14 +02002629 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar46d4e362016-05-09 09:39:26 +02002630 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
2631}
2632
2633static clib_error_t *
2634lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
Florin Corasa2157cf2016-08-16 21:09:14 +02002635 vlib_cli_command_t * cmd)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002636{
Florin Corasa2157cf2016-08-16 21:09:14 +02002637 u8 *msg = 0;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002638 msg = format (msg, "feature: %U\ngpe: %U\n",
Florin Corasa2157cf2016-08-16 21:09:14 +02002639 format_lisp_status, format_vnet_lisp_gpe_status);
Filip Tehlar46d4e362016-05-09 09:39:26 +02002640 vlib_cli_output (vm, "%v", msg);
2641 vec_free (msg);
2642 return 0;
2643}
2644
Florin Corasa2157cf2016-08-16 21:09:14 +02002645/* *INDENT-OFF* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02002646VLIB_CLI_COMMAND (lisp_show_status_command) = {
2647 .path = "show lisp status",
2648 .short_help = "show lisp status",
2649 .function = lisp_show_status_command_fn,
2650};
Florin Corasa2157cf2016-08-16 21:09:14 +02002651/* *INDENT-ON* */
Filip Tehlar324112f2016-06-02 16:07:38 +02002652
2653static clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002654lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
2655 unformat_input_t * input,
2656 vlib_cli_command_t * cmd)
Filip Tehlar324112f2016-06-02 16:07:38 +02002657{
Florin Corasa2157cf2016-08-16 21:09:14 +02002658 hash_pair_t *p;
Filip Tehlarc0681792016-08-24 14:11:07 +02002659 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corasa2157cf2016-08-16 21:09:14 +02002660 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarc0681792016-08-24 14:11:07 +02002661 uword *vni_table = 0;
2662 u8 is_l2 = 0;
Filip Tehlar324112f2016-06-02 16:07:38 +02002663
Filip Tehlarc0681792016-08-24 14:11:07 +02002664 /* Get a line of input. */
2665 if (!unformat_user (input, unformat_line_input, line_input))
2666 return 0;
2667
2668 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2669 {
2670 if (unformat (line_input, "l2"))
2671 {
2672 vni_table = lcm->bd_id_by_vni;
2673 is_l2 = 1;
2674 }
2675 else if (unformat (line_input, "l3"))
2676 {
2677 vni_table = lcm->table_id_by_vni;
2678 is_l2 = 0;
2679 }
2680 else
2681 return clib_error_return (0, "parse error: '%U'",
2682 format_unformat_error, line_input);
2683 }
2684
2685 if (!vni_table)
2686 {
2687 vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
2688 return 0;
2689 }
2690
2691 vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
Florin Corasa2157cf2016-08-16 21:09:14 +02002692
2693 /* *INDENT-OFF* */
Filip Tehlarc0681792016-08-24 14:11:07 +02002694 hash_foreach_pair (p, vni_table,
Florin Corasa2157cf2016-08-16 21:09:14 +02002695 ({
2696 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
2697 }));
2698 /* *INDENT-ON* */
2699
Filip Tehlar324112f2016-06-02 16:07:38 +02002700 return 0;
2701}
2702
Florin Corasa2157cf2016-08-16 21:09:14 +02002703/* *INDENT-OFF* */
Filip Tehlar324112f2016-06-02 16:07:38 +02002704VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
2705 .path = "show lisp eid-table map",
Filip Tehlarc0681792016-08-24 14:11:07 +02002706 .short_help = "show lisp eid-table l2|l3",
Filip Tehlar324112f2016-06-02 16:07:38 +02002707 .function = lisp_show_eid_table_map_command_fn,
2708};
Florin Corasa2157cf2016-08-16 21:09:14 +02002709/* *INDENT-ON* */
Filip Tehlar324112f2016-06-02 16:07:38 +02002710
Florin Corase127a7e2016-02-18 22:20:01 +01002711static clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002712lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
2713 unformat_input_t * input,
2714 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +01002715{
Florin Corasa2157cf2016-08-16 21:09:14 +02002716 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2717 vnet_main_t *vnm = lgm->vnet_main;
2718 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corase127a7e2016-02-18 22:20:01 +01002719 u8 is_add = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02002720 clib_error_t *error = 0;
2721 u8 *locator_set_name = 0;
2722 locator_t locator, *locators = 0;
2723 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
Florin Corase127a7e2016-02-18 22:20:01 +01002724 u32 ls_index = 0;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002725 int rv = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002726
Florin Corasa2157cf2016-08-16 21:09:14 +02002727 memset (&locator, 0, sizeof (locator));
2728 memset (a, 0, sizeof (a[0]));
Florin Corase127a7e2016-02-18 22:20:01 +01002729
2730 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02002731 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corase127a7e2016-02-18 22:20:01 +01002732 return 0;
2733
2734 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2735 {
2736 if (unformat (line_input, "add %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +02002737 is_add = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002738 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +02002739 is_add = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002740 else if (unformat (line_input, "iface %U p %d w %d",
Florin Corasa2157cf2016-08-16 21:09:14 +02002741 unformat_vnet_sw_interface, vnm,
2742 &locator.sw_if_index, &locator.priority,
2743 &locator.weight))
2744 {
2745 locator.local = 1;
2746 vec_add1 (locators, locator);
2747 }
Florin Corase127a7e2016-02-18 22:20:01 +01002748 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002749 {
2750 error = unformat_parse_error (line_input);
2751 goto done;
2752 }
Florin Corase127a7e2016-02-18 22:20:01 +01002753 }
2754
2755 a->name = locator_set_name;
2756 a->locators = locators;
2757 a->is_add = is_add;
2758 a->local = 1;
2759
Florin Corasa2157cf2016-08-16 21:09:14 +02002760 rv = vnet_lisp_add_del_locator_set (a, &ls_index);
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002761 if (0 != rv)
2762 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002763 error = clib_error_return (0, "failed to %s locator-set!",
2764 is_add ? "add" : "delete");
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002765 }
Florin Corase127a7e2016-02-18 22:20:01 +01002766
Florin Corasa2157cf2016-08-16 21:09:14 +02002767done:
2768 vec_free (locators);
Filip Tehlar53f09e32016-05-19 14:25:44 +02002769 if (locator_set_name)
2770 vec_free (locator_set_name);
Florin Corase127a7e2016-02-18 22:20:01 +01002771 return error;
2772}
2773
Florin Corasa2157cf2016-08-16 21:09:14 +02002774/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01002775VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2776 .path = "lisp locator-set",
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002777 .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2778 "p <priority> w <weight>]",
Florin Corase127a7e2016-02-18 22:20:01 +01002779 .function = lisp_add_del_locator_set_command_fn,
2780};
Florin Corasa2157cf2016-08-16 21:09:14 +02002781/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002782
2783static clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002784lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
2785 unformat_input_t * input,
2786 vlib_cli_command_t * cmd)
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002787{
Florin Corasa2157cf2016-08-16 21:09:14 +02002788 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2789 vnet_main_t *vnm = lgm->vnet_main;
2790 unformat_input_t _line_input, *line_input = &_line_input;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002791 u8 is_add = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02002792 clib_error_t *error = 0;
2793 u8 *locator_set_name = 0;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002794 u8 locator_set_name_set = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002795 locator_t locator, *locators = 0;
2796 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002797 u32 ls_index = 0;
2798
Florin Corasa2157cf2016-08-16 21:09:14 +02002799 memset (&locator, 0, sizeof (locator));
2800 memset (a, 0, sizeof (a[0]));
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002801
2802 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02002803 if (!unformat_user (input, unformat_line_input, line_input))
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002804 return 0;
2805
2806 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2807 {
2808 if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002809 is_add = 1;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002810 else if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002811 is_add = 0;
2812 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
2813 locator_set_name_set = 1;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002814 else if (unformat (line_input, "iface %U p %d w %d",
Florin Corasa2157cf2016-08-16 21:09:14 +02002815 unformat_vnet_sw_interface, vnm,
2816 &locator.sw_if_index, &locator.priority,
2817 &locator.weight))
2818 {
2819 locator.local = 1;
2820 vec_add1 (locators, locator);
2821 }
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002822 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002823 {
2824 error = unformat_parse_error (line_input);
2825 goto done;
2826 }
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002827 }
2828
2829 if (!locator_set_name_set)
2830 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002831 error = clib_error_return (0, "locator_set name not set!");
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002832 goto done;
Florin Corasa2157cf2016-08-16 21:09:14 +02002833 }
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002834
2835 a->name = locator_set_name;
2836 a->locators = locators;
2837 a->is_add = is_add;
2838 a->local = 1;
2839
Florin Corasa2157cf2016-08-16 21:09:14 +02002840 vnet_lisp_add_del_locator (a, 0, &ls_index);
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002841
Florin Corasa2157cf2016-08-16 21:09:14 +02002842done:
2843 vec_free (locators);
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002844 vec_free (locator_set_name);
2845 return error;
2846}
2847
Florin Corasa2157cf2016-08-16 21:09:14 +02002848/* *INDENT-OFF* */
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002849VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2850 .path = "lisp locator",
2851 .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2852 "p <priority> w <weight>",
2853 .function = lisp_add_del_locator_in_set_command_fn,
2854};
Florin Corasa2157cf2016-08-16 21:09:14 +02002855/* *INDENT-ON* */
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002856
2857static clib_error_t *
Florin Corase127a7e2016-02-18 22:20:01 +01002858lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02002859 unformat_input_t * input,
2860 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +01002861{
Florin Corasa2157cf2016-08-16 21:09:14 +02002862 locator_set_t *lsit;
2863 locator_t *loc;
2864 u32 *locit;
2865 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01002866
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02002867 vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
Florin Corasa2157cf2016-08-16 21:09:14 +02002868 "Priority", "Weight");
2869
2870 /* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01002871 pool_foreach (lsit, lcm->locator_set_pool,
2872 ({
2873 u8 * msg = 0;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002874 int next_line = 0;
Andrej Kozemcaka8691752016-07-27 10:33:38 +02002875 if (lsit->local)
2876 {
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02002877 msg = format (msg, "%v", lsit->name);
Andrej Kozemcaka8691752016-07-27 10:33:38 +02002878 }
2879 else
2880 {
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02002881 msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
Andrej Kozemcaka8691752016-07-27 10:33:38 +02002882 }
Florin Corase127a7e2016-02-18 22:20:01 +01002883 vec_foreach (locit, lsit->locator_indices)
2884 {
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002885 if (next_line)
2886 {
2887 msg = format (msg, "%16s", " ");
2888 }
Florin Corase127a7e2016-02-18 22:20:01 +01002889 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2890 if (loc->local)
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002891 msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
Florin Corase127a7e2016-02-18 22:20:01 +01002892 loc->weight);
2893 else
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002894 msg = format (msg, "%16U%16d%16d\n", format_ip_address,
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02002895 &gid_address_ip(&loc->address), loc->priority,
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002896 loc->weight);
2897 next_line = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002898 }
2899 vlib_cli_output (vm, "%v", msg);
2900 vec_free (msg);
2901 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002902 /* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002903 return 0;
2904}
2905
Florin Corasa2157cf2016-08-16 21:09:14 +02002906/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01002907VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2908 .path = "show lisp locator-set",
2909 .short_help = "Shows locator-sets",
2910 .function = lisp_cp_show_locator_sets_command_fn,
2911};
Florin Corasa2157cf2016-08-16 21:09:14 +02002912/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002913
2914int
2915vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2916{
Florin Corasa2157cf2016-08-16 21:09:14 +02002917 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01002918 u32 i;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002919 lisp_msmr_t _mr, *mr = &_mr;
Florin Corase127a7e2016-02-18 22:20:01 +01002920
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002921 if (vnet_lisp_enable_disable_status () == 0)
2922 {
2923 clib_warning ("LISP is disabled!");
2924 return VNET_API_ERROR_LISP_DISABLED;
2925 }
2926
Florin Corase127a7e2016-02-18 22:20:01 +01002927 if (a->is_add)
2928 {
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002929
2930 if (get_map_resolver (&a->address))
Florin Corasa2157cf2016-08-16 21:09:14 +02002931 {
2932 clib_warning ("map-resolver %U already exists!", format_ip_address,
2933 &a->address);
2934 return -1;
2935 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002936
2937 memset (mr, 0, sizeof (*mr));
Florin Corasa2157cf2016-08-16 21:09:14 +02002938 ip_address_copy (&mr->address, &a->address);
2939 vec_add1 (lcm->map_resolvers, *mr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002940
2941 if (vec_len (lcm->map_resolvers) == 1)
Florin Corasa2157cf2016-08-16 21:09:14 +02002942 lcm->do_map_resolver_election = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002943 }
2944 else
2945 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002946 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2947 {
2948 mr = vec_elt_at_index (lcm->map_resolvers, i);
2949 if (!ip_address_cmp (&mr->address, &a->address))
2950 {
2951 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2952 lcm->do_map_resolver_election = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002953
Florin Corasa2157cf2016-08-16 21:09:14 +02002954 vec_del1 (lcm->map_resolvers, i);
2955 break;
2956 }
2957 }
Florin Corase127a7e2016-02-18 22:20:01 +01002958 }
2959 return 0;
2960}
2961
2962static clib_error_t *
2963lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02002964 unformat_input_t * input,
2965 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +01002966{
Florin Corasa2157cf2016-08-16 21:09:14 +02002967 unformat_input_t _line_input, *line_input = &_line_input;
Florin Coras0f1c29c2016-07-26 12:12:17 +02002968 u8 is_add = 1, addr_set = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002969 ip_address_t ip_addr;
Florin Corasa2157cf2016-08-16 21:09:14 +02002970 clib_error_t *error = 0;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002971 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002972 vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
Florin Corase127a7e2016-02-18 22:20:01 +01002973
2974 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02002975 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corase127a7e2016-02-18 22:20:01 +01002976 return 0;
2977
2978 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2979 {
2980 if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002981 is_add = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002982 else if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002983 is_add = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002984 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
Florin Corasa2157cf2016-08-16 21:09:14 +02002985 addr_set = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002986 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002987 {
2988 error = unformat_parse_error (line_input);
2989 goto done;
2990 }
Florin Corase127a7e2016-02-18 22:20:01 +01002991 }
Florin Coras0f1c29c2016-07-26 12:12:17 +02002992
2993 if (!addr_set)
2994 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002995 error = clib_error_return (0, "Map-resolver address must be set!");
Florin Coras0f1c29c2016-07-26 12:12:17 +02002996 goto done;
2997 }
2998
Florin Corase127a7e2016-02-18 22:20:01 +01002999 a->is_add = is_add;
3000 a->address = ip_addr;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02003001 rv = vnet_lisp_add_del_map_resolver (a);
3002 if (0 != rv)
3003 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003004 error = clib_error_return (0, "failed to %s map-resolver!",
3005 is_add ? "add" : "delete");
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02003006 }
Florin Corase127a7e2016-02-18 22:20:01 +01003007
Florin Corasa2157cf2016-08-16 21:09:14 +02003008done:
Florin Corase127a7e2016-02-18 22:20:01 +01003009 return error;
3010}
3011
Florin Corasa2157cf2016-08-16 21:09:14 +02003012/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01003013VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
3014 .path = "lisp map-resolver",
3015 .short_help = "lisp map-resolver add/del <ip_address>",
3016 .function = lisp_add_del_map_resolver_command_fn,
3017};
Florin Corasa2157cf2016-08-16 21:09:14 +02003018/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01003019
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003020int
3021vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
3022{
Florin Corasa2157cf2016-08-16 21:09:14 +02003023 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3024 uword *p = 0;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003025
Florin Corasf727db92016-06-23 15:01:58 +02003026 if (vnet_lisp_enable_disable_status () == 0)
3027 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003028 clib_warning ("LISP is disabled!");
Florin Corasf727db92016-06-23 15:01:58 +02003029 return VNET_API_ERROR_LISP_DISABLED;
3030 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003031
3032 if (a->is_add)
3033 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003034 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003035 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02003036 {
3037 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
3038 return VNET_API_ERROR_INVALID_ARGUMENT;
3039 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003040
3041 lcm->mreq_itr_rlocs = p[0];
3042 }
3043 else
3044 {
3045 lcm->mreq_itr_rlocs = ~0;
3046 }
3047
3048 return 0;
3049}
3050
3051static clib_error_t *
Florin Corasf727db92016-06-23 15:01:58 +02003052lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02003053 unformat_input_t * input,
3054 vlib_cli_command_t * cmd)
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003055{
Florin Corasa2157cf2016-08-16 21:09:14 +02003056 unformat_input_t _line_input, *line_input = &_line_input;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003057 u8 is_add = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02003058 u8 *locator_set_name = 0;
3059 clib_error_t *error = 0;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003060 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02003061 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003062
3063 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02003064 if (!unformat_user (input, unformat_line_input, line_input))
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003065 return 0;
3066
3067 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3068 {
3069 if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02003070 is_add = 0;
Filip Tehlar240977f2016-10-03 14:08:59 +02003071 else if (unformat (line_input, "add %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +02003072 is_add = 1;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003073 else
Florin Corasa2157cf2016-08-16 21:09:14 +02003074 {
3075 error = unformat_parse_error (line_input);
3076 goto done;
3077 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003078 }
3079
3080 a->is_add = is_add;
3081 a->locator_set_name = locator_set_name;
3082 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
3083 if (0 != rv)
3084 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003085 error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
3086 is_add ? "add" : "delete");
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003087 }
3088
Florin Corasa2157cf2016-08-16 21:09:14 +02003089 vec_free (locator_set_name);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003090
Florin Corasa2157cf2016-08-16 21:09:14 +02003091done:
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003092 return error;
3093
3094}
3095
Florin Corasa2157cf2016-08-16 21:09:14 +02003096/* *INDENT-OFF* */
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003097VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
3098 .path = "lisp map-request itr-rlocs",
3099 .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
3100 .function = lisp_add_del_mreq_itr_rlocs_command_fn,
3101};
Florin Corasa2157cf2016-08-16 21:09:14 +02003102/* *INDENT-ON* */
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003103
3104static clib_error_t *
3105lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02003106 unformat_input_t * input,
3107 vlib_cli_command_t * cmd)
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003108{
Florin Corasa2157cf2016-08-16 21:09:14 +02003109 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3110 locator_set_t *loc_set;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003111
3112 vlib_cli_output (vm, "%=20s", "itr-rlocs");
3113
3114 if (~0 == lcm->mreq_itr_rlocs)
3115 {
3116 return 0;
3117 }
3118
3119 loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
3120
3121 vlib_cli_output (vm, "%=20s", loc_set->name);
3122
3123 return 0;
3124}
3125
Florin Corasa2157cf2016-08-16 21:09:14 +02003126/* *INDENT-OFF* */
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003127VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
3128 .path = "show lisp map-request itr-rlocs",
3129 .short_help = "Shows map-request itr-rlocs",
3130 .function = lisp_show_mreq_itr_rlocs_command_fn,
3131};
Florin Corasa2157cf2016-08-16 21:09:14 +02003132/* *INDENT-ON* */
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003133
Florin Corase127a7e2016-02-18 22:20:01 +01003134/* Statistics (not really errors) */
3135#define foreach_lisp_cp_lookup_error \
3136_(DROP, "drop") \
3137_(MAP_REQUESTS_SENT, "map-request sent")
3138
Florin Corasa2157cf2016-08-16 21:09:14 +02003139static char *lisp_cp_lookup_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01003140#define _(sym,string) string,
3141 foreach_lisp_cp_lookup_error
3142#undef _
3143};
3144
3145typedef enum
3146{
3147#define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02003148 foreach_lisp_cp_lookup_error
Florin Corase127a7e2016-02-18 22:20:01 +01003149#undef _
3150 LISP_CP_LOOKUP_N_ERROR,
3151} lisp_cp_lookup_error_t;
3152
3153typedef enum
3154{
3155 LISP_CP_LOOKUP_NEXT_DROP,
Florin Corase127a7e2016-02-18 22:20:01 +01003156 LISP_CP_LOOKUP_N_NEXT,
3157} lisp_cp_lookup_next_t;
3158
3159typedef struct
3160{
3161 gid_address_t dst_eid;
Andrej Kozemcak94e34762016-05-25 12:43:21 +02003162 ip_address_t map_resolver_ip;
Florin Corase127a7e2016-02-18 22:20:01 +01003163} lisp_cp_lookup_trace_t;
3164
3165u8 *
3166format_lisp_cp_lookup_trace (u8 * s, va_list * args)
3167{
3168 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3169 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02003170 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01003171
3172 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
Florin Corasa2157cf2016-08-16 21:09:14 +02003173 format_ip_address, &t->map_resolver_ip, format_gid_address,
3174 &t->dst_eid);
Florin Corase127a7e2016-02-18 22:20:01 +01003175 return s;
3176}
3177
Florin Coras1c494842016-06-16 21:08:56 +02003178int
3179get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
Florin Corasa2157cf2016-08-16 21:09:14 +02003180 ip_address_t * sloc)
Florin Corasc2c90082016-06-13 18:37:15 +02003181{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003182 lisp_msmr_t *mrit;
Florin Corasa2157cf2016-08-16 21:09:14 +02003183 ip_address_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01003184
Florin Corasa2157cf2016-08-16 21:09:14 +02003185 if (vec_len (lcm->map_resolvers) == 0)
Florin Corase127a7e2016-02-18 22:20:01 +01003186 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003187 clib_warning ("No map-resolver configured");
Florin Coras1c494842016-06-16 21:08:56 +02003188 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003189 }
3190
Florin Corasddfafb82016-05-13 18:09:56 +02003191 /* find the first mr ip we have a route to and the ip of the
3192 * iface that has a route to it */
Florin Corasa2157cf2016-08-16 21:09:14 +02003193 vec_foreach (mrit, lcm->map_resolvers)
3194 {
3195 a = &mrit->address;
3196 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
3197 {
3198 ip_address_copy (mr_ip, a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003199
Florin Corasa2157cf2016-08-16 21:09:14 +02003200 /* also update globals */
3201 return 1;
3202 }
3203 }
Florin Corasddfafb82016-05-13 18:09:56 +02003204
Florin Corasa2157cf2016-08-16 21:09:14 +02003205 clib_warning ("Can't find map-resolver and local interface ip!");
Florin Coras1c494842016-06-16 21:08:56 +02003206 return 0;
Florin Corasddfafb82016-05-13 18:09:56 +02003207}
Filip Tehlar254b0362016-04-07 10:04:34 +02003208
Filip Tehlarbeceab92016-04-20 17:21:55 +02003209static gid_address_t *
Filip Tehlar254b0362016-04-07 10:04:34 +02003210build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
3211{
Florin Corasa2157cf2016-08-16 21:09:14 +02003212 void *addr;
Filip Tehlar254b0362016-04-07 10:04:34 +02003213 u32 i;
Florin Corasa2157cf2016-08-16 21:09:14 +02003214 locator_t *loc;
3215 u32 *loc_indexp;
3216 ip_interface_address_t *ia = 0;
3217 gid_address_t gid_data, *gid = &gid_data;
3218 gid_address_t *rlocs = 0;
3219 ip_prefix_t *ippref = &gid_address_ippref (gid);
3220 ip_address_t *rloc = &ip_prefix_addr (ippref);
Filip Tehlar254b0362016-04-07 10:04:34 +02003221
Filip Tehlar324112f2016-06-02 16:07:38 +02003222 memset (gid, 0, sizeof (gid[0]));
Filip Tehlarbeceab92016-04-20 17:21:55 +02003223 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
Florin Corasa2157cf2016-08-16 21:09:14 +02003224 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
Filip Tehlar254b0362016-04-07 10:04:34 +02003225 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003226 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
Filip Tehlar254b0362016-04-07 10:04:34 +02003227 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
3228
Filip Tehlar254b0362016-04-07 10:04:34 +02003229 /* Add ipv4 locators first TODO sort them */
Florin Corasa2157cf2016-08-16 21:09:14 +02003230
3231 /* *INDENT-OFF* */
Filip Tehlar254b0362016-04-07 10:04:34 +02003232 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3233 loc->sw_if_index, 1 /* unnumbered */,
3234 ({
Florin Coras1c494842016-06-16 21:08:56 +02003235 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
3236 ip_address_set (rloc, addr, IP4);
Florin Corasddfafb82016-05-13 18:09:56 +02003237 ip_prefix_len (ippref) = 32;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02003238 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02003239 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02003240 }));
3241
Filip Tehlar254b0362016-04-07 10:04:34 +02003242 /* Add ipv6 locators */
3243 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3244 loc->sw_if_index, 1 /* unnumbered */,
3245 ({
Florin Coras1c494842016-06-16 21:08:56 +02003246 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
3247 ip_address_set (rloc, addr, IP6);
Florin Corasddfafb82016-05-13 18:09:56 +02003248 ip_prefix_len (ippref) = 128;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02003249 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02003250 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02003251 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02003252 /* *INDENT-ON* */
3253
Filip Tehlar254b0362016-04-07 10:04:34 +02003254 }
3255 return rlocs;
3256}
3257
Florin Corase127a7e2016-02-18 22:20:01 +01003258static vlib_buffer_t *
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003259build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
3260 ip_address_t * sloc, ip_address_t * rloc,
3261 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
3262{
3263 vlib_buffer_t *b;
3264 u32 bi;
3265 vlib_main_t *vm = lcm->vlib_main;
3266
3267 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3268 {
3269 clib_warning ("Can't allocate buffer for Map-Request!");
3270 return 0;
3271 }
3272
3273 b = vlib_get_buffer (vm, bi);
3274
3275 /* leave some space for the encap headers */
3276 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3277
3278 /* put lisp msg */
3279 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
3280 1 /* rloc probe */ , nonce_res);
3281
3282 /* push outer ip header */
3283 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3284 rloc);
3285
3286 bi_res[0] = bi;
3287
3288 return b;
3289}
3290
3291static vlib_buffer_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02003292build_encapsulated_map_request (lisp_cp_main_t * lcm,
3293 gid_address_t * seid, gid_address_t * deid,
3294 locator_set_t * loc_set, ip_address_t * mr_ip,
3295 ip_address_t * sloc, u8 is_smr_invoked,
3296 u64 * nonce_res, u32 * bi_res)
Florin Corase127a7e2016-02-18 22:20:01 +01003297{
Florin Corasa2157cf2016-08-16 21:09:14 +02003298 vlib_buffer_t *b;
Florin Corase127a7e2016-02-18 22:20:01 +01003299 u32 bi;
Florin Corasa2157cf2016-08-16 21:09:14 +02003300 gid_address_t *rlocs = 0;
3301 vlib_main_t *vm = lcm->vlib_main;
Florin Corase127a7e2016-02-18 22:20:01 +01003302
3303 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3304 {
3305 clib_warning ("Can't allocate buffer for Map-Request!");
3306 return 0;
3307 }
3308
3309 b = vlib_get_buffer (vm, bi);
3310
3311 /* leave some space for the encap headers */
3312 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3313
Filip Tehlar254b0362016-04-07 10:04:34 +02003314 /* get rlocs */
3315 rlocs = build_itr_rloc_list (lcm, loc_set);
3316
Filip Tehlard5fcc462016-10-17 16:20:18 +02003317 if (MR_MODE_SRC_DST == lcm->map_request_mode
3318 && GID_ADDR_SRC_DST != gid_address_type (deid))
Florin Corasdca88042016-09-14 16:01:38 +02003319 {
3320 gid_address_t sd;
3321 memset (&sd, 0, sizeof (sd));
3322 build_src_dst (&sd, seid, deid);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003323 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
3324 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02003325 }
3326 else
3327 {
3328 /* put lisp msg */
3329 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003330 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02003331 }
Florin Corase127a7e2016-02-18 22:20:01 +01003332
3333 /* push ecm: udp-ip-lisp */
3334 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
3335
Florin Corase127a7e2016-02-18 22:20:01 +01003336 /* push outer ip header */
Florin Corasddfafb82016-05-13 18:09:56 +02003337 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
Florin Corasa2157cf2016-08-16 21:09:14 +02003338 mr_ip);
Florin Corase127a7e2016-02-18 22:20:01 +01003339
3340 bi_res[0] = bi;
Filip Tehlar254b0362016-04-07 10:04:34 +02003341
Florin Corasa2157cf2016-08-16 21:09:14 +02003342 vec_free (rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01003343 return b;
3344}
3345
3346static void
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003347reset_pending_mr_counters (pending_map_request_t * r)
Florin Corase127a7e2016-02-18 22:20:01 +01003348{
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003349 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3350 r->retries_num = 0;
3351}
3352
3353static int
3354elect_map_resolver (lisp_cp_main_t * lcm)
3355{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003356 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003357
3358 vec_foreach (mr, lcm->map_resolvers)
Florin Corasa2157cf2016-08-16 21:09:14 +02003359 {
3360 if (!mr->is_down)
3361 {
3362 ip_address_copy (&lcm->active_map_resolver, &mr->address);
3363 lcm->do_map_resolver_election = 0;
3364 return 1;
3365 }
3366 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003367 return 0;
3368}
3369
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003370static void
3371free_map_register_records (mapping_t * maps)
3372{
3373 mapping_t *map;
3374 vec_foreach (map, maps) vec_free (map->locators);
3375
3376 vec_free (maps);
3377}
3378
3379static void
3380add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
3381 ip_address_t * probed_loc)
3382{
3383 u32 *li;
3384 locator_t *loc, new;
3385 ip_interface_address_t *ia = 0;
3386 void *addr;
3387 ip_address_t *new_ip = &gid_address_ip (&new.address);
3388
3389 m->locators = 0;
3390 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
3391 locator_set_index);
3392 vec_foreach (li, ls->locator_indices)
3393 {
3394 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
3395 new = loc[0];
3396 if (loc->local)
3397 {
3398 /* *INDENT-OFF* */
3399 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3400 loc->sw_if_index, 1 /* unnumbered */,
3401 ({
3402 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
3403 ia);
3404 ip_address_set (new_ip, addr, IP4);
3405 }));
3406
3407 /* Add ipv6 locators */
3408 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3409 loc->sw_if_index, 1 /* unnumbered */,
3410 ({
3411 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
3412 ia);
3413 ip_address_set (new_ip, addr, IP6);
3414 }));
3415 /* *INDENT-ON* */
3416
3417 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
3418 new.probed = 1;
3419 }
3420 vec_add1 (m->locators, new);
3421 }
3422}
3423
3424static mapping_t *
3425build_map_register_record_list (lisp_cp_main_t * lcm)
3426{
3427 mapping_t *recs = 0, rec, *m;
3428
3429 /* *INDENT-OFF* */
3430 pool_foreach(m, lcm->mapping_pool,
3431 {
3432 /* for now build only local mappings */
3433 if (!m->local)
3434 continue;
3435
3436 rec = m[0];
3437 add_locators (lcm, &rec, m->locator_set_index, NULL);
3438 vec_add1 (recs, rec);
3439 });
3440 /* *INDENT-ON* */
3441
3442 return recs;
3443}
3444
3445static int
3446update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
3447 lisp_key_type_t key_id, u8 * key,
3448 u16 auth_data_len, u32 msg_len)
3449{
3450 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
3451 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
3452
3453 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3454 (unsigned char *) map_reg_hdr, msg_len, NULL,
3455 NULL);
3456 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
3457
3458 return 0;
3459}
3460
3461static vlib_buffer_t *
3462build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
3463 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
3464 mapping_t * records, lisp_key_type_t key_id, u8 * key,
3465 u32 * bi_res)
3466{
3467 void *map_reg_hdr;
3468 vlib_buffer_t *b;
3469 u32 bi, auth_data_len = 0, msg_len = 0;
3470 vlib_main_t *vm = lcm->vlib_main;
3471
3472 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3473 {
3474 clib_warning ("Can't allocate buffer for Map-Register!");
3475 return 0;
3476 }
3477
3478 b = vlib_get_buffer (vm, bi);
3479
3480 /* leave some space for the encap headers */
3481 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3482
3483 auth_data_len = auth_data_len_by_key_id (key_id);
3484 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
3485 auth_data_len, nonce_res,
3486 &msg_len);
3487
3488 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
3489 msg_len);
3490
3491 /* push outer ip header */
3492 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3493 ms_ip);
3494
3495 bi_res[0] = bi;
3496 return b;
3497}
3498
3499static int
3500get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
3501{
3502 lisp_msmr_t *mr;
3503 while (lcm->do_map_resolver_election
3504 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
3505 &lcm->active_map_resolver,
3506 ip)))
3507 {
3508 if (0 == elect_map_resolver (lcm))
3509 /* all map resolvers are down */
3510 {
3511 /* restart MR checking by marking all of them up */
3512 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
3513 return -1;
3514 }
3515 }
3516 return 0;
3517}
3518
3519static int
3520send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
3521 u32 local_locator_set_index, ip_address_t * sloc,
3522 ip_address_t * rloc)
3523{
3524 locator_set_t *ls;
3525 u32 bi;
3526 vlib_buffer_t *b;
3527 vlib_frame_t *f;
3528 u64 nonce = 0;
3529 u32 next_index, *to_next;
3530 gid_address_t *itr_rlocs;
3531
3532 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
3533 itr_rlocs = build_itr_rloc_list (lcm, ls);
3534
3535 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
3536 vec_free (itr_rlocs);
3537 if (!b)
3538 return -1;
3539
3540 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3541
3542 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3543 ip4_lookup_node.index : ip6_lookup_node.index;
3544
3545 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3546
3547 /* Enqueue the packet */
3548 to_next = vlib_frame_vector_args (f);
3549 to_next[0] = bi;
3550 f->n_vectors = 1;
3551 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3552
3553 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3554 return 0;
3555}
3556
3557static int
3558send_rloc_probes (lisp_cp_main_t * lcm)
3559{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003560 u8 lprio = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003561 mapping_t *lm;
3562 fwd_entry_t *e;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003563 locator_pair_t *lp;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003564 u32 si;
3565
3566 /* *INDENT-OFF* */
3567 pool_foreach (e, lcm->fwd_entry_pool,
3568 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003569 if (vec_len (e->locator_pairs) == 0)
3570 continue;
3571
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003572 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
3573 if (~0 == si)
3574 {
3575 clib_warning ("internal error: cannot find local eid %U in "
3576 "map-cache!", format_gid_address, &e->leid);
3577 continue;
3578 }
3579 lm = pool_elt_at_index (lcm->mapping_pool, si);
3580
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003581 /* get the best (lowest) priority */
3582 lprio = e->locator_pairs[0].priority;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003583
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003584 /* send rloc-probe for pair(s) with the best remote locator priority */
3585 vec_foreach (lp, e->locator_pairs)
3586 {
3587 if (lp->priority != lprio)
3588 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003589
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003590 /* get first remote locator */
3591 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
3592 &lp->rmt_loc);
3593 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003594 });
3595 /* *INDENT-ON* */
3596
3597 return 0;
3598}
3599
3600static int
3601send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
3602{
3603 u32 bi;
3604 vlib_buffer_t *b;
3605 ip_address_t sloc;
3606 vlib_frame_t *f;
3607 u64 nonce = 0;
3608 u32 next_index, *to_next;
3609 ip_address_t *ms = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003610 mapping_t *records, *r, *g;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003611
3612 // TODO: support multiple map servers and do election
3613 if (0 == vec_len (lcm->map_servers))
3614 return -1;
3615
3616 ms = &lcm->map_servers[0].address;
3617
3618 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
3619 {
3620 clib_warning ("no eligible interface address found for %U!",
3621 format_ip_address, &lcm->map_servers[0]);
3622 return -1;
3623 }
3624
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003625 records = build_map_register_record_list (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003626 if (!records)
3627 return -1;
3628
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003629 vec_foreach (r, records)
3630 {
3631 u8 *key = r->key;
3632 u8 key_id = r->key_id;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003633
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003634 if (!key)
3635 continue; /* no secret key -> map-register cannot be sent */
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003636
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003637 g = 0;
3638 // TODO: group mappings that share common key
3639 vec_add1 (g, r[0]);
3640 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, g,
3641 key_id, key, &bi);
3642 vec_free (g);
3643 if (!b)
3644 continue;
3645
3646 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3647
3648 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3649 ip4_lookup_node.index : ip6_lookup_node.index;
3650
3651 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3652
3653 /* Enqueue the packet */
3654 to_next = vlib_frame_vector_args (f);
3655 to_next[0] = bi;
3656 f->n_vectors = 1;
3657 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3658
3659 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3660 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003661 free_map_register_records (records);
3662
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003663 return 0;
3664}
3665
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003666#define send_encapsulated_map_request(lcm, seid, deid, smr) \
3667 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
3668
3669#define resend_encapsulated_map_request(lcm, seid, deid, smr) \
3670 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
3671
3672static int
Florin Corasa2157cf2016-08-16 21:09:14 +02003673_send_encapsulated_map_request (lisp_cp_main_t * lcm,
3674 gid_address_t * seid, gid_address_t * deid,
3675 u8 is_smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003676{
Florin Corasa2157cf2016-08-16 21:09:14 +02003677 u32 next_index, bi = 0, *to_next, map_index;
3678 vlib_buffer_t *b;
3679 vlib_frame_t *f;
Florin Corase127a7e2016-02-18 22:20:01 +01003680 u64 nonce = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02003681 locator_set_t *loc_set;
3682 mapping_t *map;
3683 pending_map_request_t *pmr, *duplicate_pmr = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003684 ip_address_t sloc;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003685 u32 ls_index;
Florin Corase127a7e2016-02-18 22:20:01 +01003686
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003687 /* if there is already a pending request remember it */
Florin Corasa2157cf2016-08-16 21:09:14 +02003688
3689 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003690 pool_foreach(pmr, lcm->pending_map_requests_pool,
3691 ({
3692 if (!gid_address_cmp (&pmr->src, seid)
3693 && !gid_address_cmp (&pmr->dst, deid))
3694 {
3695 duplicate_pmr = pmr;
3696 break;
3697 }
3698 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02003699 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003700
3701 if (!is_resend && duplicate_pmr)
3702 {
3703 /* don't send the request if there is a pending map request already */
3704 return 0;
3705 }
3706
Florin Corase127a7e2016-02-18 22:20:01 +01003707 /* get locator-set for seid */
Filip Tehlar53f09e32016-05-19 14:25:44 +02003708 if (!lcm->lisp_pitr)
Florin Corase127a7e2016-02-18 22:20:01 +01003709 {
Filip Tehlar53f09e32016-05-19 14:25:44 +02003710 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
3711 if (map_index == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003712 {
3713 clib_warning ("No local mapping found in eid-table for %U!",
3714 format_gid_address, seid);
3715 return -1;
3716 }
Filip Tehlar53f09e32016-05-19 14:25:44 +02003717
3718 map = pool_elt_at_index (lcm->mapping_pool, map_index);
3719
3720 if (!map->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02003721 {
3722 clib_warning
3723 ("Mapping found for src eid %U is not marked as local!",
3724 format_gid_address, seid);
3725 return -1;
3726 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003727 ls_index = map->locator_set_index;
Filip Tehlar53f09e32016-05-19 14:25:44 +02003728 }
3729 else
3730 {
3731 map_index = lcm->pitr_map_index;
3732 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003733 ls_index = map->locator_set_index;
Florin Corase127a7e2016-02-18 22:20:01 +01003734 }
3735
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003736 /* overwrite locator set if map-request itr-rlocs configured */
3737 if (~0 != lcm->mreq_itr_rlocs)
3738 {
3739 ls_index = lcm->mreq_itr_rlocs;
3740 }
3741
3742 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01003743
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003744 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003745 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003746 if (duplicate_pmr)
3747 duplicate_pmr->to_be_removed = 1;
3748 return -1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003749 }
Florin Corasddfafb82016-05-13 18:09:56 +02003750
Florin Corase127a7e2016-02-18 22:20:01 +01003751 /* build the encapsulated map request */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003752 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
Florin Corasa2157cf2016-08-16 21:09:14 +02003753 &lcm->active_map_resolver,
3754 &sloc, is_smr_invoked, &nonce, &bi);
Florin Corase127a7e2016-02-18 22:20:01 +01003755
3756 if (!b)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003757 return -1;
Florin Corase127a7e2016-02-18 22:20:01 +01003758
Florin Corasf727db92016-06-23 15:01:58 +02003759 /* set fib index to default and lookup node */
Florin Corasa2157cf2016-08-16 21:09:14 +02003760 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3761 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3762 ip4_lookup_node.index : ip6_lookup_node.index;
Florin Corase127a7e2016-02-18 22:20:01 +01003763
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003764 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
Florin Corase127a7e2016-02-18 22:20:01 +01003765
3766 /* Enqueue the packet */
3767 to_next = vlib_frame_vector_args (f);
3768 to_next[0] = bi;
3769 f->n_vectors = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003770 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
Florin Corase127a7e2016-02-18 22:20:01 +01003771
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003772 if (duplicate_pmr)
3773 /* if there is a pending request already update it */
3774 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003775 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
3776 {
3777 /* remove the oldest nonce */
3778 u64 CLIB_UNUSED (tmp), *nonce_del;
3779 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
3780 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
3781 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
3782 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003783
Florin Corasf4691cd2016-08-15 19:16:32 +02003784 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003785 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02003786 duplicate_pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003787 }
3788 else
3789 {
3790 /* add map-request to pending requests table */
Florin Corasa2157cf2016-08-16 21:09:14 +02003791 pool_get (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003792 memset (pmr, 0, sizeof (*pmr));
3793 gid_address_copy (&pmr->src, seid);
3794 gid_address_copy (&pmr->dst, deid);
Florin Corasf4691cd2016-08-15 19:16:32 +02003795 clib_fifo_add1 (pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003796 pmr->is_smr_invoked = is_smr_invoked;
3797 reset_pending_mr_counters (pmr);
3798 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02003799 pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003800 }
3801
3802 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003803}
3804
3805static void
Florin Corasa2157cf2016-08-16 21:09:14 +02003806get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
Florin Corase127a7e2016-02-18 22:20:01 +01003807{
Florin Corasa2157cf2016-08-16 21:09:14 +02003808 ip4_header_t *ip4 = hdr;
3809 ip6_header_t *ip6;
Florin Corase127a7e2016-02-18 22:20:01 +01003810
3811 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3812 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003813 ip_address_set (src, &ip4->src_address, IP4);
3814 ip_address_set (dst, &ip4->dst_address, IP4);
Florin Corase127a7e2016-02-18 22:20:01 +01003815 }
3816 else
3817 {
3818 ip6 = hdr;
Florin Corasa2157cf2016-08-16 21:09:14 +02003819 ip_address_set (src, &ip6->src_address, IP6);
3820 ip_address_set (dst, &ip6->dst_address, IP6);
Florin Corase127a7e2016-02-18 22:20:01 +01003821 }
3822}
3823
Filip Tehlar324112f2016-06-02 16:07:38 +02003824static u32
Florin Coras1a1adc72016-07-22 01:45:30 +02003825lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
Florin Corasa2157cf2016-08-16 21:09:14 +02003826 u8 version)
Filip Tehlar324112f2016-06-02 16:07:38 +02003827{
Florin Corasa2157cf2016-08-16 21:09:14 +02003828 uword *vnip;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003829 u32 vni = ~0, table_id = ~0;
Filip Tehlar324112f2016-06-02 16:07:38 +02003830
Florin Coras87e40692016-09-22 18:02:17 +02003831 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
3832 IP4 ? FIB_PROTOCOL_IP4 :
3833 FIB_PROTOCOL_IP6),
3834 vnet_buffer
3835 (b)->sw_if_index
3836 [VLIB_RX]);
Filip Tehlar324112f2016-06-02 16:07:38 +02003837
3838 vnip = hash_get (lcm->vni_by_table_id, table_id);
3839 if (vnip)
3840 vni = vnip[0];
3841 else
3842 clib_warning ("vrf %d is not mapped to any vni!", table_id);
3843
3844 return vni;
3845}
3846
Florin Coras1a1adc72016-07-22 01:45:30 +02003847always_inline u32
3848lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3849{
Florin Corasa2157cf2016-08-16 21:09:14 +02003850 uword *vnip;
Florin Coras1a1adc72016-07-22 01:45:30 +02003851 u32 vni = ~0;
3852 u32 sw_if_index0;
3853
Florin Corasa2157cf2016-08-16 21:09:14 +02003854 l2input_main_t *l2im = &l2input_main;
3855 l2_input_config_t *config;
3856 l2_bridge_domain_t *bd_config;
Florin Coras1a1adc72016-07-22 01:45:30 +02003857
Florin Corasa2157cf2016-08-16 21:09:14 +02003858 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3859 config = vec_elt_at_index (l2im->configs, sw_if_index0);
Florin Coras1a1adc72016-07-22 01:45:30 +02003860 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3861
3862 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
3863 if (vnip)
3864 vni = vnip[0];
3865 else
Florin Corasa2157cf2016-08-16 21:09:14 +02003866 clib_warning ("bridge domain %d is not mapped to any vni!",
3867 config->bd_index);
Florin Coras1a1adc72016-07-22 01:45:30 +02003868
3869 return vni;
3870}
3871
3872always_inline void
Florin Corasa2157cf2016-08-16 21:09:14 +02003873get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3874 gid_address_t * src, gid_address_t * dst)
Florin Coras1a1adc72016-07-22 01:45:30 +02003875{
3876 u32 vni = 0;
3877 u16 type;
3878
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003879 memset (src, 0, sizeof (*src));
3880 memset (dst, 0, sizeof (*dst));
Florin Corasa2157cf2016-08-16 21:09:14 +02003881 type = vnet_buffer (b)->lisp.overlay_afi;
Florin Coras1a1adc72016-07-22 01:45:30 +02003882
3883 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3884 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003885 ip4_header_t *ip;
Florin Coras1a1adc72016-07-22 01:45:30 +02003886 u8 version, preflen;
3887
Florin Corasa2157cf2016-08-16 21:09:14 +02003888 gid_address_type (src) = GID_ADDR_IP_PREFIX;
3889 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
Florin Coras1a1adc72016-07-22 01:45:30 +02003890
3891 ip = vlib_buffer_get_current (b);
Florin Corasa2157cf2016-08-16 21:09:14 +02003892 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
Florin Coras1a1adc72016-07-22 01:45:30 +02003893
Florin Corasa2157cf2016-08-16 21:09:14 +02003894 version = gid_address_ip_version (src);
Florin Coras1a1adc72016-07-22 01:45:30 +02003895 preflen = ip_address_max_len (version);
Florin Corasa2157cf2016-08-16 21:09:14 +02003896 gid_address_ippref_len (src) = preflen;
3897 gid_address_ippref_len (dst) = preflen;
Florin Coras1a1adc72016-07-22 01:45:30 +02003898
3899 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3900 gid_address_vni (dst) = vni;
3901 gid_address_vni (src) = vni;
3902 }
3903 else if (LISP_AFI_MAC == type)
3904 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003905 ethernet_header_t *eh;
Florin Coras1a1adc72016-07-22 01:45:30 +02003906
3907 eh = vlib_buffer_get_current (b);
3908
Florin Corasa2157cf2016-08-16 21:09:14 +02003909 gid_address_type (src) = GID_ADDR_MAC;
3910 gid_address_type (dst) = GID_ADDR_MAC;
3911 mac_copy (&gid_address_mac (src), eh->src_address);
3912 mac_copy (&gid_address_mac (dst), eh->dst_address);
Florin Coras1a1adc72016-07-22 01:45:30 +02003913
3914 /* get vni */
3915 vni = lisp_get_vni_from_buffer_eth (lcm, b);
3916
3917 gid_address_vni (dst) = vni;
3918 gid_address_vni (src) = vni;
3919 }
3920}
3921
Florin Corase127a7e2016-02-18 22:20:01 +01003922static uword
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003923lisp_cp_lookup_inline (vlib_main_t * vm,
3924 vlib_node_runtime_t * node,
3925 vlib_frame_t * from_frame, int overlay)
Florin Corase127a7e2016-02-18 22:20:01 +01003926{
Florin Corasa2157cf2016-08-16 21:09:14 +02003927 u32 *from, *to_next_drop, di, si;
3928 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01003929 u32 pkts_mapped = 0;
3930 uword n_left_from, n_left_to_next_drop;
3931
3932 from = vlib_frame_vector_args (from_frame);
3933 n_left_from = from_frame->n_vectors;
3934
3935 while (n_left_from > 0)
3936 {
3937 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
Florin Corasa2157cf2016-08-16 21:09:14 +02003938 to_next_drop, n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01003939
3940 while (n_left_from > 0 && n_left_to_next_drop > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003941 {
3942 u32 pi0;
3943 vlib_buffer_t *b0;
3944 gid_address_t src, dst;
Florin Corase127a7e2016-02-18 22:20:01 +01003945
Florin Corasa2157cf2016-08-16 21:09:14 +02003946 pi0 = from[0];
3947 from += 1;
3948 n_left_from -= 1;
3949 to_next_drop[0] = pi0;
3950 to_next_drop += 1;
3951 n_left_to_next_drop -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01003952
Florin Corasa2157cf2016-08-16 21:09:14 +02003953 b0 = vlib_get_buffer (vm, pi0);
3954 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003955 vnet_buffer (b0)->lisp.overlay_afi = overlay;
Florin Corase127a7e2016-02-18 22:20:01 +01003956
Florin Corasa2157cf2016-08-16 21:09:14 +02003957 /* src/dst eid pair */
3958 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
Filip Tehlar324112f2016-06-02 16:07:38 +02003959
Florin Corasa2157cf2016-08-16 21:09:14 +02003960 /* if we have remote mapping for destination already in map-chache
3961 add forwarding tunnel directly. If not send a map-request */
Florin Corasdca88042016-09-14 16:01:38 +02003962 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3963 &src);
Florin Corasa2157cf2016-08-16 21:09:14 +02003964 if (~0 != di)
3965 {
3966 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3967 /* send a map-request also in case of negative mapping entry
3968 with corresponding action */
3969 if (m->action == LISP_SEND_MAP_REQUEST)
3970 {
3971 /* send map-request */
3972 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3973 0 /* is_resend */ );
3974 pkts_mapped++;
3975 }
3976 else
3977 {
3978 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3979 &src);
3980 if (~0 != si)
3981 {
Filip Tehlarce1aae42017-01-04 10:42:25 +01003982 dp_add_fwd_entry_from_mt (si, di);
Florin Corasa2157cf2016-08-16 21:09:14 +02003983 }
3984 }
3985 }
3986 else
3987 {
3988 /* send map-request */
3989 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3990 0 /* is_resend */ );
3991 pkts_mapped++;
3992 }
Florin Corase127a7e2016-02-18 22:20:01 +01003993
Florin Corasa2157cf2016-08-16 21:09:14 +02003994 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3995 {
3996 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3997 sizeof (*tr));
Andrej Kozemcak94e34762016-05-25 12:43:21 +02003998
Florin Corasa2157cf2016-08-16 21:09:14 +02003999 memset (tr, 0, sizeof (*tr));
4000 gid_address_copy (&tr->dst_eid, &dst);
Florin Coras5a1c11b2016-09-06 16:29:34 +02004001 ip_address_copy (&tr->map_resolver_ip,
4002 &lcm->active_map_resolver);
Florin Corasa2157cf2016-08-16 21:09:14 +02004003 }
4004 gid_address_free (&dst);
4005 gid_address_free (&src);
4006 }
Florin Corase127a7e2016-02-18 22:20:01 +01004007
Florin Corasa2157cf2016-08-16 21:09:14 +02004008 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
4009 n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01004010 }
4011 vlib_node_increment_counter (vm, node->node_index,
Florin Corasa2157cf2016-08-16 21:09:14 +02004012 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
4013 pkts_mapped);
Florin Corase127a7e2016-02-18 22:20:01 +01004014 return from_frame->n_vectors;
4015}
4016
Neale Ranns0bfe5d82016-08-25 15:29:12 +01004017static uword
4018lisp_cp_lookup_ip4 (vlib_main_t * vm,
4019 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4020{
4021 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
4022}
4023
4024static uword
4025lisp_cp_lookup_ip6 (vlib_main_t * vm,
4026 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4027{
4028 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
4029}
4030
Neale Ranns5e575b12016-10-03 09:40:25 +01004031static uword
4032lisp_cp_lookup_l2 (vlib_main_t * vm,
4033 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4034{
4035 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
4036}
4037
Florin Corasa2157cf2016-08-16 21:09:14 +02004038/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01004039VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
4040 .function = lisp_cp_lookup_ip4,
4041 .name = "lisp-cp-lookup-ip4",
4042 .vector_size = sizeof (u32),
4043 .format_trace = format_lisp_cp_lookup_trace,
4044 .type = VLIB_NODE_TYPE_INTERNAL,
4045
4046 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4047 .error_strings = lisp_cp_lookup_error_strings,
4048
4049 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4050
4051 .next_nodes = {
4052 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Neale Ranns0bfe5d82016-08-25 15:29:12 +01004053 },
4054};
4055/* *INDENT-ON* */
4056
4057/* *INDENT-OFF* */
4058VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
4059 .function = lisp_cp_lookup_ip6,
4060 .name = "lisp-cp-lookup-ip6",
Florin Corase127a7e2016-02-18 22:20:01 +01004061 .vector_size = sizeof (u32),
4062 .format_trace = format_lisp_cp_lookup_trace,
4063 .type = VLIB_NODE_TYPE_INTERNAL,
4064
4065 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4066 .error_strings = lisp_cp_lookup_error_strings,
4067
4068 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4069
4070 .next_nodes = {
4071 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Neale Ranns5e575b12016-10-03 09:40:25 +01004072 },
4073};
4074/* *INDENT-ON* */
4075
4076/* *INDENT-OFF* */
4077VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
4078 .function = lisp_cp_lookup_l2,
4079 .name = "lisp-cp-lookup-l2",
4080 .vector_size = sizeof (u32),
4081 .format_trace = format_lisp_cp_lookup_trace,
4082 .type = VLIB_NODE_TYPE_INTERNAL,
4083
4084 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4085 .error_strings = lisp_cp_lookup_error_strings,
4086
4087 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4088
4089 .next_nodes = {
4090 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Florin Corase127a7e2016-02-18 22:20:01 +01004091 },
4092};
Florin Corasa2157cf2016-08-16 21:09:14 +02004093/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01004094
4095/* lisp_cp_input statistics */
Florin Corasf727db92016-06-23 15:01:58 +02004096#define foreach_lisp_cp_input_error \
4097_(DROP, "drop") \
Florin Corase127a7e2016-02-18 22:20:01 +01004098_(MAP_REPLIES_RECEIVED, "map-replies received")
4099
Florin Corasa2157cf2016-08-16 21:09:14 +02004100static char *lisp_cp_input_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01004101#define _(sym,string) string,
4102 foreach_lisp_cp_input_error
4103#undef _
4104};
4105
4106typedef enum
4107{
4108#define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02004109 foreach_lisp_cp_input_error
Florin Corase127a7e2016-02-18 22:20:01 +01004110#undef _
4111 LISP_CP_INPUT_N_ERROR,
4112} lisp_cp_input_error_t;
4113
4114typedef enum
4115{
4116 LISP_CP_INPUT_NEXT_DROP,
4117 LISP_CP_INPUT_N_NEXT,
4118} lisp_cp_input_next_t;
4119
4120typedef struct
4121{
4122 gid_address_t dst_eid;
4123 ip4_address_t map_resolver_ip;
4124} lisp_cp_input_trace_t;
4125
4126u8 *
4127format_lisp_cp_input_trace (u8 * s, va_list * args)
4128{
4129 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
4130 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02004131 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
4132 va_arg (*args, lisp_cp_input_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01004133
4134 s = format (s, "LISP-CP-INPUT: TODO");
4135 return s;
4136}
4137
Filip Tehlar9677a942016-11-28 10:23:31 +01004138static void
4139remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
4140{
4141 mapping_t *m;
4142
4143 m = pool_elt_at_index (lcm->mapping_pool, mi);
4144 lisp_add_del_adjacency (lcm, 0, &m->eid, 0 /* is_add */ );
4145 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
4146 0 /* is_static */ , 0);
4147 mapping_delete_timer (lcm, mi);
4148}
4149
4150static void
4151mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
4152 f64 expiration_time)
4153{
4154 mapping_t *m;
4155 u64 now = clib_cpu_time_now ();
4156 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
4157 u64 exp_clock_time = now + expiration_time * cpu_cps;
4158
4159 m = pool_elt_at_index (lcm->mapping_pool, mi);
4160
4161 m->timer_set = 1;
4162 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
4163}
4164
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004165static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004166map_records_arg_free (map_records_arg_t * a)
Florin Corase127a7e2016-02-18 22:20:01 +01004167{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004168 mapping_t *m;
4169 vec_foreach (m, a->mappings)
4170 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004171 vec_free (m->locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004172 gid_address_free (&m->eid);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004173 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004174
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004175 clib_mem_free (a);
4176}
4177
4178void *
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004179process_map_reply (map_records_arg_t * a)
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004180{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004181 mapping_t *m;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004182 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4183 u32 dst_map_index = 0;
4184 pending_map_request_t *pmr;
4185 u64 *noncep;
4186 uword *pmr_index;
4187
4188 if (a->is_rloc_probe)
4189 goto done;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004190
Florin Corase127a7e2016-02-18 22:20:01 +01004191 /* Check pending requests table and nonce */
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004192 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
Florin Corase127a7e2016-02-18 22:20:01 +01004193 if (!pmr_index)
4194 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004195 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004196 goto done;
Florin Corase127a7e2016-02-18 22:20:01 +01004197 }
Florin Corasa2157cf2016-08-16 21:09:14 +02004198 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01004199
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004200 vec_foreach (m, a->mappings)
4201 {
4202 /* insert/update mappings cache */
4203 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
4204 m->authoritative, m->ttl,
4205 1, 0 /* is_static */ , &dst_map_index);
Florin Corase127a7e2016-02-18 22:20:01 +01004206
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004207 /* try to program forwarding only if mapping saved or updated */
4208 if ((u32) ~ 0 != dst_map_index)
4209 {
4210 lisp_add_del_adjacency (lcm, &pmr->src, &m->eid, 1);
4211 if ((u32) ~ 0 != m->ttl)
4212 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
4213 }
4214 }
Florin Corase127a7e2016-02-18 22:20:01 +01004215
4216 /* remove pending map request entry */
Florin Corasa2157cf2016-08-16 21:09:14 +02004217
4218 /* *INDENT-OFF* */
Florin Corasf4691cd2016-08-15 19:16:32 +02004219 clib_fifo_foreach (noncep, pmr->nonces, ({
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004220 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
Florin Corasf4691cd2016-08-15 19:16:32 +02004221 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02004222 /* *INDENT-ON* */
4223
4224 clib_fifo_free (pmr->nonces);
4225 pool_put (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004226
4227done:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004228 map_records_arg_free (a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004229 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01004230}
4231
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004232static int
4233is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
4234 lisp_key_type_t key_id, u8 * key)
4235{
4236 u8 *auth_data = 0;
4237 u16 auth_data_len;
4238 int result;
4239
4240 auth_data_len = auth_data_len_by_key_id (key_id);
4241 if ((u16) ~ 0 == auth_data_len)
4242 {
4243 clib_warning ("invalid length for key_id %d!", key_id);
4244 return 0;
4245 }
4246
4247 /* save auth data */
4248 vec_validate (auth_data, auth_data_len - 1);
4249 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
4250
4251 /* clear auth data */
4252 memset (MNOTIFY_DATA (h), 0, auth_data_len);
4253
4254 /* get hash of the message */
4255 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
4256 (unsigned char *) h, msg_len, NULL, NULL);
4257
4258 result = memcmp (code, auth_data, auth_data_len);
4259
4260 vec_free (auth_data);
4261
4262 return !result;
4263}
4264
4265static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004266process_map_notify (map_records_arg_t * a)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004267{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004268 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004269 uword *pmr_index;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004270
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004271 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004272 if (!pmr_index)
4273 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004274 clib_warning ("No pending map-register entry with nonce %lu!",
4275 a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004276 return;
4277 }
4278
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004279 map_records_arg_free (a);
4280 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
4281}
4282
4283static mapping_t *
4284get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
4285{
4286 u32 mi;
4287
4288 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
4289 if (~0 == mi)
4290 {
4291 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
4292 e);
4293 return 0;
4294 }
4295 return pool_elt_at_index (lcm->mapping_pool, mi);
4296}
4297
4298/**
4299 * When map-notify is received it is necessary that all EIDs in the record
4300 * list share common key. The key is then used to verify authentication
4301 * data in map-notify message.
4302 */
4303static int
4304map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
4305 u32 key_id, u8 ** key_out)
4306{
4307 u32 i, len = vec_len (maps);
4308 mapping_t *m;
4309
4310 /* get key of the first mapping */
4311 m = get_mapping (lcm, &maps[0].eid);
4312 if (!m || !m->key)
4313 return -1;
4314
4315 key_out[0] = m->key;
4316
4317 for (i = 1; i < len; i++)
4318 {
4319 m = get_mapping (lcm, &maps[i].eid);
4320 if (!m || !m->key)
4321 return -1;
4322
4323 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
4324 {
4325 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
4326 return -1;
4327 }
4328 }
4329 return 0;
4330}
4331
4332static int
4333parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
4334{
4335 locator_t *locators = 0;
4336 u32 i, len;
4337 gid_address_t deid;
4338 mapping_t m;
4339 locator_t *loc;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004340
4341 /* parse record eid */
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004342 for (i = 0; i < count; i++)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004343 {
4344 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
4345 if (len == ~0)
4346 {
4347 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004348 vec_foreach (loc, locators) locator_free (loc);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004349 vec_free (locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004350 return -1;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004351 }
4352
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004353 m.locators = locators;
4354 gid_address_copy (&m.eid, &deid);
4355 vec_add1 (a->mappings, m);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004356 }
4357
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004358 return 0;
4359}
4360
4361static map_records_arg_t *
4362parse_map_notify (vlib_buffer_t * b)
4363{
4364 int rc = 0;
4365 map_notify_hdr_t *mnotif_hdr;
4366 lisp_key_type_t key_id;
4367 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4368 u8 *key = 0;
4369 gid_address_t deid;
4370 u16 auth_data_len = 0;
4371 u8 record_count;
4372 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
4373
4374 memset (a, 0, sizeof (*a));
4375 mnotif_hdr = vlib_buffer_get_current (b);
4376 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
4377 memset (&deid, 0, sizeof (deid));
4378
4379 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
4380 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
4381 auth_data_len = auth_data_len_by_key_id (key_id);
4382
4383 /* advance buffer by authentication data */
4384 vlib_buffer_pull (b, auth_data_len);
4385
4386 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
4387 rc = parse_map_records (b, a, record_count);
4388 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004389 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004390 map_records_arg_free (a);
4391 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004392 }
4393
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004394 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
4395 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004396 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004397 map_records_arg_free (a);
4398 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004399 }
4400
4401 /* verify authentication data */
4402 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004403 - (u8 *) mnotif_hdr, key_id, key))
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004404 {
4405 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004406 a->nonce);
4407 map_records_arg_free (a);
4408 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004409 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004410 return a;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004411}
4412
4413static vlib_buffer_t *
4414build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
4415 ip_address_t * dst, u64 nonce, u8 probe_bit,
4416 mapping_t * records, u16 dst_port, u32 * bi_res)
4417{
4418 vlib_buffer_t *b;
4419 u32 bi;
4420 vlib_main_t *vm = lcm->vlib_main;
4421
4422 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
4423 {
4424 clib_warning ("Can't allocate buffer for Map-Register!");
4425 return 0;
4426 }
4427
4428 b = vlib_get_buffer (vm, bi);
4429
4430 /* leave some space for the encap headers */
4431 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
4432
4433 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
4434
4435 /* push outer ip header */
4436 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
4437
4438 bi_res[0] = bi;
4439 return b;
4440}
4441
4442static int
4443send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
4444 u8 probe_bit, u64 nonce, u16 dst_port,
4445 ip_address_t * probed_loc)
4446{
4447 ip_address_t src;
4448 u32 bi;
4449 vlib_buffer_t *b;
4450 vlib_frame_t *f;
4451 u32 next_index, *to_next;
4452 mapping_t *records = 0, *m;
4453
4454 m = pool_elt_at_index (lcm->mapping_pool, mi);
4455 if (!m)
4456 return -1;
4457
4458 vec_add1 (records, m[0]);
4459 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
4460 memset (&src, 0, sizeof (src));
4461
4462 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
4463 {
4464 clib_warning ("can't find inteface address for %U", format_ip_address,
4465 dst);
4466 return -1;
4467 }
4468
4469 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
4470 &bi);
4471 if (!b)
4472 return -1;
4473 free_map_register_records (records);
4474
4475 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
4476 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
4477 ip4_lookup_node.index : ip6_lookup_node.index;
4478
4479 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
4480
4481 /* Enqueue the packet */
4482 to_next = vlib_frame_vector_args (f);
4483 to_next[0] = bi;
4484 f->n_vectors = 1;
4485 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
4486 return 0;
4487}
4488
Florin Corase127a7e2016-02-18 22:20:01 +01004489void
Florin Corasa2157cf2016-08-16 21:09:14 +02004490process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm,
4491 vlib_buffer_t * b)
Florin Corase127a7e2016-02-18 22:20:01 +01004492{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004493 u8 *ip_hdr = 0, *udp_hdr;
4494 ip4_header_t *ip4;
4495 ip6_header_t *ip6;
4496 ip_address_t *dst_loc = 0, probed_loc, src_loc;
4497 mapping_t m;
Florin Corasa2157cf2016-08-16 21:09:14 +02004498 map_request_hdr_t *mreq_hdr;
Florin Corase127a7e2016-02-18 22:20:01 +01004499 gid_address_t src, dst;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004500 u64 nonce;
Florin Corase127a7e2016-02-18 22:20:01 +01004501 u32 i, len = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004502 gid_address_t *itr_rlocs = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01004503
4504 mreq_hdr = vlib_buffer_get_current (b);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004505
4506 // TODO ugly workaround to find out whether LISP is carried by ip4 or 6
4507 // and needs to be fixed
4508 udp_hdr = (u8 *) vlib_buffer_get_current (b) - sizeof (udp_header_t);
4509 ip4 = (ip4_header_t *) (udp_hdr - sizeof (ip4_header_t));
4510 ip6 = (ip6_header_t *) (udp_hdr - sizeof (ip6_header_t));
4511
4512 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
4513 ip_hdr = (u8 *) ip4;
4514 else
4515 {
4516 u32 flags = clib_net_to_host_u32
4517 (ip6->ip_version_traffic_class_and_flow_label);
4518 if ((flags & 0xF0000000) == 0x60000000)
4519 ip_hdr = (u8 *) ip6;
4520 else
4521 {
4522 clib_warning ("internal error: cannot determine whether packet "
4523 "is ip4 or 6!");
4524 return;
4525 }
4526 }
4527
Florin Corasa2157cf2016-08-16 21:09:14 +02004528 vlib_buffer_pull (b, sizeof (*mreq_hdr));
Florin Corase127a7e2016-02-18 22:20:01 +01004529
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004530 nonce = MREQ_NONCE (mreq_hdr);
Florin Corase127a7e2016-02-18 22:20:01 +01004531
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004532 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
Florin Corasa2157cf2016-08-16 21:09:14 +02004533 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004534 clib_warning
4535 ("Only SMR Map-Requests and RLOC probe supported for now!");
Florin Corase127a7e2016-02-18 22:20:01 +01004536 return;
Florin Corasa2157cf2016-08-16 21:09:14 +02004537 }
Florin Corase127a7e2016-02-18 22:20:01 +01004538
4539 /* parse src eid */
4540 len = lisp_msg_parse_addr (b, &src);
4541 if (len == ~0)
4542 return;
4543
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004544 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
4545 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
Florin Corase127a7e2016-02-18 22:20:01 +01004546 if (len == ~0)
4547 return;
4548
4549 /* parse eid records and send SMR-invoked map-requests */
Florin Corasa2157cf2016-08-16 21:09:14 +02004550 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01004551 {
Florin Corasa2157cf2016-08-16 21:09:14 +02004552 memset (&dst, 0, sizeof (dst));
Florin Corase127a7e2016-02-18 22:20:01 +01004553 len = lisp_msg_parse_eid_rec (b, &dst);
4554 if (len == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02004555 {
4556 clib_warning ("Can't parse map-request EID-record");
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004557 goto done;
Florin Corasa2157cf2016-08-16 21:09:14 +02004558 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004559
4560 if (MREQ_SMR (mreq_hdr))
4561 {
4562 /* send SMR-invoked map-requests */
4563 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
4564 }
4565 else if (MREQ_RLOC_PROBE (mreq_hdr))
4566 {
4567 memset (&m, 0, sizeof (m));
4568 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
4569
4570 // TODO: select best locator; for now use the first one
4571 dst_loc = &gid_address_ip (&itr_rlocs[0]);
4572
4573 /* get src/dst IP addresses */
4574 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
4575
4576 // TODO get source port from buffer
4577 u16 src_port = LISP_CONTROL_PORT;
4578
4579 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
4580 src_port, &probed_loc);
4581 }
Florin Corase127a7e2016-02-18 22:20:01 +01004582 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004583
4584done:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004585 vec_free (itr_rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01004586}
4587
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004588static map_records_arg_t *
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004589parse_map_reply (vlib_buffer_t * b)
4590{
4591 locator_t probed;
4592 gid_address_t deid;
4593 void *h;
4594 u32 i, len = 0;
4595 mapping_t m;
4596 map_reply_hdr_t *mrep_hdr;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004597 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004598 memset (a, 0, sizeof (*a));
4599 locator_t *locators;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004600
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004601 mrep_hdr = vlib_buffer_get_current (b);
4602 a->nonce = MREP_NONCE (mrep_hdr);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004603 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004604 vlib_buffer_pull (b, sizeof (*mrep_hdr));
4605
4606 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
4607 {
4608 memset (&m, 0, sizeof (m));
4609 locators = 0;
4610 h = vlib_buffer_get_current (b);
4611
4612 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
4613 m.action = MAP_REC_ACTION (h);
4614 m.authoritative = MAP_REC_AUTH (h);
4615
4616 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
4617 if (len == ~0)
4618 {
4619 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004620 map_records_arg_free (a);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004621 return 0;
4622 }
4623
4624 m.locators = locators;
4625 gid_address_copy (&m.eid, &deid);
4626 vec_add1 (a->mappings, m);
4627 }
4628 return a;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004629}
4630
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004631static void
4632queue_map_reply_for_processing (map_records_arg_t * a)
4633{
Florin Coras655fcc42017-01-10 08:57:54 -08004634 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004635}
4636
4637static void
4638queue_map_notify_for_processing (map_records_arg_t * a)
4639{
4640 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
4641}
4642
Florin Corase127a7e2016-02-18 22:20:01 +01004643static uword
4644lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Corasa2157cf2016-08-16 21:09:14 +02004645 vlib_frame_t * from_frame)
Florin Corase127a7e2016-02-18 22:20:01 +01004646{
Florin Corasa2157cf2016-08-16 21:09:14 +02004647 u32 n_left_from, *from, *to_next_drop;
Florin Corase127a7e2016-02-18 22:20:01 +01004648 lisp_msg_type_e type;
Florin Corasa2157cf2016-08-16 21:09:14 +02004649 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004650 map_records_arg_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01004651
4652 from = vlib_frame_vector_args (from_frame);
4653 n_left_from = from_frame->n_vectors;
4654
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004655
Florin Corase127a7e2016-02-18 22:20:01 +01004656 while (n_left_from > 0)
4657 {
4658 u32 n_left_to_next_drop;
4659
4660 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
Florin Corasa2157cf2016-08-16 21:09:14 +02004661 to_next_drop, n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01004662 while (n_left_from > 0 && n_left_to_next_drop > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02004663 {
4664 u32 bi0;
4665 vlib_buffer_t *b0;
Florin Corase127a7e2016-02-18 22:20:01 +01004666
Florin Corasa2157cf2016-08-16 21:09:14 +02004667 bi0 = from[0];
4668 from += 1;
4669 n_left_from -= 1;
4670 to_next_drop[0] = bi0;
4671 to_next_drop += 1;
4672 n_left_to_next_drop -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01004673
Florin Corasa2157cf2016-08-16 21:09:14 +02004674 b0 = vlib_get_buffer (vm, bi0);
Florin Corase127a7e2016-02-18 22:20:01 +01004675
Florin Corasa2157cf2016-08-16 21:09:14 +02004676 type = lisp_msg_type (vlib_buffer_get_current (b0));
4677 switch (type)
4678 {
4679 case LISP_MAP_REPLY:
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004680 a = parse_map_reply (b0);
4681 if (a)
4682 queue_map_reply_for_processing (a);
Florin Corasa2157cf2016-08-16 21:09:14 +02004683 break;
4684 case LISP_MAP_REQUEST:
4685 process_map_request (vm, lcm, b0);
4686 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004687 case LISP_MAP_NOTIFY:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004688 a = parse_map_notify (b0);
4689 if (a)
4690 queue_map_notify_for_processing (a);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004691 break;
Florin Corasa2157cf2016-08-16 21:09:14 +02004692 default:
4693 clib_warning ("Unsupported LISP message type %d", type);
4694 break;
4695 }
Florin Corase127a7e2016-02-18 22:20:01 +01004696
Florin Corasa2157cf2016-08-16 21:09:14 +02004697 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
Florin Corase127a7e2016-02-18 22:20:01 +01004698
Florin Corasa2157cf2016-08-16 21:09:14 +02004699 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
4700 {
Florin Corase127a7e2016-02-18 22:20:01 +01004701
Florin Corasa2157cf2016-08-16 21:09:14 +02004702 }
4703 }
Florin Corase127a7e2016-02-18 22:20:01 +01004704
Florin Corasa2157cf2016-08-16 21:09:14 +02004705 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4706 n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01004707 }
4708 return from_frame->n_vectors;
4709}
4710
Florin Corasa2157cf2016-08-16 21:09:14 +02004711/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01004712VLIB_REGISTER_NODE (lisp_cp_input_node) = {
4713 .function = lisp_cp_input,
4714 .name = "lisp-cp-input",
4715 .vector_size = sizeof (u32),
4716 .format_trace = format_lisp_cp_input_trace,
4717 .type = VLIB_NODE_TYPE_INTERNAL,
4718
4719 .n_errors = LISP_CP_INPUT_N_ERROR,
4720 .error_strings = lisp_cp_input_error_strings,
4721
4722 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
4723
4724 .next_nodes = {
4725 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
4726 },
4727};
Florin Corasa2157cf2016-08-16 21:09:14 +02004728/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01004729
4730clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02004731lisp_cp_init (vlib_main_t * vm)
Florin Corase127a7e2016-02-18 22:20:01 +01004732{
Florin Corasa2157cf2016-08-16 21:09:14 +02004733 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4734 clib_error_t *error = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01004735
4736 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
4737 return error;
4738
4739 lcm->im4 = &ip4_main;
4740 lcm->im6 = &ip6_main;
4741 lcm->vlib_main = vm;
Florin Corasa2157cf2016-08-16 21:09:14 +02004742 lcm->vnet_main = vnet_get_main ();
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02004743 lcm->mreq_itr_rlocs = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02004744 lcm->lisp_pitr = 0;
Florin Coras5a1c11b2016-09-06 16:29:34 +02004745 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
Florin Corase127a7e2016-02-18 22:20:01 +01004746
4747 gid_dictionary_init (&lcm->mapping_index_by_gid);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004748 lcm->do_map_resolver_election = 1;
Florin Corasdca88042016-09-14 16:01:38 +02004749 lcm->map_request_mode = MR_MODE_DST_ONLY;
Florin Corase127a7e2016-02-18 22:20:01 +01004750
Florin Coras577c3552016-04-21 00:45:40 +02004751 /* default vrf mapped to vni 0 */
Florin Corasa2157cf2016-08-16 21:09:14 +02004752 hash_set (lcm->table_id_by_vni, 0, 0);
4753 hash_set (lcm->vni_by_table_id, 0, 0);
Florin Coras577c3552016-04-21 00:45:40 +02004754
Florin Corase127a7e2016-02-18 22:20:01 +01004755 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
Florin Corasa2157cf2016-08-16 21:09:14 +02004756 lisp_cp_input_node.index, 1 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01004757 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
Florin Corasa2157cf2016-08-16 21:09:14 +02004758 lisp_cp_input_node.index, 0 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01004759
Filip Tehlar9677a942016-11-28 10:23:31 +01004760 u64 now = clib_cpu_time_now ();
4761 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
Florin Corase127a7e2016-02-18 22:20:01 +01004762 return 0;
4763}
4764
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004765static void *
Florin Corasa2157cf2016-08-16 21:09:14 +02004766send_map_request_thread_fn (void *arg)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004767{
Florin Corasa2157cf2016-08-16 21:09:14 +02004768 map_request_args_t *a = arg;
4769 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004770
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004771 if (a->is_resend)
4772 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4773 else
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004774 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004775
4776 return 0;
4777}
4778
4779static int
4780queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +02004781 u8 smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004782{
4783 map_request_args_t a;
4784
4785 a.is_resend = is_resend;
4786 gid_address_copy (&a.seid, seid);
4787 gid_address_copy (&a.deid, deid);
4788 a.smr_invoked = smr_invoked;
4789
4790 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
Florin Corasa2157cf2016-08-16 21:09:14 +02004791 (u8 *) & a, sizeof (a));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004792 return 0;
4793}
4794
4795/**
4796 * Take an action with a pending map request depending on expiration time
4797 * and re-try counters.
4798 */
4799static void
4800update_pending_request (pending_map_request_t * r, f64 dt)
4801{
Florin Corasa2157cf2016-08-16 21:09:14 +02004802 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004803 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004804
4805 if (r->time_to_expire - dt < 0)
4806 /* it's time to decide what to do with this pending request */
4807 {
4808 if (r->retries_num >= NUMBER_OF_RETRIES)
Florin Corasa2157cf2016-08-16 21:09:14 +02004809 /* too many retries -> assume current map resolver is not available */
4810 {
4811 mr = get_map_resolver (&lcm->active_map_resolver);
4812 if (!mr)
4813 {
4814 clib_warning ("Map resolver %U not found - probably deleted "
4815 "by the user recently.", format_ip_address,
4816 &lcm->active_map_resolver);
4817 }
4818 else
4819 {
4820 clib_warning ("map resolver %U is unreachable, ignoring",
4821 format_ip_address, &lcm->active_map_resolver);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004822
Florin Corasa2157cf2016-08-16 21:09:14 +02004823 /* mark current map resolver unavailable so it won't be
4824 * selected next time */
4825 mr->is_down = 1;
4826 mr->last_update = vlib_time_now (lcm->vlib_main);
4827 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004828
Florin Corasa2157cf2016-08-16 21:09:14 +02004829 reset_pending_mr_counters (r);
4830 elect_map_resolver (lcm);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004831
Florin Corasa2157cf2016-08-16 21:09:14 +02004832 /* try to find a next eligible map resolver and re-send */
4833 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4834 1 /* resend */ );
4835 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004836 else
Florin Corasa2157cf2016-08-16 21:09:14 +02004837 {
4838 /* try again */
4839 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4840 1 /* resend */ );
4841 r->retries_num++;
4842 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
4843 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004844 }
4845 else
4846 r->time_to_expire -= dt;
4847}
4848
4849static void
4850remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4851{
Florin Corasa2157cf2016-08-16 21:09:14 +02004852 u64 *nonce;
4853 pending_map_request_t *pmr;
4854 u32 *to_be_removed = 0, *pmr_index;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004855
Florin Corasa2157cf2016-08-16 21:09:14 +02004856 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004857 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02004858 ({
4859 if (pmr->to_be_removed)
4860 {
4861 clib_fifo_foreach (nonce, pmr->nonces, ({
4862 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4863 }));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004864
Florin Corasa2157cf2016-08-16 21:09:14 +02004865 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4866 }
4867 }));
4868 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004869
4870 vec_foreach (pmr_index, to_be_removed)
4871 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
4872
4873 vec_free (to_be_removed);
4874}
4875
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004876static void
4877update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4878{
4879 static f64 time_left = RLOC_PROBING_INTERVAL;
4880
4881 if (!lcm->is_enabled || !lcm->rloc_probing)
4882 return;
4883
4884 time_left -= dt;
4885 if (time_left <= 0)
4886 {
4887 time_left = RLOC_PROBING_INTERVAL;
4888 send_rloc_probes (lcm);
4889 }
4890}
4891
4892static void
4893update_map_register (lisp_cp_main_t * lcm, f64 dt)
4894{
4895 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4896 static u64 mreg_sent_counter = 0;
4897
4898 if (!lcm->is_enabled || !lcm->map_registering)
4899 return;
4900
4901 time_left -= dt;
4902 if (time_left <= 0)
4903 {
4904 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4905 time_left = MAP_REGISTER_INTERVAL;
4906 else
4907 {
4908 mreg_sent_counter++;
4909 time_left = QUICK_MAP_REGISTER_INTERVAL;
4910 }
4911 send_map_register (lcm, 1 /* want map notify */ );
4912 }
4913}
4914
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004915static uword
4916send_map_resolver_service (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02004917 vlib_node_runtime_t * rt, vlib_frame_t * f)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004918{
Filip Tehlar9677a942016-11-28 10:23:31 +01004919 u32 *expired = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004920 f64 period = 2.0;
Florin Corasa2157cf2016-08-16 21:09:14 +02004921 pending_map_request_t *pmr;
4922 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004923
4924 while (1)
4925 {
4926 vlib_process_wait_for_event_or_clock (vm, period);
4927
4928 /* currently no signals are expected - just wait for clock */
4929 (void) vlib_process_get_events (vm, 0);
4930
Florin Corasa2157cf2016-08-16 21:09:14 +02004931 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004932 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02004933 ({
4934 if (!pmr->to_be_removed)
4935 update_pending_request (pmr, period);
4936 }));
4937 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004938
4939 remove_dead_pending_map_requests (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004940
4941 update_map_register (lcm, period);
4942 update_rloc_probing (lcm, period);
Filip Tehlar9677a942016-11-28 10:23:31 +01004943
4944 u64 now = clib_cpu_time_now ();
4945
4946 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
4947 if (vec_len (expired) > 0)
4948 {
4949 u32 *mi = 0;
4950 vec_foreach (mi, expired)
4951 {
4952 remove_expired_mapping (lcm, mi[0]);
4953 }
4954 _vec_len (expired) = 0;
4955 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004956 }
4957
4958 /* unreachable */
4959 return 0;
4960}
4961
Florin Corasa2157cf2016-08-16 21:09:14 +02004962/* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004963VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
4964 .function = send_map_resolver_service,
4965 .type = VLIB_NODE_TYPE_PROCESS,
4966 .name = "lisp-retry-service",
4967 .process_log2_n_stack_bytes = 16,
4968};
Florin Corasa2157cf2016-08-16 21:09:14 +02004969/* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004970
Florin Corasa2157cf2016-08-16 21:09:14 +02004971VLIB_INIT_FUNCTION (lisp_cp_init);
4972
4973/*
4974 * fd.io coding-style-patch-verification: ON
4975 *
4976 * Local Variables:
4977 * eval: (c-set-style "gnu")
4978 * End:
4979 */