2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef __THROTTLE_H__
17 #define __THROTTLE_H__
19 #include <vlib/vlib.h>
20 #include <vppinfra/xxhash.h>
24 * Used in the data plane to decide if a given hash should be throttled,
25 * i.e. that the hash has been seen already 'recently'. Recent is the time
26 * given in the throttle's initialisation.
28 typedef struct throttle_t_
33 f64 *last_seed_change_time;
37 #define THROTTLE_BITS (512)
39 extern void throttle_init (throttle_t *t, u32 n_threads, u32 buckets,
43 throttle_seed (throttle_t * t, u32 thread_index, f64 time_now)
45 if (time_now - t->last_seed_change_time[thread_index] > t->time)
47 (void) random_u64 (&t->seeds[thread_index]);
48 clib_bitmap_zero (t->bitmaps[thread_index]);
50 t->last_seed_change_time[thread_index] = time_now;
52 return t->seeds[thread_index];
56 throttle_check (throttle_t * t, u32 thread_index, u64 hash, u64 seed)
58 ASSERT (is_pow2 (t->buckets));
60 hash = clib_xxhash (hash ^ seed);
62 /* Select bit number */
63 hash &= t->buckets - 1;
65 return clib_bitmap_set_no_check (t->bitmaps[thread_index], hash, 1);
71 * fd.io coding-style-patch-verification: ON
74 * eval: (c-set-style "gnu")