L2 over MPLS
[vpp.git] / src / vnet / lisp-gpe / lisp_gpe_fwd_entry.c
index efa724e..ac04814 100644 (file)
@@ -88,6 +88,7 @@ ip_dst_fib_add_route (u32 dst_fib_index, const ip_prefix_t * dst_prefix)
                                          (ip_prefix_version (dst_prefix) ==
                                           IP6 ? DPO_PROTO_IP6 :
                                           DPO_PROTO_IP4),
+                                         LOOKUP_UNICAST,
                                          LOOKUP_INPUT_SRC_ADDR,
                                          LOOKUP_TABLE_FROM_CONFIG,
                                          &src_lkup_dpo);
@@ -224,6 +225,7 @@ lisp_gpe_mk_fib_paths (const lisp_fwd_path_t * paths)
 {
   const lisp_gpe_adjacency_t *ladj;
   fib_route_path_t *rpaths = NULL;
+  fib_protocol_t fp;
   u8 best_priority;
   u32 ii;
 
@@ -238,9 +240,9 @@ lisp_gpe_mk_fib_paths (const lisp_fwd_path_t * paths)
 
     ladj = lisp_gpe_adjacency_get (paths[ii].lisp_adj);
 
-    ip_address_to_46 (&ladj->remote_rloc,
-                     &rpaths[ii].frp_addr, &rpaths[ii].frp_proto);
+    ip_address_to_46 (&ladj->remote_rloc, &rpaths[ii].frp_addr, &fp);
 
+    rpaths[ii].frp_proto = fib_proto_to_dpo (fp);
     rpaths[ii].frp_sw_if_index = ladj->sw_if_index;
     rpaths[ii].frp_weight = (paths[ii].weight ? paths[ii].weight : 1);
   }
@@ -278,14 +280,46 @@ ip_src_fib_add_route (u32 src_fib_index,
   vec_free (rpaths);
 }
 
+static void
+gpe_native_fwd_add_del_lfe (lisp_gpe_fwd_entry_t * lfe, u8 is_add)
+{
+  lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
+  u8 found = 0, ip_version;
+  u32 *lfei, new_lfei;
+  ip_version = ip_prefix_version (&lfe->key->rmt.ippref);
+
+  new_lfei = lfe - lgm->lisp_fwd_entry_pool;
+  vec_foreach (lfei, lgm->native_fwd_lfes[ip_version])
+  {
+    lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, lfei[0]);
+    if (lfei[0] == new_lfei)
+      {
+       found = 1;
+       break;
+      }
+  }
+
+  if (is_add)
+    {
+      if (!found)
+       vec_add1 (lgm->native_fwd_lfes[ip_version], new_lfei);
+    }
+  else
+    {
+      if (found)
+       vec_del1 (lgm->native_fwd_lfes[ip_version], lfei[0]);
+    }
+}
 
 static void
 create_fib_entries (lisp_gpe_fwd_entry_t * lfe)
 {
+  lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
   dpo_proto_t dproto;
   ip_prefix_t ippref;
-  dproto = (ip_prefix_version (&lfe->key->rmt.ippref) == IP4 ?
-           DPO_PROTO_IP4 : DPO_PROTO_IP6);
+  fib_prefix_t fib_prefix;
+  u8 ip_version = ip_prefix_version (&lfe->key->rmt.ippref);
+  dproto = (ip_version == IP4 ? DPO_PROTO_IP4 : DPO_PROTO_IP6);
 
   if (lfe->is_src_dst)
     {
@@ -305,11 +339,19 @@ create_fib_entries (lisp_gpe_fwd_entry_t * lfe)
 
       switch (lfe->action)
        {
+       case LISP_FORWARD_NATIVE:
+         /* TODO handle route overlaps with fib and default route */
+         if (vec_len (lgm->native_fwd_rpath[ip_version]))
+           {
+             ip_prefix_to_fib_prefix (&lfe->key->rmt.ippref, &fib_prefix);
+             fib_table_entry_update (lfe->eid_fib_index, &fib_prefix,
+                                     FIB_SOURCE_LISP, FIB_ENTRY_FLAG_NONE,
+                                     lgm->native_fwd_rpath[ip_version]);
+             gpe_native_fwd_add_del_lfe (lfe, 1);
+             break;
+           }
        case LISP_NO_ACTION:
          /* TODO update timers? */
-       case LISP_FORWARD_NATIVE:
-         /* TODO check if route/next-hop for eid exists in fib and add
-          * more specific for the eid with the next-hop found */
        case LISP_SEND_MAP_REQUEST:
          /* insert tunnel that always sends map-request */
          dpo_copy (&dpo, lisp_cp_dpo_get (dproto));
@@ -342,6 +384,7 @@ delete_fib_entries (lisp_gpe_fwd_entry_t * lfe)
       ip_prefix_to_fib_prefix (&lfe->key->rmt.ippref, &dst_fib_prefix);
       fib_table_entry_delete (lfe->src_fib_index, &dst_fib_prefix,
                              FIB_SOURCE_LISP);
+      gpe_native_fwd_add_del_lfe (lfe, 0);
     }
 }
 
@@ -409,6 +452,46 @@ lisp_gpe_fwd_entry_mk_paths (lisp_gpe_fwd_entry_t * lfe,
   vec_sort_with_function (lfe->paths, lisp_gpe_fwd_entry_path_sort);
 }
 
+void
+vnet_lisp_gpe_add_fwd_counters (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
+                               u32 fwd_entry_index)
+{
+  const lisp_gpe_adjacency_t *ladj;
+  lisp_fwd_path_t *path;
+  lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
+  u8 *dummy_elt;
+  lisp_gpe_fwd_entry_t *lfe;
+  lisp_gpe_fwd_entry_key_t fe_key;
+  lisp_stats_key_t key;
+
+  lfe = find_fwd_entry (lgm, a, &fe_key);
+
+  if (!lfe)
+    return;
+
+  if (LISP_GPE_FWD_ENTRY_TYPE_NORMAL != lfe->type)
+    return;
+
+  memset (&key, 0, sizeof (key));
+  key.fwd_entry_index = fwd_entry_index;
+
+  vec_foreach (path, lfe->paths)
+  {
+    ladj = lisp_gpe_adjacency_get (path->lisp_adj);
+    key.tunnel_index = ladj->tunnel_index;
+    lisp_stats_key_t *key_copy = clib_mem_alloc (sizeof (*key_copy));
+    memcpy (key_copy, &key, sizeof (*key_copy));
+    pool_get (lgm->dummy_stats_pool, dummy_elt);
+    hash_set_mem (lgm->lisp_stats_index_by_key, key_copy,
+                 dummy_elt - lgm->dummy_stats_pool);
+
+    vlib_validate_combined_counter (&lgm->counters,
+                                   dummy_elt - lgm->dummy_stats_pool);
+    vlib_zero_combined_counter (&lgm->counters,
+                               dummy_elt - lgm->dummy_stats_pool);
+  }
+}
+
 /**
  * @brief Add/Delete LISP IP forwarding entry.
  *
@@ -440,6 +523,7 @@ add_ip_fwd_entry (lisp_gpe_main_t * lgm,
 
   hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
                lfe - lgm->lisp_fwd_entry_pool);
+  a->fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
 
   fproto = (IP4 == ip_prefix_version (&fid_addr_ippref (&lfe->key->rmt)) ?
            FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6);
@@ -457,6 +541,10 @@ add_ip_fwd_entry (lisp_gpe_main_t * lgm,
     {
       lisp_gpe_fwd_entry_mk_paths (lfe, a);
     }
+  else
+    {
+      lfe->action = a->action;
+    }
 
   create_fib_entries (lfe);
   return (0);
@@ -468,10 +556,13 @@ del_ip_fwd_entry_i (lisp_gpe_main_t * lgm, lisp_gpe_fwd_entry_t * lfe)
   lisp_fwd_path_t *path;
   fib_protocol_t fproto;
 
-  vec_foreach (path, lfe->paths)
-  {
-    lisp_gpe_adjacency_unlock (path->lisp_adj);
-  }
+  if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE != lfe->type)
+    {
+      vec_foreach (path, lfe->paths)
+      {
+       lisp_gpe_adjacency_unlock (path->lisp_adj);
+      }
+    }
 
   delete_fib_entries (lfe);
 
@@ -746,6 +837,7 @@ add_l2_fwd_entry (lisp_gpe_main_t * lgm,
 
   hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
                lfe - lgm->lisp_fwd_entry_pool);
+  a->fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
 
   lfe->type = (a->is_negative ?
               LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
@@ -1019,6 +1111,7 @@ add_nsh_fwd_entry (lisp_gpe_main_t * lgm,
 
   hash_set_mem (lgm->lisp_gpe_fwd_entries, lfe->key,
                lfe - lgm->lisp_fwd_entry_pool);
+  a->fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
 
   lfe->type = (a->is_negative ?
               LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE :
@@ -1171,19 +1264,17 @@ vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
     }
 }
 
-void
+int
 vnet_lisp_flush_stats (void)
 {
   lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
-  lisp_stats_t *stat;
+  vlib_combined_counter_main_t *cm = &lgm->counters;
+  u32 i;
 
-  /* *INDENT-OFF* */
-  pool_foreach (stat, lgm->lisp_stats_pool,
-  {
-    stat->pkt_count = 0;
-    stat->bytes = 0;
-  });
-  /* *INDENT-ON* */
+  for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
+    vlib_zero_combined_counter (cm, i);
+
+  return 0;
 }
 
 static void
@@ -1193,7 +1284,7 @@ lisp_del_adj_stats (lisp_gpe_main_t * lgm, u32 fwd_entry_index, u32 ti)
   lisp_stats_key_t key;
   void *key_copy;
   uword *p;
-  lisp_stats_t *s;
+  u8 *s;
 
   memset (&key, 0, sizeof (key));
   key.fwd_entry_index = fwd_entry_index;
@@ -1202,18 +1293,18 @@ lisp_del_adj_stats (lisp_gpe_main_t * lgm, u32 fwd_entry_index, u32 ti)
   p = hash_get_mem (lgm->lisp_stats_index_by_key, &key);
   if (p)
     {
-      s = pool_elt_at_index (lgm->lisp_stats_pool, p[0]);
+      s = pool_elt_at_index (lgm->dummy_stats_pool, p[0]);
       hp = hash_get_pair (lgm->lisp_stats_index_by_key, &key);
       key_copy = (void *) (hp->key);
       hash_unset_mem (lgm->lisp_stats_index_by_key, &key);
       clib_mem_free (key_copy);
-      pool_put (lgm->lisp_stats_pool, s);
+      pool_put (lgm->dummy_stats_pool, s);
     }
 }
 
 void
-vnet_lisp_del_fwd_stats (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
-                        u32 fwd_entry_index)
+vnet_lisp_gpe_del_fwd_counters (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
+                               u32 fwd_entry_index)
 {
   lisp_gpe_main_t *lgm = &lisp_gpe_main;
   lisp_gpe_fwd_entry_key_t fe_key;
@@ -1402,6 +1493,23 @@ lisp_gpe_fwd_entry_init (vlib_main_t * vm)
   return (error);
 }
 
+u32 *
+vnet_lisp_gpe_get_fwd_entry_vnis (void)
+{
+  lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
+  lisp_gpe_fwd_entry_t *lfe;
+  u32 *vnis = 0;
+
+  /* *INDENT-OFF* */
+  pool_foreach (lfe, lgm->lisp_fwd_entry_pool,
+  ({
+    hash_set (vnis, lfe->key->vni, 0);
+  }));
+  /* *INDENT-ON* */
+
+  return vnis;
+}
+
 lisp_api_gpe_fwd_entry_t *
 vnet_lisp_gpe_fwd_entries_get_by_vni (u32 vni)
 {
@@ -1417,6 +1525,8 @@ vnet_lisp_gpe_fwd_entries_get_by_vni (u32 vni)
         memset (&e, 0, sizeof (e));
         e.dp_table = lfe->eid_table_id;
         e.vni = lfe->key->vni;
+        if (lfe->type == LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE)
+          e.action = lfe->action;
         e.fwd_entry_index = lfe - lgm->lisp_fwd_entry_pool;
         memcpy (&e.reid, &lfe->key->rmt, sizeof (e.reid));
         memcpy (&e.leid, &lfe->key->lcl, sizeof (e.leid));