blob: 4ce1b6d3ad5c7d570697adf5b4f84a24d92faef1 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001/* SPDX-License-Identifier: GPL-2.0 */
Sven Eckelmannac79cbb2017-01-01 00:00:00 +01002/* Copyright (C) 2006-2017 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
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
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000017 */
18
19#ifndef _NET_BATMAN_ADV_HASH_H_
20#define _NET_BATMAN_ADV_HASH_H_
21
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020022#include "main.h"
23
24#include <linux/compiler.h>
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025#include <linux/list.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020026#include <linux/rculist.h>
27#include <linux/spinlock.h>
28#include <linux/stddef.h>
29#include <linux/types.h>
30
31struct lock_class_key;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020033/* callback to a compare function. should compare 2 element datas for their
Sven Eckelmann62fe7102015-09-15 19:00:48 +020034 * keys
35 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +010036 * Return: true if same and false if not same
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020037 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +010038typedef bool (*batadv_hashdata_compare_cb)(const struct hlist_node *,
39 const void *);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000040
Sven Eckelmann62fe7102015-09-15 19:00:48 +020041/* the hashfunction
42 *
43 * Return: an index based on the key in the data of the first argument and the
44 * size the second
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020045 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020046typedef u32 (*batadv_hashdata_choose_cb)(const void *, u32);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020047typedef void (*batadv_hashdata_free_cb)(struct hlist_node *, void *);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048
Sven Eckelmannc93effc2017-12-02 19:51:50 +010049/**
50 * struct batadv_hashtable - Wrapper of simple hlist based hashtable
51 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020052struct batadv_hashtable {
Sven Eckelmannc93effc2017-12-02 19:51:50 +010053 /** @table: the hashtable itself with the buckets */
54 struct hlist_head *table;
55
56 /** @list_locks: spinlock for each hash list entry */
57 spinlock_t *list_locks;
58
59 /** @size: size of hashtable */
60 u32 size;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061};
62
63/* allocates and clears the hash */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020064struct batadv_hashtable *batadv_hash_new(u32 size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000065
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020066/* set class key for all locks */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020067void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020068 struct lock_class_key *key);
69
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000070/* free only the hashtable and the hash itself. */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020071void batadv_hash_destroy(struct batadv_hashtable *hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000072
Ben Hutchings2c530402012-07-10 10:55:09 +000073/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010074 * batadv_hash_add() - adds data to the hashtable
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +020075 * @hash: storage hash table
76 * @compare: callback to determine if 2 hash elements are identical
77 * @choose: callback calculating the hash index
78 * @data: data passed to the aforementioned callbacks as argument
79 * @data_node: to be added element
80 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020081 * Return: 0 on success, 1 if the element already is in the hash
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +020082 * and -1 on error.
83 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020084static inline int batadv_hash_add(struct batadv_hashtable *hash,
85 batadv_hashdata_compare_cb compare,
86 batadv_hashdata_choose_cb choose,
Sven Eckelmannc0a55922012-05-12 13:48:55 +020087 const void *data,
88 struct hlist_node *data_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000089{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020090 u32 index;
Antonio Quartullic90681b2011-10-05 17:05:25 +020091 int ret = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000092 struct hlist_head *head;
Marek Lindner7aadf882011-02-18 12:28:09 +000093 struct hlist_node *node;
Marek Lindnerfb778ea2011-01-19 20:01:40 +000094 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000095
96 if (!hash)
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +020097 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000098
99 index = choose(data, hash->size);
100 head = &hash->table[index];
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000101 list_lock = &hash->list_locks[index];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000102
Matthias Schiffer75c5a2e2012-05-08 22:31:57 +0200103 spin_lock_bh(list_lock);
104
105 hlist_for_each(node, head) {
Marek Lindner7aadf882011-02-18 12:28:09 +0000106 if (!compare(node, data))
107 continue;
108
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200109 ret = 1;
Matthias Schiffer75c5a2e2012-05-08 22:31:57 +0200110 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000111 }
112
113 /* no duplicate found in list, add new element */
Marek Lindner7aadf882011-02-18 12:28:09 +0000114 hlist_add_head_rcu(data_node, head);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000115
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200116 ret = 0;
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000117
Matthias Schiffer75c5a2e2012-05-08 22:31:57 +0200118unlock:
119 spin_unlock_bh(list_lock);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200120out:
121 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000122}
123
Sven Eckelmanne57acf82017-12-02 19:51:52 +0100124/**
125 * batadv_hash_remove() - Removes data from hash, if found
126 * @hash: hash table
127 * @compare: callback to determine if 2 hash elements are identical
128 * @choose: callback calculating the hash index
129 * @data: data passed to the aforementioned callbacks as argument
130 *
131 * ata could be the structure you use with just the key filled, we just need
132 * the key for comparing.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200133 *
134 * Return: returns pointer do data on success, so you can remove the used
135 * structure yourself, or NULL on error
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200136 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200137static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
138 batadv_hashdata_compare_cb compare,
139 batadv_hashdata_choose_cb choose,
140 void *data)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000141{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200142 u32 index;
Marek Lindner7aadf882011-02-18 12:28:09 +0000143 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000144 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000145 void *data_save = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000146
147 index = choose(data, hash->size);
148 head = &hash->table[index];
149
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000150 spin_lock_bh(&hash->list_locks[index]);
Marek Lindner7aadf882011-02-18 12:28:09 +0000151 hlist_for_each(node, head) {
152 if (!compare(node, data))
153 continue;
154
155 data_save = node;
156 hlist_del_rcu(node);
157 break;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000158 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000159 spin_unlock_bh(&hash->list_locks[index]);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000161 return data_save;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000162}
163
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000164#endif /* _NET_BATMAN_ADV_HASH_H_ */