From nobody Mon Sep 29 21:20:49 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D7F9C00140 for ; Mon, 15 Aug 2022 23:23:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243614AbiHOXXb (ORCPT ); Mon, 15 Aug 2022 19:23:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344690AbiHOXR5 (ORCPT ); Mon, 15 Aug 2022 19:17:57 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DAD27CB7F; Mon, 15 Aug 2022 13:03:45 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 204FBB80EB1; Mon, 15 Aug 2022 20:03:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A63EC433C1; Mon, 15 Aug 2022 20:03:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660593822; bh=abYdGz99G+a8NjFTk64PqU1lQ3mOkmqE+lQJmz80CzY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KN1TY495V2gEAj+8IVrUzc00RpeuYY6V82QWSSyQcrn9wzNFFRHi91OZRadEbKkug NWiEBjTncStkjBfyXFqKVbpwArbavTuhFQauruON08E91OB4JYMZleqTLTNZ3k765V Nrz2BNozZOmt5EHq3rXrGD6cr1yoMTo6KTaVMCnA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrii Nakryiko , Martin KaFai Lau , Song Liu , Sasha Levin Subject: [PATCH 5.19 0331/1157] selftests/bpf: Fix tc_redirect_dtime Date: Mon, 15 Aug 2022 19:54:47 +0200 Message-Id: <20220815180452.905032391@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Martin KaFai Lau [ Upstream commit e6ff92f41b65fce07365f1066fb13b5e42aca08d ] tc_redirect_dtime was reported flaky from time to time. It always fails at the udp test and complains about the bpf@tc-ingress got a skb->tstamp when handling udp packet. It is unexpected because the skb->tstamp should have been cleared when crossing different netns. The most likely cause is that the skb is actually a tcp packet from the earlier tcp test. It could be the final TCP_FIN handling. This patch tightens the skb->tstamp check in the bpf prog. It ensures the skb is the current testing traffic. First, it checks that skb matches the IPPROTO of the running test (i.e. tcp vs udp). Second, it checks the server port (dst_ns_port). The server port is unique for each test (50000 + test_enum). Also fixed a typo in test_udp_dtime(): s/P100/P101/ Fixes: c803475fd8dd ("bpf: selftests: test skb->tstamp in redirect_neigh") Reported-by: Andrii Nakryiko Signed-off-by: Martin KaFai Lau Signed-off-by: Andrii Nakryiko Acked-by: Song Liu Link: https://lore.kernel.org/bpf/20220601234050.2572671-1-kafai@fb.com Signed-off-by: Sasha Levin --- .../selftests/bpf/prog_tests/tc_redirect.c | 8 +-- .../selftests/bpf/progs/test_tc_dtime.c | 53 ++++++++++++++++++- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/tc_redirect.c b/tools/t= esting/selftests/bpf/prog_tests/tc_redirect.c index 958dae769c52..cb6a53b3e023 100644 --- a/tools/testing/selftests/bpf/prog_tests/tc_redirect.c +++ b/tools/testing/selftests/bpf/prog_tests/tc_redirect.c @@ -646,7 +646,7 @@ static void test_tcp_clear_dtime(struct test_tc_dtime *= skel) __u32 *errs =3D skel->bss->errs[t]; =20 skel->bss->test =3D t; - test_inet_dtime(AF_INET6, SOCK_STREAM, IP6_DST, 0); + test_inet_dtime(AF_INET6, SOCK_STREAM, IP6_DST, 50000 + t); =20 ASSERT_EQ(dtimes[INGRESS_FWDNS_P100], 0, dtime_cnt_str(t, INGRESS_FWDNS_P100)); @@ -683,7 +683,7 @@ static void test_tcp_dtime(struct test_tc_dtime *skel, = int family, bool bpf_fwd) errs =3D skel->bss->errs[t]; =20 skel->bss->test =3D t; - test_inet_dtime(family, SOCK_STREAM, addr, 0); + test_inet_dtime(family, SOCK_STREAM, addr, 50000 + t); =20 /* fwdns_prio100 prog does not read delivery_time_type, so * kernel puts the (rcv) timetamp in __sk_buff->tstamp @@ -715,13 +715,13 @@ static void test_udp_dtime(struct test_tc_dtime *skel= , int family, bool bpf_fwd) errs =3D skel->bss->errs[t]; =20 skel->bss->test =3D t; - test_inet_dtime(family, SOCK_DGRAM, addr, 0); + test_inet_dtime(family, SOCK_DGRAM, addr, 50000 + t); =20 ASSERT_EQ(dtimes[INGRESS_FWDNS_P100], 0, dtime_cnt_str(t, INGRESS_FWDNS_P100)); /* non mono delivery time is not forwarded */ ASSERT_EQ(dtimes[INGRESS_FWDNS_P101], 0, - dtime_cnt_str(t, INGRESS_FWDNS_P100)); + dtime_cnt_str(t, INGRESS_FWDNS_P101)); for (i =3D EGRESS_FWDNS_P100; i < SET_DTIME; i++) ASSERT_GT(dtimes[i], 0, dtime_cnt_str(t, i)); =20 diff --git a/tools/testing/selftests/bpf/progs/test_tc_dtime.c b/tools/test= ing/selftests/bpf/progs/test_tc_dtime.c index 06f300d06dbd..b596479a9ebe 100644 --- a/tools/testing/selftests/bpf/progs/test_tc_dtime.c +++ b/tools/testing/selftests/bpf/progs/test_tc_dtime.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include #include @@ -115,6 +117,19 @@ static bool bpf_fwd(void) return test < TCP_IP4_RT_FWD; } =20 +static __u8 get_proto(void) +{ + switch (test) { + case UDP_IP4: + case UDP_IP6: + case UDP_IP4_RT_FWD: + case UDP_IP6_RT_FWD: + return IPPROTO_UDP; + default: + return IPPROTO_TCP; + } +} + /* -1: parse error: TC_ACT_SHOT * 0: not testing traffic: TC_ACT_OK * >0: first byte is the inet_proto, second byte has the netns @@ -122,11 +137,16 @@ static bool bpf_fwd(void) */ static int skb_get_type(struct __sk_buff *skb) { + __u16 dst_ns_port =3D __bpf_htons(50000 + test); void *data_end =3D ctx_ptr(skb->data_end); void *data =3D ctx_ptr(skb->data); __u8 inet_proto =3D 0, ns =3D 0; struct ipv6hdr *ip6h; + __u16 sport, dport; struct iphdr *iph; + struct tcphdr *th; + struct udphdr *uh; + void *trans; =20 switch (skb->protocol) { case __bpf_htons(ETH_P_IP): @@ -138,6 +158,7 @@ static int skb_get_type(struct __sk_buff *skb) else if (iph->saddr =3D=3D ip4_dst) ns =3D DST_NS; inet_proto =3D iph->protocol; + trans =3D iph + 1; break; case __bpf_htons(ETH_P_IPV6): ip6h =3D data + sizeof(struct ethhdr); @@ -148,15 +169,43 @@ static int skb_get_type(struct __sk_buff *skb) else if (v6_equal(ip6h->saddr, (struct in6_addr)ip6_dst)) ns =3D DST_NS; inet_proto =3D ip6h->nexthdr; + trans =3D ip6h + 1; break; default: return 0; } =20 - if ((inet_proto !=3D IPPROTO_TCP && inet_proto !=3D IPPROTO_UDP) || !ns) + /* skb is not from src_ns or dst_ns. + * skb is not the testing IPPROTO. + */ + if (!ns || inet_proto !=3D get_proto()) return 0; =20 - return (ns << 8 | inet_proto); + switch (inet_proto) { + case IPPROTO_TCP: + th =3D trans; + if (th + 1 > data_end) + return -1; + sport =3D th->source; + dport =3D th->dest; + break; + case IPPROTO_UDP: + uh =3D trans; + if (uh + 1 > data_end) + return -1; + sport =3D uh->source; + dport =3D uh->dest; + break; + default: + return 0; + } + + /* The skb is the testing traffic */ + if ((ns =3D=3D SRC_NS && dport =3D=3D dst_ns_port) || + (ns =3D=3D DST_NS && sport =3D=3D dst_ns_port)) + return (ns << 8 | inet_proto); + + return 0; } =20 /* format: direction@iface@netns --=20 2.35.1