netfilter: nft_exthdr: remove redundant op parsing in tcp_set_init
Subsystems (3)
PATCH 1/2 netfilter: nft_exthdr: remove redundant op parsing in tcp_set_init
The nft_exthdr_tcp_set_init() function is exclusively used for TCP options. Therefore, parsing the NFTA_EXTHDR_OP attribute from user space is redundant, as the operation type must always be NFT_EXTHDR_OP_TCPOPT.
Remove the unnecessary netlink attribute parsing and hardcode priv->op to NFT_EXTHDR_OP_TCPOPT, which simplifies the code and reduces the object size.
./scripts/bloat-o-meter exthdr_before.o exthdr_after.o add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-54 (-54)
Function old new delta nft_exthdr_tcp_set_init 412 358 -54
Total: Before=6581, After=6527, chg -0.82%
--- net/netfilter/nft_exthdr.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
net/netfilter/nft_exthdr.c +2 -6
@@ -551,7 +551,7 @@ static int nft_exthdr_tcp_set_init(const struct nft_ctx *ctx, const struct nlattr * const tb[]) {struct nft_exthdr *priv =nft_expr_priv (expr); -u32 offset, len, flags =0 , op =NFT_EXTHDR_OP_IPV6 ; +u32 offset, len, flags =0 ;int err; if (!tb[NFTA_EXTHDR_SREG] ||
@@ -581,15 +581,11 @@ static int nft_exthdr_tcp_set_init(const struct nft_ctx *ctx,return -EOPNOTSUPP ; } - err =nft_parse_u32_check (tb[NFTA_EXTHDR_OP ],U8_MAX , &op); -if (err <0 ) -return err; - priv->type =nla_get_u8 (tb[NFTA_EXTHDR_TYPE ]); priv->offset = offset; priv->len = len; priv->flags = flags; - priv->op = op; + priv->op =NFT_EXTHDR_OP_TCPOPT ;return nft_parse_register_load (ctx, tb[NFTA_EXTHDR_SREG ], &priv->sreg, priv->len);
Signature · 1 line
2.55.0
PATCH 2/2 netfilter: nft_exthdr: avoid 60 bytes stack buffer in TCP option mangling
nft_exthdr_tcp_set_eval() and nft_exthdr_tcp_strip_eval() fetched the TCP header and options through nft_tcp_header_pointer(), which calls skb_header_pointer() a second time for the full tcphdr_len into a 60 bytes stack buffer. Since commit 28427f368f0e ("netfilter: nft_exthdr:
Fix non-linear header modification") both functions then call skb_ensure_writable() and re-read the header from skb->data, so the buffer was never used.
Only the 20 bytes fixed header is needed to learn tcphdr_len, so fetch just that, validate the length as before, and let skb_ensure_writable() pull the rest. This also drops the 60 bytes stack buffer from both functions.
Comparison of performance in mpps:
op linear/non-linear before after delta
==================================================================
set, non-linear 4.193 4.348 +3.7% mpps strip, non-linear 4.184 4.434 +6.0% mpps set, linear 5.302 5.427 +2.4% mpps strip, linear 5.452 5.527 +1.4% mpss
Comparison of performance in ns/expression:
op linear/non-linear before after delta
==================================================================
set, non-linear 34.6 25.3 -9.4 ns strip, non-linear 35.2 20.8 -14.3 ns set, linear 23.6 19.7 -3.9 ns strip, linear 22.9 19.8 -3.1 ns
./scripts/bloat-o-meter exthdr_before.o exthdr_after.o add/remove: 0/1 grow/shrink: 3/0 up/down: 233/-207 (26)
Function old new delta nft_exthdr_tcp_eval 792 946 +154 nft_exthdr_tcp_strip_eval 825 891 +66 nft_exthdr_tcp_set_eval 602 615 +13 nft_tcp_header_pointer.part.constprop 207 - -207
Total: Before=6527, After=6553, chg +0.40%
This also allow the compiler to inline nft_tcp_header_pointer inside nft_exthdr_tcp_eval() which could yield some minimal performance gain.
--- net/netfilter/nft_exthdr.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-)
net/netfilter/nft_exthdr.c +20 -6
@@ -233,16 +233,23 @@ static void nft_exthdr_tcp_set_eval(const struct nft_expr *expr,struct nft_regs *regs, const struct nft_pktinfo *pkt) { - u8 buff[sizeof(struct tcphdr) + MAX_TCP_OPTION_SPACE]; struct nft_exthdr *priv = nft_expr_priv(expr);unsigned int i, optl, tcphdr_len, offset; -struct tcphdr *tcph; +struct tcphdr *tcph, _tcph;u8 *opt; - tcph =nft_tcp_header_pointer (pkt,sizeof (buff), buff, &tcphdr_len); +if (pkt->tprot !=IPPROTO_TCP || pkt->fragoff) + goto err; + + tcph =skb_header_pointer (pkt->skb,nft_thoff (pkt),sizeof (_tcph), &_tcph);if (!tcph) goto err; + tcphdr_len =__tcp_hdrlen (tcph); +if (tcphdr_len <sizeof (*tcph) || + tcphdr_len >sizeof (*tcph) +MAX_TCP_OPTION_SPACE ) + goto err; +if (skb_ensure_writable (pkt->skb,nft_thoff (pkt) + tcphdr_len)) goto err;
@@ -313,16 +320,23 @@ static void nft_exthdr_tcp_strip_eval(const struct nft_expr *expr,struct nft_regs *regs, const struct nft_pktinfo *pkt) { - u8 buff[sizeof(struct tcphdr) + MAX_TCP_OPTION_SPACE]; struct nft_exthdr *priv = nft_expr_priv(expr);unsigned int i, tcphdr_len, optl; -struct tcphdr *tcph; +struct tcphdr *tcph, _tcph;u8 *opt; - tcph =nft_tcp_header_pointer (pkt,sizeof (buff), buff, &tcphdr_len); +if (pkt->tprot !=IPPROTO_TCP || pkt->fragoff) + goto err; + + tcph =skb_header_pointer (pkt->skb,nft_thoff (pkt),sizeof (_tcph), &_tcph);if (!tcph) goto err; + tcphdr_len =__tcp_hdrlen (tcph); +if (tcphdr_len <sizeof (*tcph) || + tcphdr_len >sizeof (*tcph) +MAX_TCP_OPTION_SPACE ) + goto err; +if (skb_ensure_writable (pkt->skb,nft_thoff (pkt) + tcphdr_len)) goto drop;
Signature · 1 line
2.55.0