Skip to content

Commit 46df417

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nf_tables: do not store rule in traceinfo structure
pass it as argument instead. This reduces size of traceinfo to 16 bytes. Total stack usage: nf_tables_core.c:252 nft_do_chain 304 static While its possible to also pass basechain as argument, doing so increases nft_do_chaininfo function size. Unlike pktinfo/verdict/rule the basechain info isn't used in the expression evaluation path. gcc places it on the stack, which results in extra push/pop when it gets passed to the trace helpers as argument rather than as part of the traceinfo structure. Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 0a20214 commit 46df417

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

include/net/netfilter/nf_tables.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,6 @@ void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
14091409
* @skbid: hash of skb to be used as trace id
14101410
* @packet_dumped: packet headers sent in a previous traceinfo message
14111411
* @basechain: base chain currently processed
1412-
* @rule: rule that was evaluated
14131412
*/
14141413
struct nft_traceinfo {
14151414
bool trace;
@@ -1418,14 +1417,14 @@ struct nft_traceinfo {
14181417
enum nft_trace_types type:8;
14191418
u32 skbid;
14201419
const struct nft_base_chain *basechain;
1421-
const struct nft_rule_dp *rule;
14221420
};
14231421

14241422
void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
14251423
const struct nft_chain *basechain);
14261424

14271425
void nft_trace_notify(const struct nft_pktinfo *pkt,
14281426
const struct nft_verdict *verdict,
1427+
const struct nft_rule_dp *rule,
14291428
struct nft_traceinfo *info);
14301429

14311430
#define MODULE_ALIAS_NFT_CHAIN(family, name) \

net/netfilter/nf_tables_core.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static inline void nf_skip_indirect_calls_enable(void) { }
4343

4444
static noinline void __nft_trace_packet(const struct nft_pktinfo *pkt,
4545
const struct nft_verdict *verdict,
46+
const struct nft_rule_dp *rule,
4647
struct nft_traceinfo *info,
4748
enum nft_trace_types type)
4849
{
@@ -51,7 +52,7 @@ static noinline void __nft_trace_packet(const struct nft_pktinfo *pkt,
5152

5253
info->type = type;
5354

54-
nft_trace_notify(pkt, verdict, info);
55+
nft_trace_notify(pkt, verdict, rule, info);
5556
}
5657

5758
static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
@@ -62,8 +63,7 @@ static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
6263
{
6364
if (static_branch_unlikely(&nft_trace_enabled)) {
6465
info->nf_trace = pkt->skb->nf_trace;
65-
info->rule = rule;
66-
__nft_trace_packet(pkt, verdict, info, type);
66+
__nft_trace_packet(pkt, verdict, rule, info, type);
6767
}
6868
}
6969

@@ -110,6 +110,7 @@ static void nft_cmp16_fast_eval(const struct nft_expr *expr,
110110

111111
static noinline void __nft_trace_verdict(const struct nft_pktinfo *pkt,
112112
struct nft_traceinfo *info,
113+
const struct nft_rule_dp *rule,
113114
const struct nft_regs *regs)
114115
{
115116
enum nft_trace_types type;
@@ -131,18 +132,16 @@ static noinline void __nft_trace_verdict(const struct nft_pktinfo *pkt,
131132
break;
132133
}
133134

134-
__nft_trace_packet(pkt, &regs->verdict, info, type);
135+
__nft_trace_packet(pkt, &regs->verdict, rule, info, type);
135136
}
136137

137138
static inline void nft_trace_verdict(const struct nft_pktinfo *pkt,
138139
struct nft_traceinfo *info,
139140
const struct nft_rule_dp *rule,
140141
const struct nft_regs *regs)
141142
{
142-
if (static_branch_unlikely(&nft_trace_enabled)) {
143-
info->rule = rule;
144-
__nft_trace_verdict(pkt, info, regs);
145-
}
143+
if (static_branch_unlikely(&nft_trace_enabled))
144+
__nft_trace_verdict(pkt, info, rule, regs);
146145
}
147146

148147
static bool nft_payload_fast_eval(const struct nft_expr *expr,

net/netfilter/nf_tables_trace.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ static int nf_trace_fill_pkt_info(struct sk_buff *nlskb,
125125

126126
static int nf_trace_fill_rule_info(struct sk_buff *nlskb,
127127
const struct nft_verdict *verdict,
128+
const struct nft_rule_dp *rule,
128129
const struct nft_traceinfo *info)
129130
{
130-
if (!info->rule || info->rule->is_last)
131+
if (!rule || rule->is_last)
131132
return 0;
132133

133134
/* a continue verdict with ->type == RETURN means that this is
@@ -140,7 +141,7 @@ static int nf_trace_fill_rule_info(struct sk_buff *nlskb,
140141
return 0;
141142

142143
return nla_put_be64(nlskb, NFTA_TRACE_RULE_HANDLE,
143-
cpu_to_be64(info->rule->handle),
144+
cpu_to_be64(rule->handle),
144145
NFTA_TRACE_PAD);
145146
}
146147

@@ -166,9 +167,9 @@ static bool nft_trace_have_verdict_chain(const struct nft_verdict *verdict,
166167
return true;
167168
}
168169

169-
static const struct nft_chain *nft_trace_get_chain(const struct nft_traceinfo *info)
170+
static const struct nft_chain *nft_trace_get_chain(const struct nft_rule_dp *rule,
171+
const struct nft_traceinfo *info)
170172
{
171-
const struct nft_rule_dp *rule = info->rule;
172173
const struct nft_rule_dp_last *last;
173174

174175
if (!rule)
@@ -187,6 +188,7 @@ static const struct nft_chain *nft_trace_get_chain(const struct nft_traceinfo *i
187188

188189
void nft_trace_notify(const struct nft_pktinfo *pkt,
189190
const struct nft_verdict *verdict,
191+
const struct nft_rule_dp *rule,
190192
struct nft_traceinfo *info)
191193
{
192194
const struct nft_chain *chain;
@@ -199,7 +201,7 @@ void nft_trace_notify(const struct nft_pktinfo *pkt,
199201
if (!nfnetlink_has_listeners(nft_net(pkt), NFNLGRP_NFTRACE))
200202
return;
201203

202-
chain = nft_trace_get_chain(info);
204+
chain = nft_trace_get_chain(rule, info);
203205

204206
size = nlmsg_total_size(sizeof(struct nfgenmsg)) +
205207
nla_total_size(strlen(chain->table->name)) +
@@ -248,7 +250,7 @@ void nft_trace_notify(const struct nft_pktinfo *pkt,
248250
if (nla_put_string(skb, NFTA_TRACE_TABLE, chain->table->name))
249251
goto nla_put_failure;
250252

251-
if (nf_trace_fill_rule_info(skb, verdict, info))
253+
if (nf_trace_fill_rule_info(skb, verdict, rule, info))
252254
goto nla_put_failure;
253255

254256
switch (info->type) {

0 commit comments

Comments
 (0)