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/api_errno.h> /* for API error numbers */
31 #include <vppinfra/error.h>
32 #include <vppinfra/hash.h>
33 #include <vppinfra/cache.h>
34 #include <vppinfra/xxhash.h>
36 extern vlib_node_registration_t ip4_classify_node;
37 extern vlib_node_registration_t ip6_classify_node;
39 #define CLASSIFY_TRACE 0
41 #define U32X4_ALIGNED(p) PREDICT_TRUE((((intptr_t)p) & 0xf) == 0)
44 * Classify table option to process packets
45 * CLASSIFY_FLAG_USE_CURR_DATA:
46 * - classify packets starting from VPP node’s current data pointer
48 #define CLASSIFY_FLAG_USE_CURR_DATA 1
51 * Classify session action
52 * CLASSIFY_ACTION_SET_IP4_FIB_INDEX:
53 * - Classified IP packets will be looked up
54 * from the specified ipv4 fib table
55 * CLASSIFY_ACTION_SET_IP6_FIB_INDEX:
56 * - Classified IP packets will be looked up
57 * from the specified ipv6 fib table
59 typedef enum vnet_classify_action_t_
61 CLASSIFY_ACTION_SET_IP4_FIB_INDEX = 1,
62 CLASSIFY_ACTION_SET_IP6_FIB_INDEX = 2,
63 CLASSIFY_ACTION_SET_METADATA = 3,
64 } __attribute__ ((packed)) vnet_classify_action_t;
66 struct _vnet_classify_main;
67 typedef struct _vnet_classify_main vnet_classify_main_t;
69 #define foreach_size_in_u32x4 \
77 typedef CLIB_PACKED(struct _vnet_classify_entry {
78 /* Graph node next index */
81 /* put into vnet_buffer(b)->l2_classfy.opaque_index */
85 /* advance on hit, note it's a signed quantity... */
91 /* Really only need 1 bit */
93 #define VNET_CLASSIFY_ENTRY_FREE (1<<0)
95 vnet_classify_action_t action;
98 /* Hit counter, last heard time */
101 struct _vnet_classify_entry * next_free;
106 /* Must be aligned to a 16-octet boundary */
108 }) vnet_classify_entry_t;
112 vnet_classify_entry_is_free (vnet_classify_entry_t * e)
114 return e->flags & VNET_CLASSIFY_ENTRY_FREE;
118 vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
120 return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
123 /* Need these to con the vector allocator */
126 typedef CLIB_PACKED(struct { \
130 }) vnet_classify_entry_##size##_t;
131 foreach_size_in_u32x4;
148 } vnet_classify_bucket_t;
152 /* Mask to apply after skipping N vectors */
154 /* Buckets and entries */
155 vnet_classify_bucket_t *buckets;
156 vnet_classify_entry_t *entries;
158 /* Config parameters */
164 int entries_per_page;
166 u32 current_data_flag;
167 int current_data_offset;
169 /* Index of next table to try */
170 u32 next_table_index;
172 /* Miss next index, return if next_table_index = 0 */
175 /* Per-bucket working copies, one per thread */
176 vnet_classify_entry_t **working_copies;
177 int *working_copy_lengths;
178 vnet_classify_bucket_t saved_bucket;
180 /* Free entry freelists */
181 vnet_classify_entry_t **freelists;
185 /* Private allocation arena, protected by the writer lock */
188 /* Writer (only) lock for this table */
189 volatile u32 *writer_lock;
191 } vnet_classify_table_t;
193 struct _vnet_classify_main
196 vnet_classify_table_t *tables;
198 /* Registered next-index, opaque unformat fcns */
199 unformat_function_t **unformat_l2_next_index_fns;
200 unformat_function_t **unformat_ip_next_index_fns;
201 unformat_function_t **unformat_acl_next_index_fns;
202 unformat_function_t **unformat_policer_next_index_fns;
203 unformat_function_t **unformat_opaque_index_fns;
205 /* convenience variables */
206 vlib_main_t *vlib_main;
207 vnet_main_t *vnet_main;
210 extern vnet_classify_main_t vnet_classify_main;
212 u8 *format_classify_table (u8 * s, va_list * args);
214 u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h);
217 vnet_classify_hash_packet_inline (vnet_classify_table_t * t, u8 * h)
225 } xor_sum __attribute__ ((aligned (sizeof (u32x4))));
229 #ifdef CLIB_HAVE_VEC128
230 if (U32X4_ALIGNED (h))
231 { //SSE can't handle unaligned data
232 u32x4 *data = (u32x4 *) h;
233 xor_sum.as_u32x4 = data[0 + t->skip_n_vectors] & mask[0];
234 switch (t->match_n_vectors)
237 xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4];
240 xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3];
243 xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2];
246 xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1];
255 #endif /* CLIB_HAVE_VEC128 */
257 u32 skip_u64 = t->skip_n_vectors * 2;
258 u64 *data64 = (u64 *) h;
259 xor_sum.as_u64[0] = data64[0 + skip_u64] & ((u64 *) mask)[0];
260 xor_sum.as_u64[1] = data64[1 + skip_u64] & ((u64 *) mask)[1];
261 switch (t->match_n_vectors)
264 xor_sum.as_u64[0] ^= data64[8 + skip_u64] & ((u64 *) mask)[8];
265 xor_sum.as_u64[1] ^= data64[9 + skip_u64] & ((u64 *) mask)[9];
268 xor_sum.as_u64[0] ^= data64[6 + skip_u64] & ((u64 *) mask)[6];
269 xor_sum.as_u64[1] ^= data64[7 + skip_u64] & ((u64 *) mask)[7];
272 xor_sum.as_u64[0] ^= data64[4 + skip_u64] & ((u64 *) mask)[4];
273 xor_sum.as_u64[1] ^= data64[5 + skip_u64] & ((u64 *) mask)[5];
276 xor_sum.as_u64[0] ^= data64[2 + skip_u64] & ((u64 *) mask)[2];
277 xor_sum.as_u64[1] ^= data64[3 + skip_u64] & ((u64 *) mask)[3];
287 return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
291 vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
295 ASSERT (is_pow2 (t->nbuckets));
297 bucket_index = hash & (t->nbuckets - 1);
299 CLIB_PREFETCH (&t->buckets[bucket_index], CLIB_CACHE_LINE_BYTES, LOAD);
302 static inline vnet_classify_entry_t *
303 vnet_classify_get_entry (vnet_classify_table_t * t, uword offset)
306 u8 *vp = hp + offset;
312 vnet_classify_get_offset (vnet_classify_table_t * t,
313 vnet_classify_entry_t * v)
317 hp = (u8 *) t->mheap;
320 ASSERT ((vp - hp) < 0x100000000ULL);
324 static inline vnet_classify_entry_t *
325 vnet_classify_entry_at_index (vnet_classify_table_t * t,
326 vnet_classify_entry_t * e, u32 index)
332 eu8 += index * (sizeof (vnet_classify_entry_t) +
333 (t->match_n_vectors * sizeof (u32x4)));
335 return (vnet_classify_entry_t *) eu8;
339 vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
343 vnet_classify_bucket_t *b;
344 vnet_classify_entry_t *e;
346 bucket_index = hash & (t->nbuckets - 1);
348 b = &t->buckets[bucket_index];
353 hash >>= t->log2_nbuckets;
355 e = vnet_classify_get_entry (t, b->offset);
356 value_index = hash & ((1 << b->log2_pages) - 1);
358 e = vnet_classify_entry_at_index (t, e, value_index);
360 CLIB_PREFETCH (e, CLIB_CACHE_LINE_BYTES, LOAD);
363 vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t,
364 u8 * h, u64 hash, f64 now);
366 static inline vnet_classify_entry_t *
367 vnet_classify_find_entry_inline (vnet_classify_table_t * t,
368 u8 * h, u64 hash, f64 now)
370 vnet_classify_entry_t *v;
376 } result __attribute__ ((aligned (sizeof (u32x4))));
377 vnet_classify_bucket_t *b;
383 bucket_index = hash & (t->nbuckets - 1);
384 b = &t->buckets[bucket_index];
390 hash >>= t->log2_nbuckets;
392 v = vnet_classify_get_entry (t, b->offset);
393 value_index = hash & ((1 << b->log2_pages) - 1);
394 limit = t->entries_per_page;
395 if (PREDICT_FALSE (b->linear_search))
398 limit *= (1 << b->log2_pages);
401 v = vnet_classify_entry_at_index (t, v, value_index);
403 #ifdef CLIB_HAVE_VEC128
404 if (U32X4_ALIGNED (h))
406 u32x4 *data = (u32x4 *) h;
407 for (i = 0; i < limit; i++)
410 result.as_u32x4 = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
411 switch (t->match_n_vectors)
415 (data[4 + t->skip_n_vectors] & mask[4]) ^ key[4];
419 (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3];
423 (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
427 (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
435 if (u32x4_zero_byte_mask (result.as_u32x4) == 0xffff)
437 if (PREDICT_TRUE (now))
444 v = vnet_classify_entry_at_index (t, v, 1);
448 #endif /* CLIB_HAVE_VEC128 */
450 u32 skip_u64 = t->skip_n_vectors * 2;
451 u64 *data64 = (u64 *) h;
452 for (i = 0; i < limit; i++)
457 (data64[0 + skip_u64] & ((u64 *) mask)[0]) ^ ((u64 *) key)[0];
459 (data64[1 + skip_u64] & ((u64 *) mask)[1]) ^ ((u64 *) key)[1];
460 switch (t->match_n_vectors)
464 (data64[8 + skip_u64] & ((u64 *) mask)[8]) ^ ((u64 *) key)[8];
466 (data64[9 + skip_u64] & ((u64 *) mask)[9]) ^ ((u64 *) key)[9];
470 (data64[6 + skip_u64] & ((u64 *) mask)[6]) ^ ((u64 *) key)[6];
472 (data64[7 + skip_u64] & ((u64 *) mask)[7]) ^ ((u64 *) key)[7];
476 (data64[4 + skip_u64] & ((u64 *) mask)[4]) ^ ((u64 *) key)[4];
478 (data64[5 + skip_u64] & ((u64 *) mask)[5]) ^ ((u64 *) key)[5];
482 (data64[2 + skip_u64] & ((u64 *) mask)[2]) ^ ((u64 *) key)[2];
484 (data64[3 + skip_u64] & ((u64 *) mask)[3]) ^ ((u64 *) key)[3];
492 if (result.as_u64[0] == 0 && result.as_u64[1] == 0)
494 if (PREDICT_TRUE (now))
502 v = vnet_classify_entry_at_index (t, v, 1);
508 vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t * cm,
509 u8 * mask, u32 nbuckets,
512 u32 match_n_vectors);
514 int vnet_classify_add_del_session (vnet_classify_main_t * cm,
520 u8 action, u32 metadata, int is_add);
522 int vnet_classify_add_del_table (vnet_classify_main_t * cm,
528 u32 next_table_index,
531 u8 current_data_flag,
532 i16 current_data_offset,
533 int is_add, int del_chain);
535 unformat_function_t unformat_ip4_mask;
536 unformat_function_t unformat_ip6_mask;
537 unformat_function_t unformat_l3_mask;
538 unformat_function_t unformat_l2_mask;
539 unformat_function_t unformat_classify_mask;
540 unformat_function_t unformat_l2_next_index;
541 unformat_function_t unformat_ip_next_index;
542 unformat_function_t unformat_ip4_match;
543 unformat_function_t unformat_ip6_match;
544 unformat_function_t unformat_l3_match;
545 unformat_function_t unformat_l4_match;
546 unformat_function_t unformat_vlan_tag;
547 unformat_function_t unformat_l2_match;
548 unformat_function_t unformat_classify_match;
550 void vnet_classify_register_unformat_ip_next_index_fn
551 (unformat_function_t * fn);
553 void vnet_classify_register_unformat_l2_next_index_fn
554 (unformat_function_t * fn);
556 void vnet_classify_register_unformat_acl_next_index_fn
557 (unformat_function_t * fn);
559 void vnet_classify_register_unformat_policer_next_index_fn
560 (unformat_function_t * fn);
562 void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
565 #endif /* __included_vnet_classify_h__ */
568 * fd.io coding-style-patch-verification: ON
571 * eval: (c-set-style "gnu")