From patchwork Wed May 22 14:57:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vadim Fedorenko X-Patchwork-Id: 13670914 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) (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 6EB6E1CD16; Wed, 22 May 2024 14:57:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=67.231.153.30 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716389862; cv=none; b=oQgeVkQpSp0a3THbLs24m39zaqowijXZWDKDC9WGBjuhevqcmlaiKctaPDIbRrOa7Y20vGQKO6uckgdEFJB3FSHvdzXpd9RGhzjP1qiwQpUFO9Hel+0OVAAme7rsz5nJ+Sea6hhG1pauiFoz+Lck+d7TibnhMd6l3zpK+BRBhBU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716389862; c=relaxed/simple; bh=pQBNxtCwqRBfHoza8gVPTs0GuyS6y6tCfPhw6gYt354=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=SI0cdOQwx4qE3xwRnwnSdeF4ZAvsW7dZg/vtXYAGvxRLQAgdvq2hC7s1brxFLlceVUybbtRcjEiX6Pdx5c3s9WmBUD2qL3vU7Kko7fHqqJexBeqlBNXExmjCDocjO5QtAfvbGkbcvDhEZ8n8Mz9LY3G6KY7xNo9d6PdrBrg8QdI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com; spf=pass smtp.mailfrom=meta.com; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b=huZXihql; arc=none smtp.client-ip=67.231.153.30 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b="huZXihql" Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.17.1.19/8.17.1.19) with ESMTP id 44MAY754003497; Wed, 22 May 2024 07:57:22 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=meta.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=s2048-2021-q4; bh=TyrAxx8h6NEXxO7Pi0+xsoDGCi8mE5xhc9IsGWQSqow=; b=huZXihql1JAtHwyasgD22Nnt4F/3UYluzYvm3mBeUax7eYrbhwfvzJLPgbeQ9oyQw9Z6 DAl5XJpenyMMfvWi39oI3uuw0/yTXfqcsYBeP/I4d4xJUKC33GeucNYFbKw8o1VeHWoi 4V15vGLvPOxAPgLaGh4tR0eBqxzHWIwK9YVPI4S/KurSCcgcYOTQNcpRAgY5n4NVPm0D 8IhfRkKAXoXcI2PFrUBWTEPOphyrnV1r38weWywULoRYcNlaZgeQnb++pUc/leHY/MGo CJx2bejohhB9aRQrF0qlXL8hMTaaRemljWWZBJvTFsyLpK8nU57M1mXLhnxa0MtPMF4W Fg== Received: from maileast.thefacebook.com ([163.114.130.16]) by m0001303.ppops.net (PPS) with ESMTPS id 3y91bsnsv4-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Wed, 22 May 2024 07:57:21 -0700 Received: from devvm4158.cln0.facebook.com (2620:10d:c0a8:1c::1b) by mail.thefacebook.com (2620:10d:c0a9:6f::8fd4) with Microsoft SMTP Server id 15.2.1544.11; Wed, 22 May 2024 14:57:19 +0000 From: Vadim Fedorenko To: Vadim Fedorenko , Martin KaFai Lau , Andrii Nakryiko , "Alexei Starovoitov" , Mykola Lysenko , Jakub Kicinski CC: Vadim Fedorenko , , Subject: [PATCH bpf-next 1/2] bpf: add CHECKSUM_COMPLETE to bpf test progs Date: Wed, 22 May 2024 07:57:10 -0700 Message-ID: <20240522145712.3523593-1-vadfed@meta.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-GUID: pBLwyzx2-0S20J3Zgxk5dAvGL5BbwQHW X-Proofpoint-ORIG-GUID: pBLwyzx2-0S20J3Zgxk5dAvGL5BbwQHW X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.650,FMLib:17.12.28.16 definitions=2024-05-22_08,2024-05-22_01,2024-05-17_01 X-Patchwork-Delegate: bpf@iogearbox.net Add special flag to validate that TC BPF program properly updates checksum information in skb. Signed-off-by: Vadim Fedorenko --- include/uapi/linux/bpf.h | 2 ++ net/bpf/test_run.c | 19 ++++++++++++++++++- tools/include/uapi/linux/bpf.h | 2 ++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 90706a47f6ff..f7d458d88111 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1425,6 +1425,8 @@ enum { #define BPF_F_TEST_RUN_ON_CPU (1U << 0) /* If set, XDP frames will be transmitted after processing */ #define BPF_F_TEST_XDP_LIVE_FRAMES (1U << 1) +/* If set, apply CHECKSUM_COMPLETE to skb and validate the checksum */ +#define BPF_F_TEST_SKB_CHECKSUM_COMPLETE (1U << 2) /* type for BPF_ENABLE_STATS */ enum bpf_stats_type { diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c index f6aad4ed2ab2..841552785c65 100644 --- a/net/bpf/test_run.c +++ b/net/bpf/test_run.c @@ -974,10 +974,13 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, int hh_len = ETH_HLEN; struct sk_buff *skb; struct sock *sk; + __wsum csum; + __sum16 sum; void *data; int ret; - if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) + if ((kattr->test.flags & ~BPF_F_TEST_SKB_CHECKSUM_COMPLETE) || + kattr->test.cpu || kattr->test.batch_size) return -EINVAL; data = bpf_test_init(kattr, kattr->test.data_size_in, @@ -1025,6 +1028,12 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); __skb_put(skb, size); + + if (kattr->test.flags & BPF_F_TEST_SKB_CHECKSUM_COMPLETE) { + skb->csum = skb_checksum(skb, 0, skb->len, 0); + skb->ip_summed = CHECKSUM_COMPLETE; + } + if (ctx && ctx->ifindex > 1) { dev = dev_get_by_index(net, ctx->ifindex); if (!dev) { @@ -1079,6 +1088,14 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, } convert_skb_to___skb(skb, ctx); + if (kattr->test.flags & BPF_F_TEST_SKB_CHECKSUM_COMPLETE) { + csum = skb_checksum(skb, 0, skb->len, 0); + if (skb->csum != csum) { + ret = -EINVAL; + goto out; + } + } + size = skb->len; /* bpf program can never convert linear skb to non-linear */ if (WARN_ON_ONCE(skb_is_nonlinear(skb))) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 90706a47f6ff..f7d458d88111 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1425,6 +1425,8 @@ enum { #define BPF_F_TEST_RUN_ON_CPU (1U << 0) /* If set, XDP frames will be transmitted after processing */ #define BPF_F_TEST_XDP_LIVE_FRAMES (1U << 1) +/* If set, apply CHECKSUM_COMPLETE to skb and validate the checksum */ +#define BPF_F_TEST_SKB_CHECKSUM_COMPLETE (1U << 2) /* type for BPF_ENABLE_STATS */ enum bpf_stats_type { From patchwork Wed May 22 14:57:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vadim Fedorenko X-Patchwork-Id: 13670915 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) (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 ECE031CAA6; Wed, 22 May 2024 14:57:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=67.231.153.30 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716389862; cv=none; b=Vw/gw4yLnHVcXic6fLegZuv7/8r1UqAy9RBNRQaQqtamTg+ghmf8pLUvtv9hPjAxK7/e1mONtXrfka56wECGvU3uo+r7bfT4vWYakxYDx/WxEFK6l+aia1ydA6XK5EVtSqGFeDgwEga9pFaGTtGUZL21YR85rkTDp0cKi8VEOgg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716389862; c=relaxed/simple; bh=c0wTFKp9XQXRgUFgtFgR9AuRbrYxnV5EJTEpQx/c25k=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=tL/hOqom1RmdCPqt2mK0zt9ANSBps/CLUbBxzkpMYG3sLw8mnU3u+gIZOYbhogQDS1Lt6b802j/DFJPzQO/5twOrG9m7EjYLgfs4k/gidiX0M3ZXZItqe368lRokUnAs7w2ghbNJR7fh/my8Ahbpt+2RtbxARp5yqmCe7XxdBBs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com; spf=pass smtp.mailfrom=meta.com; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b=O0QK0w3o; arc=none smtp.client-ip=67.231.153.30 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b="O0QK0w3o" Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.17.1.19/8.17.1.19) with ESMTP id 44MAY755003497; Wed, 22 May 2024 07:57:22 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=meta.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=s2048-2021-q4; bh=7geuP4fh0kfWIQEG10bAFf8y5oNk1kPaQirayNWOM44=; b=O0QK0w3o+Ig3wp6GJ/1sgGcuip5ZgyPIwlnTZg5dm9cNtHxihavvwh2irhJCCcQkV22b zFvVZtwZ8xCwPZu4p+YKJrjEMfVOA77zw9sGChPzdUeowHfFZOcdvxYjCmM2RKXhXSHL XATVZicemE5umRbcbbE+deRwk0y01Gxhq6Ho/QD0BHDUb0WJFBodUmoVDaCD/IgF5zkW Aorw2huRwN8M6oKFMgx1Y/7GL+9LJKmAtBXvHUKzhl3ju8TAhPQj4FrFn4BIybOUCR8u DlYtQFw2Eqf6/Bb7AkZZfJl+Wx3pmVYK4mrlf/9S7/50+Pd1DTq678Ue2cWVYozUKFRP 0A== Received: from maileast.thefacebook.com ([163.114.130.16]) by m0001303.ppops.net (PPS) with ESMTPS id 3y91bsnsv4-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Wed, 22 May 2024 07:57:22 -0700 Received: from devvm4158.cln0.facebook.com (2620:10d:c0a8:1c::1b) by mail.thefacebook.com (2620:10d:c0a9:6f::8fd4) with Microsoft SMTP Server id 15.2.1544.11; Wed, 22 May 2024 14:57:20 +0000 From: Vadim Fedorenko To: Vadim Fedorenko , Martin KaFai Lau , Andrii Nakryiko , "Alexei Starovoitov" , Mykola Lysenko , Jakub Kicinski CC: Vadim Fedorenko , , Subject: [PATCH bpf-next 2/2] selftests: bpf: validate CHECKSUM_COMPLETE option Date: Wed, 22 May 2024 07:57:11 -0700 Message-ID: <20240522145712.3523593-2-vadfed@meta.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240522145712.3523593-1-vadfed@meta.com> References: <20240522145712.3523593-1-vadfed@meta.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-GUID: ZajzN6Xf9ajorOp7XU6yo_d6Y70BqQc2 X-Proofpoint-ORIG-GUID: ZajzN6Xf9ajorOp7XU6yo_d6Y70BqQc2 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.650,FMLib:17.12.28.16 definitions=2024-05-22_08,2024-05-22_01,2024-05-17_01 X-Patchwork-Delegate: bpf@iogearbox.net Adjust skb program test to run with checksum validation. Signed-off-by: Vadim Fedorenko --- .../selftests/bpf/prog_tests/test_skb_pkt_end.c | 1 + tools/testing/selftests/bpf/progs/skb_pkt_end.c | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/test_skb_pkt_end.c b/tools/testing/selftests/bpf/prog_tests/test_skb_pkt_end.c index ae93411fd582..09ca13bdf6ca 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_skb_pkt_end.c +++ b/tools/testing/selftests/bpf/prog_tests/test_skb_pkt_end.c @@ -11,6 +11,7 @@ static int sanity_run(struct bpf_program *prog) .data_in = &pkt_v4, .data_size_in = sizeof(pkt_v4), .repeat = 1, + .flags = BPF_F_TEST_SKB_CHECKSUM_COMPLETE, ); prog_fd = bpf_program__fd(prog); diff --git a/tools/testing/selftests/bpf/progs/skb_pkt_end.c b/tools/testing/selftests/bpf/progs/skb_pkt_end.c index db4abd2682fc..3bb4451524a1 100644 --- a/tools/testing/selftests/bpf/progs/skb_pkt_end.c +++ b/tools/testing/selftests/bpf/progs/skb_pkt_end.c @@ -33,6 +33,8 @@ int main_prog(struct __sk_buff *skb) struct iphdr *ip = NULL; struct tcphdr *tcp; __u8 proto = 0; + int urg_ptr; + u32 offset; if (!(ip = get_iphdr(skb))) goto out; @@ -48,7 +50,14 @@ int main_prog(struct __sk_buff *skb) if (!tcp) goto out; - return tcp->urg_ptr; + urg_ptr = tcp->urg_ptr; + + /* Checksum validation part */ + proto++; + offset = sizeof(struct ethhdr) + offsetof(struct iphdr, protocol); + bpf_skb_store_bytes(skb, offset, &proto, sizeof(proto), BPF_F_RECOMPUTE_CSUM); + + return urg_ptr; out: return -1; }