blob: f810ae5fec46f8c4192861d7a3a7bce22be0ac03 [file] [log] [blame]
Neale Ranns5e575b12016-10-03 09:40:25 +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
16#include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
17#include <vnet/lisp-gpe/lisp_gpe_adjacency.h>
18#include <vnet/lisp-gpe/lisp_gpe_tenant.h>
19#include <vnet/lisp-cp/lisp_cp_dpo.h>
20#include <vnet/fib/fib_table.h>
21#include <vnet/fib/fib_entry.h>
22#include <vnet/fib/fib_path_list.h>
23#include <vnet/fib/ip6_fib.h>
24#include <vnet/fib/ip4_fib.h>
25#include <vnet/dpo/drop_dpo.h>
26#include <vnet/dpo/lookup_dpo.h>
27#include <vnet/dpo/load_balance.h>
28#include <vnet/adj/adj_midchain.h>
29
30/**
31 * @brief Add route to IP4 or IP6 Destination FIB.
32 *
33 * Add a route to the destination FIB that results in the lookup
34 * in the SRC FIB. The SRC FIB is created is it does not yet exist.
35 *
36 * @param[in] dst_table_id Destination FIB Table-ID
37 * @param[in] dst_prefix Destination IP prefix.
38 *
39 * @return src_fib_index The index/ID of the SRC FIB created.
40 */
41static u32
42ip_dst_fib_add_route (u32 dst_fib_index, const ip_prefix_t * dst_prefix)
43{
44 fib_node_index_t src_fib_index;
45 fib_prefix_t dst_fib_prefix;
46 fib_node_index_t dst_fei;
47
48 ASSERT (NULL != dst_prefix);
49
50 ip_prefix_to_fib_prefix (dst_prefix, &dst_fib_prefix);
51
52 /*
53 * lookup the destination prefix in the VRF table and retrieve the
54 * LISP associated data
55 */
56 dst_fei = fib_table_lookup_exact_match (dst_fib_index, &dst_fib_prefix);
57
58 /*
59 * If the FIB entry is not present, or not LISP sourced, add it
60 */
61 if (dst_fei == FIB_NODE_INDEX_INVALID ||
62 NULL == fib_entry_get_source_data (dst_fei, FIB_SOURCE_LISP))
63 {
Neale Ranns948e00f2016-10-20 13:39:34 +010064 dpo_id_t src_lkup_dpo = DPO_INVALID;
Neale Ranns5e575b12016-10-03 09:40:25 +010065
66 /* create a new src FIB. */
67 src_fib_index =
68 fib_table_create_and_lock (dst_fib_prefix.fp_proto,
Neale Ranns15002542017-09-10 04:39:11 -070069 FIB_SOURCE_LISP,
Neale Ranns5e575b12016-10-03 09:40:25 +010070 "LISP-src for [%d,%U]",
71 dst_fib_index,
72 format_fib_prefix, &dst_fib_prefix);
Filip Tehlard5fcc462016-10-17 16:20:18 +020073 /*
74 * add src fib default route
75 */
76 fib_prefix_t prefix = {
77 .fp_proto = dst_fib_prefix.fp_proto,
78 };
79 fib_table_entry_special_dpo_add (src_fib_index, &prefix,
80 FIB_SOURCE_LISP,
81 FIB_ENTRY_FLAG_EXCLUSIVE,
82 lisp_cp_dpo_get (fib_proto_to_dpo
83 (dst_fib_prefix.fp_proto)));
Neale Ranns5e575b12016-10-03 09:40:25 +010084 /*
85 * create a data-path object to perform the source address lookup
86 * in the SRC FIB
87 */
88 lookup_dpo_add_or_lock_w_fib_index (src_fib_index,
89 (ip_prefix_version (dst_prefix) ==
90 IP6 ? DPO_PROTO_IP6 :
91 DPO_PROTO_IP4),
Neale Ranns0f26c5a2017-03-01 15:12:11 -080092 LOOKUP_UNICAST,
Neale Ranns5e575b12016-10-03 09:40:25 +010093 LOOKUP_INPUT_SRC_ADDR,
94 LOOKUP_TABLE_FROM_CONFIG,
95 &src_lkup_dpo);
96
97 /*
98 * add the entry to the destination FIB that uses the lookup DPO
99 */
100 dst_fei = fib_table_entry_special_dpo_add (dst_fib_index,
101 &dst_fib_prefix,
102 FIB_SOURCE_LISP,
103 FIB_ENTRY_FLAG_EXCLUSIVE,
104 &src_lkup_dpo);
105
106 /*
107 * the DPO is locked by the FIB entry, and we have no further
108 * need for it.
109 */
110 dpo_unlock (&src_lkup_dpo);
111
112 /*
113 * save the SRC FIB index on the entry so we can retrieve it for
114 * subsequent routes.
115 */
116 fib_entry_set_source_data (dst_fei, FIB_SOURCE_LISP, &src_fib_index);
117 }
118 else
119 {
120 /*
121 * destination FIB entry already present
122 */
123 src_fib_index = *(u32 *) fib_entry_get_source_data (dst_fei,
124 FIB_SOURCE_LISP);
125 }
126
127 return (src_fib_index);
128}
129
130/**
131 * @brief Del route to IP4 or IP6 SD FIB.
132 *
133 * Remove routes from both destination and source FIBs.
134 *
135 * @param[in] src_fib_index The index/ID of the SRC FIB
136 * @param[in] src_prefix Source IP prefix.
137 * @param[in] dst_fib_index The index/ID of the DST FIB
138 * @param[in] dst_prefix Destination IP prefix.
139 */
140static void
141ip_src_dst_fib_del_route (u32 src_fib_index,
142 const ip_prefix_t * src_prefix,
143 u32 dst_fib_index, const ip_prefix_t * dst_prefix)
144{
145 fib_prefix_t dst_fib_prefix, src_fib_prefix;
Florin Coras84794402016-11-29 17:14:06 -0800146 u8 have_default = 0;
147 u32 n_entries;
Neale Ranns5e575b12016-10-03 09:40:25 +0100148
149 ASSERT (NULL != dst_prefix);
150 ASSERT (NULL != src_prefix);
151
152 ip_prefix_to_fib_prefix (dst_prefix, &dst_fib_prefix);
153 ip_prefix_to_fib_prefix (src_prefix, &src_fib_prefix);
154
155 fib_table_entry_delete (src_fib_index, &src_fib_prefix, FIB_SOURCE_LISP);
156
Florin Coras84794402016-11-29 17:14:06 -0800157 /* check if only default left or empty */
158 fib_prefix_t default_pref = {
159 .fp_proto = dst_fib_prefix.fp_proto
160 };
161
162 if (fib_table_lookup_exact_match (src_fib_index,
163 &default_pref) != FIB_NODE_INDEX_INVALID)
164 have_default = 1;
165
166 n_entries = fib_table_get_num_entries (src_fib_index,
167 src_fib_prefix.fp_proto,
168 FIB_SOURCE_LISP);
169 if (n_entries == 0 || (have_default && n_entries == 1))
Neale Ranns5e575b12016-10-03 09:40:25 +0100170 {
171 /*
Florin Corasfeeebfe2016-11-27 22:33:01 -0800172 * remove src FIB default route
173 */
Florin Coras84794402016-11-29 17:14:06 -0800174 if (have_default)
175 fib_table_entry_special_remove (src_fib_index, &default_pref,
176 FIB_SOURCE_LISP);
Florin Corasfeeebfe2016-11-27 22:33:01 -0800177
178 /*
179 * there's nothing left now, unlock the source FIB and the
Neale Ranns5e575b12016-10-03 09:40:25 +0100180 * destination route
181 */
182 fib_table_entry_special_remove (dst_fib_index,
183 &dst_fib_prefix, FIB_SOURCE_LISP);
Neale Ranns15002542017-09-10 04:39:11 -0700184 fib_table_unlock (src_fib_index, src_fib_prefix.fp_proto,
185 FIB_SOURCE_LISP);
Neale Ranns5e575b12016-10-03 09:40:25 +0100186 }
187}
188
189/**
190 * @brief Add route to IP4 or IP6 SRC FIB.
191 *
192 * Adds a route to in the LISP SRC FIB with the result of the route
193 * being the DPO passed.
194 *
195 * @param[in] src_fib_index The index/ID of the SRC FIB
196 * @param[in] src_prefix Source IP prefix.
197 * @param[in] src_dpo The DPO the route will link to.
Filip Tehlar809bc742017-08-14 19:15:36 +0200198 *
199 * @return fib index of the inserted prefix
Neale Ranns5e575b12016-10-03 09:40:25 +0100200 */
Filip Tehlar809bc742017-08-14 19:15:36 +0200201static fib_node_index_t
Neale Ranns5e575b12016-10-03 09:40:25 +0100202ip_src_fib_add_route_w_dpo (u32 src_fib_index,
203 const ip_prefix_t * src_prefix,
204 const dpo_id_t * src_dpo)
205{
Filip Tehlar809bc742017-08-14 19:15:36 +0200206 fib_node_index_t fei = ~0;
Neale Ranns5e575b12016-10-03 09:40:25 +0100207 fib_prefix_t src_fib_prefix;
208
209 ip_prefix_to_fib_prefix (src_prefix, &src_fib_prefix);
210
211 /*
212 * add the entry into the source fib.
213 */
214 fib_node_index_t src_fei;
215
216 src_fei = fib_table_lookup_exact_match (src_fib_index, &src_fib_prefix);
217
218 if (FIB_NODE_INDEX_INVALID == src_fei ||
219 !fib_entry_is_sourced (src_fei, FIB_SOURCE_LISP))
220 {
Filip Tehlar809bc742017-08-14 19:15:36 +0200221 fei = fib_table_entry_special_dpo_add (src_fib_index,
222 &src_fib_prefix,
223 FIB_SOURCE_LISP,
224 FIB_ENTRY_FLAG_EXCLUSIVE,
225 src_dpo);
Neale Ranns5e575b12016-10-03 09:40:25 +0100226 }
Filip Tehlar809bc742017-08-14 19:15:36 +0200227 return fei;
Neale Ranns5e575b12016-10-03 09:40:25 +0100228}
229
Neale Ranns5e575b12016-10-03 09:40:25 +0100230static fib_route_path_t *
231lisp_gpe_mk_fib_paths (const lisp_fwd_path_t * paths)
232{
233 const lisp_gpe_adjacency_t *ladj;
234 fib_route_path_t *rpaths = NULL;
Neale Rannsda78f952017-05-24 09:15:43 -0700235 fib_protocol_t fp;
Neale Ranns5e575b12016-10-03 09:40:25 +0100236 u8 best_priority;
237 u32 ii;
238
239 vec_validate (rpaths, vec_len (paths) - 1);
240
241 best_priority = paths[0].priority;
242
243 vec_foreach_index (ii, paths)
244 {
245 if (paths[0].priority != best_priority)
246 break;
247
248 ladj = lisp_gpe_adjacency_get (paths[ii].lisp_adj);
249
Neale Rannsda78f952017-05-24 09:15:43 -0700250 ip_address_to_46 (&ladj->remote_rloc, &rpaths[ii].frp_addr, &fp);
Neale Ranns5e575b12016-10-03 09:40:25 +0100251
Neale Rannsda78f952017-05-24 09:15:43 -0700252 rpaths[ii].frp_proto = fib_proto_to_dpo (fp);
Neale Ranns5e575b12016-10-03 09:40:25 +0100253 rpaths[ii].frp_sw_if_index = ladj->sw_if_index;
254 rpaths[ii].frp_weight = (paths[ii].weight ? paths[ii].weight : 1);
Neale Ranns5e575b12016-10-03 09:40:25 +0100255 }
256
257 ASSERT (0 != vec_len (rpaths));
258
259 return (rpaths);
260}
261
262/**
263 * @brief Add route to IP4 or IP6 SRC FIB.
264 *
265 * Adds a route to in the LISP SRC FIB for the tunnel.
266 *
267 * @param[in] src_fib_index The index/ID of the SRC FIB
268 * @param[in] src_prefix Source IP prefix.
269 * @param[in] paths The paths from which to construct the
270 * load balance
271 */
Filip Tehlar809bc742017-08-14 19:15:36 +0200272static fib_node_index_t
Neale Ranns5e575b12016-10-03 09:40:25 +0100273ip_src_fib_add_route (u32 src_fib_index,
274 const ip_prefix_t * src_prefix,
275 const lisp_fwd_path_t * paths)
276{
277 fib_prefix_t src_fib_prefix;
278 fib_route_path_t *rpaths;
279
280 ip_prefix_to_fib_prefix (src_prefix, &src_fib_prefix);
281
282 rpaths = lisp_gpe_mk_fib_paths (paths);
283
Filip Tehlar809bc742017-08-14 19:15:36 +0200284 fib_node_index_t fib_entry_index =
285 fib_table_entry_update (src_fib_index, &src_fib_prefix, FIB_SOURCE_LISP,
286 FIB_ENTRY_FLAG_NONE, rpaths);
Neale Ranns5e575b12016-10-03 09:40:25 +0100287 vec_free (rpaths);
Filip Tehlar809bc742017-08-14 19:15:36 +0200288 return fib_entry_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100289}
290
Florin Corasa4e63e52017-06-07 21:50:57 -0700291static void
292gpe_native_fwd_add_del_lfe (lisp_gpe_fwd_entry_t * lfe, u8 is_add)
293{
294 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
295 u8 found = 0, ip_version;
296 u32 *lfei, new_lfei;
297 ip_version = ip_prefix_version (&lfe->key->rmt.ippref);
298
299 new_lfei = lfe - lgm->lisp_fwd_entry_pool;
300 vec_foreach (lfei, lgm->native_fwd_lfes[ip_version])
301 {
302 lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, lfei[0]);
303 if (lfei[0] == new_lfei)
304 {
305 found = 1;
306 break;
307 }
308 }
309
310 if (is_add)
311 {
312 if (!found)
313 vec_add1 (lgm->native_fwd_lfes[ip_version], new_lfei);
314 }
315 else
316 {
317 if (found)
318 vec_del1 (lgm->native_fwd_lfes[ip_version], lfei[0]);
319 }
320}
Neale Ranns5e575b12016-10-03 09:40:25 +0100321
Filip Tehlar809bc742017-08-14 19:15:36 +0200322static index_t
Neale Ranns5e575b12016-10-03 09:40:25 +0100323create_fib_entries (lisp_gpe_fwd_entry_t * lfe)
324{
Filip Tehlar809bc742017-08-14 19:15:36 +0200325 fib_node_index_t fi;
326 fib_entry_t *fe;
Florin Corasa4e63e52017-06-07 21:50:57 -0700327 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
Neale Ranns5e575b12016-10-03 09:40:25 +0100328 dpo_proto_t dproto;
Filip Tehlar25ad0ea2017-04-04 15:26:54 +0200329 ip_prefix_t ippref;
Florin Corasa4e63e52017-06-07 21:50:57 -0700330 fib_prefix_t fib_prefix;
331 u8 ip_version = ip_prefix_version (&lfe->key->rmt.ippref);
332 dproto = (ip_version == IP4 ? DPO_PROTO_IP4 : DPO_PROTO_IP6);
Neale Ranns5e575b12016-10-03 09:40:25 +0100333
Filip Tehlar25ad0ea2017-04-04 15:26:54 +0200334 if (lfe->is_src_dst)
335 {
336 lfe->src_fib_index = ip_dst_fib_add_route (lfe->eid_fib_index,
337 &lfe->key->rmt.ippref);
338 memcpy (&ippref, &lfe->key->lcl.ippref, sizeof (ippref));
339 }
340 else
341 {
342 lfe->src_fib_index = lfe->eid_fib_index;
343 memcpy (&ippref, &lfe->key->rmt.ippref, sizeof (ippref));
344 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100345
346 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
347 {
Neale Ranns948e00f2016-10-20 13:39:34 +0100348 dpo_id_t dpo = DPO_INVALID;
Neale Ranns5e575b12016-10-03 09:40:25 +0100349
350 switch (lfe->action)
351 {
Florin Corasa4e63e52017-06-07 21:50:57 -0700352 case LISP_FORWARD_NATIVE:
353 /* TODO handle route overlaps with fib and default route */
354 if (vec_len (lgm->native_fwd_rpath[ip_version]))
355 {
356 ip_prefix_to_fib_prefix (&lfe->key->rmt.ippref, &fib_prefix);
Florin Corasa4393be2017-09-12 20:24:00 -0400357 fi = fib_table_entry_update (lfe->eid_fib_index, &fib_prefix,
358 FIB_SOURCE_LISP,
359 FIB_ENTRY_FLAG_NONE,
360 lgm->native_fwd_rpath[ip_version]);
Florin Corasa4e63e52017-06-07 21:50:57 -0700361 gpe_native_fwd_add_del_lfe (lfe, 1);
Florin Corasa4393be2017-09-12 20:24:00 -0400362 goto done;
Florin Corasa4e63e52017-06-07 21:50:57 -0700363 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100364 case LISP_NO_ACTION:
365 /* TODO update timers? */
Neale Ranns5e575b12016-10-03 09:40:25 +0100366 case LISP_SEND_MAP_REQUEST:
367 /* insert tunnel that always sends map-request */
368 dpo_copy (&dpo, lisp_cp_dpo_get (dproto));
369 break;
370 case LISP_DROP:
371 /* for drop fwd entries, just add route, no need to add encap tunnel */
372 dpo_copy (&dpo, drop_dpo_get (dproto));
373 break;
374 }
Filip Tehlar809bc742017-08-14 19:15:36 +0200375 fi = ip_src_fib_add_route_w_dpo (lfe->src_fib_index, &ippref, &dpo);
Neale Ranns5e575b12016-10-03 09:40:25 +0100376 dpo_reset (&dpo);
377 }
378 else
379 {
Filip Tehlar809bc742017-08-14 19:15:36 +0200380 fi = ip_src_fib_add_route (lfe->src_fib_index, &ippref, lfe->paths);
Neale Ranns5e575b12016-10-03 09:40:25 +0100381 }
Florin Corasa4393be2017-09-12 20:24:00 -0400382done:
Filip Tehlar809bc742017-08-14 19:15:36 +0200383 fe = fib_entry_get (fi);
384 return fe->fe_lb.dpoi_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100385}
386
387static void
388delete_fib_entries (lisp_gpe_fwd_entry_t * lfe)
389{
Filip Tehlared6b52b2017-03-22 09:02:33 +0100390 fib_prefix_t dst_fib_prefix;
391
392 if (lfe->is_src_dst)
393 ip_src_dst_fib_del_route (lfe->src_fib_index,
394 &lfe->key->lcl.ippref,
395 lfe->eid_fib_index, &lfe->key->rmt.ippref);
396 else
397 {
398 ip_prefix_to_fib_prefix (&lfe->key->rmt.ippref, &dst_fib_prefix);
399 fib_table_entry_delete (lfe->src_fib_index, &dst_fib_prefix,
400 FIB_SOURCE_LISP);
Florin Corasa4e63e52017-06-07 21:50:57 -0700401 gpe_native_fwd_add_del_lfe (lfe, 0);
Filip Tehlared6b52b2017-03-22 09:02:33 +0100402 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100403}
404
Neale Ranns5e575b12016-10-03 09:40:25 +0100405static lisp_gpe_fwd_entry_t *
406find_fwd_entry (lisp_gpe_main_t * lgm,
407 vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
408 lisp_gpe_fwd_entry_key_t * key)
409{
410 uword *p;
411
412 memset (key, 0, sizeof (*key));
413
414 if (GID_ADDR_IP_PREFIX == gid_address_type (&a->rmt_eid))
415 {
416 /*
417 * the ip version of the source is not set to ip6 when the
418 * source is all zeros. force it.
419 */
420 ip_prefix_version (&gid_address_ippref (&a->lcl_eid)) =
421 ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
422 }
423
424 gid_to_dp_address (&a->rmt_eid, &key->rmt);
425 gid_to_dp_address (&a->lcl_eid, &key->lcl);
426 key->vni = a->vni;
427
428 p = hash_get_mem (lgm->lisp_gpe_fwd_entries, key);
429
430 if (NULL != p)
431 {
432 return (pool_elt_at_index (lgm->lisp_fwd_entry_pool, p[0]));
433 }
434 return (NULL);
435}
436
437static int
438lisp_gpe_fwd_entry_path_sort (void *a1, void *a2)
439{
440 lisp_fwd_path_t *p1 = a1, *p2 = a2;
441
442 return (p1->priority - p2->priority);
443}
444
445static void
446lisp_gpe_fwd_entry_mk_paths (lisp_gpe_fwd_entry_t * lfe,
447 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
448{
Neale Ranns5e575b12016-10-03 09:40:25 +0100449 lisp_fwd_path_t *path;
450 u32 index;
451
Neale Ranns5e575b12016-10-03 09:40:25 +0100452 vec_validate (lfe->paths, vec_len (a->locator_pairs) - 1);
453
454 vec_foreach_index (index, a->locator_pairs)
455 {
456 path = &lfe->paths[index];
457
458 path->priority = a->locator_pairs[index].priority;
459 path->weight = a->locator_pairs[index].weight;
460
461 path->lisp_adj =
462 lisp_gpe_adjacency_find_or_create_and_lock (&a->locator_pairs
463 [index],
Filip Tehlard5fcc462016-10-17 16:20:18 +0200464 a->dp_table, lfe->key->vni);
Neale Ranns5e575b12016-10-03 09:40:25 +0100465 }
466 vec_sort_with_function (lfe->paths, lisp_gpe_fwd_entry_path_sort);
467}
468
Filip Tehlar21511912017-04-07 10:41:42 +0200469void
470vnet_lisp_gpe_add_fwd_counters (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
471 u32 fwd_entry_index)
472{
473 const lisp_gpe_adjacency_t *ladj;
474 lisp_fwd_path_t *path;
475 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
476 u8 *dummy_elt;
477 lisp_gpe_fwd_entry_t *lfe;
478 lisp_gpe_fwd_entry_key_t fe_key;
479 lisp_stats_key_t key;
480
481 lfe = find_fwd_entry (lgm, a, &fe_key);
482
Filip Tehlar761787b2017-06-06 15:32:52 +0200483 if (!lfe)
484 return;
485
Filip Tehlar21511912017-04-07 10:41:42 +0200486 if (LISP_GPE_FWD_ENTRY_TYPE_NORMAL != lfe->type)
487 return;
488
489 memset (&key, 0, sizeof (key));
490 key.fwd_entry_index = fwd_entry_index;
491
492 vec_foreach (path, lfe->paths)
493 {
494 ladj = lisp_gpe_adjacency_get (path->lisp_adj);
495 key.tunnel_index = ladj->tunnel_index;
496 lisp_stats_key_t *key_copy = clib_mem_alloc (sizeof (*key_copy));
497 memcpy (key_copy, &key, sizeof (*key_copy));
498 pool_get (lgm->dummy_stats_pool, dummy_elt);
499 hash_set_mem (lgm->lisp_stats_index_by_key, key_copy,
500 dummy_elt - lgm->dummy_stats_pool);
501
502 vlib_validate_combined_counter (&lgm->counters,
503 dummy_elt - lgm->dummy_stats_pool);
504 vlib_zero_combined_counter (&lgm->counters,
505 dummy_elt - lgm->dummy_stats_pool);
506 }
507}
508
Neale Ranns5e575b12016-10-03 09:40:25 +0100509/**
510 * @brief Add/Delete LISP IP forwarding entry.
511 *
512 * creation of forwarding entries for IP LISP overlay:
513 *
514 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
515 * @param[in] a Parameters for building the forwarding entry.
516 *
517 * @return 0 on success.
518 */
519static int
520add_ip_fwd_entry (lisp_gpe_main_t * lgm,
521 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
522{
523 lisp_gpe_fwd_entry_key_t key;
524 lisp_gpe_fwd_entry_t *lfe;
525 fib_protocol_t fproto;
526
527 lfe = find_fwd_entry (lgm, a, &key);
528
529 if (NULL != lfe)
530 /* don't support updates */
531 return VNET_API_ERROR_INVALID_VALUE;
532
533 pool_get (lgm->lisp_fwd_entry_pool, lfe);
534 memset (lfe, 0, sizeof (*lfe));
535 lfe->key = clib_mem_alloc (sizeof (key));
536 memcpy (lfe->key, &key, sizeof (key));
537
538 hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
539 lfe - lgm->lisp_fwd_entry_pool);
Filip Tehlar560274d2017-06-05 13:40:13 +0200540 a->fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
Neale Ranns5e575b12016-10-03 09:40:25 +0100541
542 fproto = (IP4 == ip_prefix_version (&fid_addr_ippref (&lfe->key->rmt)) ?
543 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
544
545 lfe->type = (a->is_negative ?
546 LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
547 LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
548 lfe->tenant = lisp_gpe_tenant_find_or_create (lfe->key->vni);
549 lfe->eid_table_id = a->table_id;
550 lfe->eid_fib_index = fib_table_find_or_create_and_lock (fproto,
Neale Ranns15002542017-09-10 04:39:11 -0700551 lfe->eid_table_id,
552 FIB_SOURCE_LISP);
Filip Tehlared6b52b2017-03-22 09:02:33 +0100553 lfe->is_src_dst = a->is_src_dst;
Neale Ranns5e575b12016-10-03 09:40:25 +0100554
555 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
556 {
557 lisp_gpe_fwd_entry_mk_paths (lfe, a);
558 }
Filip Tehlar0eb874e2017-05-18 14:23:32 +0200559 else
560 {
561 lfe->action = a->action;
562 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100563
Filip Tehlar809bc742017-08-14 19:15:36 +0200564 lfe->dpoi_index = create_fib_entries (lfe);
Neale Ranns5e575b12016-10-03 09:40:25 +0100565 return (0);
566}
567
568static void
569del_ip_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
570{
571 lisp_fwd_path_t *path;
572 fib_protocol_t fproto;
573
Filip Tehlar612a3832017-05-26 12:10:53 +0200574 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
575 {
576 vec_foreach (path, lfe->paths)
577 {
578 lisp_gpe_adjacency_unlock (path->lisp_adj);
579 }
580 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100581
582 delete_fib_entries (lfe);
583
584 fproto = (IP4 == ip_prefix_version (&fid_addr_ippref (&lfe->key->rmt)) ?
585 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
Neale Ranns15002542017-09-10 04:39:11 -0700586 fib_table_unlock (lfe->eid_fib_index, fproto, FIB_SOURCE_LISP);
Neale Ranns5e575b12016-10-03 09:40:25 +0100587
588 hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
589 clib_mem_free (lfe->key);
590 pool_put (lgm->lisp_fwd_entry_pool, lfe);
591}
592
593/**
594 * @brief Add/Delete LISP IP forwarding entry.
595 *
596 * removal of forwarding entries for IP LISP overlay:
597 *
598 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
599 * @param[in] a Parameters for building the forwarding entry.
600 *
601 * @return 0 on success.
602 */
603static int
604del_ip_fwd_entry (lisp_gpe_main_t * lgm,
605 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
606{
607 lisp_gpe_fwd_entry_key_t key;
608 lisp_gpe_fwd_entry_t *lfe;
609
610 lfe = find_fwd_entry (lgm, a, &key);
611
612 if (NULL == lfe)
613 /* no such entry */
614 return VNET_API_ERROR_INVALID_VALUE;
615
616 del_ip_fwd_entry_i (lgm, lfe);
617
618 return (0);
619}
620
621static void
622make_mac_fib_key (BVT (clib_bihash_kv) * kv, u16 bd_index, u8 src_mac[6],
623 u8 dst_mac[6])
624{
625 kv->key[0] = (((u64) bd_index) << 48) | mac_to_u64 (dst_mac);
626 kv->key[1] = mac_to_u64 (src_mac);
627 kv->key[2] = 0;
628}
629
630/**
631 * @brief Lookup L2 SD FIB entry
632 *
633 * Does a vni + dest + source lookup in the L2 LISP FIB. If the lookup fails
634 * it tries a second time with source set to 0 (i.e., a simple dest lookup).
635 *
636 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
637 * @param[in] bd_index Bridge domain index.
638 * @param[in] src_mac Source mac address.
639 * @param[in] dst_mac Destination mac address.
640 *
641 * @return index of mapping matching the lookup key.
642 */
643index_t
644lisp_l2_fib_lookup (lisp_gpe_main_t * lgm, u16 bd_index, u8 src_mac[6],
645 u8 dst_mac[6])
646{
647 int rv;
648 BVT (clib_bihash_kv) kv, value;
649
650 make_mac_fib_key (&kv, bd_index, src_mac, dst_mac);
651 rv = BV (clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
652
653 /* no match, try with src 0, catch all for dst */
654 if (rv != 0)
655 {
656 kv.key[1] = 0;
657 rv = BV (clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
658 if (rv == 0)
659 return value.value;
660 }
Filip Tehlard5fcc462016-10-17 16:20:18 +0200661 else
662 return value.value;
Neale Ranns5e575b12016-10-03 09:40:25 +0100663
664 return lisp_gpe_main.l2_lb_cp_lkup.dpoi_index;
665}
666
667/**
668 * @brief Add/del L2 SD FIB entry
669 *
670 * Inserts value in L2 FIB keyed by vni + dest + source. If entry is
671 * overwritten the associated value is returned.
672 *
673 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
674 * @param[in] bd_index Bridge domain index.
675 * @param[in] src_mac Source mac address.
676 * @param[in] dst_mac Destination mac address.
677 * @param[in] val Value to add.
678 * @param[in] is_add Add/del flag.
679 *
680 * @return ~0 or value of overwritten entry.
681 */
682static u32
683lisp_l2_fib_add_del_entry (u16 bd_index, u8 src_mac[6],
684 u8 dst_mac[6], const dpo_id_t * dpo, u8 is_add)
685{
686 lisp_gpe_main_t *lgm = &lisp_gpe_main;
687 BVT (clib_bihash_kv) kv, value;
688 u32 old_val = ~0;
689
690 make_mac_fib_key (&kv, bd_index, src_mac, dst_mac);
691
692 if (BV (clib_bihash_search) (&lgm->l2_fib, &kv, &value) == 0)
693 old_val = value.value;
694
695 if (!is_add)
696 BV (clib_bihash_add_del) (&lgm->l2_fib, &kv, 0 /* is_add */ );
697 else
698 {
699 kv.value = dpo->dpoi_index;
700 BV (clib_bihash_add_del) (&lgm->l2_fib, &kv, 1 /* is_add */ );
701 }
702 return old_val;
703}
704
705#define L2_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
706#define L2_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
707
708static void
709l2_fib_init (lisp_gpe_main_t * lgm)
710{
711 index_t lbi;
712
713 BV (clib_bihash_init) (&lgm->l2_fib, "l2 fib",
714 1 << max_log2 (L2_FIB_DEFAULT_HASH_NUM_BUCKETS),
715 L2_FIB_DEFAULT_HASH_MEMORY_SIZE);
716
717 /*
718 * the result from a 'miss' in a L2 Table
719 */
720 lbi = load_balance_create (1, DPO_PROTO_ETHERNET, 0);
721 load_balance_set_bucket (lbi, 0, lisp_cp_dpo_get (DPO_PROTO_ETHERNET));
722
723 dpo_set (&lgm->l2_lb_cp_lkup, DPO_LOAD_BALANCE, DPO_PROTO_ETHERNET, lbi);
724}
725
726static void
727del_l2_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
728{
729 lisp_fwd_path_t *path;
730
Florin Coras2ccf7682016-10-04 18:03:49 +0300731 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
732 {
733 vec_foreach (path, lfe->paths)
734 {
735 lisp_gpe_adjacency_unlock (path->lisp_adj);
736 }
737 fib_path_list_child_remove (lfe->l2.path_list_index,
738 lfe->l2.child_index);
739 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100740
741 lisp_l2_fib_add_del_entry (lfe->l2.eid_bd_index,
742 fid_addr_mac (&lfe->key->lcl),
743 fid_addr_mac (&lfe->key->rmt), NULL, 0);
744
Neale Ranns5e575b12016-10-03 09:40:25 +0100745 hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
746 clib_mem_free (lfe->key);
747 pool_put (lgm->lisp_fwd_entry_pool, lfe);
748}
749
750/**
751 * @brief Delete LISP L2 forwarding entry.
752 *
753 * Coordinates the removal of forwarding entries for L2 LISP overlay:
754 *
755 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
756 * @param[in] a Parameters for building the forwarding entry.
757 *
758 * @return 0 on success.
759 */
760static int
761del_l2_fwd_entry (lisp_gpe_main_t * lgm,
762 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
763{
764 lisp_gpe_fwd_entry_key_t key;
765 lisp_gpe_fwd_entry_t *lfe;
766
767 lfe = find_fwd_entry (lgm, a, &key);
768
Florin Coras2ccf7682016-10-04 18:03:49 +0300769 if (NULL == lfe)
Neale Ranns5e575b12016-10-03 09:40:25 +0100770 return VNET_API_ERROR_INVALID_VALUE;
771
772 del_l2_fwd_entry_i (lgm, lfe);
773
774 return (0);
775}
776
777/**
Florin Corasce1b4c72017-01-26 14:25:34 -0800778 * @brief Construct and insert the forwarding information used by an L2 entry
Neale Ranns5e575b12016-10-03 09:40:25 +0100779 */
780static void
781lisp_gpe_l2_update_fwding (lisp_gpe_fwd_entry_t * lfe)
782{
783 lisp_gpe_main_t *lgm = &lisp_gpe_main;
Neale Ranns948e00f2016-10-20 13:39:34 +0100784 dpo_id_t dpo = DPO_INVALID;
Neale Ranns5e575b12016-10-03 09:40:25 +0100785
786 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
787 {
788 fib_path_list_contribute_forwarding (lfe->l2.path_list_index,
789 FIB_FORW_CHAIN_TYPE_ETHERNET,
Neale Ranns91286372017-12-05 13:24:04 -0800790 FIB_PATH_LIST_FWD_FLAG_NONE,
Neale Ranns5e575b12016-10-03 09:40:25 +0100791 &lfe->l2.dpo);
792 dpo_copy (&dpo, &lfe->l2.dpo);
793 }
794 else
795 {
Florin Corasce1b4c72017-01-26 14:25:34 -0800796 switch (lfe->action)
797 {
798 case SEND_MAP_REQUEST:
799 dpo_copy (&dpo, &lgm->l2_lb_cp_lkup);
800 break;
801 case NO_ACTION:
802 case FORWARD_NATIVE:
803 case DROP:
804 dpo_copy (&dpo, drop_dpo_get (DPO_PROTO_ETHERNET));
805 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100806 }
807
808 /* add entry to l2 lisp fib */
809 lisp_l2_fib_add_del_entry (lfe->l2.eid_bd_index,
810 fid_addr_mac (&lfe->key->lcl),
811 fid_addr_mac (&lfe->key->rmt), &dpo, 1);
Filip Tehlar809bc742017-08-14 19:15:36 +0200812 lfe->dpoi_index = dpo.dpoi_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100813
814 dpo_reset (&dpo);
815}
816
817/**
818 * @brief Add LISP L2 forwarding entry.
819 *
820 * Coordinates the creation of forwarding entries for L2 LISP overlay:
821 * creates lisp-gpe tunnel and injects new entry in Source/Dest L2 FIB.
822 *
823 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
824 * @param[in] a Parameters for building the forwarding entry.
825 *
826 * @return 0 on success.
827 */
828static int
829add_l2_fwd_entry (lisp_gpe_main_t * lgm,
830 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
831{
832 lisp_gpe_fwd_entry_key_t key;
833 bd_main_t *bdm = &bd_main;
834 lisp_gpe_fwd_entry_t *lfe;
835 uword *bd_indexp;
836
837 bd_indexp = hash_get (bdm->bd_index_by_bd_id, a->bd_id);
838 if (!bd_indexp)
839 {
840 clib_warning ("bridge domain %d doesn't exist", a->bd_id);
841 return -1;
842 }
843
844 lfe = find_fwd_entry (lgm, a, &key);
845
846 if (NULL != lfe)
847 /* don't support updates */
848 return VNET_API_ERROR_INVALID_VALUE;
849
850 pool_get (lgm->lisp_fwd_entry_pool, lfe);
851 memset (lfe, 0, sizeof (*lfe));
852 lfe->key = clib_mem_alloc (sizeof (key));
853 memcpy (lfe->key, &key, sizeof (key));
854
855 hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
856 lfe - lgm->lisp_fwd_entry_pool);
Filip Tehlar560274d2017-06-05 13:40:13 +0200857 a->fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
Neale Ranns5e575b12016-10-03 09:40:25 +0100858
859 lfe->type = (a->is_negative ?
860 LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
861 LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
862 lfe->l2.eid_bd_id = a->bd_id;
863 lfe->l2.eid_bd_index = bd_indexp[0];
864 lfe->tenant = lisp_gpe_tenant_find_or_create (lfe->key->vni);
865
866 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
867 {
868 fib_route_path_t *rpaths;
869
870 /*
871 * Make the sorted array of LISP paths with their resp. adjacency
872 */
873 lisp_gpe_fwd_entry_mk_paths (lfe, a);
874
875 /*
876 * From the LISP paths, construct a FIB path list that will
877 * contribute a load-balance.
878 */
879 rpaths = lisp_gpe_mk_fib_paths (lfe->paths);
880
881 lfe->l2.path_list_index =
882 fib_path_list_create (FIB_PATH_LIST_FLAG_NONE, rpaths);
883
884 /*
885 * become a child of the path-list so we receive updates when
886 * its forwarding state changes. this includes an implicit lock.
887 */
888 lfe->l2.child_index =
889 fib_path_list_child_add (lfe->l2.path_list_index,
890 FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY,
891 lfe - lgm->lisp_fwd_entry_pool);
892 }
893 else
894 {
895 lfe->action = a->action;
896 }
897
898 lisp_gpe_l2_update_fwding (lfe);
899
900 return 0;
901}
902
903/**
Florin Corasce1b4c72017-01-26 14:25:34 -0800904 * @brief Lookup NSH SD FIB entry
905 *
906 * Does an SPI+SI lookup in the NSH LISP FIB.
907 *
908 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
909 * @param[in] spi_si SPI + SI.
910 *
911 * @return next node index.
912 */
913const dpo_id_t *
Florin Coras263440e2017-02-22 23:38:08 -0800914lisp_nsh_fib_lookup (lisp_gpe_main_t * lgm, u32 spi_si_net_order)
Florin Corasce1b4c72017-01-26 14:25:34 -0800915{
916 int rv;
917 BVT (clib_bihash_kv) kv, value;
918
919 memset (&kv, 0, sizeof (kv));
Florin Coras263440e2017-02-22 23:38:08 -0800920 kv.key[0] = spi_si_net_order;
Florin Corasce1b4c72017-01-26 14:25:34 -0800921 rv = BV (clib_bihash_search_inline_2) (&lgm->nsh_fib, &kv, &value);
922
923 if (rv != 0)
924 {
925 return lgm->nsh_cp_lkup;
926 }
927 else
928 {
929 lisp_gpe_fwd_entry_t *lfe;
930 lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, value.value);
931 return &lfe->nsh.choice;
932 }
933}
934
935/**
936 * @brief Add/del NSH FIB entry
937 *
938 * Inserts value in NSH FIB keyed by SPI+SI. If entry is
939 * overwritten the associated value is returned.
940 *
941 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
942 * @param[in] spi_si SPI + SI.
943 * @param[in] dpo Load balanced mapped to SPI + SI
944 *
945 * @return ~0 or value of overwritten entry.
946 */
947static u32
Florin Coras263440e2017-02-22 23:38:08 -0800948lisp_nsh_fib_add_del_entry (u32 spi_si_host_order, u32 lfei, u8 is_add)
Florin Corasce1b4c72017-01-26 14:25:34 -0800949{
950 lisp_gpe_main_t *lgm = &lisp_gpe_main;
951 BVT (clib_bihash_kv) kv, value;
952 u32 old_val = ~0;
953
954 memset (&kv, 0, sizeof (kv));
Florin Coras263440e2017-02-22 23:38:08 -0800955 kv.key[0] = clib_host_to_net_u32 (spi_si_host_order);
Florin Corasce1b4c72017-01-26 14:25:34 -0800956 kv.value = 0ULL;
957
958 if (BV (clib_bihash_search) (&lgm->nsh_fib, &kv, &value) == 0)
959 old_val = value.value;
960
961 if (!is_add)
962 BV (clib_bihash_add_del) (&lgm->nsh_fib, &kv, 0 /* is_add */ );
963 else
964 {
965 kv.value = lfei;
966 BV (clib_bihash_add_del) (&lgm->nsh_fib, &kv, 1 /* is_add */ );
967 }
968 return old_val;
969}
970
971#define NSH_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
972#define NSH_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
973
974static void
975nsh_fib_init (lisp_gpe_main_t * lgm)
976{
977 BV (clib_bihash_init) (&lgm->nsh_fib, "nsh fib",
978 1 << max_log2 (NSH_FIB_DEFAULT_HASH_NUM_BUCKETS),
979 NSH_FIB_DEFAULT_HASH_MEMORY_SIZE);
980
981 /*
982 * the result from a 'miss' in a NSH Table
983 */
984 lgm->nsh_cp_lkup = lisp_cp_dpo_get (DPO_PROTO_NSH);
985}
986
987static void
988del_nsh_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
989{
990 lisp_fwd_path_t *path;
991
992 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
993 {
994 vec_foreach (path, lfe->paths)
995 {
996 lisp_gpe_adjacency_unlock (path->lisp_adj);
997 }
998 fib_path_list_child_remove (lfe->nsh.path_list_index,
999 lfe->nsh.child_index);
1000 dpo_reset (&lfe->nsh.choice);
1001 }
1002
1003 lisp_nsh_fib_add_del_entry (fid_addr_nsh (&lfe->key->rmt), (u32) ~ 0, 0);
1004
1005 hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
1006 clib_mem_free (lfe->key);
1007 pool_put (lgm->lisp_fwd_entry_pool, lfe);
1008}
1009
1010/**
1011 * @brief Delete LISP NSH forwarding entry.
1012 *
1013 * Coordinates the removal of forwarding entries for NSH LISP overlay:
1014 *
1015 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
1016 * @param[in] a Parameters for building the forwarding entry.
1017 *
1018 * @return 0 on success.
1019 */
1020static int
1021del_nsh_fwd_entry (lisp_gpe_main_t * lgm,
1022 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
1023{
1024 lisp_gpe_fwd_entry_key_t key;
1025 lisp_gpe_fwd_entry_t *lfe;
1026
1027 lfe = find_fwd_entry (lgm, a, &key);
1028
1029 if (NULL == lfe)
1030 return VNET_API_ERROR_INVALID_VALUE;
1031
1032 del_nsh_fwd_entry_i (lgm, lfe);
1033
1034 return (0);
1035}
1036
1037/**
1038 * @brief Construct and insert the forwarding information used by an NSH entry
1039 */
1040static void
1041lisp_gpe_nsh_update_fwding (lisp_gpe_fwd_entry_t * lfe)
1042{
1043 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1044 dpo_id_t dpo = DPO_INVALID;
1045 vnet_hw_interface_t *hi;
1046 uword *hip;
1047
1048 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
1049 {
1050 fib_path_list_contribute_forwarding (lfe->nsh.path_list_index,
1051 FIB_FORW_CHAIN_TYPE_NSH,
Neale Ranns91286372017-12-05 13:24:04 -08001052 FIB_PATH_LIST_FWD_FLAG_NONE,
Florin Corasce1b4c72017-01-26 14:25:34 -08001053 &lfe->nsh.dpo);
1054
1055 /*
1056 * LISP encap is always the same for this SPI+SI so we do that hash now
1057 * and stack on the choice.
1058 */
1059 if (DPO_LOAD_BALANCE == lfe->nsh.dpo.dpoi_type)
1060 {
1061 const dpo_id_t *tmp;
1062 const load_balance_t *lb;
1063 int hash;
1064
1065 lb = load_balance_get (lfe->nsh.dpo.dpoi_index);
1066 hash = fid_addr_nsh (&lfe->key->rmt) % lb->lb_n_buckets;
1067 tmp =
1068 load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
1069
1070 dpo_copy (&dpo, tmp);
1071 }
1072 }
1073 else
1074 {
1075 switch (lfe->action)
1076 {
1077 case SEND_MAP_REQUEST:
1078 dpo_copy (&dpo, lgm->nsh_cp_lkup);
1079 break;
1080 case NO_ACTION:
1081 case FORWARD_NATIVE:
1082 case DROP:
1083 dpo_copy (&dpo, drop_dpo_get (DPO_PROTO_NSH));
1084 }
1085 }
1086
1087 /* We have only one nsh-lisp interface (no NSH virtualization) */
1088 hip = hash_get (lgm->nsh_ifaces.hw_if_index_by_dp_table, 0);
Shwetha Bhandarib05f1f02017-02-14 10:39:06 +05301089 if (hip)
1090 {
1091 hi = vnet_get_hw_interface (lgm->vnet_main, hip[0]);
1092 dpo_stack_from_node (hi->tx_node_index, &lfe->nsh.choice, &dpo);
1093 }
Florin Corasce1b4c72017-01-26 14:25:34 -08001094 /* add entry to nsh lisp fib */
1095 lisp_nsh_fib_add_del_entry (fid_addr_nsh (&lfe->key->rmt),
1096 lfe - lgm->lisp_fwd_entry_pool, 1);
Florin Corasce1b4c72017-01-26 14:25:34 -08001097 dpo_reset (&dpo);
Shwetha Bhandarib05f1f02017-02-14 10:39:06 +05301098
Florin Corasce1b4c72017-01-26 14:25:34 -08001099}
1100
1101/**
1102 * @brief Add LISP NSH forwarding entry.
1103 *
1104 * Coordinates the creation of forwarding entries for L2 LISP overlay:
1105 * creates lisp-gpe tunnel and injects new entry in Source/Dest L2 FIB.
1106 *
1107 * @param[in] lgm Reference to @ref lisp_gpe_main_t.
1108 * @param[in] a Parameters for building the forwarding entry.
1109 *
1110 * @return 0 on success.
1111 */
1112static int
1113add_nsh_fwd_entry (lisp_gpe_main_t * lgm,
1114 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
1115{
1116 lisp_gpe_fwd_entry_key_t key;
1117 lisp_gpe_fwd_entry_t *lfe;
1118
1119 lfe = find_fwd_entry (lgm, a, &key);
1120
1121 if (NULL != lfe)
1122 /* don't support updates */
1123 return VNET_API_ERROR_INVALID_VALUE;
1124
1125 pool_get (lgm->lisp_fwd_entry_pool, lfe);
1126 memset (lfe, 0, sizeof (*lfe));
1127 lfe->key = clib_mem_alloc (sizeof (key));
1128 memcpy (lfe->key, &key, sizeof (key));
1129
1130 hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
1131 lfe - lgm->lisp_fwd_entry_pool);
Filip Tehlar560274d2017-06-05 13:40:13 +02001132 a->fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
Florin Corasce1b4c72017-01-26 14:25:34 -08001133
1134 lfe->type = (a->is_negative ?
1135 LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
1136 LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
1137 lfe->tenant = 0;
1138
1139 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
1140 {
1141 fib_route_path_t *rpaths;
1142
1143 /*
1144 * Make the sorted array of LISP paths with their resp. adjacency
1145 */
1146 lisp_gpe_fwd_entry_mk_paths (lfe, a);
1147
1148 /*
1149 * From the LISP paths, construct a FIB path list that will
1150 * contribute a load-balance.
1151 */
1152 rpaths = lisp_gpe_mk_fib_paths (lfe->paths);
1153
1154 lfe->nsh.path_list_index =
1155 fib_path_list_create (FIB_PATH_LIST_FLAG_NONE, rpaths);
1156
1157 /*
1158 * become a child of the path-list so we receive updates when
1159 * its forwarding state changes. this includes an implicit lock.
1160 */
1161 lfe->nsh.child_index =
1162 fib_path_list_child_add (lfe->nsh.path_list_index,
1163 FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY,
1164 lfe - lgm->lisp_fwd_entry_pool);
1165 }
1166 else
1167 {
1168 lfe->action = a->action;
1169 }
1170
1171 lisp_gpe_nsh_update_fwding (lfe);
1172
1173 return 0;
1174}
1175
1176/**
Neale Ranns5e575b12016-10-03 09:40:25 +01001177 * @brief conver from the embedded fib_node_t struct to the LSIP entry
1178 */
1179static lisp_gpe_fwd_entry_t *
1180lisp_gpe_fwd_entry_from_fib_node (fib_node_t * node)
1181{
1182 return ((lisp_gpe_fwd_entry_t *) (((char *) node) -
1183 STRUCT_OFFSET_OF (lisp_gpe_fwd_entry_t,
1184 node)));
1185}
1186
1187/**
1188 * @brief Function invoked during a backwalk of the FIB graph
1189 */
1190static fib_node_back_walk_rc_t
1191lisp_gpe_fib_node_back_walk (fib_node_t * node,
1192 fib_node_back_walk_ctx_t * ctx)
1193{
Florin Corasce1b4c72017-01-26 14:25:34 -08001194 lisp_gpe_fwd_entry_t *lfe = lisp_gpe_fwd_entry_from_fib_node (node);
1195
1196 if (fid_addr_type (&lfe->key->rmt) == FID_ADDR_MAC)
1197 lisp_gpe_l2_update_fwding (lfe);
1198 else if (fid_addr_type (&lfe->key->rmt) == FID_ADDR_NSH)
1199 lisp_gpe_nsh_update_fwding (lfe);
Neale Ranns5e575b12016-10-03 09:40:25 +01001200
1201 return (FIB_NODE_BACK_WALK_CONTINUE);
1202}
1203
1204/**
1205 * @brief Get a fib_node_t struct from the index of a LISP fwd entry
1206 */
1207static fib_node_t *
1208lisp_gpe_fwd_entry_get_fib_node (fib_node_index_t index)
1209{
1210 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1211 lisp_gpe_fwd_entry_t *lfe;
1212
1213 lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, index);
1214
1215 return (&(lfe->node));
1216}
1217
1218/**
1219 * @brief An indication from the graph that the last lock has gone
1220 */
1221static void
1222lisp_gpe_fwd_entry_fib_node_last_lock_gone (fib_node_t * node)
1223{
1224 /* We don't manage the locks of the LISP objects via the graph, since
1225 * this object has no children. so this is a no-op. */
1226}
1227
1228/**
1229 * @brief Virtual function table to register with FIB for the LISP type
1230 */
1231const static fib_node_vft_t lisp_fwd_vft = {
1232 .fnv_get = lisp_gpe_fwd_entry_get_fib_node,
1233 .fnv_last_lock = lisp_gpe_fwd_entry_fib_node_last_lock_gone,
1234 .fnv_back_walk = lisp_gpe_fib_node_back_walk,
1235};
1236
1237/**
1238 * @brief Forwarding entry create/remove dispatcher.
1239 *
1240 * Calls l2 or l3 forwarding entry add/del function based on input data.
1241 *
1242 * @param[in] a Forwarding entry parameters.
1243 * @param[out] hw_if_indexp NOT USED
1244 *
1245 * @return 0 on success.
1246 */
1247int
1248vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
1249 u32 * hw_if_indexp)
1250{
1251 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1252 u8 type;
1253
1254 if (vnet_lisp_gpe_enable_disable_status () == 0)
1255 {
1256 clib_warning ("LISP is disabled!");
1257 return VNET_API_ERROR_LISP_DISABLED;
1258 }
1259
1260 type = gid_address_type (&a->rmt_eid);
1261 switch (type)
1262 {
1263 case GID_ADDR_IP_PREFIX:
1264 if (a->is_add)
1265 return add_ip_fwd_entry (lgm, a);
1266 else
1267 return del_ip_fwd_entry (lgm, a);
1268 break;
1269 case GID_ADDR_MAC:
1270 if (a->is_add)
1271 return add_l2_fwd_entry (lgm, a);
1272 else
1273 return del_l2_fwd_entry (lgm, a);
Florin Corasce1b4c72017-01-26 14:25:34 -08001274 case GID_ADDR_NSH:
1275 if (a->is_add)
1276 return add_nsh_fwd_entry (lgm, a);
1277 else
1278 return del_nsh_fwd_entry (lgm, a);
Neale Ranns5e575b12016-10-03 09:40:25 +01001279 default:
1280 clib_warning ("Forwarding entries for type %d not supported!", type);
1281 return -1;
1282 }
1283}
1284
Filip Tehlar21511912017-04-07 10:41:42 +02001285int
Filip Tehlar4868ff62017-03-09 16:48:39 +01001286vnet_lisp_flush_stats (void)
1287{
1288 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
Filip Tehlar21511912017-04-07 10:41:42 +02001289 vlib_combined_counter_main_t *cm = &lgm->counters;
1290 u32 i;
Filip Tehlar4868ff62017-03-09 16:48:39 +01001291
Swarup Nayakabbc04c2017-12-11 20:08:57 +05301292 if (cm->counters == NULL)
1293 return 0;
1294
Filip Tehlar21511912017-04-07 10:41:42 +02001295 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
1296 vlib_zero_combined_counter (cm, i);
1297
1298 return 0;
Filip Tehlar4868ff62017-03-09 16:48:39 +01001299}
1300
1301static void
1302lisp_del_adj_stats (lisp_gpe_main_t * lgm, u32 fwd_entry_index, u32 ti)
1303{
1304 hash_pair_t *hp;
1305 lisp_stats_key_t key;
1306 void *key_copy;
1307 uword *p;
Filip Tehlar21511912017-04-07 10:41:42 +02001308 u8 *s;
Filip Tehlar4868ff62017-03-09 16:48:39 +01001309
1310 memset (&key, 0, sizeof (key));
1311 key.fwd_entry_index = fwd_entry_index;
1312 key.tunnel_index = ti;
1313
1314 p = hash_get_mem (lgm->lisp_stats_index_by_key, &key);
1315 if (p)
1316 {
Filip Tehlar21511912017-04-07 10:41:42 +02001317 s = pool_elt_at_index (lgm->dummy_stats_pool, p[0]);
Filip Tehlar4868ff62017-03-09 16:48:39 +01001318 hp = hash_get_pair (lgm->lisp_stats_index_by_key, &key);
1319 key_copy = (void *) (hp->key);
1320 hash_unset_mem (lgm->lisp_stats_index_by_key, &key);
1321 clib_mem_free (key_copy);
Filip Tehlar21511912017-04-07 10:41:42 +02001322 pool_put (lgm->dummy_stats_pool, s);
Filip Tehlar4868ff62017-03-09 16:48:39 +01001323 }
1324}
1325
1326void
Filip Tehlar21511912017-04-07 10:41:42 +02001327vnet_lisp_gpe_del_fwd_counters (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
1328 u32 fwd_entry_index)
Filip Tehlar4868ff62017-03-09 16:48:39 +01001329{
1330 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1331 lisp_gpe_fwd_entry_key_t fe_key;
1332 lisp_gpe_fwd_entry_t *lfe;
1333 lisp_fwd_path_t *path;
1334 const lisp_gpe_adjacency_t *ladj;
1335
1336 lfe = find_fwd_entry (lgm, a, &fe_key);
1337 if (!lfe)
1338 return;
1339
1340 if (LISP_GPE_FWD_ENTRY_TYPE_NORMAL != lfe->type)
1341 return;
1342
1343 vec_foreach (path, lfe->paths)
1344 {
1345 ladj = lisp_gpe_adjacency_get (path->lisp_adj);
1346 lisp_del_adj_stats (lgm, fwd_entry_index, ladj->tunnel_index);
1347 }
1348}
1349
Neale Ranns5e575b12016-10-03 09:40:25 +01001350/**
1351 * @brief Flush all the forwrding entries
1352 */
1353void
1354vnet_lisp_gpe_fwd_entry_flush (void)
1355{
1356 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1357 lisp_gpe_fwd_entry_t *lfe;
1358
1359 /* *INDENT-OFF* */
1360 pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1361 ({
1362 switch (fid_addr_type(&lfe->key->rmt))
1363 {
1364 case FID_ADDR_MAC:
1365 del_l2_fwd_entry_i (lgm, lfe);
1366 break;
1367 case FID_ADDR_IP_PREF:
1368 del_ip_fwd_entry_i (lgm, lfe);
1369 break;
Florin Corasce1b4c72017-01-26 14:25:34 -08001370 case FID_ADDR_NSH:
1371 del_nsh_fwd_entry_i (lgm, lfe);
1372 break;
Neale Ranns5e575b12016-10-03 09:40:25 +01001373 }
1374 }));
1375 /* *INDENT-ON* */
1376}
1377
1378static u8 *
Christophe Fontained3c008d2017-10-02 18:10:54 +02001379format_lisp_fwd_path (u8 * s, va_list * ap)
Neale Ranns5e575b12016-10-03 09:40:25 +01001380{
Christophe Fontained3c008d2017-10-02 18:10:54 +02001381 lisp_fwd_path_t *lfp = va_arg (*ap, lisp_fwd_path_t *);
Neale Ranns5e575b12016-10-03 09:40:25 +01001382
Filip Tehlarc3af7bf2017-01-13 14:13:09 +01001383 s = format (s, "weight:%d ", lfp->weight);
Neale Ranns5e575b12016-10-03 09:40:25 +01001384 s = format (s, "adj:[%U]\n",
1385 format_lisp_gpe_adjacency,
1386 lisp_gpe_adjacency_get (lfp->lisp_adj),
1387 LISP_GPE_ADJ_FORMAT_FLAG_NONE);
1388
1389 return (s);
1390}
1391
1392typedef enum lisp_gpe_fwd_entry_format_flag_t_
1393{
1394 LISP_GPE_FWD_ENTRY_FORMAT_NONE = (0 << 0),
1395 LISP_GPE_FWD_ENTRY_FORMAT_DETAIL = (1 << 1),
1396} lisp_gpe_fwd_entry_format_flag_t;
1397
1398
1399static u8 *
Christophe Fontained3c008d2017-10-02 18:10:54 +02001400format_lisp_gpe_fwd_entry (u8 * s, va_list * ap)
Neale Ranns5e575b12016-10-03 09:40:25 +01001401{
1402 lisp_gpe_main_t *lgm = &lisp_gpe_main;
Christophe Fontained3c008d2017-10-02 18:10:54 +02001403 lisp_gpe_fwd_entry_t *lfe = va_arg (*ap, lisp_gpe_fwd_entry_t *);
Neale Ranns5e575b12016-10-03 09:40:25 +01001404 lisp_gpe_fwd_entry_format_flag_t flags =
Christophe Fontained3c008d2017-10-02 18:10:54 +02001405 va_arg (*ap, lisp_gpe_fwd_entry_format_flag_t);
Neale Ranns5e575b12016-10-03 09:40:25 +01001406
1407 s = format (s, "VNI:%d VRF:%d EID: %U -> %U [index:%d]",
1408 lfe->key->vni, lfe->eid_table_id,
1409 format_fid_address, &lfe->key->lcl,
1410 format_fid_address, &lfe->key->rmt,
1411 lfe - lgm->lisp_fwd_entry_pool);
1412
1413 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
1414 {
1415 s = format (s, "\n Negative - action:%U",
1416 format_negative_mapping_action, lfe->action);
1417 }
1418 else
1419 {
1420 lisp_fwd_path_t *path;
1421
1422 s = format (s, "\n via:");
1423 vec_foreach (path, lfe->paths)
1424 {
1425 s = format (s, "\n %U", format_lisp_fwd_path, path);
1426 }
1427 }
1428
1429 if (flags & LISP_GPE_FWD_ENTRY_FORMAT_DETAIL)
1430 {
1431 switch (fid_addr_type (&lfe->key->rmt))
1432 {
1433 case FID_ADDR_MAC:
1434 s = format (s, " fib-path-list:%d\n", lfe->l2.path_list_index);
1435 s = format (s, " dpo:%U\n", format_dpo_id, &lfe->l2.dpo, 0);
1436 break;
Florin Corasce1b4c72017-01-26 14:25:34 -08001437 case FID_ADDR_NSH:
1438 s = format (s, " fib-path-list:%d\n", lfe->nsh.path_list_index);
1439 s = format (s, " dpo:%U\n", format_dpo_id, &lfe->nsh.dpo, 0);
1440 break;
Neale Ranns5e575b12016-10-03 09:40:25 +01001441 case FID_ADDR_IP_PREF:
1442 break;
1443 }
1444 }
1445
1446 return (s);
1447}
1448
1449static clib_error_t *
1450lisp_gpe_fwd_entry_show (vlib_main_t * vm,
1451 unformat_input_t * input, vlib_cli_command_t * cmd)
1452{
1453 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1454 lisp_gpe_fwd_entry_t *lfe;
1455 index_t index;
1456 u32 vni = ~0;
1457
1458 if (unformat (input, "vni %d", &vni))
1459 ;
1460 else if (unformat (input, "%d", &index))
1461 {
1462 if (!pool_is_free_index (lgm->lisp_fwd_entry_pool, index))
1463 {
1464 lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, index);
1465
1466 vlib_cli_output (vm, "[%d@] %U",
1467 index,
1468 format_lisp_gpe_fwd_entry, lfe,
1469 LISP_GPE_FWD_ENTRY_FORMAT_DETAIL);
1470 }
1471 else
1472 {
1473 vlib_cli_output (vm, "entry %d invalid", index);
1474 }
1475
1476 return (NULL);
1477 }
1478
1479 /* *INDENT-OFF* */
1480 pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1481 ({
1482 if ((vni == ~0) ||
1483 (lfe->key->vni == vni))
1484 vlib_cli_output (vm, "%U", format_lisp_gpe_fwd_entry, lfe,
1485 LISP_GPE_FWD_ENTRY_FORMAT_NONE);
1486 }));
1487 /* *INDENT-ON* */
1488
1489 return (NULL);
1490}
1491
1492/* *INDENT-OFF* */
1493VLIB_CLI_COMMAND (lisp_gpe_fwd_entry_show_command, static) = {
Filip Tehlar82786c42017-02-20 15:20:37 +01001494 .path = "show gpe entry",
1495 .short_help = "show gpe entry vni <vni> vrf <vrf> [leid <leid>] reid <reid>",
Neale Ranns5e575b12016-10-03 09:40:25 +01001496 .function = lisp_gpe_fwd_entry_show,
1497};
1498/* *INDENT-ON* */
1499
1500clib_error_t *
1501lisp_gpe_fwd_entry_init (vlib_main_t * vm)
1502{
1503 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1504 clib_error_t *error = NULL;
1505
1506 if ((error = vlib_call_init_function (vm, lisp_cp_dpo_module_init)))
1507 return (error);
1508
1509 l2_fib_init (lgm);
Florin Corasce1b4c72017-01-26 14:25:34 -08001510 nsh_fib_init (lgm);
Neale Ranns5e575b12016-10-03 09:40:25 +01001511
1512 fib_node_register_type (FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY, &lisp_fwd_vft);
1513
1514 return (error);
1515}
1516
Filip Tehlar0eb874e2017-05-18 14:23:32 +02001517u32 *
1518vnet_lisp_gpe_get_fwd_entry_vnis (void)
1519{
1520 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1521 lisp_gpe_fwd_entry_t *lfe;
1522 u32 *vnis = 0;
1523
1524 /* *INDENT-OFF* */
1525 pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1526 ({
1527 hash_set (vnis, lfe->key->vni, 0);
1528 }));
1529 /* *INDENT-ON* */
1530
1531 return vnis;
1532}
1533
Filip Tehlar5fae99c2017-01-18 12:57:37 +01001534lisp_api_gpe_fwd_entry_t *
1535vnet_lisp_gpe_fwd_entries_get_by_vni (u32 vni)
1536{
1537 lisp_gpe_main_t *lgm = &lisp_gpe_main;
1538 lisp_gpe_fwd_entry_t *lfe;
1539 lisp_api_gpe_fwd_entry_t *entries = 0, e;
1540
1541 /* *INDENT-OFF* */
1542 pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1543 ({
1544 if (lfe->key->vni == vni)
1545 {
1546 memset (&e, 0, sizeof (e));
1547 e.dp_table = lfe->eid_table_id;
1548 e.vni = lfe->key->vni;
Filip Tehlar0eb874e2017-05-18 14:23:32 +02001549 if (lfe->type == LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE)
1550 e.action = lfe->action;
Filip Tehlar5fae99c2017-01-18 12:57:37 +01001551 e.fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
1552 memcpy (&e.reid, &lfe->key->rmt, sizeof (e.reid));
1553 memcpy (&e.leid, &lfe->key->lcl, sizeof (e.leid));
1554 vec_add1 (entries, e);
1555 }
1556 }));
1557 /* *INDENT-ON* */
1558
1559 return entries;
1560}
1561
Filip Tehlar809bc742017-08-14 19:15:36 +02001562int
1563vnet_lisp_gpe_get_fwd_stats (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
1564 vlib_counter_t * c)
1565{
1566 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1567 lisp_gpe_fwd_entry_t *lfe;
1568 lisp_gpe_fwd_entry_key_t unused;
1569
1570 lfe = find_fwd_entry (lgm, a, &unused);
1571 if (NULL == lfe)
1572 return -1;
1573
1574 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
1575 return -1;
1576
1577 if (~0 == lfe->dpoi_index)
1578 return -1;
1579
1580 vlib_get_combined_counter (&load_balance_main.lbm_to_counters,
1581 lfe->dpoi_index, c);
1582 return 0;
1583}
1584
Neale Ranns5e575b12016-10-03 09:40:25 +01001585VLIB_INIT_FUNCTION (lisp_gpe_fwd_entry_init);
1586
1587/*
1588 * fd.io coding-style-patch-verification: ON
1589 *
1590 * Local Variables:
1591 * eval: (c-set-style "gnu")
1592 * End:
1593 */