ip: Use the IP4 lookup functions

Type: refactor

as opposed to wrtiing out the mtrie steps one by one each time.

Signed-off-by: Neale Ranns <neale@graphiant.com>
Change-Id: I1248861350f9189f9a67ac6e68940813af279e03
diff --git a/src/plugins/adl/ip4_allowlist.c b/src/plugins/adl/ip4_allowlist.c
index 316e2cb..4c75572 100644
--- a/src/plugins/adl/ip4_allowlist.c
+++ b/src/plugins/adl/ip4_allowlist.c
@@ -82,21 +82,19 @@
           ip4_header_t * ip0, * ip1;
           adl_config_main_t * ccm0, * ccm1;
           adl_config_data_t * c0, * c1;
-      	  ip4_fib_mtrie_t * mtrie0, * mtrie1;
-      	  ip4_fib_mtrie_leaf_t leaf0, leaf1;
-          u32 lb_index0, lb_index1;
-          const load_balance_t * lb0, *lb1;
-          const dpo_id_t *dpo0, *dpo1;
+	  u32 lb_index0, lb_index1;
+	  const load_balance_t *lb0, *lb1;
+	  const dpo_id_t *dpo0, *dpo1;
 
-      	  /* Prefetch next iteration. */
-      	  {
-      	    vlib_buffer_t * p2, * p3;
+	  /* Prefetch next iteration. */
+	  {
+	    vlib_buffer_t *p2, *p3;
 
-      	    p2 = vlib_get_buffer (vm, from[2]);
-      	    p3 = vlib_get_buffer (vm, from[3]);
+	    p2 = vlib_get_buffer (vm, from[2]);
+	    p3 = vlib_get_buffer (vm, from[3]);
 
-      	    vlib_prefetch_buffer_header (p2, LOAD);
-      	    vlib_prefetch_buffer_header (p3, LOAD);
+	    vlib_prefetch_buffer_header (p2, LOAD);
+	    vlib_prefetch_buffer_header (p3, LOAD);
 
 	    clib_prefetch_store (p2->data);
 	    clib_prefetch_store (p3->data);
@@ -121,17 +119,8 @@
 	    &ccm0->config_main, &adl_buffer (b0)->adl.current_config_index,
 	    &next0, sizeof (c0[0]));
 
-	  mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
-
-          leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);
-
-      	  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
-                                             &ip0->src_address, 2);
-
-      	  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
-                                             &ip0->src_address, 3);
-
-      	  lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
+	  lb_index0 =
+	    ip4_fib_forwarding_lookup (c0->fib_index, &ip0->src_address);
 
 	  ASSERT (lb_index0
                   == ip4_fib_table_lookup_lb (ip4_fib_get(c0->fib_index),
@@ -158,17 +147,10 @@
                &adl_buffer (b1)->adl.current_config_index,
                &next1,
                sizeof (c1[0]));
-	  mtrie1 = &ip4_fib_get (c1->fib_index)->mtrie;
 
-          leaf1 = ip4_fib_mtrie_lookup_step_one (mtrie1, &ip1->src_address);
+	  lb_index1 =
+	    ip4_fib_forwarding_lookup (c1->fib_index, &ip1->src_address);
 
-      	  leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
-                                             &ip1->src_address, 2);
-
-      	  leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
-                                             &ip1->src_address, 3);
-
-      	  lb_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
 	  ASSERT (lb_index1
                   == ip4_fib_table_lookup_lb (ip4_fib_get(c1->fib_index),
 	  				       &ip1->src_address));
@@ -226,13 +208,11 @@
           ip4_header_t * ip0;
           adl_config_main_t *ccm0;
           adl_config_data_t *c0;
-	  ip4_fib_mtrie_t * mtrie0;
-	  ip4_fib_mtrie_leaf_t leaf0;
-          u32 lb_index0;
-          const load_balance_t * lb0;
-          const dpo_id_t *dpo0;
+	  u32 lb_index0;
+	  const load_balance_t *lb0;
+	  const dpo_id_t *dpo0;
 
-          /* speculatively enqueue b0 to the current next frame */
+	  /* speculatively enqueue b0 to the current next frame */
 	  bi0 = from[0];
 	  to_next[0] = bi0;
 	  from += 1;
@@ -253,21 +233,12 @@
                &next0,
                sizeof (c0[0]));
 
-	  mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
+	  lb_index0 =
+	    ip4_fib_forwarding_lookup (c0->fib_index, &ip0->src_address);
 
-          leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);
-
-	  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
-                                             &ip0->src_address, 2);
-
-	  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
-                                             &ip0->src_address, 3);
-
-	  lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
-
-	  ASSERT (lb_index0
-                  == ip4_fib_table_lookup_lb (ip4_fib_get(c0->fib_index),
-					      &ip0->src_address));
+	  ASSERT (lb_index0 ==
+		  ip4_fib_table_lookup_lb (ip4_fib_get (c0->fib_index),
+					   &ip0->src_address));
 
 	  lb0 = load_balance_get (lb_index0);
           dpo0 = load_balance_get_bucket_i(lb0, 0);
diff --git a/src/vnet/dpo/lookup_dpo.c b/src/vnet/dpo/lookup_dpo.c
index 3f34cef..9ce94ee 100644
--- a/src/vnet/dpo/lookup_dpo.c
+++ b/src/vnet/dpo/lookup_dpo.c
@@ -268,50 +268,6 @@
     }
 }
 
-always_inline void
-ip4_src_fib_lookup_one (u32 src_fib_index0,
-                        const ip4_address_t * addr0,
-                        u32 * src_adj_index0)
-{
-    ip4_fib_mtrie_leaf_t leaf0;
-    ip4_fib_mtrie_t * mtrie0;
-
-    mtrie0 = &ip4_fib_get (src_fib_index0)->mtrie;
-
-    leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, addr0);
-    leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 2);
-    leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 3);
-
-    src_adj_index0[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
-}
-
-always_inline void
-ip4_src_fib_lookup_two (u32 src_fib_index0,
-                        u32 src_fib_index1,
-                        const ip4_address_t * addr0,
-                        const ip4_address_t * addr1,
-                        u32 * src_adj_index0,
-                        u32 * src_adj_index1)
-{
-    ip4_fib_mtrie_leaf_t leaf0, leaf1;
-    ip4_fib_mtrie_t * mtrie0, * mtrie1;
-
-    mtrie0 = &ip4_fib_get (src_fib_index0)->mtrie;
-    mtrie1 = &ip4_fib_get (src_fib_index1)->mtrie;
-
-    leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, addr0);
-    leaf1 = ip4_fib_mtrie_lookup_step_one (mtrie1, addr1);
-
-    leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 2);
-    leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 2);
-
-    leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, addr0, 3);
-    leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, addr1, 3);
-
-    src_adj_index0[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
-    src_adj_index1[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
-}
-
 /**
  * @brief Lookup trace  data
  */
@@ -435,9 +391,8 @@
 	    }
 
 	    /* do lookup */
-	    ip4_src_fib_lookup_two (fib_index0, fib_index1,
-                                    input_addr0, input_addr1,
-                                    &lbi0, &lbi1);
+	    ip4_fib_forwarding_lookup_x2 (fib_index0, fib_index1, input_addr0,
+					  input_addr1, &lbi0, &lbi1);
 	    lb0 = load_balance_get(lbi0);
 	    lb1 = load_balance_get(lbi1);
 
@@ -573,7 +528,7 @@
 	    }
 
 	    /* do lookup */
-	    ip4_src_fib_lookup_one (fib_index0, input_addr, &lbi0);
+	    lbi0 = ip4_fib_forwarding_lookup (fib_index0, input_addr);
 	    lb0 = load_balance_get(lbi0);
 
             vnet_buffer(b0)->sw_if_index[VLIB_TX] = fib_index0;
diff --git a/src/vnet/fib/ip4_fib.h b/src/vnet/fib/ip4_fib.h
index 7d17baf..dc41eb8 100644
--- a/src/vnet/fib/ip4_fib.h
+++ b/src/vnet/fib/ip4_fib.h
@@ -197,5 +197,47 @@
     *lb1 = ip4_fib_mtrie_leaf_get_adj_index(leaf[1]);
 }
 
-#endif
+static_always_inline void
+ip4_fib_forwarding_lookup_x4 (u32 fib_index0,
+                              u32 fib_index1,
+                              u32 fib_index2,
+                              u32 fib_index3,
+                              const ip4_address_t * addr0,
+                              const ip4_address_t * addr1,
+                              const ip4_address_t * addr2,
+                              const ip4_address_t * addr3,
+                              index_t *lb0,
+                              index_t *lb1,
+                              index_t *lb2,
+                              index_t *lb3)
+{
+    ip4_fib_mtrie_leaf_t leaf[4];
+    ip4_fib_mtrie_t * mtrie[4];
 
+    mtrie[0] = &ip4_fib_get(fib_index0)->mtrie;
+    mtrie[1] = &ip4_fib_get(fib_index1)->mtrie;
+    mtrie[2] = &ip4_fib_get(fib_index2)->mtrie;
+    mtrie[3] = &ip4_fib_get(fib_index3)->mtrie;
+
+    leaf[0] = ip4_fib_mtrie_lookup_step_one (mtrie[0], addr0);
+    leaf[1] = ip4_fib_mtrie_lookup_step_one (mtrie[1], addr1);
+    leaf[2] = ip4_fib_mtrie_lookup_step_one (mtrie[2], addr2);
+    leaf[3] = ip4_fib_mtrie_lookup_step_one (mtrie[3], addr3);
+
+    leaf[0] = ip4_fib_mtrie_lookup_step (mtrie[0], leaf[0], addr0, 2);
+    leaf[1] = ip4_fib_mtrie_lookup_step (mtrie[1], leaf[1], addr1, 2);
+    leaf[2] = ip4_fib_mtrie_lookup_step (mtrie[2], leaf[2], addr2, 2);
+    leaf[3] = ip4_fib_mtrie_lookup_step (mtrie[3], leaf[3], addr3, 2);
+
+    leaf[0] = ip4_fib_mtrie_lookup_step (mtrie[0], leaf[0], addr0, 3);
+    leaf[1] = ip4_fib_mtrie_lookup_step (mtrie[1], leaf[1], addr1, 3);
+    leaf[2] = ip4_fib_mtrie_lookup_step (mtrie[2], leaf[2], addr2, 3);
+    leaf[3] = ip4_fib_mtrie_lookup_step (mtrie[3], leaf[3], addr3, 3);
+
+    *lb0 = ip4_fib_mtrie_leaf_get_adj_index(leaf[0]);
+    *lb1 = ip4_fib_mtrie_leaf_get_adj_index(leaf[1]);
+    *lb2 = ip4_fib_mtrie_leaf_get_adj_index(leaf[2]);
+    *lb3 = ip4_fib_mtrie_leaf_get_adj_index(leaf[3]);
+}
+
+#endif
diff --git a/src/vnet/ip/ip4_forward.c b/src/vnet/ip/ip4_forward.c
index be38171..ea67005 100644
--- a/src/vnet/ip/ip4_forward.c
+++ b/src/vnet/ip/ip4_forward.c
@@ -1517,8 +1517,6 @@
 ip4_local_check_src (vlib_buffer_t * b, ip4_header_t * ip0,
 		     ip4_local_last_check_t * last_check, u8 * error0)
 {
-  ip4_fib_mtrie_leaf_t leaf0;
-  ip4_fib_mtrie_t *mtrie0;
   const dpo_id_t *dpo0;
   load_balance_t *lb0;
   u32 lbi0;
@@ -1536,11 +1534,8 @@
   if (PREDICT_TRUE (last_check->src.as_u32 != ip0->src_address.as_u32) ||
       last_check->first)
     {
-      mtrie0 = &ip4_fib_get (vnet_buffer (b)->ip.fib_index)->mtrie;
-      leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);
-      leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2);
-      leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3);
-      lbi0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
+      lbi0 = ip4_fib_forwarding_lookup (vnet_buffer (b)->ip.fib_index,
+					&ip0->src_address);
 
       vnet_buffer (b)->ip.adj_index[VLIB_RX] =
 	vnet_buffer (b)->ip.adj_index[VLIB_TX];
@@ -1586,8 +1581,6 @@
 ip4_local_check_src_x2 (vlib_buffer_t ** b, ip4_header_t ** ip,
 			ip4_local_last_check_t * last_check, u8 * error)
 {
-  ip4_fib_mtrie_leaf_t leaf[2];
-  ip4_fib_mtrie_t *mtrie[2];
   const dpo_id_t *dpo[2];
   load_balance_t *lb[2];
   u32 not_last_hit;
@@ -1615,24 +1608,9 @@
    */
   if (PREDICT_TRUE (not_last_hit))
     {
-      mtrie[0] = &ip4_fib_get (vnet_buffer (b[0])->ip.fib_index)->mtrie;
-      mtrie[1] = &ip4_fib_get (vnet_buffer (b[1])->ip.fib_index)->mtrie;
-
-      leaf[0] = ip4_fib_mtrie_lookup_step_one (mtrie[0], &ip[0]->src_address);
-      leaf[1] = ip4_fib_mtrie_lookup_step_one (mtrie[1], &ip[1]->src_address);
-
-      leaf[0] = ip4_fib_mtrie_lookup_step (mtrie[0], leaf[0],
-					   &ip[0]->src_address, 2);
-      leaf[1] = ip4_fib_mtrie_lookup_step (mtrie[1], leaf[1],
-					   &ip[1]->src_address, 2);
-
-      leaf[0] = ip4_fib_mtrie_lookup_step (mtrie[0], leaf[0],
-					   &ip[0]->src_address, 3);
-      leaf[1] = ip4_fib_mtrie_lookup_step (mtrie[1], leaf[1],
-					   &ip[1]->src_address, 3);
-
-      lbi[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf[0]);
-      lbi[1] = ip4_fib_mtrie_leaf_get_adj_index (leaf[1]);
+      ip4_fib_forwarding_lookup_x2 (
+	vnet_buffer (b[0])->ip.fib_index, vnet_buffer (b[1])->ip.fib_index,
+	&ip[0]->src_address, &ip[1]->src_address, &lbi[0], &lbi[1]);
 
       vnet_buffer (b[0])->ip.adj_index[VLIB_RX] =
 	vnet_buffer (b[0])->ip.adj_index[VLIB_TX];
diff --git a/src/vnet/ip/ip4_forward.h b/src/vnet/ip/ip4_forward.h
index 8779d2d..54150d4 100644
--- a/src/vnet/ip/ip4_forward.h
+++ b/src/vnet/ip/ip4_forward.h
@@ -74,8 +74,6 @@
     {
       ip4_header_t *ip0, *ip1, *ip2, *ip3;
       const load_balance_t *lb0, *lb1, *lb2, *lb3;
-      ip4_fib_mtrie_t *mtrie0, *mtrie1, *mtrie2, *mtrie3;
-      ip4_fib_mtrie_leaf_t leaf0, leaf1, leaf2, leaf3;
       ip4_address_t *dst_addr0, *dst_addr1, *dst_addr2, *dst_addr3;
       u32 lb_index0, lb_index1, lb_index2, lb_index3;
       flow_hash_config_t flow_hash_config0, flow_hash_config1;
@@ -112,30 +110,11 @@
       ip_lookup_set_buffer_fib_index (im->fib_index_by_sw_if_index, b[2]);
       ip_lookup_set_buffer_fib_index (im->fib_index_by_sw_if_index, b[3]);
 
-      mtrie0 = &ip4_fib_get (vnet_buffer (b[0])->ip.fib_index)->mtrie;
-      mtrie1 = &ip4_fib_get (vnet_buffer (b[1])->ip.fib_index)->mtrie;
-      mtrie2 = &ip4_fib_get (vnet_buffer (b[2])->ip.fib_index)->mtrie;
-      mtrie3 = &ip4_fib_get (vnet_buffer (b[3])->ip.fib_index)->mtrie;
-
-      leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, dst_addr0);
-      leaf1 = ip4_fib_mtrie_lookup_step_one (mtrie1, dst_addr1);
-      leaf2 = ip4_fib_mtrie_lookup_step_one (mtrie2, dst_addr2);
-      leaf3 = ip4_fib_mtrie_lookup_step_one (mtrie3, dst_addr3);
-
-      leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 2);
-      leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 2);
-      leaf2 = ip4_fib_mtrie_lookup_step (mtrie2, leaf2, dst_addr2, 2);
-      leaf3 = ip4_fib_mtrie_lookup_step (mtrie3, leaf3, dst_addr3, 2);
-
-      leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 3);
-      leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 3);
-      leaf2 = ip4_fib_mtrie_lookup_step (mtrie2, leaf2, dst_addr2, 3);
-      leaf3 = ip4_fib_mtrie_lookup_step (mtrie3, leaf3, dst_addr3, 3);
-
-      lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
-      lb_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
-      lb_index2 = ip4_fib_mtrie_leaf_get_adj_index (leaf2);
-      lb_index3 = ip4_fib_mtrie_leaf_get_adj_index (leaf3);
+      ip4_fib_forwarding_lookup_x4 (
+	vnet_buffer (b[0])->ip.fib_index, vnet_buffer (b[1])->ip.fib_index,
+	vnet_buffer (b[2])->ip.fib_index, vnet_buffer (b[3])->ip.fib_index,
+	dst_addr0, dst_addr1, dst_addr2, dst_addr3, &lb_index0, &lb_index1,
+	&lb_index2, &lb_index3);
 
       ASSERT (lb_index0 && lb_index1 && lb_index2 && lb_index3);
       lb0 = load_balance_get (lb_index0);
@@ -245,8 +224,6 @@
     {
       ip4_header_t *ip0, *ip1;
       const load_balance_t *lb0, *lb1;
-      ip4_fib_mtrie_t *mtrie0, *mtrie1;
-      ip4_fib_mtrie_leaf_t leaf0, leaf1;
       ip4_address_t *dst_addr0, *dst_addr1;
       u32 lb_index0, lb_index1;
       flow_hash_config_t flow_hash_config0, flow_hash_config1;
@@ -271,20 +248,9 @@
       ip_lookup_set_buffer_fib_index (im->fib_index_by_sw_if_index, b[0]);
       ip_lookup_set_buffer_fib_index (im->fib_index_by_sw_if_index, b[1]);
 
-      mtrie0 = &ip4_fib_get (vnet_buffer (b[0])->ip.fib_index)->mtrie;
-      mtrie1 = &ip4_fib_get (vnet_buffer (b[1])->ip.fib_index)->mtrie;
-
-      leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, dst_addr0);
-      leaf1 = ip4_fib_mtrie_lookup_step_one (mtrie1, dst_addr1);
-
-      leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 2);
-      leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 2);
-
-      leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 3);
-      leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, dst_addr1, 3);
-
-      lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
-      lb_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
+      ip4_fib_forwarding_lookup_x2 (
+	vnet_buffer (b[0])->ip.fib_index, vnet_buffer (b[1])->ip.fib_index,
+	dst_addr0, dst_addr1, &lb_index0, &lb_index1);
 
       ASSERT (lb_index0 && lb_index1);
       lb0 = load_balance_get (lb_index0);
@@ -348,8 +314,6 @@
     {
       ip4_header_t *ip0;
       const load_balance_t *lb0;
-      ip4_fib_mtrie_t *mtrie0;
-      ip4_fib_mtrie_leaf_t leaf0;
       ip4_address_t *dst_addr0;
       u32 lbi0;
       flow_hash_config_t flow_hash_config0;
@@ -360,11 +324,8 @@
       dst_addr0 = &ip0->dst_address;
       ip_lookup_set_buffer_fib_index (im->fib_index_by_sw_if_index, b[0]);
 
-      mtrie0 = &ip4_fib_get (vnet_buffer (b[0])->ip.fib_index)->mtrie;
-      leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, dst_addr0);
-      leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 2);
-      leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, dst_addr0, 3);
-      lbi0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
+      lbi0 = ip4_fib_forwarding_lookup (vnet_buffer (b[0])->ip.fib_index,
+					dst_addr0);
 
       ASSERT (lbi0);
       lb0 = load_balance_get (lbi0);