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);
151 vnet_classify_bucket_t *buckets;
153 /* Private allocation arena, protected by the writer lock,
154 * where the entries are stored. */
157 /* User/client data associated with the table */
162 u32 entries_per_page;
167 /* Index of next table to try */
168 u32 next_table_index;
171 i16 current_data_offset;
172 vnet_classify_flags_t current_data_flag;
173 /* Miss next index, return if next_table_index = 0 */
177 * All members accessed in the DP above here
179 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
181 /* Config parameters */
186 /* Per-bucket working copies, one per thread */
187 vnet_classify_entry_t **working_copies;
188 int *working_copy_lengths;
189 vnet_classify_bucket_t saved_bucket;
191 /* Free entry freelists */
192 vnet_classify_entry_t **freelists;
194 /* Writer (only) lock for this table */
195 clib_spinlock_t writer_lock;
197 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
198 /* Mask to apply after skipping N vectors */
201 } vnet_classify_table_t;
204 * Ensure DP fields don't spill over to cache-line 2
206 STATIC_ASSERT_OFFSET_OF (vnet_classify_table_t, cacheline1,
207 CLIB_CACHE_LINE_BYTES);
210 * The vector size for the classifier
211 * in the add/del table 'match' is the number of vectors of this size
213 #define VNET_CLASSIFY_VECTOR_SIZE \
214 sizeof (((vnet_classify_table_t *) 0)->mask[0])
216 struct _vnet_classify_main
219 vnet_classify_table_t *tables;
221 /* Registered next-index, opaque unformat fcns */
222 unformat_function_t **unformat_l2_next_index_fns;
223 unformat_function_t **unformat_ip_next_index_fns;
224 unformat_function_t **unformat_acl_next_index_fns;
225 unformat_function_t **unformat_policer_next_index_fns;
226 unformat_function_t **unformat_opaque_index_fns;
228 /* Per-interface filter table. [0] is used for pcap */
229 u32 *classify_table_index_by_sw_if_index;
231 /* convenience variables */
232 vlib_main_t *vlib_main;
233 vnet_main_t *vnet_main;
236 extern vnet_classify_main_t vnet_classify_main;
238 u8 *format_classify_table (u8 * s, va_list * args);
239 u8 *format_vnet_classify_table (u8 *s, va_list *args);
241 u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h);
243 static_always_inline vnet_classify_table_t *
244 vnet_classify_table_get (u32 table_index)
246 vnet_classify_main_t *vcm = &vnet_classify_main;
248 return (pool_elt_at_index (vcm->tables, table_index));
252 vnet_classify_hash_packet_inline (vnet_classify_table_t *t, const u8 *h)
256 h += t->skip_n_vectors * 16;
258 #if defined(CLIB_HAVE_VEC512) && defined(CLIB_HAVE_VEC512_MASK_LOAD_STORE)
259 u64x8 xor_sum_x8, *mask = (u64x8 *) t->mask;
260 u16 load_mask = t->load_mask;
261 u64x8u *data = (u64x8u *) h;
263 xor_sum_x8 = u64x8_mask_load_zero (data, load_mask) & mask[0];
265 if (PREDICT_FALSE (load_mask >> 8))
266 xor_sum_x8 ^= u64x8_mask_load_zero (data + 1, load_mask >> 8) & mask[1];
268 xor_sum_x8 ^= u64x8_align_right (xor_sum_x8, xor_sum_x8, 4);
269 xor_sum_x8 ^= u64x8_align_right (xor_sum_x8, xor_sum_x8, 2);
270 xor_sum = xor_sum_x8[0] ^ xor_sum_x8[1];
271 #elif defined(CLIB_HAVE_VEC256) && defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
272 u64x4 xor_sum_x4, *mask = (u64x4 *) t->mask;
273 u16 load_mask = t->load_mask;
274 u64x4u *data = (u64x4u *) h;
276 xor_sum_x4 = u64x4_mask_load_zero (data, load_mask) & mask[0];
277 xor_sum_x4 ^= u64x4_mask_load_zero (data + 1, load_mask >> 4) & mask[1];
279 if (PREDICT_FALSE (load_mask >> 8))
280 xor_sum_x4 ^= u64x4_mask_load_zero (data + 2, load_mask >> 8) & mask[2];
282 xor_sum_x4 ^= u64x4_align_right (xor_sum_x4, xor_sum_x4, 2);
283 xor_sum = xor_sum_x4[0] ^ xor_sum_x4[1];
284 #elif defined(CLIB_HAVE_VEC128)
285 u64x2 *mask = (u64x2 *) t->mask;
286 u64x2u *data = (u64x2u *) h;
289 xor_sum_x2 = data[0] & mask[0];
291 switch (t->match_n_vectors)
294 xor_sum_x2 ^= data[4] & mask[4];
297 xor_sum_x2 ^= data[3] & mask[3];
300 xor_sum_x2 ^= data[2] & mask[2];
303 xor_sum_x2 ^= data[1] & mask[1];
310 xor_sum = xor_sum_x2[0] ^ xor_sum_x2[1];
312 u64 *data = (u64 *) h;
313 u64 *mask = (u64 *) t->mask;
315 xor_sum = (data[0] & mask[0]) ^ (data[1] & mask[1]);
317 switch (t->match_n_vectors)
320 xor_sum ^= (data[8] & mask[8]) ^ (data[9] & mask[9]);
323 xor_sum ^= (data[6] & mask[6]) ^ (data[7] & mask[7]);
326 xor_sum ^= (data[4] & mask[4]) ^ (data[5] & mask[5]);
329 xor_sum ^= (data[2] & mask[2]) ^ (data[3] & mask[3]);
337 #endif /* CLIB_HAVE_VEC128 */
339 #ifdef clib_crc32c_uses_intrinsics
340 return clib_crc32c ((u8 *) & xor_sum, sizeof (xor_sum));
342 return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
347 vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
351 ASSERT (is_pow2 (t->nbuckets));
353 bucket_index = hash & (t->nbuckets - 1);
355 clib_prefetch_load (&t->buckets[bucket_index]);
358 static inline vnet_classify_entry_t *
359 vnet_classify_get_entry (vnet_classify_table_t * t, uword offset)
361 u8 *hp = clib_mem_get_heap_base (t->mheap);
362 u8 *vp = hp + offset;
364 return (vnet_classify_entry_t *) vp;
368 vnet_classify_get_offset (vnet_classify_table_t * t,
369 vnet_classify_entry_t * v)
373 hp = (u8 *) clib_mem_get_heap_base (t->mheap);
376 ASSERT ((vp - hp) < 0x100000000ULL);
380 static inline vnet_classify_entry_t *
381 vnet_classify_entry_at_index (vnet_classify_table_t * t,
382 vnet_classify_entry_t * e, u32 index)
388 eu8 += index * (sizeof (vnet_classify_entry_t) +
389 (t->match_n_vectors * sizeof (u32x4)));
391 return (vnet_classify_entry_t *) eu8;
395 vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
399 vnet_classify_bucket_t *b;
400 vnet_classify_entry_t *e;
402 bucket_index = hash & (t->nbuckets - 1);
404 b = &t->buckets[bucket_index];
409 hash >>= t->log2_nbuckets;
411 e = vnet_classify_get_entry (t, b->offset);
412 value_index = hash & ((1 << b->log2_pages) - 1);
414 e = vnet_classify_entry_at_index (t, e, value_index);
416 clib_prefetch_load (e);
419 vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t,
420 u8 * h, u64 hash, f64 now);
422 static_always_inline int
423 vnet_classify_entry_is_equal (vnet_classify_entry_t *v, const u8 *d, u8 *m,
426 #ifdef CLIB_HAVE_VEC128
427 u64x2u *data = (u64x2 *) d;
428 u64x2 *key = (u64x2 *) v->key;
429 u64x2 *mask = (u64x2 *) m;
432 r = (data[0] & mask[0]) ^ key[0];
433 switch (match_n_vectors)
436 r |= (data[4] & mask[4]) ^ key[4];
439 r |= (data[3] & mask[3]) ^ key[3];
442 r |= (data[2] & mask[2]) ^ key[2];
445 r |= (data[1] & mask[1]) ^ key[1];
453 if (u64x2_is_all_zero (r))
457 u64 *data = (u64 *) d;
458 u64 *key = (u64 *) v->key;
459 u64 *mask = (u64 *) m;
462 r = ((data[0] & mask[0]) ^ key[0]) | ((data[1] & mask[1]) ^ key[1]);
463 switch (match_n_vectors)
466 r |= ((data[8] & mask[8]) ^ key[8]) | ((data[9] & mask[9]) ^ key[9]);
469 r |= ((data[6] & mask[6]) ^ key[6]) | ((data[7] & mask[7]) ^ key[7]);
472 r |= ((data[4] & mask[4]) ^ key[4]) | ((data[5] & mask[5]) ^ key[5]);
475 r |= ((data[2] & mask[2]) ^ key[2]) | ((data[3] & mask[3]) ^ key[3]);
486 #endif /* CLIB_HAVE_VEC128 */
490 static inline vnet_classify_entry_t *
491 vnet_classify_find_entry_inline (vnet_classify_table_t *t, const u8 *h,
494 vnet_classify_entry_t *v;
495 vnet_classify_bucket_t *b;
496 u32 bucket_index, limit, pages, match_n_vectors = t->match_n_vectors;
497 u8 *mask = (u8 *) t->mask;
500 bucket_index = hash & (t->nbuckets - 1);
501 b = &t->buckets[bucket_index];
506 pages = 1 << b->log2_pages;
507 v = vnet_classify_get_entry (t, b->offset);
508 limit = t->entries_per_page;
509 if (PREDICT_FALSE (b->linear_search))
512 v = vnet_classify_entry_at_index (t, v, 0);
516 hash >>= t->log2_nbuckets;
517 v = vnet_classify_entry_at_index (t, v, hash & (pages - 1));
520 h += t->skip_n_vectors * 16;
522 for (i = 0; i < limit; i++)
524 if (vnet_classify_entry_is_equal (v, h, mask, match_n_vectors))
526 if (PREDICT_TRUE (now))
533 v = vnet_classify_entry_at_index (t, v, 1);
538 vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t *cm,
539 const u8 *mask, u32 nbuckets,
542 u32 match_n_vectors);
544 int vnet_classify_add_del_session (vnet_classify_main_t *cm, u32 table_index,
545 const u8 *match, u32 hit_next_index,
546 u32 opaque_index, i32 advance, u8 action,
547 u16 metadata, int is_add);
549 int vnet_classify_add_del_table (vnet_classify_main_t *cm, const u8 *mask,
550 u32 nbuckets, u32 memory_size, u32 skip,
551 u32 match, u32 next_table_index,
552 u32 miss_next_index, u32 *table_index,
553 u8 current_data_flag, i16 current_data_offset,
554 int is_add, int del_chain);
555 void vnet_classify_delete_table_index (vnet_classify_main_t *cm,
556 u32 table_index, int del_chain);
558 unformat_function_t unformat_ip4_mask;
559 unformat_function_t unformat_ip6_mask;
560 unformat_function_t unformat_l3_mask;
561 unformat_function_t unformat_l2_mask;
562 unformat_function_t unformat_classify_mask;
563 unformat_function_t unformat_l2_next_index;
564 unformat_function_t unformat_ip_next_index;
565 unformat_function_t unformat_ip4_match;
566 unformat_function_t unformat_ip6_match;
567 unformat_function_t unformat_l3_match;
568 unformat_function_t unformat_l4_match;
569 unformat_function_t unformat_vlan_tag;
570 unformat_function_t unformat_l2_match;
571 unformat_function_t unformat_classify_match;
573 void vnet_classify_register_unformat_ip_next_index_fn
574 (unformat_function_t * fn);
576 void vnet_classify_register_unformat_l2_next_index_fn
577 (unformat_function_t * fn);
579 void vnet_classify_register_unformat_acl_next_index_fn
580 (unformat_function_t * fn);
582 void vnet_classify_register_unformat_policer_next_index_fn
583 (unformat_function_t * fn);
585 void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
588 u32 classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index);
589 void classify_set_pcap_chain (vnet_classify_main_t * cm,
590 u32 sw_if_index, u32 table_index);
592 u32 classify_get_trace_chain (void);
593 void classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index);
595 u32 classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index);
596 u32 classify_lookup_chain (u32 table_index,
597 u8 * mask, u32 n_skip, u32 n_match);
599 #endif /* __included_vnet_classify_h__ */
602 * fd.io coding-style-patch-verification: ON
605 * eval: (c-set-style "gnu")