blob: 11b05308b199752a4b9fb068a8072e4b7bd15382 [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;
165
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 /* Index of next table to try */
167 u32 next_table_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530168
Neale Ranns1441a6c2020-12-14 16:02:17 +0000169 /* packet offsets */
170 i16 current_data_offset;
171 vnet_classify_flags_t current_data_flag;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 /* Miss next index, return if next_table_index = 0 */
173 u32 miss_next_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530174
Neale Ranns1441a6c2020-12-14 16:02:17 +0000175 /**
176 * All members accessed in the DP above here
177 */
178 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
179
180 /* Config parameters */
181 u32 linear_buckets;
182 u32 active_elements;
183 u32 data_offset;
184
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 /* Per-bucket working copies, one per thread */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530186 vnet_classify_entry_t **working_copies;
Dave Barachcada2a02017-05-18 19:16:47 -0400187 int *working_copy_lengths;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188 vnet_classify_bucket_t saved_bucket;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530189
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190 /* Free entry freelists */
191 vnet_classify_entry_t **freelists;
192
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 /* Writer (only) lock for this table */
jaszha035cdde5c2019-07-11 20:47:24 +0000194 clib_spinlock_t writer_lock;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530195
Damjan Marion3bb2da92021-09-20 13:39:37 +0200196 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
197 /* Mask to apply after skipping N vectors */
198 u32x4 mask[8];
199
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200} vnet_classify_table_t;
201
Neale Ranns1441a6c2020-12-14 16:02:17 +0000202/**
203 * Ensure DP fields don't spill over to cache-line 2
204 */
205STATIC_ASSERT_OFFSET_OF (vnet_classify_table_t, cacheline1,
206 CLIB_CACHE_LINE_BYTES);
207
208/**
209 * The vector size for the classifier
210 * in the add/del table 'match' is the number of vectors of this size
211 */
212#define VNET_CLASSIFY_VECTOR_SIZE \
213 sizeof (((vnet_classify_table_t *) 0)->mask[0])
214
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530215struct _vnet_classify_main
216{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217 /* Table pool */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530218 vnet_classify_table_t *tables;
219
Dave Barachf39ff742016-03-20 10:14:45 -0400220 /* Registered next-index, opaque unformat fcns */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530221 unformat_function_t **unformat_l2_next_index_fns;
222 unformat_function_t **unformat_ip_next_index_fns;
223 unformat_function_t **unformat_acl_next_index_fns;
224 unformat_function_t **unformat_policer_next_index_fns;
225 unformat_function_t **unformat_opaque_index_fns;
Dave Barachf39ff742016-03-20 10:14:45 -0400226
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400227 /* Per-interface filter table. [0] is used for pcap */
228 u32 *classify_table_index_by_sw_if_index;
Dave Barachf5667c32019-09-25 11:27:46 -0400229
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230 /* convenience variables */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530231 vlib_main_t *vlib_main;
232 vnet_main_t *vnet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233};
234
Dave Barachf39ff742016-03-20 10:14:45 -0400235extern vnet_classify_main_t vnet_classify_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530237u8 *format_classify_table (u8 * s, va_list * args);
Neale Ranns1441a6c2020-12-14 16:02:17 +0000238u8 *format_vnet_classify_table (u8 *s, va_list *args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239
240u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h);
241
Neale Ranns1441a6c2020-12-14 16:02:17 +0000242static_always_inline vnet_classify_table_t *
243vnet_classify_table_get (u32 table_index)
244{
245 vnet_classify_main_t *vcm = &vnet_classify_main;
246
247 return (pool_elt_at_index (vcm->tables, table_index));
248}
249
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530250static inline u64
Neale Ranns1441a6c2020-12-14 16:02:17 +0000251vnet_classify_hash_packet_inline (vnet_classify_table_t *t, const u8 *h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252{
Pierre Pfistercb656302016-03-16 09:14:28 +0000253 u32x4 *mask;
254
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530255 union
256 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257 u32x4 as_u32x4;
258 u64 as_u64[2];
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530259 } xor_sum __attribute__ ((aligned (sizeof (u32x4))));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530261 ASSERT (t);
Pierre Pfistercb656302016-03-16 09:14:28 +0000262 mask = t->mask;
Adrian Oanca22ac59b2018-02-23 16:27:41 +0100263#ifdef CLIB_HAVE_VEC128
Damjan Marion83049332019-09-25 00:25:36 +0200264 u32x4u *data = (u32x4u *) h;
265 xor_sum.as_u32x4 = data[0 + t->skip_n_vectors] & mask[0];
266 switch (t->match_n_vectors)
Pierre Pfistercb656302016-03-16 09:14:28 +0000267 {
Damjan Marion83049332019-09-25 00:25:36 +0200268 case 5:
269 xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4];
270 /* FALLTHROUGH */
271 case 4:
272 xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3];
273 /* FALLTHROUGH */
274 case 3:
275 xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2];
276 /* FALLTHROUGH */
277 case 2:
278 xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1];
279 /* FALLTHROUGH */
280 case 1:
281 break;
282 default:
283 abort ();
Pierre Pfistercb656302016-03-16 09:14:28 +0000284 }
Damjan Marion83049332019-09-25 00:25:36 +0200285#else
286 u32 skip_u64 = t->skip_n_vectors * 2;
287 u64 *data64 = (u64 *) h;
288 xor_sum.as_u64[0] = data64[0 + skip_u64] & ((u64 *) mask)[0];
289 xor_sum.as_u64[1] = data64[1 + skip_u64] & ((u64 *) mask)[1];
290 switch (t->match_n_vectors)
291 {
292 case 5:
293 xor_sum.as_u64[0] ^= data64[8 + skip_u64] & ((u64 *) mask)[8];
294 xor_sum.as_u64[1] ^= data64[9 + skip_u64] & ((u64 *) mask)[9];
295 /* FALLTHROUGH */
296 case 4:
297 xor_sum.as_u64[0] ^= data64[6 + skip_u64] & ((u64 *) mask)[6];
298 xor_sum.as_u64[1] ^= data64[7 + skip_u64] & ((u64 *) mask)[7];
299 /* FALLTHROUGH */
300 case 3:
301 xor_sum.as_u64[0] ^= data64[4 + skip_u64] & ((u64 *) mask)[4];
302 xor_sum.as_u64[1] ^= data64[5 + skip_u64] & ((u64 *) mask)[5];
303 /* FALLTHROUGH */
304 case 2:
305 xor_sum.as_u64[0] ^= data64[2 + skip_u64] & ((u64 *) mask)[2];
306 xor_sum.as_u64[1] ^= data64[3 + skip_u64] & ((u64 *) mask)[3];
307 /* FALLTHROUGH */
308 case 1:
309 break;
310
311 default:
312 abort ();
313 }
314#endif /* CLIB_HAVE_VEC128 */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530315
Ray Kinsella7741afa2020-04-07 07:51:23 +0100316#ifdef clib_crc32c_uses_intrinsics
317 return clib_crc32c ((u8 *) & xor_sum, sizeof (xor_sum));
318#else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319 return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
Ray Kinsella7741afa2020-04-07 07:51:23 +0100320#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321}
322
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530323static inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
325{
326 u32 bucket_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530327
328 ASSERT (is_pow2 (t->nbuckets));
329
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330 bucket_index = hash & (t->nbuckets - 1);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530331
Damjan Marionaf7fb042021-07-15 11:54:41 +0200332 clib_prefetch_load (&t->buckets[bucket_index]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333}
334
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530335static inline vnet_classify_entry_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336vnet_classify_get_entry (vnet_classify_table_t * t, uword offset)
337{
Damjan Marion4537c302020-09-28 19:03:37 +0200338 u8 *hp = clib_mem_get_heap_base (t->mheap);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530339 u8 *vp = hp + offset;
340
Neale Ranns1441a6c2020-12-14 16:02:17 +0000341 return (vnet_classify_entry_t *) vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342}
343
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530344static inline uword
345vnet_classify_get_offset (vnet_classify_table_t * t,
346 vnet_classify_entry_t * v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530348 u8 *hp, *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349
Damjan Marion4537c302020-09-28 19:03:37 +0200350 hp = (u8 *) clib_mem_get_heap_base (t->mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351 vp = (u8 *) v;
352
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530353 ASSERT ((vp - hp) < 0x100000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354 return vp - hp;
355}
356
357static inline vnet_classify_entry_t *
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530358vnet_classify_entry_at_index (vnet_classify_table_t * t,
359 vnet_classify_entry_t * e, u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530361 u8 *eu8;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530363 eu8 = (u8 *) e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364
365 eu8 += index * (sizeof (vnet_classify_entry_t) +
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530366 (t->match_n_vectors * sizeof (u32x4)));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367
368 return (vnet_classify_entry_t *) eu8;
369}
370
371static inline void
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530372vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373{
374 u32 bucket_index;
375 u32 value_index;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530376 vnet_classify_bucket_t *b;
377 vnet_classify_entry_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378
379 bucket_index = hash & (t->nbuckets - 1);
380
381 b = &t->buckets[bucket_index];
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530382
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 if (b->offset == 0)
384 return;
385
386 hash >>= t->log2_nbuckets;
387
388 e = vnet_classify_get_entry (t, b->offset);
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530389 value_index = hash & ((1 << b->log2_pages) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
391 e = vnet_classify_entry_at_index (t, e, value_index);
392
Damjan Marionaf7fb042021-07-15 11:54:41 +0200393 clib_prefetch_load (e);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394}
395
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530396vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t,
397 u8 * h, u64 hash, f64 now);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
399static inline vnet_classify_entry_t *
Neale Ranns1441a6c2020-12-14 16:02:17 +0000400vnet_classify_find_entry_inline (vnet_classify_table_t *t, const u8 *h,
401 u64 hash, f64 now)
Dave Barachcada2a02017-05-18 19:16:47 -0400402{
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530403 vnet_classify_entry_t *v;
Pierre Pfistercb656302016-03-16 09:14:28 +0000404 u32x4 *mask, *key;
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530405 union
406 {
Pierre Pfistercb656302016-03-16 09:14:28 +0000407 u32x4 as_u32x4;
408 u64 as_u64[2];
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530409 } result __attribute__ ((aligned (sizeof (u32x4))));
410 vnet_classify_bucket_t *b;
Damjan Mariona9f5f852021-09-20 17:12:03 +0200411 u32 bucket_index, limit, pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412 int i;
413
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530414 bucket_index = hash & (t->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415 b = &t->buckets[bucket_index];
Pierre Pfistercb656302016-03-16 09:14:28 +0000416 mask = t->mask;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417
418 if (b->offset == 0)
419 return 0;
420
Damjan Mariona9f5f852021-09-20 17:12:03 +0200421 pages = 1 << b->log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422 v = vnet_classify_get_entry (t, b->offset);
Dave Barachcada2a02017-05-18 19:16:47 -0400423 limit = t->entries_per_page;
424 if (PREDICT_FALSE (b->linear_search))
425 {
Damjan Mariona9f5f852021-09-20 17:12:03 +0200426 limit *= pages;
427 v = vnet_classify_entry_at_index (t, v, 0);
Dave Barachcada2a02017-05-18 19:16:47 -0400428 }
Damjan Mariona9f5f852021-09-20 17:12:03 +0200429 else
430 {
431 hash >>= t->log2_nbuckets;
432 v = vnet_classify_entry_at_index (t, v, hash & (pages - 1));
433 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
Adrian Oanca22ac59b2018-02-23 16:27:41 +0100435#ifdef CLIB_HAVE_VEC128
Neale Ranns1441a6c2020-12-14 16:02:17 +0000436 const u32x4u *data = (const u32x4u *) h;
Damjan Marion83049332019-09-25 00:25:36 +0200437 for (i = 0; i < limit; i++)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530438 {
Damjan Marion83049332019-09-25 00:25:36 +0200439 key = v->key;
440 result.as_u32x4 = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
441 switch (t->match_n_vectors)
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530442 {
Damjan Marion83049332019-09-25 00:25:36 +0200443 case 5:
444 result.as_u32x4 |= (data[4 + t->skip_n_vectors] & mask[4]) ^ key[4];
445 /* FALLTHROUGH */
446 case 4:
447 result.as_u32x4 |= (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3];
448 /* FALLTHROUGH */
449 case 3:
450 result.as_u32x4 |= (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
451 /* FALLTHROUGH */
452 case 2:
453 result.as_u32x4 |= (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
454 /* FALLTHROUGH */
455 case 1:
456 break;
457 default:
458 abort ();
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530459 }
Damjan Marion83049332019-09-25 00:25:36 +0200460
Damjan Marion62ed5432021-07-14 12:01:17 +0200461 if (u32x4_is_all_zero (result.as_u32x4))
Damjan Marion83049332019-09-25 00:25:36 +0200462 {
463 if (PREDICT_TRUE (now))
464 {
465 v->hits++;
466 v->last_heard = now;
467 }
468 return (v);
469 }
470 v = vnet_classify_entry_at_index (t, v, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471 }
Damjan Marion83049332019-09-25 00:25:36 +0200472#else
473 u32 skip_u64 = t->skip_n_vectors * 2;
Neale Ranns1441a6c2020-12-14 16:02:17 +0000474 const u64 *data64 = (const u64 *) h;
Damjan Marion83049332019-09-25 00:25:36 +0200475 for (i = 0; i < limit; i++)
476 {
477 key = v->key;
478
479 result.as_u64[0] =
480 (data64[0 + skip_u64] & ((u64 *) mask)[0]) ^ ((u64 *) key)[0];
481 result.as_u64[1] =
482 (data64[1 + skip_u64] & ((u64 *) mask)[1]) ^ ((u64 *) key)[1];
483 switch (t->match_n_vectors)
484 {
485 case 5:
486 result.as_u64[0] |=
487 (data64[8 + skip_u64] & ((u64 *) mask)[8]) ^ ((u64 *) key)[8];
488 result.as_u64[1] |=
489 (data64[9 + skip_u64] & ((u64 *) mask)[9]) ^ ((u64 *) key)[9];
490 /* FALLTHROUGH */
491 case 4:
492 result.as_u64[0] |=
493 (data64[6 + skip_u64] & ((u64 *) mask)[6]) ^ ((u64 *) key)[6];
494 result.as_u64[1] |=
495 (data64[7 + skip_u64] & ((u64 *) mask)[7]) ^ ((u64 *) key)[7];
496 /* FALLTHROUGH */
497 case 3:
498 result.as_u64[0] |=
499 (data64[4 + skip_u64] & ((u64 *) mask)[4]) ^ ((u64 *) key)[4];
500 result.as_u64[1] |=
501 (data64[5 + skip_u64] & ((u64 *) mask)[5]) ^ ((u64 *) key)[5];
502 /* FALLTHROUGH */
503 case 2:
504 result.as_u64[0] |=
505 (data64[2 + skip_u64] & ((u64 *) mask)[2]) ^ ((u64 *) key)[2];
506 result.as_u64[1] |=
507 (data64[3 + skip_u64] & ((u64 *) mask)[3]) ^ ((u64 *) key)[3];
508 /* FALLTHROUGH */
509 case 1:
510 break;
511 default:
512 abort ();
513 }
514
515 if (result.as_u64[0] == 0 && result.as_u64[1] == 0)
516 {
517 if (PREDICT_TRUE (now))
518 {
519 v->hits++;
520 v->last_heard = now;
521 }
522 return (v);
523 }
524
525 v = vnet_classify_entry_at_index (t, v, 1);
526 }
Adrian Oanca22ac59b2018-02-23 16:27:41 +0100527#endif /* CLIB_HAVE_VEC128 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528 return 0;
Dave Barachcada2a02017-05-18 19:16:47 -0400529}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530
Neale Ranns1441a6c2020-12-14 16:02:17 +0000531vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t *cm,
532 const u8 *mask, u32 nbuckets,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530533 u32 memory_size,
534 u32 skip_n_vectors,
535 u32 match_n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536
Neale Ranns1441a6c2020-12-14 16:02:17 +0000537int vnet_classify_add_del_session (vnet_classify_main_t *cm, u32 table_index,
538 const u8 *match, u32 hit_next_index,
539 u32 opaque_index, i32 advance, u8 action,
540 u16 metadata, int is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541
Neale Ranns1441a6c2020-12-14 16:02:17 +0000542int vnet_classify_add_del_table (vnet_classify_main_t *cm, const u8 *mask,
543 u32 nbuckets, u32 memory_size, u32 skip,
544 u32 match, u32 next_table_index,
545 u32 miss_next_index, u32 *table_index,
546 u8 current_data_flag, i16 current_data_offset,
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530547 int is_add, int del_chain);
Neale Ranns1441a6c2020-12-14 16:02:17 +0000548void vnet_classify_delete_table_index (vnet_classify_main_t *cm,
549 u32 table_index, int del_chain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550
551unformat_function_t unformat_ip4_mask;
552unformat_function_t unformat_ip6_mask;
553unformat_function_t unformat_l3_mask;
554unformat_function_t unformat_l2_mask;
555unformat_function_t unformat_classify_mask;
556unformat_function_t unformat_l2_next_index;
557unformat_function_t unformat_ip_next_index;
558unformat_function_t unformat_ip4_match;
559unformat_function_t unformat_ip6_match;
560unformat_function_t unformat_l3_match;
Dave Barach4a3f69c2017-02-22 12:44:56 -0500561unformat_function_t unformat_l4_match;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562unformat_function_t unformat_vlan_tag;
563unformat_function_t unformat_l2_match;
564unformat_function_t unformat_classify_match;
565
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530566void vnet_classify_register_unformat_ip_next_index_fn
567 (unformat_function_t * fn);
Dave Barachf39ff742016-03-20 10:14:45 -0400568
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530569void vnet_classify_register_unformat_l2_next_index_fn
570 (unformat_function_t * fn);
Dave Barachf39ff742016-03-20 10:14:45 -0400571
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530572void vnet_classify_register_unformat_acl_next_index_fn
573 (unformat_function_t * fn);
Dave Barachf39ff742016-03-20 10:14:45 -0400574
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530575void vnet_classify_register_unformat_policer_next_index_fn
576 (unformat_function_t * fn);
Matus Fabian70e6a8d2016-06-20 08:10:42 -0700577
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530578void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
579 fn);
Dave Barachf39ff742016-03-20 10:14:45 -0400580
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400581u32 classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index);
582void classify_set_pcap_chain (vnet_classify_main_t * cm,
583 u32 sw_if_index, u32 table_index);
584
585u32 classify_get_trace_chain (void);
586void classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index);
587
588u32 classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index);
589u32 classify_lookup_chain (u32 table_index,
590 u8 * mask, u32 n_skip, u32 n_match);
591
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592#endif /* __included_vnet_classify_h__ */
khemendra kumard7bfa0e2017-11-27 15:15:53 +0530593
594/*
595 * fd.io coding-style-patch-verification: ON
596 *
597 * Local Variables:
598 * eval: (c-set-style "gnu")
599 * End:
600 */