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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef __included_vnet_classify_h__
16 #define __included_vnet_classify_h__
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h> /* for API error numbers */
21 #include <vppinfra/error.h>
22 #include <vppinfra/hash.h>
23 #include <vppinfra/cache.h>
24 #include <vppinfra/xxhash.h>
26 extern vlib_node_registration_t ip4_classify_node;
27 extern vlib_node_registration_t ip6_classify_node;
29 #define CLASSIFY_TRACE 0
32 * Classify table option to process packets
33 * CLASSIFY_FLAG_USE_CURR_DATA:
34 * - classify packets starting from VPP node’s current data pointer
36 #define CLASSIFY_FLAG_USE_CURR_DATA 1
39 * Classify session action
40 * CLASSIFY_ACTION_SET_IP4_FIB_INDEX:
41 * - Classified IP packets will be looked up
42 * from the specified ipv4 fib table
43 * CLASSIFY_ACTION_SET_IP6_FIB_INDEX:
44 * - Classified IP packets will be looked up
45 * from the specified ipv6 fib table
47 typedef enum vnet_classify_action_t_
49 CLASSIFY_ACTION_SET_IP4_FIB_INDEX = 1,
50 CLASSIFY_ACTION_SET_IP6_FIB_INDEX = 2,
51 CLASSIFY_ACTION_SET_METADATA = 3,
52 } __attribute__ ((packed)) vnet_classify_action_t;
54 struct _vnet_classify_main;
55 typedef struct _vnet_classify_main vnet_classify_main_t;
57 #define foreach_size_in_u32x4 \
65 typedef CLIB_PACKED(struct _vnet_classify_entry {
66 /* Graph node next index */
69 /* put into vnet_buffer(b)->l2_classfy.opaque_index */
73 /* advance on hit, note it's a signed quantity... */
79 /* Really only need 1 bit */
81 #define VNET_CLASSIFY_ENTRY_FREE (1<<0)
83 vnet_classify_action_t action;
86 /* Hit counter, last heard time */
89 struct _vnet_classify_entry * next_free;
94 /* Must be aligned to a 16-octet boundary */
96 }) vnet_classify_entry_t;
100 vnet_classify_entry_is_free (vnet_classify_entry_t * e)
102 return e->flags & VNET_CLASSIFY_ENTRY_FREE;
106 vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
108 return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
111 /* Need these to con the vector allocator */
114 typedef CLIB_PACKED(struct { \
118 }) vnet_classify_entry_##size##_t;
119 foreach_size_in_u32x4;
136 } vnet_classify_bucket_t;
140 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
141 /* Mask to apply after skipping N vectors */
143 /* Buckets and entries */
144 vnet_classify_bucket_t *buckets;
145 vnet_classify_entry_t *entries;
147 /* Config parameters */
153 int entries_per_page;
155 u32 current_data_flag;
156 int current_data_offset;
158 /* Index of next table to try */
159 u32 next_table_index;
161 /* Miss next index, return if next_table_index = 0 */
164 /* Per-bucket working copies, one per thread */
165 vnet_classify_entry_t **working_copies;
166 int *working_copy_lengths;
167 vnet_classify_bucket_t saved_bucket;
169 /* Free entry freelists */
170 vnet_classify_entry_t **freelists;
174 /* Private allocation arena, protected by the writer lock */
177 /* Writer (only) lock for this table */
178 clib_spinlock_t writer_lock;
180 } vnet_classify_table_t;
182 struct _vnet_classify_main
185 vnet_classify_table_t *tables;
187 /* Registered next-index, opaque unformat fcns */
188 unformat_function_t **unformat_l2_next_index_fns;
189 unformat_function_t **unformat_ip_next_index_fns;
190 unformat_function_t **unformat_acl_next_index_fns;
191 unformat_function_t **unformat_policer_next_index_fns;
192 unformat_function_t **unformat_opaque_index_fns;
194 /* convenience variables */
195 vlib_main_t *vlib_main;
196 vnet_main_t *vnet_main;
199 extern vnet_classify_main_t vnet_classify_main;
201 u8 *format_classify_table (u8 * s, va_list * args);
203 u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h);
206 vnet_classify_hash_packet_inline (vnet_classify_table_t * t, u8 * h)
214 } xor_sum __attribute__ ((aligned (sizeof (u32x4))));
218 #ifdef CLIB_HAVE_VEC128
219 u32x4u *data = (u32x4u *) h;
220 xor_sum.as_u32x4 = data[0 + t->skip_n_vectors] & mask[0];
221 switch (t->match_n_vectors)
224 xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4];
227 xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3];
230 xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2];
233 xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1];
241 u32 skip_u64 = t->skip_n_vectors * 2;
242 u64 *data64 = (u64 *) h;
243 xor_sum.as_u64[0] = data64[0 + skip_u64] & ((u64 *) mask)[0];
244 xor_sum.as_u64[1] = data64[1 + skip_u64] & ((u64 *) mask)[1];
245 switch (t->match_n_vectors)
248 xor_sum.as_u64[0] ^= data64[8 + skip_u64] & ((u64 *) mask)[8];
249 xor_sum.as_u64[1] ^= data64[9 + skip_u64] & ((u64 *) mask)[9];
252 xor_sum.as_u64[0] ^= data64[6 + skip_u64] & ((u64 *) mask)[6];
253 xor_sum.as_u64[1] ^= data64[7 + skip_u64] & ((u64 *) mask)[7];
256 xor_sum.as_u64[0] ^= data64[4 + skip_u64] & ((u64 *) mask)[4];
257 xor_sum.as_u64[1] ^= data64[5 + skip_u64] & ((u64 *) mask)[5];
260 xor_sum.as_u64[0] ^= data64[2 + skip_u64] & ((u64 *) mask)[2];
261 xor_sum.as_u64[1] ^= data64[3 + skip_u64] & ((u64 *) mask)[3];
269 #endif /* CLIB_HAVE_VEC128 */
271 return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
275 vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
279 ASSERT (is_pow2 (t->nbuckets));
281 bucket_index = hash & (t->nbuckets - 1);
283 CLIB_PREFETCH (&t->buckets[bucket_index], CLIB_CACHE_LINE_BYTES, LOAD);
286 static inline vnet_classify_entry_t *
287 vnet_classify_get_entry (vnet_classify_table_t * t, uword offset)
290 u8 *vp = hp + offset;
296 vnet_classify_get_offset (vnet_classify_table_t * t,
297 vnet_classify_entry_t * v)
301 hp = (u8 *) t->mheap;
304 ASSERT ((vp - hp) < 0x100000000ULL);
308 static inline vnet_classify_entry_t *
309 vnet_classify_entry_at_index (vnet_classify_table_t * t,
310 vnet_classify_entry_t * e, u32 index)
316 eu8 += index * (sizeof (vnet_classify_entry_t) +
317 (t->match_n_vectors * sizeof (u32x4)));
319 return (vnet_classify_entry_t *) eu8;
323 vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
327 vnet_classify_bucket_t *b;
328 vnet_classify_entry_t *e;
330 bucket_index = hash & (t->nbuckets - 1);
332 b = &t->buckets[bucket_index];
337 hash >>= t->log2_nbuckets;
339 e = vnet_classify_get_entry (t, b->offset);
340 value_index = hash & ((1 << b->log2_pages) - 1);
342 e = vnet_classify_entry_at_index (t, e, value_index);
344 CLIB_PREFETCH (e, CLIB_CACHE_LINE_BYTES, LOAD);
347 vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t,
348 u8 * h, u64 hash, f64 now);
350 static inline vnet_classify_entry_t *
351 vnet_classify_find_entry_inline (vnet_classify_table_t * t,
352 u8 * h, u64 hash, f64 now)
354 vnet_classify_entry_t *v;
360 } result __attribute__ ((aligned (sizeof (u32x4))));
361 vnet_classify_bucket_t *b;
367 bucket_index = hash & (t->nbuckets - 1);
368 b = &t->buckets[bucket_index];
374 hash >>= t->log2_nbuckets;
376 v = vnet_classify_get_entry (t, b->offset);
377 value_index = hash & ((1 << b->log2_pages) - 1);
378 limit = t->entries_per_page;
379 if (PREDICT_FALSE (b->linear_search))
382 limit *= (1 << b->log2_pages);
385 v = vnet_classify_entry_at_index (t, v, value_index);
387 #ifdef CLIB_HAVE_VEC128
388 u32x4u *data = (u32x4u *) h;
389 for (i = 0; i < limit; i++)
392 result.as_u32x4 = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
393 switch (t->match_n_vectors)
396 result.as_u32x4 |= (data[4 + t->skip_n_vectors] & mask[4]) ^ key[4];
399 result.as_u32x4 |= (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3];
402 result.as_u32x4 |= (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
405 result.as_u32x4 |= (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
413 if (u32x4_zero_byte_mask (result.as_u32x4) == 0xffff)
415 if (PREDICT_TRUE (now))
422 v = vnet_classify_entry_at_index (t, v, 1);
425 u32 skip_u64 = t->skip_n_vectors * 2;
426 u64 *data64 = (u64 *) h;
427 for (i = 0; i < limit; i++)
432 (data64[0 + skip_u64] & ((u64 *) mask)[0]) ^ ((u64 *) key)[0];
434 (data64[1 + skip_u64] & ((u64 *) mask)[1]) ^ ((u64 *) key)[1];
435 switch (t->match_n_vectors)
439 (data64[8 + skip_u64] & ((u64 *) mask)[8]) ^ ((u64 *) key)[8];
441 (data64[9 + skip_u64] & ((u64 *) mask)[9]) ^ ((u64 *) key)[9];
445 (data64[6 + skip_u64] & ((u64 *) mask)[6]) ^ ((u64 *) key)[6];
447 (data64[7 + skip_u64] & ((u64 *) mask)[7]) ^ ((u64 *) key)[7];
451 (data64[4 + skip_u64] & ((u64 *) mask)[4]) ^ ((u64 *) key)[4];
453 (data64[5 + skip_u64] & ((u64 *) mask)[5]) ^ ((u64 *) key)[5];
457 (data64[2 + skip_u64] & ((u64 *) mask)[2]) ^ ((u64 *) key)[2];
459 (data64[3 + skip_u64] & ((u64 *) mask)[3]) ^ ((u64 *) key)[3];
467 if (result.as_u64[0] == 0 && result.as_u64[1] == 0)
469 if (PREDICT_TRUE (now))
477 v = vnet_classify_entry_at_index (t, v, 1);
479 #endif /* CLIB_HAVE_VEC128 */
483 vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t * cm,
484 u8 * mask, u32 nbuckets,
487 u32 match_n_vectors);
489 int vnet_classify_add_del_session (vnet_classify_main_t * cm,
495 u8 action, u32 metadata, int is_add);
497 int vnet_classify_add_del_table (vnet_classify_main_t * cm,
503 u32 next_table_index,
506 u8 current_data_flag,
507 i16 current_data_offset,
508 int is_add, int del_chain);
510 unformat_function_t unformat_ip4_mask;
511 unformat_function_t unformat_ip6_mask;
512 unformat_function_t unformat_l3_mask;
513 unformat_function_t unformat_l2_mask;
514 unformat_function_t unformat_classify_mask;
515 unformat_function_t unformat_l2_next_index;
516 unformat_function_t unformat_ip_next_index;
517 unformat_function_t unformat_ip4_match;
518 unformat_function_t unformat_ip6_match;
519 unformat_function_t unformat_l3_match;
520 unformat_function_t unformat_l4_match;
521 unformat_function_t unformat_vlan_tag;
522 unformat_function_t unformat_l2_match;
523 unformat_function_t unformat_classify_match;
525 void vnet_classify_register_unformat_ip_next_index_fn
526 (unformat_function_t * fn);
528 void vnet_classify_register_unformat_l2_next_index_fn
529 (unformat_function_t * fn);
531 void vnet_classify_register_unformat_acl_next_index_fn
532 (unformat_function_t * fn);
534 void vnet_classify_register_unformat_policer_next_index_fn
535 (unformat_function_t * fn);
537 void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
540 #endif /* __included_vnet_classify_h__ */
543 * fd.io coding-style-patch-verification: ON
546 * eval: (c-set-style "gnu")