blob: 143833dfb20ecee01d5649a87f17127586d98884 [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
92 /* Really only need 1 bit */
Steve Shin25e26dc2016-11-08 10:47:10 -080093 u8 flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094#define VNET_CLASSIFY_ENTRY_FREE (1<<0)
95
Neale Ranns13eaf3e2017-05-23 06:10:33 -070096 vnet_classify_action_t action;
Steve Shin25e26dc2016-11-08 10:47:10 -080097 u16 metadata;
Neale Ranns1441a6c2020-12-14 16:02:17 +000098 /* Graph node next index */
99 u32 next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
101 /* Must be aligned to a 16-octet boundary */
102 u32x4 key[0];
Neale Ranns1441a6c2020-12-14 16:02:17 +0000103} vnet_classify_entry_t;
104
105/**
106 * Check there's no padding in the entry. the key lies on a 16 byte boundary.
107 */
108STATIC_ASSERT_OFFSET_OF (vnet_classify_entry_t, key, 32);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530110static inline int
111vnet_classify_entry_is_free (vnet_classify_entry_t * e)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112{
113 return e->flags & VNET_CLASSIFY_ENTRY_FREE;
114}
115
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530116static inline int
117vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118{
119 return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
120}
121
122/* Need these to con the vector allocator */
Neale Ranns1441a6c2020-12-14 16:02:17 +0000123#define _(size) \
124 typedef struct \
125 { \
126 vnet_classify_entry_t e; \
127 u32x4 key[size]; \
128 } __clib_packed vnet_classify_entry_##size##_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129foreach_size_in_u32x4;
130#undef _
131
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530132typedef struct
133{
134 union
135 {
136 struct
137 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 u32 offset;
Dave Barachcada2a02017-05-18 19:16:47 -0400139 u8 linear_search;
140 u8 pad[2];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 u8 log2_pages;
142 };
143 u64 as_u64;
144 };
145} vnet_classify_bucket_t;
146
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530147typedef struct
148{
David Johnsond9818dd2018-12-14 14:53:41 -0500149 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Neale Ranns1441a6c2020-12-14 16:02:17 +0000150 /* hash Buckets */
151 vnet_classify_bucket_t *buckets;
152
153 /* Private allocation arena, protected by the writer lock,
154 * where the entries are stored. */
155 void *mheap;
156
157 /* User/client data associated with the table */
158 uword user_ctx;
159
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 u32 nbuckets;
161 u32 log2_nbuckets;
Neale Ranns1441a6c2020-12-14 16:02:17 +0000162 u32 entries_per_page;
163 u32 skip_n_vectors;
164 u32 match_n_vectors;
Damjan Marion4dc098f2021-09-22 15:28:29 +0200165 u16 load_mask;
Neale Ranns1441a6c2020-12-14 16:02:17 +0000166
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 /* Index of next table to try */
168 u32 next_table_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530169
Neale Ranns1441a6c2020-12-14 16:02:17 +0000170 /* packet offsets */
171 i16 current_data_offset;
172 vnet_classify_flags_t current_data_flag;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173 /* Miss next index, return if next_table_index = 0 */
174 u32 miss_next_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530175
Neale Ranns1441a6c2020-12-14 16:02:17 +0000176 /**
177 * All members accessed in the DP above here
178 */
179 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
180
181 /* Config parameters */
182 u32 linear_buckets;
183 u32 active_elements;
184 u32 data_offset;
185
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 /* Per-bucket working copies, one per thread */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530187 vnet_classify_entry_t **working_copies;
Dave Barachcada2a02017-05-18 19:16:47 -0400188 int *working_copy_lengths;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189 vnet_classify_bucket_t saved_bucket;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530190
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191 /* Free entry freelists */
192 vnet_classify_entry_t **freelists;
193
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 /* Writer (only) lock for this table */
jaszha035cdde5c2019-07-11 20:47:24 +0000195 clib_spinlock_t writer_lock;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530196
Damjan Marion3bb2da92021-09-20 13:39:37 +0200197 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
198 /* Mask to apply after skipping N vectors */
Damjan Marion55c8fa42021-10-31 19:53:27 +0100199 union
200 {
201 u32x4 mask[8];
202 u32 mask_u32[32];
203 };
Damjan Marion3bb2da92021-09-20 13:39:37 +0200204
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205} vnet_classify_table_t;
206
Neale Ranns1441a6c2020-12-14 16:02:17 +0000207/**
208 * Ensure DP fields don't spill over to cache-line 2
209 */
210STATIC_ASSERT_OFFSET_OF (vnet_classify_table_t, cacheline1,
211 CLIB_CACHE_LINE_BYTES);
212
213/**
214 * The vector size for the classifier
215 * in the add/del table 'match' is the number of vectors of this size
216 */
217#define VNET_CLASSIFY_VECTOR_SIZE \
218 sizeof (((vnet_classify_table_t *) 0)->mask[0])
219
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530220struct _vnet_classify_main
221{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222 /* Table pool */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530223 vnet_classify_table_t *tables;
224
Dave Barachf39ff742016-03-20 10:14:45 -0400225 /* Registered next-index, opaque unformat fcns */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530226 unformat_function_t **unformat_l2_next_index_fns;
227 unformat_function_t **unformat_ip_next_index_fns;
228 unformat_function_t **unformat_acl_next_index_fns;
229 unformat_function_t **unformat_policer_next_index_fns;
230 unformat_function_t **unformat_opaque_index_fns;
Dave Barachf39ff742016-03-20 10:14:45 -0400231
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400232 /* Per-interface filter table. [0] is used for pcap */
233 u32 *classify_table_index_by_sw_if_index;
Dave Barachf5667c32019-09-25 11:27:46 -0400234
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235 /* convenience variables */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530236 vlib_main_t *vlib_main;
237 vnet_main_t *vnet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238};
239
Dave Barachf39ff742016-03-20 10:14:45 -0400240extern vnet_classify_main_t vnet_classify_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
Benoît Gannec629f902022-06-08 10:56:33 +0200242u8 *format_classify_entry (u8 *s, va_list *args);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530243u8 *format_classify_table (u8 * s, va_list * args);
Neale Ranns1441a6c2020-12-14 16:02:17 +0000244u8 *format_vnet_classify_table (u8 *s, va_list *args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
Benoît Ganneb03eec92022-06-08 10:49:17 +0200246u32 vnet_classify_hash_packet (const vnet_classify_table_t *t, u8 *h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Neale Ranns1441a6c2020-12-14 16:02:17 +0000248static_always_inline vnet_classify_table_t *
249vnet_classify_table_get (u32 table_index)
250{
251 vnet_classify_main_t *vcm = &vnet_classify_main;
252
253 return (pool_elt_at_index (vcm->tables, table_index));
254}
255
Benoît Ganneb03eec92022-06-08 10:49:17 +0200256static inline u32
257vnet_classify_hash_packet_inline (const vnet_classify_table_t *t, const u8 *h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258{
Damjan Marionc3b62d12021-09-20 17:51:31 +0200259 u64 xor_sum;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530260 ASSERT (t);
Damjan Marionc3b62d12021-09-20 17:51:31 +0200261 h += t->skip_n_vectors * 16;
262
Damjan Marion4dc098f2021-09-22 15:28:29 +0200263#if defined(CLIB_HAVE_VEC512) && defined(CLIB_HAVE_VEC512_MASK_LOAD_STORE)
264 u64x8 xor_sum_x8, *mask = (u64x8 *) t->mask;
265 u16 load_mask = t->load_mask;
266 u64x8u *data = (u64x8u *) h;
267
268 xor_sum_x8 = u64x8_mask_load_zero (data, load_mask) & mask[0];
269
270 if (PREDICT_FALSE (load_mask >> 8))
271 xor_sum_x8 ^= u64x8_mask_load_zero (data + 1, load_mask >> 8) & mask[1];
272
273 xor_sum_x8 ^= u64x8_align_right (xor_sum_x8, xor_sum_x8, 4);
274 xor_sum_x8 ^= u64x8_align_right (xor_sum_x8, xor_sum_x8, 2);
275 xor_sum = xor_sum_x8[0] ^ xor_sum_x8[1];
276#elif defined(CLIB_HAVE_VEC256) && defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
277 u64x4 xor_sum_x4, *mask = (u64x4 *) t->mask;
278 u16 load_mask = t->load_mask;
279 u64x4u *data = (u64x4u *) h;
280
281 xor_sum_x4 = u64x4_mask_load_zero (data, load_mask) & mask[0];
282 xor_sum_x4 ^= u64x4_mask_load_zero (data + 1, load_mask >> 4) & mask[1];
283
284 if (PREDICT_FALSE (load_mask >> 8))
285 xor_sum_x4 ^= u64x4_mask_load_zero (data + 2, load_mask >> 8) & mask[2];
286
287 xor_sum_x4 ^= u64x4_align_right (xor_sum_x4, xor_sum_x4, 2);
288 xor_sum = xor_sum_x4[0] ^ xor_sum_x4[1];
289#elif defined(CLIB_HAVE_VEC128)
Damjan Marionc3b62d12021-09-20 17:51:31 +0200290 u64x2 *mask = (u64x2 *) t->mask;
291 u64x2u *data = (u64x2u *) h;
292 u64x2 xor_sum_x2;
293
294 xor_sum_x2 = data[0] & mask[0];
295
Damjan Marion83049332019-09-25 00:25:36 +0200296 switch (t->match_n_vectors)
Pierre Pfistercb656302016-03-16 09:14:28 +0000297 {
Damjan Marion83049332019-09-25 00:25:36 +0200298 case 5:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200299 xor_sum_x2 ^= data[4] & mask[4];
Damjan Marion83049332019-09-25 00:25:36 +0200300 /* FALLTHROUGH */
301 case 4:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200302 xor_sum_x2 ^= data[3] & mask[3];
Damjan Marion83049332019-09-25 00:25:36 +0200303 /* FALLTHROUGH */
304 case 3:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200305 xor_sum_x2 ^= data[2] & mask[2];
Damjan Marion83049332019-09-25 00:25:36 +0200306 /* FALLTHROUGH */
307 case 2:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200308 xor_sum_x2 ^= data[1] & mask[1];
Damjan Marion83049332019-09-25 00:25:36 +0200309 /* FALLTHROUGH */
310 case 1:
311 break;
312 default:
313 abort ();
Pierre Pfistercb656302016-03-16 09:14:28 +0000314 }
Damjan Marionc3b62d12021-09-20 17:51:31 +0200315 xor_sum = xor_sum_x2[0] ^ xor_sum_x2[1];
Damjan Marion83049332019-09-25 00:25:36 +0200316#else
Damjan Marionc3b62d12021-09-20 17:51:31 +0200317 u64 *data = (u64 *) h;
318 u64 *mask = (u64 *) t->mask;
319
320 xor_sum = (data[0] & mask[0]) ^ (data[1] & mask[1]);
321
Damjan Marion83049332019-09-25 00:25:36 +0200322 switch (t->match_n_vectors)
323 {
324 case 5:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200325 xor_sum ^= (data[8] & mask[8]) ^ (data[9] & mask[9]);
Damjan Marion83049332019-09-25 00:25:36 +0200326 /* FALLTHROUGH */
327 case 4:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200328 xor_sum ^= (data[6] & mask[6]) ^ (data[7] & mask[7]);
Damjan Marion83049332019-09-25 00:25:36 +0200329 /* FALLTHROUGH */
330 case 3:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200331 xor_sum ^= (data[4] & mask[4]) ^ (data[5] & mask[5]);
Damjan Marion83049332019-09-25 00:25:36 +0200332 /* FALLTHROUGH */
333 case 2:
Damjan Marionc3b62d12021-09-20 17:51:31 +0200334 xor_sum ^= (data[2] & mask[2]) ^ (data[3] & mask[3]);
Damjan Marion83049332019-09-25 00:25:36 +0200335 /* FALLTHROUGH */
336 case 1:
337 break;
338
339 default:
340 abort ();
341 }
342#endif /* CLIB_HAVE_VEC128 */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530343
Ray Kinsella7741afa2020-04-07 07:51:23 +0100344#ifdef clib_crc32c_uses_intrinsics
345 return clib_crc32c ((u8 *) & xor_sum, sizeof (xor_sum));
346#else
Damjan Marion55c8fa42021-10-31 19:53:27 +0100347 return clib_xxhash (xor_sum);
Ray Kinsella7741afa2020-04-07 07:51:23 +0100348#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349}
350
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530351static inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
353{
354 u32 bucket_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530355
356 ASSERT (is_pow2 (t->nbuckets));
357
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 bucket_index = hash & (t->nbuckets - 1);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530359
Damjan Marionaf7fb042021-07-15 11:54:41 +0200360 clib_prefetch_load (&t->buckets[bucket_index]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361}
362
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530363static inline vnet_classify_entry_t *
Benoît Ganneb03eec92022-06-08 10:49:17 +0200364vnet_classify_get_entry (const vnet_classify_table_t *t, uword offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365{
Damjan Marion4537c302020-09-28 19:03:37 +0200366 u8 *hp = clib_mem_get_heap_base (t->mheap);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530367 u8 *vp = hp + offset;
368
Neale Ranns1441a6c2020-12-14 16:02:17 +0000369 return (vnet_classify_entry_t *) vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370}
371
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530372static inline uword
373vnet_classify_get_offset (vnet_classify_table_t * t,
374 vnet_classify_entry_t * v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530376 u8 *hp, *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377
Damjan Marion4537c302020-09-28 19:03:37 +0200378 hp = (u8 *) clib_mem_get_heap_base (t->mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379 vp = (u8 *) v;
380
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530381 ASSERT ((vp - hp) < 0x100000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382 return vp - hp;
383}
384
385static inline vnet_classify_entry_t *
Benoît Ganneb03eec92022-06-08 10:49:17 +0200386vnet_classify_entry_at_index (const vnet_classify_table_t *t,
387 vnet_classify_entry_t *e, u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530389 u8 *eu8;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530391 eu8 = (u8 *) e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
393 eu8 += index * (sizeof (vnet_classify_entry_t) +
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530394 (t->match_n_vectors * sizeof (u32x4)));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395
396 return (vnet_classify_entry_t *) eu8;
397}
398
399static inline void
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530400vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401{
402 u32 bucket_index;
403 u32 value_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530404 vnet_classify_bucket_t *b;
405 vnet_classify_entry_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406
407 bucket_index = hash & (t->nbuckets - 1);
408
409 b = &t->buckets[bucket_index];
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530410
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411 if (b->offset == 0)
412 return;
413
414 hash >>= t->log2_nbuckets;
415
416 e = vnet_classify_get_entry (t, b->offset);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530417 value_index = hash & ((1 << b->log2_pages) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
419 e = vnet_classify_entry_at_index (t, e, value_index);
420
Damjan Marionaf7fb042021-07-15 11:54:41 +0200421 clib_prefetch_load (e);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422}
423
Benoît Ganneb03eec92022-06-08 10:49:17 +0200424vnet_classify_entry_t *
425vnet_classify_find_entry (const vnet_classify_table_t *t, u8 *h, u32 hash,
426 f64 now);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
Damjan Marionc31a2342021-09-20 17:36:49 +0200428static_always_inline int
429vnet_classify_entry_is_equal (vnet_classify_entry_t *v, const u8 *d, u8 *m,
Damjan Marion26bc9f32021-09-22 15:50:35 +0200430 u32 match_n_vectors, u16 load_mask)
Damjan Marionc31a2342021-09-20 17:36:49 +0200431{
Damjan Marion26bc9f32021-09-22 15:50:35 +0200432#if defined(CLIB_HAVE_VEC512) && defined(CLIB_HAVE_VEC512_MASK_LOAD_STORE)
433 u64x8 r, *mask = (u64x8 *) m;
434 u64x8u *data = (u64x8u *) d;
Benoît Ganne89a561f2022-03-23 19:42:41 +0100435 u64x8 *key = (u64x8 *) v->key;
Damjan Marion26bc9f32021-09-22 15:50:35 +0200436
437 r = (u64x8_mask_load_zero (data, load_mask) & mask[0]) ^
438 u64x8_mask_load_zero (key, load_mask);
439 load_mask >>= 8;
440
441 if (PREDICT_FALSE (load_mask))
442 r |= (u64x8_mask_load_zero (data + 1, load_mask) & mask[1]) ^
443 u64x8_mask_load_zero (key + 1, load_mask);
444
445 if (u64x8_is_all_zero (r))
446 return 1;
447
448#elif defined(CLIB_HAVE_VEC256) && defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
449 u64x4 r, *mask = (u64x4 *) m;
450 u64x4u *data = (u64x4u *) d;
451 u64x4 *key = (u64x4 *) v->key;
452
453 r = (u64x4_mask_load_zero (data, load_mask) & mask[0]) ^
454 u64x4_mask_load_zero (key, load_mask);
455 load_mask >>= 4;
456
457 r |= (u64x4_mask_load_zero (data + 1, load_mask) & mask[1]) ^
458 u64x4_mask_load_zero (key + 1, load_mask);
459 load_mask >>= 4;
460
461 if (PREDICT_FALSE (load_mask))
462 r |= (u64x4_mask_load_zero (data + 2, load_mask) & mask[2]) ^
463 u64x4_mask_load_zero (key + 2, load_mask);
464
465 if (u64x4_is_all_zero (r))
466 return 1;
467
468#elif defined(CLIB_HAVE_VEC128)
Damjan Marionc31a2342021-09-20 17:36:49 +0200469 u64x2u *data = (u64x2 *) d;
470 u64x2 *key = (u64x2 *) v->key;
471 u64x2 *mask = (u64x2 *) m;
472 u64x2 r;
473
474 r = (data[0] & mask[0]) ^ key[0];
475 switch (match_n_vectors)
476 {
477 case 5:
478 r |= (data[4] & mask[4]) ^ key[4];
479 /* fall through */
480 case 4:
481 r |= (data[3] & mask[3]) ^ key[3];
482 /* fall through */
483 case 3:
484 r |= (data[2] & mask[2]) ^ key[2];
485 /* fall through */
486 case 2:
487 r |= (data[1] & mask[1]) ^ key[1];
488 /* fall through */
489 case 1:
490 break;
491 default:
492 abort ();
493 }
494
495 if (u64x2_is_all_zero (r))
496 return 1;
497
498#else
499 u64 *data = (u64 *) d;
500 u64 *key = (u64 *) v->key;
501 u64 *mask = (u64 *) m;
502 u64 r;
503
504 r = ((data[0] & mask[0]) ^ key[0]) | ((data[1] & mask[1]) ^ key[1]);
505 switch (match_n_vectors)
506 {
507 case 5:
508 r |= ((data[8] & mask[8]) ^ key[8]) | ((data[9] & mask[9]) ^ key[9]);
509 /* fall through */
510 case 4:
511 r |= ((data[6] & mask[6]) ^ key[6]) | ((data[7] & mask[7]) ^ key[7]);
512 /* fall through */
513 case 3:
514 r |= ((data[4] & mask[4]) ^ key[4]) | ((data[5] & mask[5]) ^ key[5]);
515 /* fall through */
516 case 2:
517 r |= ((data[2] & mask[2]) ^ key[2]) | ((data[3] & mask[3]) ^ key[3]);
518 /* fall through */
519 case 1:
520 break;
521 default:
522 abort ();
523 }
524
525 if (r == 0)
526 return 1;
527
528#endif /* CLIB_HAVE_VEC128 */
529 return 0;
530}
531
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532static inline vnet_classify_entry_t *
Benoît Ganneb03eec92022-06-08 10:49:17 +0200533vnet_classify_find_entry_inline (const vnet_classify_table_t *t, const u8 *h,
534 u32 hash, f64 now)
Dave Barachcada2a02017-05-18 19:16:47 -0400535{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530536 vnet_classify_entry_t *v;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530537 vnet_classify_bucket_t *b;
Damjan Marionc31a2342021-09-20 17:36:49 +0200538 u32 bucket_index, limit, pages, match_n_vectors = t->match_n_vectors;
Damjan Marion26bc9f32021-09-22 15:50:35 +0200539 u16 load_mask = t->load_mask;
Damjan Marionc31a2342021-09-20 17:36:49 +0200540 u8 *mask = (u8 *) t->mask;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541 int i;
542
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530543 bucket_index = hash & (t->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544 b = &t->buckets[bucket_index];
545
546 if (b->offset == 0)
547 return 0;
548
Damjan Mariona9f5f852021-09-20 17:12:03 +0200549 pages = 1 << b->log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550 v = vnet_classify_get_entry (t, b->offset);
Dave Barachcada2a02017-05-18 19:16:47 -0400551 limit = t->entries_per_page;
552 if (PREDICT_FALSE (b->linear_search))
553 {
Damjan Mariona9f5f852021-09-20 17:12:03 +0200554 limit *= pages;
555 v = vnet_classify_entry_at_index (t, v, 0);
Dave Barachcada2a02017-05-18 19:16:47 -0400556 }
Damjan Mariona9f5f852021-09-20 17:12:03 +0200557 else
558 {
559 hash >>= t->log2_nbuckets;
560 v = vnet_classify_entry_at_index (t, v, hash & (pages - 1));
561 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562
Damjan Marionc31a2342021-09-20 17:36:49 +0200563 h += t->skip_n_vectors * 16;
564
Damjan Marion83049332019-09-25 00:25:36 +0200565 for (i = 0; i < limit; i++)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530566 {
Damjan Marion26bc9f32021-09-22 15:50:35 +0200567 if (vnet_classify_entry_is_equal (v, h, mask, match_n_vectors,
568 load_mask))
Damjan Marion83049332019-09-25 00:25:36 +0200569 {
570 if (PREDICT_TRUE (now))
571 {
572 v->hits++;
573 v->last_heard = now;
574 }
575 return (v);
576 }
577 v = vnet_classify_entry_at_index (t, v, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578 }
579 return 0;
Dave Barachcada2a02017-05-18 19:16:47 -0400580}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581
Neale Ranns1441a6c2020-12-14 16:02:17 +0000582vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t *cm,
583 const u8 *mask, u32 nbuckets,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530584 u32 memory_size,
585 u32 skip_n_vectors,
586 u32 match_n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587
Neale Ranns1441a6c2020-12-14 16:02:17 +0000588int vnet_classify_add_del_session (vnet_classify_main_t *cm, u32 table_index,
589 const u8 *match, u32 hit_next_index,
590 u32 opaque_index, i32 advance, u8 action,
591 u16 metadata, int is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592
Neale Ranns1441a6c2020-12-14 16:02:17 +0000593int vnet_classify_add_del_table (vnet_classify_main_t *cm, const u8 *mask,
594 u32 nbuckets, u32 memory_size, u32 skip,
595 u32 match, u32 next_table_index,
596 u32 miss_next_index, u32 *table_index,
597 u8 current_data_flag, i16 current_data_offset,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530598 int is_add, int del_chain);
Neale Ranns1441a6c2020-12-14 16:02:17 +0000599void vnet_classify_delete_table_index (vnet_classify_main_t *cm,
600 u32 table_index, int del_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601
602unformat_function_t unformat_ip4_mask;
603unformat_function_t unformat_ip6_mask;
604unformat_function_t unformat_l3_mask;
605unformat_function_t unformat_l2_mask;
606unformat_function_t unformat_classify_mask;
607unformat_function_t unformat_l2_next_index;
608unformat_function_t unformat_ip_next_index;
609unformat_function_t unformat_ip4_match;
610unformat_function_t unformat_ip6_match;
611unformat_function_t unformat_l3_match;
Dave Barach4a3f69c2017-02-22 12:44:56 -0500612unformat_function_t unformat_l4_match;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613unformat_function_t unformat_vlan_tag;
614unformat_function_t unformat_l2_match;
615unformat_function_t unformat_classify_match;
616
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530617void vnet_classify_register_unformat_ip_next_index_fn
618 (unformat_function_t * fn);
Dave Barachf39ff742016-03-20 10:14:45 -0400619
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530620void vnet_classify_register_unformat_l2_next_index_fn
621 (unformat_function_t * fn);
Dave Barachf39ff742016-03-20 10:14:45 -0400622
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530623void vnet_classify_register_unformat_acl_next_index_fn
624 (unformat_function_t * fn);
Dave Barachf39ff742016-03-20 10:14:45 -0400625
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530626void vnet_classify_register_unformat_policer_next_index_fn
627 (unformat_function_t * fn);
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700628
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530629void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
630 fn);
Dave Barachf39ff742016-03-20 10:14:45 -0400631
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400632u32 classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index);
633void classify_set_pcap_chain (vnet_classify_main_t * cm,
634 u32 sw_if_index, u32 table_index);
635
636u32 classify_get_trace_chain (void);
637void classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index);
638
639u32 classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index);
640u32 classify_lookup_chain (u32 table_index,
641 u8 * mask, u32 n_skip, u32 n_match);
642
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643#endif /* __included_vnet_classify_h__ */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530644
645/*
646 * fd.io coding-style-patch-verification: ON
647 *
648 * Local Variables:
649 * eval: (c-set-style "gnu")
650 * End:
651 */