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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | /* Mask to apply after skipping N vectors */ |
| 151 | u32x4 *mask; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 152 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 153 | /* hash Buckets */ |
| 154 | vnet_classify_bucket_t *buckets; |
| 155 | |
| 156 | /* Private allocation arena, protected by the writer lock, |
| 157 | * where the entries are stored. */ |
| 158 | void *mheap; |
| 159 | |
| 160 | /* User/client data associated with the table */ |
| 161 | uword user_ctx; |
| 162 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | u32 nbuckets; |
| 164 | u32 log2_nbuckets; |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 165 | u32 entries_per_page; |
| 166 | u32 skip_n_vectors; |
| 167 | u32 match_n_vectors; |
| 168 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | /* Index of next table to try */ |
| 170 | u32 next_table_index; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 171 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 172 | /* packet offsets */ |
| 173 | i16 current_data_offset; |
| 174 | vnet_classify_flags_t current_data_flag; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | /* Miss next index, return if next_table_index = 0 */ |
| 176 | u32 miss_next_index; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 177 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 178 | /** |
| 179 | * All members accessed in the DP above here |
| 180 | */ |
| 181 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); |
| 182 | |
| 183 | /* Config parameters */ |
| 184 | u32 linear_buckets; |
| 185 | u32 active_elements; |
| 186 | u32 data_offset; |
| 187 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | /* Per-bucket working copies, one per thread */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 189 | vnet_classify_entry_t **working_copies; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 190 | int *working_copy_lengths; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | vnet_classify_bucket_t saved_bucket; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 192 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | /* Free entry freelists */ |
| 194 | vnet_classify_entry_t **freelists; |
| 195 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | /* Writer (only) lock for this table */ |
jaszha03 | 5cdde5c | 2019-07-11 20:47:24 +0000 | [diff] [blame] | 197 | clib_spinlock_t writer_lock; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 198 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | } vnet_classify_table_t; |
| 200 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 201 | /** |
| 202 | * Ensure DP fields don't spill over to cache-line 2 |
| 203 | */ |
| 204 | STATIC_ASSERT_OFFSET_OF (vnet_classify_table_t, cacheline1, |
| 205 | CLIB_CACHE_LINE_BYTES); |
| 206 | |
| 207 | /** |
| 208 | * The vector size for the classifier |
| 209 | * in the add/del table 'match' is the number of vectors of this size |
| 210 | */ |
| 211 | #define VNET_CLASSIFY_VECTOR_SIZE \ |
| 212 | sizeof (((vnet_classify_table_t *) 0)->mask[0]) |
| 213 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 214 | struct _vnet_classify_main |
| 215 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | /* Table pool */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 217 | vnet_classify_table_t *tables; |
| 218 | |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 219 | /* Registered next-index, opaque unformat fcns */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 220 | unformat_function_t **unformat_l2_next_index_fns; |
| 221 | unformat_function_t **unformat_ip_next_index_fns; |
| 222 | unformat_function_t **unformat_acl_next_index_fns; |
| 223 | unformat_function_t **unformat_policer_next_index_fns; |
| 224 | unformat_function_t **unformat_opaque_index_fns; |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 225 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 226 | /* Per-interface filter table. [0] is used for pcap */ |
| 227 | u32 *classify_table_index_by_sw_if_index; |
Dave Barach | f5667c3 | 2019-09-25 11:27:46 -0400 | [diff] [blame] | 228 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | /* convenience variables */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 230 | vlib_main_t *vlib_main; |
| 231 | vnet_main_t *vnet_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | }; |
| 233 | |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 234 | extern vnet_classify_main_t vnet_classify_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 236 | u8 *format_classify_table (u8 * s, va_list * args); |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 237 | u8 *format_vnet_classify_table (u8 *s, va_list *args); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | |
| 239 | u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h); |
| 240 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 241 | static_always_inline vnet_classify_table_t * |
| 242 | vnet_classify_table_get (u32 table_index) |
| 243 | { |
| 244 | vnet_classify_main_t *vcm = &vnet_classify_main; |
| 245 | |
| 246 | return (pool_elt_at_index (vcm->tables, table_index)); |
| 247 | } |
| 248 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 249 | static inline u64 |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 250 | 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] | 251 | { |
Pierre Pfister | cb65630 | 2016-03-16 09:14:28 +0000 | [diff] [blame] | 252 | u32x4 *mask; |
| 253 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 254 | union |
| 255 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | u32x4 as_u32x4; |
| 257 | u64 as_u64[2]; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 258 | } xor_sum __attribute__ ((aligned (sizeof (u32x4)))); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 259 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 260 | ASSERT (t); |
Pierre Pfister | cb65630 | 2016-03-16 09:14:28 +0000 | [diff] [blame] | 261 | mask = t->mask; |
Adrian Oanca | 22ac59b | 2018-02-23 16:27:41 +0100 | [diff] [blame] | 262 | #ifdef CLIB_HAVE_VEC128 |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 263 | u32x4u *data = (u32x4u *) h; |
| 264 | xor_sum.as_u32x4 = data[0 + t->skip_n_vectors] & mask[0]; |
| 265 | switch (t->match_n_vectors) |
Pierre Pfister | cb65630 | 2016-03-16 09:14:28 +0000 | [diff] [blame] | 266 | { |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 267 | case 5: |
| 268 | xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4]; |
| 269 | /* FALLTHROUGH */ |
| 270 | case 4: |
| 271 | xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3]; |
| 272 | /* FALLTHROUGH */ |
| 273 | case 3: |
| 274 | xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2]; |
| 275 | /* FALLTHROUGH */ |
| 276 | case 2: |
| 277 | xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1]; |
| 278 | /* FALLTHROUGH */ |
| 279 | case 1: |
| 280 | break; |
| 281 | default: |
| 282 | abort (); |
Pierre Pfister | cb65630 | 2016-03-16 09:14:28 +0000 | [diff] [blame] | 283 | } |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 284 | #else |
| 285 | u32 skip_u64 = t->skip_n_vectors * 2; |
| 286 | u64 *data64 = (u64 *) h; |
| 287 | xor_sum.as_u64[0] = data64[0 + skip_u64] & ((u64 *) mask)[0]; |
| 288 | xor_sum.as_u64[1] = data64[1 + skip_u64] & ((u64 *) mask)[1]; |
| 289 | switch (t->match_n_vectors) |
| 290 | { |
| 291 | case 5: |
| 292 | xor_sum.as_u64[0] ^= data64[8 + skip_u64] & ((u64 *) mask)[8]; |
| 293 | xor_sum.as_u64[1] ^= data64[9 + skip_u64] & ((u64 *) mask)[9]; |
| 294 | /* FALLTHROUGH */ |
| 295 | case 4: |
| 296 | xor_sum.as_u64[0] ^= data64[6 + skip_u64] & ((u64 *) mask)[6]; |
| 297 | xor_sum.as_u64[1] ^= data64[7 + skip_u64] & ((u64 *) mask)[7]; |
| 298 | /* FALLTHROUGH */ |
| 299 | case 3: |
| 300 | xor_sum.as_u64[0] ^= data64[4 + skip_u64] & ((u64 *) mask)[4]; |
| 301 | xor_sum.as_u64[1] ^= data64[5 + skip_u64] & ((u64 *) mask)[5]; |
| 302 | /* FALLTHROUGH */ |
| 303 | case 2: |
| 304 | xor_sum.as_u64[0] ^= data64[2 + skip_u64] & ((u64 *) mask)[2]; |
| 305 | xor_sum.as_u64[1] ^= data64[3 + skip_u64] & ((u64 *) mask)[3]; |
| 306 | /* FALLTHROUGH */ |
| 307 | case 1: |
| 308 | break; |
| 309 | |
| 310 | default: |
| 311 | abort (); |
| 312 | } |
| 313 | #endif /* CLIB_HAVE_VEC128 */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 314 | |
Ray Kinsella | 7741afa | 2020-04-07 07:51:23 +0100 | [diff] [blame] | 315 | #ifdef clib_crc32c_uses_intrinsics |
| 316 | return clib_crc32c ((u8 *) & xor_sum, sizeof (xor_sum)); |
| 317 | #else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 318 | 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] | 319 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 320 | } |
| 321 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 322 | static inline void |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash) |
| 324 | { |
| 325 | u32 bucket_index; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 326 | |
| 327 | ASSERT (is_pow2 (t->nbuckets)); |
| 328 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | bucket_index = hash & (t->nbuckets - 1); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 330 | |
| 331 | CLIB_PREFETCH (&t->buckets[bucket_index], CLIB_CACHE_LINE_BYTES, LOAD); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | } |
| 333 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 334 | static inline vnet_classify_entry_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | vnet_classify_get_entry (vnet_classify_table_t * t, uword offset) |
| 336 | { |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 337 | u8 *hp = clib_mem_get_heap_base (t->mheap); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 338 | u8 *vp = hp + offset; |
| 339 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 340 | return (vnet_classify_entry_t *) vp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 341 | } |
| 342 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 343 | static inline uword |
| 344 | vnet_classify_get_offset (vnet_classify_table_t * t, |
| 345 | vnet_classify_entry_t * v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 346 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 347 | u8 *hp, *vp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 348 | |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 349 | hp = (u8 *) clib_mem_get_heap_base (t->mheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | vp = (u8 *) v; |
| 351 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 352 | ASSERT ((vp - hp) < 0x100000000ULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | return vp - hp; |
| 354 | } |
| 355 | |
| 356 | static inline vnet_classify_entry_t * |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 357 | vnet_classify_entry_at_index (vnet_classify_table_t * t, |
| 358 | vnet_classify_entry_t * e, u32 index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 360 | u8 *eu8; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 362 | eu8 = (u8 *) e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 363 | |
| 364 | eu8 += index * (sizeof (vnet_classify_entry_t) + |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 365 | (t->match_n_vectors * sizeof (u32x4))); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | |
| 367 | return (vnet_classify_entry_t *) eu8; |
| 368 | } |
| 369 | |
| 370 | static inline void |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 371 | vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 372 | { |
| 373 | u32 bucket_index; |
| 374 | u32 value_index; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 375 | vnet_classify_bucket_t *b; |
| 376 | vnet_classify_entry_t *e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 377 | |
| 378 | bucket_index = hash & (t->nbuckets - 1); |
| 379 | |
| 380 | b = &t->buckets[bucket_index]; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 381 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 382 | if (b->offset == 0) |
| 383 | return; |
| 384 | |
| 385 | hash >>= t->log2_nbuckets; |
| 386 | |
| 387 | e = vnet_classify_get_entry (t, b->offset); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 388 | value_index = hash & ((1 << b->log2_pages) - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | |
| 390 | e = vnet_classify_entry_at_index (t, e, value_index); |
| 391 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 392 | CLIB_PREFETCH (e, CLIB_CACHE_LINE_BYTES, LOAD); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 393 | } |
| 394 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 395 | vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t, |
| 396 | u8 * h, u64 hash, f64 now); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 397 | |
| 398 | static inline vnet_classify_entry_t * |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 399 | vnet_classify_find_entry_inline (vnet_classify_table_t *t, const u8 *h, |
| 400 | u64 hash, f64 now) |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 401 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 402 | vnet_classify_entry_t *v; |
Pierre Pfister | cb65630 | 2016-03-16 09:14:28 +0000 | [diff] [blame] | 403 | u32x4 *mask, *key; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 404 | union |
| 405 | { |
Pierre Pfister | cb65630 | 2016-03-16 09:14:28 +0000 | [diff] [blame] | 406 | u32x4 as_u32x4; |
| 407 | u64 as_u64[2]; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 408 | } result __attribute__ ((aligned (sizeof (u32x4)))); |
| 409 | vnet_classify_bucket_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | u32 value_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 411 | u32 bucket_index; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 412 | u32 limit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 413 | int i; |
| 414 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 415 | bucket_index = hash & (t->nbuckets - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 416 | b = &t->buckets[bucket_index]; |
Pierre Pfister | cb65630 | 2016-03-16 09:14:28 +0000 | [diff] [blame] | 417 | mask = t->mask; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | |
| 419 | if (b->offset == 0) |
| 420 | return 0; |
| 421 | |
| 422 | hash >>= t->log2_nbuckets; |
| 423 | |
| 424 | v = vnet_classify_get_entry (t, b->offset); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 425 | value_index = hash & ((1 << b->log2_pages) - 1); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 426 | limit = t->entries_per_page; |
| 427 | if (PREDICT_FALSE (b->linear_search)) |
| 428 | { |
| 429 | value_index = 0; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 430 | limit *= (1 << b->log2_pages); |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 433 | v = vnet_classify_entry_at_index (t, v, value_index); |
| 434 | |
Adrian Oanca | 22ac59b | 2018-02-23 16:27:41 +0100 | [diff] [blame] | 435 | #ifdef CLIB_HAVE_VEC128 |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 436 | const u32x4u *data = (const u32x4u *) h; |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 437 | for (i = 0; i < limit; i++) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 438 | { |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 439 | key = v->key; |
| 440 | result.as_u32x4 = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0]; |
| 441 | switch (t->match_n_vectors) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 442 | { |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 443 | case 5: |
| 444 | result.as_u32x4 |= (data[4 + t->skip_n_vectors] & mask[4]) ^ key[4]; |
| 445 | /* FALLTHROUGH */ |
| 446 | case 4: |
| 447 | result.as_u32x4 |= (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3]; |
| 448 | /* FALLTHROUGH */ |
| 449 | case 3: |
| 450 | result.as_u32x4 |= (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2]; |
| 451 | /* FALLTHROUGH */ |
| 452 | case 2: |
| 453 | result.as_u32x4 |= (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1]; |
| 454 | /* FALLTHROUGH */ |
| 455 | case 1: |
| 456 | break; |
| 457 | default: |
| 458 | abort (); |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 459 | } |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 460 | |
Damjan Marion | 62ed543 | 2021-07-14 12:01:17 +0200 | [diff] [blame^] | 461 | if (u32x4_is_all_zero (result.as_u32x4)) |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 462 | { |
| 463 | if (PREDICT_TRUE (now)) |
| 464 | { |
| 465 | v->hits++; |
| 466 | v->last_heard = now; |
| 467 | } |
| 468 | return (v); |
| 469 | } |
| 470 | v = vnet_classify_entry_at_index (t, v, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | } |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 472 | #else |
| 473 | u32 skip_u64 = t->skip_n_vectors * 2; |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 474 | const u64 *data64 = (const u64 *) h; |
Damjan Marion | 8304933 | 2019-09-25 00:25:36 +0200 | [diff] [blame] | 475 | for (i = 0; i < limit; i++) |
| 476 | { |
| 477 | key = v->key; |
| 478 | |
| 479 | result.as_u64[0] = |
| 480 | (data64[0 + skip_u64] & ((u64 *) mask)[0]) ^ ((u64 *) key)[0]; |
| 481 | result.as_u64[1] = |
| 482 | (data64[1 + skip_u64] & ((u64 *) mask)[1]) ^ ((u64 *) key)[1]; |
| 483 | switch (t->match_n_vectors) |
| 484 | { |
| 485 | case 5: |
| 486 | result.as_u64[0] |= |
| 487 | (data64[8 + skip_u64] & ((u64 *) mask)[8]) ^ ((u64 *) key)[8]; |
| 488 | result.as_u64[1] |= |
| 489 | (data64[9 + skip_u64] & ((u64 *) mask)[9]) ^ ((u64 *) key)[9]; |
| 490 | /* FALLTHROUGH */ |
| 491 | case 4: |
| 492 | result.as_u64[0] |= |
| 493 | (data64[6 + skip_u64] & ((u64 *) mask)[6]) ^ ((u64 *) key)[6]; |
| 494 | result.as_u64[1] |= |
| 495 | (data64[7 + skip_u64] & ((u64 *) mask)[7]) ^ ((u64 *) key)[7]; |
| 496 | /* FALLTHROUGH */ |
| 497 | case 3: |
| 498 | result.as_u64[0] |= |
| 499 | (data64[4 + skip_u64] & ((u64 *) mask)[4]) ^ ((u64 *) key)[4]; |
| 500 | result.as_u64[1] |= |
| 501 | (data64[5 + skip_u64] & ((u64 *) mask)[5]) ^ ((u64 *) key)[5]; |
| 502 | /* FALLTHROUGH */ |
| 503 | case 2: |
| 504 | result.as_u64[0] |= |
| 505 | (data64[2 + skip_u64] & ((u64 *) mask)[2]) ^ ((u64 *) key)[2]; |
| 506 | result.as_u64[1] |= |
| 507 | (data64[3 + skip_u64] & ((u64 *) mask)[3]) ^ ((u64 *) key)[3]; |
| 508 | /* FALLTHROUGH */ |
| 509 | case 1: |
| 510 | break; |
| 511 | default: |
| 512 | abort (); |
| 513 | } |
| 514 | |
| 515 | if (result.as_u64[0] == 0 && result.as_u64[1] == 0) |
| 516 | { |
| 517 | if (PREDICT_TRUE (now)) |
| 518 | { |
| 519 | v->hits++; |
| 520 | v->last_heard = now; |
| 521 | } |
| 522 | return (v); |
| 523 | } |
| 524 | |
| 525 | v = vnet_classify_entry_at_index (t, v, 1); |
| 526 | } |
Adrian Oanca | 22ac59b | 2018-02-23 16:27:41 +0100 | [diff] [blame] | 527 | #endif /* CLIB_HAVE_VEC128 */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 528 | return 0; |
Dave Barach | cada2a0 | 2017-05-18 19:16:47 -0400 | [diff] [blame] | 529 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 530 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 531 | vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t *cm, |
| 532 | const u8 *mask, u32 nbuckets, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 533 | u32 memory_size, |
| 534 | u32 skip_n_vectors, |
| 535 | u32 match_n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 536 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 537 | int vnet_classify_add_del_session (vnet_classify_main_t *cm, u32 table_index, |
| 538 | const u8 *match, u32 hit_next_index, |
| 539 | u32 opaque_index, i32 advance, u8 action, |
| 540 | u16 metadata, int is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 541 | |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 542 | int vnet_classify_add_del_table (vnet_classify_main_t *cm, const u8 *mask, |
| 543 | u32 nbuckets, u32 memory_size, u32 skip, |
| 544 | u32 match, u32 next_table_index, |
| 545 | u32 miss_next_index, u32 *table_index, |
| 546 | u8 current_data_flag, i16 current_data_offset, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 547 | int is_add, int del_chain); |
Neale Ranns | 1441a6c | 2020-12-14 16:02:17 +0000 | [diff] [blame] | 548 | void vnet_classify_delete_table_index (vnet_classify_main_t *cm, |
| 549 | u32 table_index, int del_chain); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 550 | |
| 551 | unformat_function_t unformat_ip4_mask; |
| 552 | unformat_function_t unformat_ip6_mask; |
| 553 | unformat_function_t unformat_l3_mask; |
| 554 | unformat_function_t unformat_l2_mask; |
| 555 | unformat_function_t unformat_classify_mask; |
| 556 | unformat_function_t unformat_l2_next_index; |
| 557 | unformat_function_t unformat_ip_next_index; |
| 558 | unformat_function_t unformat_ip4_match; |
| 559 | unformat_function_t unformat_ip6_match; |
| 560 | unformat_function_t unformat_l3_match; |
Dave Barach | 4a3f69c | 2017-02-22 12:44:56 -0500 | [diff] [blame] | 561 | unformat_function_t unformat_l4_match; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 562 | unformat_function_t unformat_vlan_tag; |
| 563 | unformat_function_t unformat_l2_match; |
| 564 | unformat_function_t unformat_classify_match; |
| 565 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 566 | void vnet_classify_register_unformat_ip_next_index_fn |
| 567 | (unformat_function_t * fn); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 568 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 569 | void vnet_classify_register_unformat_l2_next_index_fn |
| 570 | (unformat_function_t * fn); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 571 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 572 | void vnet_classify_register_unformat_acl_next_index_fn |
| 573 | (unformat_function_t * fn); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 574 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 575 | void vnet_classify_register_unformat_policer_next_index_fn |
| 576 | (unformat_function_t * fn); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 577 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 578 | void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t * |
| 579 | fn); |
Dave Barach | f39ff74 | 2016-03-20 10:14:45 -0400 | [diff] [blame] | 580 | |
Jon Loeliger | 5c1e48c | 2020-10-15 14:41:36 -0400 | [diff] [blame] | 581 | u32 classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index); |
| 582 | void classify_set_pcap_chain (vnet_classify_main_t * cm, |
| 583 | u32 sw_if_index, u32 table_index); |
| 584 | |
| 585 | u32 classify_get_trace_chain (void); |
| 586 | void classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index); |
| 587 | |
| 588 | u32 classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index); |
| 589 | u32 classify_lookup_chain (u32 table_index, |
| 590 | u8 * mask, u32 n_skip, u32 n_match); |
| 591 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 592 | #endif /* __included_vnet_classify_h__ */ |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 593 | |
| 594 | /* |
| 595 | * fd.io coding-style-patch-verification: ON |
| 596 | * |
| 597 | * Local Variables: |
| 598 | * eval: (c-set-style "gnu") |
| 599 | * End: |
| 600 | */ |