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/l2_output.h>
31 #include <vnet/l2/feat_bitmap.h>
32 #include <vnet/api_errno.h> /* for API error numbers */
34 #include <vppinfra/error.h>
35 #include <vppinfra/hash.h>
36 #include <vppinfra/cache.h>
37 #include <vppinfra/xxhash.h>
39 extern vlib_node_registration_t ip4_classify_node;
40 extern vlib_node_registration_t ip6_classify_node;
42 #define CLASSIFY_TRACE 0
44 #if !defined( __aarch64__) && !defined(__arm__)
45 #define CLASSIFY_USE_SSE //Allow usage of SSE operations
48 #define U32X4_ALIGNED(p) PREDICT_TRUE((((intptr_t)p) & 0xf) == 0)
51 * Classify table option to process packets
52 * CLASSIFY_FLAG_USE_CURR_DATA:
53 * - classify packets starting from VPP node’s current data pointer
55 #define CLASSIFY_FLAG_USE_CURR_DATA 1
58 * Classify session action
59 * CLASSIFY_ACTION_SET_IP4_FIB_INDEX:
60 * - Classified IP packets will be looked up
61 * from the specified ipv4 fib table
62 * CLASSIFY_ACTION_SET_IP6_FIB_INDEX:
63 * - Classified IP packets will be looked up
64 * from the specified ipv6 fib table
66 typedef enum vnet_classify_action_t_
68 CLASSIFY_ACTION_SET_IP4_FIB_INDEX = 1,
69 CLASSIFY_ACTION_SET_IP6_FIB_INDEX = 2,
70 CLASSIFY_ACTION_SET_METADATA = 3,
71 } __attribute__ ((packed)) vnet_classify_action_t;
73 struct _vnet_classify_main;
74 typedef struct _vnet_classify_main vnet_classify_main_t;
76 #define foreach_size_in_u32x4 \
84 typedef CLIB_PACKED(struct _vnet_classify_entry {
85 /* Graph node next index */
88 /* put into vnet_buffer(b)->l2_classfy.opaque_index */
92 /* advance on hit, note it's a signed quantity... */
98 /* Really only need 1 bit */
100 #define VNET_CLASSIFY_ENTRY_FREE (1<<0)
102 vnet_classify_action_t action;
105 /* Hit counter, last heard time */
108 struct _vnet_classify_entry * next_free;
113 /* Must be aligned to a 16-octet boundary */
115 }) vnet_classify_entry_t;
119 vnet_classify_entry_is_free (vnet_classify_entry_t * e)
121 return e->flags & VNET_CLASSIFY_ENTRY_FREE;
125 vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
127 return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
130 /* Need these to con the vector allocator */
133 typedef CLIB_PACKED(struct { \
137 }) vnet_classify_entry_##size##_t;
138 foreach_size_in_u32x4;
155 } vnet_classify_bucket_t;
159 /* Mask to apply after skipping N vectors */
161 /* Buckets and entries */
162 vnet_classify_bucket_t *buckets;
163 vnet_classify_entry_t *entries;
165 /* Config parameters */
171 int entries_per_page;
173 u32 current_data_flag;
174 int current_data_offset;
176 /* Index of next table to try */
177 u32 next_table_index;
179 /* Miss next index, return if next_table_index = 0 */
182 /* Per-bucket working copies, one per thread */
183 vnet_classify_entry_t **working_copies;
184 int *working_copy_lengths;
185 vnet_classify_bucket_t saved_bucket;
187 /* Free entry freelists */
188 vnet_classify_entry_t **freelists;
192 /* Private allocation arena, protected by the writer lock */
195 /* Writer (only) lock for this table */
196 volatile u32 *writer_lock;
198 } vnet_classify_table_t;
200 struct _vnet_classify_main
203 vnet_classify_table_t *tables;
205 /* Registered next-index, opaque unformat fcns */
206 unformat_function_t **unformat_l2_next_index_fns;
207 unformat_function_t **unformat_ip_next_index_fns;
208 unformat_function_t **unformat_acl_next_index_fns;
209 unformat_function_t **unformat_policer_next_index_fns;
210 unformat_function_t **unformat_opaque_index_fns;
212 /* convenience variables */
213 vlib_main_t *vlib_main;
214 vnet_main_t *vnet_main;
217 extern vnet_classify_main_t vnet_classify_main;
219 u8 *format_classify_table (u8 * s, va_list * args);
221 u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h);
224 vnet_classify_hash_packet_inline (vnet_classify_table_t * t, u8 * h)
232 } xor_sum __attribute__ ((aligned (sizeof (u32x4))));
236 #ifdef CLASSIFY_USE_SSE
237 if (U32X4_ALIGNED (h))
238 { //SSE can't handle unaligned data
239 u32x4 *data = (u32x4 *) h;
240 xor_sum.as_u32x4 = data[0 + t->skip_n_vectors] & mask[0];
241 switch (t->match_n_vectors)
244 xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4];
247 xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3];
250 xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2];
253 xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1];
262 #endif /* CLASSIFY_USE_SSE */
264 u32 skip_u64 = t->skip_n_vectors * 2;
265 u64 *data64 = (u64 *) h;
266 xor_sum.as_u64[0] = data64[0 + skip_u64] & ((u64 *) mask)[0];
267 xor_sum.as_u64[1] = data64[1 + skip_u64] & ((u64 *) mask)[1];
268 switch (t->match_n_vectors)
271 xor_sum.as_u64[0] ^= data64[8 + skip_u64] & ((u64 *) mask)[8];
272 xor_sum.as_u64[1] ^= data64[9 + skip_u64] & ((u64 *) mask)[9];
275 xor_sum.as_u64[0] ^= data64[6 + skip_u64] & ((u64 *) mask)[6];
276 xor_sum.as_u64[1] ^= data64[7 + skip_u64] & ((u64 *) mask)[7];
279 xor_sum.as_u64[0] ^= data64[4 + skip_u64] & ((u64 *) mask)[4];
280 xor_sum.as_u64[1] ^= data64[5 + skip_u64] & ((u64 *) mask)[5];
283 xor_sum.as_u64[0] ^= data64[2 + skip_u64] & ((u64 *) mask)[2];
284 xor_sum.as_u64[1] ^= data64[3 + skip_u64] & ((u64 *) mask)[3];
294 return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
298 vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
302 ASSERT (is_pow2 (t->nbuckets));
304 bucket_index = hash & (t->nbuckets - 1);
306 CLIB_PREFETCH (&t->buckets[bucket_index], CLIB_CACHE_LINE_BYTES, LOAD);
309 static inline vnet_classify_entry_t *
310 vnet_classify_get_entry (vnet_classify_table_t * t, uword offset)
313 u8 *vp = hp + offset;
319 vnet_classify_get_offset (vnet_classify_table_t * t,
320 vnet_classify_entry_t * v)
324 hp = (u8 *) t->mheap;
327 ASSERT ((vp - hp) < 0x100000000ULL);
331 static inline vnet_classify_entry_t *
332 vnet_classify_entry_at_index (vnet_classify_table_t * t,
333 vnet_classify_entry_t * e, u32 index)
339 eu8 += index * (sizeof (vnet_classify_entry_t) +
340 (t->match_n_vectors * sizeof (u32x4)));
342 return (vnet_classify_entry_t *) eu8;
346 vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
350 vnet_classify_bucket_t *b;
351 vnet_classify_entry_t *e;
353 bucket_index = hash & (t->nbuckets - 1);
355 b = &t->buckets[bucket_index];
360 hash >>= t->log2_nbuckets;
362 e = vnet_classify_get_entry (t, b->offset);
363 value_index = hash & ((1 << b->log2_pages) - 1);
365 e = vnet_classify_entry_at_index (t, e, value_index);
367 CLIB_PREFETCH (e, CLIB_CACHE_LINE_BYTES, LOAD);
370 vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t,
371 u8 * h, u64 hash, f64 now);
373 static inline vnet_classify_entry_t *
374 vnet_classify_find_entry_inline (vnet_classify_table_t * t,
375 u8 * h, u64 hash, f64 now)
377 vnet_classify_entry_t *v;
383 } result __attribute__ ((aligned (sizeof (u32x4))));
384 vnet_classify_bucket_t *b;
390 bucket_index = hash & (t->nbuckets - 1);
391 b = &t->buckets[bucket_index];
397 hash >>= t->log2_nbuckets;
399 v = vnet_classify_get_entry (t, b->offset);
400 value_index = hash & ((1 << b->log2_pages) - 1);
401 limit = t->entries_per_page;
402 if (PREDICT_FALSE (b->linear_search))
405 limit *= (1 << b->log2_pages);
408 v = vnet_classify_entry_at_index (t, v, value_index);
410 #ifdef CLASSIFY_USE_SSE
411 if (U32X4_ALIGNED (h))
413 u32x4 *data = (u32x4 *) h;
414 for (i = 0; i < limit; i++)
417 result.as_u32x4 = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
418 switch (t->match_n_vectors)
422 (data[4 + t->skip_n_vectors] & mask[4]) ^ key[4];
426 (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3];
430 (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
434 (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
442 if (u32x4_zero_byte_mask (result.as_u32x4) == 0xffff)
444 if (PREDICT_TRUE (now))
451 v = vnet_classify_entry_at_index (t, v, 1);
455 #endif /* CLASSIFY_USE_SSE */
457 u32 skip_u64 = t->skip_n_vectors * 2;
458 u64 *data64 = (u64 *) h;
459 for (i = 0; i < limit; i++)
464 (data64[0 + skip_u64] & ((u64 *) mask)[0]) ^ ((u64 *) key)[0];
466 (data64[1 + skip_u64] & ((u64 *) mask)[1]) ^ ((u64 *) key)[1];
467 switch (t->match_n_vectors)
471 (data64[8 + skip_u64] & ((u64 *) mask)[8]) ^ ((u64 *) key)[8];
473 (data64[9 + skip_u64] & ((u64 *) mask)[9]) ^ ((u64 *) key)[9];
477 (data64[6 + skip_u64] & ((u64 *) mask)[6]) ^ ((u64 *) key)[6];
479 (data64[7 + skip_u64] & ((u64 *) mask)[7]) ^ ((u64 *) key)[7];
483 (data64[4 + skip_u64] & ((u64 *) mask)[4]) ^ ((u64 *) key)[4];
485 (data64[5 + skip_u64] & ((u64 *) mask)[5]) ^ ((u64 *) key)[5];
489 (data64[2 + skip_u64] & ((u64 *) mask)[2]) ^ ((u64 *) key)[2];
491 (data64[3 + skip_u64] & ((u64 *) mask)[3]) ^ ((u64 *) key)[3];
499 if (result.as_u64[0] == 0 && result.as_u64[1] == 0)
501 if (PREDICT_TRUE (now))
509 v = vnet_classify_entry_at_index (t, v, 1);
515 vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t * cm,
516 u8 * mask, u32 nbuckets,
519 u32 match_n_vectors);
521 int vnet_classify_add_del_session (vnet_classify_main_t * cm,
527 u8 action, u32 metadata, int is_add);
529 int vnet_classify_add_del_table (vnet_classify_main_t * cm,
535 u32 next_table_index,
538 u8 current_data_flag,
539 i16 current_data_offset,
540 int is_add, int del_chain);
542 unformat_function_t unformat_ip4_mask;
543 unformat_function_t unformat_ip6_mask;
544 unformat_function_t unformat_l3_mask;
545 unformat_function_t unformat_l2_mask;
546 unformat_function_t unformat_classify_mask;
547 unformat_function_t unformat_l2_next_index;
548 unformat_function_t unformat_ip_next_index;
549 unformat_function_t unformat_ip4_match;
550 unformat_function_t unformat_ip6_match;
551 unformat_function_t unformat_l3_match;
552 unformat_function_t unformat_l4_match;
553 unformat_function_t unformat_vlan_tag;
554 unformat_function_t unformat_l2_match;
555 unformat_function_t unformat_classify_match;
557 void vnet_classify_register_unformat_ip_next_index_fn
558 (unformat_function_t * fn);
560 void vnet_classify_register_unformat_l2_next_index_fn
561 (unformat_function_t * fn);
563 void vnet_classify_register_unformat_acl_next_index_fn
564 (unformat_function_t * fn);
566 void vnet_classify_register_unformat_policer_next_index_fn
567 (unformat_function_t * fn);
569 void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
572 #endif /* __included_vnet_classify_h__ */
575 * fd.io coding-style-patch-verification: ON
578 * eval: (c-set-style "gnu")