bpf: Add bpf_ksock_sendto() kfunc
Subsystems (6)
v2 cover bpf: Add bpf_ksock_sendto() kfunc
This patch series improves the bpf_ksock infrastructure in line with the work proposed during Prague Plumbers 2026 presentation[^1] by adding the bpf_ksock_sendto() kfunc to complement the 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.
[^1]: https://lpc.events/event/20/contributions/2430/
v2 updates:
- remove the LSM verifier filter refinements (alexei)
- remove allowing calling connect() from the send path (alexei)
- change order of the tests patches for signature definition (sashiko)
v1: https://lore.kernel.org/bpf/[email protected]/
Mahe Tardy (3):
bpf: add bpf_ksock_sendto() kfunc
selftests/bpf: Test ksock sendto()
selftests/bpf: Test forbidden bpf_ksock_sendto() LSM attach
net/core/bpf_ksock.c | 96 +++++++++++----- .../selftests/bpf/prog_tests/ksock_sendto.c | 108 ++++++++++++++++++ .../selftests/bpf/progs/ksock_common.h | 2 + .../selftests/bpf/progs/ksock_lsm_verifier.c | 25 +++- .../selftests/bpf/progs/ksock_sendto.c | 59 ++++++++++ 5 files changed, 262 insertions(+), 28 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/ksock_sendto.c create mode 100644 tools/testing/selftests/bpf/progs/ksock_sendto.c
--
2.43.0
PATCH v2 1/3 bpf: add bpf_ksock_sendto() kfunc
The motivation is to plumb more of the socket API into ksock, thus supporting sendto() in addition to send() which makes a lot of sense in the context of UDP sockets which are the only type currently supported. This is useful when ksock is used to reply to specific addresses or send to multiple addresses.
--- net/core/bpf_ksock.c | 96 +++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 27 deletions(-)
net/core/bpf_ksock.c +69 -27
@@ -37,6 +37,49 @@ 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 ; + } +} + +static int ksock_sendto (struct bpf_ksock *ks,const void *data,u32 data_sz, +const union bpf_ksock_addr *addr,u32 addr_sz) +{ +struct sockaddr_storage sa; +struct msghdr msg = { + .msg_flags =MSG_DONTWAIT , + }; +struct kvec iov = { + .iov_base = (void *)data, + .iov_len = data_sz, + }; +int addrlen; + +if (addr) { + addrlen =bpf_ksock_addr_to_sockaddr (addr, addr_sz, &sa); +if (addrlen <0 ) +return addrlen; + msg.msg_name = &sa; + msg.msg_namelen = addrlen; + } + +return kernel_sendmsg (ks->sock, &msg, &iov,1 , data_sz); +} +__bpf_kfunc_start_defs ();/**
@@ -155,22 +198,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 ); }
@@ -225,18 +255,29 @@ CFI_NOSEAL(bpf_ksock_release_dtor); __bpf_kfunc int bpf_ksock_send(struct bpf_ksock *ks, const void *data,u32 data__sz) { -struct msghdr msg = { - .msg_flags =MSG_DONTWAIT , - }; -struct kvec iov = { - .iov_base = (void *)data, - .iov_len = data__sz, - }; -int ret; - - ret =kernel_sendmsg (ks->sock, &msg, &iov,1 , data__sz); +return ksock_sendto (ks, data, data__sz,NULL ,0 ); +} -return ret; +/** +* 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 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_sendto(struct bpf_ksock *ks, const void *data, u32 data__sz, +const union bpf_ksock_addr *addr__nullable, +u32 addr__sz) +{ +return ksock_sendto (ks, data, data__sz, addr__nullable, addr__sz); }__bpf_kfunc_end_defs ();
@@ -255,6 +296,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
--
2.43.0
PATCH v2 2/3 selftests/bpf: Test ksock sendto()
In reply to Mahe TardyThese tests are covering that:
1. sendto() on a unconnected UDP socket works (and that the socket stays unconnected, i.e. send() on it fails with -EDESTADDRREQ).
2. sendto() on a connected UDP socket with a NULL address is equivalent to send().
3. sendto() on a connected socket with an explicit address doesn't change the peer of the socket (send() after sendto() still sends to the original connected address).
--- .../selftests/bpf/prog_tests/ksock_sendto.c | 108 ++++++++++++++++++ .../selftests/bpf/progs/ksock_common.h | 2 + .../selftests/bpf/progs/ksock_sendto.c | 59 ++++++++++ 3 files changed, 169 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/ksock_sendto.c create mode 100644 tools/testing/selftests/bpf/progs/ksock_sendto.c
tools/testing/selftests/bpf/prog_tests/ksock_sendto.c new file +108 -0
@@ -0,0 +1,108 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Isovalent */ + +#include "test_progs.h" +#include "network_helpers.h" +#include "ksock_sendto.skel.h" + +struct sendto_test_case { +const char *name; +bool connected; +bool use_sendto_addr; +}; + +static bool recv_payload (int fd,const void *expected,size_t len) +{ +char buf[64 ]; +ssize_t n; + + n =recv (fd, buf,sizeof (buf),MSG_TRUNC ); +if (!ASSERT_EQ (n, len,"recv length" )) +return false; +return ASSERT_MEMEQ (buf, expected, len,"recv payload" ); +} + +static void test_sendto_case (const struct sendto_test_case *test) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts); +struct ksock_sendto *skel; +int receivers[2 ] = {-1 ,-1 }; +int err, i, port, sendto_fd; +char buf[64 ]; +ssize_t n; + + skel =ksock_sendto__open_and_load (); +if (!ASSERT_OK_PTR (skel,"ksock_sendto open and load" )) +return ; + +/* Use separate receivers for the default peer and explicit destination. */ +for (i =0 ; i <ARRAY_SIZE (receivers); i++) { + receivers[i] =start_server (AF_INET ,SOCK_DGRAM ,"127.0.0.1" ,0 ,1000 ); +if (!ASSERT_OK_FD (receivers[i],"receiver socket" )) + goto out; + } + + port =get_socket_local_port (receivers[0 ]); +if (!ASSERT_GE (port,0 ,"connected port" )) + goto out; + skel->bss->connected_port = port; + port =get_socket_local_port (receivers[1 ]); +if (!ASSERT_GE (port,0 ,"sendto port" )) + goto out; + skel->bss->sendto_port = port; + skel->bss->connected = test->connected; + skel->bss->use_sendto_addr = test->use_sendto_addr; + + err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.test_sendto), &opts); +if (!ASSERT_OK (err,"run test_sendto" ) || !ASSERT_OK (opts.retval,"test_sendto retval" )) + goto out; + +/* Check sendto() outcome */ +if (!ASSERT_EQ (skel->data->sendto_ret,sizeof (skel->data->sendto_data),"sendto bytes" )) + goto out; + sendto_fd = receivers[test->use_sendto_addr ?1 :0 ]; +if (!recv_payload (sendto_fd, skel->data->sendto_data,sizeof (skel->data->sendto_data))) + goto out; + +/* Check send() outcome */ +if (test->connected) { +if (!ASSERT_EQ (skel->data->send_ret,sizeof (skel->data->send_data),"send bytes" )) + goto out; +if (!recv_payload (receivers[0 ], skel->data->send_data, +sizeof (skel->data->send_data))) + goto out; + }else if (!ASSERT_EQ (skel->data->send_ret, -EDESTADDRREQ , +"send after sendto remains unconnected" )) { + goto out; + } + +/* Catch unexpected deliveries to either destination. */ +for (i =0 ; i <ARRAY_SIZE (receivers); i++) { + n =recv (receivers[i], buf,sizeof (buf),MSG_DONTWAIT ); +if (!ASSERT_EQ (n,-1 ,"no extra datagram" ) || + !ASSERT_EQ (errno,EAGAIN ,"recv errno" )) + goto out; + } + +out: +for (i =0 ; i <ARRAY_SIZE (receivers); i++) { +if (receivers[i] >=0 ) +close (receivers[i]); + } +ksock_sendto__destroy (skel); +} + +void test_ksock_sendto (void ) +{ +static const struct sendto_test_case tests[] = { + { .name ="unconnected" , .use_sendto_addr = true }, + { .name ="connected_null_addr" , .connected = true }, + { .name ="connected_explicit_addr" , .connected = true, .use_sendto_addr = true }, + }; +int i; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { +if (test__start_subtest (tests[i].name)) +test_sendto_case (&tests[i]); + } +}
tools/testing/selftests/bpf/progs/ksock_common.h +2 -0
@@ -16,6 +16,8 @@ int bpf_ksock_connect(struct bpf_ksock *ks, const union bpf_ksock_addr *addr,struct bpf_ksock *bpf_ksock_acquire (struct bpf_ksock *ks) __ksym; void bpf_ksock_release(struct bpf_ksock *ks) __ksym; int bpf_ksock_send(struct bpf_ksock *ks, const void *data, u32 data__sz) __ksym; +int bpf_ksock_sendto(struct bpf_ksock *ks, const void *data, u32 data__sz, + const union bpf_ksock_addr *addr__nullable, u32 addr__sz) __ksym; void bpf_rcu_read_lock(void) __ksym; void bpf_rcu_read_unlock(void) __ksym;
tools/testing/selftests/bpf/progs/ksock_sendto.c new file +59 -0
59 lines not on this page · raw message
--
2.43.0
PATCH v2 3/3 selftests/bpf: Test forbidden bpf_ksock_sendto() LSM attach
In reply to Mahe TardySimilarly to '7b0dfbf5776b ("selftests/bpf: Test forbidden bpf_ksock_send() LSM attach")', tests that the verifier filter prevents bpf_ksock_sendto() to be called in a LSM program attached to security_socket_sendmsg() as it would create a direct recursion.
--- .../selftests/bpf/progs/ksock_lsm_verifier.c | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/progs/ksock_lsm_verifier.c +24 -1
@@ -12,7 +12,7 @@ char send_data[11] = "dummy data"; SEC("lsm.s/socket_sendmsg") __description("bpf_ksock_send is rejected from socket_sendmsg LSM hook") __failure __msg("calling kernel function bpf_ksock_send is not allowed") -int BPF_PROG(ksock_socket_sendmsg, struct socket *sock, struct msghdr *msg, +int BPF_PROG(socket_sendmsg_ksock_send, struct socket *sock, struct msghdr *msg, int size, int ret) {struct __ksock_ctx_value *v;
@@ -32,4 +32,27 @@ int BPF_PROG(ksock_socket_sendmsg, struct socket *sock, struct msghdr *msg,return ret; } +SEC("lsm.s/socket_sendmsg") +__description("bpf_ksock_sendto is rejected from socket_sendmsg LSM hook") +__failure __msg("calling kernel function bpf_ksock_sendto is not allowed") +int BPF_PROG(socket_sendmsg_ksock_sendto, struct socket *sock, + struct msghdr *msg, int size, int ret) +{ +struct __ksock_ctx_value *v; +struct bpf_ksock *ks; + + v =ksock_ctx_value_lookup (); +if (!v) +return ret; + + ks =bpf_kptr_xchg (&v->ctx,NULL ); +if (!ks) +return ret; + +bpf_ksock_sendto (ks, send_data,sizeof (send_data),NULL ,0 ); +bpf_ksock_release (ks); + +return ret; +} + char __license[] SEC("license") = "GPL";
--
2.43.0
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Tue, 20 Oct 2026 15:58:45 +0000 you wrote:
This patch series improves the
bpf_ksockinfrastructure in line with the work proposed during Prague Plumbers 2026 presentation[^1] by adding thebpf_ksock_sendto()kfunc to complement thebpf_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.
[...]
Here is the summary with links:
- [bpf-next,v2,1/3] bpf: add bpf_ksock_sendto() kfunc
https://git.kernel.org/bpf/bpf-next/c/74be619845bd - [bpf-next,v2,2/3] selftests/bpf: Test ksock sendto() https://git.kernel.org/bpf/bpf-next/c/7d563ae3ed74 - [bpf-next,v2,3/3] selftests/bpf: Test forbidden bpf_ksock_sendto() LSM attach https://git.kernel.org/bpf/bpf-next/c/d90912846f26
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Re: [PATCH bpf-next v2 1/3] bpf: add bpf_ksock_sendto() kfunc
In reply to Mahe TardyQuoted 116 lines · Mahe Tardy
The motivation is to plumb more of the socket API into ksock, thus supporting
sendto()in addition tosend()which makes a lot of sense in the context of UDP sockets which are the only type currently supported. This is useful when ksock is used to reply to specific addresses or send to multiple addresses.Signed-off-by Mahe Tardy <[email protected]>--- net/core/bpf_ksock.c | 96 +++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 27 deletions(-)net/core/bpf_ksock.c +54
-27@@ -37,6 +37,49 @@ 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 ; + } +} + +static int ksock_sendto (struct bpf_ksock *ks,const void *data,u32 data_sz, +const union bpf_ksock_addr *addr,u32 addr_sz) +{ +struct sockaddr_storage sa; +struct msghdr msg = { + .msg_flags =MSG_DONTWAIT , + }; +struct kvec iov = { + .iov_base = (void *)data, + .iov_len = data_sz, + }; +int addrlen; + +if (addr) { + addrlen =bpf_ksock_addr_to_sockaddr (addr, addr_sz, &sa); +if (addrlen <0 ) +return addrlen; + msg.msg_name = &sa; + msg.msg_namelen = addrlen; + } + +return kernel_sendmsg (ks->sock, &msg, &iov,1 , data_sz); +} +__bpf_kfunc_start_defs ();/** @@ -155,22 +198,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 ); }@@ -225,18 +255,29 @@ CFI_NOSEAL(bpf_ksock_release_dtor); __bpf_kfunc int bpf_ksock_send(struct bpf_ksock *ks, const void *data,u32 data__sz) { -struct msghdr msg = { - .msg_flags =MSG_DONTWAIT , - }; -struct kvec iov = { - .iov_base = (void *)data, - .iov_len = data__sz, - }; -int ret; - - ret =kernel_sendmsg (ks->sock, &msg, &iov,1 , data__sz); +return ksock_sendto (ks, data, data__sz,NULL ,0 ); +} -return ret; +/** +* 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
net/core/bpf_ksock.c CFI_NOSEAL(bpf_ksock_release_dtor); · line 276
Quoted 10 lines
net/core/bpf_ksock.c +10
-0+* the connected destination. +* @addr__sz: Size of the address union, ignored when the address is NULL. +* +* 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_sendto(struct bpf_ksock *ks, const void *data, u32 data__sz,
This would limit API to fixed-sized data only. Have you considered passing dynptr as a source of data, so you can send a wider variety of data? I don't have a strong opinion whether we should add a separate API or convert this one into dynptr, but I do think that having ability to work with variable-sized data is super useful and should be added early on.
Quoted 18 lines
net/core/bpf_ksock.c +5
-1+const union bpf_ksock_addr *addr__nullable, + u32 addr__sz) +{ + return ksock_sendto(ks, data, data__sz, addr__nullable, addr__sz); }__bpf_kfunc_end_defs ();@@ -255,6 +296,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 --2.43.0
Re: [PATCH bpf-next v2 1/3] bpf: add bpf_ksock_sendto() kfunc
Quoted 13 lines · Andrii Nakryiko
Quoted 12 lines · Mahe Tardy
The motivation is to plumb more of the socket API into ksock, thus supporting
sendto()in addition tosend()which makes a lot of sense in the context of UDP sockets which are the only type currently supported. This is useful when ksock is used to reply to specific addresses or send to multiple addresses.Signed-off-by Mahe Tardy <[email protected]>--- net/core/bpf_ksock.c | 96 +++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 27 deletions(-)
[...]
Quoted 17 lines
Quoted 17 lines
net/core/bpf_ksock.c +15
-1-return ret; +/** +* 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 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. +*/
Quoted 8 lines
Quoted 1 line
+__bpf_kfunc int bpf_ksock_sendto(struct
bpf_ksock*ks, const void *data, u32data__sz,This would limit API to fixed-sized data only. Have you considered passing dynptr as a source of data, so you can send a wider variety of data? I don't have a strong opinion whether we should add a separate API or convert this one into dynptr, but I do think that having ability to work with variable-sized data is super useful and should be added early on.
Oh wow this is really cool that you send this now. I was working this afternoon on trying to see if a ksock sendmsg() would make sense, but with these static buffer limitations as arguments, it actually didn't. I'm not familiar with dynptr so I didn't think about it, but after a quick check it looks like it would be a good match with the ksock API!
As I understand, since bpf_ksock_send() is already part of 7.3, it's best to not touch it, and having a simple send() available with a fixed-size data might make sense.
But since bpf_ksock_sendto() was just merged, we could make it evolve to use dynptr perhaps? How much time do we have for something which was merged yesterday to be part of a release? Is it going to be send with the 7.4 pull soon?
Quoted 19 lines
Quoted 19 lines
net/core/bpf_ksock.c +5
-1+const union bpf_ksock_addr *addr__nullable, + u32 addr__sz) +{ + return ksock_sendto(ks, data, data__sz, addr__nullable, addr__sz); }__bpf_kfunc_end_defs ();@@ -255,6 +296,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 --2.43.0
Re: [PATCH bpf-next v2 1/3] bpf: add bpf_ksock_sendto() kfunc
Quoted 50 lines · Mahe Tardy
Quoted 13 lines · Andrii Nakryiko
Quoted 12 lines · Mahe Tardy
The motivation is to plumb more of the socket API into ksock, thus supporting
sendto()in addition tosend()which makes a lot of sense in the context of UDP sockets which are the only type currently supported. This is useful when ksock is used to reply to specific addresses or send to multiple addresses.Signed-off-by Mahe Tardy <[email protected]>--- net/core/bpf_ksock.c | 96 +++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 27 deletions(-)[...]
Quoted 17 lines
Quoted 17 lines
net/core/bpf_ksock.c +15
-1-return ret; +/** +* 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 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. +*/ Quoted 8 lines
Quoted 1 line
+__bpf_kfunc int bpf_ksock_sendto(struct
bpf_ksock*ks, const void *data, u32data__sz,This would limit API to fixed-sized data only. Have you considered passing dynptr as a source of data, so you can send a wider variety of data? I don't have a strong opinion whether we should add a separate API or convert this one into dynptr, but I do think that having ability to work with variable-sized data is super useful and should be added early on.
Oh wow this is really cool that you send this now. I was working this afternoon on trying to see if a ksock
sendmsg()would make sense, but with these static buffer limitations as arguments, it actually didn't. I'm not familiar with dynptr so I didn't think about it, but after a quick check it looks like it would be a good match with the ksock API!
Quoted 3 lines
As I understand, since
bpf_ksock_send()is already part of 7.3, it's best to not touch it, and having a simplesend()available with a fixed-size data might make sense.
agreed
Quoted 6 lines
But since
bpf_ksock_sendto()was just merged, we could make it evolve to use dynptr perhaps? How much time do we have for something which was merged yesterday to be part of a release? Is it going to be send with the 7.4 pull soon?
I'd send a follow up in the next few days, it can go in as a fix for kfunc in rc1 or something, unless others have differing opinions on this
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
net/core/bpf_ksock.c +5
-1+const union bpf_ksock_addr *addr__nullable, + u32 addr__sz) +{ + return ksock_sendto(ks, data, data__sz, addr__nullable, addr__sz); }__bpf_kfunc_end_defs ();@@ -255,6 +296,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 --2.43.0
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 1/3] bpf: add bpf_ksock_sendto() kfunc
Quoted 64 lines · Andrii Nakryiko
Quoted 50 lines · Mahe Tardy
Quoted 13 lines · Andrii Nakryiko
Quoted 12 lines · Mahe Tardy
The motivation is to plumb more of the socket API into ksock, thus supporting
sendto()in addition tosend()which makes a lot of sense in the context of UDP sockets which are the only type currently supported. This is useful when ksock is used to reply to specific addresses or send to multiple addresses.Signed-off-by Mahe Tardy <[email protected]>--- net/core/bpf_ksock.c | 96 +++++++++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 27 deletions(-)[...]
Quoted 17 lines
Quoted 17 lines
net/core/bpf_ksock.c +15
-1-return ret; +/** +* 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 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. +*/ Quoted 8 lines
Quoted 1 line
+__bpf_kfunc int bpf_ksock_sendto(struct
bpf_ksock*ks, const void *data, u32data__sz,This would limit API to fixed-sized data only. Have you considered passing dynptr as a source of data, so you can send a wider variety of data? I don't have a strong opinion whether we should add a separate API or convert this one into dynptr, but I do think that having ability to work with variable-sized data is super useful and should be added early on.
Oh wow this is really cool that you send this now. I was working this afternoon on trying to see if a ksock
sendmsg()would make sense, but with these static buffer limitations as arguments, it actually didn't. I'm not familiar with dynptr so I didn't think about it, but after a quick check it looks like it would be a good match with the ksock API!Quoted 3 lines
As I understand, since
bpf_ksock_send()is already part of 7.3, it's best to not touch it, and having a simplesend()available with a fixed-size data might make sense.agreed
Quoted 6 lines
But since
bpf_ksock_sendto()was just merged, we could make it evolve to use dynptr perhaps? How much time do we have for something which was merged yesterday to be part of a release? Is it going to be send with the 7.4 pull soon?
Quoted 4 lines
I'd send a follow up in the next few days, it can go in as a fix for kfunc in rc1 or something, unless others have differing opinions on this
Another option is to add dynptr variants of both, a pattern we have in other APIs as well, so there would be no rush. We just have two more kfuncs:bpf_ksock_send_dynptr() and bpf_ksock_sendto_dynptr().
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
net/core/bpf_ksock.c +5
-1+const union bpf_ksock_addr *addr__nullable, + u32 addr__sz) +{ + return ksock_sendto(ks, data, data__sz, addr__nullable, addr__sz); }__bpf_kfunc_end_defs ();@@ -255,6 +296,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 --2.43.0