From patchwork Wed Oct 2 17:57:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mahe Tardy X-Patchwork-Id: 13820187 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 395F41D1E73 for ; Wed, 2 Oct 2024 17:58:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727891890; cv=none; b=FUv3OXT4yBEq0+B19XQRlIJKTZVsvYz9ndGNux5BQV3IOiGt74AiDqNBlVRD0RbLO9zgH6noMw0sAgjnEAnrK6Xl2X5lZxb/ytpw+O1Mfb7KVw53cW243Mi/eqDJ8IK25IdKFZ/C7MCe3YHHIfn5QXdLCgBElg1zNJU2Qp56Mg4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727891890; c=relaxed/simple; bh=b6KvePfffFEKiu1M2rqJgK1IsIos6O71hpMN9iIyrUo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Dz2/LZJ9PrHrBIU1ms6VN14Ml9qYvNLukjWPKD2yGPbYtHbq/cKqrSQPrGdTqab6G7dU0SxI0fDxlSX8XmX8UGgcwbpS3jMVbg9nD7MoCmUrrn5YUfcGy8dS/R+nN6/qFw8TlNzMYDAFQGcEcgngM5ueTlHQlWc0VQ1IH8gZ7G4= 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=JsRi/526; arc=none smtp.client-ip=209.85.128.52 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="JsRi/526" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-42e7b7bef42so94155e9.3 for ; Wed, 02 Oct 2024 10:58:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727891887; x=1728496687; 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=h7mWAMXxlBASlbTkvKyCIwAQyyzMjvxLrTr0mAC83f0=; b=JsRi/526weWxhH24GO21jkd981i6+mycmNHLOD6XH6hX/b2iqW0AqK1HDqfPOa5boq H0rX+imvBeWXicZV9sdqcRGzfFvLXVo7eDb8H5bZwxssmqKWgT1Kx09yYxXCrqehxZtE Qze9q9H/Ism4Q3sVeb3SZttrhNDLl144Q6M8XF3mfkKrcuWwvowRrDLZYBzP1urzYRPD Yj/eM4KvMv97PkzjS131KjfOIafw2BhGh96PAn5tAJgbped6i8d1jyx57y9zvSZ2XnNu +ihmktYQv/vUO/seT8yw9eS0J/PNGgoHJ8xnqm8iWitDCTEbpbInb+QWZ7qAaXb6EBkp 73rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727891887; x=1728496687; 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=h7mWAMXxlBASlbTkvKyCIwAQyyzMjvxLrTr0mAC83f0=; b=EaJL8izRxoJ7as/FwGu20WFz8XUjEwbedMdXfy2NIs53rzzxm4pcl+y9MfzYtabqnG p0oC7BNd13FFtZSURREU7G9TNfVeTq+1cf8je1aWLE31Uyrwz0KZcymDCPQqT3WWb/A+ V+/0RQfpccLxbU/jdQ61ztUYDKxwTMyKlTx/EfZjInIgAlzjbnKogqXfZR/LhLaLWt9X vjoBCkMkUgB4D0CqNU8uNuCQcCM5R59jcDODC8eLXtsQNWUIKxm0HBBfAER8hFkB/jWC E7MTXmd9SgpHJpSX/IrCkWAv5I52lltivpSaeRnfX1k0/HYtpjFi2fo15oIW87TPFoHL LDZQ== X-Gm-Message-State: AOJu0YyEDZEgbRt49fYSjrON1QUv1ucbNIQx9DpyVxSvodNyUMWi5kuC omi/RWedm6wFf9VFDmfLFK2EeNK5+VNaY+nDi3y+cjBJNIgltk76Vvjp5jQlDqhm6g== X-Google-Smtp-Source: AGHT+IFpL8yB3pBsNMk6iByKyIPqQxTPZ21qoAAhNhRbbf7KD2L29RGScTTgR1KWFDRd3WguVkEAsw== X-Received: by 2002:a05:600c:3504:b0:42c:a802:a8cd with SMTP id 5b1f17b1804b1-42f777b72e1mr30435635e9.11.1727891887176; Wed, 02 Oct 2024 10:58:07 -0700 (PDT) Received: from mtardy-friendly-lvh-runner.c.cilium-dev.internal (114.73.211.130.bc.googleusercontent.com. [130.211.73.114]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-42f7a00bc5csm25616495e9.45.2024.10.02.10.58.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 10:58:06 -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 v2 1/2] bpf: add get_netns_cookie helper to tc programs Date: Wed, 2 Oct 2024 17:57:25 +0000 Message-Id: <20241002175726.304608-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..6e80991125ba 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->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 Wed Oct 2 17:57:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mahe Tardy X-Patchwork-Id: 13820188 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 B835E1D1E7C for ; Wed, 2 Oct 2024 17:58:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727891891; cv=none; b=B42V0rgwFFOhWvMlipSOI1lyY2Oy5NMJGKfvN07zgGcrXtJHaLQGcdGQK/ZOg3wW4VjNDe6NQ6ltntIbpDmu7mUmhghFndxCCztADf9u/wwho0XTuNub9m2aXc7phO54dZ5d9wS16wxnAL+hPtd1DQA0lTBO4hhWThYw0XxovWw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727891891; c=relaxed/simple; bh=ARccJMCiTtTfWzlmhDUm4xi8p0VIp4X3nP38O8rlkjQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NKD5a2kFmr13tyjR56CQDBuB6tVJQQViSqRbHkbAHyL+qt8m2ky7jfLL36Mjf2MA1ilySLXiIXjURfX5/cQM4WFE2PIqCbMcKL2W9YIyEQzMmL4r55izxf67xSCdhAk92jEISetNhKuTJVxvs+2h0GbuMhpzWgDuXGVISKmsn1w= 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=AZsS8Bof; arc=none smtp.client-ip=209.85.128.45 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="AZsS8Bof" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-42cbface8d6so204425e9.3 for ; Wed, 02 Oct 2024 10:58:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727891888; x=1728496688; 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=aUzWevPH/3NOwhroQAK/SlMUjSktsgPIJ0EELVj5jkc=; b=AZsS8BofxuGCZNvUIHNtLLzh1aVddDtmaOvqyTmuySGSYME7d59kili6tbcBYphmXR e/6uwtYGp61rQuk2V33gIaEj2jetimAGR5SJSUBhYwbbgmMJ75SLsl0zlrhOjCdlY8zW YA6NmtkU/1lf/Teh1iR5FMgxOHXBebdB9y9dGNe5pkxRk+AdgavPCw3UjACOaYN5IiWi pVhvMN6VfVwCQVDIhQ5swzvkZZYdCZcTxWXyLYWgTBGo2kwPyVvmqQkxqLwCnDgDa9PG EaugurbMsj+2tggOYzvG1o8V8Yu+09jkzR/U5qAQbKJLWXBNywWmo03SZ6Z9ezYBH2ON JMJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727891888; x=1728496688; 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=aUzWevPH/3NOwhroQAK/SlMUjSktsgPIJ0EELVj5jkc=; b=UxDSmhI1NLj86esgPvnKgxWAiy9WEm8v0xHFtksTIoDL3wbjZIZ9RkW8QNRUS3PFkg I1+crFVuscHsVAWTHDcq7GL/K/TYxAG8BAEDZZ1ROEZB/0gUV/tU5IrgOWwvJz4kgAwm 5aGDokKRgFudenCaChlDtD+JOE8BHMyKb18vaozwnQ9jfUR1njFw4vACqk6wJy7WOqmH nlWj7Y6d8+tbLXMYyyleK/OOOf5oRC9E9QRzkU34tGMf7Cy0yvWqsynbzvngXl6HcIXp rNyPcNQAwxLfKA6WA5OEO45+H8wlTy22GzNh8sw9jhFdCeZBa0ld376FqefgXMl09PC9 3BHg== X-Gm-Message-State: AOJu0YxF1Sse+UEZ34IAC0RASHbnMwUl6XClf6RYriTil/zg92VsiXXM yHM2/9NH4/X34adfVxca25TupX5vWWJoIFFHA3nAnXv7NeMAaUsa3hjvwAhe0WZe7w== X-Google-Smtp-Source: AGHT+IEokQKAE/+0iw/sHElfQHlg7ytryID1hK73DxfXWXh3X1srWf9pD64j0E4ZeEUZrJ0UuTe9sA== X-Received: by 2002:a05:600c:4587:b0:42c:bae0:f05f with SMTP id 5b1f17b1804b1-42f777ba357mr35959605e9.13.1727891887808; Wed, 02 Oct 2024 10:58:07 -0700 (PDT) Received: from mtardy-friendly-lvh-runner.c.cilium-dev.internal (114.73.211.130.bc.googleusercontent.com. [130.211.73.114]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-42f7a00bc5csm25616495e9.45.2024.10.02.10.58.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 10:58:07 -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 v2 2/2] selftests/bpf: add tcx netns cookie tests Date: Wed, 2 Oct 2024 17:57:26 +0000 Message-Id: <20241002175726.304608-2-mahe.tardy@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241002175726.304608-1-mahe.tardy@gmail.com> References: <20241002175726.304608-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 | 28 ++++++++++++++----- .../selftests/bpf/progs/netns_cookie_prog.c | 9 ++++++ 2 files changed, 30 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..a014082d1e09 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,46 @@ 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_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..207f0e6c20b7 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_netns_cookie; + SEC("sockops") int get_netns_cookie_sockops(struct bpf_sock_ops *ctx) { @@ -81,4 +83,11 @@ 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_netns_cookie = bpf_get_netns_cookie(skb); + return TCX_PASS; +} + char _license[] SEC("license") = "GPL";