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 #define CLASSIFY_FLAG_USE_CURR_DATA 1
40 * Classify session action
41 * CLASSIFY_ACTION_SET_IP4_FIB_INDEX:
42 * - Classified IP packets will be looked up
43 * from the specified ipv4 fib table
44 * CLASSIFY_ACTION_SET_IP6_FIB_INDEX:
45 * - Classified IP packets will be looked up
46 * from the specified ipv6 fib table
48 typedef enum vnet_classify_action_t_
50 CLASSIFY_ACTION_SET_IP4_FIB_INDEX = 1,
51 CLASSIFY_ACTION_SET_IP6_FIB_INDEX = 2,
52 CLASSIFY_ACTION_SET_METADATA = 3,
53 } __attribute__ ((packed)) vnet_classify_action_t;
55 struct _vnet_classify_main;
56 typedef struct _vnet_classify_main vnet_classify_main_t;
58 #define foreach_size_in_u32x4 \
66 typedef CLIB_PACKED(struct _vnet_classify_entry {
67 /* Graph node next index */
70 /* put into vnet_buffer(b)->l2_classfy.opaque_index */
74 /* advance on hit, note it's a signed quantity... */
80 /* Really only need 1 bit */
82 #define VNET_CLASSIFY_ENTRY_FREE (1<<0)
84 vnet_classify_action_t action;
87 /* Hit counter, last heard time */
90 struct _vnet_classify_entry * next_free;
95 /* Must be aligned to a 16-octet boundary */
97 }) vnet_classify_entry_t;
101 vnet_classify_entry_is_free (vnet_classify_entry_t * e)
103 return e->flags & VNET_CLASSIFY_ENTRY_FREE;
107 vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
109 return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
112 /* Need these to con the vector allocator */
115 typedef CLIB_PACKED(struct { \
119 }) vnet_classify_entry_##size##_t;
120 foreach_size_in_u32x4;
137 } vnet_classify_bucket_t;
141 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
142 /* Mask to apply after skipping N vectors */
144 /* Buckets and entries */
145 vnet_classify_bucket_t *buckets;
146 vnet_classify_entry_t *entries;
148 /* Config parameters */
154 int entries_per_page;
156 u32 current_data_flag;
157 int current_data_offset;
159 /* Index of next table to try */
160 u32 next_table_index;
162 /* Miss next index, return if next_table_index = 0 */
165 /* Per-bucket working copies, one per thread */
166 vnet_classify_entry_t **working_copies;
167 int *working_copy_lengths;
168 vnet_classify_bucket_t saved_bucket;
170 /* Free entry freelists */
171 vnet_classify_entry_t **freelists;
175 /* Private allocation arena, protected by the writer lock */
178 /* Writer (only) lock for this table */
179 clib_spinlock_t writer_lock;
181 } vnet_classify_table_t;
183 struct _vnet_classify_main
186 vnet_classify_table_t *tables;
188 /* Registered next-index, opaque unformat fcns */
189 unformat_function_t **unformat_l2_next_index_fns;
190 unformat_function_t **unformat_ip_next_index_fns;
191 unformat_function_t **unformat_acl_next_index_fns;
192 unformat_function_t **unformat_policer_next_index_fns;
193 unformat_function_t **unformat_opaque_index_fns;
195 /* Per-interface filter table. [0] is used for pcap */
196 u32 *classify_table_index_by_sw_if_index;
198 /* convenience variables */
199 vlib_main_t *vlib_main;
200 vnet_main_t *vnet_main;
203 extern vnet_classify_main_t vnet_classify_main;
205 u8 *format_classify_table (u8 * s, va_list * args);
207 u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h);
210 vnet_classify_hash_packet_inline (vnet_classify_table_t * t, u8 * h)
218 } xor_sum __attribute__ ((aligned (sizeof (u32x4))));
222 #ifdef CLIB_HAVE_VEC128
223 u32x4u *data = (u32x4u *) h;
224 xor_sum.as_u32x4 = data[0 + t->skip_n_vectors] & mask[0];
225 switch (t->match_n_vectors)
228 xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4];
231 xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3];
234 xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2];
237 xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1];
245 u32 skip_u64 = t->skip_n_vectors * 2;
246 u64 *data64 = (u64 *) h;
247 xor_sum.as_u64[0] = data64[0 + skip_u64] & ((u64 *) mask)[0];
248 xor_sum.as_u64[1] = data64[1 + skip_u64] & ((u64 *) mask)[1];
249 switch (t->match_n_vectors)
252 xor_sum.as_u64[0] ^= data64[8 + skip_u64] & ((u64 *) mask)[8];
253 xor_sum.as_u64[1] ^= data64[9 + skip_u64] & ((u64 *) mask)[9];
256 xor_sum.as_u64[0] ^= data64[6 + skip_u64] & ((u64 *) mask)[6];
257 xor_sum.as_u64[1] ^= data64[7 + skip_u64] & ((u64 *) mask)[7];
260 xor_sum.as_u64[0] ^= data64[4 + skip_u64] & ((u64 *) mask)[4];
261 xor_sum.as_u64[1] ^= data64[5 + skip_u64] & ((u64 *) mask)[5];
264 xor_sum.as_u64[0] ^= data64[2 + skip_u64] & ((u64 *) mask)[2];
265 xor_sum.as_u64[1] ^= data64[3 + skip_u64] & ((u64 *) mask)[3];
273 #endif /* CLIB_HAVE_VEC128 */
275 #ifdef clib_crc32c_uses_intrinsics
276 return clib_crc32c ((u8 *) & xor_sum, sizeof (xor_sum));
278 return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
283 vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
287 ASSERT (is_pow2 (t->nbuckets));
289 bucket_index = hash & (t->nbuckets - 1);
291 CLIB_PREFETCH (&t->buckets[bucket_index], CLIB_CACHE_LINE_BYTES, LOAD);
294 static inline vnet_classify_entry_t *
295 vnet_classify_get_entry (vnet_classify_table_t * t, uword offset)
297 u8 *hp = clib_mem_get_heap_base (t->mheap);
298 u8 *vp = hp + offset;
304 vnet_classify_get_offset (vnet_classify_table_t * t,
305 vnet_classify_entry_t * v)
309 hp = (u8 *) clib_mem_get_heap_base (t->mheap);
312 ASSERT ((vp - hp) < 0x100000000ULL);
316 static inline vnet_classify_entry_t *
317 vnet_classify_entry_at_index (vnet_classify_table_t * t,
318 vnet_classify_entry_t * e, u32 index)
324 eu8 += index * (sizeof (vnet_classify_entry_t) +
325 (t->match_n_vectors * sizeof (u32x4)));
327 return (vnet_classify_entry_t *) eu8;
331 vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
335 vnet_classify_bucket_t *b;
336 vnet_classify_entry_t *e;
338 bucket_index = hash & (t->nbuckets - 1);
340 b = &t->buckets[bucket_index];
345 hash >>= t->log2_nbuckets;
347 e = vnet_classify_get_entry (t, b->offset);
348 value_index = hash & ((1 << b->log2_pages) - 1);
350 e = vnet_classify_entry_at_index (t, e, value_index);
352 CLIB_PREFETCH (e, CLIB_CACHE_LINE_BYTES, LOAD);
355 vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t,
356 u8 * h, u64 hash, f64 now);
358 static inline vnet_classify_entry_t *
359 vnet_classify_find_entry_inline (vnet_classify_table_t * t,
360 u8 * h, u64 hash, f64 now)
362 vnet_classify_entry_t *v;
368 } result __attribute__ ((aligned (sizeof (u32x4))));
369 vnet_classify_bucket_t *b;
375 bucket_index = hash & (t->nbuckets - 1);
376 b = &t->buckets[bucket_index];
382 hash >>= t->log2_nbuckets;
384 v = vnet_classify_get_entry (t, b->offset);
385 value_index = hash & ((1 << b->log2_pages) - 1);
386 limit = t->entries_per_page;
387 if (PREDICT_FALSE (b->linear_search))
390 limit *= (1 << b->log2_pages);
393 v = vnet_classify_entry_at_index (t, v, value_index);
395 #ifdef CLIB_HAVE_VEC128
396 u32x4u *data = (u32x4u *) h;
397 for (i = 0; i < limit; i++)
400 result.as_u32x4 = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
401 switch (t->match_n_vectors)
404 result.as_u32x4 |= (data[4 + t->skip_n_vectors] & mask[4]) ^ key[4];
407 result.as_u32x4 |= (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3];
410 result.as_u32x4 |= (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
413 result.as_u32x4 |= (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
421 if (u32x4_zero_byte_mask (result.as_u32x4) == 0xffff)
423 if (PREDICT_TRUE (now))
430 v = vnet_classify_entry_at_index (t, v, 1);
433 u32 skip_u64 = t->skip_n_vectors * 2;
434 u64 *data64 = (u64 *) h;
435 for (i = 0; i < limit; i++)
440 (data64[0 + skip_u64] & ((u64 *) mask)[0]) ^ ((u64 *) key)[0];
442 (data64[1 + skip_u64] & ((u64 *) mask)[1]) ^ ((u64 *) key)[1];
443 switch (t->match_n_vectors)
447 (data64[8 + skip_u64] & ((u64 *) mask)[8]) ^ ((u64 *) key)[8];
449 (data64[9 + skip_u64] & ((u64 *) mask)[9]) ^ ((u64 *) key)[9];
453 (data64[6 + skip_u64] & ((u64 *) mask)[6]) ^ ((u64 *) key)[6];
455 (data64[7 + skip_u64] & ((u64 *) mask)[7]) ^ ((u64 *) key)[7];
459 (data64[4 + skip_u64] & ((u64 *) mask)[4]) ^ ((u64 *) key)[4];
461 (data64[5 + skip_u64] & ((u64 *) mask)[5]) ^ ((u64 *) key)[5];
465 (data64[2 + skip_u64] & ((u64 *) mask)[2]) ^ ((u64 *) key)[2];
467 (data64[3 + skip_u64] & ((u64 *) mask)[3]) ^ ((u64 *) key)[3];
475 if (result.as_u64[0] == 0 && result.as_u64[1] == 0)
477 if (PREDICT_TRUE (now))
485 v = vnet_classify_entry_at_index (t, v, 1);
487 #endif /* CLIB_HAVE_VEC128 */
491 vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t * cm,
492 u8 * mask, u32 nbuckets,
495 u32 match_n_vectors);
497 int vnet_classify_add_del_session (vnet_classify_main_t * cm,
503 u8 action, u32 metadata, int is_add);
505 int vnet_classify_add_del_table (vnet_classify_main_t * cm,
511 u32 next_table_index,
514 u8 current_data_flag,
515 i16 current_data_offset,
516 int is_add, int del_chain);
518 unformat_function_t unformat_ip4_mask;
519 unformat_function_t unformat_ip6_mask;
520 unformat_function_t unformat_l3_mask;
521 unformat_function_t unformat_l2_mask;
522 unformat_function_t unformat_classify_mask;
523 unformat_function_t unformat_l2_next_index;
524 unformat_function_t unformat_ip_next_index;
525 unformat_function_t unformat_ip4_match;
526 unformat_function_t unformat_ip6_match;
527 unformat_function_t unformat_l3_match;
528 unformat_function_t unformat_l4_match;
529 unformat_function_t unformat_vlan_tag;
530 unformat_function_t unformat_l2_match;
531 unformat_function_t unformat_classify_match;
533 void vnet_classify_register_unformat_ip_next_index_fn
534 (unformat_function_t * fn);
536 void vnet_classify_register_unformat_l2_next_index_fn
537 (unformat_function_t * fn);
539 void vnet_classify_register_unformat_acl_next_index_fn
540 (unformat_function_t * fn);
542 void vnet_classify_register_unformat_policer_next_index_fn
543 (unformat_function_t * fn);
545 void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
548 u32 classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index);
549 void classify_set_pcap_chain (vnet_classify_main_t * cm,
550 u32 sw_if_index, u32 table_index);
552 u32 classify_get_trace_chain (void);
553 void classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index);
555 u32 classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index);
556 u32 classify_lookup_chain (u32 table_index,
557 u8 * mask, u32 n_skip, u32 n_match);
559 #endif /* __included_vnet_classify_h__ */
562 * fd.io coding-style-patch-verification: ON
565 * eval: (c-set-style "gnu")