Skip to content

Commit 136e92b

Browse files
KOVACS Krisztiandavem330
authored andcommitted
[NETFILTER] CLUSTERIP: use a bitmap to store node responsibility data
Instead of maintaining an array containing a list of nodes this instance is responsible for let's use a simple bitmap. This provides the following features: * clusterip_responsible() and the add_node()/delete_node() operations become very simple and don't need locking * the config structure is much smaller In spite of the completely different internal data representation the user-space interface remains almost unchanged; the only difference is that the proc file does not list nodes in the order they were added. (The target info structure remains the same.) Signed-off-by: KOVACS Krisztian <[email protected]> Signed-off-by: Harald Welte <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 4451362 commit 136e92b

File tree

1 file changed

+61
-82
lines changed

1 file changed

+61
-82
lines changed

net/ipv4/netfilter/ipt_CLUSTERIP.c

Lines changed: 61 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/config.h>
1414
#include <linux/proc_fs.h>
1515
#include <linux/jhash.h>
16+
#include <linux/bitops.h>
1617
#include <linux/skbuff.h>
1718
#include <linux/ip.h>
1819
#include <linux/tcp.h>
@@ -30,7 +31,7 @@
3031
#include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
3132
#include <linux/netfilter_ipv4/ip_conntrack.h>
3233

33-
#define CLUSTERIP_VERSION "0.7"
34+
#define CLUSTERIP_VERSION "0.8"
3435

3536
#define DEBUG_CLUSTERIP
3637

@@ -56,8 +57,7 @@ struct clusterip_config {
5657
u_int8_t clustermac[ETH_ALEN]; /* the MAC address */
5758
struct net_device *dev; /* device */
5859
u_int16_t num_total_nodes; /* total number of nodes */
59-
u_int16_t num_local_nodes; /* number of local nodes */
60-
u_int16_t local_nodes[CLUSTERIP_MAX_NODES]; /* node number array */
60+
unsigned long local_nodes; /* node number array */
6161

6262
#ifdef CONFIG_PROC_FS
6363
struct proc_dir_entry *pde; /* proc dir entry */
@@ -68,8 +68,7 @@ struct clusterip_config {
6868

6969
static LIST_HEAD(clusterip_configs);
7070

71-
/* clusterip_lock protects the clusterip_configs list _AND_ the configurable
72-
* data within all structurses (num_local_nodes, local_nodes[]) */
71+
/* clusterip_lock protects the clusterip_configs list */
7372
static DEFINE_RWLOCK(clusterip_lock);
7473

7574
#ifdef CONFIG_PROC_FS
@@ -156,6 +155,17 @@ clusterip_config_find_get(u_int32_t clusterip, int entry)
156155
return c;
157156
}
158157

158+
static void
159+
clusterip_config_init_nodelist(struct clusterip_config *c,
160+
const struct ipt_clusterip_tgt_info *i)
161+
{
162+
int n;
163+
164+
for (n = 0; n < i->num_local_nodes; n++) {
165+
set_bit(i->local_nodes[n] - 1, &c->local_nodes);
166+
}
167+
}
168+
159169
static struct clusterip_config *
160170
clusterip_config_init(struct ipt_clusterip_tgt_info *i, u_int32_t ip,
161171
struct net_device *dev)
@@ -172,8 +182,7 @@ clusterip_config_init(struct ipt_clusterip_tgt_info *i, u_int32_t ip,
172182
c->clusterip = ip;
173183
memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);
174184
c->num_total_nodes = i->num_total_nodes;
175-
c->num_local_nodes = i->num_local_nodes;
176-
memcpy(&c->local_nodes, &i->local_nodes, sizeof(c->local_nodes));
185+
clusterip_config_init_nodelist(c, i);
177186
c->hash_mode = i->hash_mode;
178187
c->hash_initval = i->hash_initval;
179188
atomic_set(&c->refcount, 1);
@@ -201,53 +210,28 @@ clusterip_config_init(struct ipt_clusterip_tgt_info *i, u_int32_t ip,
201210
static int
202211
clusterip_add_node(struct clusterip_config *c, u_int16_t nodenum)
203212
{
204-
int i;
205-
206-
write_lock_bh(&clusterip_lock);
207213

208-
if (c->num_local_nodes >= CLUSTERIP_MAX_NODES
209-
|| nodenum > CLUSTERIP_MAX_NODES) {
210-
write_unlock_bh(&clusterip_lock);
214+
if (nodenum == 0 ||
215+
nodenum > c->num_total_nodes)
211216
return 1;
212-
}
213-
214-
/* check if we alrady have this number in our array */
215-
for (i = 0; i < c->num_local_nodes; i++) {
216-
if (c->local_nodes[i] == nodenum) {
217-
write_unlock_bh(&clusterip_lock);
218-
return 1;
219-
}
220-
}
221217

222-
c->local_nodes[c->num_local_nodes++] = nodenum;
218+
/* check if we already have this number in our bitfield */
219+
if (test_and_set_bit(nodenum - 1, &c->local_nodes))
220+
return 1;
223221

224-
write_unlock_bh(&clusterip_lock);
225222
return 0;
226223
}
227224

228225
static int
229226
clusterip_del_node(struct clusterip_config *c, u_int16_t nodenum)
230227
{
231-
int i;
232-
233-
write_lock_bh(&clusterip_lock);
234-
235-
if (c->num_local_nodes <= 1 || nodenum > CLUSTERIP_MAX_NODES) {
236-
write_unlock_bh(&clusterip_lock);
228+
if (nodenum == 0 ||
229+
nodenum > c->num_total_nodes)
237230
return 1;
238-
}
239231

240-
for (i = 0; i < c->num_local_nodes; i++) {
241-
if (c->local_nodes[i] == nodenum) {
242-
int size = sizeof(u_int16_t)*(c->num_local_nodes-(i+1));
243-
memmove(&c->local_nodes[i], &c->local_nodes[i+1], size);
244-
c->num_local_nodes--;
245-
write_unlock_bh(&clusterip_lock);
246-
return 0;
247-
}
248-
}
232+
if (test_and_clear_bit(nodenum - 1, &c->local_nodes))
233+
return 0;
249234

250-
write_unlock_bh(&clusterip_lock);
251235
return 1;
252236
}
253237

@@ -315,25 +299,7 @@ clusterip_hashfn(struct sk_buff *skb, struct clusterip_config *config)
315299
static inline int
316300
clusterip_responsible(struct clusterip_config *config, u_int32_t hash)
317301
{
318-
int i;
319-
320-
read_lock_bh(&clusterip_lock);
321-
322-
if (config->num_local_nodes == 0) {
323-
read_unlock_bh(&clusterip_lock);
324-
return 0;
325-
}
326-
327-
for (i = 0; i < config->num_local_nodes; i++) {
328-
if (config->local_nodes[i] == hash) {
329-
read_unlock_bh(&clusterip_lock);
330-
return 1;
331-
}
332-
}
333-
334-
read_unlock_bh(&clusterip_lock);
335-
336-
return 0;
302+
return test_bit(hash - 1, &config->local_nodes);
337303
}
338304

339305
/***********************************************************************
@@ -618,56 +584,69 @@ static struct nf_hook_ops cip_arp_ops = {
618584

619585
#ifdef CONFIG_PROC_FS
620586

587+
struct clusterip_seq_position {
588+
unsigned int pos; /* position */
589+
unsigned int weight; /* number of bits set == size */
590+
unsigned int bit; /* current bit */
591+
unsigned long val; /* current value */
592+
};
593+
621594
static void *clusterip_seq_start(struct seq_file *s, loff_t *pos)
622595
{
623596
struct proc_dir_entry *pde = s->private;
624597
struct clusterip_config *c = pde->data;
625-
unsigned int *nodeidx;
626-
627-
read_lock_bh(&clusterip_lock);
628-
if (*pos >= c->num_local_nodes)
598+
unsigned int weight;
599+
u_int32_t local_nodes;
600+
struct clusterip_seq_position *idx;
601+
602+
/* FIXME: possible race */
603+
local_nodes = c->local_nodes;
604+
weight = hweight32(local_nodes);
605+
if (*pos >= weight)
629606
return NULL;
630607

631-
nodeidx = kmalloc(sizeof(unsigned int), GFP_KERNEL);
632-
if (!nodeidx)
608+
idx = kmalloc(sizeof(struct clusterip_seq_position), GFP_KERNEL);
609+
if (!idx)
633610
return ERR_PTR(-ENOMEM);
634611

635-
*nodeidx = *pos;
636-
return nodeidx;
612+
idx->pos = *pos;
613+
idx->weight = weight;
614+
idx->bit = ffs(local_nodes);
615+
idx->val = local_nodes;
616+
clear_bit(idx->bit - 1, &idx->val);
617+
618+
return idx;
637619
}
638620

639621
static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
640622
{
641-
struct proc_dir_entry *pde = s->private;
642-
struct clusterip_config *c = pde->data;
643-
unsigned int *nodeidx = (unsigned int *)v;
623+
struct clusterip_seq_position *idx = (struct clusterip_seq_position *)v;
644624

645-
*pos = ++(*nodeidx);
646-
if (*pos >= c->num_local_nodes) {
625+
*pos = ++idx->pos;
626+
if (*pos >= idx->weight) {
647627
kfree(v);
648628
return NULL;
649629
}
650-
return nodeidx;
630+
idx->bit = ffs(idx->val);
631+
clear_bit(idx->bit - 1, &idx->val);
632+
return idx;
651633
}
652634

653635
static void clusterip_seq_stop(struct seq_file *s, void *v)
654636
{
655637
kfree(v);
656-
657-
read_unlock_bh(&clusterip_lock);
658638
}
659639

660640
static int clusterip_seq_show(struct seq_file *s, void *v)
661641
{
662-
struct proc_dir_entry *pde = s->private;
663-
struct clusterip_config *c = pde->data;
664-
unsigned int *nodeidx = (unsigned int *)v;
642+
struct clusterip_seq_position *idx = (struct clusterip_seq_position *)v;
665643

666-
if (*nodeidx != 0)
644+
if (idx->pos != 0)
667645
seq_putc(s, ',');
668-
seq_printf(s, "%u", c->local_nodes[*nodeidx]);
669646

670-
if (*nodeidx == c->num_local_nodes-1)
647+
seq_printf(s, "%u", idx->bit);
648+
649+
if (idx->pos == idx->weight - 1)
671650
seq_putc(s, '\n');
672651

673652
return 0;

0 commit comments

Comments
 (0)