hash: add support for hashing infra
[vpp.git] / src / vnet / hash / hash.c
1 /*
2  * SPDX-License-Identifier: Apache-2.0
3  * Copyright(c) 2021 Cisco Systems, Inc.
4  */
5
6 #include <vlib/vlib.h>
7 #include <vnet/vnet.h>
8 #include <vnet/interface.h>
9 #include <vnet/hash/hash.h>
10
11 vnet_hash_main_t vnet_hash_main;
12
13 u8 *
14 format_vnet_hash (u8 *s, va_list *args)
15 {
16   vnet_hash_function_registration_t *hash =
17     va_arg (*args, vnet_hash_function_registration_t *);
18
19   s = format (s, "[name: %s ", hash->name);
20   s = format (s, "priority: %u ", hash->priority);
21   s = format (s, "description: %s]", hash->description);
22   return s;
23 }
24
25 /**
26  * select hash func with highest priority
27  */
28 vnet_hash_fn_t
29 vnet_hash_default_function (vnet_hash_fn_type_t ftype)
30 {
31   vnet_hash_function_registration_t *hash = vnet_hash_main.hash_registrations;
32   vnet_hash_function_registration_t *tmp_hash = hash;
33   while (hash)
34     {
35       if (hash->priority > tmp_hash->priority)
36         tmp_hash = hash;
37       hash = hash->next;
38     }
39   return tmp_hash->function[ftype];
40 }
41
42 vnet_hash_fn_t
43 vnet_hash_function_from_name (const char *name, vnet_hash_fn_type_t ftype)
44 {
45   vnet_hash_function_registration_t *hash = vnet_hash_main.hash_registrations;
46   while (hash)
47     {
48       if (strcmp (hash->name, name) == 0)
49         break;
50       hash = hash->next;
51     }
52   if (!hash)
53     return (0);
54   return hash->function[ftype];
55 }
56
57 vnet_hash_function_registration_t *
58 vnet_hash_function_from_func (vnet_hash_fn_t fn, vnet_hash_fn_type_t ftype)
59 {
60   vnet_hash_function_registration_t *hash = vnet_hash_main.hash_registrations;
61   while (hash)
62     {
63       if (hash->function[ftype] == fn)
64         break;
65       hash = hash->next;
66     }
67   return hash;
68 }
69
70 static clib_error_t *
71 vnet_hash_init (vlib_main_t *vm)
72 {
73   return (0);
74 }
75
76 VLIB_INIT_FUNCTION (vnet_hash_init);