Sven Eckelmann | 7db7d9f | 2017-11-19 15:05:11 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Sven Eckelmann | ac79cbb | 2017-01-01 00:00:00 +0100 | [diff] [blame] | 2 | /* Copyright (C) 2006-2017 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 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 |
Antonio Quartulli | ebf38fb | 2013-11-03 20:40:48 +0100 | [diff] [blame] | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #ifndef _NET_BATMAN_ADV_HASH_H_ |
| 20 | #define _NET_BATMAN_ADV_HASH_H_ |
| 21 | |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 22 | #include "main.h" |
| 23 | |
| 24 | #include <linux/compiler.h> |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 25 | #include <linux/list.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 26 | #include <linux/rculist.h> |
| 27 | #include <linux/spinlock.h> |
| 28 | #include <linux/stddef.h> |
| 29 | #include <linux/types.h> |
| 30 | |
| 31 | struct lock_class_key; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 32 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 33 | /* callback to a compare function. should compare 2 element datas for their |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 34 | * keys |
| 35 | * |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 36 | * Return: true if same and false if not same |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 37 | */ |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 38 | typedef bool (*batadv_hashdata_compare_cb)(const struct hlist_node *, |
| 39 | const void *); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 40 | |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 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 |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 45 | */ |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 46 | typedef u32 (*batadv_hashdata_choose_cb)(const void *, u32); |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 47 | typedef void (*batadv_hashdata_free_cb)(struct hlist_node *, void *); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 48 | |
Sven Eckelmann | c93effc | 2017-12-02 19:51:50 +0100 | [diff] [blame] | 49 | /** |
| 50 | * struct batadv_hashtable - Wrapper of simple hlist based hashtable |
| 51 | */ |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 52 | struct batadv_hashtable { |
Sven Eckelmann | c93effc | 2017-12-02 19:51:50 +0100 | [diff] [blame] | 53 | /** @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 Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /* allocates and clears the hash */ |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 64 | struct batadv_hashtable *batadv_hash_new(u32 size); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 65 | |
Sven Eckelmann | 5d52dad | 2012-03-29 12:38:20 +0200 | [diff] [blame] | 66 | /* set class key for all locks */ |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 67 | void batadv_hash_set_lock_class(struct batadv_hashtable *hash, |
Sven Eckelmann | 5d52dad | 2012-03-29 12:38:20 +0200 | [diff] [blame] | 68 | struct lock_class_key *key); |
| 69 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 70 | /* free only the hashtable and the hash itself. */ |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 71 | void batadv_hash_destroy(struct batadv_hashtable *hash); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 72 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 73 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 74 | * batadv_hash_add() - adds data to the hashtable |
Antonio Quartulli | 1a1f37d | 2011-07-10 00:36:36 +0200 | [diff] [blame] | 75 | * @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 Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 81 | * Return: 0 on success, 1 if the element already is in the hash |
Antonio Quartulli | 1a1f37d | 2011-07-10 00:36:36 +0200 | [diff] [blame] | 82 | * and -1 on error. |
| 83 | */ |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 84 | static inline int batadv_hash_add(struct batadv_hashtable *hash, |
| 85 | batadv_hashdata_compare_cb compare, |
| 86 | batadv_hashdata_choose_cb choose, |
Sven Eckelmann | c0a5592 | 2012-05-12 13:48:55 +0200 | [diff] [blame] | 87 | const void *data, |
| 88 | struct hlist_node *data_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 89 | { |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 90 | u32 index; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 91 | int ret = -1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 92 | struct hlist_head *head; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 93 | struct hlist_node *node; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 94 | spinlock_t *list_lock; /* spinlock to protect write access */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 95 | |
| 96 | if (!hash) |
Antonio Quartulli | 1a1f37d | 2011-07-10 00:36:36 +0200 | [diff] [blame] | 97 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 98 | |
| 99 | index = choose(data, hash->size); |
| 100 | head = &hash->table[index]; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 101 | list_lock = &hash->list_locks[index]; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 102 | |
Matthias Schiffer | 75c5a2e | 2012-05-08 22:31:57 +0200 | [diff] [blame] | 103 | spin_lock_bh(list_lock); |
| 104 | |
| 105 | hlist_for_each(node, head) { |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 106 | if (!compare(node, data)) |
| 107 | continue; |
| 108 | |
Antonio Quartulli | 1a1f37d | 2011-07-10 00:36:36 +0200 | [diff] [blame] | 109 | ret = 1; |
Matthias Schiffer | 75c5a2e | 2012-05-08 22:31:57 +0200 | [diff] [blame] | 110 | goto unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* no duplicate found in list, add new element */ |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 114 | hlist_add_head_rcu(data_node, head); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 115 | |
Antonio Quartulli | 1a1f37d | 2011-07-10 00:36:36 +0200 | [diff] [blame] | 116 | ret = 0; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 117 | |
Matthias Schiffer | 75c5a2e | 2012-05-08 22:31:57 +0200 | [diff] [blame] | 118 | unlock: |
| 119 | spin_unlock_bh(list_lock); |
Antonio Quartulli | 1a1f37d | 2011-07-10 00:36:36 +0200 | [diff] [blame] | 120 | out: |
| 121 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Sven Eckelmann | e57acf8 | 2017-12-02 19:51:52 +0100 | [diff] [blame^] | 124 | /** |
| 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 Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 133 | * |
| 134 | * Return: returns pointer do data on success, so you can remove the used |
| 135 | * structure yourself, or NULL on error |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 136 | */ |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 137 | static 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 Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 141 | { |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 142 | u32 index; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 143 | struct hlist_node *node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 144 | struct hlist_head *head; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 145 | void *data_save = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 146 | |
| 147 | index = choose(data, hash->size); |
| 148 | head = &hash->table[index]; |
| 149 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 150 | spin_lock_bh(&hash->list_locks[index]); |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 151 | 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 Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 158 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 159 | spin_unlock_bh(&hash->list_locks[index]); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 160 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 161 | return data_save; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 164 | #endif /* _NET_BATMAN_ADV_HASH_H_ */ |