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 vlib_node_registration_t ip4_classify_node;
39 vlib_node_registration_t ip6_classify_node;
41 #define CLASSIFY_TRACE 0
43 struct _vnet_classify_main;
44 typedef struct _vnet_classify_main vnet_classify_main_t;
46 #define foreach_size_in_u32x4 \
53 typedef CLIB_PACKED(struct _vnet_classify_entry {
54 /* Graph node next index */
57 /* put into vnet_buffer(b)->l2_classfy.opaque_index */
61 /* advance on hit, note it's a signed quantity... */
67 /* Really only need 1 bit */
69 #define VNET_CLASSIFY_ENTRY_FREE (1<<0)
71 /* Hit counter, last heard time */
74 struct _vnet_classify_entry * next_free;
79 /* Must be aligned to a 16-octet boundary */
81 }) vnet_classify_entry_t;
83 static inline int vnet_classify_entry_is_free (vnet_classify_entry_t * e)
85 return e->flags & VNET_CLASSIFY_ENTRY_FREE;
88 static inline int vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
90 return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
93 /* Need these to con the vector allocator */
95 typedef CLIB_PACKED(struct { \
99 }) vnet_classify_entry_##size##_t;
100 foreach_size_in_u32x4;
112 } vnet_classify_bucket_t;
115 /* Mask to apply after skipping N vectors */
117 /* Buckets and entries */
118 vnet_classify_bucket_t * buckets;
119 vnet_classify_entry_t * entries;
121 /* Config parameters */
126 int entries_per_page;
128 /* Index of next table to try */
129 u32 next_table_index;
131 /* Miss next index, return if next_table_index = 0 */
134 /* Per-bucket working copies, one per thread */
135 vnet_classify_entry_t ** working_copies;
136 vnet_classify_bucket_t saved_bucket;
138 /* Free entry freelists */
139 vnet_classify_entry_t **freelists;
143 /* Private allocation arena, protected by the writer lock */
146 /* Writer (only) lock for this table */
147 volatile u32 * writer_lock;
149 } vnet_classify_table_t;
151 struct _vnet_classify_main {
153 vnet_classify_table_t * tables;
155 /* convenience variables */
156 vlib_main_t * vlib_main;
157 vnet_main_t * vnet_main;
160 vnet_classify_main_t vnet_classify_main;
162 u8 * format_classify_table (u8 * s, va_list * args);
164 u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h);
167 vnet_classify_hash_packet_inline (vnet_classify_table_t * t,
175 } xor_sum __attribute__((aligned(sizeof(u32x4))));
182 ASSERT ((((u64)h) & 0xf) == 0);
184 xor_sum.as_u32x4 = data[0 + t->skip_n_vectors] & mask[0];
186 switch (t->match_n_vectors)
189 xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4];
192 xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3];
195 xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2];
198 xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1];
207 return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
211 vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
215 ASSERT (is_pow2(t->nbuckets));
217 bucket_index = hash & (t->nbuckets - 1);
219 CLIB_PREFETCH(&t->buckets[bucket_index], CLIB_CACHE_LINE_BYTES, LOAD);
222 static inline vnet_classify_entry_t *
223 vnet_classify_get_entry (vnet_classify_table_t * t, uword offset)
226 u8 * vp = hp + offset;
231 static inline uword vnet_classify_get_offset (vnet_classify_table_t * t,
232 vnet_classify_entry_t * v)
236 hp = (u8 *) t->mheap;
239 ASSERT((vp - hp) < 0x100000000ULL);
243 static inline vnet_classify_entry_t *
244 vnet_classify_entry_at_index (vnet_classify_table_t * t,
245 vnet_classify_entry_t * e,
252 eu8 += index * (sizeof (vnet_classify_entry_t) +
253 (t->match_n_vectors * sizeof (u32x4)));
255 return (vnet_classify_entry_t *) eu8;
259 vnet_classify_prefetch_entry (vnet_classify_table_t * t,
264 vnet_classify_bucket_t * b;
265 vnet_classify_entry_t * e;
267 bucket_index = hash & (t->nbuckets - 1);
269 b = &t->buckets[bucket_index];
274 hash >>= t->log2_nbuckets;
276 e = vnet_classify_get_entry (t, b->offset);
277 value_index = hash & ((1<<b->log2_pages)-1);
279 e = vnet_classify_entry_at_index (t, e, value_index);
281 CLIB_PREFETCH(e, CLIB_CACHE_LINE_BYTES, LOAD);
284 vnet_classify_entry_t *
285 vnet_classify_find_entry (vnet_classify_table_t * t,
286 u8 * h, u64 hash, f64 now);
288 static inline vnet_classify_entry_t *
289 vnet_classify_find_entry_inline (vnet_classify_table_t * t,
290 u8 * h, u64 hash, f64 now)
292 vnet_classify_entry_t * v;
293 u32x4 * mask, * data, *data_start, * key;
294 u32x4 result __attribute__((aligned(sizeof(u32x4))));
295 vnet_classify_bucket_t * b;
301 ASSERT ((((u64)h) & 0xf) == 0);
303 data_start = (u32x4 *) h;
305 bucket_index = hash & (t->nbuckets-1);
306 b = &t->buckets[bucket_index];
311 hash >>= t->log2_nbuckets;
313 v = vnet_classify_get_entry (t, b->offset);
314 value_index = hash & ((1<<b->log2_pages)-1);
316 v = vnet_classify_entry_at_index (t, v, value_index);
318 for (i = 0; i < t->entries_per_page; i++)
324 switch (t->match_n_vectors)
327 result = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
331 result = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
332 result |= (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
336 result = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
337 result |= (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
338 result |= (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
342 result = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
343 result |= (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
344 result |= (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
345 result |= (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3];
349 result = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
350 result |= (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
351 result |= (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
352 result |= (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3];
353 result |= (data[4 + t->skip_n_vectors] & mask[4]) ^ key[4];
360 result_mask = u32x4_zero_byte_mask (result);
361 if (result_mask == 0xffff)
363 if (PREDICT_TRUE(now))
370 v = vnet_classify_entry_at_index (t, v, 1);
375 vnet_classify_table_t *
376 vnet_classify_new_table (vnet_classify_main_t *cm,
377 u8 * mask, u32 nbuckets, u32 memory_size,
379 u32 match_n_vectors);
381 int vnet_classify_add_del_session (vnet_classify_main_t * cm,
389 int vnet_classify_add_del_table (vnet_classify_main_t * cm,
395 u32 next_table_index,
400 unformat_function_t unformat_ip4_mask;
401 unformat_function_t unformat_ip6_mask;
402 unformat_function_t unformat_l3_mask;
403 unformat_function_t unformat_l2_mask;
404 unformat_function_t unformat_classify_mask;
405 unformat_function_t unformat_l2_next_index;
406 unformat_function_t unformat_ip_next_index;
407 unformat_function_t unformat_ip4_match;
408 unformat_function_t unformat_ip6_match;
409 unformat_function_t unformat_l3_match;
410 unformat_function_t unformat_vlan_tag;
411 unformat_function_t unformat_l2_match;
412 unformat_function_t unformat_classify_match;
414 #endif /* __included_vnet_classify_h__ */