]>
Commit | Line | Data |
---|---|---|
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 | 25 | |
745f5450 PM |
26 | struct nft_hash { |
27 | struct rhashtable ht; | |
28 | }; | |
29 | ||
96518518 | 30 | struct nft_hash_elem { |
cfe4a9dd | 31 | struct rhash_head node; |
fe2811eb | 32 | struct nft_set_ext ext; |
96518518 PM |
33 | }; |
34 | ||
bfd6e327 PM |
35 | struct nft_hash_cmp_arg { |
36 | const struct nft_set *set; | |
37 | const struct nft_data *key; | |
38 | }; | |
39 | ||
fa377321 HX |
40 | static const struct rhashtable_params nft_hash_params; |
41 | ||
bfd6e327 PM |
42 | static inline u32 nft_hash_key(const void *data, u32 len, u32 seed) |
43 | { | |
44 | const struct nft_hash_cmp_arg *arg = data; | |
45 | ||
46 | return jhash(arg->key, len, seed); | |
47 | } | |
48 | ||
49 | static inline u32 nft_hash_obj(const void *data, u32 len, u32 seed) | |
50 | { | |
51 | const struct nft_hash_elem *he = data; | |
52 | ||
fe2811eb | 53 | return jhash(nft_set_ext_key(&he->ext), len, seed); |
bfd6e327 PM |
54 | } |
55 | ||
56 | static inline int nft_hash_cmp(struct rhashtable_compare_arg *arg, | |
57 | const void *ptr) | |
58 | { | |
59 | const struct nft_hash_cmp_arg *x = arg->key; | |
60 | const struct nft_hash_elem *he = ptr; | |
61 | ||
fe2811eb | 62 | if (nft_data_cmp(nft_set_ext_key(&he->ext), x->key, x->set->klen)) |
bfd6e327 PM |
63 | return 1; |
64 | return 0; | |
65 | } | |
66 | ||
20a69341 PM |
67 | static bool nft_hash_lookup(const struct nft_set *set, |
68 | const struct nft_data *key, | |
69 | struct nft_data *data) | |
96518518 | 70 | { |
745f5450 | 71 | struct nft_hash *priv = nft_set_priv(set); |
20a69341 | 72 | const struct nft_hash_elem *he; |
bfd6e327 PM |
73 | struct nft_hash_cmp_arg arg = { |
74 | .set = set, | |
75 | .key = key, | |
76 | }; | |
ce6eb0d7 | 77 | |
bfd6e327 | 78 | he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params); |
cfe4a9dd | 79 | if (he && set->flags & NFT_SET_MAP) |
fe2811eb | 80 | nft_data_copy(data, nft_set_ext_data(&he->ext)); |
ce6eb0d7 | 81 | |
cfe4a9dd | 82 | return !!he; |
96518518 PM |
83 | } |
84 | ||
20a69341 PM |
85 | static int nft_hash_insert(const struct nft_set *set, |
86 | const struct nft_set_elem *elem) | |
96518518 | 87 | { |
745f5450 | 88 | struct nft_hash *priv = nft_set_priv(set); |
fe2811eb | 89 | struct nft_hash_elem *he = elem->priv; |
bfd6e327 PM |
90 | struct nft_hash_cmp_arg arg = { |
91 | .set = set, | |
92 | .key = &elem->key, | |
93 | }; | |
ce6eb0d7 | 94 | |
fe2811eb PM |
95 | return rhashtable_lookup_insert_key(&priv->ht, &arg, &he->node, |
96 | nft_hash_params); | |
96518518 PM |
97 | } |
98 | ||
ce6eb0d7 PM |
99 | static void nft_hash_elem_destroy(const struct nft_set *set, |
100 | struct nft_hash_elem *he) | |
101 | { | |
fe2811eb | 102 | nft_data_uninit(nft_set_ext_key(&he->ext), NFT_DATA_VALUE); |
ce6eb0d7 | 103 | if (set->flags & NFT_SET_MAP) |
fe2811eb | 104 | nft_data_uninit(nft_set_ext_data(&he->ext), set->dtype); |
ce6eb0d7 PM |
105 | kfree(he); |
106 | } | |
107 | ||
20a69341 PM |
108 | static void nft_hash_remove(const struct nft_set *set, |
109 | const struct nft_set_elem *elem) | |
96518518 | 110 | { |
745f5450 | 111 | struct nft_hash *priv = nft_set_priv(set); |
ce6eb0d7 | 112 | |
745f5450 | 113 | rhashtable_remove_fast(&priv->ht, elem->cookie, nft_hash_params); |
ce6eb0d7 | 114 | synchronize_rcu(); |
897362e4 | 115 | kfree(elem->cookie); |
20a69341 | 116 | } |
96518518 | 117 | |
20a69341 PM |
118 | static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem) |
119 | { | |
745f5450 | 120 | struct nft_hash *priv = nft_set_priv(set); |
fa377321 | 121 | struct nft_hash_elem *he; |
bfd6e327 PM |
122 | struct nft_hash_cmp_arg arg = { |
123 | .set = set, | |
124 | .key = &elem->key, | |
125 | }; | |
fa377321 | 126 | |
bfd6e327 | 127 | he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params); |
fa377321 HX |
128 | if (!he) |
129 | return -ENOENT; | |
8d24c0b4 | 130 | |
fe2811eb | 131 | elem->priv = he; |
8d24c0b4 | 132 | |
fa377321 | 133 | return 0; |
96518518 PM |
134 | } |
135 | ||
20a69341 PM |
136 | static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set, |
137 | struct nft_set_iter *iter) | |
96518518 | 138 | { |
745f5450 | 139 | struct nft_hash *priv = nft_set_priv(set); |
fe2811eb | 140 | struct nft_hash_elem *he; |
9a776628 | 141 | struct rhashtable_iter hti; |
20a69341 | 142 | struct nft_set_elem elem; |
9a776628 | 143 | int err; |
96518518 | 144 | |
745f5450 | 145 | err = rhashtable_walk_init(&priv->ht, &hti); |
9a776628 HX |
146 | iter->err = err; |
147 | if (err) | |
148 | return; | |
88d6ed15 | 149 | |
9a776628 HX |
150 | err = rhashtable_walk_start(&hti); |
151 | if (err && err != -EAGAIN) { | |
152 | iter->err = err; | |
153 | goto out; | |
154 | } | |
155 | ||
156 | while ((he = rhashtable_walk_next(&hti))) { | |
157 | if (IS_ERR(he)) { | |
158 | err = PTR_ERR(he); | |
159 | if (err != -EAGAIN) { | |
160 | iter->err = err; | |
161 | goto out; | |
162 | } | |
d8bdff59 HX |
163 | |
164 | continue; | |
9a776628 HX |
165 | } |
166 | ||
167 | if (iter->count < iter->skip) | |
168 | goto cont; | |
20a69341 | 169 | |
fe2811eb | 170 | elem.priv = he; |
9a776628 HX |
171 | |
172 | iter->err = iter->fn(ctx, set, iter, &elem); | |
173 | if (iter->err < 0) | |
174 | goto out; | |
20a69341 | 175 | |
20a69341 | 176 | cont: |
9a776628 | 177 | iter->count++; |
96518518 | 178 | } |
9a776628 HX |
179 | |
180 | out: | |
181 | rhashtable_walk_stop(&hti); | |
182 | rhashtable_walk_exit(&hti); | |
96518518 PM |
183 | } |
184 | ||
20a69341 PM |
185 | static unsigned int nft_hash_privsize(const struct nlattr * const nla[]) |
186 | { | |
745f5450 | 187 | return sizeof(struct nft_hash); |
cfe4a9dd TG |
188 | } |
189 | ||
fa377321 | 190 | static const struct rhashtable_params nft_hash_params = { |
45d84751 | 191 | .head_offset = offsetof(struct nft_hash_elem, node), |
bfd6e327 PM |
192 | .hashfn = nft_hash_key, |
193 | .obj_hashfn = nft_hash_obj, | |
194 | .obj_cmpfn = nft_hash_cmp, | |
45d84751 | 195 | .automatic_shrinking = true, |
fa377321 HX |
196 | }; |
197 | ||
20a69341 | 198 | static int nft_hash_init(const struct nft_set *set, |
c50b960c | 199 | const struct nft_set_desc *desc, |
96518518 PM |
200 | const struct nlattr * const tb[]) |
201 | { | |
745f5450 | 202 | struct nft_hash *priv = nft_set_priv(set); |
fa377321 HX |
203 | struct rhashtable_params params = nft_hash_params; |
204 | ||
205 | params.nelem_hint = desc->size ?: NFT_HASH_ELEMENT_HINT; | |
45d84751 | 206 | params.key_len = set->klen; |
96518518 | 207 | |
745f5450 | 208 | return rhashtable_init(&priv->ht, ¶ms); |
96518518 PM |
209 | } |
210 | ||
6b6f302c | 211 | static void nft_free_element(void *ptr, void *arg) |
96518518 | 212 | { |
6b6f302c TG |
213 | nft_hash_elem_destroy((const struct nft_set *)arg, ptr); |
214 | } | |
97defe1e | 215 | |
6b6f302c TG |
216 | static void nft_hash_destroy(const struct nft_set *set) |
217 | { | |
745f5450 PM |
218 | struct nft_hash *priv = nft_set_priv(set); |
219 | ||
220 | rhashtable_free_and_destroy(&priv->ht, nft_free_element, (void *)set); | |
96518518 PM |
221 | } |
222 | ||
c50b960c PM |
223 | static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, |
224 | struct nft_set_estimate *est) | |
225 | { | |
226 | unsigned int esize; | |
227 | ||
228 | esize = sizeof(struct nft_hash_elem); | |
c50b960c | 229 | if (desc->size) { |
745f5450 | 230 | est->size = sizeof(struct nft_hash) + |
cfe4a9dd | 231 | roundup_pow_of_two(desc->size * 4 / 3) * |
c50b960c PM |
232 | sizeof(struct nft_hash_elem *) + |
233 | desc->size * esize; | |
234 | } else { | |
235 | /* Resizing happens when the load drops below 30% or goes | |
236 | * above 75%. The average of 52.5% load (approximated by 50%) | |
237 | * is used for the size estimation of the hash buckets, | |
238 | * meaning we calculate two buckets per element. | |
239 | */ | |
240 | est->size = esize + 2 * sizeof(struct nft_hash_elem *); | |
241 | } | |
242 | ||
243 | est->class = NFT_SET_CLASS_O_1; | |
244 | ||
245 | return true; | |
246 | } | |
247 | ||
20a69341 PM |
248 | static struct nft_set_ops nft_hash_ops __read_mostly = { |
249 | .privsize = nft_hash_privsize, | |
fe2811eb | 250 | .elemsize = offsetof(struct nft_hash_elem, ext), |
c50b960c | 251 | .estimate = nft_hash_estimate, |
96518518 PM |
252 | .init = nft_hash_init, |
253 | .destroy = nft_hash_destroy, | |
20a69341 PM |
254 | .get = nft_hash_get, |
255 | .insert = nft_hash_insert, | |
256 | .remove = nft_hash_remove, | |
257 | .lookup = nft_hash_lookup, | |
258 | .walk = nft_hash_walk, | |
259 | .features = NFT_SET_MAP, | |
260 | .owner = THIS_MODULE, | |
96518518 PM |
261 | }; |
262 | ||
263 | static int __init nft_hash_module_init(void) | |
264 | { | |
20a69341 | 265 | return nft_register_set(&nft_hash_ops); |
96518518 PM |
266 | } |
267 | ||
268 | static void __exit nft_hash_module_exit(void) | |
269 | { | |
20a69341 | 270 | nft_unregister_set(&nft_hash_ops); |
96518518 PM |
271 | } |
272 | ||
273 | module_init(nft_hash_module_init); | |
274 | module_exit(nft_hash_module_exit); | |
275 | ||
276 | MODULE_LICENSE("GPL"); | |
277 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
20a69341 | 278 | MODULE_ALIAS_NFT_SET(); |