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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
19 #include <netinet/in.h>
21 #include <vlibapi/api.h>
22 #include <vlibmemory/api.h>
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>
30 #include <vppinfra/bihash_48_8.h>
32 #include "hash_lookup.h"
33 #include "hash_lookup_private.h"
36 always_inline applied_hash_ace_entry_t **get_applied_hash_aces(acl_main_t *am, u32 lc_index)
38 applied_hash_ace_entry_t **applied_hash_aces = vec_elt_at_index(am->hash_entry_vec_by_lc_index, lc_index);
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);
43 return applied_hash_aces;
48 hashtable_add_del(acl_main_t *am, clib_bihash_kv_48_8_t *kv, int is_add)
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);
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.
65 * [1] James Daly, Eric Torng "TupleMerge: Building Online Packet Classifiers
66 * by Omitting Bits", In Proc. IEEE ICCCN 2017, pp. 1-10
82 /* check if mask2 can be contained by mask1 */
84 first_mask_contains_second_mask(int is_ip6, fa_5tuple_t * mask1, fa_5tuple_t * mask2)
89 for (i = 0; i < 2; i++)
91 if ((mask1->ip6_addr[0].as_u64[i] & mask2->ip6_addr[0].as_u64[i]) !=
92 mask1->ip6_addr[0].as_u64[i])
94 if ((mask1->ip6_addr[1].as_u64[i] & mask2->ip6_addr[1].as_u64[i]) !=
95 mask1->ip6_addr[1].as_u64[i])
101 /* check the pads, both masks must have it 0 */
104 for (i=0; i<6; i++) {
105 padcheck |= mask1->l3_zero_pad[i];
106 padcheck |= mask2->l3_zero_pad[i];
110 if ((mask1->ip4_addr[0].as_u32 & mask2->ip4_addr[0].as_u32) !=
111 mask1->ip4_addr[0].as_u32)
113 if ((mask1->ip4_addr[1].as_u32 & mask2->ip4_addr[1].as_u32) !=
114 mask1->ip4_addr[1].as_u32)
118 /* take care if port are not exact-match */
119 if ((mask1->l4.as_u64 & mask2->l4.as_u64) != mask1->l4.as_u64)
122 if ((mask1->pkt.as_u64 & mask2->pkt.as_u64) != mask1->pkt.as_u64)
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.
165 shift_ip4_if(u32 mask, u32 thresh, int numshifts, u32 else_val)
168 return clib_host_to_net_u32((clib_net_to_host_u32(mask) << numshifts) & 0xFFFFFFFF);
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 } };
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);
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)));
187 relax_ip6_addr(ip6_address_t *ip6_mask, int relax2) {
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.
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);
205 relax_tuple(fa_5tuple_t *mask, int is_ip6, int relax2){
206 fa_5tuple_t save_mask = *mask;
208 int counter_s = 0, counter_d = 0;
212 counter_s += count_bits(mask->ip6_addr[0].as_u64[i]);
213 counter_d += count_bits(mask->ip6_addr[1].as_u64[i]);
216 counter_s += count_bits(mask->ip4_addr[0].as_u32);
217 counter_d += count_bits(mask->ip4_addr[1].as_u32);
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
228 const int deltaThreshold = 4;
229 /* const int deltaThreshold = 8; if IPV6? */
230 int delta = counter_s - counter_d;
231 if (-delta > deltaThreshold) {
233 mask->ip6_addr[0].as_u64[1] = mask->ip6_addr[0].as_u64[0] = 0;
235 mask->ip4_addr[0].as_u32 = 0;
236 mask->l4.port[0] = 0;
237 } else if (delta > deltaThreshold) {
239 mask->ip6_addr[1].as_u64[1] = mask->ip6_addr[1].as_u64[0] = 0;
241 mask->ip4_addr[1].as_u32 = 0;
242 mask->l4.port[1] = 0;
246 relax_ip6_addr(&mask->ip6_addr[0], relax2);
247 relax_ip6_addr(&mask->ip6_addr[1], relax2);
249 relax_ip4_addr(&mask->ip4_addr[0], relax2);
250 relax_ip4_addr(&mask->ip4_addr[1], relax2);
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");
258 DBG( "TM-relaxing-end");
262 find_mask_type_index(acl_main_t *am, fa_5tuple_t *mask)
264 ace_mask_type_entry_t *mte;
266 pool_foreach(mte, am->ace_mask_type_pool,
268 if(memcmp(&mte->mask, mask, sizeof(*mask)) == 0)
269 return (mte - am->ace_mask_type_pool);
276 assign_mask_type_index(acl_main_t *am, fa_5tuple_t *mask)
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(&mte->mask, mask, sizeof(mte->mask));
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.
291 ASSERT(mask_type_index < 32768);
293 mte = am->ace_mask_type_pool + mask_type_index;
295 DBG0("ASSIGN MTE index %d new refcount %d", mask_type_index, mte->refcount);
296 return mask_type_index;
300 lock_mask_type_index(acl_main_t *am, u32 mask_type_index)
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);
305 DBG0("LOCK MTE index %d new refcount %d", mask_type_index, mte->refcount);
310 release_mask_type_index(acl_main_t *am, u32 mask_type_index)
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);
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 memset(mte, 0xae, sizeof(*mte));
319 pool_put(am->ace_mask_type_pool, mte);
325 tm_assign_mask_type_index(acl_main_t *am, fa_5tuple_t *mask, int is_ip6, u32 lc_index)
327 u32 mask_type_index = ~0;
328 u32 for_mask_type_index = ~0;
329 ace_mask_type_entry_t *mte = 0;
331 /* look for existing mask comparable with the one in input */
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;
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);
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);
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);
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;
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.
371 ASSERT(mask_type_index < 32768);
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;
380 fill_applied_hash_ace_kv(acl_main_t *am,
381 applied_hash_ace_entry_t **applied_hash_aces,
383 u32 new_index, clib_bihash_kv_48_8_t *kv)
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);
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);
394 u64 *pmatch = (u64 *) &ace_info->match;
395 u64 *pmask = (u64 *)&mte->mask;
396 u64 *pkey = (u64 *)kv->key;
398 *pkey++ = *pmatch++ & *pmask++;
399 *pkey++ = *pmatch++ & *pmask++;
400 *pkey++ = *pmatch++ & *pmask++;
401 *pkey++ = *pmatch++ & *pmask++;
402 *pkey++ = *pmatch++ & *pmask++;
403 *pkey++ = *pmatch++ & *pmask++;
405 kv_key->pkt.mask_type_index_lsb = pae->mask_type_index;
406 kv_key->pkt.lc_index = lc_index;
408 kv_val->applied_entry_index = new_index;
412 add_del_hashtable_entry(acl_main_t *am,
414 applied_hash_ace_entry_t **applied_hash_aces,
415 u32 index, int is_add)
417 clib_bihash_kv_48_8_t kv;
419 fill_applied_hash_ace_kv(am, applied_hash_aces, lc_index, index, &kv);
420 hashtable_add_del(am, &kv, is_add);
425 remake_hash_applied_mask_info_vec (acl_main_t * am,
426 applied_hash_ace_entry_t **
427 applied_hash_aces, u32 lc_index)
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);
433 hash_applied_mask_info_t *minfo;
435 for (i = 0; i < vec_len ((*applied_hash_aces)); i++)
437 applied_hash_ace_entry_t *pae =
438 vec_elt_at_index ((*applied_hash_aces), i);
440 /* check if mask_type_index is already there */
441 u32 new_pointer = vec_len (new_hash_applied_mask_info_vec);
443 for (search = 0; search < vec_len (new_hash_applied_mask_info_vec);
446 minfo = vec_elt_at_index (new_hash_applied_mask_info_vec, search);
447 if (minfo->mask_type_index == pae->mask_type_index)
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)
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;
462 minfo->num_entries = minfo->num_entries + 1;
464 if (vec_len (pae->colliding_rules) > minfo->max_collisions)
465 minfo->max_collisions = vec_len (pae->colliding_rules);
467 if (minfo->first_rule_index > i)
468 minfo->first_rule_index = i;
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);
474 vec_free ((*hash_applied_mask_info_vec));
475 (*hash_applied_mask_info_vec) = new_hash_applied_mask_info_vec;
479 vec_del_collision_rule (collision_match_rule_t ** pvec,
480 u32 applied_entry_index)
484 while (i < _vec_len ((*pvec)))
486 collision_match_rule_t *cr = vec_elt_at_index ((*pvec), i);
487 if (cr->applied_entry_index == applied_entry_index)
489 /* vec_del1 ((*pvec), i) would be more efficient but would reorder the elements. */
490 vec_delete((*pvec), 1, i);
492 DBG0("vec_del_collision_rule deleting one at index %d", i);
503 acl_plugin_print_pae (vlib_main_t * vm, int j, applied_hash_ace_entry_t * pae);
506 del_colliding_rule (applied_hash_ace_entry_t ** applied_hash_aces,
507 u32 head_index, u32 applied_entry_index)
509 DBG0("DEL COLLIDING RULE: head_index %d applied index %d", head_index, applied_entry_index);
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);
520 if (ACL_HASH_LOOKUP_DEBUG > 0)
521 acl_plugin_print_pae(acl_main.vlib_main, head_index, head_pae);
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)
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);
537 collision_match_rule_t cr;
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 vec_add1 (head_pae->colliding_rules, cr);
545 if (ACL_HASH_LOOKUP_DEBUG > 0)
546 acl_plugin_print_pae(acl_main.vlib_main, head_index, head_pae);
550 activate_applied_ace_hash_entry(acl_main_t *am,
552 applied_hash_ace_entry_t **applied_hash_aces,
555 clib_bihash_kv_48_8_t kv;
556 ASSERT(new_index != ~0);
557 applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), new_index);
558 DBG("activate_applied_ace_hash_entry lc_index %d new_index %d", lc_index, new_index);
560 fill_applied_hash_ace_kv(am, applied_hash_aces, lc_index, new_index, &kv);
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]);
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)));
572 /* There already exists an entry or more. Append at the end. */
573 u32 first_index = result_val->applied_entry_index;
574 ASSERT(first_index != ~0);
575 DBG("A key already exists, with applied entry index: %d", first_index);
576 applied_hash_ace_entry_t *first_pae = vec_elt_at_index((*applied_hash_aces), first_index);
577 u32 last_index = first_pae->tail_applied_entry_index;
578 ASSERT(last_index != ~0);
579 applied_hash_ace_entry_t *last_pae = vec_elt_at_index((*applied_hash_aces), last_index);
580 DBG("...advance to chained entry index: %d", last_index);
581 /* link ourselves in */
582 last_pae->next_applied_entry_index = new_index;
583 pae->prev_applied_entry_index = last_index;
584 /* adjust the pointer to the new tail */
585 first_pae->tail_applied_entry_index = new_index;
586 add_colliding_rule(am, applied_hash_aces, first_index, new_index);
589 /* It's the very first entry */
590 hashtable_add_del(am, &kv, 1);
591 ASSERT(new_index != ~0);
592 pae->tail_applied_entry_index = new_index;
593 add_colliding_rule(am, applied_hash_aces, new_index, new_index);
600 hash_acl_set_heap(acl_main_t *am)
602 if (0 == am->hash_lookup_mheap) {
603 am->hash_lookup_mheap = mheap_alloc_with_lock (0 /* use VM */ ,
604 am->hash_lookup_mheap_size,
606 if (0 == am->hash_lookup_mheap) {
607 clib_error("ACL plugin failed to allocate lookup heap of %U bytes",
608 format_memory_size, am->hash_lookup_mheap_size);
611 void *oldheap = clib_mem_set_heap(am->hash_lookup_mheap);
616 acl_plugin_hash_acl_set_validate_heap(int on)
618 acl_main_t *am = &acl_main;
619 clib_mem_set_heap(hash_acl_set_heap(am));
620 #if USE_DLMALLOC == 0
621 mheap_t *h = mheap_header (am->hash_lookup_mheap);
623 h->flags |= MHEAP_FLAG_VALIDATE;
624 h->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
627 h->flags &= ~MHEAP_FLAG_VALIDATE;
628 h->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
634 acl_plugin_hash_acl_set_trace_heap(int on)
636 acl_main_t *am = &acl_main;
637 clib_mem_set_heap(hash_acl_set_heap(am));
638 #if USE_DLMALLOC == 0
639 mheap_t *h = mheap_header (am->hash_lookup_mheap);
641 h->flags |= MHEAP_FLAG_TRACE;
643 h->flags &= ~MHEAP_FLAG_TRACE;
649 assign_mask_type_index_to_pae(acl_main_t *am, u32 lc_index, int is_ip6, applied_hash_ace_entry_t *pae)
651 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, pae->acl_index);
652 hash_ace_info_t *ace_info = vec_elt_at_index(ha->rules, pae->hash_ace_info_index);
654 ace_mask_type_entry_t *mte;
657 * Start taking base_mask associated to ace, and essentially copy it.
658 * With TupleMerge we will assign a relaxed mask here.
660 mte = vec_elt_at_index(am->ace_mask_type_pool, ace_info->base_mask_type_index);
662 if (am->use_tuple_merge)
663 pae->mask_type_index = tm_assign_mask_type_index(am, &mask, is_ip6, lc_index);
665 pae->mask_type_index = assign_mask_type_index(am, &mask);
669 split_partition(acl_main_t *am, u32 first_index,
670 u32 lc_index, int is_ip6);
674 check_collision_count_and_maybe_split(acl_main_t *am, u32 lc_index, int is_ip6, u32 first_index)
676 applied_hash_ace_entry_t **applied_hash_aces = get_applied_hash_aces(am, lc_index);
677 applied_hash_ace_entry_t *first_pae = vec_elt_at_index((*applied_hash_aces), first_index);
678 if (vec_len(first_pae->colliding_rules) > am->tuple_merge_split_threshold) {
679 split_partition(am, first_index, lc_index, is_ip6);
684 hash_acl_apply(acl_main_t *am, u32 lc_index, int acl_index, u32 acl_position)
688 DBG0("HASH ACL apply: lc_index %d acl %d", lc_index, acl_index);
689 if (!am->acl_lookup_hash_initialized) {
690 BV (clib_bihash_init) (&am->acl_lookup_hash, "ACL plugin rule lookup bihash",
691 am->hash_lookup_hash_buckets, am->hash_lookup_hash_memory);
692 am->acl_lookup_hash_initialized = 1;
695 void *oldheap = hash_acl_set_heap(am);
696 vec_validate(am->hash_entry_vec_by_lc_index, lc_index);
697 vec_validate(am->hash_acl_infos, acl_index);
698 applied_hash_ace_entry_t **applied_hash_aces = get_applied_hash_aces(am, lc_index);
700 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
701 u32 **hash_acl_applied_lc_index = &ha->lc_index_list;
703 int base_offset = vec_len(*applied_hash_aces);
705 /* Update the bitmap of the mask types with which the lookup
706 needs to happen for the ACLs applied to this lc_index */
707 applied_hash_acl_info_t **applied_hash_acls = &am->applied_hash_acl_info_by_lc_index;
708 vec_validate((*applied_hash_acls), lc_index);
709 applied_hash_acl_info_t *pal = vec_elt_at_index((*applied_hash_acls), lc_index);
711 /* ensure the list of applied hash acls is initialized and add this acl# to it */
712 u32 index = vec_search(pal->applied_acls, acl_index);
714 clib_warning("BUG: trying to apply twice acl_index %d on lc_index %d, according to lc",
715 acl_index, lc_index);
718 vec_add1(pal->applied_acls, acl_index);
719 u32 index2 = vec_search((*hash_acl_applied_lc_index), lc_index);
721 clib_warning("BUG: trying to apply twice acl_index %d on lc_index %d, according to hash h-acl info",
722 acl_index, lc_index);
725 vec_add1((*hash_acl_applied_lc_index), lc_index);
728 * if the applied ACL is empty, the current code will cause a
729 * different behavior compared to current linear search: an empty ACL will
730 * simply fallthrough to the next ACL, or the default deny in the end.
732 * This is not a problem, because after vpp-dev discussion,
733 * the consensus was it should not be possible to apply the non-existent
734 * ACL, so the change adding this code also takes care of that.
738 vec_validate(am->hash_applied_mask_info_vec_by_lc_index, lc_index);
739 /* add the rules from the ACL to the hash table for lookup and append to the vector*/
740 for(i=0; i < vec_len(ha->rules); i++) {
742 * Expand the applied aces vector to fit a new entry.
743 * One by one not to upset split_partition() if it is called.
745 vec_resize((*applied_hash_aces), 1);
747 int is_ip6 = ha->rules[i].match.pkt.is_ip6;
748 u32 new_index = base_offset + i;
749 applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), new_index);
750 pae->acl_index = acl_index;
751 pae->ace_index = ha->rules[i].ace_index;
752 pae->acl_position = acl_position;
753 pae->action = ha->rules[i].action;
755 pae->hash_ace_info_index = i;
756 /* we might link it in later */
757 pae->next_applied_entry_index = ~0;
758 pae->prev_applied_entry_index = ~0;
759 pae->tail_applied_entry_index = ~0;
760 pae->colliding_rules = NULL;
761 pae->mask_type_index = ~0;
762 assign_mask_type_index_to_pae(am, lc_index, is_ip6, pae);
763 u32 first_index = activate_applied_ace_hash_entry(am, lc_index, applied_hash_aces, new_index);
764 if (am->use_tuple_merge)
765 check_collision_count_and_maybe_split(am, lc_index, is_ip6, first_index);
767 remake_hash_applied_mask_info_vec(am, applied_hash_aces, lc_index);
769 clib_mem_set_heap (oldheap);
773 find_head_applied_ace_index(applied_hash_ace_entry_t **applied_hash_aces, u32 curr_index)
776 * find back the first entry. Inefficient so might need to be a bit cleverer
777 * if this proves to be a problem..
779 u32 an_index = curr_index;
780 ASSERT(an_index != ~0);
781 applied_hash_ace_entry_t *head_pae = vec_elt_at_index((*applied_hash_aces), an_index);
782 while(head_pae->prev_applied_entry_index != ~0) {
783 an_index = head_pae->prev_applied_entry_index;
784 ASSERT(an_index != ~0);
785 head_pae = vec_elt_at_index((*applied_hash_aces), an_index);
791 move_applied_ace_hash_entry(acl_main_t *am,
793 applied_hash_ace_entry_t **applied_hash_aces,
794 u32 old_index, u32 new_index)
796 ASSERT(old_index != ~0);
797 ASSERT(new_index != ~0);
799 *vec_elt_at_index((*applied_hash_aces), new_index) = *vec_elt_at_index((*applied_hash_aces), old_index);
801 /* update the linkage and hash table if necessary */
802 applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), old_index);
803 applied_hash_ace_entry_t *new_pae = vec_elt_at_index((*applied_hash_aces), new_index);
805 if (ACL_HASH_LOOKUP_DEBUG > 0) {
806 clib_warning("Moving pae from %d to %d", old_index, new_index);
807 acl_plugin_print_pae(am->vlib_main, old_index, pae);
810 if (new_pae->tail_applied_entry_index == old_index) {
811 /* fix-up the tail index if we are the tail and the start */
812 new_pae->tail_applied_entry_index = new_index;
815 if (pae->prev_applied_entry_index != ~0) {
816 applied_hash_ace_entry_t *prev_pae = vec_elt_at_index((*applied_hash_aces), pae->prev_applied_entry_index);
817 ASSERT(prev_pae->next_applied_entry_index == old_index);
818 prev_pae->next_applied_entry_index = new_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 ASSERT(pae->tail_applied_entry_index != ~0);
825 if (pae->next_applied_entry_index != ~0) {
826 applied_hash_ace_entry_t *next_pae = vec_elt_at_index((*applied_hash_aces), pae->next_applied_entry_index);
827 ASSERT(next_pae->prev_applied_entry_index == old_index);
828 next_pae->prev_applied_entry_index = new_index;
831 * Moving the very last entry, so we need to update the tail pointer in the first one.
833 u32 head_index = find_head_applied_ace_index(applied_hash_aces, old_index);
834 ASSERT(head_index != ~0);
835 applied_hash_ace_entry_t *head_pae = vec_elt_at_index((*applied_hash_aces), head_index);
837 ASSERT(head_pae->tail_applied_entry_index == old_index);
838 head_pae->tail_applied_entry_index = new_index;
840 if (new_pae->colliding_rules) {
841 /* update the information within the collision rule entry */
842 ASSERT(vec_len(new_pae->colliding_rules) > 0);
843 collision_match_rule_t *cr = vec_elt_at_index (new_pae->colliding_rules, 0);
844 ASSERT(cr->applied_entry_index == old_index);
845 cr->applied_entry_index = new_index;
847 /* find the index in the collision rule entry on the head element */
848 u32 head_index = find_head_applied_ace_index(applied_hash_aces, new_index);
849 ASSERT(head_index != ~0);
850 applied_hash_ace_entry_t *head_pae = vec_elt_at_index((*applied_hash_aces), head_index);
851 ASSERT(vec_len(head_pae->colliding_rules) > 0);
853 for (i=0; i<vec_len(head_pae->colliding_rules); i++) {
854 collision_match_rule_t *cr = vec_elt_at_index (head_pae->colliding_rules, i);
855 if (cr->applied_entry_index == old_index) {
856 cr->applied_entry_index = new_index;
859 if (ACL_HASH_LOOKUP_DEBUG > 0) {
860 clib_warning("Head pae at index %d after adjustment", head_index);
861 acl_plugin_print_pae(am->vlib_main, head_index, head_pae);
864 /* invalidate the old entry */
865 pae->prev_applied_entry_index = ~0;
866 pae->next_applied_entry_index = ~0;
867 pae->tail_applied_entry_index = ~0;
868 pae->colliding_rules = NULL;
872 deactivate_applied_ace_hash_entry(acl_main_t *am,
874 applied_hash_ace_entry_t **applied_hash_aces,
877 applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), old_index);
878 DBG("UNAPPLY DEACTIVATE: lc_index %d applied index %d", lc_index, old_index);
879 if (ACL_HASH_LOOKUP_DEBUG > 0) {
880 clib_warning("Deactivating pae at index %d", old_index);
881 acl_plugin_print_pae(am->vlib_main, old_index, pae);
884 if (pae->prev_applied_entry_index != ~0) {
885 DBG("UNAPPLY = index %d has prev_applied_entry_index %d", old_index, pae->prev_applied_entry_index);
886 applied_hash_ace_entry_t *prev_pae = vec_elt_at_index((*applied_hash_aces), pae->prev_applied_entry_index);
887 ASSERT(prev_pae->next_applied_entry_index == old_index);
888 prev_pae->next_applied_entry_index = pae->next_applied_entry_index;
890 u32 head_index = find_head_applied_ace_index(applied_hash_aces, old_index);
891 ASSERT(head_index != ~0);
892 applied_hash_ace_entry_t *head_pae = vec_elt_at_index((*applied_hash_aces), head_index);
893 del_colliding_rule(applied_hash_aces, head_index, old_index);
895 if (pae->next_applied_entry_index == ~0) {
896 /* it was a last entry we removed, update the pointer on the first one */
897 ASSERT(head_pae->tail_applied_entry_index == old_index);
898 head_pae->tail_applied_entry_index = pae->prev_applied_entry_index;
900 applied_hash_ace_entry_t *next_pae = vec_elt_at_index((*applied_hash_aces), pae->next_applied_entry_index);
901 next_pae->prev_applied_entry_index = pae->prev_applied_entry_index;
904 /* It was the first entry. We need either to reset the hash entry or delete it */
905 /* delete our entry from the collision vector first */
906 del_colliding_rule(applied_hash_aces, old_index, old_index);
907 if (pae->next_applied_entry_index != ~0) {
908 /* the next element becomes the new first one, so needs the tail pointer to be set */
909 applied_hash_ace_entry_t *next_pae = vec_elt_at_index((*applied_hash_aces), pae->next_applied_entry_index);
910 ASSERT(pae->tail_applied_entry_index != ~0);
911 next_pae->tail_applied_entry_index = pae->tail_applied_entry_index;
912 /* Remove ourselves and transfer the ownership of the colliding rules vector */
913 next_pae->colliding_rules = pae->colliding_rules;
914 /* unlink from the next element */
915 next_pae->prev_applied_entry_index = ~0;
916 add_del_hashtable_entry(am, lc_index,
917 applied_hash_aces, pae->next_applied_entry_index, 1);
919 /* no next entry, so just delete the entry in the hash table */
920 add_del_hashtable_entry(am, lc_index,
921 applied_hash_aces, old_index, 0);
924 DBG0("Releasing mask type index %d for pae index %d on lc_index %d", pae->mask_type_index, old_index, lc_index);
925 release_mask_type_index(am, pae->mask_type_index);
926 /* invalidate the old entry */
927 pae->mask_type_index = ~0;
928 pae->prev_applied_entry_index = ~0;
929 pae->next_applied_entry_index = ~0;
930 pae->tail_applied_entry_index = ~0;
931 /* always has to be 0 */
932 pae->colliding_rules = NULL;
937 hash_acl_unapply(acl_main_t *am, u32 lc_index, int acl_index)
941 DBG0("HASH ACL unapply: lc_index %d acl %d", lc_index, acl_index);
942 applied_hash_acl_info_t **applied_hash_acls = &am->applied_hash_acl_info_by_lc_index;
943 applied_hash_acl_info_t *pal = vec_elt_at_index((*applied_hash_acls), lc_index);
945 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
946 u32 **hash_acl_applied_lc_index = &ha->lc_index_list;
948 if (ACL_HASH_LOOKUP_DEBUG > 0) {
949 clib_warning("unapplying acl %d", acl_index);
950 acl_plugin_show_tables_mask_type();
951 acl_plugin_show_tables_acl_hash_info(acl_index);
952 acl_plugin_show_tables_applied_info(lc_index);
955 /* remove this acl# from the list of applied hash acls */
956 u32 index = vec_search(pal->applied_acls, acl_index);
958 clib_warning("BUG: trying to unapply unapplied acl_index %d on lc_index %d, according to lc",
959 acl_index, lc_index);
962 vec_del1(pal->applied_acls, index);
964 u32 index2 = vec_search((*hash_acl_applied_lc_index), lc_index);
966 clib_warning("BUG: trying to unapply twice acl_index %d on lc_index %d, according to h-acl info",
967 acl_index, lc_index);
970 vec_del1((*hash_acl_applied_lc_index), index2);
972 applied_hash_ace_entry_t **applied_hash_aces = get_applied_hash_aces(am, lc_index);
974 for(i=0; i < vec_len((*applied_hash_aces)); i++) {
975 if (vec_elt_at_index(*applied_hash_aces,i)->acl_index == acl_index) {
976 DBG("Found applied ACL#%d at applied index %d", acl_index, i);
980 if (vec_len((*applied_hash_aces)) <= i) {
981 DBG("Did not find applied ACL#%d at lc_index %d", acl_index, lc_index);
982 /* we went all the way without finding any entries. Probably a list was empty. */
986 void *oldheap = hash_acl_set_heap(am);
988 int tail_offset = base_offset + vec_len(ha->rules);
989 int tail_len = vec_len((*applied_hash_aces)) - tail_offset;
990 DBG("base_offset: %d, tail_offset: %d, tail_len: %d", base_offset, tail_offset, tail_len);
992 for(i=0; i < vec_len(ha->rules); i ++) {
993 deactivate_applied_ace_hash_entry(am, lc_index,
994 applied_hash_aces, base_offset + i);
996 for(i=0; i < tail_len; i ++) {
997 /* move the entry at tail offset to base offset */
998 /* that is, from (tail_offset+i) -> (base_offset+i) */
999 DBG0("UNAPPLY MOVE: lc_index %d, applied index %d -> %d", lc_index, tail_offset+i, base_offset + i);
1000 move_applied_ace_hash_entry(am, lc_index, applied_hash_aces, tail_offset + i, base_offset + i);
1002 /* trim the end of the vector */
1003 _vec_len((*applied_hash_aces)) -= vec_len(ha->rules);
1005 remake_hash_applied_mask_info_vec(am, applied_hash_aces, lc_index);
1007 if (vec_len((*applied_hash_aces)) == 0) {
1008 vec_free((*applied_hash_aces));
1011 clib_mem_set_heap (oldheap);
1015 * Create the applied ACEs and update the hash table,
1016 * taking into account that the ACL may not be the last
1017 * in the vector of applied ACLs.
1019 * For now, walk from the end of the vector and unapply the ACLs,
1020 * then apply the one in question and reapply the rest.
1024 hash_acl_reapply(acl_main_t *am, u32 lc_index, int acl_index)
1026 acl_lookup_context_t *acontext = pool_elt_at_index(am->acl_lookup_contexts, lc_index);
1027 u32 **applied_acls = &acontext->acl_indices;
1029 int start_index = vec_search((*applied_acls), acl_index);
1031 DBG0("Start index for acl %d in lc_index %d is %d", acl_index, lc_index, start_index);
1033 * This function is called after we find out the lc_index where ACL is applied.
1034 * If the by-lc_index vector does not have the ACL#, then it's a bug.
1036 ASSERT(start_index < vec_len(*applied_acls));
1038 /* unapply all the ACLs at the tail side, up to the current one */
1039 for(i = vec_len(*applied_acls) - 1; i > start_index; i--) {
1040 hash_acl_unapply(am, lc_index, *vec_elt_at_index(*applied_acls, i));
1042 for(i = start_index; i < vec_len(*applied_acls); i++) {
1043 hash_acl_apply(am, lc_index, *vec_elt_at_index(*applied_acls, i), i);
1048 make_ip6_address_mask(ip6_address_t *addr, u8 prefix_len)
1050 ip6_address_mask_from_width(addr, prefix_len);
1054 /* Maybe should be moved into the core somewhere */
1056 ip4_address_mask_from_width (ip4_address_t * a, u32 width)
1058 int i, byte, bit, bitnum;
1059 ASSERT (width <= 32);
1060 memset (a, 0, sizeof (a[0]));
1061 for (i = 0; i < width; i++)
1063 bitnum = (7 - (i & 7));
1066 a->as_u8[byte] |= bit;
1072 make_ip4_address_mask(ip4_address_t *addr, u8 prefix_len)
1074 ip4_address_mask_from_width(addr, prefix_len);
1078 make_port_mask(u16 *portmask, u16 port_first, u16 port_last)
1080 if (port_first == port_last) {
1082 /* single port is representable by masked value */
1091 make_mask_and_match_from_rule(fa_5tuple_t *mask, acl_rule_t *r, hash_ace_info_t *hi)
1093 memset(mask, 0, sizeof(*mask));
1094 memset(&hi->match, 0, sizeof(hi->match));
1095 hi->action = r->is_permit;
1097 /* we will need to be matching based on lc_index and mask_type_index when applied */
1098 mask->pkt.lc_index = ~0;
1099 /* we will assign the match of mask_type_index later when we find it*/
1100 mask->pkt.mask_type_index_lsb = ~0;
1102 mask->pkt.is_ip6 = 1;
1103 hi->match.pkt.is_ip6 = r->is_ipv6;
1105 make_ip6_address_mask(&mask->ip6_addr[0], r->src_prefixlen);
1106 hi->match.ip6_addr[0] = r->src.ip6;
1107 make_ip6_address_mask(&mask->ip6_addr[1], r->dst_prefixlen);
1108 hi->match.ip6_addr[1] = r->dst.ip6;
1110 memset(hi->match.l3_zero_pad, 0, sizeof(hi->match.l3_zero_pad));
1111 make_ip4_address_mask(&mask->ip4_addr[0], r->src_prefixlen);
1112 hi->match.ip4_addr[0] = r->src.ip4;
1113 make_ip4_address_mask(&mask->ip4_addr[1], r->dst_prefixlen);
1114 hi->match.ip4_addr[1] = r->dst.ip4;
1117 if (r->proto != 0) {
1118 mask->l4.proto = ~0; /* L4 proto needs to be matched */
1119 hi->match.l4.proto = r->proto;
1121 /* Calculate the src/dst port masks and make the src/dst port matches accordingly */
1122 make_port_mask(&mask->l4.port[0], r->src_port_or_type_first, r->src_port_or_type_last);
1123 hi->match.l4.port[0] = r->src_port_or_type_first & mask->l4.port[0];
1125 make_port_mask(&mask->l4.port[1], r->dst_port_or_code_first, r->dst_port_or_code_last);
1126 hi->match.l4.port[1] = r->dst_port_or_code_first & mask->l4.port[1];
1127 /* L4 info must be valid in order to match */
1128 mask->pkt.l4_valid = 1;
1129 hi->match.pkt.l4_valid = 1;
1130 /* And we must set the mask to check that it is an initial fragment */
1131 mask->pkt.is_nonfirst_fragment = 1;
1132 hi->match.pkt.is_nonfirst_fragment = 0;
1133 if ((r->proto == IPPROTO_TCP) && (r->tcp_flags_mask != 0)) {
1134 /* if we want to match on TCP flags, they must be masked off as well */
1135 mask->pkt.tcp_flags = r->tcp_flags_mask;
1136 hi->match.pkt.tcp_flags = r->tcp_flags_value;
1137 /* and the flags need to be present within the packet being matched */
1138 mask->pkt.tcp_flags_valid = 1;
1139 hi->match.pkt.tcp_flags_valid = 1;
1142 /* Sanitize the mask and the match */
1143 u64 *pmask = (u64 *)mask;
1144 u64 *pmatch = (u64 *)&hi->match;
1146 for(j=0; j<6; j++) {
1147 pmatch[j] = pmatch[j] & pmask[j];
1152 int hash_acl_exists(acl_main_t *am, int acl_index)
1154 if (acl_index >= vec_len(am->hash_acl_infos))
1157 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
1158 return ha->hash_acl_exists;
1161 void hash_acl_add(acl_main_t *am, int acl_index)
1163 void *oldheap = hash_acl_set_heap(am);
1164 DBG("HASH ACL add : %d", acl_index);
1166 acl_list_t *a = &am->acls[acl_index];
1167 vec_validate(am->hash_acl_infos, acl_index);
1168 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
1169 memset(ha, 0, sizeof(*ha));
1170 ha->hash_acl_exists = 1;
1172 /* walk the newly added ACL entries and ensure that for each of them there
1173 is a mask type, increment a reference count for that mask type */
1174 for(i=0; i < a->count; i++) {
1175 hash_ace_info_t ace_info;
1177 memset(&ace_info, 0, sizeof(ace_info));
1178 ace_info.acl_index = acl_index;
1179 ace_info.ace_index = i;
1181 make_mask_and_match_from_rule(&mask, &a->rules[i], &ace_info);
1182 mask.pkt.flags_reserved = 0b000;
1183 ace_info.base_mask_type_index = assign_mask_type_index(am, &mask);
1184 /* assign the mask type index for matching itself */
1185 ace_info.match.pkt.mask_type_index_lsb = ace_info.base_mask_type_index;
1186 DBG("ACE: %d mask_type_index: %d", i, ace_info.base_mask_type_index);
1187 vec_add1(ha->rules, ace_info);
1190 * if an ACL is applied somewhere, fill the corresponding lookup data structures.
1191 * We need to take care if the ACL is not the last one in the vector of ACLs applied to the interface.
1193 if (acl_index < vec_len(am->lc_index_vec_by_acl)) {
1195 vec_foreach(lc_index, am->lc_index_vec_by_acl[acl_index]) {
1196 hash_acl_reapply(am, *lc_index, acl_index);
1199 clib_mem_set_heap (oldheap);
1202 void hash_acl_delete(acl_main_t *am, int acl_index)
1204 void *oldheap = hash_acl_set_heap(am);
1205 DBG0("HASH ACL delete : %d", acl_index);
1207 * If the ACL is applied somewhere, remove the references of it (call hash_acl_unapply)
1208 * this is a different behavior from the linear lookup where an empty ACL is "deny all",
1210 * However, following vpp-dev discussion the ACL that is referenced elsewhere
1211 * should not be possible to delete, and the change adding this also adds
1212 * the safeguards to that respect, so this is not a problem.
1214 * The part to remember is that this routine is called in process of reapplication
1215 * during the acl_add_replace() API call - the old acl ruleset is deleted, then
1216 * the new one is added, without the change in the applied ACLs - so this case
1217 * has to be handled.
1219 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
1220 u32 *lc_list_copy = 0;
1223 lc_list_copy = vec_dup(ha->lc_index_list);
1224 vec_foreach(lc_index, lc_list_copy) {
1225 hash_acl_unapply(am, *lc_index, acl_index);
1227 vec_free(lc_list_copy);
1229 vec_free(ha->lc_index_list);
1231 /* walk the mask types for the ACL about-to-be-deleted, and decrease
1232 * the reference count, possibly freeing up some of them */
1234 for(i=0; i < vec_len(ha->rules); i++) {
1235 release_mask_type_index(am, ha->rules[i].base_mask_type_index);
1237 ha->hash_acl_exists = 0;
1238 vec_free(ha->rules);
1239 clib_mem_set_heap (oldheap);
1244 show_hash_acl_hash (vlib_main_t * vm, acl_main_t *am, u32 verbose)
1246 vlib_cli_output(vm, "\nACL lookup hash table:\n%U\n",
1247 BV (format_bihash), &am->acl_lookup_hash, verbose);
1251 acl_plugin_show_tables_mask_type (void)
1253 acl_main_t *am = &acl_main;
1254 vlib_main_t *vm = am->vlib_main;
1255 ace_mask_type_entry_t *mte;
1257 vlib_cli_output (vm, "Mask-type entries:");
1259 pool_foreach(mte, am->ace_mask_type_pool,
1261 vlib_cli_output(vm, " %3d: %016llx %016llx %016llx %016llx %016llx %016llx refcount %d",
1262 mte - am->ace_mask_type_pool,
1263 mte->mask.kv_40_8.key[0], mte->mask.kv_40_8.key[1], mte->mask.kv_40_8.key[2],
1264 mte->mask.kv_40_8.key[3], mte->mask.kv_40_8.key[4], mte->mask.kv_40_8.value, mte->refcount);
1270 acl_plugin_show_tables_acl_hash_info (u32 acl_index)
1272 acl_main_t *am = &acl_main;
1273 vlib_main_t *vm = am->vlib_main;
1276 vlib_cli_output (vm, "Mask-ready ACL representations\n");
1277 for (i = 0; i < vec_len (am->hash_acl_infos); i++)
1279 if ((acl_index != ~0) && (acl_index != i))
1283 hash_acl_info_t *ha = &am->hash_acl_infos[i];
1284 vlib_cli_output (vm, "acl-index %u bitmask-ready layout\n", i);
1285 vlib_cli_output (vm, " applied lc_index list: %U\n",
1286 format_vec32, ha->lc_index_list, "%d");
1287 for (j = 0; j < vec_len (ha->rules); j++)
1289 hash_ace_info_t *pa = &ha->rules[j];
1290 m = (u64 *) & pa->match;
1291 vlib_cli_output (vm,
1292 " %4d: %016llx %016llx %016llx %016llx %016llx %016llx base mask index %d acl %d rule %d action %d\n",
1293 j, m[0], m[1], m[2], m[3], m[4], m[5],
1294 pa->base_mask_type_index, pa->acl_index, pa->ace_index,
1301 acl_plugin_print_colliding_rule (vlib_main_t * vm, int j, collision_match_rule_t *cr) {
1303 " %4d: acl %d ace %d acl pos %d pae index: %d",
1304 j, cr->acl_index, cr->ace_index, cr->acl_position, cr->applied_entry_index);
1308 acl_plugin_print_pae (vlib_main_t * vm, int j, applied_hash_ace_entry_t * pae)
1310 vlib_cli_output (vm,
1311 " %4d: acl %d rule %d action %d bitmask-ready rule %d mask type index: %d colliding_rules: %d next %d prev %d tail %d hitcount %lld acl_pos: %d",
1312 j, pae->acl_index, pae->ace_index, pae->action,
1313 pae->hash_ace_info_index, pae->mask_type_index, vec_len(pae->colliding_rules), pae->next_applied_entry_index,
1314 pae->prev_applied_entry_index,
1315 pae->tail_applied_entry_index, pae->hitcount, pae->acl_position);
1317 for(jj=0; jj<vec_len(pae->colliding_rules); jj++)
1318 acl_plugin_print_colliding_rule(vm, jj, vec_elt_at_index(pae->colliding_rules, jj));
1322 acl_plugin_print_applied_mask_info (vlib_main_t * vm, int j, hash_applied_mask_info_t *mi)
1324 vlib_cli_output (vm,
1325 " %4d: mask type index %d first rule index %d num_entries %d max_collisions %d",
1326 j, mi->mask_type_index, mi->first_rule_index, mi->num_entries, mi->max_collisions);
1330 acl_plugin_show_tables_applied_info (u32 lc_index)
1332 acl_main_t *am = &acl_main;
1333 vlib_main_t *vm = am->vlib_main;
1335 vlib_cli_output (vm, "Applied lookup entries for lookup contexts");
1338 (lci < vec_len(am->applied_hash_acl_info_by_lc_index)); lci++)
1340 if ((lc_index != ~0) && (lc_index != lci))
1344 vlib_cli_output (vm, "lc_index %d:", lci);
1345 if (lci < vec_len (am->applied_hash_acl_info_by_lc_index))
1347 applied_hash_acl_info_t *pal =
1348 &am->applied_hash_acl_info_by_lc_index[lci];
1349 vlib_cli_output (vm, " applied acls: %U", format_vec32,
1350 pal->applied_acls, "%d");
1352 if (lci < vec_len (am->hash_applied_mask_info_vec_by_lc_index))
1354 vlib_cli_output (vm, " applied mask info entries:");
1356 j < vec_len (am->hash_applied_mask_info_vec_by_lc_index[lci]);
1359 acl_plugin_print_applied_mask_info (vm, j,
1360 &am->hash_applied_mask_info_vec_by_lc_index
1364 if (lci < vec_len (am->hash_entry_vec_by_lc_index))
1366 vlib_cli_output (vm, " lookup applied entries:");
1368 j < vec_len (am->hash_entry_vec_by_lc_index[lci]);
1371 acl_plugin_print_pae (vm, j,
1372 &am->hash_entry_vec_by_lc_index
1380 acl_plugin_show_tables_bihash (u32 show_bihash_verbose)
1382 acl_main_t *am = &acl_main;
1383 vlib_main_t *vm = am->vlib_main;
1384 show_hash_acl_hash (vm, am, show_bihash_verbose);
1388 * Split of the partition needs to happen when the collision count
1389 * goes over a specified threshold.
1391 * This is a signal that we ignored too many bits in
1392 * mT and we need to split the table into two tables. We select
1393 * all of the colliding rules L and find their maximum common
1394 * tuple mL. Normally mL is specific enough to hash L with few
1395 * or no collisions. We then create a new table T2 with tuple mL
1396 * and transfer all compatible rules from T to T2. If mL is not
1397 * specific enough, we find the field with the biggest difference
1398 * between the minimum and maximum tuple lengths for all of
1399 * the rules in L and set that field to be the average of those two
1400 * values. We then transfer all compatible rules as before. This
1401 * guarantees that some rules from L will move and that T2 will
1402 * have a smaller number of collisions than T did.
1407 ensure_ip6_min_addr (ip6_address_t * min_addr, ip6_address_t * mask_addr)
1410 (clib_net_to_host_u64 (mask_addr->as_u64[0]) <
1411 clib_net_to_host_u64 (min_addr->as_u64[0]))
1413 ((clib_net_to_host_u64 (mask_addr->as_u64[0]) ==
1414 clib_net_to_host_u64 (min_addr->as_u64[0]))
1415 && (clib_net_to_host_u64 (mask_addr->as_u64[1]) <
1416 clib_net_to_host_u64 (min_addr->as_u64[1])));
1419 min_addr->as_u64[0] = mask_addr->as_u64[0];
1420 min_addr->as_u64[1] = mask_addr->as_u64[1];
1425 ensure_ip6_max_addr (ip6_address_t * max_addr, ip6_address_t * mask_addr)
1428 (clib_net_to_host_u64 (mask_addr->as_u64[0]) >
1429 clib_net_to_host_u64 (max_addr->as_u64[0]))
1431 ((clib_net_to_host_u64 (mask_addr->as_u64[0]) ==
1432 clib_net_to_host_u64 (max_addr->as_u64[0]))
1433 && (clib_net_to_host_u64 (mask_addr->as_u64[1]) >
1434 clib_net_to_host_u64 (max_addr->as_u64[1])));
1437 max_addr->as_u64[0] = mask_addr->as_u64[0];
1438 max_addr->as_u64[1] = mask_addr->as_u64[1];
1443 ensure_ip4_min_addr (ip4_address_t * min_addr, ip4_address_t * mask_addr)
1446 (clib_net_to_host_u32 (mask_addr->as_u32) <
1447 clib_net_to_host_u32 (min_addr->as_u32));
1449 min_addr->as_u32 = mask_addr->as_u32;
1453 ensure_ip4_max_addr (ip4_address_t * max_addr, ip4_address_t * mask_addr)
1456 (clib_net_to_host_u32 (mask_addr->as_u32) >
1457 clib_net_to_host_u32 (max_addr->as_u32));
1459 max_addr->as_u32 = mask_addr->as_u32;
1473 split_partition(acl_main_t *am, u32 first_index,
1474 u32 lc_index, int is_ip6){
1475 DBG( "TM-split_partition - first_entry:%d", first_index);
1476 applied_hash_ace_entry_t **applied_hash_aces = get_applied_hash_aces(am, lc_index);
1477 ace_mask_type_entry_t *mte;
1478 fa_5tuple_t the_min_tuple, *min_tuple = &the_min_tuple;
1479 fa_5tuple_t the_max_tuple, *max_tuple = &the_max_tuple;
1480 applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), first_index);
1481 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, pae->acl_index);
1482 hash_ace_info_t *ace_info;
1483 u32 coll_mask_type_index = pae->mask_type_index;
1484 memset(&the_min_tuple, 0, sizeof(the_min_tuple));
1485 memset(&the_max_tuple, 0, sizeof(the_max_tuple));
1488 u64 collisions = vec_len(pae->colliding_rules);
1489 for(i=0; i<collisions; i++){
1490 /* reload the hash acl info as it might be a different ACL# */
1491 ha = vec_elt_at_index(am->hash_acl_infos, pae->acl_index);
1493 DBG( "TM-collision: base_ace:%d (ace_mask:%d, first_collision_mask:%d)",
1494 pae->ace_index, pae->mask_type_index, coll_mask_type_index);
1496 ace_info = vec_elt_at_index(ha->rules, pae->hash_ace_info_index);
1497 mte = vec_elt_at_index(am->ace_mask_type_pool, ace_info->base_mask_type_index);
1498 fa_5tuple_t *mask = &mte->mask;
1500 if(pae->mask_type_index != coll_mask_type_index) continue;
1501 /* Computing min_mask and max_mask for colliding rules */
1503 clib_memcpy(min_tuple, mask, sizeof(fa_5tuple_t));
1504 clib_memcpy(max_tuple, mask, sizeof(fa_5tuple_t));
1509 ensure_ip6_min_addr(&min_tuple->ip6_addr[j], &mask->ip6_addr[j]);
1511 ensure_ip4_min_addr(&min_tuple->ip4_addr[j], &mask->ip4_addr[j]);
1513 if ((mask->l4.port[j] < min_tuple->l4.port[j]))
1514 min_tuple->l4.port[j] = mask->l4.port[j];
1517 if ((mask->l4.proto < min_tuple->l4.proto))
1518 min_tuple->l4.proto = mask->l4.proto;
1520 if(mask->pkt.as_u64 < min_tuple->pkt.as_u64)
1521 min_tuple->pkt.as_u64 = mask->pkt.as_u64;
1526 ensure_ip6_max_addr(&max_tuple->ip6_addr[j], &mask->ip6_addr[j]);
1528 ensure_ip4_max_addr(&max_tuple->ip4_addr[j], &mask->ip4_addr[j]);
1530 if ((mask->l4.port[j] > max_tuple->l4.port[j]))
1531 max_tuple->l4.port[j] = mask->l4.port[j];
1534 if ((mask->l4.proto < max_tuple->l4.proto))
1535 max_tuple->l4.proto = mask->l4.proto;
1537 if(mask->pkt.as_u64 > max_tuple->pkt.as_u64)
1538 max_tuple->pkt.as_u64 = mask->pkt.as_u64;
1541 pae = pae->next_applied_entry_index == ~0 ? 0 : vec_elt_at_index((*applied_hash_aces), pae->next_applied_entry_index);
1544 /* Computing field with max difference between (min/max)_mask */
1545 int best_dim=-1, best_delta=0, delta=0;
1547 /* SRC_addr dimension */
1551 delta += count_bits(max_tuple->ip6_addr[0].as_u64[i]) - count_bits(min_tuple->ip6_addr[0].as_u64[i]);
1554 delta += count_bits(max_tuple->ip4_addr[0].as_u32) - count_bits(min_tuple->ip4_addr[0].as_u32);
1556 if(delta > best_delta){
1558 best_dim = DIM_SRC_ADDR;
1561 /* DST_addr dimension */
1566 delta += count_bits(max_tuple->ip6_addr[1].as_u64[i]) - count_bits(min_tuple->ip6_addr[1].as_u64[i]);
1569 delta += count_bits(max_tuple->ip4_addr[1].as_u32) - count_bits(min_tuple->ip4_addr[1].as_u32);
1571 if(delta > best_delta){
1573 best_dim = DIM_DST_ADDR;
1576 /* SRC_port dimension */
1577 delta = count_bits(max_tuple->l4.port[0]) - count_bits(min_tuple->l4.port[0]);
1578 if(delta > best_delta){
1580 best_dim = DIM_SRC_PORT;
1583 /* DST_port dimension */
1584 delta = count_bits(max_tuple->l4.port[1]) - count_bits(min_tuple->l4.port[1]);
1585 if(delta > best_delta){
1587 best_dim = DIM_DST_PORT;
1590 /* Proto dimension */
1591 delta = count_bits(max_tuple->l4.proto) - count_bits(min_tuple->l4.proto);
1592 if(delta > best_delta){
1594 best_dim = DIM_PROTO;
1597 int shifting = 0; //, ipv4_block = 0;
1600 shifting = (best_delta)/2; // FIXME IPV4-only
1601 // ipv4_block = count_bits(max_tuple->ip4_addr[0].as_u32);
1602 min_tuple->ip4_addr[0].as_u32 =
1603 clib_host_to_net_u32((clib_net_to_host_u32(max_tuple->ip4_addr[0].as_u32) << (shifting))&0xFFFFFFFF);
1607 shifting = (best_delta)/2;
1609 ipv4_block = count_bits(max_tuple->addr[1].as_u64[1]);
1610 if(ipv4_block > shifting)
1611 min_tuple->addr[1].as_u64[1] =
1612 clib_host_to_net_u64((clib_net_to_host_u64(max_tuple->addr[1].as_u64[1]) << (shifting))&0xFFFFFFFF);
1614 shifting = shifting - ipv4_block;
1615 min_tuple->addr[1].as_u64[1] = 0;
1616 min_tuple->addr[1].as_u64[0] =
1617 clib_host_to_net_u64((clib_net_to_host_u64(max_tuple->addr[1].as_u64[0]) << (shifting))&0xFFFFFFFF);
1620 min_tuple->ip4_addr[1].as_u32 =
1621 clib_host_to_net_u32((clib_net_to_host_u32(max_tuple->ip4_addr[1].as_u32) << (shifting))&0xFFFFFFFF);
1624 case DIM_SRC_PORT: min_tuple->l4.port[0] = max_tuple->l4.port[0] << (best_delta)/2;
1626 case DIM_DST_PORT: min_tuple->l4.port[1] = max_tuple->l4.port[1] << (best_delta)/2;
1628 case DIM_PROTO: min_tuple->l4.proto = max_tuple->l4.proto << (best_delta)/2;
1630 default: relax_tuple(min_tuple, is_ip6, 1);
1634 min_tuple->pkt.is_nonfirst_fragment = 0;
1635 u32 new_mask_type_index = assign_mask_type_index(am, min_tuple);
1637 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);
1639 hash_applied_mask_info_t *minfo;
1640 //search in order pool if mask_type_index is already there
1642 for (search=0; search < vec_len((*hash_applied_mask_info_vec)); search++){
1643 minfo = vec_elt_at_index((*hash_applied_mask_info_vec), search);
1644 if(minfo->mask_type_index == new_mask_type_index)
1648 vec_validate((*hash_applied_mask_info_vec), search);
1649 minfo = vec_elt_at_index((*hash_applied_mask_info_vec), search);
1650 minfo->mask_type_index = new_mask_type_index;
1651 minfo->num_entries = 0;
1652 minfo->max_collisions = 0;
1653 minfo->first_rule_index = ~0;
1655 DBG( "TM-split_partition - mask type index-assigned!! -> %d", new_mask_type_index);
1657 if(coll_mask_type_index == new_mask_type_index){
1658 //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);
1663 /* populate new partition */
1664 DBG( "TM-Populate new partition");
1665 u32 r_ace_index = first_index;
1666 int repopulate_count = 0;
1668 // for(i=0; i<collisions; i++){
1669 for(r_ace_index=0; r_ace_index < vec_len((*applied_hash_aces)); r_ace_index++) {
1671 applied_hash_ace_entry_t *pop_pae = vec_elt_at_index((*applied_hash_aces), r_ace_index);
1672 DBG( "TM-Population-collision: base_ace:%d (ace_mask:%d, first_collision_mask:%d)",
1673 pop_pae->ace_index, pop_pae->mask_type_index, coll_mask_type_index);
1675 if(pop_pae->mask_type_index != coll_mask_type_index) continue;
1676 u32 next_index = pop_pae->next_applied_entry_index;
1678 ace_info = vec_elt_at_index(ha->rules, pop_pae->hash_ace_info_index);
1679 mte = vec_elt_at_index(am->ace_mask_type_pool, ace_info->base_mask_type_index);
1681 //mte = vec_elt_at_index(am->ace_mask_type_pool, pop_pae->mask_type_index);
1682 fa_5tuple_t *pop_mask = &mte->mask;
1684 if(!first_mask_contains_second_mask(is_ip6, min_tuple, pop_mask)) continue;
1685 DBG( "TM-new partition can insert -> applied_ace:%d", r_ace_index);
1687 //delete and insert in new format
1688 deactivate_applied_ace_hash_entry(am, lc_index, applied_hash_aces, r_ace_index);
1690 /* insert the new entry */
1691 pop_pae->mask_type_index = new_mask_type_index;
1692 /* The very first repopulation gets the lock by virtue of a new mask being created above */
1693 if (++repopulate_count > 1)
1694 lock_mask_type_index(am, new_mask_type_index);
1696 activate_applied_ace_hash_entry(am, lc_index, applied_hash_aces, r_ace_index);
1698 r_ace_index = next_index;
1701 DBG( "TM-Populate new partition-END");
1702 DBG( "TM-split_partition - END");