From patchwork Mon Oct 7 09:59:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mahe Tardy X-Patchwork-Id: 13824407 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 5827718A6C3 for ; Mon, 7 Oct 2024 10:00:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728295218; cv=none; b=EKpD+6c8yIYHEstlrOUkodlovNUyY+3sB3EHBafgdY642NDfbq9EheKOBXO5Th8zfhH8l7XGj0eSQ9R2oV3PIvb03+zCRvXkB819MSmIYhNw2Q3tIXEtVdwqeZtQfiLiNE7MjXhgqbf0P1UW9ebxJuohd+czLBf+FuF2PTxhUY0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728295218; c=relaxed/simple; bh=cYo9V1UIltaO9ShHBWLec362vugAkhYmHURfIX94hFk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=j1l1s5+F75NKEpAlm+YJLadn9YZG+xOqTEO0pw0D4/FlZuzUxB+lbQ5wsaBm5TRLC+DLEF0mmi77NRGwjhS6gbkfiAQrL5mBoz72mMnXMZkE5eCNDJX3BRxuA0BAlSaI1OH1z5YUwHoqjy8/uIICuQCmB5lK1V4yE2t+VJxxAIk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cMcBQjT9; arc=none smtp.client-ip=209.85.221.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cMcBQjT9" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-37ccebd7f0dso2807930f8f.1 for ; Mon, 07 Oct 2024 03:00:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728295214; x=1728900014; 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=sll7JLEHgXGJR32nDZ0iI2qJz9B5qTzitcjl3kS5oJs=; b=cMcBQjT9qZcGczO63m6H8ZlH++kxXHqgIUd1eAmE29IPE7hDS4Cno67GmoAFnjrn8q FtNUVAG5KEuXSukcnpom5kMUuGMdC7+Lvq/5gV2iey86hA0OK2JwzmPd5DuocEm/Mdjj pr5R2u9frvLqEYpCpWhFH6fLBEDYNg/EkakTnRt/Z8ZF8XaQHyOjqzswWNBI41huJ67L bPSjvc+LpeD1pCuTytBfau3tt/ayOAXGOg6k3QC2lrKl3rEOOjlpIQKB7WghYb+Kqqz1 06mbhhHMxM0IfwtRXEQ4Dg8Z8hVltpb2okcT3sz9DFF4pK+6L+ig9hkZbcIP3Z6Kth3o PzMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728295214; x=1728900014; 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=sll7JLEHgXGJR32nDZ0iI2qJz9B5qTzitcjl3kS5oJs=; b=SXWPMTImY3vRGVpil4lveVtOBbP74Nwij+8vbtsO3lpsILcyGgFk6Qw3bckunAVzFy Si+8stxmMj7qFtHzZlDapxaqBc44qJyMYwczlgnoztxIOV0mrvj0NGV/tVwb7xlxNMOJ J61Uotr1r2O16FUHY01Wb6V4MuDOJ1vuGY7FAhmG2gIKRpaz+5g4SMBp/nftYYeiMVmU jw8Fj4AcAvK28fuo02+nEeZBJFiqlCxa8/wb7XWEigMr//zgewot/Ji4DEYIW3JoYe3n FgCQ+NR+D+0eeGtpO1aFCNHGxogRFsuJ3P7ULm7ck0DQQj3/WnrblaOtPCgyyXS7Cdhh 44Yw== X-Gm-Message-State: AOJu0YzxUNjCI5n809buVeIdFjHy5ClftyDjVfmUPW45BQrG0wq1JVhm vfGE/9+padVq7P8AVrvC/6Tf/oqSxR6/GzgiGcNhhSeoKTHCRlWsT2QeOrRyfLE= X-Google-Smtp-Source: AGHT+IEbdDZ8BOhqvMcnPZNbdNrTBLicZnCYEsE9j8scLfc12ZYINFIxKGXky2J41jJMsU8m2DulhA== X-Received: by 2002:adf:ce0e:0:b0:37c:d2be:6213 with SMTP id ffacd0b85a97d-37d0e6f2b03mr6670915f8f.21.1728295214308; Mon, 07 Oct 2024 03:00:14 -0700 (PDT) Received: from mtardy-friendly-lvh-runner.c.cilium-dev.internal (169.218.205.35.bc.googleusercontent.com. [35.205.218.169]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-37d1695e84fsm5341188f8f.75.2024.10.07.03.00.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 03:00:13 -0700 (PDT) From: Mahe Tardy To: bpf@vger.kernel.org Cc: martin.lau@linux.dev, daniel@iogearbox.net, john.fastabend@gmail.com, Mahe Tardy Subject: [PATCH bpf-next v3 1/2] bpf: add get_netns_cookie helper to tc programs Date: Mon, 7 Oct 2024 09:59:57 +0000 Message-Id: <20241007095958.97442-1-mahe.tardy@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net This is needed in the context of Cilium and Tetragon to retrieve netns cookie from hostns when traffic leaves Pod, so that we can correlate skb->sk's netns cookie. Signed-off-by: Mahe Tardy --- net/core/filter.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) -- 2.34.1 diff --git a/net/core/filter.c b/net/core/filter.c index cd3524cb326b..944bbe12a039 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5138,6 +5138,17 @@ static u64 __bpf_get_netns_cookie(struct sock *sk) return net->net_cookie; } +BPF_CALL_1(bpf_get_netns_cookie, struct sk_buff *, skb) +{ + return __bpf_get_netns_cookie(skb && skb->sk ? skb->sk : NULL); +} + +static const struct bpf_func_proto bpf_get_netns_cookie_proto = { + .func = bpf_get_netns_cookie, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX_OR_NULL, +}; + BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx) { return __bpf_get_netns_cookie(ctx); @@ -8209,6 +8220,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_skb_under_cgroup_proto; case BPF_FUNC_get_socket_cookie: return &bpf_get_socket_cookie_proto; + case BPF_FUNC_get_netns_cookie: + return &bpf_get_netns_cookie_proto; case BPF_FUNC_get_socket_uid: return &bpf_get_socket_uid_proto; case BPF_FUNC_fib_lookup: From patchwork Mon Oct 7 09:59:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mahe Tardy X-Patchwork-Id: 13824408 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 BB6A318B476 for ; Mon, 7 Oct 2024 10:00:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728295218; cv=none; b=jaD1F3nLWTAGtko0XmZju6bMCkLgGXJW0mmmGYION0bwXzbBroq9ahHyNscyDSDfjwcZqf5jGtrL8fM2yYKfCg6hVsoNuxCP7xNJDoRiPhOxniEi0ylykhndLpVUGimbRCReklWNglrQfklOG6L8bE9C+C/bLscdPl9y5j90niM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728295218; c=relaxed/simple; bh=FygPPuoSezLwu+yCFbVDYwFpbX1IFaDFIrbU7VNJdxI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=R6wmn7kdZD2PkRTjHzVPw+DDG3q2YaOrOmEg/cAerqRXZt5zEjDPH/5X1OMhjCSrOIKVN7BMpmKRzvWmPtVXCMMxgR7AP2Bov0JSvXVxm/k2/Kn4+Vch6lvBQl09z5xvEdMKAVgjqvZrczli6BkbZsd4cMEDdq7F9XnuZEDfEU4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=lpdqfHgk; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lpdqfHgk" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-42e5e758093so35983585e9.1 for ; Mon, 07 Oct 2024 03:00:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728295215; x=1728900015; 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=O207R4WqoktJZt+A3zTrnk0InCe8Hn4tCEWXqpWCt1s=; b=lpdqfHgkM45YbQTM/WR8PrP7/6RpIzWOXbCv/uFJCwU8Lax8QKp5rYC3fRCad7T0SE LWK+Fb5ygIXoXZsFS6vGM0COT+3dp37CoTARpYHztVEejsOVq3kA9XkAbSg86o8qDocw 8MK+9S8XbQ9iL6gyXxftjAi0Ou2PGWrxqnMpdgpy82fX6S5McgXfGTSylOxTg38Zt9o0 SZYZJ5MRXlA0hCcZzTbgfnPCxriuPFPMrijldl3t8S515YLuBuj5och48eMoZH1Z+H9Z 7XyoFRXd3zVPrSHUxeho7ggK36Gvx/A4WcgWgfc7Ay3LzLhBU3Ka81GaRuB385NqIEsd LjtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728295215; x=1728900015; 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=O207R4WqoktJZt+A3zTrnk0InCe8Hn4tCEWXqpWCt1s=; b=R98YXd5X93vseph84xa+hfLUnYceMs9MQSCBk56dL89DWR16HWPWTCtPE0ey4zNez5 +5wT74YWqluT39znCf7l/venyU/h/DEr9vLuyBlCc7k+GLs1E/ynoTMv7Kvb0XD+Q0XF Kot2J6zJD8WdYKMBnL0FJuD6J9eNAxXOkdIw+JokdugfnRadLOmsWbKLqXBYoHullO5H xmBxWz6WEDqCFvGoAyI3nConI1x090u/hyA8Mzy8AxpUrxHwyXllvO2L3+UWGmUvYjr5 67vaA+VnhuwCnb3UHnSwmgiE29Y5SbAoJph+N9Z5piF8jSKUk0lb3hUV2wj9nUBxiySt 43eQ== X-Gm-Message-State: AOJu0Yy72M2ykGl4ayZQskhgoGxMLugcQCwvn+lLXOfxjI+3rDBgyYik +JU+qhL29itopj66faYJTDUpZT260iyIWLsikBsi/RTMrxK78RqrNvXwgiDvFjM= X-Google-Smtp-Source: AGHT+IFrY056TsjSZ2YB5wW8WUsgqF8uUb1uc4LF+DMhwKZNgK52Etr63I/uyvyR41ee1f3Wa6gd0w== X-Received: by 2002:a05:600c:4449:b0:42c:bf70:a303 with SMTP id 5b1f17b1804b1-42f85af4635mr78128565e9.29.1728295214802; Mon, 07 Oct 2024 03:00:14 -0700 (PDT) Received: from mtardy-friendly-lvh-runner.c.cilium-dev.internal (169.218.205.35.bc.googleusercontent.com. [35.205.218.169]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-37d1695e84fsm5341188f8f.75.2024.10.07.03.00.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 03:00:14 -0700 (PDT) From: Mahe Tardy To: bpf@vger.kernel.org Cc: martin.lau@linux.dev, daniel@iogearbox.net, john.fastabend@gmail.com, Mahe Tardy Subject: [PATCH bpf-next v3 2/2] selftests/bpf: add tcx netns cookie tests Date: Mon, 7 Oct 2024 09:59:58 +0000 Message-Id: <20241007095958.97442-2-mahe.tardy@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241007095958.97442-1-mahe.tardy@gmail.com> References: <20241007095958.97442-1-mahe.tardy@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add netns cookie test that verifies the helper is now supported and work in the context of tc programs. Signed-off-by: Mahe Tardy --- .../selftests/bpf/prog_tests/netns_cookie.c | 29 ++++++++++++++----- .../selftests/bpf/progs/netns_cookie_prog.c | 10 +++++++ 2 files changed, 32 insertions(+), 7 deletions(-) -- 2.34.1 diff --git a/tools/testing/selftests/bpf/prog_tests/netns_cookie.c b/tools/testing/selftests/bpf/prog_tests/netns_cookie.c index 71d8f3ba7d6b..ac3c3c097c0e 100644 --- a/tools/testing/selftests/bpf/prog_tests/netns_cookie.c +++ b/tools/testing/selftests/bpf/prog_tests/netns_cookie.c @@ -8,12 +8,16 @@ #define SO_NETNS_COOKIE 71 #endif +#define loopback 1 + static int duration; void test_netns_cookie(void) { + LIBBPF_OPTS(bpf_prog_attach_opts, opta); + LIBBPF_OPTS(bpf_prog_detach_opts, optd); int server_fd = -1, client_fd = -1, cgroup_fd = -1; - int err, val, ret, map, verdict; + int err, val, ret, map, verdict, tc_fd; struct netns_cookie_prog *skel; uint64_t cookie_expected_value; socklen_t vallen = sizeof(cookie_expected_value); @@ -38,36 +42,47 @@ void test_netns_cookie(void) if (!ASSERT_OK(err, "prog_attach")) goto done; + tc_fd = bpf_program__fd(skel->progs.get_netns_cookie_tcx); + err = bpf_prog_attach_opts(tc_fd, loopback, BPF_TCX_INGRESS, &opta); + if (!ASSERT_OK(err, "prog_attach")) + goto done; + server_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0); if (CHECK(server_fd < 0, "start_server", "errno %d\n", errno)) - goto done; + goto cleanup_tc; client_fd = connect_to_fd(server_fd, 0); if (CHECK(client_fd < 0, "connect_to_fd", "errno %d\n", errno)) - goto done; + goto cleanup_tc; ret = send(client_fd, send_msg, sizeof(send_msg), 0); if (CHECK(ret != sizeof(send_msg), "send(msg)", "ret:%d\n", ret)) - goto done; + goto cleanup_tc; err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sockops_netns_cookies), &client_fd, &val); if (!ASSERT_OK(err, "map_lookup(sockops_netns_cookies)")) - goto done; + goto cleanup_tc; err = getsockopt(client_fd, SOL_SOCKET, SO_NETNS_COOKIE, &cookie_expected_value, &vallen); if (!ASSERT_OK(err, "getsockopt")) - goto done; + goto cleanup_tc; ASSERT_EQ(val, cookie_expected_value, "cookie_value"); err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sk_msg_netns_cookies), &client_fd, &val); if (!ASSERT_OK(err, "map_lookup(sk_msg_netns_cookies)")) - goto done; + goto cleanup_tc; ASSERT_EQ(val, cookie_expected_value, "cookie_value"); + ASSERT_EQ(skel->bss->tcx_init_netns_cookie, cookie_expected_value, "cookie_value"); + ASSERT_EQ(skel->bss->tcx_netns_cookie, cookie_expected_value, "cookie_value"); + +cleanup_tc: + err = bpf_prog_detach_opts(tc_fd, loopback, BPF_TCX_INGRESS, &optd); + ASSERT_OK(err, "prog_detach"); done: if (server_fd != -1) diff --git a/tools/testing/selftests/bpf/progs/netns_cookie_prog.c b/tools/testing/selftests/bpf/progs/netns_cookie_prog.c index aeff3a4f9287..c6edf8dbefeb 100644 --- a/tools/testing/selftests/bpf/progs/netns_cookie_prog.c +++ b/tools/testing/selftests/bpf/progs/netns_cookie_prog.c @@ -27,6 +27,8 @@ struct { __type(value, __u64); } sock_map SEC(".maps"); +int tcx_init_netns_cookie, tcx_netns_cookie; + SEC("sockops") int get_netns_cookie_sockops(struct bpf_sock_ops *ctx) { @@ -81,4 +83,12 @@ int get_netns_cookie_sk_msg(struct sk_msg_md *msg) return 1; } +SEC("tcx/ingress") +int get_netns_cookie_tcx(struct __sk_buff *skb) +{ + tcx_init_netns_cookie = bpf_get_netns_cookie(NULL); + tcx_netns_cookie = bpf_get_netns_cookie(skb); + return TCX_PASS; +} + char _license[] SEC("license") = "GPL";