fib: Adjacency flag for midchain to perfom flow hash (on inner packet)

Type: feature

Signed-off-by: Neale Ranns <neale.ranns@cisco.com>
Change-Id: I964afd9266645de5c87d49c58ce6b48c2c18f97f
diff --git a/src/vnet/adj/adj.h b/src/vnet/adj/adj.h
index b29b1b0..44bb2bd 100644
--- a/src/vnet/adj/adj.h
+++ b/src/vnet/adj/adj.h
@@ -185,6 +185,12 @@
      * the fixup function is standard IP4o4 header
      */
     ADJ_ATTR_MIDCHAIN_FIXUP_IP4O4_HDR,
+    /**
+     * the fixup function performs the flow hash
+     * this means the flow hash is performed on the inner
+     * header, where the entropy is higher.
+     */
+    ADJ_ATTR_MIDCHAIN_FIXUP_FLOW_HASH,
 }  adj_attr_t;
 
 #define ADJ_ATTR_NAMES {                                        \
@@ -193,11 +199,12 @@
     [ADJ_ATTR_MIDCHAIN_IP_STACK] = "midchain-ip-stack",         \
     [ADJ_ATTR_MIDCHAIN_LOOPED] = "midchain-looped",             \
     [ADJ_ATTR_MIDCHAIN_FIXUP_IP4O4_HDR] = "midchain-ip4o4-hdr-fixup",   \
+    [ADJ_ATTR_MIDCHAIN_FIXUP_FLOW_HASH] = "midchain-flow-hash",   \
 }
 
 #define FOR_EACH_ADJ_ATTR(_attr)                        \
     for (_attr = ADJ_ATTR_SYNC_WALK_ACTIVE;             \
-	 _attr <= ADJ_ATTR_MIDCHAIN_FIXUP_IP4O4_HDR;    \
+	 _attr <= ADJ_ATTR_MIDCHAIN_FIXUP_FLOW_HASH;    \
 	 _attr++)
 
 /**
@@ -211,6 +218,7 @@
     ADJ_FLAG_MIDCHAIN_IP_STACK = (1 << ADJ_ATTR_MIDCHAIN_IP_STACK),
     ADJ_FLAG_MIDCHAIN_LOOPED = (1 << ADJ_ATTR_MIDCHAIN_LOOPED),
     ADJ_FLAG_MIDCHAIN_FIXUP_IP4O4_HDR = (1 << ADJ_ATTR_MIDCHAIN_FIXUP_IP4O4_HDR),
+    ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH = (1 << ADJ_ATTR_MIDCHAIN_FIXUP_FLOW_HASH),
 }  __attribute__ ((packed)) adj_flags_t;
 
 /**
diff --git a/src/vnet/adj/adj_dp.h b/src/vnet/adj/adj_dp.h
index 27c0581..aff1a2b 100644
--- a/src/vnet/adj/adj_dp.h
+++ b/src/vnet/adj/adj_dp.h
@@ -18,6 +18,9 @@
 
 #include <vnet/adj/adj.h>
 #include <vnet/tunnel/tunnel_dp.h>
+#include <vnet/ip/ip4_inlines.h>
+#include <vnet/ip/ip6_inlines.h>
+#include <vnet/mpls/mpls_lookup.h>
 
 static_always_inline void
 adj_midchain_ipip44_fixup (vlib_main_t * vm,
@@ -54,13 +57,32 @@
 static_always_inline void
 adj_midchain_fixup (vlib_main_t *vm,
                     const ip_adjacency_t *adj,
-                    vlib_buffer_t * b)
+                    vlib_buffer_t * b,
+                    vnet_link_t lt)
 {
-    if (PREDICT_TRUE(adj->rewrite_header.flags & VNET_REWRITE_FIXUP_IP4_O_4))
+    if (PREDICT_TRUE(adj->rewrite_header.flags &
+                     VNET_REWRITE_FIXUP_IP4_O_4))
         adj_midchain_ipip44_fixup (vm, adj, b);
     else if (adj->sub_type.midchain.fixup_func)
         adj->sub_type.midchain.fixup_func
             (vm, adj, b, adj->sub_type.midchain.fixup_data);
+
+    if (PREDICT_FALSE(adj->rewrite_header.flags &
+                      VNET_REWRITE_FIXUP_FLOW_HASH))
+    {
+        if (VNET_LINK_IP4 == lt)
+            vnet_buffer (b)->ip.flow_hash =
+                ip4_compute_flow_hash (vlib_buffer_get_current (b) + adj->rewrite_header.data_bytes,
+                                       IP_FLOW_HASH_DEFAULT);
+        else if (VNET_LINK_IP6 == lt)
+            vnet_buffer (b)->ip.flow_hash =
+                ip6_compute_flow_hash (vlib_buffer_get_current (b) + adj->rewrite_header.data_bytes,
+                                       IP_FLOW_HASH_DEFAULT);
+        else if (VNET_LINK_MPLS == lt)
+            vnet_buffer (b)->ip.flow_hash =
+                mpls_compute_flow_hash (vlib_buffer_get_current (b) + adj->rewrite_header.data_bytes,
+                                       IP_FLOW_HASH_DEFAULT);
+    }
 }
 
 #endif
diff --git a/src/vnet/adj/adj_midchain.c b/src/vnet/adj/adj_midchain.c
index 15fef86..93cfb55 100644
--- a/src/vnet/adj/adj_midchain.c
+++ b/src/vnet/adj/adj_midchain.c
@@ -403,6 +403,10 @@
     {
         adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_IP4_O_4;
     }
+    if (!(flags & ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH))
+    {
+        adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_FLOW_HASH;
+    }
 
     tx_node = adj_nbr_midchain_get_tx_node(adj);
 
@@ -582,39 +586,47 @@
     {
         fib_entry_contribute_forwarding (fei, fct, &tmp);
 
-        if ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_IP_STACK) &&
-            (DPO_LOAD_BALANCE == tmp.dpoi_type))
+        if (DPO_LOAD_BALANCE == tmp.dpoi_type)
         {
-            /*
-             * do that hash now and stack on the choice.
-             * If the choice is an incomplete adj then we will need a poke when
-             * it becomes complete. This happens since the adj update walk propagates
-             * as far a recursive paths.
-             */
-            const dpo_id_t *choice;
             load_balance_t *lb;
-            int hash;
 
             lb = load_balance_get (tmp.dpoi_index);
 
-            if (FIB_FORW_CHAIN_TYPE_UNICAST_IP4 == fct)
+            if ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_IP_STACK) ||
+                lb->lb_n_buckets == 1)
             {
-                hash = ip4_compute_flow_hash ((ip4_header_t *) adj_get_rewrite (ai),
-                                              lb->lb_hash_config);
-            }
-            else if (FIB_FORW_CHAIN_TYPE_UNICAST_IP6 == fct)
-            {
-                hash = ip6_compute_flow_hash ((ip6_header_t *) adj_get_rewrite (ai),
-                                              lb->lb_hash_config);
-            }
-            else
-            {
-                hash = 0;
-                ASSERT(0);
-            }
+                /*
+                 * do that hash now and stack on the choice.
+                 * If the choice is an incomplete adj then we will need a poke when
+                 * it becomes complete. This happens since the adj update walk propagates
+                 * as far a recursive paths.
+                 */
+                const dpo_id_t *choice;
+                int hash;
 
-            choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
-            dpo_copy (&tmp, choice);
+                if (FIB_FORW_CHAIN_TYPE_UNICAST_IP4 == fct)
+                {
+                    hash = ip4_compute_flow_hash ((ip4_header_t *) adj_get_rewrite (ai),
+                                                  lb->lb_hash_config);
+                }
+                else if (FIB_FORW_CHAIN_TYPE_UNICAST_IP6 == fct)
+                {
+                    hash = ip6_compute_flow_hash ((ip6_header_t *) adj_get_rewrite (ai),
+                                                  lb->lb_hash_config);
+                }
+                else
+                {
+                    hash = 0;
+                    ASSERT(0);
+                }
+
+                choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
+                dpo_copy (&tmp, choice);
+            }
+            else if (adj->ia_flags & ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH)
+            {
+                adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_FLOW_HASH;
+            }
         }
     }
     adj_nbr_midchain_stack (ai, &tmp);
diff --git a/src/vnet/adj/rewrite.h b/src/vnet/adj/rewrite.h
index c23edbe..496bb14 100644
--- a/src/vnet/adj/rewrite.h
+++ b/src/vnet/adj/rewrite.h
@@ -60,6 +60,11 @@
    * this adj performs IP4 over IP4 fixup
    */
   VNET_REWRITE_FIXUP_IP4_O_4 = (1 << 1),
+
+  /**
+   * this adj performs the flow hash fixup
+   */
+  VNET_REWRITE_FIXUP_FLOW_HASH = (1 << 2),
 } __attribute__ ((packed)) vnet_rewrite_flags_t;
 
 typedef struct vnet_rewrite_header_t_
diff --git a/src/vnet/ip/ip4_forward.c b/src/vnet/ip/ip4_forward.c
index 5903ef8..fdc92ca 100644
--- a/src/vnet/ip/ip4_forward.c
+++ b/src/vnet/ip/ip4_forward.c
@@ -2265,9 +2265,9 @@
       if (is_midchain)
 	{
 	  if (error0 == IP4_ERROR_NONE)
-	    adj_midchain_fixup (vm, adj0, b[0]);
+	    adj_midchain_fixup (vm, adj0, b[0], VNET_LINK_IP4);
 	  if (error1 == IP4_ERROR_NONE)
-	    adj_midchain_fixup (vm, adj1, b[1]);
+	    adj_midchain_fixup (vm, adj1, b[1], VNET_LINK_IP4);
 	}
 
       if (is_mcast)
@@ -2399,7 +2399,7 @@
 							   b[0]) + rw_len0);
 
 	  if (is_midchain)
-	    adj_midchain_fixup (vm, adj0, b[0]);
+	    adj_midchain_fixup (vm, adj0, b[0], VNET_LINK_IP4);
 
 	  if (is_mcast)
 	    /* copy bytes from the IP address into the MAC rewrite */
@@ -2502,7 +2502,7 @@
 	       vlib_buffer_length_in_chain (vm, b[0]) + rw_len0);
 
 	  if (is_midchain)
-	    adj_midchain_fixup (vm, adj0, b[0]);
+	    adj_midchain_fixup (vm, adj0, b[0], VNET_LINK_IP4);
 
 	  if (is_mcast)
 	    /* copy bytes from the IP address into the MAC rewrite */