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