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