GBP: redirect contracts
[vpp.git] / src / plugins / gbp / gbp_endpoint.c
index 47adbb5..e9ced3f 100644 (file)
@@ -31,6 +31,7 @@
 #include <vnet/l2/l2_fib.h>
 #include <vnet/fib/fib_table.h>
 #include <vnet/ip/ip_neighbor.h>
+#include <vnet/fib/fib_walk.h>
 
 static const char *gbp_endpoint_attr_names[] = GBP_ENDPOINT_ATTR_NAMES;
 
@@ -39,6 +40,8 @@ static const char *gbp_endpoint_attr_names[] = GBP_ENDPOINT_ATTR_NAMES;
  */
 gbp_ep_db_t gbp_ep_db;
 
+fib_node_type_t gbp_endpoint_fib_type;
+
 vlib_log_class_t gbp_ep_logger;
 
 #define GBP_ENDPOINT_DBG(...)                           \
@@ -89,7 +92,7 @@ format_gbp_endpoint_flags (u8 * s, va_list * args)
 int
 gbp_endpoint_is_remote (const gbp_endpoint_t * ge)
 {
-  return (ge->ge_flags & GBP_ENDPOINT_FLAG_REMOTE);
+  return (ge->ge_fwd.gef_flags & GBP_ENDPOINT_FLAG_REMOTE);
 }
 
 static void
@@ -194,264 +197,522 @@ ip46_address_get_type (const ip46_address_t * a)
   return (ip46_address_is_ip4 (a) ? IP46_TYPE_IP4 : IP46_TYPE_IP6);
 }
 
-static ip46_type_t
-ip46_address_get_len (const ip46_address_t * a)
+static int
+gbp_endpoint_ip_is_equal (const fib_prefix_t * fp, const ip46_address_t * ip)
 {
-  return (ip46_address_is_ip4 (a) ? 32 : 128);
+  return (ip46_address_is_equal (ip, &fp->fp_addr));
+}
+
+static void
+gbp_endpoint_ips_update (gbp_endpoint_t * ge,
+                        const ip46_address_t * ips,
+                        const gbp_route_domain_t * grd)
+{
+  const ip46_address_t *ip;
+  index_t gei, grdi;
+
+  gei = gbp_endpoint_index (ge);
+  grdi = gbp_route_domain_index (grd);
+
+  ASSERT ((ge->ge_key.gek_grd == INDEX_INVALID) ||
+         (ge->ge_key.gek_grd == grdi));
+
+  vec_foreach (ip, ips)
+  {
+    if (~0 == vec_search_with_function (ge->ge_key.gek_ips, ip,
+                                       gbp_endpoint_ip_is_equal))
+      {
+       fib_prefix_t *pfx;
+
+       vec_add2 (ge->ge_key.gek_ips, pfx, 1);
+       fib_prefix_from_ip46_addr (ip, pfx);
+
+       gbp_endpoint_add_ip (&pfx->fp_addr,
+                            grd->grd_fib_index[pfx->fp_proto], gei);
+      }
+    ge->ge_key.gek_grd = grdi;
+  }
 }
 
 static gbp_endpoint_t *
-gbp_endpoint_alloc (epg_id_t epg_id,
-                   index_t ggi, u32 sw_if_index, gbp_endpoint_flags_t flags,
-                   const ip46_address_t * tun_src,
-                   const ip46_address_t * tun_dst)
+gbp_endpoint_alloc (const ip46_address_t * ips,
+                   const gbp_route_domain_t * grd,
+                   const mac_address_t * mac,
+                   const gbp_bridge_domain_t * gbd)
 {
   gbp_endpoint_t *ge;
+  index_t gei;
 
   pool_get_zero (gbp_endpoint_pool, ge);
 
-  ge->ge_epg = ggi;
-  ge->ge_epg_id = epg_id;
-  ge->ge_flags = flags;
-  ge->ge_sw_if_index = sw_if_index;
+  fib_node_init (&ge->ge_node, gbp_endpoint_fib_type);
+  gei = gbp_endpoint_index (ge);
+  ge->ge_key.gek_gbd =
+    ge->ge_key.gek_grd = ge->ge_fwd.gef_itf = INDEX_INVALID;
   ge->ge_last_time = vlib_time_now (vlib_get_main ());
+  ge->ge_key.gek_gbd = gbp_bridge_domain_index (gbd);
+
+  if (NULL != mac)
+    {
+      mac_address_copy (&ge->ge_key.gek_mac, mac);
+      gbp_endpoint_add_mac (mac, gbd->gb_bd_index, gei);
+    }
+  gbp_endpoint_ips_update (ge, ips, grd);
+
+  return (ge);
+}
+
+static int
+gbp_endpoint_loc_is_equal (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
+{
+  return (a->gel_src == b->gel_src);
+}
+
+static int
+gbp_endpoint_loc_cmp_for_sort (gbp_endpoint_loc_t * a, gbp_endpoint_loc_t * b)
+{
+  return (a->gel_src - b->gel_src);
+}
+
+static gbp_endpoint_loc_t *
+gbp_endpoint_loc_find (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
+{
+  gbp_endpoint_loc_t gel = {
+    .gel_src = src,
+  };
+  u32 pos;
+
+  pos = vec_search_with_function (ge->ge_locs, &gel,
+                                 gbp_endpoint_loc_is_equal);
+
+  if (~0 != pos)
+    return (&ge->ge_locs[pos]);
+
+  return NULL;
+}
+
+static int
+gbp_endpoint_loc_unlock (gbp_endpoint_t * ge, gbp_endpoint_loc_t * gel)
+{
+  u32 pos;
+
+  gel->gel_locks--;
+
+  if (0 == gel->gel_locks)
+    {
+      pos = gel - ge->ge_locs;
+
+      vec_del1 (ge->ge_locs, pos);
+      if (vec_len (ge->ge_locs) > 1)
+       vec_sort_with_function (ge->ge_locs, gbp_endpoint_loc_cmp_for_sort);
+
+      /* This could be the last lock, so don't access the EP from
+       * this point on */
+      fib_node_unlock (&ge->ge_node);
+
+      return (1);
+    }
+  return (0);
+}
+
+static void
+gbp_endpoint_loc_destroy (gbp_endpoint_loc_t * gel)
+{
+  gbp_endpoint_group_unlock (gel->gel_epg);
+
+  if (gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE)
+    {
+      vxlan_gbp_tunnel_unlock (gel->gel_sw_if_index);
+    }
+}
+
+static gbp_endpoint_loc_t *
+gbp_endpoint_loc_find_or_add (gbp_endpoint_t * ge, gbp_endpoint_src_t src)
+{
+  gbp_endpoint_loc_t gel = {
+    .gel_src = src,
+    .gel_epg = INDEX_INVALID,
+    .gel_sw_if_index = INDEX_INVALID,
+    .gel_locks = 0,
+  };
+  u32 pos;
+
+  pos = vec_search_with_function (ge->ge_locs, &gel,
+                                 gbp_endpoint_loc_is_equal);
+
+  if (~0 == pos)
+    {
+      vec_add1 (ge->ge_locs, gel);
+
+      if (vec_len (ge->ge_locs) > 1)
+       {
+         vec_sort_with_function (ge->ge_locs, gbp_endpoint_loc_cmp_for_sort);
+
+         pos = vec_search_with_function (ge->ge_locs, &gel,
+                                         gbp_endpoint_loc_is_equal);
+       }
+      else
+       pos = 0;
+
+      /*
+       * it's the sources and children that lock the endpoints
+       */
+      fib_node_lock (&ge->ge_node);
+    }
+
+  return (&ge->ge_locs[pos]);
+}
+
+/**
+ * Find an EP inthe DBs and check that if we find it in the L2 DB
+ * it has the same IPs as this update
+ */
+static int
+gbp_endpoint_find_for_update (const ip46_address_t * ips,
+                             const gbp_route_domain_t * grd,
+                             const mac_address_t * mac,
+                             const gbp_bridge_domain_t * gbd,
+                             gbp_endpoint_t ** ge)
+{
+  gbp_endpoint_t *l2_ge, *l3_ge, *tmp;
+
+  l2_ge = l3_ge = NULL;
+
+  if (NULL != mac && !mac_address_is_zero (mac))
+    {
+      ASSERT (gbd);
+      l2_ge = gbp_endpoint_find_mac (mac->bytes, gbd->gb_bd_index);
+    }
+  if (NULL != ips && !ip46_address_is_zero (ips))
+    {
+      const ip46_address_t *ip;
+      fib_protocol_t fproto;
+
+      ASSERT (grd);
+      vec_foreach (ip, ips)
+      {
+       fproto = fib_proto_from_ip46 (ip46_address_get_type (ip));
+
+       tmp = gbp_endpoint_find_ip (ip, grd->grd_fib_index[fproto]);
+
+       if (NULL == tmp)
+         /* not found */
+         continue;
+       else if (NULL == l3_ge)
+         /* first match against an IP address */
+         l3_ge = tmp;
+       else if (tmp == l3_ge)
+         /* another match against IP address that is the same endpoint */
+         continue;
+       else
+         {
+           /*
+            *  a match agains a different endpoint.
+            * this means the KEY of the EP is changing which is not allowed
+            */
+           return (-1);
+         }
+      }
+    }
+
+  if (NULL == l2_ge && NULL == l3_ge)
+    /* not found */
+    *ge = NULL;
+  else if (NULL == l2_ge)
+    /* found at L3 */
+    *ge = l3_ge;
+  else if (NULL == l3_ge)
+    /* found at L2 */
+    *ge = l2_ge;
+  else
+    {
+      /* found both L3 and L2 - they must be the same else the KEY
+       * is changing
+       */
+      if (l2_ge == l3_ge)
+       *ge = l2_ge;
+      else
+       return (-1);
+    }
+
+  return (0);
+}
 
-  gbp_endpoint_group_find_and_lock (epg_id);
+static gbp_endpoint_src_t
+gbp_endpoint_get_best_src (const gbp_endpoint_t * ge)
+{
+  if (0 == vec_len (ge->ge_locs))
+    return (GBP_ENDPOINT_SRC_MAX);
+
+  return (ge->ge_locs[0].gel_src);
+}
+
+static void
+gbp_endpoint_n_learned (int n)
+{
+  gbp_n_learnt_endpoints += n;
+
+  if (n > 0 && 1 == gbp_n_learnt_endpoints)
+    {
+      vlib_process_signal_event (vlib_get_main (),
+                                gbp_scanner_node.index,
+                                GBP_ENDPOINT_SCAN_START, 0);
+    }
+  if (n < 0 && 0 == gbp_n_learnt_endpoints)
+    {
+      vlib_process_signal_event (vlib_get_main (),
+                                gbp_scanner_node.index,
+                                GBP_ENDPOINT_SCAN_STOP, 0);
+    }
+}
+
+static void
+gbp_endpoint_loc_update (gbp_endpoint_loc_t * gel,
+                        u32 sw_if_index,
+                        index_t ggi,
+                        gbp_endpoint_flags_t flags,
+                        const ip46_address_t * tun_src,
+                        const ip46_address_t * tun_dst)
+{
+  int was_learnt, is_learnt;
+
+  gel->gel_locks++;
+  was_learnt = ! !(gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE);
+  gel->gel_flags = flags;
+  is_learnt = ! !(gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE);
+
+  gbp_endpoint_n_learned (is_learnt - was_learnt);
 
-  if (gbp_endpoint_is_remote (ge))
+  if (INDEX_INVALID == gel->gel_epg)
+    {
+      gel->gel_epg = ggi;
+      if (INDEX_INVALID != gel->gel_epg)
+       {
+         gbp_endpoint_group_lock (gel->gel_epg);
+       }
+    }
+  else
+    {
+      ASSERT (gel->gel_epg == ggi);
+    }
+
+  if (gel->gel_flags & GBP_ENDPOINT_FLAG_REMOTE)
     {
       if (NULL != tun_src)
-       ip46_address_copy (&ge->tun.ge_src, tun_src);
+       ip46_address_copy (&gel->tun.gel_src, tun_src);
       if (NULL != tun_dst)
-       ip46_address_copy (&ge->tun.ge_dst, tun_dst);
+       ip46_address_copy (&gel->tun.gel_dst, tun_dst);
 
       /*
        * the input interface may be the parent GBP-vxlan interface,
        * create a child vlxan-gbp tunnel and use that as the endpoint's
        * interface.
        */
+      if (~0 != gel->gel_sw_if_index)
+       vxlan_gbp_tunnel_unlock (gel->gel_sw_if_index);
+
       switch (gbp_vxlan_tunnel_get_type (sw_if_index))
        {
        case GBP_VXLAN_TEMPLATE_TUNNEL:
-         ge->tun.ge_parent_sw_if_index = sw_if_index;
-         ge->ge_sw_if_index =
+         gel->tun.gel_parent_sw_if_index = sw_if_index;
+         gel->gel_sw_if_index =
            gbp_vxlan_tunnel_clone_and_lock (sw_if_index,
-                                            &ge->tun.ge_src,
-                                            &ge->tun.ge_dst);
+                                            &gel->tun.gel_src,
+                                            &gel->tun.gel_dst);
          break;
        case VXLAN_GBP_TUNNEL:
-         ge->tun.ge_parent_sw_if_index =
+         gel->tun.gel_parent_sw_if_index =
            vxlan_gbp_tunnel_get_parent (sw_if_index);
-         ge->ge_sw_if_index = sw_if_index;
-         vxlan_gbp_tunnel_lock (ge->ge_sw_if_index);
+         gel->gel_sw_if_index = sw_if_index;
+         vxlan_gbp_tunnel_lock (gel->gel_sw_if_index);
          break;
        }
     }
-
-  return (ge);
+  else
+    {
+      gel->gel_sw_if_index = sw_if_index;
+    }
 }
 
-int
-gbp_endpoint_update (u32 sw_if_index,
-                    const ip46_address_t * ips,
-                    const mac_address_t * mac,
-                    epg_id_t epg_id,
-                    gbp_endpoint_flags_t flags,
-                    const ip46_address_t * tun_src,
-                    const ip46_address_t * tun_dst, u32 * handle)
+static void
+gbb_endpoint_fwd_reset (gbp_endpoint_t * ge)
 {
-  gbp_endpoint_group_t *gg;
-  gbp_endpoint_t *ge;
-  index_t ggi, gei;
+  const gbp_route_domain_t *grd;
+  const gbp_bridge_domain_t *gbd;
+  gbp_endpoint_fwd_t *gef;
+  const fib_prefix_t *pfx;
+  index_t *ai;
+  index_t gei;
+
+  gei = gbp_endpoint_index (ge);
+  gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
+  gef = &ge->ge_fwd;
+
+  vec_foreach (pfx, ge->ge_key.gek_ips)
+  {
+    u32 fib_index;
 
-  if (~0 == sw_if_index)
-    return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
+    grd = gbp_route_domain_get (ge->ge_key.gek_grd);
+    fib_index = grd->grd_fib_index[pfx->fp_proto];
 
-  ge = NULL;
-  ggi = gbp_endpoint_group_find_and_lock (epg_id);
+    bd_add_del_ip_mac (gbd->gb_bd_index, fib_proto_to_ip46 (pfx->fp_proto),
+                      &pfx->fp_addr, &ge->ge_key.gek_mac, 0);
 
-  if (INDEX_INVALID == ggi)
-    return (VNET_API_ERROR_NO_SUCH_ENTRY);
+    /*
+     * remove a host route
+     */
+    if (gbp_endpoint_is_remote (ge))
+      {
+       fib_table_entry_special_remove (fib_index, pfx, FIB_SOURCE_PLUGIN_HI);
+      }
 
-  gg = gbp_endpoint_group_get (ggi);
+    fib_table_entry_delete (fib_index, pfx, FIB_SOURCE_PLUGIN_LOW);
+  }
+  vec_foreach (ai, gef->gef_adjs)
+  {
+    adj_unlock (*ai);
+  }
 
-  /*
-   * L2 EP
-   */
-  if (NULL != mac && !mac_address_is_zero (mac))
+  if (INDEX_INVALID != gef->gef_itf)
     {
-      /*
-       * find an existing endpoint matching one of the key types
-       */
-      ge = gbp_endpoint_find_mac (mac->bytes, gg->gg_bd_index);
-      if (NULL == ge)
-       {
-         /*
-          * new entry
-          */
-         ge = gbp_endpoint_alloc (epg_id, ggi, sw_if_index, flags,
-                                  tun_src, tun_dst);
-         gei = gbp_endpoint_index (ge);
-         mac_address_copy (&ge->ge_mac, mac);
-
-         ge->ge_itf = gbp_itf_add_and_lock (ge->ge_sw_if_index,
-                                            gg->gg_bd_index);
-
-         gbp_itf_set_l2_input_feature (ge->ge_itf, gei,
-                                       L2INPUT_FEAT_GBP_FWD);
+      l2fib_del_entry (ge->ge_key.gek_mac.bytes,
+                      gbd->gb_bd_index, gef->gef_itf);
+      gbp_itf_set_l2_input_feature (gef->gef_itf, gei, (L2INPUT_FEAT_NONE));
+      gbp_itf_set_l2_output_feature (gef->gef_itf, gei, L2OUTPUT_FEAT_NONE);
 
-         if (gbp_endpoint_is_remote (ge))
-           {
-             gbp_itf_set_l2_output_feature (ge->ge_itf, gei,
-                                            L2OUTPUT_FEAT_GBP_POLICY_MAC);
-           }
-         else
-           {
-             gbp_endpoint_add_itf (ge->ge_sw_if_index, gei);
-             gbp_itf_set_l2_output_feature (ge->ge_itf, gei,
-                                            L2OUTPUT_FEAT_GBP_POLICY_PORT);
-           }
+      gbp_itf_unlock (gef->gef_itf);
+      gef->gef_itf = INDEX_INVALID;
+    }
 
-         gbp_endpoint_add_mac (mac, gg->gg_bd_index, gei);
+  vec_free (gef->gef_adjs);
+}
 
-         l2fib_add_entry (mac->bytes, gg->gg_bd_index, ge->ge_sw_if_index,
-                          L2FIB_ENTRY_RESULT_FLAG_STATIC);
-       }
-      else
-       {
-         /*
-          * update existing entry..
-          */
-         ge->ge_flags = flags;
-         gei = gbp_endpoint_index (ge);
-         goto out;
-       }
-    }
+static void
+gbb_endpoint_fwd_recalc (gbp_endpoint_t * ge)
+{
+  const gbp_route_domain_t *grd;
+  const gbp_bridge_domain_t *gbd;
+  const gbp_endpoint_group_t *gg;
+  gbp_endpoint_loc_t *gel;
+  gbp_endpoint_fwd_t *gef;
+  const fib_prefix_t *pfx;
+  index_t gei;
 
   /*
-   * L3 EP
+   * locations are sort in source priority order
    */
-  if (NULL != ips && !ip46_address_is_zero (ips))
+  gei = gbp_endpoint_index (ge);
+  gel = &ge->ge_locs[0];
+  gef = &ge->ge_fwd;
+  gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
+
+  gef->gef_flags = gel->gel_flags;
+
+  if (INDEX_INVALID != gel->gel_epg)
     {
-      const ip46_address_t *ip;
-      fib_protocol_t fproto;
-      gbp_endpoint_t *l3_ge;
-      u32 ii;
+      gg = gbp_endpoint_group_get (gel->gel_epg);
+      gef->gef_epg_id = gg->gg_id;
+    }
+  else
+    {
+      gg = NULL;
+    }
 
-      /*
-       * look for a matching EP by any of the address
-       * An EP's IP addresses cannot change so we can search based on
-       * the first
-       */
-      fproto = fib_proto_from_ip46 (ip46_address_get_type (&ips[0]));
+  gef->gef_itf = gbp_itf_add_and_lock (gel->gel_sw_if_index,
+                                      gbd->gb_bd_index);
+
+  if (!mac_address_is_zero (&ge->ge_key.gek_mac))
+    {
+      gbp_itf_set_l2_input_feature (gef->gef_itf, gei, L2INPUT_FEAT_GBP_FWD);
 
-      l3_ge = gbp_endpoint_find_ip (&ips[0],
-                                   gbp_endpoint_group_get_fib_index (gg,
-                                                                     fproto));
-      if (NULL == l3_ge)
+      if (gbp_endpoint_is_remote (ge))
        {
-         if (NULL == ge)
-           {
-             ge = gbp_endpoint_alloc (epg_id, ggi, sw_if_index, flags,
-                                      tun_src, tun_dst);
-             ge->ge_itf = gbp_itf_add_and_lock (sw_if_index, ~0);
-           }
-         /* else
-          *   L2 EP with IPs
-          */
+         gbp_itf_set_l2_output_feature (gef->gef_itf, gei,
+                                        L2OUTPUT_FEAT_GBP_POLICY_MAC);
        }
       else
        {
-         /* modify */
-         ge = l3_ge;
-         ge->ge_flags = flags;
-         gei = gbp_endpoint_index (ge);
-         goto out;
+         gbp_endpoint_add_itf (gef->gef_itf, gei);
+         gbp_itf_set_l2_output_feature (gef->gef_itf, gei,
+                                        L2OUTPUT_FEAT_GBP_POLICY_PORT);
        }
+      l2fib_add_entry (ge->ge_key.gek_mac.bytes,
+                      gbd->gb_bd_index,
+                      gef->gef_itf, L2FIB_ENTRY_RESULT_FLAG_STATIC);
+    }
 
-      gei = gbp_endpoint_index (ge);
-      ge->ge_ips = ips;
-      vec_validate (ge->ge_adjs, vec_len (ips) - 1);
-
-      vec_foreach_index (ii, ge->ge_ips)
+  vec_foreach (pfx, ge->ge_key.gek_ips)
+  {
+    ethernet_header_t *eth;
+    u32 ip_sw_if_index;
+    u32 fib_index;
+    u8 *rewrite;
+    index_t ai;
+
+    rewrite = NULL;
+    grd = gbp_route_domain_get (ge->ge_key.gek_grd);
+    fib_index = grd->grd_fib_index[pfx->fp_proto];
+
+    bd_add_del_ip_mac (gbd->gb_bd_index, fib_proto_to_ip46 (pfx->fp_proto),
+                      &pfx->fp_addr, &ge->ge_key.gek_mac, 1);
+
+    /*
+     * add a host route via the EPG's BVI we need this because the
+     * adj fib does not install, due to cover refinement check, since
+     * the BVI's prefix is /32
+     */
+    vec_validate (rewrite, sizeof (*eth) - 1);
+    eth = (ethernet_header_t *) rewrite;
+
+    eth->type = clib_host_to_net_u16 ((pfx->fp_proto == FIB_PROTOCOL_IP4 ?
+                                      ETHERNET_TYPE_IP4 :
+                                      ETHERNET_TYPE_IP6));
+
+    if (gbp_endpoint_is_remote (ge))
       {
-       ethernet_header_t *eth;
-       ip46_type_t ip_type;
-       u32 ip_sw_if_index;
-       u8 *rewrite;
-
-       rewrite = NULL;
-       ip = &ge->ge_ips[ii];
-       ip_type = ip46_address_get_type (ip);
-       fproto = fib_proto_from_ip46 (ip_type);
-
-       bd_add_del_ip_mac (gg->gg_bd_index, ip_type, ip, &ge->ge_mac, 1);
-
-       // FIXME - check error
-       gbp_endpoint_add_ip (ip,
-                            gbp_endpoint_group_get_fib_index (gg, fproto),
-                            gei);
-
        /*
-        * add a host route via the EPG's BVI we need this because the
-        * adj fib does not install, due to cover refinement check, since
-        * the BVI's prefix is /32
+        * for dynamic EPs we must add the IP adjacency via the learned
+        * tunnel since the BD will not contain the EP's MAC since it was
+        * L3 learned. The dst MAC address used is the 'BD's MAC'.
         */
-       fib_prefix_t pfx = {
-         .fp_proto = fproto,
-         .fp_len = ip46_address_get_len (ip),
-         .fp_addr = *ip,
-       };
-       vec_validate (rewrite, sizeof (*eth) - 1);
-       eth = (ethernet_header_t *) rewrite;
-
-       eth->type = clib_host_to_net_u16 ((fproto == FIB_PROTOCOL_IP4 ?
-                                          ETHERNET_TYPE_IP4 :
-                                          ETHERNET_TYPE_IP6));
-
-       if (gbp_endpoint_is_remote (ge))
-         {
-           /*
-            * for dynamic EPs we msut add the IP adjacency via the learned
-            * tunnel since the BD will not contain the EP's MAC since it was
-            * L3 learned. The dst MAC address used is the 'BD's MAC'.
-            */
-           ip_sw_if_index = ge->ge_sw_if_index;
+       ip_sw_if_index = gef->gef_itf;
 
-           mac_address_to_bytes (gbp_route_domain_get_local_mac (),
-                                 eth->src_address);
-           mac_address_to_bytes (gbp_route_domain_get_remote_mac (),
-                                 eth->dst_address);
-         }
-       else
-         {
-           /*
-            * for the static EPs we add the IP adjacency via the BVI
-            * knowing that the BD has the MAC address to route to and
-            * that policy will be applied on egress to the EP's port
-            */
-           ip_sw_if_index = gbp_endpoint_group_get_bvi (gg);
+       mac_address_to_bytes (gbp_route_domain_get_local_mac (),
+                             eth->src_address);
+       mac_address_to_bytes (gbp_route_domain_get_remote_mac (),
+                             eth->dst_address);
+      }
+    else
+      {
+       /*
+        * for the static EPs we add the IP adjacency via the BVI
+        * knowing that the BD has the MAC address to route to and
+        * that policy will be applied on egress to the EP's port
+        */
+       ip_sw_if_index = gbd->gb_bvi_sw_if_index;
 
-           clib_memcpy (eth->src_address,
-                        vnet_sw_interface_get_hw_address (vnet_get_main (),
-                                                          ip_sw_if_index),
-                        sizeof (eth->src_address));
-           mac_address_to_bytes (&ge->ge_mac, eth->dst_address);
-         }
+       clib_memcpy (eth->src_address,
+                    vnet_sw_interface_get_hw_address (vnet_get_main (),
+                                                      ip_sw_if_index),
+                    sizeof (eth->src_address));
+       mac_address_to_bytes (&ge->ge_key.gek_mac, eth->dst_address);
+      }
 
-       fib_table_entry_path_add
-         (gbp_endpoint_group_get_fib_index (gg, fproto),
-          &pfx, FIB_SOURCE_PLUGIN_LOW,
-          FIB_ENTRY_FLAG_NONE,
-          fib_proto_to_dpo (fproto), ip, ip_sw_if_index,
-          ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
+    fib_table_entry_path_add (fib_index, pfx,
+                             FIB_SOURCE_PLUGIN_LOW,
+                             FIB_ENTRY_FLAG_NONE,
+                             fib_proto_to_dpo (pfx->fp_proto),
+                             &pfx->fp_addr, ip_sw_if_index,
+                             ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
 
-       ge->ge_adjs[ii] = adj_nbr_add_or_lock_w_rewrite (fproto,
-                                                        fib_proto_to_link
-                                                        (fproto), ip,
-                                                        ip_sw_if_index,
-                                                        rewrite);
+    ai = adj_nbr_add_or_lock_w_rewrite (pfx->fp_proto,
+                                       fib_proto_to_link (pfx->fp_proto),
+                                       &pfx->fp_addr,
+                                       ip_sw_if_index, rewrite);
+    vec_add1 (gef->gef_adjs, ai);
 
+    if (NULL != gg)
+      {
        if (gbp_endpoint_is_remote (ge))
          {
            dpo_id_t policy_dpo = DPO_INVALID;
@@ -460,13 +721,13 @@ gbp_endpoint_update (u32 sw_if_index,
             * interpose a policy DPO from the endpoint so that policy
             * is applied
             */
-           gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (fproto),
+           gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (pfx->fp_proto),
                                        gg->gg_id, ~0, &policy_dpo);
 
-           fib_table_entry_special_dpo_add
-             (gbp_endpoint_group_get_fib_index (gg, fproto),
-              &pfx,
-              FIB_SOURCE_PLUGIN_HI, FIB_ENTRY_FLAG_INTERPOSE, &policy_dpo);
+           fib_table_entry_special_dpo_add (fib_index, pfx,
+                                            FIB_SOURCE_PLUGIN_HI,
+                                            FIB_ENTRY_FLAG_INTERPOSE,
+                                            &policy_dpo);
          }
 
        /*
@@ -474,68 +735,148 @@ gbp_endpoint_update (u32 sw_if_index,
         * that if this EP has moved from some other place in the
         * 'fabric', upstream devices are informed
         */
-       if (!(gbp_endpoint_is_remote (ge)) && ~0 != gg->gg_uplink_sw_if_index)
+       if (!gbp_endpoint_is_remote (ge) && ~0 != gg->gg_uplink_sw_if_index)
          {
-           gbp_endpoint_add_itf (sw_if_index, gei);
-           if (ip46_address_is_ip4 (ip))
+           gbp_endpoint_add_itf (gef->gef_itf, gei);
+           if (FIB_PROTOCOL_IP4 == pfx->fp_proto)
              send_ip4_garp_w_addr (vlib_get_main (),
-                                   &ip->ip4, gg->gg_uplink_sw_if_index);
+                                   &pfx->fp_addr.ip4,
+                                   gg->gg_uplink_sw_if_index);
            else
              send_ip6_na_w_addr (vlib_get_main (),
-                                 &ip->ip6, gg->gg_uplink_sw_if_index);
+                                 &pfx->fp_addr.ip6,
+                                 gg->gg_uplink_sw_if_index);
          }
       }
+  }
+
+  if (!gbp_endpoint_is_remote (ge))
+    {
+      /*
+       * non-remote endpoints (i.e. those not arriving on iVXLAN
+       * tunnels) need to be classifed based on the the input interface.
+       * We enable the GBP-FWD feature only if the group has an uplink
+       * interface (on which the GBP-FWD feature would send UU traffic).
+       */
+      l2input_feat_masks_t feats = L2INPUT_FEAT_GBP_SRC_CLASSIFY;
+
+      if (NULL != gg && ~0 != gg->gg_uplink_sw_if_index)
+       feats |= L2INPUT_FEAT_GBP_FWD;
+      gbp_itf_set_l2_input_feature (gef->gef_itf, gei, feats);
     }
 
-  if (NULL == ge)
-    return (0);
+  /*
+   * update children with the new forwarding info
+   */
+  fib_node_back_walk_ctx_t bw_ctx = {
+    .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
+    .fnbw_flags = FIB_NODE_BW_FLAG_FORCE_SYNC,
+  };
+
+  fib_walk_sync (gbp_endpoint_fib_type, gei, &bw_ctx);
+}
+
+int
+gbp_endpoint_update_and_lock (gbp_endpoint_src_t src,
+                             u32 sw_if_index,
+                             const ip46_address_t * ips,
+                             const mac_address_t * mac,
+                             index_t gbdi, index_t grdi, epg_id_t epg_id,
+                             gbp_endpoint_flags_t flags,
+                             const ip46_address_t * tun_src,
+                             const ip46_address_t * tun_dst, u32 * handle)
+{
+  gbp_bridge_domain_t *gbd;
+  gbp_endpoint_group_t *gg;
+  gbp_endpoint_src_t best;
+  gbp_route_domain_t *grd;
+  gbp_endpoint_loc_t *gel;
+  gbp_endpoint_t *ge;
+  index_t ggi, gei;
+  int rv;
+
+  if (~0 == sw_if_index)
+    return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
+
+  ge = NULL;
+  gg = NULL;
 
   /*
-   * count the number of dynamic entries and kick off the scanner
-   * process is this is our first.
+   * we need to determine the bridge-domain, either from the EPG or
+   * the BD passed
    */
-  if (gbp_endpoint_is_remote (ge))
+  if (EPG_INVALID != epg_id)
     {
-      gbp_n_learnt_endpoints++;
+      ggi = gbp_endpoint_group_find (epg_id);
 
-      if (1 == gbp_n_learnt_endpoints)
-       {
-         vlib_process_signal_event (vlib_get_main (),
-                                    gbp_scanner_node.index,
-                                    GBP_ENDPOINT_SCAN_START, 0);
-       }
+      if (INDEX_INVALID == ggi)
+       return (VNET_API_ERROR_NO_SUCH_ENTRY);
+
+      gg = gbp_endpoint_group_get (ggi);
+      gbdi = gg->gg_gbd;
+      grdi = gg->gg_rd;
+    }
+  else
+    {
+      if (INDEX_INVALID == gbdi)
+       return (VNET_API_ERROR_NO_SUCH_ENTRY);
+      if (INDEX_INVALID == grdi)
+       return (VNET_API_ERROR_NO_SUCH_FIB);
+      ggi = INDEX_INVALID;
+    }
+
+  gbd = gbp_bridge_domain_get (gbdi);
+  grd = gbp_route_domain_get (grdi);
+  rv = gbp_endpoint_find_for_update (ips, grd, mac, gbd, &ge);
+
+  if (0 != rv)
+    return (rv);
+
+  if (NULL == ge)
+    {
+      ge = gbp_endpoint_alloc (ips, grd, mac, gbd);
     }
   else
+    {
+      gbp_endpoint_ips_update (ge, ips, grd);
+    }
+
+  best = gbp_endpoint_get_best_src (ge);
+  gei = gbp_endpoint_index (ge);
+  gel = gbp_endpoint_loc_find_or_add (ge, src);
+
+  gbp_endpoint_loc_update (gel, sw_if_index, ggi, flags, tun_src, tun_dst);
+
+  if (src <= best)
     {
       /*
-       * non-remote endpoints (i.e. those not arriving on iVXLAN
-       * tunnels) need to be classifed based on the the input interface.
-       * We enable the GBP-FWD feature only is the group has an uplink
-       * interface (on which the GBP-FWD feature would send UU traffic).
+       * either the best source has been updated or we have a new best source
+       */
+      gbb_endpoint_fwd_reset (ge);
+      gbb_endpoint_fwd_recalc (ge);
+    }
+  else
+    {
+      /*
+       * an update to a lower priority source, so we need do nothing
        */
-      l2input_feat_masks_t feats = L2INPUT_FEAT_GBP_SRC_CLASSIFY;
-
-      if (~0 != gg->gg_uplink_sw_if_index)
-       feats |= L2INPUT_FEAT_GBP_FWD;
-      gbp_itf_set_l2_input_feature (ge->ge_itf, gbp_endpoint_index (ge),
-                                   feats);
     }
-out:
 
   if (handle)
-    *handle = (ge - gbp_endpoint_pool);
+    *handle = gei;
 
-  gbp_endpoint_group_unlock (ggi);
   GBP_ENDPOINT_INFO ("update: %U", format_gbp_endpoint, gei);
 
   return (0);
 }
 
 void
-gbp_endpoint_delete (index_t gei)
+gbp_endpoint_unlock (gbp_endpoint_src_t src, index_t gei)
 {
-  gbp_endpoint_group_t *gg;
+  gbp_endpoint_loc_t *gel, gel_copy;
+  gbp_endpoint_src_t best;
   gbp_endpoint_t *ge;
+  int removed;
 
   if (pool_is_free_index (gbp_endpoint_pool, gei))
     return;
@@ -543,86 +884,90 @@ gbp_endpoint_delete (index_t gei)
   GBP_ENDPOINT_INFO ("delete: %U", format_gbp_endpoint, gei);
 
   ge = gbp_endpoint_get (gei);
-  gg = gbp_endpoint_group_get (ge->ge_epg);
-
-  gbp_endpoint_del_mac (&ge->ge_mac, gg->gg_bd_index);
-  l2fib_del_entry (ge->ge_mac.bytes, gg->gg_bd_index, ge->ge_sw_if_index);
-  gbp_itf_set_l2_input_feature (ge->ge_itf, gei, (L2INPUT_FEAT_NONE));
-  gbp_itf_set_l2_output_feature (ge->ge_itf, gei, L2OUTPUT_FEAT_NONE);
-
-  if (NULL != ge->ge_ips)
-    {
-      const ip46_address_t *ip;
-      index_t *ai;
-
-      vec_foreach (ai, ge->ge_adjs)
-      {
-       adj_unlock (*ai);
-      }
-      vec_foreach (ip, ge->ge_ips)
-      {
-       fib_protocol_t fproto;
-       ip46_type_t ip_type;
 
-       ip_type = ip46_address_get_type (ip);
-       fproto = fib_proto_from_ip46 (ip_type);
+  gel = gbp_endpoint_loc_find (ge, src);
 
-       gbp_endpoint_del_ip (ip,
-                            gbp_endpoint_group_get_fib_index (gg, fproto));
-
-       bd_add_del_ip_mac (gg->gg_bd_index, ip_type, ip, &ge->ge_mac, 0);
+  if (NULL == gel)
+    return;
 
-       /*
-        * remove a host route via the EPG's BVI
-        */
-       fib_prefix_t pfx = {
-         .fp_proto = fproto,
-         .fp_len = ip46_address_get_len (ip),
-         .fp_addr = *ip,
-       };
+  /*
+   * lock the EP so we can control when it is deleted
+   */
+  fib_node_lock (&ge->ge_node);
+  best = gbp_endpoint_get_best_src (ge);
 
-       if (gbp_endpoint_is_remote (ge))
-         {
-           fib_table_entry_special_remove
-             (gbp_endpoint_group_get_fib_index (gg, fproto),
-              &pfx, FIB_SOURCE_PLUGIN_HI);
-         }
+  /*
+   * copy the location info since we'll lose it when it's removed from
+   * the vector
+   */
+  clib_memcpy (&gel_copy, gel, sizeof (gel_copy));
 
-       fib_table_entry_path_remove
-         (gbp_endpoint_group_get_fib_index (gg, fproto),
-          &pfx, FIB_SOURCE_PLUGIN_LOW,
-          fib_proto_to_dpo (fproto), ip,
-          (gbp_endpoint_is_remote (ge) ?
-           ge->ge_sw_if_index :
-           gbp_endpoint_group_get_bvi (gg)),
-          ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
-      }
-    }
+  /*
+   * remove the source we no longer need
+   */
+  removed = gbp_endpoint_loc_unlock (ge, gel);
 
-  if (ge->ge_flags & GBP_ENDPOINT_FLAG_LEARNT)
+  if (src == best)
     {
-      gbp_n_learnt_endpoints--;
-
-      if (0 == gbp_n_learnt_endpoints)
+      /*
+       * we have removed the old best source => recalculate fwding
+       */
+      if (0 == vec_len (ge->ge_locs))
+       {
+         /*
+          * if there are no more sources left, then we need only release
+          * the fwding resources held and then this EP is gawn.
+          */
+         gbb_endpoint_fwd_reset (ge);
+       }
+      else
        {
-         vlib_process_signal_event (vlib_get_main (),
-                                    gbp_scanner_node.index,
-                                    GBP_ENDPOINT_SCAN_STOP, 0);
+         /*
+          * else there are more sources. release the old and get new
+          * fwding objects
+          */
+         gbb_endpoint_fwd_reset (ge);
+         gbb_endpoint_fwd_recalc (ge);
        }
     }
+  /*
+   * else
+   *  we removed a lower priority source so we need to do nothing
+   */
 
-  gbp_itf_unlock (ge->ge_itf);
-  if (gbp_endpoint_is_remote (ge))
-    {
-      vxlan_gbp_tunnel_unlock (ge->ge_sw_if_index);
-    }
-  gbp_endpoint_group_unlock (ge->ge_epg);
-  pool_put (gbp_endpoint_pool, ge);
+  /*
+   * clear up any resources held by the source
+   */
+  if (removed)
+    gbp_endpoint_loc_destroy (&gel_copy);
+
+  /*
+   * remove the lock taken above
+   */
+  fib_node_unlock (&ge->ge_node);
+  /*
+   *  We may have removed the last source and so this EP is now TOAST
+   *  DO NOTHING BELOW HERE
+   */
+}
+
+u32
+gbp_endpoint_child_add (index_t gei,
+                       fib_node_type_t type, fib_node_index_t index)
+{
+  return (fib_node_child_add (gbp_endpoint_fib_type, gei, type, index));
+}
+
+void
+gbp_endpoint_child_remove (index_t gei, u32 sibling)
+{
+  return (fib_node_child_remove (gbp_endpoint_fib_type, gei, sibling));
 }
 
 typedef struct gbp_endpoint_flush_ctx_t_
 {
   u32 sw_if_index;
+  gbp_endpoint_src_t src;
   index_t *geis;
 } gbp_endpoint_flush_ctx_t;
 
@@ -630,12 +975,13 @@ static walk_rc_t
 gbp_endpoint_flush_cb (index_t gei, void *args)
 {
   gbp_endpoint_flush_ctx_t *ctx = args;
+  gbp_endpoint_loc_t *gel;
   gbp_endpoint_t *ge;
 
   ge = gbp_endpoint_get (gei);
+  gel = gbp_endpoint_loc_find (ge, ctx->src);
 
-  if (gbp_endpoint_is_remote (ge) &&
-      ctx->sw_if_index == ge->tun.ge_parent_sw_if_index)
+  if ((NULL != gel) && ctx->sw_if_index == gel->tun.gel_parent_sw_if_index)
     {
       vec_add1 (ctx->geis, gei);
     }
@@ -647,18 +993,23 @@ gbp_endpoint_flush_cb (index_t gei, void *args)
  * remove all learnt endpoints using the interface
  */
 void
-gbp_endpoint_flush (u32 sw_if_index)
+gbp_endpoint_flush (gbp_endpoint_src_t src, u32 sw_if_index)
 {
   gbp_endpoint_flush_ctx_t ctx = {
     .sw_if_index = sw_if_index,
+    .src = src,
   };
   index_t *gei;
 
+  GBP_ENDPOINT_INFO ("flush: %U %U",
+                    format_gbp_endpoint_src, src,
+                    format_vnet_sw_if_index_name, vnet_get_main (),
+                    sw_if_index);
   gbp_endpoint_walk (gbp_endpoint_flush_cb, &ctx);
 
   vec_foreach (gei, ctx.geis)
   {
-    gbp_endpoint_delete (*gei);
+    gbp_endpoint_unlock (src, *gei);
   }
 
   vec_free (ctx.geis);
@@ -724,8 +1075,12 @@ gbp_endpoint_cli (vlib_main_t * vm,
        return clib_error_return (0, "EPG-ID must be specified");
 
       rv =
-       gbp_endpoint_update (sw_if_index, ips, &mac, epg_id,
-                            GBP_ENDPOINT_FLAG_NONE, NULL, NULL, &handle);
+       gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_CP,
+                                     sw_if_index, ips, &mac,
+                                     INDEX_INVALID, INDEX_INVALID,
+                                     epg_id,
+                                     GBP_ENDPOINT_FLAG_NONE,
+                                     NULL, NULL, &handle);
 
       if (rv)
        return clib_error_return (0, "GBP Endpoint update returned %d", rv);
@@ -737,7 +1092,7 @@ gbp_endpoint_cli (vlib_main_t * vm,
       if (INDEX_INVALID == handle)
        return clib_error_return (0, "handle must be specified");
 
-      gbp_endpoint_delete (handle);
+      gbp_endpoint_unlock (GBP_ENDPOINT_SRC_CP, handle);
     }
 
   vec_free (ips);
@@ -745,7 +1100,6 @@ gbp_endpoint_cli (vlib_main_t * vm,
   return (NULL);
 }
 
-
 /*?
  * Configure a GBP Endpoint
  *
@@ -762,33 +1116,100 @@ VLIB_CLI_COMMAND (gbp_endpoint_cli_node, static) = {
 /* *INDENT-ON* */
 
 u8 *
-format_gbp_endpoint (u8 * s, va_list * args)
+format_gbp_endpoint_src (u8 * s, va_list * args)
 {
-  index_t gei = va_arg (*args, index_t);
-  const ip46_address_t *ip;
-  gbp_endpoint_t *ge;
+  gbp_endpoint_src_t action = va_arg (*args, gbp_endpoint_src_t);
 
-  ge = gbp_endpoint_get (gei);
+  switch (action)
+    {
+#define _(v,a) case GBP_ENDPOINT_SRC_##v: return (format (s, "%s", a));
+      foreach_gbp_endpoint_src
+#undef _
+    }
+
+  return (format (s, "unknown"));
+}
+
+static u8 *
+format_gbp_endpoint_fwd (u8 * s, va_list * args)
+{
+  gbp_endpoint_fwd_t *gef = va_arg (*args, gbp_endpoint_fwd_t *);
+
+  s = format (s, "fwd:");
+  s = format (s, "\n   itf:[%U]", format_gbp_itf, gef->gef_itf);
+  if (GBP_ENDPOINT_FLAG_NONE != gef->gef_flags)
+    {
+      s = format (s, " flags:%U", format_gbp_endpoint_flags, gef->gef_flags);
+    }
+
+  return (s);
+}
+
+static u8 *
+format_gbp_endpoint_key (u8 * s, va_list * args)
+{
+  gbp_endpoint_key_t *gek = va_arg (*args, gbp_endpoint_key_t *);
+  const fib_prefix_t *pfx;
 
-  s = format (s, "[@%d] ", gei);
-  s = format (s, "IPs:[");
+  s = format (s, "ips:[");
 
-  vec_foreach (ip, ge->ge_ips)
+  vec_foreach (pfx, gek->gek_ips)
   {
-    s = format (s, "%U, ", format_ip46_address, ip, IP46_TYPE_ANY);
+    s = format (s, "%U, ", format_fib_prefix, pfx);
   }
   s = format (s, "]");
 
-  s = format (s, " MAC:%U", format_mac_address_t, &ge->ge_mac);
-  s = format (s, " EPG-ID:%d", ge->ge_epg_id);
-  if (GBP_ENDPOINT_FLAG_NONE != ge->ge_flags)
+  s = format (s, " mac:%U", format_mac_address_t, &gek->gek_mac);
+
+  return (s);
+}
+
+static u8 *
+format_gbp_endpoint_loc (u8 * s, va_list * args)
+{
+  gbp_endpoint_loc_t *gel = va_arg (*args, gbp_endpoint_loc_t *);
+
+  s = format (s, "%U", format_gbp_endpoint_src, gel->gel_src);
+  s =
+    format (s, "\n    %U", format_vnet_sw_if_index_name, vnet_get_main (),
+           gel->gel_sw_if_index);
+  s = format (s, " EPG:%d", gel->gel_epg);
+
+  if (GBP_ENDPOINT_FLAG_NONE != gel->gel_flags)
+    {
+      s = format (s, " flags:%U", format_gbp_endpoint_flags, gel->gel_flags);
+    }
+  if (GBP_ENDPOINT_FLAG_REMOTE & gel->gel_flags)
     {
-      s = format (s, " flags:%U", format_gbp_endpoint_flags, ge->ge_flags);
+      s = format (s, " tun:[");
+      s = format (s, "parent:%U", format_vnet_sw_if_index_name,
+                 vnet_get_main (), gel->tun.gel_parent_sw_if_index);
+      s = format (s, " {%U,%U}]",
+                 format_ip46_address, &gel->tun.gel_src, IP46_TYPE_ANY,
+                 format_ip46_address, &gel->tun.gel_dst, IP46_TYPE_ANY);
     }
 
-  s = format (s, " itf:[%U]", format_gbp_itf, ge->ge_itf);
+  return (s);
+}
+
+u8 *
+format_gbp_endpoint (u8 * s, va_list * args)
+{
+  index_t gei = va_arg (*args, index_t);
+  gbp_endpoint_loc_t *gel;
+  gbp_endpoint_t *ge;
+
+  ge = gbp_endpoint_get (gei);
+
+  s = format (s, "[@%d] %U", gei, format_gbp_endpoint_key, &ge->ge_key);
   s = format (s, " last-time:[%f]", ge->ge_last_time);
 
+  vec_foreach (gel, ge->ge_locs)
+  {
+    s = format (s, "\n  %U", format_gbp_endpoint_loc, gel);
+  }
+  s = format (s, "\n  %U", format_gbp_endpoint_fwd, &ge->ge_fwd);
+
   return s;
 }
 
@@ -895,16 +1316,16 @@ VLIB_CLI_COMMAND (gbp_endpoint_show_node, static) = {
 static void
 gbp_endpoint_check (index_t gei, f64 start_time)
 {
+  gbp_endpoint_loc_t *gel;
   gbp_endpoint_t *ge;
 
   ge = gbp_endpoint_get (gei);
+  gel = gbp_endpoint_loc_find (ge, GBP_ENDPOINT_SRC_DP);
 
-  GBP_ENDPOINT_DBG ("scan at:%f -> %U", start_time, format_gbp_endpoint, gei);
-
-  if ((ge->ge_flags & GBP_ENDPOINT_FLAG_LEARNT) &&
+  if ((NULL != gel) &&
       ((start_time - ge->ge_last_time) > GBP_ENDPOINT_INACTIVE_TIME))
     {
-      gbp_endpoint_delete (gei);
+      gbp_endpoint_unlock (GBP_ENDPOINT_SRC_DP, gei);
     }
 }
 
@@ -1031,12 +1452,77 @@ gbp_endpoint_scan_threshold (void)
   return (GBP_ENDPOINT_INACTIVE_TIME);
 }
 
-#define GBP_EP_HASH_NUM_BUCKETS (2 * 1024)
-#define GBP_EP_HASH_MEMORY_SIZE (1 << 20)
+static fib_node_t *
+gbp_endpoint_get_node (fib_node_index_t index)
+{
+  gbp_endpoint_t *ge;
+
+  ge = gbp_endpoint_get (index);
+
+  return (&ge->ge_node);
+}
+
+static gbp_endpoint_t *
+gbp_endpoint_from_fib_node (fib_node_t * node)
+{
+  ASSERT (gbp_endpoint_fib_type == node->fn_type);
+  return ((gbp_endpoint_t *) node);
+}
+
+static void
+gbp_endpoint_last_lock_gone (fib_node_t * node)
+{
+  const gbp_bridge_domain_t *gbd;
+  const gbp_route_domain_t *grd;
+  const fib_prefix_t *pfx;
+  gbp_endpoint_t *ge;
+
+  ge = gbp_endpoint_from_fib_node (node);
+
+  ASSERT (0 == vec_len (ge->ge_locs));
+
+  gbd = gbp_bridge_domain_get (ge->ge_key.gek_gbd);
+
+  /*
+   * we have removed the last source. this EP is toast
+   */
+  if (INDEX_INVALID != ge->ge_key.gek_gbd)
+    {
+      gbp_endpoint_del_mac (&ge->ge_key.gek_mac, gbd->gb_bd_index);
+    }
+  vec_foreach (pfx, ge->ge_key.gek_ips)
+  {
+    grd = gbp_route_domain_get (ge->ge_key.gek_grd);
+    gbp_endpoint_del_ip (&pfx->fp_addr, grd->grd_fib_index[pfx->fp_proto]);
+  }
+  pool_put (gbp_endpoint_pool, ge);
+}
+
+static fib_node_back_walk_rc_t
+gbp_endpoint_back_walk_notify (fib_node_t * node,
+                              fib_node_back_walk_ctx_t * ctx)
+{
+  ASSERT (0);
+
+  return (FIB_NODE_BACK_WALK_CONTINUE);
+}
+
+/*
+ * The FIB path's graph node virtual function table
+ */
+static const fib_node_vft_t gbp_endpoint_vft = {
+  .fnv_get = gbp_endpoint_get_node,
+  .fnv_last_lock = gbp_endpoint_last_lock_gone,
+  .fnv_back_walk = gbp_endpoint_back_walk_notify,
+  // .fnv_mem_show = fib_path_memory_show,
+};
 
 static clib_error_t *
 gbp_endpoint_init (vlib_main_t * vm)
 {
+#define GBP_EP_HASH_NUM_BUCKETS (2 * 1024)
+#define GBP_EP_HASH_MEMORY_SIZE (1 << 20)
+
   clib_bihash_init_24_8 (&gbp_ep_db.ged_by_ip_rd,
                         "GBP Endpoints - IP/RD",
                         GBP_EP_HASH_NUM_BUCKETS, GBP_EP_HASH_MEMORY_SIZE);
@@ -1046,6 +1532,7 @@ gbp_endpoint_init (vlib_main_t * vm)
                         GBP_EP_HASH_NUM_BUCKETS, GBP_EP_HASH_MEMORY_SIZE);
 
   gbp_ep_logger = vlib_log_register_class ("gbp", "ep");
+  gbp_endpoint_fib_type = fib_node_register_new_type (&gbp_endpoint_vft);
 
   return (NULL);
 }