Neale Ranns | c8352bc | 2018-08-29 10:23:58 -0700 | [diff] [blame] | 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> |
Neale Ranns | cd35e53 | 2018-08-31 02:51:45 -0700 | [diff] [blame] | 20 | #include <vppinfra/xxhash.h> |
Neale Ranns | c8352bc | 2018-08-29 10:23:58 -0700 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * @brief A throttle |
| 24 | * Used in the data plane to decide if a given hash should be throttled, |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 25 | * i.e. that the hash has been seen already 'recently'. Recent is the time |
Neale Ranns | c8352bc | 2018-08-29 10:23:58 -0700 | [diff] [blame] | 26 | * given in the throttle's initialisation. |
| 27 | */ |
| 28 | typedef struct throttle_t_ |
| 29 | { |
| 30 | f64 time; |
| 31 | uword **bitmaps; |
Neale Ranns | cd35e53 | 2018-08-31 02:51:45 -0700 | [diff] [blame] | 32 | u64 *seeds; |
Neale Ranns | c8352bc | 2018-08-29 10:23:58 -0700 | [diff] [blame] | 33 | f64 *last_seed_change_time; |
Maxime Peim | f6ba562 | 2023-02-06 10:14:20 +0000 | [diff] [blame] | 34 | u32 buckets; |
Neale Ranns | c8352bc | 2018-08-29 10:23:58 -0700 | [diff] [blame] | 35 | } throttle_t; |
| 36 | |
| 37 | #define THROTTLE_BITS (512) |
| 38 | |
Maxime Peim | f6ba562 | 2023-02-06 10:14:20 +0000 | [diff] [blame] | 39 | extern void throttle_init (throttle_t *t, u32 n_threads, u32 buckets, |
| 40 | f64 time); |
Neale Ranns | c8352bc | 2018-08-29 10:23:58 -0700 | [diff] [blame] | 41 | |
Neale Ranns | cd35e53 | 2018-08-31 02:51:45 -0700 | [diff] [blame] | 42 | always_inline u64 |
Neale Ranns | c8352bc | 2018-08-29 10:23:58 -0700 | [diff] [blame] | 43 | throttle_seed (throttle_t * t, u32 thread_index, f64 time_now) |
| 44 | { |
| 45 | if (time_now - t->last_seed_change_time[thread_index] > t->time) |
| 46 | { |
Neale Ranns | cd35e53 | 2018-08-31 02:51:45 -0700 | [diff] [blame] | 47 | (void) random_u64 (&t->seeds[thread_index]); |
Maxime Peim | f6ba562 | 2023-02-06 10:14:20 +0000 | [diff] [blame] | 48 | clib_bitmap_zero (t->bitmaps[thread_index]); |
Neale Ranns | c8352bc | 2018-08-29 10:23:58 -0700 | [diff] [blame] | 49 | |
| 50 | t->last_seed_change_time[thread_index] = time_now; |
| 51 | } |
| 52 | return t->seeds[thread_index]; |
| 53 | } |
| 54 | |
| 55 | always_inline int |
Neale Ranns | cd35e53 | 2018-08-31 02:51:45 -0700 | [diff] [blame] | 56 | throttle_check (throttle_t * t, u32 thread_index, u64 hash, u64 seed) |
Neale Ranns | c8352bc | 2018-08-29 10:23:58 -0700 | [diff] [blame] | 57 | { |
Maxime Peim | f6ba562 | 2023-02-06 10:14:20 +0000 | [diff] [blame] | 58 | ASSERT (is_pow2 (t->buckets)); |
Neale Ranns | c8352bc | 2018-08-29 10:23:58 -0700 | [diff] [blame] | 59 | |
Neale Ranns | cd35e53 | 2018-08-31 02:51:45 -0700 | [diff] [blame] | 60 | hash = clib_xxhash (hash ^ seed); |
| 61 | |
Neale Ranns | c8352bc | 2018-08-29 10:23:58 -0700 | [diff] [blame] | 62 | /* Select bit number */ |
Maxime Peim | f6ba562 | 2023-02-06 10:14:20 +0000 | [diff] [blame] | 63 | hash &= t->buckets - 1; |
Neale Ranns | c8352bc | 2018-08-29 10:23:58 -0700 | [diff] [blame] | 64 | |
Maxime Peim | f6ba562 | 2023-02-06 10:14:20 +0000 | [diff] [blame] | 65 | return clib_bitmap_set_no_check (t->bitmaps[thread_index], hash, 1); |
Neale Ranns | c8352bc | 2018-08-29 10:23:58 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | #endif |
| 69 | |
| 70 | /* |
| 71 | * fd.io coding-style-patch-verification: ON |
| 72 | * |
| 73 | * Local Variables: |
| 74 | * eval: (c-set-style "gnu") |
| 75 | * End: |
| 76 | */ |