FIB table add/delete 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                                    FIB_SOURCE_LISP,
70                                    "LISP-src for [%d,%U]",
71                                    dst_fib_index,
72                                    format_fib_prefix, &dst_fib_prefix);
73       /*
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)));
84       /*
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),
92                                           LOOKUP_UNICAST,
93                                           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  */
140 static void
141 ip_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;
146   u8 have_default = 0;
147   u32 n_entries;
148
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
157   /* 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))
170     {
171       /*
172        * remove src FIB default route
173        */
174       if (have_default)
175         fib_table_entry_special_remove (src_fib_index, &default_pref,
176                                         FIB_SOURCE_LISP);
177
178       /*
179        * there's nothing left now, unlock the source FIB and the
180        * destination route
181        */
182       fib_table_entry_special_remove (dst_fib_index,
183                                       &dst_fib_prefix, FIB_SOURCE_LISP);
184       fib_table_unlock (src_fib_index, src_fib_prefix.fp_proto,
185                         FIB_SOURCE_LISP);
186     }
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.
198  *
199  * @return fib index of the inserted prefix
200  */
201 static fib_node_index_t
202 ip_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 {
206   fib_node_index_t fei = ~0;
207   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     {
221       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);
226     }
227   return fei;
228 }
229
230 static fib_route_path_t *
231 lisp_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;
235   fib_protocol_t fp;
236   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
250     ip_address_to_46 (&ladj->remote_rloc, &rpaths[ii].frp_addr, &fp);
251
252     rpaths[ii].frp_proto = fib_proto_to_dpo (fp);
253     rpaths[ii].frp_sw_if_index = ladj->sw_if_index;
254     rpaths[ii].frp_weight = (paths[ii].weight ? paths[ii].weight : 1);
255   }
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  */
272 static fib_node_index_t
273 ip_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
284   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);
287   vec_free (rpaths);
288   return fib_entry_index;
289 }
290
291 static void
292 gpe_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 }
321
322 static index_t
323 create_fib_entries (lisp_gpe_fwd_entry_t * lfe)
324 {
325   fib_node_index_t fi;
326   fib_entry_t *fe;
327   lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
328   dpo_proto_t dproto;
329   ip_prefix_t ippref;
330   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);
333
334   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     }
345
346   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
347     {
348       dpo_id_t dpo = DPO_INVALID;
349
350       switch (lfe->action)
351         {
352         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);
357               fib_table_entry_update (lfe->eid_fib_index, &fib_prefix,
358                                       FIB_SOURCE_LISP, FIB_ENTRY_FLAG_NONE,
359                                       lgm->native_fwd_rpath[ip_version]);
360               gpe_native_fwd_add_del_lfe (lfe, 1);
361               break;
362             }
363         case LISP_NO_ACTION:
364           /* TODO update timers? */
365         case LISP_SEND_MAP_REQUEST:
366           /* insert tunnel that always sends map-request */
367           dpo_copy (&dpo, lisp_cp_dpo_get (dproto));
368           break;
369         case LISP_DROP:
370           /* for drop fwd entries, just add route, no need to add encap tunnel */
371           dpo_copy (&dpo, drop_dpo_get (dproto));
372           break;
373         }
374       fi = ip_src_fib_add_route_w_dpo (lfe->src_fib_index, &ippref, &dpo);
375       dpo_reset (&dpo);
376     }
377   else
378     {
379       fi = ip_src_fib_add_route (lfe->src_fib_index, &ippref, lfe->paths);
380     }
381   fe = fib_entry_get (fi);
382   return fe->fe_lb.dpoi_index;
383 }
384
385 static void
386 delete_fib_entries (lisp_gpe_fwd_entry_t * lfe)
387 {
388   fib_prefix_t dst_fib_prefix;
389
390   if (lfe->is_src_dst)
391     ip_src_dst_fib_del_route (lfe->src_fib_index,
392                               &lfe->key->lcl.ippref,
393                               lfe->eid_fib_index, &lfe->key->rmt.ippref);
394   else
395     {
396       ip_prefix_to_fib_prefix (&lfe->key->rmt.ippref, &dst_fib_prefix);
397       fib_table_entry_delete (lfe->src_fib_index, &dst_fib_prefix,
398                               FIB_SOURCE_LISP);
399       gpe_native_fwd_add_del_lfe (lfe, 0);
400     }
401 }
402
403 static lisp_gpe_fwd_entry_t *
404 find_fwd_entry (lisp_gpe_main_t * lgm,
405                 vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
406                 lisp_gpe_fwd_entry_key_t * key)
407 {
408   uword *p;
409
410   memset (key, 0, sizeof (*key));
411
412   if (GID_ADDR_IP_PREFIX == gid_address_type (&a->rmt_eid))
413     {
414       /*
415        * the ip version of the source is not set to ip6 when the
416        * source is all zeros. force it.
417        */
418       ip_prefix_version (&gid_address_ippref (&a->lcl_eid)) =
419         ip_prefix_version (&gid_address_ippref (&a->rmt_eid));
420     }
421
422   gid_to_dp_address (&a->rmt_eid, &key->rmt);
423   gid_to_dp_address (&a->lcl_eid, &key->lcl);
424   key->vni = a->vni;
425
426   p = hash_get_mem (lgm->lisp_gpe_fwd_entries, key);
427
428   if (NULL != p)
429     {
430       return (pool_elt_at_index (lgm->lisp_fwd_entry_pool, p[0]));
431     }
432   return (NULL);
433 }
434
435 static int
436 lisp_gpe_fwd_entry_path_sort (void *a1, void *a2)
437 {
438   lisp_fwd_path_t *p1 = a1, *p2 = a2;
439
440   return (p1->priority - p2->priority);
441 }
442
443 static void
444 lisp_gpe_fwd_entry_mk_paths (lisp_gpe_fwd_entry_t * lfe,
445                              vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
446 {
447   lisp_fwd_path_t *path;
448   u32 index;
449
450   vec_validate (lfe->paths, vec_len (a->locator_pairs) - 1);
451
452   vec_foreach_index (index, a->locator_pairs)
453   {
454     path = &lfe->paths[index];
455
456     path->priority = a->locator_pairs[index].priority;
457     path->weight = a->locator_pairs[index].weight;
458
459     path->lisp_adj =
460       lisp_gpe_adjacency_find_or_create_and_lock (&a->locator_pairs
461                                                   [index],
462                                                   a->dp_table, lfe->key->vni);
463   }
464   vec_sort_with_function (lfe->paths, lisp_gpe_fwd_entry_path_sort);
465 }
466
467 void
468 vnet_lisp_gpe_add_fwd_counters (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
469                                 u32 fwd_entry_index)
470 {
471   const lisp_gpe_adjacency_t *ladj;
472   lisp_fwd_path_t *path;
473   lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
474   u8 *dummy_elt;
475   lisp_gpe_fwd_entry_t *lfe;
476   lisp_gpe_fwd_entry_key_t fe_key;
477   lisp_stats_key_t key;
478
479   lfe = find_fwd_entry (lgm, a, &fe_key);
480
481   if (!lfe)
482     return;
483
484   if (LISP_GPE_FWD_ENTRY_TYPE_NORMAL != lfe->type)
485     return;
486
487   memset (&key, 0, sizeof (key));
488   key.fwd_entry_index = fwd_entry_index;
489
490   vec_foreach (path, lfe->paths)
491   {
492     ladj = lisp_gpe_adjacency_get (path->lisp_adj);
493     key.tunnel_index = ladj->tunnel_index;
494     lisp_stats_key_t *key_copy = clib_mem_alloc (sizeof (*key_copy));
495     memcpy (key_copy, &key, sizeof (*key_copy));
496     pool_get (lgm->dummy_stats_pool, dummy_elt);
497     hash_set_mem (lgm->lisp_stats_index_by_key, key_copy,
498                   dummy_elt - lgm->dummy_stats_pool);
499
500     vlib_validate_combined_counter (&lgm->counters,
501                                     dummy_elt - lgm->dummy_stats_pool);
502     vlib_zero_combined_counter (&lgm->counters,
503                                 dummy_elt - lgm->dummy_stats_pool);
504   }
505 }
506
507 /**
508  * @brief Add/Delete LISP IP forwarding entry.
509  *
510  * creation of forwarding entries for IP LISP overlay:
511  *
512  * @param[in]   lgm     Reference to @ref lisp_gpe_main_t.
513  * @param[in]   a       Parameters for building the forwarding entry.
514  *
515  * @return 0 on success.
516  */
517 static int
518 add_ip_fwd_entry (lisp_gpe_main_t * lgm,
519                   vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
520 {
521   lisp_gpe_fwd_entry_key_t key;
522   lisp_gpe_fwd_entry_t *lfe;
523   fib_protocol_t fproto;
524
525   lfe = find_fwd_entry (lgm, a, &key);
526
527   if (NULL != lfe)
528     /* don't support updates */
529     return VNET_API_ERROR_INVALID_VALUE;
530
531   pool_get (lgm->lisp_fwd_entry_pool, lfe);
532   memset (lfe, 0, sizeof (*lfe));
533   lfe->key = clib_mem_alloc (sizeof (key));
534   memcpy (lfe->key, &key, sizeof (key));
535
536   hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
537                 lfe - lgm->lisp_fwd_entry_pool);
538   a->fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
539
540   fproto = (IP4 == ip_prefix_version (&fid_addr_ippref (&lfe->key->rmt)) ?
541             FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
542
543   lfe->type = (a->is_negative ?
544                LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
545                LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
546   lfe->tenant = lisp_gpe_tenant_find_or_create (lfe->key->vni);
547   lfe->eid_table_id = a->table_id;
548   lfe->eid_fib_index = fib_table_find_or_create_and_lock (fproto,
549                                                           lfe->eid_table_id,
550                                                           FIB_SOURCE_LISP);
551   lfe->is_src_dst = a->is_src_dst;
552
553   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
554     {
555       lisp_gpe_fwd_entry_mk_paths (lfe, a);
556     }
557   else
558     {
559       lfe->action = a->action;
560     }
561
562   lfe->dpoi_index = create_fib_entries (lfe);
563   return (0);
564 }
565
566 static void
567 del_ip_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
568 {
569   lisp_fwd_path_t *path;
570   fib_protocol_t fproto;
571
572   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
573     {
574       vec_foreach (path, lfe->paths)
575       {
576         lisp_gpe_adjacency_unlock (path->lisp_adj);
577       }
578     }
579
580   delete_fib_entries (lfe);
581
582   fproto = (IP4 == ip_prefix_version (&fid_addr_ippref (&lfe->key->rmt)) ?
583             FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
584   fib_table_unlock (lfe->eid_fib_index, fproto, FIB_SOURCE_LISP);
585
586   hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
587   clib_mem_free (lfe->key);
588   pool_put (lgm->lisp_fwd_entry_pool, lfe);
589 }
590
591 /**
592  * @brief Add/Delete LISP IP forwarding entry.
593  *
594  * removal of forwarding entries for IP LISP overlay:
595  *
596  * @param[in]   lgm     Reference to @ref lisp_gpe_main_t.
597  * @param[in]   a       Parameters for building the forwarding entry.
598  *
599  * @return 0 on success.
600  */
601 static int
602 del_ip_fwd_entry (lisp_gpe_main_t * lgm,
603                   vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
604 {
605   lisp_gpe_fwd_entry_key_t key;
606   lisp_gpe_fwd_entry_t *lfe;
607
608   lfe = find_fwd_entry (lgm, a, &key);
609
610   if (NULL == lfe)
611     /* no such entry */
612     return VNET_API_ERROR_INVALID_VALUE;
613
614   del_ip_fwd_entry_i (lgm, lfe);
615
616   return (0);
617 }
618
619 static void
620 make_mac_fib_key (BVT (clib_bihash_kv) * kv, u16 bd_index, u8 src_mac[6],
621                   u8 dst_mac[6])
622 {
623   kv->key[0] = (((u64) bd_index) << 48) | mac_to_u64 (dst_mac);
624   kv->key[1] = mac_to_u64 (src_mac);
625   kv->key[2] = 0;
626 }
627
628 /**
629  * @brief Lookup L2 SD FIB entry
630  *
631  * Does a vni + dest + source lookup in the L2 LISP FIB. If the lookup fails
632  * it tries a second time with source set to 0 (i.e., a simple dest lookup).
633  *
634  * @param[in]   lgm             Reference to @ref lisp_gpe_main_t.
635  * @param[in]   bd_index        Bridge domain index.
636  * @param[in]   src_mac         Source mac address.
637  * @param[in]   dst_mac         Destination mac address.
638  *
639  * @return index of mapping matching the lookup key.
640  */
641 index_t
642 lisp_l2_fib_lookup (lisp_gpe_main_t * lgm, u16 bd_index, u8 src_mac[6],
643                     u8 dst_mac[6])
644 {
645   int rv;
646   BVT (clib_bihash_kv) kv, value;
647
648   make_mac_fib_key (&kv, bd_index, src_mac, dst_mac);
649   rv = BV (clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
650
651   /* no match, try with src 0, catch all for dst */
652   if (rv != 0)
653     {
654       kv.key[1] = 0;
655       rv = BV (clib_bihash_search_inline_2) (&lgm->l2_fib, &kv, &value);
656       if (rv == 0)
657         return value.value;
658     }
659   else
660     return value.value;
661
662   return lisp_gpe_main.l2_lb_cp_lkup.dpoi_index;
663 }
664
665 /**
666  * @brief Add/del L2 SD FIB entry
667  *
668  * Inserts value in L2 FIB keyed by vni + dest + source. If entry is
669  * overwritten the associated value is returned.
670  *
671  * @param[in]   lgm             Reference to @ref lisp_gpe_main_t.
672  * @param[in]   bd_index        Bridge domain index.
673  * @param[in]   src_mac         Source mac address.
674  * @param[in]   dst_mac         Destination mac address.
675  * @param[in]   val             Value to add.
676  * @param[in]   is_add          Add/del flag.
677  *
678  * @return ~0 or value of overwritten entry.
679  */
680 static u32
681 lisp_l2_fib_add_del_entry (u16 bd_index, u8 src_mac[6],
682                            u8 dst_mac[6], const dpo_id_t * dpo, u8 is_add)
683 {
684   lisp_gpe_main_t *lgm = &lisp_gpe_main;
685   BVT (clib_bihash_kv) kv, value;
686   u32 old_val = ~0;
687
688   make_mac_fib_key (&kv, bd_index, src_mac, dst_mac);
689
690   if (BV (clib_bihash_search) (&lgm->l2_fib, &kv, &value) == 0)
691     old_val = value.value;
692
693   if (!is_add)
694     BV (clib_bihash_add_del) (&lgm->l2_fib, &kv, 0 /* is_add */ );
695   else
696     {
697       kv.value = dpo->dpoi_index;
698       BV (clib_bihash_add_del) (&lgm->l2_fib, &kv, 1 /* is_add */ );
699     }
700   return old_val;
701 }
702
703 #define L2_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
704 #define L2_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
705
706 static void
707 l2_fib_init (lisp_gpe_main_t * lgm)
708 {
709   index_t lbi;
710
711   BV (clib_bihash_init) (&lgm->l2_fib, "l2 fib",
712                          1 << max_log2 (L2_FIB_DEFAULT_HASH_NUM_BUCKETS),
713                          L2_FIB_DEFAULT_HASH_MEMORY_SIZE);
714
715   /*
716    * the result from a 'miss' in a L2 Table
717    */
718   lbi = load_balance_create (1, DPO_PROTO_ETHERNET, 0);
719   load_balance_set_bucket (lbi, 0, lisp_cp_dpo_get (DPO_PROTO_ETHERNET));
720
721   dpo_set (&lgm->l2_lb_cp_lkup, DPO_LOAD_BALANCE, DPO_PROTO_ETHERNET, lbi);
722 }
723
724 static void
725 del_l2_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
726 {
727   lisp_fwd_path_t *path;
728
729   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
730     {
731       vec_foreach (path, lfe->paths)
732       {
733         lisp_gpe_adjacency_unlock (path->lisp_adj);
734       }
735       fib_path_list_child_remove (lfe->l2.path_list_index,
736                                   lfe->l2.child_index);
737     }
738
739   lisp_l2_fib_add_del_entry (lfe->l2.eid_bd_index,
740                              fid_addr_mac (&lfe->key->lcl),
741                              fid_addr_mac (&lfe->key->rmt), NULL, 0);
742
743   hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
744   clib_mem_free (lfe->key);
745   pool_put (lgm->lisp_fwd_entry_pool, lfe);
746 }
747
748 /**
749  * @brief Delete LISP L2 forwarding entry.
750  *
751  * Coordinates the removal of forwarding entries for L2 LISP overlay:
752  *
753  * @param[in]   lgm     Reference to @ref lisp_gpe_main_t.
754  * @param[in]   a       Parameters for building the forwarding entry.
755  *
756  * @return 0 on success.
757  */
758 static int
759 del_l2_fwd_entry (lisp_gpe_main_t * lgm,
760                   vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
761 {
762   lisp_gpe_fwd_entry_key_t key;
763   lisp_gpe_fwd_entry_t *lfe;
764
765   lfe = find_fwd_entry (lgm, a, &key);
766
767   if (NULL == lfe)
768     return VNET_API_ERROR_INVALID_VALUE;
769
770   del_l2_fwd_entry_i (lgm, lfe);
771
772   return (0);
773 }
774
775 /**
776  * @brief Construct and insert the forwarding information used by an L2 entry
777  */
778 static void
779 lisp_gpe_l2_update_fwding (lisp_gpe_fwd_entry_t * lfe)
780 {
781   lisp_gpe_main_t *lgm = &lisp_gpe_main;
782   dpo_id_t dpo = DPO_INVALID;
783
784   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
785     {
786       fib_path_list_contribute_forwarding (lfe->l2.path_list_index,
787                                            FIB_FORW_CHAIN_TYPE_ETHERNET,
788                                            &lfe->l2.dpo);
789       dpo_copy (&dpo, &lfe->l2.dpo);
790     }
791   else
792     {
793       switch (lfe->action)
794         {
795         case SEND_MAP_REQUEST:
796           dpo_copy (&dpo, &lgm->l2_lb_cp_lkup);
797           break;
798         case NO_ACTION:
799         case FORWARD_NATIVE:
800         case DROP:
801           dpo_copy (&dpo, drop_dpo_get (DPO_PROTO_ETHERNET));
802         }
803     }
804
805   /* add entry to l2 lisp fib */
806   lisp_l2_fib_add_del_entry (lfe->l2.eid_bd_index,
807                              fid_addr_mac (&lfe->key->lcl),
808                              fid_addr_mac (&lfe->key->rmt), &dpo, 1);
809   lfe->dpoi_index = dpo.dpoi_index;
810
811   dpo_reset (&dpo);
812 }
813
814 /**
815  * @brief Add LISP L2 forwarding entry.
816  *
817  * Coordinates the creation of forwarding entries for L2 LISP overlay:
818  * creates lisp-gpe tunnel and injects new entry in Source/Dest L2 FIB.
819  *
820  * @param[in]   lgm     Reference to @ref lisp_gpe_main_t.
821  * @param[in]   a       Parameters for building the forwarding entry.
822  *
823  * @return 0 on success.
824  */
825 static int
826 add_l2_fwd_entry (lisp_gpe_main_t * lgm,
827                   vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
828 {
829   lisp_gpe_fwd_entry_key_t key;
830   bd_main_t *bdm = &bd_main;
831   lisp_gpe_fwd_entry_t *lfe;
832   uword *bd_indexp;
833
834   bd_indexp = hash_get (bdm->bd_index_by_bd_id, a->bd_id);
835   if (!bd_indexp)
836     {
837       clib_warning ("bridge domain %d doesn't exist", a->bd_id);
838       return -1;
839     }
840
841   lfe = find_fwd_entry (lgm, a, &key);
842
843   if (NULL != lfe)
844     /* don't support updates */
845     return VNET_API_ERROR_INVALID_VALUE;
846
847   pool_get (lgm->lisp_fwd_entry_pool, lfe);
848   memset (lfe, 0, sizeof (*lfe));
849   lfe->key = clib_mem_alloc (sizeof (key));
850   memcpy (lfe->key, &key, sizeof (key));
851
852   hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
853                 lfe - lgm->lisp_fwd_entry_pool);
854   a->fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
855
856   lfe->type = (a->is_negative ?
857                LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
858                LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
859   lfe->l2.eid_bd_id = a->bd_id;
860   lfe->l2.eid_bd_index = bd_indexp[0];
861   lfe->tenant = lisp_gpe_tenant_find_or_create (lfe->key->vni);
862
863   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
864     {
865       fib_route_path_t *rpaths;
866
867       /*
868        * Make the sorted array of LISP paths with their resp. adjacency
869        */
870       lisp_gpe_fwd_entry_mk_paths (lfe, a);
871
872       /*
873        * From the LISP paths, construct a FIB path list that will
874        * contribute a load-balance.
875        */
876       rpaths = lisp_gpe_mk_fib_paths (lfe->paths);
877
878       lfe->l2.path_list_index =
879         fib_path_list_create (FIB_PATH_LIST_FLAG_NONE, rpaths);
880
881       /*
882        * become a child of the path-list so we receive updates when
883        * its forwarding state changes. this includes an implicit lock.
884        */
885       lfe->l2.child_index =
886         fib_path_list_child_add (lfe->l2.path_list_index,
887                                  FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY,
888                                  lfe - lgm->lisp_fwd_entry_pool);
889     }
890   else
891     {
892       lfe->action = a->action;
893     }
894
895   lisp_gpe_l2_update_fwding (lfe);
896
897   return 0;
898 }
899
900 /**
901  * @brief Lookup NSH SD FIB entry
902  *
903  * Does an SPI+SI lookup in the NSH LISP FIB.
904  *
905  * @param[in]   lgm             Reference to @ref lisp_gpe_main_t.
906  * @param[in]   spi_si          SPI + SI.
907  *
908  * @return next node index.
909  */
910 const dpo_id_t *
911 lisp_nsh_fib_lookup (lisp_gpe_main_t * lgm, u32 spi_si_net_order)
912 {
913   int rv;
914   BVT (clib_bihash_kv) kv, value;
915
916   memset (&kv, 0, sizeof (kv));
917   kv.key[0] = spi_si_net_order;
918   rv = BV (clib_bihash_search_inline_2) (&lgm->nsh_fib, &kv, &value);
919
920   if (rv != 0)
921     {
922       return lgm->nsh_cp_lkup;
923     }
924   else
925     {
926       lisp_gpe_fwd_entry_t *lfe;
927       lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, value.value);
928       return &lfe->nsh.choice;
929     }
930 }
931
932 /**
933  * @brief Add/del NSH FIB entry
934  *
935  * Inserts value in NSH FIB keyed by SPI+SI. If entry is
936  * overwritten the associated value is returned.
937  *
938  * @param[in]   lgm             Reference to @ref lisp_gpe_main_t.
939  * @param[in]   spi_si          SPI + SI.
940  * @param[in]   dpo             Load balanced mapped to SPI + SI
941  *
942  * @return ~0 or value of overwritten entry.
943  */
944 static u32
945 lisp_nsh_fib_add_del_entry (u32 spi_si_host_order, u32 lfei, u8 is_add)
946 {
947   lisp_gpe_main_t *lgm = &lisp_gpe_main;
948   BVT (clib_bihash_kv) kv, value;
949   u32 old_val = ~0;
950
951   memset (&kv, 0, sizeof (kv));
952   kv.key[0] = clib_host_to_net_u32 (spi_si_host_order);
953   kv.value = 0ULL;
954
955   if (BV (clib_bihash_search) (&lgm->nsh_fib, &kv, &value) == 0)
956     old_val = value.value;
957
958   if (!is_add)
959     BV (clib_bihash_add_del) (&lgm->nsh_fib, &kv, 0 /* is_add */ );
960   else
961     {
962       kv.value = lfei;
963       BV (clib_bihash_add_del) (&lgm->nsh_fib, &kv, 1 /* is_add */ );
964     }
965   return old_val;
966 }
967
968 #define NSH_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
969 #define NSH_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
970
971 static void
972 nsh_fib_init (lisp_gpe_main_t * lgm)
973 {
974   BV (clib_bihash_init) (&lgm->nsh_fib, "nsh fib",
975                          1 << max_log2 (NSH_FIB_DEFAULT_HASH_NUM_BUCKETS),
976                          NSH_FIB_DEFAULT_HASH_MEMORY_SIZE);
977
978   /*
979    * the result from a 'miss' in a NSH Table
980    */
981   lgm->nsh_cp_lkup = lisp_cp_dpo_get (DPO_PROTO_NSH);
982 }
983
984 static void
985 del_nsh_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
986 {
987   lisp_fwd_path_t *path;
988
989   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
990     {
991       vec_foreach (path, lfe->paths)
992       {
993         lisp_gpe_adjacency_unlock (path->lisp_adj);
994       }
995       fib_path_list_child_remove (lfe->nsh.path_list_index,
996                                   lfe->nsh.child_index);
997       dpo_reset (&lfe->nsh.choice);
998     }
999
1000   lisp_nsh_fib_add_del_entry (fid_addr_nsh (&lfe->key->rmt), (u32) ~ 0, 0);
1001
1002   hash_unset_mem (lgm->lisp_gpe_fwd_entries, lfe->key);
1003   clib_mem_free (lfe->key);
1004   pool_put (lgm->lisp_fwd_entry_pool, lfe);
1005 }
1006
1007 /**
1008  * @brief Delete LISP NSH forwarding entry.
1009  *
1010  * Coordinates the removal of forwarding entries for NSH LISP overlay:
1011  *
1012  * @param[in]   lgm     Reference to @ref lisp_gpe_main_t.
1013  * @param[in]   a       Parameters for building the forwarding entry.
1014  *
1015  * @return 0 on success.
1016  */
1017 static int
1018 del_nsh_fwd_entry (lisp_gpe_main_t * lgm,
1019                    vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
1020 {
1021   lisp_gpe_fwd_entry_key_t key;
1022   lisp_gpe_fwd_entry_t *lfe;
1023
1024   lfe = find_fwd_entry (lgm, a, &key);
1025
1026   if (NULL == lfe)
1027     return VNET_API_ERROR_INVALID_VALUE;
1028
1029   del_nsh_fwd_entry_i (lgm, lfe);
1030
1031   return (0);
1032 }
1033
1034 /**
1035  * @brief Construct and insert the forwarding information used by an NSH entry
1036  */
1037 static void
1038 lisp_gpe_nsh_update_fwding (lisp_gpe_fwd_entry_t * lfe)
1039 {
1040   lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1041   dpo_id_t dpo = DPO_INVALID;
1042   vnet_hw_interface_t *hi;
1043   uword *hip;
1044
1045   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
1046     {
1047       fib_path_list_contribute_forwarding (lfe->nsh.path_list_index,
1048                                            FIB_FORW_CHAIN_TYPE_NSH,
1049                                            &lfe->nsh.dpo);
1050
1051       /*
1052        * LISP encap is always the same for this SPI+SI so we do that hash now
1053        * and stack on the choice.
1054        */
1055       if (DPO_LOAD_BALANCE == lfe->nsh.dpo.dpoi_type)
1056         {
1057           const dpo_id_t *tmp;
1058           const load_balance_t *lb;
1059           int hash;
1060
1061           lb = load_balance_get (lfe->nsh.dpo.dpoi_index);
1062           hash = fid_addr_nsh (&lfe->key->rmt) % lb->lb_n_buckets;
1063           tmp =
1064             load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
1065
1066           dpo_copy (&dpo, tmp);
1067         }
1068     }
1069   else
1070     {
1071       switch (lfe->action)
1072         {
1073         case SEND_MAP_REQUEST:
1074           dpo_copy (&dpo, lgm->nsh_cp_lkup);
1075           break;
1076         case NO_ACTION:
1077         case FORWARD_NATIVE:
1078         case DROP:
1079           dpo_copy (&dpo, drop_dpo_get (DPO_PROTO_NSH));
1080         }
1081     }
1082
1083   /* We have only one nsh-lisp interface (no NSH virtualization) */
1084   hip = hash_get (lgm->nsh_ifaces.hw_if_index_by_dp_table, 0);
1085   if (hip)
1086     {
1087       hi = vnet_get_hw_interface (lgm->vnet_main, hip[0]);
1088       dpo_stack_from_node (hi->tx_node_index, &lfe->nsh.choice, &dpo);
1089     }
1090   /* add entry to nsh lisp fib */
1091   lisp_nsh_fib_add_del_entry (fid_addr_nsh (&lfe->key->rmt),
1092                               lfe - lgm->lisp_fwd_entry_pool, 1);
1093   dpo_reset (&dpo);
1094
1095 }
1096
1097 /**
1098  * @brief Add LISP NSH forwarding entry.
1099  *
1100  * Coordinates the creation of forwarding entries for L2 LISP overlay:
1101  * creates lisp-gpe tunnel and injects new entry in Source/Dest L2 FIB.
1102  *
1103  * @param[in]   lgm     Reference to @ref lisp_gpe_main_t.
1104  * @param[in]   a       Parameters for building the forwarding entry.
1105  *
1106  * @return 0 on success.
1107  */
1108 static int
1109 add_nsh_fwd_entry (lisp_gpe_main_t * lgm,
1110                    vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
1111 {
1112   lisp_gpe_fwd_entry_key_t key;
1113   lisp_gpe_fwd_entry_t *lfe;
1114
1115   lfe = find_fwd_entry (lgm, a, &key);
1116
1117   if (NULL != lfe)
1118     /* don't support updates */
1119     return VNET_API_ERROR_INVALID_VALUE;
1120
1121   pool_get (lgm->lisp_fwd_entry_pool, lfe);
1122   memset (lfe, 0, sizeof (*lfe));
1123   lfe->key = clib_mem_alloc (sizeof (key));
1124   memcpy (lfe->key, &key, sizeof (key));
1125
1126   hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
1127                 lfe - lgm->lisp_fwd_entry_pool);
1128   a->fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
1129
1130   lfe->type = (a->is_negative ?
1131                LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
1132                LISP_GPE_FWD_ENTRY_TYPE_NORMAL);
1133   lfe->tenant = 0;
1134
1135   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
1136     {
1137       fib_route_path_t *rpaths;
1138
1139       /*
1140        * Make the sorted array of LISP paths with their resp. adjacency
1141        */
1142       lisp_gpe_fwd_entry_mk_paths (lfe, a);
1143
1144       /*
1145        * From the LISP paths, construct a FIB path list that will
1146        * contribute a load-balance.
1147        */
1148       rpaths = lisp_gpe_mk_fib_paths (lfe->paths);
1149
1150       lfe->nsh.path_list_index =
1151         fib_path_list_create (FIB_PATH_LIST_FLAG_NONE, rpaths);
1152
1153       /*
1154        * become a child of the path-list so we receive updates when
1155        * its forwarding state changes. this includes an implicit lock.
1156        */
1157       lfe->nsh.child_index =
1158         fib_path_list_child_add (lfe->nsh.path_list_index,
1159                                  FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY,
1160                                  lfe - lgm->lisp_fwd_entry_pool);
1161     }
1162   else
1163     {
1164       lfe->action = a->action;
1165     }
1166
1167   lisp_gpe_nsh_update_fwding (lfe);
1168
1169   return 0;
1170 }
1171
1172 /**
1173  * @brief conver from the embedded fib_node_t struct to the LSIP entry
1174  */
1175 static lisp_gpe_fwd_entry_t *
1176 lisp_gpe_fwd_entry_from_fib_node (fib_node_t * node)
1177 {
1178   return ((lisp_gpe_fwd_entry_t *) (((char *) node) -
1179                                     STRUCT_OFFSET_OF (lisp_gpe_fwd_entry_t,
1180                                                       node)));
1181 }
1182
1183 /**
1184  * @brief Function invoked during a backwalk of the FIB graph
1185  */
1186 static fib_node_back_walk_rc_t
1187 lisp_gpe_fib_node_back_walk (fib_node_t * node,
1188                              fib_node_back_walk_ctx_t * ctx)
1189 {
1190   lisp_gpe_fwd_entry_t *lfe = lisp_gpe_fwd_entry_from_fib_node (node);
1191
1192   if (fid_addr_type (&lfe->key->rmt) == FID_ADDR_MAC)
1193     lisp_gpe_l2_update_fwding (lfe);
1194   else if (fid_addr_type (&lfe->key->rmt) == FID_ADDR_NSH)
1195     lisp_gpe_nsh_update_fwding (lfe);
1196
1197   return (FIB_NODE_BACK_WALK_CONTINUE);
1198 }
1199
1200 /**
1201  * @brief Get a fib_node_t struct from the index of a LISP fwd entry
1202  */
1203 static fib_node_t *
1204 lisp_gpe_fwd_entry_get_fib_node (fib_node_index_t index)
1205 {
1206   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1207   lisp_gpe_fwd_entry_t *lfe;
1208
1209   lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, index);
1210
1211   return (&(lfe->node));
1212 }
1213
1214 /**
1215  * @brief An indication from the graph that the last lock has gone
1216  */
1217 static void
1218 lisp_gpe_fwd_entry_fib_node_last_lock_gone (fib_node_t * node)
1219 {
1220   /* We don't manage the locks of the LISP objects via the graph, since
1221    * this object has no children. so this is a no-op. */
1222 }
1223
1224 /**
1225  * @brief Virtual function table to register with FIB for the LISP type
1226  */
1227 const static fib_node_vft_t lisp_fwd_vft = {
1228   .fnv_get = lisp_gpe_fwd_entry_get_fib_node,
1229   .fnv_last_lock = lisp_gpe_fwd_entry_fib_node_last_lock_gone,
1230   .fnv_back_walk = lisp_gpe_fib_node_back_walk,
1231 };
1232
1233 /**
1234  * @brief Forwarding entry create/remove dispatcher.
1235  *
1236  * Calls l2 or l3 forwarding entry add/del function based on input data.
1237  *
1238  * @param[in]   a       Forwarding entry parameters.
1239  * @param[out]  hw_if_indexp    NOT USED
1240  *
1241  * @return 0 on success.
1242  */
1243 int
1244 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
1245                                  u32 * hw_if_indexp)
1246 {
1247   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1248   u8 type;
1249
1250   if (vnet_lisp_gpe_enable_disable_status () == 0)
1251     {
1252       clib_warning ("LISP is disabled!");
1253       return VNET_API_ERROR_LISP_DISABLED;
1254     }
1255
1256   type = gid_address_type (&a->rmt_eid);
1257   switch (type)
1258     {
1259     case GID_ADDR_IP_PREFIX:
1260       if (a->is_add)
1261         return add_ip_fwd_entry (lgm, a);
1262       else
1263         return del_ip_fwd_entry (lgm, a);
1264       break;
1265     case GID_ADDR_MAC:
1266       if (a->is_add)
1267         return add_l2_fwd_entry (lgm, a);
1268       else
1269         return del_l2_fwd_entry (lgm, a);
1270     case GID_ADDR_NSH:
1271       if (a->is_add)
1272         return add_nsh_fwd_entry (lgm, a);
1273       else
1274         return del_nsh_fwd_entry (lgm, a);
1275     default:
1276       clib_warning ("Forwarding entries for type %d not supported!", type);
1277       return -1;
1278     }
1279 }
1280
1281 int
1282 vnet_lisp_flush_stats (void)
1283 {
1284   lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1285   vlib_combined_counter_main_t *cm = &lgm->counters;
1286   u32 i;
1287
1288   for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
1289     vlib_zero_combined_counter (cm, i);
1290
1291   return 0;
1292 }
1293
1294 static void
1295 lisp_del_adj_stats (lisp_gpe_main_t * lgm, u32 fwd_entry_index, u32 ti)
1296 {
1297   hash_pair_t *hp;
1298   lisp_stats_key_t key;
1299   void *key_copy;
1300   uword *p;
1301   u8 *s;
1302
1303   memset (&key, 0, sizeof (key));
1304   key.fwd_entry_index = fwd_entry_index;
1305   key.tunnel_index = ti;
1306
1307   p = hash_get_mem (lgm->lisp_stats_index_by_key, &key);
1308   if (p)
1309     {
1310       s = pool_elt_at_index (lgm->dummy_stats_pool, p[0]);
1311       hp = hash_get_pair (lgm->lisp_stats_index_by_key, &key);
1312       key_copy = (void *) (hp->key);
1313       hash_unset_mem (lgm->lisp_stats_index_by_key, &key);
1314       clib_mem_free (key_copy);
1315       pool_put (lgm->dummy_stats_pool, s);
1316     }
1317 }
1318
1319 void
1320 vnet_lisp_gpe_del_fwd_counters (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
1321                                 u32 fwd_entry_index)
1322 {
1323   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1324   lisp_gpe_fwd_entry_key_t fe_key;
1325   lisp_gpe_fwd_entry_t *lfe;
1326   lisp_fwd_path_t *path;
1327   const lisp_gpe_adjacency_t *ladj;
1328
1329   lfe = find_fwd_entry (lgm, a, &fe_key);
1330   if (!lfe)
1331     return;
1332
1333   if (LISP_GPE_FWD_ENTRY_TYPE_NORMAL != lfe->type)
1334     return;
1335
1336   vec_foreach (path, lfe->paths)
1337   {
1338     ladj = lisp_gpe_adjacency_get (path->lisp_adj);
1339     lisp_del_adj_stats (lgm, fwd_entry_index, ladj->tunnel_index);
1340   }
1341 }
1342
1343 /**
1344  * @brief Flush all the forwrding entries
1345  */
1346 void
1347 vnet_lisp_gpe_fwd_entry_flush (void)
1348 {
1349   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1350   lisp_gpe_fwd_entry_t *lfe;
1351
1352   /* *INDENT-OFF* */
1353   pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1354   ({
1355     switch (fid_addr_type(&lfe->key->rmt))
1356       {
1357       case FID_ADDR_MAC:
1358         del_l2_fwd_entry_i (lgm, lfe);
1359         break;
1360       case FID_ADDR_IP_PREF:
1361         del_ip_fwd_entry_i (lgm, lfe);
1362         break;
1363       case FID_ADDR_NSH:
1364         del_nsh_fwd_entry_i (lgm, lfe);
1365         break;
1366       }
1367   }));
1368   /* *INDENT-ON* */
1369 }
1370
1371 static u8 *
1372 format_lisp_fwd_path (u8 * s, va_list ap)
1373 {
1374   lisp_fwd_path_t *lfp = va_arg (ap, lisp_fwd_path_t *);
1375
1376   s = format (s, "weight:%d ", lfp->weight);
1377   s = format (s, "adj:[%U]\n",
1378               format_lisp_gpe_adjacency,
1379               lisp_gpe_adjacency_get (lfp->lisp_adj),
1380               LISP_GPE_ADJ_FORMAT_FLAG_NONE);
1381
1382   return (s);
1383 }
1384
1385 typedef enum lisp_gpe_fwd_entry_format_flag_t_
1386 {
1387   LISP_GPE_FWD_ENTRY_FORMAT_NONE = (0 << 0),
1388   LISP_GPE_FWD_ENTRY_FORMAT_DETAIL = (1 << 1),
1389 } lisp_gpe_fwd_entry_format_flag_t;
1390
1391
1392 static u8 *
1393 format_lisp_gpe_fwd_entry (u8 * s, va_list ap)
1394 {
1395   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1396   lisp_gpe_fwd_entry_t *lfe = va_arg (ap, lisp_gpe_fwd_entry_t *);
1397   lisp_gpe_fwd_entry_format_flag_t flags =
1398     va_arg (ap, lisp_gpe_fwd_entry_format_flag_t);
1399
1400   s = format (s, "VNI:%d VRF:%d EID: %U -> %U  [index:%d]",
1401               lfe->key->vni, lfe->eid_table_id,
1402               format_fid_address, &lfe->key->lcl,
1403               format_fid_address, &lfe->key->rmt,
1404               lfe - lgm->lisp_fwd_entry_pool);
1405
1406   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
1407     {
1408       s = format (s, "\n Negative - action:%U",
1409                   format_negative_mapping_action, lfe->action);
1410     }
1411   else
1412     {
1413       lisp_fwd_path_t *path;
1414
1415       s = format (s, "\n via:");
1416       vec_foreach (path, lfe->paths)
1417       {
1418         s = format (s, "\n  %U", format_lisp_fwd_path, path);
1419       }
1420     }
1421
1422   if (flags & LISP_GPE_FWD_ENTRY_FORMAT_DETAIL)
1423     {
1424       switch (fid_addr_type (&lfe->key->rmt))
1425         {
1426         case FID_ADDR_MAC:
1427           s = format (s, " fib-path-list:%d\n", lfe->l2.path_list_index);
1428           s = format (s, " dpo:%U\n", format_dpo_id, &lfe->l2.dpo, 0);
1429           break;
1430         case FID_ADDR_NSH:
1431           s = format (s, " fib-path-list:%d\n", lfe->nsh.path_list_index);
1432           s = format (s, " dpo:%U\n", format_dpo_id, &lfe->nsh.dpo, 0);
1433           break;
1434         case FID_ADDR_IP_PREF:
1435           break;
1436         }
1437     }
1438
1439   return (s);
1440 }
1441
1442 static clib_error_t *
1443 lisp_gpe_fwd_entry_show (vlib_main_t * vm,
1444                          unformat_input_t * input, vlib_cli_command_t * cmd)
1445 {
1446   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1447   lisp_gpe_fwd_entry_t *lfe;
1448   index_t index;
1449   u32 vni = ~0;
1450
1451   if (unformat (input, "vni %d", &vni))
1452     ;
1453   else if (unformat (input, "%d", &index))
1454     {
1455       if (!pool_is_free_index (lgm->lisp_fwd_entry_pool, index))
1456         {
1457           lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, index);
1458
1459           vlib_cli_output (vm, "[%d@] %U",
1460                            index,
1461                            format_lisp_gpe_fwd_entry, lfe,
1462                            LISP_GPE_FWD_ENTRY_FORMAT_DETAIL);
1463         }
1464       else
1465         {
1466           vlib_cli_output (vm, "entry %d invalid", index);
1467         }
1468
1469       return (NULL);
1470     }
1471
1472   /* *INDENT-OFF* */
1473   pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1474   ({
1475     if ((vni == ~0) ||
1476         (lfe->key->vni == vni))
1477       vlib_cli_output (vm, "%U", format_lisp_gpe_fwd_entry, lfe,
1478                        LISP_GPE_FWD_ENTRY_FORMAT_NONE);
1479   }));
1480   /* *INDENT-ON* */
1481
1482   return (NULL);
1483 }
1484
1485 /* *INDENT-OFF* */
1486 VLIB_CLI_COMMAND (lisp_gpe_fwd_entry_show_command, static) = {
1487   .path = "show gpe entry",
1488   .short_help = "show gpe entry vni <vni> vrf <vrf> [leid <leid>] reid <reid>",
1489   .function = lisp_gpe_fwd_entry_show,
1490 };
1491 /* *INDENT-ON* */
1492
1493 clib_error_t *
1494 lisp_gpe_fwd_entry_init (vlib_main_t * vm)
1495 {
1496   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1497   clib_error_t *error = NULL;
1498
1499   if ((error = vlib_call_init_function (vm, lisp_cp_dpo_module_init)))
1500     return (error);
1501
1502   l2_fib_init (lgm);
1503   nsh_fib_init (lgm);
1504
1505   fib_node_register_type (FIB_NODE_TYPE_LISP_GPE_FWD_ENTRY, &lisp_fwd_vft);
1506
1507   return (error);
1508 }
1509
1510 u32 *
1511 vnet_lisp_gpe_get_fwd_entry_vnis (void)
1512 {
1513   lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1514   lisp_gpe_fwd_entry_t *lfe;
1515   u32 *vnis = 0;
1516
1517   /* *INDENT-OFF* */
1518   pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1519   ({
1520     hash_set (vnis, lfe->key->vni, 0);
1521   }));
1522   /* *INDENT-ON* */
1523
1524   return vnis;
1525 }
1526
1527 lisp_api_gpe_fwd_entry_t *
1528 vnet_lisp_gpe_fwd_entries_get_by_vni (u32 vni)
1529 {
1530   lisp_gpe_main_t *lgm = &lisp_gpe_main;
1531   lisp_gpe_fwd_entry_t *lfe;
1532   lisp_api_gpe_fwd_entry_t *entries = 0, e;
1533
1534   /* *INDENT-OFF* */
1535   pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
1536   ({
1537     if (lfe->key->vni == vni)
1538       {
1539         memset (&e, 0, sizeof (e));
1540         e.dp_table = lfe->eid_table_id;
1541         e.vni = lfe->key->vni;
1542         if (lfe->type == LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE)
1543           e.action = lfe->action;
1544         e.fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
1545         memcpy (&e.reid, &lfe->key->rmt, sizeof (e.reid));
1546         memcpy (&e.leid, &lfe->key->lcl, sizeof (e.leid));
1547         vec_add1 (entries, e);
1548       }
1549   }));
1550   /* *INDENT-ON* */
1551
1552   return entries;
1553 }
1554
1555 int
1556 vnet_lisp_gpe_get_fwd_stats (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
1557                              vlib_counter_t * c)
1558 {
1559   lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
1560   lisp_gpe_fwd_entry_t *lfe;
1561   lisp_gpe_fwd_entry_key_t unused;
1562
1563   lfe = find_fwd_entry (lgm, a, &unused);
1564   if (NULL == lfe)
1565     return -1;
1566
1567   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
1568     return -1;
1569
1570   if (~0 == lfe->dpoi_index)
1571     return -1;
1572
1573   vlib_get_combined_counter (&load_balance_main.lbm_to_counters,
1574                              lfe->dpoi_index, c);
1575   return 0;
1576 }
1577
1578 VLIB_INIT_FUNCTION (lisp_gpe_fwd_entry_init);
1579
1580 /*
1581  * fd.io coding-style-patch-verification: ON
1582  *
1583  * Local Variables:
1584  * eval: (c-set-style "gnu")
1585  * End:
1586  */