rhashtable: Use rht_obj() instead of manual offset calculation
[deliverable/linux.git] / net / netfilter / nft_hash.c
CommitLineData
96518518 1/*
ce6eb0d7 2 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
96518518
PM
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/list.h>
c50b960c 15#include <linux/log2.h>
96518518
PM
16#include <linux/jhash.h>
17#include <linux/netlink.h>
cfe4a9dd 18#include <linux/rhashtable.h>
96518518
PM
19#include <linux/netfilter.h>
20#include <linux/netfilter/nf_tables.h>
21#include <net/netfilter/nf_tables.h>
22
cfe4a9dd
TG
23/* We target a hash table size of 4, element hint is 75% of final size */
24#define NFT_HASH_ELEMENT_HINT 3
96518518
PM
25
26struct nft_hash_elem {
cfe4a9dd 27 struct rhash_head node;
ce6eb0d7
PM
28 struct nft_data key;
29 struct nft_data data[];
96518518
PM
30};
31
20a69341
PM
32static bool nft_hash_lookup(const struct nft_set *set,
33 const struct nft_data *key,
34 struct nft_data *data)
96518518 35{
cfe4a9dd 36 const struct rhashtable *priv = nft_set_priv(set);
20a69341 37 const struct nft_hash_elem *he;
ce6eb0d7 38
cfe4a9dd
TG
39 he = rhashtable_lookup(priv, key);
40 if (he && set->flags & NFT_SET_MAP)
41 nft_data_copy(data, he->data);
ce6eb0d7 42
cfe4a9dd 43 return !!he;
96518518
PM
44}
45
20a69341
PM
46static int nft_hash_insert(const struct nft_set *set,
47 const struct nft_set_elem *elem)
96518518 48{
cfe4a9dd 49 struct rhashtable *priv = nft_set_priv(set);
20a69341 50 struct nft_hash_elem *he;
cfe4a9dd 51 unsigned int size;
96518518 52
20a69341 53 if (elem->flags != 0)
96518518 54 return -EINVAL;
96518518 55
20a69341
PM
56 size = sizeof(*he);
57 if (set->flags & NFT_SET_MAP)
58 size += sizeof(he->data[0]);
59
60 he = kzalloc(size, GFP_KERNEL);
61 if (he == NULL)
96518518
PM
62 return -ENOMEM;
63
20a69341
PM
64 nft_data_copy(&he->key, &elem->key);
65 if (set->flags & NFT_SET_MAP)
66 nft_data_copy(he->data, &elem->data);
96518518 67
6eba8224 68 rhashtable_insert(priv, &he->node);
ce6eb0d7 69
96518518 70 return 0;
96518518
PM
71}
72
ce6eb0d7
PM
73static void nft_hash_elem_destroy(const struct nft_set *set,
74 struct nft_hash_elem *he)
75{
76 nft_data_uninit(&he->key, NFT_DATA_VALUE);
77 if (set->flags & NFT_SET_MAP)
78 nft_data_uninit(he->data, set->dtype);
79 kfree(he);
80}
81
20a69341
PM
82static void nft_hash_remove(const struct nft_set *set,
83 const struct nft_set_elem *elem)
96518518 84{
cfe4a9dd
TG
85 struct rhashtable *priv = nft_set_priv(set);
86 struct rhash_head *he, __rcu **pprev;
96518518 87
ce6eb0d7 88 pprev = elem->cookie;
cfe4a9dd
TG
89 he = rht_dereference((*pprev), priv);
90
6eba8224 91 rhashtable_remove_pprev(priv, he, pprev);
ce6eb0d7 92
ce6eb0d7 93 synchronize_rcu();
20a69341
PM
94 kfree(he);
95}
96518518 96
8d24c0b4
TG
97struct nft_compare_arg {
98 const struct nft_set *set;
99 struct nft_set_elem *elem;
100};
101
102static bool nft_hash_compare(void *ptr, void *arg)
103{
104 struct nft_hash_elem *he = ptr;
105 struct nft_compare_arg *x = arg;
106
107 if (!nft_data_cmp(&he->key, &x->elem->key, x->set->klen)) {
108 x->elem->cookie = &he->node;
109 x->elem->flags = 0;
110 if (x->set->flags & NFT_SET_MAP)
111 nft_data_copy(&x->elem->data, he->data);
112
113 return true;
114 }
115
116 return false;
117}
118
20a69341
PM
119static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem)
120{
cfe4a9dd 121 const struct rhashtable *priv = nft_set_priv(set);
8d24c0b4
TG
122 struct nft_compare_arg arg = {
123 .set = set,
124 .elem = elem,
125 };
126
127 if (rhashtable_lookup_compare(priv, &elem->key,
128 &nft_hash_compare, &arg))
20a69341 129 return 0;
8d24c0b4 130
20a69341 131 return -ENOENT;
96518518
PM
132}
133
20a69341
PM
134static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
135 struct nft_set_iter *iter)
96518518 136{
cfe4a9dd
TG
137 const struct rhashtable *priv = nft_set_priv(set);
138 const struct bucket_table *tbl;
20a69341
PM
139 const struct nft_hash_elem *he;
140 struct nft_set_elem elem;
96518518
PM
141 unsigned int i;
142
cfe4a9dd 143 tbl = rht_dereference_rcu(priv->tbl, priv);
ce6eb0d7 144 for (i = 0; i < tbl->size; i++) {
cfe4a9dd 145 rht_for_each_entry_rcu(he, tbl->buckets[i], node) {
20a69341
PM
146 if (iter->count < iter->skip)
147 goto cont;
148
149 memcpy(&elem.key, &he->key, sizeof(elem.key));
150 if (set->flags & NFT_SET_MAP)
151 memcpy(&elem.data, he->data, sizeof(elem.data));
152 elem.flags = 0;
153
154 iter->err = iter->fn(ctx, set, iter, &elem);
155 if (iter->err < 0)
156 return;
157cont:
158 iter->count++;
96518518
PM
159 }
160 }
96518518
PM
161}
162
20a69341
PM
163static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
164{
cfe4a9dd
TG
165 return sizeof(struct rhashtable);
166}
167
1f501d62 168#ifdef CONFIG_PROVE_LOCKING
7b4ce235 169static int lockdep_nfnl_lock_is_held(void *parent)
cfe4a9dd
TG
170{
171 return lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES);
20a69341 172}
1f501d62 173#endif
96518518 174
20a69341 175static int nft_hash_init(const struct nft_set *set,
c50b960c 176 const struct nft_set_desc *desc,
96518518
PM
177 const struct nlattr * const tb[])
178{
cfe4a9dd
TG
179 struct rhashtable *priv = nft_set_priv(set);
180 struct rhashtable_params params = {
181 .nelem_hint = desc->size ? : NFT_HASH_ELEMENT_HINT,
182 .head_offset = offsetof(struct nft_hash_elem, node),
183 .key_offset = offsetof(struct nft_hash_elem, key),
184 .key_len = set->klen,
185 .hashfn = jhash,
186 .grow_decision = rht_grow_above_75,
187 .shrink_decision = rht_shrink_below_30,
1f501d62 188#ifdef CONFIG_PROVE_LOCKING
cfe4a9dd 189 .mutex_is_held = lockdep_nfnl_lock_is_held,
1f501d62 190#endif
cfe4a9dd 191 };
96518518 192
cfe4a9dd 193 return rhashtable_init(priv, &params);
96518518
PM
194}
195
20a69341 196static void nft_hash_destroy(const struct nft_set *set)
96518518 197{
cfe4a9dd 198 const struct rhashtable *priv = nft_set_priv(set);
39f39016 199 const struct bucket_table *tbl = priv->tbl;
ce6eb0d7 200 struct nft_hash_elem *he, *next;
96518518
PM
201 unsigned int i;
202
39f39016
PNA
203 for (i = 0; i < tbl->size; i++) {
204 for (he = rht_entry(tbl->buckets[i], struct nft_hash_elem, node);
205 he != NULL; he = next) {
206 next = rht_entry(he->node.next, struct nft_hash_elem, node);
ce6eb0d7 207 nft_hash_elem_destroy(set, he);
39f39016
PNA
208 }
209 }
cfe4a9dd 210 rhashtable_destroy(priv);
96518518
PM
211}
212
c50b960c
PM
213static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
214 struct nft_set_estimate *est)
215{
216 unsigned int esize;
217
218 esize = sizeof(struct nft_hash_elem);
219 if (features & NFT_SET_MAP)
220 esize += FIELD_SIZEOF(struct nft_hash_elem, data[0]);
221
222 if (desc->size) {
cfe4a9dd
TG
223 est->size = sizeof(struct rhashtable) +
224 roundup_pow_of_two(desc->size * 4 / 3) *
c50b960c
PM
225 sizeof(struct nft_hash_elem *) +
226 desc->size * esize;
227 } else {
228 /* Resizing happens when the load drops below 30% or goes
229 * above 75%. The average of 52.5% load (approximated by 50%)
230 * is used for the size estimation of the hash buckets,
231 * meaning we calculate two buckets per element.
232 */
233 est->size = esize + 2 * sizeof(struct nft_hash_elem *);
234 }
235
236 est->class = NFT_SET_CLASS_O_1;
237
238 return true;
239}
240
20a69341
PM
241static struct nft_set_ops nft_hash_ops __read_mostly = {
242 .privsize = nft_hash_privsize,
c50b960c 243 .estimate = nft_hash_estimate,
96518518
PM
244 .init = nft_hash_init,
245 .destroy = nft_hash_destroy,
20a69341
PM
246 .get = nft_hash_get,
247 .insert = nft_hash_insert,
248 .remove = nft_hash_remove,
249 .lookup = nft_hash_lookup,
250 .walk = nft_hash_walk,
251 .features = NFT_SET_MAP,
252 .owner = THIS_MODULE,
96518518
PM
253};
254
255static int __init nft_hash_module_init(void)
256{
20a69341 257 return nft_register_set(&nft_hash_ops);
96518518
PM
258}
259
260static void __exit nft_hash_module_exit(void)
261{
20a69341 262 nft_unregister_set(&nft_hash_ops);
96518518
PM
263}
264
265module_init(nft_hash_module_init);
266module_exit(nft_hash_module_exit);
267
268MODULE_LICENSE("GPL");
269MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
20a69341 270MODULE_ALIAS_NFT_SET();
This page took 0.095158 seconds and 5 git commands to generate.