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