ip: add support for buffer offload metadata in ip midchain
[vpp.git] / src / vnet / adj / adj_glean.c
index e956318..0313407 100644 (file)
@@ -186,10 +186,35 @@ adj_glean_update_rewrite_walk (adj_index_t ai,
     return (ADJ_WALK_RC_CONTINUE);
 }
 
-void
-adj_glean_update_rewrite_itf (u32 sw_if_index)
+static void
+adj_glean_walk_proto (fib_protocol_t proto,
+                      u32 sw_if_index,
+                      adj_walk_cb_t cb,
+                      void *data)
 {
-    adj_glean_walk (sw_if_index, adj_glean_update_rewrite_walk, NULL);
+    adj_index_t ai, *aip, *ais = NULL;
+    ip46_address_t *conn;
+
+    if (vec_len(adj_gleans[proto]) <= sw_if_index ||
+        NULL == adj_gleans[proto][sw_if_index])
+        return;
+
+    /*
+     * Walk first to collect the indices
+     * then walk the collection. This is safe
+     * to modifications of the hash table
+     */
+    hash_foreach_mem(conn, ai, adj_gleans[proto][sw_if_index],
+    ({
+        vec_add1(ais, ai);
+    }));
+
+    vec_foreach(aip, ais)
+    {
+        if (ADJ_WALK_RC_STOP == cb(*aip, data))
+            return;
+    }
+    vec_free(ais);
 }
 
 void
@@ -201,29 +226,7 @@ adj_glean_walk (u32 sw_if_index,
 
     FOR_EACH_FIB_IP_PROTOCOL(proto)
     {
-        adj_index_t ai, *aip, *ais = NULL;
-        ip46_address_t *conn;
-
-        if (vec_len(adj_gleans[proto]) <= sw_if_index ||
-            NULL == adj_gleans[proto][sw_if_index])
-            continue;
-
-        /*
-         * Walk first to collect the indices
-         * then walk the collection. This is safe
-         * to modifications of the hash table
-         */
-        hash_foreach_mem(conn, ai, adj_gleans[proto][sw_if_index],
-        ({
-            vec_add1(ais, ai);
-        }));
-
-        vec_foreach(aip, ais)
-        {
-            if (ADJ_WALK_RC_STOP == cb(*aip, data))
-                break;
-        }
-        vec_free(ais);
+      adj_glean_walk_proto (proto, sw_if_index, cb, data);
     }
 }
 
@@ -258,8 +261,8 @@ adj_glean_get_src (fib_protocol_t proto,
                    u32 sw_if_index,
                    const ip46_address_t *nh)
 {
+    const ip46_address_t *conn, *source;
     const ip_adjacency_t *adj;
-    ip46_address_t *conn;
     adj_index_t ai;
 
     if (vec_len(adj_gleans[proto]) <= sw_if_index ||
@@ -274,23 +277,33 @@ adj_glean_get_src (fib_protocol_t proto,
     if (nh)
         pfx.fp_addr = *nh;
 
+    /*
+     * An interface can have more than one glean address. Where
+     * possible we want to return a source address from the same
+     * subnet as the destination. If this is not possible then any address
+     * will do.
+     */
+    source = NULL;
+
     hash_foreach_mem(conn, ai, adj_gleans[proto][sw_if_index],
     ({
         adj = adj_get(ai);
 
         if (adj->sub_type.glean.rx_pfx.fp_len > 0)
         {
+            source = &adj->sub_type.glean.rx_pfx.fp_addr;
+
             /* if no destination is specified use the just glean */
             if (NULL == nh)
-                return (&adj->sub_type.glean.rx_pfx.fp_addr);
+                return (source);
 
             /* check the clean covers the desintation */
             if (fib_prefix_is_cover(&adj->sub_type.glean.rx_pfx, &pfx))
-                return (&adj->sub_type.glean.rx_pfx.fp_addr);
+                return (source);
         }
     }));
 
-    return (NULL);
+    return (source);
 }
 
 void
@@ -415,6 +428,64 @@ adj_glean_interface_delete (vnet_main_t * vnm,
 
 VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_glean_interface_delete);
 
+/**
+ * Callback function invoked when an interface's MAC Address changes
+ */
+static void
+adj_glean_ethernet_change_mac (ethernet_main_t * em,
+                               u32 sw_if_index,
+                               uword opaque)
+{
+    adj_glean_walk (sw_if_index, adj_glean_update_rewrite_walk, NULL);
+}
+
+static void
+adj_glean_table_bind (fib_protocol_t fproto,
+                      u32 sw_if_index,
+                      u32 itf_fib_index)
+{
+    /*
+     * for each glean on the interface trigger a walk back to the children
+     */
+    fib_node_back_walk_ctx_t bw_ctx = {
+        .fnbw_reason =  FIB_NODE_BW_REASON_FLAG_INTERFACE_BIND,
+        .interface_bind = {
+            .fnbw_to_fib_index = itf_fib_index,
+        },
+    };
+
+    adj_glean_walk_proto (fproto, sw_if_index, adj_glean_start_backwalk, &bw_ctx);
+}
+
+
+/**
+ * Callback function invoked when an interface's IPv6 Table
+ * binding changes
+ */
+static void
+adj_glean_ip6_table_bind (ip6_main_t * im,
+                          uword opaque,
+                          u32 sw_if_index,
+                          u32 new_fib_index,
+                          u32 old_fib_index)
+{
+  adj_glean_table_bind (FIB_PROTOCOL_IP6, sw_if_index, new_fib_index);
+}
+
+/**
+ * Callback function invoked when an interface's IPv4 Table
+ * binding changes
+ */
+static void
+adj_glean_ip4_table_bind (ip4_main_t * im,
+                          uword opaque,
+                          u32 sw_if_index,
+                          u32 new_fib_index,
+                          u32 old_fib_index)
+{
+  adj_glean_table_bind (FIB_PROTOCOL_IP4, sw_if_index, new_fib_index);
+}
+
 u8*
 format_adj_glean (u8* s, va_list *ap)
 {
@@ -499,4 +570,20 @@ void
 adj_glean_module_init (void)
 {
     dpo_register(DPO_ADJACENCY_GLEAN, &adj_glean_dpo_vft, glean_nodes);
+
+    ethernet_address_change_ctx_t ctx = {
+        .function = adj_glean_ethernet_change_mac,
+        .function_opaque = 0,
+    };
+    vec_add1 (ethernet_main.address_change_callbacks, ctx);
+
+    ip6_table_bind_callback_t cbt6 = {
+        .function = adj_glean_ip6_table_bind,
+    };
+    vec_add1 (ip6_main.table_bind_callbacks, cbt6);
+
+    ip4_table_bind_callback_t cbt4 = {
+        .function = adj_glean_ip4_table_bind,
+    };
+    vec_add1 (ip4_main.table_bind_callbacks, cbt4);
 }