Trivial: Clean up some typos.
[vpp.git] / src / vnet / util / throttle.h
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #ifndef __THROTTLE_H__
17 #define __THROTTLE_H__
18
19 #include <vlib/vlib.h>
20
21 /**
22  * @brief A throttle
23  *  Used in the data plane to decide if a given hash should be throttled,
24  *  i.e. that the hash has been seen already 'recently'. Recent is the time
25  *  given in the throttle's initialisation.
26  */
27 typedef struct throttle_t_
28 {
29   f64 time;
30   uword **bitmaps;
31   u32 *seeds;
32   f64 *last_seed_change_time;
33 } throttle_t;
34
35 #define THROTTLE_BITS   (512)
36
37 extern void throttle_init (throttle_t * t, u32 n_threads, f64 time);
38
39 always_inline u32
40 throttle_seed (throttle_t * t, u32 thread_index, f64 time_now)
41 {
42   if (time_now - t->last_seed_change_time[thread_index] > t->time)
43     {
44       (void) random_u32 (&t->seeds[thread_index]);
45       memset (t->bitmaps[thread_index], 0, THROTTLE_BITS / BITS (u8));
46
47       t->last_seed_change_time[thread_index] = time_now;
48     }
49   return t->seeds[thread_index];
50 }
51
52 always_inline int
53 throttle_check (throttle_t * t, u32 thread_index, u32 hash, u32 seed)
54 {
55   int drop;
56   uword m;
57   u32 w;
58
59   hash ^= seed;
60   /* Select bit number */
61   hash &= THROTTLE_BITS - 1;
62   w = hash / BITS (uword);
63   m = (uword) 1 << (hash % BITS (uword));
64
65   drop = (t->bitmaps[thread_index][w] & m) != 0;
66   t->bitmaps[thread_index][w] |= m;
67
68   return (drop);
69 }
70
71 #endif
72
73 /*
74  * fd.io coding-style-patch-verification: ON
75  *
76  * Local Variables:
77  * eval: (c-set-style "gnu")
78  * End:
79  */