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);
57 fill_applied_hash_ace_kv(acl_main_t *am,
58 applied_hash_ace_entry_t **applied_hash_aces,
60 u32 new_index, clib_bihash_kv_48_8_t *kv)
62 fa_5tuple_t *kv_key = (fa_5tuple_t *)kv->key;
63 hash_acl_lookup_value_t *kv_val = (hash_acl_lookup_value_t *)&kv->value;
64 applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), new_index);
65 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, pae->acl_index);
67 memcpy(kv_key, &(vec_elt_at_index(ha->rules, pae->hash_ace_info_index)->match), sizeof(*kv_key));
68 /* initialize the sw_if_index and direction */
69 kv_key->pkt.lc_index = lc_index;
71 kv_val->applied_entry_index = new_index;
72 kv_val->need_portrange_check = vec_elt_at_index(ha->rules, pae->hash_ace_info_index)->src_portrange_not_powerof2 ||
73 vec_elt_at_index(ha->rules, pae->hash_ace_info_index)->dst_portrange_not_powerof2;
74 /* by default assume all values are shadowed -> check all mask types */
79 add_del_hashtable_entry(acl_main_t *am,
81 applied_hash_ace_entry_t **applied_hash_aces,
82 u32 index, int is_add)
84 clib_bihash_kv_48_8_t kv;
86 fill_applied_hash_ace_kv(am, applied_hash_aces, lc_index, index, &kv);
87 hashtable_add_del(am, &kv, is_add);
93 activate_applied_ace_hash_entry(acl_main_t *am,
95 applied_hash_ace_entry_t **applied_hash_aces,
98 clib_bihash_kv_48_8_t kv;
99 ASSERT(new_index != ~0);
100 applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), new_index);
101 DBG("activate_applied_ace_hash_entry lc_index %d new_index %d", lc_index, new_index);
103 fill_applied_hash_ace_kv(am, applied_hash_aces, lc_index, new_index, &kv);
105 DBG("APPLY ADD KY: %016llx %016llx %016llx %016llx %016llx %016llx",
106 kv.key[0], kv.key[1], kv.key[2],
107 kv.key[3], kv.key[4], kv.key[5]);
109 clib_bihash_kv_48_8_t result;
110 hash_acl_lookup_value_t *result_val = (hash_acl_lookup_value_t *)&result.value;
111 int res = BV (clib_bihash_search) (&am->acl_lookup_hash, &kv, &result);
112 ASSERT(new_index != ~0);
113 ASSERT(new_index < vec_len((*applied_hash_aces)));
115 /* There already exists an entry or more. Append at the end. */
116 u32 first_index = result_val->applied_entry_index;
117 ASSERT(first_index != ~0);
118 DBG("A key already exists, with applied entry index: %d", first_index);
119 applied_hash_ace_entry_t *first_pae = vec_elt_at_index((*applied_hash_aces), first_index);
120 u32 last_index = first_pae->tail_applied_entry_index;
121 ASSERT(last_index != ~0);
122 applied_hash_ace_entry_t *last_pae = vec_elt_at_index((*applied_hash_aces), last_index);
123 DBG("...advance to chained entry index: %d", last_index);
124 /* link ourseves in */
125 last_pae->next_applied_entry_index = new_index;
126 pae->prev_applied_entry_index = last_index;
127 /* adjust the pointer to the new tail */
128 first_pae->tail_applied_entry_index = new_index;
130 /* It's the very first entry */
131 hashtable_add_del(am, &kv, 1);
132 ASSERT(new_index != ~0);
133 pae->tail_applied_entry_index = new_index;
138 applied_hash_entries_analyze(acl_main_t *am, applied_hash_ace_entry_t **applied_hash_aces)
141 * Go over the rules and check which ones are shadowed and which aren't.
142 * Naive approach: try to match the match value from every ACE as if it
143 * was a live packet, and see if the resulting match happens earlier in the list.
144 * if it does not match or it is later in the ACL - then the entry is not shadowed.
146 * This approach fails, an example:
147 * deny tcp 2001:db8::/32 2001:db8::/32
148 * permit ip 2001:db8::1/128 2001:db8::2/128
153 hash_acl_set_heap(acl_main_t *am)
155 if (0 == am->hash_lookup_mheap) {
156 am->hash_lookup_mheap = mheap_alloc (0 /* use VM */ , am->hash_lookup_mheap_size);
157 mheap_t *h = mheap_header (am->hash_lookup_mheap);
158 h->flags |= MHEAP_FLAG_THREAD_SAFE;
160 void *oldheap = clib_mem_set_heap(am->hash_lookup_mheap);
165 acl_plugin_hash_acl_set_validate_heap(int on)
167 acl_main_t *am = &acl_main;
168 clib_mem_set_heap(hash_acl_set_heap(am));
169 mheap_t *h = mheap_header (am->hash_lookup_mheap);
171 h->flags |= MHEAP_FLAG_VALIDATE;
172 h->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
175 h->flags &= ~MHEAP_FLAG_VALIDATE;
176 h->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
181 acl_plugin_hash_acl_set_trace_heap(int on)
183 acl_main_t *am = &acl_main;
184 clib_mem_set_heap(hash_acl_set_heap(am));
185 mheap_t *h = mheap_header (am->hash_lookup_mheap);
187 h->flags |= MHEAP_FLAG_TRACE;
189 h->flags &= ~MHEAP_FLAG_TRACE;
194 hash_acl_apply(acl_main_t *am, u32 lc_index, int acl_index, u32 acl_position)
198 DBG0("HASH ACL apply: lc_index %d acl %d", lc_index, acl_index);
199 if (!am->acl_lookup_hash_initialized) {
200 BV (clib_bihash_init) (&am->acl_lookup_hash, "ACL plugin rule lookup bihash",
201 am->hash_lookup_hash_buckets, am->hash_lookup_hash_memory);
202 am->acl_lookup_hash_initialized = 1;
205 void *oldheap = hash_acl_set_heap(am);
206 vec_validate(am->hash_entry_vec_by_lc_index, lc_index);
207 vec_validate(am->hash_acl_infos, acl_index);
208 applied_hash_ace_entry_t **applied_hash_aces = get_applied_hash_aces(am, lc_index);
210 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
211 u32 **hash_acl_applied_lc_index = &ha->lc_index_list;
213 int base_offset = vec_len(*applied_hash_aces);
215 /* Update the bitmap of the mask types with which the lookup
216 needs to happen for the ACLs applied to this lc_index */
217 applied_hash_acl_info_t **applied_hash_acls = &am->applied_hash_acl_info_by_lc_index;
218 vec_validate((*applied_hash_acls), lc_index);
219 applied_hash_acl_info_t *pal = vec_elt_at_index((*applied_hash_acls), lc_index);
221 /* ensure the list of applied hash acls is initialized and add this acl# to it */
222 u32 index = vec_search(pal->applied_acls, acl_index);
224 clib_warning("BUG: trying to apply twice acl_index %d on lc_index %d, according to lc",
225 acl_index, lc_index);
228 vec_add1(pal->applied_acls, acl_index);
229 u32 index2 = vec_search((*hash_acl_applied_lc_index), lc_index);
231 clib_warning("BUG: trying to apply twice acl_index %d on lc_index %d, according to hash h-acl info",
232 acl_index, lc_index);
235 vec_add1((*hash_acl_applied_lc_index), lc_index);
237 pal->mask_type_index_bitmap = clib_bitmap_or(pal->mask_type_index_bitmap,
238 ha->mask_type_index_bitmap);
240 * if the applied ACL is empty, the current code will cause a
241 * different behavior compared to current linear search: an empty ACL will
242 * simply fallthrough to the next ACL, or the default deny in the end.
244 * This is not a problem, because after vpp-dev discussion,
245 * the consensus was it should not be possible to apply the non-existent
246 * ACL, so the change adding this code also takes care of that.
249 /* expand the applied aces vector by the necessary amount */
250 vec_resize((*applied_hash_aces), vec_len(ha->rules));
252 /* add the rules from the ACL to the hash table for lookup and append to the vector*/
253 for(i=0; i < vec_len(ha->rules); i++) {
254 u32 new_index = base_offset + i;
255 applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), new_index);
256 pae->acl_index = acl_index;
257 pae->ace_index = ha->rules[i].ace_index;
258 pae->acl_position = acl_position;
259 pae->action = ha->rules[i].action;
261 pae->hash_ace_info_index = i;
262 /* we might link it in later */
263 pae->next_applied_entry_index = ~0;
264 pae->prev_applied_entry_index = ~0;
265 pae->tail_applied_entry_index = ~0;
266 activate_applied_ace_hash_entry(am, lc_index, applied_hash_aces, new_index);
268 applied_hash_entries_analyze(am, applied_hash_aces);
270 clib_mem_set_heap (oldheap);
274 find_head_applied_ace_index(applied_hash_ace_entry_t **applied_hash_aces, u32 curr_index)
277 * find back the first entry. Inefficient so might need to be a bit cleverer
278 * if this proves to be a problem..
280 u32 an_index = curr_index;
281 ASSERT(an_index != ~0);
282 applied_hash_ace_entry_t *head_pae = vec_elt_at_index((*applied_hash_aces), an_index);
283 while(head_pae->prev_applied_entry_index != ~0) {
284 an_index = head_pae->prev_applied_entry_index;
285 ASSERT(an_index != ~0);
286 head_pae = vec_elt_at_index((*applied_hash_aces), an_index);
292 move_applied_ace_hash_entry(acl_main_t *am,
294 applied_hash_ace_entry_t **applied_hash_aces,
295 u32 old_index, u32 new_index)
297 ASSERT(old_index != ~0);
298 ASSERT(new_index != ~0);
300 *vec_elt_at_index((*applied_hash_aces), new_index) = *vec_elt_at_index((*applied_hash_aces), old_index);
302 /* update the linkage and hash table if necessary */
303 applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), old_index);
305 if (pae->prev_applied_entry_index != ~0) {
306 applied_hash_ace_entry_t *prev_pae = vec_elt_at_index((*applied_hash_aces), pae->prev_applied_entry_index);
307 ASSERT(prev_pae->next_applied_entry_index == old_index);
308 prev_pae->next_applied_entry_index = new_index;
310 /* first entry - so the hash points to it, update */
311 add_del_hashtable_entry(am, lc_index,
312 applied_hash_aces, new_index, 1);
313 ASSERT(pae->tail_applied_entry_index != ~0);
315 if (pae->next_applied_entry_index != ~0) {
316 applied_hash_ace_entry_t *next_pae = vec_elt_at_index((*applied_hash_aces), pae->next_applied_entry_index);
317 ASSERT(next_pae->prev_applied_entry_index == old_index);
318 next_pae->prev_applied_entry_index = new_index;
321 * Moving the very last entry, so we need to update the tail pointer in the first one.
323 u32 head_index = find_head_applied_ace_index(applied_hash_aces, old_index);
324 ASSERT(head_index != ~0);
325 applied_hash_ace_entry_t *head_pae = vec_elt_at_index((*applied_hash_aces), head_index);
327 ASSERT(head_pae->tail_applied_entry_index == old_index);
328 head_pae->tail_applied_entry_index = new_index;
330 /* invalidate the old entry */
331 pae->prev_applied_entry_index = ~0;
332 pae->next_applied_entry_index = ~0;
333 pae->tail_applied_entry_index = ~0;
337 deactivate_applied_ace_hash_entry(acl_main_t *am,
339 applied_hash_ace_entry_t **applied_hash_aces,
342 applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), old_index);
343 DBG("UNAPPLY DEACTIVATE: lc_index %d applied index %d", lc_index, old_index);
345 if (pae->prev_applied_entry_index != ~0) {
346 DBG("UNAPPLY = index %d has prev_applied_entry_index %d", old_index, pae->prev_applied_entry_index);
347 applied_hash_ace_entry_t *prev_pae = vec_elt_at_index((*applied_hash_aces), pae->prev_applied_entry_index);
348 ASSERT(prev_pae->next_applied_entry_index == old_index);
349 prev_pae->next_applied_entry_index = pae->next_applied_entry_index;
350 if (pae->next_applied_entry_index == ~0) {
351 /* it was a last entry we removed, update the pointer on the first one */
352 u32 head_index = find_head_applied_ace_index(applied_hash_aces, old_index);
353 DBG("UNAPPLY = index %d head index to update %d", old_index, head_index);
354 ASSERT(head_index != ~0);
355 applied_hash_ace_entry_t *head_pae = vec_elt_at_index((*applied_hash_aces), head_index);
357 ASSERT(head_pae->tail_applied_entry_index == old_index);
358 head_pae->tail_applied_entry_index = pae->prev_applied_entry_index;
360 applied_hash_ace_entry_t *next_pae = vec_elt_at_index((*applied_hash_aces), pae->next_applied_entry_index);
361 next_pae->prev_applied_entry_index = pae->prev_applied_entry_index;
364 /* It was the first entry. We need either to reset the hash entry or delete it */
365 if (pae->next_applied_entry_index != ~0) {
366 /* the next element becomes the new first one, so needs the tail pointer to be set */
367 applied_hash_ace_entry_t *next_pae = vec_elt_at_index((*applied_hash_aces), pae->next_applied_entry_index);
368 ASSERT(pae->tail_applied_entry_index != ~0);
369 next_pae->tail_applied_entry_index = pae->tail_applied_entry_index;
370 DBG("Resetting the hash table entry from %d to %d, setting tail index to %d", old_index, pae->next_applied_entry_index, pae->tail_applied_entry_index);
371 /* unlink from the next element */
372 next_pae->prev_applied_entry_index = ~0;
373 add_del_hashtable_entry(am, lc_index,
374 applied_hash_aces, pae->next_applied_entry_index, 1);
376 /* no next entry, so just delete the entry in the hash table */
377 add_del_hashtable_entry(am, lc_index,
378 applied_hash_aces, old_index, 0);
381 /* invalidate the old entry */
382 pae->prev_applied_entry_index = ~0;
383 pae->next_applied_entry_index = ~0;
384 pae->tail_applied_entry_index = ~0;
389 hash_acl_build_applied_lookup_bitmap(acl_main_t *am, u32 lc_index)
392 uword *new_lookup_bitmap = 0;
394 applied_hash_acl_info_t **applied_hash_acls = &am->applied_hash_acl_info_by_lc_index;
395 vec_validate((*applied_hash_acls), lc_index);
396 applied_hash_acl_info_t *pal = vec_elt_at_index((*applied_hash_acls), lc_index);
398 for(i=0; i < vec_len(pal->applied_acls); i++) {
399 u32 a_acl_index = *vec_elt_at_index((pal->applied_acls), i);
400 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, a_acl_index);
401 DBG("Update bitmask = %U or %U (acl_index %d)\n", format_bitmap_hex, new_lookup_bitmap,
402 format_bitmap_hex, ha->mask_type_index_bitmap, a_acl_index);
403 new_lookup_bitmap = clib_bitmap_or(new_lookup_bitmap,
404 ha->mask_type_index_bitmap);
406 uword *old_lookup_bitmap = pal->mask_type_index_bitmap;
407 pal->mask_type_index_bitmap = new_lookup_bitmap;
408 clib_bitmap_free(old_lookup_bitmap);
412 hash_acl_unapply(acl_main_t *am, u32 lc_index, int acl_index)
416 DBG0("HASH ACL unapply: lc_index %d acl %d", lc_index, acl_index);
417 applied_hash_acl_info_t **applied_hash_acls = &am->applied_hash_acl_info_by_lc_index;
418 applied_hash_acl_info_t *pal = vec_elt_at_index((*applied_hash_acls), lc_index);
420 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
421 u32 **hash_acl_applied_lc_index = &ha->lc_index_list;
423 /* remove this acl# from the list of applied hash acls */
424 u32 index = vec_search(pal->applied_acls, acl_index);
426 clib_warning("BUG: trying to unapply unapplied acl_index %d on lc_index %d, according to lc",
427 acl_index, lc_index);
430 vec_del1(pal->applied_acls, index);
432 u32 index2 = vec_search((*hash_acl_applied_lc_index), lc_index);
434 clib_warning("BUG: trying to unapply twice acl_index %d on lc_index %d, according to h-acl info",
435 acl_index, lc_index);
438 vec_del1((*hash_acl_applied_lc_index), index2);
440 applied_hash_ace_entry_t **applied_hash_aces = get_applied_hash_aces(am, lc_index);
442 for(i=0; i < vec_len((*applied_hash_aces)); i++) {
443 if (vec_elt_at_index(*applied_hash_aces,i)->acl_index == acl_index) {
444 DBG("Found applied ACL#%d at applied index %d", acl_index, i);
448 if (vec_len((*applied_hash_aces)) <= i) {
449 DBG("Did not find applied ACL#%d at lc_index %d", acl_index, lc_index);
450 /* we went all the way without finding any entries. Probably a list was empty. */
454 void *oldheap = hash_acl_set_heap(am);
456 int tail_offset = base_offset + vec_len(ha->rules);
457 int tail_len = vec_len((*applied_hash_aces)) - tail_offset;
458 DBG("base_offset: %d, tail_offset: %d, tail_len: %d", base_offset, tail_offset, tail_len);
460 for(i=0; i < vec_len(ha->rules); i ++) {
461 deactivate_applied_ace_hash_entry(am, lc_index,
462 applied_hash_aces, base_offset + i);
464 for(i=0; i < tail_len; i ++) {
465 /* move the entry at tail offset to base offset */
466 /* that is, from (tail_offset+i) -> (base_offset+i) */
467 DBG("UNAPPLY MOVE: lc_index %d, applied index %d -> %d", lc_index, tail_offset+i, base_offset + i);
468 move_applied_ace_hash_entry(am, lc_index, applied_hash_aces, tail_offset + i, base_offset + i);
470 /* trim the end of the vector */
471 _vec_len((*applied_hash_aces)) -= vec_len(ha->rules);
473 applied_hash_entries_analyze(am, applied_hash_aces);
475 /* After deletion we might not need some of the mask-types anymore... */
476 hash_acl_build_applied_lookup_bitmap(am, lc_index);
477 clib_mem_set_heap (oldheap);
481 * Create the applied ACEs and update the hash table,
482 * taking into account that the ACL may not be the last
483 * in the vector of applied ACLs.
485 * For now, walk from the end of the vector and unapply the ACLs,
486 * then apply the one in question and reapply the rest.
490 hash_acl_reapply(acl_main_t *am, u32 lc_index, int acl_index)
492 acl_lookup_context_t *acontext = pool_elt_at_index(am->acl_lookup_contexts, lc_index);
493 u32 **applied_acls = &acontext->acl_indices;
495 int start_index = vec_search((*applied_acls), acl_index);
497 DBG0("Start index for acl %d in lc_index %d is %d", acl_index, lc_index, start_index);
499 * This function is called after we find out the sw_if_index where ACL is applied.
500 * If the by-sw_if_index vector does not have the ACL#, then it's a bug.
502 ASSERT(start_index < vec_len(*applied_acls));
504 /* unapply all the ACLs at the tail side, up to the current one */
505 for(i = vec_len(*applied_acls) - 1; i > start_index; i--) {
506 hash_acl_unapply(am, lc_index, *vec_elt_at_index(*applied_acls, i));
508 for(i = start_index; i < vec_len(*applied_acls); i++) {
509 hash_acl_apply(am, lc_index, *vec_elt_at_index(*applied_acls, i), i);
514 make_address_mask(ip46_address_t *addr, u8 is_ipv6, u8 prefix_len)
517 ip6_address_mask_from_width(&addr->ip6, prefix_len);
519 /* FIXME: this may not be correct way */
520 ip6_address_mask_from_width(&addr->ip6, prefix_len + 3*32);
521 ip46_address_mask_ip4(addr);
526 make_port_mask(u16 *portmask, u16 port_first, u16 port_last)
528 if (port_first == port_last) {
530 /* single port is representable by masked value */
533 if ((port_first == 0) && (port_last == 65535)) {
535 /* wildcard port is representable by a masked value */
540 * For now match all the ports, later
541 * here might be a better optimization which would
542 * pick out bitmaskable portranges.
544 * However, adding a new mask type potentially
545 * adds a per-packet extra lookup, so the benefit is not clear.
548 /* This port range can't be represented via bitmask exactly. */
553 make_mask_and_match_from_rule(fa_5tuple_t *mask, acl_rule_t *r, hash_ace_info_t *hi, int match_nonfirst_fragment)
555 memset(mask, 0, sizeof(*mask));
556 memset(&hi->match, 0, sizeof(hi->match));
557 hi->action = r->is_permit;
559 /* we will need to be matching based on lc_index and mask_type_index when applied */
560 mask->pkt.lc_index = ~0;
561 /* we will assign the match of mask_type_index later when we find it*/
562 mask->pkt.mask_type_index_lsb = ~0;
564 mask->pkt.is_ip6 = 1;
565 hi->match.pkt.is_ip6 = r->is_ipv6;
567 make_address_mask(&mask->addr[0], r->is_ipv6, r->src_prefixlen);
568 hi->match.addr[0] = r->src;
569 make_address_mask(&mask->addr[1], r->is_ipv6, r->dst_prefixlen);
570 hi->match.addr[1] = r->dst;
573 mask->l4.proto = ~0; /* L4 proto needs to be matched */
574 hi->match.l4.proto = r->proto;
575 if (match_nonfirst_fragment) {
576 /* match the non-first fragments only */
577 mask->pkt.is_nonfirst_fragment = 1;
578 hi->match.pkt.is_nonfirst_fragment = 1;
580 /* Calculate the src/dst port masks and make the src/dst port matches accordingly */
581 hi->src_portrange_not_powerof2 = make_port_mask(&mask->l4.port[0], r->src_port_or_type_first, r->src_port_or_type_last);
582 hi->match.l4.port[0] = r->src_port_or_type_first & mask->l4.port[0];
583 hi->dst_portrange_not_powerof2 = make_port_mask(&mask->l4.port[1], r->dst_port_or_code_first, r->dst_port_or_code_last);
584 hi->match.l4.port[1] = r->dst_port_or_code_first & mask->l4.port[1];
585 /* L4 info must be valid in order to match */
586 mask->pkt.l4_valid = 1;
587 hi->match.pkt.l4_valid = 1;
588 /* And we must set the mask to check that it is an initial fragment */
589 mask->pkt.is_nonfirst_fragment = 1;
590 hi->match.pkt.is_nonfirst_fragment = 0;
591 if ((r->proto == IPPROTO_TCP) && (r->tcp_flags_mask != 0)) {
592 /* if we want to match on TCP flags, they must be masked off as well */
593 mask->pkt.tcp_flags = r->tcp_flags_mask;
594 hi->match.pkt.tcp_flags = r->tcp_flags_value;
595 /* and the flags need to be present within the packet being matched */
596 mask->pkt.tcp_flags_valid = 1;
597 hi->match.pkt.tcp_flags_valid = 1;
601 /* Sanitize the mask and the match */
602 u64 *pmask = (u64 *)mask;
603 u64 *pmatch = (u64 *)&hi->match;
606 pmatch[j] = pmatch[j] & pmask[j];
611 find_mask_type_index(acl_main_t *am, fa_5tuple_t *mask)
613 ace_mask_type_entry_t *mte;
615 pool_foreach(mte, am->ace_mask_type_pool,
617 if(memcmp(&mte->mask, mask, sizeof(*mask)) == 0)
618 return (mte - am->ace_mask_type_pool);
625 assign_mask_type_index(acl_main_t *am, fa_5tuple_t *mask)
627 u32 mask_type_index = find_mask_type_index(am, mask);
628 ace_mask_type_entry_t *mte;
629 if(~0 == mask_type_index) {
630 pool_get_aligned (am->ace_mask_type_pool, mte, CLIB_CACHE_LINE_BYTES);
631 mask_type_index = mte - am->ace_mask_type_pool;
632 clib_memcpy(&mte->mask, mask, sizeof(mte->mask));
635 * We can use only 16 bits, since in the match there is only u16 field.
636 * Realistically, once you go to 64K of mask types, it is a huge
637 * problem anyway, so we might as well stop half way.
639 ASSERT(mask_type_index < 32768);
641 mte = am->ace_mask_type_pool + mask_type_index;
643 return mask_type_index;
647 release_mask_type_index(acl_main_t *am, u32 mask_type_index)
649 ace_mask_type_entry_t *mte = pool_elt_at_index(am->ace_mask_type_pool, mask_type_index);
651 if (mte->refcount == 0) {
652 /* we are not using this entry anymore */
653 pool_put(am->ace_mask_type_pool, mte);
657 int hash_acl_exists(acl_main_t *am, int acl_index)
659 if (acl_index >= vec_len(am->hash_acl_infos))
662 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
663 return ha->hash_acl_exists;
666 void hash_acl_add(acl_main_t *am, int acl_index)
668 void *oldheap = hash_acl_set_heap(am);
669 DBG("HASH ACL add : %d", acl_index);
671 acl_list_t *a = &am->acls[acl_index];
672 vec_validate(am->hash_acl_infos, acl_index);
673 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
674 memset(ha, 0, sizeof(*ha));
675 ha->hash_acl_exists = 1;
677 /* walk the newly added ACL entries and ensure that for each of them there
678 is a mask type, increment a reference count for that mask type */
679 for(i=0; i < a->count; i++) {
680 hash_ace_info_t ace_info;
682 memset(&ace_info, 0, sizeof(ace_info));
683 ace_info.acl_index = acl_index;
684 ace_info.ace_index = i;
686 make_mask_and_match_from_rule(&mask, &a->rules[i], &ace_info, 0);
687 ace_info.mask_type_index = assign_mask_type_index(am, &mask);
688 /* assign the mask type index for matching itself */
689 ace_info.match.pkt.mask_type_index_lsb = ace_info.mask_type_index;
690 DBG("ACE: %d mask_type_index: %d", i, ace_info.mask_type_index);
691 /* Ensure a given index is set in the mask type index bitmap for this ACL */
692 ha->mask_type_index_bitmap = clib_bitmap_set(ha->mask_type_index_bitmap, ace_info.mask_type_index, 1);
693 vec_add1(ha->rules, ace_info);
694 if (am->l4_match_nonfirst_fragment) {
695 /* add the second rule which matches the noninitial fragments with the respective mask */
696 make_mask_and_match_from_rule(&mask, &a->rules[i], &ace_info, 1);
697 ace_info.mask_type_index = assign_mask_type_index(am, &mask);
698 ace_info.match.pkt.mask_type_index_lsb = ace_info.mask_type_index;
699 DBG("ACE: %d (non-initial frags) mask_type_index: %d", i, ace_info.mask_type_index);
700 /* Ensure a given index is set in the mask type index bitmap for this ACL */
701 ha->mask_type_index_bitmap = clib_bitmap_set(ha->mask_type_index_bitmap, ace_info.mask_type_index, 1);
702 vec_add1(ha->rules, ace_info);
706 * if an ACL is applied somewhere, fill the corresponding lookup data structures.
707 * We need to take care if the ACL is not the last one in the vector of ACLs applied to the interface.
709 if (acl_index < vec_len(am->lc_index_vec_by_acl)) {
711 vec_foreach(lc_index, am->lc_index_vec_by_acl[acl_index]) {
712 hash_acl_reapply(am, *lc_index, acl_index);
715 clib_mem_set_heap (oldheap);
718 void hash_acl_delete(acl_main_t *am, int acl_index)
720 void *oldheap = hash_acl_set_heap(am);
721 DBG0("HASH ACL delete : %d", acl_index);
723 * If the ACL is applied somewhere, remove the references of it (call hash_acl_unapply)
724 * this is a different behavior from the linear lookup where an empty ACL is "deny all",
726 * However, following vpp-dev discussion the ACL that is referenced elsewhere
727 * should not be possible to delete, and the change adding this also adds
728 * the safeguards to that respect, so this is not a problem.
730 * The part to rememeber is that this routine is called in process of reapplication
731 * during the acl_add_replace() API call - the old acl ruleset is deleted, then
732 * the new one is added, without the change in the applied ACLs - so this case
735 hash_acl_info_t *ha = vec_elt_at_index(am->hash_acl_infos, acl_index);
736 u32 *lc_list_copy = 0;
739 lc_list_copy = vec_dup(ha->lc_index_list);
740 vec_foreach(lc_index, lc_list_copy) {
741 hash_acl_unapply(am, *lc_index, acl_index);
743 vec_free(lc_list_copy);
746 /* walk the mask types for the ACL about-to-be-deleted, and decrease
747 * the reference count, possibly freeing up some of them */
749 for(i=0; i < vec_len(ha->rules); i++) {
750 release_mask_type_index(am, ha->rules[i].mask_type_index);
752 clib_bitmap_free(ha->mask_type_index_bitmap);
753 ha->hash_acl_exists = 0;
755 clib_mem_set_heap (oldheap);
760 show_hash_acl_hash (vlib_main_t * vm, acl_main_t *am, u32 verbose)
762 vlib_cli_output(vm, "\nACL lookup hash table:\n%U\n",
763 BV (format_bihash), &am->acl_lookup_hash, verbose);
767 acl_plugin_show_tables_mask_type (void)
769 acl_main_t *am = &acl_main;
770 vlib_main_t *vm = am->vlib_main;
771 ace_mask_type_entry_t *mte;
773 vlib_cli_output (vm, "Mask-type entries:");
775 pool_foreach(mte, am->ace_mask_type_pool,
777 vlib_cli_output(vm, " %3d: %016llx %016llx %016llx %016llx %016llx %016llx refcount %d",
778 mte - am->ace_mask_type_pool,
779 mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
780 mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
786 acl_plugin_show_tables_acl_hash_info (u32 acl_index)
788 acl_main_t *am = &acl_main;
789 vlib_main_t *vm = am->vlib_main;
792 vlib_cli_output (vm, "Mask-ready ACL representations\n");
793 for (i = 0; i < vec_len (am->hash_acl_infos); i++)
795 if ((acl_index != ~0) && (acl_index != i))
799 hash_acl_info_t *ha = &am->hash_acl_infos[i];
800 vlib_cli_output (vm, "acl-index %u bitmask-ready layout\n", i);
801 vlib_cli_output (vm, " applied lc_index list: %U\n",
802 format_vec32, ha->lc_index_list, "%d");
803 vlib_cli_output (vm, " mask type index bitmap: %U\n",
804 format_bitmap_hex, ha->mask_type_index_bitmap);
805 for (j = 0; j < vec_len (ha->rules); j++)
807 hash_ace_info_t *pa = &ha->rules[j];
808 m = (u64 *) & pa->match;
810 " %4d: %016llx %016llx %016llx %016llx %016llx %016llx mask index %d acl %d rule %d action %d src/dst portrange not ^2: %d,%d\n",
811 j, m[0], m[1], m[2], m[3], m[4], m[5],
812 pa->mask_type_index, pa->acl_index, pa->ace_index,
813 pa->action, pa->src_portrange_not_powerof2,
814 pa->dst_portrange_not_powerof2);
820 acl_plugin_print_pae (vlib_main_t * vm, int j, applied_hash_ace_entry_t * pae)
823 " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d hitcount %lld",
824 j, pae->acl_index, pae->ace_index, pae->action,
825 pae->hash_ace_info_index, pae->next_applied_entry_index,
826 pae->prev_applied_entry_index,
827 pae->tail_applied_entry_index, pae->hitcount);
831 acl_plugin_show_tables_applied_info (u32 sw_if_index)
833 acl_main_t *am = &acl_main;
834 vlib_main_t *vm = am->vlib_main;
836 vlib_cli_output (vm, "Applied lookup entries for interfaces");
839 (swi < vec_len (am->input_lc_index_by_sw_if_index))
840 || (swi < vec_len (am->output_lc_index_by_sw_if_index)); swi++)
842 if ((sw_if_index != ~0) && (sw_if_index != swi))
847 vlib_cli_output (vm, "sw_if_index %d:", swi);
848 if (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
850 applied_hash_acl_info_t *pal =
851 &am->input_applied_hash_acl_info_by_sw_if_index[swi];
852 vlib_cli_output (vm, " input lookup mask_type_index_bitmap: %U",
853 format_bitmap_hex, pal->mask_type_index_bitmap);
854 vlib_cli_output (vm, " input applied acls: %U", format_vec32,
855 pal->applied_acls, "%d");
857 if (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
859 vlib_cli_output (vm, " input lookup applied entries:");
861 j < vec_len (am->input_hash_entry_vec_by_sw_if_index[swi]);
864 acl_plugin_print_pae (vm, j,
865 &am->input_hash_entry_vec_by_sw_if_index
870 if (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
872 applied_hash_acl_info_t *pal =
873 &am->output_applied_hash_acl_info_by_sw_if_index[swi];
874 vlib_cli_output (vm, " output lookup mask_type_index_bitmap: %U",
875 format_bitmap_hex, pal->mask_type_index_bitmap);
876 vlib_cli_output (vm, " output applied acls: %U", format_vec32,
877 pal->applied_acls, "%d");
879 if (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index))
881 vlib_cli_output (vm, " output lookup applied entries:");
883 j < vec_len (am->output_hash_entry_vec_by_sw_if_index[swi]);
886 acl_plugin_print_pae (vm, j,
887 &am->output_hash_entry_vec_by_sw_if_index
896 acl_plugin_show_tables_bihash (u32 show_bihash_verbose)
898 acl_main_t *am = &acl_main;
899 vlib_main_t *vm = am->vlib_main;
900 show_hash_acl_hash (vm, am, show_bihash_verbose);