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