ip: add support for buffer offload metadata in ip midchain
[vpp.git] / src / vnet / adj / adj_nbr.c
index 55c76bd..b3a027b 100644 (file)
@@ -39,7 +39,20 @@ typedef struct adj_nbr_key_t_
 
 #define ADJ_NBR_ITF_OK(_proto, _itf)                   \
     (((_itf) < vec_len(adj_nbr_tables[_proto])) &&     \
-     (NULL != adj_nbr_tables[_proto][sw_if_index]))
+     (NULL != adj_nbr_tables[_proto][(_itf)]))
+
+#define ADJ_NBR_ASSERT_NH_PROTO(nh_proto, err)          \
+  do {                                                  \
+      ASSERT (nh_proto < FIB_PROTOCOL_IP_MAX);          \
+      const fib_protocol_t nh_proto__ = (nh_proto);     \
+      if (nh_proto__ >= FIB_PROTOCOL_IP_MAX)            \
+        {                                               \
+          clib_warning ("BUG: protocol %d > %d\n",      \
+                        (int)nh_proto__,                \
+                        FIB_PROTOCOL_IP_MAX);           \
+          return err;                                   \
+        }                                               \
+  } while (0)
 
 static void
 adj_nbr_insert (fib_protocol_t nh_proto,
@@ -50,6 +63,8 @@ adj_nbr_insert (fib_protocol_t nh_proto,
 {
     adj_nbr_key_t kv;
 
+    ADJ_NBR_ASSERT_NH_PROTO (nh_proto,);
+
     if (sw_if_index >= vec_len(adj_nbr_tables[nh_proto]))
     {
        vec_validate(adj_nbr_tables[nh_proto], sw_if_index);
@@ -75,6 +90,8 @@ adj_nbr_remove (adj_index_t ai,
 {
     adj_nbr_key_t kv;
 
+    ADJ_NBR_ASSERT_NH_PROTO (nh_proto,);
+
     if (!ADJ_NBR_ITF_OK(nh_proto, sw_if_index))
        return;
 
@@ -88,6 +105,46 @@ adj_nbr_remove (adj_index_t ai,
     }
 }
 
+typedef struct adj_nbr_get_n_adjs_walk_ctx_t_
+{
+    vnet_link_t linkt;
+    u32 count;
+} adj_nbr_get_n_adjs_walk_ctx_t;
+
+static adj_walk_rc_t
+adj_nbr_get_n_adjs_walk (adj_index_t ai,
+                         void *data)
+{
+    adj_nbr_get_n_adjs_walk_ctx_t *ctx = data;
+    const ip_adjacency_t *adj;
+
+    adj = adj_get(ai);
+
+    if (ctx->linkt == adj->ia_link)
+        ctx->count++;
+
+    return (ADJ_WALK_RC_CONTINUE);
+}
+
+u32
+adj_nbr_get_n_adjs (vnet_link_t link_type, u32 sw_if_index)
+{
+    adj_nbr_get_n_adjs_walk_ctx_t ctx = {
+        .linkt = link_type,
+    };
+    fib_protocol_t fproto;
+
+    FOR_EACH_FIB_IP_PROTOCOL(fproto)
+    {
+        adj_nbr_walk (sw_if_index,
+                      fproto,
+                      adj_nbr_get_n_adjs_walk,
+                      &ctx);
+    }
+
+    return (ctx.count);
+}
+
 adj_index_t
 adj_nbr_find (fib_protocol_t nh_proto,
              vnet_link_t link_type,
@@ -97,6 +154,8 @@ adj_nbr_find (fib_protocol_t nh_proto,
     adj_nbr_key_t kv;
     uword *p;
 
+    ADJ_NBR_ASSERT_NH_PROTO (nh_proto, ADJ_INDEX_INVALID);
+
     ADJ_NBR_SET_KEY(kv, link_type, nh_addr);
 
     if (!ADJ_NBR_ITF_OK(nh_proto, sw_if_index))
@@ -203,6 +262,27 @@ adj_nbr_alloc (fib_protocol_t nh_proto,
     return (adj);
 }
 
+void
+adj_nbr_set_mtu (adj_index_t adj_index, u16 mtu)
+{
+    ip_adjacency_t *adj;
+
+    ASSERT(ADJ_INDEX_INVALID != adj_index);
+
+    adj = adj_get(adj_index);
+
+    if (0 == mtu)
+        vnet_rewrite_update_mtu(vnet_get_main(), adj->ia_link,
+                                &adj->rewrite_header);
+    else
+    {
+        vnet_rewrite_update_mtu(vnet_get_main(), adj->ia_link,
+                                &adj->rewrite_header);
+        adj->rewrite_header.max_l3_packet_bytes =
+            clib_min (adj->rewrite_header.max_l3_packet_bytes, mtu);
+    }
+}
+
 /*
  * adj_nbr_add_or_lock
  *
@@ -249,13 +329,13 @@ adj_nbr_add_or_lock (fib_protocol_t nh_proto,
         * So ask the interface to do it.
         */
        vnet_update_adjacency_for_sw_interface(vnm, sw_if_index, adj_index);
+        adj_delegate_adj_created(adj_get(adj_index));
     }
     else
     {
        adj_lock(adj_index);
     }
 
-    adj_delegate_adj_created(adj_get(adj_index));
     return (adj_index);
 }
 
@@ -452,7 +532,7 @@ adj_nbr_update_rewrite_internal (ip_adjacency_t *adj,
 
        fib_walk_sync(FIB_NODE_TYPE_ADJ, walk_ai, &bw_ctx);
        /*
-        * fib_walk_sync may allocate a new adjacency and potentially cuase a
+        * fib_walk_sync may allocate a new adjacency and potentially cause a
         * realloc for adj_pool. When that happens, adj pointer is no longer
         * valid here. We refresh the adj pointer accordingly.
         */
@@ -520,7 +600,7 @@ adj_nbr_update_rewrite_internal (ip_adjacency_t *adj,
         walk_adj->ia_flags &= ~ADJ_FLAG_SYNC_WALK_ACTIVE;
     }
 
-    adj_delegate_adj_modified(adj);
+    adj_delegate_adj_modified(adj_get(ai));
     adj_unlock(ai);
     adj_unlock(walk_ai);
 }
@@ -554,21 +634,30 @@ adj_nbr_walk (u32 sw_if_index,
              adj_walk_cb_t cb,
              void *ctx)
 {
+    adj_index_t ai, *ais, *aip;
     adj_nbr_key_t *key;
-    adj_index_t ai;
+
+    ADJ_NBR_ASSERT_NH_PROTO (adj_nh_proto,);
 
     if (!ADJ_NBR_ITF_OK(adj_nh_proto, sw_if_index))
        return;
 
-    if (adj_nbr_tables[adj_nh_proto][sw_if_index] ||
-        hash_elts(adj_nbr_tables[adj_nh_proto][sw_if_index]))
+    ais = NULL;
+
+    /* elements may be removed from the table during the walk, so
+     * collect the set first then process them */
+    hash_foreach_mem (key, ai, adj_nbr_tables[adj_nh_proto][sw_if_index],
+    ({
+        vec_add1(ais, ai);
+    }));
+
+    vec_foreach(aip, ais)
     {
-        hash_foreach_mem (key, ai, adj_nbr_tables[adj_nh_proto][sw_if_index],
-        ({
-            ASSERT(key);
-            cb(ai, ctx);
-        }));
+        /* An adj may be deleted during the walk so check first */
+        if (!pool_is_free_index(adj_pool, *aip))
+            cb(*aip, ctx);
     }
+    vec_free(ais);
 }
 
 /**
@@ -638,6 +727,8 @@ adj_nbr_walk_nh (u32 sw_if_index,
                 adj_walk_cb_t cb,
                 void *ctx)
 {
+    ADJ_NBR_ASSERT_NH_PROTO (adj_nh_proto,);
+
     if (!ADJ_NBR_ITF_OK(adj_nh_proto, sw_if_index))
        return;
 
@@ -699,12 +790,21 @@ adj_nbr_interface_state_change_one (adj_index_t ai,
     };
     ip_adjacency_t *adj;
 
-    adj = adj_get(ai);
+    adj_lock (ai);
 
+    adj = adj_get(ai);
     adj->ia_flags |= ADJ_FLAG_SYNC_WALK_ACTIVE;
     fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &bw_ctx);
+
+    /*
+     * fib_walk_sync may allocate a new adjacency and potentially cause a
+     * realloc for adj_pool. When that happens, adj pointer is no longer
+     * valid here. We refresh the adj pointer accordingly.
+     */
+    adj = adj_get(ai);
     adj->ia_flags &= ~ADJ_FLAG_SYNC_WALK_ACTIVE;
 
+    adj_unlock (ai);
     return (ADJ_WALK_RC_CONTINUE);
 }
 
@@ -809,9 +909,15 @@ adj_nbr_interface_delete_one (adj_index_t ai,
     adj_lock(ai);
 
     adj = adj_get(ai);
-
     adj->ia_flags |= ADJ_FLAG_SYNC_WALK_ACTIVE;
     fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &bw_ctx);
+
+    /*
+     * fib_walk_sync may allocate a new adjacency and potentially cause a
+     * realloc for adj_pool. When that happens, adj pointer is no longer
+     * valid here. We refresh the adj pointer accordingly.
+     */
+    adj = adj_get(ai);
     adj->ia_flags &= ~ADJ_FLAG_SYNC_WALK_ACTIVE;
 
     adj_unlock(ai);
@@ -856,12 +962,39 @@ adj_nbr_interface_add_del (vnet_main_t * vnm,
     }
 
     return (NULL);
-   
 }
 
 VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_nbr_interface_add_del);
 
 
+static adj_walk_rc_t
+adj_nbr_ethernet_mac_change_one (adj_index_t ai,
+                                 void *arg)
+{
+    vnet_update_adjacency_for_sw_interface(vnet_get_main(),
+                                           adj_get_sw_if_index(ai),
+                                           ai);
+
+    return (ADJ_WALK_RC_CONTINUE);
+}
+
+/**
+ * Callback function invoked when an interface's MAC Address changes
+ */
+static void
+adj_nbr_ethernet_change_mac (ethernet_main_t * em,
+                             u32 sw_if_index, uword opaque)
+{
+    fib_protocol_t proto;
+
+    FOR_EACH_FIB_IP_PROTOCOL(proto)
+    {
+       adj_nbr_walk(sw_if_index, proto,
+                    adj_nbr_ethernet_mac_change_one,
+                    NULL);
+    }
+}
+
 static adj_walk_rc_t
 adj_nbr_show_one (adj_index_t ai,
                  void *arg)
@@ -1025,12 +1158,14 @@ const static dpo_vft_t adj_nbr_dpo_vft = {
     .dv_format = format_adj_nbr,
     .dv_mem_show = adj_mem_show,
     .dv_get_urpf = adj_dpo_get_urpf,
+    .dv_get_mtu = adj_dpo_get_mtu,
 };
 const static dpo_vft_t adj_nbr_incompl_dpo_vft = {
     .dv_lock = adj_dpo_lock,
     .dv_unlock = adj_dpo_unlock,
     .dv_format = format_adj_nbr_incomplete,
     .dv_get_urpf = adj_dpo_get_urpf,
+    .dv_get_mtu = adj_dpo_get_mtu,
 };
 
 /**
@@ -1100,4 +1235,10 @@ adj_nbr_module_init (void)
     dpo_register(DPO_ADJACENCY_INCOMPLETE,
                  &adj_nbr_incompl_dpo_vft,
                  nbr_incomplete_nodes);
+
+    ethernet_address_change_ctx_t ctx = {
+        .function = adj_nbr_ethernet_change_mac,
+        .function_opaque = 0,
+    };
+    vec_add1 (ethernet_main.address_change_callbacks, ctx);
 }