Skip to content

Commit e3b37f1

Browse files
apconoleummakynes
authored andcommitted
netfilter: replace list_head with single linked list
The netfilter hook list never uses the prev pointer, and so can be trimmed to be a simple singly-linked list. In addition to having a more light weight structure for hook traversal, struct net becomes 5568 bytes (down from 6400) and struct net_device becomes 2176 bytes (down from 2240). Signed-off-by: Aaron Conole <aconole@bytheb.org> Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 54f17bb commit e3b37f1

10 files changed

Lines changed: 167 additions & 116 deletions

File tree

‎include/linux/netdevice.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,7 @@ struct net_device {
17831783
#endif
17841784
struct netdev_queue __rcu *ingress_queue;
17851785
#ifdef CONFIG_NETFILTER_INGRESS
1786-
struct list_head nf_hooks_ingress;
1786+
struct nf_hook_entry __rcu *nf_hooks_ingress;
17871787
#endif
17881788

17891789
unsigned char broadcast[MAX_ADDR_LEN];

‎include/linux/netfilter.h‎

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,34 @@ struct nf_hook_state {
5555
struct net_device *out;
5656
struct sock *sk;
5757
struct net *net;
58-
struct list_head *hook_list;
58+
struct nf_hook_entry __rcu *hook_entries;
5959
int (*okfn)(struct net *, struct sock *, struct sk_buff *);
6060
};
6161

62+
typedef unsigned int nf_hookfn(void *priv,
63+
struct sk_buff *skb,
64+
const struct nf_hook_state *state);
65+
struct nf_hook_ops {
66+
struct list_head list;
67+
68+
/* User fills in from here down. */
69+
nf_hookfn *hook;
70+
struct net_device *dev;
71+
void *priv;
72+
u_int8_t pf;
73+
unsigned int hooknum;
74+
/* Hooks are ordered in ascending priority. */
75+
int priority;
76+
};
77+
78+
struct nf_hook_entry {
79+
struct nf_hook_entry __rcu *next;
80+
struct nf_hook_ops ops;
81+
const struct nf_hook_ops *orig_ops;
82+
};
83+
6284
static inline void nf_hook_state_init(struct nf_hook_state *p,
63-
struct list_head *hook_list,
85+
struct nf_hook_entry *hook_entry,
6486
unsigned int hook,
6587
int thresh, u_int8_t pf,
6688
struct net_device *indev,
@@ -76,26 +98,11 @@ static inline void nf_hook_state_init(struct nf_hook_state *p,
7698
p->out = outdev;
7799
p->sk = sk;
78100
p->net = net;
79-
p->hook_list = hook_list;
101+
RCU_INIT_POINTER(p->hook_entries, hook_entry);
80102
p->okfn = okfn;
81103
}
82104

83-
typedef unsigned int nf_hookfn(void *priv,
84-
struct sk_buff *skb,
85-
const struct nf_hook_state *state);
86-
87-
struct nf_hook_ops {
88-
struct list_head list;
89105

90-
/* User fills in from here down. */
91-
nf_hookfn *hook;
92-
struct net_device *dev;
93-
void *priv;
94-
u_int8_t pf;
95-
unsigned int hooknum;
96-
/* Hooks are ordered in ascending priority. */
97-
int priority;
98-
};
99106

100107
struct nf_sockopt_ops {
101108
struct list_head list;
@@ -161,7 +168,8 @@ static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
161168
int (*okfn)(struct net *, struct sock *, struct sk_buff *),
162169
int thresh)
163170
{
164-
struct list_head *hook_list;
171+
struct nf_hook_entry *hook_head;
172+
int ret = 1;
165173

166174
#ifdef HAVE_JUMP_LABEL
167175
if (__builtin_constant_p(pf) &&
@@ -170,22 +178,19 @@ static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
170178
return 1;
171179
#endif
172180

173-
hook_list = &net->nf.hooks[pf][hook];
174-
175-
if (!list_empty(hook_list)) {
181+
rcu_read_lock();
182+
hook_head = rcu_dereference(net->nf.hooks[pf][hook]);
183+
if (hook_head) {
176184
struct nf_hook_state state;
177-
int ret;
178185

179-
/* We may already have this, but read-locks nest anyway */
180-
rcu_read_lock();
181-
nf_hook_state_init(&state, hook_list, hook, thresh,
186+
nf_hook_state_init(&state, hook_head, hook, thresh,
182187
pf, indev, outdev, sk, net, okfn);
183188

184189
ret = nf_hook_slow(skb, &state);
185-
rcu_read_unlock();
186-
return ret;
187190
}
188-
return 1;
191+
rcu_read_unlock();
192+
193+
return ret;
189194
}
190195

191196
static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,

‎include/linux/netfilter_ingress.h‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,30 @@ static inline bool nf_hook_ingress_active(const struct sk_buff *skb)
1111
if (!static_key_false(&nf_hooks_needed[NFPROTO_NETDEV][NF_NETDEV_INGRESS]))
1212
return false;
1313
#endif
14-
return !list_empty(&skb->dev->nf_hooks_ingress);
14+
return rcu_access_pointer(skb->dev->nf_hooks_ingress);
1515
}
1616

1717
/* caller must hold rcu_read_lock */
1818
static inline int nf_hook_ingress(struct sk_buff *skb)
1919
{
20+
struct nf_hook_entry *e = rcu_dereference(skb->dev->nf_hooks_ingress);
2021
struct nf_hook_state state;
2122

22-
nf_hook_state_init(&state, &skb->dev->nf_hooks_ingress,
23-
NF_NETDEV_INGRESS, INT_MIN, NFPROTO_NETDEV,
24-
skb->dev, NULL, NULL, dev_net(skb->dev), NULL);
23+
/* Must recheck the ingress hook head, in the event it became NULL
24+
* after the check in nf_hook_ingress_active evaluated to true.
25+
*/
26+
if (unlikely(!e))
27+
return 0;
28+
29+
nf_hook_state_init(&state, e, NF_NETDEV_INGRESS, INT_MIN,
30+
NFPROTO_NETDEV, skb->dev, NULL, NULL,
31+
dev_net(skb->dev), NULL);
2532
return nf_hook_slow(skb, &state);
2633
}
2734

2835
static inline void nf_hook_ingress_init(struct net_device *dev)
2936
{
30-
INIT_LIST_HEAD(&dev->nf_hooks_ingress);
37+
RCU_INIT_POINTER(dev->nf_hooks_ingress, NULL);
3138
}
3239
#else /* CONFIG_NETFILTER_INGRESS */
3340
static inline int nf_hook_ingress_active(struct sk_buff *skb)

‎include/net/netfilter/nf_queue.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ struct nf_queue_entry {
1111
struct sk_buff *skb;
1212
unsigned int id;
1313

14-
struct nf_hook_ops *elem;
1514
struct nf_hook_state state;
1615
u16 size; /* sizeof(entry) + saved route keys */
1716

@@ -25,7 +24,7 @@ struct nf_queue_handler {
2524
int (*outfn)(struct nf_queue_entry *entry,
2625
unsigned int queuenum);
2726
void (*nf_hook_drop)(struct net *net,
28-
struct nf_hook_ops *ops);
27+
const struct nf_hook_entry *hooks);
2928
};
3029

3130
void nf_register_queue_handler(struct net *net, const struct nf_queue_handler *qh);

‎include/net/netns/netfilter.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ struct netns_nf {
1616
#ifdef CONFIG_SYSCTL
1717
struct ctl_table_header *nf_log_dir_header;
1818
#endif
19-
struct list_head hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
19+
struct nf_hook_entry __rcu *hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
2020
};
2121
#endif

‎net/bridge/br_netfilter_hooks.c‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,28 +1002,21 @@ int br_nf_hook_thresh(unsigned int hook, struct net *net,
10021002
int (*okfn)(struct net *, struct sock *,
10031003
struct sk_buff *))
10041004
{
1005-
struct nf_hook_ops *elem;
1005+
struct nf_hook_entry *elem;
10061006
struct nf_hook_state state;
1007-
struct list_head *head;
10081007
int ret;
10091008

1010-
head = &net->nf.hooks[NFPROTO_BRIDGE][hook];
1009+
elem = rcu_dereference(net->nf.hooks[NFPROTO_BRIDGE][hook]);
10111010

1012-
list_for_each_entry_rcu(elem, head, list) {
1013-
struct nf_hook_ops *next;
1011+
while (elem && (elem->ops.priority <= NF_BR_PRI_BRNF))
1012+
elem = rcu_dereference(elem->next);
10141013

1015-
next = list_entry_rcu(list_next_rcu(&elem->list),
1016-
struct nf_hook_ops, list);
1017-
if (next->priority <= NF_BR_PRI_BRNF)
1018-
continue;
1019-
}
1020-
1021-
if (&elem->list == head)
1014+
if (!elem)
10221015
return okfn(net, sk, skb);
10231016

10241017
/* We may already have this, but read-locks nest anyway */
10251018
rcu_read_lock();
1026-
nf_hook_state_init(&state, head, hook, NF_BR_PRI_BRNF + 1,
1019+
nf_hook_state_init(&state, elem, hook, NF_BR_PRI_BRNF + 1,
10271020
NFPROTO_BRIDGE, indev, outdev, sk, net, okfn);
10281021

10291022
ret = nf_hook_slow(skb, &state);

0 commit comments

Comments
 (0)