436e5122b50b397476fc73522277b5b348256f9d
[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
24 #include <vlib/vlib.h>
25 #include <vnet/vnet.h>
26 #include <vnet/pg/pg.h>
27 #include <vppinfra/error.h>
28 #include <vnet/plugin/plugin.h>
29 #include <acl/acl.h>
30 #include <vppinfra/bihash_48_8.h>
31
32 #include "hash_lookup.h"
33 #include "hash_lookup_private.h"
34
35
36 always_inline applied_hash_ace_entry_t **get_applied_hash_aces(acl_main_t *am, u32 lc_index)
37 {
38   applied_hash_ace_entry_t **applied_hash_aces = vec_elt_at_index(am->hash_entry_vec_by_lc_index, lc_index);
39
40 /*is_input ? vec_elt_at_index(am->input_hash_entry_vec_by_sw_if_index, sw_if_index)
41                                                           : vec_elt_at_index(am->output_hash_entry_vec_by_sw_if_index, sw_if_index);
42 */
43   return applied_hash_aces;
44 }
45
46
47 static void
48 hashtable_add_del(acl_main_t *am, clib_bihash_kv_48_8_t *kv, int is_add)
49 {
50     DBG("HASH ADD/DEL: %016llx %016llx %016llx %016llx %016llx %016llx %016llx add %d",
51                         kv->key[0], kv->key[1], kv->key[2],
52                         kv->key[3], kv->key[4], kv->key[5], kv->value, is_add);
53     BV (clib_bihash_add_del) (&am->acl_lookup_hash, kv, is_add);
54 }
55
56 /*
57  * TupleMerge
58  *
59  * Initial adaptation by Valerio Bruschi (valerio.bruschi@telecom-paristech.fr)
60  * based on the TupleMerge [1] simulator kindly made available
61  * by  James Daly (dalyjamese@gmail.com) and  Eric Torng (torng@cse.msu.edu)
62  * ( http://www.cse.msu.edu/~dalyjame/ or http://www.cse.msu.edu/~torng/ ),
63  * refactoring by Andrew Yourtchenko.
64  *
65  * [1] James Daly, Eric Torng "TupleMerge: Building Online Packet Classifiers
66  * by Omitting Bits", In Proc. IEEE ICCCN 2017, pp. 1-10
67  *
68  */
69
70 static int
71 count_bits (u64 word)
72 {
73   int counter = 0;
74   while (word)
75     {
76       counter += word & 1;
77       word >>= 1;
78     }
79   return counter;
80 }
81
82 /* check if mask2 can be contained by mask1 */
83 static u8
84 first_mask_contains_second_mask(int is_ip6, fa_5tuple_t * mask1, fa_5tuple_t * mask2)
85 {
86   int i;
87   if (is_ip6)
88     {
89       for (i = 0; i < 2; i++)
90         {
91           if ((mask1->ip6_addr[0].as_u64[i] & mask2->ip6_addr[0].as_u64[i]) !=
92               mask1->ip6_addr[0].as_u64[i])
93             return 0;
94           if ((mask1->ip6_addr[1].as_u64[i] & mask2->ip6_addr[1].as_u64[i]) !=
95               mask1->ip6_addr[1].as_u64[i])
96             return 0;
97         }
98     }
99   else
100     {
101       /* check the pads, both masks must have it 0 */
102       u32 padcheck = 0;
103       int i;
104       for (i=0; i<6; i++) {
105         padcheck |= mask1->l3_zero_pad[i];
106         padcheck |= mask2->l3_zero_pad[i];
107       }
108       if (padcheck != 0)
109         return 0;
110       if ((mask1->ip4_addr[0].as_u32 & mask2->ip4_addr[0].as_u32) !=
111           mask1->ip4_addr[0].as_u32)
112         return 0;
113       if ((mask1->ip4_addr[1].as_u32 & mask2->ip4_addr[1].as_u32) !=
114           mask1->ip4_addr[1].as_u32)
115         return 0;
116     }
117
118   /* take care if port are not exact-match  */
119   if ((mask1->l4.as_u64 & mask2->l4.as_u64) != mask1->l4.as_u64)
120     return 0;
121
122   if ((mask1->pkt.as_u64 & mask2->pkt.as_u64) != mask1->pkt.as_u64)
123     return 0;
124
125   return 1;
126 }
127
128
129
130 /*
131  * TupleMerge:
132  *
133  * Consider the situation when we have to create a new table
134  * T for a given rule R. This occurs for the first rule inserted and
135  * for later rules if it is incompatible with all existing tables.
136  * In this event, we need to determine mT for a new table.
137  * Setting mT = mR is not a good strategy; if another similar,
138  * but slightly less specific, rule appears we will be unable to
139  * add it to T and will thus have to create another new table. We
140  * thus consider two factors: is the rule more strongly aligned
141  * with source or destination addresses (usually the two most
142  * important fields) and how much slack needs to be given to
143  * allow for other rules. If the source and destination addresses
144  * are close together (within 4 bits for our experiments), we use
145  * both of them. Otherwise, we drop the smaller (less specific)
146  * address and its associated port field from consideration; R is
147  * predominantly aligned with one of the two fields and should
148  * be grouped with other similar rules. This is similar to TSS
149  * dropping port fields, but since it is based on observable rule
150  * characteristics it is more likely to keep important fields and
151  * discard less useful ones.
152  * We then look at the absolute lengths of the addresses. If
153  * the address is long, we are more likely to try to add shorter
154  * lengths and likewise the reverse. We thus remove a few bits
155  * from both address fields with more bits removed from longer
156  * addresses. For 32 bit addresses, we remove 4 bits, 3 for more
157  * than 24, 2 for more than 16, and so on (so 8 and fewer bits
158  * don’t have any removed). We only do this for prefix fields like
159  * addresses; both range fields (like ports) and exact match fields
160  * (like protocol) should remain as they are.
161  */
162
163
164 static u32
165 shift_ip4_if(u32 mask, u32 thresh, int numshifts, u32 else_val)
166 {
167   if (mask > thresh)
168      return clib_host_to_net_u32((clib_net_to_host_u32(mask) << numshifts) & 0xFFFFFFFF);
169   else
170      return else_val;
171 }
172
173 static void
174 relax_ip4_addr(ip4_address_t *ip4_mask, int relax2) {
175   int shifts_per_relax[2][4] = { { 6, 5, 4, 2 }, { 3, 2, 1, 1 } };
176
177   int *shifts = shifts_per_relax[relax2];
178   if(ip4_mask->as_u32 == 0xffffffff)
179     ip4_mask->as_u32 = clib_host_to_net_u32((clib_net_to_host_u32(ip4_mask->as_u32) << shifts[0])&0xFFFFFFFF);
180   else
181     ip4_mask->as_u32 = shift_ip4_if(ip4_mask->as_u32, 0xffffff00, shifts[1],
182                         shift_ip4_if(ip4_mask->as_u32, 0xffff0000, shifts[2],
183                           shift_ip4_if(ip4_mask->as_u32, 0xff000000, shifts[3], ip4_mask->as_u32)));
184 }
185
186 static void
187 relax_ip6_addr(ip6_address_t *ip6_mask, int relax2) {
188   /*
189    * This "better than nothing" relax logic is based on heuristics
190    * from IPv6 knowledge, and may not be optimal.
191    * Some further tuning may be needed in the future.
192    */
193   if (ip6_mask->as_u64[0] == 0xffffffffffffffffULL) {
194     if (ip6_mask->as_u64[1] == 0xffffffffffffffffULL) {
195       /* relax a /128 down to /64  - likely to have more hosts */
196       ip6_mask->as_u64[1] = 0;
197     } else if (ip6_mask->as_u64[1] == 0) {
198       /* relax a /64 down to /56 - likely to have more subnets */
199       ip6_mask->as_u64[0] = clib_host_to_net_u64(0xffffffffffffff00ULL);
200     }
201   }
202 }
203
204 static void
205 relax_tuple(fa_5tuple_t *mask, int is_ip6, int relax2){
206         fa_5tuple_t save_mask = *mask;
207
208         int counter_s = 0, counter_d = 0;
209         if (is_ip6) {
210           int i;
211           for(i=0; i<2; i++){
212                 counter_s += count_bits(mask->ip6_addr[0].as_u64[i]);
213                 counter_d += count_bits(mask->ip6_addr[1].as_u64[i]);
214           }
215         } else {
216                 counter_s += count_bits(mask->ip4_addr[0].as_u32);
217                 counter_d += count_bits(mask->ip4_addr[1].as_u32);
218         }
219
220 /*
221  * is the rule more strongly aligned with source or destination addresses
222  * (usually the two most important fields) and how much slack needs to be
223  * given to allow for other rules. If the source and destination addresses
224  * are close together (within 4 bits for our experiments), we use both of them.
225  * Otherwise, we drop the smaller (less specific) address and its associated
226  * port field from consideration
227  */
228         const int deltaThreshold = 4;
229         /* const int deltaThreshold = 8; if IPV6? */
230         int delta = counter_s - counter_d;
231         if (-delta > deltaThreshold) {
232                 if (is_ip6)
233                   mask->ip6_addr[0].as_u64[1] = mask->ip6_addr[0].as_u64[0] = 0;
234                 else
235                   mask->ip4_addr[0].as_u32 = 0;
236                 mask->l4.port[0] = 0;
237         } else if (delta > deltaThreshold) {
238                 if (is_ip6)
239                   mask->ip6_addr[1].as_u64[1] = mask->ip6_addr[1].as_u64[0] = 0;
240                 else
241                   mask->ip4_addr[1].as_u32 = 0;
242                 mask->l4.port[1] = 0;
243         }
244
245         if (is_ip6) {
246           relax_ip6_addr(&mask->ip6_addr[0], relax2);
247           relax_ip6_addr(&mask->ip6_addr[1], relax2);
248         } else {
249           relax_ip4_addr(&mask->ip4_addr[0], relax2);
250           relax_ip4_addr(&mask->ip4_addr[1], relax2);
251         }
252         mask->pkt.is_nonfirst_fragment = 0;
253         mask->pkt.l4_valid = 0;
254         if(!first_mask_contains_second_mask(is_ip6, mask, &save_mask)){
255                 DBG( "TM-relaxing-ERROR");
256                 *mask = save_mask;
257         }
258         DBG( "TM-relaxing-end");
259 }
260
261 static u32
262 find_mask_type_index(acl_main_t *am, fa_5tuple_t *mask)
263 {
264   ace_mask_type_entry_t *mte;
265   /* *INDENT-OFF* */
266   pool_foreach(mte, am->ace_mask_type_pool,
267   ({
268     if(memcmp(&mte->mask, mask, sizeof(*mask)) == 0)
269       return (mte - am->ace_mask_type_pool);
270   }));
271   /* *INDENT-ON* */
272   return ~0;
273 }
274
275 static u32
276 assign_mask_type_index(acl_main_t *am, fa_5tuple_t *mask)
277 {
278   u32 mask_type_index = find_mask_type_index(am, mask);
279   ace_mask_type_entry_t *mte;
280   if(~0 == mask_type_index) {
281     pool_get_aligned (am->ace_mask_type_pool, mte, CLIB_CACHE_LINE_BYTES);
282     mask_type_index = mte - am->ace_mask_type_pool;
283     clib_memcpy_fast(&mte->mask, mask, sizeof(mte->mask));
284     mte->refcount = 0;
285
286     /*
287      * We can use only 16 bits, since in the match there is only u16 field.
288      * Realistically, once you go to 64K of mask types, it is a huge
289      * problem anyway, so we might as well stop half way.
290      */
291     ASSERT(mask_type_index < 32768);
292   }
293   mte = am->ace_mask_type_pool + mask_type_index;
294   mte->refcount++;
295   DBG0("ASSIGN MTE index %d new refcount %d", mask_type_index, mte->refcount);
296   return mask_type_index;
297 }
298
299 static void
300 lock_mask_type_index(acl_main_t *am, u32 mask_type_index)
301 {
302   DBG0("LOCK MTE index %d", mask_type_index);
303   ace_mask_type_entry_t *mte = pool_elt_at_index(am->ace_mask_type_pool, mask_type_index);
304   mte->refcount++;
305   DBG0("LOCK MTE index %d new refcount %d", mask_type_index, mte->refcount);
306 }
307
308
309 static void
310 release_mask_type_index(acl_main_t *am, u32 mask_type_index)
311 {
312   DBG0("RELEAS MTE index %d", mask_type_index);
313   ace_mask_type_entry_t *mte = pool_elt_at_index(am->ace_mask_type_pool, mask_type_index);
314   mte->refcount--;
315   DBG0("RELEAS MTE index %d new refcount %d", mask_type_index, mte->refcount);
316   if (mte->refcount == 0) {
317     /* we are not using this entry anymore */
318     clib_memset(mte, 0xae, sizeof(*mte));
319     pool_put(am->ace_mask_type_pool, mte);
320   }
321 }
322
323
324 static u32
325 tm_assign_mask_type_index(acl_main_t *am, fa_5tuple_t *mask, int is_ip6, u32 lc_index)
326 {
327         u32 mask_type_index = ~0;
328         u32 for_mask_type_index = ~0;
329         ace_mask_type_entry_t *mte = 0;
330         int order_index;
331         /* look for existing mask comparable with the one in input */
332
333         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);
334         hash_applied_mask_info_t *minfo;
335
336         if (vec_len(*hash_applied_mask_info_vec) > 0) {
337             for(order_index = vec_len((*hash_applied_mask_info_vec)) -1; order_index >= 0; order_index--) {
338                 minfo = vec_elt_at_index((*hash_applied_mask_info_vec), order_index);
339                 for_mask_type_index = minfo->mask_type_index;
340                 mte = vec_elt_at_index(am->ace_mask_type_pool, for_mask_type_index);
341                 if(first_mask_contains_second_mask(is_ip6, &mte->mask, mask)){
342                         mask_type_index = (mte - am->ace_mask_type_pool);
343                         lock_mask_type_index(am, mask_type_index);
344                         break;
345                 }
346             }
347         }
348
349         if(~0 == mask_type_index) {
350                 /* if no mask is found, then let's use a relaxed version of the original one, in order to be used by new ace_entries */
351                 DBG( "TM-assigning mask type index-new one");
352                 fa_5tuple_t relaxed_mask = *mask;
353                 relax_tuple(&relaxed_mask, is_ip6, 0);
354                 mask_type_index = assign_mask_type_index(am, &relaxed_mask);
355
356                 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);
357
358                 int spot = vec_len((*hash_applied_mask_info_vec));
359                 vec_validate((*hash_applied_mask_info_vec), spot);
360                 minfo = vec_elt_at_index((*hash_applied_mask_info_vec), spot);
361                 minfo->mask_type_index = mask_type_index;
362                 minfo->num_entries = 0;
363                 minfo->max_collisions = 0;
364                 minfo->first_rule_index = ~0;
365
366                 /*
367                  * We can use only 16 bits, since in the match there is only u16 field.
368                  * Realistically, once you go to 64K of mask types, it is a huge
369                  * problem anyway, so we might as well stop half way.
370                  */
371                 ASSERT(mask_type_index < 32768);
372         }
373         mte = am->ace_mask_type_pool + mask_type_index;
374         DBG0("TM-ASSIGN MTE index %d new refcount %d", mask_type_index, mte->refcount);
375         return mask_type_index;
376 }
377
378
379 static void
380 fill_applied_hash_ace_kv(acl_main_t *am,
381                             applied_hash_ace_entry_t **applied_hash_aces,
382                             u32 lc_index,
383                             u32 new_index, clib_bihash_kv_48_8_t *kv)
384 {
385   fa_5tuple_t *kv_key = (fa_5tuple_t *)kv->key;
386   hash_acl_lookup_value_t *kv_val = (hash_acl_lookup_value_t *)&kv->value;
387   applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), new_index);
388   hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, pae->acl_index);
389
390   /* apply the mask to ace key */
391   hash_ace_info_t *ace_info = vec_elt_at_index(ha->rules, pae->hash_ace_info_index);
392   ace_mask_type_entry_t *mte = vec_elt_at_index(am->ace_mask_type_pool, pae->mask_type_index);
393
394   u64 *pmatch = (u64 *) &ace_info->match;
395   u64 *pmask = (u64 *)&mte->mask;
396   u64 *pkey = (u64 *)kv->key;
397
398   *pkey++ = *pmatch++ & *pmask++;
399   *pkey++ = *pmatch++ & *pmask++;
400   *pkey++ = *pmatch++ & *pmask++;
401   *pkey++ = *pmatch++ & *pmask++;
402   *pkey++ = *pmatch++ & *pmask++;
403   *pkey++ = *pmatch++ & *pmask++;
404
405   kv_key->pkt.mask_type_index_lsb = pae->mask_type_index;
406   kv_key->pkt.lc_index = lc_index;
407   kv_val->as_u64 = 0;
408   kv_val->applied_entry_index = new_index;
409 }
410
411 static void
412 add_del_hashtable_entry(acl_main_t *am,
413                             u32 lc_index,
414                             applied_hash_ace_entry_t **applied_hash_aces,
415                             u32 index, int is_add)
416 {
417   clib_bihash_kv_48_8_t kv;
418
419   fill_applied_hash_ace_kv(am, applied_hash_aces, lc_index, index, &kv);
420   hashtable_add_del(am, &kv, is_add);
421 }
422
423
424 static void
425 remake_hash_applied_mask_info_vec (acl_main_t * am,
426                                    applied_hash_ace_entry_t **
427                                    applied_hash_aces, u32 lc_index)
428 {
429   DBG0("remake applied hash mask info lc_index %d", lc_index);
430   hash_applied_mask_info_t *new_hash_applied_mask_info_vec =
431     vec_new (hash_applied_mask_info_t, 0);
432
433   hash_applied_mask_info_t *minfo;
434   int i;
435   for (i = 0; i < vec_len ((*applied_hash_aces)); i++)
436     {
437       applied_hash_ace_entry_t *pae =
438         vec_elt_at_index ((*applied_hash_aces), i);
439
440       /* check if mask_type_index is already there */
441       u32 new_pointer = vec_len (new_hash_applied_mask_info_vec);
442       int search;
443       for (search = 0; search < vec_len (new_hash_applied_mask_info_vec);
444            search++)
445         {
446           minfo = vec_elt_at_index (new_hash_applied_mask_info_vec, search);
447           if (minfo->mask_type_index == pae->mask_type_index)
448             break;
449         }
450        
451       vec_validate ((new_hash_applied_mask_info_vec), search);
452       minfo = vec_elt_at_index ((new_hash_applied_mask_info_vec), search);
453       if (search == new_pointer)
454         {
455           DBG0("remaking index %d", search);
456           minfo->mask_type_index = pae->mask_type_index;
457           minfo->num_entries = 0;
458           minfo->max_collisions = 0;
459           minfo->first_rule_index = ~0;
460         }
461
462       minfo->num_entries = minfo->num_entries + 1;
463
464       if (vec_len (pae->colliding_rules) > minfo->max_collisions)
465         minfo->max_collisions = vec_len (pae->colliding_rules);
466
467       if (minfo->first_rule_index > i)
468         minfo->first_rule_index = i;
469     }
470
471   hash_applied_mask_info_t **hash_applied_mask_info_vec =
472     vec_elt_at_index (am->hash_applied_mask_info_vec_by_lc_index, lc_index);
473
474   vec_free ((*hash_applied_mask_info_vec));
475   (*hash_applied_mask_info_vec) = new_hash_applied_mask_info_vec;
476 }
477
478 static void
479 vec_del_collision_rule (collision_match_rule_t ** pvec,
480                         u32 applied_entry_index)
481 {
482   u32 i = 0;
483   u32 deleted = 0;
484   while (i < _vec_len ((*pvec)))
485     {
486       collision_match_rule_t *cr = vec_elt_at_index ((*pvec), i);
487       if (cr->applied_entry_index == applied_entry_index)
488         {
489           /* vec_del1 ((*pvec), i) would be more efficient but would reorder the elements. */
490           vec_delete((*pvec), 1, i);
491           deleted++;
492           DBG0("vec_del_collision_rule deleting one at index %d", i);
493         }
494       else
495         {
496           i++;
497         }
498     }
499   ASSERT(deleted > 0);
500 }
501
502 static void
503 acl_plugin_print_pae (vlib_main_t * vm, int j, applied_hash_ace_entry_t * pae);
504
505 static void
506 del_colliding_rule (applied_hash_ace_entry_t ** applied_hash_aces,
507                     u32 head_index, u32 applied_entry_index)
508 {
509   DBG0("DEL COLLIDING RULE: head_index %d applied index %d", head_index, applied_entry_index);
510
511
512   applied_hash_ace_entry_t *head_pae =
513     vec_elt_at_index ((*applied_hash_aces), head_index);
514   if (ACL_HASH_LOOKUP_DEBUG > 0)
515     acl_plugin_print_pae(acl_main.vlib_main, head_index, head_pae);
516   vec_del_collision_rule (&head_pae->colliding_rules, applied_entry_index);
517   if (vec_len(head_pae->colliding_rules) == 0) {
518     vec_free(head_pae->colliding_rules);
519   }
520   if (ACL_HASH_LOOKUP_DEBUG > 0)
521     acl_plugin_print_pae(acl_main.vlib_main, head_index, head_pae);
522 }
523
524 static void
525 add_colliding_rule (acl_main_t * am,
526                     applied_hash_ace_entry_t ** applied_hash_aces,
527                     u32 head_index, u32 applied_entry_index)
528 {
529   applied_hash_ace_entry_t *head_pae =
530     vec_elt_at_index ((*applied_hash_aces), head_index);
531   applied_hash_ace_entry_t *pae =
532     vec_elt_at_index ((*applied_hash_aces), applied_entry_index);
533   DBG0("ADD COLLIDING RULE: head_index %d applied index %d", head_index, applied_entry_index);
534   if (ACL_HASH_LOOKUP_DEBUG > 0)
535     acl_plugin_print_pae(acl_main.vlib_main, head_index, head_pae);
536
537   collision_match_rule_t cr;
538
539   cr.acl_index = pae->acl_index;
540   cr.ace_index = pae->ace_index;
541   cr.acl_position = pae->acl_position;
542   cr.applied_entry_index = applied_entry_index;
543   cr.rule = am->acls[pae->acl_index].rules[pae->ace_index];
544   pae->collision_head_ae_index = head_index;
545   vec_add1 (head_pae->colliding_rules, cr);
546   if (ACL_HASH_LOOKUP_DEBUG > 0)
547     acl_plugin_print_pae(acl_main.vlib_main, head_index, head_pae);
548 }
549
550 static u32
551 activate_applied_ace_hash_entry(acl_main_t *am,
552                             u32 lc_index,
553                             applied_hash_ace_entry_t **applied_hash_aces,
554                             u32 new_index)
555 {
556   clib_bihash_kv_48_8_t kv;
557   ASSERT(new_index != ~0);
558   DBG("activate_applied_ace_hash_entry lc_index %d new_index %d", lc_index, new_index);
559
560   fill_applied_hash_ace_kv(am, applied_hash_aces, lc_index, new_index, &kv);
561
562   DBG("APPLY ADD KY: %016llx %016llx %016llx %016llx %016llx %016llx",
563                         kv.key[0], kv.key[1], kv.key[2],
564                         kv.key[3], kv.key[4], kv.key[5]);
565
566   clib_bihash_kv_48_8_t result;
567   hash_acl_lookup_value_t *result_val = (hash_acl_lookup_value_t *)&result.value;
568   int res = BV (clib_bihash_search) (&am->acl_lookup_hash, &kv, &result);
569   ASSERT(new_index != ~0);
570   ASSERT(new_index < vec_len((*applied_hash_aces)));
571   if (res == 0) {
572     u32 first_index = result_val->applied_entry_index;
573     ASSERT(first_index != ~0);
574     ASSERT(first_index < vec_len((*applied_hash_aces)));
575     /* There already exists an entry or more. Append at the end. */
576     DBG("A key already exists, with applied entry index: %d", first_index);
577     add_colliding_rule(am, applied_hash_aces, first_index, new_index);
578     return first_index;
579   } else {
580     /* It's the very first entry */
581     hashtable_add_del(am, &kv, 1);
582     ASSERT(new_index != ~0);
583     add_colliding_rule(am, applied_hash_aces, new_index, new_index);
584     return new_index;
585   }
586 }
587
588
589 static void *
590 hash_acl_set_heap(acl_main_t *am)
591 {
592   if (0 == am->hash_lookup_mheap) {
593     am->hash_lookup_mheap = mheap_alloc_with_lock (0 /* use VM */ , 
594                                                    am->hash_lookup_mheap_size,
595                                                    1 /* locked */);
596     if (0 == am->hash_lookup_mheap) {
597         clib_error("ACL plugin failed to allocate lookup heap of %U bytes", 
598                    format_memory_size, am->hash_lookup_mheap_size);
599     }
600 #if USE_DLMALLOC != 0
601     /*
602      * DLMALLOC is being "helpful" in that it ignores the heap size parameter
603      * by default and tries to allocate the larger amount of memory.
604      *
605      * Pin the heap so this does not happen and if we run out of memory
606      * in this heap, we will bail out with "out of memory", rather than
607      * an obscure error sometime later.
608      */
609     mspace_disable_expand(am->hash_lookup_mheap);
610 #endif
611   }
612   void *oldheap = clib_mem_set_heap(am->hash_lookup_mheap);
613   return oldheap;
614 }
615
616 void
617 acl_plugin_hash_acl_set_validate_heap(int on)
618 {
619   acl_main_t *am = &acl_main;
620   clib_mem_set_heap(hash_acl_set_heap(am));
621 #if USE_DLMALLOC == 0
622   mheap_t *h = mheap_header (am->hash_lookup_mheap);
623   if (on) {
624     h->flags |= MHEAP_FLAG_VALIDATE;
625     h->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
626     mheap_validate(h);
627   } else {
628     h->flags &= ~MHEAP_FLAG_VALIDATE;
629     h->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
630   }
631 #endif
632 }
633
634 void
635 acl_plugin_hash_acl_set_trace_heap(int on)
636 {
637   acl_main_t *am = &acl_main;
638   clib_mem_set_heap(hash_acl_set_heap(am));
639 #if USE_DLMALLOC == 0
640   mheap_t *h = mheap_header (am->hash_lookup_mheap);
641   if (on) {
642     h->flags |= MHEAP_FLAG_TRACE;
643   } else {
644     h->flags &= ~MHEAP_FLAG_TRACE;
645   }
646 #endif
647 }
648
649 static void
650 assign_mask_type_index_to_pae(acl_main_t *am, u32 lc_index, int is_ip6, applied_hash_ace_entry_t *pae)
651 {
652   hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, pae->acl_index);
653   hash_ace_info_t *ace_info = vec_elt_at_index(ha->rules, pae->hash_ace_info_index);
654
655   ace_mask_type_entry_t *mte;
656   fa_5tuple_t mask;
657   /*
658    * Start taking base_mask associated to ace, and essentially copy it.
659    * With TupleMerge we will assign a relaxed mask here.
660    */
661   mte = vec_elt_at_index(am->ace_mask_type_pool, ace_info->base_mask_type_index);
662   mask = mte->mask;
663   if (am->use_tuple_merge)
664     pae->mask_type_index = tm_assign_mask_type_index(am, &mask, is_ip6, lc_index);
665   else
666     pae->mask_type_index = assign_mask_type_index(am, &mask);
667 }
668
669 static void
670 split_partition(acl_main_t *am, u32 first_index,
671                             u32 lc_index, int is_ip6);
672
673
674 static void
675 check_collision_count_and_maybe_split(acl_main_t *am, u32 lc_index, int is_ip6, u32 first_index)
676 {
677   applied_hash_ace_entry_t **applied_hash_aces = get_applied_hash_aces(am, lc_index);
678   applied_hash_ace_entry_t *first_pae = vec_elt_at_index((*applied_hash_aces), first_index);
679   if (vec_len(first_pae->colliding_rules) > am->tuple_merge_split_threshold) {
680     split_partition(am, first_index, lc_index, is_ip6);
681   }
682 }
683
684 void
685 hash_acl_apply(acl_main_t *am, u32 lc_index, int acl_index, u32 acl_position)
686 {
687   int i;
688
689   DBG0("HASH ACL apply: lc_index %d acl %d", lc_index, acl_index);
690   if (!am->acl_lookup_hash_initialized) {
691     BV (clib_bihash_init) (&am->acl_lookup_hash, "ACL plugin rule lookup bihash",
692                            am->hash_lookup_hash_buckets, am->hash_lookup_hash_memory);
693     am->acl_lookup_hash_initialized = 1;
694   }
695
696   void *oldheap = hash_acl_set_heap(am);
697   vec_validate(am->hash_entry_vec_by_lc_index, lc_index);
698   vec_validate(am->hash_acl_infos, acl_index);
699   applied_hash_ace_entry_t **applied_hash_aces = get_applied_hash_aces(am, lc_index);
700
701   hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
702   u32 **hash_acl_applied_lc_index = &ha->lc_index_list;
703
704   int base_offset = vec_len(*applied_hash_aces);
705
706   /* Update the bitmap of the mask types with which the lookup
707      needs to happen for the ACLs applied to this lc_index */
708   applied_hash_acl_info_t **applied_hash_acls = &am->applied_hash_acl_info_by_lc_index;
709   vec_validate((*applied_hash_acls), lc_index);
710   applied_hash_acl_info_t *pal = vec_elt_at_index((*applied_hash_acls), lc_index);
711
712   /* ensure the list of applied hash acls is initialized and add this acl# to it */
713   u32 index = vec_search(pal->applied_acls, acl_index);
714   if (index != ~0) {
715     clib_warning("BUG: trying to apply twice acl_index %d on lc_index %d, according to lc",
716                  acl_index, lc_index);
717     goto done;
718   }
719   vec_add1(pal->applied_acls, acl_index);
720   u32 index2 = vec_search((*hash_acl_applied_lc_index), lc_index);
721   if (index2 != ~0) {
722     clib_warning("BUG: trying to apply twice acl_index %d on lc_index %d, according to hash h-acl info",
723                  acl_index, lc_index);
724     goto done;
725   }
726   vec_add1((*hash_acl_applied_lc_index), lc_index);
727
728   /*
729    * if the applied ACL is empty, the current code will cause a
730    * different behavior compared to current linear search: an empty ACL will
731    * simply fallthrough to the next ACL, or the default deny in the end.
732    *
733    * This is not a problem, because after vpp-dev discussion,
734    * the consensus was it should not be possible to apply the non-existent
735    * ACL, so the change adding this code also takes care of that.
736    */
737
738
739   vec_validate(am->hash_applied_mask_info_vec_by_lc_index, lc_index);
740
741   /* since we know (in case of no split) how much we expand, preallocate that space */
742   if (vec_len(ha->rules) > 0) {
743     int old_vec_len = vec_len(*applied_hash_aces);
744     vec_validate((*applied_hash_aces), old_vec_len + vec_len(ha->rules) - 1);
745     _vec_len((*applied_hash_aces)) = old_vec_len;
746   }
747
748   /* add the rules from the ACL to the hash table for lookup and append to the vector*/
749   for(i=0; i < vec_len(ha->rules); i++) {
750     /*
751      * Expand the applied aces vector to fit a new entry.
752      * One by one not to upset split_partition() if it is called.
753      */
754     vec_resize((*applied_hash_aces), 1);
755
756     int is_ip6 = ha->rules[i].match.pkt.is_ip6;
757     u32 new_index = base_offset + i;
758     applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), new_index);
759     pae->acl_index = acl_index;
760     pae->ace_index = ha->rules[i].ace_index;
761     pae->acl_position = acl_position;
762     pae->action = ha->rules[i].action;
763     pae->hitcount = 0;
764     pae->hash_ace_info_index = i;
765     /* we might link it in later */
766     pae->collision_head_ae_index = ~0;
767     pae->colliding_rules = NULL;
768     pae->mask_type_index = ~0;
769     assign_mask_type_index_to_pae(am, lc_index, is_ip6, pae);
770     u32 first_index = activate_applied_ace_hash_entry(am, lc_index, applied_hash_aces, new_index);
771     if (am->use_tuple_merge)
772       check_collision_count_and_maybe_split(am, lc_index, is_ip6, first_index);
773   }
774   remake_hash_applied_mask_info_vec(am, applied_hash_aces, lc_index);
775 done:
776   clib_mem_set_heap (oldheap);
777 }
778
779 static u32
780 find_head_applied_ace_index(applied_hash_ace_entry_t **applied_hash_aces, u32 curr_index)
781 {
782   ASSERT(curr_index != ~0);
783   applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), curr_index);
784   ASSERT(pae);
785   ASSERT(pae->collision_head_ae_index != ~0);
786   return pae->collision_head_ae_index;
787 }
788
789 static void
790 set_collision_head_ae_index(applied_hash_ace_entry_t **applied_hash_aces, collision_match_rule_t *colliding_rules, u32 new_index)
791 {
792         collision_match_rule_t *cr;
793         vec_foreach(cr, colliding_rules) {
794             applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), cr->applied_entry_index);
795             pae->collision_head_ae_index = new_index;
796         }
797 }
798
799 static void
800 move_applied_ace_hash_entry(acl_main_t *am,
801                             u32 lc_index,
802                             applied_hash_ace_entry_t **applied_hash_aces,
803                             u32 old_index, u32 new_index)
804 {
805   ASSERT(old_index != ~0);
806   ASSERT(new_index != ~0);
807   /* move the entry */
808   *vec_elt_at_index((*applied_hash_aces), new_index) = *vec_elt_at_index((*applied_hash_aces), old_index);
809
810   /* update the linkage and hash table if necessary */
811   applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), old_index);
812   applied_hash_ace_entry_t *new_pae = vec_elt_at_index((*applied_hash_aces), new_index);
813
814   if (ACL_HASH_LOOKUP_DEBUG > 0) {
815     clib_warning("Moving pae from %d to %d", old_index, new_index);
816     acl_plugin_print_pae(am->vlib_main, old_index, pae);
817   }
818
819   if (pae->collision_head_ae_index == old_index) {
820     /* first entry - so the hash points to it, update */
821     add_del_hashtable_entry(am, lc_index,
822                             applied_hash_aces, new_index, 1);
823   }
824   if (new_pae->colliding_rules) {
825     /* update the information within the collision rule entry */
826     ASSERT(vec_len(new_pae->colliding_rules) > 0);
827     collision_match_rule_t *cr = vec_elt_at_index (new_pae->colliding_rules, 0);
828     ASSERT(cr->applied_entry_index == old_index);
829     cr->applied_entry_index = new_index;
830     set_collision_head_ae_index(applied_hash_aces, new_pae->colliding_rules, new_index);
831   } else {
832     /* find the index in the collision rule entry on the head element */
833     u32 head_index = find_head_applied_ace_index(applied_hash_aces, new_index);
834     ASSERT(head_index != ~0);
835     applied_hash_ace_entry_t *head_pae = vec_elt_at_index((*applied_hash_aces), head_index);
836     ASSERT(vec_len(head_pae->colliding_rules) > 0);
837     u32 i;
838     for (i=0; i<vec_len(head_pae->colliding_rules); i++) {
839       collision_match_rule_t *cr = vec_elt_at_index (head_pae->colliding_rules, i);
840       if (cr->applied_entry_index == old_index) {
841         cr->applied_entry_index = new_index;
842       }
843     }
844     if (ACL_HASH_LOOKUP_DEBUG > 0) {
845       clib_warning("Head pae at index %d after adjustment", head_index);
846       acl_plugin_print_pae(am->vlib_main, head_index, head_pae);
847     }
848   }
849   /* invalidate the old entry */
850   pae->collision_head_ae_index = ~0;
851   pae->colliding_rules = NULL;
852 }
853
854 static void
855 deactivate_applied_ace_hash_entry(acl_main_t *am,
856                             u32 lc_index,
857                             applied_hash_ace_entry_t **applied_hash_aces,
858                             u32 old_index)
859 {
860   applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), old_index);
861   DBG("UNAPPLY DEACTIVATE: lc_index %d applied index %d", lc_index, old_index);
862   if (ACL_HASH_LOOKUP_DEBUG > 0) {
863     clib_warning("Deactivating pae at index %d", old_index);
864     acl_plugin_print_pae(am->vlib_main, old_index, pae);
865   }
866
867   if (pae->collision_head_ae_index != old_index) {
868     DBG("UNAPPLY = index %d has collision head %d", old_index, pae->collision_head_ae_index);
869
870     u32 head_index = find_head_applied_ace_index(applied_hash_aces, old_index);
871     ASSERT(head_index != ~0);
872     del_colliding_rule(applied_hash_aces, head_index, old_index);
873
874   } else {
875     /* It was the first entry. We need either to reset the hash entry or delete it */
876     /* delete our entry from the collision vector first */
877     del_colliding_rule(applied_hash_aces, old_index, old_index);
878     if (vec_len(pae->colliding_rules) > 0) {
879       u32 next_pae_index = pae->colliding_rules[0].applied_entry_index;
880       applied_hash_ace_entry_t *next_pae = vec_elt_at_index((*applied_hash_aces), next_pae_index);
881       /* Remove ourselves and transfer the ownership of the colliding rules vector */
882       next_pae->colliding_rules = pae->colliding_rules;
883       set_collision_head_ae_index(applied_hash_aces, next_pae->colliding_rules, next_pae_index);
884       add_del_hashtable_entry(am, lc_index,
885                               applied_hash_aces, next_pae_index, 1);
886     } else {
887       /* no next entry, so just delete the entry in the hash table */
888       add_del_hashtable_entry(am, lc_index,
889                               applied_hash_aces, old_index, 0);
890     }
891   }
892   DBG0("Releasing mask type index %d for pae index %d on lc_index %d", pae->mask_type_index, old_index, lc_index);
893   release_mask_type_index(am, pae->mask_type_index);
894   /* invalidate the old entry */
895   pae->mask_type_index = ~0;
896   pae->collision_head_ae_index = ~0;
897   /* always has to be 0 */
898   pae->colliding_rules = NULL;
899 }
900
901
902 void
903 hash_acl_unapply(acl_main_t *am, u32 lc_index, int acl_index)
904 {
905   int i;
906
907   DBG0("HASH ACL unapply: lc_index %d acl %d", lc_index, acl_index);
908   applied_hash_acl_info_t **applied_hash_acls = &am->applied_hash_acl_info_by_lc_index;
909   applied_hash_acl_info_t *pal = vec_elt_at_index((*applied_hash_acls), lc_index);
910
911   hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
912   u32 **hash_acl_applied_lc_index = &ha->lc_index_list;
913
914   if (ACL_HASH_LOOKUP_DEBUG > 0) {
915     clib_warning("unapplying acl %d", acl_index);
916     acl_plugin_show_tables_mask_type();
917     acl_plugin_show_tables_acl_hash_info(acl_index);
918     acl_plugin_show_tables_applied_info(lc_index);
919   }
920
921   /* remove this acl# from the list of applied hash acls */
922   u32 index = vec_search(pal->applied_acls, acl_index);
923   if (index == ~0) {
924     clib_warning("BUG: trying to unapply unapplied acl_index %d on lc_index %d, according to lc",
925                  acl_index, lc_index);
926     return;
927   }
928   vec_del1(pal->applied_acls, index);
929
930   u32 index2 = vec_search((*hash_acl_applied_lc_index), lc_index);
931   if (index2 == ~0) {
932     clib_warning("BUG: trying to unapply twice acl_index %d on lc_index %d, according to h-acl info",
933                  acl_index, lc_index);
934     return;
935   }
936   vec_del1((*hash_acl_applied_lc_index), index2);
937
938   applied_hash_ace_entry_t **applied_hash_aces = get_applied_hash_aces(am, lc_index);
939
940   for(i=0; i < vec_len((*applied_hash_aces)); i++) {
941     if (vec_elt_at_index(*applied_hash_aces,i)->acl_index == acl_index) {
942       DBG("Found applied ACL#%d at applied index %d", acl_index, i);
943       break;
944     }
945   }
946   if (vec_len((*applied_hash_aces)) <= i) {
947     DBG("Did not find applied ACL#%d at lc_index %d", acl_index, lc_index);
948     /* we went all the way without finding any entries. Probably a list was empty. */
949     return;
950   }
951
952   void *oldheap = hash_acl_set_heap(am);
953   int base_offset = i;
954   int tail_offset = base_offset + vec_len(ha->rules);
955   int tail_len = vec_len((*applied_hash_aces)) - tail_offset;
956   DBG("base_offset: %d, tail_offset: %d, tail_len: %d", base_offset, tail_offset, tail_len);
957
958   for(i=0; i < vec_len(ha->rules); i ++) {
959     deactivate_applied_ace_hash_entry(am, lc_index,
960                                       applied_hash_aces, base_offset + i);
961   }
962   for(i=0; i < tail_len; i ++) {
963     /* move the entry at tail offset to base offset */
964     /* that is, from (tail_offset+i) -> (base_offset+i) */
965     DBG0("UNAPPLY MOVE: lc_index %d, applied index %d -> %d", lc_index, tail_offset+i, base_offset + i);
966     move_applied_ace_hash_entry(am, lc_index, applied_hash_aces, tail_offset + i, base_offset + i);
967   }
968   /* trim the end of the vector */
969   _vec_len((*applied_hash_aces)) -= vec_len(ha->rules);
970
971   remake_hash_applied_mask_info_vec(am, applied_hash_aces, lc_index);
972
973   if (vec_len((*applied_hash_aces)) == 0) {
974     vec_free((*applied_hash_aces));
975   }
976
977   clib_mem_set_heap (oldheap);
978 }
979
980 /*
981  * Create the applied ACEs and update the hash table,
982  * taking into account that the ACL may not be the last
983  * in the vector of applied ACLs.
984  *
985  * For now, walk from the end of the vector and unapply the ACLs,
986  * then apply the one in question and reapply the rest.
987  */
988
989 void
990 hash_acl_reapply(acl_main_t *am, u32 lc_index, int acl_index)
991 {
992   acl_lookup_context_t *acontext = pool_elt_at_index(am->acl_lookup_contexts, lc_index);
993   u32 **applied_acls = &acontext->acl_indices;
994   int i;
995   int start_index = vec_search((*applied_acls), acl_index);
996
997   DBG0("Start index for acl %d in lc_index %d is %d", acl_index, lc_index, start_index);
998   /*
999    * This function is called after we find out the lc_index where ACL is applied.
1000    * If the by-lc_index vector does not have the ACL#, then it's a bug.
1001    */
1002   ASSERT(start_index < vec_len(*applied_acls));
1003
1004   /* unapply all the ACLs at the tail side, up to the current one */
1005   for(i = vec_len(*applied_acls) - 1; i > start_index; i--) {
1006     hash_acl_unapply(am, lc_index, *vec_elt_at_index(*applied_acls, i));
1007   }
1008   for(i = start_index; i < vec_len(*applied_acls); i++) {
1009     hash_acl_apply(am, lc_index, *vec_elt_at_index(*applied_acls, i), i);
1010   }
1011 }
1012
1013 static void
1014 make_ip6_address_mask(ip6_address_t *addr, u8 prefix_len)
1015 {
1016   ip6_address_mask_from_width(addr, prefix_len);
1017 }
1018
1019
1020 /* Maybe should be moved into the core somewhere */
1021 always_inline void
1022 ip4_address_mask_from_width (ip4_address_t * a, u32 width)
1023 {
1024   int i, byte, bit, bitnum;
1025   ASSERT (width <= 32);
1026   clib_memset (a, 0, sizeof (a[0]));
1027   for (i = 0; i < width; i++)
1028     {
1029       bitnum = (7 - (i & 7));
1030       byte = i / 8;
1031       bit = 1 << bitnum;
1032       a->as_u8[byte] |= bit;
1033     }
1034 }
1035
1036
1037 static void
1038 make_ip4_address_mask(ip4_address_t *addr, u8 prefix_len)
1039 {
1040   ip4_address_mask_from_width(addr, prefix_len);
1041 }
1042
1043 static void
1044 make_port_mask(u16 *portmask, u16 port_first, u16 port_last)
1045 {
1046   if (port_first == port_last) {
1047     *portmask = 0xffff;
1048     /* single port is representable by masked value */
1049     return;
1050   }
1051
1052   *portmask = 0;
1053   return;
1054 }
1055
1056 static void
1057 make_mask_and_match_from_rule(fa_5tuple_t *mask, acl_rule_t *r, hash_ace_info_t *hi)
1058 {
1059   clib_memset(mask, 0, sizeof(*mask));
1060   clib_memset(&hi->match, 0, sizeof(hi->match));
1061   hi->action = r->is_permit;
1062
1063   /* we will need to be matching based on lc_index and mask_type_index when applied */
1064   mask->pkt.lc_index = ~0;
1065   /* we will assign the match of mask_type_index later when we find it*/
1066   mask->pkt.mask_type_index_lsb = ~0;
1067
1068   mask->pkt.is_ip6 = 1;
1069   hi->match.pkt.is_ip6 = r->is_ipv6;
1070   if (r->is_ipv6) {
1071     make_ip6_address_mask(&mask->ip6_addr[0], r->src_prefixlen);
1072     hi->match.ip6_addr[0] = r->src.ip6;
1073     make_ip6_address_mask(&mask->ip6_addr[1], r->dst_prefixlen);
1074     hi->match.ip6_addr[1] = r->dst.ip6;
1075   } else {
1076     clib_memset(hi->match.l3_zero_pad, 0, sizeof(hi->match.l3_zero_pad));
1077     make_ip4_address_mask(&mask->ip4_addr[0], r->src_prefixlen);
1078     hi->match.ip4_addr[0] = r->src.ip4;
1079     make_ip4_address_mask(&mask->ip4_addr[1], r->dst_prefixlen);
1080     hi->match.ip4_addr[1] = r->dst.ip4;
1081   }
1082
1083   if (r->proto != 0) {
1084     mask->l4.proto = ~0; /* L4 proto needs to be matched */
1085     hi->match.l4.proto = r->proto;
1086
1087     /* Calculate the src/dst port masks and make the src/dst port matches accordingly */
1088     make_port_mask(&mask->l4.port[0], r->src_port_or_type_first, r->src_port_or_type_last);
1089     hi->match.l4.port[0] = r->src_port_or_type_first & mask->l4.port[0];
1090
1091     make_port_mask(&mask->l4.port[1], r->dst_port_or_code_first, r->dst_port_or_code_last);
1092     hi->match.l4.port[1] = r->dst_port_or_code_first & mask->l4.port[1];
1093     /* L4 info must be valid in order to match */
1094     mask->pkt.l4_valid = 1;
1095     hi->match.pkt.l4_valid = 1;
1096     /* And we must set the mask to check that it is an initial fragment */
1097     mask->pkt.is_nonfirst_fragment = 1;
1098     hi->match.pkt.is_nonfirst_fragment = 0;
1099     if ((r->proto == IPPROTO_TCP) && (r->tcp_flags_mask != 0)) {
1100       /* if we want to match on TCP flags, they must be masked off as well */
1101       mask->pkt.tcp_flags = r->tcp_flags_mask;
1102       hi->match.pkt.tcp_flags = r->tcp_flags_value;
1103       /* and the flags need to be present within the packet being matched */
1104       mask->pkt.tcp_flags_valid = 1;
1105       hi->match.pkt.tcp_flags_valid = 1;
1106     }
1107   }
1108   /* Sanitize the mask and the match */
1109   u64 *pmask = (u64 *)mask;
1110   u64 *pmatch = (u64 *)&hi->match;
1111   int j;
1112   for(j=0; j<6; j++) {
1113     pmatch[j] = pmatch[j] & pmask[j];
1114   }
1115 }
1116
1117
1118 int hash_acl_exists(acl_main_t *am, int acl_index)
1119 {
1120   if (acl_index >= vec_len(am->hash_acl_infos))
1121     return 0;
1122
1123   hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
1124   return ha->hash_acl_exists;
1125 }
1126
1127 void hash_acl_add(acl_main_t *am, int acl_index)
1128 {
1129   void *oldheap = hash_acl_set_heap(am);
1130   DBG("HASH ACL add : %d", acl_index);
1131   int i;
1132   acl_rule_t *acl_rules = am->acls[acl_index].rules;
1133   vec_validate(am->hash_acl_infos, acl_index);
1134   hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
1135   clib_memset(ha, 0, sizeof(*ha));
1136   ha->hash_acl_exists = 1;
1137
1138   /* walk the newly added ACL entries and ensure that for each of them there
1139      is a mask type, increment a reference count for that mask type */
1140
1141   /* avoid small requests by preallocating the entire vector before running the additions */
1142   if (vec_len(acl_rules) > 0) {
1143     vec_validate(ha->rules, vec_len(acl_rules)-1);
1144     vec_reset_length(ha->rules);
1145   }
1146
1147   for(i=0; i < vec_len(acl_rules); i++) {
1148     hash_ace_info_t ace_info;
1149     fa_5tuple_t mask;
1150     clib_memset(&ace_info, 0, sizeof(ace_info));
1151     ace_info.acl_index = acl_index;
1152     ace_info.ace_index = i;
1153
1154     make_mask_and_match_from_rule(&mask, &acl_rules[i], &ace_info);
1155     mask.pkt.flags_reserved = 0b000;
1156     ace_info.base_mask_type_index = assign_mask_type_index(am, &mask);
1157     /* assign the mask type index for matching itself */
1158     ace_info.match.pkt.mask_type_index_lsb = ace_info.base_mask_type_index;
1159     DBG("ACE: %d mask_type_index: %d", i, ace_info.base_mask_type_index);
1160     vec_add1(ha->rules, ace_info);
1161   }
1162   /*
1163    * if an ACL is applied somewhere, fill the corresponding lookup data structures.
1164    * We need to take care if the ACL is not the last one in the vector of ACLs applied to the interface.
1165    */
1166   if (acl_index < vec_len(am->lc_index_vec_by_acl)) {
1167     u32 *lc_index;
1168     vec_foreach(lc_index, am->lc_index_vec_by_acl[acl_index]) {
1169       hash_acl_reapply(am, *lc_index, acl_index);
1170     }
1171   }
1172   clib_mem_set_heap (oldheap);
1173 }
1174
1175 void hash_acl_delete(acl_main_t *am, int acl_index)
1176 {
1177   void *oldheap = hash_acl_set_heap(am);
1178   DBG0("HASH ACL delete : %d", acl_index);
1179   /*
1180    * If the ACL is applied somewhere, remove the references of it (call hash_acl_unapply)
1181    * this is a different behavior from the linear lookup where an empty ACL is "deny all",
1182    *
1183    * However, following vpp-dev discussion the ACL that is referenced elsewhere
1184    * should not be possible to delete, and the change adding this also adds
1185    * the safeguards to that respect, so this is not a problem.
1186    *
1187    * The part to remember is that this routine is called in process of reapplication
1188    * during the acl_add_replace() API call - the old acl ruleset is deleted, then
1189    * the new one is added, without the change in the applied ACLs - so this case
1190    * has to be handled.
1191    */
1192   hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
1193   u32 *lc_list_copy = 0;
1194   {
1195     u32 *lc_index;
1196     lc_list_copy = vec_dup(ha->lc_index_list);
1197     vec_foreach(lc_index, lc_list_copy) {
1198       hash_acl_unapply(am, *lc_index, acl_index);
1199     }
1200     vec_free(lc_list_copy);
1201   }
1202   vec_free(ha->lc_index_list);
1203
1204   /* walk the mask types for the ACL about-to-be-deleted, and decrease
1205    * the reference count, possibly freeing up some of them */
1206   int i;
1207   for(i=0; i < vec_len(ha->rules); i++) {
1208     release_mask_type_index(am, ha->rules[i].base_mask_type_index);
1209   }
1210   ha->hash_acl_exists = 0;
1211   vec_free(ha->rules);
1212   clib_mem_set_heap (oldheap);
1213 }
1214
1215
1216 void
1217 show_hash_acl_hash (vlib_main_t * vm, acl_main_t *am, u32 verbose)
1218 {
1219   vlib_cli_output(vm, "\nACL lookup hash table:\n%U\n",
1220                   BV (format_bihash), &am->acl_lookup_hash, verbose);
1221 }
1222
1223 void
1224 acl_plugin_show_tables_mask_type (void)
1225 {
1226   acl_main_t *am = &acl_main;
1227   vlib_main_t *vm = am->vlib_main;
1228   ace_mask_type_entry_t *mte;
1229
1230   vlib_cli_output (vm, "Mask-type entries:");
1231     /* *INDENT-OFF* */
1232     pool_foreach(mte, am->ace_mask_type_pool,
1233     ({
1234       vlib_cli_output(vm, "     %3d: %016llx %016llx %016llx %016llx %016llx %016llx  refcount %d",
1235                     mte - am->ace_mask_type_pool,
1236                     mte->mask.kv_40_8.key[0], mte->mask.kv_40_8.key[1], mte->mask.kv_40_8.key[2],
1237                     mte->mask.kv_40_8.key[3], mte->mask.kv_40_8.key[4], mte->mask.kv_40_8.value, mte->refcount);
1238     }));
1239     /* *INDENT-ON* */
1240 }
1241
1242 void
1243 acl_plugin_show_tables_acl_hash_info (u32 acl_index)
1244 {
1245   acl_main_t *am = &acl_main;
1246   vlib_main_t *vm = am->vlib_main;
1247   u32 i, j;
1248   u64 *m;
1249   vlib_cli_output (vm, "Mask-ready ACL representations\n");
1250   for (i = 0; i < vec_len (am->hash_acl_infos); i++)
1251     {
1252       if ((acl_index != ~0) && (acl_index != i))
1253         {
1254           continue;
1255         }
1256       hash_acl_info_t *ha = &am->hash_acl_infos[i];
1257       vlib_cli_output (vm, "acl-index %u bitmask-ready layout\n", i);
1258       vlib_cli_output (vm, "  applied lc_index list: %U\n",
1259                        format_vec32, ha->lc_index_list, "%d");
1260       for (j = 0; j < vec_len (ha->rules); j++)
1261         {
1262           hash_ace_info_t *pa = &ha->rules[j];
1263           m = (u64 *) & pa->match;
1264           vlib_cli_output (vm,
1265                            "    %4d: %016llx %016llx %016llx %016llx %016llx %016llx base mask index %d acl %d rule %d action %d\n",
1266                            j, m[0], m[1], m[2], m[3], m[4], m[5],
1267                            pa->base_mask_type_index, pa->acl_index, pa->ace_index,
1268                            pa->action);
1269         }
1270     }
1271 }
1272
1273 static void
1274 acl_plugin_print_colliding_rule (vlib_main_t * vm, int j, collision_match_rule_t *cr) {
1275   vlib_cli_output(vm,
1276                   "        %4d: acl %d ace %d acl pos %d pae index: %d",
1277                   j, cr->acl_index, cr->ace_index, cr->acl_position, cr->applied_entry_index);
1278 }
1279
1280 static void
1281 acl_plugin_print_pae (vlib_main_t * vm, int j, applied_hash_ace_entry_t * pae)
1282 {
1283   vlib_cli_output (vm,
1284                    "    %4d: acl %d rule %d action %d bitmask-ready rule %d mask type index: %d colliding_rules: %d collision_head_ae_idx %d hitcount %lld acl_pos: %d",
1285                    j, pae->acl_index, pae->ace_index, pae->action,
1286                    pae->hash_ace_info_index, pae->mask_type_index, vec_len(pae->colliding_rules), pae->collision_head_ae_index,
1287                    pae->hitcount, pae->acl_position);
1288   int jj;
1289   for(jj=0; jj<vec_len(pae->colliding_rules); jj++)
1290     acl_plugin_print_colliding_rule(vm, jj, vec_elt_at_index(pae->colliding_rules, jj));
1291 }
1292
1293 static void
1294 acl_plugin_print_applied_mask_info (vlib_main_t * vm, int j, hash_applied_mask_info_t *mi)
1295 {
1296   vlib_cli_output (vm,
1297                    "    %4d: mask type index %d first rule index %d num_entries %d max_collisions %d",
1298                    j, mi->mask_type_index, mi->first_rule_index, mi->num_entries, mi->max_collisions);
1299 }
1300
1301 void
1302 acl_plugin_show_tables_applied_info (u32 lc_index)
1303 {
1304   acl_main_t *am = &acl_main;
1305   vlib_main_t *vm = am->vlib_main;
1306   u32 lci, j;
1307   vlib_cli_output (vm, "Applied lookup entries for lookup contexts");
1308
1309   for (lci = 0;
1310        (lci < vec_len(am->applied_hash_acl_info_by_lc_index)); lci++)
1311     {
1312       if ((lc_index != ~0) && (lc_index != lci))
1313         {
1314           continue;
1315         }
1316       vlib_cli_output (vm, "lc_index %d:", lci);
1317       if (lci < vec_len (am->applied_hash_acl_info_by_lc_index))
1318         {
1319           applied_hash_acl_info_t *pal =
1320             &am->applied_hash_acl_info_by_lc_index[lci];
1321           vlib_cli_output (vm, "  applied acls: %U", format_vec32,
1322                            pal->applied_acls, "%d");
1323         }
1324       if (lci < vec_len (am->hash_applied_mask_info_vec_by_lc_index))
1325         {
1326           vlib_cli_output (vm, "  applied mask info entries:");
1327           for (j = 0;
1328                j < vec_len (am->hash_applied_mask_info_vec_by_lc_index[lci]);
1329                j++)
1330             {
1331               acl_plugin_print_applied_mask_info (vm, j,
1332                                     &am->hash_applied_mask_info_vec_by_lc_index
1333                                     [lci][j]);
1334             }
1335         }
1336       if (lci < vec_len (am->hash_entry_vec_by_lc_index))
1337         {
1338           vlib_cli_output (vm, "  lookup applied entries:");
1339           for (j = 0;
1340                j < vec_len (am->hash_entry_vec_by_lc_index[lci]);
1341                j++)
1342             {
1343               acl_plugin_print_pae (vm, j,
1344                                     &am->hash_entry_vec_by_lc_index
1345                                     [lci][j]);
1346             }
1347         }
1348     }
1349 }
1350
1351 void
1352 acl_plugin_show_tables_bihash (u32 show_bihash_verbose)
1353 {
1354   acl_main_t *am = &acl_main;
1355   vlib_main_t *vm = am->vlib_main;
1356   show_hash_acl_hash (vm, am, show_bihash_verbose);
1357 }
1358
1359 /*
1360  * Split of the partition needs to happen when the collision count
1361  * goes over a specified threshold.
1362  *
1363  * This is a signal that we ignored too many bits in
1364  * mT and we need to split the table into two tables. We select
1365  * all of the colliding rules L and find their maximum common
1366  * tuple mL. Normally mL is specific enough to hash L with few
1367  * or no collisions. We then create a new table T2 with tuple mL
1368  * and transfer all compatible rules from T to T2. If mL is not
1369  * specific enough, we find the field with the biggest difference
1370  * between the minimum and maximum tuple lengths for all of
1371  * the rules in L and set that field to be the average of those two
1372  * values. We then transfer all compatible rules as before. This
1373  * guarantees that some rules from L will move and that T2 will
1374  * have a smaller number of collisions than T did.
1375  */
1376
1377
1378 static void
1379 ensure_ip6_min_addr (ip6_address_t * min_addr, ip6_address_t * mask_addr)
1380 {
1381   int update =
1382     (clib_net_to_host_u64 (mask_addr->as_u64[0]) <
1383      clib_net_to_host_u64 (min_addr->as_u64[0]))
1384     ||
1385     ((clib_net_to_host_u64 (mask_addr->as_u64[0]) ==
1386       clib_net_to_host_u64 (min_addr->as_u64[0]))
1387      && (clib_net_to_host_u64 (mask_addr->as_u64[1]) <
1388          clib_net_to_host_u64 (min_addr->as_u64[1])));
1389   if (update)
1390     {
1391       min_addr->as_u64[0] = mask_addr->as_u64[0];
1392       min_addr->as_u64[1] = mask_addr->as_u64[1];
1393     }
1394 }
1395
1396 static void
1397 ensure_ip6_max_addr (ip6_address_t * max_addr, ip6_address_t * mask_addr)
1398 {
1399   int update =
1400     (clib_net_to_host_u64 (mask_addr->as_u64[0]) >
1401      clib_net_to_host_u64 (max_addr->as_u64[0]))
1402     ||
1403     ((clib_net_to_host_u64 (mask_addr->as_u64[0]) ==
1404       clib_net_to_host_u64 (max_addr->as_u64[0]))
1405      && (clib_net_to_host_u64 (mask_addr->as_u64[1]) >
1406          clib_net_to_host_u64 (max_addr->as_u64[1])));
1407   if (update)
1408     {
1409       max_addr->as_u64[0] = mask_addr->as_u64[0];
1410       max_addr->as_u64[1] = mask_addr->as_u64[1];
1411     }
1412 }
1413
1414 static void
1415 ensure_ip4_min_addr (ip4_address_t * min_addr, ip4_address_t * mask_addr)
1416 {
1417   int update =
1418     (clib_net_to_host_u32 (mask_addr->as_u32) <
1419      clib_net_to_host_u32 (min_addr->as_u32));
1420   if (update)
1421     min_addr->as_u32 = mask_addr->as_u32;
1422 }
1423
1424 static void
1425 ensure_ip4_max_addr (ip4_address_t * max_addr, ip4_address_t * mask_addr)
1426 {
1427   int update =
1428     (clib_net_to_host_u32 (mask_addr->as_u32) >
1429      clib_net_to_host_u32 (max_addr->as_u32));
1430   if (update)
1431     max_addr->as_u32 = mask_addr->as_u32;
1432 }
1433
1434 enum {
1435   DIM_SRC_ADDR = 0,
1436   DIM_DST_ADDR,
1437   DIM_SRC_PORT,
1438   DIM_DST_PORT,
1439   DIM_PROTO,
1440 };
1441
1442
1443
1444 static void
1445 split_partition(acl_main_t *am, u32 first_index,
1446                             u32 lc_index, int is_ip6){
1447         DBG( "TM-split_partition - first_entry:%d", first_index);
1448         applied_hash_ace_entry_t **applied_hash_aces = get_applied_hash_aces(am, lc_index);
1449         ace_mask_type_entry_t *mte;
1450         fa_5tuple_t the_min_tuple, *min_tuple = &the_min_tuple;
1451         fa_5tuple_t the_max_tuple, *max_tuple = &the_max_tuple;
1452         applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), first_index);
1453         hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, pae->acl_index);
1454         hash_ace_info_t *ace_info;
1455         u32 coll_mask_type_index = pae->mask_type_index;
1456         clib_memset(&the_min_tuple, 0, sizeof(the_min_tuple));
1457         clib_memset(&the_max_tuple, 0, sizeof(the_max_tuple));
1458
1459         int i=0;
1460         collision_match_rule_t *colliding_rules = pae->colliding_rules;
1461         u64 collisions = vec_len(pae->colliding_rules);
1462         for(i=0; i<collisions; i++){
1463                 /* reload the hash acl info as it might be a different ACL# */
1464                 pae = vec_elt_at_index((*applied_hash_aces), colliding_rules[i].applied_entry_index);
1465                 ha = vec_elt_at_index(am->hash_acl_infos, pae->acl_index);
1466
1467                 DBG( "TM-collision: base_ace:%d (ace_mask:%d, first_collision_mask:%d)",
1468                                 pae->ace_index, pae->mask_type_index, coll_mask_type_index);
1469
1470                 ace_info = vec_elt_at_index(ha->rules, pae->hash_ace_info_index);
1471                 mte = vec_elt_at_index(am->ace_mask_type_pool, ace_info->base_mask_type_index);
1472                 fa_5tuple_t *mask = &mte->mask;
1473
1474                 if(pae->mask_type_index != coll_mask_type_index) continue;
1475                 /* Computing min_mask and max_mask for colliding rules */
1476                 if(i==0){
1477                         clib_memcpy_fast(min_tuple, mask, sizeof(fa_5tuple_t));
1478                         clib_memcpy_fast(max_tuple, mask, sizeof(fa_5tuple_t));
1479                 }else{
1480                         int j;
1481                         for(j=0; j<2; j++){
1482                                 if (is_ip6)
1483                                   ensure_ip6_min_addr(&min_tuple->ip6_addr[j], &mask->ip6_addr[j]);
1484                                 else
1485                                   ensure_ip4_min_addr(&min_tuple->ip4_addr[j], &mask->ip4_addr[j]);
1486
1487                                 if ((mask->l4.port[j] < min_tuple->l4.port[j]))
1488                                         min_tuple->l4.port[j] = mask->l4.port[j];
1489                         }
1490
1491                         if ((mask->l4.proto < min_tuple->l4.proto))
1492                                 min_tuple->l4.proto = mask->l4.proto;
1493
1494                         if(mask->pkt.as_u64 < min_tuple->pkt.as_u64)
1495                                 min_tuple->pkt.as_u64 = mask->pkt.as_u64;
1496
1497
1498                         for(j=0; j<2; j++){
1499                                 if (is_ip6)
1500                                   ensure_ip6_max_addr(&max_tuple->ip6_addr[j], &mask->ip6_addr[j]);
1501                                 else
1502                                   ensure_ip4_max_addr(&max_tuple->ip4_addr[j], &mask->ip4_addr[j]);
1503
1504                                 if ((mask->l4.port[j] > max_tuple->l4.port[j]))
1505                                         max_tuple->l4.port[j] = mask->l4.port[j];
1506                         }
1507
1508                         if ((mask->l4.proto < max_tuple->l4.proto))
1509                                 max_tuple->l4.proto = mask->l4.proto;
1510
1511                         if(mask->pkt.as_u64 > max_tuple->pkt.as_u64)
1512                                 max_tuple->pkt.as_u64 = mask->pkt.as_u64;
1513                 }
1514         }
1515
1516         /* Computing field with max difference between (min/max)_mask */
1517         int best_dim=-1, best_delta=0, delta=0;
1518
1519         /* SRC_addr dimension */
1520         if (is_ip6) {
1521           int i;
1522           for(i=0; i<2; i++){
1523                 delta += count_bits(max_tuple->ip6_addr[0].as_u64[i]) - count_bits(min_tuple->ip6_addr[0].as_u64[i]);
1524           }
1525         } else {
1526                 delta += count_bits(max_tuple->ip4_addr[0].as_u32) - count_bits(min_tuple->ip4_addr[0].as_u32);
1527         }
1528         if(delta > best_delta){
1529                 best_delta = delta;
1530                 best_dim = DIM_SRC_ADDR;
1531         }
1532
1533         /* DST_addr dimension */
1534         delta = 0;
1535         if (is_ip6) {
1536           int i;
1537           for(i=0; i<2; i++){
1538                 delta += count_bits(max_tuple->ip6_addr[1].as_u64[i]) - count_bits(min_tuple->ip6_addr[1].as_u64[i]);
1539           }
1540         } else {
1541                 delta += count_bits(max_tuple->ip4_addr[1].as_u32) - count_bits(min_tuple->ip4_addr[1].as_u32);
1542         }
1543         if(delta > best_delta){
1544                 best_delta = delta;
1545                 best_dim = DIM_DST_ADDR;
1546         }
1547
1548         /* SRC_port dimension */
1549         delta = count_bits(max_tuple->l4.port[0]) - count_bits(min_tuple->l4.port[0]);
1550         if(delta > best_delta){
1551                 best_delta = delta;
1552                 best_dim = DIM_SRC_PORT;
1553         }
1554
1555         /* DST_port dimension */
1556         delta = count_bits(max_tuple->l4.port[1]) - count_bits(min_tuple->l4.port[1]);
1557         if(delta > best_delta){
1558                 best_delta = delta;
1559                 best_dim = DIM_DST_PORT;
1560         }
1561
1562         /* Proto dimension */
1563         delta = count_bits(max_tuple->l4.proto) - count_bits(min_tuple->l4.proto);
1564         if(delta > best_delta){
1565                 best_delta = delta;
1566                 best_dim = DIM_PROTO;
1567         }
1568
1569         int shifting = 0; //, ipv4_block = 0;
1570         switch(best_dim){
1571                 case DIM_SRC_ADDR:
1572                         shifting = (best_delta)/2; // FIXME IPV4-only
1573                         // ipv4_block = count_bits(max_tuple->ip4_addr[0].as_u32);
1574                         min_tuple->ip4_addr[0].as_u32 =
1575                                         clib_host_to_net_u32((clib_net_to_host_u32(max_tuple->ip4_addr[0].as_u32) << (shifting))&0xFFFFFFFF);
1576
1577                         break;
1578                 case DIM_DST_ADDR:
1579                         shifting = (best_delta)/2;
1580 /*
1581                         ipv4_block = count_bits(max_tuple->addr[1].as_u64[1]);
1582                         if(ipv4_block > shifting)
1583                                 min_tuple->addr[1].as_u64[1] =
1584                                         clib_host_to_net_u64((clib_net_to_host_u64(max_tuple->addr[1].as_u64[1]) << (shifting))&0xFFFFFFFF);
1585                         else{
1586                                 shifting = shifting - ipv4_block;
1587                                 min_tuple->addr[1].as_u64[1] = 0;
1588                                 min_tuple->addr[1].as_u64[0] =
1589                                         clib_host_to_net_u64((clib_net_to_host_u64(max_tuple->addr[1].as_u64[0]) << (shifting))&0xFFFFFFFF);
1590                         }
1591 */
1592                         min_tuple->ip4_addr[1].as_u32 =
1593                                         clib_host_to_net_u32((clib_net_to_host_u32(max_tuple->ip4_addr[1].as_u32) << (shifting))&0xFFFFFFFF);
1594
1595                         break;
1596                 case DIM_SRC_PORT: min_tuple->l4.port[0] = max_tuple->l4.port[0]  << (best_delta)/2;
1597                         break;
1598                 case DIM_DST_PORT: min_tuple->l4.port[1] = max_tuple->l4.port[1] << (best_delta)/2;
1599                         break;
1600                 case DIM_PROTO: min_tuple->l4.proto = max_tuple->l4.proto << (best_delta)/2;
1601                         break;
1602                 default: relax_tuple(min_tuple, is_ip6, 1);
1603                         break;
1604         }
1605
1606         min_tuple->pkt.is_nonfirst_fragment = 0;
1607         u32 new_mask_type_index = assign_mask_type_index(am, min_tuple);
1608
1609         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);
1610
1611         hash_applied_mask_info_t *minfo;
1612         //search in order pool if mask_type_index is already there
1613         int search;
1614         for (search=0; search < vec_len((*hash_applied_mask_info_vec)); search++){
1615                 minfo = vec_elt_at_index((*hash_applied_mask_info_vec), search);
1616                 if(minfo->mask_type_index == new_mask_type_index)
1617                         break;
1618         }
1619
1620         vec_validate((*hash_applied_mask_info_vec), search);
1621         minfo = vec_elt_at_index((*hash_applied_mask_info_vec), search);
1622         minfo->mask_type_index = new_mask_type_index;
1623         minfo->num_entries = 0;
1624         minfo->max_collisions = 0;
1625         minfo->first_rule_index = ~0;
1626
1627         DBG( "TM-split_partition - mask type index-assigned!! -> %d", new_mask_type_index);
1628
1629         if(coll_mask_type_index == new_mask_type_index){
1630                 //vlib_cli_output(vm, "TM-There are collisions over threshold, but i'm not able to split! %d %d", coll_mask_type_index, new_mask_type_index);
1631                 return;
1632         }
1633
1634
1635         /* populate new partition */
1636         DBG( "TM-Populate new partition");
1637         u32 r_ace_index = first_index;
1638         int repopulate_count = 0;
1639
1640         collision_match_rule_t *temp_colliding_rules = vec_dup(colliding_rules);
1641         collisions = vec_len(temp_colliding_rules);
1642
1643         for(i=0; i<collisions; i++){
1644
1645                 r_ace_index = temp_colliding_rules[i].applied_entry_index;
1646
1647                 applied_hash_ace_entry_t *pop_pae = vec_elt_at_index((*applied_hash_aces), r_ace_index);
1648                 ha = vec_elt_at_index(am->hash_acl_infos, pop_pae->acl_index);
1649                 DBG( "TM-Population-collision: base_ace:%d (ace_mask:%d, first_collision_mask:%d)",
1650                                 pop_pae->ace_index, pop_pae->mask_type_index, coll_mask_type_index);
1651
1652                 ASSERT(pop_pae->mask_type_index == coll_mask_type_index);
1653
1654                 ace_info = vec_elt_at_index(ha->rules, pop_pae->hash_ace_info_index);
1655                 mte = vec_elt_at_index(am->ace_mask_type_pool, ace_info->base_mask_type_index);
1656                 //can insert rule?
1657                 //mte = vec_elt_at_index(am->ace_mask_type_pool, pop_pae->mask_type_index);
1658                 fa_5tuple_t *pop_mask = &mte->mask;
1659
1660                 if(!first_mask_contains_second_mask(is_ip6, min_tuple, pop_mask)) continue;
1661                 DBG( "TM-new partition can insert -> applied_ace:%d", r_ace_index);
1662
1663                 //delete and insert in new format
1664                 deactivate_applied_ace_hash_entry(am, lc_index, applied_hash_aces, r_ace_index);
1665
1666                 /* insert the new entry */
1667                 pop_pae->mask_type_index = new_mask_type_index;
1668                 /* The very first repopulation gets the lock by virtue of a new mask being created above */
1669                 if (++repopulate_count > 1)
1670                   lock_mask_type_index(am, new_mask_type_index);
1671
1672                 activate_applied_ace_hash_entry(am, lc_index, applied_hash_aces, r_ace_index);
1673
1674         }
1675         vec_free(temp_colliding_rules);
1676
1677         DBG( "TM-Populate new partition-END");
1678         DBG( "TM-split_partition - END");
1679
1680 }
1681