f4942ec1a24b158ac3538293708750ac5584489c
[vpp.git] / src / plugins / acl / public_inlines.h
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef included_acl_inlines_h
17 #define included_acl_inlines_h
18
19 #include <stdint.h>
20
21 #include <plugins/acl/acl.h>
22 #include <plugins/acl/fa_node.h>
23 #include <plugins/acl/hash_lookup_private.h>
24
25
26 /* check if a given ACL exists */
27
28 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
29 u8 (*acl_plugin_acl_exists) (u32 acl_index);
30 #else
31 u8 acl_plugin_acl_exists (u32 acl_index);
32 #endif
33
34
35 /*
36  * If you are using ACL plugin, get this unique ID first,
37  * so you can identify yourself when creating the lookup contexts.
38  */
39
40 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
41 u32 (*acl_plugin_register_user_module) (char *caller_module_string, char *val1_label, char *val2_label);
42 #else
43 u32 acl_plugin_register_user_module (char *caller_module_string, char *val1_label, char *val2_label);
44 #endif
45
46 /*
47  * Allocate a new lookup context index.
48  * Supply the id assigned to your module during registration,
49  * and two values of your choice identifying instances
50  * of use within your module. They are useful for debugging.
51  */
52 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
53 int (*acl_plugin_get_lookup_context_index) (u32 acl_user_id, u32 val1, u32 val2);
54 #else
55 int acl_plugin_get_lookup_context_index (u32 acl_user_id, u32 val1, u32 val2);
56 #endif
57
58 /*
59  * Release the lookup context index and destroy
60  * any asssociated data structures.
61  */
62 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
63 void (*acl_plugin_put_lookup_context_index) (u32 lc_index);
64 #else
65 void acl_plugin_put_lookup_context_index (u32 lc_index);
66 #endif
67
68 /*
69  * Prepare the sequential vector of ACL#s to lookup within a given context.
70  * Any existing list will be overwritten. acl_list is a vector.
71  */
72 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
73 int (*acl_plugin_set_acl_vec_for_context) (u32 lc_index, u32 *acl_list);
74 #else
75 int acl_plugin_set_acl_vec_for_context (u32 lc_index, u32 *acl_list);
76 #endif
77
78 /* Fill the 5-tuple from the packet */
79
80 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
81 void (*acl_plugin_fill_5tuple) (u32 lc_index, vlib_buffer_t * b0, int is_ip6, int is_input,
82                                 int is_l2_path, fa_5tuple_opaque_t * p5tuple_pkt);
83 #else
84 void acl_plugin_fill_5tuple (u32 lc_index, vlib_buffer_t * b0, int is_ip6, int is_input,
85                                 int is_l2_path, fa_5tuple_opaque_t * p5tuple_pkt);
86 #endif
87
88 #ifdef ACL_PLUGIN_DEFINED_BELOW_IN_FILE
89 static inline
90 void acl_plugin_fill_5tuple_inline (u32 lc_index, vlib_buffer_t * b0, int is_ip6, int is_input,
91                                 int is_l2_path, fa_5tuple_opaque_t * p5tuple_pkt) {
92   /* FIXME: normally the inlined version of filling in the 5-tuple. But for now just call the non-inlined version */
93   acl_plugin_fill_5tuple(lc_index, b0, is_ip6, is_input, is_l2_path, p5tuple_pkt);
94 }
95 #endif
96
97
98 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
99 int (*acl_plugin_match_5tuple) (u32 lc_index,
100                                            fa_5tuple_opaque_t * pkt_5tuple,
101                                            int is_ip6, u8 * r_action,
102                                            u32 * r_acl_pos_p,
103                                            u32 * r_acl_match_p,
104                                            u32 * r_rule_match_p,
105                                            u32 * trace_bitmap);
106 #else
107 int acl_plugin_match_5tuple (u32 lc_index,
108                                            fa_5tuple_opaque_t * pkt_5tuple,
109                                            int is_ip6, u8 * r_action,
110                                            u32 * r_acl_pos_p,
111                                            u32 * r_acl_match_p,
112                                            u32 * r_rule_match_p,
113                                            u32 * trace_bitmap);
114 #endif
115
116 #ifdef ACL_PLUGIN_DEFINED_BELOW_IN_FILE
117 static inline int
118 acl_plugin_match_5tuple_inline (u32 lc_index,
119                                            fa_5tuple_opaque_t * pkt_5tuple,
120                                            int is_ip6, u8 * r_action,
121                                            u32 * r_acl_pos_p,
122                                            u32 * r_acl_match_p,
123                                            u32 * r_rule_match_p,
124                                            u32 * trace_bitmap) {
125   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);
126 }
127 #endif
128
129 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
130
131 #define LOAD_SYMBOL_FROM_PLUGIN(p, s)                                     \
132 ({                                                                        \
133     s = vlib_get_plugin_symbol(p, #s);                                    \
134     if (!s)                                                               \
135         return clib_error_return(0,                                       \
136                 "Plugin %s and/or symbol %s not found.", p, #s);          \
137 })
138
139 #define LOAD_SYMBOL(s) LOAD_SYMBOL_FROM_PLUGIN("acl_plugin.so", s)
140
141 static inline clib_error_t * acl_plugin_exports_init (void)
142 {
143     LOAD_SYMBOL(acl_plugin_acl_exists);
144     LOAD_SYMBOL(acl_plugin_register_user_module);
145     LOAD_SYMBOL(acl_plugin_get_lookup_context_index);
146     LOAD_SYMBOL(acl_plugin_put_lookup_context_index);
147     LOAD_SYMBOL(acl_plugin_set_acl_vec_for_context);
148     LOAD_SYMBOL(acl_plugin_fill_5tuple);
149     LOAD_SYMBOL(acl_plugin_match_5tuple);
150     return 0;
151 }
152
153 #endif
154
155
156
157 always_inline void *
158 get_ptr_to_offset (vlib_buffer_t * b0, int offset)
159 {
160   u8 *p = vlib_buffer_get_current (b0) + offset;
161   return p;
162 }
163
164 always_inline int
165 offset_within_packet (vlib_buffer_t * b0, int offset)
166 {
167   /* For the purposes of this code, "within" means we have at least 8 bytes after it */
168   return (offset <= (b0->current_length - 8));
169 }
170
171 always_inline void
172 acl_fill_5tuple (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
173                  int is_input, int is_l2_path, fa_5tuple_t * p5tuple_pkt)
174 {
175   /* IP4 and IP6 protocol numbers of ICMP */
176   static u8 icmp_protos_v4v6[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
177
178   int l3_offset;
179   int l4_offset;
180   u16 ports[2];
181   u16 proto;
182
183   if (is_l2_path)
184     {
185       l3_offset = ethernet_buffer_header_size(b0);
186     }
187   else
188     {
189       if (is_input)
190         l3_offset = 0;
191       else
192         l3_offset = vnet_buffer(b0)->ip.save_rewrite_length;
193     }
194
195   /* key[0..3] contains src/dst address and is cleared/set below */
196   /* Remainder of the key and per-packet non-key data */
197   p5tuple_pkt->kv.key[4] = 0;
198   p5tuple_pkt->kv.value = 0;
199
200   if (is_ip6)
201     {
202       clib_memcpy (&p5tuple_pkt->addr,
203                    get_ptr_to_offset (b0,
204                                       offsetof (ip6_header_t,
205                                                 src_address) + l3_offset),
206                    sizeof (p5tuple_pkt->addr));
207       proto =
208         *(u8 *) get_ptr_to_offset (b0,
209                                    offsetof (ip6_header_t,
210                                              protocol) + l3_offset);
211       l4_offset = l3_offset + sizeof (ip6_header_t);
212 #ifdef FA_NODE_VERBOSE_DEBUG
213       clib_warning ("ACL_FA_NODE_DBG: proto: %d, l4_offset: %d", proto,
214                     l4_offset);
215 #endif
216       /* IP6 EH handling is here, increment l4_offset if needs to, update the proto */
217       int need_skip_eh = clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
218       if (PREDICT_FALSE (need_skip_eh))
219         {
220           while (need_skip_eh && offset_within_packet (b0, l4_offset))
221             {
222               /* Fragment header needs special handling */
223               if (PREDICT_FALSE(ACL_EH_FRAGMENT == proto))
224                 {
225                   proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
226                   u16 frag_offset;
227                   clib_memcpy (&frag_offset, get_ptr_to_offset (b0, 2 + l4_offset), sizeof(frag_offset));
228                   frag_offset = clib_net_to_host_u16(frag_offset) >> 3;
229                   if (frag_offset)
230                     {
231                       p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
232                       /* invalidate L4 offset so we don't try to find L4 info */
233                       l4_offset += b0->current_length;
234                     }
235                   else
236                     {
237                       /* First fragment: skip the frag header and move on. */
238                       l4_offset += 8;
239                     }
240                 }
241               else
242                 {
243                   u8 nwords = *(u8 *) get_ptr_to_offset (b0, 1 + l4_offset);
244                   proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
245                   l4_offset += 8 * (1 + (u16) nwords);
246                 }
247 #ifdef FA_NODE_VERBOSE_DEBUG
248               clib_warning ("ACL_FA_NODE_DBG: new proto: %d, new offset: %d",
249                             proto, l4_offset);
250 #endif
251               need_skip_eh =
252                 clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
253             }
254         }
255     }
256   else
257     {
258       p5tuple_pkt->kv.key[0] = 0;
259       p5tuple_pkt->kv.key[1] = 0;
260       p5tuple_pkt->kv.key[2] = 0;
261       p5tuple_pkt->kv.key[3] = 0;
262       clib_memcpy (&p5tuple_pkt->addr[0].ip4,
263                    get_ptr_to_offset (b0,
264                                       offsetof (ip4_header_t,
265                                                 src_address) + l3_offset),
266                    sizeof (p5tuple_pkt->addr[0].ip4));
267       clib_memcpy (&p5tuple_pkt->addr[1].ip4,
268                    get_ptr_to_offset (b0,
269                                       offsetof (ip4_header_t,
270                                                 dst_address) + l3_offset),
271                    sizeof (p5tuple_pkt->addr[1].ip4));
272       proto =
273         *(u8 *) get_ptr_to_offset (b0,
274                                    offsetof (ip4_header_t,
275                                              protocol) + l3_offset);
276       l4_offset = l3_offset + sizeof (ip4_header_t);
277       u16 flags_and_fragment_offset;
278       clib_memcpy (&flags_and_fragment_offset,
279                    get_ptr_to_offset (b0,
280                                       offsetof (ip4_header_t,
281                                                 flags_and_fragment_offset)) + l3_offset,
282                                                 sizeof(flags_and_fragment_offset));
283       flags_and_fragment_offset = clib_net_to_host_u16 (flags_and_fragment_offset);
284
285       /* non-initial fragments have non-zero offset */
286       if ((PREDICT_FALSE(0xfff & flags_and_fragment_offset)))
287         {
288           p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
289           /* invalidate L4 offset so we don't try to find L4 info */
290           l4_offset += b0->current_length;
291         }
292
293     }
294   p5tuple_pkt->l4.proto = proto;
295   if (PREDICT_TRUE (offset_within_packet (b0, l4_offset)))
296     {
297       p5tuple_pkt->pkt.l4_valid = 1;
298       if (icmp_protos_v4v6[is_ip6] == proto)
299         {
300           /* type */
301           p5tuple_pkt->l4.port[0] =
302             *(u8 *) get_ptr_to_offset (b0,
303                                        l4_offset + offsetof (icmp46_header_t,
304                                                              type));
305           /* code */
306           p5tuple_pkt->l4.port[1] =
307             *(u8 *) get_ptr_to_offset (b0,
308                                        l4_offset + offsetof (icmp46_header_t,
309                                                              code));
310         }
311       else if ((IP_PROTOCOL_TCP == proto) || (IP_PROTOCOL_UDP == proto))
312         {
313           clib_memcpy (&ports,
314                        get_ptr_to_offset (b0,
315                                           l4_offset + offsetof (tcp_header_t,
316                                                                 src_port)),
317                        sizeof (ports));
318           p5tuple_pkt->l4.port[0] = clib_net_to_host_u16 (ports[0]);
319           p5tuple_pkt->l4.port[1] = clib_net_to_host_u16 (ports[1]);
320
321           p5tuple_pkt->pkt.tcp_flags =
322             *(u8 *) get_ptr_to_offset (b0,
323                                        l4_offset + offsetof (tcp_header_t,
324                                                              flags));
325           p5tuple_pkt->pkt.tcp_flags_valid = (proto == IP_PROTOCOL_TCP);
326         }
327       /*
328        * FIXME: rather than the above conditional, here could
329        * be a nice generic mechanism to extract two L4 values:
330        *
331        * have a per-protocol array of 4 elements like this:
332        *   u8 offset; to take the byte from, off L4 header
333        *   u8 mask; to mask it with, before storing
334        *
335        * this way we can describe UDP, TCP and ICMP[46] semantics,
336        * and add a sort of FPM-type behavior for other protocols.
337        *
338        * Of course, is it faster ? and is it needed ?
339        *
340        */
341     }
342 }
343
344 always_inline void
345 acl_plugin_fill_5tuple_inline (u32 lc_index, vlib_buffer_t * b0, int is_ip6,
346                  int is_input, int is_l2_path, fa_5tuple_opaque_t * p5tuple_pkt)
347 {
348   acl_main_t *am = &acl_main;
349   acl_fill_5tuple(am, b0, is_ip6, is_input, is_l2_path, (fa_5tuple_t *)p5tuple_pkt);
350 }
351
352
353
354 always_inline int
355 fa_acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2,
356                    int prefixlen, int is_ip6)
357 {
358   if (prefixlen == 0)
359     {
360       /* match any always succeeds */
361       return 1;
362     }
363   if (is_ip6)
364     {
365       if (memcmp (addr1, addr2, prefixlen / 8))
366         {
367           /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
368           return 0;
369         }
370       if (prefixlen % 8)
371         {
372           u8 b1 = *((u8 *) addr1 + 1 + prefixlen / 8);
373           u8 b2 = *((u8 *) addr2 + 1 + prefixlen / 8);
374           u8 mask0 = (0xff - ((1 << (8 - (prefixlen % 8))) - 1));
375           return (b1 & mask0) == b2;
376         }
377       else
378         {
379           /* The prefix fits into integer number of bytes, so nothing left to do */
380           return 1;
381         }
382     }
383   else
384     {
385       uint32_t a1 = clib_net_to_host_u32 (addr1->ip4.as_u32);
386       uint32_t a2 = clib_net_to_host_u32 (addr2->ip4.as_u32);
387       uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
388       return (a1 & mask0) == a2;
389     }
390 }
391
392 always_inline int
393 fa_acl_match_port (u16 port, u16 port_first, u16 port_last, int is_ip6)
394 {
395   return ((port >= port_first) && (port <= port_last));
396 }
397
398 always_inline int
399 single_acl_match_5tuple (acl_main_t * am, u32 acl_index, fa_5tuple_t * pkt_5tuple,
400                   int is_ip6, u8 * r_action, u32 * r_acl_match_p,
401                   u32 * r_rule_match_p, u32 * trace_bitmap)
402 {
403   int i;
404   acl_list_t *a;
405   acl_rule_t *r;
406
407   if (pool_is_free_index (am->acls, acl_index))
408     {
409       if (r_acl_match_p)
410         *r_acl_match_p = acl_index;
411       if (r_rule_match_p)
412         *r_rule_match_p = -1;
413       /* the ACL does not exist but is used for policy. Block traffic. */
414       return 0;
415     }
416   a = am->acls + acl_index;
417   for (i = 0; i < a->count; i++)
418     {
419       r = a->rules + i;
420 #ifdef FA_NODE_VERBOSE_DEBUG
421       clib_warning("ACL_FA_NODE_DBG acl %d rule %d tag %s", acl_index, i, a->tag);
422 #endif
423       if (is_ip6 != r->is_ipv6)
424         {
425           continue;
426         }
427       if (!fa_acl_match_addr
428           (&pkt_5tuple->addr[1], &r->dst, r->dst_prefixlen, is_ip6))
429         continue;
430
431 #ifdef FA_NODE_VERBOSE_DEBUG
432       clib_warning
433         ("ACL_FA_NODE_DBG acl %d rule %d pkt dst addr %U match rule addr %U/%d",
434          acl_index, i, format_ip46_address, &pkt_5tuple->addr[1],
435          r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4, format_ip46_address,
436          &r->dst, r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4,
437          r->dst_prefixlen);
438 #endif
439
440       if (!fa_acl_match_addr
441           (&pkt_5tuple->addr[0], &r->src, r->src_prefixlen, is_ip6))
442         continue;
443
444 #ifdef FA_NODE_VERBOSE_DEBUG
445       clib_warning
446         ("ACL_FA_NODE_DBG acl %d rule %d pkt src addr %U match rule addr %U/%d",
447          acl_index, i, format_ip46_address, &pkt_5tuple->addr[0],
448          r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4, format_ip46_address,
449          &r->src, r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4,
450          r->src_prefixlen);
451       clib_warning
452         ("ACL_FA_NODE_DBG acl %d rule %d trying to match pkt proto %d with rule %d",
453          acl_index, i, pkt_5tuple->l4.proto, r->proto);
454 #endif
455       if (r->proto)
456         {
457           if (pkt_5tuple->l4.proto != r->proto)
458             continue;
459
460           if (PREDICT_FALSE (pkt_5tuple->pkt.is_nonfirst_fragment &&
461                      am->l4_match_nonfirst_fragment))
462           {
463             /* non-initial fragment with frag match configured - match this rule */
464             *trace_bitmap |= 0x80000000;
465             *r_action = r->is_permit;
466             if (r_acl_match_p)
467               *r_acl_match_p = acl_index;
468             if (r_rule_match_p)
469               *r_rule_match_p = i;
470             return 1;
471           }
472
473           /* A sanity check just to ensure we are about to match the ports extracted from the packet */
474           if (PREDICT_FALSE (!pkt_5tuple->pkt.l4_valid))
475             continue;
476
477 #ifdef FA_NODE_VERBOSE_DEBUG
478           clib_warning
479             ("ACL_FA_NODE_DBG acl %d rule %d pkt proto %d match rule %d",
480              acl_index, i, pkt_5tuple->l4.proto, r->proto);
481 #endif
482
483           if (!fa_acl_match_port
484               (pkt_5tuple->l4.port[0], r->src_port_or_type_first,
485                r->src_port_or_type_last, is_ip6))
486             continue;
487
488 #ifdef FA_NODE_VERBOSE_DEBUG
489           clib_warning
490             ("ACL_FA_NODE_DBG acl %d rule %d pkt sport %d match rule [%d..%d]",
491              acl_index, i, pkt_5tuple->l4.port[0], r->src_port_or_type_first,
492              r->src_port_or_type_last);
493 #endif
494
495           if (!fa_acl_match_port
496               (pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
497                r->dst_port_or_code_last, is_ip6))
498             continue;
499
500 #ifdef FA_NODE_VERBOSE_DEBUG
501           clib_warning
502             ("ACL_FA_NODE_DBG acl %d rule %d pkt dport %d match rule [%d..%d]",
503              acl_index, i, pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
504              r->dst_port_or_code_last);
505 #endif
506           if (pkt_5tuple->pkt.tcp_flags_valid
507               && ((pkt_5tuple->pkt.tcp_flags & r->tcp_flags_mask) !=
508                   r->tcp_flags_value))
509             continue;
510         }
511       /* everything matches! */
512 #ifdef FA_NODE_VERBOSE_DEBUG
513       clib_warning ("ACL_FA_NODE_DBG acl %d rule %d FULL-MATCH, action %d",
514                     acl_index, i, r->is_permit);
515 #endif
516       *r_action = r->is_permit;
517       if (r_acl_match_p)
518         *r_acl_match_p = acl_index;
519       if (r_rule_match_p)
520         *r_rule_match_p = i;
521       return 1;
522     }
523   return 0;
524 }
525
526 always_inline int
527 acl_plugin_single_acl_match_5tuple (u32 acl_index, fa_5tuple_t * pkt_5tuple,
528                   int is_ip6, u8 * r_action, u32 * r_acl_match_p,
529                   u32 * r_rule_match_p, u32 * trace_bitmap)
530 {
531   acl_main_t * am = &acl_main;
532   return single_acl_match_5tuple(am, acl_index, pkt_5tuple, is_ip6, r_action,
533                                  r_acl_match_p, r_rule_match_p, trace_bitmap);
534 }
535
536 always_inline int
537 linear_multi_acl_match_5tuple (u32 lc_index, fa_5tuple_t * pkt_5tuple, 
538                        int is_ip6, u8 *r_action, u32 *acl_pos_p, u32 * acl_match_p,
539                        u32 * rule_match_p, u32 * trace_bitmap)
540 {
541   acl_main_t *am = &acl_main;
542   int i;
543   u32 *acl_vector;
544   u8 action = 0;
545   acl_lookup_context_t *acontext = pool_elt_at_index(am->acl_lookup_contexts, lc_index);
546
547   acl_vector = acontext->acl_indices;
548
549   for (i = 0; i < vec_len (acl_vector); i++)
550     {
551 #ifdef FA_NODE_VERBOSE_DEBUG
552       clib_warning ("ACL_FA_NODE_DBG: Trying to match ACL: %d",
553                     acl_vector[i]);
554 #endif
555       if (single_acl_match_5tuple
556           (am, acl_vector[i], pkt_5tuple, is_ip6, &action,
557            acl_match_p, rule_match_p, trace_bitmap))
558         {
559           *r_action = action;
560           *acl_pos_p = i;
561           return 1;
562         }
563     }
564   if (vec_len (acl_vector) > 0)
565     {
566       return 0;
567     }
568 #ifdef FA_NODE_VERBOSE_DEBUG
569   clib_warning ("ACL_FA_NODE_DBG: No ACL on lc_index %d", lc_index);
570 #endif
571   /* If there are no ACLs defined we should not be here. */
572   return 0;
573 }
574
575
576
577 /*
578  * This returns true if there is indeed a match on the portranges.
579  * With all these levels of indirections, this is not going to be very fast,
580  * so, best use the individual ports or wildcard ports for performance.
581  */
582 always_inline int
583 match_portranges(acl_main_t *am, fa_5tuple_t *match, u32 index)
584 {
585
586   applied_hash_ace_entry_t **applied_hash_aces = vec_elt_at_index(am->hash_entry_vec_by_lc_index, match->pkt.lc_index);
587   applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), index);
588
589   acl_rule_t *r = &(am->acls[pae->acl_index].rules[pae->ace_index]);
590
591 #ifdef FA_NODE_VERBOSE_DEBUG
592   clib_warning("PORTMATCH: %d <= %d <= %d && %d <= %d <= %d ?",
593                 r->src_port_or_type_first, match->l4.port[0], r->src_port_or_type_last,
594                 r->dst_port_or_code_first, match->l4.port[1], r->dst_port_or_code_last);
595 #endif
596
597   return ( ((r->src_port_or_type_first <= match->l4.port[0]) && r->src_port_or_type_last >= match->l4.port[0]) &&
598            ((r->dst_port_or_code_first <= match->l4.port[1]) && r->dst_port_or_code_last >= match->l4.port[1]) );
599 }
600
601 always_inline u32
602 multi_acl_match_get_applied_ace_index(acl_main_t *am, fa_5tuple_t *match)
603 {
604   clib_bihash_kv_48_8_t kv;
605   clib_bihash_kv_48_8_t result;
606   fa_5tuple_t *kv_key = (fa_5tuple_t *)kv.key;
607   hash_acl_lookup_value_t *result_val = (hash_acl_lookup_value_t *)&result.value;
608   u64 *pmatch = (u64 *)match;
609   u64 *pmask;
610   u64 *pkey;
611   int mask_type_index;
612   u32 curr_match_index = ~0;
613
614   u32 lc_index = match->pkt.lc_index;
615   applied_hash_ace_entry_t **applied_hash_aces = vec_elt_at_index(am->hash_entry_vec_by_lc_index, match->pkt.lc_index);
616   applied_hash_acl_info_t **applied_hash_acls = &am->applied_hash_acl_info_by_lc_index;
617
618   DBG("TRYING TO MATCH: %016llx %016llx %016llx %016llx %016llx %016llx",
619                pmatch[0], pmatch[1], pmatch[2], pmatch[3], pmatch[4], pmatch[5]);
620
621   for(mask_type_index=0; mask_type_index < pool_len(am->ace_mask_type_pool); mask_type_index++) {
622     if (!clib_bitmap_get(vec_elt_at_index((*applied_hash_acls), lc_index)->mask_type_index_bitmap, mask_type_index)) {
623       /* This bit is not set. Avoid trying to match */
624       continue;
625     }
626     ace_mask_type_entry_t *mte = vec_elt_at_index(am->ace_mask_type_pool, mask_type_index);
627     pmatch = (u64 *)match;
628     pmask = (u64 *)&mte->mask;
629     pkey = (u64 *)kv.key;
630     /*
631     * unrolling the below loop results in a noticeable performance increase.
632     int i;
633     for(i=0; i<6; i++) {
634       kv.key[i] = pmatch[i] & pmask[i];
635     }
636     */
637
638     *pkey++ = *pmatch++ & *pmask++;
639     *pkey++ = *pmatch++ & *pmask++;
640     *pkey++ = *pmatch++ & *pmask++;
641     *pkey++ = *pmatch++ & *pmask++;
642     *pkey++ = *pmatch++ & *pmask++;
643     *pkey++ = *pmatch++ & *pmask++;
644
645     kv_key->pkt.mask_type_index_lsb = mask_type_index;
646     DBG("        KEY %3d: %016llx %016llx %016llx %016llx %016llx %016llx", mask_type_index,
647                 kv.key[0], kv.key[1], kv.key[2], kv.key[3], kv.key[4], kv.key[5]);
648     int res = clib_bihash_search_48_8 (&am->acl_lookup_hash, &kv, &result);
649     if (res == 0) {
650       DBG("ACL-MATCH! result_val: %016llx", result_val->as_u64);
651       if (result_val->applied_entry_index < curr_match_index) {
652         if (PREDICT_FALSE(result_val->need_portrange_check)) {
653           /*
654            * This is going to be slow, since we can have multiple superset
655            * entries for narrow-ish portranges, e.g.:
656            * 0..42 100..400, 230..60000,
657            * so we need to walk linearly and check if they match.
658            */
659
660           u32 curr_index = result_val->applied_entry_index;
661           while ((curr_index != ~0) && !match_portranges(am, match, curr_index)) {
662             /* while no match and there are more entries, walk... */
663             applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces),curr_index);
664             DBG("entry %d did not portmatch, advancing to %d", curr_index, pae->next_applied_entry_index);
665             curr_index = pae->next_applied_entry_index;
666           }
667           if (curr_index < curr_match_index) {
668             DBG("The index %d is the new candidate in portrange matches.", curr_index);
669             curr_match_index = curr_index;
670           } else {
671             DBG("Curr portmatch index %d is too big vs. current matched one %d", curr_index, curr_match_index);
672           }
673         } else {
674           /* The usual path is here. Found an entry in front of the current candiate - so it's a new one */
675           DBG("This match is the new candidate");
676           curr_match_index = result_val->applied_entry_index;
677           if (!result_val->shadowed) {
678           /* new result is known to not be shadowed, so no point to look up further */
679             break;
680           }
681         }
682       }
683     }
684   }
685   DBG("MATCH-RESULT: %d", curr_match_index);
686   return curr_match_index;
687 }
688
689 always_inline int
690 hash_multi_acl_match_5tuple (u32 lc_index, fa_5tuple_t * pkt_5tuple,
691                        int is_ip6, u8 *action, u32 *acl_pos_p, u32 * acl_match_p,
692                        u32 * rule_match_p, u32 * trace_bitmap)
693 {
694   acl_main_t *am = &acl_main;
695   applied_hash_ace_entry_t **applied_hash_aces = vec_elt_at_index(am->hash_entry_vec_by_lc_index, lc_index);
696   u32 match_index = multi_acl_match_get_applied_ace_index(am, pkt_5tuple);
697   if (match_index < vec_len((*applied_hash_aces))) {
698     applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), match_index);
699     pae->hitcount++;
700     *acl_pos_p = pae->acl_position;
701     *acl_match_p = pae->acl_index;
702     *rule_match_p = pae->ace_index;
703     *action = pae->action;
704     return 1;
705   }
706   return 0;
707 }
708
709
710
711 always_inline int
712 acl_plugin_match_5tuple_inline (u32 lc_index,
713                                            fa_5tuple_opaque_t * pkt_5tuple,
714                                            int is_ip6, u8 * r_action,
715                                            u32 * r_acl_pos_p,
716                                            u32 * r_acl_match_p,
717                                            u32 * r_rule_match_p,
718                                            u32 * trace_bitmap)
719 {
720   acl_main_t *am = &acl_main;
721   if (am->use_hash_acl_matching) {
722     return hash_multi_acl_match_5tuple(lc_index, (fa_5tuple_t *)pkt_5tuple, is_ip6, r_action,
723                                  r_acl_pos_p, r_acl_match_p, r_rule_match_p, trace_bitmap);
724   } else {
725     return linear_multi_acl_match_5tuple(lc_index, (fa_5tuple_t *)pkt_5tuple, is_ip6, r_action,
726                                  r_acl_pos_p, r_acl_match_p, r_rule_match_p, trace_bitmap);
727   }
728 }
729
730
731
732 #endif