-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhash.c
132 lines (108 loc) · 3.08 KB
/
hash.c
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright (C) 2013:
* Simon Wunderlich <[email protected]>
* Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#include "wifi_statistics.h"
static inline u32 ws_hash_choose(void *mac)
{
u32 hash = 0;
const u8 *key = mac;
int i;
for (i = 0; i < ETH_ALEN; i++) {
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
return hash % WS_HASH_SIZE;
}
/* try to find an element in the hash, return NULL if not found */
struct ws_sta *ws_hash_find(struct ws_hash *hash, u8 *mac)
{
struct ws_sta *res = NULL, *tmp_sta;
spinlock_t *list_lock; /* spinlock to protect write access */
struct hlist_head *head;
u32 index;
index = ws_hash_choose(mac);
head = &hash->table[index];
list_lock = &hash->list_locks[index];
rcu_read_lock();
hlist_for_each_entry_rcu(tmp_sta, head, hash_entry) {
if (memcmp(mac, tmp_sta->mac, ETH_ALEN))
continue;
if (!atomic_inc_not_zero(&tmp_sta->refcount))
continue;
res = tmp_sta;
break;
}
rcu_read_unlock();
return res;
}
/* like hash_find, but assigns a new element if not present yet */
struct ws_sta *ws_hash_get(struct ws_hash *hash, u8 *mac)
{
struct ws_sta *ws_sta;
spinlock_t *list_lock; /* spinlock to protect write access */
struct hlist_head *head;
u32 index;
ws_sta = ws_hash_find(hash, mac);
if (ws_sta)
return ws_sta;
ws_sta = kzalloc(sizeof(*ws_sta), GFP_ATOMIC);
if (!ws_sta)
return NULL;
ws_sta_init(ws_sta);
memcpy(ws_sta->mac, mac, ETH_ALEN);
/* add new element */
index = ws_hash_choose(mac);
head = &hash->table[index];
list_lock = &hash->list_locks[index];
/* one for the hash, one for returning */
atomic_set(&ws_sta->refcount, 2);
spin_lock_bh(list_lock);
hlist_add_head_rcu(&ws_sta->hash_entry, head);
spin_unlock_bh(list_lock);
return ws_sta;
}
int ws_hash_free(struct ws_hash *hash)
{
struct ws_sta *ws_sta;
struct hlist_node *node;
struct hlist_head *head;
spinlock_t *list_lock; /* protects write access to the hash lists */
int i;
for (i = 0; i < WS_HASH_SIZE; i++) {
head = &hash->table[i];
list_lock = &hash->list_locks[i];
spin_lock_bh(list_lock);
hlist_for_each_entry_safe(ws_sta, node, head, hash_entry) {
hlist_del_rcu(&ws_sta->hash_entry);
ws_sta_free_ref(ws_sta);
}
spin_unlock_bh(list_lock);
}
return 0;
}
int ws_hash_init(struct ws_hash *hash)
{
int i;
for (i = 0; i < WS_HASH_SIZE; i++) {
INIT_HLIST_HEAD(&hash->table[i]);
spin_lock_init(&hash->list_locks[i]);
}
return 0;
}