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