hash: add support for hashing infra
[vpp.git] / src / vnet / hash / hash.h
1 /*
2  * SPDX-License-Identifier: Apache-2.0
3  * Copyright(c) 2021 Cisco Systems, Inc.
4  */
5
6 #ifndef __VNET_HASH_H__
7 #define __VNET_HASH_H__
8
9 #include <vlib/vlib.h>
10
11 #define foreach_vnet_hash_fn_types                                            \
12   _ (ETHERNET, 0, "hash-fn-ethernet")                                         \
13   _ (IP, 1, "hash-fn-ip")
14
15 typedef enum
16 {
17 #define _(f, n, s) VNET_HASH_FN_TYPE_##f,
18   foreach_vnet_hash_fn_types
19 #undef _
20     VNET_HASH_FN_TYPE_N,
21 } vnet_hash_fn_type_t;
22
23 typedef void (*vnet_hash_fn_t) (void **p, u32 *h, u32 n_packets);
24
25 typedef struct vnet_hash_function_registration
26 {
27   const char *name;
28   const char *description;
29   int priority;
30   vnet_hash_fn_t function[VNET_HASH_FN_TYPE_N];
31
32   struct vnet_hash_function_registration *next;
33 } vnet_hash_function_registration_t;
34
35 typedef struct
36 {
37   vnet_hash_function_registration_t *hash_registrations;
38 } vnet_hash_main_t;
39
40 extern vnet_hash_main_t vnet_hash_main;
41
42 #define VNET_REGISTER_HASH_FUNCTION(x, ...)                                   \
43   __VA_ARGS__ vnet_hash_function_registration_t __vnet_hash_function_##x;     \
44   static void __clib_constructor __vnet_hash_function_registration_##x (void) \
45   {                                                                           \
46     vnet_hash_main_t *hm = &vnet_hash_main;                                   \
47     __vnet_hash_function_##x.next = hm->hash_registrations;                   \
48     hm->hash_registrations = &__vnet_hash_function_##x;                       \
49   }                                                                           \
50   __VA_ARGS__ vnet_hash_function_registration_t __vnet_hash_function_##x
51
52 vnet_hash_fn_t vnet_hash_default_function (vnet_hash_fn_type_t ftype);
53 vnet_hash_fn_t vnet_hash_function_from_name (const char *name,
54                                              vnet_hash_fn_type_t ftype);
55 vnet_hash_function_registration_t *
56 vnet_hash_function_from_func (vnet_hash_fn_t fn, vnet_hash_fn_type_t ftype);
57 format_function_t format_vnet_hash;
58
59 #endif