blob: 768593c45afc19b52da731919e048f70570fcd09 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Warnickecb9cada2015-12-08 15:45:58 -070018#include <vnet/vnet.h>
khemendra kumard7bfa0e2017-11-27 15:15:53 +053019#include <vnet/api_errno.h> /* for API error numbers */
Ed Warnickecb9cada2015-12-08 15:45:58 -070020
21#include <vppinfra/error.h>
22#include <vppinfra/hash.h>
23#include <vppinfra/cache.h>
Ray Kinsella7741afa2020-04-07 07:51:23 +010024#include <vppinfra/crc32.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070025#include <vppinfra/xxhash.h>
26
Jean-Mickael Guerin8941ec22016-03-04 14:14:21 +010027extern vlib_node_registration_t ip4_classify_node;
28extern vlib_node_registration_t ip6_classify_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
30#define CLASSIFY_TRACE 0
31
Steve Shin25e26dc2016-11-08 10:47:10 -080032/*
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 */
Neale Ranns1441a6c2020-12-14 16:02:17 +000037typedef enum vnet_classify_flags_t_
38{
39 CLASSIFY_FLAG_NONE = 0,
40 CLASSIFY_FLAG_USE_CURR_DATA = (1 << 0),
41} __clib_packed vnet_classify_flags_t;
Steve Shin25e26dc2016-11-08 10:47:10 -080042
43/*
44 * Classify session action
45 * CLASSIFY_ACTION_SET_IP4_FIB_INDEX:
46 * - Classified IP packets will be looked up
47 * from the specified ipv4 fib table
48 * CLASSIFY_ACTION_SET_IP6_FIB_INDEX:
49 * - Classified IP packets will be looked up
50 * from the specified ipv6 fib table
51 */
Neale Ranns13eaf3e2017-05-23 06:10:33 -070052typedef enum vnet_classify_action_t_
53{
Neale Ranns1441a6c2020-12-14 16:02:17 +000054 CLASSIFY_ACTION_NONE = 0,
Neale Ranns13eaf3e2017-05-23 06:10:33 -070055 CLASSIFY_ACTION_SET_IP4_FIB_INDEX = 1,
56 CLASSIFY_ACTION_SET_IP6_FIB_INDEX = 2,
Dave Barach630a8e22017-11-18 06:58:34 -050057 CLASSIFY_ACTION_SET_METADATA = 3,
Neale Ranns1441a6c2020-12-14 16:02:17 +000058} __clib_packed vnet_classify_action_t;
Steve Shin25e26dc2016-11-08 10:47:10 -080059
Ed Warnickecb9cada2015-12-08 15:45:58 -070060struct _vnet_classify_main;
61typedef struct _vnet_classify_main vnet_classify_main_t;
62
63#define foreach_size_in_u32x4 \
64_(1) \
65_(2) \
66_(3) \
67_(4) \
68_(5)
69
Neale Ranns1441a6c2020-12-14 16:02:17 +000070typedef struct _vnet_classify_entry
71{
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 /* put into vnet_buffer(b)->l2_classfy.opaque_index */
Neale Ranns1441a6c2020-12-14 16:02:17 +000073 union
74 {
75 struct
76 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 u32 opaque_index;
78 /* advance on hit, note it's a signed quantity... */
79 i32 advance;
80 };
81 u64 opaque_count;
82 };
Neale Ranns1441a6c2020-12-14 16:02:17 +000083 /* Hit counter */
84 union
85 {
86 u64 hits;
87 struct _vnet_classify_entry *next_free;
88 };
89 /* last heard time */
90 f64 last_heard;
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
Benoît Gannebd9cde82022-12-01 15:58:36 +010092 u32 metadata;
93
94 /* Graph node next index */
95 u16 next_index;
96
97 vnet_classify_action_t action;
98
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 /* Really only need 1 bit */
Steve Shin25e26dc2016-11-08 10:47:10 -0800100 u8 flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101#define VNET_CLASSIFY_ENTRY_FREE (1<<0)
102
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 /* Must be aligned to a 16-octet boundary */
104 u32x4 key[0];
Neale Ranns1441a6c2020-12-14 16:02:17 +0000105} vnet_classify_entry_t;
106
107/**
108 * Check there's no padding in the entry. the key lies on a 16 byte boundary.
109 */
110STATIC_ASSERT_OFFSET_OF (vnet_classify_entry_t, key, 32);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530112static inline int
113vnet_classify_entry_is_free (vnet_classify_entry_t * e)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114{
115 return e->flags & VNET_CLASSIFY_ENTRY_FREE;
116}
117
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530118static inline int
119vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120{
121 return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
122}
123
124/* Need these to con the vector allocator */
Neale Ranns1441a6c2020-12-14 16:02:17 +0000125#define _(size) \
126 typedef struct \
127 { \
128 vnet_classify_entry_t e; \
129 u32x4 key[size]; \
130 } __clib_packed vnet_classify_entry_##size##_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131foreach_size_in_u32x4;
132#undef _
133
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530134typedef struct
135{
136 union
137 {
138 struct
139 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 u32 offset;
Dave Barachcada2a02017-05-18 19:16:47 -0400141 u8 linear_search;
142 u8 pad[2];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 u8 log2_pages;
144 };
145 u64 as_u64;
146 };
147} vnet_classify_bucket_t;
148
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530149typedef struct
150{
David Johnsond9818dd2018-12-14 14:53:41 -0500151 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Neale Ranns1441a6c2020-12-14 16:02:17 +0000152 /* hash Buckets */
153 vnet_classify_bucket_t *buckets;
154
155 /* Private allocation arena, protected by the writer lock,
156 * where the entries are stored. */
157 void *mheap;
158
159 /* User/client data associated with the table */
160 uword user_ctx;
161
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 u32 nbuckets;
163 u32 log2_nbuckets;
Neale Ranns1441a6c2020-12-14 16:02:17 +0000164 u32 entries_per_page;
165 u32 skip_n_vectors;
166 u32 match_n_vectors;
Damjan Marion4dc098f2021-09-22 15:28:29 +0200167 u16 load_mask;
Neale Ranns1441a6c2020-12-14 16:02:17 +0000168
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 /* Index of next table to try */
170 u32 next_table_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530171
Neale Ranns1441a6c2020-12-14 16:02:17 +0000172 /* packet offsets */
173 i16 current_data_offset;
174 vnet_classify_flags_t current_data_flag;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 /* Miss next index, return if next_table_index = 0 */
176 u32 miss_next_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530177
Neale Ranns1441a6c2020-12-14 16:02:17 +0000178 /**
179 * All members accessed in the DP above here
180 */
181 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
182
183 /* Config parameters */
184 u32 linear_buckets;
185 u32 active_elements;
186 u32 data_offset;
187
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188 /* Per-bucket working copies, one per thread */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530189 vnet_classify_entry_t **working_copies;
Dave Barachcada2a02017-05-18 19:16:47 -0400190 int *working_copy_lengths;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191 vnet_classify_bucket_t saved_bucket;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530192
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 /* Free entry freelists */
194 vnet_classify_entry_t **freelists;
195
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196 /* Writer (only) lock for this table */
jaszha035cdde5c2019-07-11 20:47:24 +0000197 clib_spinlock_t writer_lock;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530198
Damjan Marion3bb2da92021-09-20 13:39:37 +0200199 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
200 /* Mask to apply after skipping N vectors */
Damjan Marion55c8fa42021-10-31 19:53:27 +0100201 union
202 {
203 u32x4 mask[8];
204 u32 mask_u32[32];
205 };
Damjan Marion3bb2da92021-09-20 13:39:37 +0200206
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207} vnet_classify_table_t;
208
Neale Ranns1441a6c2020-12-14 16:02:17 +0000209/**
210 * Ensure DP fields don't spill over to cache-line 2
211 */
212STATIC_ASSERT_OFFSET_OF (vnet_classify_table_t, cacheline1,
213 CLIB_CACHE_LINE_BYTES);
214
215/**
216 * The vector size for the classifier
217 * in the add/del table 'match' is the number of vectors of this size
218 */
219#define VNET_CLASSIFY_VECTOR_SIZE \
220 sizeof (((vnet_classify_table_t *) 0)->mask[0])
221
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530222struct _vnet_classify_main
223{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224 /* Table pool */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530225 vnet_classify_table_t *tables;
226
Dave Barachf39ff742016-03-20 10:14:45 -0400227 /* Registered next-index, opaque unformat fcns */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530228 unformat_function_t **unformat_l2_next_index_fns;
229 unformat_function_t **unformat_ip_next_index_fns;
230 unformat_function_t **unformat_acl_next_index_fns;
231 unformat_function_t **unformat_policer_next_index_fns;
232 unformat_function_t **unformat_opaque_index_fns;
Dave Barachf39ff742016-03-20 10:14:45 -0400233
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400234 /* Per-interface filter table. [0] is used for pcap */
235 u32 *classify_table_index_by_sw_if_index;
Dave Barachf5667c32019-09-25 11:27:46 -0400236
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 /* convenience variables */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530238 vlib_main_t *vlib_main;
239 vnet_main_t *vnet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240};
241
Dave Barachf39ff742016-03-20 10:14:45 -0400242extern vnet_classify_main_t vnet_classify_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243
Benoît Gannec629f902022-06-08 10:56:33 +0200244u8 *format_classify_entry (u8 *s, va_list *args);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530245u8 *format_classify_table (u8 * s, va_list * args);
Neale Ranns1441a6c2020-12-14 16:02:17 +0000246u8 *format_vnet_classify_table (u8 *s, va_list *args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Benoît Ganneb03eec92022-06-08 10:49:17 +0200248u32 vnet_classify_hash_packet (const vnet_classify_table_t *t, u8 *h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
Neale Ranns1441a6c2020-12-14 16:02:17 +0000250static_always_inline vnet_classify_table_t *
251vnet_classify_table_get (u32 table_index)
252{
253 vnet_classify_main_t *vcm = &vnet_classify_main;
254
255 return (pool_elt_at_index (vcm->tables, table_index));
256}
257
Benoît Ganneb03eec92022-06-08 10:49:17 +0200258static inline u32
259vnet_classify_hash_packet_inline (const vnet_classify_table_t *t, const u8 *h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260{
Damjan Marionc3b62d12021-09-20 17:51:31 +0200261 u64 xor_sum;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530262 ASSERT (t);
Damjan Marionc3b62d12021-09-20 17:51:31 +0200263 h += t->skip_n_vectors * 16;
264
Damjan Marion4dc098f2021-09-22 15:28:29 +0200265#if defined(CLIB_HAVE_VEC512) && defined(CLIB_HAVE_VEC512_MASK_LOAD_STORE)
266 u64x8 xor_sum_x8, *mask = (u64x8 *) t->mask;
267 u16 load_mask = t->load_mask;
268 u64x8u *data = (u64x8u *) h;
269
270 xor_sum_x8 = u64x8_mask_load_zero (data, load_mask) & mask[0];
271
272 if (PREDICT_FALSE (load_mask >> 8))
273 xor_sum_x8 ^= u64x8_mask_load_zero (data + 1, load_mask >> 8) & mask[1];
274
275 xor_sum_x8 ^= u64x8_align_right (xor_sum_x8, xor_sum_x8, 4);
276 xor_sum_x8 ^= u64x8_align_right (xor_sum_x8, xor_sum_x8, 2);
277 xor_sum = xor_sum_x8[0] ^ xor_sum_x8[1];
278#elif defined(CLIB_HAVE_VEC256) && defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
279 u64x4 xor_sum_x4, *mask = (u64x4 *) t->mask;
280 u16 load_mask = t->load_mask;
281 u64x4u *data = (u64x4u *) h;
282
283 xor_sum_x4 = u64x4_mask_load_zero (data, load_mask) & mask[0];
284 xor_sum_x4 ^= u64x4_mask_load_zero (data + 1, load_mask >> 4) & mask[1];
285
286 if (PREDICT_FALSE (load_mask >> 8))
287 xor_sum_x4 ^= u64x4_mask_load_zero (data + 2, load_mask >> 8) & mask[2];
288
289 xor_sum_x4 ^= u64x4_align_right (xor_sum_x4, xor_sum_x4, 2);
290 xor_sum = xor_sum_x4[0] ^ xor_sum_x4[1];
291#elif defined(CLIB_HAVE_VEC128)
Damjan Marionc3b62d12021-09-20 17:51:31 +0200292 u64x2 *mask = (u64x2 *) t->mask;
293 u64x2u *data = (u64x2u *) h;
294 u64x2 xor_sum_x2;
295
296 xor_sum_x2 = data[0] & mask[0];
297
Damjan Marion83049332019-09-25 00:25:36 +0200298 switch (t->match_n_vectors)
Pierre Pfistercb656302016-03-16 09:14:28 +0000299 {
Damjan Marion83049332019-09-25 00:25:36 +0200300 case 5:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200301 xor_sum_x2 ^= data[4] & mask[4];
Damjan Marion83049332019-09-25 00:25:36 +0200302 /* FALLTHROUGH */
303 case 4:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200304 xor_sum_x2 ^= data[3] & mask[3];
Damjan Marion83049332019-09-25 00:25:36 +0200305 /* FALLTHROUGH */
306 case 3:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200307 xor_sum_x2 ^= data[2] & mask[2];
Damjan Marion83049332019-09-25 00:25:36 +0200308 /* FALLTHROUGH */
309 case 2:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200310 xor_sum_x2 ^= data[1] & mask[1];
Damjan Marion83049332019-09-25 00:25:36 +0200311 /* FALLTHROUGH */
312 case 1:
313 break;
314 default:
315 abort ();
Pierre Pfistercb656302016-03-16 09:14:28 +0000316 }
Damjan Marionc3b62d12021-09-20 17:51:31 +0200317 xor_sum = xor_sum_x2[0] ^ xor_sum_x2[1];
Damjan Marion83049332019-09-25 00:25:36 +0200318#else
Damjan Marionc3b62d12021-09-20 17:51:31 +0200319 u64 *data = (u64 *) h;
320 u64 *mask = (u64 *) t->mask;
321
322 xor_sum = (data[0] & mask[0]) ^ (data[1] & mask[1]);
323
Damjan Marion83049332019-09-25 00:25:36 +0200324 switch (t->match_n_vectors)
325 {
326 case 5:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200327 xor_sum ^= (data[8] & mask[8]) ^ (data[9] & mask[9]);
Damjan Marion83049332019-09-25 00:25:36 +0200328 /* FALLTHROUGH */
329 case 4:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200330 xor_sum ^= (data[6] & mask[6]) ^ (data[7] & mask[7]);
Damjan Marion83049332019-09-25 00:25:36 +0200331 /* FALLTHROUGH */
332 case 3:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200333 xor_sum ^= (data[4] & mask[4]) ^ (data[5] & mask[5]);
Damjan Marion83049332019-09-25 00:25:36 +0200334 /* FALLTHROUGH */
335 case 2:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200336 xor_sum ^= (data[2] & mask[2]) ^ (data[3] & mask[3]);
Damjan Marion83049332019-09-25 00:25:36 +0200337 /* FALLTHROUGH */
338 case 1:
339 break;
340
341 default:
342 abort ();
343 }
344#endif /* CLIB_HAVE_VEC128 */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530345
Ray Kinsella7741afa2020-04-07 07:51:23 +0100346#ifdef clib_crc32c_uses_intrinsics
347 return clib_crc32c ((u8 *) & xor_sum, sizeof (xor_sum));
348#else
Damjan Marion55c8fa42021-10-31 19:53:27 +0100349 return clib_xxhash (xor_sum);
Ray Kinsella7741afa2020-04-07 07:51:23 +0100350#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351}
352
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530353static inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
355{
356 u32 bucket_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530357
358 ASSERT (is_pow2 (t->nbuckets));
359
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360 bucket_index = hash & (t->nbuckets - 1);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530361
Damjan Marionaf7fb042021-07-15 11:54:41 +0200362 clib_prefetch_load (&t->buckets[bucket_index]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363}
364
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530365static inline vnet_classify_entry_t *
Benoît Ganneb03eec92022-06-08 10:49:17 +0200366vnet_classify_get_entry (const vnet_classify_table_t *t, uword offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367{
Damjan Marion4537c302020-09-28 19:03:37 +0200368 u8 *hp = clib_mem_get_heap_base (t->mheap);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530369 u8 *vp = hp + offset;
370
Neale Ranns1441a6c2020-12-14 16:02:17 +0000371 return (vnet_classify_entry_t *) vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372}
373
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530374static inline uword
375vnet_classify_get_offset (vnet_classify_table_t * t,
376 vnet_classify_entry_t * v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530378 u8 *hp, *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379
Damjan Marion4537c302020-09-28 19:03:37 +0200380 hp = (u8 *) clib_mem_get_heap_base (t->mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381 vp = (u8 *) v;
382
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530383 ASSERT ((vp - hp) < 0x100000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384 return vp - hp;
385}
386
387static inline vnet_classify_entry_t *
Benoît Ganneb03eec92022-06-08 10:49:17 +0200388vnet_classify_entry_at_index (const vnet_classify_table_t *t,
389 vnet_classify_entry_t *e, u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530391 u8 *eu8;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530393 eu8 = (u8 *) e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
395 eu8 += index * (sizeof (vnet_classify_entry_t) +
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530396 (t->match_n_vectors * sizeof (u32x4)));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397
398 return (vnet_classify_entry_t *) eu8;
399}
400
401static inline void
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530402vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403{
404 u32 bucket_index;
405 u32 value_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530406 vnet_classify_bucket_t *b;
407 vnet_classify_entry_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
409 bucket_index = hash & (t->nbuckets - 1);
410
411 b = &t->buckets[bucket_index];
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530412
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 if (b->offset == 0)
414 return;
415
416 hash >>= t->log2_nbuckets;
417
418 e = vnet_classify_get_entry (t, b->offset);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530419 value_index = hash & ((1 << b->log2_pages) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420
421 e = vnet_classify_entry_at_index (t, e, value_index);
422
Damjan Marionaf7fb042021-07-15 11:54:41 +0200423 clib_prefetch_load (e);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424}
425
Benoît Ganneb03eec92022-06-08 10:49:17 +0200426vnet_classify_entry_t *
427vnet_classify_find_entry (const vnet_classify_table_t *t, u8 *h, u32 hash,
428 f64 now);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429
Damjan Marionc31a2342021-09-20 17:36:49 +0200430static_always_inline int
431vnet_classify_entry_is_equal (vnet_classify_entry_t *v, const u8 *d, u8 *m,
Damjan Marion26bc9f32021-09-22 15:50:35 +0200432 u32 match_n_vectors, u16 load_mask)
Damjan Marionc31a2342021-09-20 17:36:49 +0200433{
Damjan Marion26bc9f32021-09-22 15:50:35 +0200434#if defined(CLIB_HAVE_VEC512) && defined(CLIB_HAVE_VEC512_MASK_LOAD_STORE)
435 u64x8 r, *mask = (u64x8 *) m;
436 u64x8u *data = (u64x8u *) d;
Benoît Ganne89a561f2022-03-23 19:42:41 +0100437 u64x8 *key = (u64x8 *) v->key;
Damjan Marion26bc9f32021-09-22 15:50:35 +0200438
439 r = (u64x8_mask_load_zero (data, load_mask) & mask[0]) ^
440 u64x8_mask_load_zero (key, load_mask);
441 load_mask >>= 8;
442
443 if (PREDICT_FALSE (load_mask))
444 r |= (u64x8_mask_load_zero (data + 1, load_mask) & mask[1]) ^
445 u64x8_mask_load_zero (key + 1, load_mask);
446
447 if (u64x8_is_all_zero (r))
448 return 1;
449
450#elif defined(CLIB_HAVE_VEC256) && defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
451 u64x4 r, *mask = (u64x4 *) m;
452 u64x4u *data = (u64x4u *) d;
453 u64x4 *key = (u64x4 *) v->key;
454
455 r = (u64x4_mask_load_zero (data, load_mask) & mask[0]) ^
456 u64x4_mask_load_zero (key, load_mask);
457 load_mask >>= 4;
458
459 r |= (u64x4_mask_load_zero (data + 1, load_mask) & mask[1]) ^
460 u64x4_mask_load_zero (key + 1, load_mask);
461 load_mask >>= 4;
462
463 if (PREDICT_FALSE (load_mask))
464 r |= (u64x4_mask_load_zero (data + 2, load_mask) & mask[2]) ^
465 u64x4_mask_load_zero (key + 2, load_mask);
466
467 if (u64x4_is_all_zero (r))
468 return 1;
469
470#elif defined(CLIB_HAVE_VEC128)
Damjan Marionc31a2342021-09-20 17:36:49 +0200471 u64x2u *data = (u64x2 *) d;
472 u64x2 *key = (u64x2 *) v->key;
473 u64x2 *mask = (u64x2 *) m;
474 u64x2 r;
475
476 r = (data[0] & mask[0]) ^ key[0];
477 switch (match_n_vectors)
478 {
479 case 5:
480 r |= (data[4] & mask[4]) ^ key[4];
481 /* fall through */
482 case 4:
483 r |= (data[3] & mask[3]) ^ key[3];
484 /* fall through */
485 case 3:
486 r |= (data[2] & mask[2]) ^ key[2];
487 /* fall through */
488 case 2:
489 r |= (data[1] & mask[1]) ^ key[1];
490 /* fall through */
491 case 1:
492 break;
493 default:
494 abort ();
495 }
496
497 if (u64x2_is_all_zero (r))
498 return 1;
499
500#else
501 u64 *data = (u64 *) d;
502 u64 *key = (u64 *) v->key;
503 u64 *mask = (u64 *) m;
504 u64 r;
505
506 r = ((data[0] & mask[0]) ^ key[0]) | ((data[1] & mask[1]) ^ key[1]);
507 switch (match_n_vectors)
508 {
509 case 5:
510 r |= ((data[8] & mask[8]) ^ key[8]) | ((data[9] & mask[9]) ^ key[9]);
511 /* fall through */
512 case 4:
513 r |= ((data[6] & mask[6]) ^ key[6]) | ((data[7] & mask[7]) ^ key[7]);
514 /* fall through */
515 case 3:
516 r |= ((data[4] & mask[4]) ^ key[4]) | ((data[5] & mask[5]) ^ key[5]);
517 /* fall through */
518 case 2:
519 r |= ((data[2] & mask[2]) ^ key[2]) | ((data[3] & mask[3]) ^ key[3]);
520 /* fall through */
521 case 1:
522 break;
523 default:
524 abort ();
525 }
526
527 if (r == 0)
528 return 1;
529
530#endif /* CLIB_HAVE_VEC128 */
531 return 0;
532}
533
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534static inline vnet_classify_entry_t *
Benoît Ganneb03eec92022-06-08 10:49:17 +0200535vnet_classify_find_entry_inline (const vnet_classify_table_t *t, const u8 *h,
536 u32 hash, f64 now)
Dave Barachcada2a02017-05-18 19:16:47 -0400537{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530538 vnet_classify_entry_t *v;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530539 vnet_classify_bucket_t *b;
Damjan Marionc31a2342021-09-20 17:36:49 +0200540 u32 bucket_index, limit, pages, match_n_vectors = t->match_n_vectors;
Damjan Marion26bc9f32021-09-22 15:50:35 +0200541 u16 load_mask = t->load_mask;
Damjan Marionc31a2342021-09-20 17:36:49 +0200542 u8 *mask = (u8 *) t->mask;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 int i;
544
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530545 bucket_index = hash & (t->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546 b = &t->buckets[bucket_index];
547
548 if (b->offset == 0)
549 return 0;
550
Damjan Mariona9f5f852021-09-20 17:12:03 +0200551 pages = 1 << b->log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552 v = vnet_classify_get_entry (t, b->offset);
Dave Barachcada2a02017-05-18 19:16:47 -0400553 limit = t->entries_per_page;
554 if (PREDICT_FALSE (b->linear_search))
555 {
Damjan Mariona9f5f852021-09-20 17:12:03 +0200556 limit *= pages;
557 v = vnet_classify_entry_at_index (t, v, 0);
Dave Barachcada2a02017-05-18 19:16:47 -0400558 }
Damjan Mariona9f5f852021-09-20 17:12:03 +0200559 else
560 {
561 hash >>= t->log2_nbuckets;
562 v = vnet_classify_entry_at_index (t, v, hash & (pages - 1));
563 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564
Damjan Marionc31a2342021-09-20 17:36:49 +0200565 h += t->skip_n_vectors * 16;
566
Damjan Marion83049332019-09-25 00:25:36 +0200567 for (i = 0; i < limit; i++)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530568 {
Damjan Marion26bc9f32021-09-22 15:50:35 +0200569 if (vnet_classify_entry_is_equal (v, h, mask, match_n_vectors,
570 load_mask))
Damjan Marion83049332019-09-25 00:25:36 +0200571 {
572 if (PREDICT_TRUE (now))
573 {
574 v->hits++;
575 v->last_heard = now;
576 }
577 return (v);
578 }
579 v = vnet_classify_entry_at_index (t, v, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580 }
581 return 0;
Dave Barachcada2a02017-05-18 19:16:47 -0400582}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583
Neale Ranns1441a6c2020-12-14 16:02:17 +0000584vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t *cm,
585 const u8 *mask, u32 nbuckets,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530586 u32 memory_size,
587 u32 skip_n_vectors,
588 u32 match_n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589
Neale Ranns1441a6c2020-12-14 16:02:17 +0000590int vnet_classify_add_del_session (vnet_classify_main_t *cm, u32 table_index,
Benoît Gannebd9cde82022-12-01 15:58:36 +0100591 const u8 *match, u16 hit_next_index,
Neale Ranns1441a6c2020-12-14 16:02:17 +0000592 u32 opaque_index, i32 advance, u8 action,
Benoît Gannebd9cde82022-12-01 15:58:36 +0100593 u32 metadata, int is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594
Neale Ranns1441a6c2020-12-14 16:02:17 +0000595int vnet_classify_add_del_table (vnet_classify_main_t *cm, const u8 *mask,
596 u32 nbuckets, u32 memory_size, u32 skip,
597 u32 match, u32 next_table_index,
598 u32 miss_next_index, u32 *table_index,
599 u8 current_data_flag, i16 current_data_offset,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530600 int is_add, int del_chain);
Neale Ranns1441a6c2020-12-14 16:02:17 +0000601void vnet_classify_delete_table_index (vnet_classify_main_t *cm,
602 u32 table_index, int del_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603
604unformat_function_t unformat_ip4_mask;
605unformat_function_t unformat_ip6_mask;
606unformat_function_t unformat_l3_mask;
607unformat_function_t unformat_l2_mask;
608unformat_function_t unformat_classify_mask;
609unformat_function_t unformat_l2_next_index;
610unformat_function_t unformat_ip_next_index;
611unformat_function_t unformat_ip4_match;
612unformat_function_t unformat_ip6_match;
613unformat_function_t unformat_l3_match;
Dave Barach4a3f69c2017-02-22 12:44:56 -0500614unformat_function_t unformat_l4_match;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700615unformat_function_t unformat_vlan_tag;
616unformat_function_t unformat_l2_match;
617unformat_function_t unformat_classify_match;
618
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530619void vnet_classify_register_unformat_ip_next_index_fn
620 (unformat_function_t * fn);
Dave Barachf39ff742016-03-20 10:14:45 -0400621
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530622void vnet_classify_register_unformat_l2_next_index_fn
623 (unformat_function_t * fn);
Dave Barachf39ff742016-03-20 10:14:45 -0400624
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530625void vnet_classify_register_unformat_acl_next_index_fn
626 (unformat_function_t * fn);
Dave Barachf39ff742016-03-20 10:14:45 -0400627
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530628void vnet_classify_register_unformat_policer_next_index_fn
629 (unformat_function_t * fn);
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700630
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530631void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
632 fn);
Dave Barachf39ff742016-03-20 10:14:45 -0400633
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400634u32 classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index);
635void classify_set_pcap_chain (vnet_classify_main_t * cm,
636 u32 sw_if_index, u32 table_index);
637
638u32 classify_get_trace_chain (void);
639void classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index);
640
641u32 classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index);
642u32 classify_lookup_chain (u32 table_index,
643 u8 * mask, u32 n_skip, u32 n_match);
644
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645#endif /* __included_vnet_classify_h__ */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530646
647/*
648 * fd.io coding-style-patch-verification: ON
649 *
650 * Local Variables:
651 * eval: (c-set-style "gnu")
652 * End:
653 */