c11 safe string handling support
[vpp.git] / src / plugins / acl / public_inlines.h
index e7e47c4..850babf 100644 (file)
 
 #include <stdint.h>
 
+#include <vlib/unix/plugin.h>
 #include <plugins/acl/acl.h>
 #include <plugins/acl/fa_node.h>
 #include <plugins/acl/hash_lookup_private.h>
 
-
-/* check if a given ACL exists */
-
-#ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
-
-/*
- * Define a pointer to the acl_main which will be filled during the initialization.
- */
-acl_main_t *p_acl_main = 0;
-
-/*
- * If the file is included more than once, the symbol collision will make the problem obvious.
- * If the include is done only once, it is just a lonely null var
- * sitting around.
- */
-void *ERROR_ACL_PLUGIN_EXPORTS_FILE_MUST_BE_INCLUDED_ONLY_IN_ONE_PLACE = 0;
-
-u8 (*acl_plugin_acl_exists) (u32 acl_index);
-#else
-u8 acl_plugin_acl_exists (u32 acl_index);
-#endif
-
-
-/*
- * If you are using ACL plugin, get this unique ID first,
- * so you can identify yourself when creating the lookup contexts.
- */
-
-#ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
-u32 (*acl_plugin_register_user_module) (char *caller_module_string, char *val1_label, char *val2_label);
-#else
-u32 acl_plugin_register_user_module (char *caller_module_string, char *val1_label, char *val2_label);
-#endif
-
-/*
- * Allocate a new lookup context index.
- * Supply the id assigned to your module during registration,
- * and two values of your choice identifying instances
- * of use within your module. They are useful for debugging.
- */
-#ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
-int (*acl_plugin_get_lookup_context_index) (u32 acl_user_id, u32 val1, u32 val2);
-#else
-int acl_plugin_get_lookup_context_index (u32 acl_user_id, u32 val1, u32 val2);
-#endif
-
-/*
- * Release the lookup context index and destroy
- * any asssociated data structures.
- */
-#ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
-void (*acl_plugin_put_lookup_context_index) (u32 lc_index);
-#else
-void acl_plugin_put_lookup_context_index (u32 lc_index);
-#endif
-
-/*
- * Prepare the sequential vector of ACL#s to lookup within a given context.
- * Any existing list will be overwritten. acl_list is a vector.
- */
-#ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
-int (*acl_plugin_set_acl_vec_for_context) (u32 lc_index, u32 *acl_list);
-#else
-int acl_plugin_set_acl_vec_for_context (u32 lc_index, u32 *acl_list);
-#endif
-
-/* Fill the 5-tuple from the packet */
-
-#ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
-void (*acl_plugin_fill_5tuple) (u32 lc_index, vlib_buffer_t * b0, int is_ip6, int is_input,
-                                int is_l2_path, fa_5tuple_opaque_t * p5tuple_pkt);
-#else
-void acl_plugin_fill_5tuple (u32 lc_index, vlib_buffer_t * b0, int is_ip6, int is_input,
-                                int is_l2_path, fa_5tuple_opaque_t * p5tuple_pkt);
-#endif
-
-#ifdef ACL_PLUGIN_DEFINED_BELOW_IN_FILE
-static inline
-void acl_plugin_fill_5tuple_inline (u32 lc_index, vlib_buffer_t * b0, int is_ip6, int is_input,
-                                int is_l2_path, fa_5tuple_opaque_t * p5tuple_pkt) {
-  /* FIXME: normally the inlined version of filling in the 5-tuple. But for now just call the non-inlined version */
-  acl_plugin_fill_5tuple(lc_index, b0, is_ip6, is_input, is_l2_path, p5tuple_pkt);
-}
-#endif
-
-
-#ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
-int (*acl_plugin_match_5tuple) (u32 lc_index,
-                                           fa_5tuple_opaque_t * pkt_5tuple,
-                                           int is_ip6, u8 * r_action,
-                                           u32 * r_acl_pos_p,
-                                           u32 * r_acl_match_p,
-                                           u32 * r_rule_match_p,
-                                           u32 * trace_bitmap);
-#else
-int acl_plugin_match_5tuple (u32 lc_index,
-                                           fa_5tuple_opaque_t * pkt_5tuple,
-                                           int is_ip6, u8 * r_action,
-                                           u32 * r_acl_pos_p,
-                                           u32 * r_acl_match_p,
-                                           u32 * r_rule_match_p,
-                                           u32 * trace_bitmap);
-#endif
-
-#ifdef ACL_PLUGIN_DEFINED_BELOW_IN_FILE
-static inline int
-acl_plugin_match_5tuple_inline (u32 lc_index,
-                                           fa_5tuple_opaque_t * pkt_5tuple,
-                                           int is_ip6, u8 * r_action,
-                                           u32 * r_acl_pos_p,
-                                           u32 * r_acl_match_p,
-                                           u32 * r_rule_match_p,
-                                           u32 * trace_bitmap) {
-  return acl_plugin_match_5tuple(lc_index, pkt_5tuple, is_ip6, r_action, r_acl_pos_p, r_acl_match_p, r_rule_match_p, trace_bitmap);
-}
-#endif
-
-#ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
+#include <plugins/acl/exported_types.h>
 
 #define LOAD_SYMBOL_FROM_PLUGIN_TO(p, s, st)                              \
 ({                                                                        \
@@ -151,22 +35,14 @@ acl_plugin_match_5tuple_inline (u32 lc_index,
 
 #define LOAD_SYMBOL(s) LOAD_SYMBOL_FROM_PLUGIN_TO("acl_plugin.so", s, s)
 
-static inline clib_error_t * acl_plugin_exports_init (void)
-{
-    LOAD_SYMBOL_FROM_PLUGIN_TO("acl_plugin.so", acl_main, p_acl_main);
-    LOAD_SYMBOL(acl_plugin_acl_exists);
-    LOAD_SYMBOL(acl_plugin_register_user_module);
-    LOAD_SYMBOL(acl_plugin_get_lookup_context_index);
-    LOAD_SYMBOL(acl_plugin_put_lookup_context_index);
-    LOAD_SYMBOL(acl_plugin_set_acl_vec_for_context);
-    LOAD_SYMBOL(acl_plugin_fill_5tuple);
-    LOAD_SYMBOL(acl_plugin_match_5tuple);
-    return 0;
-}
-
-#endif
 
+static inline clib_error_t * acl_plugin_exports_init (acl_plugin_methods_t *m)
+{
+    acl_plugin_methods_vtable_init_fn_t mvi;
 
+    LOAD_SYMBOL_FROM_PLUGIN_TO("acl_plugin.so", acl_plugin_methods_vtable_init, mvi);
+    return (mvi(m));
+}
 
 always_inline void *
 get_ptr_to_offset (vlib_buffer_t * b0, int offset)
@@ -182,42 +58,46 @@ offset_within_packet (vlib_buffer_t * b0, int offset)
   return (offset <= (b0->current_length - 8));
 }
 
+
 always_inline void
-acl_fill_5tuple (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
-                int is_input, int is_l2_path, fa_5tuple_t * p5tuple_pkt)
+acl_fill_5tuple_l3_data (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
+                int l3_offset, fa_5tuple_t * p5tuple_pkt)
 {
-  /* IP4 and IP6 protocol numbers of ICMP */
-  static u8 icmp_protos_v4v6[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
-
-  int l3_offset;
-  int l4_offset;
-  u16 ports[2];
-  u16 proto;
-
-  if (is_l2_path)
+  if (is_ip6)
     {
-      l3_offset = ethernet_buffer_header_size(b0);
+      clib_memcpy (&p5tuple_pkt->ip6_addr,
+                  get_ptr_to_offset (b0,
+                                     offsetof (ip6_header_t,
+                                               src_address) + l3_offset),
+                  sizeof (p5tuple_pkt->ip6_addr));
     }
   else
     {
-      if (is_input)
-        l3_offset = 0;
-      else
-        l3_offset = vnet_buffer(b0)->ip.save_rewrite_length;
+      clib_memset(p5tuple_pkt->l3_zero_pad, 0, sizeof(p5tuple_pkt->l3_zero_pad));
+      clib_memcpy (&p5tuple_pkt->ip4_addr,
+                  get_ptr_to_offset (b0,
+                                     offsetof (ip4_header_t,
+                                               src_address) + l3_offset),
+                  sizeof (p5tuple_pkt->ip4_addr));
     }
+}
 
-  /* key[0..3] contains src/dst address and is cleared/set below */
-  /* Remainder of the key and per-packet non-key data */
-  p5tuple_pkt->kv.key[4] = 0;
-  p5tuple_pkt->kv.value = 0;
+always_inline void
+acl_fill_5tuple_l4_and_pkt_data (acl_main_t * am, u32 sw_if_index0, vlib_buffer_t * b0, int is_ip6, int is_input,
+                int l3_offset, fa_session_l4_key_t *p5tuple_l4, fa_packet_info_t *p5tuple_pkt)
+{
+  /* IP4 and IP6 protocol numbers of ICMP */
+  static u8 icmp_protos_v4v6[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
+
+  int l4_offset;
+  u16 ports[2];
+  u8 proto;
+
+  fa_session_l4_key_t tmp_l4 = { .lsb_of_sw_if_index = sw_if_index0 & 0xffff };
+  fa_packet_info_t tmp_pkt = { .is_ip6 = is_ip6, .mask_type_index_lsb = ~0 };
 
   if (is_ip6)
     {
-      clib_memcpy (&p5tuple_pkt->addr,
-                  get_ptr_to_offset (b0,
-                                     offsetof (ip6_header_t,
-                                               src_address) + l3_offset),
-                  sizeof (p5tuple_pkt->addr));
       proto =
        *(u8 *) get_ptr_to_offset (b0,
                                   offsetof (ip6_header_t,
@@ -242,7 +122,7 @@ acl_fill_5tuple (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
                  frag_offset = clib_net_to_host_u16(frag_offset) >> 3;
                  if (frag_offset)
                    {
-                      p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
+                      tmp_pkt.is_nonfirst_fragment = 1;
                       /* invalidate L4 offset so we don't try to find L4 info */
                       l4_offset += b0->current_length;
                    }
@@ -269,20 +149,6 @@ acl_fill_5tuple (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
     }
   else
     {
-      p5tuple_pkt->kv.key[0] = 0;
-      p5tuple_pkt->kv.key[1] = 0;
-      p5tuple_pkt->kv.key[2] = 0;
-      p5tuple_pkt->kv.key[3] = 0;
-      clib_memcpy (&p5tuple_pkt->addr[0].ip4,
-                  get_ptr_to_offset (b0,
-                                     offsetof (ip4_header_t,
-                                               src_address) + l3_offset),
-                  sizeof (p5tuple_pkt->addr[0].ip4));
-      clib_memcpy (&p5tuple_pkt->addr[1].ip4,
-                  get_ptr_to_offset (b0,
-                                     offsetof (ip4_header_t,
-                                               dst_address) + l3_offset),
-                  sizeof (p5tuple_pkt->addr[1].ip4));
       proto =
        *(u8 *) get_ptr_to_offset (b0,
                                   offsetof (ip4_header_t,
@@ -299,28 +165,31 @@ acl_fill_5tuple (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
       /* non-initial fragments have non-zero offset */
       if ((PREDICT_FALSE(0xfff & flags_and_fragment_offset)))
         {
-          p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
+          tmp_pkt.is_nonfirst_fragment = 1;
           /* invalidate L4 offset so we don't try to find L4 info */
           l4_offset += b0->current_length;
         }
 
     }
-  p5tuple_pkt->l4.proto = proto;
+  tmp_l4.proto = proto;
+  tmp_l4.is_input = is_input;
+
   if (PREDICT_TRUE (offset_within_packet (b0, l4_offset)))
     {
-      p5tuple_pkt->pkt.l4_valid = 1;
+      tmp_pkt.l4_valid = 1;
       if (icmp_protos_v4v6[is_ip6] == proto)
        {
          /* type */
-         p5tuple_pkt->l4.port[0] =
+         tmp_l4.port[0] =
            *(u8 *) get_ptr_to_offset (b0,
                                       l4_offset + offsetof (icmp46_header_t,
                                                             type));
          /* code */
-         p5tuple_pkt->l4.port[1] =
+         tmp_l4.port[1] =
            *(u8 *) get_ptr_to_offset (b0,
                                       l4_offset + offsetof (icmp46_header_t,
                                                             code));
+          tmp_l4.is_slowpath = 1;
        }
       else if ((IP_PROTOCOL_TCP == proto) || (IP_PROTOCOL_UDP == proto))
        {
@@ -329,53 +198,84 @@ acl_fill_5tuple (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
                                          l4_offset + offsetof (tcp_header_t,
                                                                src_port)),
                       sizeof (ports));
-         p5tuple_pkt->l4.port[0] = clib_net_to_host_u16 (ports[0]);
-         p5tuple_pkt->l4.port[1] = clib_net_to_host_u16 (ports[1]);
+         tmp_l4.port[0] = clib_net_to_host_u16 (ports[0]);
+         tmp_l4.port[1] = clib_net_to_host_u16 (ports[1]);
 
-         p5tuple_pkt->pkt.tcp_flags =
+         tmp_pkt.tcp_flags =
            *(u8 *) get_ptr_to_offset (b0,
                                       l4_offset + offsetof (tcp_header_t,
                                                             flags));
-         p5tuple_pkt->pkt.tcp_flags_valid = (proto == IP_PROTOCOL_TCP);
+         tmp_pkt.tcp_flags_valid = (proto == IP_PROTOCOL_TCP);
+          tmp_l4.is_slowpath = 0;
        }
-      /*
-       * FIXME: rather than the above conditional, here could
-       * be a nice generic mechanism to extract two L4 values:
-       *
-       * have a per-protocol array of 4 elements like this:
-       *   u8 offset; to take the byte from, off L4 header
-       *   u8 mask; to mask it with, before storing
-       *
-       * this way we can describe UDP, TCP and ICMP[46] semantics,
-       * and add a sort of FPM-type behavior for other protocols.
-       *
-       * Of course, is it faster ? and is it needed ?
-       *
-       */
+      else
+        {
+          tmp_l4.is_slowpath = 1;
+        }
     }
+
+  p5tuple_pkt->as_u64 = tmp_pkt.as_u64;
+  p5tuple_l4->as_u64 = tmp_l4.as_u64;
 }
 
 always_inline void
-acl_plugin_fill_5tuple_inline (u32 lc_index, vlib_buffer_t * b0, int is_ip6,
+acl_fill_5tuple (acl_main_t * am, u32 sw_if_index0, vlib_buffer_t * b0, int is_ip6,
+                int is_input, int is_l2_path, fa_5tuple_t * p5tuple_pkt)
+{
+  int l3_offset;
+
+  if (is_l2_path)
+    {
+      l3_offset = ethernet_buffer_header_size(b0);
+    }
+  else
+    {
+      if (is_input)
+        l3_offset = 0;
+      else
+        l3_offset = vnet_buffer(b0)->ip.save_rewrite_length;
+    }
+
+  /* key[0..3] contains src/dst address and is cleared/set below */
+  /* Remainder of the key and per-packet non-key data */
+  acl_fill_5tuple_l3_data(am, b0, is_ip6, l3_offset, p5tuple_pkt);
+  acl_fill_5tuple_l4_and_pkt_data(am, sw_if_index0, b0, is_ip6, is_input, l3_offset, &p5tuple_pkt->l4, &p5tuple_pkt->pkt);
+}
+
+always_inline void
+acl_plugin_fill_5tuple_inline (void *p_acl_main, u32 lc_index, vlib_buffer_t * b0, int is_ip6,
                 int is_input, int is_l2_path, fa_5tuple_opaque_t * p5tuple_pkt)
 {
   acl_main_t *am = p_acl_main;
-  acl_fill_5tuple(am, b0, is_ip6, is_input, is_l2_path, (fa_5tuple_t *)p5tuple_pkt);
+  acl_fill_5tuple(am, 0, b0, is_ip6, is_input, is_l2_path, (fa_5tuple_t *)p5tuple_pkt);
 }
 
 
 
 always_inline int
-fa_acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2,
-                  int prefixlen, int is_ip6)
+fa_acl_match_ip4_addr (ip4_address_t * addr1, ip4_address_t * addr2,
+                  int prefixlen)
 {
   if (prefixlen == 0)
     {
       /* match any always succeeds */
       return 1;
     }
-  if (is_ip6)
+      uint32_t a1 = clib_net_to_host_u32 (addr1->as_u32);
+      uint32_t a2 = clib_net_to_host_u32 (addr2->as_u32);
+      uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
+      return (a1 & mask0) == a2;
+}
+
+always_inline int
+fa_acl_match_ip6_addr (ip6_address_t * addr1, ip6_address_t * addr2,
+                  int prefixlen)
+{
+  if (prefixlen == 0)
     {
+      /* match any always succeeds */
+      return 1;
+    }
       if (memcmp (addr1, addr2, prefixlen / 8))
        {
          /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
@@ -393,14 +293,6 @@ fa_acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2,
          /* The prefix fits into integer number of bytes, so nothing left to do */
          return 1;
        }
-    }
-  else
-    {
-      uint32_t a1 = clib_net_to_host_u32 (addr1->ip4.as_u32);
-      uint32_t a2 = clib_net_to_host_u32 (addr2->ip4.as_u32);
-      uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
-      return (a1 & mask0) == a2;
-    }
 }
 
 always_inline int
@@ -431,41 +323,26 @@ single_acl_match_5tuple (acl_main_t * am, u32 acl_index, fa_5tuple_t * pkt_5tupl
   for (i = 0; i < a->count; i++)
     {
       r = a->rules + i;
-#ifdef FA_NODE_VERBOSE_DEBUG
-      clib_warning("ACL_FA_NODE_DBG acl %d rule %d tag %s", acl_index, i, a->tag);
-#endif
       if (is_ip6 != r->is_ipv6)
        {
          continue;
        }
-      if (!fa_acl_match_addr
-         (&pkt_5tuple->addr[1], &r->dst, r->dst_prefixlen, is_ip6))
+      if (is_ip6) {
+        if (!fa_acl_match_ip6_addr
+         (&pkt_5tuple->ip6_addr[1], &r->dst.ip6, r->dst_prefixlen))
        continue;
-
-#ifdef FA_NODE_VERBOSE_DEBUG
-      clib_warning
-       ("ACL_FA_NODE_DBG acl %d rule %d pkt dst addr %U match rule addr %U/%d",
-        acl_index, i, format_ip46_address, &pkt_5tuple->addr[1],
-        r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4, format_ip46_address,
-         &r->dst, r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4,
-        r->dst_prefixlen);
-#endif
-
-      if (!fa_acl_match_addr
-         (&pkt_5tuple->addr[0], &r->src, r->src_prefixlen, is_ip6))
+        if (!fa_acl_match_ip6_addr
+         (&pkt_5tuple->ip6_addr[0], &r->src.ip6, r->src_prefixlen))
+       continue;
+      } else {
+        if (!fa_acl_match_ip4_addr
+         (&pkt_5tuple->ip4_addr[1], &r->dst.ip4, r->dst_prefixlen))
+       continue;
+        if (!fa_acl_match_ip4_addr
+         (&pkt_5tuple->ip4_addr[0], &r->src.ip4, r->src_prefixlen))
        continue;
+      }
 
-#ifdef FA_NODE_VERBOSE_DEBUG
-      clib_warning
-       ("ACL_FA_NODE_DBG acl %d rule %d pkt src addr %U match rule addr %U/%d",
-        acl_index, i, format_ip46_address, &pkt_5tuple->addr[0],
-        r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4, format_ip46_address,
-         &r->src, r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4,
-        r->src_prefixlen);
-      clib_warning
-       ("ACL_FA_NODE_DBG acl %d rule %d trying to match pkt proto %d with rule %d",
-        acl_index, i, pkt_5tuple->l4.proto, r->proto);
-#endif
       if (r->proto)
        {
          if (pkt_5tuple->l4.proto != r->proto)
@@ -538,7 +415,7 @@ single_acl_match_5tuple (acl_main_t * am, u32 acl_index, fa_5tuple_t * pkt_5tupl
 }
 
 always_inline int
-acl_plugin_single_acl_match_5tuple (u32 acl_index, fa_5tuple_t * pkt_5tuple,
+acl_plugin_single_acl_match_5tuple (void *p_acl_main, u32 acl_index, fa_5tuple_t * pkt_5tuple,
                  int is_ip6, u8 * r_action, u32 * r_acl_match_p,
                  u32 * r_rule_match_p, u32 * trace_bitmap)
 {
@@ -548,7 +425,7 @@ acl_plugin_single_acl_match_5tuple (u32 acl_index, fa_5tuple_t * pkt_5tuple,
 }
 
 always_inline int
-linear_multi_acl_match_5tuple (u32 lc_index, fa_5tuple_t * pkt_5tuple, 
+linear_multi_acl_match_5tuple (void *p_acl_main, u32 lc_index, fa_5tuple_t * pkt_5tuple,
                       int is_ip6, u8 *r_action, u32 *acl_pos_p, u32 * acl_match_p,
                       u32 * rule_match_p, u32 * trace_bitmap)
 {
@@ -612,102 +489,167 @@ match_portranges(acl_main_t *am, fa_5tuple_t *match, u32 index)
            ((r->dst_port_or_code_first <= match->l4.port[1]) && r->dst_port_or_code_last >= match->l4.port[1]) );
 }
 
+always_inline int
+single_rule_match_5tuple (acl_rule_t * r, int is_ip6, fa_5tuple_t * pkt_5tuple)
+{
+  if (is_ip6 != r->is_ipv6)
+    {
+      return 0;
+    }
+
+  if (is_ip6)
+    {
+      if (!fa_acl_match_ip6_addr
+         (&pkt_5tuple->ip6_addr[1], &r->dst.ip6, r->dst_prefixlen))
+       return 0;
+      if (!fa_acl_match_ip6_addr
+         (&pkt_5tuple->ip6_addr[0], &r->src.ip6, r->src_prefixlen))
+       return 0;
+    }
+  else
+    {
+      if (!fa_acl_match_ip4_addr
+         (&pkt_5tuple->ip4_addr[1], &r->dst.ip4, r->dst_prefixlen))
+       return 0;
+      if (!fa_acl_match_ip4_addr
+         (&pkt_5tuple->ip4_addr[0], &r->src.ip4, r->src_prefixlen))
+       return 0;
+    }
+
+  if (r->proto)
+    {
+      if (pkt_5tuple->l4.proto != r->proto)
+       return 0;
+
+      /* A sanity check just to ensure we are about to match the ports extracted from the packet */
+      if (PREDICT_FALSE (!pkt_5tuple->pkt.l4_valid))
+       return 0;
+
+
+      if (!fa_acl_match_port
+         (pkt_5tuple->l4.port[0], r->src_port_or_type_first,
+          r->src_port_or_type_last, pkt_5tuple->pkt.is_ip6))
+       return 0;
+
+
+      if (!fa_acl_match_port
+         (pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
+          r->dst_port_or_code_last, pkt_5tuple->pkt.is_ip6))
+       return 0;
+
+      if (pkt_5tuple->pkt.tcp_flags_valid
+         && ((pkt_5tuple->pkt.tcp_flags & r->tcp_flags_mask) !=
+             r->tcp_flags_value))
+       return 0;
+    }
+  /* everything matches! */
+  return 1;
+}
+
 always_inline u32
-multi_acl_match_get_applied_ace_index(acl_main_t *am, fa_5tuple_t *match)
+multi_acl_match_get_applied_ace_index (acl_main_t * am, int is_ip6, fa_5tuple_t * match)
 {
   clib_bihash_kv_48_8_t kv;
   clib_bihash_kv_48_8_t result;
-  fa_5tuple_t *kv_key = (fa_5tuple_t *)kv.key;
-  hash_acl_lookup_value_t *result_val = (hash_acl_lookup_value_t *)&result.value;
-  u64 *pmatch = (u64 *)match;
+  fa_5tuple_t *kv_key = (fa_5tuple_t *) kv.key;
+  hash_acl_lookup_value_t *result_val =
+    (hash_acl_lookup_value_t *) & result.value;
+  u64 *pmatch = (u64 *) match;
   u64 *pmask;
   u64 *pkey;
-  int mask_type_index;
-  u32 curr_match_index = ~0;
+  int mask_type_index, order_index;
+  u32 curr_match_index = (~0 - 1);
+
+
 
   u32 lc_index = match->pkt.lc_index;
-  applied_hash_ace_entry_t **applied_hash_aces = vec_elt_at_index(am->hash_entry_vec_by_lc_index, match->pkt.lc_index);
-  applied_hash_acl_info_t **applied_hash_acls = &am->applied_hash_acl_info_by_lc_index;
+  applied_hash_ace_entry_t **applied_hash_aces =
+    vec_elt_at_index (am->hash_entry_vec_by_lc_index, lc_index);
 
-  DBG("TRYING TO MATCH: %016llx %016llx %016llx %016llx %016llx %016llx",
-              pmatch[0], pmatch[1], pmatch[2], pmatch[3], pmatch[4], pmatch[5]);
+  hash_applied_mask_info_t **hash_applied_mask_info_vec =
+    vec_elt_at_index (am->hash_applied_mask_info_vec_by_lc_index, lc_index);
 
-  for(mask_type_index=0; mask_type_index < pool_len(am->ace_mask_type_pool); mask_type_index++) {
-    if (!clib_bitmap_get(vec_elt_at_index((*applied_hash_acls), lc_index)->mask_type_index_bitmap, mask_type_index)) {
-      /* This bit is not set. Avoid trying to match */
-      continue;
-    }
-    ace_mask_type_entry_t *mte = vec_elt_at_index(am->ace_mask_type_pool, mask_type_index);
-    pmatch = (u64 *)match;
-    pmask = (u64 *)&mte->mask;
-    pkey = (u64 *)kv.key;
-    /*
-    * unrolling the below loop results in a noticeable performance increase.
-    int i;
-    for(i=0; i<6; i++) {
-      kv.key[i] = pmatch[i] & pmask[i];
-    }
-    */
-
-    *pkey++ = *pmatch++ & *pmask++;
-    *pkey++ = *pmatch++ & *pmask++;
-    *pkey++ = *pmatch++ & *pmask++;
-    *pkey++ = *pmatch++ & *pmask++;
-    *pkey++ = *pmatch++ & *pmask++;
-    *pkey++ = *pmatch++ & *pmask++;
-
-    kv_key->pkt.mask_type_index_lsb = mask_type_index;
-    DBG("        KEY %3d: %016llx %016llx %016llx %016llx %016llx %016llx", mask_type_index,
-               kv.key[0], kv.key[1], kv.key[2], kv.key[3], kv.key[4], kv.key[5]);
-    int res = clib_bihash_search_48_8 (&am->acl_lookup_hash, &kv, &result);
-    if (res == 0) {
-      DBG("ACL-MATCH! result_val: %016llx", result_val->as_u64);
-      if (result_val->applied_entry_index < curr_match_index) {
-       if (PREDICT_FALSE(result_val->need_portrange_check)) {
-          /*
-           * This is going to be slow, since we can have multiple superset
-           * entries for narrow-ish portranges, e.g.:
-           * 0..42 100..400, 230..60000,
-           * so we need to walk linearly and check if they match.
-           */
-
-          u32 curr_index = result_val->applied_entry_index;
-          while ((curr_index != ~0) && !match_portranges(am, match, curr_index)) {
-            /* while no match and there are more entries, walk... */
-            applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces),curr_index);
-            DBG("entry %d did not portmatch, advancing to %d", curr_index, pae->next_applied_entry_index);
-            curr_index = pae->next_applied_entry_index;
-          }
-          if (curr_index < curr_match_index) {
-            DBG("The index %d is the new candidate in portrange matches.", curr_index);
-            curr_match_index = curr_index;
-          } else {
-            DBG("Curr portmatch index %d is too big vs. current matched one %d", curr_index, curr_match_index);
-          }
-        } else {
-          /* The usual path is here. Found an entry in front of the current candiate - so it's a new one */
-          DBG("This match is the new candidate");
-          curr_match_index = result_val->applied_entry_index;
-         if (!result_val->shadowed) {
-          /* new result is known to not be shadowed, so no point to look up further */
-            break;
-         }
-        }
-      }
+  hash_applied_mask_info_t *minfo;
+
+  DBG ("TRYING TO MATCH: %016llx %016llx %016llx %016llx %016llx %016llx",
+       pmatch[0], pmatch[1], pmatch[2], pmatch[3], pmatch[4], pmatch[5]);
+
+  for (order_index = 0; order_index < vec_len ((*hash_applied_mask_info_vec));
+       order_index++)
+    {
+      minfo = vec_elt_at_index ((*hash_applied_mask_info_vec), order_index);
+      if (minfo->first_rule_index > curr_match_index)
+       {
+         /* Index in this and following (by construction) partitions are greater than our candidate, Avoid trying to match! */
+         break;
+       }
+
+      mask_type_index = minfo->mask_type_index;
+      ace_mask_type_entry_t *mte =
+       vec_elt_at_index (am->ace_mask_type_pool, mask_type_index);
+      pmatch = (u64 *) match;
+      pmask = (u64 *) & mte->mask;
+      pkey = (u64 *) kv.key;
+      /*
+       * unrolling the below loop results in a noticeable performance increase.
+       int i;
+       for(i=0; i<6; i++) {
+       kv.key[i] = pmatch[i] & pmask[i];
+       }
+       */
+
+      *pkey++ = *pmatch++ & *pmask++;
+      *pkey++ = *pmatch++ & *pmask++;
+      *pkey++ = *pmatch++ & *pmask++;
+      *pkey++ = *pmatch++ & *pmask++;
+      *pkey++ = *pmatch++ & *pmask++;
+      *pkey++ = *pmatch++ & *pmask++;
+
+      /*
+       * The use of temporary variable convinces the compiler
+       * to make a u64 write, avoiding the stall on crc32 operation
+       * just a bit later.
+       */
+      fa_packet_info_t tmp_pkt = kv_key->pkt;
+      tmp_pkt.mask_type_index_lsb = mask_type_index;
+      kv_key->pkt.as_u64 = tmp_pkt.as_u64;
+
+      int res =
+       clib_bihash_search_inline_2_48_8 (&am->acl_lookup_hash, &kv, &result);
+
+      if (res == 0)
+       {
+         /* There is a hit in the hash, so check the collision vector */
+         u32 curr_index = result_val->applied_entry_index;
+         applied_hash_ace_entry_t *pae =
+           vec_elt_at_index ((*applied_hash_aces), curr_index);
+         collision_match_rule_t *crs = pae->colliding_rules;
+         int i;
+         for (i = 0; i < vec_len (crs); i++)
+           {
+             if (crs[i].applied_entry_index >= curr_match_index)
+               {
+                 continue;
+               }
+             if (single_rule_match_5tuple (&crs[i].rule, is_ip6, match))
+               {
+                 curr_match_index = crs[i].applied_entry_index;
+               }
+           }
+       }
     }
-  }
-  DBG("MATCH-RESULT: %d", curr_match_index);
+  DBG ("MATCH-RESULT: %d", curr_match_index);
   return curr_match_index;
 }
 
 always_inline int
-hash_multi_acl_match_5tuple (u32 lc_index, fa_5tuple_t * pkt_5tuple,
+hash_multi_acl_match_5tuple (void *p_acl_main, u32 lc_index, fa_5tuple_t * pkt_5tuple,
                        int is_ip6, u8 *action, u32 *acl_pos_p, u32 * acl_match_p,
                        u32 * rule_match_p, u32 * trace_bitmap)
 {
   acl_main_t *am = p_acl_main;
   applied_hash_ace_entry_t **applied_hash_aces = vec_elt_at_index(am->hash_entry_vec_by_lc_index, lc_index);
-  u32 match_index = multi_acl_match_get_applied_ace_index(am, pkt_5tuple);
+  u32 match_index = multi_acl_match_get_applied_ace_index(am, is_ip6, pkt_5tuple);
   if (match_index < vec_len((*applied_hash_aces))) {
     applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), match_index);
     pae->hitcount++;
@@ -723,7 +665,7 @@ hash_multi_acl_match_5tuple (u32 lc_index, fa_5tuple_t * pkt_5tuple,
 
 
 always_inline int
-acl_plugin_match_5tuple_inline (u32 lc_index,
+acl_plugin_match_5tuple_inline (void *p_acl_main, u32 lc_index,
                                            fa_5tuple_opaque_t * pkt_5tuple,
                                            int is_ip6, u8 * r_action,
                                            u32 * r_acl_pos_p,
@@ -732,11 +674,24 @@ acl_plugin_match_5tuple_inline (u32 lc_index,
                                            u32 * trace_bitmap)
 {
   acl_main_t *am = p_acl_main;
-  if (am->use_hash_acl_matching) {
-    return hash_multi_acl_match_5tuple(lc_index, (fa_5tuple_t *)pkt_5tuple, is_ip6, r_action,
+  fa_5tuple_t * pkt_5tuple_internal = (fa_5tuple_t *)pkt_5tuple;
+  pkt_5tuple_internal->pkt.lc_index = lc_index;
+  if (PREDICT_TRUE(am->use_hash_acl_matching)) {
+    if (PREDICT_FALSE(pkt_5tuple_internal->pkt.is_nonfirst_fragment)) {
+      /*
+       * tuplemerge does not take fragments into account,
+       * and in general making fragments first class citizens has
+       * proved more overhead than it's worth - so just fall back to linear
+       * matching in that case.
+       */
+      return linear_multi_acl_match_5tuple(p_acl_main, lc_index, pkt_5tuple_internal, is_ip6, r_action,
+                                 r_acl_pos_p, r_acl_match_p, r_rule_match_p, trace_bitmap);
+    } else {
+      return hash_multi_acl_match_5tuple(p_acl_main, lc_index, pkt_5tuple_internal, is_ip6, r_action,
                                  r_acl_pos_p, r_acl_match_p, r_rule_match_p, trace_bitmap);
+    }
   } else {
-    return linear_multi_acl_match_5tuple(lc_index, (fa_5tuple_t *)pkt_5tuple, is_ip6, r_action,
+    return linear_multi_acl_match_5tuple(p_acl_main, lc_index, pkt_5tuple_internal, is_ip6, r_action,
                                  r_acl_pos_p, r_acl_match_p, r_rule_match_p, trace_bitmap);
   }
 }