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