blob: de048a4144a6d087816c0366a22c2496163bc634 [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 Tehlar69a9b762016-09-23 10:00:52 +0200515 gid_address_copy (&fe->leid, &src_map->eid);
516 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
521/**
Filip Tehlar69a9b762016-09-23 10:00:52 +0200522 * Returns vector of adjacencies.
523 *
524 * The caller must free the vector returned by this function.
525 *
526 * @param vni virtual network identifier
527 * @return vector of adjacencies
528 */
529lisp_adjacency_t *
530vnet_lisp_adjacencies_get_by_vni (u32 vni)
531{
532 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
533 fwd_entry_t *fwd;
534 lisp_adjacency_t *adjs = 0, adj;
535
536 /* *INDENT-OFF* */
537 pool_foreach(fwd, lcm->fwd_entry_pool,
538 ({
539 if (gid_address_vni (&fwd->reid) != vni)
540 continue;
541
542 gid_address_copy (&adj.reid, &fwd->reid);
543 gid_address_copy (&adj.leid, &fwd->leid);
544 vec_add1 (adjs, adj);
545 }));
546 /* *INDENT-ON* */
547
548 return adjs;
549}
550
551static clib_error_t *
552lisp_show_adjacencies_command_fn (vlib_main_t * vm,
553 unformat_input_t * input,
554 vlib_cli_command_t * cmd)
555{
556 lisp_adjacency_t *adjs, *adj;
557 vlib_cli_output (vm, "%s %40s\n", "leid", "reid");
558 unformat_input_t _line_input, *line_input = &_line_input;
559 u32 vni = ~0;
560
561 /* Get a line of input. */
562 if (!unformat_user (input, unformat_line_input, line_input))
563 return 0;
564
565 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
566 {
567 if (unformat (line_input, "vni %d", &vni))
568 ;
569 else
570 {
571 vlib_cli_output (vm, "parse error: '%U'",
572 format_unformat_error, line_input);
573 return 0;
574 }
575 }
576
577 if (~0 == vni)
578 {
579 vlib_cli_output (vm, "error: no vni specified!");
580 return 0;
581 }
582
583 adjs = vnet_lisp_adjacencies_get_by_vni (vni);
584
585 vec_foreach (adj, adjs)
586 {
587 vlib_cli_output (vm, "%U %40U\n", format_gid_address, &adj->leid,
588 format_gid_address, &adj->reid);
589 }
590 vec_free (adjs);
591
592 return 0;
593}
594
595/* *INDENT-OFF* */
596VLIB_CLI_COMMAND (lisp_show_adjacencies_command) = {
597 .path = "show lisp adjacencies",
598 .short_help = "show lisp adjacencies",
599 .function = lisp_show_adjacencies_command_fn,
600};
601/* *INDENT-ON* */
602
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200603static lisp_msmr_t *
604get_map_server (ip_address_t * a)
605{
606 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
607 lisp_msmr_t *m;
608
609 vec_foreach (m, lcm->map_servers)
610 {
611 if (!ip_address_cmp (&m->address, a))
612 {
613 return m;
614 }
615 }
616 return 0;
617}
618
619static lisp_msmr_t *
620get_map_resolver (ip_address_t * a)
621{
622 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
623 lisp_msmr_t *m;
624
625 vec_foreach (m, lcm->map_resolvers)
626 {
627 if (!ip_address_cmp (&m->address, a))
628 {
629 return m;
630 }
631 }
632 return 0;
633}
634
635int
636vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
637{
638 u32 i;
639 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
640 lisp_msmr_t _ms, *ms = &_ms;
641
642 if (vnet_lisp_enable_disable_status () == 0)
643 {
644 clib_warning ("LISP is disabled!");
645 return VNET_API_ERROR_LISP_DISABLED;
646 }
647
648 if (is_add)
649 {
650 if (get_map_server (addr))
651 {
652 clib_warning ("map-server %U already exists!", format_ip_address,
653 addr);
654 return -1;
655 }
656
657 memset (ms, 0, sizeof (*ms));
658 ip_address_copy (&ms->address, addr);
659 vec_add1 (lcm->map_servers, ms[0]);
660 }
661 else
662 {
663 for (i = 0; i < vec_len (lcm->map_servers); i++)
664 {
665 ms = vec_elt_at_index (lcm->map_servers, i);
666 if (!ip_address_cmp (&ms->address, addr))
667 {
668 vec_del1 (lcm->map_servers, i);
669 break;
670 }
671 }
672 }
673
674 return 0;
675}
676
677static clib_error_t *
678lisp_add_del_map_server_command_fn (vlib_main_t * vm,
679 unformat_input_t * input,
680 vlib_cli_command_t * cmd)
681{
682 int rv = 0;
683 u8 is_add = 1, ip_set = 0;
684 ip_address_t ip;
685 unformat_input_t _line_input, *line_input = &_line_input;
686
687 /* Get a line of input. */
688 if (!unformat_user (input, unformat_line_input, line_input))
689 return 0;
690
691 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
692 {
693 if (unformat (line_input, "add"))
694 is_add = 1;
695 else if (unformat (line_input, "del"))
696 is_add = 0;
697 else if (unformat (line_input, "%U", unformat_ip_address, &ip))
698 ip_set = 1;
699 else
700 {
701 vlib_cli_output (vm, "parse error: '%U'",
702 format_unformat_error, line_input);
703 return 0;
704 }
705 }
706
707 if (!ip_set)
708 {
709 vlib_cli_output (vm, "map-server ip address not set!");
710 return 0;
711 }
712
713 rv = vnet_lisp_add_del_map_server (&ip, is_add);
714 if (!rv)
715 vlib_cli_output (vm, "failed to %s map-server!",
716 is_add ? "add" : "delete");
717
718 return 0;
719}
720
721/* *INDENT-OFF* */
722VLIB_CLI_COMMAND (lisp_add_del_map_server_command) = {
723 .path = "lisp map-server",
724 .short_help = "lisp map-server add|del <ip>",
725 .function = lisp_add_del_map_server_command_fn,
726};
727/* *INDENT-ON* */
728
Filip Tehlar69a9b762016-09-23 10:00:52 +0200729/**
Florin Corasf727db92016-06-23 15:01:58 +0200730 * Add/remove mapping to/from map-cache. Overwriting not allowed.
731 */
732int
733vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200734 u32 * map_index_result)
Florin Corase127a7e2016-02-18 22:20:01 +0100735{
Florin Corasa2157cf2016-08-16 21:09:14 +0200736 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
737 u32 mi, *map_indexp, map_index, i;
738 mapping_t *m, *old_map;
739 u32 **eid_indexes;
Florin Corase127a7e2016-02-18 22:20:01 +0100740
Florin Corasf727db92016-06-23 15:01:58 +0200741 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
Filip Tehlar53f09e32016-05-19 14:25:44 +0200742 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100743 if (a->is_add)
744 {
745 /* TODO check if overwriting and take appropriate actions */
Florin Corasa2157cf2016-08-16 21:09:14 +0200746 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
747 {
748 clib_warning ("eid %U found in the eid-table", format_gid_address,
749 &a->eid);
750 return VNET_API_ERROR_VALUE_EXIST;
751 }
Florin Corase127a7e2016-02-18 22:20:01 +0100752
Florin Corasa2157cf2016-08-16 21:09:14 +0200753 pool_get (lcm->mapping_pool, m);
Florin Corasf727db92016-06-23 15:01:58 +0200754 gid_address_copy (&m->eid, &a->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100755 m->locator_set_index = a->locator_set_index;
756 m->ttl = a->ttl;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200757 m->action = a->action;
Florin Corase127a7e2016-02-18 22:20:01 +0100758 m->local = a->local;
Filip Tehlar3cd9e732016-08-23 10:52:44 +0200759 m->is_static = a->is_static;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200760 m->key = vec_dup (a->key);
761 m->key_id = a->key_id;
Florin Corase127a7e2016-02-18 22:20:01 +0100762
763 map_index = m - lcm->mapping_pool;
Florin Corasf727db92016-06-23 15:01:58 +0200764 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200765 1);
Florin Corase127a7e2016-02-18 22:20:01 +0100766
Florin Corasa2157cf2016-08-16 21:09:14 +0200767 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
768 {
769 clib_warning ("Locator set with index %d doesn't exist",
770 a->locator_set_index);
771 return VNET_API_ERROR_INVALID_VALUE;
772 }
Florin Corase127a7e2016-02-18 22:20:01 +0100773
774 /* add eid to list of eids supported by locator-set */
775 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
Florin Corasa2157cf2016-08-16 21:09:14 +0200776 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
777 a->locator_set_index);
778 vec_add1 (eid_indexes[0], map_index);
Florin Corase127a7e2016-02-18 22:20:01 +0100779
780 if (a->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200781 {
782 /* mark as local */
783 vec_add1 (lcm->local_mappings_indexes, map_index);
784 }
Florin Corase127a7e2016-02-18 22:20:01 +0100785 map_index_result[0] = map_index;
786 }
787 else
788 {
Florin Coras577c3552016-04-21 00:45:40 +0200789 if (mi == GID_LOOKUP_MISS)
Florin Corasa2157cf2016-08-16 21:09:14 +0200790 {
791 clib_warning ("eid %U not found in the eid-table",
792 format_gid_address, &a->eid);
793 return VNET_API_ERROR_INVALID_VALUE;
794 }
Florin Corase127a7e2016-02-18 22:20:01 +0100795
796 /* clear locator-set to eids binding */
Florin Corasa2157cf2016-08-16 21:09:14 +0200797 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
798 a->locator_set_index);
799 for (i = 0; i < vec_len (eid_indexes[0]); i++)
800 {
801 map_indexp = vec_elt_at_index (eid_indexes[0], i);
802 if (map_indexp[0] == mi)
803 break;
804 }
805 vec_del1 (eid_indexes[0], i);
Florin Corase127a7e2016-02-18 22:20:01 +0100806
807 /* remove local mark if needed */
Florin Corasa2157cf2016-08-16 21:09:14 +0200808 m = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corase127a7e2016-02-18 22:20:01 +0100809 if (m->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200810 {
811 u32 k, *lm_indexp;
812 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
813 {
814 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
815 if (lm_indexp[0] == mi)
816 break;
817 }
818 vec_del1 (lcm->local_mappings_indexes, k);
819 }
Florin Corase127a7e2016-02-18 22:20:01 +0100820
821 /* remove mapping from dictionary */
Florin Corasf727db92016-06-23 15:01:58 +0200822 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
Filip Tehlar324112f2016-06-02 16:07:38 +0200823 gid_address_free (&m->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100824 pool_put_index (lcm->mapping_pool, mi);
825 }
826
827 return 0;
828}
829
Florin Corasf727db92016-06-23 15:01:58 +0200830/**
831 * Add/update/delete mapping to/in/from map-cache.
832 */
Florin Coras577c3552016-04-21 00:45:40 +0200833int
834vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200835 u32 * map_index_result)
Florin Coras577c3552016-04-21 00:45:40 +0200836{
Florin Corasa2157cf2016-08-16 21:09:14 +0200837 uword *dp_table = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200838 u32 vni;
Florin Coras1a1adc72016-07-22 01:45:30 +0200839 u8 type;
Florin Corasf727db92016-06-23 15:01:58 +0200840
Florin Corasa2157cf2016-08-16 21:09:14 +0200841 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Coras577c3552016-04-21 00:45:40 +0200842
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200843 if (vnet_lisp_enable_disable_status () == 0)
844 {
845 clib_warning ("LISP is disabled!");
846 return VNET_API_ERROR_LISP_DISABLED;
847 }
848
Florin Corasa2157cf2016-08-16 21:09:14 +0200849 vni = gid_address_vni (&a->eid);
850 type = gid_address_type (&a->eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200851 if (GID_ADDR_IP_PREFIX == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200852 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200853 else if (GID_ADDR_MAC == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200854 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras577c3552016-04-21 00:45:40 +0200855
Florin Coras1a1adc72016-07-22 01:45:30 +0200856 if (!dp_table)
Florin Coras577c3552016-04-21 00:45:40 +0200857 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200858 clib_warning ("vni %d not associated to a %s!", vni,
859 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
Florin Coras577c3552016-04-21 00:45:40 +0200860 return VNET_API_ERROR_INVALID_VALUE;
861 }
862
Florin Corasf727db92016-06-23 15:01:58 +0200863 /* store/remove mapping from map-cache */
864 return vnet_lisp_map_cache_add_del (a, map_index_result);
Florin Coras577c3552016-04-21 00:45:40 +0200865}
866
Florin Corase127a7e2016-02-18 22:20:01 +0100867static clib_error_t *
868lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
Florin Corasa2157cf2016-08-16 21:09:14 +0200869 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +0100870{
Florin Corasa2157cf2016-08-16 21:09:14 +0200871 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
872 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corase127a7e2016-02-18 22:20:01 +0100873 u8 is_add = 1;
874 gid_address_t eid;
Florin Corasa2157cf2016-08-16 21:09:14 +0200875 gid_address_t *eids = 0;
876 clib_error_t *error = 0;
877 u8 *locator_set_name = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100878 u32 locator_set_index = 0, map_index = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +0200879 uword *p;
880 vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200881 int rv = 0;
Filip Tehlar324112f2016-06-02 16:07:38 +0200882 u32 vni = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200883 u8 *key = 0;
884 u32 key_id = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100885
Filip Tehlar324112f2016-06-02 16:07:38 +0200886 memset (&eid, 0, sizeof (eid));
Filip Tehlar5ca5dc82016-08-02 19:32:25 +0200887 memset (a, 0, sizeof (*a));
888
Florin Corase127a7e2016-02-18 22:20:01 +0100889 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +0200890 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corase127a7e2016-02-18 22:20:01 +0100891 return 0;
892
893 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
894 {
895 if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +0200896 is_add = 1;
Florin Corase127a7e2016-02-18 22:20:01 +0100897 else if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +0200898 is_add = 0;
Filip Tehlar5ca5dc82016-08-02 19:32:25 +0200899 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
Florin Corasa2157cf2016-08-16 21:09:14 +0200900 ;
Filip Tehlar324112f2016-06-02 16:07:38 +0200901 else if (unformat (line_input, "vni %d", &vni))
Florin Corasa2157cf2016-08-16 21:09:14 +0200902 gid_address_vni (&eid) = vni;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200903 else if (unformat (line_input, "secret-key %_%v%_", &key))
904 ;
905 else if (unformat (line_input, "key-id %U", unformat_hmac_key_id,
906 &key_id))
907 ;
Florin Corase127a7e2016-02-18 22:20:01 +0100908 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +0200909 {
910 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
911 if (!p)
912 {
913 error = clib_error_return (0, "locator-set %s doesn't exist",
914 locator_set_name);
915 goto done;
916 }
917 locator_set_index = p[0];
918 }
Florin Corase127a7e2016-02-18 22:20:01 +0100919 else
Florin Corasa2157cf2016-08-16 21:09:14 +0200920 {
921 error = unformat_parse_error (line_input);
922 goto done;
923 }
Florin Corase127a7e2016-02-18 22:20:01 +0100924 }
Florin Corase127a7e2016-02-18 22:20:01 +0100925 /* XXX treat batch configuration */
Filip Tehlar324112f2016-06-02 16:07:38 +0200926
Florin Corasa2157cf2016-08-16 21:09:14 +0200927 if (GID_ADDR_SRC_DST == gid_address_type (&eid))
Filip Tehlar5ca5dc82016-08-02 19:32:25 +0200928 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200929 error =
930 clib_error_return (0, "src/dst is not supported for local EIDs!");
Filip Tehlar5ca5dc82016-08-02 19:32:25 +0200931 goto done;
932 }
933
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200934 if (key && (0 == key_id))
935 {
936 vlib_cli_output (vm, "invalid key_id!");
937 return 0;
938 }
939
Florin Corasa2157cf2016-08-16 21:09:14 +0200940 gid_address_copy (&a->eid, &eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100941 a->is_add = is_add;
942 a->locator_set_index = locator_set_index;
943 a->local = 1;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200944 a->key = key;
945 a->key_id = key_id;
Florin Corase127a7e2016-02-18 22:20:01 +0100946
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200947 rv = vnet_lisp_add_del_local_mapping (a, &map_index);
948 if (0 != rv)
Florin Corasa2157cf2016-08-16 21:09:14 +0200949 {
950 error = clib_error_return (0, "failed to %s local mapping!",
951 is_add ? "add" : "delete");
952 }
953done:
954 vec_free (eids);
Filip Tehlar53f09e32016-05-19 14:25:44 +0200955 if (locator_set_name)
956 vec_free (locator_set_name);
Florin Corasf727db92016-06-23 15:01:58 +0200957 gid_address_free (&a->eid);
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200958 vec_free (a->key);
Florin Corase127a7e2016-02-18 22:20:01 +0100959 return error;
960}
961
Florin Corasa2157cf2016-08-16 21:09:14 +0200962/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +0100963VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
964 .path = "lisp eid-table",
Filip Tehlar324112f2016-06-02 16:07:38 +0200965 .short_help = "lisp eid-table add/del [vni <vni>] eid <eid> "
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200966 "locator-set <locator-set> [key <secret-key> key-id sha1|sha256 ]",
Florin Corase127a7e2016-02-18 22:20:01 +0100967 .function = lisp_add_del_local_eid_command_fn,
968};
Florin Corasa2157cf2016-08-16 21:09:14 +0200969/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +0100970
Filip Tehlar324112f2016-06-02 16:07:38 +0200971int
Florin Coras1a1adc72016-07-22 01:45:30 +0200972vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
Filip Tehlar324112f2016-06-02 16:07:38 +0200973{
Florin Corasa2157cf2016-08-16 21:09:14 +0200974 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
975 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
Filip Tehlar324112f2016-06-02 16:07:38 +0200976
977 if (vnet_lisp_enable_disable_status () == 0)
978 {
979 clib_warning ("LISP is disabled!");
980 return -1;
981 }
982
Florin Coras1a1adc72016-07-22 01:45:30 +0200983 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
984 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
985
986 if (!is_l2 && (vni == 0 || dp_id == 0))
Filip Tehlar324112f2016-06-02 16:07:38 +0200987 {
988 clib_warning ("can't add/del default vni-vrf mapping!");
989 return -1;
990 }
991
Florin Coras1a1adc72016-07-22 01:45:30 +0200992 dp_idp = hash_get (dp_table_by_vni[0], vni);
993 vnip = hash_get (vni_by_dp_table[0], dp_id);
Filip Tehlar324112f2016-06-02 16:07:38 +0200994
995 if (is_add)
996 {
Florin Coras1a1adc72016-07-22 01:45:30 +0200997 if (dp_idp || vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +0200998 {
999 clib_warning ("vni %d or vrf %d already used in vrf/vni "
1000 "mapping!", vni, dp_id);
1001 return -1;
1002 }
Florin Coras1a1adc72016-07-22 01:45:30 +02001003 hash_set (dp_table_by_vni[0], vni, dp_id);
1004 hash_set (vni_by_dp_table[0], dp_id, vni);
Florin Corasf727db92016-06-23 15:01:58 +02001005
1006 /* create dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +02001007 dp_add_del_iface (lcm, vni, is_l2, 1);
Filip Tehlar324112f2016-06-02 16:07:38 +02001008 }
1009 else
1010 {
Florin Coras1a1adc72016-07-22 01:45:30 +02001011 if (!dp_idp || !vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +02001012 {
1013 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
1014 "mapping!", vni, dp_id);
1015 return -1;
1016 }
Florin Coras1a1adc72016-07-22 01:45:30 +02001017 hash_unset (dp_table_by_vni[0], vni);
1018 hash_unset (vni_by_dp_table[0], dp_id);
Florin Corasf727db92016-06-23 15:01:58 +02001019
1020 /* remove dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +02001021 dp_add_del_iface (lcm, vni, is_l2, 0);
Filip Tehlar324112f2016-06-02 16:07:38 +02001022 }
1023 return 0;
Florin Coras1a1adc72016-07-22 01:45:30 +02001024
Filip Tehlar324112f2016-06-02 16:07:38 +02001025}
1026
1027static clib_error_t *
1028lisp_eid_table_map_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001029 unformat_input_t * input,
1030 vlib_cli_command_t * cmd)
Filip Tehlar324112f2016-06-02 16:07:38 +02001031{
Florin Coras1a1adc72016-07-22 01:45:30 +02001032 u8 is_add = 1, is_l2 = 0;
1033 u32 vni = 0, dp_id = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001034 unformat_input_t _line_input, *line_input = &_line_input;
Filip Tehlar324112f2016-06-02 16:07:38 +02001035
1036 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02001037 if (!unformat_user (input, unformat_line_input, line_input))
Filip Tehlar324112f2016-06-02 16:07:38 +02001038 return 0;
1039
1040 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1041 {
1042 if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001043 is_add = 0;
Filip Tehlar324112f2016-06-02 16:07:38 +02001044 else if (unformat (line_input, "vni %d", &vni))
Florin Corasa2157cf2016-08-16 21:09:14 +02001045 ;
Florin Coras1a1adc72016-07-22 01:45:30 +02001046 else if (unformat (line_input, "vrf %d", &dp_id))
Florin Corasa2157cf2016-08-16 21:09:14 +02001047 ;
Florin Coras1a1adc72016-07-22 01:45:30 +02001048 else if (unformat (line_input, "bd %d", &dp_id))
Florin Corasa2157cf2016-08-16 21:09:14 +02001049 is_l2 = 1;
Filip Tehlar324112f2016-06-02 16:07:38 +02001050 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001051 {
1052 return unformat_parse_error (line_input);
1053 }
Filip Tehlar324112f2016-06-02 16:07:38 +02001054 }
Florin Coras1a1adc72016-07-22 01:45:30 +02001055 vnet_lisp_eid_table_map (vni, dp_id, is_l2, is_add);
Filip Tehlar324112f2016-06-02 16:07:38 +02001056 return 0;
1057}
1058
Florin Corasa2157cf2016-08-16 21:09:14 +02001059/* *INDENT-OFF* */
Filip Tehlar324112f2016-06-02 16:07:38 +02001060VLIB_CLI_COMMAND (lisp_eid_table_map_command) = {
1061 .path = "lisp eid-table map",
Florin Coras1a1adc72016-07-22 01:45:30 +02001062 .short_help = "lisp eid-table map [del] vni <vni> vrf <vrf> | bd <bdi>",
Filip Tehlar324112f2016-06-02 16:07:38 +02001063 .function = lisp_eid_table_map_command_fn,
1064};
Florin Corasa2157cf2016-08-16 21:09:14 +02001065/* *INDENT-ON* */
Florin Corasf727db92016-06-23 15:01:58 +02001066
1067/* return 0 if the two locator sets are identical 1 otherwise */
1068static u8
Florin Corasa2157cf2016-08-16 21:09:14 +02001069compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
1070 locator_t * new_locators)
Filip Tehlar53f09e32016-05-19 14:25:44 +02001071{
Florin Corasf727db92016-06-23 15:01:58 +02001072 u32 i, old_li;
Florin Corasa2157cf2016-08-16 21:09:14 +02001073 locator_t *old_loc, *new_loc;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001074
Florin Corasa2157cf2016-08-16 21:09:14 +02001075 if (vec_len (old_ls_indexes) != vec_len (new_locators))
Florin Corasf727db92016-06-23 15:01:58 +02001076 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001077
Florin Corasa2157cf2016-08-16 21:09:14 +02001078 for (i = 0; i < vec_len (new_locators); i++)
Filip Tehlar53f09e32016-05-19 14:25:44 +02001079 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001080 old_li = vec_elt (old_ls_indexes, i);
1081 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
Florin Corasf727db92016-06-23 15:01:58 +02001082
Florin Corasa2157cf2016-08-16 21:09:14 +02001083 new_loc = vec_elt_at_index (new_locators, i);
Florin Corasf727db92016-06-23 15:01:58 +02001084
1085 if (locator_cmp (old_loc, new_loc))
Florin Corasa2157cf2016-08-16 21:09:14 +02001086 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001087 }
Florin Corasf727db92016-06-23 15:01:58 +02001088 return 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001089}
1090
Filip Tehlard5fcc462016-10-17 16:20:18 +02001091typedef struct
1092{
1093 u8 is_negative;
1094 void *lcm;
1095 gid_address_t *eids_to_be_deleted;
1096} remove_mapping_args_t;
1097
1098/**
1099 * Callback invoked when a sub-prefix is found
1100 */
1101static void
1102remove_mapping_if_needed (u32 mi, void *arg)
1103{
1104 u8 delete = 0;
1105 remove_mapping_args_t *a = arg;
1106 lisp_cp_main_t *lcm = a->lcm;
1107 mapping_t *m;
1108 locator_set_t *ls;
1109
1110 m = pool_elt_at_index (lcm->mapping_pool, mi);
1111 if (!m)
1112 return;
1113
1114 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1115
1116 if (a->is_negative)
1117 {
1118 if (0 != vec_len (ls->locator_indices))
1119 delete = 1;
1120 }
1121 else
1122 {
1123 if (0 == vec_len (ls->locator_indices))
1124 delete = 1;
1125 }
1126
1127 if (delete)
1128 vec_add1 (a->eids_to_be_deleted, m->eid);
1129}
1130
1131/**
1132 * This function searches map cache and looks for IP prefixes that are subset
1133 * of the provided one. If such prefix is found depending on 'is_negative'
1134 * it does follows:
1135 *
1136 * 1) if is_negative is true and found prefix points to positive mapping,
1137 * then the mapping is removed
1138 * 2) if is_negative is false and found prefix points to negative mapping,
1139 * then the mapping is removed
1140 */
1141static void
1142remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1143 u8 is_negative)
1144{
1145 gid_address_t *e;
1146 remove_mapping_args_t a;
1147 memset (&a, 0, sizeof (a));
1148
1149 /* do this only in src/dst mode ... */
1150 if (MR_MODE_SRC_DST != lcm->map_request_mode)
1151 return;
1152
1153 /* ... and only for IP prefix */
1154 if (GID_ADDR_SRC_DST != gid_address_type (eid)
1155 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1156 return;
1157
1158 a.is_negative = is_negative;
1159 a.lcm = lcm;
1160
1161 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1162 remove_mapping_if_needed, &a);
1163
1164 vec_foreach (e, a.eids_to_be_deleted)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001165 {
1166 lisp_add_del_adjacency (lcm, 0, e, 0 /* is_add */ );
Filip Tehlard5fcc462016-10-17 16:20:18 +02001167 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001168 }
Filip Tehlard5fcc462016-10-17 16:20:18 +02001169
1170 vec_free (a.eids_to_be_deleted);
1171}
1172
Filip Tehlar9677a942016-11-28 10:23:31 +01001173static void
1174mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1175{
1176 timing_wheel_delete (&lcm->wheel, mi);
1177}
1178
Filip Tehlar195bcee2016-05-13 17:37:35 +02001179/**
Florin Corasf727db92016-06-23 15:01:58 +02001180 * Adds/removes/updates mapping. Does not program forwarding.
Filip Tehlar195bcee2016-05-13 17:37:35 +02001181 *
Florin Coras71893ac2016-07-10 20:09:32 +02001182 * @param eid end-host identifier
Filip Tehlar195bcee2016-05-13 17:37:35 +02001183 * @param rlocs vector of remote locators
1184 * @param action action for negative map-reply
1185 * @param is_add add mapping if non-zero, delete otherwise
Florin Coras71893ac2016-07-10 20:09:32 +02001186 * @param res_map_index the map-index that was created/updated/removed. It is
1187 * set to ~0 if no action is taken.
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001188 * @param is_static used for distinguishing between statically learned
1189 remote mappings and mappings obtained from MR
Filip Tehlar195bcee2016-05-13 17:37:35 +02001190 * @return return code
1191 */
1192int
Florin Coras71893ac2016-07-10 20:09:32 +02001193vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001194 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
Florin Corasa2157cf2016-08-16 21:09:14 +02001195 u32 * res_map_index)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001196{
Florin Corasa2157cf2016-08-16 21:09:14 +02001197 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1198 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1199 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corasf727db92016-06-23 15:01:58 +02001200 u32 mi, ls_index = 0, dst_map_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02001201 mapping_t *old_map;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001202
Florin Corasa2157cf2016-08-16 21:09:14 +02001203 if (vnet_lisp_enable_disable_status () == 0)
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001204 {
1205 clib_warning ("LISP is disabled!");
1206 return VNET_API_ERROR_LISP_DISABLED;
1207 }
1208
Florin Corasf727db92016-06-23 15:01:58 +02001209 if (res_map_index)
1210 res_map_index[0] = ~0;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001211
Florin Corasf727db92016-06-23 15:01:58 +02001212 memset (m_args, 0, sizeof (m_args[0]));
1213 memset (ls_args, 0, sizeof (ls_args[0]));
Filip Tehlar195bcee2016-05-13 17:37:35 +02001214
Florin Corasf727db92016-06-23 15:01:58 +02001215 ls_args->locators = rlocs;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001216
Florin Coras71893ac2016-07-10 20:09:32 +02001217 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
Florin Corasa2157cf2016-08-16 21:09:14 +02001218 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corasf727db92016-06-23 15:01:58 +02001219
1220 if (is_add)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001221 {
Florin Corasf727db92016-06-23 15:01:58 +02001222 /* overwrite: if mapping already exists, decide if locators should be
1223 * updated and be done */
Florin Coras71893ac2016-07-10 20:09:32 +02001224 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001225 {
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001226 if (!is_static && (old_map->is_static || old_map->local))
1227 {
1228 /* do not overwrite local or static remote mappings */
1229 clib_warning ("mapping %U rejected due to collision with local "
1230 "or static remote mapping!", format_gid_address,
Filip Tehlard5fcc462016-10-17 16:20:18 +02001231 eid);
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001232 return 0;
1233 }
1234
Florin Corasa2157cf2016-08-16 21:09:14 +02001235 locator_set_t *old_ls;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001236
Florin Corasa2157cf2016-08-16 21:09:14 +02001237 /* update mapping attributes */
1238 old_map->action = action;
1239 old_map->authoritative = authoritative;
1240 old_map->ttl = ttl;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001241
Florin Corasa2157cf2016-08-16 21:09:14 +02001242 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1243 old_map->locator_set_index);
1244 if (compare_locators (lcm, old_ls->locator_indices,
1245 ls_args->locators))
1246 {
1247 /* set locator-set index to overwrite */
1248 ls_args->is_add = 1;
1249 ls_args->index = old_map->locator_set_index;
1250 vnet_lisp_add_del_locator_set (ls_args, 0);
1251 if (res_map_index)
1252 res_map_index[0] = mi;
1253 }
1254 }
Florin Corasf727db92016-06-23 15:01:58 +02001255 /* new mapping */
1256 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001257 {
Filip Tehlard5fcc462016-10-17 16:20:18 +02001258 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1259
Florin Corasa2157cf2016-08-16 21:09:14 +02001260 ls_args->is_add = 1;
1261 ls_args->index = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001262
Florin Corasa2157cf2016-08-16 21:09:14 +02001263 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
Florin Corasf727db92016-06-23 15:01:58 +02001264
Florin Corasa2157cf2016-08-16 21:09:14 +02001265 /* add mapping */
1266 gid_address_copy (&m_args->eid, eid);
1267 m_args->is_add = 1;
1268 m_args->action = action;
1269 m_args->locator_set_index = ls_index;
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001270 m_args->is_static = is_static;
Filip Tehlar9677a942016-11-28 10:23:31 +01001271 m_args->ttl = ttl;
Florin Corasa2157cf2016-08-16 21:09:14 +02001272 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +02001273
Florin Corasa2157cf2016-08-16 21:09:14 +02001274 if (res_map_index)
1275 res_map_index[0] = dst_map_index;
1276 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001277 }
Florin Corasf727db92016-06-23 15:01:58 +02001278 else
1279 {
Florin Coras71893ac2016-07-10 20:09:32 +02001280 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001281 {
1282 clib_warning ("cannot delete mapping for eid %U",
1283 format_gid_address, eid);
1284 return -1;
1285 }
Florin Corasf727db92016-06-23 15:01:58 +02001286
1287 m_args->is_add = 0;
Florin Coras71893ac2016-07-10 20:09:32 +02001288 gid_address_copy (&m_args->eid, eid);
Florin Corasf727db92016-06-23 15:01:58 +02001289 m_args->locator_set_index = old_map->locator_set_index;
1290
1291 /* delete mapping associated from map-cache */
1292 vnet_lisp_map_cache_add_del (m_args, 0);
1293
1294 ls_args->is_add = 0;
1295 ls_args->index = old_map->locator_set_index;
1296 /* delete locator set */
1297 vnet_lisp_add_del_locator_set (ls_args, 0);
Filip Tehlarb93222f2016-07-07 09:58:08 +02001298
Filip Tehlar9677a942016-11-28 10:23:31 +01001299 /* delete timer associated to the mapping if any */
1300 if (old_map->timer_set)
1301 mapping_delete_timer (lcm, mi);
1302
Filip Tehlarb93222f2016-07-07 09:58:08 +02001303 /* return old mapping index */
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001304 if (res_map_index)
Florin Corasa2157cf2016-08-16 21:09:14 +02001305 res_map_index[0] = mi;
Florin Corasf727db92016-06-23 15:01:58 +02001306 }
1307
Filip Tehlar195bcee2016-05-13 17:37:35 +02001308 /* success */
Florin Corasf727db92016-06-23 15:01:58 +02001309 return 0;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001310}
1311
Filip Tehlar58f886a2016-05-30 15:57:40 +02001312int
Florin Corasf727db92016-06-23 15:01:58 +02001313vnet_lisp_clear_all_remote_adjacencies (void)
Filip Tehlar58f886a2016-05-30 15:57:40 +02001314{
1315 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001316 u32 mi, *map_indices = 0, *map_indexp;
1317 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1318 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1319 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001320
Florin Corasa2157cf2016-08-16 21:09:14 +02001321 /* *INDENT-OFF* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001322 pool_foreach_index (mi, lcm->mapping_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02001323 ({
1324 vec_add1 (map_indices, mi);
1325 }));
1326 /* *INDENT-ON* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001327
1328 vec_foreach (map_indexp, map_indices)
Florin Corasa2157cf2016-08-16 21:09:14 +02001329 {
1330 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1331 if (!map->local)
1332 {
1333 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001334
Florin Corasa2157cf2016-08-16 21:09:14 +02001335 dm_args->is_add = 0;
1336 gid_address_copy (&dm_args->eid, &map->eid);
1337 dm_args->locator_set_index = map->locator_set_index;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001338
Florin Corasa2157cf2016-08-16 21:09:14 +02001339 /* delete mapping associated to fwd entry */
1340 vnet_lisp_map_cache_add_del (dm_args, 0);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001341
Florin Corasa2157cf2016-08-16 21:09:14 +02001342 ls->is_add = 0;
1343 ls->local = 0;
1344 ls->index = map->locator_set_index;
1345 /* delete locator set */
1346 rv = vnet_lisp_add_del_locator_set (ls, 0);
1347 if (rv != 0)
1348 goto cleanup;
1349 }
1350 }
Filip Tehlar58f886a2016-05-30 15:57:40 +02001351
1352cleanup:
1353 if (map_indices)
1354 vec_free (map_indices);
1355 return rv;
1356}
1357
Filip Tehlar195bcee2016-05-13 17:37:35 +02001358/**
Florin Coras71893ac2016-07-10 20:09:32 +02001359 * Adds adjacency or removes forwarding entry associated to remote mapping.
1360 * Note that adjacencies are not stored, they only result in forwarding entries
1361 * being created.
Florin Corasf727db92016-06-23 15:01:58 +02001362 */
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001363static int
Florin Coras71893ac2016-07-10 20:09:32 +02001364lisp_add_del_adjacency (lisp_cp_main_t * lcm, gid_address_t * local_eid,
Florin Corasa2157cf2016-08-16 21:09:14 +02001365 gid_address_t * remote_eid, u8 is_add)
Florin Corasf727db92016-06-23 15:01:58 +02001366{
Florin Coras71893ac2016-07-10 20:09:32 +02001367 u32 local_mi, remote_mi = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001368
1369 if (vnet_lisp_enable_disable_status () == 0)
1370 {
1371 clib_warning ("LISP is disabled!");
1372 return VNET_API_ERROR_LISP_DISABLED;
1373 }
1374
Filip Tehlar69a9b762016-09-23 10:00:52 +02001375 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
1376 remote_eid, local_eid);
Florin Coras71893ac2016-07-10 20:09:32 +02001377 if (GID_LOOKUP_MISS == remote_mi)
1378 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001379 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
1380 format_gid_address, remote_eid);
Florin Corasf727db92016-06-23 15:01:58 +02001381
Florin Coras71893ac2016-07-10 20:09:32 +02001382 return -1;
1383 }
1384
1385 if (is_add)
Florin Corasf727db92016-06-23 15:01:58 +02001386 {
1387 /* TODO 1) check if src/dst 2) once we have src/dst working, use it in
1388 * delete*/
1389
1390 /* check if source eid has an associated mapping. If pitr mode is on,
1391 * just use the pitr's mapping */
Florin Coras71893ac2016-07-10 20:09:32 +02001392 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
Florin Corasa2157cf2016-08-16 21:09:14 +02001393 gid_dictionary_lookup (&lcm->mapping_index_by_gid, local_eid);
Florin Corasf727db92016-06-23 15:01:58 +02001394
1395
Florin Coras71893ac2016-07-10 20:09:32 +02001396 if (GID_LOOKUP_MISS == local_mi)
Florin Corasa2157cf2016-08-16 21:09:14 +02001397 {
1398 clib_warning ("Local eid %U not found. Cannot add adjacency!",
1399 format_gid_address, local_eid);
Florin Corasf727db92016-06-23 15:01:58 +02001400
Florin Corasa2157cf2016-08-16 21:09:14 +02001401 return -1;
1402 }
Florin Corasf727db92016-06-23 15:01:58 +02001403
Florin Coras71893ac2016-07-10 20:09:32 +02001404 /* update forwarding */
1405 dp_add_fwd_entry (lcm, local_mi, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001406 }
1407 else
Florin Coras71893ac2016-07-10 20:09:32 +02001408 dp_del_fwd_entry (lcm, 0, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001409
1410 return 0;
1411}
1412
1413int
1414vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
1415{
Florin Corasa2157cf2016-08-16 21:09:14 +02001416 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001417 return lisp_add_del_adjacency (lcm, &a->leid, &a->reid, a->is_add);
Florin Corasf727db92016-06-23 15:01:58 +02001418}
1419
1420/**
Filip Tehlar195bcee2016-05-13 17:37:35 +02001421 * Handler for add/del remote mapping CLI.
1422 *
1423 * @param vm vlib context
1424 * @param input input from user
1425 * @param cmd cmd
1426 * @return pointer to clib error structure
1427 */
1428static clib_error_t *
1429lisp_add_del_remote_mapping_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001430 unformat_input_t * input,
1431 vlib_cli_command_t * cmd)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001432{
Florin Corasa2157cf2016-08-16 21:09:14 +02001433 clib_error_t *error = 0;
1434 unformat_input_t _line_input, *line_input = &_line_input;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001435 u8 is_add = 1, del_all = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001436 locator_t rloc, *rlocs = 0, *curr_rloc = 0;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001437 gid_address_t eid;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001438 u8 eid_set = 0;
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02001439 u32 vni, action = ~0, p, w;
Florin Corasf727db92016-06-23 15:01:58 +02001440 int rv;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001441
1442 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02001443 if (!unformat_user (input, unformat_line_input, line_input))
Filip Tehlar195bcee2016-05-13 17:37:35 +02001444 return 0;
1445
Florin Corasa2157cf2016-08-16 21:09:14 +02001446 memset (&eid, 0, sizeof (eid));
1447 memset (&rloc, 0, sizeof (rloc));
Filip Tehlar195bcee2016-05-13 17:37:35 +02001448
Filip Tehlar195bcee2016-05-13 17:37:35 +02001449 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1450 {
Filip Tehlar58f886a2016-05-30 15:57:40 +02001451 if (unformat (line_input, "del-all"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001452 del_all = 1;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001453 else if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001454 is_add = 0;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001455 else if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001456 ;
Florin Corasbb5c22f2016-08-02 02:31:03 +02001457 else if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
Florin Corasa2157cf2016-08-16 21:09:14 +02001458 eid_set = 1;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001459 else if (unformat (line_input, "vni %u", &vni))
Florin Corasa2157cf2016-08-16 21:09:14 +02001460 {
1461 gid_address_vni (&eid) = vni;
1462 }
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02001463 else if (unformat (line_input, "p %d w %d", &p, &w))
Florin Corasa2157cf2016-08-16 21:09:14 +02001464 {
1465 if (!curr_rloc)
1466 {
1467 clib_warning
1468 ("No RLOC configured for setting priority/weight!");
1469 goto done;
1470 }
1471 curr_rloc->priority = p;
1472 curr_rloc->weight = w;
1473 }
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02001474 else if (unformat (line_input, "rloc %U", unformat_ip_address,
Florin Corasa2157cf2016-08-16 21:09:14 +02001475 &gid_address_ip (&rloc.address)))
1476 {
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02001477 /* since rloc is stored in ip prefix we need to set prefix length */
1478 ip_prefix_t *pref = &gid_address_ippref (&rloc.address);
1479
1480 u8 version = gid_address_ip_version (&rloc.address);
1481 ip_prefix_len (pref) = ip_address_max_len (version);
1482
Florin Corasa2157cf2016-08-16 21:09:14 +02001483 vec_add1 (rlocs, rloc);
1484 curr_rloc = &rlocs[vec_len (rlocs) - 1];
1485 }
Florin Corasf7643fd2016-08-01 15:35:20 +02001486 else if (unformat (line_input, "action %U",
Florin Corasa2157cf2016-08-16 21:09:14 +02001487 unformat_negative_mapping_action, &action))
1488 ;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001489 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001490 {
1491 clib_warning ("parse error");
1492 goto done;
1493 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001494 }
1495
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001496 if (!eid_set)
1497 {
1498 clib_warning ("missing eid!");
1499 goto done;
1500 }
1501
Filip Tehlar58f886a2016-05-30 15:57:40 +02001502 if (!del_all)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001503 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001504 if (is_add && (~0 == action) && 0 == vec_len (rlocs))
1505 {
1506 clib_warning ("no action set for negative map-reply!");
1507 goto done;
1508 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001509 }
Florin Corasf727db92016-06-23 15:01:58 +02001510 else
1511 {
1512 vnet_lisp_clear_all_remote_adjacencies ();
1513 goto done;
1514 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001515
Florin Corasa2157cf2016-08-16 21:09:14 +02001516 /* TODO build src/dst with seid */
Florin Corasf727db92016-06-23 15:01:58 +02001517
1518 /* if it's a delete, clean forwarding */
1519 if (!is_add)
1520 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001521 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001522 rv = lisp_add_del_adjacency (lcm, 0, &eid, /* is_add */ 0);
1523 if (rv)
Florin Corasa2157cf2016-08-16 21:09:14 +02001524 {
1525 goto done;
1526 }
Florin Corasf727db92016-06-23 15:01:58 +02001527 }
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001528
1529 /* add as static remote mapping, i.e., not authoritative and infinite
1530 * ttl */
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001531 rv = vnet_lisp_add_del_mapping (&eid, rlocs, action, 0, ~0, is_add,
1532 1 /* is_static */ , 0);
Florin Corasf727db92016-06-23 15:01:58 +02001533
Filip Tehlar195bcee2016-05-13 17:37:35 +02001534 if (rv)
Florin Corasa2157cf2016-08-16 21:09:14 +02001535 clib_warning ("failed to %s remote mapping!", is_add ? "add" : "delete");
Filip Tehlar195bcee2016-05-13 17:37:35 +02001536
1537done:
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02001538 vec_free (rlocs);
Filip Tehlar195bcee2016-05-13 17:37:35 +02001539 unformat_free (line_input);
1540 return error;
1541}
1542
Florin Corasa2157cf2016-08-16 21:09:14 +02001543VLIB_CLI_COMMAND (lisp_add_del_remote_mapping_command) =
1544{
1545.path = "lisp remote-mapping",.short_help =
1546 "lisp remote-mapping add|del [del-all] vni <vni> "
1547 "eid <est-eid> [action <no-action|natively-forward|"
1548 "send-map-request|drop>] rloc <dst-locator> p <prio> w <weight> "
1549 "[rloc <dst-locator> ... ]",.function =
1550 lisp_add_del_remote_mapping_command_fn,};
Filip Tehlar195bcee2016-05-13 17:37:35 +02001551
Florin Corasf727db92016-06-23 15:01:58 +02001552/**
1553 * Handler for add/del adjacency CLI.
1554 */
1555static clib_error_t *
1556lisp_add_del_adjacency_command_fn (vlib_main_t * vm, unformat_input_t * input,
Florin Corasa2157cf2016-08-16 21:09:14 +02001557 vlib_cli_command_t * cmd)
Florin Corasf727db92016-06-23 15:01:58 +02001558{
Florin Corasa2157cf2016-08-16 21:09:14 +02001559 clib_error_t *error = 0;
1560 unformat_input_t _line_input, *line_input = &_line_input;
1561 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
Florin Corasf727db92016-06-23 15:01:58 +02001562 u8 is_add = 1;
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001563 ip_prefix_t *reid_ippref, *leid_ippref;
1564 gid_address_t leid, reid;
1565 u8 *dmac = gid_address_mac (&reid);
1566 u8 *smac = gid_address_mac (&leid);
1567 u8 reid_set = 0, leid_set = 0;
1568 u32 vni;
Florin Corasf727db92016-06-23 15:01:58 +02001569 int rv;
1570
1571 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02001572 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corasf727db92016-06-23 15:01:58 +02001573 return 0;
1574
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001575 memset (&reid, 0, sizeof (reid));
1576 memset (&leid, 0, sizeof (leid));
Florin Corasf727db92016-06-23 15:01:58 +02001577
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001578 leid_ippref = &gid_address_ippref (&leid);
1579 reid_ippref = &gid_address_ippref (&reid);
Florin Corasf727db92016-06-23 15:01:58 +02001580
1581 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1582 {
1583 if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001584 is_add = 0;
Florin Corasf727db92016-06-23 15:01:58 +02001585 else if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001586 ;
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001587 else if (unformat (line_input, "reid %U",
1588 unformat_ip_prefix, reid_ippref))
Florin Corasa2157cf2016-08-16 21:09:14 +02001589 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001590 gid_address_type (&reid) = GID_ADDR_IP_PREFIX;
1591 reid_set = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001592 }
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001593 else if (unformat (line_input, "reid %U", unformat_mac_address, dmac))
Florin Corasa2157cf2016-08-16 21:09:14 +02001594 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001595 gid_address_type (&reid) = GID_ADDR_MAC;
1596 reid_set = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001597 }
Florin Corasf727db92016-06-23 15:01:58 +02001598 else if (unformat (line_input, "vni %u", &vni))
Florin Corasa2157cf2016-08-16 21:09:14 +02001599 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001600 gid_address_vni (&leid) = vni;
1601 gid_address_vni (&reid) = vni;
Florin Corasa2157cf2016-08-16 21:09:14 +02001602 }
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001603 else if (unformat (line_input, "leid %U",
1604 unformat_ip_prefix, leid_ippref))
Florin Corasa2157cf2016-08-16 21:09:14 +02001605 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001606 gid_address_type (&leid) = GID_ADDR_IP_PREFIX;
1607 leid_set = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001608 }
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001609 else if (unformat (line_input, "leid %U", unformat_mac_address, smac))
Florin Corasa2157cf2016-08-16 21:09:14 +02001610 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001611 gid_address_type (&leid) = GID_ADDR_MAC;
1612 leid_set = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001613 }
Florin Corasf727db92016-06-23 15:01:58 +02001614 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001615 {
1616 clib_warning ("parse error");
1617 goto done;
1618 }
Florin Corasf727db92016-06-23 15:01:58 +02001619 }
1620
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001621 if (!reid_set || !leid_set)
Florin Corasf727db92016-06-23 15:01:58 +02001622 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001623 clib_warning ("missing remote or local eid!");
Florin Corasf727db92016-06-23 15:01:58 +02001624 goto done;
1625 }
1626
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001627 if ((gid_address_type (&leid) != gid_address_type (&reid))
1628 || (gid_address_type (&reid) == GID_ADDR_IP_PREFIX
1629 && ip_prefix_version (reid_ippref)
1630 != ip_prefix_version (leid_ippref)))
Florin Corasf727db92016-06-23 15:01:58 +02001631 {
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001632 clib_warning ("remote and local EIDs are of different types!");
1633 return error;
Florin Corasf727db92016-06-23 15:01:58 +02001634 }
1635
Florin Corasa2157cf2016-08-16 21:09:14 +02001636 memset (a, 0, sizeof (a[0]));
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001637 gid_address_copy (&a->leid, &leid);
1638 gid_address_copy (&a->reid, &reid);
Florin Coras71893ac2016-07-10 20:09:32 +02001639
Florin Corasf727db92016-06-23 15:01:58 +02001640 a->is_add = is_add;
Florin Corasf727db92016-06-23 15:01:58 +02001641 rv = vnet_lisp_add_del_adjacency (a);
1642
1643 if (rv)
Florin Corasa2157cf2016-08-16 21:09:14 +02001644 clib_warning ("failed to %s adjacency!", is_add ? "add" : "delete");
Florin Corasf727db92016-06-23 15:01:58 +02001645
1646done:
1647 unformat_free (line_input);
Florin Corasf727db92016-06-23 15:01:58 +02001648 return error;
1649}
1650
Florin Corasa2157cf2016-08-16 21:09:14 +02001651/* *INDENT-OFF* */
Florin Corasf727db92016-06-23 15:01:58 +02001652VLIB_CLI_COMMAND (lisp_add_del_adjacency_command) = {
1653 .path = "lisp adjacency",
Filip Tehlar2fdaece2016-09-28 14:27:59 +02001654 .short_help = "lisp adjacency add|del vni <vni> reid <remote-eid> "
1655 "leid <local-eid>",
Florin Corasf727db92016-06-23 15:01:58 +02001656 .function = lisp_add_del_adjacency_command_fn,
1657};
Florin Corasa2157cf2016-08-16 21:09:14 +02001658/* *INDENT-ON* */
Florin Corasf727db92016-06-23 15:01:58 +02001659
Florin Corasdca88042016-09-14 16:01:38 +02001660int
1661vnet_lisp_set_map_request_mode (u8 mode)
1662{
1663 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1664
1665 if (vnet_lisp_enable_disable_status () == 0)
1666 {
1667 clib_warning ("LISP is disabled!");
1668 return VNET_API_ERROR_LISP_DISABLED;
1669 }
1670
1671 if (mode >= _MR_MODE_MAX)
1672 {
1673 clib_warning ("Invalid LISP map request mode %d!", mode);
1674 return VNET_API_ERROR_INVALID_ARGUMENT;
1675 }
1676
1677 lcm->map_request_mode = mode;
1678 return 0;
1679}
1680
1681static clib_error_t *
1682lisp_map_request_mode_command_fn (vlib_main_t * vm,
1683 unformat_input_t * input,
1684 vlib_cli_command_t * cmd)
1685{
1686 unformat_input_t _i, *i = &_i;
1687 map_request_mode_t mr_mode = _MR_MODE_MAX;
1688
1689 /* Get a line of input. */
1690 if (!unformat_user (input, unformat_line_input, i))
1691 return 0;
1692
1693 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1694 {
1695 if (unformat (i, "dst-only"))
1696 mr_mode = MR_MODE_DST_ONLY;
1697 else if (unformat (i, "src-dst"))
1698 mr_mode = MR_MODE_SRC_DST;
1699 else
1700 {
1701 clib_warning ("parse error '%U'", format_unformat_error, i);
1702 goto done;
1703 }
1704 }
1705
1706 if (_MR_MODE_MAX == mr_mode)
1707 {
1708 clib_warning ("No LISP map request mode entered!");
1709 return 0;
1710 }
1711
1712 vnet_lisp_set_map_request_mode (mr_mode);
1713done:
1714 return 0;
1715}
1716
1717/* *INDENT-OFF* */
1718VLIB_CLI_COMMAND (lisp_map_request_mode_command) = {
1719 .path = "lisp map-request mode",
1720 .short_help = "lisp map-request mode dst-only|src-dst",
1721 .function = lisp_map_request_mode_command_fn,
1722};
1723/* *INDENT-ON* */
1724
1725static u8 *
1726format_lisp_map_request_mode (u8 * s, va_list * args)
1727{
1728 u32 mode = va_arg (*args, u32);
1729
1730 switch (mode)
1731 {
1732 case 0:
1733 return format (0, "dst-only");
1734 case 1:
1735 return format (0, "src-dst");
1736 }
1737 return 0;
1738}
1739
1740static clib_error_t *
1741lisp_show_map_request_mode_command_fn (vlib_main_t * vm,
1742 unformat_input_t * input,
1743 vlib_cli_command_t * cmd)
1744{
1745 vlib_cli_output (vm, "map-request mode: %U", format_lisp_map_request_mode,
1746 vnet_lisp_get_map_request_mode ());
1747 return 0;
1748}
1749
1750/* *INDENT-OFF* */
1751VLIB_CLI_COMMAND (lisp_show_map_request_mode_command) = {
1752 .path = "show lisp map-request mode",
1753 .short_help = "show lisp map-request mode",
1754 .function = lisp_show_map_request_mode_command_fn,
1755};
1756/* *INDENT-ON* */
1757
Filip Tehlarf747cd82016-05-26 16:47:11 +02001758static clib_error_t *
1759lisp_show_map_resolvers_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001760 unformat_input_t * input,
1761 vlib_cli_command_t * cmd)
Filip Tehlarf747cd82016-05-26 16:47:11 +02001762{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001763 lisp_msmr_t *mr;
Florin Corasa2157cf2016-08-16 21:09:14 +02001764 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarf747cd82016-05-26 16:47:11 +02001765
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001766 vec_foreach (mr, lcm->map_resolvers)
Florin Corasa2157cf2016-08-16 21:09:14 +02001767 {
1768 vlib_cli_output (vm, "%U", format_ip_address, &mr->address);
1769 }
Filip Tehlarf747cd82016-05-26 16:47:11 +02001770 return 0;
1771}
1772
Florin Corasa2157cf2016-08-16 21:09:14 +02001773/* *INDENT-OFF* */
Filip Tehlarf747cd82016-05-26 16:47:11 +02001774VLIB_CLI_COMMAND (lisp_show_map_resolvers_command) = {
1775 .path = "show lisp map-resolvers",
1776 .short_help = "show lisp map-resolvers",
1777 .function = lisp_show_map_resolvers_command_fn,
1778};
Florin Corasa2157cf2016-08-16 21:09:14 +02001779/* *INDENT-ON* */
Filip Tehlarf747cd82016-05-26 16:47:11 +02001780
Filip Tehlar53f09e32016-05-19 14:25:44 +02001781int
1782vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1783{
Florin Corasa2157cf2016-08-16 21:09:14 +02001784 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar53f09e32016-05-19 14:25:44 +02001785 u32 locator_set_index = ~0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001786 mapping_t *m;
1787 uword *p;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001788
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001789 if (vnet_lisp_enable_disable_status () == 0)
1790 {
1791 clib_warning ("LISP is disabled!");
1792 return VNET_API_ERROR_LISP_DISABLED;
1793 }
1794
Filip Tehlar53f09e32016-05-19 14:25:44 +02001795 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1796 if (!p)
1797 {
1798 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1799 return -1;
1800 }
1801 locator_set_index = p[0];
1802
1803 if (is_add)
1804 {
1805 pool_get (lcm->mapping_pool, m);
1806 m->locator_set_index = locator_set_index;
1807 m->local = 1;
1808 lcm->pitr_map_index = m - lcm->mapping_pool;
1809
1810 /* enable pitr mode */
1811 lcm->lisp_pitr = 1;
1812 }
1813 else
1814 {
1815 /* remove pitr mapping */
1816 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1817
1818 /* disable pitr mode */
1819 lcm->lisp_pitr = 0;
1820 }
1821 return 0;
1822}
1823
1824static clib_error_t *
1825lisp_pitr_set_locator_set_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001826 unformat_input_t * input,
1827 vlib_cli_command_t * cmd)
Filip Tehlar53f09e32016-05-19 14:25:44 +02001828{
1829 u8 locator_name_set = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001830 u8 *locator_set_name = 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001831 u8 is_add = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001832 unformat_input_t _line_input, *line_input = &_line_input;
1833 clib_error_t *error = 0;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001834 int rv = 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001835
1836 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02001837 if (!unformat_user (input, unformat_line_input, line_input))
Filip Tehlar53f09e32016-05-19 14:25:44 +02001838 return 0;
1839
1840 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1841 {
1842 if (unformat (line_input, "ls %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +02001843 locator_name_set = 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001844 else if (unformat (line_input, "disable"))
Florin Corasa2157cf2016-08-16 21:09:14 +02001845 is_add = 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001846 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001847 return clib_error_return (0, "parse error");
Filip Tehlar53f09e32016-05-19 14:25:44 +02001848 }
1849
1850 if (!locator_name_set)
1851 {
1852 clib_warning ("No locator set specified!");
1853 goto done;
1854 }
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001855 rv = vnet_lisp_pitr_set_locator_set (locator_set_name, is_add);
1856 if (0 != rv)
1857 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001858 error = clib_error_return (0, "failed to %s pitr!",
1859 is_add ? "add" : "delete");
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001860 }
Filip Tehlar53f09e32016-05-19 14:25:44 +02001861
1862done:
1863 if (locator_set_name)
1864 vec_free (locator_set_name);
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001865 return error;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001866}
1867
Florin Corasa2157cf2016-08-16 21:09:14 +02001868/* *INDENT-OFF* */
Filip Tehlar53f09e32016-05-19 14:25:44 +02001869VLIB_CLI_COMMAND (lisp_pitr_set_locator_set_command) = {
1870 .path = "lisp pitr",
1871 .short_help = "lisp pitr [disable] ls <locator-set-name>",
1872 .function = lisp_pitr_set_locator_set_command_fn,
1873};
Florin Corasa2157cf2016-08-16 21:09:14 +02001874/* *INDENT-ON* */
Filip Tehlar53f09e32016-05-19 14:25:44 +02001875
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001876static clib_error_t *
1877lisp_show_pitr_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001878 unformat_input_t * input, vlib_cli_command_t * cmd)
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001879{
Florin Corasa2157cf2016-08-16 21:09:14 +02001880 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1881 mapping_t *m;
1882 locator_set_t *ls;
1883 u8 *tmp_str = 0;
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001884
1885 vlib_cli_output (vm, "%=20s%=16s",
Florin Corasa2157cf2016-08-16 21:09:14 +02001886 "pitr", lcm->lisp_pitr ? "locator-set" : "");
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001887
Florin Corasa2157cf2016-08-16 21:09:14 +02001888 if (!lcm->lisp_pitr)
1889 {
1890 vlib_cli_output (vm, "%=20s", "disable");
1891 return 0;
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001892 }
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001893
Florin Corasa2157cf2016-08-16 21:09:14 +02001894 if (~0 == lcm->pitr_map_index)
1895 {
1896 tmp_str = format (0, "N/A");
1897 }
1898 else
1899 {
1900 m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
1901 if (~0 != m->locator_set_index)
1902 {
1903 ls =
1904 pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1905 tmp_str = format (0, "%s", ls->name);
1906 }
1907 else
1908 {
1909 tmp_str = format (0, "N/A");
1910 }
1911 }
1912 vec_add1 (tmp_str, 0);
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001913
Florin Corasa2157cf2016-08-16 21:09:14 +02001914 vlib_cli_output (vm, "%=20s%=16s", "enable", tmp_str);
1915
1916 vec_free (tmp_str);
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001917
1918 return 0;
1919}
1920
Florin Corasa2157cf2016-08-16 21:09:14 +02001921/* *INDENT-OFF* */
Andrej Kozemcak914f91b2016-07-18 13:55:37 +02001922VLIB_CLI_COMMAND (lisp_show_pitr_command) = {
1923 .path = "show lisp pitr",
1924 .short_help = "Show pitr",
1925 .function = lisp_show_pitr_command_fn,
1926};
Florin Corasa2157cf2016-08-16 21:09:14 +02001927/* *INDENT-ON* */
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001928
1929static u8 *
1930format_eid_entry (u8 * s, va_list * args)
1931{
Florin Corasa2157cf2016-08-16 21:09:14 +02001932 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
1933 lisp_cp_main_t *lcm = va_arg (*args, lisp_cp_main_t *);
1934 mapping_t *mapit = va_arg (*args, mapping_t *);
1935 locator_set_t *ls = va_arg (*args, locator_set_t *);
1936 gid_address_t *gid = &mapit->eid;
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02001937 u32 ttl = mapit->ttl;
1938 u8 aut = mapit->authoritative;
Florin Corasa2157cf2016-08-16 21:09:14 +02001939 u32 *loc_index;
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001940 u8 first_line = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02001941 u8 *loc;
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001942
Florin Corasa2157cf2016-08-16 21:09:14 +02001943 u8 *type = ls->local ? format (0, "local(%s)", ls->name)
1944 : format (0, "remote");
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001945
1946 if (vec_len (ls->locator_indices) == 0)
1947 {
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02001948 s = format (s, "%-35U%-30s%-20u%-u", format_gid_address, gid,
Florin Corasa2157cf2016-08-16 21:09:14 +02001949 type, ttl, aut);
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001950 }
1951 else
1952 {
1953 vec_foreach (loc_index, ls->locator_indices)
Florin Corasa2157cf2016-08-16 21:09:14 +02001954 {
1955 locator_t *l = pool_elt_at_index (lcm->locator_pool, loc_index[0]);
1956 if (l->local)
1957 loc = format (0, "%U", format_vnet_sw_if_index_name, vnm,
1958 l->sw_if_index);
1959 else
1960 loc = format (0, "%U", format_ip_address,
1961 &gid_address_ip (&l->address));
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001962
Florin Corasa2157cf2016-08-16 21:09:14 +02001963 if (first_line)
1964 {
1965 s = format (s, "%-35U%-20s%-30v%-20u%-u\n", format_gid_address,
1966 gid, type, loc, ttl, aut);
1967 first_line = 0;
1968 }
1969 else
1970 s = format (s, "%55s%v\n", "", loc);
1971 }
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001972 }
1973 return s;
1974}
1975
Florin Corase127a7e2016-02-18 22:20:01 +01001976static clib_error_t *
Filip Tehlarb0ccf032016-06-22 09:18:12 +02001977lisp_show_eid_table_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02001978 unformat_input_t * input,
1979 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +01001980{
Florin Corasa2157cf2016-08-16 21:09:14 +02001981 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1982 mapping_t *mapit;
1983 unformat_input_t _line_input, *line_input = &_line_input;
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02001984 u32 mi;
1985 gid_address_t eid;
1986 u8 print_all = 1;
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02001987 u8 filter = 0;
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02001988
Florin Corasa2157cf2016-08-16 21:09:14 +02001989 memset (&eid, 0, sizeof (eid));
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02001990
1991 /* Get a line of input. */
1992 if (!unformat_user (input, unformat_line_input, line_input))
1993 return 0;
1994
1995 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1996 {
1997 if (unformat (line_input, "eid %U", unformat_gid_address, &eid))
Florin Corasa2157cf2016-08-16 21:09:14 +02001998 print_all = 0;
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02001999 else if (unformat (line_input, "local"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002000 filter = 1;
2001 else if (unformat (line_input, "remote"))
2002 filter = 2;
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002003 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002004 return clib_error_return (0, "parse error: '%U'",
2005 format_unformat_error, line_input);
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002006 }
Florin Corase127a7e2016-02-18 22:20:01 +01002007
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002008 vlib_cli_output (vm, "%-35s%-20s%-30s%-20s%-s",
Florin Corasa2157cf2016-08-16 21:09:14 +02002009 "EID", "type", "locators", "ttl", "autoritative");
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002010
2011 if (print_all)
2012 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002013 /* *INDENT-OFF* */
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002014 pool_foreach (mapit, lcm->mapping_pool,
2015 ({
2016 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
2017 mapit->locator_set_index);
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002018 if (filter && !((1 == filter && ls->local) ||
2019 (2 == filter && !ls->local)))
2020 {
2021 continue;
2022 }
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002023 vlib_cli_output (vm, "%U", format_eid_entry, lcm->vnet_main,
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002024 lcm, mapit, ls);
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002025 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002026 /* *INDENT-ON* */
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002027 }
2028 else
2029 {
2030 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &eid);
Florin Corasa2157cf2016-08-16 21:09:14 +02002031 if ((u32) ~ 0 == mi)
2032 return 0;
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002033
2034 mapit = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corasa2157cf2016-08-16 21:09:14 +02002035 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2036 mapit->locator_set_index);
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002037
2038 if (filter && !((1 == filter && ls->local) ||
Florin Corasa2157cf2016-08-16 21:09:14 +02002039 (2 == filter && !ls->local)))
2040 {
2041 return 0;
2042 }
Andrej Kozemcak6cc6f912016-07-13 13:01:01 +02002043
2044 vlib_cli_output (vm, "%U,", format_eid_entry, lcm->vnet_main,
Florin Corasa2157cf2016-08-16 21:09:14 +02002045 lcm, mapit, ls);
Filip Tehlar1b1ee4f2016-07-04 11:43:11 +02002046 }
Florin Corase127a7e2016-02-18 22:20:01 +01002047
2048 return 0;
2049}
2050
Florin Corasa2157cf2016-08-16 21:09:14 +02002051/* *INDENT-OFF* */
Filip Tehlarb0ccf032016-06-22 09:18:12 +02002052VLIB_CLI_COMMAND (lisp_cp_show_eid_table_command) = {
Florin Corase127a7e2016-02-18 22:20:01 +01002053 .path = "show lisp eid-table",
Filip Tehlarb0ccf032016-06-22 09:18:12 +02002054 .short_help = "Shows EID table",
2055 .function = lisp_show_eid_table_command_fn,
Florin Corase127a7e2016-02-18 22:20:01 +01002056};
Florin Corasa2157cf2016-08-16 21:09:14 +02002057/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002058
2059/* cleans locator to locator-set data and removes locators not part of
2060 * any locator-set */
2061static void
2062clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
2063{
Filip Tehlard1c5cc32016-05-30 10:39:20 +02002064 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002065 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
2066 for (i = 0; i < vec_len (ls->locator_indices); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01002067 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002068 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
2069 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2070 loc_indexp[0]);
2071 for (j = 0; j < vec_len (ls_indexes[0]); j++)
2072 {
2073 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
2074 if (ls_indexp[0] == lsi)
2075 break;
2076 }
Florin Corase127a7e2016-02-18 22:20:01 +01002077
Florin Corasa2157cf2016-08-16 21:09:14 +02002078 /* delete index for removed locator-set */
2079 vec_del1 (ls_indexes[0], j);
Florin Corase127a7e2016-02-18 22:20:01 +01002080
2081 /* delete locator if it's part of no locator-set */
2082 if (vec_len (ls_indexes[0]) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02002083 {
2084 pool_put_index (lcm->locator_pool, loc_indexp[0]);
2085 vec_add1 (to_be_deleted, i);
2086 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02002087 }
2088
2089 if (to_be_deleted)
2090 {
2091 for (i = 0; i < vec_len (to_be_deleted); i++)
Florin Corasa2157cf2016-08-16 21:09:14 +02002092 {
2093 loc_indexp = vec_elt_at_index (to_be_deleted, i);
2094 vec_del1 (ls->locator_indices, loc_indexp[0]);
2095 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02002096 vec_free (to_be_deleted);
Florin Corase127a7e2016-02-18 22:20:01 +01002097 }
2098}
Filip Tehlard1c5cc32016-05-30 10:39:20 +02002099
Florin Corasf727db92016-06-23 15:01:58 +02002100static inline uword *
2101get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002102{
Florin Corasa2157cf2016-08-16 21:09:14 +02002103 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002104
Florin Corasa2157cf2016-08-16 21:09:14 +02002105 ASSERT (a != NULL);
2106 ASSERT (p != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002107
2108 /* find locator-set */
2109 if (a->local)
2110 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002111 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002112 }
2113 else
2114 {
2115 *p = a->index;
2116 }
2117
2118 return p;
2119}
2120
Florin Corasf727db92016-06-23 15:01:58 +02002121static inline int
2122is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
Florin Corasa2157cf2016-08-16 21:09:14 +02002123 locator_t * loc)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002124{
Florin Corasa2157cf2016-08-16 21:09:14 +02002125 locator_t *itloc;
2126 u32 *locit;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002127
Florin Corasa2157cf2016-08-16 21:09:14 +02002128 ASSERT (ls != NULL);
2129 ASSERT (loc != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002130
Florin Corasa2157cf2016-08-16 21:09:14 +02002131 vec_foreach (locit, ls->locator_indices)
2132 {
2133 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2134 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
2135 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
2136 {
2137 clib_warning ("Duplicate locator");
2138 return VNET_API_ERROR_VALUE_EXIST;
2139 }
2140 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002141
2142 return 0;
2143}
2144
Florin Corasf727db92016-06-23 15:01:58 +02002145static inline void
Florin Corasa2157cf2016-08-16 21:09:14 +02002146remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
2147 u32 ls_index, u32 loc_id)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002148{
Florin Corasa2157cf2016-08-16 21:09:14 +02002149 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2150 u32 **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002151
Florin Corasa2157cf2016-08-16 21:09:14 +02002152 ASSERT (ls != NULL);
2153 ASSERT (locit != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002154
Florin Corasa2157cf2016-08-16 21:09:14 +02002155 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
2156 pool_put_index (lcm->locator_pool, locit[0]);
2157 vec_del1 (ls->locator_indices, loc_id);
2158 vec_del1 (ls_indexes[0], ls_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002159}
2160
2161int
2162vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02002163 locator_set_t * ls, u32 * ls_result)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002164{
Florin Corasa2157cf2016-08-16 21:09:14 +02002165 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2166 locator_t *loc = NULL, *itloc = NULL;
2167 uword _p = (u32) ~ 0, *p = &_p;
2168 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002169 u32 loc_id = ~0;
2170 int ret = 0;
2171
Florin Corasa2157cf2016-08-16 21:09:14 +02002172 ASSERT (a != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002173
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002174 if (vnet_lisp_enable_disable_status () == 0)
2175 {
2176 clib_warning ("LISP is disabled!");
2177 return VNET_API_ERROR_LISP_DISABLED;
2178 }
2179
Florin Corasa2157cf2016-08-16 21:09:14 +02002180 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002181 if (!p)
2182 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002183 clib_warning ("locator-set %v doesn't exist", a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002184 return VNET_API_ERROR_INVALID_ARGUMENT;
2185 }
2186
2187 if (ls == 0)
2188 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002189 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002190 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02002191 {
2192 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2193 p[0]);
2194 return VNET_API_ERROR_INVALID_ARGUMENT;
2195 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002196 }
2197
2198 if (a->is_add)
2199 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002200 if (ls_result)
2201 ls_result[0] = p[0];
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002202
Florin Corasa2157cf2016-08-16 21:09:14 +02002203 /* allocate locators */
2204 vec_foreach (itloc, a->locators)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002205 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002206 ret = is_locator_in_locator_set (lcm, ls, itloc);
2207 if (0 != ret)
2208 {
2209 return ret;
2210 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002211
Florin Corasa2157cf2016-08-16 21:09:14 +02002212 pool_get (lcm->locator_pool, loc);
2213 loc[0] = itloc[0];
2214 loc_index = loc - lcm->locator_pool;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002215
Florin Corasa2157cf2016-08-16 21:09:14 +02002216 vec_add1 (ls->locator_indices, loc_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002217
Florin Corasa2157cf2016-08-16 21:09:14 +02002218 vec_validate (lcm->locator_to_locator_sets, loc_index);
2219 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
2220 loc_index);
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02002221 vec_add1 (ls_indexes[0], p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002222 }
Florin Corasa2157cf2016-08-16 21:09:14 +02002223 }
2224 else
2225 {
2226 ls_index = p[0];
2227
2228 itloc = a->locators;
2229 loc_id = 0;
2230 vec_foreach (locit, ls->locator_indices)
2231 {
2232 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2233
2234 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
2235 {
2236 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2237 }
2238 if (0 == loc->local &&
2239 !gid_address_cmp (&loc->address, &itloc->address))
2240 {
2241 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
2242 }
2243
2244 loc_id++;
2245 }
2246 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002247
2248 return 0;
2249}
2250
Florin Corase127a7e2016-02-18 22:20:01 +01002251int
2252vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02002253 u32 * ls_result)
Florin Corase127a7e2016-02-18 22:20:01 +01002254{
Florin Corasa2157cf2016-08-16 21:09:14 +02002255 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2256 locator_set_t *ls;
2257 uword _p = (u32) ~ 0, *p = &_p;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002258 u32 ls_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02002259 u32 **eid_indexes;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002260 int ret = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002261
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002262 if (vnet_lisp_enable_disable_status () == 0)
2263 {
2264 clib_warning ("LISP is disabled!");
2265 return VNET_API_ERROR_LISP_DISABLED;
2266 }
2267
Florin Corase127a7e2016-02-18 22:20:01 +01002268 if (a->is_add)
2269 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002270 p = get_locator_set_index (a, p);
Florin Corase127a7e2016-02-18 22:20:01 +01002271
2272 /* overwrite */
Florin Corasa2157cf2016-08-16 21:09:14 +02002273 if (p && p[0] != (u32) ~ 0)
2274 {
2275 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
2276 if (!ls)
2277 {
2278 clib_warning ("locator-set %d to be overwritten doesn't exist!",
2279 p[0]);
2280 return -1;
2281 }
Florin Corase127a7e2016-02-18 22:20:01 +01002282
Florin Corasa2157cf2016-08-16 21:09:14 +02002283 /* clean locator to locator-set vectors and remove locators if
2284 * they're not part of another locator-set */
2285 clean_locator_to_locator_set (lcm, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01002286
Florin Corasa2157cf2016-08-16 21:09:14 +02002287 /* remove locator indices from locator set */
2288 vec_free (ls->locator_indices);
Florin Corase127a7e2016-02-18 22:20:01 +01002289
Florin Corasa2157cf2016-08-16 21:09:14 +02002290 ls_index = p[0];
Florin Corase127a7e2016-02-18 22:20:01 +01002291
Florin Corasa2157cf2016-08-16 21:09:14 +02002292 if (ls_result)
2293 ls_result[0] = p[0];
2294 }
Florin Corase127a7e2016-02-18 22:20:01 +01002295 /* new locator-set */
2296 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002297 {
2298 pool_get (lcm->locator_set_pool, ls);
2299 memset (ls, 0, sizeof (*ls));
2300 ls_index = ls - lcm->locator_set_pool;
Florin Corase127a7e2016-02-18 22:20:01 +01002301
Florin Corasa2157cf2016-08-16 21:09:14 +02002302 if (a->local)
2303 {
2304 ls->name = vec_dup (a->name);
Florin Corase127a7e2016-02-18 22:20:01 +01002305
Florin Corasa2157cf2016-08-16 21:09:14 +02002306 if (!lcm->locator_set_index_by_name)
2307 lcm->locator_set_index_by_name = hash_create_vec (
2308 /* size */
2309 0,
2310 sizeof
2311 (ls->name
2312 [0]),
2313 sizeof
2314 (uword));
2315 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
2316 ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01002317
Florin Corasa2157cf2016-08-16 21:09:14 +02002318 /* mark as local locator-set */
2319 vec_add1 (lcm->local_locator_set_indexes, ls_index);
2320 }
2321 ls->local = a->local;
2322 if (ls_result)
2323 ls_result[0] = ls_index;
2324 }
Florin Corase127a7e2016-02-18 22:20:01 +01002325
Florin Corasa2157cf2016-08-16 21:09:14 +02002326 ret = vnet_lisp_add_del_locator (a, ls, NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002327 if (0 != ret)
Florin Corasa2157cf2016-08-16 21:09:14 +02002328 {
2329 return ret;
2330 }
Florin Corase127a7e2016-02-18 22:20:01 +01002331 }
2332 else
2333 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002334 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02002335 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02002336 {
2337 clib_warning ("locator-set %v doesn't exists", a->name);
2338 return -1;
2339 }
Florin Corase127a7e2016-02-18 22:20:01 +01002340
Florin Corasa2157cf2016-08-16 21:09:14 +02002341 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01002342 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02002343 {
2344 clib_warning ("locator-set with index %d doesn't exists", p[0]);
2345 return -1;
2346 }
Florin Corase127a7e2016-02-18 22:20:01 +01002347
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002348 if (lcm->mreq_itr_rlocs == p[0])
Florin Corasa2157cf2016-08-16 21:09:14 +02002349 {
2350 clib_warning ("Can't delete the locator-set used to constrain "
2351 "the itr-rlocs in map-requests!");
2352 return -1;
2353 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002354
Florin Corasa2157cf2016-08-16 21:09:14 +02002355 if (vec_len (lcm->locator_set_to_eids) != 0)
2356 {
2357 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
2358 if (vec_len (eid_indexes[0]) != 0)
2359 {
2360 clib_warning
2361 ("Can't delete a locator that supports a mapping!");
2362 return -1;
2363 }
2364 }
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02002365
2366 /* clean locator to locator-sets data */
2367 clean_locator_to_locator_set (lcm, p[0]);
2368
2369 if (ls->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02002370 {
2371 u32 it, lsi;
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02002372
Florin Corasa2157cf2016-08-16 21:09:14 +02002373 vec_foreach_index (it, lcm->local_locator_set_indexes)
2374 {
2375 lsi = vec_elt (lcm->local_locator_set_indexes, it);
2376 if (lsi == p[0])
2377 {
2378 vec_del1 (lcm->local_locator_set_indexes, it);
2379 break;
2380 }
2381 }
2382 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
2383 }
2384 vec_free (ls->name);
2385 vec_free (ls->locator_indices);
2386 pool_put (lcm->locator_set_pool, ls);
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02002387 }
2388 return 0;
2389}
2390
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002391int
2392vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
2393{
2394 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2395
2396 lcm->rloc_probing = is_enable;
2397 return 0;
2398}
2399
2400int
2401vnet_lisp_map_register_enable_disable (u8 is_enable)
2402{
2403 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2404
2405 lcm->map_registering = is_enable;
2406 return 0;
2407}
2408
Filip Tehlar46d4e362016-05-09 09:39:26 +02002409clib_error_t *
Florin Corasf727db92016-06-23 15:01:58 +02002410vnet_lisp_enable_disable (u8 is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002411{
Florin Coras1a1adc72016-07-22 01:45:30 +02002412 u32 vni, dp_table;
Florin Corasa2157cf2016-08-16 21:09:14 +02002413 clib_error_t *error = 0;
2414 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2415 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002416
Florin Corasf727db92016-06-23 15:01:58 +02002417 a->is_en = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002418 error = vnet_lisp_gpe_enable_disable (a);
2419 if (error)
2420 {
2421 return clib_error_return (0, "failed to %s data-plane!",
Florin Corasa2157cf2016-08-16 21:09:14 +02002422 a->is_en ? "enable" : "disable");
Filip Tehlar46d4e362016-05-09 09:39:26 +02002423 }
2424
Florin Corasf727db92016-06-23 15:01:58 +02002425 if (is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002426 {
Florin Coras1a1adc72016-07-22 01:45:30 +02002427 /* enable all l2 and l3 ifaces */
Florin Corasa2157cf2016-08-16 21:09:14 +02002428
2429 /* *INDENT-OFF* */
Florin Coras1a1adc72016-07-22 01:45:30 +02002430 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
2431 dp_add_del_iface(lcm, vni, 0, 1);
2432 }));
Florin Coras1a1adc72016-07-22 01:45:30 +02002433 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
2434 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
Florin Corasf727db92016-06-23 15:01:58 +02002435 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002436 /* *INDENT-ON* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02002437 }
2438 else
2439 {
Florin Corasf727db92016-06-23 15:01:58 +02002440 /* clear interface table */
Florin Corasa2157cf2016-08-16 21:09:14 +02002441 hash_free (lcm->fwd_entry_by_mapping_index);
2442 pool_free (lcm->fwd_entry_pool);
Filip Tehlar46d4e362016-05-09 09:39:26 +02002443 }
2444
2445 /* update global flag */
Florin Corasf727db92016-06-23 15:01:58 +02002446 lcm->is_enabled = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002447
2448 return 0;
2449}
2450
2451static clib_error_t *
2452lisp_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
Florin Corasa2157cf2016-08-16 21:09:14 +02002453 vlib_cli_command_t * cmd)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002454{
Florin Corasa2157cf2016-08-16 21:09:14 +02002455 unformat_input_t _line_input, *line_input = &_line_input;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002456 u8 is_enabled = 0;
2457 u8 is_set = 0;
2458
2459 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02002460 if (!unformat_user (input, unformat_line_input, line_input))
Filip Tehlar46d4e362016-05-09 09:39:26 +02002461 return 0;
2462
2463 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2464 {
2465 if (unformat (line_input, "enable"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002466 {
2467 is_set = 1;
2468 is_enabled = 1;
2469 }
Filip Tehlar46d4e362016-05-09 09:39:26 +02002470 else if (unformat (line_input, "disable"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002471 is_set = 1;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002472 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002473 {
2474 return clib_error_return (0, "parse error: '%U'",
2475 format_unformat_error, line_input);
2476 }
Filip Tehlar46d4e362016-05-09 09:39:26 +02002477 }
2478
2479 if (!is_set)
Florin Corasa2157cf2016-08-16 21:09:14 +02002480 return clib_error_return (0, "state not set");
Filip Tehlar46d4e362016-05-09 09:39:26 +02002481
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002482 vnet_lisp_enable_disable (is_enabled);
2483 return 0;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002484}
2485
Florin Corasa2157cf2016-08-16 21:09:14 +02002486/* *INDENT-OFF* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02002487VLIB_CLI_COMMAND (lisp_cp_enable_disable_command) = {
2488 .path = "lisp",
2489 .short_help = "lisp [enable|disable]",
2490 .function = lisp_enable_disable_command_fn,
2491};
Florin Corasa2157cf2016-08-16 21:09:14 +02002492/* *INDENT-ON* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02002493
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002494static clib_error_t *
2495lisp_map_register_enable_disable_command_fn (vlib_main_t * vm,
2496 unformat_input_t * input,
2497 vlib_cli_command_t * cmd)
2498{
2499 unformat_input_t _line_input, *line_input = &_line_input;
2500 u8 is_enabled = 0;
2501 u8 is_set = 0;
2502
2503 /* Get a line of input. */
2504 if (!unformat_user (input, unformat_line_input, line_input))
2505 return 0;
2506
2507 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2508 {
2509 if (unformat (line_input, "enable"))
2510 {
2511 is_set = 1;
2512 is_enabled = 1;
2513 }
2514 else if (unformat (line_input, "disable"))
2515 is_set = 1;
2516 else
2517 {
2518 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2519 line_input);
2520 return 0;
2521 }
2522 }
2523
2524 if (!is_set)
2525 {
2526 vlib_cli_output (vm, "state not set!");
2527 return 0;
2528 }
2529
2530 vnet_lisp_map_register_enable_disable (is_enabled);
2531 return 0;
2532}
2533
2534/* *INDENT-OFF* */
2535VLIB_CLI_COMMAND (lisp_map_register_enable_disable_command) = {
2536 .path = "lisp map-register",
2537 .short_help = "lisp map-register [enable|disable]",
2538 .function = lisp_map_register_enable_disable_command_fn,
2539};
2540/* *INDENT-ON* */
2541
2542static clib_error_t *
2543lisp_rloc_probe_enable_disable_command_fn (vlib_main_t * vm,
2544 unformat_input_t * input,
2545 vlib_cli_command_t * cmd)
2546{
2547 unformat_input_t _line_input, *line_input = &_line_input;
2548 u8 is_enabled = 0;
2549 u8 is_set = 0;
2550
2551 /* Get a line of input. */
2552 if (!unformat_user (input, unformat_line_input, line_input))
2553 return 0;
2554
2555 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2556 {
2557 if (unformat (line_input, "enable"))
2558 {
2559 is_set = 1;
2560 is_enabled = 1;
2561 }
2562 else if (unformat (line_input, "disable"))
2563 is_set = 1;
2564 else
2565 {
2566 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
2567 line_input);
2568 return 0;
2569 }
2570 }
2571
2572 if (!is_set)
2573 {
2574 vlib_cli_output (vm, "state not set!");
2575 return 0;
2576 }
2577
2578 vnet_lisp_rloc_probe_enable_disable (is_enabled);
2579 return 0;
2580}
2581
2582/* *INDENT-OFF* */
2583VLIB_CLI_COMMAND (lisp_rloc_probe_enable_disable_command) = {
2584 .path = "lisp rloc-probe",
2585 .short_help = "lisp rloc-probe [enable|disable]",
2586 .function = lisp_rloc_probe_enable_disable_command_fn,
2587};
2588/* *INDENT-ON* */
2589
Filip Tehlar46d4e362016-05-09 09:39:26 +02002590u8
2591vnet_lisp_enable_disable_status (void)
2592{
Florin Corasa2157cf2016-08-16 21:09:14 +02002593 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar46d4e362016-05-09 09:39:26 +02002594 return lcm->is_enabled;
2595}
2596
2597static u8 *
2598format_lisp_status (u8 * s, va_list * args)
2599{
Florin Corasa2157cf2016-08-16 21:09:14 +02002600 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar46d4e362016-05-09 09:39:26 +02002601 return format (s, "%s", lcm->is_enabled ? "enabled" : "disabled");
2602}
2603
2604static clib_error_t *
2605lisp_show_status_command_fn (vlib_main_t * vm, unformat_input_t * input,
Florin Corasa2157cf2016-08-16 21:09:14 +02002606 vlib_cli_command_t * cmd)
Filip Tehlar46d4e362016-05-09 09:39:26 +02002607{
Florin Corasa2157cf2016-08-16 21:09:14 +02002608 u8 *msg = 0;
Filip Tehlar46d4e362016-05-09 09:39:26 +02002609 msg = format (msg, "feature: %U\ngpe: %U\n",
Florin Corasa2157cf2016-08-16 21:09:14 +02002610 format_lisp_status, format_vnet_lisp_gpe_status);
Filip Tehlar46d4e362016-05-09 09:39:26 +02002611 vlib_cli_output (vm, "%v", msg);
2612 vec_free (msg);
2613 return 0;
2614}
2615
Florin Corasa2157cf2016-08-16 21:09:14 +02002616/* *INDENT-OFF* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02002617VLIB_CLI_COMMAND (lisp_show_status_command) = {
2618 .path = "show lisp status",
2619 .short_help = "show lisp status",
2620 .function = lisp_show_status_command_fn,
2621};
Florin Corasa2157cf2016-08-16 21:09:14 +02002622/* *INDENT-ON* */
Filip Tehlar324112f2016-06-02 16:07:38 +02002623
2624static clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002625lisp_show_eid_table_map_command_fn (vlib_main_t * vm,
2626 unformat_input_t * input,
2627 vlib_cli_command_t * cmd)
Filip Tehlar324112f2016-06-02 16:07:38 +02002628{
Florin Corasa2157cf2016-08-16 21:09:14 +02002629 hash_pair_t *p;
Filip Tehlarc0681792016-08-24 14:11:07 +02002630 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corasa2157cf2016-08-16 21:09:14 +02002631 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarc0681792016-08-24 14:11:07 +02002632 uword *vni_table = 0;
2633 u8 is_l2 = 0;
Filip Tehlar324112f2016-06-02 16:07:38 +02002634
Filip Tehlarc0681792016-08-24 14:11:07 +02002635 /* Get a line of input. */
2636 if (!unformat_user (input, unformat_line_input, line_input))
2637 return 0;
2638
2639 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2640 {
2641 if (unformat (line_input, "l2"))
2642 {
2643 vni_table = lcm->bd_id_by_vni;
2644 is_l2 = 1;
2645 }
2646 else if (unformat (line_input, "l3"))
2647 {
2648 vni_table = lcm->table_id_by_vni;
2649 is_l2 = 0;
2650 }
2651 else
2652 return clib_error_return (0, "parse error: '%U'",
2653 format_unformat_error, line_input);
2654 }
2655
2656 if (!vni_table)
2657 {
2658 vlib_cli_output (vm, "Error: expected l2|l3 param!\n");
2659 return 0;
2660 }
2661
2662 vlib_cli_output (vm, "%=10s%=10s", "VNI", is_l2 ? "BD" : "VRF");
Florin Corasa2157cf2016-08-16 21:09:14 +02002663
2664 /* *INDENT-OFF* */
Filip Tehlarc0681792016-08-24 14:11:07 +02002665 hash_foreach_pair (p, vni_table,
Florin Corasa2157cf2016-08-16 21:09:14 +02002666 ({
2667 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
2668 }));
2669 /* *INDENT-ON* */
2670
Filip Tehlar324112f2016-06-02 16:07:38 +02002671 return 0;
2672}
2673
Florin Corasa2157cf2016-08-16 21:09:14 +02002674/* *INDENT-OFF* */
Filip Tehlar324112f2016-06-02 16:07:38 +02002675VLIB_CLI_COMMAND (lisp_show_eid_table_map_command) = {
2676 .path = "show lisp eid-table map",
Filip Tehlarc0681792016-08-24 14:11:07 +02002677 .short_help = "show lisp eid-table l2|l3",
Filip Tehlar324112f2016-06-02 16:07:38 +02002678 .function = lisp_show_eid_table_map_command_fn,
2679};
Florin Corasa2157cf2016-08-16 21:09:14 +02002680/* *INDENT-ON* */
Filip Tehlar324112f2016-06-02 16:07:38 +02002681
Florin Corase127a7e2016-02-18 22:20:01 +01002682static clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002683lisp_add_del_locator_set_command_fn (vlib_main_t * vm,
2684 unformat_input_t * input,
2685 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +01002686{
Florin Corasa2157cf2016-08-16 21:09:14 +02002687 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2688 vnet_main_t *vnm = lgm->vnet_main;
2689 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corase127a7e2016-02-18 22:20:01 +01002690 u8 is_add = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02002691 clib_error_t *error = 0;
2692 u8 *locator_set_name = 0;
2693 locator_t locator, *locators = 0;
2694 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
Florin Corase127a7e2016-02-18 22:20:01 +01002695 u32 ls_index = 0;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002696 int rv = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002697
Florin Corasa2157cf2016-08-16 21:09:14 +02002698 memset (&locator, 0, sizeof (locator));
2699 memset (a, 0, sizeof (a[0]));
Florin Corase127a7e2016-02-18 22:20:01 +01002700
2701 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02002702 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corase127a7e2016-02-18 22:20:01 +01002703 return 0;
2704
2705 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2706 {
2707 if (unformat (line_input, "add %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +02002708 is_add = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002709 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +02002710 is_add = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002711 else if (unformat (line_input, "iface %U p %d w %d",
Florin Corasa2157cf2016-08-16 21:09:14 +02002712 unformat_vnet_sw_interface, vnm,
2713 &locator.sw_if_index, &locator.priority,
2714 &locator.weight))
2715 {
2716 locator.local = 1;
2717 vec_add1 (locators, locator);
2718 }
Florin Corase127a7e2016-02-18 22:20:01 +01002719 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002720 {
2721 error = unformat_parse_error (line_input);
2722 goto done;
2723 }
Florin Corase127a7e2016-02-18 22:20:01 +01002724 }
2725
2726 a->name = locator_set_name;
2727 a->locators = locators;
2728 a->is_add = is_add;
2729 a->local = 1;
2730
Florin Corasa2157cf2016-08-16 21:09:14 +02002731 rv = vnet_lisp_add_del_locator_set (a, &ls_index);
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002732 if (0 != rv)
2733 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002734 error = clib_error_return (0, "failed to %s locator-set!",
2735 is_add ? "add" : "delete");
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002736 }
Florin Corase127a7e2016-02-18 22:20:01 +01002737
Florin Corasa2157cf2016-08-16 21:09:14 +02002738done:
2739 vec_free (locators);
Filip Tehlar53f09e32016-05-19 14:25:44 +02002740 if (locator_set_name)
2741 vec_free (locator_set_name);
Florin Corase127a7e2016-02-18 22:20:01 +01002742 return error;
2743}
2744
Florin Corasa2157cf2016-08-16 21:09:14 +02002745/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01002746VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
2747 .path = "lisp locator-set",
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002748 .short_help = "lisp locator-set add/del <name> [iface <iface-name> "
2749 "p <priority> w <weight>]",
Florin Corase127a7e2016-02-18 22:20:01 +01002750 .function = lisp_add_del_locator_set_command_fn,
2751};
Florin Corasa2157cf2016-08-16 21:09:14 +02002752/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002753
2754static clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002755lisp_add_del_locator_in_set_command_fn (vlib_main_t * vm,
2756 unformat_input_t * input,
2757 vlib_cli_command_t * cmd)
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002758{
Florin Corasa2157cf2016-08-16 21:09:14 +02002759 lisp_gpe_main_t *lgm = &lisp_gpe_main;
2760 vnet_main_t *vnm = lgm->vnet_main;
2761 unformat_input_t _line_input, *line_input = &_line_input;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002762 u8 is_add = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02002763 clib_error_t *error = 0;
2764 u8 *locator_set_name = 0;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002765 u8 locator_set_name_set = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002766 locator_t locator, *locators = 0;
2767 vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002768 u32 ls_index = 0;
2769
Florin Corasa2157cf2016-08-16 21:09:14 +02002770 memset (&locator, 0, sizeof (locator));
2771 memset (a, 0, sizeof (a[0]));
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002772
2773 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02002774 if (!unformat_user (input, unformat_line_input, line_input))
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002775 return 0;
2776
2777 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2778 {
2779 if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002780 is_add = 1;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002781 else if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002782 is_add = 0;
2783 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
2784 locator_set_name_set = 1;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002785 else if (unformat (line_input, "iface %U p %d w %d",
Florin Corasa2157cf2016-08-16 21:09:14 +02002786 unformat_vnet_sw_interface, vnm,
2787 &locator.sw_if_index, &locator.priority,
2788 &locator.weight))
2789 {
2790 locator.local = 1;
2791 vec_add1 (locators, locator);
2792 }
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002793 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002794 {
2795 error = unformat_parse_error (line_input);
2796 goto done;
2797 }
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002798 }
2799
2800 if (!locator_set_name_set)
2801 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002802 error = clib_error_return (0, "locator_set name not set!");
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002803 goto done;
Florin Corasa2157cf2016-08-16 21:09:14 +02002804 }
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002805
2806 a->name = locator_set_name;
2807 a->locators = locators;
2808 a->is_add = is_add;
2809 a->local = 1;
2810
Florin Corasa2157cf2016-08-16 21:09:14 +02002811 vnet_lisp_add_del_locator (a, 0, &ls_index);
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002812
Florin Corasa2157cf2016-08-16 21:09:14 +02002813done:
2814 vec_free (locators);
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002815 vec_free (locator_set_name);
2816 return error;
2817}
2818
Florin Corasa2157cf2016-08-16 21:09:14 +02002819/* *INDENT-OFF* */
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002820VLIB_CLI_COMMAND (lisp_cp_add_del_locator_in_set_command) = {
2821 .path = "lisp locator",
2822 .short_help = "lisp locator add/del locator-set <name> iface <iface-name> "
2823 "p <priority> w <weight>",
2824 .function = lisp_add_del_locator_in_set_command_fn,
2825};
Florin Corasa2157cf2016-08-16 21:09:14 +02002826/* *INDENT-ON* */
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002827
2828static clib_error_t *
Florin Corase127a7e2016-02-18 22:20:01 +01002829lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02002830 unformat_input_t * input,
2831 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +01002832{
Florin Corasa2157cf2016-08-16 21:09:14 +02002833 locator_set_t *lsit;
2834 locator_t *loc;
2835 u32 *locit;
2836 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01002837
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02002838 vlib_cli_output (vm, "%s%=16s%=16s%=16s", "Locator-set", "Locator",
Florin Corasa2157cf2016-08-16 21:09:14 +02002839 "Priority", "Weight");
2840
2841 /* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01002842 pool_foreach (lsit, lcm->locator_set_pool,
2843 ({
2844 u8 * msg = 0;
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002845 int next_line = 0;
Andrej Kozemcaka8691752016-07-27 10:33:38 +02002846 if (lsit->local)
2847 {
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02002848 msg = format (msg, "%v", lsit->name);
Andrej Kozemcaka8691752016-07-27 10:33:38 +02002849 }
2850 else
2851 {
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02002852 msg = format (msg, "<%s-%d>", "remote", lsit - lcm->locator_set_pool);
Andrej Kozemcaka8691752016-07-27 10:33:38 +02002853 }
Florin Corase127a7e2016-02-18 22:20:01 +01002854 vec_foreach (locit, lsit->locator_indices)
2855 {
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002856 if (next_line)
2857 {
2858 msg = format (msg, "%16s", " ");
2859 }
Florin Corase127a7e2016-02-18 22:20:01 +01002860 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
2861 if (loc->local)
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002862 msg = format (msg, "%16d%16d%16d\n", loc->sw_if_index, loc->priority,
Florin Corase127a7e2016-02-18 22:20:01 +01002863 loc->weight);
2864 else
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002865 msg = format (msg, "%16U%16d%16d\n", format_ip_address,
Filip Tehlar4d5cabd2016-07-07 15:40:36 +02002866 &gid_address_ip(&loc->address), loc->priority,
Andrej Kozemcak7511ae82016-06-20 10:08:20 +02002867 loc->weight);
2868 next_line = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002869 }
2870 vlib_cli_output (vm, "%v", msg);
2871 vec_free (msg);
2872 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002873 /* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002874 return 0;
2875}
2876
Florin Corasa2157cf2016-08-16 21:09:14 +02002877/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01002878VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
2879 .path = "show lisp locator-set",
2880 .short_help = "Shows locator-sets",
2881 .function = lisp_cp_show_locator_sets_command_fn,
2882};
Florin Corasa2157cf2016-08-16 21:09:14 +02002883/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002884
2885int
2886vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
2887{
Florin Corasa2157cf2016-08-16 21:09:14 +02002888 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01002889 u32 i;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002890 lisp_msmr_t _mr, *mr = &_mr;
Florin Corase127a7e2016-02-18 22:20:01 +01002891
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002892 if (vnet_lisp_enable_disable_status () == 0)
2893 {
2894 clib_warning ("LISP is disabled!");
2895 return VNET_API_ERROR_LISP_DISABLED;
2896 }
2897
Florin Corase127a7e2016-02-18 22:20:01 +01002898 if (a->is_add)
2899 {
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002900
2901 if (get_map_resolver (&a->address))
Florin Corasa2157cf2016-08-16 21:09:14 +02002902 {
2903 clib_warning ("map-resolver %U already exists!", format_ip_address,
2904 &a->address);
2905 return -1;
2906 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002907
2908 memset (mr, 0, sizeof (*mr));
Florin Corasa2157cf2016-08-16 21:09:14 +02002909 ip_address_copy (&mr->address, &a->address);
2910 vec_add1 (lcm->map_resolvers, *mr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002911
2912 if (vec_len (lcm->map_resolvers) == 1)
Florin Corasa2157cf2016-08-16 21:09:14 +02002913 lcm->do_map_resolver_election = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002914 }
2915 else
2916 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002917 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
2918 {
2919 mr = vec_elt_at_index (lcm->map_resolvers, i);
2920 if (!ip_address_cmp (&mr->address, &a->address))
2921 {
2922 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
2923 lcm->do_map_resolver_election = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002924
Florin Corasa2157cf2016-08-16 21:09:14 +02002925 vec_del1 (lcm->map_resolvers, i);
2926 break;
2927 }
2928 }
Florin Corase127a7e2016-02-18 22:20:01 +01002929 }
2930 return 0;
2931}
2932
2933static clib_error_t *
2934lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02002935 unformat_input_t * input,
2936 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +01002937{
Florin Corasa2157cf2016-08-16 21:09:14 +02002938 unformat_input_t _line_input, *line_input = &_line_input;
Florin Coras0f1c29c2016-07-26 12:12:17 +02002939 u8 is_add = 1, addr_set = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002940 ip_address_t ip_addr;
Florin Corasa2157cf2016-08-16 21:09:14 +02002941 clib_error_t *error = 0;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002942 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002943 vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
Florin Corase127a7e2016-02-18 22:20:01 +01002944
2945 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02002946 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corase127a7e2016-02-18 22:20:01 +01002947 return 0;
2948
2949 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2950 {
2951 if (unformat (line_input, "add"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002952 is_add = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002953 else if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02002954 is_add = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002955 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
Florin Corasa2157cf2016-08-16 21:09:14 +02002956 addr_set = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002957 else
Florin Corasa2157cf2016-08-16 21:09:14 +02002958 {
2959 error = unformat_parse_error (line_input);
2960 goto done;
2961 }
Florin Corase127a7e2016-02-18 22:20:01 +01002962 }
Florin Coras0f1c29c2016-07-26 12:12:17 +02002963
2964 if (!addr_set)
2965 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002966 error = clib_error_return (0, "Map-resolver address must be set!");
Florin Coras0f1c29c2016-07-26 12:12:17 +02002967 goto done;
2968 }
2969
Florin Corase127a7e2016-02-18 22:20:01 +01002970 a->is_add = is_add;
2971 a->address = ip_addr;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002972 rv = vnet_lisp_add_del_map_resolver (a);
2973 if (0 != rv)
2974 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002975 error = clib_error_return (0, "failed to %s map-resolver!",
2976 is_add ? "add" : "delete");
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02002977 }
Florin Corase127a7e2016-02-18 22:20:01 +01002978
Florin Corasa2157cf2016-08-16 21:09:14 +02002979done:
Florin Corase127a7e2016-02-18 22:20:01 +01002980 return error;
2981}
2982
Florin Corasa2157cf2016-08-16 21:09:14 +02002983/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01002984VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
2985 .path = "lisp map-resolver",
2986 .short_help = "lisp map-resolver add/del <ip_address>",
2987 .function = lisp_add_del_map_resolver_command_fn,
2988};
Florin Corasa2157cf2016-08-16 21:09:14 +02002989/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01002990
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002991int
2992vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
2993{
Florin Corasa2157cf2016-08-16 21:09:14 +02002994 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
2995 uword *p = 0;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002996
Florin Corasf727db92016-06-23 15:01:58 +02002997 if (vnet_lisp_enable_disable_status () == 0)
2998 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002999 clib_warning ("LISP is disabled!");
Florin Corasf727db92016-06-23 15:01:58 +02003000 return VNET_API_ERROR_LISP_DISABLED;
3001 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003002
3003 if (a->is_add)
3004 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003005 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003006 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02003007 {
3008 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
3009 return VNET_API_ERROR_INVALID_ARGUMENT;
3010 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003011
3012 lcm->mreq_itr_rlocs = p[0];
3013 }
3014 else
3015 {
3016 lcm->mreq_itr_rlocs = ~0;
3017 }
3018
3019 return 0;
3020}
3021
3022static clib_error_t *
Florin Corasf727db92016-06-23 15:01:58 +02003023lisp_add_del_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02003024 unformat_input_t * input,
3025 vlib_cli_command_t * cmd)
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003026{
Florin Corasa2157cf2016-08-16 21:09:14 +02003027 unformat_input_t _line_input, *line_input = &_line_input;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003028 u8 is_add = 1;
Florin Corasa2157cf2016-08-16 21:09:14 +02003029 u8 *locator_set_name = 0;
3030 clib_error_t *error = 0;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003031 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02003032 vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003033
3034 /* Get a line of input. */
Florin Corasa2157cf2016-08-16 21:09:14 +02003035 if (!unformat_user (input, unformat_line_input, line_input))
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003036 return 0;
3037
3038 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3039 {
3040 if (unformat (line_input, "del"))
Florin Corasa2157cf2016-08-16 21:09:14 +02003041 is_add = 0;
Filip Tehlar240977f2016-10-03 14:08:59 +02003042 else if (unformat (line_input, "add %_%v%_", &locator_set_name))
Florin Corasa2157cf2016-08-16 21:09:14 +02003043 is_add = 1;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003044 else
Florin Corasa2157cf2016-08-16 21:09:14 +02003045 {
3046 error = unformat_parse_error (line_input);
3047 goto done;
3048 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003049 }
3050
3051 a->is_add = is_add;
3052 a->locator_set_name = locator_set_name;
3053 rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
3054 if (0 != rv)
3055 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003056 error = clib_error_return (0, "failed to %s map-request itr-rlocs!",
3057 is_add ? "add" : "delete");
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003058 }
3059
Florin Corasa2157cf2016-08-16 21:09:14 +02003060 vec_free (locator_set_name);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003061
Florin Corasa2157cf2016-08-16 21:09:14 +02003062done:
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003063 return error;
3064
3065}
3066
Florin Corasa2157cf2016-08-16 21:09:14 +02003067/* *INDENT-OFF* */
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003068VLIB_CLI_COMMAND (lisp_add_del_map_request_command) = {
3069 .path = "lisp map-request itr-rlocs",
3070 .short_help = "lisp map-request itr-rlocs add/del <locator_set_name>",
3071 .function = lisp_add_del_mreq_itr_rlocs_command_fn,
3072};
Florin Corasa2157cf2016-08-16 21:09:14 +02003073/* *INDENT-ON* */
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003074
3075static clib_error_t *
3076lisp_show_mreq_itr_rlocs_command_fn (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02003077 unformat_input_t * input,
3078 vlib_cli_command_t * cmd)
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003079{
Florin Corasa2157cf2016-08-16 21:09:14 +02003080 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3081 locator_set_t *loc_set;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003082
3083 vlib_cli_output (vm, "%=20s", "itr-rlocs");
3084
3085 if (~0 == lcm->mreq_itr_rlocs)
3086 {
3087 return 0;
3088 }
3089
3090 loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
3091
3092 vlib_cli_output (vm, "%=20s", loc_set->name);
3093
3094 return 0;
3095}
3096
Florin Corasa2157cf2016-08-16 21:09:14 +02003097/* *INDENT-OFF* */
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003098VLIB_CLI_COMMAND (lisp_show_map_request_command) = {
3099 .path = "show lisp map-request itr-rlocs",
3100 .short_help = "Shows map-request itr-rlocs",
3101 .function = lisp_show_mreq_itr_rlocs_command_fn,
3102};
Florin Corasa2157cf2016-08-16 21:09:14 +02003103/* *INDENT-ON* */
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003104
Florin Corase127a7e2016-02-18 22:20:01 +01003105/* Statistics (not really errors) */
3106#define foreach_lisp_cp_lookup_error \
3107_(DROP, "drop") \
3108_(MAP_REQUESTS_SENT, "map-request sent")
3109
Florin Corasa2157cf2016-08-16 21:09:14 +02003110static char *lisp_cp_lookup_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01003111#define _(sym,string) string,
3112 foreach_lisp_cp_lookup_error
3113#undef _
3114};
3115
3116typedef enum
3117{
3118#define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02003119 foreach_lisp_cp_lookup_error
Florin Corase127a7e2016-02-18 22:20:01 +01003120#undef _
3121 LISP_CP_LOOKUP_N_ERROR,
3122} lisp_cp_lookup_error_t;
3123
3124typedef enum
3125{
3126 LISP_CP_LOOKUP_NEXT_DROP,
Florin Corase127a7e2016-02-18 22:20:01 +01003127 LISP_CP_LOOKUP_N_NEXT,
3128} lisp_cp_lookup_next_t;
3129
3130typedef struct
3131{
3132 gid_address_t dst_eid;
Andrej Kozemcak94e34762016-05-25 12:43:21 +02003133 ip_address_t map_resolver_ip;
Florin Corase127a7e2016-02-18 22:20:01 +01003134} lisp_cp_lookup_trace_t;
3135
3136u8 *
3137format_lisp_cp_lookup_trace (u8 * s, va_list * args)
3138{
3139 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3140 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02003141 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01003142
3143 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
Florin Corasa2157cf2016-08-16 21:09:14 +02003144 format_ip_address, &t->map_resolver_ip, format_gid_address,
3145 &t->dst_eid);
Florin Corase127a7e2016-02-18 22:20:01 +01003146 return s;
3147}
3148
Florin Coras1c494842016-06-16 21:08:56 +02003149int
3150get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
Florin Corasa2157cf2016-08-16 21:09:14 +02003151 ip_address_t * sloc)
Florin Corasc2c90082016-06-13 18:37:15 +02003152{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003153 lisp_msmr_t *mrit;
Florin Corasa2157cf2016-08-16 21:09:14 +02003154 ip_address_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01003155
Florin Corasa2157cf2016-08-16 21:09:14 +02003156 if (vec_len (lcm->map_resolvers) == 0)
Florin Corase127a7e2016-02-18 22:20:01 +01003157 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003158 clib_warning ("No map-resolver configured");
Florin Coras1c494842016-06-16 21:08:56 +02003159 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003160 }
3161
Florin Corasddfafb82016-05-13 18:09:56 +02003162 /* find the first mr ip we have a route to and the ip of the
3163 * iface that has a route to it */
Florin Corasa2157cf2016-08-16 21:09:14 +02003164 vec_foreach (mrit, lcm->map_resolvers)
3165 {
3166 a = &mrit->address;
3167 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
3168 {
3169 ip_address_copy (mr_ip, a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003170
Florin Corasa2157cf2016-08-16 21:09:14 +02003171 /* also update globals */
3172 return 1;
3173 }
3174 }
Florin Corasddfafb82016-05-13 18:09:56 +02003175
Florin Corasa2157cf2016-08-16 21:09:14 +02003176 clib_warning ("Can't find map-resolver and local interface ip!");
Florin Coras1c494842016-06-16 21:08:56 +02003177 return 0;
Florin Corasddfafb82016-05-13 18:09:56 +02003178}
Filip Tehlar254b0362016-04-07 10:04:34 +02003179
Filip Tehlarbeceab92016-04-20 17:21:55 +02003180static gid_address_t *
Filip Tehlar254b0362016-04-07 10:04:34 +02003181build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
3182{
Florin Corasa2157cf2016-08-16 21:09:14 +02003183 void *addr;
Filip Tehlar254b0362016-04-07 10:04:34 +02003184 u32 i;
Florin Corasa2157cf2016-08-16 21:09:14 +02003185 locator_t *loc;
3186 u32 *loc_indexp;
3187 ip_interface_address_t *ia = 0;
3188 gid_address_t gid_data, *gid = &gid_data;
3189 gid_address_t *rlocs = 0;
3190 ip_prefix_t *ippref = &gid_address_ippref (gid);
3191 ip_address_t *rloc = &ip_prefix_addr (ippref);
Filip Tehlar254b0362016-04-07 10:04:34 +02003192
Filip Tehlar324112f2016-06-02 16:07:38 +02003193 memset (gid, 0, sizeof (gid[0]));
Filip Tehlarbeceab92016-04-20 17:21:55 +02003194 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
Florin Corasa2157cf2016-08-16 21:09:14 +02003195 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
Filip Tehlar254b0362016-04-07 10:04:34 +02003196 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003197 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
Filip Tehlar254b0362016-04-07 10:04:34 +02003198 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
3199
Filip Tehlar254b0362016-04-07 10:04:34 +02003200 /* Add ipv4 locators first TODO sort them */
Florin Corasa2157cf2016-08-16 21:09:14 +02003201
3202 /* *INDENT-OFF* */
Filip Tehlar254b0362016-04-07 10:04:34 +02003203 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3204 loc->sw_if_index, 1 /* unnumbered */,
3205 ({
Florin Coras1c494842016-06-16 21:08:56 +02003206 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
3207 ip_address_set (rloc, addr, IP4);
Florin Corasddfafb82016-05-13 18:09:56 +02003208 ip_prefix_len (ippref) = 32;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02003209 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02003210 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02003211 }));
3212
Filip Tehlar254b0362016-04-07 10:04:34 +02003213 /* Add ipv6 locators */
3214 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3215 loc->sw_if_index, 1 /* unnumbered */,
3216 ({
Florin Coras1c494842016-06-16 21:08:56 +02003217 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
3218 ip_address_set (rloc, addr, IP6);
Florin Corasddfafb82016-05-13 18:09:56 +02003219 ip_prefix_len (ippref) = 128;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02003220 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02003221 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02003222 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02003223 /* *INDENT-ON* */
3224
Filip Tehlar254b0362016-04-07 10:04:34 +02003225 }
3226 return rlocs;
3227}
3228
Florin Corase127a7e2016-02-18 22:20:01 +01003229static vlib_buffer_t *
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003230build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
3231 ip_address_t * sloc, ip_address_t * rloc,
3232 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
3233{
3234 vlib_buffer_t *b;
3235 u32 bi;
3236 vlib_main_t *vm = lcm->vlib_main;
3237
3238 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3239 {
3240 clib_warning ("Can't allocate buffer for Map-Request!");
3241 return 0;
3242 }
3243
3244 b = vlib_get_buffer (vm, bi);
3245
3246 /* leave some space for the encap headers */
3247 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3248
3249 /* put lisp msg */
3250 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
3251 1 /* rloc probe */ , nonce_res);
3252
3253 /* push outer ip header */
3254 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3255 rloc);
3256
3257 bi_res[0] = bi;
3258
3259 return b;
3260}
3261
3262static vlib_buffer_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02003263build_encapsulated_map_request (lisp_cp_main_t * lcm,
3264 gid_address_t * seid, gid_address_t * deid,
3265 locator_set_t * loc_set, ip_address_t * mr_ip,
3266 ip_address_t * sloc, u8 is_smr_invoked,
3267 u64 * nonce_res, u32 * bi_res)
Florin Corase127a7e2016-02-18 22:20:01 +01003268{
Florin Corasa2157cf2016-08-16 21:09:14 +02003269 vlib_buffer_t *b;
Florin Corase127a7e2016-02-18 22:20:01 +01003270 u32 bi;
Florin Corasa2157cf2016-08-16 21:09:14 +02003271 gid_address_t *rlocs = 0;
3272 vlib_main_t *vm = lcm->vlib_main;
Florin Corase127a7e2016-02-18 22:20:01 +01003273
3274 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3275 {
3276 clib_warning ("Can't allocate buffer for Map-Request!");
3277 return 0;
3278 }
3279
3280 b = vlib_get_buffer (vm, bi);
3281
3282 /* leave some space for the encap headers */
3283 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3284
Filip Tehlar254b0362016-04-07 10:04:34 +02003285 /* get rlocs */
3286 rlocs = build_itr_rloc_list (lcm, loc_set);
3287
Filip Tehlard5fcc462016-10-17 16:20:18 +02003288 if (MR_MODE_SRC_DST == lcm->map_request_mode
3289 && GID_ADDR_SRC_DST != gid_address_type (deid))
Florin Corasdca88042016-09-14 16:01:38 +02003290 {
3291 gid_address_t sd;
3292 memset (&sd, 0, sizeof (sd));
3293 build_src_dst (&sd, seid, deid);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003294 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
3295 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02003296 }
3297 else
3298 {
3299 /* put lisp msg */
3300 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003301 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02003302 }
Florin Corase127a7e2016-02-18 22:20:01 +01003303
3304 /* push ecm: udp-ip-lisp */
3305 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
3306
Florin Corase127a7e2016-02-18 22:20:01 +01003307 /* push outer ip header */
Florin Corasddfafb82016-05-13 18:09:56 +02003308 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
Florin Corasa2157cf2016-08-16 21:09:14 +02003309 mr_ip);
Florin Corase127a7e2016-02-18 22:20:01 +01003310
3311 bi_res[0] = bi;
Filip Tehlar254b0362016-04-07 10:04:34 +02003312
Florin Corasa2157cf2016-08-16 21:09:14 +02003313 vec_free (rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01003314 return b;
3315}
3316
3317static void
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003318reset_pending_mr_counters (pending_map_request_t * r)
Florin Corase127a7e2016-02-18 22:20:01 +01003319{
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003320 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3321 r->retries_num = 0;
3322}
3323
3324static int
3325elect_map_resolver (lisp_cp_main_t * lcm)
3326{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003327 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003328
3329 vec_foreach (mr, lcm->map_resolvers)
Florin Corasa2157cf2016-08-16 21:09:14 +02003330 {
3331 if (!mr->is_down)
3332 {
3333 ip_address_copy (&lcm->active_map_resolver, &mr->address);
3334 lcm->do_map_resolver_election = 0;
3335 return 1;
3336 }
3337 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003338 return 0;
3339}
3340
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003341static void
3342free_map_register_records (mapping_t * maps)
3343{
3344 mapping_t *map;
3345 vec_foreach (map, maps) vec_free (map->locators);
3346
3347 vec_free (maps);
3348}
3349
3350static void
3351add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
3352 ip_address_t * probed_loc)
3353{
3354 u32 *li;
3355 locator_t *loc, new;
3356 ip_interface_address_t *ia = 0;
3357 void *addr;
3358 ip_address_t *new_ip = &gid_address_ip (&new.address);
3359
3360 m->locators = 0;
3361 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
3362 locator_set_index);
3363 vec_foreach (li, ls->locator_indices)
3364 {
3365 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
3366 new = loc[0];
3367 if (loc->local)
3368 {
3369 /* *INDENT-OFF* */
3370 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
3371 loc->sw_if_index, 1 /* unnumbered */,
3372 ({
3373 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
3374 ia);
3375 ip_address_set (new_ip, addr, IP4);
3376 }));
3377
3378 /* Add ipv6 locators */
3379 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
3380 loc->sw_if_index, 1 /* unnumbered */,
3381 ({
3382 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
3383 ia);
3384 ip_address_set (new_ip, addr, IP6);
3385 }));
3386 /* *INDENT-ON* */
3387
3388 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
3389 new.probed = 1;
3390 }
3391 vec_add1 (m->locators, new);
3392 }
3393}
3394
3395static mapping_t *
3396build_map_register_record_list (lisp_cp_main_t * lcm)
3397{
3398 mapping_t *recs = 0, rec, *m;
3399
3400 /* *INDENT-OFF* */
3401 pool_foreach(m, lcm->mapping_pool,
3402 {
3403 /* for now build only local mappings */
3404 if (!m->local)
3405 continue;
3406
3407 rec = m[0];
3408 add_locators (lcm, &rec, m->locator_set_index, NULL);
3409 vec_add1 (recs, rec);
3410 });
3411 /* *INDENT-ON* */
3412
3413 return recs;
3414}
3415
3416static int
3417update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
3418 lisp_key_type_t key_id, u8 * key,
3419 u16 auth_data_len, u32 msg_len)
3420{
3421 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
3422 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
3423
3424 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3425 (unsigned char *) map_reg_hdr, msg_len, NULL,
3426 NULL);
3427 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
3428
3429 return 0;
3430}
3431
3432static vlib_buffer_t *
3433build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
3434 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
3435 mapping_t * records, lisp_key_type_t key_id, u8 * key,
3436 u32 * bi_res)
3437{
3438 void *map_reg_hdr;
3439 vlib_buffer_t *b;
3440 u32 bi, auth_data_len = 0, msg_len = 0;
3441 vlib_main_t *vm = lcm->vlib_main;
3442
3443 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3444 {
3445 clib_warning ("Can't allocate buffer for Map-Register!");
3446 return 0;
3447 }
3448
3449 b = vlib_get_buffer (vm, bi);
3450
3451 /* leave some space for the encap headers */
3452 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3453
3454 auth_data_len = auth_data_len_by_key_id (key_id);
3455 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
3456 auth_data_len, nonce_res,
3457 &msg_len);
3458
3459 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
3460 msg_len);
3461
3462 /* push outer ip header */
3463 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
3464 ms_ip);
3465
3466 bi_res[0] = bi;
3467 return b;
3468}
3469
3470static int
3471get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
3472{
3473 lisp_msmr_t *mr;
3474 while (lcm->do_map_resolver_election
3475 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
3476 &lcm->active_map_resolver,
3477 ip)))
3478 {
3479 if (0 == elect_map_resolver (lcm))
3480 /* all map resolvers are down */
3481 {
3482 /* restart MR checking by marking all of them up */
3483 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
3484 return -1;
3485 }
3486 }
3487 return 0;
3488}
3489
3490static int
3491send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
3492 u32 local_locator_set_index, ip_address_t * sloc,
3493 ip_address_t * rloc)
3494{
3495 locator_set_t *ls;
3496 u32 bi;
3497 vlib_buffer_t *b;
3498 vlib_frame_t *f;
3499 u64 nonce = 0;
3500 u32 next_index, *to_next;
3501 gid_address_t *itr_rlocs;
3502
3503 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
3504 itr_rlocs = build_itr_rloc_list (lcm, ls);
3505
3506 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
3507 vec_free (itr_rlocs);
3508 if (!b)
3509 return -1;
3510
3511 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3512
3513 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3514 ip4_lookup_node.index : ip6_lookup_node.index;
3515
3516 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3517
3518 /* Enqueue the packet */
3519 to_next = vlib_frame_vector_args (f);
3520 to_next[0] = bi;
3521 f->n_vectors = 1;
3522 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3523
3524 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3525 return 0;
3526}
3527
3528static int
3529send_rloc_probes (lisp_cp_main_t * lcm)
3530{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003531 u8 lprio = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003532 mapping_t *lm;
3533 fwd_entry_t *e;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003534 locator_pair_t *lp;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003535 u32 si;
3536
3537 /* *INDENT-OFF* */
3538 pool_foreach (e, lcm->fwd_entry_pool,
3539 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003540 if (vec_len (e->locator_pairs) == 0)
3541 continue;
3542
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003543 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
3544 if (~0 == si)
3545 {
3546 clib_warning ("internal error: cannot find local eid %U in "
3547 "map-cache!", format_gid_address, &e->leid);
3548 continue;
3549 }
3550 lm = pool_elt_at_index (lcm->mapping_pool, si);
3551
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003552 /* get the best (lowest) priority */
3553 lprio = e->locator_pairs[0].priority;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003554
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003555 /* send rloc-probe for pair(s) with the best remote locator priority */
3556 vec_foreach (lp, e->locator_pairs)
3557 {
3558 if (lp->priority != lprio)
3559 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003560
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003561 /* get first remote locator */
3562 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
3563 &lp->rmt_loc);
3564 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003565 });
3566 /* *INDENT-ON* */
3567
3568 return 0;
3569}
3570
3571static int
3572send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
3573{
3574 u32 bi;
3575 vlib_buffer_t *b;
3576 ip_address_t sloc;
3577 vlib_frame_t *f;
3578 u64 nonce = 0;
3579 u32 next_index, *to_next;
3580 ip_address_t *ms = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003581 mapping_t *records, *r, *g;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003582
3583 // TODO: support multiple map servers and do election
3584 if (0 == vec_len (lcm->map_servers))
3585 return -1;
3586
3587 ms = &lcm->map_servers[0].address;
3588
3589 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
3590 {
3591 clib_warning ("no eligible interface address found for %U!",
3592 format_ip_address, &lcm->map_servers[0]);
3593 return -1;
3594 }
3595
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003596 records = build_map_register_record_list (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003597 if (!records)
3598 return -1;
3599
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003600 vec_foreach (r, records)
3601 {
3602 u8 *key = r->key;
3603 u8 key_id = r->key_id;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003604
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003605 if (!key)
3606 continue; /* no secret key -> map-register cannot be sent */
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003607
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003608 g = 0;
3609 // TODO: group mappings that share common key
3610 vec_add1 (g, r[0]);
3611 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, g,
3612 key_id, key, &bi);
3613 vec_free (g);
3614 if (!b)
3615 continue;
3616
3617 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3618
3619 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3620 ip4_lookup_node.index : ip6_lookup_node.index;
3621
3622 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3623
3624 /* Enqueue the packet */
3625 to_next = vlib_frame_vector_args (f);
3626 to_next[0] = bi;
3627 f->n_vectors = 1;
3628 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3629
3630 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
3631 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003632 free_map_register_records (records);
3633
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003634 return 0;
3635}
3636
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003637#define send_encapsulated_map_request(lcm, seid, deid, smr) \
3638 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
3639
3640#define resend_encapsulated_map_request(lcm, seid, deid, smr) \
3641 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
3642
3643static int
Florin Corasa2157cf2016-08-16 21:09:14 +02003644_send_encapsulated_map_request (lisp_cp_main_t * lcm,
3645 gid_address_t * seid, gid_address_t * deid,
3646 u8 is_smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003647{
Florin Corasa2157cf2016-08-16 21:09:14 +02003648 u32 next_index, bi = 0, *to_next, map_index;
3649 vlib_buffer_t *b;
3650 vlib_frame_t *f;
Florin Corase127a7e2016-02-18 22:20:01 +01003651 u64 nonce = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02003652 locator_set_t *loc_set;
3653 mapping_t *map;
3654 pending_map_request_t *pmr, *duplicate_pmr = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003655 ip_address_t sloc;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003656 u32 ls_index;
Florin Corase127a7e2016-02-18 22:20:01 +01003657
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003658 /* if there is already a pending request remember it */
Florin Corasa2157cf2016-08-16 21:09:14 +02003659
3660 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003661 pool_foreach(pmr, lcm->pending_map_requests_pool,
3662 ({
3663 if (!gid_address_cmp (&pmr->src, seid)
3664 && !gid_address_cmp (&pmr->dst, deid))
3665 {
3666 duplicate_pmr = pmr;
3667 break;
3668 }
3669 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02003670 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003671
3672 if (!is_resend && duplicate_pmr)
3673 {
3674 /* don't send the request if there is a pending map request already */
3675 return 0;
3676 }
3677
Florin Corase127a7e2016-02-18 22:20:01 +01003678 /* get locator-set for seid */
Filip Tehlar53f09e32016-05-19 14:25:44 +02003679 if (!lcm->lisp_pitr)
Florin Corase127a7e2016-02-18 22:20:01 +01003680 {
Filip Tehlar53f09e32016-05-19 14:25:44 +02003681 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
3682 if (map_index == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003683 {
3684 clib_warning ("No local mapping found in eid-table for %U!",
3685 format_gid_address, seid);
3686 return -1;
3687 }
Filip Tehlar53f09e32016-05-19 14:25:44 +02003688
3689 map = pool_elt_at_index (lcm->mapping_pool, map_index);
3690
3691 if (!map->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02003692 {
3693 clib_warning
3694 ("Mapping found for src eid %U is not marked as local!",
3695 format_gid_address, seid);
3696 return -1;
3697 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003698 ls_index = map->locator_set_index;
Filip Tehlar53f09e32016-05-19 14:25:44 +02003699 }
3700 else
3701 {
3702 map_index = lcm->pitr_map_index;
3703 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003704 ls_index = map->locator_set_index;
Florin Corase127a7e2016-02-18 22:20:01 +01003705 }
3706
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003707 /* overwrite locator set if map-request itr-rlocs configured */
3708 if (~0 != lcm->mreq_itr_rlocs)
3709 {
3710 ls_index = lcm->mreq_itr_rlocs;
3711 }
3712
3713 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01003714
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003715 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003716 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003717 if (duplicate_pmr)
3718 duplicate_pmr->to_be_removed = 1;
3719 return -1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003720 }
Florin Corasddfafb82016-05-13 18:09:56 +02003721
Florin Corase127a7e2016-02-18 22:20:01 +01003722 /* build the encapsulated map request */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003723 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
Florin Corasa2157cf2016-08-16 21:09:14 +02003724 &lcm->active_map_resolver,
3725 &sloc, is_smr_invoked, &nonce, &bi);
Florin Corase127a7e2016-02-18 22:20:01 +01003726
3727 if (!b)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003728 return -1;
Florin Corase127a7e2016-02-18 22:20:01 +01003729
Florin Corasf727db92016-06-23 15:01:58 +02003730 /* set fib index to default and lookup node */
Florin Corasa2157cf2016-08-16 21:09:14 +02003731 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3732 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3733 ip4_lookup_node.index : ip6_lookup_node.index;
Florin Corase127a7e2016-02-18 22:20:01 +01003734
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003735 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
Florin Corase127a7e2016-02-18 22:20:01 +01003736
3737 /* Enqueue the packet */
3738 to_next = vlib_frame_vector_args (f);
3739 to_next[0] = bi;
3740 f->n_vectors = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003741 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
Florin Corase127a7e2016-02-18 22:20:01 +01003742
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003743 if (duplicate_pmr)
3744 /* if there is a pending request already update it */
3745 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003746 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
3747 {
3748 /* remove the oldest nonce */
3749 u64 CLIB_UNUSED (tmp), *nonce_del;
3750 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
3751 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
3752 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
3753 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003754
Florin Corasf4691cd2016-08-15 19:16:32 +02003755 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003756 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02003757 duplicate_pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003758 }
3759 else
3760 {
3761 /* add map-request to pending requests table */
Florin Corasa2157cf2016-08-16 21:09:14 +02003762 pool_get (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003763 memset (pmr, 0, sizeof (*pmr));
3764 gid_address_copy (&pmr->src, seid);
3765 gid_address_copy (&pmr->dst, deid);
Florin Corasf4691cd2016-08-15 19:16:32 +02003766 clib_fifo_add1 (pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003767 pmr->is_smr_invoked = is_smr_invoked;
3768 reset_pending_mr_counters (pmr);
3769 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02003770 pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003771 }
3772
3773 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003774}
3775
3776static void
Florin Corasa2157cf2016-08-16 21:09:14 +02003777get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
Florin Corase127a7e2016-02-18 22:20:01 +01003778{
Florin Corasa2157cf2016-08-16 21:09:14 +02003779 ip4_header_t *ip4 = hdr;
3780 ip6_header_t *ip6;
Florin Corase127a7e2016-02-18 22:20:01 +01003781
3782 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
3783 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003784 ip_address_set (src, &ip4->src_address, IP4);
3785 ip_address_set (dst, &ip4->dst_address, IP4);
Florin Corase127a7e2016-02-18 22:20:01 +01003786 }
3787 else
3788 {
3789 ip6 = hdr;
Florin Corasa2157cf2016-08-16 21:09:14 +02003790 ip_address_set (src, &ip6->src_address, IP6);
3791 ip_address_set (dst, &ip6->dst_address, IP6);
Florin Corase127a7e2016-02-18 22:20:01 +01003792 }
3793}
3794
Filip Tehlar324112f2016-06-02 16:07:38 +02003795static u32
Florin Coras1a1adc72016-07-22 01:45:30 +02003796lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
Florin Corasa2157cf2016-08-16 21:09:14 +02003797 u8 version)
Filip Tehlar324112f2016-06-02 16:07:38 +02003798{
Florin Corasa2157cf2016-08-16 21:09:14 +02003799 uword *vnip;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003800 u32 vni = ~0, table_id = ~0;
Filip Tehlar324112f2016-06-02 16:07:38 +02003801
Florin Coras87e40692016-09-22 18:02:17 +02003802 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
3803 IP4 ? FIB_PROTOCOL_IP4 :
3804 FIB_PROTOCOL_IP6),
3805 vnet_buffer
3806 (b)->sw_if_index
3807 [VLIB_RX]);
Filip Tehlar324112f2016-06-02 16:07:38 +02003808
3809 vnip = hash_get (lcm->vni_by_table_id, table_id);
3810 if (vnip)
3811 vni = vnip[0];
3812 else
3813 clib_warning ("vrf %d is not mapped to any vni!", table_id);
3814
3815 return vni;
3816}
3817
Florin Coras1a1adc72016-07-22 01:45:30 +02003818always_inline u32
3819lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
3820{
Florin Corasa2157cf2016-08-16 21:09:14 +02003821 uword *vnip;
Florin Coras1a1adc72016-07-22 01:45:30 +02003822 u32 vni = ~0;
3823 u32 sw_if_index0;
3824
Florin Corasa2157cf2016-08-16 21:09:14 +02003825 l2input_main_t *l2im = &l2input_main;
3826 l2_input_config_t *config;
3827 l2_bridge_domain_t *bd_config;
Florin Coras1a1adc72016-07-22 01:45:30 +02003828
Florin Corasa2157cf2016-08-16 21:09:14 +02003829 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
3830 config = vec_elt_at_index (l2im->configs, sw_if_index0);
Florin Coras1a1adc72016-07-22 01:45:30 +02003831 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
3832
3833 vnip = hash_get (lcm->vni_by_bd_id, bd_config->bd_id);
3834 if (vnip)
3835 vni = vnip[0];
3836 else
Florin Corasa2157cf2016-08-16 21:09:14 +02003837 clib_warning ("bridge domain %d is not mapped to any vni!",
3838 config->bd_index);
Florin Coras1a1adc72016-07-22 01:45:30 +02003839
3840 return vni;
3841}
3842
3843always_inline void
Florin Corasa2157cf2016-08-16 21:09:14 +02003844get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
3845 gid_address_t * src, gid_address_t * dst)
Florin Coras1a1adc72016-07-22 01:45:30 +02003846{
3847 u32 vni = 0;
3848 u16 type;
3849
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003850 memset (src, 0, sizeof (*src));
3851 memset (dst, 0, sizeof (*dst));
Florin Corasa2157cf2016-08-16 21:09:14 +02003852 type = vnet_buffer (b)->lisp.overlay_afi;
Florin Coras1a1adc72016-07-22 01:45:30 +02003853
3854 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
3855 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003856 ip4_header_t *ip;
Florin Coras1a1adc72016-07-22 01:45:30 +02003857 u8 version, preflen;
3858
Florin Corasa2157cf2016-08-16 21:09:14 +02003859 gid_address_type (src) = GID_ADDR_IP_PREFIX;
3860 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
Florin Coras1a1adc72016-07-22 01:45:30 +02003861
3862 ip = vlib_buffer_get_current (b);
Florin Corasa2157cf2016-08-16 21:09:14 +02003863 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
Florin Coras1a1adc72016-07-22 01:45:30 +02003864
Florin Corasa2157cf2016-08-16 21:09:14 +02003865 version = gid_address_ip_version (src);
Florin Coras1a1adc72016-07-22 01:45:30 +02003866 preflen = ip_address_max_len (version);
Florin Corasa2157cf2016-08-16 21:09:14 +02003867 gid_address_ippref_len (src) = preflen;
3868 gid_address_ippref_len (dst) = preflen;
Florin Coras1a1adc72016-07-22 01:45:30 +02003869
3870 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
3871 gid_address_vni (dst) = vni;
3872 gid_address_vni (src) = vni;
3873 }
3874 else if (LISP_AFI_MAC == type)
3875 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003876 ethernet_header_t *eh;
Florin Coras1a1adc72016-07-22 01:45:30 +02003877
3878 eh = vlib_buffer_get_current (b);
3879
Florin Corasa2157cf2016-08-16 21:09:14 +02003880 gid_address_type (src) = GID_ADDR_MAC;
3881 gid_address_type (dst) = GID_ADDR_MAC;
3882 mac_copy (&gid_address_mac (src), eh->src_address);
3883 mac_copy (&gid_address_mac (dst), eh->dst_address);
Florin Coras1a1adc72016-07-22 01:45:30 +02003884
3885 /* get vni */
3886 vni = lisp_get_vni_from_buffer_eth (lcm, b);
3887
3888 gid_address_vni (dst) = vni;
3889 gid_address_vni (src) = vni;
3890 }
3891}
3892
Florin Corase127a7e2016-02-18 22:20:01 +01003893static uword
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003894lisp_cp_lookup_inline (vlib_main_t * vm,
3895 vlib_node_runtime_t * node,
3896 vlib_frame_t * from_frame, int overlay)
Florin Corase127a7e2016-02-18 22:20:01 +01003897{
Florin Corasa2157cf2016-08-16 21:09:14 +02003898 u32 *from, *to_next_drop, di, si;
3899 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01003900 u32 pkts_mapped = 0;
3901 uword n_left_from, n_left_to_next_drop;
3902
3903 from = vlib_frame_vector_args (from_frame);
3904 n_left_from = from_frame->n_vectors;
3905
3906 while (n_left_from > 0)
3907 {
3908 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
Florin Corasa2157cf2016-08-16 21:09:14 +02003909 to_next_drop, n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01003910
3911 while (n_left_from > 0 && n_left_to_next_drop > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003912 {
3913 u32 pi0;
3914 vlib_buffer_t *b0;
3915 gid_address_t src, dst;
Florin Corase127a7e2016-02-18 22:20:01 +01003916
Florin Corasa2157cf2016-08-16 21:09:14 +02003917 pi0 = from[0];
3918 from += 1;
3919 n_left_from -= 1;
3920 to_next_drop[0] = pi0;
3921 to_next_drop += 1;
3922 n_left_to_next_drop -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01003923
Florin Corasa2157cf2016-08-16 21:09:14 +02003924 b0 = vlib_get_buffer (vm, pi0);
3925 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003926 vnet_buffer (b0)->lisp.overlay_afi = overlay;
Florin Corase127a7e2016-02-18 22:20:01 +01003927
Florin Corasa2157cf2016-08-16 21:09:14 +02003928 /* src/dst eid pair */
3929 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst);
Filip Tehlar324112f2016-06-02 16:07:38 +02003930
Florin Corasa2157cf2016-08-16 21:09:14 +02003931 /* if we have remote mapping for destination already in map-chache
3932 add forwarding tunnel directly. If not send a map-request */
Florin Corasdca88042016-09-14 16:01:38 +02003933 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
3934 &src);
Florin Corasa2157cf2016-08-16 21:09:14 +02003935 if (~0 != di)
3936 {
3937 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3938 /* send a map-request also in case of negative mapping entry
3939 with corresponding action */
3940 if (m->action == LISP_SEND_MAP_REQUEST)
3941 {
3942 /* send map-request */
3943 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3944 0 /* is_resend */ );
3945 pkts_mapped++;
3946 }
3947 else
3948 {
3949 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3950 &src);
3951 if (~0 != si)
3952 {
3953 dp_add_fwd_entry (lcm, si, di);
3954 }
3955 }
3956 }
3957 else
3958 {
3959 /* send map-request */
3960 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3961 0 /* is_resend */ );
3962 pkts_mapped++;
3963 }
Florin Corase127a7e2016-02-18 22:20:01 +01003964
Florin Corasa2157cf2016-08-16 21:09:14 +02003965 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3966 {
3967 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3968 sizeof (*tr));
Andrej Kozemcak94e34762016-05-25 12:43:21 +02003969
Florin Corasa2157cf2016-08-16 21:09:14 +02003970 memset (tr, 0, sizeof (*tr));
3971 gid_address_copy (&tr->dst_eid, &dst);
Florin Coras5a1c11b2016-09-06 16:29:34 +02003972 ip_address_copy (&tr->map_resolver_ip,
3973 &lcm->active_map_resolver);
Florin Corasa2157cf2016-08-16 21:09:14 +02003974 }
3975 gid_address_free (&dst);
3976 gid_address_free (&src);
3977 }
Florin Corase127a7e2016-02-18 22:20:01 +01003978
Florin Corasa2157cf2016-08-16 21:09:14 +02003979 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
3980 n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01003981 }
3982 vlib_node_increment_counter (vm, node->node_index,
Florin Corasa2157cf2016-08-16 21:09:14 +02003983 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
3984 pkts_mapped);
Florin Corase127a7e2016-02-18 22:20:01 +01003985 return from_frame->n_vectors;
3986}
3987
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003988static uword
3989lisp_cp_lookup_ip4 (vlib_main_t * vm,
3990 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3991{
3992 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3993}
3994
3995static uword
3996lisp_cp_lookup_ip6 (vlib_main_t * vm,
3997 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3998{
3999 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
4000}
4001
Neale Ranns5e575b12016-10-03 09:40:25 +01004002static uword
4003lisp_cp_lookup_l2 (vlib_main_t * vm,
4004 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
4005{
4006 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
4007}
4008
Florin Corasa2157cf2016-08-16 21:09:14 +02004009/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01004010VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
4011 .function = lisp_cp_lookup_ip4,
4012 .name = "lisp-cp-lookup-ip4",
4013 .vector_size = sizeof (u32),
4014 .format_trace = format_lisp_cp_lookup_trace,
4015 .type = VLIB_NODE_TYPE_INTERNAL,
4016
4017 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4018 .error_strings = lisp_cp_lookup_error_strings,
4019
4020 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4021
4022 .next_nodes = {
4023 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Neale Ranns0bfe5d82016-08-25 15:29:12 +01004024 },
4025};
4026/* *INDENT-ON* */
4027
4028/* *INDENT-OFF* */
4029VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
4030 .function = lisp_cp_lookup_ip6,
4031 .name = "lisp-cp-lookup-ip6",
Florin Corase127a7e2016-02-18 22:20:01 +01004032 .vector_size = sizeof (u32),
4033 .format_trace = format_lisp_cp_lookup_trace,
4034 .type = VLIB_NODE_TYPE_INTERNAL,
4035
4036 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4037 .error_strings = lisp_cp_lookup_error_strings,
4038
4039 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4040
4041 .next_nodes = {
4042 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Neale Ranns5e575b12016-10-03 09:40:25 +01004043 },
4044};
4045/* *INDENT-ON* */
4046
4047/* *INDENT-OFF* */
4048VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
4049 .function = lisp_cp_lookup_l2,
4050 .name = "lisp-cp-lookup-l2",
4051 .vector_size = sizeof (u32),
4052 .format_trace = format_lisp_cp_lookup_trace,
4053 .type = VLIB_NODE_TYPE_INTERNAL,
4054
4055 .n_errors = LISP_CP_LOOKUP_N_ERROR,
4056 .error_strings = lisp_cp_lookup_error_strings,
4057
4058 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
4059
4060 .next_nodes = {
4061 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Florin Corase127a7e2016-02-18 22:20:01 +01004062 },
4063};
Florin Corasa2157cf2016-08-16 21:09:14 +02004064/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01004065
4066/* lisp_cp_input statistics */
Florin Corasf727db92016-06-23 15:01:58 +02004067#define foreach_lisp_cp_input_error \
4068_(DROP, "drop") \
Florin Corase127a7e2016-02-18 22:20:01 +01004069_(MAP_REPLIES_RECEIVED, "map-replies received")
4070
Florin Corasa2157cf2016-08-16 21:09:14 +02004071static char *lisp_cp_input_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01004072#define _(sym,string) string,
4073 foreach_lisp_cp_input_error
4074#undef _
4075};
4076
4077typedef enum
4078{
4079#define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02004080 foreach_lisp_cp_input_error
Florin Corase127a7e2016-02-18 22:20:01 +01004081#undef _
4082 LISP_CP_INPUT_N_ERROR,
4083} lisp_cp_input_error_t;
4084
4085typedef enum
4086{
4087 LISP_CP_INPUT_NEXT_DROP,
4088 LISP_CP_INPUT_N_NEXT,
4089} lisp_cp_input_next_t;
4090
4091typedef struct
4092{
4093 gid_address_t dst_eid;
4094 ip4_address_t map_resolver_ip;
4095} lisp_cp_input_trace_t;
4096
4097u8 *
4098format_lisp_cp_input_trace (u8 * s, va_list * args)
4099{
4100 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
4101 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02004102 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
4103 va_arg (*args, lisp_cp_input_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01004104
4105 s = format (s, "LISP-CP-INPUT: TODO");
4106 return s;
4107}
4108
Filip Tehlar9677a942016-11-28 10:23:31 +01004109static void
4110remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
4111{
4112 mapping_t *m;
4113
4114 m = pool_elt_at_index (lcm->mapping_pool, mi);
4115 lisp_add_del_adjacency (lcm, 0, &m->eid, 0 /* is_add */ );
4116 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
4117 0 /* is_static */ , 0);
4118 mapping_delete_timer (lcm, mi);
4119}
4120
4121static void
4122mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
4123 f64 expiration_time)
4124{
4125 mapping_t *m;
4126 u64 now = clib_cpu_time_now ();
4127 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
4128 u64 exp_clock_time = now + expiration_time * cpu_cps;
4129
4130 m = pool_elt_at_index (lcm->mapping_pool, mi);
4131
4132 m->timer_set = 1;
4133 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
4134}
4135
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004136static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004137map_records_arg_free (map_records_arg_t * a)
Florin Corase127a7e2016-02-18 22:20:01 +01004138{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004139 mapping_t *m;
4140 vec_foreach (m, a->mappings)
4141 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004142 vec_free (m->locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004143 gid_address_free (&m->eid);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004144 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004145
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004146 clib_mem_free (a);
4147}
4148
4149void *
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004150process_map_reply (map_records_arg_t * a)
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004151{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004152 mapping_t *m;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004153 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4154 u32 dst_map_index = 0;
4155 pending_map_request_t *pmr;
4156 u64 *noncep;
4157 uword *pmr_index;
4158
4159 if (a->is_rloc_probe)
4160 goto done;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004161
Florin Corase127a7e2016-02-18 22:20:01 +01004162 /* Check pending requests table and nonce */
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004163 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
Florin Corase127a7e2016-02-18 22:20:01 +01004164 if (!pmr_index)
4165 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004166 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004167 goto done;
Florin Corase127a7e2016-02-18 22:20:01 +01004168 }
Florin Corasa2157cf2016-08-16 21:09:14 +02004169 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01004170
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004171 vec_foreach (m, a->mappings)
4172 {
4173 /* insert/update mappings cache */
4174 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
4175 m->authoritative, m->ttl,
4176 1, 0 /* is_static */ , &dst_map_index);
Florin Corase127a7e2016-02-18 22:20:01 +01004177
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004178 /* try to program forwarding only if mapping saved or updated */
4179 if ((u32) ~ 0 != dst_map_index)
4180 {
4181 lisp_add_del_adjacency (lcm, &pmr->src, &m->eid, 1);
4182 if ((u32) ~ 0 != m->ttl)
4183 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
4184 }
4185 }
Florin Corase127a7e2016-02-18 22:20:01 +01004186
4187 /* remove pending map request entry */
Florin Corasa2157cf2016-08-16 21:09:14 +02004188
4189 /* *INDENT-OFF* */
Florin Corasf4691cd2016-08-15 19:16:32 +02004190 clib_fifo_foreach (noncep, pmr->nonces, ({
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004191 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
Florin Corasf4691cd2016-08-15 19:16:32 +02004192 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02004193 /* *INDENT-ON* */
4194
4195 clib_fifo_free (pmr->nonces);
4196 pool_put (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004197
4198done:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004199 map_records_arg_free (a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004200 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01004201}
4202
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004203static int
4204is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
4205 lisp_key_type_t key_id, u8 * key)
4206{
4207 u8 *auth_data = 0;
4208 u16 auth_data_len;
4209 int result;
4210
4211 auth_data_len = auth_data_len_by_key_id (key_id);
4212 if ((u16) ~ 0 == auth_data_len)
4213 {
4214 clib_warning ("invalid length for key_id %d!", key_id);
4215 return 0;
4216 }
4217
4218 /* save auth data */
4219 vec_validate (auth_data, auth_data_len - 1);
4220 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
4221
4222 /* clear auth data */
4223 memset (MNOTIFY_DATA (h), 0, auth_data_len);
4224
4225 /* get hash of the message */
4226 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
4227 (unsigned char *) h, msg_len, NULL, NULL);
4228
4229 result = memcmp (code, auth_data, auth_data_len);
4230
4231 vec_free (auth_data);
4232
4233 return !result;
4234}
4235
4236static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004237process_map_notify (map_records_arg_t * a)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004238{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004239 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004240 uword *pmr_index;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004241
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004242 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004243 if (!pmr_index)
4244 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004245 clib_warning ("No pending map-register entry with nonce %lu!",
4246 a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004247 return;
4248 }
4249
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004250 map_records_arg_free (a);
4251 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
4252}
4253
4254static mapping_t *
4255get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
4256{
4257 u32 mi;
4258
4259 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
4260 if (~0 == mi)
4261 {
4262 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
4263 e);
4264 return 0;
4265 }
4266 return pool_elt_at_index (lcm->mapping_pool, mi);
4267}
4268
4269/**
4270 * When map-notify is received it is necessary that all EIDs in the record
4271 * list share common key. The key is then used to verify authentication
4272 * data in map-notify message.
4273 */
4274static int
4275map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
4276 u32 key_id, u8 ** key_out)
4277{
4278 u32 i, len = vec_len (maps);
4279 mapping_t *m;
4280
4281 /* get key of the first mapping */
4282 m = get_mapping (lcm, &maps[0].eid);
4283 if (!m || !m->key)
4284 return -1;
4285
4286 key_out[0] = m->key;
4287
4288 for (i = 1; i < len; i++)
4289 {
4290 m = get_mapping (lcm, &maps[i].eid);
4291 if (!m || !m->key)
4292 return -1;
4293
4294 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
4295 {
4296 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
4297 return -1;
4298 }
4299 }
4300 return 0;
4301}
4302
4303static int
4304parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
4305{
4306 locator_t *locators = 0;
4307 u32 i, len;
4308 gid_address_t deid;
4309 mapping_t m;
4310 locator_t *loc;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004311
4312 /* parse record eid */
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004313 for (i = 0; i < count; i++)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004314 {
4315 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
4316 if (len == ~0)
4317 {
4318 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004319 vec_foreach (loc, locators) locator_free (loc);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004320 vec_free (locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004321 return -1;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004322 }
4323
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004324 m.locators = locators;
4325 gid_address_copy (&m.eid, &deid);
4326 vec_add1 (a->mappings, m);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004327 }
4328
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004329 return 0;
4330}
4331
4332static map_records_arg_t *
4333parse_map_notify (vlib_buffer_t * b)
4334{
4335 int rc = 0;
4336 map_notify_hdr_t *mnotif_hdr;
4337 lisp_key_type_t key_id;
4338 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4339 u8 *key = 0;
4340 gid_address_t deid;
4341 u16 auth_data_len = 0;
4342 u8 record_count;
4343 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
4344
4345 memset (a, 0, sizeof (*a));
4346 mnotif_hdr = vlib_buffer_get_current (b);
4347 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
4348 memset (&deid, 0, sizeof (deid));
4349
4350 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
4351 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
4352 auth_data_len = auth_data_len_by_key_id (key_id);
4353
4354 /* advance buffer by authentication data */
4355 vlib_buffer_pull (b, auth_data_len);
4356
4357 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
4358 rc = parse_map_records (b, a, record_count);
4359 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004360 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004361 map_records_arg_free (a);
4362 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004363 }
4364
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004365 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
4366 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004367 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004368 map_records_arg_free (a);
4369 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004370 }
4371
4372 /* verify authentication data */
4373 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004374 - (u8 *) mnotif_hdr, key_id, key))
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004375 {
4376 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004377 a->nonce);
4378 map_records_arg_free (a);
4379 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004380 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004381 return a;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004382}
4383
4384static vlib_buffer_t *
4385build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
4386 ip_address_t * dst, u64 nonce, u8 probe_bit,
4387 mapping_t * records, u16 dst_port, u32 * bi_res)
4388{
4389 vlib_buffer_t *b;
4390 u32 bi;
4391 vlib_main_t *vm = lcm->vlib_main;
4392
4393 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
4394 {
4395 clib_warning ("Can't allocate buffer for Map-Register!");
4396 return 0;
4397 }
4398
4399 b = vlib_get_buffer (vm, bi);
4400
4401 /* leave some space for the encap headers */
4402 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
4403
4404 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
4405
4406 /* push outer ip header */
4407 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
4408
4409 bi_res[0] = bi;
4410 return b;
4411}
4412
4413static int
4414send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
4415 u8 probe_bit, u64 nonce, u16 dst_port,
4416 ip_address_t * probed_loc)
4417{
4418 ip_address_t src;
4419 u32 bi;
4420 vlib_buffer_t *b;
4421 vlib_frame_t *f;
4422 u32 next_index, *to_next;
4423 mapping_t *records = 0, *m;
4424
4425 m = pool_elt_at_index (lcm->mapping_pool, mi);
4426 if (!m)
4427 return -1;
4428
4429 vec_add1 (records, m[0]);
4430 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
4431 memset (&src, 0, sizeof (src));
4432
4433 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
4434 {
4435 clib_warning ("can't find inteface address for %U", format_ip_address,
4436 dst);
4437 return -1;
4438 }
4439
4440 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
4441 &bi);
4442 if (!b)
4443 return -1;
4444 free_map_register_records (records);
4445
4446 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
4447 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
4448 ip4_lookup_node.index : ip6_lookup_node.index;
4449
4450 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
4451
4452 /* Enqueue the packet */
4453 to_next = vlib_frame_vector_args (f);
4454 to_next[0] = bi;
4455 f->n_vectors = 1;
4456 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
4457 return 0;
4458}
4459
Florin Corase127a7e2016-02-18 22:20:01 +01004460void
Florin Corasa2157cf2016-08-16 21:09:14 +02004461process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm,
4462 vlib_buffer_t * b)
Florin Corase127a7e2016-02-18 22:20:01 +01004463{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004464 u8 *ip_hdr = 0, *udp_hdr;
4465 ip4_header_t *ip4;
4466 ip6_header_t *ip6;
4467 ip_address_t *dst_loc = 0, probed_loc, src_loc;
4468 mapping_t m;
Florin Corasa2157cf2016-08-16 21:09:14 +02004469 map_request_hdr_t *mreq_hdr;
Florin Corase127a7e2016-02-18 22:20:01 +01004470 gid_address_t src, dst;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004471 u64 nonce;
Florin Corase127a7e2016-02-18 22:20:01 +01004472 u32 i, len = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004473 gid_address_t *itr_rlocs = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01004474
4475 mreq_hdr = vlib_buffer_get_current (b);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004476
4477 // TODO ugly workaround to find out whether LISP is carried by ip4 or 6
4478 // and needs to be fixed
4479 udp_hdr = (u8 *) vlib_buffer_get_current (b) - sizeof (udp_header_t);
4480 ip4 = (ip4_header_t *) (udp_hdr - sizeof (ip4_header_t));
4481 ip6 = (ip6_header_t *) (udp_hdr - sizeof (ip6_header_t));
4482
4483 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
4484 ip_hdr = (u8 *) ip4;
4485 else
4486 {
4487 u32 flags = clib_net_to_host_u32
4488 (ip6->ip_version_traffic_class_and_flow_label);
4489 if ((flags & 0xF0000000) == 0x60000000)
4490 ip_hdr = (u8 *) ip6;
4491 else
4492 {
4493 clib_warning ("internal error: cannot determine whether packet "
4494 "is ip4 or 6!");
4495 return;
4496 }
4497 }
4498
Florin Corasa2157cf2016-08-16 21:09:14 +02004499 vlib_buffer_pull (b, sizeof (*mreq_hdr));
Florin Corase127a7e2016-02-18 22:20:01 +01004500
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004501 nonce = MREQ_NONCE (mreq_hdr);
Florin Corase127a7e2016-02-18 22:20:01 +01004502
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004503 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
Florin Corasa2157cf2016-08-16 21:09:14 +02004504 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004505 clib_warning
4506 ("Only SMR Map-Requests and RLOC probe supported for now!");
Florin Corase127a7e2016-02-18 22:20:01 +01004507 return;
Florin Corasa2157cf2016-08-16 21:09:14 +02004508 }
Florin Corase127a7e2016-02-18 22:20:01 +01004509
4510 /* parse src eid */
4511 len = lisp_msg_parse_addr (b, &src);
4512 if (len == ~0)
4513 return;
4514
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004515 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
4516 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
Florin Corase127a7e2016-02-18 22:20:01 +01004517 if (len == ~0)
4518 return;
4519
4520 /* parse eid records and send SMR-invoked map-requests */
Florin Corasa2157cf2016-08-16 21:09:14 +02004521 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01004522 {
Florin Corasa2157cf2016-08-16 21:09:14 +02004523 memset (&dst, 0, sizeof (dst));
Florin Corase127a7e2016-02-18 22:20:01 +01004524 len = lisp_msg_parse_eid_rec (b, &dst);
4525 if (len == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02004526 {
4527 clib_warning ("Can't parse map-request EID-record");
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004528 goto done;
Florin Corasa2157cf2016-08-16 21:09:14 +02004529 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004530
4531 if (MREQ_SMR (mreq_hdr))
4532 {
4533 /* send SMR-invoked map-requests */
4534 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
4535 }
4536 else if (MREQ_RLOC_PROBE (mreq_hdr))
4537 {
4538 memset (&m, 0, sizeof (m));
4539 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
4540
4541 // TODO: select best locator; for now use the first one
4542 dst_loc = &gid_address_ip (&itr_rlocs[0]);
4543
4544 /* get src/dst IP addresses */
4545 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
4546
4547 // TODO get source port from buffer
4548 u16 src_port = LISP_CONTROL_PORT;
4549
4550 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
4551 src_port, &probed_loc);
4552 }
Florin Corase127a7e2016-02-18 22:20:01 +01004553 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004554
4555done:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004556 vec_free (itr_rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01004557}
4558
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004559static map_records_arg_t *
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004560parse_map_reply (vlib_buffer_t * b)
4561{
4562 locator_t probed;
4563 gid_address_t deid;
4564 void *h;
4565 u32 i, len = 0;
4566 mapping_t m;
4567 map_reply_hdr_t *mrep_hdr;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004568 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004569 memset (a, 0, sizeof (*a));
4570 locator_t *locators;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004571
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004572 mrep_hdr = vlib_buffer_get_current (b);
4573 a->nonce = MREP_NONCE (mrep_hdr);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004574 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004575 vlib_buffer_pull (b, sizeof (*mrep_hdr));
4576
4577 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
4578 {
4579 memset (&m, 0, sizeof (m));
4580 locators = 0;
4581 h = vlib_buffer_get_current (b);
4582
4583 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
4584 m.action = MAP_REC_ACTION (h);
4585 m.authoritative = MAP_REC_AUTH (h);
4586
4587 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
4588 if (len == ~0)
4589 {
4590 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004591 map_records_arg_free (a);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004592 return 0;
4593 }
4594
4595 m.locators = locators;
4596 gid_address_copy (&m.eid, &deid);
4597 vec_add1 (a->mappings, m);
4598 }
4599 return a;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004600}
4601
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004602static void
4603queue_map_reply_for_processing (map_records_arg_t * a)
4604{
4605 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (a));
4606}
4607
4608static void
4609queue_map_notify_for_processing (map_records_arg_t * a)
4610{
4611 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
4612}
4613
Florin Corase127a7e2016-02-18 22:20:01 +01004614static uword
4615lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Corasa2157cf2016-08-16 21:09:14 +02004616 vlib_frame_t * from_frame)
Florin Corase127a7e2016-02-18 22:20:01 +01004617{
Florin Corasa2157cf2016-08-16 21:09:14 +02004618 u32 n_left_from, *from, *to_next_drop;
Florin Corase127a7e2016-02-18 22:20:01 +01004619 lisp_msg_type_e type;
Florin Corasa2157cf2016-08-16 21:09:14 +02004620 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004621 map_records_arg_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01004622
4623 from = vlib_frame_vector_args (from_frame);
4624 n_left_from = from_frame->n_vectors;
4625
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004626
Florin Corase127a7e2016-02-18 22:20:01 +01004627 while (n_left_from > 0)
4628 {
4629 u32 n_left_to_next_drop;
4630
4631 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
Florin Corasa2157cf2016-08-16 21:09:14 +02004632 to_next_drop, n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01004633 while (n_left_from > 0 && n_left_to_next_drop > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02004634 {
4635 u32 bi0;
4636 vlib_buffer_t *b0;
Florin Corase127a7e2016-02-18 22:20:01 +01004637
Florin Corasa2157cf2016-08-16 21:09:14 +02004638 bi0 = from[0];
4639 from += 1;
4640 n_left_from -= 1;
4641 to_next_drop[0] = bi0;
4642 to_next_drop += 1;
4643 n_left_to_next_drop -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01004644
Florin Corasa2157cf2016-08-16 21:09:14 +02004645 b0 = vlib_get_buffer (vm, bi0);
Florin Corase127a7e2016-02-18 22:20:01 +01004646
Florin Corasa2157cf2016-08-16 21:09:14 +02004647 type = lisp_msg_type (vlib_buffer_get_current (b0));
4648 switch (type)
4649 {
4650 case LISP_MAP_REPLY:
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004651 a = parse_map_reply (b0);
4652 if (a)
4653 queue_map_reply_for_processing (a);
Florin Corasa2157cf2016-08-16 21:09:14 +02004654 break;
4655 case LISP_MAP_REQUEST:
4656 process_map_request (vm, lcm, b0);
4657 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004658 case LISP_MAP_NOTIFY:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01004659 a = parse_map_notify (b0);
4660 if (a)
4661 queue_map_notify_for_processing (a);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004662 break;
Florin Corasa2157cf2016-08-16 21:09:14 +02004663 default:
4664 clib_warning ("Unsupported LISP message type %d", type);
4665 break;
4666 }
Florin Corase127a7e2016-02-18 22:20:01 +01004667
Florin Corasa2157cf2016-08-16 21:09:14 +02004668 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
Florin Corase127a7e2016-02-18 22:20:01 +01004669
Florin Corasa2157cf2016-08-16 21:09:14 +02004670 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
4671 {
Florin Corase127a7e2016-02-18 22:20:01 +01004672
Florin Corasa2157cf2016-08-16 21:09:14 +02004673 }
4674 }
Florin Corase127a7e2016-02-18 22:20:01 +01004675
Florin Corasa2157cf2016-08-16 21:09:14 +02004676 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
4677 n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01004678 }
4679 return from_frame->n_vectors;
4680}
4681
Florin Corasa2157cf2016-08-16 21:09:14 +02004682/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01004683VLIB_REGISTER_NODE (lisp_cp_input_node) = {
4684 .function = lisp_cp_input,
4685 .name = "lisp-cp-input",
4686 .vector_size = sizeof (u32),
4687 .format_trace = format_lisp_cp_input_trace,
4688 .type = VLIB_NODE_TYPE_INTERNAL,
4689
4690 .n_errors = LISP_CP_INPUT_N_ERROR,
4691 .error_strings = lisp_cp_input_error_strings,
4692
4693 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
4694
4695 .next_nodes = {
4696 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
4697 },
4698};
Florin Corasa2157cf2016-08-16 21:09:14 +02004699/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01004700
4701clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02004702lisp_cp_init (vlib_main_t * vm)
Florin Corase127a7e2016-02-18 22:20:01 +01004703{
Florin Corasa2157cf2016-08-16 21:09:14 +02004704 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4705 clib_error_t *error = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01004706
4707 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
4708 return error;
4709
4710 lcm->im4 = &ip4_main;
4711 lcm->im6 = &ip6_main;
4712 lcm->vlib_main = vm;
Florin Corasa2157cf2016-08-16 21:09:14 +02004713 lcm->vnet_main = vnet_get_main ();
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02004714 lcm->mreq_itr_rlocs = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02004715 lcm->lisp_pitr = 0;
Florin Coras5a1c11b2016-09-06 16:29:34 +02004716 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
Florin Corase127a7e2016-02-18 22:20:01 +01004717
4718 gid_dictionary_init (&lcm->mapping_index_by_gid);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004719 lcm->do_map_resolver_election = 1;
Florin Corasdca88042016-09-14 16:01:38 +02004720 lcm->map_request_mode = MR_MODE_DST_ONLY;
Florin Corase127a7e2016-02-18 22:20:01 +01004721
Florin Coras577c3552016-04-21 00:45:40 +02004722 /* default vrf mapped to vni 0 */
Florin Corasa2157cf2016-08-16 21:09:14 +02004723 hash_set (lcm->table_id_by_vni, 0, 0);
4724 hash_set (lcm->vni_by_table_id, 0, 0);
Florin Coras577c3552016-04-21 00:45:40 +02004725
Florin Corase127a7e2016-02-18 22:20:01 +01004726 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
Florin Corasa2157cf2016-08-16 21:09:14 +02004727 lisp_cp_input_node.index, 1 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01004728 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
Florin Corasa2157cf2016-08-16 21:09:14 +02004729 lisp_cp_input_node.index, 0 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01004730
Filip Tehlar9677a942016-11-28 10:23:31 +01004731 u64 now = clib_cpu_time_now ();
4732 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
Florin Corase127a7e2016-02-18 22:20:01 +01004733 return 0;
4734}
4735
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004736static void *
Florin Corasa2157cf2016-08-16 21:09:14 +02004737send_map_request_thread_fn (void *arg)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004738{
Florin Corasa2157cf2016-08-16 21:09:14 +02004739 map_request_args_t *a = arg;
4740 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004741
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004742 if (a->is_resend)
4743 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
4744 else
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01004745 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004746
4747 return 0;
4748}
4749
4750static int
4751queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +02004752 u8 smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004753{
4754 map_request_args_t a;
4755
4756 a.is_resend = is_resend;
4757 gid_address_copy (&a.seid, seid);
4758 gid_address_copy (&a.deid, deid);
4759 a.smr_invoked = smr_invoked;
4760
4761 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
Florin Corasa2157cf2016-08-16 21:09:14 +02004762 (u8 *) & a, sizeof (a));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004763 return 0;
4764}
4765
4766/**
4767 * Take an action with a pending map request depending on expiration time
4768 * and re-try counters.
4769 */
4770static void
4771update_pending_request (pending_map_request_t * r, f64 dt)
4772{
Florin Corasa2157cf2016-08-16 21:09:14 +02004773 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004774 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004775
4776 if (r->time_to_expire - dt < 0)
4777 /* it's time to decide what to do with this pending request */
4778 {
4779 if (r->retries_num >= NUMBER_OF_RETRIES)
Florin Corasa2157cf2016-08-16 21:09:14 +02004780 /* too many retries -> assume current map resolver is not available */
4781 {
4782 mr = get_map_resolver (&lcm->active_map_resolver);
4783 if (!mr)
4784 {
4785 clib_warning ("Map resolver %U not found - probably deleted "
4786 "by the user recently.", format_ip_address,
4787 &lcm->active_map_resolver);
4788 }
4789 else
4790 {
4791 clib_warning ("map resolver %U is unreachable, ignoring",
4792 format_ip_address, &lcm->active_map_resolver);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004793
Florin Corasa2157cf2016-08-16 21:09:14 +02004794 /* mark current map resolver unavailable so it won't be
4795 * selected next time */
4796 mr->is_down = 1;
4797 mr->last_update = vlib_time_now (lcm->vlib_main);
4798 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004799
Florin Corasa2157cf2016-08-16 21:09:14 +02004800 reset_pending_mr_counters (r);
4801 elect_map_resolver (lcm);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004802
Florin Corasa2157cf2016-08-16 21:09:14 +02004803 /* try to find a next eligible map resolver and re-send */
4804 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4805 1 /* resend */ );
4806 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004807 else
Florin Corasa2157cf2016-08-16 21:09:14 +02004808 {
4809 /* try again */
4810 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
4811 1 /* resend */ );
4812 r->retries_num++;
4813 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
4814 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004815 }
4816 else
4817 r->time_to_expire -= dt;
4818}
4819
4820static void
4821remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4822{
Florin Corasa2157cf2016-08-16 21:09:14 +02004823 u64 *nonce;
4824 pending_map_request_t *pmr;
4825 u32 *to_be_removed = 0, *pmr_index;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004826
Florin Corasa2157cf2016-08-16 21:09:14 +02004827 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004828 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02004829 ({
4830 if (pmr->to_be_removed)
4831 {
4832 clib_fifo_foreach (nonce, pmr->nonces, ({
4833 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4834 }));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004835
Florin Corasa2157cf2016-08-16 21:09:14 +02004836 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4837 }
4838 }));
4839 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004840
4841 vec_foreach (pmr_index, to_be_removed)
4842 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
4843
4844 vec_free (to_be_removed);
4845}
4846
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004847static void
4848update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4849{
4850 static f64 time_left = RLOC_PROBING_INTERVAL;
4851
4852 if (!lcm->is_enabled || !lcm->rloc_probing)
4853 return;
4854
4855 time_left -= dt;
4856 if (time_left <= 0)
4857 {
4858 time_left = RLOC_PROBING_INTERVAL;
4859 send_rloc_probes (lcm);
4860 }
4861}
4862
4863static void
4864update_map_register (lisp_cp_main_t * lcm, f64 dt)
4865{
4866 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4867 static u64 mreg_sent_counter = 0;
4868
4869 if (!lcm->is_enabled || !lcm->map_registering)
4870 return;
4871
4872 time_left -= dt;
4873 if (time_left <= 0)
4874 {
4875 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4876 time_left = MAP_REGISTER_INTERVAL;
4877 else
4878 {
4879 mreg_sent_counter++;
4880 time_left = QUICK_MAP_REGISTER_INTERVAL;
4881 }
4882 send_map_register (lcm, 1 /* want map notify */ );
4883 }
4884}
4885
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004886static uword
4887send_map_resolver_service (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02004888 vlib_node_runtime_t * rt, vlib_frame_t * f)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004889{
Filip Tehlar9677a942016-11-28 10:23:31 +01004890 u32 *expired = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004891 f64 period = 2.0;
Florin Corasa2157cf2016-08-16 21:09:14 +02004892 pending_map_request_t *pmr;
4893 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004894
4895 while (1)
4896 {
4897 vlib_process_wait_for_event_or_clock (vm, period);
4898
4899 /* currently no signals are expected - just wait for clock */
4900 (void) vlib_process_get_events (vm, 0);
4901
Florin Corasa2157cf2016-08-16 21:09:14 +02004902 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004903 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02004904 ({
4905 if (!pmr->to_be_removed)
4906 update_pending_request (pmr, period);
4907 }));
4908 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004909
4910 remove_dead_pending_map_requests (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004911
4912 update_map_register (lcm, period);
4913 update_rloc_probing (lcm, period);
Filip Tehlar9677a942016-11-28 10:23:31 +01004914
4915 u64 now = clib_cpu_time_now ();
4916
4917 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
4918 if (vec_len (expired) > 0)
4919 {
4920 u32 *mi = 0;
4921 vec_foreach (mi, expired)
4922 {
4923 remove_expired_mapping (lcm, mi[0]);
4924 }
4925 _vec_len (expired) = 0;
4926 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004927 }
4928
4929 /* unreachable */
4930 return 0;
4931}
4932
Florin Corasa2157cf2016-08-16 21:09:14 +02004933/* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004934VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
4935 .function = send_map_resolver_service,
4936 .type = VLIB_NODE_TYPE_PROCESS,
4937 .name = "lisp-retry-service",
4938 .process_log2_n_stack_bytes = 16,
4939};
Florin Corasa2157cf2016-08-16 21:09:14 +02004940/* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004941
Florin Corasa2157cf2016-08-16 21:09:14 +02004942VLIB_INIT_FUNCTION (lisp_cp_init);
4943
4944/*
4945 * fd.io coding-style-patch-verification: ON
4946 *
4947 * Local Variables:
4948 * eval: (c-set-style "gnu")
4949 * End:
4950 */