blob: 28fb8f38e6ba7df25fc8120dd82080d817f638be [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 Grafcfe4a9d2014-08-02 11:47:46 +020036 const 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 Grafcfe4a9d2014-08-02 11:47:46 +020068 rhashtable_insert(priv, &he->node, GFP_KERNEL);
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);
86 struct rhash_head *he, __rcu **pprev;
Patrick McHardy96518512013-10-14 11:00:02 +020087
Patrick McHardyce6eb0d2014-03-04 16:21:51 +010088 pprev = elem->cookie;
Thomas Grafcfe4a9d2014-08-02 11:47:46 +020089 he = rht_dereference((*pprev), priv);
Patrick McHardyce6eb0d2014-03-04 16:21:51 +010090
Thomas Grafcfe4a9d2014-08-02 11:47:46 +020091 rhashtable_remove_pprev(priv, he, pprev, GFP_KERNEL);
92
Patrick McHardyce6eb0d2014-03-04 16:21:51 +010093 synchronize_rcu();
Patrick McHardy20a69342013-10-11 12:06:22 +020094 kfree(he);
Patrick McHardy96518512013-10-14 11:00:02 +020095}
96
Patrick McHardy20a69342013-10-11 12:06:22 +020097static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem)
Patrick McHardy96518512013-10-14 11:00:02 +020098{
Thomas Grafcfe4a9d2014-08-02 11:47:46 +020099 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;
Patrick McHardy20a69342013-10-11 12:06:22 +0200102 struct nft_hash_elem *he;
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200103 u32 h;
Patrick McHardy20a69342013-10-11 12:06:22 +0200104
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200105 h = rhashtable_hashfn(priv, &elem->key, set->klen);
Patrick McHardyce6eb0d2014-03-04 16:21:51 +0100106 pprev = &tbl->buckets[h];
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200107 rht_for_each_entry_rcu(he, tbl->buckets[h], node) {
Patrick McHardyce6eb0d2014-03-04 16:21:51 +0100108 if (nft_data_cmp(&he->key, &elem->key, set->klen)) {
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200109 pprev = &he->node.next;
Patrick McHardy20a69342013-10-11 12:06:22 +0200110 continue;
Patrick McHardyce6eb0d2014-03-04 16:21:51 +0100111 }
Patrick McHardy20a69342013-10-11 12:06:22 +0200112
Patrick McHardyce6eb0d2014-03-04 16:21:51 +0100113 elem->cookie = (void *)pprev;
114 elem->flags = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +0200115 if (set->flags & NFT_SET_MAP)
116 nft_data_copy(&elem->data, he->data);
117 return 0;
118 }
119 return -ENOENT;
120}
121
122static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
123 struct nft_set_iter *iter)
124{
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200125 const struct rhashtable *priv = nft_set_priv(set);
126 const struct bucket_table *tbl;
Patrick McHardy20a69342013-10-11 12:06:22 +0200127 const struct nft_hash_elem *he;
128 struct nft_set_elem elem;
Patrick McHardy96518512013-10-14 11:00:02 +0200129 unsigned int i;
130
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200131 tbl = rht_dereference_rcu(priv->tbl, priv);
Patrick McHardyce6eb0d2014-03-04 16:21:51 +0100132 for (i = 0; i < tbl->size; i++) {
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200133 rht_for_each_entry_rcu(he, tbl->buckets[i], node) {
Patrick McHardy20a69342013-10-11 12:06:22 +0200134 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;
145cont:
146 iter->count++;
Patrick McHardy96518512013-10-14 11:00:02 +0200147 }
148 }
Patrick McHardy96518512013-10-14 11:00:02 +0200149}
150
Patrick McHardy20a69342013-10-11 12:06:22 +0200151static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
152{
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200153 return sizeof(struct rhashtable);
154}
155
156static int lockdep_nfnl_lock_is_held(void)
157{
158 return lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES);
Patrick McHardy20a69342013-10-11 12:06:22 +0200159}
Patrick McHardy96518512013-10-14 11:00:02 +0200160
Patrick McHardy20a69342013-10-11 12:06:22 +0200161static int nft_hash_init(const struct nft_set *set,
Patrick McHardyc50b9602014-03-28 10:19:47 +0000162 const struct nft_set_desc *desc,
Patrick McHardy96518512013-10-14 11:00:02 +0200163 const struct nlattr * const tb[])
164{
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200165 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 };
Patrick McHardy96518512013-10-14 11:00:02 +0200176
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200177 return rhashtable_init(priv, &params);
Patrick McHardy96518512013-10-14 11:00:02 +0200178}
179
Patrick McHardy20a69342013-10-11 12:06:22 +0200180static void nft_hash_destroy(const struct nft_set *set)
Patrick McHardy96518512013-10-14 11:00:02 +0200181{
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200182 const struct rhashtable *priv = nft_set_priv(set);
183 const struct bucket_table *tbl;
Patrick McHardyce6eb0d2014-03-04 16:21:51 +0100184 struct nft_hash_elem *he, *next;
Patrick McHardy96518512013-10-14 11:00:02 +0200185 unsigned int i;
186
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200187 tbl = rht_dereference(priv->tbl, priv);
188 for (i = 0; i < tbl->size; i++)
189 rht_for_each_entry_safe(he, next, tbl->buckets[i], priv, node)
Patrick McHardyce6eb0d2014-03-04 16:21:51 +0100190 nft_hash_elem_destroy(set, he);
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200191
192 rhashtable_destroy(priv);
Patrick McHardy96518512013-10-14 11:00:02 +0200193}
194
Patrick McHardyc50b9602014-03-28 10:19:47 +0000195static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
196 struct nft_set_estimate *est)
197{
198 unsigned int esize;
199
200 esize = sizeof(struct nft_hash_elem);
201 if (features & NFT_SET_MAP)
202 esize += FIELD_SIZEOF(struct nft_hash_elem, data[0]);
203
204 if (desc->size) {
Thomas Grafcfe4a9d2014-08-02 11:47:46 +0200205 est->size = sizeof(struct rhashtable) +
206 roundup_pow_of_two(desc->size * 4 / 3) *
Patrick McHardyc50b9602014-03-28 10:19:47 +0000207 sizeof(struct nft_hash_elem *) +
208 desc->size * esize;
209 } else {
210 /* Resizing happens when the load drops below 30% or goes
211 * above 75%. The average of 52.5% load (approximated by 50%)
212 * is used for the size estimation of the hash buckets,
213 * meaning we calculate two buckets per element.
214 */
215 est->size = esize + 2 * sizeof(struct nft_hash_elem *);
216 }
217
218 est->class = NFT_SET_CLASS_O_1;
219
220 return true;
221}
222
Patrick McHardy20a69342013-10-11 12:06:22 +0200223static struct nft_set_ops nft_hash_ops __read_mostly = {
224 .privsize = nft_hash_privsize,
Patrick McHardyc50b9602014-03-28 10:19:47 +0000225 .estimate = nft_hash_estimate,
Patrick McHardy96518512013-10-14 11:00:02 +0200226 .init = nft_hash_init,
227 .destroy = nft_hash_destroy,
Patrick McHardy20a69342013-10-11 12:06:22 +0200228 .get = nft_hash_get,
229 .insert = nft_hash_insert,
230 .remove = nft_hash_remove,
231 .lookup = nft_hash_lookup,
232 .walk = nft_hash_walk,
233 .features = NFT_SET_MAP,
234 .owner = THIS_MODULE,
Patrick McHardy96518512013-10-14 11:00:02 +0200235};
236
237static int __init nft_hash_module_init(void)
238{
Patrick McHardy20a69342013-10-11 12:06:22 +0200239 return nft_register_set(&nft_hash_ops);
Patrick McHardy96518512013-10-14 11:00:02 +0200240}
241
242static void __exit nft_hash_module_exit(void)
243{
Patrick McHardy20a69342013-10-11 12:06:22 +0200244 nft_unregister_set(&nft_hash_ops);
Patrick McHardy96518512013-10-14 11:00:02 +0200245}
246
247module_init(nft_hash_module_init);
248module_exit(nft_hash_module_exit);
249
250MODULE_LICENSE("GPL");
251MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
Patrick McHardy20a69342013-10-11 12:06:22 +0200252MODULE_ALIAS_NFT_SET();