From nobody Mon Nov 25 09:46:08 2024 Received: from serv108.segi.ulg.ac.be (serv108.segi.ulg.ac.be [139.165.32.111]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EBDF21C1F30; Tue, 19 Nov 2024 22:21:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=139.165.32.111 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732054921; cv=none; b=hIPS077mvVVXiqZ27ifPm1ni/EqAVg26PxdUvm+45xj/C7UkmWo6JKWnWx8ptoEvmiD+Nm24p/Bwzda9w+nq2pnTZCXxXscKnEcM92lsRdB1tbe2lPv9taNvO3twVRuQfzS8QuAcYHV0IJYzaOlYtzMoJmO6RI7MGa7BvEnfuuA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732054921; c=relaxed/simple; bh=ZEG9otDQvINqY6OReyg8BWgPrE/7dIYiKGpsAvyKPLo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kD+QM5Hf7pB8ghMpZD1xbaX/zx2HbheXQ5e957la0WqEWtT9lLe+k5HccPoZY4ramyMZ7ElZDOX+edu0lil05Cqtp4QeLlVNzwjae2gS0bYqnnSvTrCmwJypsZgNWDOC1n8ISVE1X5JQU12XjdtAYevBXH/foRpBw0+EEYS9Dj4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=uliege.be; spf=pass smtp.mailfrom=uliege.be; dkim=pass (2048-bit key) header.d=uliege.be header.i=@uliege.be header.b=XGRqZAjv; arc=none smtp.client-ip=139.165.32.111 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=uliege.be Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=uliege.be Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=uliege.be header.i=@uliege.be header.b="XGRqZAjv" Received: from ubuntu.home (220.24-245-81.adsl-dyn.isp.belgacom.be [81.245.24.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by serv108.segi.ulg.ac.be (Postfix) with ESMTPSA id EAE19200CCF5; Tue, 19 Nov 2024 23:21:50 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 serv108.segi.ulg.ac.be EAE19200CCF5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uliege.be; s=ulg20190529; t=1732054911; bh=Mtv8ELvUZ9071b3yI3eGE0meU9AdbEXLRQz1hY3sUiA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XGRqZAjvrGMReDZSVfpg+eFTg3VxmR5MRQ6IRgYZRg8M/qItRAz6YNrrqR7TWzbcx DhPCv06ar44z5I40CUXFFmwgMJqP3BwR+zqSxHTtBkwVO2ZtcIgI3hRcruNECQ7kSC yzqvsM5nQ8/sDeOyHAZS3Vdu1vNFjwfHf/HZfnuq6NqHfUSAgRWXRXCNAJpix5O/Ue JxHM1MSplYZB6vn6N9PKFShZGiQ0XmDMp53aiqOQhzSD98lIdeYk96jpAk2+I9o35Q jEkXpmCHGn+QgCeRjp9DyVHVzdsM938ZinzsY40kwRdtKYHam72ibdjk+ZczzG60rs 2Zgo9KmySvGkA== From: Justin Iurman To: netdev@vger.kernel.org Cc: davem@davemloft.net, dsahern@kernel.org, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, linux-kernel@vger.kernel.org, justin.iurman@uliege.be, David Lebrun Subject: [PATCH net-next v5 3/4] net: ipv6: seg6_iptunnel: mitigate 2-realloc issue Date: Tue, 19 Nov 2024 23:21:38 +0100 Message-Id: <20241119222139.14338-4-justin.iurman@uliege.be> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241119222139.14338-1-justin.iurman@uliege.be> References: <20241119222139.14338-1-justin.iurman@uliege.be> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch mitigates the two-reallocations issue with seg6_iptunnel by providing the dst_entry (in the cache) to the first call to skb_cow_head(). As a result, the very first iteration would still trigger two reallocations (i.e., empty cache), while next iterations would only trigger a single reallocation. Performance tests before/after applying this patch, which clearly shows the improvement: - before: https://ibb.co/3Cg4sNH - after: https://ibb.co/8rQ350r Signed-off-by: Justin Iurman Cc: David Lebrun --- net/ipv6/seg6_iptunnel.c | 85 ++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 33 deletions(-) diff --git a/net/ipv6/seg6_iptunnel.c b/net/ipv6/seg6_iptunnel.c index 098632adc9b5..4bf937bfc263 100644 --- a/net/ipv6/seg6_iptunnel.c +++ b/net/ipv6/seg6_iptunnel.c @@ -124,8 +124,8 @@ static __be32 seg6_make_flowlabel(struct net *net, stru= ct sk_buff *skb, return flowlabel; } =20 -/* encapsulate an IPv6 packet within an outer IPv6 header with a given SRH= */ -int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh, int p= roto) +static int __seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *os= rh, + int proto, struct dst_entry *cache_dst) { struct dst_entry *dst =3D skb_dst(skb); struct net *net =3D dev_net(dst->dev); @@ -137,7 +137,7 @@ int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_= sr_hdr *osrh, int proto) hdrlen =3D (osrh->hdrlen + 1) << 3; tot_len =3D hdrlen + sizeof(*hdr); =20 - err =3D skb_cow_head(skb, tot_len + skb->mac_len); + err =3D skb_cow_head(skb, tot_len + dst_dev_overhead(cache_dst, skb)); if (unlikely(err)) return err; =20 @@ -197,11 +197,18 @@ int seg6_do_srh_encap(struct sk_buff *skb, struct ipv= 6_sr_hdr *osrh, int proto) =20 return 0; } + +/* encapsulate an IPv6 packet within an outer IPv6 header with a given SRH= */ +int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh, int p= roto) +{ + return __seg6_do_srh_encap(skb, osrh, proto, NULL); +} EXPORT_SYMBOL_GPL(seg6_do_srh_encap); =20 /* encapsulate an IPv6 packet within an outer IPv6 header with reduced SRH= */ static int seg6_do_srh_encap_red(struct sk_buff *skb, - struct ipv6_sr_hdr *osrh, int proto) + struct ipv6_sr_hdr *osrh, int proto, + struct dst_entry *cache_dst) { __u8 first_seg =3D osrh->first_segment; struct dst_entry *dst =3D skb_dst(skb); @@ -230,7 +237,7 @@ static int seg6_do_srh_encap_red(struct sk_buff *skb, =20 tot_len =3D red_hdrlen + sizeof(struct ipv6hdr); =20 - err =3D skb_cow_head(skb, tot_len + skb->mac_len); + err =3D skb_cow_head(skb, tot_len + dst_dev_overhead(cache_dst, skb)); if (unlikely(err)) return err; =20 @@ -317,8 +324,8 @@ static int seg6_do_srh_encap_red(struct sk_buff *skb, return 0; } =20 -/* insert an SRH within an IPv6 packet, just after the IPv6 header */ -int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh) +static int __seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *o= srh, + struct dst_entry *cache_dst) { struct ipv6hdr *hdr, *oldhdr; struct ipv6_sr_hdr *isrh; @@ -326,7 +333,7 @@ int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6= _sr_hdr *osrh) =20 hdrlen =3D (osrh->hdrlen + 1) << 3; =20 - err =3D skb_cow_head(skb, hdrlen + skb->mac_len); + err =3D skb_cow_head(skb, hdrlen + dst_dev_overhead(cache_dst, skb)); if (unlikely(err)) return err; =20 @@ -369,9 +376,8 @@ int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6= _sr_hdr *osrh) =20 return 0; } -EXPORT_SYMBOL_GPL(seg6_do_srh_inline); =20 -static int seg6_do_srh(struct sk_buff *skb) +static int seg6_do_srh(struct sk_buff *skb, struct dst_entry *cache_dst) { struct dst_entry *dst =3D skb_dst(skb); struct seg6_iptunnel_encap *tinfo; @@ -384,7 +390,7 @@ static int seg6_do_srh(struct sk_buff *skb) if (skb->protocol !=3D htons(ETH_P_IPV6)) return -EINVAL; =20 - err =3D seg6_do_srh_inline(skb, tinfo->srh); + err =3D __seg6_do_srh_inline(skb, tinfo->srh, cache_dst); if (err) return err; break; @@ -402,9 +408,11 @@ static int seg6_do_srh(struct sk_buff *skb) return -EINVAL; =20 if (tinfo->mode =3D=3D SEG6_IPTUN_MODE_ENCAP) - err =3D seg6_do_srh_encap(skb, tinfo->srh, proto); + err =3D __seg6_do_srh_encap(skb, tinfo->srh, + proto, cache_dst); else - err =3D seg6_do_srh_encap_red(skb, tinfo->srh, proto); + err =3D seg6_do_srh_encap_red(skb, tinfo->srh, + proto, cache_dst); =20 if (err) return err; @@ -425,11 +433,13 @@ static int seg6_do_srh(struct sk_buff *skb) skb_push(skb, skb->mac_len); =20 if (tinfo->mode =3D=3D SEG6_IPTUN_MODE_L2ENCAP) - err =3D seg6_do_srh_encap(skb, tinfo->srh, - IPPROTO_ETHERNET); + err =3D __seg6_do_srh_encap(skb, tinfo->srh, + IPPROTO_ETHERNET, + cache_dst); else err =3D seg6_do_srh_encap_red(skb, tinfo->srh, - IPPROTO_ETHERNET); + IPPROTO_ETHERNET, + cache_dst); =20 if (err) return err; @@ -444,6 +454,13 @@ static int seg6_do_srh(struct sk_buff *skb) return 0; } =20 +/* insert an SRH within an IPv6 packet, just after the IPv6 header */ +int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh) +{ + return __seg6_do_srh_inline(skb, osrh, NULL); +} +EXPORT_SYMBOL_GPL(seg6_do_srh_inline); + static int seg6_input_finish(struct net *net, struct sock *sk, struct sk_buff *skb) { @@ -458,31 +475,33 @@ static int seg6_input_core(struct net *net, struct so= ck *sk, struct seg6_lwt *slwt; int err; =20 - err =3D seg6_do_srh(skb); - if (unlikely(err)) - goto drop; - slwt =3D seg6_lwt_lwtunnel(orig_dst->lwtstate); =20 local_bh_disable(); dst =3D dst_cache_get(&slwt->cache); + local_bh_enable(); + + err =3D seg6_do_srh(skb, dst); + if (unlikely(err)) + goto drop; =20 if (!dst) { ip6_route_input(skb); dst =3D skb_dst(skb); if (!dst->error) { + local_bh_disable(); dst_cache_set_ip6(&slwt->cache, dst, &ipv6_hdr(skb)->saddr); + local_bh_enable(); } + + err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); + if (unlikely(err)) + goto drop; } else { skb_dst_drop(skb); skb_dst_set(skb, dst); } - local_bh_enable(); - - err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); - if (unlikely(err)) - goto drop; =20 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, @@ -528,16 +547,16 @@ static int seg6_output_core(struct net *net, struct s= ock *sk, struct seg6_lwt *slwt; int err; =20 - err =3D seg6_do_srh(skb); - if (unlikely(err)) - goto drop; - slwt =3D seg6_lwt_lwtunnel(orig_dst->lwtstate); =20 local_bh_disable(); dst =3D dst_cache_get(&slwt->cache); local_bh_enable(); =20 + err =3D seg6_do_srh(skb, dst); + if (unlikely(err)) + goto drop; + if (unlikely(!dst)) { struct ipv6hdr *hdr =3D ipv6_hdr(skb); struct flowi6 fl6; @@ -559,15 +578,15 @@ static int seg6_output_core(struct net *net, struct s= ock *sk, local_bh_disable(); dst_cache_set_ip6(&slwt->cache, dst, &fl6.saddr); local_bh_enable(); + + err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); + if (unlikely(err)) + goto drop; } =20 skb_dst_drop(skb); skb_dst_set(skb, dst); =20 - err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); - if (unlikely(err)) - goto drop; - if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb, NULL, skb_dst(skb)->dev, dst_output); --=20 2.34.1