From nobody Fri Nov 29 14:50:58 2024 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54A5916EBE8 for ; Wed, 18 Sep 2024 07:45:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726645548; cv=none; b=cqOmvD5eJnVOVo3gkbcPQY6tua3drGkgRqsdU4UTlcZKzXg7NB63k5BxgLcLM7Qd8AR5TgbRk3ON5Mcy2zG4LW1SB5qFvXJSe1HCfDgUthkS/J4FQL7gWpJj1WUzsUv0qMOMvjsLy1Zgebjdl+wSNkGfLUZhHq9UZUVvJ0ubxAU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726645548; c=relaxed/simple; bh=DydfF8Pc439Wt1w5M+gW9vt8fw8CSe77hlZ+Dv3Cw6w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T47pKGQhX3tketdDHjqdAXCMqFEAo7HbPsQ6XKP+KsKT7kTFuLwJuU0kfGDWAVs/b9xa+BJSg8fywUxYj4tb5DwBcCr0+fviEn3o4ddCyFLllzcQopPFijvUIUeFbE4W+Oip1ZGdspYppMQ3xNQ7eRBoNcE264iY4fRSYX/I1WY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=ARRtXAqB; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="ARRtXAqB" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2054e22ce3fso70522215ad.2 for ; Wed, 18 Sep 2024 00:45:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1726645546; x=1727250346; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nWqoVAK5c9pomygUoi1VenHzK4hkTh05ICoLnHqqtLI=; b=ARRtXAqBLB5lln5zEU7CA/T2/PCbYoMNvcefdCEVN7b3giGTdjP+X7ysrq8y+Ay1cc lfljyn2Ft/rK9KyWEjztTTw0nlcWWC34Gqsul0llDGtFrqnX97QiNAx03w4cLRvXwxkx 1eRnNxFcxWvRDfR7d5oWrSs7bij97KjQle4VyNHp20uCNtzEOTE0h92yjJ4bDAHTZKYl +qN70KdIg1HWJtxVFSx7QMXKMMAeiq7aHB44UZSnYyLUK8vgC/Cifs5anPAjQSvCcodb sIfrZci0VzyGcrK6ZD/smD5j3jeVdeUSiWfrVDrVbn1m1vQzRnpRa3SwBZaAmyYEL6O/ NTrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726645546; x=1727250346; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nWqoVAK5c9pomygUoi1VenHzK4hkTh05ICoLnHqqtLI=; b=uSLLKJQsj7stbrxxcbCslV1FV+6N/LzFfbWK+6I9lHBd51Bmqv1Vwkgnl5QMp+Nw3f MrDsQOow37ahGKCy3CalIu2LQGdt/C6/aLXZQoyQn9/vYD/gsN50Cr2BoqwhN6B1/kqI fsIX5LlLS73NsQqxh/wMdI4tDufXTgGzWnDFWF4QGHmYdwTbVnh2zhQwN4jOZtTMNSp3 9MYOKHJPMikNDGu7RTWPJegxYHvq0WoJCwK1JoZribx0v7HLNnQNdnGqmgS+3/hNYFcJ OU6fNDspq7CepD/z2T899NAxMyetFdHmIRV9kOT9zCuWDosbaHlwUh8BT7bQGoAU4kkD p/ag== X-Forwarded-Encrypted: i=1; AJvYcCX6h9ITrOCgbdENt1EbNtA26JhjfIbH9NeZ3vjS0AG3FB/DHpdRJXcHudOhBEETHrr4TXPPyiC4ojWYZsc=@vger.kernel.org X-Gm-Message-State: AOJu0Yyr6QYAMJP3gqX/ItAdsgSSHcsQNajpnNVrF8sENfoDuBPZ/bW0 ZaPJXq9Z0ZqfwWJKmscDZxqS3EZw0b3R23E6F+Hde/crOMhlPcwnFy9WWkmCgRc= X-Google-Smtp-Source: AGHT+IGVPp2n40PuG/8KbAxshSN8LnfmvU7GlDwe9HJB5DsmANQ6cspxjCvOOmjG4ThxTHfHWIy/5A== X-Received: by 2002:a17:903:26ce:b0:206:d66d:a30d with SMTP id d9443c01a7336-2076e3155bdmr241941755ad.6.1726645546538; Wed, 18 Sep 2024 00:45:46 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([203.208.167.149]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20794747379sm60412995ad.288.2024.09.18.00.45.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Sep 2024 00:45:46 -0700 (PDT) From: Feng zhou To: martin.lau@linux.dev, daniel@iogearbox.net, john.fastabend@gmail.com, ast@kernel.org, andrii@kernel.org, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mykolal@fb.com, shuah@kernel.org, geliang@kernel.org, laoar.shao@gmail.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, yangzhenze@bytedance.com, wangdongdong.6@bytedance.com, zhoufeng.zf@bytedance.com Subject: [PATCH bpf-next v2 1/2] bpf: cg_skb add get classid helper Date: Wed, 18 Sep 2024 15:45:14 +0800 Message-Id: <20240918074516.5697-2-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240918074516.5697-1-zhoufeng.zf@bytedance.com> References: <20240918074516.5697-1-zhoufeng.zf@bytedance.com> 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" From: Feng Zhou At cg_skb hook point, can get classid for v1 or v2, allowing users to do more functions such as acl. Signed-off-by: Feng Zhou --- net/core/filter.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/net/core/filter.c b/net/core/filter.c index e4a4454df5f9..a4aa39b6dbba 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -8115,6 +8115,10 @@ cg_skb_func_proto(enum bpf_func_id func_id, const st= ruct bpf_prog *prog) return &bpf_get_listener_sock_proto; case BPF_FUNC_skb_ecn_set_ce: return &bpf_skb_ecn_set_ce_proto; +#endif +#ifdef CONFIG_CGROUP_NET_CLASSID + case BPF_FUNC_skb_cgroup_classid: + return &bpf_skb_cgroup_classid_proto; #endif default: return sk_filter_func_proto(func_id, prog); --=20 2.30.2 From nobody Fri Nov 29 14:50:58 2024 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C2507170A0E for ; Wed, 18 Sep 2024 07:45:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726645557; cv=none; b=mr1/3sFrNkzU82CYhu6kab/O/1hLuh/bYxQgEUE4X76mC4tnaMpDTJ70C2EtQR9nIxbXSGfvO9jFxAECZM+MPPuFKwE4j9PJEhUPveB7Pu88oC3dbx2pYHYlFFkDPc394zT6ck9/qSNpBeeWssTlqKPlXfGrL7hGZoofxVOqkNk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726645557; c=relaxed/simple; bh=e0fPdTDwtnmoK4xu8HnZAvgPT5kDUB7AgygyE4ppIAA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dB0U8+xtB1YLhbNT/whcd3CbMvS3bmtHhDB7VF5zC4bhObfe78n4HW+584SV5w3KGDIvu9igWYkJntnHy3c1hTcH1tTpynpxcsLZvcx6wlZZXxKTy/jcsJKXa+Li7C+3Hq7OIKGFqd1aFohkTg0PpMCL1/9RZBLppxDmw5Qbjfs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=Rsqk4uOp; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="Rsqk4uOp" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-207115e3056so59099975ad.2 for ; Wed, 18 Sep 2024 00:45:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1726645555; x=1727250355; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=51Wax743nO5HpUDmJh8xGR9vIMqlrrkhnCJydTiox1Y=; b=Rsqk4uOpPvkSeIUizid0FrWFN/hcEGNMDFpkM4i5ddq8yddQDle/DwtR80sJgsavH3 ZigeZQOKEGL05d/J3n5jVlmtZ2Z1QJaLtpENFF7C2AUU3/yQdlq4xuso1mJPBYvJBW4S 5xzpZk1KpwqhXD9/gfqz6scpYIxluZNCN2EKWSF6hZLuZz7qyHzyBqxXAaZooLLno9f5 NHJjI4e50P1rVSrZmAlhimC6QP3P8UZkvQa42s2+8ur2nqmo39HfQvuSkecmwfrkCKEE edaafCkQrp7s4fad+KKOuhFQHEeKt3aoDhyqc0fGg4KCCOO748WClqke0ZtJhMdORhq3 vTzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726645555; x=1727250355; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=51Wax743nO5HpUDmJh8xGR9vIMqlrrkhnCJydTiox1Y=; b=eAVqcX4x4Ia96vtUIJt0nXK+FyS9t0nZA0usd/bn0nlsZsqr1IidmMtzVB5/Bkmle3 ufApyyvPcgJRfBpfCwj6Ada5rsQr4UF39OY1o9AWbQr3uvJkvXUF81cDEfsIzJPeykkV VicEve7cZNcgskaIy0O4apZj63d4caXIm2XKGvmMGG6IssXCega4gJRgmks+oLTD/Gtb hP8er3mLO5BAHurXQyHcJWvSiBFh88MAoFwQp/Cch0W8y4V1OVB+ar26SNbXmsJQuQD0 howWVl0+8jtY04+SI6R8L0c1vbCK8HBSu+vqK3rJIBLImmHGtJ8SIglhJweOMVsTu+cC +/Ig== X-Forwarded-Encrypted: i=1; AJvYcCXzU3l2VTCYZjBRtNZKnLc9EaswuUi4PYCnmElG915omheVOOupuS1A8RWN2Ty7ENUoY3i4/hXag8FxCwA=@vger.kernel.org X-Gm-Message-State: AOJu0YxibTGfLRRQatFl/W4SnpQpmCWxeW6RjAQizbAMO7+1IWB5ECcK BxKKJson4hG65S2k9a5fZeUWFa9FqNah2pd6tRStExrruumkvEIqiKZ1dBoBl3U= X-Google-Smtp-Source: AGHT+IGAGrQ7T/LTGDHFLHHxnP/Q17eKGNrI6ExFScNumI/xIpXTn7piGZ925jdHHFgVSgT8qy7eMw== X-Received: by 2002:a17:903:41cc:b0:207:6d2:2645 with SMTP id d9443c01a7336-2076e46177cmr318284595ad.47.1726645555086; Wed, 18 Sep 2024 00:45:55 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([203.208.167.149]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20794747379sm60412995ad.288.2024.09.18.00.45.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Sep 2024 00:45:54 -0700 (PDT) From: Feng zhou To: martin.lau@linux.dev, daniel@iogearbox.net, john.fastabend@gmail.com, ast@kernel.org, andrii@kernel.org, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mykolal@fb.com, shuah@kernel.org, geliang@kernel.org, laoar.shao@gmail.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, yangzhenze@bytedance.com, wangdongdong.6@bytedance.com, zhoufeng.zf@bytedance.com Subject: [PATCH bpf-next v2 2/2] bpf, selftests: Add test case for cgroup skb to get net_cls classid helpers Date: Wed, 18 Sep 2024 15:45:15 +0800 Message-Id: <20240918074516.5697-3-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240918074516.5697-1-zhoufeng.zf@bytedance.com> References: <20240918074516.5697-1-zhoufeng.zf@bytedance.com> 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" From: Feng Zhou This patch adds a test for cgroup skb to get classid. Signed-off-by: Feng Zhou --- .../bpf/prog_tests/cg_skb_get_classid.c | 87 +++++++++++++++++++ .../selftests/bpf/progs/cg_skb_get_classid.c | 19 ++++ 2 files changed, 106 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/cg_skb_get_class= id.c create mode 100644 tools/testing/selftests/bpf/progs/cg_skb_get_classid.c diff --git a/tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c b/= tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c new file mode 100644 index 000000000000..13a5943c387d --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/cg_skb_get_classid.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Copyright 2024 Bytedance. + */ + +#include + +#include "cg_skb_get_classid.skel.h" + +#include "cgroup_helpers.h" +#include "network_helpers.h" + +static int run_test(int cgroup_fd, int server_fd) +{ + struct cg_skb_get_classid *skel; + int fd, err =3D 0; + + skel =3D cg_skb_get_classid__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return -1; + + skel->links.cg_skb_classid =3D + bpf_program__attach_cgroup(skel->progs.cg_skb_classid, + cgroup_fd); + if (!ASSERT_OK_PTR(skel->links.cg_skb_classid, "prog_attach")) { + err =3D -1; + goto out; + } + + if (!ASSERT_OK(join_classid(), "join_classid")) { + err =3D -1; + goto out; + } + + errno =3D 0; + fd =3D connect_to_fd_opts(server_fd, NULL); + if (fd >=3D 0) { + if (skel->bss->classid !=3D getpid()) { + log_err("Get unexpected classid"); + err =3D -1; + } + + close(fd); + } else { + log_err("Unexpected errno from connect to server"); + err =3D -1; + } +out: + cg_skb_get_classid__destroy(skel); + return err; +} + +void test_cg_skb_get_classid(void) +{ + struct network_helper_opts opts =3D {}; + int server_fd, client_fd, cgroup_fd; + static const int port =3D 60120; + + /* Step 1: Check base connectivity works without any BPF. */ + server_fd =3D start_server(AF_INET, SOCK_STREAM, NULL, port, 0); + if (!ASSERT_GE(server_fd, 0, "server_fd")) + return; + client_fd =3D connect_to_fd_opts(server_fd, &opts); + if (!ASSERT_GE(client_fd, 0, "client_fd")) { + close(server_fd); + return; + } + close(client_fd); + close(server_fd); + + /* Step 2: Check BPF prog attached to cgroups. */ + cgroup_fd =3D test__join_cgroup("/cg_skb_get_classid"); + if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd")) + return; + server_fd =3D start_server(AF_INET, SOCK_STREAM, NULL, port, 0); + if (!ASSERT_GE(server_fd, 0, "server_fd")) { + close(cgroup_fd); + return; + } + setup_classid_environment(); + set_classid(); + ASSERT_OK(run_test(cgroup_fd, server_fd), "cg_skb_get_classid"); + cleanup_classid_environment(); + close(server_fd); + close(cgroup_fd); +} diff --git a/tools/testing/selftests/bpf/progs/cg_skb_get_classid.c b/tools= /testing/selftests/bpf/progs/cg_skb_get_classid.c new file mode 100644 index 000000000000..aef0265d24eb --- /dev/null +++ b/tools/testing/selftests/bpf/progs/cg_skb_get_classid.c @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Copyright 2024 Bytedance. + */ + +#include +#include +#include + +__u32 classid =3D 0; + +SEC("cgroup_skb/egress") +int cg_skb_classid(struct __sk_buff *ctx) +{ + classid =3D bpf_skb_cgroup_classid(ctx); + + return 1; +} --=20 2.30.2