blob: 31693c9889b98fdfb8ce9d9f0ca76765950a73d8 [file] [log] [blame]
Mohsin Kazmi41b23bc2021-06-30 18:26:25 +00001/*
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
11vnet_hash_main_t vnet_hash_main;
12
13u8 *
14format_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 */
28vnet_hash_fn_t
29vnet_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
42vnet_hash_fn_t
43vnet_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
57vnet_hash_function_registration_t *
58vnet_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
70static clib_error_t *
71vnet_hash_init (vlib_main_t *vm)
72{
73 return (0);
74}
75
76VLIB_INIT_FUNCTION (vnet_hash_init);