aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/hash/hash.c
blob: 31693c9889b98fdfb8ce9d9f0ca76765950a73d8 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * SPDX-License-Identifier: Apache-2.0
 * Copyright(c) 2021 Cisco Systems, Inc.
 */

#include <vlib/vlib.h>
#include <vnet/vnet.h>
#include <vnet/interface.h>
#include <vnet/hash/hash.h>

vnet_hash_main_t vnet_hash_main;

u8 *
format_vnet_hash (u8 *s, va_list *args)
{
  vnet_hash_function_registration_t *hash =
    va_arg (*args, vnet_hash_function_registration_t *);

  s = format (s, "[name: %s ", hash->name);
  s = format (s, "priority: %u ", hash->priority);
  s = format (s, "description: %s]", hash->description);
  return s;
}

/**
 * select hash func with highest priority
 */
vnet_hash_fn_t
vnet_hash_default_function (vnet_hash_fn_type_t ftype)
{
  vnet_hash_function_registration_t *hash = vnet_hash_main.hash_registrations;
  vnet_hash_function_registration_t *tmp_hash = hash;
  while (hash)
    {
      if (hash->priority > tmp_hash->priority)
	tmp_hash = hash;
      hash = hash->next;
    }
  return tmp_hash->function[ftype];
}

vnet_hash_fn_t
vnet_hash_function_from_name (const char *name, vnet_hash_fn_type_t ftype)
{
  vnet_hash_function_registration_t *hash = vnet_hash_main.hash_registrations;
  while (hash)
    {
      if (strcmp (hash->name, name) == 0)
	break;
      hash = hash->next;
    }
  if (!hash)
    return (0);
  return hash->function[ftype];
}

vnet_hash_function_registration_t *
vnet_hash_function_from_func (vnet_hash_fn_t fn, vnet_hash_fn_type_t ftype)
{
  vnet_hash_function_registration_t *hash = vnet_hash_main.hash_registrations;
  while (hash)
    {
      if (hash->function[ftype] == fn)
	break;
      hash = hash->next;
    }
  return hash;
}

static clib_error_t *
vnet_hash_init (vlib_main_t *vm)
{
  return (0);
}

VLIB_INIT_FUNCTION (vnet_hash_init);