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 #define U32X4_ALIGNED(p) PREDICT_TRUE((((intptr_t)p) & 0xf) == 0)
47 * Classify table option to process packets
48 * CLASSIFY_FLAG_USE_CURR_DATA:
49 * - classify packets starting from VPP node’s current data pointer
51 #define CLASSIFY_FLAG_USE_CURR_DATA 1
54 * Classify session action
55 * CLASSIFY_ACTION_SET_IP4_FIB_INDEX:
56 * - Classified IP packets will be looked up
57 * from the specified ipv4 fib table
58 * CLASSIFY_ACTION_SET_IP6_FIB_INDEX:
59 * - Classified IP packets will be looked up
60 * from the specified ipv6 fib table
62 typedef enum vnet_classify_action_t_
64 CLASSIFY_ACTION_SET_IP4_FIB_INDEX = 1,
65 CLASSIFY_ACTION_SET_IP6_FIB_INDEX = 2,
66 CLASSIFY_ACTION_SET_METADATA = 3,
67 } __attribute__ ((packed)) vnet_classify_action_t;
69 struct _vnet_classify_main;
70 typedef struct _vnet_classify_main vnet_classify_main_t;
72 #define foreach_size_in_u32x4 \
80 typedef CLIB_PACKED(struct _vnet_classify_entry {
81 /* Graph node next index */
84 /* put into vnet_buffer(b)->l2_classfy.opaque_index */
88 /* advance on hit, note it's a signed quantity... */
94 /* Really only need 1 bit */
96 #define VNET_CLASSIFY_ENTRY_FREE (1<<0)
98 vnet_classify_action_t action;
101 /* Hit counter, last heard time */
104 struct _vnet_classify_entry * next_free;
109 /* Must be aligned to a 16-octet boundary */
111 }) vnet_classify_entry_t;
115 vnet_classify_entry_is_free (vnet_classify_entry_t * e)
117 return e->flags & VNET_CLASSIFY_ENTRY_FREE;
121 vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
123 return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
126 /* Need these to con the vector allocator */
129 typedef CLIB_PACKED(struct { \
133 }) vnet_classify_entry_##size##_t;
134 foreach_size_in_u32x4;
151 } vnet_classify_bucket_t;
155 /* Mask to apply after skipping N vectors */
157 /* Buckets and entries */
158 vnet_classify_bucket_t *buckets;
159 vnet_classify_entry_t *entries;
161 /* Config parameters */
167 int entries_per_page;
169 u32 current_data_flag;
170 int current_data_offset;
172 /* Index of next table to try */
173 u32 next_table_index;
175 /* Miss next index, return if next_table_index = 0 */
178 /* Per-bucket working copies, one per thread */
179 vnet_classify_entry_t **working_copies;
180 int *working_copy_lengths;
181 vnet_classify_bucket_t saved_bucket;
183 /* Free entry freelists */
184 vnet_classify_entry_t **freelists;
188 /* Private allocation arena, protected by the writer lock */
191 /* Writer (only) lock for this table */
192 volatile u32 *writer_lock;
194 } vnet_classify_table_t;
196 struct _vnet_classify_main
199 vnet_classify_table_t *tables;
201 /* Registered next-index, opaque unformat fcns */
202 unformat_function_t **unformat_l2_next_index_fns;
203 unformat_function_t **unformat_ip_next_index_fns;
204 unformat_function_t **unformat_acl_next_index_fns;
205 unformat_function_t **unformat_policer_next_index_fns;
206 unformat_function_t **unformat_opaque_index_fns;
208 /* convenience variables */
209 vlib_main_t *vlib_main;
210 vnet_main_t *vnet_main;
213 extern vnet_classify_main_t vnet_classify_main;
215 u8 *format_classify_table (u8 * s, va_list * args);
217 u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h);
220 vnet_classify_hash_packet_inline (vnet_classify_table_t * t, u8 * h)
228 } xor_sum __attribute__ ((aligned (sizeof (u32x4))));
232 #ifdef CLIB_HAVE_VEC128
233 if (U32X4_ALIGNED (h))
234 { //SSE can't handle unaligned data
235 u32x4 *data = (u32x4 *) h;
236 xor_sum.as_u32x4 = data[0 + t->skip_n_vectors] & mask[0];
237 switch (t->match_n_vectors)
240 xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4];
243 xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3];
246 xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2];
249 xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1];
258 #endif /* CLIB_HAVE_VEC128 */
260 u32 skip_u64 = t->skip_n_vectors * 2;
261 u64 *data64 = (u64 *) h;
262 xor_sum.as_u64[0] = data64[0 + skip_u64] & ((u64 *) mask)[0];
263 xor_sum.as_u64[1] = data64[1 + skip_u64] & ((u64 *) mask)[1];
264 switch (t->match_n_vectors)
267 xor_sum.as_u64[0] ^= data64[8 + skip_u64] & ((u64 *) mask)[8];
268 xor_sum.as_u64[1] ^= data64[9 + skip_u64] & ((u64 *) mask)[9];
271 xor_sum.as_u64[0] ^= data64[6 + skip_u64] & ((u64 *) mask)[6];
272 xor_sum.as_u64[1] ^= data64[7 + skip_u64] & ((u64 *) mask)[7];
275 xor_sum.as_u64[0] ^= data64[4 + skip_u64] & ((u64 *) mask)[4];
276 xor_sum.as_u64[1] ^= data64[5 + skip_u64] & ((u64 *) mask)[5];
279 xor_sum.as_u64[0] ^= data64[2 + skip_u64] & ((u64 *) mask)[2];
280 xor_sum.as_u64[1] ^= data64[3 + skip_u64] & ((u64 *) mask)[3];
290 return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
294 vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
298 ASSERT (is_pow2 (t->nbuckets));
300 bucket_index = hash & (t->nbuckets - 1);
302 CLIB_PREFETCH (&t->buckets[bucket_index], CLIB_CACHE_LINE_BYTES, LOAD);
305 static inline vnet_classify_entry_t *
306 vnet_classify_get_entry (vnet_classify_table_t * t, uword offset)
309 u8 *vp = hp + offset;
315 vnet_classify_get_offset (vnet_classify_table_t * t,
316 vnet_classify_entry_t * v)
320 hp = (u8 *) t->mheap;
323 ASSERT ((vp - hp) < 0x100000000ULL);
327 static inline vnet_classify_entry_t *
328 vnet_classify_entry_at_index (vnet_classify_table_t * t,
329 vnet_classify_entry_t * e, u32 index)
335 eu8 += index * (sizeof (vnet_classify_entry_t) +
336 (t->match_n_vectors * sizeof (u32x4)));
338 return (vnet_classify_entry_t *) eu8;
342 vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
346 vnet_classify_bucket_t *b;
347 vnet_classify_entry_t *e;
349 bucket_index = hash & (t->nbuckets - 1);
351 b = &t->buckets[bucket_index];
356 hash >>= t->log2_nbuckets;
358 e = vnet_classify_get_entry (t, b->offset);
359 value_index = hash & ((1 << b->log2_pages) - 1);
361 e = vnet_classify_entry_at_index (t, e, value_index);
363 CLIB_PREFETCH (e, CLIB_CACHE_LINE_BYTES, LOAD);
366 vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t,
367 u8 * h, u64 hash, f64 now);
369 static inline vnet_classify_entry_t *
370 vnet_classify_find_entry_inline (vnet_classify_table_t * t,
371 u8 * h, u64 hash, f64 now)
373 vnet_classify_entry_t *v;
379 } result __attribute__ ((aligned (sizeof (u32x4))));
380 vnet_classify_bucket_t *b;
386 bucket_index = hash & (t->nbuckets - 1);
387 b = &t->buckets[bucket_index];
393 hash >>= t->log2_nbuckets;
395 v = vnet_classify_get_entry (t, b->offset);
396 value_index = hash & ((1 << b->log2_pages) - 1);
397 limit = t->entries_per_page;
398 if (PREDICT_FALSE (b->linear_search))
401 limit *= (1 << b->log2_pages);
404 v = vnet_classify_entry_at_index (t, v, value_index);
406 #ifdef CLIB_HAVE_VEC128
407 if (U32X4_ALIGNED (h))
409 u32x4 *data = (u32x4 *) h;
410 for (i = 0; i < limit; i++)
413 result.as_u32x4 = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
414 switch (t->match_n_vectors)
418 (data[4 + t->skip_n_vectors] & mask[4]) ^ key[4];
422 (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3];
426 (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
430 (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
438 if (u32x4_zero_byte_mask (result.as_u32x4) == 0xffff)
440 if (PREDICT_TRUE (now))
447 v = vnet_classify_entry_at_index (t, v, 1);
451 #endif /* CLIB_HAVE_VEC128 */
453 u32 skip_u64 = t->skip_n_vectors * 2;
454 u64 *data64 = (u64 *) h;
455 for (i = 0; i < limit; i++)
460 (data64[0 + skip_u64] & ((u64 *) mask)[0]) ^ ((u64 *) key)[0];
462 (data64[1 + skip_u64] & ((u64 *) mask)[1]) ^ ((u64 *) key)[1];
463 switch (t->match_n_vectors)
467 (data64[8 + skip_u64] & ((u64 *) mask)[8]) ^ ((u64 *) key)[8];
469 (data64[9 + skip_u64] & ((u64 *) mask)[9]) ^ ((u64 *) key)[9];
473 (data64[6 + skip_u64] & ((u64 *) mask)[6]) ^ ((u64 *) key)[6];
475 (data64[7 + skip_u64] & ((u64 *) mask)[7]) ^ ((u64 *) key)[7];
479 (data64[4 + skip_u64] & ((u64 *) mask)[4]) ^ ((u64 *) key)[4];
481 (data64[5 + skip_u64] & ((u64 *) mask)[5]) ^ ((u64 *) key)[5];
485 (data64[2 + skip_u64] & ((u64 *) mask)[2]) ^ ((u64 *) key)[2];
487 (data64[3 + skip_u64] & ((u64 *) mask)[3]) ^ ((u64 *) key)[3];
495 if (result.as_u64[0] == 0 && result.as_u64[1] == 0)
497 if (PREDICT_TRUE (now))
505 v = vnet_classify_entry_at_index (t, v, 1);
511 vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t * cm,
512 u8 * mask, u32 nbuckets,
515 u32 match_n_vectors);
517 int vnet_classify_add_del_session (vnet_classify_main_t * cm,
523 u8 action, u32 metadata, int is_add);
525 int vnet_classify_add_del_table (vnet_classify_main_t * cm,
531 u32 next_table_index,
534 u8 current_data_flag,
535 i16 current_data_offset,
536 int is_add, int del_chain);
538 unformat_function_t unformat_ip4_mask;
539 unformat_function_t unformat_ip6_mask;
540 unformat_function_t unformat_l3_mask;
541 unformat_function_t unformat_l2_mask;
542 unformat_function_t unformat_classify_mask;
543 unformat_function_t unformat_l2_next_index;
544 unformat_function_t unformat_ip_next_index;
545 unformat_function_t unformat_ip4_match;
546 unformat_function_t unformat_ip6_match;
547 unformat_function_t unformat_l3_match;
548 unformat_function_t unformat_l4_match;
549 unformat_function_t unformat_vlan_tag;
550 unformat_function_t unformat_l2_match;
551 unformat_function_t unformat_classify_match;
553 void vnet_classify_register_unformat_ip_next_index_fn
554 (unformat_function_t * fn);
556 void vnet_classify_register_unformat_l2_next_index_fn
557 (unformat_function_t * fn);
559 void vnet_classify_register_unformat_acl_next_index_fn
560 (unformat_function_t * fn);
562 void vnet_classify_register_unformat_policer_next_index_fn
563 (unformat_function_t * fn);
565 void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
568 #endif /* __included_vnet_classify_h__ */
571 * fd.io coding-style-patch-verification: ON
574 * eval: (c-set-style "gnu")