acl-plugin: bihash-based ACL lookup
[vpp.git] / src / plugins / acl / hash_lookup.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <stddef.h>
19 #include <netinet/in.h>
20
21 #include <vlibapi/api.h>
22 #include <vlibmemory/api.h>
23 #include <vlibsocket/api.h>
24
25 #include <vlib/vlib.h>
26 #include <vnet/vnet.h>
27 #include <vnet/pg/pg.h>
28 #include <vppinfra/error.h>
29 #include <vnet/plugin/plugin.h>
30 #include <acl/acl.h>
31 #include <vppinfra/bihash_48_8.h>
32
33 #include "hash_lookup.h"
34 #include "hash_lookup_private.h"
35
36 /*
37  * This returns true if there is indeed a match on the portranges.
38  * With all these levels of indirections, this is not going to be very fast,
39  * so, best use the individual ports or wildcard ports for performance.
40  */
41 static int
42 match_portranges(acl_main_t *am, fa_5tuple_t *match, u32 index)
43 {
44   applied_hash_ace_entry_t **applied_hash_aces = match->pkt.is_input ? &am->input_hash_entry_vec_by_sw_if_index[match->pkt.sw_if_index] :
45                                                     &am->output_hash_entry_vec_by_sw_if_index[match->pkt.sw_if_index];
46   applied_hash_ace_entry_t *pae = &((*applied_hash_aces)[index]);
47
48   // hash_acl_info_t *ha = &am->hash_acl_infos[pae->acl_index];
49   acl_rule_t *r = &(am->acls[pae->acl_index].rules[pae->ace_index]);
50   DBG("PORTMATCH: %d <= %d <= %d && %d <= %d <= %d ?",
51                 r->src_port_or_type_first, match->l4.port[0], r->src_port_or_type_last,
52                 r->dst_port_or_code_first, match->l4.port[1], r->dst_port_or_code_last);
53
54   return ( ((r->src_port_or_type_first <= match->l4.port[0]) && r->src_port_or_type_last >= match->l4.port[0]) &&
55            ((r->dst_port_or_code_first <= match->l4.port[1]) && r->dst_port_or_code_last >= match->l4.port[1]) );
56 }
57
58 static u32
59 multi_acl_match_get_applied_ace_index(acl_main_t *am, fa_5tuple_t *match)
60 {
61   clib_bihash_kv_48_8_t kv;
62   clib_bihash_kv_48_8_t result;
63   fa_5tuple_t *kv_key = (fa_5tuple_t *)kv.key;
64   hash_acl_lookup_value_t *result_val = (hash_acl_lookup_value_t *)&result.value;
65   u64 *pmatch = (u64 *)match;
66   u64 *pmask;
67   u64 *pkey;
68   int mask_type_index;
69   u32 curr_match_index = ~0;
70
71   u32 sw_if_index = match->pkt.sw_if_index;
72   u8 is_input = match->pkt.is_input;
73   applied_hash_ace_entry_t **applied_hash_aces = is_input ? &am->input_hash_entry_vec_by_sw_if_index[sw_if_index] :
74                                                     &am->output_hash_entry_vec_by_sw_if_index[sw_if_index];
75   applied_hash_acl_info_t **applied_hash_acls = is_input ? &am->input_applied_hash_acl_info_by_sw_if_index :
76                                                     &am->output_applied_hash_acl_info_by_sw_if_index;
77
78   DBG("TRYING TO MATCH: %016llx %016llx %016llx %016llx %016llx %016llx",
79                pmatch[0], pmatch[1], pmatch[2], pmatch[3], pmatch[4], pmatch[5]);
80
81   for(mask_type_index=0; mask_type_index < pool_len(am->ace_mask_type_pool); mask_type_index++) {
82     if (!clib_bitmap_get((*applied_hash_acls)[sw_if_index].mask_type_index_bitmap, mask_type_index)) {
83       /* This bit is not set. Avoid trying to match */
84       continue;
85     }
86     ace_mask_type_entry_t *mte = &am->ace_mask_type_pool[mask_type_index];
87     pmatch = (u64 *)match;
88     pmask = (u64 *)&mte->mask;
89     pkey = (u64 *)kv.key;
90     /*
91     * unrolling the below loop results in a noticeable performance increase.
92     int i;
93     for(i=0; i<6; i++) {
94       kv.key[i] = pmatch[i] & pmask[i];
95     }
96     */
97
98     *pkey++ = *pmatch++ & *pmask++;
99     *pkey++ = *pmatch++ & *pmask++;
100     *pkey++ = *pmatch++ & *pmask++;
101     *pkey++ = *pmatch++ & *pmask++;
102     *pkey++ = *pmatch++ & *pmask++;
103     *pkey++ = *pmatch++ & *pmask++;
104
105     kv_key->pkt.mask_type_index_lsb = mask_type_index;
106     DBG("        KEY %3d: %016llx %016llx %016llx %016llx %016llx %016llx", mask_type_index,
107                 kv.key[0], kv.key[1], kv.key[2], kv.key[3], kv.key[4], kv.key[5]);
108     int res = BV (clib_bihash_search) (&am->acl_lookup_hash, &kv, &result);
109     if (res == 0) {
110       DBG("ACL-MATCH! result_val: %016llx", result_val->as_u64);
111       if (result_val->applied_entry_index < curr_match_index) {
112         if (PREDICT_FALSE(result_val->need_portrange_check)) {
113           /*
114            * This is going to be slow, since we can have multiple superset
115            * entries for narrow-ish portranges, e.g.:
116            * 0..42 100..400, 230..60000,
117            * so we need to walk linearly and check if they match.
118            */
119
120           u32 curr_index = result_val->applied_entry_index;
121           while ((curr_index != ~0) && !match_portranges(am, match, curr_index)) {
122             /* while no match and there are more entries, walk... */
123             applied_hash_ace_entry_t *pae = &((*applied_hash_aces)[curr_index]);
124             DBG("entry %d did not portmatch, advancing to %d", curr_index, pae->next_applied_entry_index);
125             curr_index = pae->next_applied_entry_index;
126           }
127           if (curr_index < curr_match_index) {
128             DBG("The index %d is the new candidate in portrange matches.", curr_index);
129             curr_match_index = result_val->applied_entry_index;
130             if (!result_val->shadowed) {
131               /* new result is known to not be shadowed, so no point to look up further */
132               break;
133             }
134           } else {
135             DBG("Curr portmatch index %d is too big vs. current matched one %d", curr_index, curr_match_index);
136           }
137         } else {
138           /* The usual path is here. Found an entry in front of the current candiate - so it's a new one */
139           DBG("This match is the new candidate");
140           curr_match_index = result_val->applied_entry_index;
141           if (!result_val->shadowed) {
142           /* new result is known to not be shadowed, so no point to look up further */
143             break;
144           }
145         }
146       }
147     }
148   }
149   DBG("MATCH-RESULT: %d", curr_match_index);
150   return curr_match_index;
151 }
152
153 static void
154 hashtable_add_del(acl_main_t *am, clib_bihash_kv_48_8_t *kv, int is_add)
155 {
156     DBG("HASH ADD/DEL: %016llx %016llx %016llx %016llx %016llx %016llx add %d",
157                         kv->key[0], kv->key[1], kv->key[2],
158                         kv->key[3], kv->key[4], kv->key[5], is_add);
159     BV (clib_bihash_add_del) (&am->acl_lookup_hash, kv, is_add);
160 }
161
162 static void
163 fill_applied_hash_ace_kv(acl_main_t *am,
164                             applied_hash_ace_entry_t **applied_hash_aces,
165                             u32 sw_if_index, u8 is_input,
166                             u32 new_index, clib_bihash_kv_48_8_t *kv)
167 {
168   fa_5tuple_t *kv_key = (fa_5tuple_t *)kv->key;
169   hash_acl_lookup_value_t *kv_val = (hash_acl_lookup_value_t *)&kv->value;
170   applied_hash_ace_entry_t *pae = &((*applied_hash_aces)[new_index]);
171   hash_acl_info_t *ha = &am->hash_acl_infos[pae->acl_index];
172
173   memcpy(kv_key, &ha->rules[pae->hash_ace_info_index].match, sizeof(*kv_key));
174   /* initialize the sw_if_index and direction */
175   kv_key->pkt.sw_if_index = sw_if_index;
176   kv_key->pkt.is_input = is_input;
177   kv_val->as_u64 = 0;
178   kv_val->applied_entry_index = new_index;
179   kv_val->need_portrange_check = ha->rules[pae->hash_ace_info_index].src_portrange_not_powerof2 ||
180                                    ha->rules[pae->hash_ace_info_index].dst_portrange_not_powerof2;
181   /* by default assume all values are shadowed -> check all mask types */
182   kv_val->shadowed = 1;
183 }
184
185 static void
186 add_del_hashtable_entry(acl_main_t *am,
187                             u32 sw_if_index, u8 is_input,
188                             applied_hash_ace_entry_t **applied_hash_aces,
189                             u32 index, int is_add)
190 {
191   clib_bihash_kv_48_8_t kv;
192
193   fill_applied_hash_ace_kv(am, applied_hash_aces, sw_if_index, is_input, index, &kv);
194   hashtable_add_del(am, &kv, is_add);
195 }
196
197
198
199 static void
200 activate_applied_ace_hash_entry(acl_main_t *am,
201                             u32 sw_if_index, u8 is_input,
202                             applied_hash_ace_entry_t **applied_hash_aces,
203                             u32 new_index)
204 {
205   clib_bihash_kv_48_8_t kv;
206   applied_hash_ace_entry_t *pae = &((*applied_hash_aces)[new_index]);
207
208   fill_applied_hash_ace_kv(am, applied_hash_aces, sw_if_index, is_input, new_index, &kv);
209
210   DBG("APPLY ADD KY: %016llx %016llx %016llx %016llx %016llx %016llx",
211                         kv.key[0], kv.key[1], kv.key[2],
212                         kv.key[3], kv.key[4], kv.key[5]);
213
214   clib_bihash_kv_48_8_t result;
215   hash_acl_lookup_value_t *result_val = (hash_acl_lookup_value_t *)&result.value;
216   int res = BV (clib_bihash_search) (&am->acl_lookup_hash, &kv, &result);
217   if (res == 0) {
218     /* There already exists an entry or more. Append at the end. */
219     u32 existing_index = result_val->applied_entry_index;
220     DBG("A key already exists, with applied entry index: %d", existing_index);
221     ASSERT(existing_index != ~0);
222     applied_hash_ace_entry_t *existing_pae = &((*applied_hash_aces)[existing_index]);
223     /* walk the list to the last element */
224     while (existing_pae->next_applied_entry_index != ~0) {
225       if (existing_index == existing_pae->next_applied_entry_index) {
226         DBG("existing and next index of entry %d are the same, abort", existing_index);
227         ASSERT(existing_index != existing_pae->next_applied_entry_index);
228       }
229       existing_index = existing_pae->next_applied_entry_index;
230       existing_pae = &((*applied_hash_aces)[existing_index]);
231       DBG("...advance to chained entry index: %d", existing_index);
232     }
233     /* link ourseves in */
234     existing_pae->next_applied_entry_index = new_index;
235     pae->prev_applied_entry_index = existing_index;
236   } else {
237     /* It's the very first entry */
238     hashtable_add_del(am, &kv, 1);
239   }
240 }
241
242 static void
243 applied_hash_entries_analyze(acl_main_t *am, applied_hash_ace_entry_t **applied_hash_aces)
244 {
245   /*
246    * Go over the rules and check which ones are shadowed and which aren't.
247    * Naive approach: try to match the match value from every ACE as if it
248    * was a live packet, and see if the resulting match happens earlier in the list.
249    * if it does not match or it is later in the ACL - then the entry is not shadowed.
250    *
251    * This approach fails, an example:
252    *   deny tcp 2001:db8::/32 2001:db8::/32
253    *   permit ip 2001:db8::1/128 2001:db8::2/128
254    */
255 }
256
257 void
258 hash_acl_apply(acl_main_t *am, u32 sw_if_index, u8 is_input, int acl_index)
259 {
260   int i;
261
262   DBG("HASH ACL apply: sw_if_index %d is_input %d acl %d", sw_if_index, is_input, acl_index);
263   u32 *acl_vec = is_input ? am->input_acl_vec_by_sw_if_index[sw_if_index] :
264                             am->output_acl_vec_by_sw_if_index[sw_if_index];
265   if (is_input) {
266     vec_validate(am->input_hash_entry_vec_by_sw_if_index, sw_if_index);
267   } else {
268     vec_validate(am->output_hash_entry_vec_by_sw_if_index, sw_if_index);
269   }
270   vec_validate(am->hash_acl_infos, acl_index);
271   applied_hash_ace_entry_t **applied_hash_aces = is_input ? &am->input_hash_entry_vec_by_sw_if_index[sw_if_index] :
272                                                     &am->output_hash_entry_vec_by_sw_if_index[sw_if_index];
273   u32 order_index = vec_search(acl_vec, acl_index);
274   hash_acl_info_t *ha = &am->hash_acl_infos[acl_index];
275   ASSERT(order_index != ~0);
276
277   if (!am->acl_lookup_hash_initialized) {
278     BV (clib_bihash_init) (&am->acl_lookup_hash, "ACL plugin rule lookup bihash",
279                            65536, 2 << 25);
280     am->acl_lookup_hash_initialized = 1;
281   }
282   int base_offset = vec_len(*applied_hash_aces);
283
284   /* Update the bitmap of the mask types with which the lookup
285      needs to happen for the ACLs applied to this sw_if_index */
286   applied_hash_acl_info_t **applied_hash_acls = is_input ? &am->input_applied_hash_acl_info_by_sw_if_index :
287                                                     &am->output_applied_hash_acl_info_by_sw_if_index;
288   vec_validate((*applied_hash_acls), sw_if_index);
289   applied_hash_acl_info_t *pal = &(*applied_hash_acls)[sw_if_index];
290   pal->mask_type_index_bitmap = clib_bitmap_or(pal->mask_type_index_bitmap,
291                                      ha->mask_type_index_bitmap);
292   /*
293    * if the applied ACL is empty, the current code will cause a
294    * different behavior compared to current linear search: an empty ACL will
295    * simply fallthrough to the next ACL, or the default deny in the end.
296    *
297    * This is not a problem, because after vpp-dev discussion,
298    * the consensus was it should not be possible to apply the non-existent
299    * ACL, so the change adding this code also takes care of that.
300    */
301
302   /* expand the applied aces vector by the necessary amount */
303   vec_resize((*applied_hash_aces), vec_len(ha->rules));
304
305   /* add the rules from the ACL to the hash table for lookup and append to the vector*/
306   for(i=0; i < vec_len(ha->rules); i++) {
307     u32 new_index = base_offset + i;
308     applied_hash_ace_entry_t *pae = &((*applied_hash_aces)[new_index]);
309     pae->acl_index = acl_index;
310     pae->ace_index = ha->rules[i].ace_index;
311     pae->action = ha->rules[i].action;
312     pae->hash_ace_info_index = i;
313     /* we might link it in later */
314     pae->next_applied_entry_index = ~0;
315     pae->prev_applied_entry_index = ~0;
316     activate_applied_ace_hash_entry(am, sw_if_index, is_input, applied_hash_aces, new_index);
317   }
318   applied_hash_entries_analyze(am, applied_hash_aces);
319 }
320
321 static void
322 move_applied_ace_hash_entry(acl_main_t *am,
323                             u32 sw_if_index, u8 is_input,
324                             applied_hash_ace_entry_t **applied_hash_aces,
325                             u32 old_index, u32 new_index)
326 {
327
328   /* move the entry */
329   (*applied_hash_aces)[new_index] = (*applied_hash_aces)[old_index];
330
331   /* update the linkage and hash table if necessary */
332   applied_hash_ace_entry_t *pae = &((*applied_hash_aces)[old_index]);
333
334   if (pae->prev_applied_entry_index != ~0) {
335     applied_hash_ace_entry_t *prev_pae = &((*applied_hash_aces)[pae->prev_applied_entry_index]);
336     ASSERT(prev_pae->next_applied_entry_index == old_index);
337     prev_pae->next_applied_entry_index = new_index;
338   } else {
339     /* first entry - so the hash points to it, update */
340     add_del_hashtable_entry(am, sw_if_index, is_input,
341                             applied_hash_aces, new_index, 1);
342   }
343   if (pae->next_applied_entry_index != ~0) {
344     applied_hash_ace_entry_t *next_pae = &((*applied_hash_aces)[pae->next_applied_entry_index]);
345     ASSERT(next_pae->prev_applied_entry_index == old_index);
346     next_pae->prev_applied_entry_index = new_index;
347   }
348   /* invalidate the old entry */
349   pae->prev_applied_entry_index = ~0;
350   pae->next_applied_entry_index = ~0;
351 }
352
353 static void
354 deactivate_applied_ace_hash_entry(acl_main_t *am,
355                             u32 sw_if_index, u8 is_input,
356                             applied_hash_ace_entry_t **applied_hash_aces,
357                             u32 old_index)
358 {
359   applied_hash_ace_entry_t *pae = &((*applied_hash_aces)[old_index]);
360
361   if (pae->next_applied_entry_index != ~0) {
362     applied_hash_ace_entry_t *next_pae = &((*applied_hash_aces)[pae->next_applied_entry_index]);
363     ASSERT(next_pae->prev_applied_entry_index == old_index);
364     next_pae->prev_applied_entry_index = pae->prev_applied_entry_index;
365   }
366
367   if (pae->prev_applied_entry_index != ~0) {
368     applied_hash_ace_entry_t *prev_pae = &((*applied_hash_aces)[pae->prev_applied_entry_index]);
369     ASSERT(prev_pae->next_applied_entry_index == old_index);
370     prev_pae->next_applied_entry_index = pae->next_applied_entry_index;
371   } else {
372     /* It was the first entry. We need either to reset the hash entry or delete it */
373     if (pae->next_applied_entry_index != ~0) {
374       add_del_hashtable_entry(am, sw_if_index, is_input,
375                               applied_hash_aces, pae->next_applied_entry_index, 1);
376     } else {
377       /* no next entry, so just delete the entry in the hash table */
378       add_del_hashtable_entry(am, sw_if_index, is_input,
379                               applied_hash_aces, old_index, 0);
380     }
381   }
382 }
383
384
385 static void
386 hash_acl_build_applied_lookup_bitmap(acl_main_t *am, u32 sw_if_index, u8 is_input)
387 {
388   int i;
389   uword *new_lookup_bitmap = 0;
390   u32 **applied_acls = is_input ? &am->input_acl_vec_by_sw_if_index[sw_if_index] :
391                                   &am->output_acl_vec_by_sw_if_index[sw_if_index];
392   applied_hash_acl_info_t **applied_hash_acls = is_input ? &am->input_applied_hash_acl_info_by_sw_if_index :
393                                                     &am->output_applied_hash_acl_info_by_sw_if_index;
394   applied_hash_acl_info_t *pal = &(*applied_hash_acls)[sw_if_index];
395   for(i=0; i < vec_len(*applied_acls); i++) {
396     u32 a_acl_index = (*applied_acls)[i];
397     hash_acl_info_t *ha = &am->hash_acl_infos[a_acl_index];
398     new_lookup_bitmap = clib_bitmap_or(new_lookup_bitmap,
399                                        ha->mask_type_index_bitmap);
400   }
401   uword *old_lookup_bitmap = pal->mask_type_index_bitmap;
402   pal->mask_type_index_bitmap = new_lookup_bitmap;
403   clib_bitmap_free(old_lookup_bitmap);
404 }
405
406 void
407 hash_acl_unapply(acl_main_t *am, u32 sw_if_index, u8 is_input, int acl_index)
408 {
409   int i;
410
411   DBG("HASH ACL unapply: sw_if_index %d is_input %d acl %d", sw_if_index, is_input, acl_index);
412   hash_acl_info_t *ha = &am->hash_acl_infos[acl_index];
413   applied_hash_ace_entry_t **applied_hash_aces = is_input ? &am->input_hash_entry_vec_by_sw_if_index[sw_if_index] :
414                                                     &am->output_hash_entry_vec_by_sw_if_index[sw_if_index];
415
416   for(i=0; i < vec_len((*applied_hash_aces)); i++) {
417     if ((*applied_hash_aces)[i].acl_index == acl_index) {
418       DBG("Found applied ACL#%d at applied index %d", acl_index, i);
419       break;
420     }
421   }
422   if (vec_len((*applied_hash_aces)) <= i) {
423     DBG("Did not find applied ACL#%d at sw_if_index %d", acl_index, sw_if_index);
424     /* we went all the way without finding any entries. Probably a list was empty. */
425     return;
426   }
427   int base_offset = i;
428   int tail_offset = base_offset + vec_len(ha->rules);
429   int tail_len = vec_len((*applied_hash_aces)) - tail_offset;
430   DBG("base_offset: %d, tail_offset: %d, tail_len: %d", base_offset, tail_offset, tail_len);
431
432   for(i=0; i < vec_len(ha->rules); i ++) {
433     DBG("UNAPPLY DEACTIVATE: sw_if_index %d is_input %d, applied index %d", sw_if_index, is_input, base_offset + i);
434     deactivate_applied_ace_hash_entry(am, sw_if_index, is_input,
435                                       applied_hash_aces, base_offset + i);
436   }
437   for(i=0; i < tail_len; i ++) {
438     /* move the entry at tail offset to base offset */
439     /* that is, from (tail_offset+i) -> (base_offset+i) */
440     DBG("UNAPPLY MOVE: sw_if_index %d is_input %d, applied index %d ->", sw_if_index, is_input, tail_offset+i, base_offset + i);
441     move_applied_ace_hash_entry(am, sw_if_index, is_input, applied_hash_aces, tail_offset + i, base_offset + i);
442   }
443   /* trim the end of the vector */
444   _vec_len((*applied_hash_aces)) -= vec_len(ha->rules);
445
446   applied_hash_entries_analyze(am, applied_hash_aces);
447
448   /* After deletion we might not need some of the mask-types anymore... */
449   hash_acl_build_applied_lookup_bitmap(am, sw_if_index, is_input);
450 }
451
452 /*
453  * Create the applied ACEs and update the hash table,
454  * taking into account that the ACL may not be the last
455  * in the vector of applied ACLs.
456  *
457  * For now, walk from the end of the vector and unapply the ACLs,
458  * then apply the one in question and reapply the rest.
459  */
460
461 void
462 hash_acl_reapply(acl_main_t *am, u32 sw_if_index, u8 is_input, int acl_index)
463 {
464   u32 **applied_acls = is_input ? &am->input_acl_vec_by_sw_if_index[sw_if_index] :
465                                   &am->output_acl_vec_by_sw_if_index[sw_if_index];
466   int i;
467   int start_index = vec_search((*applied_acls), acl_index);
468   /*
469    * This function is called after we find out the sw_if_index where ACL is applied.
470    * If the by-sw_if_index vector does not have the ACL#, then it's a bug.
471    */
472   ASSERT(start_index < vec_len(*applied_acls));
473
474   /* unapply all the ACLs till the current one */
475   for(i = vec_len(*applied_acls) - 1; i >= start_index; i--) {
476     hash_acl_unapply(am, sw_if_index, is_input, (*applied_acls)[i]);
477   }
478   for(i = start_index; i < vec_len(*applied_acls); i++) {
479     hash_acl_apply(am, sw_if_index, is_input, (*applied_acls)[i]);
480   }
481 }
482
483 static void
484 make_address_mask(ip46_address_t *addr, u8 is_ipv6, u8 prefix_len)
485 {
486   if (is_ipv6) {
487     ip6_address_mask_from_width(&addr->ip6, prefix_len);
488   } else {
489     /* FIXME: this may not be correct way */
490     ip6_address_mask_from_width(&addr->ip6, prefix_len + 3*32);
491     ip46_address_mask_ip4(addr);
492   }
493 }
494
495 static u8
496 make_port_mask(u16 *portmask, u16 port_first, u16 port_last)
497 {
498   if (port_first == port_last) {
499     *portmask = 0xffff;
500     /* single port is representable by masked value */
501     return 0;
502   }
503   if ((port_first == 0) && (port_last == 65535)) {
504     *portmask = 0;
505     /* wildcard port is representable by a masked value */
506     return 0;
507   }
508
509   /*
510    * For now match all the ports, later
511    * here might be a better optimization which would
512    * pick out bitmaskable portranges.
513    *
514    * However, adding a new mask type potentially
515    * adds a per-packet extra lookup, so the benefit is not clear.
516    */
517   *portmask = 0;
518   /* This port range can't be represented via bitmask exactly. */
519   return 1;
520 }
521
522 static void
523 make_mask_and_match_from_rule(fa_5tuple_t *mask, acl_rule_t *r, hash_ace_info_t *hi, int match_nonfirst_fragment)
524 {
525   memset(mask, 0, sizeof(*mask));
526   memset(&hi->match, 0, sizeof(hi->match));
527   hi->action = r->is_permit;
528
529   /* we will need to be matching based on sw_if_index, direction, and mask_type_index when applied */
530   mask->pkt.sw_if_index = ~0;
531   mask->pkt.is_input = 1;
532   /* we will assign the match of mask_type_index later when we find it*/
533   mask->pkt.mask_type_index_lsb = ~0;
534
535   mask->pkt.is_ip6 = 1;
536   hi->match.pkt.is_ip6 = r->is_ipv6;
537
538   make_address_mask(&mask->addr[0], r->is_ipv6, r->src_prefixlen);
539   hi->match.addr[0] = r->src;
540   make_address_mask(&mask->addr[1], r->is_ipv6, r->dst_prefixlen);
541   hi->match.addr[1] = r->dst;
542
543   if (r->proto != 0) {
544     mask->l4.proto = ~0; /* L4 proto needs to be matched */
545     hi->match.l4.proto = r->proto;
546     if (match_nonfirst_fragment) {
547       /* match the non-first fragments only */
548       mask->pkt.is_nonfirst_fragment = 1;
549       hi->match.pkt.is_nonfirst_fragment = 1;
550     } else {
551       /* Calculate the src/dst port masks and make the src/dst port matches accordingly */
552       hi->src_portrange_not_powerof2 = make_port_mask(&mask->l4.port[0], r->src_port_or_type_first, r->src_port_or_type_last);
553       hi->match.l4.port[0] = r->src_port_or_type_first & mask->l4.port[0];
554       hi->dst_portrange_not_powerof2 = make_port_mask(&mask->l4.port[1], r->dst_port_or_code_first, r->dst_port_or_code_last);
555       hi->match.l4.port[1] = r->dst_port_or_code_first & mask->l4.port[1];
556       /* L4 info must be valid in order to match */
557       mask->pkt.l4_valid = 1;
558       hi->match.pkt.l4_valid = 1;
559       /* And we must set the mask to check that it is an initial fragment */
560       mask->pkt.is_nonfirst_fragment = 1;
561       hi->match.pkt.is_nonfirst_fragment = 0;
562       if ((r->proto == IPPROTO_TCP) && (r->tcp_flags_mask != 0)) {
563         /* if we want to match on TCP flags, they must be masked off as well */
564         mask->pkt.tcp_flags = r->tcp_flags_mask;
565         hi->match.pkt.tcp_flags = r->tcp_flags_value;
566         /* and the flags need to be present within the packet being matched */
567         mask->pkt.tcp_flags_valid = 1;
568         hi->match.pkt.tcp_flags_valid = 1;
569       }
570     }
571   }
572   /* Sanitize the mask and the match */
573   u64 *pmask = (u64 *)mask;
574   u64 *pmatch = (u64 *)&hi->match;
575   int j;
576   for(j=0; j<6; j++) {
577     pmatch[j] = pmatch[j] & pmask[j];
578   }
579 }
580
581 static u32
582 find_mask_type_index(acl_main_t *am, fa_5tuple_t *mask)
583 {
584   ace_mask_type_entry_t *mte;
585   /* *INDENT-OFF* */
586   pool_foreach(mte, am->ace_mask_type_pool,
587   ({
588     if(memcmp(&mte->mask, mask, sizeof(*mask)) == 0)
589       return (mte - am->ace_mask_type_pool);
590   }));
591   /* *INDENT-ON* */
592   return ~0;
593 }
594
595 static u32
596 assign_mask_type_index(acl_main_t *am, fa_5tuple_t *mask)
597 {
598   u32 mask_type_index = find_mask_type_index(am, mask);
599   ace_mask_type_entry_t *mte;
600   if(~0 == mask_type_index) {
601     pool_get_aligned (am->ace_mask_type_pool, mte, CLIB_CACHE_LINE_BYTES);
602     mask_type_index = mte - am->ace_mask_type_pool;
603     clib_memcpy(&mte->mask, mask, sizeof(mte->mask));
604     mte->refcount = 0;
605     /*
606      * We can use only 16 bits, since in the match there is only u16 field.
607      * Realistically, once you go to 64K of mask types, it is a huge
608      * problem anyway, so we might as well stop half way.
609      */
610     ASSERT(mask_type_index < 32768);
611   }
612   mte = am->ace_mask_type_pool + mask_type_index;
613   mte->refcount++;
614   return mask_type_index;
615 }
616
617 static void
618 release_mask_type_index(acl_main_t *am, u32 mask_type_index)
619 {
620   ace_mask_type_entry_t *mte = &am->ace_mask_type_pool[mask_type_index];
621   mte->refcount--;
622   if (mte->refcount == 0) {
623     /* we are not using this entry anymore */
624     pool_put(am->ace_mask_type_pool, mte);
625   }
626 }
627
628 void hash_acl_add(acl_main_t *am, int acl_index)
629 {
630   DBG("HASH ACL add : %d", acl_index);
631   int i;
632   acl_list_t *a = &am->acls[acl_index];
633   vec_validate(am->hash_acl_infos, acl_index);
634   hash_acl_info_t *ha = &am->hash_acl_infos[acl_index];
635   memset(ha, 0, sizeof(*ha));
636
637   /* walk the newly added ACL entries and ensure that for each of them there
638      is a mask type, increment a reference count for that mask type */
639   for(i=0; i < a->count; i++) {
640     hash_ace_info_t ace_info;
641     fa_5tuple_t mask;
642     memset(&ace_info, 0, sizeof(ace_info));
643     ace_info.acl_index = acl_index;
644     ace_info.ace_index = i;
645
646     make_mask_and_match_from_rule(&mask, &a->rules[i], &ace_info, 0);
647     ace_info.mask_type_index = assign_mask_type_index(am, &mask);
648     /* assign the mask type index for matching itself */
649     ace_info.match.pkt.mask_type_index_lsb = ace_info.mask_type_index;
650     DBG("ACE: %d mask_type_index: %d", i, ace_info.mask_type_index);
651     /* Ensure a given index is set in the mask type index bitmap for this ACL */
652     ha->mask_type_index_bitmap = clib_bitmap_set(ha->mask_type_index_bitmap, ace_info.mask_type_index, 1);
653     vec_add1(ha->rules, ace_info);
654     if (am->l4_match_nonfirst_fragment) {
655       /* add the second rule which matches the noninitial fragments with the respective mask */
656       make_mask_and_match_from_rule(&mask, &a->rules[i], &ace_info, 1);
657       ace_info.mask_type_index = assign_mask_type_index(am, &mask);
658       ace_info.match.pkt.mask_type_index_lsb = ace_info.mask_type_index;
659       DBG("ACE: %d (non-initial frags) mask_type_index: %d", i, ace_info.mask_type_index);
660       /* Ensure a given index is set in the mask type index bitmap for this ACL */
661       ha->mask_type_index_bitmap = clib_bitmap_set(ha->mask_type_index_bitmap, ace_info.mask_type_index, 1);
662       vec_add1(ha->rules, ace_info);
663     }
664   }
665   /*
666    * if an ACL is applied somewhere, fill the corresponding lookup data structures.
667    * We need to take care if the ACL is not the last one in the vector of ACLs applied to the interface.
668    */
669   if (acl_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
670     u32 *sw_if_index;
671     vec_foreach(sw_if_index, am->input_sw_if_index_vec_by_acl[acl_index]) {
672       hash_acl_reapply(am, *sw_if_index, 1, acl_index);
673     }
674   }
675   if (acl_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
676     u32 *sw_if_index;
677     vec_foreach(sw_if_index, am->output_sw_if_index_vec_by_acl[acl_index]) {
678       hash_acl_reapply(am, *sw_if_index, 0, acl_index);
679     }
680   }
681 }
682
683 void hash_acl_delete(acl_main_t *am, int acl_index)
684 {
685   DBG("HASH ACL delete : %d", acl_index);
686   /*
687    * If the ACL is applied somewhere, remove the references of it (call hash_acl_unapply)
688    * this is a different behavior from the linear lookup where an empty ACL is "deny all",
689    *
690    * However, following vpp-dev discussion the ACL that is referenced elsewhere
691    * should not be possible to delete, and the change adding this also adds
692    * the safeguards to that respect, so this is not a problem.
693    */
694   if (acl_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
695     u32 *sw_if_index;
696     vec_foreach(sw_if_index, am->input_sw_if_index_vec_by_acl[acl_index]) {
697       hash_acl_unapply(am, *sw_if_index, 1, acl_index);
698     }
699   }
700   if (acl_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
701     u32 *sw_if_index;
702     vec_foreach(sw_if_index, am->output_sw_if_index_vec_by_acl[acl_index]) {
703       hash_acl_unapply(am, *sw_if_index, 0, acl_index);
704     }
705   }
706
707   /* walk the mask types for the ACL about-to-be-deleted, and decrease
708    * the reference count, possibly freeing up some of them */
709   int i;
710   hash_acl_info_t *ha = &am->hash_acl_infos[acl_index];
711   for(i=0; i < vec_len(ha->rules); i++) {
712     release_mask_type_index(am, ha->rules[i].mask_type_index);
713   }
714   clib_bitmap_free(ha->mask_type_index_bitmap);
715   vec_free(ha->rules);
716 }
717
718 u8
719 hash_multi_acl_match_5tuple (u32 sw_if_index, fa_5tuple_t * pkt_5tuple, int is_l2,
720                        int is_ip6, int is_input, u32 * acl_match_p,
721                        u32 * rule_match_p, u32 * trace_bitmap)
722 {
723   acl_main_t *am = &acl_main;
724   applied_hash_ace_entry_t **applied_hash_aces = is_input ? &am->input_hash_entry_vec_by_sw_if_index[sw_if_index] :
725                                                     &am->output_hash_entry_vec_by_sw_if_index[sw_if_index];
726   u32 match_index = multi_acl_match_get_applied_ace_index(am, pkt_5tuple);
727   if (match_index < vec_len((*applied_hash_aces))) {
728     applied_hash_ace_entry_t *pae = &((*applied_hash_aces)[match_index]);
729     *acl_match_p = pae->acl_index;
730     *rule_match_p = pae->ace_index;
731     return pae->action;
732   }
733   return 0;
734 }
735
736
737 void
738 show_hash_acl_hash (vlib_main_t * vm, acl_main_t *am, u32 verbose)
739 {
740   vlib_cli_output(vm, "\nACL lookup hash table:\n%U\n",
741                   BV (format_bihash), &am->acl_lookup_hash, verbose);
742 }