Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | #ifndef __included_vnet_classify_h__ |
| 16 | #define __included_vnet_classify_h__ |
| 17 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 18 | #include <vnet/vnet.h> |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 19 | #include <vnet/api_errno.h> /* for API error numbers */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 20 | |
| 21 | #include <vppinfra/error.h> |
| 22 | #include <vppinfra/hash.h> |
| 23 | #include <vppinfra/cache.h> |
Ray Kinsella | 7741afa | 2020-04-07 07:51:23 +0100 | [diff] [blame] | 24 | #include <vppinfra/crc32.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | #include <vppinfra/xxhash.h> |
| 26 | |
Jean-Mickael Guerin | 8941ec2 | 2016-03-04 14:14:21 +0100 | [diff] [blame] | 27 | extern vlib_node_registration_t ip4_classify_node; |
| 28 | extern vlib_node_registration_t ip6_classify_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | |
| 30 | #define CLASSIFY_TRACE 0 |
| 31 | |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 32 | /* |
| 33 | * Classify table option to process packets |
| 34 | * CLASSIFY_FLAG_USE_CURR_DATA: |
| 35 | * - classify packets starting from VPP node’s current data pointer |
| 36 | */ |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 37 | typedef enum vnet_classify_flags_t_ |
| 38 | { |
| 39 | CLASSIFY_FLAG_NONE = 0, |
| 40 | CLASSIFY_FLAG_USE_CURR_DATA = (1 << 0), |
| 41 | } __clib_packed vnet_classify_flags_t; |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Classify session action |
| 45 | * CLASSIFY_ACTION_SET_IP4_FIB_INDEX: |
| 46 | * - Classified IP packets will be looked up |
| 47 | * from the specified ipv4 fib table |
| 48 | * CLASSIFY_ACTION_SET_IP6_FIB_INDEX: |
| 49 | * - Classified IP packets will be looked up |
| 50 | * from the specified ipv6 fib table |
| 51 | */ |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 52 | typedef enum vnet_classify_action_t_ |
| 53 | { |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 54 | CLASSIFY_ACTION_NONE = 0, |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 55 | CLASSIFY_ACTION_SET_IP4_FIB_INDEX = 1, |
| 56 | CLASSIFY_ACTION_SET_IP6_FIB_INDEX = 2, |
Dave Barach | 630a8e2 | 2017-11-18 06:58:34 -0500 | [diff] [blame] | 57 | CLASSIFY_ACTION_SET_METADATA = 3, |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 58 | } __clib_packed vnet_classify_action_t; |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 59 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | struct _vnet_classify_main; |
| 61 | typedef struct _vnet_classify_main vnet_classify_main_t; |
| 62 | |
| 63 | #define foreach_size_in_u32x4 \ |
| 64 | _(1) \ |
| 65 | _(2) \ |
| 66 | _(3) \ |
| 67 | _(4) \ |
| 68 | _(5) |
| 69 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 70 | typedef struct _vnet_classify_entry |
| 71 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | /* put into vnet_buffer(b)->l2_classfy.opaque_index */ |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 73 | union |
| 74 | { |
| 75 | struct |
| 76 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | u32 opaque_index; |
| 78 | /* advance on hit, note it's a signed quantity... */ |
| 79 | i32 advance; |
| 80 | }; |
| 81 | u64 opaque_count; |
| 82 | }; |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 83 | /* Hit counter */ |
| 84 | union |
| 85 | { |
| 86 | u64 hits; |
| 87 | struct _vnet_classify_entry *next_free; |
| 88 | }; |
| 89 | /* last heard time */ |
| 90 | f64 last_heard; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | |
| 92 | /* Really only need 1 bit */ |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 93 | u8 flags; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | #define VNET_CLASSIFY_ENTRY_FREE (1<<0) |
| 95 | |
Neale Ranns | 13eaf3e | 2017-05-23 06:10:33 -0700 | [diff] [blame] | 96 | vnet_classify_action_t action; |
Steve Shin | 25e26dc | 2016-11-08 10:47:10 -0800 | [diff] [blame] | 97 | u16 metadata; |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 98 | /* Graph node next index */ |
| 99 | u32 next_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | |
| 101 | /* Must be aligned to a 16-octet boundary */ |
| 102 | u32x4 key[0]; |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 103 | } vnet_classify_entry_t; |
| 104 | |
| 105 | /** |
| 106 | * Check there's no padding in the entry. the key lies on a 16 byte boundary. |
| 107 | */ |
| 108 | STATIC_ASSERT_OFFSET_OF (vnet_classify_entry_t, key, 32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 110 | static inline int |
| 111 | vnet_classify_entry_is_free (vnet_classify_entry_t * e) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | { |
| 113 | return e->flags & VNET_CLASSIFY_ENTRY_FREE; |
| 114 | } |
| 115 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 116 | static inline int |
| 117 | vnet_classify_entry_is_busy (vnet_classify_entry_t * e) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | { |
| 119 | return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0); |
| 120 | } |
| 121 | |
| 122 | /* Need these to con the vector allocator */ |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 123 | #define _(size) \ |
| 124 | typedef struct \ |
| 125 | { \ |
| 126 | vnet_classify_entry_t e; \ |
| 127 | u32x4 key[size]; \ |
| 128 | } __clib_packed vnet_classify_entry_##size##_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | foreach_size_in_u32x4; |
| 130 | #undef _ |
| 131 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 132 | typedef struct |
| 133 | { |
| 134 | union |
| 135 | { |
| 136 | struct |
| 137 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | u32 offset; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 139 | u8 linear_search; |
| 140 | u8 pad[2]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | u8 log2_pages; |
| 142 | }; |
| 143 | u64 as_u64; |
| 144 | }; |
| 145 | } vnet_classify_bucket_t; |
| 146 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 147 | typedef struct |
| 148 | { |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 149 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 150 | /* hash Buckets */ |
| 151 | vnet_classify_bucket_t *buckets; |
| 152 | |
| 153 | /* Private allocation arena, protected by the writer lock, |
| 154 | * where the entries are stored. */ |
| 155 | void *mheap; |
| 156 | |
| 157 | /* User/client data associated with the table */ |
| 158 | uword user_ctx; |
| 159 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | u32 nbuckets; |
| 161 | u32 log2_nbuckets; |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 162 | u32 entries_per_page; |
| 163 | u32 skip_n_vectors; |
| 164 | u32 match_n_vectors; |
| 165 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | /* Index of next table to try */ |
| 167 | u32 next_table_index; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 168 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 169 | /* packet offsets */ |
| 170 | i16 current_data_offset; |
| 171 | vnet_classify_flags_t current_data_flag; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | /* Miss next index, return if next_table_index = 0 */ |
| 173 | u32 miss_next_index; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 174 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 175 | /** |
| 176 | * All members accessed in the DP above here |
| 177 | */ |
| 178 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); |
| 179 | |
| 180 | /* Config parameters */ |
| 181 | u32 linear_buckets; |
| 182 | u32 active_elements; |
| 183 | u32 data_offset; |
| 184 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | /* Per-bucket working copies, one per thread */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 186 | vnet_classify_entry_t **working_copies; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 187 | int *working_copy_lengths; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | vnet_classify_bucket_t saved_bucket; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 189 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | /* Free entry freelists */ |
| 191 | vnet_classify_entry_t **freelists; |
| 192 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | /* Writer (only) lock for this table */ |
jaszha03 | 5cdde5c | 2019-07-11 20:47:24 +0000 | [diff] [blame] | 194 | clib_spinlock_t writer_lock; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 195 | |
Damjan Marion | 3bb2da9 | 2021-09-20 13:39:37 +0200 | [diff] [blame] | 196 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline2); |
| 197 | /* Mask to apply after skipping N vectors */ |
| 198 | u32x4 mask[8]; |
| 199 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | } vnet_classify_table_t; |
| 201 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 202 | /** |
| 203 | * Ensure DP fields don't spill over to cache-line 2 |
| 204 | */ |
| 205 | STATIC_ASSERT_OFFSET_OF (vnet_classify_table_t, cacheline1, |
| 206 | CLIB_CACHE_LINE_BYTES); |
| 207 | |
| 208 | /** |
| 209 | * The vector size for the classifier |
| 210 | * in the add/del table 'match' is the number of vectors of this size |
| 211 | */ |
| 212 | #define VNET_CLASSIFY_VECTOR_SIZE \ |
| 213 | sizeof (((vnet_classify_table_t *) 0)->mask[0]) |
| 214 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 215 | struct _vnet_classify_main |
| 216 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | /* Table pool */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 218 | vnet_classify_table_t *tables; |
| 219 | |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 220 | /* Registered next-index, opaque unformat fcns */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 221 | unformat_function_t **unformat_l2_next_index_fns; |
| 222 | unformat_function_t **unformat_ip_next_index_fns; |
| 223 | unformat_function_t **unformat_acl_next_index_fns; |
| 224 | unformat_function_t **unformat_policer_next_index_fns; |
| 225 | unformat_function_t **unformat_opaque_index_fns; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 226 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 227 | /* Per-interface filter table. [0] is used for pcap */ |
| 228 | u32 *classify_table_index_by_sw_if_index; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 229 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | /* convenience variables */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 231 | vlib_main_t *vlib_main; |
| 232 | vnet_main_t *vnet_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | }; |
| 234 | |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 235 | extern vnet_classify_main_t vnet_classify_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 237 | u8 *format_classify_table (u8 * s, va_list * args); |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 238 | u8 *format_vnet_classify_table (u8 *s, va_list *args); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | |
| 240 | u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h); |
| 241 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 242 | static_always_inline vnet_classify_table_t * |
| 243 | vnet_classify_table_get (u32 table_index) |
| 244 | { |
| 245 | vnet_classify_main_t *vcm = &vnet_classify_main; |
| 246 | |
| 247 | return (pool_elt_at_index (vcm->tables, table_index)); |
| 248 | } |
| 249 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 250 | static inline u64 |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 251 | vnet_classify_hash_packet_inline (vnet_classify_table_t *t, const u8 *h) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | { |
Pierre Pfister | cb65630 | 2016-03-16 09:14:28 +0000 | [diff] [blame] | 253 | u32x4 *mask; |
| 254 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 255 | union |
| 256 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | u32x4 as_u32x4; |
| 258 | u64 as_u64[2]; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 259 | } xor_sum __attribute__ ((aligned (sizeof (u32x4)))); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 261 | ASSERT (t); |
Pierre Pfister | cb65630 | 2016-03-16 09:14:28 +0000 | [diff] [blame] | 262 | mask = t->mask; |
Adrian Oanca | 22ac59b | 2018-02-23 16:27:41 +0100 | [diff] [blame] | 263 | #ifdef CLIB_HAVE_VEC128 |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 264 | u32x4u *data = (u32x4u *) h; |
| 265 | xor_sum.as_u32x4 = data[0 + t->skip_n_vectors] & mask[0]; |
| 266 | switch (t->match_n_vectors) |
Pierre Pfister | cb65630 | 2016-03-16 09:14:28 +0000 | [diff] [blame] | 267 | { |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 268 | case 5: |
| 269 | xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4]; |
| 270 | /* FALLTHROUGH */ |
| 271 | case 4: |
| 272 | xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3]; |
| 273 | /* FALLTHROUGH */ |
| 274 | case 3: |
| 275 | xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2]; |
| 276 | /* FALLTHROUGH */ |
| 277 | case 2: |
| 278 | xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1]; |
| 279 | /* FALLTHROUGH */ |
| 280 | case 1: |
| 281 | break; |
| 282 | default: |
| 283 | abort (); |
Pierre Pfister | cb65630 | 2016-03-16 09:14:28 +0000 | [diff] [blame] | 284 | } |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 285 | #else |
| 286 | u32 skip_u64 = t->skip_n_vectors * 2; |
| 287 | u64 *data64 = (u64 *) h; |
| 288 | xor_sum.as_u64[0] = data64[0 + skip_u64] & ((u64 *) mask)[0]; |
| 289 | xor_sum.as_u64[1] = data64[1 + skip_u64] & ((u64 *) mask)[1]; |
| 290 | switch (t->match_n_vectors) |
| 291 | { |
| 292 | case 5: |
| 293 | xor_sum.as_u64[0] ^= data64[8 + skip_u64] & ((u64 *) mask)[8]; |
| 294 | xor_sum.as_u64[1] ^= data64[9 + skip_u64] & ((u64 *) mask)[9]; |
| 295 | /* FALLTHROUGH */ |
| 296 | case 4: |
| 297 | xor_sum.as_u64[0] ^= data64[6 + skip_u64] & ((u64 *) mask)[6]; |
| 298 | xor_sum.as_u64[1] ^= data64[7 + skip_u64] & ((u64 *) mask)[7]; |
| 299 | /* FALLTHROUGH */ |
| 300 | case 3: |
| 301 | xor_sum.as_u64[0] ^= data64[4 + skip_u64] & ((u64 *) mask)[4]; |
| 302 | xor_sum.as_u64[1] ^= data64[5 + skip_u64] & ((u64 *) mask)[5]; |
| 303 | /* FALLTHROUGH */ |
| 304 | case 2: |
| 305 | xor_sum.as_u64[0] ^= data64[2 + skip_u64] & ((u64 *) mask)[2]; |
| 306 | xor_sum.as_u64[1] ^= data64[3 + skip_u64] & ((u64 *) mask)[3]; |
| 307 | /* FALLTHROUGH */ |
| 308 | case 1: |
| 309 | break; |
| 310 | |
| 311 | default: |
| 312 | abort (); |
| 313 | } |
| 314 | #endif /* CLIB_HAVE_VEC128 */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 315 | |
Ray Kinsella | 7741afa | 2020-04-07 07:51:23 +0100 | [diff] [blame] | 316 | #ifdef clib_crc32c_uses_intrinsics |
| 317 | return clib_crc32c ((u8 *) & xor_sum, sizeof (xor_sum)); |
| 318 | #else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]); |
Ray Kinsella | 7741afa | 2020-04-07 07:51:23 +0100 | [diff] [blame] | 320 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 321 | } |
| 322 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 323 | static inline void |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 324 | vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash) |
| 325 | { |
| 326 | u32 bucket_index; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 327 | |
| 328 | ASSERT (is_pow2 (t->nbuckets)); |
| 329 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 330 | bucket_index = hash & (t->nbuckets - 1); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 331 | |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 332 | clib_prefetch_load (&t->buckets[bucket_index]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 333 | } |
| 334 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 335 | static inline vnet_classify_entry_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 336 | vnet_classify_get_entry (vnet_classify_table_t * t, uword offset) |
| 337 | { |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 338 | u8 *hp = clib_mem_get_heap_base (t->mheap); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 339 | u8 *vp = hp + offset; |
| 340 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 341 | return (vnet_classify_entry_t *) vp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 342 | } |
| 343 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 344 | static inline uword |
| 345 | vnet_classify_get_offset (vnet_classify_table_t * t, |
| 346 | vnet_classify_entry_t * v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 347 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 348 | u8 *hp, *vp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 349 | |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 350 | hp = (u8 *) clib_mem_get_heap_base (t->mheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 351 | vp = (u8 *) v; |
| 352 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 353 | ASSERT ((vp - hp) < 0x100000000ULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | return vp - hp; |
| 355 | } |
| 356 | |
| 357 | static inline vnet_classify_entry_t * |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 358 | vnet_classify_entry_at_index (vnet_classify_table_t * t, |
| 359 | vnet_classify_entry_t * e, u32 index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 361 | u8 *eu8; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 363 | eu8 = (u8 *) e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | |
| 365 | eu8 += index * (sizeof (vnet_classify_entry_t) + |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 366 | (t->match_n_vectors * sizeof (u32x4))); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 367 | |
| 368 | return (vnet_classify_entry_t *) eu8; |
| 369 | } |
| 370 | |
| 371 | static inline void |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 372 | vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 373 | { |
| 374 | u32 bucket_index; |
| 375 | u32 value_index; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 376 | vnet_classify_bucket_t *b; |
| 377 | vnet_classify_entry_t *e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 378 | |
| 379 | bucket_index = hash & (t->nbuckets - 1); |
| 380 | |
| 381 | b = &t->buckets[bucket_index]; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 382 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | if (b->offset == 0) |
| 384 | return; |
| 385 | |
| 386 | hash >>= t->log2_nbuckets; |
| 387 | |
| 388 | e = vnet_classify_get_entry (t, b->offset); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 389 | value_index = hash & ((1 << b->log2_pages) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | |
| 391 | e = vnet_classify_entry_at_index (t, e, value_index); |
| 392 | |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 393 | clib_prefetch_load (e); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | } |
| 395 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 396 | vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t, |
| 397 | u8 * h, u64 hash, f64 now); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 398 | |
Damjan Marion | c31a234 | 2021-09-20 17:36:49 +0200 | [diff] [blame^] | 399 | static_always_inline int |
| 400 | vnet_classify_entry_is_equal (vnet_classify_entry_t *v, const u8 *d, u8 *m, |
| 401 | u32 match_n_vectors) |
| 402 | { |
| 403 | #ifdef CLIB_HAVE_VEC128 |
| 404 | u64x2u *data = (u64x2 *) d; |
| 405 | u64x2 *key = (u64x2 *) v->key; |
| 406 | u64x2 *mask = (u64x2 *) m; |
| 407 | u64x2 r; |
| 408 | |
| 409 | r = (data[0] & mask[0]) ^ key[0]; |
| 410 | switch (match_n_vectors) |
| 411 | { |
| 412 | case 5: |
| 413 | r |= (data[4] & mask[4]) ^ key[4]; |
| 414 | /* fall through */ |
| 415 | case 4: |
| 416 | r |= (data[3] & mask[3]) ^ key[3]; |
| 417 | /* fall through */ |
| 418 | case 3: |
| 419 | r |= (data[2] & mask[2]) ^ key[2]; |
| 420 | /* fall through */ |
| 421 | case 2: |
| 422 | r |= (data[1] & mask[1]) ^ key[1]; |
| 423 | /* fall through */ |
| 424 | case 1: |
| 425 | break; |
| 426 | default: |
| 427 | abort (); |
| 428 | } |
| 429 | |
| 430 | if (u64x2_is_all_zero (r)) |
| 431 | return 1; |
| 432 | |
| 433 | #else |
| 434 | u64 *data = (u64 *) d; |
| 435 | u64 *key = (u64 *) v->key; |
| 436 | u64 *mask = (u64 *) m; |
| 437 | u64 r; |
| 438 | |
| 439 | r = ((data[0] & mask[0]) ^ key[0]) | ((data[1] & mask[1]) ^ key[1]); |
| 440 | switch (match_n_vectors) |
| 441 | { |
| 442 | case 5: |
| 443 | r |= ((data[8] & mask[8]) ^ key[8]) | ((data[9] & mask[9]) ^ key[9]); |
| 444 | /* fall through */ |
| 445 | case 4: |
| 446 | r |= ((data[6] & mask[6]) ^ key[6]) | ((data[7] & mask[7]) ^ key[7]); |
| 447 | /* fall through */ |
| 448 | case 3: |
| 449 | r |= ((data[4] & mask[4]) ^ key[4]) | ((data[5] & mask[5]) ^ key[5]); |
| 450 | /* fall through */ |
| 451 | case 2: |
| 452 | r |= ((data[2] & mask[2]) ^ key[2]) | ((data[3] & mask[3]) ^ key[3]); |
| 453 | /* fall through */ |
| 454 | case 1: |
| 455 | break; |
| 456 | default: |
| 457 | abort (); |
| 458 | } |
| 459 | |
| 460 | if (r == 0) |
| 461 | return 1; |
| 462 | |
| 463 | #endif /* CLIB_HAVE_VEC128 */ |
| 464 | return 0; |
| 465 | } |
| 466 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 467 | static inline vnet_classify_entry_t * |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 468 | vnet_classify_find_entry_inline (vnet_classify_table_t *t, const u8 *h, |
| 469 | u64 hash, f64 now) |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 470 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 471 | vnet_classify_entry_t *v; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 472 | vnet_classify_bucket_t *b; |
Damjan Marion | c31a234 | 2021-09-20 17:36:49 +0200 | [diff] [blame^] | 473 | u32 bucket_index, limit, pages, match_n_vectors = t->match_n_vectors; |
| 474 | u8 *mask = (u8 *) t->mask; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 475 | int i; |
| 476 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 477 | bucket_index = hash & (t->nbuckets - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 478 | b = &t->buckets[bucket_index]; |
| 479 | |
| 480 | if (b->offset == 0) |
| 481 | return 0; |
| 482 | |
Damjan Marion | a9f5f85 | 2021-09-20 17:12:03 +0200 | [diff] [blame] | 483 | pages = 1 << b->log2_pages; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 484 | v = vnet_classify_get_entry (t, b->offset); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 485 | limit = t->entries_per_page; |
| 486 | if (PREDICT_FALSE (b->linear_search)) |
| 487 | { |
Damjan Marion | a9f5f85 | 2021-09-20 17:12:03 +0200 | [diff] [blame] | 488 | limit *= pages; |
| 489 | v = vnet_classify_entry_at_index (t, v, 0); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 490 | } |
Damjan Marion | a9f5f85 | 2021-09-20 17:12:03 +0200 | [diff] [blame] | 491 | else |
| 492 | { |
| 493 | hash >>= t->log2_nbuckets; |
| 494 | v = vnet_classify_entry_at_index (t, v, hash & (pages - 1)); |
| 495 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 496 | |
Damjan Marion | c31a234 | 2021-09-20 17:36:49 +0200 | [diff] [blame^] | 497 | h += t->skip_n_vectors * 16; |
| 498 | |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 499 | for (i = 0; i < limit; i++) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 500 | { |
Damjan Marion | c31a234 | 2021-09-20 17:36:49 +0200 | [diff] [blame^] | 501 | if (vnet_classify_entry_is_equal (v, h, mask, match_n_vectors)) |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 502 | { |
| 503 | if (PREDICT_TRUE (now)) |
| 504 | { |
| 505 | v->hits++; |
| 506 | v->last_heard = now; |
| 507 | } |
| 508 | return (v); |
| 509 | } |
| 510 | v = vnet_classify_entry_at_index (t, v, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | } |
| 512 | return 0; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 513 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 515 | vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t *cm, |
| 516 | const u8 *mask, u32 nbuckets, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 517 | u32 memory_size, |
| 518 | u32 skip_n_vectors, |
| 519 | u32 match_n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 520 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 521 | int vnet_classify_add_del_session (vnet_classify_main_t *cm, u32 table_index, |
| 522 | const u8 *match, u32 hit_next_index, |
| 523 | u32 opaque_index, i32 advance, u8 action, |
| 524 | u16 metadata, int is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 525 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 526 | int vnet_classify_add_del_table (vnet_classify_main_t *cm, const u8 *mask, |
| 527 | u32 nbuckets, u32 memory_size, u32 skip, |
| 528 | u32 match, u32 next_table_index, |
| 529 | u32 miss_next_index, u32 *table_index, |
| 530 | u8 current_data_flag, i16 current_data_offset, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 531 | int is_add, int del_chain); |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 532 | void vnet_classify_delete_table_index (vnet_classify_main_t *cm, |
| 533 | u32 table_index, int del_chain); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 534 | |
| 535 | unformat_function_t unformat_ip4_mask; |
| 536 | unformat_function_t unformat_ip6_mask; |
| 537 | unformat_function_t unformat_l3_mask; |
| 538 | unformat_function_t unformat_l2_mask; |
| 539 | unformat_function_t unformat_classify_mask; |
| 540 | unformat_function_t unformat_l2_next_index; |
| 541 | unformat_function_t unformat_ip_next_index; |
| 542 | unformat_function_t unformat_ip4_match; |
| 543 | unformat_function_t unformat_ip6_match; |
| 544 | unformat_function_t unformat_l3_match; |
Dave Barach | 4a3f69c | 2017-02-22 12:44:56 -0500 | [diff] [blame] | 545 | unformat_function_t unformat_l4_match; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 546 | unformat_function_t unformat_vlan_tag; |
| 547 | unformat_function_t unformat_l2_match; |
| 548 | unformat_function_t unformat_classify_match; |
| 549 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 550 | void vnet_classify_register_unformat_ip_next_index_fn |
| 551 | (unformat_function_t * fn); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 552 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 553 | void vnet_classify_register_unformat_l2_next_index_fn |
| 554 | (unformat_function_t * fn); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 555 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 556 | void vnet_classify_register_unformat_acl_next_index_fn |
| 557 | (unformat_function_t * fn); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 558 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 559 | void vnet_classify_register_unformat_policer_next_index_fn |
| 560 | (unformat_function_t * fn); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 561 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 562 | void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t * |
| 563 | fn); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 564 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 565 | u32 classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index); |
| 566 | void classify_set_pcap_chain (vnet_classify_main_t * cm, |
| 567 | u32 sw_if_index, u32 table_index); |
| 568 | |
| 569 | u32 classify_get_trace_chain (void); |
| 570 | void classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index); |
| 571 | |
| 572 | u32 classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index); |
| 573 | u32 classify_lookup_chain (u32 table_index, |
| 574 | u8 * mask, u32 n_skip, u32 n_match); |
| 575 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 576 | #endif /* __included_vnet_classify_h__ */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 577 | |
| 578 | /* |
| 579 | * fd.io coding-style-patch-verification: ON |
| 580 | * |
| 581 | * Local Variables: |
| 582 | * eval: (c-set-style "gnu") |
| 583 | * End: |
| 584 | */ |