From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Mahe Tardy" <mahe.tardy@gmail.com>, <bpf@vger.kernel.org>
Cc: <andrii@kernel.org>, <daniel@iogearbox.net>,
<davem@davemloft.net>, <willemb@google.com>, <eddyz87@gmail.com>,
<edumazet@google.com>, <john.fastabend@gmail.com>,
<kuba@kernel.org>, <horms@kernel.org>, <liamwisehart@meta.com>,
<martin.lau@linux.dev>, <pabeni@redhat.com>, <song@kernel.org>,
<netdev@vger.kernel.org>, <sdf.kernel@gmail.com>,
<yonghong.song@linux.dev>, <jolsa@kernel.org>,
<kuniyu@google.com>, <memxor@gmail.com>, <emil@etsalapatis.com>,
<almagui@kernel.org>
Subject: Re: [PATCH bpf-next 0/8] bpf: Expand ksock send-path support
Date: Tue, 20 Oct 2026 15:07:31 +0000 [thread overview]
Message-ID: <DM9RCU3BDWEG.1C32ZIVU95S76@gmail.com> (raw)
In-Reply-To: <20261020102512.518479-1-mahe.tardy@gmail.com>
On Tue, Oct 20, 2026 at 03:25 AM Mahe Tardy <mahe.tardy@gmail.com> wrote:
> * allow bpf_ksock_connect() to be called in the send path
> * add bpf_ksock_sendto() kfunc to complement to bpf_ksock_send()
>
> This is useful when using ksock to reply to differents peers while
> reusing the same UDP socket and generally works toward the goal of ksock
> to support the basic socket operations.
can we drop connect from LSM progs and patches 1-5 altogether ?
bpf_ksock_sendto() alone replies to different peers on one socket.
connect + send from LSM progs can't do that reliably. The socket is
shared through the kptr and sleepable progs run concurrently. Between
bpf_ksock_connect(A) and bpf_ksock_send() on one CPU, another CPU does
bpf_ksock_connect(B) and the datagram for A goes to B.
Without connect there, acquire/release in socket_sendmsg has no user:
nothing can be done with that reference. bpf_ksock_sendto() is in
ksock_kfunc_btf_ids, so the current filter rejects it in socket_sendmsg
already. Then no need for bpf_ksock_send_id, bpf_ksock_sendto_id,
bpf_ksock_connect_id, bpf_lsm_socket_connect_id and the cycle comment.
Selftests are left out below. Patch 8 stays as is, patch 7 stays
without the socket_connect hunk.
Something like this (compile tested only):
diff --git a/net/core/bpf_ksock.c b/net/core/bpf_ksock.c
index 48a2e23..48757dc 100644
--- a/net/core/bpf_ksock.c
+++ b/net/core/bpf_ksock.c
@@ -37,6 +37,25 @@ static void ksock_release_work_fn(struct work_struct *work)
kfree(ks);
}
+static int bpf_ksock_addr_to_sockaddr(const union bpf_ksock_addr *addr,
+ u32 addr_sz, struct sockaddr_storage *sa)
+{
+ if (!addr || addr_sz != sizeof(*addr))
+ return -EINVAL;
+
+ /* Kfunc memory arguments may be unaligned. */
+ memcpy(sa, addr, sizeof(*addr));
+
+ switch (sa->ss_family) {
+ case AF_INET:
+ return sizeof(struct sockaddr_in);
+ case AF_INET6:
+ return sizeof(struct sockaddr_in6);
+ default:
+ return -EAFNOSUPPORT;
+ }
+}
+
__bpf_kfunc_start_defs();
/**
@@ -155,22 +174,9 @@ __bpf_kfunc int bpf_ksock_connect(struct bpf_ksock *ks,
struct sockaddr_storage sa;
int addrlen;
- if (!addr || addr__sz != sizeof(*addr))
- return -EINVAL;
-
- /* Kfunc memory arguments may be unaligned. */
- memcpy(&sa, addr, sizeof(*addr));
-
- switch (sa.ss_family) {
- case AF_INET:
- addrlen = sizeof(struct sockaddr_in);
- break;
- case AF_INET6:
- addrlen = sizeof(struct sockaddr_in6);
- break;
- default:
- return -EAFNOSUPPORT;
- }
+ addrlen = bpf_ksock_addr_to_sockaddr(addr, addr__sz, &sa);
+ if (addrlen < 0)
+ return addrlen;
return connect_socket(ks->sock, &sa, addrlen, 0);
}
@@ -212,19 +218,25 @@ __bpf_kfunc void bpf_ksock_release_dtor(void *ks)
CFI_NOSEAL(bpf_ksock_release_dtor);
/**
- * bpf_ksock_send() - Send data through a BPF kernel socket.
+ * bpf_ksock_sendto() - Send data through a BPF kernel socket to a remote address.
* @ks: The BPF kernel socket context. Must be an acquired reference.
* @data: Pointer to the data to send.
* @data__sz: Size of the data to send.
+ * @addr__nullable: Pointer to an IPv4 or IPv6 socket address, or NULL to use
+ * the connected destination.
+ * @addr__sz: Size of the address union, ignored when the address is NULL.
*
- * Sends data on a connected socket, best-effort and nonblocking. This may sleep
+ * Sends data to the specified address, or to the connected destination when
+ * the address is NULL, best-effort and nonblocking. This may sleep
* (kernel_sendmsg), so it can only be called from sleepable BPF programs.
*
* Return: Number of bytes sent on success, negative errno on error.
*/
-__bpf_kfunc int bpf_ksock_send(struct bpf_ksock *ks, const void *data,
- u32 data__sz)
+__bpf_kfunc int bpf_ksock_sendto(struct bpf_ksock *ks, const void *data, u32 data__sz,
+ const union bpf_ksock_addr *addr__nullable,
+ u32 addr__sz)
{
+ struct sockaddr_storage sa;
struct msghdr msg = {
.msg_flags = MSG_DONTWAIT,
};
@@ -232,11 +244,34 @@ __bpf_kfunc int bpf_ksock_send(struct bpf_ksock *ks, const void *data,
.iov_base = (void *)data,
.iov_len = data__sz,
};
- int ret;
+ int addrlen;
- ret = kernel_sendmsg(ks->sock, &msg, &iov, 1, data__sz);
+ if (addr__nullable) {
+ addrlen = bpf_ksock_addr_to_sockaddr(addr__nullable, addr__sz, &sa);
+ if (addrlen < 0)
+ return addrlen;
+ msg.msg_name = &sa;
+ msg.msg_namelen = addrlen;
+ }
- return ret;
+ return kernel_sendmsg(ks->sock, &msg, &iov, 1, data__sz);
+}
+
+/**
+ * bpf_ksock_send() - Send data through a BPF kernel socket.
+ * @ks: The BPF kernel socket context. Must be an acquired reference.
+ * @data: Pointer to the data to send.
+ * @data__sz: Size of the data to send.
+ *
+ * Sends data on a connected socket, best-effort and nonblocking. This may sleep
+ * (kernel_sendmsg), so it can only be called from sleepable BPF programs.
+ *
+ * Return: Number of bytes sent on success, negative errno on error.
+ */
+__bpf_kfunc int bpf_ksock_send(struct bpf_ksock *ks, const void *data,
+ u32 data__sz)
+{
+ return bpf_ksock_sendto(ks, data, data__sz, NULL, 0);
}
__bpf_kfunc_end_defs();
@@ -255,6 +290,7 @@ BTF_KFUNCS_START(ksock_kfunc_btf_ids)
BTF_ID_FLAGS(func, bpf_ksock_release, KF_RELEASE)
BTF_ID_FLAGS(func, bpf_ksock_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_ksock_send, KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_ksock_sendto, KF_SLEEPABLE)
BTF_KFUNCS_END(ksock_kfunc_btf_ids)
#ifdef CONFIG_BPF_LSM
pw-bot: cr
next prev parent reply other threads:[~2026-10-20 15:07 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-20 10:25 [PATCH bpf-next 0/8] bpf: Expand ksock send-path support Mahe Tardy
2026-10-20 10:25 ` [PATCH bpf-next 1/8] bpf: improve ksock_kfunc_set LSM recursion filter Mahe Tardy
2026-10-20 10:25 ` [PATCH bpf-next 2/8] selftests/bpf: Test allowed bpf_ksock operations at send LSM attach Mahe Tardy
2026-10-20 10:52 ` bot+bpf-ci
2026-10-20 11:06 ` Mahe Tardy
2026-10-20 10:25 ` [PATCH bpf-next 3/8] bpf: move ksock_connect() out of init kfunc set Mahe Tardy
2026-10-20 10:25 ` [PATCH bpf-next 4/8] selftests/bpf: Test verifier filter for ksock connect on LSM attach Mahe Tardy
2026-10-20 10:25 ` [PATCH bpf-next 5/8] selftests/bpf: Test ksock connect/send combinations Mahe Tardy
2026-10-20 10:25 ` [PATCH bpf-next 6/8] bpf: add bpf_ksock_sendto() kfunc Mahe Tardy
2026-10-20 10:25 ` [PATCH bpf-next 7/8] selftests/bpf: Test forbidden bpf_ksock_sendto() LSM attach Mahe Tardy
2026-10-20 10:52 ` bot+bpf-ci
2026-10-20 11:08 ` Mahe Tardy
2026-10-20 10:25 ` [PATCH bpf-next 8/8] selftests/bpf: Test ksock sendto() Mahe Tardy
2026-10-20 15:07 ` Alexei Starovoitov [this message]
2026-10-20 15:47 ` [PATCH bpf-next 0/8] bpf: Expand ksock send-path support Mahe Tardy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DM9RCU3BDWEG.1C32ZIVU95S76@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=almagui@kernel.org \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@google.com \
--cc=emil@etsalapatis.com \
--cc=horms@kernel.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=liamwisehart@meta.com \
--cc=mahe.tardy@gmail.com \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf.kernel@gmail.com \
--cc=song@kernel.org \
--cc=willemb@google.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox