blob: c82df0a48fcd8a649921b3fcb6b5d1edd39c6295 [file] [log] [blame]
Patrick McHardy96518512013-10-14 11:00:02 +02001/*
Patrick McHardyce6eb0d2014-03-04 16:21:51 +01002 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
Patrick McHardy96518512013-10-14 11:00:02 +02003 *
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>
Patrick McHardyc50b9602014-03-28 10:19:47 +000015#include <linux/log2.h>
Patrick McHardy96518512013-10-14 11:00:02 +020016#include <linux/jhash.h>
17#include <linux/netlink.h>
Thomas Grafcfe4a9d2014-08-02 11:47:46 +020018#include <linux/rhashtable.h>
Patrick McHardy96518512013-10-14 11:00:02 +020019#include <linux/netfilter.h>
20#include <linux/netfilter/nf_tables.h>
21#include <net/netfilter/nf_tables.h>
22
Thomas Grafcfe4a9d2014-08-02 11:47:46 +020023/* We target a hash table size of 4, element hint is 75% of final size */
24#define NFT_HASH_ELEMENT_HINT 3
Patrick McHardy96518512013-10-14 11:00:02 +020025
26struct nft_hash_elem {
Thomas Grafcfe4a9d2014-08-02 11:47:46 +020027 struct rhash_head node;
Patrick McHardyce6eb0d2014-03-04 16:21:51 +010028 struct nft_data key;
29 struct nft_data data[];
Patrick McHardy96518512013-10-14 11:00:02 +020030};
31
Patrick McHardy20a69342013-10-11 12:06:22 +020032static bool nft_hash_lookup(const struct nft_set *set,
33 const struct nft_data *key,
34 struct nft_data *data)
Patrick McHardy96518512013-10-14 11:00:02 +020035{
Thomas Graf97defe12015-01-02 23:00:20 +010036 struct rhashtable *priv = nft_set_priv(set);
Patrick McHardy20a69342013-10-11 12:06:22 +020037 const struct nft_hash_elem *he;
Patrick McHardy96518512013-10-14 11:00:02 +020038
Thomas Grafcfe4a9d2014-08-02 11:47:46 +020039 he = rhashtable_lookup(priv, key);
40 if (he && set->flags & NFT_SET_MAP)
41 nft_data_copy(data, he->data);
Patrick McHardy96518512013-10-14 11:00:02 +020042
Thomas Grafcfe4a9d2014-08-02 11:47:46 +020043 return !!he;
Patrick McHardy96518512013-10-14 11:00:02 +020044}
45
Patrick McHardy20a69342013-10-11 12:06:22 +020046static int nft_hash_insert(const struct nft_set *set,
47 const struct nft_set_elem *elem)
Patrick McHardy96518512013-10-14 11:00:02 +020048{
Thomas Grafcfe4a9d2014-08-02 11:47:46 +020049 struct rhashtable *priv = nft_set_priv(set);
Patrick McHardy20a69342013-10-11 12:06:22 +020050 struct nft_hash_elem *he;
Thomas Grafcfe4a9d2014-08-02 11:47:46 +020051 unsigned int size;
Patrick McHardy96518512013-10-14 11:00:02 +020052
Patrick McHardy20a69342013-10-11 12:06:22 +020053 if (elem->flags != 0)
Patrick McHardy96518512013-10-14 11:00:02 +020054 return -EINVAL;
Patrick McHardy96518512013-10-14 11:00:02 +020055
Patrick McHardy20a69342013-10-11 12:06:22 +020056 size = sizeof(*he);
57 if (set->flags & NFT_SET_MAP)
58 size += sizeof(he->data[0]);
Patrick McHardy96518512013-10-14 11:00:02 +020059
Patrick McHardy20a69342013-10-11 12:06:22 +020060 he = kzalloc(size, GFP_KERNEL);
61 if (he == NULL)
Patrick McHardy96518512013-10-14 11:00:02 +020062 return -ENOMEM;
63
Patrick McHardy20a69342013-10-11 12:06:22 +020064 nft_data_copy(&he->key, &elem->key);
65 if (set->flags & NFT_SET_MAP)
66 nft_data_copy(he->data, &elem->data);
Patrick McHardy96518512013-10-14 11:00:02 +020067
Thomas Graf6eba8222014-11-13 13:45:46 +010068 rhashtable_insert(priv, &he->node);
Patrick McHardyce6eb0d2014-03-04 16:21:51 +010069
Patrick McHardy96518512013-10-14 11:00:02 +020070 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +020071}
72
Patrick McHardyce6eb0d2014-03-04 16:21:51 +010073static 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
Patrick McHardy20a69342013-10-11 12:06:22 +020082static void nft_hash_remove(const struct nft_set *set,
83 const struct nft_set_elem *elem)
Patrick McHardy96518512013-10-14 11:00:02 +020084{
Thomas Grafcfe4a9d2014-08-02 11:47:46 +020085 struct rhashtable *priv = nft_set_priv(set);
Patrick McHardy96518512013-10-14 11:00:02 +020086
Thomas Graf897362e2015-01-02 23:00:18 +010087 rhashtable_remove(priv, elem->cookie);
Patrick McHardyce6eb0d2014-03-04 16:21:51 +010088 synchronize_rcu();
Thomas Graf897362e2015-01-02 23:00:18 +010089 kfree(elem->cookie);
Patrick McHardy96518512013-10-14 11:00:02 +020090}
91
Thomas Graf8d24c0b2015-01-02 23:00:14 +010092struct nft_compare_arg {
93 const struct nft_set *set;
94 struct nft_set_elem *elem;
95};
96
97static bool nft_hash_compare(void *ptr, void *arg)
98{
99 struct nft_hash_elem *he = ptr;
100 struct nft_compare_arg *x = arg;
101
102 if (!nft_data_cmp(&he->key, &x->elem->key, x->set->klen)) {
Thomas Graf897362e2015-01-02 23:00:18 +0100103 x->elem->cookie = he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100104 x->elem->flags = 0;
105 if (x->set->flags & NFT_SET_MAP)
106 nft_data_copy(&x->elem->data, he->data);
107
108 return true;
109 }
110
111 return false;
112}
113
Patrick McHardy20a69342013-10-11 12:06:22 +0200114static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem)
Patrick McHardy96518512013-10-14 11:00:02 +0200115{
Thomas Graf97defe12015-01-02 23:00:20 +0100116 struct rhashtable *priv = nft_set_priv(set);
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100117 struct nft_compare_arg arg = {
118 .set = set,
119 .elem = elem,
120 };
Patrick McHardy20a69342013-10-11 12:06:22 +0200121
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100122 if (rhashtable_lookup_compare(priv, &elem->key,
123 &nft_hash_compare, &arg))
Patrick McHardy20a69342013-10-11 12:06:22 +0200124 return 0;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100125
Patrick McHardy20a69342013-10-11 12:06:22 +0200126 return -ENOENT;
127}
128
129static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
130 struct nft_set_iter *iter)
131{
Thomas Graf97defe12015-01-02 23:00:20 +0100132 struct rhashtable *priv = nft_set_priv(set);
Patrick McHardy20a69342013-10-11 12:06:22 +0200133 const struct nft_hash_elem *he;
Herbert Xu9a776622015-02-04 07:33:25 +1100134 struct rhashtable_iter hti;
Patrick McHardy20a69342013-10-11 12:06:22 +0200135 struct nft_set_elem elem;
Herbert Xu9a776622015-02-04 07:33:25 +1100136 int err;
Patrick McHardy96518512013-10-14 11:00:02 +0200137
Herbert Xu9a776622015-02-04 07:33:25 +1100138 err = rhashtable_walk_init(priv, &hti);
139 iter->err = err;
140 if (err)
141 return;
Thomas Graf88d6ed12015-01-02 23:00:16 +0100142
Herbert Xu9a776622015-02-04 07:33:25 +1100143 err = rhashtable_walk_start(&hti);
144 if (err && err != -EAGAIN) {
145 iter->err = err;
146 goto out;
Patrick McHardy96518512013-10-14 11:00:02 +0200147 }
Herbert Xu9a776622015-02-04 07:33:25 +1100148
149 while ((he = rhashtable_walk_next(&hti))) {
150 if (IS_ERR(he)) {
151 err = PTR_ERR(he);
152 if (err != -EAGAIN) {
153 iter->err = err;
154 goto out;
155 }
156 }
157
158 if (iter->count < iter->skip)
159 goto cont;
160
161 memcpy(&elem.key, &he->key, sizeof(elem.key));
162 if (set->flags & NFT_SET_MAP)
163 memcpy(&elem.data, he->data, sizeof(elem.data));
164 elem.flags = 0;
165
166 iter->err = iter->fn(ctx, set, iter, &elem);
167 if (iter->err < 0)
168 goto out;
169
170cont:
171 iter->count++;
172 }
173
174out:
175 rhashtable_walk_stop(&hti);
176 rhashtable_walk_exit(&hti);
Patrick McHardy96518512013-10-14 11:00:02 +0200177}
178
Patrick McHardy20a69342013-10-11 12:06:22 +0200179static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
180{
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200181 return sizeof(struct rhashtable);
182}
183
Patrick McHardy20a69342013-10-11 12:06:22 +0200184static int nft_hash_init(const struct nft_set *set,
Patrick McHardyc50b9602014-03-28 10:19:47 +0000185 const struct nft_set_desc *desc,
Patrick McHardy96518512013-10-14 11:00:02 +0200186 const struct nlattr * const tb[])
187{
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200188 struct rhashtable *priv = nft_set_priv(set);
189 struct rhashtable_params params = {
190 .nelem_hint = desc->size ? : NFT_HASH_ELEMENT_HINT,
191 .head_offset = offsetof(struct nft_hash_elem, node),
192 .key_offset = offsetof(struct nft_hash_elem, key),
193 .key_len = set->klen,
194 .hashfn = jhash,
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200195 };
Patrick McHardy96518512013-10-14 11:00:02 +0200196
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200197 return rhashtable_init(priv, &params);
Patrick McHardy96518512013-10-14 11:00:02 +0200198}
199
Patrick McHardy20a69342013-10-11 12:06:22 +0200200static void nft_hash_destroy(const struct nft_set *set)
Patrick McHardy96518512013-10-14 11:00:02 +0200201{
Thomas Graf97defe12015-01-02 23:00:20 +0100202 struct rhashtable *priv = nft_set_priv(set);
203 const struct bucket_table *tbl;
Thomas Graf88d6ed12015-01-02 23:00:16 +0100204 struct nft_hash_elem *he;
205 struct rhash_head *pos, *next;
Patrick McHardy96518512013-10-14 11:00:02 +0200206 unsigned int i;
207
Thomas Graf97defe12015-01-02 23:00:20 +0100208 /* Stop an eventual async resizing */
209 priv->being_destroyed = true;
210 mutex_lock(&priv->mutex);
211
212 tbl = rht_dereference(priv->tbl, priv);
Pablo Neira Ayuso39f39012014-09-01 11:09:35 +0200213 for (i = 0; i < tbl->size; i++) {
Thomas Graf88d6ed12015-01-02 23:00:16 +0100214 rht_for_each_entry_safe(he, pos, next, tbl, i, node)
Patrick McHardyce6eb0d2014-03-04 16:21:51 +0100215 nft_hash_elem_destroy(set, he);
Pablo Neira Ayuso39f39012014-09-01 11:09:35 +0200216 }
Thomas Graf97defe12015-01-02 23:00:20 +0100217 mutex_unlock(&priv->mutex);
218
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200219 rhashtable_destroy(priv);
Patrick McHardy96518512013-10-14 11:00:02 +0200220}
221
Patrick McHardyc50b9602014-03-28 10:19:47 +0000222static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
223 struct nft_set_estimate *est)
224{
225 unsigned int esize;
226
227 esize = sizeof(struct nft_hash_elem);
228 if (features & NFT_SET_MAP)
229 esize += FIELD_SIZEOF(struct nft_hash_elem, data[0]);
230
231 if (desc->size) {
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200232 est->size = sizeof(struct rhashtable) +
233 roundup_pow_of_two(desc->size * 4 / 3) *
Patrick McHardyc50b9602014-03-28 10:19:47 +0000234 sizeof(struct nft_hash_elem *) +
235 desc->size * esize;
236 } else {
237 /* Resizing happens when the load drops below 30% or goes
238 * above 75%. The average of 52.5% load (approximated by 50%)
239 * is used for the size estimation of the hash buckets,
240 * meaning we calculate two buckets per element.
241 */
242 est->size = esize + 2 * sizeof(struct nft_hash_elem *);
243 }
244
245 est->class = NFT_SET_CLASS_O_1;
246
247 return true;
248}
249
Patrick McHardy20a69342013-10-11 12:06:22 +0200250static struct nft_set_ops nft_hash_ops __read_mostly = {
251 .privsize = nft_hash_privsize,
Patrick McHardyc50b9602014-03-28 10:19:47 +0000252 .estimate = nft_hash_estimate,
Patrick McHardy96518512013-10-14 11:00:02 +0200253 .init = nft_hash_init,
254 .destroy = nft_hash_destroy,
Patrick McHardy20a69342013-10-11 12:06:22 +0200255 .get = nft_hash_get,
256 .insert = nft_hash_insert,
257 .remove = nft_hash_remove,
258 .lookup = nft_hash_lookup,
259 .walk = nft_hash_walk,
260 .features = NFT_SET_MAP,
261 .owner = THIS_MODULE,
Patrick McHardy96518512013-10-14 11:00:02 +0200262};
263
264static int __init nft_hash_module_init(void)
265{
Patrick McHardy20a69342013-10-11 12:06:22 +0200266 return nft_register_set(&nft_hash_ops);
Patrick McHardy96518512013-10-14 11:00:02 +0200267}
268
269static void __exit nft_hash_module_exit(void)
270{
Patrick McHardy20a69342013-10-11 12:06:22 +0200271 nft_unregister_set(&nft_hash_ops);
Patrick McHardy96518512013-10-14 11:00:02 +0200272}
273
274module_init(nft_hash_module_init);
275module_exit(nft_hash_module_exit);
276
277MODULE_LICENSE("GPL");
278MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
Patrick McHardy20a69342013-10-11 12:06:22 +0200279MODULE_ALIAS_NFT_SET();