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__
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/ethernet/packet.h>
25 #include <vnet/ip/ip_packet.h>
26 #include <vnet/ip/ip4_packet.h>
27 #include <vnet/ip/ip6_packet.h>
29 #include <vnet/l2/l2_input.h>
30 #include <vnet/l2/feat_bitmap.h>
31 #include <vnet/api_errno.h> /* for API error numbers */
33 #include <vppinfra/error.h>
34 #include <vppinfra/hash.h>
35 #include <vppinfra/cache.h>
36 #include <vppinfra/xxhash.h>
38 extern vlib_node_registration_t ip4_classify_node;
39 extern vlib_node_registration_t ip6_classify_node;
41 #define CLASSIFY_TRACE 0
43 #if !defined( __aarch64__) && !defined(__arm__)
44 #define CLASSIFY_USE_SSE //Allow usage of SSE operations
47 #define U32X4_ALIGNED(p) PREDICT_TRUE((((intptr_t)p) & 0xf) == 0)
50 * Classify table option to process packets
51 * CLASSIFY_FLAG_USE_CURR_DATA:
52 * - classify packets starting from VPP node’s current data pointer
54 #define CLASSIFY_FLAG_USE_CURR_DATA 1
57 * Classify session action
58 * CLASSIFY_ACTION_SET_IP4_FIB_INDEX:
59 * - Classified IP packets will be looked up
60 * from the specified ipv4 fib table
61 * CLASSIFY_ACTION_SET_IP6_FIB_INDEX:
62 * - Classified IP packets will be looked up
63 * from the specified ipv6 fib table
65 typedef enum vnet_classify_action_t_
67 CLASSIFY_ACTION_SET_IP4_FIB_INDEX = 1,
68 CLASSIFY_ACTION_SET_IP6_FIB_INDEX = 2,
69 CLASSIFY_ACTION_SET_SR_POLICY_INDEX = 3,
70 } __attribute__ ((packed)) vnet_classify_action_t;
72 struct _vnet_classify_main;
73 typedef struct _vnet_classify_main vnet_classify_main_t;
75 #define foreach_size_in_u32x4 \
82 typedef CLIB_PACKED(struct _vnet_classify_entry {
83 /* Graph node next index */
86 /* put into vnet_buffer(b)->l2_classfy.opaque_index */
90 /* advance on hit, note it's a signed quantity... */
96 /* Really only need 1 bit */
98 #define VNET_CLASSIFY_ENTRY_FREE (1<<0)
100 vnet_classify_action_t action;
103 /* Hit counter, last heard time */
106 struct _vnet_classify_entry * next_free;
111 /* Must be aligned to a 16-octet boundary */
113 }) vnet_classify_entry_t;
115 static inline int vnet_classify_entry_is_free (vnet_classify_entry_t * e)
117 return e->flags & VNET_CLASSIFY_ENTRY_FREE;
120 static inline int vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
122 return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
125 /* Need these to con the vector allocator */
127 typedef CLIB_PACKED(struct { \
131 }) vnet_classify_entry_##size##_t;
132 foreach_size_in_u32x4;
145 } vnet_classify_bucket_t;
148 /* Mask to apply after skipping N vectors */
150 /* Buckets and entries */
151 vnet_classify_bucket_t * buckets;
152 vnet_classify_entry_t * entries;
154 /* Config parameters */
160 int entries_per_page;
162 u32 current_data_flag;
163 int current_data_offset;
165 /* Index of next table to try */
166 u32 next_table_index;
168 /* Miss next index, return if next_table_index = 0 */
171 /* Per-bucket working copies, one per thread */
172 vnet_classify_entry_t ** working_copies;
173 int *working_copy_lengths;
174 vnet_classify_bucket_t saved_bucket;
176 /* Free entry freelists */
177 vnet_classify_entry_t **freelists;
181 /* Private allocation arena, protected by the writer lock */
184 /* Writer (only) lock for this table */
185 volatile u32 * writer_lock;
187 } vnet_classify_table_t;
189 struct _vnet_classify_main {
191 vnet_classify_table_t * tables;
193 /* Registered next-index, opaque unformat fcns */
194 unformat_function_t ** unformat_l2_next_index_fns;
195 unformat_function_t ** unformat_ip_next_index_fns;
196 unformat_function_t ** unformat_acl_next_index_fns;
197 unformat_function_t ** unformat_policer_next_index_fns;
198 unformat_function_t ** unformat_opaque_index_fns;
200 /* convenience variables */
201 vlib_main_t * vlib_main;
202 vnet_main_t * vnet_main;
205 extern vnet_classify_main_t vnet_classify_main;
207 u8 * format_classify_table (u8 * s, va_list * args);
209 u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h);
212 vnet_classify_hash_packet_inline (vnet_classify_table_t * t,
220 } xor_sum __attribute__((aligned(sizeof(u32x4))));
224 #ifdef CLASSIFY_USE_SSE
225 if (U32X4_ALIGNED(h)) { //SSE can't handle unaligned data
226 u32x4 *data = (u32x4 *)h;
227 xor_sum.as_u32x4 = data[0 + t->skip_n_vectors] & mask[0];
228 switch (t->match_n_vectors)
231 xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4];
234 xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3];
237 xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2];
240 xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1];
248 #endif /* CLASSIFY_USE_SSE */
250 u32 skip_u64 = t->skip_n_vectors * 2;
251 u64 *data64 = (u64 *)h;
252 xor_sum.as_u64[0] = data64[0 + skip_u64] & ((u64 *)mask)[0];
253 xor_sum.as_u64[1] = data64[1 + skip_u64] & ((u64 *)mask)[1];
254 switch (t->match_n_vectors)
257 xor_sum.as_u64[0] ^= data64[8 + skip_u64] & ((u64 *)mask)[8];
258 xor_sum.as_u64[1] ^= data64[9 + skip_u64] & ((u64 *)mask)[9];
261 xor_sum.as_u64[0] ^= data64[6 + skip_u64] & ((u64 *)mask)[6];
262 xor_sum.as_u64[1] ^= data64[7 + skip_u64] & ((u64 *)mask)[7];
265 xor_sum.as_u64[0] ^= data64[4 + skip_u64] & ((u64 *)mask)[4];
266 xor_sum.as_u64[1] ^= data64[5 + skip_u64] & ((u64 *)mask)[5];
269 xor_sum.as_u64[0] ^= data64[2 + skip_u64] & ((u64 *)mask)[2];
270 xor_sum.as_u64[1] ^= data64[3 + skip_u64] & ((u64 *)mask)[3];
280 return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
284 vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
288 ASSERT (is_pow2(t->nbuckets));
290 bucket_index = hash & (t->nbuckets - 1);
292 CLIB_PREFETCH(&t->buckets[bucket_index], CLIB_CACHE_LINE_BYTES, LOAD);
295 static inline vnet_classify_entry_t *
296 vnet_classify_get_entry (vnet_classify_table_t * t, uword offset)
299 u8 * vp = hp + offset;
304 static inline uword vnet_classify_get_offset (vnet_classify_table_t * t,
305 vnet_classify_entry_t * v)
309 hp = (u8 *) 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,
325 eu8 += index * (sizeof (vnet_classify_entry_t) +
326 (t->match_n_vectors * sizeof (u32x4)));
328 return (vnet_classify_entry_t *) eu8;
332 vnet_classify_prefetch_entry (vnet_classify_table_t * t,
337 vnet_classify_bucket_t * b;
338 vnet_classify_entry_t * e;
340 bucket_index = hash & (t->nbuckets - 1);
342 b = &t->buckets[bucket_index];
347 hash >>= t->log2_nbuckets;
349 e = vnet_classify_get_entry (t, b->offset);
350 value_index = hash & ((1<<b->log2_pages)-1);
352 e = vnet_classify_entry_at_index (t, e, value_index);
354 CLIB_PREFETCH(e, CLIB_CACHE_LINE_BYTES, LOAD);
357 vnet_classify_entry_t *
358 vnet_classify_find_entry (vnet_classify_table_t * t,
359 u8 * h, u64 hash, f64 now);
361 static inline vnet_classify_entry_t *
362 vnet_classify_find_entry_inline (vnet_classify_table_t * t,
363 u8 * h, u64 hash, f64 now)
365 vnet_classify_entry_t * v;
370 } result __attribute__((aligned(sizeof(u32x4))));
371 vnet_classify_bucket_t * b;
377 bucket_index = hash & (t->nbuckets-1);
378 b = &t->buckets[bucket_index];
384 hash >>= t->log2_nbuckets;
386 v = vnet_classify_get_entry (t, b->offset);
387 value_index = hash & ((1<<b->log2_pages)-1);
388 limit = t->entries_per_page;
389 if (PREDICT_FALSE (b->linear_search))
392 limit *= (1<<b->log2_pages);
395 v = vnet_classify_entry_at_index (t, v, value_index);
397 #ifdef CLASSIFY_USE_SSE
398 if (U32X4_ALIGNED(h)) {
399 u32x4 *data = (u32x4 *) h;
400 for (i = 0; i < limit; i++) {
402 result.as_u32x4 = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
403 switch (t->match_n_vectors)
406 result.as_u32x4 |= (data[4 + t->skip_n_vectors] & mask[4]) ^ key[4];
409 result.as_u32x4 |= (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3];
412 result.as_u32x4 |= (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
415 result.as_u32x4 |= (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
423 if (u32x4_zero_byte_mask (result.as_u32x4) == 0xffff) {
424 if (PREDICT_TRUE(now)) {
430 v = vnet_classify_entry_at_index (t, v, 1);
433 #endif /* CLASSIFY_USE_SSE */
435 u32 skip_u64 = t->skip_n_vectors * 2;
436 u64 *data64 = (u64 *)h;
437 for (i = 0; i < limit; i++) {
440 result.as_u64[0] = (data64[0 + skip_u64] & ((u64 *)mask)[0]) ^ ((u64 *)key)[0];
441 result.as_u64[1] = (data64[1 + skip_u64] & ((u64 *)mask)[1]) ^ ((u64 *)key)[1];
442 switch (t->match_n_vectors)
445 result.as_u64[0] |= (data64[8 + skip_u64] & ((u64 *)mask)[8]) ^ ((u64 *)key)[8];
446 result.as_u64[1] |= (data64[9 + skip_u64] & ((u64 *)mask)[9]) ^ ((u64 *)key)[9];
449 result.as_u64[0] |= (data64[6 + skip_u64] & ((u64 *)mask)[6]) ^ ((u64 *)key)[6];
450 result.as_u64[1] |= (data64[7 + skip_u64] & ((u64 *)mask)[7]) ^ ((u64 *)key)[7];
453 result.as_u64[0] |= (data64[4 + skip_u64] & ((u64 *)mask)[4]) ^ ((u64 *)key)[4];
454 result.as_u64[1] |= (data64[5 + skip_u64] & ((u64 *)mask)[5]) ^ ((u64 *)key)[5];
457 result.as_u64[0] |= (data64[2 + skip_u64] & ((u64 *)mask)[2]) ^ ((u64 *)key)[2];
458 result.as_u64[1] |= (data64[3 + skip_u64] & ((u64 *)mask)[3]) ^ ((u64 *)key)[3];
466 if (result.as_u64[0] == 0 && result.as_u64[1] == 0) {
467 if (PREDICT_TRUE(now)) {
474 v = vnet_classify_entry_at_index (t, v, 1);
480 vnet_classify_table_t *
481 vnet_classify_new_table (vnet_classify_main_t *cm,
482 u8 * mask, u32 nbuckets, u32 memory_size,
484 u32 match_n_vectors);
486 int vnet_classify_add_del_session (vnet_classify_main_t * cm,
496 int vnet_classify_add_del_table (vnet_classify_main_t * cm,
502 u32 next_table_index,
505 u8 current_data_flag,
506 i16 current_data_offset,
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 * fn);
539 #endif /* __included_vnet_classify_h__ */