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