]>
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 PM |
25 | |
26 | struct 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 |
32 | static 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 |
46 | static 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 | |
cfe4a9dd | 68 | rhashtable_insert(priv, &he->node, GFP_KERNEL); |
ce6eb0d7 | 69 | |
96518518 | 70 | return 0; |
96518518 PM |
71 | } |
72 | ||
ce6eb0d7 PM |
73 | static 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 |
82 | static 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 | ||
91 | rhashtable_remove_pprev(priv, he, pprev, GFP_KERNEL); | |
ce6eb0d7 | 92 | |
ce6eb0d7 | 93 | synchronize_rcu(); |
20a69341 PM |
94 | kfree(he); |
95 | } | |
96518518 | 96 | |
20a69341 PM |
97 | static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem) |
98 | { | |
cfe4a9dd TG |
99 | const struct rhashtable *priv = nft_set_priv(set); |
100 | const struct bucket_table *tbl = rht_dereference_rcu(priv->tbl, priv); | |
101 | struct rhash_head __rcu * const *pprev; | |
20a69341 | 102 | struct nft_hash_elem *he; |
cfe4a9dd | 103 | u32 h; |
96518518 | 104 | |
cfe4a9dd | 105 | h = rhashtable_hashfn(priv, &elem->key, set->klen); |
ce6eb0d7 | 106 | pprev = &tbl->buckets[h]; |
cfe4a9dd | 107 | rht_for_each_entry_rcu(he, tbl->buckets[h], node) { |
ce6eb0d7 | 108 | if (nft_data_cmp(&he->key, &elem->key, set->klen)) { |
cfe4a9dd | 109 | pprev = &he->node.next; |
20a69341 | 110 | continue; |
ce6eb0d7 | 111 | } |
96518518 | 112 | |
ce6eb0d7 PM |
113 | elem->cookie = (void *)pprev; |
114 | elem->flags = 0; | |
20a69341 PM |
115 | if (set->flags & NFT_SET_MAP) |
116 | nft_data_copy(&elem->data, he->data); | |
117 | return 0; | |
118 | } | |
119 | return -ENOENT; | |
96518518 PM |
120 | } |
121 | ||
20a69341 PM |
122 | static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set, |
123 | struct nft_set_iter *iter) | |
96518518 | 124 | { |
cfe4a9dd TG |
125 | const struct rhashtable *priv = nft_set_priv(set); |
126 | const struct bucket_table *tbl; | |
20a69341 PM |
127 | const struct nft_hash_elem *he; |
128 | struct nft_set_elem elem; | |
96518518 PM |
129 | unsigned int i; |
130 | ||
cfe4a9dd | 131 | tbl = rht_dereference_rcu(priv->tbl, priv); |
ce6eb0d7 | 132 | for (i = 0; i < tbl->size; i++) { |
cfe4a9dd | 133 | rht_for_each_entry_rcu(he, tbl->buckets[i], node) { |
20a69341 PM |
134 | if (iter->count < iter->skip) |
135 | goto cont; | |
136 | ||
137 | memcpy(&elem.key, &he->key, sizeof(elem.key)); | |
138 | if (set->flags & NFT_SET_MAP) | |
139 | memcpy(&elem.data, he->data, sizeof(elem.data)); | |
140 | elem.flags = 0; | |
141 | ||
142 | iter->err = iter->fn(ctx, set, iter, &elem); | |
143 | if (iter->err < 0) | |
144 | return; | |
145 | cont: | |
146 | iter->count++; | |
96518518 PM |
147 | } |
148 | } | |
96518518 PM |
149 | } |
150 | ||
20a69341 PM |
151 | static unsigned int nft_hash_privsize(const struct nlattr * const nla[]) |
152 | { | |
cfe4a9dd TG |
153 | return sizeof(struct rhashtable); |
154 | } | |
155 | ||
156 | static int lockdep_nfnl_lock_is_held(void) | |
157 | { | |
158 | return lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES); | |
20a69341 | 159 | } |
96518518 | 160 | |
20a69341 | 161 | static int nft_hash_init(const struct nft_set *set, |
c50b960c | 162 | const struct nft_set_desc *desc, |
96518518 PM |
163 | const struct nlattr * const tb[]) |
164 | { | |
cfe4a9dd TG |
165 | struct rhashtable *priv = nft_set_priv(set); |
166 | struct rhashtable_params params = { | |
167 | .nelem_hint = desc->size ? : NFT_HASH_ELEMENT_HINT, | |
168 | .head_offset = offsetof(struct nft_hash_elem, node), | |
169 | .key_offset = offsetof(struct nft_hash_elem, key), | |
170 | .key_len = set->klen, | |
171 | .hashfn = jhash, | |
172 | .grow_decision = rht_grow_above_75, | |
173 | .shrink_decision = rht_shrink_below_30, | |
174 | .mutex_is_held = lockdep_nfnl_lock_is_held, | |
175 | }; | |
96518518 | 176 | |
cfe4a9dd | 177 | return rhashtable_init(priv, ¶ms); |
96518518 PM |
178 | } |
179 | ||
20a69341 | 180 | static void nft_hash_destroy(const struct nft_set *set) |
96518518 | 181 | { |
cfe4a9dd | 182 | const struct rhashtable *priv = nft_set_priv(set); |
39f39016 | 183 | const struct bucket_table *tbl = priv->tbl; |
ce6eb0d7 | 184 | struct nft_hash_elem *he, *next; |
96518518 PM |
185 | unsigned int i; |
186 | ||
39f39016 PNA |
187 | for (i = 0; i < tbl->size; i++) { |
188 | for (he = rht_entry(tbl->buckets[i], struct nft_hash_elem, node); | |
189 | he != NULL; he = next) { | |
190 | next = rht_entry(he->node.next, struct nft_hash_elem, node); | |
ce6eb0d7 | 191 | nft_hash_elem_destroy(set, he); |
39f39016 PNA |
192 | } |
193 | } | |
cfe4a9dd | 194 | rhashtable_destroy(priv); |
96518518 PM |
195 | } |
196 | ||
c50b960c PM |
197 | static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, |
198 | struct nft_set_estimate *est) | |
199 | { | |
200 | unsigned int esize; | |
201 | ||
202 | esize = sizeof(struct nft_hash_elem); | |
203 | if (features & NFT_SET_MAP) | |
204 | esize += FIELD_SIZEOF(struct nft_hash_elem, data[0]); | |
205 | ||
206 | if (desc->size) { | |
cfe4a9dd TG |
207 | est->size = sizeof(struct rhashtable) + |
208 | roundup_pow_of_two(desc->size * 4 / 3) * | |
c50b960c PM |
209 | sizeof(struct nft_hash_elem *) + |
210 | desc->size * esize; | |
211 | } else { | |
212 | /* Resizing happens when the load drops below 30% or goes | |
213 | * above 75%. The average of 52.5% load (approximated by 50%) | |
214 | * is used for the size estimation of the hash buckets, | |
215 | * meaning we calculate two buckets per element. | |
216 | */ | |
217 | est->size = esize + 2 * sizeof(struct nft_hash_elem *); | |
218 | } | |
219 | ||
220 | est->class = NFT_SET_CLASS_O_1; | |
221 | ||
222 | return true; | |
223 | } | |
224 | ||
20a69341 PM |
225 | static struct nft_set_ops nft_hash_ops __read_mostly = { |
226 | .privsize = nft_hash_privsize, | |
c50b960c | 227 | .estimate = nft_hash_estimate, |
96518518 PM |
228 | .init = nft_hash_init, |
229 | .destroy = nft_hash_destroy, | |
20a69341 PM |
230 | .get = nft_hash_get, |
231 | .insert = nft_hash_insert, | |
232 | .remove = nft_hash_remove, | |
233 | .lookup = nft_hash_lookup, | |
234 | .walk = nft_hash_walk, | |
235 | .features = NFT_SET_MAP, | |
236 | .owner = THIS_MODULE, | |
96518518 PM |
237 | }; |
238 | ||
239 | static int __init nft_hash_module_init(void) | |
240 | { | |
20a69341 | 241 | return nft_register_set(&nft_hash_ops); |
96518518 PM |
242 | } |
243 | ||
244 | static void __exit nft_hash_module_exit(void) | |
245 | { | |
20a69341 | 246 | nft_unregister_set(&nft_hash_ops); |
96518518 PM |
247 | } |
248 | ||
249 | module_init(nft_hash_module_init); | |
250 | module_exit(nft_hash_module_exit); | |
251 | ||
252 | MODULE_LICENSE("GPL"); | |
253 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
20a69341 | 254 | MODULE_ALIAS_NFT_SET(); |