2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
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.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/list.h>
15 #include <linux/rbtree.h>
16 #include <linux/netlink.h>
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_tables.h>
21 static DEFINE_SPINLOCK(nft_rbtree_lock
);
27 struct nft_rbtree_elem
{
29 struct nft_set_ext ext
;
33 static bool nft_rbtree_lookup(const struct nft_set
*set
, const u32
*key
,
34 const struct nft_set_ext
**ext
)
36 const struct nft_rbtree
*priv
= nft_set_priv(set
);
37 const struct nft_rbtree_elem
*rbe
, *interval
= NULL
;
38 const struct rb_node
*parent
;
39 u8 genmask
= nft_genmask_cur(read_pnet(&set
->pnet
));
42 spin_lock_bh(&nft_rbtree_lock
);
43 parent
= priv
->root
.rb_node
;
44 while (parent
!= NULL
) {
45 rbe
= rb_entry(parent
, struct nft_rbtree_elem
, node
);
47 d
= memcmp(nft_set_ext_key(&rbe
->ext
), key
, set
->klen
);
49 parent
= parent
->rb_left
;
52 parent
= parent
->rb_right
;
55 if (!nft_set_elem_active(&rbe
->ext
, genmask
)) {
56 parent
= parent
->rb_left
;
59 if (nft_set_ext_exists(&rbe
->ext
, NFT_SET_EXT_FLAGS
) &&
60 *nft_set_ext_flags(&rbe
->ext
) &
61 NFT_SET_ELEM_INTERVAL_END
)
63 spin_unlock_bh(&nft_rbtree_lock
);
70 if (set
->flags
& NFT_SET_INTERVAL
&& interval
!= NULL
) {
75 spin_unlock_bh(&nft_rbtree_lock
);
79 static int __nft_rbtree_insert(const struct nft_set
*set
,
80 struct nft_rbtree_elem
*new)
82 struct nft_rbtree
*priv
= nft_set_priv(set
);
83 struct nft_rbtree_elem
*rbe
;
84 struct rb_node
*parent
, **p
;
85 u8 genmask
= nft_genmask_next(read_pnet(&set
->pnet
));
89 p
= &priv
->root
.rb_node
;
92 rbe
= rb_entry(parent
, struct nft_rbtree_elem
, node
);
93 d
= memcmp(nft_set_ext_key(&rbe
->ext
),
94 nft_set_ext_key(&new->ext
),
99 p
= &parent
->rb_right
;
101 if (nft_set_elem_active(&rbe
->ext
, genmask
))
103 p
= &parent
->rb_left
;
106 rb_link_node(&new->node
, parent
, p
);
107 rb_insert_color(&new->node
, &priv
->root
);
111 static int nft_rbtree_insert(const struct nft_set
*set
,
112 const struct nft_set_elem
*elem
)
114 struct nft_rbtree_elem
*rbe
= elem
->priv
;
117 spin_lock_bh(&nft_rbtree_lock
);
118 err
= __nft_rbtree_insert(set
, rbe
);
119 spin_unlock_bh(&nft_rbtree_lock
);
124 static void nft_rbtree_remove(const struct nft_set
*set
,
125 const struct nft_set_elem
*elem
)
127 struct nft_rbtree
*priv
= nft_set_priv(set
);
128 struct nft_rbtree_elem
*rbe
= elem
->priv
;
130 spin_lock_bh(&nft_rbtree_lock
);
131 rb_erase(&rbe
->node
, &priv
->root
);
132 spin_unlock_bh(&nft_rbtree_lock
);
135 static void nft_rbtree_activate(const struct nft_set
*set
,
136 const struct nft_set_elem
*elem
)
138 struct nft_rbtree_elem
*rbe
= elem
->priv
;
140 nft_set_elem_change_active(set
, &rbe
->ext
);
143 static void *nft_rbtree_deactivate(const struct nft_set
*set
,
144 const struct nft_set_elem
*elem
)
146 const struct nft_rbtree
*priv
= nft_set_priv(set
);
147 const struct rb_node
*parent
= priv
->root
.rb_node
;
148 struct nft_rbtree_elem
*rbe
;
149 u8 genmask
= nft_genmask_cur(read_pnet(&set
->pnet
));
152 while (parent
!= NULL
) {
153 rbe
= rb_entry(parent
, struct nft_rbtree_elem
, node
);
155 d
= memcmp(nft_set_ext_key(&rbe
->ext
), &elem
->key
.val
,
158 parent
= parent
->rb_left
;
160 parent
= parent
->rb_right
;
162 if (!nft_set_elem_active(&rbe
->ext
, genmask
)) {
163 parent
= parent
->rb_left
;
166 nft_set_elem_change_active(set
, &rbe
->ext
);
173 static void nft_rbtree_walk(const struct nft_ctx
*ctx
,
174 const struct nft_set
*set
,
175 struct nft_set_iter
*iter
)
177 const struct nft_rbtree
*priv
= nft_set_priv(set
);
178 struct nft_rbtree_elem
*rbe
;
179 struct nft_set_elem elem
;
180 struct rb_node
*node
;
181 u8 genmask
= nft_genmask_cur(read_pnet(&set
->pnet
));
183 spin_lock_bh(&nft_rbtree_lock
);
184 for (node
= rb_first(&priv
->root
); node
!= NULL
; node
= rb_next(node
)) {
185 rbe
= rb_entry(node
, struct nft_rbtree_elem
, node
);
187 if (iter
->count
< iter
->skip
)
189 if (!nft_set_elem_active(&rbe
->ext
, genmask
))
194 iter
->err
= iter
->fn(ctx
, set
, iter
, &elem
);
196 spin_unlock_bh(&nft_rbtree_lock
);
202 spin_unlock_bh(&nft_rbtree_lock
);
205 static unsigned int nft_rbtree_privsize(const struct nlattr
* const nla
[])
207 return sizeof(struct nft_rbtree
);
210 static int nft_rbtree_init(const struct nft_set
*set
,
211 const struct nft_set_desc
*desc
,
212 const struct nlattr
* const nla
[])
214 struct nft_rbtree
*priv
= nft_set_priv(set
);
216 priv
->root
= RB_ROOT
;
220 static void nft_rbtree_destroy(const struct nft_set
*set
)
222 struct nft_rbtree
*priv
= nft_set_priv(set
);
223 struct nft_rbtree_elem
*rbe
;
224 struct rb_node
*node
;
226 while ((node
= priv
->root
.rb_node
) != NULL
) {
227 rb_erase(node
, &priv
->root
);
228 rbe
= rb_entry(node
, struct nft_rbtree_elem
, node
);
229 nft_set_elem_destroy(set
, rbe
);
233 static bool nft_rbtree_estimate(const struct nft_set_desc
*desc
, u32 features
,
234 struct nft_set_estimate
*est
)
238 nsize
= sizeof(struct nft_rbtree_elem
);
240 est
->size
= sizeof(struct nft_rbtree
) + desc
->size
* nsize
;
244 est
->class = NFT_SET_CLASS_O_LOG_N
;
249 static struct nft_set_ops nft_rbtree_ops __read_mostly
= {
250 .privsize
= nft_rbtree_privsize
,
251 .elemsize
= offsetof(struct nft_rbtree_elem
, ext
),
252 .estimate
= nft_rbtree_estimate
,
253 .init
= nft_rbtree_init
,
254 .destroy
= nft_rbtree_destroy
,
255 .insert
= nft_rbtree_insert
,
256 .remove
= nft_rbtree_remove
,
257 .deactivate
= nft_rbtree_deactivate
,
258 .activate
= nft_rbtree_activate
,
259 .lookup
= nft_rbtree_lookup
,
260 .walk
= nft_rbtree_walk
,
261 .features
= NFT_SET_INTERVAL
| NFT_SET_MAP
,
262 .owner
= THIS_MODULE
,
265 static int __init
nft_rbtree_module_init(void)
267 return nft_register_set(&nft_rbtree_ops
);
270 static void __exit
nft_rbtree_module_exit(void)
272 nft_unregister_set(&nft_rbtree_ops
);
275 module_init(nft_rbtree_module_init
);
276 module_exit(nft_rbtree_module_exit
);
278 MODULE_LICENSE("GPL");
279 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
280 MODULE_ALIAS_NFT_SET();