From patchwork Wed Oct 2 16:01:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mahe Tardy X-Patchwork-Id: 13820009 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 A89CF79F5 for ; Wed, 2 Oct 2024 16:01:30 +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=1727884892; cv=none; b=pU5gibWHSlTs1Am+P/S2bRBcoDHpKJWawkd0SURLmUL7vWaoK/8Z+BHk7sEh0HGAwOVClE3zvMbPRHUc6Qp7fXCPAW4PUPFaXVcVI0TvevP8fL0axucXgDu1uu9mYOyJxwr96+d5U8WNNOo0Xe8vaSvxvM0OieEu0Ku4OSKExss= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727884892; c=relaxed/simple; bh=b6KvePfffFEKiu1M2rqJgK1IsIos6O71hpMN9iIyrUo=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=MaapPa+0S/CMWy1ag9UiN3dvdgJyKtzsacV4KO8AonxVdsapY2r1BV0CVZVkX2VXdW2jHX1hmNe+xJ7mfbsU3YXktcUSFmJICtjMN34tlrRpfMuRWTIs9I1puHxVYxzkzB/R6Gh35F6MqcgCj1bY5XQaGZxBRBrTyZCwtZqX9qc= 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=I/dDn2e0; 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="I/dDn2e0" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-37cea34cb57so2177833f8f.0 for ; Wed, 02 Oct 2024 09:01:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727884889; x=1728489689; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=h7mWAMXxlBASlbTkvKyCIwAQyyzMjvxLrTr0mAC83f0=; b=I/dDn2e0XI5ZFuc758zGcCPg4s6T7Dve/V5N7ySVfArSJIRcihqSXzawKJUd8phfll FJNFkUa3WHhZfyVlvu4woZ15fnKI4gy+n5f7/a2smc6Xqxe3bTWdvR/xyFifWvryjoTL n7fActvOQSl/s7yTS0jOpEW/p1DVin6jp9VNVWv/kJ2rtQ2xTQsTYHoOGbE1l1rtEBs3 Cu5BPYxuOyE/gUIWZgoF3qpXgYOtDKQ26oXo3NsHea5Qq0AJuQ37WeMnaHRt94V1Tw6Y HSxTK+FdI3/FBFpsRyu0+9rT7/x9qwp4KNclDmwNqqgm2U+R16AE5BAVGYSLB0X5qXSZ uZug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727884889; x=1728489689; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=h7mWAMXxlBASlbTkvKyCIwAQyyzMjvxLrTr0mAC83f0=; b=ww8VDJUH0ryM54RJLu2OQ/nI0KIFkeolZJXc9msAzMcndDMq1h5siQd1fjQ3PM6GLq 4xKD1CET/2VxyTgsWQ+ATlvbBT5+yA5HXj1sF3/TV7UJBmcLH605dlWSKBCyP7x2cuut Szhie7At5wkg23KJhPJjfv8uazI62py/6fGVhwJsBxIjtazH1Z4GyaSUHbWvf3HczwFe uz7pwObtfT9kQEm5jP/VIkjtPQ+18ah57KbsnZodlsedzSIxjbtXizQq00pjWwal8JpL sLNsXvENtw93QXS9sbwKGewHPQwwNadhG5DD6fJbjREMspO0hLUArLm8/iW4nXbRFnn5 1uXw== X-Gm-Message-State: AOJu0YwFiuLrawcSN4wSSM/ncFxRGhxxxfTYeOAtI+MntdP+T5PuhIXB RtN+0xVoF+HN8eEFuVPrxuuR3MhROcYS/qMBTWuWrQBawJmy8+0XT1qYBs5+g5qQmw== X-Google-Smtp-Source: AGHT+IEsTrvkIYp2s+z27+zfKgeee1eb2O/eGLDXG06qWBxsVrm9zlji5rEnOslIaziEeASG0EO91Q== X-Received: by 2002:adf:f70c:0:b0:378:8dea:4bee with SMTP id ffacd0b85a97d-37cfb8d0a68mr2173249f8f.33.1727884888394; Wed, 02 Oct 2024 09:01:28 -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 ffacd0b85a97d-37cd56e65c3sm14193982f8f.60.2024.10.02.09.01.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 09:01:28 -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 1/2] bpf: add get_netns_cookie helper to tc programs Date: Wed, 2 Oct 2024 16:01:21 +0000 Message-Id: <20241002160122.148980-1-mahe.tardy@gmail.com> X-Mailer: git-send-email 2.34.1 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 16:01:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mahe Tardy X-Patchwork-Id: 13820010 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) (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 7703E1CFEA5 for ; Wed, 2 Oct 2024 16:01:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727884893; cv=none; b=L2UD2QOEdT3e8zOOs7FLcAx8OZUqvuhi1GM0B093LQG4GHvHe13HqYRgroJUApXvBfO0xSmFQtpzsVVIQVxkZyR2H+OjOyYGNOJAcfufoKOb8wZoL8ehJqUuDSJwG3195bJ0fW3j3jcWw25xaTbkdvT535fO4hFsVZvE8QyFhyc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727884893; c=relaxed/simple; bh=BtnsRGPA8XOiZYbKnNCisjXiOnbN+quxIWmQj1Mw1w8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ikHzNDiOfuu+V+q8YY/NeDEXRaK8nYrNnDaNLpOTof/bMFz3T1m3qPgoGJ7iMXs0IwycldhSBi7pokBej1Q3ay1VuLSVZTFT+3dDHiUDLQSj63urdN7oqSYqfYk55hxM5ClBa2pWi0Aky1Dy61566xYsweQreD1o+2gwCCAkoWg= 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=bRmG1PAr; arc=none smtp.client-ip=209.85.167.41 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="bRmG1PAr" Received: by mail-lf1-f41.google.com with SMTP id 2adb3069b0e04-5389917ef34so8111380e87.2 for ; Wed, 02 Oct 2024 09:01:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727884889; x=1728489689; 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=wgb/2NooKv77QEDz66ed5qIFDIWE50J5diu8JTbvXBk=; b=bRmG1PAr2a1dotACImMeHxkzMZI747G+DzfRSZ9hUBK5mEbSEvSv2krtfGD6YBYzHF ftXD8ZGJVo1BBW0Fpfz8RGGRNm4/f7j9XljeXzJHv2hICy5B40snWoSJuuzDP2idpzyc jcmJAH84I4JW/SI6ULyA3eUKjUs8DpfUL95cvBE5g771NFOxcFmYIgJpzE6K4SaQBJ/J FMFBeMOp0RyEzByB8enBMVuZph5wK90ApJVcS3iMvPANJdxhCcz5fbYcdmjn1gHYWW8I vZzSHsTcd8RzBfdiYphHRG+iYp48t6DMGvMhcVREP3smCm0gxc+rsUfiruwsHx/9jdP7 bY9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727884889; x=1728489689; 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=wgb/2NooKv77QEDz66ed5qIFDIWE50J5diu8JTbvXBk=; b=oow/IZ1aksaGNP3Fxl2VGfu39YGyoU8rXjrTAVzW1AmCk/4ouTUR75a5NyTqXTw/np oY92vUdGbV9grOD3pFntLqgq3PsZcJxeRwb+QMfWPuWCnsD2+erQvG9IZcl0wSjxTGnO 1AjaWPV16SD8lEUwbzSKBMI3Pz0HnFJiyFDxCC23sZMh1OVzt5TSXPvwUOJ6OU1FEmIc 4QyyhkEh+lNO0J7D1hHe9/vNPRh2+QPocf9pAvFY2QwlT8EnzaZv8eiuZIbQ2FHDwAXo itm83Wa+Jak7X2gMvzm/dEI90dWZl7MqFDWZSzL40LYdAg2P6JB/Q7+YLcyHP7LgCRM/ bV0Q== X-Gm-Message-State: AOJu0Yy5FK7q1YRWynvJ1u9TBldFjmANualIC0ZRdt/hMHlEWphpv+iz xQWBwq3D8IEWloilD3Eon5Mma0JKrmSny4LHWSRQJRmSd2L6291y3rBCsGUmG0aCyQ== X-Google-Smtp-Source: AGHT+IH/3nidgN05YUBIxXJ33tpMQW/fkNj5XmyMQ5iRgH7NaGcAMxZSVFmOPM8l48K4pt9nHl21QQ== X-Received: by 2002:a05:6512:b18:b0:52c:dfa0:dca0 with SMTP id 2adb3069b0e04-539a0793cd9mr2273204e87.43.1727884889009; Wed, 02 Oct 2024 09:01:29 -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 ffacd0b85a97d-37cd56e65c3sm14193982f8f.60.2024.10.02.09.01.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 09:01:28 -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 2/2] selftests/bpf: add tcx netns cookie tests Date: Wed, 2 Oct 2024 16:01:22 +0000 Message-Id: <20241002160122.148980-2-mahe.tardy@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241002160122.148980-1-mahe.tardy@gmail.com> References: <20241002160122.148980-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 --- tools/testing/selftests/bpf/prog_tests/netns_cookie.c | 7 +++++++ tools/testing/selftests/bpf/progs/netns_cookie_prog.c | 9 +++++++++ 2 files changed, 16 insertions(+) -- 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..233fd66f59ee 100644 --- a/tools/testing/selftests/bpf/prog_tests/netns_cookie.c +++ b/tools/testing/selftests/bpf/prog_tests/netns_cookie.c @@ -12,6 +12,7 @@ static int duration; void test_netns_cookie(void) { + LIBBPF_OPTS(bpf_prog_attach_opts, opta); int server_fd = -1, client_fd = -1, cgroup_fd = -1; int err, val, ret, map, verdict; struct netns_cookie_prog *skel; @@ -38,6 +39,11 @@ void test_netns_cookie(void) if (!ASSERT_OK(err, "prog_attach")) goto done; + verdict = bpf_program__fd(skel->progs.get_netns_cookie_tcx); + err = bpf_prog_attach_opts(verdict, 1, BPF_TCX_INGRESS, &opta); + if (!ASSERT_EQ(err, 0, "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; @@ -68,6 +74,7 @@ void test_netns_cookie(void) goto done; ASSERT_EQ(val, cookie_expected_value, "cookie_value"); + ASSERT_EQ(skel->bss->tcx_netns_cookie, cookie_expected_value, "cookie_value"); 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";