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;
94 /* Graph node next index */
97 vnet_classify_action_t action;
99 /* Really only need 1 bit */
101 #define VNET_CLASSIFY_ENTRY_FREE (1<<0)
103 /* Must be aligned to a 16-octet boundary */
105 } vnet_classify_entry_t;
108 * Check there's no padding in the entry. the key lies on a 16 byte boundary.
110 STATIC_ASSERT_OFFSET_OF (vnet_classify_entry_t, key, 32);
113 vnet_classify_entry_is_free (vnet_classify_entry_t * e)
115 return e->flags & VNET_CLASSIFY_ENTRY_FREE;
119 vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
121 return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
124 /* Need these to con the vector allocator */
128 vnet_classify_entry_t e; \
130 } __clib_packed vnet_classify_entry_##size##_t;
131 foreach_size_in_u32x4;
147 } vnet_classify_bucket_t;
151 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
153 vnet_classify_bucket_t *buckets;
155 /* Private allocation arena, protected by the writer lock,
156 * where the entries are stored. */
159 /* User/client data associated with the table */
164 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 CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
200 /* Mask to apply after skipping N vectors */
207 } vnet_classify_table_t;
210 * Ensure DP fields don't spill over to cache-line 2
212 STATIC_ASSERT_OFFSET_OF (vnet_classify_table_t, cacheline1,
213 CLIB_CACHE_LINE_BYTES);
216 * The vector size for the classifier
217 * in the add/del table 'match' is the number of vectors of this size
219 #define VNET_CLASSIFY_VECTOR_SIZE \
220 sizeof (((vnet_classify_table_t *) 0)->mask[0])
222 struct _vnet_classify_main
225 vnet_classify_table_t *tables;
227 /* Registered next-index, opaque unformat fcns */
228 unformat_function_t **unformat_l2_next_index_fns;
229 unformat_function_t **unformat_ip_next_index_fns;
230 unformat_function_t **unformat_acl_next_index_fns;
231 unformat_function_t **unformat_policer_next_index_fns;
232 unformat_function_t **unformat_opaque_index_fns;
234 /* Per-interface filter table. [0] is used for pcap */
235 u32 *classify_table_index_by_sw_if_index;
237 /* convenience variables */
238 vlib_main_t *vlib_main;
239 vnet_main_t *vnet_main;
242 extern vnet_classify_main_t vnet_classify_main;
244 u8 *format_classify_entry (u8 *s, va_list *args);
245 u8 *format_classify_table (u8 * s, va_list * args);
246 u8 *format_vnet_classify_table (u8 *s, va_list *args);
248 u32 vnet_classify_hash_packet (const vnet_classify_table_t *t, u8 *h);
250 static_always_inline vnet_classify_table_t *
251 vnet_classify_table_get (u32 table_index)
253 vnet_classify_main_t *vcm = &vnet_classify_main;
255 return (pool_elt_at_index (vcm->tables, table_index));
259 vnet_classify_hash_packet_inline (const vnet_classify_table_t *t, const u8 *h)
263 h += t->skip_n_vectors * 16;
265 #if defined(CLIB_HAVE_VEC512) && defined(CLIB_HAVE_VEC512_MASK_LOAD_STORE)
266 u64x8 xor_sum_x8, *mask = (u64x8 *) t->mask;
267 u16 load_mask = t->load_mask;
268 u64x8u *data = (u64x8u *) h;
270 xor_sum_x8 = u64x8_mask_load_zero (data, load_mask) & mask[0];
272 if (PREDICT_FALSE (load_mask >> 8))
273 xor_sum_x8 ^= u64x8_mask_load_zero (data + 1, load_mask >> 8) & mask[1];
275 xor_sum_x8 ^= u64x8_align_right (xor_sum_x8, xor_sum_x8, 4);
276 xor_sum_x8 ^= u64x8_align_right (xor_sum_x8, xor_sum_x8, 2);
277 xor_sum = xor_sum_x8[0] ^ xor_sum_x8[1];
278 #elif defined(CLIB_HAVE_VEC256) && defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
279 u64x4 xor_sum_x4, *mask = (u64x4 *) t->mask;
280 u16 load_mask = t->load_mask;
281 u64x4u *data = (u64x4u *) h;
283 xor_sum_x4 = u64x4_mask_load_zero (data, load_mask) & mask[0];
284 xor_sum_x4 ^= u64x4_mask_load_zero (data + 1, load_mask >> 4) & mask[1];
286 if (PREDICT_FALSE (load_mask >> 8))
287 xor_sum_x4 ^= u64x4_mask_load_zero (data + 2, load_mask >> 8) & mask[2];
289 xor_sum_x4 ^= u64x4_align_right (xor_sum_x4, xor_sum_x4, 2);
290 xor_sum = xor_sum_x4[0] ^ xor_sum_x4[1];
291 #elif defined(CLIB_HAVE_VEC128)
292 u64x2 *mask = (u64x2 *) t->mask;
293 u64x2u *data = (u64x2u *) h;
296 xor_sum_x2 = data[0] & mask[0];
298 switch (t->match_n_vectors)
301 xor_sum_x2 ^= data[4] & mask[4];
304 xor_sum_x2 ^= data[3] & mask[3];
307 xor_sum_x2 ^= data[2] & mask[2];
310 xor_sum_x2 ^= data[1] & mask[1];
317 xor_sum = xor_sum_x2[0] ^ xor_sum_x2[1];
319 u64 *data = (u64 *) h;
320 u64 *mask = (u64 *) t->mask;
322 xor_sum = (data[0] & mask[0]) ^ (data[1] & mask[1]);
324 switch (t->match_n_vectors)
327 xor_sum ^= (data[8] & mask[8]) ^ (data[9] & mask[9]);
330 xor_sum ^= (data[6] & mask[6]) ^ (data[7] & mask[7]);
333 xor_sum ^= (data[4] & mask[4]) ^ (data[5] & mask[5]);
336 xor_sum ^= (data[2] & mask[2]) ^ (data[3] & mask[3]);
344 #endif /* CLIB_HAVE_VEC128 */
346 #ifdef clib_crc32c_uses_intrinsics
347 return clib_crc32c ((u8 *) & xor_sum, sizeof (xor_sum));
349 return clib_xxhash (xor_sum);
354 vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
358 ASSERT (is_pow2 (t->nbuckets));
360 bucket_index = hash & (t->nbuckets - 1);
362 clib_prefetch_load (&t->buckets[bucket_index]);
365 static inline vnet_classify_entry_t *
366 vnet_classify_get_entry (const vnet_classify_table_t *t, uword offset)
368 u8 *hp = clib_mem_get_heap_base (t->mheap);
369 u8 *vp = hp + offset;
371 return (vnet_classify_entry_t *) vp;
375 vnet_classify_get_offset (vnet_classify_table_t * t,
376 vnet_classify_entry_t * v)
380 hp = (u8 *) clib_mem_get_heap_base (t->mheap);
383 ASSERT ((vp - hp) < 0x100000000ULL);
387 static inline vnet_classify_entry_t *
388 vnet_classify_entry_at_index (const vnet_classify_table_t *t,
389 vnet_classify_entry_t *e, u32 index)
395 eu8 += index * (sizeof (vnet_classify_entry_t) +
396 (t->match_n_vectors * sizeof (u32x4)));
398 return (vnet_classify_entry_t *) eu8;
402 vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
406 vnet_classify_bucket_t *b;
407 vnet_classify_entry_t *e;
409 bucket_index = hash & (t->nbuckets - 1);
411 b = &t->buckets[bucket_index];
416 hash >>= t->log2_nbuckets;
418 e = vnet_classify_get_entry (t, b->offset);
419 value_index = hash & ((1 << b->log2_pages) - 1);
421 e = vnet_classify_entry_at_index (t, e, value_index);
423 clib_prefetch_load (e);
426 vnet_classify_entry_t *
427 vnet_classify_find_entry (const vnet_classify_table_t *t, u8 *h, u32 hash,
430 static_always_inline int
431 vnet_classify_entry_is_equal (vnet_classify_entry_t *v, const u8 *d, u8 *m,
432 u32 match_n_vectors, u16 load_mask)
434 #if defined(CLIB_HAVE_VEC512) && defined(CLIB_HAVE_VEC512_MASK_LOAD_STORE)
435 u64x8 r, *mask = (u64x8 *) m;
436 u64x8u *data = (u64x8u *) d;
437 u64x8 *key = (u64x8 *) v->key;
439 r = (u64x8_mask_load_zero (data, load_mask) & mask[0]) ^
440 u64x8_mask_load_zero (key, load_mask);
443 if (PREDICT_FALSE (load_mask))
444 r |= (u64x8_mask_load_zero (data + 1, load_mask) & mask[1]) ^
445 u64x8_mask_load_zero (key + 1, load_mask);
447 if (u64x8_is_all_zero (r))
450 #elif defined(CLIB_HAVE_VEC256) && defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
451 u64x4 r, *mask = (u64x4 *) m;
452 u64x4u *data = (u64x4u *) d;
453 u64x4 *key = (u64x4 *) v->key;
455 r = (u64x4_mask_load_zero (data, load_mask) & mask[0]) ^
456 u64x4_mask_load_zero (key, load_mask);
459 r |= (u64x4_mask_load_zero (data + 1, load_mask) & mask[1]) ^
460 u64x4_mask_load_zero (key + 1, load_mask);
463 if (PREDICT_FALSE (load_mask))
464 r |= (u64x4_mask_load_zero (data + 2, load_mask) & mask[2]) ^
465 u64x4_mask_load_zero (key + 2, load_mask);
467 if (u64x4_is_all_zero (r))
470 #elif defined(CLIB_HAVE_VEC128)
471 u64x2u *data = (u64x2 *) d;
472 u64x2 *key = (u64x2 *) v->key;
473 u64x2 *mask = (u64x2 *) m;
476 r = (data[0] & mask[0]) ^ key[0];
477 switch (match_n_vectors)
480 r |= (data[4] & mask[4]) ^ key[4];
483 r |= (data[3] & mask[3]) ^ key[3];
486 r |= (data[2] & mask[2]) ^ key[2];
489 r |= (data[1] & mask[1]) ^ key[1];
497 if (u64x2_is_all_zero (r))
501 u64 *data = (u64 *) d;
502 u64 *key = (u64 *) v->key;
503 u64 *mask = (u64 *) m;
506 r = ((data[0] & mask[0]) ^ key[0]) | ((data[1] & mask[1]) ^ key[1]);
507 switch (match_n_vectors)
510 r |= ((data[8] & mask[8]) ^ key[8]) | ((data[9] & mask[9]) ^ key[9]);
513 r |= ((data[6] & mask[6]) ^ key[6]) | ((data[7] & mask[7]) ^ key[7]);
516 r |= ((data[4] & mask[4]) ^ key[4]) | ((data[5] & mask[5]) ^ key[5]);
519 r |= ((data[2] & mask[2]) ^ key[2]) | ((data[3] & mask[3]) ^ key[3]);
530 #endif /* CLIB_HAVE_VEC128 */
534 static inline vnet_classify_entry_t *
535 vnet_classify_find_entry_inline (const vnet_classify_table_t *t, const u8 *h,
538 vnet_classify_entry_t *v;
539 vnet_classify_bucket_t *b;
540 u32 bucket_index, limit, pages, match_n_vectors = t->match_n_vectors;
541 u16 load_mask = t->load_mask;
542 u8 *mask = (u8 *) t->mask;
545 bucket_index = hash & (t->nbuckets - 1);
546 b = &t->buckets[bucket_index];
551 pages = 1 << b->log2_pages;
552 v = vnet_classify_get_entry (t, b->offset);
553 limit = t->entries_per_page;
554 if (PREDICT_FALSE (b->linear_search))
557 v = vnet_classify_entry_at_index (t, v, 0);
561 hash >>= t->log2_nbuckets;
562 v = vnet_classify_entry_at_index (t, v, hash & (pages - 1));
565 h += t->skip_n_vectors * 16;
567 for (i = 0; i < limit; i++)
569 if (vnet_classify_entry_is_equal (v, h, mask, match_n_vectors,
572 if (PREDICT_TRUE (now))
579 v = vnet_classify_entry_at_index (t, v, 1);
584 vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t *cm,
585 const u8 *mask, u32 nbuckets,
588 u32 match_n_vectors);
590 int vnet_classify_add_del_session (vnet_classify_main_t *cm, u32 table_index,
591 const u8 *match, u16 hit_next_index,
592 u32 opaque_index, i32 advance, u8 action,
593 u32 metadata, int is_add);
595 int vnet_classify_add_del_table (vnet_classify_main_t *cm, const u8 *mask,
596 u32 nbuckets, u32 memory_size, u32 skip,
597 u32 match, u32 next_table_index,
598 u32 miss_next_index, u32 *table_index,
599 u8 current_data_flag, i16 current_data_offset,
600 int is_add, int del_chain);
601 void vnet_classify_delete_table_index (vnet_classify_main_t *cm,
602 u32 table_index, int del_chain);
604 unformat_function_t unformat_ip4_mask;
605 unformat_function_t unformat_ip6_mask;
606 unformat_function_t unformat_l3_mask;
607 unformat_function_t unformat_l2_mask;
608 unformat_function_t unformat_classify_mask;
609 unformat_function_t unformat_l2_next_index;
610 unformat_function_t unformat_ip_next_index;
611 unformat_function_t unformat_ip4_match;
612 unformat_function_t unformat_ip6_match;
613 unformat_function_t unformat_l3_match;
614 unformat_function_t unformat_l4_match;
615 unformat_function_t unformat_vlan_tag;
616 unformat_function_t unformat_l2_match;
617 unformat_function_t unformat_classify_match;
619 void vnet_classify_register_unformat_ip_next_index_fn
620 (unformat_function_t * fn);
622 void vnet_classify_register_unformat_l2_next_index_fn
623 (unformat_function_t * fn);
625 void vnet_classify_register_unformat_acl_next_index_fn
626 (unformat_function_t * fn);
628 void vnet_classify_register_unformat_policer_next_index_fn
629 (unformat_function_t * fn);
631 void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
634 u32 classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index);
635 void classify_set_pcap_chain (vnet_classify_main_t * cm,
636 u32 sw_if_index, u32 table_index);
638 u32 classify_get_trace_chain (void);
639 void classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index);
641 u32 classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index);
642 u32 classify_lookup_chain (u32 table_index,
643 u8 * mask, u32 n_skip, u32 n_match);
645 #endif /* __included_vnet_classify_h__ */
648 * fd.io coding-style-patch-verification: ON
651 * eval: (c-set-style "gnu")