blob: cea9255628458973f76a08c4869ce0af4c14403a [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>
Neale Ranns5e575b12016-10-03 09:40:25 +010020#include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
21#include <vnet/lisp-gpe/lisp_gpe_tenant.h>
Filip Tehlar4868ff62017-03-09 16:48:39 +010022#include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010023#include <vnet/fib/fib_entry.h>
24#include <vnet/fib/fib_table.h>
Filip Tehlard5a65db2017-05-17 17:21:10 +020025#include <vnet/ethernet/arp_packet.h>
26#include <vnet/ethernet/packet.h>
Florin Corase127a7e2016-02-18 22:20:01 +010027
Filip Tehlar397fd7d2016-10-26 14:31:24 +020028#include <openssl/evp.h>
29#include <openssl/hmac.h>
30
Florin Corasf3dc11a2017-01-24 03:13:43 -080031lisp_cp_main_t lisp_control_main;
32
Filip Tehlarcda70662017-01-16 10:30:03 +010033u8 *format_lisp_cp_input_trace (u8 * s, va_list * args);
34
35typedef enum
36{
37 LISP_CP_INPUT_NEXT_DROP,
38 LISP_CP_INPUT_N_NEXT,
39} lisp_cp_input_next_t;
40
Filip Tehlara5abdeb2016-07-18 17:35:40 +020041typedef struct
42{
43 u8 is_resend;
44 gid_address_t seid;
45 gid_address_t deid;
46 u8 smr_invoked;
47} map_request_args_t;
48
Florin Corasdca88042016-09-14 16:01:38 +020049u8
50vnet_lisp_get_map_request_mode (void)
51{
52 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
53 return lcm->map_request_mode;
54}
55
Filip Tehlar397fd7d2016-10-26 14:31:24 +020056static u16
57auth_data_len_by_key_id (lisp_key_type_t key_id)
58{
59 switch (key_id)
60 {
61 case HMAC_SHA_1_96:
62 return SHA1_AUTH_DATA_LEN;
63 case HMAC_SHA_256_128:
64 return SHA256_AUTH_DATA_LEN;
65 default:
66 clib_warning ("unsupported key type: %d!", key_id);
67 return (u16) ~ 0;
68 }
69 return (u16) ~ 0;
70}
71
72static const EVP_MD *
73get_encrypt_fcn (lisp_key_type_t key_id)
74{
75 switch (key_id)
76 {
77 case HMAC_SHA_1_96:
78 return EVP_sha1 ();
79 case HMAC_SHA_256_128:
80 return EVP_sha256 ();
81 default:
82 clib_warning ("unsupported encryption key type: %d!", key_id);
83 break;
84 }
85 return 0;
86}
87
Filip Tehlara5abdeb2016-07-18 17:35:40 +020088static int
89queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +020090 u8 smr_invoked, u8 is_resend);
Filip Tehlara5abdeb2016-07-18 17:35:40 +020091
Florin Corasf727db92016-06-23 15:01:58 +020092ip_interface_address_t *
Florin Corasa2157cf2016-08-16 21:09:14 +020093ip_interface_get_first_interface_address (ip_lookup_main_t * lm,
94 u32 sw_if_index, u8 loop)
Florin Corasf727db92016-06-23 15:01:58 +020095{
96 vnet_main_t *vnm = vnet_get_main ();
Florin Corasa2157cf2016-08-16 21:09:14 +020097 vnet_sw_interface_t *swif = vnet_get_sw_interface (vnm, sw_if_index);
Florin Corasf727db92016-06-23 15:01:58 +020098 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
99 sw_if_index = swif->unnumbered_sw_if_index;
100 u32 ia =
Florin Corasa2157cf2016-08-16 21:09:14 +0200101 (vec_len ((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
102 vec_elt ((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
103 (u32) ~ 0;
104 return pool_elt_at_index ((lm)->if_address_pool, ia);
Florin Corasf727db92016-06-23 15:01:58 +0200105}
Filip Tehlar215104e2016-05-10 16:58:29 +0200106
Florin Corasf727db92016-06-23 15:01:58 +0200107void *
108ip_interface_get_first_address (ip_lookup_main_t * lm, u32 sw_if_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200109 u8 version)
Florin Corasf727db92016-06-23 15:01:58 +0200110{
Florin Corasa2157cf2016-08-16 21:09:14 +0200111 ip_interface_address_t *ia;
Filip Tehlar195bcee2016-05-13 17:37:35 +0200112
Florin Corasf727db92016-06-23 15:01:58 +0200113 ia = ip_interface_get_first_interface_address (lm, sw_if_index, 1);
114 if (!ia)
115 return 0;
116 return ip_interface_address_get_address (lm, ia);
117}
Filip Tehlar195bcee2016-05-13 17:37:35 +0200118
Florin Corase127a7e2016-02-18 22:20:01 +0100119int
Florin Corasf727db92016-06-23 15:01:58 +0200120ip_interface_get_first_ip_address (lisp_cp_main_t * lcm, u32 sw_if_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200121 u8 version, ip_address_t * result)
Florin Corasf727db92016-06-23 15:01:58 +0200122{
Florin Corasa2157cf2016-08-16 21:09:14 +0200123 ip_lookup_main_t *lm;
124 void *addr;
Florin Corasf727db92016-06-23 15:01:58 +0200125
126 lm = (version == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
127 addr = ip_interface_get_first_address (lm, sw_if_index, version);
128 if (!addr)
129 return 0;
130
131 ip_address_set (result, addr, version);
132 return 1;
133}
134
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100135/**
136 * convert from a LISP address to a FIB prefix
137 */
138void
139ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
Florin Corasf727db92016-06-23 15:01:58 +0200140{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100141 if (addr->version == IP4)
142 {
143 prefix->fp_len = 32;
144 prefix->fp_proto = FIB_PROTOCOL_IP4;
145 memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
146 memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
147 }
Florin Corasf727db92016-06-23 15:01:58 +0200148 else
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100149 {
150 prefix->fp_len = 128;
151 prefix->fp_proto = FIB_PROTOCOL_IP6;
152 memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
153 }
Florin Corasf727db92016-06-23 15:01:58 +0200154}
155
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100156/**
157 * convert from a LISP to a FIB prefix
158 */
159void
160ip_prefix_to_fib_prefix (const ip_prefix_t * ip_prefix,
161 fib_prefix_t * fib_prefix)
Florin Corasf727db92016-06-23 15:01:58 +0200162{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100163 ip_address_to_fib_prefix (&ip_prefix->addr, fib_prefix);
164 fib_prefix->fp_len = ip_prefix->len;
Florin Corasf727db92016-06-23 15:01:58 +0200165}
166
167/**
168 * Find the sw_if_index of the interface that would be used to egress towards
169 * dst.
170 */
171u32
172ip_fib_get_egress_iface_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst)
173{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100174 fib_node_index_t fei;
175 fib_prefix_t prefix;
Florin Corasf727db92016-06-23 15:01:58 +0200176
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100177 ip_address_to_fib_prefix (dst, &prefix);
Florin Corasf727db92016-06-23 15:01:58 +0200178
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100179 fei = fib_table_lookup (0, &prefix);
180
181 return (fib_entry_get_resolving_interface (fei));
Florin Corasf727db92016-06-23 15:01:58 +0200182}
183
184/**
185 * Find first IP of the interface that would be used to egress towards dst.
186 * Returns 1 if the address is found 0 otherwise.
187 */
188int
189ip_fib_get_first_egress_ip_for_dst (lisp_cp_main_t * lcm, ip_address_t * dst,
Florin Corasa2157cf2016-08-16 21:09:14 +0200190 ip_address_t * result)
Florin Corasf727db92016-06-23 15:01:58 +0200191{
192 u32 si;
Florin Corasa2157cf2016-08-16 21:09:14 +0200193 ip_lookup_main_t *lm;
194 void *addr = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200195 u8 ipver;
196
Florin Corasa2157cf2016-08-16 21:09:14 +0200197 ASSERT (result != 0);
Florin Corasf727db92016-06-23 15:01:58 +0200198
Florin Corasa2157cf2016-08-16 21:09:14 +0200199 ipver = ip_addr_version (dst);
Florin Corasf727db92016-06-23 15:01:58 +0200200
201 lm = (ipver == IP4) ? &lcm->im4->lookup_main : &lcm->im6->lookup_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100202 si = ip_fib_get_egress_iface_for_dst (lcm, dst);
Florin Corasf727db92016-06-23 15:01:58 +0200203
Florin Corasa2157cf2016-08-16 21:09:14 +0200204 if ((u32) ~ 0 == si)
Florin Corasf727db92016-06-23 15:01:58 +0200205 return 0;
206
207 /* find the first ip address */
208 addr = ip_interface_get_first_address (lm, si, ipver);
209 if (0 == addr)
210 return 0;
211
212 ip_address_set (result, addr, ipver);
213 return 1;
214}
215
216static int
Florin Coras1a1adc72016-07-22 01:45:30 +0200217dp_add_del_iface (lisp_cp_main_t * lcm, u32 vni, u8 is_l2, u8 is_add)
Florin Corasf727db92016-06-23 15:01:58 +0200218{
Neale Ranns5e575b12016-10-03 09:40:25 +0100219 uword *dp_table;
Florin Corasf727db92016-06-23 15:01:58 +0200220
Florin Coras1a1adc72016-07-22 01:45:30 +0200221 if (!is_l2)
Florin Corasf727db92016-06-23 15:01:58 +0200222 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200223 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200224
225 if (!dp_table)
Florin Corasa2157cf2016-08-16 21:09:14 +0200226 {
227 clib_warning ("vni %d not associated to a vrf!", vni);
228 return VNET_API_ERROR_INVALID_VALUE;
229 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200230 }
231 else
232 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200233 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200234 if (!dp_table)
Florin Corasa2157cf2016-08-16 21:09:14 +0200235 {
236 clib_warning ("vni %d not associated to a bridge domain!", vni);
237 return VNET_API_ERROR_INVALID_VALUE;
238 }
Florin Corasf727db92016-06-23 15:01:58 +0200239 }
240
Florin Corasf727db92016-06-23 15:01:58 +0200241 /* enable/disable data-plane interface */
242 if (is_add)
243 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100244 if (is_l2)
245 lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table[0]);
246 else
247 lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200248 }
249 else
250 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100251 if (is_l2)
252 lisp_gpe_tenant_l2_iface_unlock (vni);
253 else
254 lisp_gpe_tenant_l3_iface_unlock (vni);
Florin Corasf727db92016-06-23 15:01:58 +0200255 }
256
257 return 0;
258}
259
260static void
261dp_del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
262{
Florin Corasa2157cf2016-08-16 21:09:14 +0200263 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
264 fwd_entry_t *fe = 0;
265 uword *feip = 0;
266 memset (a, 0, sizeof (*a));
Florin Corasf727db92016-06-23 15:01:58 +0200267
Florin Corasa2157cf2016-08-16 21:09:14 +0200268 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +0200269 if (!feip)
270 return;
271
Florin Corasa2157cf2016-08-16 21:09:14 +0200272 fe = pool_elt_at_index (lcm->fwd_entry_pool, feip[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200273
274 /* delete dp fwd entry */
275 u32 sw_if_index;
276 a->is_add = 0;
Florin Corasbb5c22f2016-08-02 02:31:03 +0200277 a->locator_pairs = fe->locator_pairs;
Filip Tehlar2fdaece2016-09-28 14:27:59 +0200278 a->vni = gid_address_vni (&fe->reid);
279 gid_address_copy (&a->rmt_eid, &fe->reid);
Filip Tehlard5fcc462016-10-17 16:20:18 +0200280 if (fe->is_src_dst)
281 gid_address_copy (&a->lcl_eid, &fe->leid);
Florin Corasf727db92016-06-23 15:01:58 +0200282
Filip Tehlar21511912017-04-07 10:41:42 +0200283 vnet_lisp_gpe_del_fwd_counters (a, feip[0]);
Florin Corasf727db92016-06-23 15:01:58 +0200284 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
285
286 /* delete entry in fwd table */
Florin Corasa2157cf2016-08-16 21:09:14 +0200287 hash_unset (lcm->fwd_entry_by_mapping_index, dst_map_index);
288 vec_free (fe->locator_pairs);
289 pool_put (lcm->fwd_entry_pool, fe);
Florin Corasf727db92016-06-23 15:01:58 +0200290}
291
292/**
293 * Finds first remote locator with best (lowest) priority that has a local
294 * peer locator with an underlying route to it.
295 *
296 */
297static u32
Florin Corasa2157cf2016-08-16 21:09:14 +0200298get_locator_pairs (lisp_cp_main_t * lcm, mapping_t * lcl_map,
299 mapping_t * rmt_map, locator_pair_t ** locator_pairs)
Florin Corasf727db92016-06-23 15:01:58 +0200300{
Florin Coras3590ac52016-08-08 16:04:26 +0200301 u32 i, limitp = 0, li, found = 0, esi;
Florin Corasa2157cf2016-08-16 21:09:14 +0200302 locator_set_t *rmt_ls, *lcl_ls;
303 ip_address_t _lcl_addr, *lcl_addr = &_lcl_addr;
304 locator_t *lp, *rmt = 0;
305 uword *checked = 0;
Florin Corasbb5c22f2016-08-02 02:31:03 +0200306 locator_pair_t pair;
Florin Corasf727db92016-06-23 15:01:58 +0200307
Florin Corasa2157cf2016-08-16 21:09:14 +0200308 rmt_ls =
309 pool_elt_at_index (lcm->locator_set_pool, rmt_map->locator_set_index);
310 lcl_ls =
311 pool_elt_at_index (lcm->locator_set_pool, lcl_map->locator_set_index);
Florin Corasf727db92016-06-23 15:01:58 +0200312
Florin Corasa2157cf2016-08-16 21:09:14 +0200313 if (!rmt_ls || vec_len (rmt_ls->locator_indices) == 0)
Florin Corasf727db92016-06-23 15:01:58 +0200314 return 0;
315
Florin Coras3590ac52016-08-08 16:04:26 +0200316 while (1)
Florin Corasf727db92016-06-23 15:01:58 +0200317 {
318 rmt = 0;
319
320 /* find unvisited remote locator with best priority */
Florin Corasa2157cf2016-08-16 21:09:14 +0200321 for (i = 0; i < vec_len (rmt_ls->locator_indices); i++)
322 {
323 if (0 != hash_get (checked, i))
324 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200325
Florin Corasa2157cf2016-08-16 21:09:14 +0200326 li = vec_elt (rmt_ls->locator_indices, i);
327 lp = pool_elt_at_index (lcm->locator_pool, li);
Florin Corasf727db92016-06-23 15:01:58 +0200328
Florin Corasa2157cf2016-08-16 21:09:14 +0200329 /* we don't support non-IP locators for now */
330 if (gid_address_type (&lp->address) != GID_ADDR_IP_PREFIX)
331 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200332
Florin Corasa2157cf2016-08-16 21:09:14 +0200333 if ((found && lp->priority == limitp)
334 || (!found && lp->priority >= limitp))
335 {
336 rmt = lp;
Florin Coras3590ac52016-08-08 16:04:26 +0200337
Florin Corasa2157cf2016-08-16 21:09:14 +0200338 /* don't search for locators with lower priority and don't
339 * check this locator again*/
340 limitp = lp->priority;
341 hash_set (checked, i, 1);
342 break;
343 }
344 }
Florin Corasf727db92016-06-23 15:01:58 +0200345 /* check if a local locator with a route to remote locator exists */
346 if (rmt != 0)
Florin Corasa2157cf2016-08-16 21:09:14 +0200347 {
348 /* find egress sw_if_index for rmt locator */
349 esi =
350 ip_fib_get_egress_iface_for_dst (lcm,
351 &gid_address_ip (&rmt->address));
352 if ((u32) ~ 0 == esi)
353 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200354
Florin Corasa2157cf2016-08-16 21:09:14 +0200355 for (i = 0; i < vec_len (lcl_ls->locator_indices); i++)
356 {
357 li = vec_elt (lcl_ls->locator_indices, i);
358 locator_t *sl = pool_elt_at_index (lcm->locator_pool, li);
Florin Corasf727db92016-06-23 15:01:58 +0200359
Florin Corasa2157cf2016-08-16 21:09:14 +0200360 /* found local locator with the needed sw_if_index */
361 if (sl->sw_if_index == esi)
362 {
363 /* and it has an address */
364 if (0 == ip_interface_get_first_ip_address (lcm,
365 sl->sw_if_index,
366 gid_address_ip_version
367 (&rmt->address),
368 lcl_addr))
369 continue;
Florin Corasf727db92016-06-23 15:01:58 +0200370
Florin Corasa2157cf2016-08-16 21:09:14 +0200371 memset (&pair, 0, sizeof (pair));
372 ip_address_copy (&pair.rmt_loc,
373 &gid_address_ip (&rmt->address));
374 ip_address_copy (&pair.lcl_loc, lcl_addr);
375 pair.weight = rmt->weight;
Filip Tehlarfb9931f2016-12-09 13:52:38 +0100376 pair.priority = rmt->priority;
Florin Corasa2157cf2016-08-16 21:09:14 +0200377 vec_add1 (locator_pairs[0], pair);
378 found = 1;
379 }
380 }
381 }
Florin Corasf727db92016-06-23 15:01:58 +0200382 else
Florin Corasa2157cf2016-08-16 21:09:14 +0200383 break;
Florin Corasf727db92016-06-23 15:01:58 +0200384 }
Florin Coras3590ac52016-08-08 16:04:26 +0200385
Florin Corasa2157cf2016-08-16 21:09:14 +0200386 hash_free (checked);
Florin Coras3590ac52016-08-08 16:04:26 +0200387 return found;
Florin Corasf727db92016-06-23 15:01:58 +0200388}
389
390static void
Florin Corasdca88042016-09-14 16:01:38 +0200391gid_address_sd_to_flat (gid_address_t * dst, gid_address_t * src,
392 fid_address_t * fid)
393{
394 ASSERT (GID_ADDR_SRC_DST == gid_address_type (src));
395
396 dst[0] = src[0];
397
398 switch (fid_addr_type (fid))
399 {
400 case FID_ADDR_IP_PREF:
401 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
402 gid_address_ippref (dst) = fid_addr_ippref (fid);
403 break;
404 case FID_ADDR_MAC:
405 gid_address_type (dst) = GID_ADDR_MAC;
406 mac_copy (gid_address_mac (dst), fid_addr_mac (fid));
407 break;
408 default:
409 clib_warning ("Unsupported fid type %d!", fid_addr_type (fid));
410 break;
411 }
412}
413
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200414u8
415vnet_lisp_map_register_state_get (void)
416{
417 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
418 return lcm->map_registering;
419}
420
421u8
422vnet_lisp_rloc_probe_state_get (void)
423{
424 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
425 return lcm->rloc_probing;
426}
427
Florin Corasdca88042016-09-14 16:01:38 +0200428static void
Florin Corasa2157cf2016-08-16 21:09:14 +0200429dp_add_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index, u32 dst_map_index)
Florin Corasf727db92016-06-23 15:01:58 +0200430{
Florin Corasa2157cf2016-08-16 21:09:14 +0200431 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
Florin Corasba888e42017-01-24 11:38:18 -0800432 gid_address_t *rmt_eid, *lcl_eid;
433 mapping_t *lcl_map, *rmt_map;
Florin Corasf727db92016-06-23 15:01:58 +0200434 u32 sw_if_index;
Florin Corasa2157cf2016-08-16 21:09:14 +0200435 uword *feip = 0, *dpid;
436 fwd_entry_t *fe;
Filip Tehlar69a9b762016-09-23 10:00:52 +0200437 u8 type, is_src_dst = 0;
Florin Corasba888e42017-01-24 11:38:18 -0800438 int rv;
Florin Corasf727db92016-06-23 15:01:58 +0200439
Florin Corasa2157cf2016-08-16 21:09:14 +0200440 memset (a, 0, sizeof (*a));
Florin Corasf727db92016-06-23 15:01:58 +0200441
442 /* remove entry if it already exists */
443 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
444 if (feip)
445 dp_del_fwd_entry (lcm, src_map_index, dst_map_index);
446
Florin Corasba888e42017-01-24 11:38:18 -0800447 /*
448 * Determine local mapping and eid
449 */
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200450 if (lcm->lisp_pitr)
Florin Corasba888e42017-01-24 11:38:18 -0800451 lcl_map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200452 else
Florin Corasba888e42017-01-24 11:38:18 -0800453 lcl_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
454 lcl_eid = &lcl_map->eid;
Florin Corasf727db92016-06-23 15:01:58 +0200455
Florin Corasba888e42017-01-24 11:38:18 -0800456 /*
457 * Determine remote mapping and eid
458 */
459 rmt_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
460 rmt_eid = &rmt_map->eid;
461
462 /*
463 * Build and insert data plane forwarding entry
464 */
Florin Corasf727db92016-06-23 15:01:58 +0200465 a->is_add = 1;
466
Filip Tehlard5fcc462016-10-17 16:20:18 +0200467 if (MR_MODE_SRC_DST == lcm->map_request_mode)
Florin Corasdca88042016-09-14 16:01:38 +0200468 {
Florin Corasba888e42017-01-24 11:38:18 -0800469 if (GID_ADDR_SRC_DST == gid_address_type (rmt_eid))
Filip Tehlard5fcc462016-10-17 16:20:18 +0200470 {
Florin Corasba888e42017-01-24 11:38:18 -0800471 gid_address_sd_to_flat (&a->rmt_eid, rmt_eid,
472 &gid_address_sd_dst (rmt_eid));
473 gid_address_sd_to_flat (&a->lcl_eid, rmt_eid,
474 &gid_address_sd_src (rmt_eid));
Filip Tehlard5fcc462016-10-17 16:20:18 +0200475 }
476 else
477 {
Florin Corasba888e42017-01-24 11:38:18 -0800478 gid_address_copy (&a->rmt_eid, rmt_eid);
479 gid_address_copy (&a->lcl_eid, lcl_eid);
Filip Tehlard5fcc462016-10-17 16:20:18 +0200480 }
Filip Tehlar69a9b762016-09-23 10:00:52 +0200481 is_src_dst = 1;
Florin Corasdca88042016-09-14 16:01:38 +0200482 }
483 else
Florin Corasba888e42017-01-24 11:38:18 -0800484 gid_address_copy (&a->rmt_eid, rmt_eid);
Florin Corasdca88042016-09-14 16:01:38 +0200485
Florin Corasa2157cf2016-08-16 21:09:14 +0200486 a->vni = gid_address_vni (&a->rmt_eid);
Filip Tehlared6b52b2017-03-22 09:02:33 +0100487 a->is_src_dst = is_src_dst;
Florin Coras1a1adc72016-07-22 01:45:30 +0200488
489 /* get vrf or bd_index associated to vni */
Florin Corasdca88042016-09-14 16:01:38 +0200490 type = gid_address_type (&a->rmt_eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200491 if (GID_ADDR_IP_PREFIX == type)
492 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200493 dpid = hash_get (lcm->table_id_by_vni, a->vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200494 if (!dpid)
Florin Corasa2157cf2016-08-16 21:09:14 +0200495 {
496 clib_warning ("vni %d not associated to a vrf!", a->vni);
497 return;
498 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200499 a->table_id = dpid[0];
500 }
501 else if (GID_ADDR_MAC == type)
502 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200503 dpid = hash_get (lcm->bd_id_by_vni, a->vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200504 if (!dpid)
Florin Corasa2157cf2016-08-16 21:09:14 +0200505 {
506 clib_warning ("vni %d not associated to a bridge domain !", a->vni);
507 return;
508 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200509 a->bd_id = dpid[0];
510 }
511
Florin Corasf727db92016-06-23 15:01:58 +0200512 /* find best locator pair that 1) verifies LISP policy 2) are connected */
Florin Corasba888e42017-01-24 11:38:18 -0800513 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
514
515 /* Either rmt mapping is negative or we can't find underlay path.
516 * Try again with petr if configured */
517 if (rv == 0 && (lcm->flags & LISP_FLAG_USE_PETR))
Florin Corasf727db92016-06-23 15:01:58 +0200518 {
Florin Corasba888e42017-01-24 11:38:18 -0800519 rmt_map = lisp_get_petr_mapping (lcm);
520 rv = get_locator_pairs (lcm, lcl_map, rmt_map, &a->locator_pairs);
Florin Corasf727db92016-06-23 15:01:58 +0200521 }
522
Florin Corasba888e42017-01-24 11:38:18 -0800523 /* negative entry */
524 if (rv == 0)
525 {
526 a->is_negative = 1;
527 a->action = rmt_map->action;
528 }
Florin Corasf727db92016-06-23 15:01:58 +0200529
Filip Tehlar21511912017-04-07 10:41:42 +0200530 rv = vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
531 if (rv)
532 {
533 if (a->locator_pairs)
534 vec_free (a->locator_pairs);
535 return;
536 }
Florin Corasf727db92016-06-23 15:01:58 +0200537
Filip Tehlar21511912017-04-07 10:41:42 +0200538 /* add tunnel to fwd entry table */
Florin Corasf727db92016-06-23 15:01:58 +0200539 pool_get (lcm->fwd_entry_pool, fe);
Filip Tehlar21511912017-04-07 10:41:42 +0200540 vnet_lisp_gpe_add_fwd_counters (a, fe - lcm->fwd_entry_pool);
541
Florin Corasbb5c22f2016-08-02 02:31:03 +0200542 fe->locator_pairs = a->locator_pairs;
Filip Tehlar2fdaece2016-09-28 14:27:59 +0200543 gid_address_copy (&fe->reid, &a->rmt_eid);
Filip Tehlarb601f222017-01-02 10:22:56 +0100544
545 if (is_src_dst)
546 gid_address_copy (&fe->leid, &a->lcl_eid);
547 else
Florin Corasba888e42017-01-24 11:38:18 -0800548 gid_address_copy (&fe->leid, lcl_eid);
Filip Tehlarb601f222017-01-02 10:22:56 +0100549
Filip Tehlar69a9b762016-09-23 10:00:52 +0200550 fe->is_src_dst = is_src_dst;
Florin Corasf727db92016-06-23 15:01:58 +0200551 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200552 fe - lcm->fwd_entry_pool);
Florin Corasf727db92016-06-23 15:01:58 +0200553}
554
Filip Tehlarce1aae42017-01-04 10:42:25 +0100555typedef struct
556{
557 u32 si;
558 u32 di;
559} fwd_entry_mt_arg_t;
560
561static void *
562dp_add_fwd_entry_thread_fn (void *arg)
563{
564 fwd_entry_mt_arg_t *a = arg;
565 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
566 dp_add_fwd_entry (lcm, a->si, a->di);
567 return 0;
568}
569
570static int
571dp_add_fwd_entry_from_mt (u32 si, u32 di)
572{
573 fwd_entry_mt_arg_t a;
574
575 memset (&a, 0, sizeof (a));
576 a.si = si;
577 a.di = di;
578
579 vl_api_rpc_call_main_thread (dp_add_fwd_entry_thread_fn,
580 (u8 *) & a, sizeof (a));
581 return 0;
582}
583
Florin Corasf727db92016-06-23 15:01:58 +0200584/**
Filip Tehlar69a9b762016-09-23 10:00:52 +0200585 * Returns vector of adjacencies.
586 *
587 * The caller must free the vector returned by this function.
588 *
589 * @param vni virtual network identifier
590 * @return vector of adjacencies
591 */
592lisp_adjacency_t *
593vnet_lisp_adjacencies_get_by_vni (u32 vni)
594{
595 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
596 fwd_entry_t *fwd;
597 lisp_adjacency_t *adjs = 0, adj;
598
599 /* *INDENT-OFF* */
600 pool_foreach(fwd, lcm->fwd_entry_pool,
601 ({
602 if (gid_address_vni (&fwd->reid) != vni)
603 continue;
604
605 gid_address_copy (&adj.reid, &fwd->reid);
606 gid_address_copy (&adj.leid, &fwd->leid);
607 vec_add1 (adjs, adj);
608 }));
609 /* *INDENT-ON* */
610
611 return adjs;
612}
613
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200614static lisp_msmr_t *
615get_map_server (ip_address_t * a)
616{
617 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
618 lisp_msmr_t *m;
619
620 vec_foreach (m, lcm->map_servers)
621 {
622 if (!ip_address_cmp (&m->address, a))
623 {
624 return m;
625 }
626 }
627 return 0;
628}
629
630static lisp_msmr_t *
631get_map_resolver (ip_address_t * a)
632{
633 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
634 lisp_msmr_t *m;
635
636 vec_foreach (m, lcm->map_resolvers)
637 {
638 if (!ip_address_cmp (&m->address, a))
639 {
640 return m;
641 }
642 }
643 return 0;
644}
645
646int
647vnet_lisp_add_del_map_server (ip_address_t * addr, u8 is_add)
648{
649 u32 i;
650 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
651 lisp_msmr_t _ms, *ms = &_ms;
652
653 if (vnet_lisp_enable_disable_status () == 0)
654 {
655 clib_warning ("LISP is disabled!");
656 return VNET_API_ERROR_LISP_DISABLED;
657 }
658
659 if (is_add)
660 {
661 if (get_map_server (addr))
662 {
663 clib_warning ("map-server %U already exists!", format_ip_address,
664 addr);
665 return -1;
666 }
667
668 memset (ms, 0, sizeof (*ms));
669 ip_address_copy (&ms->address, addr);
670 vec_add1 (lcm->map_servers, ms[0]);
671 }
672 else
673 {
674 for (i = 0; i < vec_len (lcm->map_servers); i++)
675 {
676 ms = vec_elt_at_index (lcm->map_servers, i);
677 if (!ip_address_cmp (&ms->address, addr))
678 {
679 vec_del1 (lcm->map_servers, i);
680 break;
681 }
682 }
683 }
684
685 return 0;
686}
687
Filip Tehlar69a9b762016-09-23 10:00:52 +0200688/**
Florin Corasf727db92016-06-23 15:01:58 +0200689 * Add/remove mapping to/from map-cache. Overwriting not allowed.
690 */
691int
692vnet_lisp_map_cache_add_del (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200693 u32 * map_index_result)
Florin Corase127a7e2016-02-18 22:20:01 +0100694{
Florin Corasa2157cf2016-08-16 21:09:14 +0200695 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
696 u32 mi, *map_indexp, map_index, i;
697 mapping_t *m, *old_map;
698 u32 **eid_indexes;
Florin Corase127a7e2016-02-18 22:20:01 +0100699
Florin Corasf727db92016-06-23 15:01:58 +0200700 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->eid);
Filip Tehlar53f09e32016-05-19 14:25:44 +0200701 old_map = mi != ~0 ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100702 if (a->is_add)
703 {
704 /* TODO check if overwriting and take appropriate actions */
Florin Corasa2157cf2016-08-16 21:09:14 +0200705 if (mi != GID_LOOKUP_MISS && !gid_address_cmp (&old_map->eid, &a->eid))
706 {
707 clib_warning ("eid %U found in the eid-table", format_gid_address,
708 &a->eid);
709 return VNET_API_ERROR_VALUE_EXIST;
710 }
Florin Corase127a7e2016-02-18 22:20:01 +0100711
Florin Corasa2157cf2016-08-16 21:09:14 +0200712 pool_get (lcm->mapping_pool, m);
Florin Corasf727db92016-06-23 15:01:58 +0200713 gid_address_copy (&m->eid, &a->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100714 m->locator_set_index = a->locator_set_index;
715 m->ttl = a->ttl;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200716 m->action = a->action;
Florin Corase127a7e2016-02-18 22:20:01 +0100717 m->local = a->local;
Filip Tehlar3cd9e732016-08-23 10:52:44 +0200718 m->is_static = a->is_static;
Filip Tehlar397fd7d2016-10-26 14:31:24 +0200719 m->key = vec_dup (a->key);
720 m->key_id = a->key_id;
Florin Corase127a7e2016-02-18 22:20:01 +0100721
722 map_index = m - lcm->mapping_pool;
Florin Corasf727db92016-06-23 15:01:58 +0200723 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, map_index,
Florin Corasa2157cf2016-08-16 21:09:14 +0200724 1);
Florin Corase127a7e2016-02-18 22:20:01 +0100725
Florin Corasa2157cf2016-08-16 21:09:14 +0200726 if (pool_is_free_index (lcm->locator_set_pool, a->locator_set_index))
727 {
728 clib_warning ("Locator set with index %d doesn't exist",
729 a->locator_set_index);
730 return VNET_API_ERROR_INVALID_VALUE;
731 }
Florin Corase127a7e2016-02-18 22:20:01 +0100732
733 /* add eid to list of eids supported by locator-set */
734 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
Florin Corasa2157cf2016-08-16 21:09:14 +0200735 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
736 a->locator_set_index);
737 vec_add1 (eid_indexes[0], map_index);
Florin Corase127a7e2016-02-18 22:20:01 +0100738
739 if (a->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200740 {
741 /* mark as local */
742 vec_add1 (lcm->local_mappings_indexes, map_index);
743 }
Florin Corase127a7e2016-02-18 22:20:01 +0100744 map_index_result[0] = map_index;
745 }
746 else
747 {
Florin Coras577c3552016-04-21 00:45:40 +0200748 if (mi == GID_LOOKUP_MISS)
Florin Corasa2157cf2016-08-16 21:09:14 +0200749 {
750 clib_warning ("eid %U not found in the eid-table",
751 format_gid_address, &a->eid);
752 return VNET_API_ERROR_INVALID_VALUE;
753 }
Florin Corase127a7e2016-02-18 22:20:01 +0100754
755 /* clear locator-set to eids binding */
Florin Corasa2157cf2016-08-16 21:09:14 +0200756 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids,
757 a->locator_set_index);
758 for (i = 0; i < vec_len (eid_indexes[0]); i++)
759 {
760 map_indexp = vec_elt_at_index (eid_indexes[0], i);
761 if (map_indexp[0] == mi)
762 break;
763 }
764 vec_del1 (eid_indexes[0], i);
Florin Corase127a7e2016-02-18 22:20:01 +0100765
766 /* remove local mark if needed */
Florin Corasa2157cf2016-08-16 21:09:14 +0200767 m = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corase127a7e2016-02-18 22:20:01 +0100768 if (m->local)
Florin Corasa2157cf2016-08-16 21:09:14 +0200769 {
770 u32 k, *lm_indexp;
771 for (k = 0; k < vec_len (lcm->local_mappings_indexes); k++)
772 {
773 lm_indexp = vec_elt_at_index (lcm->local_mappings_indexes, k);
774 if (lm_indexp[0] == mi)
775 break;
776 }
777 vec_del1 (lcm->local_mappings_indexes, k);
778 }
Florin Corase127a7e2016-02-18 22:20:01 +0100779
780 /* remove mapping from dictionary */
Florin Corasf727db92016-06-23 15:01:58 +0200781 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->eid, 0, 0);
Filip Tehlar324112f2016-06-02 16:07:38 +0200782 gid_address_free (&m->eid);
Florin Corase127a7e2016-02-18 22:20:01 +0100783 pool_put_index (lcm->mapping_pool, mi);
784 }
785
786 return 0;
787}
788
Florin Corasf727db92016-06-23 15:01:58 +0200789/**
790 * Add/update/delete mapping to/in/from map-cache.
791 */
Florin Coras577c3552016-04-21 00:45:40 +0200792int
793vnet_lisp_add_del_local_mapping (vnet_lisp_add_del_mapping_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +0200794 u32 * map_index_result)
Florin Coras577c3552016-04-21 00:45:40 +0200795{
Florin Corasa2157cf2016-08-16 21:09:14 +0200796 uword *dp_table = 0;
Florin Corasf727db92016-06-23 15:01:58 +0200797 u32 vni;
Florin Coras1a1adc72016-07-22 01:45:30 +0200798 u8 type;
Florin Corasf727db92016-06-23 15:01:58 +0200799
Florin Corasa2157cf2016-08-16 21:09:14 +0200800 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Coras577c3552016-04-21 00:45:40 +0200801
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +0200802 if (vnet_lisp_enable_disable_status () == 0)
803 {
804 clib_warning ("LISP is disabled!");
805 return VNET_API_ERROR_LISP_DISABLED;
806 }
807
Florin Corasa2157cf2016-08-16 21:09:14 +0200808 vni = gid_address_vni (&a->eid);
809 type = gid_address_type (&a->eid);
Florin Coras1a1adc72016-07-22 01:45:30 +0200810 if (GID_ADDR_IP_PREFIX == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200811 dp_table = hash_get (lcm->table_id_by_vni, vni);
Florin Coras1a1adc72016-07-22 01:45:30 +0200812 else if (GID_ADDR_MAC == type)
Florin Corasa2157cf2016-08-16 21:09:14 +0200813 dp_table = hash_get (lcm->bd_id_by_vni, vni);
Florin Coras577c3552016-04-21 00:45:40 +0200814
Florin Coras1a1adc72016-07-22 01:45:30 +0200815 if (!dp_table)
Florin Coras577c3552016-04-21 00:45:40 +0200816 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200817 clib_warning ("vni %d not associated to a %s!", vni,
818 GID_ADDR_IP_PREFIX == type ? "vrf" : "bd");
Florin Coras577c3552016-04-21 00:45:40 +0200819 return VNET_API_ERROR_INVALID_VALUE;
820 }
821
Florin Corasf727db92016-06-23 15:01:58 +0200822 /* store/remove mapping from map-cache */
823 return vnet_lisp_map_cache_add_del (a, map_index_result);
Florin Coras577c3552016-04-21 00:45:40 +0200824}
825
Filip Tehlard5a65db2017-05-17 17:21:10 +0200826static void
827add_l2_arp_bd (BVT (clib_bihash_kv) * kvp, void *arg)
828{
829 u32 **ht = arg;
830 u32 bd = (u32) kvp->key[0];
831 hash_set (ht[0], bd, 0);
832}
833
834u32 *
835vnet_lisp_l2_arp_bds_get (void)
836{
837 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
838 u32 *bds = 0;
839
840 gid_dict_foreach_l2_arp_entry (&lcm->mapping_index_by_gid,
841 add_l2_arp_bd, &bds);
842 return bds;
843}
844
845typedef struct
846{
847 void *vector;
848 u32 bd;
849} lisp_add_l2_arp_args_t;
850
851static void
852add_l2_arp_entry (BVT (clib_bihash_kv) * kvp, void *arg)
853{
854 lisp_add_l2_arp_args_t *a = arg;
855 lisp_api_l2_arp_entry_t **vector = a->vector, e;
856
857 if ((u32) kvp->key[0] == a->bd)
858 {
859 mac_copy (e.mac, (void *) &kvp->value);
860 e.ip4 = (u32) kvp->key[1];
861 vec_add1 (vector[0], e);
862 }
863}
864
865lisp_api_l2_arp_entry_t *
866vnet_lisp_l2_arp_entries_get_by_bd (u32 bd)
867{
868 lisp_api_l2_arp_entry_t *entries = 0;
869 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
870 lisp_add_l2_arp_args_t a;
871
872 a.vector = &entries;
873 a.bd = bd;
874
875 gid_dict_foreach_l2_arp_entry (&lcm->mapping_index_by_gid,
876 add_l2_arp_entry, &a);
877 return entries;
878}
879
880int
881vnet_lisp_add_del_l2_arp_entry (gid_address_t * key, u8 * mac, u8 is_add)
882{
883 if (vnet_lisp_enable_disable_status () == 0)
884 {
885 clib_warning ("LISP is disabled!");
886 return VNET_API_ERROR_LISP_DISABLED;
887 }
888
889 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
890 int rc = 0;
891
892 u64 res = gid_dictionary_lookup (&lcm->mapping_index_by_gid, key);
893 if (is_add)
894 {
895 if (res != GID_LOOKUP_MISS_L2)
896 {
897 clib_warning ("Entry %U exists in DB!", format_gid_address, key);
898 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
899 }
900 u64 val = mac_to_u64 (mac);
901 gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, val,
902 1 /* is_add */ );
903 }
904 else
905 {
906 if (res == GID_LOOKUP_MISS_L2)
907 {
908 clib_warning ("ONE ARP entry %U not found - cannot delete!",
909 format_gid_address, key);
910 return -1;
911 }
912 gid_dictionary_add_del (&lcm->mapping_index_by_gid, key, 0,
913 0 /* is_add */ );
914 }
915
916 return rc;
917}
918
Filip Tehlar324112f2016-06-02 16:07:38 +0200919int
Florin Coras1a1adc72016-07-22 01:45:30 +0200920vnet_lisp_eid_table_map (u32 vni, u32 dp_id, u8 is_l2, u8 is_add)
Filip Tehlar324112f2016-06-02 16:07:38 +0200921{
Florin Corasa2157cf2016-08-16 21:09:14 +0200922 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
923 uword *dp_idp, *vnip, **dp_table_by_vni, **vni_by_dp_table;
Filip Tehlar324112f2016-06-02 16:07:38 +0200924
925 if (vnet_lisp_enable_disable_status () == 0)
926 {
927 clib_warning ("LISP is disabled!");
Filip Tehlard5a65db2017-05-17 17:21:10 +0200928 return VNET_API_ERROR_LISP_DISABLED;
Filip Tehlar324112f2016-06-02 16:07:38 +0200929 }
930
Florin Coras1a1adc72016-07-22 01:45:30 +0200931 dp_table_by_vni = is_l2 ? &lcm->bd_id_by_vni : &lcm->table_id_by_vni;
932 vni_by_dp_table = is_l2 ? &lcm->vni_by_bd_id : &lcm->vni_by_table_id;
933
934 if (!is_l2 && (vni == 0 || dp_id == 0))
Filip Tehlar324112f2016-06-02 16:07:38 +0200935 {
936 clib_warning ("can't add/del default vni-vrf mapping!");
937 return -1;
938 }
939
Florin Coras1a1adc72016-07-22 01:45:30 +0200940 dp_idp = hash_get (dp_table_by_vni[0], vni);
941 vnip = hash_get (vni_by_dp_table[0], dp_id);
Filip Tehlar324112f2016-06-02 16:07:38 +0200942
943 if (is_add)
944 {
Florin Coras1a1adc72016-07-22 01:45:30 +0200945 if (dp_idp || vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +0200946 {
947 clib_warning ("vni %d or vrf %d already used in vrf/vni "
948 "mapping!", vni, dp_id);
949 return -1;
950 }
Florin Coras1a1adc72016-07-22 01:45:30 +0200951 hash_set (dp_table_by_vni[0], vni, dp_id);
952 hash_set (vni_by_dp_table[0], dp_id, vni);
Florin Corasf727db92016-06-23 15:01:58 +0200953
954 /* create dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +0200955 dp_add_del_iface (lcm, vni, is_l2, 1);
Filip Tehlar324112f2016-06-02 16:07:38 +0200956 }
957 else
958 {
Florin Coras1a1adc72016-07-22 01:45:30 +0200959 if (!dp_idp || !vnip)
Florin Corasa2157cf2016-08-16 21:09:14 +0200960 {
961 clib_warning ("vni %d or vrf %d not used in any vrf/vni! "
962 "mapping!", vni, dp_id);
963 return -1;
964 }
Florin Corasf727db92016-06-23 15:01:58 +0200965 /* remove dp iface */
Florin Coras1a1adc72016-07-22 01:45:30 +0200966 dp_add_del_iface (lcm, vni, is_l2, 0);
Filip Tehlarfc568412017-04-05 10:06:03 +0200967
968 hash_unset (dp_table_by_vni[0], vni);
969 hash_unset (vni_by_dp_table[0], dp_id);
Filip Tehlar324112f2016-06-02 16:07:38 +0200970 }
971 return 0;
Florin Coras1a1adc72016-07-22 01:45:30 +0200972
Filip Tehlar324112f2016-06-02 16:07:38 +0200973}
974
Florin Corasf727db92016-06-23 15:01:58 +0200975/* return 0 if the two locator sets are identical 1 otherwise */
976static u8
Florin Corasa2157cf2016-08-16 21:09:14 +0200977compare_locators (lisp_cp_main_t * lcm, u32 * old_ls_indexes,
978 locator_t * new_locators)
Filip Tehlar53f09e32016-05-19 14:25:44 +0200979{
Florin Corasf727db92016-06-23 15:01:58 +0200980 u32 i, old_li;
Florin Corasa2157cf2016-08-16 21:09:14 +0200981 locator_t *old_loc, *new_loc;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200982
Florin Corasa2157cf2016-08-16 21:09:14 +0200983 if (vec_len (old_ls_indexes) != vec_len (new_locators))
Florin Corasf727db92016-06-23 15:01:58 +0200984 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200985
Florin Corasa2157cf2016-08-16 21:09:14 +0200986 for (i = 0; i < vec_len (new_locators); i++)
Filip Tehlar53f09e32016-05-19 14:25:44 +0200987 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200988 old_li = vec_elt (old_ls_indexes, i);
989 old_loc = pool_elt_at_index (lcm->locator_pool, old_li);
Florin Corasf727db92016-06-23 15:01:58 +0200990
Florin Corasa2157cf2016-08-16 21:09:14 +0200991 new_loc = vec_elt_at_index (new_locators, i);
Florin Corasf727db92016-06-23 15:01:58 +0200992
993 if (locator_cmp (old_loc, new_loc))
Florin Corasa2157cf2016-08-16 21:09:14 +0200994 return 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200995 }
Florin Corasf727db92016-06-23 15:01:58 +0200996 return 0;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200997}
998
Filip Tehlard5fcc462016-10-17 16:20:18 +0200999typedef struct
1000{
1001 u8 is_negative;
1002 void *lcm;
1003 gid_address_t *eids_to_be_deleted;
1004} remove_mapping_args_t;
1005
1006/**
1007 * Callback invoked when a sub-prefix is found
1008 */
1009static void
1010remove_mapping_if_needed (u32 mi, void *arg)
1011{
1012 u8 delete = 0;
1013 remove_mapping_args_t *a = arg;
1014 lisp_cp_main_t *lcm = a->lcm;
1015 mapping_t *m;
1016 locator_set_t *ls;
1017
1018 m = pool_elt_at_index (lcm->mapping_pool, mi);
1019 if (!m)
1020 return;
1021
1022 ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
1023
1024 if (a->is_negative)
1025 {
1026 if (0 != vec_len (ls->locator_indices))
1027 delete = 1;
1028 }
1029 else
1030 {
1031 if (0 == vec_len (ls->locator_indices))
1032 delete = 1;
1033 }
1034
1035 if (delete)
1036 vec_add1 (a->eids_to_be_deleted, m->eid);
1037}
1038
1039/**
1040 * This function searches map cache and looks for IP prefixes that are subset
1041 * of the provided one. If such prefix is found depending on 'is_negative'
1042 * it does follows:
1043 *
1044 * 1) if is_negative is true and found prefix points to positive mapping,
1045 * then the mapping is removed
1046 * 2) if is_negative is false and found prefix points to negative mapping,
1047 * then the mapping is removed
1048 */
1049static void
1050remove_overlapping_sub_prefixes (lisp_cp_main_t * lcm, gid_address_t * eid,
1051 u8 is_negative)
1052{
1053 gid_address_t *e;
1054 remove_mapping_args_t a;
Florin Corasf3dc11a2017-01-24 03:13:43 -08001055
Filip Tehlard5fcc462016-10-17 16:20:18 +02001056 memset (&a, 0, sizeof (a));
1057
1058 /* do this only in src/dst mode ... */
1059 if (MR_MODE_SRC_DST != lcm->map_request_mode)
1060 return;
1061
1062 /* ... and only for IP prefix */
1063 if (GID_ADDR_SRC_DST != gid_address_type (eid)
1064 || (FID_ADDR_IP_PREF != gid_address_sd_dst_type (eid)))
1065 return;
1066
1067 a.is_negative = is_negative;
1068 a.lcm = lcm;
1069
1070 gid_dict_foreach_subprefix (&lcm->mapping_index_by_gid, eid,
1071 remove_mapping_if_needed, &a);
1072
1073 vec_foreach (e, a.eids_to_be_deleted)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001074 {
Florin Corasf3dc11a2017-01-24 03:13:43 -08001075 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
1076
1077 memset (adj_args, 0, sizeof (adj_args[0]));
1078 gid_address_copy (&adj_args->reid, e);
1079 adj_args->is_add = 0;
1080 if (vnet_lisp_add_del_adjacency (adj_args))
1081 clib_warning ("failed to del adjacency!");
1082
Filip Tehlard5fcc462016-10-17 16:20:18 +02001083 vnet_lisp_add_del_mapping (e, 0, 0, 0, 0, 0 /* is add */ , 0, 0);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01001084 }
Filip Tehlard5fcc462016-10-17 16:20:18 +02001085
1086 vec_free (a.eids_to_be_deleted);
1087}
1088
Filip Tehlar9677a942016-11-28 10:23:31 +01001089static void
1090mapping_delete_timer (lisp_cp_main_t * lcm, u32 mi)
1091{
1092 timing_wheel_delete (&lcm->wheel, mi);
1093}
1094
Filip Tehlara6bce492017-02-03 10:17:49 +01001095static int
1096is_local_ip (lisp_cp_main_t * lcm, ip_address_t * addr)
1097{
1098 fib_node_index_t fei;
1099 fib_prefix_t prefix;
1100 fib_entry_flag_t flags;
1101
1102 ip_address_to_fib_prefix (addr, &prefix);
1103
1104 fei = fib_table_lookup (0, &prefix);
1105 flags = fib_entry_get_flags (fei);
1106 return (FIB_ENTRY_FLAG_LOCAL & flags);
1107}
1108
Filip Tehlar195bcee2016-05-13 17:37:35 +02001109/**
Florin Corasf727db92016-06-23 15:01:58 +02001110 * Adds/removes/updates mapping. Does not program forwarding.
Filip Tehlar195bcee2016-05-13 17:37:35 +02001111 *
Florin Coras71893ac2016-07-10 20:09:32 +02001112 * @param eid end-host identifier
Filip Tehlar195bcee2016-05-13 17:37:35 +02001113 * @param rlocs vector of remote locators
1114 * @param action action for negative map-reply
1115 * @param is_add add mapping if non-zero, delete otherwise
Florin Coras71893ac2016-07-10 20:09:32 +02001116 * @param res_map_index the map-index that was created/updated/removed. It is
1117 * set to ~0 if no action is taken.
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001118 * @param is_static used for distinguishing between statically learned
1119 remote mappings and mappings obtained from MR
Filip Tehlar195bcee2016-05-13 17:37:35 +02001120 * @return return code
1121 */
1122int
Florin Coras71893ac2016-07-10 20:09:32 +02001123vnet_lisp_add_del_mapping (gid_address_t * eid, locator_t * rlocs, u8 action,
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001124 u8 authoritative, u32 ttl, u8 is_add, u8 is_static,
Florin Corasa2157cf2016-08-16 21:09:14 +02001125 u32 * res_map_index)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001126{
Florin Corasa2157cf2016-08-16 21:09:14 +02001127 vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args;
1128 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1129 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corasf727db92016-06-23 15:01:58 +02001130 u32 mi, ls_index = 0, dst_map_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02001131 mapping_t *old_map;
Filip Tehlara6bce492017-02-03 10:17:49 +01001132 locator_t *loc;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001133
Florin Corasa2157cf2016-08-16 21:09:14 +02001134 if (vnet_lisp_enable_disable_status () == 0)
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001135 {
1136 clib_warning ("LISP is disabled!");
1137 return VNET_API_ERROR_LISP_DISABLED;
1138 }
1139
Florin Corasf727db92016-06-23 15:01:58 +02001140 if (res_map_index)
1141 res_map_index[0] = ~0;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001142
Florin Corasf727db92016-06-23 15:01:58 +02001143 memset (m_args, 0, sizeof (m_args[0]));
1144 memset (ls_args, 0, sizeof (ls_args[0]));
Filip Tehlar195bcee2016-05-13 17:37:35 +02001145
Florin Corasf727db92016-06-23 15:01:58 +02001146 ls_args->locators = rlocs;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001147
Florin Coras71893ac2016-07-10 20:09:32 +02001148 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
Florin Corasa2157cf2016-08-16 21:09:14 +02001149 old_map = ((u32) ~ 0 != mi) ? pool_elt_at_index (lcm->mapping_pool, mi) : 0;
Florin Corasf727db92016-06-23 15:01:58 +02001150
1151 if (is_add)
Filip Tehlar195bcee2016-05-13 17:37:35 +02001152 {
Filip Tehlar272c69e2017-02-15 16:40:35 +01001153 /* check if none of the locators match localy configured address */
1154 vec_foreach (loc, rlocs)
1155 {
1156 ip_prefix_t *p = &gid_address_ippref (&loc->address);
1157 if (is_local_ip (lcm, &ip_prefix_addr (p)))
1158 {
1159 clib_warning ("RLOC %U matches a local address!",
1160 format_gid_address, &loc->address);
1161 return VNET_API_ERROR_LISP_RLOC_LOCAL;
1162 }
1163 }
1164
Florin Corasf727db92016-06-23 15:01:58 +02001165 /* overwrite: if mapping already exists, decide if locators should be
1166 * updated and be done */
Florin Coras71893ac2016-07-10 20:09:32 +02001167 if (old_map && gid_address_cmp (&old_map->eid, eid) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001168 {
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001169 if (!is_static && (old_map->is_static || old_map->local))
1170 {
1171 /* do not overwrite local or static remote mappings */
1172 clib_warning ("mapping %U rejected due to collision with local "
1173 "or static remote mapping!", format_gid_address,
Filip Tehlard5fcc462016-10-17 16:20:18 +02001174 eid);
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001175 return 0;
1176 }
1177
Florin Corasa2157cf2016-08-16 21:09:14 +02001178 locator_set_t *old_ls;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001179
Florin Corasa2157cf2016-08-16 21:09:14 +02001180 /* update mapping attributes */
1181 old_map->action = action;
1182 old_map->authoritative = authoritative;
1183 old_map->ttl = ttl;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001184
Florin Corasa2157cf2016-08-16 21:09:14 +02001185 old_ls = pool_elt_at_index (lcm->locator_set_pool,
1186 old_map->locator_set_index);
1187 if (compare_locators (lcm, old_ls->locator_indices,
1188 ls_args->locators))
1189 {
1190 /* set locator-set index to overwrite */
1191 ls_args->is_add = 1;
1192 ls_args->index = old_map->locator_set_index;
1193 vnet_lisp_add_del_locator_set (ls_args, 0);
1194 if (res_map_index)
1195 res_map_index[0] = mi;
1196 }
1197 }
Florin Corasf727db92016-06-23 15:01:58 +02001198 /* new mapping */
1199 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001200 {
Filip Tehlard5fcc462016-10-17 16:20:18 +02001201 remove_overlapping_sub_prefixes (lcm, eid, 0 == ls_args->locators);
1202
Florin Corasa2157cf2016-08-16 21:09:14 +02001203 ls_args->is_add = 1;
1204 ls_args->index = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001205
Florin Corasa2157cf2016-08-16 21:09:14 +02001206 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
Florin Corasf727db92016-06-23 15:01:58 +02001207
Florin Corasa2157cf2016-08-16 21:09:14 +02001208 /* add mapping */
1209 gid_address_copy (&m_args->eid, eid);
1210 m_args->is_add = 1;
1211 m_args->action = action;
1212 m_args->locator_set_index = ls_index;
Filip Tehlar3cd9e732016-08-23 10:52:44 +02001213 m_args->is_static = is_static;
Filip Tehlar9677a942016-11-28 10:23:31 +01001214 m_args->ttl = ttl;
Florin Corasa2157cf2016-08-16 21:09:14 +02001215 vnet_lisp_map_cache_add_del (m_args, &dst_map_index);
Florin Corasf727db92016-06-23 15:01:58 +02001216
Florin Corasa2157cf2016-08-16 21:09:14 +02001217 if (res_map_index)
1218 res_map_index[0] = dst_map_index;
1219 }
Filip Tehlar195bcee2016-05-13 17:37:35 +02001220 }
Florin Corasf727db92016-06-23 15:01:58 +02001221 else
1222 {
Florin Coras71893ac2016-07-10 20:09:32 +02001223 if (old_map == 0 || gid_address_cmp (&old_map->eid, eid) != 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001224 {
1225 clib_warning ("cannot delete mapping for eid %U",
1226 format_gid_address, eid);
1227 return -1;
1228 }
Florin Corasf727db92016-06-23 15:01:58 +02001229
1230 m_args->is_add = 0;
Florin Coras71893ac2016-07-10 20:09:32 +02001231 gid_address_copy (&m_args->eid, eid);
Florin Corasf727db92016-06-23 15:01:58 +02001232 m_args->locator_set_index = old_map->locator_set_index;
1233
1234 /* delete mapping associated from map-cache */
1235 vnet_lisp_map_cache_add_del (m_args, 0);
1236
1237 ls_args->is_add = 0;
1238 ls_args->index = old_map->locator_set_index;
1239 /* delete locator set */
1240 vnet_lisp_add_del_locator_set (ls_args, 0);
Filip Tehlarb93222f2016-07-07 09:58:08 +02001241
Filip Tehlar9677a942016-11-28 10:23:31 +01001242 /* delete timer associated to the mapping if any */
1243 if (old_map->timer_set)
1244 mapping_delete_timer (lcm, mi);
1245
Filip Tehlarb93222f2016-07-07 09:58:08 +02001246 /* return old mapping index */
Andrej Kozemcak438109d2016-07-22 12:54:12 +02001247 if (res_map_index)
Florin Corasa2157cf2016-08-16 21:09:14 +02001248 res_map_index[0] = mi;
Florin Corasf727db92016-06-23 15:01:58 +02001249 }
1250
Filip Tehlar195bcee2016-05-13 17:37:35 +02001251 /* success */
Florin Corasf727db92016-06-23 15:01:58 +02001252 return 0;
Filip Tehlar195bcee2016-05-13 17:37:35 +02001253}
1254
Filip Tehlar58f886a2016-05-30 15:57:40 +02001255int
Florin Corasf727db92016-06-23 15:01:58 +02001256vnet_lisp_clear_all_remote_adjacencies (void)
Filip Tehlar58f886a2016-05-30 15:57:40 +02001257{
1258 int rv = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001259 u32 mi, *map_indices = 0, *map_indexp;
1260 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1261 vnet_lisp_add_del_mapping_args_t _dm_args, *dm_args = &_dm_args;
1262 vnet_lisp_add_del_locator_set_args_t _ls, *ls = &_ls;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001263
Florin Corasa2157cf2016-08-16 21:09:14 +02001264 /* *INDENT-OFF* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001265 pool_foreach_index (mi, lcm->mapping_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02001266 ({
1267 vec_add1 (map_indices, mi);
1268 }));
1269 /* *INDENT-ON* */
Filip Tehlar58f886a2016-05-30 15:57:40 +02001270
1271 vec_foreach (map_indexp, map_indices)
Florin Corasa2157cf2016-08-16 21:09:14 +02001272 {
1273 mapping_t *map = pool_elt_at_index (lcm->mapping_pool, map_indexp[0]);
1274 if (!map->local)
1275 {
1276 dp_del_fwd_entry (lcm, 0, map_indexp[0]);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001277
Florin Corasa2157cf2016-08-16 21:09:14 +02001278 dm_args->is_add = 0;
1279 gid_address_copy (&dm_args->eid, &map->eid);
1280 dm_args->locator_set_index = map->locator_set_index;
Filip Tehlar58f886a2016-05-30 15:57:40 +02001281
Florin Corasa2157cf2016-08-16 21:09:14 +02001282 /* delete mapping associated to fwd entry */
1283 vnet_lisp_map_cache_add_del (dm_args, 0);
Filip Tehlar58f886a2016-05-30 15:57:40 +02001284
Florin Corasa2157cf2016-08-16 21:09:14 +02001285 ls->is_add = 0;
1286 ls->local = 0;
1287 ls->index = map->locator_set_index;
1288 /* delete locator set */
1289 rv = vnet_lisp_add_del_locator_set (ls, 0);
1290 if (rv != 0)
1291 goto cleanup;
1292 }
1293 }
Filip Tehlar58f886a2016-05-30 15:57:40 +02001294
1295cleanup:
1296 if (map_indices)
1297 vec_free (map_indices);
1298 return rv;
1299}
1300
Filip Tehlar195bcee2016-05-13 17:37:35 +02001301/**
Florin Coras71893ac2016-07-10 20:09:32 +02001302 * Adds adjacency or removes forwarding entry associated to remote mapping.
1303 * Note that adjacencies are not stored, they only result in forwarding entries
1304 * being created.
Florin Corasf727db92016-06-23 15:01:58 +02001305 */
Florin Corasf3dc11a2017-01-24 03:13:43 -08001306int
1307vnet_lisp_add_del_adjacency (vnet_lisp_add_del_adjacency_args_t * a)
Florin Corasf727db92016-06-23 15:01:58 +02001308{
Florin Corasf3dc11a2017-01-24 03:13:43 -08001309 lisp_cp_main_t *lcm = &lisp_control_main;
Florin Coras71893ac2016-07-10 20:09:32 +02001310 u32 local_mi, remote_mi = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02001311
1312 if (vnet_lisp_enable_disable_status () == 0)
1313 {
1314 clib_warning ("LISP is disabled!");
1315 return VNET_API_ERROR_LISP_DISABLED;
1316 }
1317
Filip Tehlar69a9b762016-09-23 10:00:52 +02001318 remote_mi = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid,
Florin Corasf3dc11a2017-01-24 03:13:43 -08001319 &a->reid, &a->leid);
Florin Coras71893ac2016-07-10 20:09:32 +02001320 if (GID_LOOKUP_MISS == remote_mi)
1321 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001322 clib_warning ("Remote eid %U not found. Cannot add adjacency!",
Florin Corasf3dc11a2017-01-24 03:13:43 -08001323 format_gid_address, &a->reid);
Florin Corasf727db92016-06-23 15:01:58 +02001324
Florin Coras71893ac2016-07-10 20:09:32 +02001325 return -1;
1326 }
1327
Florin Corasf3dc11a2017-01-24 03:13:43 -08001328 if (a->is_add)
Florin Corasf727db92016-06-23 15:01:58 +02001329 {
Florin Corasf727db92016-06-23 15:01:58 +02001330 /* check if source eid has an associated mapping. If pitr mode is on,
1331 * just use the pitr's mapping */
Florin Coras71893ac2016-07-10 20:09:32 +02001332 local_mi = lcm->lisp_pitr ? lcm->pitr_map_index :
Florin Corasf3dc11a2017-01-24 03:13:43 -08001333 gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->leid);
Florin Corasf727db92016-06-23 15:01:58 +02001334
Florin Coras71893ac2016-07-10 20:09:32 +02001335 if (GID_LOOKUP_MISS == local_mi)
Florin Corasa2157cf2016-08-16 21:09:14 +02001336 {
1337 clib_warning ("Local eid %U not found. Cannot add adjacency!",
Florin Corasf3dc11a2017-01-24 03:13:43 -08001338 format_gid_address, &a->leid);
Florin Corasf727db92016-06-23 15:01:58 +02001339
Florin Corasa2157cf2016-08-16 21:09:14 +02001340 return -1;
1341 }
Florin Corasf727db92016-06-23 15:01:58 +02001342
Florin Coras71893ac2016-07-10 20:09:32 +02001343 /* update forwarding */
1344 dp_add_fwd_entry (lcm, local_mi, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001345 }
1346 else
Florin Coras71893ac2016-07-10 20:09:32 +02001347 dp_del_fwd_entry (lcm, 0, remote_mi);
Florin Corasf727db92016-06-23 15:01:58 +02001348
1349 return 0;
1350}
1351
1352int
Florin Corasdca88042016-09-14 16:01:38 +02001353vnet_lisp_set_map_request_mode (u8 mode)
1354{
1355 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1356
1357 if (vnet_lisp_enable_disable_status () == 0)
1358 {
1359 clib_warning ("LISP is disabled!");
1360 return VNET_API_ERROR_LISP_DISABLED;
1361 }
1362
1363 if (mode >= _MR_MODE_MAX)
1364 {
1365 clib_warning ("Invalid LISP map request mode %d!", mode);
1366 return VNET_API_ERROR_INVALID_ARGUMENT;
1367 }
1368
1369 lcm->map_request_mode = mode;
1370 return 0;
1371}
1372
Filip Tehlar53f09e32016-05-19 14:25:44 +02001373int
1374vnet_lisp_pitr_set_locator_set (u8 * locator_set_name, u8 is_add)
1375{
Florin Corasa2157cf2016-08-16 21:09:14 +02001376 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar53f09e32016-05-19 14:25:44 +02001377 u32 locator_set_index = ~0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001378 mapping_t *m;
1379 uword *p;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001380
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001381 if (vnet_lisp_enable_disable_status () == 0)
1382 {
1383 clib_warning ("LISP is disabled!");
1384 return VNET_API_ERROR_LISP_DISABLED;
1385 }
1386
Filip Tehlar53f09e32016-05-19 14:25:44 +02001387 p = hash_get_mem (lcm->locator_set_index_by_name, locator_set_name);
1388 if (!p)
1389 {
1390 clib_warning ("locator-set %v doesn't exist", locator_set_name);
1391 return -1;
1392 }
1393 locator_set_index = p[0];
1394
1395 if (is_add)
1396 {
1397 pool_get (lcm->mapping_pool, m);
1398 m->locator_set_index = locator_set_index;
1399 m->local = 1;
Filip Tehlar38206ee2017-02-20 17:31:57 +01001400 m->pitr_set = 1;
Filip Tehlar53f09e32016-05-19 14:25:44 +02001401 lcm->pitr_map_index = m - lcm->mapping_pool;
1402
1403 /* enable pitr mode */
1404 lcm->lisp_pitr = 1;
1405 }
1406 else
1407 {
1408 /* remove pitr mapping */
1409 pool_put_index (lcm->mapping_pool, lcm->pitr_map_index);
1410
1411 /* disable pitr mode */
1412 lcm->lisp_pitr = 0;
1413 }
1414 return 0;
1415}
1416
Florin Corasba888e42017-01-24 11:38:18 -08001417/**
1418 * Configure Proxy-ETR
1419 *
1420 * @param ip PETR's IP address
1421 * @param is_add Flag that indicates if this is an addition or removal
1422 *
1423 * return 0 on success
1424 */
1425int
1426vnet_lisp_use_petr (ip_address_t * ip, u8 is_add)
1427{
1428 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1429 u32 ls_index = ~0;
1430 mapping_t *m;
1431 vnet_lisp_add_del_locator_set_args_t _ls_args, *ls_args = &_ls_args;
1432 locator_t loc;
1433
1434 if (vnet_lisp_enable_disable_status () == 0)
1435 {
1436 clib_warning ("LISP is disabled!");
1437 return VNET_API_ERROR_LISP_DISABLED;
1438 }
1439
1440 memset (ls_args, 0, sizeof (*ls_args));
1441
1442 if (is_add)
1443 {
1444 /* Create dummy petr locator-set */
Florin Corasb69259a2017-03-09 14:39:54 -08001445 memset (&loc, 0, sizeof (loc));
Florin Corasba888e42017-01-24 11:38:18 -08001446 gid_address_from_ip (&loc.address, ip);
1447 loc.priority = 1;
1448 loc.state = loc.weight = 1;
Florin Coras2743cc42017-01-29 16:42:20 -08001449 loc.local = 0;
Florin Corasba888e42017-01-24 11:38:18 -08001450
1451 ls_args->is_add = 1;
1452 ls_args->index = ~0;
1453 vec_add1 (ls_args->locators, loc);
1454 vnet_lisp_add_del_locator_set (ls_args, &ls_index);
1455
1456 /* Add petr mapping */
1457 pool_get (lcm->mapping_pool, m);
1458 m->locator_set_index = ls_index;
1459 lcm->petr_map_index = m - lcm->mapping_pool;
1460
1461 /* Enable use-petr */
1462 lcm->flags |= LISP_FLAG_USE_PETR;
1463 }
1464 else
1465 {
1466 m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index);
1467
1468 /* Remove petr locator */
1469 ls_args->is_add = 0;
1470 ls_args->index = m->locator_set_index;
1471 vnet_lisp_add_del_locator_set (ls_args, 0);
1472
1473 /* Remove petr mapping */
1474 pool_put_index (lcm->mapping_pool, lcm->petr_map_index);
1475
1476 /* Disable use-petr */
1477 lcm->flags &= ~LISP_FLAG_USE_PETR;
1478 }
1479 return 0;
1480}
1481
Florin Corase127a7e2016-02-18 22:20:01 +01001482/* cleans locator to locator-set data and removes locators not part of
1483 * any locator-set */
1484static void
1485clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
1486{
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001487 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes, *to_be_deleted = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02001488 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool, lsi);
1489 for (i = 0; i < vec_len (ls->locator_indices); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01001490 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001491 loc_indexp = vec_elt_at_index (ls->locator_indices, i);
1492 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1493 loc_indexp[0]);
1494 for (j = 0; j < vec_len (ls_indexes[0]); j++)
1495 {
1496 ls_indexp = vec_elt_at_index (ls_indexes[0], j);
1497 if (ls_indexp[0] == lsi)
1498 break;
1499 }
Florin Corase127a7e2016-02-18 22:20:01 +01001500
Florin Corasa2157cf2016-08-16 21:09:14 +02001501 /* delete index for removed locator-set */
1502 vec_del1 (ls_indexes[0], j);
Florin Corase127a7e2016-02-18 22:20:01 +01001503
1504 /* delete locator if it's part of no locator-set */
1505 if (vec_len (ls_indexes[0]) == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02001506 {
1507 pool_put_index (lcm->locator_pool, loc_indexp[0]);
1508 vec_add1 (to_be_deleted, i);
1509 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001510 }
1511
1512 if (to_be_deleted)
1513 {
1514 for (i = 0; i < vec_len (to_be_deleted); i++)
Florin Corasa2157cf2016-08-16 21:09:14 +02001515 {
1516 loc_indexp = vec_elt_at_index (to_be_deleted, i);
1517 vec_del1 (ls->locator_indices, loc_indexp[0]);
1518 }
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001519 vec_free (to_be_deleted);
Florin Corase127a7e2016-02-18 22:20:01 +01001520 }
1521}
Filip Tehlard1c5cc32016-05-30 10:39:20 +02001522
Florin Corasf727db92016-06-23 15:01:58 +02001523static inline uword *
1524get_locator_set_index (vnet_lisp_add_del_locator_set_args_t * a, uword * p)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001525{
Florin Corasa2157cf2016-08-16 21:09:14 +02001526 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001527
Florin Corasa2157cf2016-08-16 21:09:14 +02001528 ASSERT (a != NULL);
1529 ASSERT (p != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001530
1531 /* find locator-set */
1532 if (a->local)
1533 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001534 p = hash_get_mem (lcm->locator_set_index_by_name, a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001535 }
1536 else
1537 {
1538 *p = a->index;
1539 }
1540
1541 return p;
1542}
1543
Florin Corasf727db92016-06-23 15:01:58 +02001544static inline int
1545is_locator_in_locator_set (lisp_cp_main_t * lcm, locator_set_t * ls,
Florin Corasa2157cf2016-08-16 21:09:14 +02001546 locator_t * loc)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001547{
Florin Corasa2157cf2016-08-16 21:09:14 +02001548 locator_t *itloc;
1549 u32 *locit;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001550
Florin Corasa2157cf2016-08-16 21:09:14 +02001551 ASSERT (ls != NULL);
1552 ASSERT (loc != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001553
Florin Corasa2157cf2016-08-16 21:09:14 +02001554 vec_foreach (locit, ls->locator_indices)
1555 {
1556 itloc = pool_elt_at_index (lcm->locator_pool, locit[0]);
1557 if ((ls->local && itloc->sw_if_index == loc->sw_if_index) ||
1558 (!ls->local && !gid_address_cmp (&itloc->address, &loc->address)))
1559 {
1560 clib_warning ("Duplicate locator");
1561 return VNET_API_ERROR_VALUE_EXIST;
1562 }
1563 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001564
1565 return 0;
1566}
1567
Filip Tehlar68d2e242017-04-21 12:05:58 +02001568static void
1569update_adjacencies_by_map_index (lisp_cp_main_t * lcm, u8 is_local,
1570 u32 mapping_index, u8 remove_only)
1571{
1572 fwd_entry_t *fwd;
1573 mapping_t *map;
1574 vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
1575
1576 map = pool_elt_at_index (lcm->mapping_pool, mapping_index);
1577
1578 /* *INDENT-OFF* */
1579 pool_foreach(fwd, lcm->fwd_entry_pool,
1580 ({
1581 if ((is_local && 0 == gid_address_cmp (&map->eid, &fwd->leid)) ||
1582 (!is_local && 0 == gid_address_cmp (&map->eid, &fwd->reid)))
1583 {
1584 a->is_add = 0;
1585 gid_address_copy (&a->leid, &fwd->leid);
1586 gid_address_copy (&a->reid, &fwd->reid);
1587
1588 vnet_lisp_add_del_adjacency (a);
1589
1590 if (!remove_only)
1591 {
1592 a->is_add = 1;
1593 vnet_lisp_add_del_adjacency (a);
1594 }
1595 }
1596 }));
1597 /* *INDENT-ON* */
1598}
1599
1600static void
1601update_fwd_entries_by_locator_set (lisp_cp_main_t * lcm, u8 is_local,
1602 u32 ls_index, u8 remove_only)
1603{
1604 u32 i, *map_indexp;
1605 u32 **eid_indexes;
Filip Tehlarfacee282017-04-27 14:29:27 +02001606
1607 if (vec_len (lcm->locator_set_to_eids) <= ls_index)
1608 return;
1609
Filip Tehlar68d2e242017-04-21 12:05:58 +02001610 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, ls_index);
1611
1612 for (i = 0; i < vec_len (eid_indexes[0]); i++)
1613 {
1614 map_indexp = vec_elt_at_index (eid_indexes[0], i);
1615 update_adjacencies_by_map_index (lcm, is_local, map_indexp[0],
1616 remove_only);
1617 }
1618}
1619
Florin Corasf727db92016-06-23 15:01:58 +02001620static inline void
Florin Corasa2157cf2016-08-16 21:09:14 +02001621remove_locator_from_locator_set (locator_set_t * ls, u32 * locit,
1622 u32 ls_index, u32 loc_id)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001623{
Florin Corasa2157cf2016-08-16 21:09:14 +02001624 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1625 u32 **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001626
Florin Corasa2157cf2016-08-16 21:09:14 +02001627 ASSERT (ls != NULL);
1628 ASSERT (locit != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001629
Florin Corasa2157cf2016-08-16 21:09:14 +02001630 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets, locit[0]);
1631 pool_put_index (lcm->locator_pool, locit[0]);
1632 vec_del1 (ls->locator_indices, loc_id);
1633 vec_del1 (ls_indexes[0], ls_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001634}
1635
1636int
1637vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02001638 locator_set_t * ls, u32 * ls_result)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001639{
Florin Corasa2157cf2016-08-16 21:09:14 +02001640 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1641 locator_t *loc = NULL, *itloc = NULL;
1642 uword _p = (u32) ~ 0, *p = &_p;
1643 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001644 u32 loc_id = ~0;
1645 int ret = 0;
1646
Florin Corasa2157cf2016-08-16 21:09:14 +02001647 ASSERT (a != NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001648
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001649 if (vnet_lisp_enable_disable_status () == 0)
1650 {
1651 clib_warning ("LISP is disabled!");
1652 return VNET_API_ERROR_LISP_DISABLED;
1653 }
1654
Florin Corasa2157cf2016-08-16 21:09:14 +02001655 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001656 if (!p)
1657 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001658 clib_warning ("locator-set %v doesn't exist", a->name);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001659 return VNET_API_ERROR_INVALID_ARGUMENT;
1660 }
1661
1662 if (ls == 0)
1663 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001664 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001665 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02001666 {
1667 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1668 p[0]);
1669 return VNET_API_ERROR_INVALID_ARGUMENT;
1670 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001671 }
1672
1673 if (a->is_add)
1674 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001675 if (ls_result)
1676 ls_result[0] = p[0];
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001677
Florin Corasa2157cf2016-08-16 21:09:14 +02001678 /* allocate locators */
1679 vec_foreach (itloc, a->locators)
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001680 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001681 ret = is_locator_in_locator_set (lcm, ls, itloc);
1682 if (0 != ret)
1683 {
1684 return ret;
1685 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001686
Florin Corasa2157cf2016-08-16 21:09:14 +02001687 pool_get (lcm->locator_pool, loc);
1688 loc[0] = itloc[0];
1689 loc_index = loc - lcm->locator_pool;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001690
Florin Corasa2157cf2016-08-16 21:09:14 +02001691 vec_add1 (ls->locator_indices, loc_index);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001692
Florin Corasa2157cf2016-08-16 21:09:14 +02001693 vec_validate (lcm->locator_to_locator_sets, loc_index);
1694 ls_indexes = vec_elt_at_index (lcm->locator_to_locator_sets,
1695 loc_index);
Filip Tehlarc5bb0d62016-09-02 12:14:31 +02001696 vec_add1 (ls_indexes[0], p[0]);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001697 }
Florin Corasa2157cf2016-08-16 21:09:14 +02001698 }
1699 else
1700 {
1701 ls_index = p[0];
Filip Tehlar68d2e242017-04-21 12:05:58 +02001702 u8 removed;
Florin Corasa2157cf2016-08-16 21:09:14 +02001703
Filip Tehlar68d2e242017-04-21 12:05:58 +02001704 vec_foreach (itloc, a->locators)
Florin Corasa2157cf2016-08-16 21:09:14 +02001705 {
Filip Tehlar68d2e242017-04-21 12:05:58 +02001706 removed = 0;
1707 loc_id = 0;
1708 vec_foreach (locit, ls->locator_indices)
1709 {
1710 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
Florin Corasa2157cf2016-08-16 21:09:14 +02001711
Filip Tehlar68d2e242017-04-21 12:05:58 +02001712 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
1713 {
1714 removed = 1;
1715 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1716 }
1717 if (0 == loc->local &&
1718 !gid_address_cmp (&loc->address, &itloc->address))
1719 {
1720 removed = 1;
1721 remove_locator_from_locator_set (ls, locit, ls_index, loc_id);
1722 }
Florin Corasa2157cf2016-08-16 21:09:14 +02001723
Filip Tehlar68d2e242017-04-21 12:05:58 +02001724 if (removed)
1725 {
1726 /* update fwd entries using this locator in DP */
1727 update_fwd_entries_by_locator_set (lcm, loc->local, ls_index,
1728 vec_len (ls->locator_indices)
1729 == 0);
1730 }
1731
1732 loc_id++;
1733 }
Florin Corasa2157cf2016-08-16 21:09:14 +02001734 }
1735 }
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001736
1737 return 0;
1738}
1739
Florin Corase127a7e2016-02-18 22:20:01 +01001740int
1741vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
Florin Corasa2157cf2016-08-16 21:09:14 +02001742 u32 * ls_result)
Florin Corase127a7e2016-02-18 22:20:01 +01001743{
Florin Corasa2157cf2016-08-16 21:09:14 +02001744 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1745 locator_set_t *ls;
1746 uword _p = (u32) ~ 0, *p = &_p;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001747 u32 ls_index;
Florin Corasa2157cf2016-08-16 21:09:14 +02001748 u32 **eid_indexes;
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001749 int ret = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01001750
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001751 if (vnet_lisp_enable_disable_status () == 0)
1752 {
1753 clib_warning ("LISP is disabled!");
1754 return VNET_API_ERROR_LISP_DISABLED;
1755 }
1756
Florin Corase127a7e2016-02-18 22:20:01 +01001757 if (a->is_add)
1758 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001759 p = get_locator_set_index (a, p);
Florin Corase127a7e2016-02-18 22:20:01 +01001760
1761 /* overwrite */
Florin Corasa2157cf2016-08-16 21:09:14 +02001762 if (p && p[0] != (u32) ~ 0)
1763 {
1764 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
1765 if (!ls)
1766 {
1767 clib_warning ("locator-set %d to be overwritten doesn't exist!",
1768 p[0]);
1769 return -1;
1770 }
Florin Corase127a7e2016-02-18 22:20:01 +01001771
Florin Corasa2157cf2016-08-16 21:09:14 +02001772 /* clean locator to locator-set vectors and remove locators if
1773 * they're not part of another locator-set */
1774 clean_locator_to_locator_set (lcm, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01001775
Florin Corasa2157cf2016-08-16 21:09:14 +02001776 /* remove locator indices from locator set */
1777 vec_free (ls->locator_indices);
Florin Corase127a7e2016-02-18 22:20:01 +01001778
Florin Corasa2157cf2016-08-16 21:09:14 +02001779 ls_index = p[0];
Florin Corase127a7e2016-02-18 22:20:01 +01001780
Florin Corasa2157cf2016-08-16 21:09:14 +02001781 if (ls_result)
1782 ls_result[0] = p[0];
1783 }
Florin Corase127a7e2016-02-18 22:20:01 +01001784 /* new locator-set */
1785 else
Florin Corasa2157cf2016-08-16 21:09:14 +02001786 {
1787 pool_get (lcm->locator_set_pool, ls);
1788 memset (ls, 0, sizeof (*ls));
1789 ls_index = ls - lcm->locator_set_pool;
Florin Corase127a7e2016-02-18 22:20:01 +01001790
Florin Corasa2157cf2016-08-16 21:09:14 +02001791 if (a->local)
1792 {
1793 ls->name = vec_dup (a->name);
Florin Corase127a7e2016-02-18 22:20:01 +01001794
Florin Corasa2157cf2016-08-16 21:09:14 +02001795 if (!lcm->locator_set_index_by_name)
1796 lcm->locator_set_index_by_name = hash_create_vec (
1797 /* size */
1798 0,
1799 sizeof
1800 (ls->name
1801 [0]),
1802 sizeof
1803 (uword));
1804 hash_set_mem (lcm->locator_set_index_by_name, ls->name,
1805 ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01001806
Florin Corasa2157cf2016-08-16 21:09:14 +02001807 /* mark as local locator-set */
1808 vec_add1 (lcm->local_locator_set_indexes, ls_index);
1809 }
1810 ls->local = a->local;
1811 if (ls_result)
1812 ls_result[0] = ls_index;
1813 }
Florin Corase127a7e2016-02-18 22:20:01 +01001814
Florin Corasa2157cf2016-08-16 21:09:14 +02001815 ret = vnet_lisp_add_del_locator (a, ls, NULL);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001816 if (0 != ret)
Florin Corasa2157cf2016-08-16 21:09:14 +02001817 {
1818 return ret;
1819 }
Florin Corase127a7e2016-02-18 22:20:01 +01001820 }
1821 else
1822 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001823 p = get_locator_set_index (a, p);
Andrej Kozemcak6a2e4392016-05-26 12:20:08 +02001824 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02001825 {
1826 clib_warning ("locator-set %v doesn't exists", a->name);
1827 return -1;
1828 }
Florin Corase127a7e2016-02-18 22:20:01 +01001829
Florin Corasa2157cf2016-08-16 21:09:14 +02001830 ls = pool_elt_at_index (lcm->locator_set_pool, p[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01001831 if (!ls)
Florin Corasa2157cf2016-08-16 21:09:14 +02001832 {
1833 clib_warning ("locator-set with index %d doesn't exists", p[0]);
1834 return -1;
1835 }
Florin Corase127a7e2016-02-18 22:20:01 +01001836
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001837 if (lcm->mreq_itr_rlocs == p[0])
Florin Corasa2157cf2016-08-16 21:09:14 +02001838 {
1839 clib_warning ("Can't delete the locator-set used to constrain "
1840 "the itr-rlocs in map-requests!");
1841 return -1;
1842 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001843
Florin Corasa2157cf2016-08-16 21:09:14 +02001844 if (vec_len (lcm->locator_set_to_eids) != 0)
1845 {
1846 eid_indexes = vec_elt_at_index (lcm->locator_set_to_eids, p[0]);
1847 if (vec_len (eid_indexes[0]) != 0)
1848 {
1849 clib_warning
1850 ("Can't delete a locator that supports a mapping!");
1851 return -1;
1852 }
1853 }
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02001854
1855 /* clean locator to locator-sets data */
1856 clean_locator_to_locator_set (lcm, p[0]);
1857
1858 if (ls->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02001859 {
1860 u32 it, lsi;
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02001861
Florin Corasa2157cf2016-08-16 21:09:14 +02001862 vec_foreach_index (it, lcm->local_locator_set_indexes)
1863 {
1864 lsi = vec_elt (lcm->local_locator_set_indexes, it);
1865 if (lsi == p[0])
1866 {
1867 vec_del1 (lcm->local_locator_set_indexes, it);
1868 break;
1869 }
1870 }
1871 hash_unset_mem (lcm->locator_set_index_by_name, ls->name);
1872 }
1873 vec_free (ls->name);
1874 vec_free (ls->locator_indices);
1875 pool_put (lcm->locator_set_pool, ls);
Andrej Kozemcakb92feb62016-03-31 13:51:42 +02001876 }
1877 return 0;
1878}
1879
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001880int
1881vnet_lisp_rloc_probe_enable_disable (u8 is_enable)
1882{
1883 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1884
1885 lcm->rloc_probing = is_enable;
1886 return 0;
1887}
1888
1889int
1890vnet_lisp_map_register_enable_disable (u8 is_enable)
1891{
1892 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1893
1894 lcm->map_registering = is_enable;
1895 return 0;
1896}
1897
Filip Tehlar46d4e362016-05-09 09:39:26 +02001898clib_error_t *
Florin Corasf727db92016-06-23 15:01:58 +02001899vnet_lisp_enable_disable (u8 is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02001900{
Florin Coras1a1adc72016-07-22 01:45:30 +02001901 u32 vni, dp_table;
Florin Corasa2157cf2016-08-16 21:09:14 +02001902 clib_error_t *error = 0;
1903 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1904 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
Filip Tehlar46d4e362016-05-09 09:39:26 +02001905
Florin Corasf727db92016-06-23 15:01:58 +02001906 a->is_en = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02001907 error = vnet_lisp_gpe_enable_disable (a);
1908 if (error)
1909 {
1910 return clib_error_return (0, "failed to %s data-plane!",
Florin Corasa2157cf2016-08-16 21:09:14 +02001911 a->is_en ? "enable" : "disable");
Filip Tehlar46d4e362016-05-09 09:39:26 +02001912 }
1913
Florin Corasf727db92016-06-23 15:01:58 +02001914 if (is_enable)
Filip Tehlar46d4e362016-05-09 09:39:26 +02001915 {
Florin Coras1a1adc72016-07-22 01:45:30 +02001916 /* enable all l2 and l3 ifaces */
Florin Corasa2157cf2016-08-16 21:09:14 +02001917
1918 /* *INDENT-OFF* */
Florin Coras1a1adc72016-07-22 01:45:30 +02001919 hash_foreach(vni, dp_table, lcm->table_id_by_vni, ({
1920 dp_add_del_iface(lcm, vni, 0, 1);
1921 }));
Florin Coras1a1adc72016-07-22 01:45:30 +02001922 hash_foreach(vni, dp_table, lcm->bd_id_by_vni, ({
1923 dp_add_del_iface(lcm, vni, /* is_l2 */ 1, 1);
Florin Corasf727db92016-06-23 15:01:58 +02001924 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02001925 /* *INDENT-ON* */
Filip Tehlar46d4e362016-05-09 09:39:26 +02001926 }
1927 else
1928 {
Florin Corasf727db92016-06-23 15:01:58 +02001929 /* clear interface table */
Florin Corasa2157cf2016-08-16 21:09:14 +02001930 hash_free (lcm->fwd_entry_by_mapping_index);
1931 pool_free (lcm->fwd_entry_pool);
Filip Tehlar46d4e362016-05-09 09:39:26 +02001932 }
1933
1934 /* update global flag */
Florin Corasf727db92016-06-23 15:01:58 +02001935 lcm->is_enabled = is_enable;
Filip Tehlar46d4e362016-05-09 09:39:26 +02001936
1937 return 0;
1938}
1939
Filip Tehlar46d4e362016-05-09 09:39:26 +02001940u8
1941vnet_lisp_enable_disable_status (void)
1942{
Florin Corasa2157cf2016-08-16 21:09:14 +02001943 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar46d4e362016-05-09 09:39:26 +02001944 return lcm->is_enabled;
1945}
1946
Florin Corase127a7e2016-02-18 22:20:01 +01001947int
1948vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
1949{
Florin Corasa2157cf2016-08-16 21:09:14 +02001950 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01001951 u32 i;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02001952 lisp_msmr_t _mr, *mr = &_mr;
Florin Corase127a7e2016-02-18 22:20:01 +01001953
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +02001954 if (vnet_lisp_enable_disable_status () == 0)
1955 {
1956 clib_warning ("LISP is disabled!");
1957 return VNET_API_ERROR_LISP_DISABLED;
1958 }
1959
Florin Corase127a7e2016-02-18 22:20:01 +01001960 if (a->is_add)
1961 {
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001962
1963 if (get_map_resolver (&a->address))
Florin Corasa2157cf2016-08-16 21:09:14 +02001964 {
1965 clib_warning ("map-resolver %U already exists!", format_ip_address,
1966 &a->address);
1967 return -1;
1968 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001969
1970 memset (mr, 0, sizeof (*mr));
Florin Corasa2157cf2016-08-16 21:09:14 +02001971 ip_address_copy (&mr->address, &a->address);
1972 vec_add1 (lcm->map_resolvers, *mr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001973
1974 if (vec_len (lcm->map_resolvers) == 1)
Florin Corasa2157cf2016-08-16 21:09:14 +02001975 lcm->do_map_resolver_election = 1;
Florin Corase127a7e2016-02-18 22:20:01 +01001976 }
1977 else
1978 {
Florin Corasa2157cf2016-08-16 21:09:14 +02001979 for (i = 0; i < vec_len (lcm->map_resolvers); i++)
1980 {
1981 mr = vec_elt_at_index (lcm->map_resolvers, i);
1982 if (!ip_address_cmp (&mr->address, &a->address))
1983 {
1984 if (!ip_address_cmp (&mr->address, &lcm->active_map_resolver))
1985 lcm->do_map_resolver_election = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02001986
Florin Corasa2157cf2016-08-16 21:09:14 +02001987 vec_del1 (lcm->map_resolvers, i);
1988 break;
1989 }
1990 }
Florin Corase127a7e2016-02-18 22:20:01 +01001991 }
1992 return 0;
1993}
1994
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02001995int
1996vnet_lisp_add_del_mreq_itr_rlocs (vnet_lisp_add_del_mreq_itr_rloc_args_t * a)
1997{
Florin Corasa2157cf2016-08-16 21:09:14 +02001998 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
1999 uword *p = 0;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002000
Florin Corasf727db92016-06-23 15:01:58 +02002001 if (vnet_lisp_enable_disable_status () == 0)
2002 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002003 clib_warning ("LISP is disabled!");
Florin Corasf727db92016-06-23 15:01:58 +02002004 return VNET_API_ERROR_LISP_DISABLED;
2005 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002006
2007 if (a->is_add)
2008 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002009 p = hash_get_mem (lcm->locator_set_index_by_name, a->locator_set_name);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002010 if (!p)
Florin Corasa2157cf2016-08-16 21:09:14 +02002011 {
2012 clib_warning ("locator-set %v doesn't exist", a->locator_set_name);
2013 return VNET_API_ERROR_INVALID_ARGUMENT;
2014 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002015
2016 lcm->mreq_itr_rlocs = p[0];
2017 }
2018 else
2019 {
2020 lcm->mreq_itr_rlocs = ~0;
2021 }
2022
2023 return 0;
2024}
2025
Florin Corase127a7e2016-02-18 22:20:01 +01002026/* Statistics (not really errors) */
2027#define foreach_lisp_cp_lookup_error \
2028_(DROP, "drop") \
Filip Tehlard5a65db2017-05-17 17:21:10 +02002029_(MAP_REQUESTS_SENT, "map-request sent") \
2030_(ARP_REPLY_TX, "ARP replies sent")
Florin Corase127a7e2016-02-18 22:20:01 +01002031
Florin Corasa2157cf2016-08-16 21:09:14 +02002032static char *lisp_cp_lookup_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01002033#define _(sym,string) string,
2034 foreach_lisp_cp_lookup_error
2035#undef _
2036};
2037
2038typedef enum
2039{
2040#define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02002041 foreach_lisp_cp_lookup_error
Florin Corase127a7e2016-02-18 22:20:01 +01002042#undef _
2043 LISP_CP_LOOKUP_N_ERROR,
2044} lisp_cp_lookup_error_t;
2045
2046typedef enum
2047{
2048 LISP_CP_LOOKUP_NEXT_DROP,
Filip Tehlard5a65db2017-05-17 17:21:10 +02002049 LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX,
Florin Corase127a7e2016-02-18 22:20:01 +01002050 LISP_CP_LOOKUP_N_NEXT,
2051} lisp_cp_lookup_next_t;
2052
2053typedef struct
2054{
2055 gid_address_t dst_eid;
Andrej Kozemcak94e34762016-05-25 12:43:21 +02002056 ip_address_t map_resolver_ip;
Florin Corase127a7e2016-02-18 22:20:01 +01002057} lisp_cp_lookup_trace_t;
2058
2059u8 *
2060format_lisp_cp_lookup_trace (u8 * s, va_list * args)
2061{
2062 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2063 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02002064 lisp_cp_lookup_trace_t *t = va_arg (*args, lisp_cp_lookup_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01002065
2066 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
Florin Corasa2157cf2016-08-16 21:09:14 +02002067 format_ip_address, &t->map_resolver_ip, format_gid_address,
2068 &t->dst_eid);
Florin Corase127a7e2016-02-18 22:20:01 +01002069 return s;
2070}
2071
Florin Coras1c494842016-06-16 21:08:56 +02002072int
2073get_mr_and_local_iface_ip (lisp_cp_main_t * lcm, ip_address_t * mr_ip,
Florin Corasa2157cf2016-08-16 21:09:14 +02002074 ip_address_t * sloc)
Florin Corasc2c90082016-06-13 18:37:15 +02002075{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002076 lisp_msmr_t *mrit;
Florin Corasa2157cf2016-08-16 21:09:14 +02002077 ip_address_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01002078
Florin Corasa2157cf2016-08-16 21:09:14 +02002079 if (vec_len (lcm->map_resolvers) == 0)
Florin Corase127a7e2016-02-18 22:20:01 +01002080 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002081 clib_warning ("No map-resolver configured");
Florin Coras1c494842016-06-16 21:08:56 +02002082 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002083 }
2084
Florin Corasddfafb82016-05-13 18:09:56 +02002085 /* find the first mr ip we have a route to and the ip of the
2086 * iface that has a route to it */
Florin Corasa2157cf2016-08-16 21:09:14 +02002087 vec_foreach (mrit, lcm->map_resolvers)
2088 {
2089 a = &mrit->address;
2090 if (0 != ip_fib_get_first_egress_ip_for_dst (lcm, a, sloc))
2091 {
2092 ip_address_copy (mr_ip, a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002093
Florin Corasa2157cf2016-08-16 21:09:14 +02002094 /* also update globals */
2095 return 1;
2096 }
2097 }
Florin Corasddfafb82016-05-13 18:09:56 +02002098
Florin Corasa2157cf2016-08-16 21:09:14 +02002099 clib_warning ("Can't find map-resolver and local interface ip!");
Florin Coras1c494842016-06-16 21:08:56 +02002100 return 0;
Florin Corasddfafb82016-05-13 18:09:56 +02002101}
Filip Tehlar254b0362016-04-07 10:04:34 +02002102
Filip Tehlarbeceab92016-04-20 17:21:55 +02002103static gid_address_t *
Filip Tehlar254b0362016-04-07 10:04:34 +02002104build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
2105{
Florin Corasa2157cf2016-08-16 21:09:14 +02002106 void *addr;
Filip Tehlar254b0362016-04-07 10:04:34 +02002107 u32 i;
Florin Corasa2157cf2016-08-16 21:09:14 +02002108 locator_t *loc;
2109 u32 *loc_indexp;
2110 ip_interface_address_t *ia = 0;
2111 gid_address_t gid_data, *gid = &gid_data;
2112 gid_address_t *rlocs = 0;
2113 ip_prefix_t *ippref = &gid_address_ippref (gid);
2114 ip_address_t *rloc = &ip_prefix_addr (ippref);
Filip Tehlar254b0362016-04-07 10:04:34 +02002115
Filip Tehlar324112f2016-06-02 16:07:38 +02002116 memset (gid, 0, sizeof (gid[0]));
Filip Tehlarbeceab92016-04-20 17:21:55 +02002117 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
Florin Corasa2157cf2016-08-16 21:09:14 +02002118 for (i = 0; i < vec_len (loc_set->locator_indices); i++)
Filip Tehlar254b0362016-04-07 10:04:34 +02002119 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002120 loc_indexp = vec_elt_at_index (loc_set->locator_indices, i);
Filip Tehlar254b0362016-04-07 10:04:34 +02002121 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
2122
Filip Tehlar254b0362016-04-07 10:04:34 +02002123 /* Add ipv4 locators first TODO sort them */
Florin Corasa2157cf2016-08-16 21:09:14 +02002124
2125 /* *INDENT-OFF* */
Filip Tehlar254b0362016-04-07 10:04:34 +02002126 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2127 loc->sw_if_index, 1 /* unnumbered */,
2128 ({
Florin Coras1c494842016-06-16 21:08:56 +02002129 addr = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
2130 ip_address_set (rloc, addr, IP4);
Florin Corasddfafb82016-05-13 18:09:56 +02002131 ip_prefix_len (ippref) = 32;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02002132 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02002133 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02002134 }));
2135
Filip Tehlar254b0362016-04-07 10:04:34 +02002136 /* Add ipv6 locators */
2137 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2138 loc->sw_if_index, 1 /* unnumbered */,
2139 ({
Florin Coras1c494842016-06-16 21:08:56 +02002140 addr = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
2141 ip_address_set (rloc, addr, IP6);
Florin Corasddfafb82016-05-13 18:09:56 +02002142 ip_prefix_len (ippref) = 128;
Andrej Kozemcak438109d2016-07-22 12:54:12 +02002143 ip_prefix_normalize (ippref);
Filip Tehlarbeceab92016-04-20 17:21:55 +02002144 vec_add1 (rlocs, gid[0]);
Filip Tehlar254b0362016-04-07 10:04:34 +02002145 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002146 /* *INDENT-ON* */
2147
Filip Tehlar254b0362016-04-07 10:04:34 +02002148 }
2149 return rlocs;
2150}
2151
Florin Corase127a7e2016-02-18 22:20:01 +01002152static vlib_buffer_t *
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002153build_map_request (lisp_cp_main_t * lcm, gid_address_t * deid,
2154 ip_address_t * sloc, ip_address_t * rloc,
2155 gid_address_t * itr_rlocs, u64 * nonce_res, u32 * bi_res)
2156{
2157 vlib_buffer_t *b;
2158 u32 bi;
2159 vlib_main_t *vm = lcm->vlib_main;
2160
2161 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2162 {
2163 clib_warning ("Can't allocate buffer for Map-Request!");
2164 return 0;
2165 }
2166
2167 b = vlib_get_buffer (vm, bi);
2168
2169 /* leave some space for the encap headers */
2170 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2171
2172 /* put lisp msg */
2173 lisp_msg_put_mreq (lcm, b, NULL, deid, itr_rlocs, 0 /* smr invoked */ ,
2174 1 /* rloc probe */ , nonce_res);
2175
2176 /* push outer ip header */
2177 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2178 rloc);
2179
2180 bi_res[0] = bi;
2181
2182 return b;
2183}
2184
2185static vlib_buffer_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02002186build_encapsulated_map_request (lisp_cp_main_t * lcm,
2187 gid_address_t * seid, gid_address_t * deid,
2188 locator_set_t * loc_set, ip_address_t * mr_ip,
2189 ip_address_t * sloc, u8 is_smr_invoked,
2190 u64 * nonce_res, u32 * bi_res)
Florin Corase127a7e2016-02-18 22:20:01 +01002191{
Florin Corasa2157cf2016-08-16 21:09:14 +02002192 vlib_buffer_t *b;
Florin Corase127a7e2016-02-18 22:20:01 +01002193 u32 bi;
Florin Corasa2157cf2016-08-16 21:09:14 +02002194 gid_address_t *rlocs = 0;
2195 vlib_main_t *vm = lcm->vlib_main;
Florin Corase127a7e2016-02-18 22:20:01 +01002196
2197 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2198 {
2199 clib_warning ("Can't allocate buffer for Map-Request!");
2200 return 0;
2201 }
2202
2203 b = vlib_get_buffer (vm, bi);
2204
2205 /* leave some space for the encap headers */
2206 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2207
Filip Tehlar254b0362016-04-07 10:04:34 +02002208 /* get rlocs */
2209 rlocs = build_itr_rloc_list (lcm, loc_set);
2210
Filip Tehlard5fcc462016-10-17 16:20:18 +02002211 if (MR_MODE_SRC_DST == lcm->map_request_mode
2212 && GID_ADDR_SRC_DST != gid_address_type (deid))
Florin Corasdca88042016-09-14 16:01:38 +02002213 {
2214 gid_address_t sd;
2215 memset (&sd, 0, sizeof (sd));
2216 build_src_dst (&sd, seid, deid);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002217 lisp_msg_put_mreq (lcm, b, seid, &sd, rlocs, is_smr_invoked,
2218 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02002219 }
2220 else
2221 {
2222 /* put lisp msg */
2223 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked,
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002224 0 /* rloc probe */ , nonce_res);
Florin Corasdca88042016-09-14 16:01:38 +02002225 }
Florin Corase127a7e2016-02-18 22:20:01 +01002226
2227 /* push ecm: udp-ip-lisp */
2228 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
2229
Florin Corase127a7e2016-02-18 22:20:01 +01002230 /* push outer ip header */
Florin Corasddfafb82016-05-13 18:09:56 +02002231 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
Florin Corasa2157cf2016-08-16 21:09:14 +02002232 mr_ip);
Florin Corase127a7e2016-02-18 22:20:01 +01002233
2234 bi_res[0] = bi;
Filip Tehlar254b0362016-04-07 10:04:34 +02002235
Florin Corasa2157cf2016-08-16 21:09:14 +02002236 vec_free (rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01002237 return b;
2238}
2239
2240static void
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002241reset_pending_mr_counters (pending_map_request_t * r)
Florin Corase127a7e2016-02-18 22:20:01 +01002242{
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002243 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
2244 r->retries_num = 0;
2245}
2246
2247static int
2248elect_map_resolver (lisp_cp_main_t * lcm)
2249{
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002250 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002251
2252 vec_foreach (mr, lcm->map_resolvers)
Florin Corasa2157cf2016-08-16 21:09:14 +02002253 {
2254 if (!mr->is_down)
2255 {
2256 ip_address_copy (&lcm->active_map_resolver, &mr->address);
2257 lcm->do_map_resolver_election = 0;
2258 return 1;
2259 }
2260 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002261 return 0;
2262}
2263
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002264static void
2265free_map_register_records (mapping_t * maps)
2266{
2267 mapping_t *map;
2268 vec_foreach (map, maps) vec_free (map->locators);
2269
2270 vec_free (maps);
2271}
2272
2273static void
2274add_locators (lisp_cp_main_t * lcm, mapping_t * m, u32 locator_set_index,
2275 ip_address_t * probed_loc)
2276{
2277 u32 *li;
2278 locator_t *loc, new;
2279 ip_interface_address_t *ia = 0;
2280 void *addr;
2281 ip_address_t *new_ip = &gid_address_ip (&new.address);
2282
2283 m->locators = 0;
2284 locator_set_t *ls = pool_elt_at_index (lcm->locator_set_pool,
2285 locator_set_index);
2286 vec_foreach (li, ls->locator_indices)
2287 {
2288 loc = pool_elt_at_index (lcm->locator_pool, li[0]);
2289 new = loc[0];
2290 if (loc->local)
2291 {
2292 /* *INDENT-OFF* */
2293 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
2294 loc->sw_if_index, 1 /* unnumbered */,
2295 ({
2296 addr = ip_interface_address_get_address (&lcm->im4->lookup_main,
2297 ia);
2298 ip_address_set (new_ip, addr, IP4);
2299 }));
2300
2301 /* Add ipv6 locators */
2302 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
2303 loc->sw_if_index, 1 /* unnumbered */,
2304 ({
2305 addr = ip_interface_address_get_address (&lcm->im6->lookup_main,
2306 ia);
2307 ip_address_set (new_ip, addr, IP6);
2308 }));
2309 /* *INDENT-ON* */
2310
2311 if (probed_loc && ip_address_cmp (probed_loc, new_ip) == 0)
2312 new.probed = 1;
2313 }
2314 vec_add1 (m->locators, new);
2315 }
2316}
2317
2318static mapping_t *
2319build_map_register_record_list (lisp_cp_main_t * lcm)
2320{
2321 mapping_t *recs = 0, rec, *m;
2322
2323 /* *INDENT-OFF* */
2324 pool_foreach(m, lcm->mapping_pool,
2325 {
2326 /* for now build only local mappings */
2327 if (!m->local)
2328 continue;
2329
2330 rec = m[0];
2331 add_locators (lcm, &rec, m->locator_set_index, NULL);
2332 vec_add1 (recs, rec);
2333 });
2334 /* *INDENT-ON* */
2335
2336 return recs;
2337}
2338
2339static int
2340update_map_register_auth_data (map_register_hdr_t * map_reg_hdr,
2341 lisp_key_type_t key_id, u8 * key,
2342 u16 auth_data_len, u32 msg_len)
2343{
2344 MREG_KEY_ID (map_reg_hdr) = clib_host_to_net_u16 (key_id);
2345 MREG_AUTH_DATA_LEN (map_reg_hdr) = clib_host_to_net_u16 (auth_data_len);
2346
2347 unsigned char *result = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
2348 (unsigned char *) map_reg_hdr, msg_len, NULL,
2349 NULL);
2350 clib_memcpy (MREG_DATA (map_reg_hdr), result, auth_data_len);
2351
2352 return 0;
2353}
2354
2355static vlib_buffer_t *
2356build_map_register (lisp_cp_main_t * lcm, ip_address_t * sloc,
2357 ip_address_t * ms_ip, u64 * nonce_res, u8 want_map_notif,
2358 mapping_t * records, lisp_key_type_t key_id, u8 * key,
2359 u32 * bi_res)
2360{
2361 void *map_reg_hdr;
2362 vlib_buffer_t *b;
2363 u32 bi, auth_data_len = 0, msg_len = 0;
2364 vlib_main_t *vm = lcm->vlib_main;
2365
2366 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
2367 {
2368 clib_warning ("Can't allocate buffer for Map-Register!");
2369 return 0;
2370 }
2371
2372 b = vlib_get_buffer (vm, bi);
2373
2374 /* leave some space for the encap headers */
2375 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
2376
2377 auth_data_len = auth_data_len_by_key_id (key_id);
2378 map_reg_hdr = lisp_msg_put_map_register (b, records, want_map_notif,
2379 auth_data_len, nonce_res,
2380 &msg_len);
2381
2382 update_map_register_auth_data (map_reg_hdr, key_id, key, auth_data_len,
2383 msg_len);
2384
2385 /* push outer ip header */
2386 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, sloc,
2387 ms_ip);
2388
2389 bi_res[0] = bi;
2390 return b;
2391}
2392
2393static int
2394get_egress_map_resolver_ip (lisp_cp_main_t * lcm, ip_address_t * ip)
2395{
2396 lisp_msmr_t *mr;
2397 while (lcm->do_map_resolver_election
2398 | (0 == ip_fib_get_first_egress_ip_for_dst (lcm,
2399 &lcm->active_map_resolver,
2400 ip)))
2401 {
2402 if (0 == elect_map_resolver (lcm))
2403 /* all map resolvers are down */
2404 {
2405 /* restart MR checking by marking all of them up */
2406 vec_foreach (mr, lcm->map_resolvers) mr->is_down = 0;
2407 return -1;
2408 }
2409 }
2410 return 0;
2411}
2412
Filip Tehlarcda70662017-01-16 10:30:03 +01002413/* CP output statistics */
2414#define foreach_lisp_cp_output_error \
2415_(MAP_REGISTERS_SENT, "map-registers sent") \
2416_(RLOC_PROBES_SENT, "rloc-probes sent")
2417
2418static char *lisp_cp_output_error_strings[] = {
2419#define _(sym,string) string,
2420 foreach_lisp_cp_output_error
2421#undef _
2422};
2423
2424typedef enum
2425{
2426#define _(sym,str) LISP_CP_OUTPUT_ERROR_##sym,
2427 foreach_lisp_cp_output_error
2428#undef _
2429 LISP_CP_OUTPUT_N_ERROR,
2430} lisp_cp_output_error_t;
2431
2432static uword
2433lisp_cp_output (vlib_main_t * vm, vlib_node_runtime_t * node,
2434 vlib_frame_t * from_frame)
2435{
2436 return 0;
2437}
2438
2439/* dummy node used only for statistics */
2440/* *INDENT-OFF* */
2441VLIB_REGISTER_NODE (lisp_cp_output_node) = {
2442 .function = lisp_cp_output,
2443 .name = "lisp-cp-output",
2444 .vector_size = sizeof (u32),
2445 .format_trace = format_lisp_cp_input_trace,
2446 .type = VLIB_NODE_TYPE_INTERNAL,
2447
2448 .n_errors = LISP_CP_OUTPUT_N_ERROR,
2449 .error_strings = lisp_cp_output_error_strings,
2450
2451 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
2452
2453 .next_nodes = {
2454 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
2455 },
2456};
2457/* *INDENT-ON* */
2458
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002459static int
2460send_rloc_probe (lisp_cp_main_t * lcm, gid_address_t * deid,
2461 u32 local_locator_set_index, ip_address_t * sloc,
2462 ip_address_t * rloc)
2463{
2464 locator_set_t *ls;
2465 u32 bi;
2466 vlib_buffer_t *b;
2467 vlib_frame_t *f;
2468 u64 nonce = 0;
2469 u32 next_index, *to_next;
2470 gid_address_t *itr_rlocs;
2471
2472 ls = pool_elt_at_index (lcm->locator_set_pool, local_locator_set_index);
2473 itr_rlocs = build_itr_rloc_list (lcm, ls);
2474
2475 b = build_map_request (lcm, deid, sloc, rloc, itr_rlocs, &nonce, &bi);
2476 vec_free (itr_rlocs);
2477 if (!b)
2478 return -1;
2479
2480 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2481
Filip Tehlar272c69e2017-02-15 16:40:35 +01002482 next_index = (ip_addr_version (rloc) == IP4) ?
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002483 ip4_lookup_node.index : ip6_lookup_node.index;
2484
2485 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2486
2487 /* Enqueue the packet */
2488 to_next = vlib_frame_vector_args (f);
2489 to_next[0] = bi;
2490 f->n_vectors = 1;
2491 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
2492
2493 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2494 return 0;
2495}
2496
2497static int
2498send_rloc_probes (lisp_cp_main_t * lcm)
2499{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002500 u8 lprio = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002501 mapping_t *lm;
2502 fwd_entry_t *e;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002503 locator_pair_t *lp;
Filip Tehlarcda70662017-01-16 10:30:03 +01002504 u32 si, rloc_probes_sent = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002505
2506 /* *INDENT-OFF* */
2507 pool_foreach (e, lcm->fwd_entry_pool,
2508 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002509 if (vec_len (e->locator_pairs) == 0)
2510 continue;
2511
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002512 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &e->leid);
2513 if (~0 == si)
2514 {
2515 clib_warning ("internal error: cannot find local eid %U in "
2516 "map-cache!", format_gid_address, &e->leid);
2517 continue;
2518 }
2519 lm = pool_elt_at_index (lcm->mapping_pool, si);
2520
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002521 /* get the best (lowest) priority */
2522 lprio = e->locator_pairs[0].priority;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002523
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002524 /* send rloc-probe for pair(s) with the best remote locator priority */
2525 vec_foreach (lp, e->locator_pairs)
2526 {
2527 if (lp->priority != lprio)
2528 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002529
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002530 /* get first remote locator */
2531 send_rloc_probe (lcm, &e->reid, lm->locator_set_index, &lp->lcl_loc,
2532 &lp->rmt_loc);
Filip Tehlarcda70662017-01-16 10:30:03 +01002533 rloc_probes_sent++;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002534 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002535 });
2536 /* *INDENT-ON* */
2537
Filip Tehlarcda70662017-01-16 10:30:03 +01002538 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2539 LISP_CP_OUTPUT_ERROR_RLOC_PROBES_SENT,
2540 rloc_probes_sent);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002541 return 0;
2542}
2543
2544static int
2545send_map_register (lisp_cp_main_t * lcm, u8 want_map_notif)
2546{
Filip Tehlarcda70662017-01-16 10:30:03 +01002547 u32 bi, map_registers_sent = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002548 vlib_buffer_t *b;
2549 ip_address_t sloc;
2550 vlib_frame_t *f;
2551 u64 nonce = 0;
2552 u32 next_index, *to_next;
2553 ip_address_t *ms = 0;
Filip Tehlarcf121c82017-05-03 10:12:44 +02002554 mapping_t *records, *r, *group, *k;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002555
2556 // TODO: support multiple map servers and do election
2557 if (0 == vec_len (lcm->map_servers))
2558 return -1;
2559
2560 ms = &lcm->map_servers[0].address;
2561
2562 if (0 == ip_fib_get_first_egress_ip_for_dst (lcm, ms, &sloc))
2563 {
2564 clib_warning ("no eligible interface address found for %U!",
2565 format_ip_address, &lcm->map_servers[0]);
2566 return -1;
2567 }
2568
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002569 records = build_map_register_record_list (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002570 if (!records)
2571 return -1;
2572
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002573 vec_foreach (r, records)
2574 {
2575 u8 *key = r->key;
2576 u8 key_id = r->key_id;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002577
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002578 if (!key)
2579 continue; /* no secret key -> map-register cannot be sent */
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002580
Filip Tehlarcf121c82017-05-03 10:12:44 +02002581 group = 0;
2582 vec_add1 (group, r[0]);
2583
2584 /* group mappings that share common key */
2585 for (k = r + 1; k < vec_end (records); k++)
2586 {
2587 if (k->key_id != r->key_id)
2588 continue;
2589
2590 if (vec_is_equal (k->key, r->key))
2591 {
2592 vec_add1 (group, k[0]);
2593 k->key = 0; /* don't process this mapping again */
2594 }
2595 }
2596
2597 b = build_map_register (lcm, &sloc, ms, &nonce, want_map_notif, group,
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002598 key_id, key, &bi);
Filip Tehlarcf121c82017-05-03 10:12:44 +02002599 vec_free (group);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002600 if (!b)
2601 continue;
2602
2603 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2604
2605 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2606 ip4_lookup_node.index : ip6_lookup_node.index;
2607
2608 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
2609
2610 /* Enqueue the packet */
2611 to_next = vlib_frame_vector_args (f);
2612 to_next[0] = bi;
2613 f->n_vectors = 1;
2614 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
Filip Tehlarcda70662017-01-16 10:30:03 +01002615 map_registers_sent++;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01002616
2617 hash_set (lcm->map_register_messages_by_nonce, nonce, 0);
2618 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002619 free_map_register_records (records);
2620
Filip Tehlarcda70662017-01-16 10:30:03 +01002621 vlib_node_increment_counter (vlib_get_main (), lisp_cp_output_node.index,
2622 LISP_CP_OUTPUT_ERROR_MAP_REGISTERS_SENT,
2623 map_registers_sent);
2624
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002625 return 0;
2626}
2627
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002628#define send_encapsulated_map_request(lcm, seid, deid, smr) \
2629 _send_encapsulated_map_request(lcm, seid, deid, smr, 0)
2630
2631#define resend_encapsulated_map_request(lcm, seid, deid, smr) \
2632 _send_encapsulated_map_request(lcm, seid, deid, smr, 1)
2633
2634static int
Florin Corasa2157cf2016-08-16 21:09:14 +02002635_send_encapsulated_map_request (lisp_cp_main_t * lcm,
2636 gid_address_t * seid, gid_address_t * deid,
2637 u8 is_smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002638{
Florin Corasa2157cf2016-08-16 21:09:14 +02002639 u32 next_index, bi = 0, *to_next, map_index;
2640 vlib_buffer_t *b;
2641 vlib_frame_t *f;
Florin Corase127a7e2016-02-18 22:20:01 +01002642 u64 nonce = 0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002643 locator_set_t *loc_set;
2644 mapping_t *map;
2645 pending_map_request_t *pmr, *duplicate_pmr = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002646 ip_address_t sloc;
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002647 u32 ls_index;
Florin Corase127a7e2016-02-18 22:20:01 +01002648
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002649 /* if there is already a pending request remember it */
Florin Corasa2157cf2016-08-16 21:09:14 +02002650
2651 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002652 pool_foreach(pmr, lcm->pending_map_requests_pool,
2653 ({
2654 if (!gid_address_cmp (&pmr->src, seid)
2655 && !gid_address_cmp (&pmr->dst, deid))
2656 {
2657 duplicate_pmr = pmr;
2658 break;
2659 }
2660 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02002661 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002662
2663 if (!is_resend && duplicate_pmr)
2664 {
2665 /* don't send the request if there is a pending map request already */
2666 return 0;
2667 }
2668
Florin Corase127a7e2016-02-18 22:20:01 +01002669 /* get locator-set for seid */
Filip Tehlar53f09e32016-05-19 14:25:44 +02002670 if (!lcm->lisp_pitr)
Florin Corase127a7e2016-02-18 22:20:01 +01002671 {
Filip Tehlar53f09e32016-05-19 14:25:44 +02002672 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
2673 if (map_index == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02002674 {
2675 clib_warning ("No local mapping found in eid-table for %U!",
2676 format_gid_address, seid);
2677 return -1;
2678 }
Filip Tehlar53f09e32016-05-19 14:25:44 +02002679
2680 map = pool_elt_at_index (lcm->mapping_pool, map_index);
2681
2682 if (!map->local)
Florin Corasa2157cf2016-08-16 21:09:14 +02002683 {
2684 clib_warning
2685 ("Mapping found for src eid %U is not marked as local!",
2686 format_gid_address, seid);
2687 return -1;
2688 }
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002689 ls_index = map->locator_set_index;
Filip Tehlar53f09e32016-05-19 14:25:44 +02002690 }
2691 else
2692 {
2693 map_index = lcm->pitr_map_index;
2694 map = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002695 ls_index = map->locator_set_index;
Florin Corase127a7e2016-02-18 22:20:01 +01002696 }
2697
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02002698 /* overwrite locator set if map-request itr-rlocs configured */
2699 if (~0 != lcm->mreq_itr_rlocs)
2700 {
2701 ls_index = lcm->mreq_itr_rlocs;
2702 }
2703
2704 loc_set = pool_elt_at_index (lcm->locator_set_pool, ls_index);
Florin Corase127a7e2016-02-18 22:20:01 +01002705
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002706 if (get_egress_map_resolver_ip (lcm, &sloc) < 0)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002707 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02002708 if (duplicate_pmr)
2709 duplicate_pmr->to_be_removed = 1;
2710 return -1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002711 }
Florin Corasddfafb82016-05-13 18:09:56 +02002712
Florin Corase127a7e2016-02-18 22:20:01 +01002713 /* build the encapsulated map request */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002714 b = build_encapsulated_map_request (lcm, seid, deid, loc_set,
Florin Corasa2157cf2016-08-16 21:09:14 +02002715 &lcm->active_map_resolver,
2716 &sloc, is_smr_invoked, &nonce, &bi);
Florin Corase127a7e2016-02-18 22:20:01 +01002717
2718 if (!b)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002719 return -1;
Florin Corase127a7e2016-02-18 22:20:01 +01002720
Florin Corasf727db92016-06-23 15:01:58 +02002721 /* set fib index to default and lookup node */
Florin Corasa2157cf2016-08-16 21:09:14 +02002722 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
2723 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
2724 ip4_lookup_node.index : ip6_lookup_node.index;
Florin Corase127a7e2016-02-18 22:20:01 +01002725
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002726 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
Florin Corase127a7e2016-02-18 22:20:01 +01002727
2728 /* Enqueue the packet */
2729 to_next = vlib_frame_vector_args (f);
2730 to_next[0] = bi;
2731 f->n_vectors = 1;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002732 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
Florin Corase127a7e2016-02-18 22:20:01 +01002733
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002734 if (duplicate_pmr)
2735 /* if there is a pending request already update it */
2736 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002737 if (clib_fifo_elts (duplicate_pmr->nonces) >= PENDING_MREQ_QUEUE_LEN)
2738 {
2739 /* remove the oldest nonce */
2740 u64 CLIB_UNUSED (tmp), *nonce_del;
2741 nonce_del = clib_fifo_head (duplicate_pmr->nonces);
2742 hash_unset (lcm->pending_map_requests_by_nonce, nonce_del[0]);
2743 clib_fifo_sub1 (duplicate_pmr->nonces, tmp);
2744 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002745
Florin Corasf4691cd2016-08-15 19:16:32 +02002746 clib_fifo_add1 (duplicate_pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002747 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02002748 duplicate_pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002749 }
2750 else
2751 {
2752 /* add map-request to pending requests table */
Florin Corasa2157cf2016-08-16 21:09:14 +02002753 pool_get (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002754 memset (pmr, 0, sizeof (*pmr));
2755 gid_address_copy (&pmr->src, seid);
2756 gid_address_copy (&pmr->dst, deid);
Florin Corasf4691cd2016-08-15 19:16:32 +02002757 clib_fifo_add1 (pmr->nonces, nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002758 pmr->is_smr_invoked = is_smr_invoked;
2759 reset_pending_mr_counters (pmr);
2760 hash_set (lcm->pending_map_requests_by_nonce, nonce,
Florin Corasa2157cf2016-08-16 21:09:14 +02002761 pmr - lcm->pending_map_requests_pool);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002762 }
2763
2764 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01002765}
2766
2767static void
Florin Corasa2157cf2016-08-16 21:09:14 +02002768get_src_and_dst_ip (void *hdr, ip_address_t * src, ip_address_t * dst)
Florin Corase127a7e2016-02-18 22:20:01 +01002769{
Florin Corasa2157cf2016-08-16 21:09:14 +02002770 ip4_header_t *ip4 = hdr;
2771 ip6_header_t *ip6;
Florin Corase127a7e2016-02-18 22:20:01 +01002772
2773 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
2774 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002775 ip_address_set (src, &ip4->src_address, IP4);
2776 ip_address_set (dst, &ip4->dst_address, IP4);
Florin Corase127a7e2016-02-18 22:20:01 +01002777 }
2778 else
2779 {
2780 ip6 = hdr;
Florin Corasa2157cf2016-08-16 21:09:14 +02002781 ip_address_set (src, &ip6->src_address, IP6);
2782 ip_address_set (dst, &ip6->dst_address, IP6);
Florin Corase127a7e2016-02-18 22:20:01 +01002783 }
2784}
2785
Filip Tehlar324112f2016-06-02 16:07:38 +02002786static u32
Florin Coras1a1adc72016-07-22 01:45:30 +02002787lisp_get_vni_from_buffer_ip (lisp_cp_main_t * lcm, vlib_buffer_t * b,
Florin Corasa2157cf2016-08-16 21:09:14 +02002788 u8 version)
Filip Tehlar324112f2016-06-02 16:07:38 +02002789{
Florin Corasa2157cf2016-08-16 21:09:14 +02002790 uword *vnip;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002791 u32 vni = ~0, table_id = ~0;
Filip Tehlar324112f2016-06-02 16:07:38 +02002792
Florin Coras87e40692016-09-22 18:02:17 +02002793 table_id = fib_table_get_table_id_for_sw_if_index ((version ==
2794 IP4 ? FIB_PROTOCOL_IP4 :
2795 FIB_PROTOCOL_IP6),
2796 vnet_buffer
2797 (b)->sw_if_index
2798 [VLIB_RX]);
Filip Tehlar324112f2016-06-02 16:07:38 +02002799
2800 vnip = hash_get (lcm->vni_by_table_id, table_id);
2801 if (vnip)
2802 vni = vnip[0];
2803 else
2804 clib_warning ("vrf %d is not mapped to any vni!", table_id);
2805
2806 return vni;
2807}
2808
Florin Coras1a1adc72016-07-22 01:45:30 +02002809always_inline u32
Filip Tehlard5a65db2017-05-17 17:21:10 +02002810lisp_get_bd_from_buffer_eth (vlib_buffer_t * b)
Florin Coras1a1adc72016-07-22 01:45:30 +02002811{
Florin Coras1a1adc72016-07-22 01:45:30 +02002812 u32 sw_if_index0;
2813
Florin Corasa2157cf2016-08-16 21:09:14 +02002814 l2input_main_t *l2im = &l2input_main;
2815 l2_input_config_t *config;
2816 l2_bridge_domain_t *bd_config;
Florin Coras1a1adc72016-07-22 01:45:30 +02002817
Florin Corasa2157cf2016-08-16 21:09:14 +02002818 sw_if_index0 = vnet_buffer (b)->sw_if_index[VLIB_RX];
2819 config = vec_elt_at_index (l2im->configs, sw_if_index0);
Florin Coras1a1adc72016-07-22 01:45:30 +02002820 bd_config = vec_elt_at_index (l2im->bd_configs, config->bd_index);
2821
Filip Tehlard5a65db2017-05-17 17:21:10 +02002822 return bd_config->bd_id;
2823}
2824
2825always_inline u32
2826lisp_get_vni_from_buffer_eth (lisp_cp_main_t * lcm, vlib_buffer_t * b)
2827{
2828 uword *vnip;
2829 u32 vni = ~0;
2830 u32 bd = lisp_get_bd_from_buffer_eth (b);
2831
2832 vnip = hash_get (lcm->vni_by_bd_id, bd);
Florin Coras1a1adc72016-07-22 01:45:30 +02002833 if (vnip)
2834 vni = vnip[0];
2835 else
Filip Tehlard5a65db2017-05-17 17:21:10 +02002836 clib_warning ("bridge domain %d is not mapped to any vni!", bd);
Florin Coras1a1adc72016-07-22 01:45:30 +02002837
2838 return vni;
2839}
2840
Filip Tehlar4868ff62017-03-09 16:48:39 +01002841void
Florin Corasa2157cf2016-08-16 21:09:14 +02002842get_src_and_dst_eids_from_buffer (lisp_cp_main_t * lcm, vlib_buffer_t * b,
Filip Tehlar4868ff62017-03-09 16:48:39 +01002843 gid_address_t * src, gid_address_t * dst,
2844 u16 type)
Florin Coras1a1adc72016-07-22 01:45:30 +02002845{
2846 u32 vni = 0;
Florin Coras1a1adc72016-07-22 01:45:30 +02002847
Filip Tehlara5abdeb2016-07-18 17:35:40 +02002848 memset (src, 0, sizeof (*src));
2849 memset (dst, 0, sizeof (*dst));
Florin Coras1a1adc72016-07-22 01:45:30 +02002850
Filip Tehlard5a65db2017-05-17 17:21:10 +02002851 gid_address_type (dst) = GID_ADDR_NO_ADDRESS;
2852 gid_address_type (src) = GID_ADDR_NO_ADDRESS;
2853
Florin Coras1a1adc72016-07-22 01:45:30 +02002854 if (LISP_AFI_IP == type || LISP_AFI_IP6 == type)
2855 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002856 ip4_header_t *ip;
Florin Coras1a1adc72016-07-22 01:45:30 +02002857 u8 version, preflen;
2858
Florin Corasa2157cf2016-08-16 21:09:14 +02002859 gid_address_type (src) = GID_ADDR_IP_PREFIX;
2860 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
Florin Coras1a1adc72016-07-22 01:45:30 +02002861
2862 ip = vlib_buffer_get_current (b);
Florin Corasa2157cf2016-08-16 21:09:14 +02002863 get_src_and_dst_ip (ip, &gid_address_ip (src), &gid_address_ip (dst));
Florin Coras1a1adc72016-07-22 01:45:30 +02002864
Florin Corasa2157cf2016-08-16 21:09:14 +02002865 version = gid_address_ip_version (src);
Florin Coras1a1adc72016-07-22 01:45:30 +02002866 preflen = ip_address_max_len (version);
Florin Corasa2157cf2016-08-16 21:09:14 +02002867 gid_address_ippref_len (src) = preflen;
2868 gid_address_ippref_len (dst) = preflen;
Florin Coras1a1adc72016-07-22 01:45:30 +02002869
2870 vni = lisp_get_vni_from_buffer_ip (lcm, b, version);
2871 gid_address_vni (dst) = vni;
2872 gid_address_vni (src) = vni;
2873 }
2874 else if (LISP_AFI_MAC == type)
2875 {
Florin Corasa2157cf2016-08-16 21:09:14 +02002876 ethernet_header_t *eh;
Filip Tehlard5a65db2017-05-17 17:21:10 +02002877 ethernet_arp_header_t *ah;
Florin Coras1a1adc72016-07-22 01:45:30 +02002878
2879 eh = vlib_buffer_get_current (b);
2880
Filip Tehlard5a65db2017-05-17 17:21:10 +02002881 if (clib_net_to_host_u16 (eh->type) == ETHERNET_TYPE_ARP)
2882 {
2883 ah = (ethernet_arp_header_t *) (((u8 *) eh) + sizeof (*eh));
2884 if (clib_net_to_host_u16 (ah->opcode)
2885 != ETHERNET_ARP_OPCODE_request)
2886 return;
Florin Coras1a1adc72016-07-22 01:45:30 +02002887
Filip Tehlard5a65db2017-05-17 17:21:10 +02002888 gid_address_type (dst) = GID_ADDR_ARP;
2889 gid_address_arp_bd (dst) = lisp_get_bd_from_buffer_eth (b);
2890 clib_memcpy (&gid_address_arp_ip4 (dst),
2891 &ah->ip4_over_ethernet[1].ip4, 4);
2892 }
2893 else
2894 {
2895 gid_address_type (src) = GID_ADDR_MAC;
2896 gid_address_type (dst) = GID_ADDR_MAC;
2897 mac_copy (&gid_address_mac (src), eh->src_address);
2898 mac_copy (&gid_address_mac (dst), eh->dst_address);
Florin Coras1a1adc72016-07-22 01:45:30 +02002899
Filip Tehlard5a65db2017-05-17 17:21:10 +02002900 /* get vni */
2901 vni = lisp_get_vni_from_buffer_eth (lcm, b);
2902
2903 gid_address_vni (dst) = vni;
2904 gid_address_vni (src) = vni;
2905 }
Florin Coras1a1adc72016-07-22 01:45:30 +02002906 }
Florin Corasce1b4c72017-01-26 14:25:34 -08002907 else if (LISP_AFI_LCAF == type)
2908 {
2909 /* Eventually extend this to support NSH and other */
2910 ASSERT (0);
2911 }
Florin Coras1a1adc72016-07-22 01:45:30 +02002912}
2913
Florin Corase127a7e2016-02-18 22:20:01 +01002914static uword
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002915lisp_cp_lookup_inline (vlib_main_t * vm,
2916 vlib_node_runtime_t * node,
2917 vlib_frame_t * from_frame, int overlay)
Florin Corase127a7e2016-02-18 22:20:01 +01002918{
Filip Tehlard5a65db2017-05-17 17:21:10 +02002919 u32 *from, *to_next, di, si;
Florin Corasa2157cf2016-08-16 21:09:14 +02002920 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlard5a65db2017-05-17 17:21:10 +02002921 u32 pkts_mapped = 0, next_index;
2922 uword n_left_from, n_left_to_next;
2923 vnet_main_t *vnm = vnet_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +01002924
2925 from = vlib_frame_vector_args (from_frame);
2926 n_left_from = from_frame->n_vectors;
Filip Tehlard5a65db2017-05-17 17:21:10 +02002927 next_index = node->cached_next_index;
Florin Corase127a7e2016-02-18 22:20:01 +01002928
2929 while (n_left_from > 0)
2930 {
Filip Tehlard5a65db2017-05-17 17:21:10 +02002931 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corase127a7e2016-02-18 22:20:01 +01002932
Filip Tehlard5a65db2017-05-17 17:21:10 +02002933 while (n_left_from > 0 && n_left_to_next > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02002934 {
Filip Tehlard5a65db2017-05-17 17:21:10 +02002935 u32 pi0, sw_if_index0, next0;
2936 u64 mac0;
Florin Corasa2157cf2016-08-16 21:09:14 +02002937 vlib_buffer_t *b0;
2938 gid_address_t src, dst;
Filip Tehlard5a65db2017-05-17 17:21:10 +02002939 ethernet_arp_header_t *arp0;
2940 ethernet_header_t *eth0;
2941 vnet_hw_interface_t *hw_if0;
Florin Corase127a7e2016-02-18 22:20:01 +01002942
Florin Corasa2157cf2016-08-16 21:09:14 +02002943 pi0 = from[0];
2944 from += 1;
2945 n_left_from -= 1;
Filip Tehlard5a65db2017-05-17 17:21:10 +02002946 to_next[0] = pi0;
2947 to_next += 1;
2948 n_left_to_next -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01002949
Florin Corasa2157cf2016-08-16 21:09:14 +02002950 b0 = vlib_get_buffer (vm, pi0);
Florin Corase127a7e2016-02-18 22:20:01 +01002951
Florin Corasa2157cf2016-08-16 21:09:14 +02002952 /* src/dst eid pair */
Filip Tehlar4868ff62017-03-09 16:48:39 +01002953 get_src_and_dst_eids_from_buffer (lcm, b0, &src, &dst, overlay);
Filip Tehlar324112f2016-06-02 16:07:38 +02002954
Filip Tehlard5a65db2017-05-17 17:21:10 +02002955 if (gid_address_type (&dst) == GID_ADDR_ARP)
2956 {
2957 mac0 = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
2958 if (GID_LOOKUP_MISS_L2 != mac0)
2959 {
2960 /* send ARP reply */
2961
2962 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2963 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
2964
2965 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
2966
2967 eth0 = vlib_buffer_get_current (b0);
2968 arp0 = (ethernet_arp_header_t *) (((u8 *) eth0)
2969 + sizeof (*eth0));
2970 arp0->opcode =
2971 clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_reply);
2972 arp0->ip4_over_ethernet[1] = arp0->ip4_over_ethernet[0];
2973 clib_memcpy (arp0->ip4_over_ethernet[0].ethernet,
2974 (u8 *) & mac0, 6);
2975 clib_memcpy (&arp0->ip4_over_ethernet[0].ip4,
2976 &gid_address_arp_ip4 (&dst), 4);
2977
2978 /* Hardware must be ethernet-like. */
2979 ASSERT (vec_len (hw_if0->hw_address) == 6);
2980
2981 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
2982 clib_memcpy (eth0->src_address, hw_if0->hw_address, 6);
2983
2984 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_ARP_REPLY_TX];
2985 next0 = LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX;
2986 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2987 to_next,
2988 n_left_to_next, pi0,
2989 next0);
2990 }
2991 continue;
2992 }
2993
Florin Corasa2157cf2016-08-16 21:09:14 +02002994 /* if we have remote mapping for destination already in map-chache
2995 add forwarding tunnel directly. If not send a map-request */
Florin Corasdca88042016-09-14 16:01:38 +02002996 di = gid_dictionary_sd_lookup (&lcm->mapping_index_by_gid, &dst,
2997 &src);
Florin Corasa2157cf2016-08-16 21:09:14 +02002998 if (~0 != di)
2999 {
3000 mapping_t *m = vec_elt_at_index (lcm->mapping_pool, di);
3001 /* send a map-request also in case of negative mapping entry
3002 with corresponding action */
3003 if (m->action == LISP_SEND_MAP_REQUEST)
3004 {
3005 /* send map-request */
3006 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3007 0 /* is_resend */ );
3008 pkts_mapped++;
3009 }
3010 else
3011 {
3012 si = gid_dictionary_lookup (&lcm->mapping_index_by_gid,
3013 &src);
3014 if (~0 != si)
3015 {
Filip Tehlarce1aae42017-01-04 10:42:25 +01003016 dp_add_fwd_entry_from_mt (si, di);
Florin Corasa2157cf2016-08-16 21:09:14 +02003017 }
3018 }
3019 }
3020 else
3021 {
3022 /* send map-request */
3023 queue_map_request (&src, &dst, 0 /* smr_invoked */ ,
3024 0 /* is_resend */ );
3025 pkts_mapped++;
3026 }
Florin Corase127a7e2016-02-18 22:20:01 +01003027
Filip Tehlard5a65db2017-05-17 17:21:10 +02003028 b0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
Florin Corasa2157cf2016-08-16 21:09:14 +02003029 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3030 {
3031 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, b0,
3032 sizeof (*tr));
Andrej Kozemcak94e34762016-05-25 12:43:21 +02003033
Florin Corasa2157cf2016-08-16 21:09:14 +02003034 memset (tr, 0, sizeof (*tr));
3035 gid_address_copy (&tr->dst_eid, &dst);
Florin Coras5a1c11b2016-09-06 16:29:34 +02003036 ip_address_copy (&tr->map_resolver_ip,
3037 &lcm->active_map_resolver);
Florin Corasa2157cf2016-08-16 21:09:14 +02003038 }
3039 gid_address_free (&dst);
3040 gid_address_free (&src);
Filip Tehlard5a65db2017-05-17 17:21:10 +02003041 next0 = LISP_CP_LOOKUP_NEXT_DROP;
3042 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3043 to_next,
3044 n_left_to_next, pi0, next0);
Florin Corasa2157cf2016-08-16 21:09:14 +02003045 }
Florin Corase127a7e2016-02-18 22:20:01 +01003046
Filip Tehlard5a65db2017-05-17 17:21:10 +02003047 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Florin Corase127a7e2016-02-18 22:20:01 +01003048 }
3049 vlib_node_increment_counter (vm, node->node_index,
Florin Corasa2157cf2016-08-16 21:09:14 +02003050 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
3051 pkts_mapped);
Florin Corase127a7e2016-02-18 22:20:01 +01003052 return from_frame->n_vectors;
3053}
3054
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003055static uword
3056lisp_cp_lookup_ip4 (vlib_main_t * vm,
3057 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3058{
3059 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP));
3060}
3061
3062static uword
3063lisp_cp_lookup_ip6 (vlib_main_t * vm,
3064 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3065{
3066 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_IP6));
3067}
3068
Neale Ranns5e575b12016-10-03 09:40:25 +01003069static uword
3070lisp_cp_lookup_l2 (vlib_main_t * vm,
3071 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3072{
3073 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_MAC));
3074}
3075
Florin Corasce1b4c72017-01-26 14:25:34 -08003076static uword
3077lisp_cp_lookup_nsh (vlib_main_t * vm,
3078 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
3079{
3080 /* TODO decide if NSH should be propagated as LCAF or not */
3081 return (lisp_cp_lookup_inline (vm, node, from_frame, LISP_AFI_LCAF));
3082}
3083
Florin Corasa2157cf2016-08-16 21:09:14 +02003084/* *INDENT-OFF* */
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003085VLIB_REGISTER_NODE (lisp_cp_lookup_ip4_node) = {
3086 .function = lisp_cp_lookup_ip4,
3087 .name = "lisp-cp-lookup-ip4",
3088 .vector_size = sizeof (u32),
3089 .format_trace = format_lisp_cp_lookup_trace,
3090 .type = VLIB_NODE_TYPE_INTERNAL,
3091
3092 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3093 .error_strings = lisp_cp_lookup_error_strings,
3094
3095 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3096
3097 .next_nodes = {
3098 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Filip Tehlard5a65db2017-05-17 17:21:10 +02003099 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003100 },
3101};
3102/* *INDENT-ON* */
3103
3104/* *INDENT-OFF* */
3105VLIB_REGISTER_NODE (lisp_cp_lookup_ip6_node) = {
3106 .function = lisp_cp_lookup_ip6,
3107 .name = "lisp-cp-lookup-ip6",
Florin Corase127a7e2016-02-18 22:20:01 +01003108 .vector_size = sizeof (u32),
3109 .format_trace = format_lisp_cp_lookup_trace,
3110 .type = VLIB_NODE_TYPE_INTERNAL,
3111
3112 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3113 .error_strings = lisp_cp_lookup_error_strings,
3114
3115 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3116
3117 .next_nodes = {
3118 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Filip Tehlard5a65db2017-05-17 17:21:10 +02003119 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
Neale Ranns5e575b12016-10-03 09:40:25 +01003120 },
3121};
3122/* *INDENT-ON* */
3123
3124/* *INDENT-OFF* */
3125VLIB_REGISTER_NODE (lisp_cp_lookup_l2_node) = {
3126 .function = lisp_cp_lookup_l2,
3127 .name = "lisp-cp-lookup-l2",
3128 .vector_size = sizeof (u32),
3129 .format_trace = format_lisp_cp_lookup_trace,
3130 .type = VLIB_NODE_TYPE_INTERNAL,
3131
3132 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3133 .error_strings = lisp_cp_lookup_error_strings,
3134
3135 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3136
3137 .next_nodes = {
3138 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Filip Tehlard5a65db2017-05-17 17:21:10 +02003139 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
Florin Corase127a7e2016-02-18 22:20:01 +01003140 },
3141};
Florin Corasa2157cf2016-08-16 21:09:14 +02003142/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01003143
Florin Corasce1b4c72017-01-26 14:25:34 -08003144/* *INDENT-OFF* */
3145VLIB_REGISTER_NODE (lisp_cp_lookup_nsh_node) = {
3146 .function = lisp_cp_lookup_nsh,
3147 .name = "lisp-cp-lookup-nsh",
3148 .vector_size = sizeof (u32),
3149 .format_trace = format_lisp_cp_lookup_trace,
3150 .type = VLIB_NODE_TYPE_INTERNAL,
3151
3152 .n_errors = LISP_CP_LOOKUP_N_ERROR,
3153 .error_strings = lisp_cp_lookup_error_strings,
3154
3155 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
3156
3157 .next_nodes = {
3158 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
Filip Tehlard5a65db2017-05-17 17:21:10 +02003159 [LISP_CP_LOOKUP_NEXT_ARP_REPLY_TX] = "interface-output",
Florin Corasce1b4c72017-01-26 14:25:34 -08003160 },
3161};
3162/* *INDENT-ON* */
3163
Florin Corase127a7e2016-02-18 22:20:01 +01003164/* lisp_cp_input statistics */
Filip Tehlarcda70662017-01-16 10:30:03 +01003165#define foreach_lisp_cp_input_error \
3166_(DROP, "drop") \
3167_(RLOC_PROBE_REQ_RECEIVED, "rloc-probe requests received") \
3168_(RLOC_PROBE_REP_RECEIVED, "rloc-probe replies received") \
3169_(MAP_NOTIFIES_RECEIVED, "map-notifies received") \
Florin Corase127a7e2016-02-18 22:20:01 +01003170_(MAP_REPLIES_RECEIVED, "map-replies received")
3171
Florin Corasa2157cf2016-08-16 21:09:14 +02003172static char *lisp_cp_input_error_strings[] = {
Florin Corase127a7e2016-02-18 22:20:01 +01003173#define _(sym,string) string,
3174 foreach_lisp_cp_input_error
3175#undef _
3176};
3177
3178typedef enum
3179{
3180#define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
Florin Corasa2157cf2016-08-16 21:09:14 +02003181 foreach_lisp_cp_input_error
Florin Corase127a7e2016-02-18 22:20:01 +01003182#undef _
3183 LISP_CP_INPUT_N_ERROR,
3184} lisp_cp_input_error_t;
3185
Florin Corase127a7e2016-02-18 22:20:01 +01003186typedef struct
3187{
3188 gid_address_t dst_eid;
3189 ip4_address_t map_resolver_ip;
3190} lisp_cp_input_trace_t;
3191
3192u8 *
3193format_lisp_cp_input_trace (u8 * s, va_list * args)
3194{
3195 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
3196 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Florin Corasa2157cf2016-08-16 21:09:14 +02003197 CLIB_UNUSED (lisp_cp_input_trace_t * t) =
3198 va_arg (*args, lisp_cp_input_trace_t *);
Florin Corase127a7e2016-02-18 22:20:01 +01003199
3200 s = format (s, "LISP-CP-INPUT: TODO");
3201 return s;
3202}
3203
Filip Tehlar9677a942016-11-28 10:23:31 +01003204static void
3205remove_expired_mapping (lisp_cp_main_t * lcm, u32 mi)
3206{
3207 mapping_t *m;
Florin Corasf3dc11a2017-01-24 03:13:43 -08003208 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3209 memset (adj_args, 0, sizeof (adj_args[0]));
Filip Tehlar9677a942016-11-28 10:23:31 +01003210
3211 m = pool_elt_at_index (lcm->mapping_pool, mi);
Florin Corasf3dc11a2017-01-24 03:13:43 -08003212
3213 gid_address_copy (&adj_args->reid, &m->eid);
3214 adj_args->is_add = 0;
3215 if (vnet_lisp_add_del_adjacency (adj_args))
3216 clib_warning ("failed to del adjacency!");
3217
Filip Tehlar9677a942016-11-28 10:23:31 +01003218 vnet_lisp_add_del_mapping (&m->eid, 0, 0, 0, ~0, 0 /* is_add */ ,
3219 0 /* is_static */ , 0);
3220 mapping_delete_timer (lcm, mi);
3221}
3222
3223static void
3224mapping_start_expiration_timer (lisp_cp_main_t * lcm, u32 mi,
3225 f64 expiration_time)
3226{
3227 mapping_t *m;
3228 u64 now = clib_cpu_time_now ();
3229 u64 cpu_cps = lcm->vlib_main->clib_time.clocks_per_second;
3230 u64 exp_clock_time = now + expiration_time * cpu_cps;
3231
3232 m = pool_elt_at_index (lcm->mapping_pool, mi);
3233
3234 m->timer_set = 1;
3235 timing_wheel_insert (&lcm->wheel, exp_clock_time, mi);
3236}
3237
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003238static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003239map_records_arg_free (map_records_arg_t * a)
Florin Corase127a7e2016-02-18 22:20:01 +01003240{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003241 mapping_t *m;
3242 vec_foreach (m, a->mappings)
3243 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003244 vec_free (m->locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003245 gid_address_free (&m->eid);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003246 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003247
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003248 clib_mem_free (a);
3249}
3250
3251void *
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003252process_map_reply (map_records_arg_t * a)
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003253{
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003254 mapping_t *m;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003255 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3256 u32 dst_map_index = 0;
3257 pending_map_request_t *pmr;
3258 u64 *noncep;
3259 uword *pmr_index;
3260
3261 if (a->is_rloc_probe)
3262 goto done;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003263
Florin Corase127a7e2016-02-18 22:20:01 +01003264 /* Check pending requests table and nonce */
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003265 pmr_index = hash_get (lcm->pending_map_requests_by_nonce, a->nonce);
Florin Corase127a7e2016-02-18 22:20:01 +01003266 if (!pmr_index)
3267 {
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003268 clib_warning ("No pending map-request entry with nonce %lu!", a->nonce);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003269 goto done;
Florin Corase127a7e2016-02-18 22:20:01 +01003270 }
Florin Corasa2157cf2016-08-16 21:09:14 +02003271 pmr = pool_elt_at_index (lcm->pending_map_requests_pool, pmr_index[0]);
Florin Corase127a7e2016-02-18 22:20:01 +01003272
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003273 vec_foreach (m, a->mappings)
3274 {
3275 /* insert/update mappings cache */
3276 vnet_lisp_add_del_mapping (&m->eid, m->locators, m->action,
3277 m->authoritative, m->ttl,
3278 1, 0 /* is_static */ , &dst_map_index);
Florin Corase127a7e2016-02-18 22:20:01 +01003279
Florin Corasba888e42017-01-24 11:38:18 -08003280 if (dst_map_index == (u32) ~ 0)
3281 continue;
3282
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003283 /* try to program forwarding only if mapping saved or updated */
Florin Corasba888e42017-01-24 11:38:18 -08003284 vnet_lisp_add_del_adjacency_args_t _adj_args, *adj_args = &_adj_args;
3285 memset (adj_args, 0, sizeof (adj_args[0]));
Florin Corasf3dc11a2017-01-24 03:13:43 -08003286
Florin Corasba888e42017-01-24 11:38:18 -08003287 gid_address_copy (&adj_args->leid, &pmr->src);
3288 gid_address_copy (&adj_args->reid, &m->eid);
3289 adj_args->is_add = 1;
3290 if (vnet_lisp_add_del_adjacency (adj_args))
3291 clib_warning ("failed to add adjacency!");
Florin Corasf3dc11a2017-01-24 03:13:43 -08003292
Florin Corasba888e42017-01-24 11:38:18 -08003293 if ((u32) ~ 0 != m->ttl)
3294 mapping_start_expiration_timer (lcm, dst_map_index, m->ttl * 60);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003295 }
Florin Corase127a7e2016-02-18 22:20:01 +01003296
3297 /* remove pending map request entry */
Florin Corasa2157cf2016-08-16 21:09:14 +02003298
3299 /* *INDENT-OFF* */
Florin Corasf4691cd2016-08-15 19:16:32 +02003300 clib_fifo_foreach (noncep, pmr->nonces, ({
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003301 hash_unset(lcm->pending_map_requests_by_nonce, noncep[0]);
Florin Corasf4691cd2016-08-15 19:16:32 +02003302 }));
Florin Corasa2157cf2016-08-16 21:09:14 +02003303 /* *INDENT-ON* */
3304
3305 clib_fifo_free (pmr->nonces);
3306 pool_put (lcm->pending_map_requests_pool, pmr);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003307
3308done:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003309 map_records_arg_free (a);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003310 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003311}
3312
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003313static int
3314is_auth_data_valid (map_notify_hdr_t * h, u32 msg_len,
3315 lisp_key_type_t key_id, u8 * key)
3316{
3317 u8 *auth_data = 0;
3318 u16 auth_data_len;
3319 int result;
3320
3321 auth_data_len = auth_data_len_by_key_id (key_id);
3322 if ((u16) ~ 0 == auth_data_len)
3323 {
3324 clib_warning ("invalid length for key_id %d!", key_id);
3325 return 0;
3326 }
3327
3328 /* save auth data */
3329 vec_validate (auth_data, auth_data_len - 1);
3330 clib_memcpy (auth_data, MNOTIFY_DATA (h), auth_data_len);
3331
3332 /* clear auth data */
3333 memset (MNOTIFY_DATA (h), 0, auth_data_len);
3334
3335 /* get hash of the message */
3336 unsigned char *code = HMAC (get_encrypt_fcn (key_id), key, vec_len (key),
3337 (unsigned char *) h, msg_len, NULL, NULL);
3338
3339 result = memcmp (code, auth_data, auth_data_len);
3340
3341 vec_free (auth_data);
3342
3343 return !result;
3344}
3345
3346static void
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003347process_map_notify (map_records_arg_t * a)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003348{
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003349 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003350 uword *pmr_index;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003351
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003352 pmr_index = hash_get (lcm->map_register_messages_by_nonce, a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003353 if (!pmr_index)
3354 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003355 clib_warning ("No pending map-register entry with nonce %lu!",
3356 a->nonce);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003357 return;
3358 }
3359
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003360 map_records_arg_free (a);
3361 hash_unset (lcm->map_register_messages_by_nonce, a->nonce);
3362}
3363
3364static mapping_t *
3365get_mapping (lisp_cp_main_t * lcm, gid_address_t * e)
3366{
3367 u32 mi;
3368
3369 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, e);
3370 if (~0 == mi)
3371 {
3372 clib_warning ("eid %U not found in map-cache!", unformat_gid_address,
3373 e);
3374 return 0;
3375 }
3376 return pool_elt_at_index (lcm->mapping_pool, mi);
3377}
3378
3379/**
3380 * When map-notify is received it is necessary that all EIDs in the record
3381 * list share common key. The key is then used to verify authentication
3382 * data in map-notify message.
3383 */
3384static int
3385map_record_integrity_check (lisp_cp_main_t * lcm, mapping_t * maps,
3386 u32 key_id, u8 ** key_out)
3387{
3388 u32 i, len = vec_len (maps);
3389 mapping_t *m;
3390
3391 /* get key of the first mapping */
3392 m = get_mapping (lcm, &maps[0].eid);
3393 if (!m || !m->key)
3394 return -1;
3395
3396 key_out[0] = m->key;
3397
3398 for (i = 1; i < len; i++)
3399 {
3400 m = get_mapping (lcm, &maps[i].eid);
3401 if (!m || !m->key)
3402 return -1;
3403
3404 if (key_id != m->key_id || vec_cmp (m->key, key_out[0]))
3405 {
3406 clib_warning ("keys does not match! %v, %v", key_out[0], m->key);
3407 return -1;
3408 }
3409 }
3410 return 0;
3411}
3412
3413static int
3414parse_map_records (vlib_buffer_t * b, map_records_arg_t * a, u8 count)
3415{
3416 locator_t *locators = 0;
3417 u32 i, len;
3418 gid_address_t deid;
3419 mapping_t m;
3420 locator_t *loc;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003421
Filip Tehlar68c74fc2017-05-04 14:52:42 +02003422 memset (&m, 0, sizeof (m));
3423
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003424 /* parse record eid */
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003425 for (i = 0; i < count; i++)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003426 {
3427 len = lisp_msg_parse_mapping_record (b, &deid, &locators, NULL);
3428 if (len == ~0)
3429 {
3430 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003431 vec_foreach (loc, locators) locator_free (loc);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003432 vec_free (locators);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003433 return -1;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003434 }
3435
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003436 m.locators = locators;
3437 gid_address_copy (&m.eid, &deid);
3438 vec_add1 (a->mappings, m);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003439 }
3440
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003441 return 0;
3442}
3443
3444static map_records_arg_t *
3445parse_map_notify (vlib_buffer_t * b)
3446{
3447 int rc = 0;
3448 map_notify_hdr_t *mnotif_hdr;
3449 lisp_key_type_t key_id;
3450 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3451 u8 *key = 0;
3452 gid_address_t deid;
3453 u16 auth_data_len = 0;
3454 u8 record_count;
3455 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
3456
3457 memset (a, 0, sizeof (*a));
3458 mnotif_hdr = vlib_buffer_get_current (b);
3459 vlib_buffer_pull (b, sizeof (*mnotif_hdr));
3460 memset (&deid, 0, sizeof (deid));
3461
3462 a->nonce = MNOTIFY_NONCE (mnotif_hdr);
3463 key_id = clib_net_to_host_u16 (MNOTIFY_KEY_ID (mnotif_hdr));
3464 auth_data_len = auth_data_len_by_key_id (key_id);
3465
3466 /* advance buffer by authentication data */
3467 vlib_buffer_pull (b, auth_data_len);
3468
3469 record_count = MNOTIFY_REC_COUNT (mnotif_hdr);
3470 rc = parse_map_records (b, a, record_count);
3471 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003472 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003473 map_records_arg_free (a);
3474 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003475 }
3476
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003477 rc = map_record_integrity_check (lcm, a->mappings, key_id, &key);
3478 if (rc != 0)
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003479 {
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003480 map_records_arg_free (a);
3481 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003482 }
3483
3484 /* verify authentication data */
3485 if (!is_auth_data_valid (mnotif_hdr, vlib_buffer_get_tail (b)
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003486 - (u8 *) mnotif_hdr, key_id, key))
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003487 {
3488 clib_warning ("Map-notify auth data verification failed for nonce %lu!",
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003489 a->nonce);
3490 map_records_arg_free (a);
3491 return 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003492 }
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003493 return a;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003494}
3495
3496static vlib_buffer_t *
3497build_map_reply (lisp_cp_main_t * lcm, ip_address_t * sloc,
3498 ip_address_t * dst, u64 nonce, u8 probe_bit,
3499 mapping_t * records, u16 dst_port, u32 * bi_res)
3500{
3501 vlib_buffer_t *b;
3502 u32 bi;
3503 vlib_main_t *vm = lcm->vlib_main;
3504
3505 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
3506 {
3507 clib_warning ("Can't allocate buffer for Map-Register!");
3508 return 0;
3509 }
3510
3511 b = vlib_get_buffer (vm, bi);
3512
3513 /* leave some space for the encap headers */
3514 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
3515
3516 lisp_msg_put_map_reply (b, records, nonce, probe_bit);
3517
3518 /* push outer ip header */
3519 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, dst_port, sloc, dst);
3520
3521 bi_res[0] = bi;
3522 return b;
3523}
3524
3525static int
3526send_map_reply (lisp_cp_main_t * lcm, u32 mi, ip_address_t * dst,
3527 u8 probe_bit, u64 nonce, u16 dst_port,
3528 ip_address_t * probed_loc)
3529{
3530 ip_address_t src;
3531 u32 bi;
3532 vlib_buffer_t *b;
3533 vlib_frame_t *f;
3534 u32 next_index, *to_next;
3535 mapping_t *records = 0, *m;
3536
3537 m = pool_elt_at_index (lcm->mapping_pool, mi);
3538 if (!m)
3539 return -1;
3540
3541 vec_add1 (records, m[0]);
3542 add_locators (lcm, &records[0], m->locator_set_index, probed_loc);
3543 memset (&src, 0, sizeof (src));
3544
3545 if (!ip_fib_get_first_egress_ip_for_dst (lcm, dst, &src))
3546 {
3547 clib_warning ("can't find inteface address for %U", format_ip_address,
3548 dst);
3549 return -1;
3550 }
3551
3552 b = build_map_reply (lcm, &src, dst, nonce, probe_bit, records, dst_port,
3553 &bi);
3554 if (!b)
3555 return -1;
3556 free_map_register_records (records);
3557
3558 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
3559 next_index = (ip_addr_version (&lcm->active_map_resolver) == IP4) ?
3560 ip4_lookup_node.index : ip6_lookup_node.index;
3561
3562 f = vlib_get_frame_to_node (lcm->vlib_main, next_index);
3563
3564 /* Enqueue the packet */
3565 to_next = vlib_frame_vector_args (f);
3566 to_next[0] = bi;
3567 f->n_vectors = 1;
3568 vlib_put_frame_to_node (lcm->vlib_main, next_index, f);
3569 return 0;
3570}
3571
Filip Tehlarb601f222017-01-02 10:22:56 +01003572static void
3573find_ip_header (vlib_buffer_t * b, u8 ** ip_hdr)
3574{
3575 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
3576 if (start < 0 && start < -sizeof (b->pre_data))
3577 {
3578 *ip_hdr = 0;
3579 return;
3580 }
3581
3582 *ip_hdr = b->data + start;
3583 if ((u8 *) * ip_hdr > (u8 *) vlib_buffer_get_current (b))
3584 *ip_hdr = 0;
3585}
3586
Florin Corase127a7e2016-02-18 22:20:01 +01003587void
Filip Tehlarcda70662017-01-16 10:30:03 +01003588process_map_request (vlib_main_t * vm, vlib_node_runtime_t * node,
3589 lisp_cp_main_t * lcm, vlib_buffer_t * b)
Florin Corase127a7e2016-02-18 22:20:01 +01003590{
Filip Tehlarb601f222017-01-02 10:22:56 +01003591 u8 *ip_hdr = 0;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003592 ip_address_t *dst_loc = 0, probed_loc, src_loc;
3593 mapping_t m;
Florin Corasa2157cf2016-08-16 21:09:14 +02003594 map_request_hdr_t *mreq_hdr;
Florin Corase127a7e2016-02-18 22:20:01 +01003595 gid_address_t src, dst;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003596 u64 nonce;
Filip Tehlarcda70662017-01-16 10:30:03 +01003597 u32 i, len = 0, rloc_probe_recv = 0;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003598 gid_address_t *itr_rlocs = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003599
3600 mreq_hdr = vlib_buffer_get_current (b);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003601 if (!MREQ_SMR (mreq_hdr) && !MREQ_RLOC_PROBE (mreq_hdr))
Florin Corasa2157cf2016-08-16 21:09:14 +02003602 {
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003603 clib_warning
3604 ("Only SMR Map-Requests and RLOC probe supported for now!");
Florin Corase127a7e2016-02-18 22:20:01 +01003605 return;
Florin Corasa2157cf2016-08-16 21:09:14 +02003606 }
Florin Corase127a7e2016-02-18 22:20:01 +01003607
Filip Tehlarb601f222017-01-02 10:22:56 +01003608 vlib_buffer_pull (b, sizeof (*mreq_hdr));
3609 nonce = MREQ_NONCE (mreq_hdr);
3610
Florin Corase127a7e2016-02-18 22:20:01 +01003611 /* parse src eid */
3612 len = lisp_msg_parse_addr (b, &src);
3613 if (len == ~0)
3614 return;
3615
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003616 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs,
3617 MREQ_ITR_RLOC_COUNT (mreq_hdr) + 1);
Florin Corase127a7e2016-02-18 22:20:01 +01003618 if (len == ~0)
Filip Tehlarcda70662017-01-16 10:30:03 +01003619 goto done;
Florin Corase127a7e2016-02-18 22:20:01 +01003620
3621 /* parse eid records and send SMR-invoked map-requests */
Florin Corasa2157cf2016-08-16 21:09:14 +02003622 for (i = 0; i < MREQ_REC_COUNT (mreq_hdr); i++)
Florin Corase127a7e2016-02-18 22:20:01 +01003623 {
Florin Corasa2157cf2016-08-16 21:09:14 +02003624 memset (&dst, 0, sizeof (dst));
Florin Corase127a7e2016-02-18 22:20:01 +01003625 len = lisp_msg_parse_eid_rec (b, &dst);
3626 if (len == ~0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003627 {
3628 clib_warning ("Can't parse map-request EID-record");
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003629 goto done;
Florin Corasa2157cf2016-08-16 21:09:14 +02003630 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003631
3632 if (MREQ_SMR (mreq_hdr))
3633 {
3634 /* send SMR-invoked map-requests */
3635 queue_map_request (&dst, &src, 1 /* invoked */ , 0 /* resend */ );
3636 }
3637 else if (MREQ_RLOC_PROBE (mreq_hdr))
3638 {
Filip Tehlarb601f222017-01-02 10:22:56 +01003639 find_ip_header (b, &ip_hdr);
3640 if (!ip_hdr)
3641 {
3642 clib_warning ("Cannot find the IP header!");
Filip Tehlarcda70662017-01-16 10:30:03 +01003643 goto done;
Filip Tehlarb601f222017-01-02 10:22:56 +01003644 }
Filip Tehlarcda70662017-01-16 10:30:03 +01003645 rloc_probe_recv++;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003646 memset (&m, 0, sizeof (m));
3647 u32 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &dst);
3648
3649 // TODO: select best locator; for now use the first one
3650 dst_loc = &gid_address_ip (&itr_rlocs[0]);
3651
3652 /* get src/dst IP addresses */
3653 get_src_and_dst_ip (ip_hdr, &src_loc, &probed_loc);
3654
3655 // TODO get source port from buffer
3656 u16 src_port = LISP_CONTROL_PORT;
3657
3658 send_map_reply (lcm, mi, dst_loc, 1 /* probe-bit */ , nonce,
3659 src_port, &probed_loc);
3660 }
Florin Corase127a7e2016-02-18 22:20:01 +01003661 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003662
3663done:
Filip Tehlarcda70662017-01-16 10:30:03 +01003664 vlib_node_increment_counter (vm, node->node_index,
3665 LISP_CP_INPUT_ERROR_RLOC_PROBE_REQ_RECEIVED,
3666 rloc_probe_recv);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003667 vec_free (itr_rlocs);
Florin Corase127a7e2016-02-18 22:20:01 +01003668}
3669
Filip Tehlar816f4372017-04-26 16:09:06 +02003670map_records_arg_t *
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003671parse_map_reply (vlib_buffer_t * b)
3672{
3673 locator_t probed;
3674 gid_address_t deid;
3675 void *h;
3676 u32 i, len = 0;
3677 mapping_t m;
3678 map_reply_hdr_t *mrep_hdr;
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003679 map_records_arg_t *a = clib_mem_alloc (sizeof (*a));
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003680 memset (a, 0, sizeof (*a));
3681 locator_t *locators;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003682
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003683 mrep_hdr = vlib_buffer_get_current (b);
3684 a->nonce = MREP_NONCE (mrep_hdr);
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003685 a->is_rloc_probe = MREP_RLOC_PROBE (mrep_hdr);
Filip Tehlar816f4372017-04-26 16:09:06 +02003686 if (!vlib_buffer_has_space (b, sizeof (*mrep_hdr)))
3687 {
3688 clib_mem_free (a);
3689 return 0;
3690 }
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003691 vlib_buffer_pull (b, sizeof (*mrep_hdr));
3692
3693 for (i = 0; i < MREP_REC_COUNT (mrep_hdr); i++)
3694 {
3695 memset (&m, 0, sizeof (m));
3696 locators = 0;
3697 h = vlib_buffer_get_current (b);
3698
3699 m.ttl = clib_net_to_host_u32 (MAP_REC_TTL (h));
3700 m.action = MAP_REC_ACTION (h);
3701 m.authoritative = MAP_REC_AUTH (h);
3702
3703 len = lisp_msg_parse_mapping_record (b, &deid, &locators, &probed);
3704 if (len == ~0)
3705 {
3706 clib_warning ("Failed to parse mapping record!");
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003707 map_records_arg_free (a);
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003708 return 0;
3709 }
3710
3711 m.locators = locators;
3712 gid_address_copy (&m.eid, &deid);
3713 vec_add1 (a->mappings, m);
3714 }
3715 return a;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003716}
3717
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003718static void
3719queue_map_reply_for_processing (map_records_arg_t * a)
3720{
Florin Coras655fcc42017-01-10 08:57:54 -08003721 vl_api_rpc_call_main_thread (process_map_reply, (u8 *) a, sizeof (*a));
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003722}
3723
3724static void
3725queue_map_notify_for_processing (map_records_arg_t * a)
3726{
3727 vl_api_rpc_call_main_thread (process_map_notify, (u8 *) a, sizeof (a[0]));
3728}
3729
Florin Corase127a7e2016-02-18 22:20:01 +01003730static uword
3731lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Corasa2157cf2016-08-16 21:09:14 +02003732 vlib_frame_t * from_frame)
Florin Corase127a7e2016-02-18 22:20:01 +01003733{
Filip Tehlarcda70662017-01-16 10:30:03 +01003734 u32 n_left_from, *from, *to_next_drop, rloc_probe_rep_recv = 0,
3735 map_notifies_recv = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003736 lisp_msg_type_e type;
Florin Corasa2157cf2016-08-16 21:09:14 +02003737 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003738 map_records_arg_t *a;
Florin Corase127a7e2016-02-18 22:20:01 +01003739
3740 from = vlib_frame_vector_args (from_frame);
3741 n_left_from = from_frame->n_vectors;
3742
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003743
Florin Corase127a7e2016-02-18 22:20:01 +01003744 while (n_left_from > 0)
3745 {
3746 u32 n_left_to_next_drop;
3747
3748 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
Florin Corasa2157cf2016-08-16 21:09:14 +02003749 to_next_drop, n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01003750 while (n_left_from > 0 && n_left_to_next_drop > 0)
Florin Corasa2157cf2016-08-16 21:09:14 +02003751 {
3752 u32 bi0;
3753 vlib_buffer_t *b0;
Florin Corase127a7e2016-02-18 22:20:01 +01003754
Florin Corasa2157cf2016-08-16 21:09:14 +02003755 bi0 = from[0];
3756 from += 1;
3757 n_left_from -= 1;
3758 to_next_drop[0] = bi0;
3759 to_next_drop += 1;
3760 n_left_to_next_drop -= 1;
Florin Corase127a7e2016-02-18 22:20:01 +01003761
Florin Corasa2157cf2016-08-16 21:09:14 +02003762 b0 = vlib_get_buffer (vm, bi0);
Florin Corase127a7e2016-02-18 22:20:01 +01003763
Florin Corasa2157cf2016-08-16 21:09:14 +02003764 type = lisp_msg_type (vlib_buffer_get_current (b0));
3765 switch (type)
3766 {
3767 case LISP_MAP_REPLY:
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003768 a = parse_map_reply (b0);
3769 if (a)
Filip Tehlarcda70662017-01-16 10:30:03 +01003770 {
3771 if (a->is_rloc_probe)
3772 rloc_probe_rep_recv++;
3773 queue_map_reply_for_processing (a);
3774 }
Florin Corasa2157cf2016-08-16 21:09:14 +02003775 break;
3776 case LISP_MAP_REQUEST:
Filip Tehlarcda70662017-01-16 10:30:03 +01003777 process_map_request (vm, node, lcm, b0);
Florin Corasa2157cf2016-08-16 21:09:14 +02003778 break;
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003779 case LISP_MAP_NOTIFY:
Filip Tehlarfb9931f2016-12-09 13:52:38 +01003780 a = parse_map_notify (b0);
3781 if (a)
Filip Tehlarcda70662017-01-16 10:30:03 +01003782 {
3783 map_notifies_recv++;
3784 queue_map_notify_for_processing (a);
3785 }
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003786 break;
Florin Corasa2157cf2016-08-16 21:09:14 +02003787 default:
3788 clib_warning ("Unsupported LISP message type %d", type);
3789 break;
3790 }
Florin Corase127a7e2016-02-18 22:20:01 +01003791
Florin Corasa2157cf2016-08-16 21:09:14 +02003792 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
Florin Corase127a7e2016-02-18 22:20:01 +01003793
Florin Corasa2157cf2016-08-16 21:09:14 +02003794 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
3795 {
Florin Corase127a7e2016-02-18 22:20:01 +01003796
Florin Corasa2157cf2016-08-16 21:09:14 +02003797 }
3798 }
Florin Corase127a7e2016-02-18 22:20:01 +01003799
Florin Corasa2157cf2016-08-16 21:09:14 +02003800 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
3801 n_left_to_next_drop);
Florin Corase127a7e2016-02-18 22:20:01 +01003802 }
Filip Tehlarcda70662017-01-16 10:30:03 +01003803 vlib_node_increment_counter (vm, node->node_index,
3804 LISP_CP_INPUT_ERROR_RLOC_PROBE_REP_RECEIVED,
3805 rloc_probe_rep_recv);
3806 vlib_node_increment_counter (vm, node->node_index,
3807 LISP_CP_INPUT_ERROR_MAP_NOTIFIES_RECEIVED,
3808 map_notifies_recv);
Florin Corase127a7e2016-02-18 22:20:01 +01003809 return from_frame->n_vectors;
3810}
3811
Florin Corasa2157cf2016-08-16 21:09:14 +02003812/* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +01003813VLIB_REGISTER_NODE (lisp_cp_input_node) = {
3814 .function = lisp_cp_input,
3815 .name = "lisp-cp-input",
3816 .vector_size = sizeof (u32),
3817 .format_trace = format_lisp_cp_input_trace,
3818 .type = VLIB_NODE_TYPE_INTERNAL,
3819
3820 .n_errors = LISP_CP_INPUT_N_ERROR,
3821 .error_strings = lisp_cp_input_error_strings,
3822
3823 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
3824
3825 .next_nodes = {
3826 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
3827 },
3828};
Florin Corasa2157cf2016-08-16 21:09:14 +02003829/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +01003830
3831clib_error_t *
Florin Corasa2157cf2016-08-16 21:09:14 +02003832lisp_cp_init (vlib_main_t * vm)
Florin Corase127a7e2016-02-18 22:20:01 +01003833{
Florin Corasa2157cf2016-08-16 21:09:14 +02003834 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3835 clib_error_t *error = 0;
Florin Corase127a7e2016-02-18 22:20:01 +01003836
3837 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
3838 return error;
3839
3840 lcm->im4 = &ip4_main;
3841 lcm->im6 = &ip6_main;
3842 lcm->vlib_main = vm;
Florin Corasa2157cf2016-08-16 21:09:14 +02003843 lcm->vnet_main = vnet_get_main ();
Andrej Kozemcakb6e4d392016-06-14 13:55:57 +02003844 lcm->mreq_itr_rlocs = ~0;
Florin Corasf727db92016-06-23 15:01:58 +02003845 lcm->lisp_pitr = 0;
Florin Corasba888e42017-01-24 11:38:18 -08003846 lcm->flags = 0;
Florin Coras5a1c11b2016-09-06 16:29:34 +02003847 memset (&lcm->active_map_resolver, 0, sizeof (lcm->active_map_resolver));
Florin Corase127a7e2016-02-18 22:20:01 +01003848
3849 gid_dictionary_init (&lcm->mapping_index_by_gid);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003850 lcm->do_map_resolver_election = 1;
Florin Corasdca88042016-09-14 16:01:38 +02003851 lcm->map_request_mode = MR_MODE_DST_ONLY;
Florin Corase127a7e2016-02-18 22:20:01 +01003852
Florin Coras577c3552016-04-21 00:45:40 +02003853 /* default vrf mapped to vni 0 */
Florin Corasa2157cf2016-08-16 21:09:14 +02003854 hash_set (lcm->table_id_by_vni, 0, 0);
3855 hash_set (lcm->vni_by_table_id, 0, 0);
Florin Coras577c3552016-04-21 00:45:40 +02003856
Florin Corase127a7e2016-02-18 22:20:01 +01003857 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
Florin Corasa2157cf2016-08-16 21:09:14 +02003858 lisp_cp_input_node.index, 1 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01003859 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
Florin Corasa2157cf2016-08-16 21:09:14 +02003860 lisp_cp_input_node.index, 0 /* is_ip4 */ );
Florin Corase127a7e2016-02-18 22:20:01 +01003861
Filip Tehlar9677a942016-11-28 10:23:31 +01003862 u64 now = clib_cpu_time_now ();
3863 timing_wheel_init (&lcm->wheel, now, vm->clib_time.clocks_per_second);
Florin Corase127a7e2016-02-18 22:20:01 +01003864 return 0;
3865}
3866
Filip Tehlar4868ff62017-03-09 16:48:39 +01003867static int
3868lisp_stats_api_fill (lisp_cp_main_t * lcm, lisp_gpe_main_t * lgm,
3869 lisp_api_stats_t * stat, lisp_stats_key_t * key,
3870 u32 stats_index)
3871{
Filip Tehlar21511912017-04-07 10:41:42 +02003872 vlib_counter_t v;
3873 vlib_combined_counter_main_t *cm = &lgm->counters;
Filip Tehlar4868ff62017-03-09 16:48:39 +01003874 lisp_gpe_fwd_entry_key_t fwd_key;
3875 const lisp_gpe_tunnel_t *lgt;
3876 fwd_entry_t *fe;
3877
3878 memset (stat, 0, sizeof (*stat));
3879 memset (&fwd_key, 0, sizeof (fwd_key));
3880
3881 fe = pool_elt_at_index (lcm->fwd_entry_pool, key->fwd_entry_index);
3882 ASSERT (fe != 0);
3883
3884 gid_to_dp_address (&fe->reid, &stat->deid);
3885 gid_to_dp_address (&fe->leid, &stat->seid);
3886 stat->vni = gid_address_vni (&fe->reid);
3887
3888 lgt = lisp_gpe_tunnel_get (key->tunnel_index);
3889 stat->loc_rloc = lgt->key->lcl;
3890 stat->rmt_rloc = lgt->key->rmt;
3891
Filip Tehlar21511912017-04-07 10:41:42 +02003892 vlib_get_combined_counter (cm, stats_index, &v);
3893 stat->counters = v;
Filip Tehlar4868ff62017-03-09 16:48:39 +01003894 return 1;
3895}
3896
3897lisp_api_stats_t *
3898vnet_lisp_get_stats (void)
3899{
3900 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
3901 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
3902 lisp_api_stats_t *stats = 0, stat;
3903 lisp_stats_key_t *key;
3904 u32 index;
3905
3906 /* *INDENT-OFF* */
3907 hash_foreach_mem (key, index, lgm->lisp_stats_index_by_key,
3908 {
3909 if (lisp_stats_api_fill (lcm, lgm, &stat, key, index))
3910 vec_add1 (stats, stat);
3911 });
3912 /* *INDENT-ON* */
3913
3914 return stats;
3915}
3916
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003917static void *
Florin Corasa2157cf2016-08-16 21:09:14 +02003918send_map_request_thread_fn (void *arg)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003919{
Florin Corasa2157cf2016-08-16 21:09:14 +02003920 map_request_args_t *a = arg;
3921 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003922
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003923 if (a->is_resend)
3924 resend_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
3925 else
Filip Tehlarcdab4bd2016-12-06 10:31:57 +01003926 send_encapsulated_map_request (lcm, &a->seid, &a->deid, a->smr_invoked);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003927
3928 return 0;
3929}
3930
3931static int
3932queue_map_request (gid_address_t * seid, gid_address_t * deid,
Florin Corasa2157cf2016-08-16 21:09:14 +02003933 u8 smr_invoked, u8 is_resend)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003934{
3935 map_request_args_t a;
3936
3937 a.is_resend = is_resend;
3938 gid_address_copy (&a.seid, seid);
3939 gid_address_copy (&a.deid, deid);
3940 a.smr_invoked = smr_invoked;
3941
3942 vl_api_rpc_call_main_thread (send_map_request_thread_fn,
Florin Corasa2157cf2016-08-16 21:09:14 +02003943 (u8 *) & a, sizeof (a));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003944 return 0;
3945}
3946
3947/**
3948 * Take an action with a pending map request depending on expiration time
3949 * and re-try counters.
3950 */
3951static void
3952update_pending_request (pending_map_request_t * r, f64 dt)
3953{
Florin Corasa2157cf2016-08-16 21:09:14 +02003954 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlar397fd7d2016-10-26 14:31:24 +02003955 lisp_msmr_t *mr;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003956
3957 if (r->time_to_expire - dt < 0)
3958 /* it's time to decide what to do with this pending request */
3959 {
3960 if (r->retries_num >= NUMBER_OF_RETRIES)
Florin Corasa2157cf2016-08-16 21:09:14 +02003961 /* too many retries -> assume current map resolver is not available */
3962 {
3963 mr = get_map_resolver (&lcm->active_map_resolver);
3964 if (!mr)
3965 {
3966 clib_warning ("Map resolver %U not found - probably deleted "
3967 "by the user recently.", format_ip_address,
3968 &lcm->active_map_resolver);
3969 }
3970 else
3971 {
3972 clib_warning ("map resolver %U is unreachable, ignoring",
3973 format_ip_address, &lcm->active_map_resolver);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003974
Florin Corasa2157cf2016-08-16 21:09:14 +02003975 /* mark current map resolver unavailable so it won't be
3976 * selected next time */
3977 mr->is_down = 1;
3978 mr->last_update = vlib_time_now (lcm->vlib_main);
3979 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003980
Florin Corasa2157cf2016-08-16 21:09:14 +02003981 reset_pending_mr_counters (r);
3982 elect_map_resolver (lcm);
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003983
Florin Corasa2157cf2016-08-16 21:09:14 +02003984 /* try to find a next eligible map resolver and re-send */
3985 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3986 1 /* resend */ );
3987 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003988 else
Florin Corasa2157cf2016-08-16 21:09:14 +02003989 {
3990 /* try again */
3991 queue_map_request (&r->src, &r->dst, r->is_smr_invoked,
3992 1 /* resend */ );
3993 r->retries_num++;
3994 r->time_to_expire = PENDING_MREQ_EXPIRATION_TIME;
3995 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02003996 }
3997 else
3998 r->time_to_expire -= dt;
3999}
4000
4001static void
4002remove_dead_pending_map_requests (lisp_cp_main_t * lcm)
4003{
Florin Corasa2157cf2016-08-16 21:09:14 +02004004 u64 *nonce;
4005 pending_map_request_t *pmr;
4006 u32 *to_be_removed = 0, *pmr_index;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004007
Florin Corasa2157cf2016-08-16 21:09:14 +02004008 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004009 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02004010 ({
4011 if (pmr->to_be_removed)
4012 {
4013 clib_fifo_foreach (nonce, pmr->nonces, ({
4014 hash_unset (lcm->pending_map_requests_by_nonce, nonce[0]);
4015 }));
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004016
Florin Corasa2157cf2016-08-16 21:09:14 +02004017 vec_add1 (to_be_removed, pmr - lcm->pending_map_requests_pool);
4018 }
4019 }));
4020 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004021
4022 vec_foreach (pmr_index, to_be_removed)
4023 pool_put_index (lcm->pending_map_requests_by_nonce, pmr_index[0]);
4024
4025 vec_free (to_be_removed);
4026}
4027
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004028static void
4029update_rloc_probing (lisp_cp_main_t * lcm, f64 dt)
4030{
4031 static f64 time_left = RLOC_PROBING_INTERVAL;
4032
4033 if (!lcm->is_enabled || !lcm->rloc_probing)
4034 return;
4035
4036 time_left -= dt;
4037 if (time_left <= 0)
4038 {
4039 time_left = RLOC_PROBING_INTERVAL;
4040 send_rloc_probes (lcm);
4041 }
4042}
4043
4044static void
4045update_map_register (lisp_cp_main_t * lcm, f64 dt)
4046{
4047 static f64 time_left = QUICK_MAP_REGISTER_INTERVAL;
4048 static u64 mreg_sent_counter = 0;
4049
4050 if (!lcm->is_enabled || !lcm->map_registering)
4051 return;
4052
4053 time_left -= dt;
4054 if (time_left <= 0)
4055 {
4056 if (mreg_sent_counter >= QUICK_MAP_REGISTER_MSG_COUNT)
4057 time_left = MAP_REGISTER_INTERVAL;
4058 else
4059 {
4060 mreg_sent_counter++;
4061 time_left = QUICK_MAP_REGISTER_INTERVAL;
4062 }
4063 send_map_register (lcm, 1 /* want map notify */ );
4064 }
4065}
4066
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004067static uword
4068send_map_resolver_service (vlib_main_t * vm,
Florin Corasa2157cf2016-08-16 21:09:14 +02004069 vlib_node_runtime_t * rt, vlib_frame_t * f)
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004070{
Filip Tehlar9677a942016-11-28 10:23:31 +01004071 u32 *expired = 0;
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004072 f64 period = 2.0;
Florin Corasa2157cf2016-08-16 21:09:14 +02004073 pending_map_request_t *pmr;
4074 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004075
4076 while (1)
4077 {
4078 vlib_process_wait_for_event_or_clock (vm, period);
4079
4080 /* currently no signals are expected - just wait for clock */
4081 (void) vlib_process_get_events (vm, 0);
4082
Florin Corasa2157cf2016-08-16 21:09:14 +02004083 /* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004084 pool_foreach (pmr, lcm->pending_map_requests_pool,
Florin Corasa2157cf2016-08-16 21:09:14 +02004085 ({
4086 if (!pmr->to_be_removed)
4087 update_pending_request (pmr, period);
4088 }));
4089 /* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004090
4091 remove_dead_pending_map_requests (lcm);
Filip Tehlar397fd7d2016-10-26 14:31:24 +02004092
4093 update_map_register (lcm, period);
4094 update_rloc_probing (lcm, period);
Filip Tehlar9677a942016-11-28 10:23:31 +01004095
4096 u64 now = clib_cpu_time_now ();
4097
4098 expired = timing_wheel_advance (&lcm->wheel, now, expired, 0);
4099 if (vec_len (expired) > 0)
4100 {
4101 u32 *mi = 0;
4102 vec_foreach (mi, expired)
4103 {
4104 remove_expired_mapping (lcm, mi[0]);
4105 }
4106 _vec_len (expired) = 0;
4107 }
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004108 }
4109
4110 /* unreachable */
4111 return 0;
4112}
4113
Filip Tehlar7eaf0e52017-03-08 08:46:51 +01004114vnet_api_error_t
4115vnet_lisp_stats_enable_disable (u8 enable)
4116{
4117 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4118
4119 if (vnet_lisp_enable_disable_status () == 0)
4120 return VNET_API_ERROR_LISP_DISABLED;
4121
Filip Tehlar4868ff62017-03-09 16:48:39 +01004122 if (enable)
4123 lcm->flags |= LISP_FLAG_STATS_ENABLED;
4124 else
4125 lcm->flags &= ~LISP_FLAG_STATS_ENABLED;
4126
Filip Tehlar7eaf0e52017-03-08 08:46:51 +01004127 return 0;
4128}
4129
4130u8
4131vnet_lisp_stats_enable_disable_state (void)
4132{
4133 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
4134
4135 if (vnet_lisp_enable_disable_status () == 0)
4136 return VNET_API_ERROR_LISP_DISABLED;
4137
Filip Tehlar4868ff62017-03-09 16:48:39 +01004138 return lcm->flags & LISP_FLAG_STATS_ENABLED;
Filip Tehlar7eaf0e52017-03-08 08:46:51 +01004139}
4140
Florin Corasa2157cf2016-08-16 21:09:14 +02004141/* *INDENT-OFF* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004142VLIB_REGISTER_NODE (lisp_retry_service_node,static) = {
4143 .function = send_map_resolver_service,
4144 .type = VLIB_NODE_TYPE_PROCESS,
4145 .name = "lisp-retry-service",
4146 .process_log2_n_stack_bytes = 16,
4147};
Florin Corasa2157cf2016-08-16 21:09:14 +02004148/* *INDENT-ON* */
Filip Tehlara5abdeb2016-07-18 17:35:40 +02004149
Florin Corasa2157cf2016-08-16 21:09:14 +02004150VLIB_INIT_FUNCTION (lisp_cp_init);
4151
4152/*
4153 * fd.io coding-style-patch-verification: ON
4154 *
4155 * Local Variables:
4156 * eval: (c-set-style "gnu")
4157 * End:
4158 */