summaryrefslogtreecommitdiffstats
path: root/src/vnet/hash/hash.h
blob: c1eb9475e28490720b9cc222f13806ad7c9bcc30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * SPDX-License-Identifier: Apache-2.0
 * Copyright(c) 2021 Cisco Systems, Inc.
 */

#ifndef __VNET_HASH_H__
#define __VNET_HASH_H__

#include <vlib/vlib.h>

#define foreach_vnet_hash_fn_types                                            \
  _ (ETHERNET, 0, "hash-fn-ethernet")                                         \
  _ (IP, 1, "hash-fn-ip")

typedef enum
{
#define _(f, n, s) VNET_HASH_FN_TYPE_##f,
  foreach_vnet_hash_fn_types
#undef _
    VNET_HASH_FN_TYPE_N,
} vnet_hash_fn_type_t;

typedef void (*vnet_hash_fn_t) (void **p, u32 *h, u32 n_packets);

typedef struct vnet_hash_function_registration
{
  const char *name;
  const char *description;
  int priority;
  vnet_hash_fn_t function[VNET_HASH_FN_TYPE_N];

  struct vnet_hash_function_registration *next;
} vnet_hash_function_registration_t;

typedef struct
{
  vnet_hash_function_registration_t *hash_registrations;
} vnet_hash_main_t;

extern vnet_hash_main_t vnet_hash_main;

#define VNET_REGISTER_HASH_FUNCTION(x, ...)                                   \
  __VA_ARGS__ vnet_hash_function_registration_t __vnet_hash_function_##x;     \
  static void __clib_constructor __vnet_hash_function_registration_##x (void) \
  {                                                                           \
    vnet_hash_main_t *hm = &vnet_hash_main;                                   \
    __vnet_hash_function_##x.next = hm->hash_registrations;                   \
    hm->hash_registrations = &__vnet_hash_function_##x;                       \
  }                                                                           \
  __VA_ARGS__ vnet_hash_function_registration_t __vnet_hash_function_##x

vnet_hash_fn_t vnet_hash_default_function (vnet_hash_fn_type_t ftype);
vnet_hash_fn_t vnet_hash_function_from_name (const char *name,
					     vnet_hash_fn_type_t ftype);
vnet_hash_function_registration_t *
vnet_hash_function_from_func (vnet_hash_fn_t fn, vnet_hash_fn_type_t ftype);
format_function_t format_vnet_hash;

#endif
>; }; } clib_random_buffer_t; always_inline void clib_random_buffer_free (clib_random_buffer_t * b) { vec_free (b->buffer); } /* Fill random buffer. */ void clib_random_buffer_fill (clib_random_buffer_t * b, uword n_words); /* Initialize random buffer. */ void clib_random_buffer_init (clib_random_buffer_t * b, uword seed); /* Returns word aligned random data, possibly filling buffer. */ always_inline void * clib_random_buffer_get_data (clib_random_buffer_t * b, uword n_bytes) { uword n_words, i, l; l = b->n_cached_bytes; if (n_bytes <= l) { b->n_cached_bytes = l - n_bytes; return &b->cached_bytes[l]; } n_words = n_bytes / sizeof (uword); if (n_bytes % sizeof (uword)) n_words++; /* Enough random words left? */ if (PREDICT_FALSE (n_words > vec_len (b->buffer))) clib_random_buffer_fill (b, n_words); i = vec_len (b->buffer) - n_words; _vec_len (b->buffer) = i; if (n_bytes < sizeof (uword)) { b->cached_word = b->buffer[i]; b->n_cached_bytes = sizeof (uword) - n_bytes; return b->cached_bytes; } else return b->buffer + i; } #endif /* included_clib_random_buffer_h */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */