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