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/crc32.h>
25 #include <vppinfra/xxhash.h>
27 extern vlib_node_registration_t ip4_classify_node;
28 extern vlib_node_registration_t ip6_classify_node;
30 #define CLASSIFY_TRACE 0
33 * Classify table option to process packets
34 * CLASSIFY_FLAG_USE_CURR_DATA:
35 * - classify packets starting from VPP node’s current data pointer
37 typedef enum vnet_classify_flags_t_
39 CLASSIFY_FLAG_NONE = 0,
40 CLASSIFY_FLAG_USE_CURR_DATA = (1 << 0),
41 } __clib_packed vnet_classify_flags_t;
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
52 typedef enum vnet_classify_action_t_
54 CLASSIFY_ACTION_NONE = 0,
55 CLASSIFY_ACTION_SET_IP4_FIB_INDEX = 1,
56 CLASSIFY_ACTION_SET_IP6_FIB_INDEX = 2,
57 CLASSIFY_ACTION_SET_METADATA = 3,
58 } __clib_packed vnet_classify_action_t;
60 struct _vnet_classify_main;
61 typedef struct _vnet_classify_main vnet_classify_main_t;
63 #define foreach_size_in_u32x4 \
70 typedef struct _vnet_classify_entry
72 /* put into vnet_buffer(b)->l2_classfy.opaque_index */
78 /* advance on hit, note it's a signed quantity... */
87 struct _vnet_classify_entry *next_free;
92 /* Really only need 1 bit */
94 #define VNET_CLASSIFY_ENTRY_FREE (1<<0)
96 vnet_classify_action_t action;
98 /* Graph node next index */
101 /* Must be aligned to a 16-octet boundary */
103 } vnet_classify_entry_t;
106 * Check there's no padding in the entry. the key lies on a 16 byte boundary.
108 STATIC_ASSERT_OFFSET_OF (vnet_classify_entry_t, key, 32);
111 vnet_classify_entry_is_free (vnet_classify_entry_t * e)
113 return e->flags & VNET_CLASSIFY_ENTRY_FREE;
117 vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
119 return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
122 /* Need these to con the vector allocator */
126 vnet_classify_entry_t e; \
128 } __clib_packed vnet_classify_entry_##size##_t;
129 foreach_size_in_u32x4;
145 } vnet_classify_bucket_t;
149 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
150 /* Mask to apply after skipping N vectors */
154 vnet_classify_bucket_t *buckets;
156 /* Private allocation arena, protected by the writer lock,
157 * where the entries are stored. */
160 /* User/client data associated with the table */
165 u32 entries_per_page;
169 /* Index of next table to try */
170 u32 next_table_index;
173 i16 current_data_offset;
174 vnet_classify_flags_t current_data_flag;
175 /* Miss next index, return if next_table_index = 0 */
179 * All members accessed in the DP above here
181 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
183 /* Config parameters */
188 /* Per-bucket working copies, one per thread */
189 vnet_classify_entry_t **working_copies;
190 int *working_copy_lengths;
191 vnet_classify_bucket_t saved_bucket;
193 /* Free entry freelists */
194 vnet_classify_entry_t **freelists;
196 /* Writer (only) lock for this table */
197 clib_spinlock_t writer_lock;
199 } vnet_classify_table_t;
202 * Ensure DP fields don't spill over to cache-line 2
204 STATIC_ASSERT_OFFSET_OF (vnet_classify_table_t, cacheline1,
205 CLIB_CACHE_LINE_BYTES);
208 * The vector size for the classifier
209 * in the add/del table 'match' is the number of vectors of this size
211 #define VNET_CLASSIFY_VECTOR_SIZE \
212 sizeof (((vnet_classify_table_t *) 0)->mask[0])
214 struct _vnet_classify_main
217 vnet_classify_table_t *tables;
219 /* Registered next-index, opaque unformat fcns */
220 unformat_function_t **unformat_l2_next_index_fns;
221 unformat_function_t **unformat_ip_next_index_fns;
222 unformat_function_t **unformat_acl_next_index_fns;
223 unformat_function_t **unformat_policer_next_index_fns;
224 unformat_function_t **unformat_opaque_index_fns;
226 /* Per-interface filter table. [0] is used for pcap */
227 u32 *classify_table_index_by_sw_if_index;
229 /* convenience variables */
230 vlib_main_t *vlib_main;
231 vnet_main_t *vnet_main;
234 extern vnet_classify_main_t vnet_classify_main;
236 u8 *format_classify_table (u8 * s, va_list * args);
237 u8 *format_vnet_classify_table (u8 *s, va_list *args);
239 u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h);
241 static_always_inline vnet_classify_table_t *
242 vnet_classify_table_get (u32 table_index)
244 vnet_classify_main_t *vcm = &vnet_classify_main;
246 return (pool_elt_at_index (vcm->tables, table_index));
250 vnet_classify_hash_packet_inline (vnet_classify_table_t *t, const u8 *h)
258 } xor_sum __attribute__ ((aligned (sizeof (u32x4))));
262 #ifdef CLIB_HAVE_VEC128
263 u32x4u *data = (u32x4u *) h;
264 xor_sum.as_u32x4 = data[0 + t->skip_n_vectors] & mask[0];
265 switch (t->match_n_vectors)
268 xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4];
271 xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3];
274 xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2];
277 xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1];
285 u32 skip_u64 = t->skip_n_vectors * 2;
286 u64 *data64 = (u64 *) h;
287 xor_sum.as_u64[0] = data64[0 + skip_u64] & ((u64 *) mask)[0];
288 xor_sum.as_u64[1] = data64[1 + skip_u64] & ((u64 *) mask)[1];
289 switch (t->match_n_vectors)
292 xor_sum.as_u64[0] ^= data64[8 + skip_u64] & ((u64 *) mask)[8];
293 xor_sum.as_u64[1] ^= data64[9 + skip_u64] & ((u64 *) mask)[9];
296 xor_sum.as_u64[0] ^= data64[6 + skip_u64] & ((u64 *) mask)[6];
297 xor_sum.as_u64[1] ^= data64[7 + skip_u64] & ((u64 *) mask)[7];
300 xor_sum.as_u64[0] ^= data64[4 + skip_u64] & ((u64 *) mask)[4];
301 xor_sum.as_u64[1] ^= data64[5 + skip_u64] & ((u64 *) mask)[5];
304 xor_sum.as_u64[0] ^= data64[2 + skip_u64] & ((u64 *) mask)[2];
305 xor_sum.as_u64[1] ^= data64[3 + skip_u64] & ((u64 *) mask)[3];
313 #endif /* CLIB_HAVE_VEC128 */
315 #ifdef clib_crc32c_uses_intrinsics
316 return clib_crc32c ((u8 *) & xor_sum, sizeof (xor_sum));
318 return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
323 vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
327 ASSERT (is_pow2 (t->nbuckets));
329 bucket_index = hash & (t->nbuckets - 1);
331 CLIB_PREFETCH (&t->buckets[bucket_index], CLIB_CACHE_LINE_BYTES, LOAD);
334 static inline vnet_classify_entry_t *
335 vnet_classify_get_entry (vnet_classify_table_t * t, uword offset)
337 u8 *hp = clib_mem_get_heap_base (t->mheap);
338 u8 *vp = hp + offset;
340 return (vnet_classify_entry_t *) vp;
344 vnet_classify_get_offset (vnet_classify_table_t * t,
345 vnet_classify_entry_t * v)
349 hp = (u8 *) clib_mem_get_heap_base (t->mheap);
352 ASSERT ((vp - hp) < 0x100000000ULL);
356 static inline vnet_classify_entry_t *
357 vnet_classify_entry_at_index (vnet_classify_table_t * t,
358 vnet_classify_entry_t * e, u32 index)
364 eu8 += index * (sizeof (vnet_classify_entry_t) +
365 (t->match_n_vectors * sizeof (u32x4)));
367 return (vnet_classify_entry_t *) eu8;
371 vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
375 vnet_classify_bucket_t *b;
376 vnet_classify_entry_t *e;
378 bucket_index = hash & (t->nbuckets - 1);
380 b = &t->buckets[bucket_index];
385 hash >>= t->log2_nbuckets;
387 e = vnet_classify_get_entry (t, b->offset);
388 value_index = hash & ((1 << b->log2_pages) - 1);
390 e = vnet_classify_entry_at_index (t, e, value_index);
392 CLIB_PREFETCH (e, CLIB_CACHE_LINE_BYTES, LOAD);
395 vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t,
396 u8 * h, u64 hash, f64 now);
398 static inline vnet_classify_entry_t *
399 vnet_classify_find_entry_inline (vnet_classify_table_t *t, const u8 *h,
402 vnet_classify_entry_t *v;
408 } result __attribute__ ((aligned (sizeof (u32x4))));
409 vnet_classify_bucket_t *b;
415 bucket_index = hash & (t->nbuckets - 1);
416 b = &t->buckets[bucket_index];
422 hash >>= t->log2_nbuckets;
424 v = vnet_classify_get_entry (t, b->offset);
425 value_index = hash & ((1 << b->log2_pages) - 1);
426 limit = t->entries_per_page;
427 if (PREDICT_FALSE (b->linear_search))
430 limit *= (1 << b->log2_pages);
433 v = vnet_classify_entry_at_index (t, v, value_index);
435 #ifdef CLIB_HAVE_VEC128
436 const u32x4u *data = (const u32x4u *) h;
437 for (i = 0; i < limit; i++)
440 result.as_u32x4 = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
441 switch (t->match_n_vectors)
444 result.as_u32x4 |= (data[4 + t->skip_n_vectors] & mask[4]) ^ key[4];
447 result.as_u32x4 |= (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3];
450 result.as_u32x4 |= (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
453 result.as_u32x4 |= (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
461 if (u32x4_zero_byte_mask (result.as_u32x4) == 0xffff)
463 if (PREDICT_TRUE (now))
470 v = vnet_classify_entry_at_index (t, v, 1);
473 u32 skip_u64 = t->skip_n_vectors * 2;
474 const u64 *data64 = (const u64 *) h;
475 for (i = 0; i < limit; i++)
480 (data64[0 + skip_u64] & ((u64 *) mask)[0]) ^ ((u64 *) key)[0];
482 (data64[1 + skip_u64] & ((u64 *) mask)[1]) ^ ((u64 *) key)[1];
483 switch (t->match_n_vectors)
487 (data64[8 + skip_u64] & ((u64 *) mask)[8]) ^ ((u64 *) key)[8];
489 (data64[9 + skip_u64] & ((u64 *) mask)[9]) ^ ((u64 *) key)[9];
493 (data64[6 + skip_u64] & ((u64 *) mask)[6]) ^ ((u64 *) key)[6];
495 (data64[7 + skip_u64] & ((u64 *) mask)[7]) ^ ((u64 *) key)[7];
499 (data64[4 + skip_u64] & ((u64 *) mask)[4]) ^ ((u64 *) key)[4];
501 (data64[5 + skip_u64] & ((u64 *) mask)[5]) ^ ((u64 *) key)[5];
505 (data64[2 + skip_u64] & ((u64 *) mask)[2]) ^ ((u64 *) key)[2];
507 (data64[3 + skip_u64] & ((u64 *) mask)[3]) ^ ((u64 *) key)[3];
515 if (result.as_u64[0] == 0 && result.as_u64[1] == 0)
517 if (PREDICT_TRUE (now))
525 v = vnet_classify_entry_at_index (t, v, 1);
527 #endif /* CLIB_HAVE_VEC128 */
531 vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t *cm,
532 const u8 *mask, u32 nbuckets,
535 u32 match_n_vectors);
537 int 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);
542 int 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,
547 int is_add, int del_chain);
548 void vnet_classify_delete_table_index (vnet_classify_main_t *cm,
549 u32 table_index, int del_chain);
551 unformat_function_t unformat_ip4_mask;
552 unformat_function_t unformat_ip6_mask;
553 unformat_function_t unformat_l3_mask;
554 unformat_function_t unformat_l2_mask;
555 unformat_function_t unformat_classify_mask;
556 unformat_function_t unformat_l2_next_index;
557 unformat_function_t unformat_ip_next_index;
558 unformat_function_t unformat_ip4_match;
559 unformat_function_t unformat_ip6_match;
560 unformat_function_t unformat_l3_match;
561 unformat_function_t unformat_l4_match;
562 unformat_function_t unformat_vlan_tag;
563 unformat_function_t unformat_l2_match;
564 unformat_function_t unformat_classify_match;
566 void vnet_classify_register_unformat_ip_next_index_fn
567 (unformat_function_t * fn);
569 void vnet_classify_register_unformat_l2_next_index_fn
570 (unformat_function_t * fn);
572 void vnet_classify_register_unformat_acl_next_index_fn
573 (unformat_function_t * fn);
575 void vnet_classify_register_unformat_policer_next_index_fn
576 (unformat_function_t * fn);
578 void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
581 u32 classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index);
582 void classify_set_pcap_chain (vnet_classify_main_t * cm,
583 u32 sw_if_index, u32 table_index);
585 u32 classify_get_trace_chain (void);
586 void classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index);
588 u32 classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index);
589 u32 classify_lookup_chain (u32 table_index,
590 u8 * mask, u32 n_skip, u32 n_match);
592 #endif /* __included_vnet_classify_h__ */
595 * fd.io coding-style-patch-verification: ON
598 * eval: (c-set-style "gnu")