FD.io VPP  v16.06
Vector Packet Processing
vnet_classify.h
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #ifndef __included_vnet_classify_h__
16 #define __included_vnet_classify_h__
17 
18 #include <stdarg.h>
19 
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>
28 #include <vlib/cli.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 */
32 
33 #include <vppinfra/error.h>
34 #include <vppinfra/hash.h>
35 #include <vppinfra/cache.h>
36 #include <vppinfra/xxhash.h>
37 
40 
41 #define CLASSIFY_TRACE 0
42 
43 #if !defined( __aarch64__) && !defined(__arm__)
44 #define CLASSIFY_USE_SSE //Allow usage of SSE operations
45 #endif
46 
47 #define U32X4_ALIGNED(p) PREDICT_TRUE((((intptr_t)p) & 0xf) == 0)
48 
49 struct _vnet_classify_main;
50 typedef struct _vnet_classify_main vnet_classify_main_t;
51 
52 #define foreach_size_in_u32x4 \
53 _(1) \
54 _(2) \
55 _(3) \
56 _(4) \
57 _(5)
58 
59 typedef CLIB_PACKED(struct _vnet_classify_entry {
60  /* Graph node next index */
61  u32 next_index;
62 
63  /* put into vnet_buffer(b)->l2_classfy.opaque_index */
64  union {
65  struct {
66  u32 opaque_index;
67  /* advance on hit, note it's a signed quantity... */
68  i32 advance;
69  };
70  u64 opaque_count;
71  };
72 
73  /* Really only need 1 bit */
74  u32 flags;
75 #define VNET_CLASSIFY_ENTRY_FREE (1<<0)
76 
77  /* Hit counter, last heard time */
78  union {
79  u64 hits;
80  struct _vnet_classify_entry * next_free;
81  };
82 
83  f64 last_heard;
84 
85  /* Must be aligned to a 16-octet boundary */
86  u32x4 key[0];
87 }) vnet_classify_entry_t;
88 
89 static inline int vnet_classify_entry_is_free (vnet_classify_entry_t * e)
90 {
91  return e->flags & VNET_CLASSIFY_ENTRY_FREE;
92 }
93 
94 static inline int vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
95 {
96  return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
97 }
98 
99 /* Need these to con the vector allocator */
100 #define _(size) \
101 typedef CLIB_PACKED(struct { \
102  u32 pad0[4]; \
103  u64 pad1[2]; \
104  u32x4 key[size]; \
105 }) vnet_classify_entry_##size##_t;
107 #undef _
108 
109 typedef struct {
110  union {
111  struct {
113  u8 pad[3];
115  };
117  };
119 
120 typedef struct {
121  /* Mask to apply after skipping N vectors */
122  u32x4 *mask;
123  /* Buckets and entries */
125  vnet_classify_entry_t * entries;
126 
127  /* Config parameters */
134  /* Index of next table to try */
136 
137  /* Miss next index, return if next_table_index = 0 */
139 
140  /* Per-bucket working copies, one per thread */
141  vnet_classify_entry_t ** working_copies;
143 
144  /* Free entry freelists */
145  vnet_classify_entry_t **freelists;
146 
147  u8 * name;
148 
149  /* Private allocation arena, protected by the writer lock */
150  void * mheap;
151 
152  /* Writer (only) lock for this table */
153  volatile u32 * writer_lock;
154 
156 
157 struct _vnet_classify_main {
158  /* Table pool */
159  vnet_classify_table_t * tables;
160 
161  /* Registered next-index, opaque unformat fcns */
162  unformat_function_t ** unformat_l2_next_index_fns;
163  unformat_function_t ** unformat_ip_next_index_fns;
164  unformat_function_t ** unformat_acl_next_index_fns;
165  unformat_function_t ** unformat_opaque_index_fns;
166 
167  /* convenience variables */
170 };
171 
173 
174 u8 * format_classify_table (u8 * s, va_list * args);
175 
177 
178 static inline u64
180  u8 * h)
181 {
182  u32x4 *mask;
183 
184  union {
185  u32x4 as_u32x4;
186  u64 as_u64[2];
187  } xor_sum __attribute__((aligned(sizeof(u32x4))));
188 
189  ASSERT(t);
190  mask = t->mask;
191 #ifdef CLASSIFY_USE_SSE
192  if (U32X4_ALIGNED(h)) { //SSE can't handle unaligned data
193  u32x4 *data = (u32x4 *)h;
194  xor_sum.as_u32x4 = data[0 + t->skip_n_vectors] & mask[0];
195  switch (t->match_n_vectors)
196  {
197  case 5:
198  xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4];
199  /* FALLTHROUGH */
200  case 4:
201  xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3];
202  /* FALLTHROUGH */
203  case 3:
204  xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2];
205  /* FALLTHROUGH */
206  case 2:
207  xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1];
208  /* FALLTHROUGH */
209  case 1:
210  break;
211  default:
212  abort();
213  }
214  } else
215 #endif /* CLASSIFY_USE_SSE */
216  {
217  u32 skip_u64 = t->skip_n_vectors * 2;
218  u64 *data64 = (u64 *)h;
219  xor_sum.as_u64[0] = data64[0 + skip_u64] & ((u64 *)mask)[0];
220  xor_sum.as_u64[1] = data64[1 + skip_u64] & ((u64 *)mask)[1];
221  switch (t->match_n_vectors)
222  {
223  case 5:
224  xor_sum.as_u64[0] ^= data64[8 + skip_u64] & ((u64 *)mask)[8];
225  xor_sum.as_u64[1] ^= data64[9 + skip_u64] & ((u64 *)mask)[9];
226  /* FALLTHROUGH */
227  case 4:
228  xor_sum.as_u64[0] ^= data64[6 + skip_u64] & ((u64 *)mask)[6];
229  xor_sum.as_u64[1] ^= data64[7 + skip_u64] & ((u64 *)mask)[7];
230  /* FALLTHROUGH */
231  case 3:
232  xor_sum.as_u64[0] ^= data64[4 + skip_u64] & ((u64 *)mask)[4];
233  xor_sum.as_u64[1] ^= data64[5 + skip_u64] & ((u64 *)mask)[5];
234  /* FALLTHROUGH */
235  case 2:
236  xor_sum.as_u64[0] ^= data64[2 + skip_u64] & ((u64 *)mask)[2];
237  xor_sum.as_u64[1] ^= data64[3 + skip_u64] & ((u64 *)mask)[3];
238  /* FALLTHROUGH */
239  case 1:
240  break;
241 
242  default:
243  abort();
244  }
245  }
246 
247  return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
248 }
249 
250 static inline void
252 {
253  u32 bucket_index;
254 
255  ASSERT (is_pow2(t->nbuckets));
256 
257  bucket_index = hash & (t->nbuckets - 1);
258 
259  CLIB_PREFETCH(&t->buckets[bucket_index], CLIB_CACHE_LINE_BYTES, LOAD);
260 }
261 
262 static inline vnet_classify_entry_t *
264 {
265  u8 * hp = t->mheap;
266  u8 * vp = hp + offset;
267 
268  return (void *) vp;
269 }
270 
272  vnet_classify_entry_t * v)
273 {
274  u8 * hp, * vp;
275 
276  hp = (u8 *) t->mheap;
277  vp = (u8 *) v;
278 
279  ASSERT((vp - hp) < 0x100000000ULL);
280  return vp - hp;
281 }
282 
283 static inline vnet_classify_entry_t *
285  vnet_classify_entry_t * e,
286  u32 index)
287 {
288  u8 * eu8;
289 
290  eu8 = (u8 *)e;
291 
292  eu8 += index * (sizeof (vnet_classify_entry_t) +
293  (t->match_n_vectors * sizeof (u32x4)));
294 
295  return (vnet_classify_entry_t *) eu8;
296 }
297 
298 static inline void
300  u64 hash)
301 {
302  u32 bucket_index;
303  u32 value_index;
305  vnet_classify_entry_t * e;
306 
307  bucket_index = hash & (t->nbuckets - 1);
308 
309  b = &t->buckets[bucket_index];
310 
311  if (b->offset == 0)
312  return;
313 
314  hash >>= t->log2_nbuckets;
315 
316  e = vnet_classify_get_entry (t, b->offset);
317  value_index = hash & ((1<<b->log2_pages)-1);
318 
319  e = vnet_classify_entry_at_index (t, e, value_index);
320 
322 }
323 
324 vnet_classify_entry_t *
326  u8 * h, u64 hash, f64 now);
327 
328 static inline vnet_classify_entry_t *
330  u8 * h, u64 hash, f64 now)
331  {
332  vnet_classify_entry_t * v;
333  u32x4 *mask, *key;
334  union {
335  u32x4 as_u32x4;
336  u64 as_u64[2];
337  } result __attribute__((aligned(sizeof(u32x4))));
339  u32 value_index;
340  u32 bucket_index;
341  int i;
342 
343  bucket_index = hash & (t->nbuckets-1);
344  b = &t->buckets[bucket_index];
345  mask = t->mask;
346 
347  if (b->offset == 0)
348  return 0;
349 
350  hash >>= t->log2_nbuckets;
351 
352  v = vnet_classify_get_entry (t, b->offset);
353  value_index = hash & ((1<<b->log2_pages)-1);
354  v = vnet_classify_entry_at_index (t, v, value_index);
355 
356 #ifdef CLASSIFY_USE_SSE
357  if (U32X4_ALIGNED(h)) {
358  u32x4 *data = (u32x4 *) h;
359  for (i = 0; i < t->entries_per_page; i++) {
360  key = v->key;
361  result.as_u32x4 = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
362  switch (t->match_n_vectors)
363  {
364  case 5:
365  result.as_u32x4 |= (data[4 + t->skip_n_vectors] & mask[4]) ^ key[4];
366  /* FALLTHROUGH */
367  case 4:
368  result.as_u32x4 |= (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3];
369  /* FALLTHROUGH */
370  case 3:
371  result.as_u32x4 |= (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
372  /* FALLTHROUGH */
373  case 2:
374  result.as_u32x4 |= (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
375  /* FALLTHROUGH */
376  case 1:
377  break;
378  default:
379  abort();
380  }
381 
382  if (u32x4_zero_byte_mask (result.as_u32x4) == 0xffff) {
383  if (PREDICT_TRUE(now)) {
384  v->hits++;
385  v->last_heard = now;
386  }
387  return (v);
388  }
389  v = vnet_classify_entry_at_index (t, v, 1);
390  }
391  } else
392 #endif /* CLASSIFY_USE_SSE */
393  {
394  u32 skip_u64 = t->skip_n_vectors * 2;
395  u64 *data64 = (u64 *)h;
396  for (i = 0; i < t->entries_per_page; i++) {
397  key = v->key;
398 
399  result.as_u64[0] = (data64[0 + skip_u64] & ((u64 *)mask)[0]) ^ ((u64 *)key)[0];
400  result.as_u64[1] = (data64[1 + skip_u64] & ((u64 *)mask)[1]) ^ ((u64 *)key)[1];
401  switch (t->match_n_vectors)
402  {
403  case 5:
404  result.as_u64[0] |= (data64[8 + skip_u64] & ((u64 *)mask)[8]) ^ ((u64 *)key)[8];
405  result.as_u64[1] |= (data64[9 + skip_u64] & ((u64 *)mask)[9]) ^ ((u64 *)key)[9];
406  /* FALLTHROUGH */
407  case 4:
408  result.as_u64[0] |= (data64[6 + skip_u64] & ((u64 *)mask)[6]) ^ ((u64 *)key)[6];
409  result.as_u64[1] |= (data64[7 + skip_u64] & ((u64 *)mask)[7]) ^ ((u64 *)key)[7];
410  /* FALLTHROUGH */
411  case 3:
412  result.as_u64[0] |= (data64[4 + skip_u64] & ((u64 *)mask)[4]) ^ ((u64 *)key)[4];
413  result.as_u64[1] |= (data64[5 + skip_u64] & ((u64 *)mask)[5]) ^ ((u64 *)key)[5];
414  /* FALLTHROUGH */
415  case 2:
416  result.as_u64[0] |= (data64[2 + skip_u64] & ((u64 *)mask)[2]) ^ ((u64 *)key)[2];
417  result.as_u64[1] |= (data64[3 + skip_u64] & ((u64 *)mask)[3]) ^ ((u64 *)key)[3];
418  /* FALLTHROUGH */
419  case 1:
420  break;
421  default:
422  abort();
423  }
424 
425  if (result.as_u64[0] == 0 && result.as_u64[1] == 0) {
426  if (PREDICT_TRUE(now)) {
427  v->hits++;
428  v->last_heard = now;
429  }
430  return (v);
431  }
432 
433  v = vnet_classify_entry_at_index (t, v, 1);
434  }
435  }
436  return 0;
437  }
438 
441  u8 * mask, u32 nbuckets, u32 memory_size,
442  u32 skip_n_vectors,
443  u32 match_n_vectors);
444 
446  u32 table_index,
447  u8 * match,
448  u32 hit_next_index,
449  u32 opaque_index,
450  i32 advance,
451  int is_add);
452 
454  u8 * mask,
455  u32 nbuckets,
456  u32 memory_size,
457  u32 skip,
458  u32 match,
459  u32 next_table_index,
460  u32 miss_next_index,
461  u32 * table_index,
462  int is_add);
463 
477 
479 (unformat_function_t * fn);
480 
482 (unformat_function_t * fn);
483 
485 (unformat_function_t * fn);
486 
488 
489 #endif /* __included_vnet_classify_h__ */
u64 vnet_classify_hash_packet(vnet_classify_table_t *t, u8 *h)
unformat_function_t unformat_ip4_match
vnet_classify_entry_t ** working_copies
unformat_function_t unformat_vlan_tag
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
unformat_function_t unformat_l2_mask
unformat_function_t unformat_ip_next_index
uword( unformat_function_t)(unformat_input_t *input, va_list *args)
Definition: format.h:229
#define foreach_size_in_u32x4
Definition: vnet_classify.h:52
#define PREDICT_TRUE(x)
Definition: clib.h:98
static vnet_classify_entry_t * vnet_classify_find_entry_inline(vnet_classify_table_t *t, u8 *h, u64 hash, f64 now)
#define VNET_CLASSIFY_ENTRY_FREE
unformat_function_t unformat_ip6_mask
#define U32X4_ALIGNED(p)
Definition: vnet_classify.h:47
static u64 clib_xxhash(u64 key)
Definition: xxhash.h:57
always_inline u32 u32x4_zero_byte_mask(u32x4 x)
struct _vlib_node_registration vlib_node_registration_t
unformat_function_t unformat_classify_match
vnet_classify_table_t * vnet_classify_new_table(vnet_classify_main_t *cm, u8 *mask, u32 nbuckets, u32 memory_size, u32 skip_n_vectors, u32 match_n_vectors)
Definition: vnet_classify.c:99
unformat_function_t unformat_l3_mask
unformat_function_t unformat_ip4_mask
unformat_function_t unformat_classify_mask
int i32
Definition: types.h:81
void vnet_classify_register_unformat_opaque_index_fn(unformat_function_t *fn)
Definition: vnet_classify.c:91
unsigned long u64
Definition: types.h:89
int vlib_main(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:1538
static void vnet_classify_prefetch_bucket(vnet_classify_table_t *t, u64 hash)
u8 * format_classify_table(u8 *s, va_list *args)
int vnet_classify_add_del_table(vnet_classify_main_t *cm, u8 *mask, u32 nbuckets, u32 memory_size, u32 skip, u32 match, u32 next_table_index, u32 miss_next_index, u32 *table_index, int is_add)
static void vnet_classify_prefetch_entry(vnet_classify_table_t *t, u64 hash)
static int vnet_classify_entry_is_free(vnet_classify_entry_t *e)
Definition: vnet_classify.h:89
static u64 vnet_classify_hash_packet_inline(vnet_classify_table_t *t, u8 *h)
unformat_function_t unformat_l3_match
vnet_classify_entry_t * entries
typedef CLIB_PACKED(struct _vnet_classify_entry{u32 next_index;union{struct{u32 opaque_index;i32 advance;};u64 opaque_count;};u32 flags;#define VNET_CLASSIFY_ENTRY_FREEunion{u64 hits;struct _vnet_classify_entry *next_free;};f64 last_heard;u32x4 key[0];}) vnet_classify_entry_t
unformat_function_t unformat_l2_next_index
vnet_main_t vnet_main
Definition: misc.c:42
static vnet_classify_entry_t * vnet_classify_entry_at_index(vnet_classify_table_t *t, vnet_classify_entry_t *e, u32 index)
static uword vnet_classify_get_offset(vnet_classify_table_t *t, vnet_classify_entry_t *v)
vnet_classify_bucket_t saved_bucket
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
void vnet_classify_register_unformat_l2_next_index_fn(unformat_function_t *fn)
Definition: vnet_classify.c:69
void vnet_classify_register_unformat_acl_next_index_fn(unformat_function_t *fn)
Definition: vnet_classify.c:84
struct _vnet_classify_main vnet_classify_main_t
Definition: vnet_classify.h:50
always_inline uword is_pow2(uword x)
Definition: clib.h:252
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
unformat_function_t unformat_ip6_match
u64 uword
Definition: types.h:112
static int vnet_classify_entry_is_busy(vnet_classify_entry_t *e)
Definition: vnet_classify.h:94
vlib_node_registration_t ip6_classify_node
(constructor) VLIB_REGISTER_NODE (ip6_classify_node)
Definition: ip_classify.c:38
double f64
Definition: types.h:140
vnet_classify_bucket_t * buckets
unsigned char u8
Definition: types.h:56
vnet_classify_entry_t * vnet_classify_find_entry(vnet_classify_table_t *t, u8 *h, u64 hash, f64 now)
vlib_node_registration_t ip4_classify_node
(constructor) VLIB_REGISTER_NODE (ip4_classify_node)
Definition: ip_classify.c:37
volatile u32 * writer_lock
void vnet_classify_register_unformat_ip_next_index_fn(unformat_function_t *fn)
Definition: vnet_classify.c:76
unformat_function_t unformat_l2_match
u32 flags
Definition: vhost-user.h:73
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
vnet_classify_entry_t ** freelists
vnet_classify_main_t vnet_classify_main
Definition: vnet_classify.c:21
int vnet_classify_add_del_session(vnet_classify_main_t *cm, u32 table_index, u8 *match, u32 hit_next_index, u32 opaque_index, i32 advance, int is_add)
static vnet_classify_entry_t * vnet_classify_get_entry(vnet_classify_table_t *t, uword offset)