LISP: Fix gpe API
[vpp.git] / src / vnet / lisp-gpe / lisp_gpe_fwd_entry.c
1 /*
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  */
41 static u32
42 ip_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     {
64       dpo_id_t src_lkup_dpo = DPO_INVALID;
65
66       /* create a new src FIB.  */
67       src_fib_index =
68         fib_table_create_and_lock (dst_fib_prefix.fp_proto,
69                                    "LISP-src for [%d,%U]",
70                                    dst_fib_index,
71                                    format_fib_prefix, &dst_fib_prefix);
72       /*
73        * add src fib default route
74        */
75       fib_prefix_t prefix = {
76         .fp_proto = dst_fib_prefix.fp_proto,
77       };
78       fib_table_entry_special_dpo_add (src_fib_index, &prefix,
79                                        FIB_SOURCE_LISP,
80                                        FIB_ENTRY_FLAG_EXCLUSIVE,
81                                        lisp_cp_dpo_get (fib_proto_to_dpo
82                                                         (dst_fib_prefix.fp_proto)));
83       /*
84        * create a data-path object to perform the source address lookup
85        * in the SRC FIB
86        */
87       lookup_dpo_add_or_lock_w_fib_index (src_fib_index,
88                                           (ip_prefix_version (dst_prefix) ==
89                                            IP6 ? DPO_PROTO_IP6 :
90                                            DPO_PROTO_IP4),
91                                           LOOKUP_INPUT_SRC_ADDR,
92                                           LOOKUP_TABLE_FROM_CONFIG,
93                                           &src_lkup_dpo);
94
95       /*
96        * add the entry to the destination FIB that uses the lookup DPO
97        */
98       dst_fei = fib_table_entry_special_dpo_add (dst_fib_index,
99                                                  &dst_fib_prefix,
100                                                  FIB_SOURCE_LISP,
101                                                  FIB_ENTRY_FLAG_EXCLUSIVE,
102                                                  &src_lkup_dpo);
103
104       /*
105        * the DPO is locked by the FIB entry, and we have no further
106        * need for it.
107        */
108       dpo_unlock (&src_lkup_dpo);
109
110       /*
111        * save the SRC FIB index on the entry so we can retrieve it for
112        * subsequent routes.
113        */
114       fib_entry_set_source_data (dst_fei, FIB_SOURCE_LISP, &src_fib_index);
115     }
116   else
117     {
118       /*
119        * destination FIB entry already present
120        */
121       src_fib_index = *(u32 *) fib_entry_get_source_data (dst_fei,
122                                                           FIB_SOURCE_LISP);
123     }
124
125   return (src_fib_index);
126 }
127
128 /**
129  * @brief Del route to IP4 or IP6 SD FIB.
130  *
131  * Remove routes from both destination and source FIBs.
132  *
133  * @param[in]   src_fib_index   The index/ID of the SRC FIB
134  * @param[in]   src_prefix      Source IP prefix.
135  * @param[in]   dst_fib_index   The index/ID of the DST FIB
136  * @param[in]   dst_prefix      Destination IP prefix.
137  */
138 static void
139 ip_src_dst_fib_del_route (u32 src_fib_index,
140                           const ip_prefix_t * src_prefix,
141                           u32 dst_fib_index, const ip_prefix_t * dst_prefix)
142 {
143   fib_prefix_t dst_fib_prefix, src_fib_prefix;
144   u8 have_default = 0;
145   u32 n_entries;
146
147   ASSERT (NULL != dst_prefix);
148   ASSERT (NULL != src_prefix);
149
150   ip_prefix_to_fib_prefix (dst_prefix, &dst_fib_prefix);
151   ip_prefix_to_fib_prefix (src_prefix, &src_fib_prefix);
152
153   fib_table_entry_delete (src_fib_index, &src_fib_prefix, FIB_SOURCE_LISP);
154
155   /* check if only default left or empty */
156   fib_prefix_t default_pref = {
157     .fp_proto = dst_fib_prefix.fp_proto
158   };
159
160   if (fib_table_lookup_exact_match (src_fib_index,
161                                     &default_pref) != FIB_NODE_INDEX_INVALID)
162     have_default = 1;
163
164   n_entries = fib_table_get_num_entries (src_fib_index,
165                                          src_fib_prefix.fp_proto,
166                                          FIB_SOURCE_LISP);
167   if (n_entries == 0 || (have_default && n_entries == 1))
168     {
169       /*
170        * remove src FIB default route
171        */
172       if (have_default)
173         fib_table_entry_special_remove (src_fib_index, &default_pref,
174                                         FIB_SOURCE_LISP);
175
176       /*
177        * there's nothing left now, unlock the source FIB and the
178        * destination route
179        */
180       fib_table_entry_special_remove (dst_fib_index,
181                                       &dst_fib_prefix, FIB_SOURCE_LISP);
182       fib_table_unlock (src_fib_index, src_fib_prefix.fp_proto);
183     }
184 }
185
186 /**
187  * @brief Add route to IP4 or IP6 SRC FIB.
188  *
189  * Adds a route to in the LISP SRC FIB with the result of the route
190  * being the DPO passed.
191  *
192  * @param[in]   src_fib_index   The index/ID of the SRC FIB
193  * @param[in]   src_prefix      Source IP prefix.
194  * @param[in]   src_dpo         The DPO the route will link to.
195  */
196 static void
197 ip_src_fib_add_route_w_dpo (u32 src_fib_index,
198                             const ip_prefix_t * src_prefix,
199                             const dpo_id_t * src_dpo)
200 {
201   fib_prefix_t src_fib_prefix;
202
203   ip_prefix_to_fib_prefix (src_prefix, &src_fib_prefix);
204
205   /*
206    * add the entry into the source fib.
207    */
208   fib_node_index_t src_fei;
209
210   src_fei = fib_table_lookup_exact_match (src_fib_index, &src_fib_prefix);
211
212   if (FIB_NODE_INDEX_INVALID == src_fei ||
213       !fib_entry_is_sourced (src_fei, FIB_SOURCE_LISP))
214     {
215       fib_table_entry_special_dpo_add (src_fib_index,
216                                        &src_fib_prefix,
217                                        FIB_SOURCE_LISP,
218                                        FIB_ENTRY_FLAG_EXCLUSIVE, src_dpo);
219     }
220 }
221
222 static fib_route_path_t *
223 lisp_gpe_mk_fib_paths (const lisp_fwd_path_t * paths)
224 {
225   const lisp_gpe_adjacency_t *ladj;
226   fib_route_path_t *rpaths = NULL;
227   u8 best_priority;
228   u32 ii;
229
230   vec_validate (rpaths, vec_len (paths) - 1);
231
232   best_priority = paths[0].priority;
233
234   vec_foreach_index (ii, paths)
235   {
236     if (paths[0].priority != best_priority)
237       break;
238
239     ladj = lisp_gpe_adjacency_get (paths[ii].lisp_adj);
240
241     ip_address_to_46 (&ladj->remote_rloc,
242                       &rpaths[ii].frp_addr, &rpaths[ii].frp_proto);
243
244     rpaths[ii].frp_sw_if_index = ladj->sw_if_index;
245     rpaths[ii].frp_weight = (paths[ii].weight ? paths[ii].weight : 1);
246   }
247
248   ASSERT (0 != vec_len (rpaths));
249
250   return (rpaths);
251 }
252
253 /**
254  * @brief Add route to IP4 or IP6 SRC FIB.
255  *
256  * Adds a route to in the LISP SRC FIB for the tunnel.
257  *
258  * @param[in]   src_fib_index   The index/ID of the SRC FIB
259  * @param[in]   src_prefix      Source IP prefix.
260  * @param[in]   paths           The paths from which to construct the
261  *                              load balance
262  */
263 static void
264 ip_src_fib_add_route (u32 src_fib_index,
265                       const ip_prefix_t * src_prefix,
266                       const lisp_fwd_path_t * paths)
267 {
268   fib_prefix_t src_fib_prefix;
269   fib_route_path_t *rpaths;
270
271   ip_prefix_to_fib_prefix (src_prefix, &src_fib_prefix);
272
273   rpaths = lisp_gpe_mk_fib_paths (paths);
274
275   fib_table_entry_update (src_fib_index,
276                           &src_fib_prefix,
277                           FIB_SOURCE_LISP, FIB_ENTRY_FLAG_NONE, rpaths);
278   vec_free (rpaths);
279 }
280
281
282 static void
283 create_fib_entries (lisp_gpe_fwd_entry_t * lfe)
284 {
285   dpo_proto_t dproto;
286
287   dproto = (ip_prefix_version (&lfe->key->rmt.ippref) == IP4 ?
288             DPO_PROTO_IP4 : DPO_PROTO_IP6);
289
290   lfe->src_fib_index = ip_dst_fib_add_route (lfe->eid_fib_index,
291                                              &lfe->key->rmt.ippref);
292
293   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
294     {
295       dpo_id_t dpo = DPO_INVALID;
296
297       switch (lfe->action)
298         {
299         case LISP_NO_ACTION:
300           /* TODO update timers? */
301         case LISP_FORWARD_NATIVE:
302           /* TODO check if route/next-hop for eid exists in fib and add
303            * more specific for the eid with the next-hop found */
304         case LISP_SEND_MAP_REQUEST:
305           /* insert tunnel that always sends map-request */
306           dpo_copy (&dpo, lisp_cp_dpo_get (dproto));
307           break;
308         case LISP_DROP:
309           /* for drop fwd entries, just add route, no need to add encap tunnel */
310           dpo_copy (&dpo, drop_dpo_get (dproto));
311           break;
312         }
313       ip_src_fib_add_route_w_dpo (lfe->src_fib_index,
314                                   &lfe->key->lcl.ippref, &dpo);
315       dpo_reset (&dpo);
316     }
317   else
318     {
319       ip_src_fib_add_route (lfe->src_fib_index,
320                             &lfe->key->lcl.ippref, lfe->paths);
321     }
322 }
323
324 static void
325 delete_fib_entries (lisp_gpe_fwd_entry_t * lfe)
326 {
327   ip_src_dst_fib_del_route (lfe->src_fib_index,
328                             &lfe->key->lcl.ippref,
329                             lfe->eid_fib_index, &lfe->key->rmt.ippref);
330 }
331
332 static void
333 gid_to_dp_address (gid_address_t * g, dp_address_t * d)
334 {
335   switch (gid_address_type (g))
336     {
337     case GID_ADDR_IP_PREFIX:
338     case GID_ADDR_SRC_DST:
339       ip_prefix_copy (&d->ippref, &gid_address_ippref (g));
340       d->type = FID_ADDR_IP_PREF;
341       break;
342     case GID_ADDR_MAC:
343     default:
344       mac_copy (&d->mac, &gid_address_mac (g));
345       d->type = FID_ADDR_MAC;
346       break;
347     }
348 }
349
350 static lisp_gpe_fwd_entry_t *
351 find_fwd_entry (lisp_gpe_main_t * lgm,
352                 vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
353                 lisp_gpe_fwd_entry_key_t * key)
354 {
355   uword *p;
356
357   memset (key, 0, sizeof (*key));
358
359   if (GID_ADDR_IP_PREFIX == gid_address_type (&a->rmt_eid))
360     {
361       /*
362        * the ip version of the source is not set to ip6 when the
363        * source is all zeros. force it.
364        */
365       ip_prefix_version (&gid_address_ippref (&a->lcl_eid)) =
366         ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
367     }
368
369   gid_to_dp_address (&a->rmt_eid, &key->rmt);
370   gid_to_dp_address (&a->lcl_eid, &key->lcl);
371   key->vni = a->vni;
372
373   p = hash_get_mem (lgm->lisp_gpe_fwd_entries, key);
374
375   if (NULL != p)
376     {
377       return (pool_elt_at_index (lgm->lisp_fwd_entry_pool, p[0]));
378     }
379   return (NULL);
380 }
381
382 static int
383 lisp_gpe_fwd_entry_path_sort (void *a1, void *a2)
384 {
385   lisp_fwd_path_t *p1 = a1, *p2 = a2;
386
387   return (p1->priority - p2->priority);
388 }
389
390 static void
391 lisp_gpe_fwd_entry_mk_paths (lisp_gpe_fwd_entry_t * lfe,
392                              vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
393 {
394   lisp_fwd_path_t *path;
395   u32 index;
396
397   vec_validate (lfe->paths, vec_len (a->locator_pairs) - 1);
398
399   vec_foreach_index (index, a->locator_pairs)
400   {
401     path = &lfe->paths[index];
402
403     path->priority = a->locator_pairs[index].priority;
404     path->weight = a->locator_pairs[index].weight;
405
406     path->lisp_adj =
407       lisp_gpe_adjacency_find_or_create_and_lock (&a->locator_pairs
408                                                   [index],
409                                                   a->dp_table, lfe->key->vni);
410   }
411   vec_sort_with_function (lfe->paths, lisp_gpe_fwd_entry_path_sort);
412 }
413
414 /**
415  * @brief Add/Delete LISP IP forwarding entry.
416  *
417  * creation of forwarding entries for IP LISP overlay:
418  *
419  * @param[in]   lgm     Reference to @ref lisp_gpe_main_t.
420  * @param[in]   a       Parameters for building the forwarding entry.
421  *
422  * @return 0 on success.
423  */
424 static int
425 add_ip_fwd_entry (lisp_gpe_main_t * lgm,
426                   vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
427 {
428   lisp_gpe_fwd_entry_key_t key;
429   lisp_gpe_fwd_entry_t *lfe;
430   fib_protocol_t fproto;
431
432   lfe = find_fwd_entry (lgm, a, &key);
433
434   if (NULL != lfe)
435     /* don't support updates */
436     return VNET_API_ERROR_INVALID_VALUE;
437
438   pool_get (lgm->lisp_fwd_entry_pool, lfe);
439   memset (lfe, 0, sizeof (*lfe));
440   lfe->key = clib_mem_alloc (sizeof (key));
441   memcpy (lfe->key, &key, sizeof (key));
442
443   hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
444                 lfe - lgm->lisp_fwd_entry_pool);
445
446   fproto = (IP4 == ip_prefix_version (&fid_addr_ippref (&lfe->key->rmt)) ?
447             FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
448
449   lfe->type = (a->is_negative ?
450                LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
451                LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
452   lfe->tenant = lisp_gpe_tenant_find_or_create (lfe->key->vni);
453   lfe->eid_table_id = a->table_id;
454   lfe->eid_fib_index = fib_table_find_or_create_and_lock (fproto,
455                                                           lfe->eid_table_id);
456
457   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
458     {
459       lisp_gpe_fwd_entry_mk_paths (lfe, a);
460     }
461
462   create_fib_entries (lfe);
463
464   return (0);
465 }
466
467 static void
468 del_ip_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
469 {
470   lisp_fwd_path_t *path;
471   fib_protocol_t fproto;
472
473   vec_foreach (path, lfe->paths)
474   {
475     lisp_gpe_adjacency_unlock (path->lisp_adj);
476   }
477
478   delete_fib_entries (lfe);
479
480   fproto = (IP4 == ip_prefix_version (&fid_addr_ippref (&lfe->key->rmt)) ?
481             FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
482   fib_table_unlock (lfe->eid_fib_index, fproto);
483
484   hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
485   clib_mem_free (lfe->key);
486   pool_put (lgm->lisp_fwd_entry_pool, lfe);
487 }
488
489 /**
490  * @brief Add/Delete LISP IP forwarding entry.
491  *
492  * removal of forwarding entries for IP LISP overlay:
493  *
494  * @param[in]   lgm     Reference to @ref lisp_gpe_main_t.
495  * @param[in]   a       Parameters for building the forwarding entry.
496  *
497  * @return 0 on success.
498  */
499 static int
500 del_ip_fwd_entry (lisp_gpe_main_t * lgm,
501                   vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
502 {
503   lisp_gpe_fwd_entry_key_t key;
504   lisp_gpe_fwd_entry_t *lfe;
505
506   lfe = find_fwd_entry (lgm, a, &key);
507
508   if (NULL == lfe)
509     /* no such entry */
510     return VNET_API_ERROR_INVALID_VALUE;
511
512   del_ip_fwd_entry_i (lgm, lfe);
513
514   return (0);
515 }
516
517 static void
518 make_mac_fib_key (BVT (clib_bihash_kv) * kv, u16 bd_index, u8 src_mac[6],
519                   u8 dst_mac[6])
520 {
521   kv->key[0] = (((u64) bd_index) << 48) | mac_to_u64 (dst_mac);
522   kv->key[1] = mac_to_u64 (src_mac);
523   kv->key[2] = 0;
524 }
525
526 /**
527  * @brief Lookup L2 SD FIB entry
528  *
529  * Does a vni + dest + source lookup in the L2 LISP FIB. If the lookup fails
530  * it tries a second time with source set to 0 (i.e., a simple dest lookup).
531  *
532  * @param[in]   lgm             Reference to @ref lisp_gpe_main_t.
533  * @param[in]   bd_index        Bridge domain index.
534  * @param[in]   src_mac         Source mac address.
535  * @param[in]   dst_mac         Destination mac address.
536  *
537  * @return index of mapping matching the lookup key.
538  */
539 index_t
540 lisp_l2_fib_lookup (lisp_gpe_main_t * lgm, u16 bd_index, u8 src_mac[6],
541                     u8 dst_mac[6])
542 {
543   int rv;
544   BVT (clib_bihash_kv) kv, value;
545
546   make_mac_fib_key (&kv, bd_index, src_mac, dst_mac);
547   rv = BV (clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
548
549   /* no match, try with src 0, catch all for dst */
550   if (rv != 0)
551     {
552       kv.key[1] = 0;
553       rv = BV (clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
554       if (rv == 0)
555         return value.value;
556     }
557   else
558     return value.value;
559
560   return lisp_gpe_main.l2_lb_cp_lkup.dpoi_index;
561 }
562
563 /**
564  * @brief Add/del L2 SD FIB entry
565  *
566  * Inserts value in L2 FIB keyed by vni + dest + source. If entry is
567  * overwritten the associated value is returned.
568  *
569  * @param[in]   lgm             Reference to @ref lisp_gpe_main_t.
570  * @param[in]   bd_index        Bridge domain index.
571  * @param[in]   src_mac         Source mac address.
572  * @param[in]   dst_mac         Destination mac address.
573  * @param[in]   val             Value to add.
574  * @param[in]   is_add          Add/del flag.
575  *
576  * @return ~0 or value of overwritten entry.
577  */
578 static u32
579 lisp_l2_fib_add_del_entry (u16 bd_index, u8 src_mac[6],
580                            u8 dst_mac[6], const dpo_id_t * dpo, u8 is_add)
581 {
582   lisp_gpe_main_t *lgm = &lisp_gpe_main;
583   BVT (clib_bihash_kv) kv, value;
584   u32 old_val = ~0;
585
586   make_mac_fib_key (&kv, bd_index, src_mac, dst_mac);
587
588   if (BV (clib_bihash_search) (&lgm->l2_fib, &kv, &value) == 0)
589     old_val = value.value;
590
591   if (!is_add)
592     BV (clib_bihash_add_del) (&lgm->l2_fib, &kv, 0 /* is_add */ );
593   else
594     {
595       kv.value = dpo->dpoi_index;
596       BV (clib_bihash_add_del) (&lgm->l2_fib, &kv, 1 /* is_add */ );
597     }
598   return old_val;
599 }
600
601 #define L2_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
602 #define L2_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
603
604 static void
605 l2_fib_init (lisp_gpe_main_t * lgm)
606 {
607   index_t lbi;
608
609   BV (clib_bihash_init) (&lgm->l2_fib, "l2 fib",
610                          1 << max_log2 (L2_FIB_DEFAULT_HASH_NUM_BUCKETS),
611                          L2_FIB_DEFAULT_HASH_MEMORY_SIZE);
612
613   /*
614    * the result from a 'miss' in a L2 Table
615    */
616   lbi = load_balance_create (1, DPO_PROTO_ETHERNET, 0);
617   load_balance_set_bucket (lbi, 0, lisp_cp_dpo_get (DPO_PROTO_ETHERNET));
618
619   dpo_set (&lgm->l2_lb_cp_lkup, DPO_LOAD_BALANCE, DPO_PROTO_ETHERNET, lbi);
620 }
621
622 static void
623 del_l2_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
624 {
625   lisp_fwd_path_t *path;
626
627   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
628     {
629       vec_foreach (path, lfe->paths)
630       {
631         lisp_gpe_adjacency_unlock (path->lisp_adj);
632       }
633       fib_path_list_child_remove (lfe->l2.path_list_index,
634                                   lfe->l2.child_index);
635     }
636
637   lisp_l2_fib_add_del_entry (lfe->l2.eid_bd_index,
638                              fid_addr_mac (&lfe->key->lcl),
639                              fid_addr_mac (&lfe->key->rmt), NULL, 0);
640
641   hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
642   clib_mem_free (lfe->key);
643   pool_put (lgm->lisp_fwd_entry_pool, lfe);
644 }
645
646 /**
647  * @brief Delete LISP L2 forwarding entry.
648  *
649  * Coordinates the removal of forwarding entries for L2 LISP overlay:
650  *
651  * @param[in]   lgm     Reference to @ref lisp_gpe_main_t.
652  * @param[in]   a       Parameters for building the forwarding entry.
653  *
654  * @return 0 on success.
655  */
656 static int
657 del_l2_fwd_entry (lisp_gpe_main_t * lgm,
658                   vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
659 {
660   lisp_gpe_fwd_entry_key_t key;
661   lisp_gpe_fwd_entry_t *lfe;
662
663   lfe = find_fwd_entry (lgm, a, &key);
664
665   if (NULL == lfe)
666     return VNET_API_ERROR_INVALID_VALUE;
667
668   del_l2_fwd_entry_i (lgm, lfe);
669
670   return (0);
671 }
672
673 /**
674  * @brief Construct and insert the forwarding information used by a L2 entry
675  */
676 static void
677 lisp_gpe_l2_update_fwding (lisp_gpe_fwd_entry_t * lfe)
678 {
679   lisp_gpe_main_t *lgm = &lisp_gpe_main;
680   dpo_id_t dpo = DPO_INVALID;
681
682   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
683     {
684       fib_path_list_contribute_forwarding (lfe->l2.path_list_index,
685                                            FIB_FORW_CHAIN_TYPE_ETHERNET,
686                                            &lfe->l2.dpo);
687       dpo_copy (&dpo, &lfe->l2.dpo);
688     }
689   else
690     {
691       dpo_copy (&dpo, &lgm->l2_lb_cp_lkup);
692     }
693
694   /* add entry to l2 lisp fib */
695   lisp_l2_fib_add_del_entry (lfe->l2.eid_bd_index,
696                              fid_addr_mac (&lfe->key->lcl),
697                              fid_addr_mac (&lfe->key->rmt), &dpo, 1);
698
699   dpo_reset (&dpo);
700 }
701
702 /**
703  * @brief Add LISP L2 forwarding entry.
704  *
705  * Coordinates the creation of forwarding entries for L2 LISP overlay:
706  * creates lisp-gpe tunnel and injects new entry in Source/Dest L2 FIB.
707  *
708  * @param[in]   lgm     Reference to @ref lisp_gpe_main_t.
709  * @param[in]   a       Parameters for building the forwarding entry.
710  *
711  * @return 0 on success.
712  */
713 static int
714 add_l2_fwd_entry (lisp_gpe_main_t * lgm,
715                   vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
716 {
717   lisp_gpe_fwd_entry_key_t key;
718   bd_main_t *bdm = &bd_main;
719   lisp_gpe_fwd_entry_t *lfe;
720   uword *bd_indexp;
721
722   bd_indexp = hash_get (bdm->bd_index_by_bd_id, a->bd_id);
723   if (!bd_indexp)
724     {
725       clib_warning ("bridge domain %d doesn't exist", a->bd_id);
726       return -1;
727     }
728
729   lfe = find_fwd_entry (lgm, a, &key);
730
731   if (NULL != lfe)
732     /* don't support updates */
733     return VNET_API_ERROR_INVALID_VALUE;
734
735   pool_get (lgm->lisp_fwd_entry_pool, lfe);
736   memset (lfe, 0, sizeof (*lfe));
737   lfe->key = clib_mem_alloc (sizeof (key));
738   memcpy (lfe->key, &key, sizeof (key));
739
740   hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
741                 lfe - lgm->lisp_fwd_entry_pool);
742
743   lfe->type = (a->is_negative ?
744                LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
745                LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
746   lfe->l2.eid_bd_id = a->bd_id;
747   lfe->l2.eid_bd_index = bd_indexp[0];
748   lfe->tenant = lisp_gpe_tenant_find_or_create (lfe->key->vni);
749
750   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
751     {
752       fib_route_path_t *rpaths;
753
754       /*
755        * Make the sorted array of LISP paths with their resp. adjacency
756        */
757       lisp_gpe_fwd_entry_mk_paths (lfe, a);
758
759       /*
760        * From the LISP paths, construct a FIB path list that will
761        * contribute a load-balance.
762        */
763       rpaths = lisp_gpe_mk_fib_paths (lfe->paths);
764
765       lfe->l2.path_list_index =
766         fib_path_list_create (FIB_PATH_LIST_FLAG_NONE, rpaths);
767
768       /*
769        * become a child of the path-list so we receive updates when
770        * its forwarding state changes. this includes an implicit lock.
771        */
772       lfe->l2.child_index =
773         fib_path_list_child_add (lfe->l2.path_list_index,
774                                  FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY,
775                                  lfe - lgm->lisp_fwd_entry_pool);
776     }
777   else
778     {
779       lfe->action = a->action;
780     }
781
782   lisp_gpe_l2_update_fwding (lfe);
783
784   return 0;
785 }
786
787 /**
788  * @brief conver from the embedded fib_node_t struct to the LSIP entry
789  */
790 static lisp_gpe_fwd_entry_t *
791 lisp_gpe_fwd_entry_from_fib_node (fib_node_t * node)
792 {
793   return ((lisp_gpe_fwd_entry_t *) (((char *) node) -
794                                     STRUCT_OFFSET_OF (lisp_gpe_fwd_entry_t,
795                                                       node)));
796 }
797
798 /**
799  * @brief Function invoked during a backwalk of the FIB graph
800  */
801 static fib_node_back_walk_rc_t
802 lisp_gpe_fib_node_back_walk (fib_node_t * node,
803                              fib_node_back_walk_ctx_t * ctx)
804 {
805   lisp_gpe_l2_update_fwding (lisp_gpe_fwd_entry_from_fib_node (node));
806
807   return (FIB_NODE_BACK_WALK_CONTINUE);
808 }
809
810 /**
811  * @brief Get a fib_node_t struct from the index of a LISP fwd entry
812  */
813 static fib_node_t *
814 lisp_gpe_fwd_entry_get_fib_node (fib_node_index_t index)
815 {
816   lisp_gpe_main_t *lgm = &lisp_gpe_main;
817   lisp_gpe_fwd_entry_t *lfe;
818
819   lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, index);
820
821   return (&(lfe->node));
822 }
823
824 /**
825  * @brief An indication from the graph that the last lock has gone
826  */
827 static void
828 lisp_gpe_fwd_entry_fib_node_last_lock_gone (fib_node_t * node)
829 {
830   /* We don't manage the locks of the LISP objects via the graph, since
831    * this object has no children. so this is a no-op. */
832 }
833
834 /**
835  * @brief Virtual function table to register with FIB for the LISP type
836  */
837 const static fib_node_vft_t lisp_fwd_vft = {
838   .fnv_get = lisp_gpe_fwd_entry_get_fib_node,
839   .fnv_last_lock = lisp_gpe_fwd_entry_fib_node_last_lock_gone,
840   .fnv_back_walk = lisp_gpe_fib_node_back_walk,
841 };
842
843 /**
844  * @brief Forwarding entry create/remove dispatcher.
845  *
846  * Calls l2 or l3 forwarding entry add/del function based on input data.
847  *
848  * @param[in]   a       Forwarding entry parameters.
849  * @param[out]  hw_if_indexp    NOT USED
850  *
851  * @return 0 on success.
852  */
853 int
854 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
855                                  u32 * hw_if_indexp)
856 {
857   lisp_gpe_main_t *lgm = &lisp_gpe_main;
858   u8 type;
859
860   if (vnet_lisp_gpe_enable_disable_status () == 0)
861     {
862       clib_warning ("LISP is disabled!");
863       return VNET_API_ERROR_LISP_DISABLED;
864     }
865
866   type = gid_address_type (&a->rmt_eid);
867   switch (type)
868     {
869     case GID_ADDR_IP_PREFIX:
870       if (a->is_add)
871         return add_ip_fwd_entry (lgm, a);
872       else
873         return del_ip_fwd_entry (lgm, a);
874       break;
875     case GID_ADDR_MAC:
876       if (a->is_add)
877         return add_l2_fwd_entry (lgm, a);
878       else
879         return del_l2_fwd_entry (lgm, a);
880     default:
881       clib_warning ("Forwarding entries for type %d not supported!", type);
882       return -1;
883     }
884 }
885
886 /**
887  * @brief Flush all the forwrding entries
888  */
889 void
890 vnet_lisp_gpe_fwd_entry_flush (void)
891 {
892   lisp_gpe_main_t *lgm = &lisp_gpe_main;
893   lisp_gpe_fwd_entry_t *lfe;
894
895   /* *INDENT-OFF* */
896   pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
897   ({
898     switch (fid_addr_type(&lfe->key->rmt))
899       {
900       case FID_ADDR_MAC:
901         del_l2_fwd_entry_i (lgm, lfe);
902         break;
903       case FID_ADDR_IP_PREF:
904         del_ip_fwd_entry_i (lgm, lfe);
905         break;
906       }
907   }));
908   /* *INDENT-ON* */
909 }
910
911 static u8 *
912 format_lisp_fwd_path (u8 * s, va_list ap)
913 {
914   lisp_fwd_path_t *lfp = va_arg (ap, lisp_fwd_path_t *);
915
916   s = format (s, "weight:%d ", lfp->weight);
917   s = format (s, "adj:[%U]\n",
918               format_lisp_gpe_adjacency,
919               lisp_gpe_adjacency_get (lfp->lisp_adj),
920               LISP_GPE_ADJ_FORMAT_FLAG_NONE);
921
922   return (s);
923 }
924
925 typedef enum lisp_gpe_fwd_entry_format_flag_t_
926 {
927   LISP_GPE_FWD_ENTRY_FORMAT_NONE = (0 << 0),
928   LISP_GPE_FWD_ENTRY_FORMAT_DETAIL = (1 << 1),
929 } lisp_gpe_fwd_entry_format_flag_t;
930
931
932 static u8 *
933 format_lisp_gpe_fwd_entry (u8 * s, va_list ap)
934 {
935   lisp_gpe_main_t *lgm = &lisp_gpe_main;
936   lisp_gpe_fwd_entry_t *lfe = va_arg (ap, lisp_gpe_fwd_entry_t *);
937   lisp_gpe_fwd_entry_format_flag_t flags =
938     va_arg (ap, lisp_gpe_fwd_entry_format_flag_t);
939
940   s = format (s, "VNI:%d VRF:%d EID: %U -> %U  [index:%d]",
941               lfe->key->vni, lfe->eid_table_id,
942               format_fid_address, &lfe->key->lcl,
943               format_fid_address, &lfe->key->rmt,
944               lfe - lgm->lisp_fwd_entry_pool);
945
946   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
947     {
948       s = format (s, "\n Negative - action:%U",
949                   format_negative_mapping_action, lfe->action);
950     }
951   else
952     {
953       lisp_fwd_path_t *path;
954
955       s = format (s, "\n via:");
956       vec_foreach (path, lfe->paths)
957       {
958         s = format (s, "\n  %U", format_lisp_fwd_path, path);
959       }
960     }
961
962   if (flags & LISP_GPE_FWD_ENTRY_FORMAT_DETAIL)
963     {
964       switch (fid_addr_type (&lfe->key->rmt))
965         {
966         case FID_ADDR_MAC:
967           s = format (s, " fib-path-list:%d\n", lfe->l2.path_list_index);
968           s = format (s, " dpo:%U\n", format_dpo_id, &lfe->l2.dpo, 0);
969           break;
970         case FID_ADDR_IP_PREF:
971           break;
972         }
973     }
974
975   return (s);
976 }
977
978 static clib_error_t *
979 lisp_gpe_fwd_entry_show (vlib_main_t * vm,
980                          unformat_input_t * input, vlib_cli_command_t * cmd)
981 {
982   lisp_gpe_main_t *lgm = &lisp_gpe_main;
983   lisp_gpe_fwd_entry_t *lfe;
984   index_t index;
985   u32 vni = ~0;
986
987   if (unformat (input, "vni %d", &vni))
988     ;
989   else if (unformat (input, "%d", &index))
990     {
991       if (!pool_is_free_index (lgm->lisp_fwd_entry_pool, index))
992         {
993           lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, index);
994
995           vlib_cli_output (vm, "[%d@] %U",
996                            index,
997                            format_lisp_gpe_fwd_entry, lfe,
998                            LISP_GPE_FWD_ENTRY_FORMAT_DETAIL);
999         }
1000       else
1001         {
1002           vlib_cli_output (vm, "entry %d invalid", index);
1003         }
1004
1005       return (NULL);
1006     }
1007
1008   /* *INDENT-OFF* */
1009   pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1010   ({
1011     if ((vni == ~0) ||
1012         (lfe->key->vni == vni))
1013       vlib_cli_output (vm, "%U", format_lisp_gpe_fwd_entry, lfe,
1014                        LISP_GPE_FWD_ENTRY_FORMAT_NONE);
1015   }));
1016   /* *INDENT-ON* */
1017
1018   return (NULL);
1019 }
1020
1021 /* *INDENT-OFF* */
1022 VLIB_CLI_COMMAND (lisp_gpe_fwd_entry_show_command, static) = {
1023   .path = "show lisp gpe entry",
1024   .short_help = "show lisp gpe entry vni <vni> vrf <vrf> [leid <leid>] reid <reid>",
1025   .function = lisp_gpe_fwd_entry_show,
1026 };
1027 /* *INDENT-ON* */
1028
1029 clib_error_t *
1030 lisp_gpe_fwd_entry_init (vlib_main_t * vm)
1031 {
1032   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1033   clib_error_t *error = NULL;
1034
1035   if ((error = vlib_call_init_function (vm, lisp_cp_dpo_module_init)))
1036     return (error);
1037
1038   l2_fib_init (lgm);
1039
1040   fib_node_register_type (FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY, &lisp_fwd_vft);
1041
1042   return (error);
1043 }
1044
1045 VLIB_INIT_FUNCTION (lisp_gpe_fwd_entry_init);
1046
1047 /*
1048  * fd.io coding-style-patch-verification: ON
1049  *
1050  * Local Variables:
1051  * eval: (c-set-style "gnu")
1052  * End:
1053  */