aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Westphal <fw@strlen.de>2014-03-12 18:49:51 -0400
committerPablo Neira Ayuso <pablo@netfilter.org>2014-03-17 06:11:57 -0400
commit7d08487777c8b30dea34790734d708470faaf1e5 (patch)
tree143fc9e0ec00cab5fe98fe2dd89239e3c7386c03
parent50e0e9b12914dd82d1ece22d57bf8c146a1d1b52 (diff)
netfilter: connlimit: use rbtree for per-host conntrack obj storage
With current match design every invocation of the connlimit_match function means we have to perform (number_of_conntracks % 256) lookups in the conntrack table [ to perform GC/delete stale entries ]. This is also the reason why ____nf_conntrack_find() in perf top has > 20% cpu time per core. This patch changes the storage to rbtree which cuts down the number of ct objects that need testing. When looking up a new tuple, we only test the connections of the host objects we visit while searching for the wanted host/network (or the leaf we need to insert at). The slot count is reduced to 32. Increasing slot count doesn't speed up things much because of rbtree nature. before patch (50kpps rx, 10kpps tx): + 20.95% ksoftirqd/0 [nf_conntrack] [k] ____nf_conntrack_find + 20.50% ksoftirqd/1 [nf_conntrack] [k] ____nf_conntrack_find + 20.27% ksoftirqd/2 [nf_conntrack] [k] ____nf_conntrack_find + 5.76% ksoftirqd/1 [nf_conntrack] [k] hash_conntrack_raw + 5.39% ksoftirqd/2 [nf_conntrack] [k] hash_conntrack_raw + 5.35% ksoftirqd/0 [nf_conntrack] [k] hash_conntrack_raw after (90kpps, 51kpps tx): + 17.24% swapper [nf_conntrack] [k] ____nf_conntrack_find + 6.60% ksoftirqd/2 [nf_conntrack] [k] ____nf_conntrack_find + 2.73% swapper [nf_conntrack] [k] hash_conntrack_raw + 2.36% swapper [xt_connlimit] [k] count_tree Obvious disadvantages to previous version are the increase in code complexity and the increased memory cost. Partially based on Eric Dumazets fq scheduler. Reviewed-by: Jesper Dangaard Brouer <brouer@redhat.com> Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
-rw-r--r--net/netfilter/xt_connlimit.c224
1 files changed, 177 insertions, 47 deletions
diff --git a/net/netfilter/xt_connlimit.c b/net/netfilter/xt_connlimit.c
index dc5207f7a7fa..458464e7bd7a 100644
--- a/net/netfilter/xt_connlimit.c
+++ b/net/netfilter/xt_connlimit.c
@@ -19,6 +19,7 @@
19#include <linux/jhash.h> 19#include <linux/jhash.h>
20#include <linux/slab.h> 20#include <linux/slab.h>
21#include <linux/list.h> 21#include <linux/list.h>
22#include <linux/rbtree.h>
22#include <linux/module.h> 23#include <linux/module.h>
23#include <linux/random.h> 24#include <linux/random.h>
24#include <linux/skbuff.h> 25#include <linux/skbuff.h>
@@ -31,8 +32,9 @@
31#include <net/netfilter/nf_conntrack_tuple.h> 32#include <net/netfilter/nf_conntrack_tuple.h>
32#include <net/netfilter/nf_conntrack_zones.h> 33#include <net/netfilter/nf_conntrack_zones.h>
33 34
34#define CONNLIMIT_SLOTS 256 35#define CONNLIMIT_SLOTS 32
35#define CONNLIMIT_LOCK_SLOTS 32 36#define CONNLIMIT_LOCK_SLOTS 32
37#define CONNLIMIT_GC_MAX_NODES 8
36 38
37/* we will save the tuples of all connections we care about */ 39/* we will save the tuples of all connections we care about */
38struct xt_connlimit_conn { 40struct xt_connlimit_conn {
@@ -41,12 +43,20 @@ struct xt_connlimit_conn {
41 union nf_inet_addr addr; 43 union nf_inet_addr addr;
42}; 44};
43 45
46struct xt_connlimit_rb {
47 struct rb_node node;
48 struct hlist_head hhead; /* connections/hosts in same subnet */
49 union nf_inet_addr addr; /* search key */
50};
51
44struct xt_connlimit_data { 52struct xt_connlimit_data {
45 struct hlist_head iphash[CONNLIMIT_SLOTS]; 53 struct rb_root climit_root4[CONNLIMIT_SLOTS];
54 struct rb_root climit_root6[CONNLIMIT_SLOTS];
46 spinlock_t locks[CONNLIMIT_LOCK_SLOTS]; 55 spinlock_t locks[CONNLIMIT_LOCK_SLOTS];
47}; 56};
48 57
49static u_int32_t connlimit_rnd __read_mostly; 58static u_int32_t connlimit_rnd __read_mostly;
59static struct kmem_cache *connlimit_rb_cachep __read_mostly;
50static struct kmem_cache *connlimit_conn_cachep __read_mostly; 60static struct kmem_cache *connlimit_conn_cachep __read_mostly;
51 61
52static inline unsigned int connlimit_iphash(__be32 addr) 62static inline unsigned int connlimit_iphash(__be32 addr)
@@ -99,19 +109,33 @@ same_source_net(const union nf_inet_addr *addr,
99 } 109 }
100} 110}
101 111
102static int count_hlist(struct net *net, 112static bool add_hlist(struct hlist_head *head,
103 struct hlist_head *head, 113 const struct nf_conntrack_tuple *tuple,
104 const struct nf_conntrack_tuple *tuple, 114 const union nf_inet_addr *addr)
105 const union nf_inet_addr *addr, 115{
106 const union nf_inet_addr *mask, 116 struct xt_connlimit_conn *conn;
107 u_int8_t family, bool *addit) 117
118 conn = kmem_cache_alloc(connlimit_conn_cachep, GFP_ATOMIC);
119 if (conn == NULL)
120 return false;
121 conn->tuple = *tuple;
122 conn->addr = *addr;
123 hlist_add_head(&conn->node, head);
124 return true;
125}
126
127static unsigned int check_hlist(struct net *net,
128 struct hlist_head *head,
129 const struct nf_conntrack_tuple *tuple,
130 bool *addit)
108{ 131{
109 const struct nf_conntrack_tuple_hash *found; 132 const struct nf_conntrack_tuple_hash *found;
110 struct xt_connlimit_conn *conn; 133 struct xt_connlimit_conn *conn;
111 struct hlist_node *n; 134 struct hlist_node *n;
112 struct nf_conn *found_ct; 135 struct nf_conn *found_ct;
113 int matches = 0; 136 unsigned int length = 0;
114 137
138 *addit = true;
115 rcu_read_lock(); 139 rcu_read_lock();
116 140
117 /* check the saved connections */ 141 /* check the saved connections */
@@ -144,30 +168,114 @@ static int count_hlist(struct net *net,
144 continue; 168 continue;
145 } 169 }
146 170
147 if (same_source_net(addr, mask, &conn->addr, family) == 0)
148 /* same source network -> be counted! */
149 ++matches;
150 nf_ct_put(found_ct); 171 nf_ct_put(found_ct);
172 length++;
151 } 173 }
152 174
153 rcu_read_unlock(); 175 rcu_read_unlock();
154 176
155 return matches; 177 return length;
156} 178}
157 179
158static bool add_hlist(struct hlist_head *head, 180static void tree_nodes_free(struct rb_root *root,
159 const struct nf_conntrack_tuple *tuple, 181 struct xt_connlimit_rb *gc_nodes[],
160 const union nf_inet_addr *addr) 182 unsigned int gc_count)
183{
184 struct xt_connlimit_rb *rbconn;
185
186 while (gc_count) {
187 rbconn = gc_nodes[--gc_count];
188 rb_erase(&rbconn->node, root);
189 kmem_cache_free(connlimit_rb_cachep, rbconn);
190 }
191}
192
193static unsigned int
194count_tree(struct net *net, struct rb_root *root,
195 const struct nf_conntrack_tuple *tuple,
196 const union nf_inet_addr *addr, const union nf_inet_addr *mask,
197 u8 family)
161{ 198{
199 struct xt_connlimit_rb *gc_nodes[CONNLIMIT_GC_MAX_NODES];
200 struct rb_node **rbnode, *parent;
201 struct xt_connlimit_rb *rbconn;
162 struct xt_connlimit_conn *conn; 202 struct xt_connlimit_conn *conn;
203 unsigned int gc_count;
204 bool no_gc = false;
205
206 restart:
207 gc_count = 0;
208 parent = NULL;
209 rbnode = &(root->rb_node);
210 while (*rbnode) {
211 int diff;
212 bool addit;
213
214 rbconn = container_of(*rbnode, struct xt_connlimit_rb, node);
215
216 parent = *rbnode;
217 diff = same_source_net(addr, mask, &rbconn->addr, family);
218 if (diff < 0) {
219 rbnode = &((*rbnode)->rb_left);
220 } else if (diff > 0) {
221 rbnode = &((*rbnode)->rb_right);
222 } else {
223 /* same source network -> be counted! */
224 unsigned int count;
225 count = check_hlist(net, &rbconn->hhead, tuple, &addit);
226
227 tree_nodes_free(root, gc_nodes, gc_count);
228 if (!addit)
229 return count;
230
231 if (!add_hlist(&rbconn->hhead, tuple, addr))
232 return 0; /* hotdrop */
233
234 return count + 1;
235 }
236
237 if (no_gc || gc_count >= ARRAY_SIZE(gc_nodes))
238 continue;
239
240 /* only used for GC on hhead, retval and 'addit' ignored */
241 check_hlist(net, &rbconn->hhead, tuple, &addit);
242 if (hlist_empty(&rbconn->hhead))
243 gc_nodes[gc_count++] = rbconn;
244 }
245
246 if (gc_count) {
247 no_gc = true;
248 tree_nodes_free(root, gc_nodes, gc_