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