blob: fb251c385a1bbe7aa4fc83909ee52c4c6189d921 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001/* SPDX-License-Identifier: GPL-2.0 */
Sven Eckelmanncfa55c62021-01-01 00:00:01 +01002/* Copyright (C) B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Simon Wunderlich, Marek Lindner
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00005 */
6
7#ifndef _NET_BATMAN_ADV_HASH_H_
8#define _NET_BATMAN_ADV_HASH_H_
9
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020010#include "main.h"
11
Sven Eckelmann05abd7b2018-10-30 22:01:25 +010012#include <linux/atomic.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020013#include <linux/compiler.h>
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000014#include <linux/list.h>
Sven Eckelmann68a600d2019-05-24 20:11:17 +020015#include <linux/lockdep.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020016#include <linux/rculist.h>
17#include <linux/spinlock.h>
18#include <linux/stddef.h>
19#include <linux/types.h>
20
Zheng Yongjun791ad7f2021-06-02 14:56:03 +080021/* callback to a compare function. should compare 2 element data for their
Sven Eckelmann62fe7102015-09-15 19:00:48 +020022 * keys
23 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +010024 * Return: true if same and false if not same
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020025 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +010026typedef bool (*batadv_hashdata_compare_cb)(const struct hlist_node *,
27 const void *);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000028
Sven Eckelmann62fe7102015-09-15 19:00:48 +020029/* the hashfunction
30 *
31 * Return: an index based on the key in the data of the first argument and the
32 * size the second
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020033 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020034typedef u32 (*batadv_hashdata_choose_cb)(const void *, u32);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020035typedef void (*batadv_hashdata_free_cb)(struct hlist_node *, void *);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036
Sven Eckelmannc93effc2017-12-02 19:51:50 +010037/**
38 * struct batadv_hashtable - Wrapper of simple hlist based hashtable
39 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020040struct batadv_hashtable {
Sven Eckelmannc93effc2017-12-02 19:51:50 +010041 /** @table: the hashtable itself with the buckets */
42 struct hlist_head *table;
43
44 /** @list_locks: spinlock for each hash list entry */
45 spinlock_t *list_locks;
46
47 /** @size: size of hashtable */
48 u32 size;
Sven Eckelmann05abd7b2018-10-30 22:01:25 +010049
50 /** @generation: current (generation) sequence number */
51 atomic_t generation;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052};
53
54/* allocates and clears the hash */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020055struct batadv_hashtable *batadv_hash_new(u32 size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020057/* set class key for all locks */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020058void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020059 struct lock_class_key *key);
60
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061/* free only the hashtable and the hash itself. */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020062void batadv_hash_destroy(struct batadv_hashtable *hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063
Ben Hutchings2c530402012-07-10 10:55:09 +000064/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010065 * batadv_hash_add() - adds data to the hashtable
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +020066 * @hash: storage hash table
67 * @compare: callback to determine if 2 hash elements are identical
68 * @choose: callback calculating the hash index
69 * @data: data passed to the aforementioned callbacks as argument
70 * @data_node: to be added element
71 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020072 * Return: 0 on success, 1 if the element already is in the hash
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +020073 * and -1 on error.
74 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020075static inline int batadv_hash_add(struct batadv_hashtable *hash,
76 batadv_hashdata_compare_cb compare,
77 batadv_hashdata_choose_cb choose,
Sven Eckelmannc0a55922012-05-12 13:48:55 +020078 const void *data,
79 struct hlist_node *data_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000080{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020081 u32 index;
Antonio Quartullic90681b2011-10-05 17:05:25 +020082 int ret = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000083 struct hlist_head *head;
Marek Lindner7aadf882011-02-18 12:28:09 +000084 struct hlist_node *node;
Marek Lindnerfb778ea2011-01-19 20:01:40 +000085 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000086
87 if (!hash)
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +020088 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000089
90 index = choose(data, hash->size);
91 head = &hash->table[index];
Marek Lindnerfb778ea2011-01-19 20:01:40 +000092 list_lock = &hash->list_locks[index];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000093
Matthias Schiffer75c5a2e2012-05-08 22:31:57 +020094 spin_lock_bh(list_lock);
95
96 hlist_for_each(node, head) {
Marek Lindner7aadf882011-02-18 12:28:09 +000097 if (!compare(node, data))
98 continue;
99
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200100 ret = 1;
Matthias Schiffer75c5a2e2012-05-08 22:31:57 +0200101 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000102 }
103
104 /* no duplicate found in list, add new element */
Marek Lindner7aadf882011-02-18 12:28:09 +0000105 hlist_add_head_rcu(data_node, head);
Sven Eckelmann05abd7b2018-10-30 22:01:25 +0100106 atomic_inc(&hash->generation);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000107
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200108 ret = 0;
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000109
Matthias Schiffer75c5a2e2012-05-08 22:31:57 +0200110unlock:
111 spin_unlock_bh(list_lock);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200112out:
113 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000114}
115
Sven Eckelmanne57acf82017-12-02 19:51:52 +0100116/**
117 * batadv_hash_remove() - Removes data from hash, if found
118 * @hash: hash table
119 * @compare: callback to determine if 2 hash elements are identical
120 * @choose: callback calculating the hash index
121 * @data: data passed to the aforementioned callbacks as argument
122 *
123 * ata could be the structure you use with just the key filled, we just need
124 * the key for comparing.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200125 *
126 * Return: returns pointer do data on success, so you can remove the used
127 * structure yourself, or NULL on error
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200128 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200129static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
130 batadv_hashdata_compare_cb compare,
131 batadv_hashdata_choose_cb choose,
132 void *data)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200134 u32 index;
Marek Lindner7aadf882011-02-18 12:28:09 +0000135 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000137 void *data_save = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000138
139 index = choose(data, hash->size);
140 head = &hash->table[index];
141
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000142 spin_lock_bh(&hash->list_locks[index]);
Marek Lindner7aadf882011-02-18 12:28:09 +0000143 hlist_for_each(node, head) {
144 if (!compare(node, data))
145 continue;
146
147 data_save = node;
148 hlist_del_rcu(node);
Sven Eckelmann05abd7b2018-10-30 22:01:25 +0100149 atomic_inc(&hash->generation);
Marek Lindner7aadf882011-02-18 12:28:09 +0000150 break;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000152 spin_unlock_bh(&hash->list_locks[index]);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000153
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000154 return data_save;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155}
156
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000157#endif /* _NET_BATMAN_ADV_HASH_H_ */