batman-adv: Add SPDX license identifier above copyright header
[linux-2.6-block.git] / net / batman-adv / hash.h
CommitLineData
7db7d9f3 1/* SPDX-License-Identifier: GPL-2.0 */
ac79cbb9 2/* Copyright (C) 2006-2017 B.A.T.M.A.N. contributors:
c6c8fea2
SE
3 *
4 * Simon Wunderlich, Marek Lindner
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
ebf38fb7 16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
c6c8fea2
SE
17 */
18
19#ifndef _NET_BATMAN_ADV_HASH_H_
20#define _NET_BATMAN_ADV_HASH_H_
21
1e2c2a4f
SE
22#include "main.h"
23
24#include <linux/compiler.h>
c6c8fea2 25#include <linux/list.h>
1e2c2a4f
SE
26#include <linux/rculist.h>
27#include <linux/spinlock.h>
28#include <linux/stddef.h>
29#include <linux/types.h>
30
31struct lock_class_key;
c6c8fea2 32
9cfc7bd6 33/* callback to a compare function. should compare 2 element datas for their
62fe710f
SE
34 * keys
35 *
4b426b10 36 * Return: true if same and false if not same
9cfc7bd6 37 */
4b426b10
SE
38typedef bool (*batadv_hashdata_compare_cb)(const struct hlist_node *,
39 const void *);
c6c8fea2 40
62fe710f
SE
41/* the hashfunction
42 *
43 * Return: an index based on the key in the data of the first argument and the
44 * size the second
9cfc7bd6 45 */
6b5e971a 46typedef u32 (*batadv_hashdata_choose_cb)(const void *, u32);
5bf74e9c 47typedef void (*batadv_hashdata_free_cb)(struct hlist_node *, void *);
c6c8fea2 48
5bf74e9c 49struct batadv_hashtable {
fb778ea1
ML
50 struct hlist_head *table; /* the hashtable itself with the buckets */
51 spinlock_t *list_locks; /* spinlock for each hash list entry */
6b5e971a 52 u32 size; /* size of hashtable */
c6c8fea2
SE
53};
54
55/* allocates and clears the hash */
6b5e971a 56struct batadv_hashtable *batadv_hash_new(u32 size);
c6c8fea2 57
5d52dad2 58/* set class key for all locks */
5bf74e9c 59void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
5d52dad2
SE
60 struct lock_class_key *key);
61
c6c8fea2 62/* free only the hashtable and the hash itself. */
5bf74e9c 63void batadv_hash_destroy(struct batadv_hashtable *hash);
c6c8fea2 64
2c53040f
BH
65/**
66 * batadv_hash_add - adds data to the hashtable
1a1f37d9
AQ
67 * @hash: storage hash table
68 * @compare: callback to determine if 2 hash elements are identical
69 * @choose: callback calculating the hash index
70 * @data: data passed to the aforementioned callbacks as argument
71 * @data_node: to be added element
72 *
62fe710f 73 * Return: 0 on success, 1 if the element already is in the hash
1a1f37d9
AQ
74 * and -1 on error.
75 */
5bf74e9c
SE
76static inline int batadv_hash_add(struct batadv_hashtable *hash,
77 batadv_hashdata_compare_cb compare,
78 batadv_hashdata_choose_cb choose,
c0a55929
SE
79 const void *data,
80 struct hlist_node *data_node)
c6c8fea2 81{
6b5e971a 82 u32 index;
c90681b8 83 int ret = -1;
c6c8fea2 84 struct hlist_head *head;
7aadf889 85 struct hlist_node *node;
fb778ea1 86 spinlock_t *list_lock; /* spinlock to protect write access */
c6c8fea2
SE
87
88 if (!hash)
1a1f37d9 89 goto out;
c6c8fea2
SE
90
91 index = choose(data, hash->size);
92 head = &hash->table[index];
fb778ea1 93 list_lock = &hash->list_locks[index];
c6c8fea2 94
75c5a2e7
MS
95 spin_lock_bh(list_lock);
96
97 hlist_for_each(node, head) {
7aadf889
ML
98 if (!compare(node, data))
99 continue;
100
1a1f37d9 101 ret = 1;
75c5a2e7 102 goto unlock;
c6c8fea2
SE
103 }
104
105 /* no duplicate found in list, add new element */
7aadf889 106 hlist_add_head_rcu(data_node, head);
c6c8fea2 107
1a1f37d9 108 ret = 0;
fb778ea1 109
75c5a2e7
MS
110unlock:
111 spin_unlock_bh(list_lock);
1a1f37d9
AQ
112out:
113 return ret;
c6c8fea2
SE
114}
115
62fe710f
SE
116/* removes data from hash, if found. data could be the structure you use with
117 * just the key filled, we just need the key for comparing.
118 *
119 * Return: returns pointer do data on success, so you can remove the used
120 * structure yourself, or NULL on error
9cfc7bd6 121 */
5bf74e9c
SE
122static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
123 batadv_hashdata_compare_cb compare,
124 batadv_hashdata_choose_cb choose,
125 void *data)
c6c8fea2 126{
6b5e971a 127 u32 index;
7aadf889 128 struct hlist_node *node;
c6c8fea2 129 struct hlist_head *head;
fb778ea1 130 void *data_save = NULL;
c6c8fea2
SE
131
132 index = choose(data, hash->size);
133 head = &hash->table[index];
134
fb778ea1 135 spin_lock_bh(&hash->list_locks[index]);
7aadf889
ML
136 hlist_for_each(node, head) {
137 if (!compare(node, data))
138 continue;
139
140 data_save = node;
141 hlist_del_rcu(node);
142 break;
c6c8fea2 143 }
fb778ea1 144 spin_unlock_bh(&hash->list_locks[index]);
c6c8fea2 145
fb778ea1 146 return data_save;
c6c8fea2
SE
147}
148
c6c8fea2 149#endif /* _NET_BATMAN_ADV_HASH_H_ */