FD.io VPP  v18.10-34-gcce845e
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/api_errno.h> /* for API error numbers */
30 
31 #include <vppinfra/error.h>
32 #include <vppinfra/hash.h>
33 #include <vppinfra/cache.h>
34 #include <vppinfra/xxhash.h>
35 
38 
39 #define CLASSIFY_TRACE 0
40 
41 #define U32X4_ALIGNED(p) PREDICT_TRUE((((intptr_t)p) & 0xf) == 0)
42 
43 /*
44  * Classify table option to process packets
45  * CLASSIFY_FLAG_USE_CURR_DATA:
46  * - classify packets starting from VPP node’s current data pointer
47  */
48 #define CLASSIFY_FLAG_USE_CURR_DATA 1
49 
50 /*
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
58  */
60 {
64 } __attribute__ ((packed)) vnet_classify_action_t;
65 
66 struct _vnet_classify_main;
67 typedef struct _vnet_classify_main vnet_classify_main_t;
68 
69 #define foreach_size_in_u32x4 \
70 _(1) \
71 _(2) \
72 _(3) \
73 _(4) \
74 _(5)
75 
76 /* *INDENT-OFF* */
77 typedef CLIB_PACKED(struct _vnet_classify_entry {
78  /* Graph node next index */
79  u32 next_index;
80 
81  /* put into vnet_buffer(b)->l2_classfy.opaque_index */
82  union {
83  struct {
84  u32 opaque_index;
85  /* advance on hit, note it's a signed quantity... */
86  i32 advance;
87  };
88  u64 opaque_count;
89  };
90 
91  /* Really only need 1 bit */
92  u8 flags;
93 #define VNET_CLASSIFY_ENTRY_FREE (1<<0)
94 
95  vnet_classify_action_t action;
96  u16 metadata;
97 
98  /* Hit counter, last heard time */
99  union {
100  u64 hits;
101  struct _vnet_classify_entry * next_free;
102  };
103 
104  f64 last_heard;
105 
106  /* Must be aligned to a 16-octet boundary */
107  u32x4 key[0];
108 }) vnet_classify_entry_t;
109 /* *INDENT-ON* */
110 
111 static inline int
112 vnet_classify_entry_is_free (vnet_classify_entry_t * e)
113 {
114  return e->flags & VNET_CLASSIFY_ENTRY_FREE;
115 }
116 
117 static inline int
118 vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
119 {
120  return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
121 }
122 
123 /* Need these to con the vector allocator */
124 /* *INDENT-OFF* */
125 #define _(size) \
126 typedef CLIB_PACKED(struct { \
127  u32 pad0[4]; \
128  u64 pad1[2]; \
129  u32x4 key[size]; \
130 }) vnet_classify_entry_##size##_t;
132 /* *INDENT-ON* */
133 #undef _
134 
135 typedef struct
136 {
137  union
138  {
139  struct
140  {
143  u8 pad[2];
145  };
147  };
149 
150 typedef struct
151 {
152  /* Mask to apply after skipping N vectors */
154  /* Buckets and entries */
156  vnet_classify_entry_t *entries;
157 
158  /* Config parameters */
169  /* Index of next table to try */
171 
172  /* Miss next index, return if next_table_index = 0 */
174 
175  /* Per-bucket working copies, one per thread */
176  vnet_classify_entry_t **working_copies;
179 
180  /* Free entry freelists */
181  vnet_classify_entry_t **freelists;
182 
184 
185  /* Private allocation arena, protected by the writer lock */
186  void *mheap;
187 
188  /* Writer (only) lock for this table */
189  volatile u32 *writer_lock;
190 
192 
193 struct _vnet_classify_main
194 {
195  /* Table pool */
196  vnet_classify_table_t *tables;
197 
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;
204 
205  /* convenience variables */
208 };
209 
210 extern vnet_classify_main_t vnet_classify_main;
211 
212 u8 *format_classify_table (u8 * s, va_list * args);
213 
215 
216 static inline u64
218 {
219  u32x4 *mask;
220 
221  union
222  {
223  u32x4 as_u32x4;
224  u64 as_u64[2];
225  } xor_sum __attribute__ ((aligned (sizeof (u32x4))));
226 
227  ASSERT (t);
228  mask = t->mask;
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)
235  {
236  case 5:
237  xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4];
238  /* FALLTHROUGH */
239  case 4:
240  xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3];
241  /* FALLTHROUGH */
242  case 3:
243  xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2];
244  /* FALLTHROUGH */
245  case 2:
246  xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1];
247  /* FALLTHROUGH */
248  case 1:
249  break;
250  default:
251  abort ();
252  }
253  }
254  else
255 #endif /* CLIB_HAVE_VEC128 */
256  {
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)
262  {
263  case 5:
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];
266  /* FALLTHROUGH */
267  case 4:
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];
270  /* FALLTHROUGH */
271  case 3:
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];
274  /* FALLTHROUGH */
275  case 2:
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];
278  /* FALLTHROUGH */
279  case 1:
280  break;
281 
282  default:
283  abort ();
284  }
285  }
286 
287  return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
288 }
289 
290 static inline void
292 {
293  u32 bucket_index;
294 
295  ASSERT (is_pow2 (t->nbuckets));
296 
297  bucket_index = hash & (t->nbuckets - 1);
298 
299  CLIB_PREFETCH (&t->buckets[bucket_index], CLIB_CACHE_LINE_BYTES, LOAD);
300 }
301 
302 static inline vnet_classify_entry_t *
304 {
305  u8 *hp = t->mheap;
306  u8 *vp = hp + offset;
307 
308  return (void *) vp;
309 }
310 
311 static inline uword
313  vnet_classify_entry_t * v)
314 {
315  u8 *hp, *vp;
316 
317  hp = (u8 *) t->mheap;
318  vp = (u8 *) v;
319 
320  ASSERT ((vp - hp) < 0x100000000ULL);
321  return vp - hp;
322 }
323 
324 static inline vnet_classify_entry_t *
326  vnet_classify_entry_t * e, u32 index)
327 {
328  u8 *eu8;
329 
330  eu8 = (u8 *) e;
331 
332  eu8 += index * (sizeof (vnet_classify_entry_t) +
333  (t->match_n_vectors * sizeof (u32x4)));
334 
335  return (vnet_classify_entry_t *) eu8;
336 }
337 
338 static inline void
340 {
341  u32 bucket_index;
342  u32 value_index;
344  vnet_classify_entry_t *e;
345 
346  bucket_index = hash & (t->nbuckets - 1);
347 
348  b = &t->buckets[bucket_index];
349 
350  if (b->offset == 0)
351  return;
352 
353  hash >>= t->log2_nbuckets;
354 
355  e = vnet_classify_get_entry (t, b->offset);
356  value_index = hash & ((1 << b->log2_pages) - 1);
357 
358  e = vnet_classify_entry_at_index (t, e, value_index);
359 
361 }
362 
363 vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t,
364  u8 * h, u64 hash, f64 now);
365 
366 static inline vnet_classify_entry_t *
368  u8 * h, u64 hash, f64 now)
369 {
370  vnet_classify_entry_t *v;
371  u32x4 *mask, *key;
372  union
373  {
374  u32x4 as_u32x4;
375  u64 as_u64[2];
376  } result __attribute__ ((aligned (sizeof (u32x4))));
378  u32 value_index;
379  u32 bucket_index;
380  u32 limit;
381  int i;
382 
383  bucket_index = hash & (t->nbuckets - 1);
384  b = &t->buckets[bucket_index];
385  mask = t->mask;
386 
387  if (b->offset == 0)
388  return 0;
389 
390  hash >>= t->log2_nbuckets;
391 
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))
396  {
397  value_index = 0;
398  limit *= (1 << b->log2_pages);
399  }
400 
401  v = vnet_classify_entry_at_index (t, v, value_index);
402 
403 #ifdef CLIB_HAVE_VEC128
404  if (U32X4_ALIGNED (h))
405  {
406  u32x4 *data = (u32x4 *) h;
407  for (i = 0; i < limit; i++)
408  {
409  key = v->key;
410  result.as_u32x4 = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
411  switch (t->match_n_vectors)
412  {
413  case 5:
414  result.as_u32x4 |=
415  (data[4 + t->skip_n_vectors] & mask[4]) ^ key[4];
416  /* FALLTHROUGH */
417  case 4:
418  result.as_u32x4 |=
419  (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3];
420  /* FALLTHROUGH */
421  case 3:
422  result.as_u32x4 |=
423  (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
424  /* FALLTHROUGH */
425  case 2:
426  result.as_u32x4 |=
427  (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
428  /* FALLTHROUGH */
429  case 1:
430  break;
431  default:
432  abort ();
433  }
434 
435  if (u32x4_zero_byte_mask (result.as_u32x4) == 0xffff)
436  {
437  if (PREDICT_TRUE (now))
438  {
439  v->hits++;
440  v->last_heard = now;
441  }
442  return (v);
443  }
444  v = vnet_classify_entry_at_index (t, v, 1);
445  }
446  }
447  else
448 #endif /* CLIB_HAVE_VEC128 */
449  {
450  u32 skip_u64 = t->skip_n_vectors * 2;
451  u64 *data64 = (u64 *) h;
452  for (i = 0; i < limit; i++)
453  {
454  key = v->key;
455 
456  result.as_u64[0] =
457  (data64[0 + skip_u64] & ((u64 *) mask)[0]) ^ ((u64 *) key)[0];
458  result.as_u64[1] =
459  (data64[1 + skip_u64] & ((u64 *) mask)[1]) ^ ((u64 *) key)[1];
460  switch (t->match_n_vectors)
461  {
462  case 5:
463  result.as_u64[0] |=
464  (data64[8 + skip_u64] & ((u64 *) mask)[8]) ^ ((u64 *) key)[8];
465  result.as_u64[1] |=
466  (data64[9 + skip_u64] & ((u64 *) mask)[9]) ^ ((u64 *) key)[9];
467  /* FALLTHROUGH */
468  case 4:
469  result.as_u64[0] |=
470  (data64[6 + skip_u64] & ((u64 *) mask)[6]) ^ ((u64 *) key)[6];
471  result.as_u64[1] |=
472  (data64[7 + skip_u64] & ((u64 *) mask)[7]) ^ ((u64 *) key)[7];
473  /* FALLTHROUGH */
474  case 3:
475  result.as_u64[0] |=
476  (data64[4 + skip_u64] & ((u64 *) mask)[4]) ^ ((u64 *) key)[4];
477  result.as_u64[1] |=
478  (data64[5 + skip_u64] & ((u64 *) mask)[5]) ^ ((u64 *) key)[5];
479  /* FALLTHROUGH */
480  case 2:
481  result.as_u64[0] |=
482  (data64[2 + skip_u64] & ((u64 *) mask)[2]) ^ ((u64 *) key)[2];
483  result.as_u64[1] |=
484  (data64[3 + skip_u64] & ((u64 *) mask)[3]) ^ ((u64 *) key)[3];
485  /* FALLTHROUGH */
486  case 1:
487  break;
488  default:
489  abort ();
490  }
491 
492  if (result.as_u64[0] == 0 && result.as_u64[1] == 0)
493  {
494  if (PREDICT_TRUE (now))
495  {
496  v->hits++;
497  v->last_heard = now;
498  }
499  return (v);
500  }
501 
502  v = vnet_classify_entry_at_index (t, v, 1);
503  }
504  }
505  return 0;
506 }
507 
508 vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t * cm,
509  u8 * mask, u32 nbuckets,
510  u32 memory_size,
511  u32 skip_n_vectors,
512  u32 match_n_vectors);
513 
514 int vnet_classify_add_del_session (vnet_classify_main_t * cm,
515  u32 table_index,
516  u8 * match,
517  u32 hit_next_index,
518  u32 opaque_index,
519  i32 advance,
520  u8 action, u32 metadata, int is_add);
521 
522 int vnet_classify_add_del_table (vnet_classify_main_t * cm,
523  u8 * mask,
524  u32 nbuckets,
525  u32 memory_size,
526  u32 skip,
527  u32 match,
528  u32 next_table_index,
529  u32 miss_next_index,
530  u32 * table_index,
531  u8 current_data_flag,
532  i16 current_data_offset,
533  int is_add, int del_chain);
534 
549 
551  (unformat_function_t * fn);
552 
554  (unformat_function_t * fn);
555 
557  (unformat_function_t * fn);
558 
560  (unformat_function_t * fn);
561 
563  fn);
564 
565 #endif /* __included_vnet_classify_h__ */
566 
567 /*
568  * fd.io coding-style-patch-verification: ON
569  *
570  * Local Variables:
571  * eval: (c-set-style "gnu")
572  * End:
573  */
u64 vnet_classify_hash_packet(vnet_classify_table_t *t, u8 *h)
unformat_function_t unformat_ip4_match
vnet_classify_entry_t ** working_copies
uword( unformat_function_t)(unformat_input_t *input, va_list *args)
Definition: format.h:232
unformat_function_t unformat_vlan_tag
u8 pad[3]
log2 (size of the packing page block)
Definition: bihash_doc.h:61
unformat_function_t unformat_l2_mask
unformat_function_t unformat_ip_next_index
#define foreach_size_in_u32x4
Definition: vnet_classify.h:69
u64 as_u64
Definition: bihash_doc.h:63
#define PREDICT_TRUE(x)
Definition: clib.h:108
static vnet_classify_entry_t * vnet_classify_find_entry_inline(vnet_classify_table_t *t, u8 *h, u64 hash, f64 now)
unsigned long u64
Definition: types.h:89
#define VNET_CLASSIFY_ENTRY_FREE
unformat_function_t unformat_ip6_mask
#define U32X4_ALIGNED(p)
Definition: vnet_classify.h:41
static u64 clib_xxhash(u64 key)
Definition: xxhash.h:58
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, u8 action, u32 metadata, int is_add)
int i
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)
unformat_function_t unformat_l3_mask
unsigned char u8
Definition: types.h:56
unformat_function_t unformat_ip4_mask
double f64
Definition: types.h:142
unformat_function_t unformat_classify_mask
vnet_classify_action_t_
Definition: vnet_classify.h:59
typedef CLIB_PACKED(struct _vnet_classify_entry{u32 next_index;union{struct{u32 opaque_index;i32 advance;};u64 opaque_count;};u8 flags;#define VNET_CLASSIFY_ENTRY_FREE vnet_classify_action_t action;u16 metadata;union{u64 hits;struct _vnet_classify_entry *next_free;};f64 last_heard;u32x4 key[0];}) vnet_classify_entry_t
void vnet_classify_register_unformat_opaque_index_fn(unformat_function_t *fn)
unsigned int u32
Definition: types.h:88
static void vnet_classify_prefetch_bucket(vnet_classify_table_t *t, u64 hash)
u8 * format_classify_table(u8 *s, va_list *args)
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)
static u64 vnet_classify_hash_packet_inline(vnet_classify_table_t *t, u8 *h)
unformat_function_t unformat_l3_match
#define v
Definition: acl.c:496
unsigned short u16
Definition: types.h:57
vnet_classify_entry_t * entries
#define PREDICT_FALSE(x)
Definition: clib.h:107
unformat_function_t unformat_l2_next_index
vnet_main_t vnet_main
Definition: misc.c:43
void vnet_classify_register_unformat_policer_next_index_fn(unformat_function_t *fn)
static vnet_classify_entry_t * vnet_classify_entry_at_index(vnet_classify_table_t *t, vnet_classify_entry_t *e, u32 index)
u32 flags
Definition: vhost_user.h:115
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:79
void vnet_classify_register_unformat_l2_next_index_fn(unformat_function_t *fn)
Definition: vnet_classify.c:80
void vnet_classify_register_unformat_acl_next_index_fn(unformat_function_t *fn)
Definition: vnet_classify.c:96
signed int i32
Definition: types.h:77
#define ASSERT(truth)
unformat_function_t unformat_ip6_match
static uword is_pow2(uword x)
Definition: clib.h:231
int vlib_main(vlib_main_t *volatile vm, unformat_input_t *input)
Definition: main.c:1689
static int vnet_classify_entry_is_busy(vnet_classify_entry_t *e)
struct _vlib_node_registration vlib_node_registration_t
template key/value backing page structure
Definition: bihash_doc.h:44
vlib_node_registration_t ip6_classify_node
(constructor) VLIB_REGISTER_NODE (ip6_classify_node)
Definition: ip_classify.c:41
vnet_classify_bucket_t * buckets
u64 uword
Definition: types.h:112
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:40
unformat_function_t unformat_l4_match
volatile u32 * writer_lock
void vnet_classify_register_unformat_ip_next_index_fn(unformat_function_t *fn)
Definition: vnet_classify.c:88
struct clib_bihash_value offset
template key/value backing page structure
unsigned long long u32x4
Definition: ixge.c:28
unformat_function_t unformat_l2_match
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
vnet_classify_entry_t ** freelists
vnet_classify_main_t vnet_classify_main
Definition: vnet_classify.c:22
static u32 u32x4_zero_byte_mask(u32x4 x)
static vnet_classify_entry_t * vnet_classify_get_entry(vnet_classify_table_t *t, uword offset)
signed short i16
Definition: types.h:46
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, u8 current_data_flag, i16 current_data_offset, int is_add, int del_chain)