From patchwork Thu Mar 17 11:39:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Sitnicki X-Patchwork-Id: 12783902 X-Patchwork-Delegate: bpf@iogearbox.net 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 B68E2C43217 for ; Thu, 17 Mar 2022 11:39:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233003AbiCQLkl (ORCPT ); Thu, 17 Mar 2022 07:40:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230054AbiCQLkk (ORCPT ); Thu, 17 Mar 2022 07:40:40 -0400 Received: from mail-lf1-x135.google.com (mail-lf1-x135.google.com [IPv6:2a00:1450:4864:20::135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFCBF1E5200 for ; Thu, 17 Mar 2022 04:39:23 -0700 (PDT) Received: by mail-lf1-x135.google.com with SMTP id p6so1317483lfh.1 for ; Thu, 17 Mar 2022 04:39:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MQgZAkq+ORk9ENUakuwLhhkNNQ+vKaeY9GX3+UUqx8k=; b=lgYhAxitjoyP+MJwFELpUSNbr8ad29xlnUpzRY8b2rQo6nwLeV3DG6J2YYEscVC+0O 3bQzAZ6ea8OK8y3HsC4fhd/uaX28aQmYPHLiHh9TLcl1utGyj9rW1c5+KuWZfegXkzGg HN0JDauYgZDF2uT8ijyHIgCGNxEjyQF6KbNrw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=MQgZAkq+ORk9ENUakuwLhhkNNQ+vKaeY9GX3+UUqx8k=; b=UiUNFqRUSCTRvxqd1djSAgocv1LaM6Z8oxyyIlzvqlClzcvENO6/NLytLrbu7Zm4PG qRrpvX5hKiM21JgNplBxMAbSNcq6NJNHbHNl65ASsHubgRgmra/9MWnV3o7c6NMCiaWv UscMmLNrVIdsxMlEQKGfgzcP+o61v9TGhq9tAyJq0ZWvyJ3l+b5cjNoIbpHV/xcejtPM 6SvlohIxNpDbnHBJ44s2AIFPXx7Gc3ucgMwJTCJIhwSqmG8Ma8x7VyT3ixzs5RPRB5Bx y7RYsQWqBSb/NRYo6mkB0GCHYWiYWTdq2vRGOz5/OKdyK9/+VjvwGeTDY6aNQpQv5a6e 7u4A== X-Gm-Message-State: AOAM533+vh5fpBPZICFAcb3CzNEguPwFwS26AGhzfa5wZRPq4o1e/Yed +lGxOuoiPjVySFVW7MuMNjlygA== X-Google-Smtp-Source: ABdhPJwfy++rrH/kaz3cZwg2wuOC4ouKNCNoiW9xSTCYcKcSHsSmiXt5+r4wDAgNoKotDCAuf1HfTQ== X-Received: by 2002:a05:6512:2027:b0:448:bdb3:a238 with SMTP id s7-20020a056512202700b00448bdb3a238mr2654501lfs.266.1647517162095; Thu, 17 Mar 2022 04:39:22 -0700 (PDT) Received: from cloudflare.com ([2a01:110f:4809:d800::f9c]) by smtp.gmail.com with ESMTPSA id 206-20020a2e09d7000000b00247eb27d491sm395404ljj.103.2022.03.17.04.39.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Mar 2022 04:39:21 -0700 (PDT) From: Jakub Sitnicki To: bpf@vger.kernel.org Cc: netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , kernel-team@cloudflare.com, Ilya Leoshkevich , Martin KaFai Lau Subject: [PATCH bpf-next v3 1/4] selftests/bpf: Fix error reporting from sock_fields programs Date: Thu, 17 Mar 2022 12:39:17 +0100 Message-Id: <20220317113920.1068535-2-jakub@cloudflare.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220317113920.1068535-1-jakub@cloudflare.com> References: <20220317113920.1068535-1-jakub@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The helper macro that records an error in BPF programs that exercise sock fields access has been inadvertently broken by adaptation work that happened in commit b18c1f0aa477 ("bpf: selftest: Adapt sock_fields test to use skel and global variables"). BPF_NOEXIST flag cannot be used to update BPF_MAP_TYPE_ARRAY. The operation always fails with -EEXIST, which in turn means the error never gets recorded, and the checks for errors always pass. Revert the change in update flags. Fixes: b18c1f0aa477 ("bpf: selftest: Adapt sock_fields test to use skel and global variables") Acked-by: Martin KaFai Lau Signed-off-by: Jakub Sitnicki --- tools/testing/selftests/bpf/progs/test_sock_fields.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/progs/test_sock_fields.c b/tools/testing/selftests/bpf/progs/test_sock_fields.c index 246f1f001813..3e2e3ee51cc9 100644 --- a/tools/testing/selftests/bpf/progs/test_sock_fields.c +++ b/tools/testing/selftests/bpf/progs/test_sock_fields.c @@ -114,7 +114,7 @@ static void tpcpy(struct bpf_tcp_sock *dst, #define RET_LOG() ({ \ linum = __LINE__; \ - bpf_map_update_elem(&linum_map, &linum_idx, &linum, BPF_NOEXIST); \ + bpf_map_update_elem(&linum_map, &linum_idx, &linum, BPF_ANY); \ return CG_OK; \ }) From patchwork Thu Mar 17 11:39:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Sitnicki X-Patchwork-Id: 12783903 X-Patchwork-Delegate: bpf@iogearbox.net 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 88E4FC433EF for ; Thu, 17 Mar 2022 11:39:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233060AbiCQLkm (ORCPT ); Thu, 17 Mar 2022 07:40:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233011AbiCQLkl (ORCPT ); Thu, 17 Mar 2022 07:40:41 -0400 Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E13651E3E39 for ; Thu, 17 Mar 2022 04:39:24 -0700 (PDT) Received: by mail-lj1-x232.google.com with SMTP id h11so6848982ljb.2 for ; Thu, 17 Mar 2022 04:39:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6cIvA5fpC+1DumvjmukuahH9VsbyXJK8ay6q2GqRmP4=; b=cn48Ouj69DPYuZLYMon7O7fwvUqM2b0ec32EnA0eZRwgGJqyyptVZyg229V6aXcTYa OjFgI1IndQwf2rvt0xYKGD+lsotTmeYcnS8B4qdsGLbQidBs6uZnIerk+wsR/5spIYNa QM99hwuArD1DBrjkPX3eY9A+JsnS865I/qjzs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6cIvA5fpC+1DumvjmukuahH9VsbyXJK8ay6q2GqRmP4=; b=Ov9qA2IgwNfhONLbttCS7ZXyH3y52qep8/KXsGHTvDvtSUbHXlP0wlhBD99NYTs7o+ jxXFV7v0ssVALFbqICckwKQj5XZ+nDQ2A8jpcpQKkbGXCLfdkGBSMVErfJ8gHBpGqrEf NDPr/aXnbSioeY/roZDK70Cw2DhQQM7/ZQ0qkI2xrgMgRP+LXUwBk6K46gW2nnSDcDDz MnBantlTab6HWAJ2yL9c8jvTDzE7ngGgE09gBBGF+0nrGMGB4HKwyCB0zFQEfyecDb/J LyVPvmvWYOXeHuXiowbt3lIN/T3JxCVhplajLswoW8uO15xXQNF6tMByEKmJFhm2L0x8 VCQQ== X-Gm-Message-State: AOAM530B2NQmoE+Cc2Pvropx1BZCTnpVcr46CEJfTEqd7QI9irFHCqCV BEFQ5oU2O84QwfUeeJ4F/w54PQ== X-Google-Smtp-Source: ABdhPJzn9RvbWs4yOtGQDUQGDqIej9Wn7dFkMFASqdSoewsqDfGJlBj4R7m2Dzu4EcAfHFOCMFh/Wg== X-Received: by 2002:a2e:b706:0:b0:247:ff35:cfd7 with SMTP id j6-20020a2eb706000000b00247ff35cfd7mr2493655ljo.510.1647517163048; Thu, 17 Mar 2022 04:39:23 -0700 (PDT) Received: from cloudflare.com ([2a01:110f:4809:d800::f9c]) by smtp.gmail.com with ESMTPSA id f14-20020a056512092e00b004423570c03asm425012lft.287.2022.03.17.04.39.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Mar 2022 04:39:22 -0700 (PDT) From: Jakub Sitnicki To: bpf@vger.kernel.org Cc: netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , kernel-team@cloudflare.com, Ilya Leoshkevich , Martin KaFai Lau Subject: [PATCH bpf-next v3 2/4] selftests/bpf: Check dst_port only on the client socket Date: Thu, 17 Mar 2022 12:39:18 +0100 Message-Id: <20220317113920.1068535-3-jakub@cloudflare.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220317113920.1068535-1-jakub@cloudflare.com> References: <20220317113920.1068535-1-jakub@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net cgroup_skb/egress programs which sock_fields test installs process packets flying in both directions, from the client to the server, and in reverse direction. Recently added dst_port check relies on the fact that destination port (remote peer port) of the socket which sends the packet is known ahead of time. This holds true only for the client socket, which connects to the known server port. Filter out any traffic that is not egressing from the client socket in the BPF program that tests reading the dst_port. Fixes: 8f50f16ff39d ("selftests/bpf: Extend verifier and bpf_sock tests for dst_port loads") Signed-off-by: Jakub Sitnicki --- tools/testing/selftests/bpf/progs/test_sock_fields.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/test_sock_fields.c b/tools/testing/selftests/bpf/progs/test_sock_fields.c index 3e2e3ee51cc9..43b31aa1fcf7 100644 --- a/tools/testing/selftests/bpf/progs/test_sock_fields.c +++ b/tools/testing/selftests/bpf/progs/test_sock_fields.c @@ -281,6 +281,10 @@ int read_sk_dst_port(struct __sk_buff *skb) if (!sk) RET_LOG(); + /* Ignore everything but the SYN from the client socket */ + if (sk->state != BPF_TCP_SYN_SENT) + return CG_OK; + if (!sk_dst_port__load_word(sk)) RET_LOG(); if (!sk_dst_port__load_half(sk)) From patchwork Thu Mar 17 11:39:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Sitnicki X-Patchwork-Id: 12783904 X-Patchwork-Delegate: bpf@iogearbox.net 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 DE36EC43217 for ; Thu, 17 Mar 2022 11:39:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233072AbiCQLkv (ORCPT ); Thu, 17 Mar 2022 07:40:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233044AbiCQLkm (ORCPT ); Thu, 17 Mar 2022 07:40:42 -0400 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98F141E3E22 for ; Thu, 17 Mar 2022 04:39:25 -0700 (PDT) Received: by mail-lf1-x133.google.com with SMTP id w12so8491634lfr.9 for ; Thu, 17 Mar 2022 04:39:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=al55RwOac+/z9BBt+f6IKfG92+gHay1WckoruFKyL+E=; b=MKV1gMjokNAMx+1BuKa6Cs9I6ohJP6o3FEP3ch6dRlvZk7AzLx90cM8XlR2BX5OdF0 BG9FQzMtZVbSv6b2YQWPUG56ELF8DuawKNouWUf/8v7t5KxSNhIRXM/jusetCmrQ7yYQ YTL5KtKt2IiAHd+jN9up5Xlnx0sgjYDPNoeqo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=al55RwOac+/z9BBt+f6IKfG92+gHay1WckoruFKyL+E=; b=hqMosngcZBuG1zOCuUh8kqpqHJbbzDoa+OUVRkbIVuohbdvju39hgoBkNSf35D9pZ9 vfXHdrNAU7lDewpYxbE2c4V0RgcTvxtJ8CiXLiWgR8aXpNwAb/eJq+lJu8fA2NXyZ53b G214XvllXJftWd/94HVH7DI4C7OpSoWJG90Mpyh7oa4HsgTmVPxkzTRwRat61DjoLMbw FOa48SguKMRIp3TDqA6Js0us+EdEaHUp2Pq8HIBtrfSQW9feMOAPkpHR7GzV9NSnA6tY HhOZ9KLpk16OnIbLCwbRWN7rQoAmPO04eFSKqHbMxd3DObXm+SkSBt5JMP5v0lgR3Jmz 0oxA== X-Gm-Message-State: AOAM531uLGCTy+aDIgUeZRPllFVDqVS8rCjXzE7Yd6Kq+6/vHiCh3hqh NoiBWtKAWRmX+OHikNaPmKR1vg== X-Google-Smtp-Source: ABdhPJyDGHOeK9PboPorQnDj2A7ABx/II7JKZ/K9Fk4pu/tDlrlEcCL8skcXftGW4jME2XyexcoDbQ== X-Received: by 2002:a05:6512:68e:b0:449:fb65:c532 with SMTP id t14-20020a056512068e00b00449fb65c532mr1162481lfe.386.1647517163970; Thu, 17 Mar 2022 04:39:23 -0700 (PDT) Received: from cloudflare.com ([2a01:110f:4809:d800::f9c]) by smtp.gmail.com with ESMTPSA id t25-20020ac243b9000000b0044824f72b16sm430873lfl.80.2022.03.17.04.39.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Mar 2022 04:39:23 -0700 (PDT) From: Jakub Sitnicki To: bpf@vger.kernel.org Cc: netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , kernel-team@cloudflare.com, Ilya Leoshkevich , Martin KaFai Lau Subject: [PATCH bpf-next v3 3/4] selftests/bpf: Use constants for socket states in sock_fields test Date: Thu, 17 Mar 2022 12:39:19 +0100 Message-Id: <20220317113920.1068535-4-jakub@cloudflare.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220317113920.1068535-1-jakub@cloudflare.com> References: <20220317113920.1068535-1-jakub@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Replace magic numbers in BPF code with constants from bpf.h, so that they don't require an explanation in the comments. Signed-off-by: Jakub Sitnicki --- tools/testing/selftests/bpf/progs/test_sock_fields.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/bpf/progs/test_sock_fields.c b/tools/testing/selftests/bpf/progs/test_sock_fields.c index 43b31aa1fcf7..43a17fdef226 100644 --- a/tools/testing/selftests/bpf/progs/test_sock_fields.c +++ b/tools/testing/selftests/bpf/progs/test_sock_fields.c @@ -134,11 +134,11 @@ int egress_read_sock_fields(struct __sk_buff *skb) if (!sk) RET_LOG(); - /* Not the testing egress traffic or - * TCP_LISTEN (10) socket will be copied at the ingress side. + /* Not testing the egress traffic or the listening socket, + * which are covered by the cgroup_skb/ingress test program. */ if (sk->family != AF_INET6 || !is_loopback6(sk->src_ip6) || - sk->state == 10) + sk->state == BPF_TCP_LISTEN) return CG_OK; if (sk->src_port == bpf_ntohs(srv_sa6.sin6_port)) { @@ -232,8 +232,8 @@ int ingress_read_sock_fields(struct __sk_buff *skb) sk->src_port != bpf_ntohs(srv_sa6.sin6_port)) return CG_OK; - /* Only interested in TCP_LISTEN */ - if (sk->state != 10) + /* Only interested in the listening socket */ + if (sk->state != BPF_TCP_LISTEN) return CG_OK; /* It must be a fullsock for cgroup_skb/ingress prog */ From patchwork Thu Mar 17 11:39:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Sitnicki X-Patchwork-Id: 12783905 X-Patchwork-Delegate: bpf@iogearbox.net 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 752DEC433EF for ; Thu, 17 Mar 2022 11:39:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233089AbiCQLku (ORCPT ); Thu, 17 Mar 2022 07:40:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233011AbiCQLkt (ORCPT ); Thu, 17 Mar 2022 07:40:49 -0400 Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8630C1E5217 for ; Thu, 17 Mar 2022 04:39:26 -0700 (PDT) Received: by mail-lj1-x229.google.com with SMTP id y17so6781180ljd.12 for ; Thu, 17 Mar 2022 04:39:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gDhbf6FjOQGMDCKaqwepB50GDaXw7puLrLCj6LY2Vos=; b=jtZQSvWCYXcRu1ik+ACl7fK4pju9Jc1SHBubB9gFWB8JkLseTCOqR6VJ6cXdH5hkIm KkdyX62hYV2U0+cZUrCT29heGXSIH9JQEtDZbzFTbM4YUlLrSntJlbqUtELMfudZ/E8i EIWsigdUIJD04YH8gsfnJRH6lSQs3be3ne4K4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gDhbf6FjOQGMDCKaqwepB50GDaXw7puLrLCj6LY2Vos=; b=piNtXtsUzC/jkyJ9GP67FIyY3eawhKFKGaTy7oAp8mz+OwPYRH/THAdpnVnO1LQtyT TWhXMBgQEvaSpGf2s3q28oq9DdHcR3qYbPLJQId7WoIwVYuxAOH8R8nx3zH8PvjpHAO1 5O3aPrtc9a1K2ORcl4HMZ68NTxW5scqVcVXJrQtu80oqRILWO3aPc2oqOE8JXGmEUK3g XLV9u6LDXPnqdBfRwdazSlcJxaVrVXQn8Vd//6+cWcgrC/D/yrF5++cu/NGp22ewAeyE 4IO4nyiqSP3yQJwHD1uWnrMOS+ilbNWdu4yAtrqhVniKcdyoLYaB+7gJ8AyB5ZN/YLXT s3UA== X-Gm-Message-State: AOAM533hsk7baiKeILCmQO9hQCkJuMTobkdRTNmRyKIXXOVzsx5cTTP9 /8ysa6cGKYZhI+XM5hsO45sXig== X-Google-Smtp-Source: ABdhPJw3apKHdVOMzgB87LZ/8nBEL7E0nIS19wrE53QAZiGONL1j33utcKU1lfXm2y0NDsummUJvCw== X-Received: by 2002:a2e:97c8:0:b0:248:542:6a2f with SMTP id m8-20020a2e97c8000000b0024805426a2fmr2579327ljj.417.1647517164883; Thu, 17 Mar 2022 04:39:24 -0700 (PDT) Received: from cloudflare.com ([2a01:110f:4809:d800::f9c]) by smtp.gmail.com with ESMTPSA id m21-20020a197115000000b0044895f0608asm428123lfc.37.2022.03.17.04.39.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Mar 2022 04:39:24 -0700 (PDT) From: Jakub Sitnicki To: bpf@vger.kernel.org Cc: netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , kernel-team@cloudflare.com, Ilya Leoshkevich , Martin KaFai Lau Subject: [PATCH bpf-next v3 4/4] selftests/bpf: Fix test for 4-byte load from dst_port on big-endian Date: Thu, 17 Mar 2022 12:39:20 +0100 Message-Id: <20220317113920.1068535-5-jakub@cloudflare.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220317113920.1068535-1-jakub@cloudflare.com> References: <20220317113920.1068535-1-jakub@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The check for 4-byte load from dst_port offset into bpf_sock is failing on big-endian architecture - s390. The bpf access converter rewrites the 4-byte load to a 2-byte load from sock_common at skc_dport offset, as shown below. * s390 / llvm-objdump -S --no-show-raw-insn 00000000000002a0 : 84: r1 = *(u32 *)(r1 + 48) 85: w0 = 1 86: if w1 == 51966 goto +1 87: w0 = 0 00000000000002c0 : 88: exit * s390 / bpftool prog dump xlated _Bool sk_dst_port__load_word(struct bpf_sock * sk): 35: (69) r1 = *(u16 *)(r1 +12) 36: (bc) w1 = w1 37: (b4) w0 = 1 38: (16) if w1 == 0xcafe goto pc+1 39: (b4) w0 = 0 40: (95) exit * x86_64 / llvm-objdump -S --no-show-raw-insn 00000000000002a0 : 84: r1 = *(u32 *)(r1 + 48) 85: w0 = 1 86: if w1 == 65226 goto +1 87: w0 = 0 00000000000002c0 : 88: exit * x86_64 / bpftool prog dump xlated _Bool sk_dst_port__load_word(struct bpf_sock * sk): 33: (69) r1 = *(u16 *)(r1 +12) 34: (b4) w0 = 1 35: (16) if w1 == 0xfeca goto pc+1 36: (b4) w0 = 0 37: (95) exit This leads to surprises if we treat the destination register contents as a 32-bit value, ignoring the fact that in reality it contains a 16-bit value. On little-endian the register contents reflect the bpf_sock struct definition, where the lower 16-bits contain the port number: struct bpf_sock { ... __be16 dst_port; /* offset 48 */ __u16 :16; ... }; However, on big-endian the register contents suggest that field the layout of bpf_sock struct is as so: struct bpf_sock { ... __u16 :16; /* offset 48 */ __be16 dst_port; ... }; Account for this quirky access conversion in the test case exercising the 4-byte load by treating the result as 16-bit wide. Signed-off-by: Jakub Sitnicki --- tools/testing/selftests/bpf/progs/test_sock_fields.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/progs/test_sock_fields.c b/tools/testing/selftests/bpf/progs/test_sock_fields.c index 43a17fdef226..9f4b8f9f1181 100644 --- a/tools/testing/selftests/bpf/progs/test_sock_fields.c +++ b/tools/testing/selftests/bpf/progs/test_sock_fields.c @@ -251,10 +251,16 @@ int ingress_read_sock_fields(struct __sk_buff *skb) return CG_OK; } +/* + * NOTE: 4-byte load from bpf_sock at dst_port offset is quirky. It + * gets rewritten by the access converter to a 2-byte load for + * backward compatibility. Treating the load result as a be16 value + * makes the code portable across little- and big-endian platforms. + */ static __noinline bool sk_dst_port__load_word(struct bpf_sock *sk) { __u32 *word = (__u32 *)&sk->dst_port; - return word[0] == bpf_htonl(0xcafe0000); + return word[0] == bpf_htons(0xcafe); } static __noinline bool sk_dst_port__load_half(struct bpf_sock *sk)