Skip to content

Commit d9e7891

Browse files
author
Florian Westphal
committed
netfilter: nf_tables: avoid retpoline overhead for some ct expression calls
nft_ct expression cannot be made builtin to nf_tables without also forcing the conntrack itself to be builtin. However, this can be avoided by splitting retrieval of a few selector keys that only need to access the nf_conn structure, i.e. no function calls to nf_conntrack code. Many rulesets start with something like "ct status established,related accept" With this change, this no longer requires an indirect call, which gives about 1.8% more throughput with a simple conntrack-enabled forwarding test (retpoline thunk used). Signed-off-by: Florian Westphal <[email protected]>
1 parent 2032e90 commit d9e7891

File tree

5 files changed

+104
-12
lines changed

5 files changed

+104
-12
lines changed

include/net/netfilter/nf_tables_core.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ struct nft_immediate_expr {
6161
extern const struct nft_expr_ops nft_cmp_fast_ops;
6262
extern const struct nft_expr_ops nft_cmp16_fast_ops;
6363

64+
struct nft_ct {
65+
enum nft_ct_keys key:8;
66+
enum ip_conntrack_dir dir:8;
67+
u8 len;
68+
union {
69+
u8 dreg;
70+
u8 sreg;
71+
};
72+
};
73+
6474
struct nft_payload {
6575
enum nft_payload_bases base:8;
6676
u8 offset;
@@ -140,6 +150,8 @@ void nft_rt_get_eval(const struct nft_expr *expr,
140150
struct nft_regs *regs, const struct nft_pktinfo *pkt);
141151
void nft_counter_eval(const struct nft_expr *expr, struct nft_regs *regs,
142152
const struct nft_pktinfo *pkt);
153+
void nft_ct_get_fast_eval(const struct nft_expr *expr,
154+
struct nft_regs *regs, const struct nft_pktinfo *pkt);
143155

144156
enum {
145157
NFT_PAYLOAD_CTX_INNER_TUN = (1 << 0),

net/netfilter/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ nf_tables-objs += nft_set_pipapo_avx2.o
9898
endif
9999
endif
100100

101+
ifdef CONFIG_NFT_CT
102+
ifdef CONFIG_RETPOLINE
103+
nf_tables-objs += nft_ct_fast.o
104+
endif
105+
endif
106+
101107
obj-$(CONFIG_NF_TABLES) += nf_tables.o
102108
obj-$(CONFIG_NFT_COMPAT) += nft_compat.o
103109
obj-$(CONFIG_NFT_CONNLIMIT) += nft_connlimit.o

net/netfilter/nf_tables_core.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ static void expr_call_ops_eval(const struct nft_expr *expr,
228228
X(e, nft_counter_eval);
229229
X(e, nft_meta_get_eval);
230230
X(e, nft_lookup_eval);
231+
#if IS_ENABLED(CONFIG_NFT_CT)
232+
X(e, nft_ct_get_fast_eval);
233+
#endif
231234
X(e, nft_range_eval);
232235
X(e, nft_immediate_eval);
233236
X(e, nft_byteorder_eval);

net/netfilter/nft_ct.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <linux/netlink.h>
1313
#include <linux/netfilter.h>
1414
#include <linux/netfilter/nf_tables.h>
15-
#include <net/netfilter/nf_tables.h>
15+
#include <net/netfilter/nf_tables_core.h>
1616
#include <net/netfilter/nf_conntrack.h>
1717
#include <net/netfilter/nf_conntrack_acct.h>
1818
#include <net/netfilter/nf_conntrack_tuple.h>
@@ -23,16 +23,6 @@
2323
#include <net/netfilter/nf_conntrack_l4proto.h>
2424
#include <net/netfilter/nf_conntrack_expect.h>
2525

26-
struct nft_ct {
27-
enum nft_ct_keys key:8;
28-
enum ip_conntrack_dir dir:8;
29-
u8 len;
30-
union {
31-
u8 dreg;
32-
u8 sreg;
33-
};
34-
};
35-
3626
struct nft_ct_helper_obj {
3727
struct nf_conntrack_helper *helper4;
3828
struct nf_conntrack_helper *helper6;
@@ -759,6 +749,18 @@ static bool nft_ct_set_reduce(struct nft_regs_track *track,
759749
return false;
760750
}
761751

752+
#ifdef CONFIG_RETPOLINE
753+
static const struct nft_expr_ops nft_ct_get_fast_ops = {
754+
.type = &nft_ct_type,
755+
.size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
756+
.eval = nft_ct_get_fast_eval,
757+
.init = nft_ct_get_init,
758+
.destroy = nft_ct_get_destroy,
759+
.dump = nft_ct_get_dump,
760+
.reduce = nft_ct_set_reduce,
761+
};
762+
#endif
763+
762764
static const struct nft_expr_ops nft_ct_set_ops = {
763765
.type = &nft_ct_type,
764766
.size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
@@ -791,8 +793,21 @@ nft_ct_select_ops(const struct nft_ctx *ctx,
791793
if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG])
792794
return ERR_PTR(-EINVAL);
793795

794-
if (tb[NFTA_CT_DREG])
796+
if (tb[NFTA_CT_DREG]) {
797+
#ifdef CONFIG_RETPOLINE
798+
u32 k = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
799+
800+
switch (k) {
801+
case NFT_CT_STATE:
802+
case NFT_CT_DIRECTION:
803+
case NFT_CT_STATUS:
804+
case NFT_CT_MARK:
805+
case NFT_CT_SECMARK:
806+
return &nft_ct_get_fast_ops;
807+
}
808+
#endif
795809
return &nft_ct_get_ops;
810+
}
796811

797812
if (tb[NFTA_CT_SREG]) {
798813
#ifdef CONFIG_NF_CONNTRACK_ZONES

net/netfilter/nft_ct_fast.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#if IS_ENABLED(CONFIG_NFT_CT)
3+
#include <linux/netfilter/nf_tables.h>
4+
#include <net/netfilter/nf_tables_core.h>
5+
#include <net/netfilter/nf_conntrack.h>
6+
7+
void nft_ct_get_fast_eval(const struct nft_expr *expr,
8+
struct nft_regs *regs,
9+
const struct nft_pktinfo *pkt)
10+
{
11+
const struct nft_ct *priv = nft_expr_priv(expr);
12+
u32 *dest = &regs->data[priv->dreg];
13+
enum ip_conntrack_info ctinfo;
14+
const struct nf_conn *ct;
15+
unsigned int state;
16+
17+
ct = nf_ct_get(pkt->skb, &ctinfo);
18+
if (!ct) {
19+
regs->verdict.code = NFT_BREAK;
20+
return;
21+
}
22+
23+
switch (priv->key) {
24+
case NFT_CT_STATE:
25+
if (ct)
26+
state = NF_CT_STATE_BIT(ctinfo);
27+
else if (ctinfo == IP_CT_UNTRACKED)
28+
state = NF_CT_STATE_UNTRACKED_BIT;
29+
else
30+
state = NF_CT_STATE_INVALID_BIT;
31+
*dest = state;
32+
return;
33+
case NFT_CT_DIRECTION:
34+
nft_reg_store8(dest, CTINFO2DIR(ctinfo));
35+
return;
36+
case NFT_CT_STATUS:
37+
*dest = ct->status;
38+
return;
39+
#ifdef CONFIG_NF_CONNTRACK_MARK
40+
case NFT_CT_MARK:
41+
*dest = ct->mark;
42+
return;
43+
#endif
44+
#ifdef CONFIG_NF_CONNTRACK_SECMARK
45+
case NFT_CT_SECMARK:
46+
*dest = ct->secmark;
47+
return;
48+
#endif
49+
default:
50+
WARN_ON_ONCE(1);
51+
regs->verdict.code = NFT_BREAK;
52+
break;
53+
}
54+
}
55+
EXPORT_SYMBOL_GPL(nft_ct_get_fast_eval);
56+
#endif

0 commit comments

Comments
 (0)