Skip to contentSkip to conversation

bpf: Introduce bpf_netpoll

PATCH NAK Mahe Tardy 7 participants 19 messages May 11 – Jul 16

Subsystems (6)
lore
Share & tools
lore

Mahe Tardy

author
#0 lore raw

cover bpf: Introduce bpf_netpoll

This patch series introduces bpf_netpoll, a set of BPF kfuncs that allow BPF programs to send UDP packets via the netpoll infrastructure. This provides a mechanism for BPF programs (e.g., LSM hooks) to emit telemetry over UDP without depending on the regular networking stack.

For reference, this was discussed at LSF/MM/BPF 2025[^1] in Montreal, again at Plumbers 2025 in Tokyo. Liam Wisehart mentioned this work during his presentation of BpfJailer[^2]. Then it was also discussed during LSF/MM/BPF 2026.

The main use case is to be able to completely dispense with agents/daemons for BPF programs after startup. In the case of Isovalent's Tetragon, the idea would be to be able to emit security alerts or export data from BPF even when the agent is down. For meta, according to Liam presentation[^2], this could replace logging via ringbuffers which created cross-binary versioning issues.

The implementation follows the established kfunc lifecycle pattern (create/acquire/release with refcounting, kptr map storage, dtor registration), for example used by the network bpf_crypto kfuncs.

Note that network program types should not encounter recursion issues as netpoll bypasses the network stack and sends directly to the driver.

v1 updates:
- use kzalloc_obj instead of kzalloc after tree wide update (alexei)
- extend bpf_netpoll_send_udp to all program types (mahe)
- correct documentation on send return value (ci bpf bot)
- add IPv6 tests as a subtest (mahe)
- we had some discussions at LSF/MM/BPF 2026 in Zagreb, demoing how this could be used, generally agreeing we could move forward with such an API.

[^1]: https://lwn.net/Articles/1022034/
[^2]: https://lpc.events/event/19/contributions/2159/

Mahe Tardy (2):
selftests/bpf: Add netpoll kfunc IPv6 variant test selftests/bpf: Add netpoll setup basic tests

Song Liu (2):
bpf: Add netpoll kfuncs for sending UDP packets
selftests/bpf: Add netpoll kfunc sanity test

 drivers/net/Kconfig                           |  11 +
 include/linux/bpf_netpoll.h                   |  38 ++++
 kernel/bpf/verifier.c                         |   3 +
 net/core/Makefile                             |   1 +
 net/core/bpf_netpoll.c                        | 209 ++++++++++++++++++
 tools/testing/selftests/bpf/config            |   1 +
 .../selftests/bpf/prog_tests/netpoll.c        | 139 ++++++++++++
 .../selftests/bpf/progs/netpoll_basic.c       |  47 ++++
 .../selftests/bpf/progs/netpoll_common.h      |  64 ++++++
 .../selftests/bpf/progs/netpoll_sanity.c      | 135 +++++++++++
 10 files changed, 648 insertions(+)
 create mode 100644 include/linux/bpf_netpoll.h
 create mode 100644 net/core/bpf_netpoll.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/netpoll.c
 create mode 100644 tools/testing/selftests/bpf/progs/netpoll_basic.c
 create mode 100644 tools/testing/selftests/bpf/progs/netpoll_common.h
 create mode 100644 tools/testing/selftests/bpf/progs/netpoll_sanity.c

--
2.34.1

author
#1 lore raw

PATCH 1/4 bpf: Add netpoll kfuncs for sending UDP packets

From: Song Liu <[email protected]>

Add BPF kfuncs that allow BPF programs to send UDP packets via the netpoll infrastructure. This provides a mechanism for BPF programs (e.g., LSM hooks) to emit telemetry over UDP without depending on the regular networking stack.

The API consists of four kfuncs:

bpf_netpoll_create() - Allocate and set up a netpoll context
(sleepable, SYSCALL prog type only)
bpf_netpoll_acquire() - Acquire a reference to a netpoll context bpf_netpoll_release() - Release a reference (cleanup via
queue_rcu_work since netpoll_cleanup sleeps)
bpf_netpoll_send_udp() - Send a UDP packet (any context, LSM prog
type only for now)

The implementation follows the established kfunc lifecycle pattern (create/acquire/release with refcounting, kptr map storage, dtor registration). The netpoll context is wrapped in a refcounted bpf_netpoll struct. Cleanup is deferred via queue_rcu_work() because netpoll_cleanup() takes rtnl_lock.

AI was used to generate the code and each line was manually reviewed.

---
 drivers/net/Kconfig         |  11 ++
 include/linux/bpf_netpoll.h |  38 +++++++
 kernel/bpf/verifier.c       |   3 +
 net/core/Makefile           |   1 +
 net/core/bpf_netpoll.c      | 209 ++++++++++++++++++++++++++++++++++++
 5 files changed, 262 insertions(+)
 create mode 100644 include/linux/bpf_netpoll.h
 create mode 100644 net/core/bpf_netpoll.c
drivers/net/Kconfig +11 -0
index edaab759dc50..8d94c9d58f32 100644
@@ -364,6 +364,17 @@ config NETCONSOLE_PREPEND_RELEASE
 	  message.  See <file:Documentation/networking/netconsole.rst> for
 	  details.
 
+config BPF_NETPOLL
+	bool "BPF netpoll UDP support"
+	depends on BPF_SYSCALL && NET
+	select NETPOLL
+	help
+	  Enable BPF kfuncs for sending UDP packets via netpoll.
+	  This allows BPF programs to send UDP packets from any
+	  context using the netpoll infrastructure.
+
+	  If unsure, say N.
+
 config NETPOLL
 	def_bool NETCONSOLE
 
include/linux/bpf_netpoll.h new file +38 -0
new file mode 100644
index 000000000000..b738032548c7
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#ifndef _BPF_NETPOLL_H
+#define _BPF_NETPOLL_H
+
+#include <linux/types.h>
+
+#define BPF_NETPOLL_DEV_NAME_LEN 16	/* IFNAMSIZ */
+
+/**
+ * struct bpf_netpoll_opts - BPF netpoll initialization parameters
+ * @dev_name:	Network device name (e.g. "eth0"), null-terminated.
+ * @local_ip:	Local IPv4 address in network byte order. 0 = auto-detect.
+ * @remote_ip:	Remote IPv4 address in network byte order.
+ * @local_port:	Local UDP port in host byte order.
+ * @remote_port: Remote UDP port in host byte order.
+ * @remote_mac:	Remote MAC address (6 bytes).
+ * @ipv6:	Set to 1 for IPv6, 0 for IPv4.
+ * @reserved:	Must be zero. Reserved for future use.
+ * @local_ip6:	Local IPv6 address (16 bytes). Used when ipv6=1.
+ *		Zero = auto-detect.
+ * @remote_ip6:	Remote IPv6 address (16 bytes). Used when ipv6=1.
+ */
+struct bpf_netpoll_opts {
+	char dev_name[BPF_NETPOLL_DEV_NAME_LEN];
+	__be32 local_ip;
+	__be32 remote_ip;
+	__u16 local_port;
+	__u16 remote_port;
+	__u8 remote_mac[6];
+	__u8 ipv6;
+	__u8 reserved;
+	__u8 local_ip6[16];
+	__u8 remote_ip6[16];
+};
+
+#endif /* _BPF_NETPOLL_H */
kernel/bpf/verifier.c +3 -0
index 185210b73385..2dc8d2fb4a01 100644
@@ -4641,6 +4641,9 @@ BTF_ID(struct, task_struct)
 #ifdef CONFIG_CRYPTO
 BTF_ID(struct, bpf_crypto_ctx)
 #endif
+#ifdef CONFIG_BPF_NETPOLL
+BTF_ID(struct, bpf_netpoll)
+#endif
 BTF_SET_END(rcu_protected_types)
 
 static bool rcu_protected_object(const struct btf *btf, u32 btf_id)
net/core/Makefile +1 -0
index dc17c5a61e9a..6484eb595698 100644
@@ -26,6 +26,7 @@ obj-$(CONFIG_PAGE_POOL) += page_pool.o page_pool_user.o
 obj-$(CONFIG_PROC_FS) += net-procfs.o
 obj-$(CONFIG_NET_PKTGEN) += pktgen.o
 obj-$(CONFIG_NETPOLL) += netpoll.o
+obj-$(CONFIG_BPF_NETPOLL) += bpf_netpoll.o
 obj-$(CONFIG_FIB_RULES) += fib_rules.o
 obj-$(CONFIG_TRACEPOINTS) += net-traces.o
 obj-$(CONFIG_NET_DROP_MONITOR) += drop_monitor.o
net/core/bpf_netpoll.c new file +209 -0

209 lines not on this page · raw message

--
2.34.1

#2 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

The commit message states bpf_netpoll_send_udp() is "LSM prog type only for now," but looking at the implementation in bpf_netpoll_kfunc_init(), the function registers netpoll_kfunc_set using…
Quoted 9 lines

The API consists of four kfuncs:

bpf_netpoll_create() - Allocate and set up a netpoll context
(sleepable, SYSCALL prog type only)
bpf_netpoll_acquire() - Acquire a reference to a netpoll context bpf_netpoll_release() - Release a reference (cleanup via
queue_rcu_work since netpoll_cleanup sleeps)
bpf_netpoll_send_udp() - Send a UDP packet (any context, LSM prog
type only for now)

The commit message states bpf_netpoll_send_udp() is "LSM prog type only for now," but looking at the implementation in bpf_netpoll_kfunc_init(), the function registers netpoll_kfunc_set using BPF_PROG_TYPE_UNSPEC:

net/core/bpf_netpoll.c:bpf_netpoll_kfunc_init() {
    ...
    ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC,
                                           &netpoll_kfunc_set);
    ...
}

Per bpf_prog_type_to_kfunc_hook() in kernel/bpf/btf.c, BPF_PROG_TYPE_UNSPEC maps to BTF_KFUNC_HOOK_COMMON, which makes the kfunc available to ALL BPF program types, not just LSM.

The check in btf_kfunc_is_allowed() tests BTF_KFUNC_HOOK_COMMON first and returns true if the kfunc is present there, so any program type (tracing, kprobe, XDP, sched-cls, etc.) can call bpf_netpoll_send_udp().

Is the commit message stale, or should the registration be BPF_PROG_TYPE_LSM instead of BPF_PROG_TYPE_UNSPEC to match the stated design?

Quoted 3 lines
drivers/net/Kconfig +0 -0

[ ... ]

Quoted 3 lines
include/linux/bpf_netpoll.h new file +0 -0

[ ... ]

Quoted 3 lines
kernel/bpf/verifier.c +0 -0

[ ... ]

Quoted 3 lines
net/core/Makefile +0 -0

[ ... ]

Quoted 4 lines
net/core/bpf_netpoll.c new file +0 -0
@@ -0,0 +1,209 @@

[ ... ]

Quoted 23 lines
net/core/bpf_netpoll.c new file +23 -0
+BTF_KFUNCS_START(netpoll_kfunc_btf_ids)
+BTF_ID_FLAGS(func, bpf_netpoll_send_udp, KF_RCU)
+BTF_KFUNCS_END(netpoll_kfunc_btf_ids)
+
+static const struct btf_kfunc_id_set netpoll_kfunc_set = {
+	.owner = THIS_MODULE,
+	.set   = &netpoll_kfunc_btf_ids,
+};
+
+BTF_ID_LIST(bpf_netpoll_dtor_ids)
+BTF_ID(struct, bpf_netpoll)
+BTF_ID(func, bpf_netpoll_release_dtor)
+
+static int __init bpf_netpoll_kfunc_init(void)
+{
+	int ret;
+	const struct btf_id_dtor_kfunc bpf_netpoll_dtors[] = {
+		{
+			.btf_id       = bpf_netpoll_dtor_ids[0],
+			.kfunc_btf_id = bpf_netpoll_dtor_ids[1],
+		},
+	};
+

net/core/bpf_netpoll.c line 209

Quoted 10 lines
net/core/bpf_netpoll.c new file +10 -0
+	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
+					&netpoll_init_kfunc_set);
+	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC,
+					       &netpoll_kfunc_set);
+	return ret ?: register_btf_id_dtor_kfuncs(bpf_netpoll_dtors,
+						  ARRAY_SIZE(bpf_netpoll_dtors),
+						  THIS_MODULE);
+}
+
+late_initcall(bpf_netpoll_kfunc_init);

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/25660780250

author
#3 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

Quoted 25 lines · On Mon, May 11, 2026 at 09:40:32AM +0000, [email protected] wrote:
Quoted 9 lines

The API consists of four kfuncs:

bpf_netpoll_create() - Allocate and set up a netpoll context
(sleepable, SYSCALL prog type only)
bpf_netpoll_acquire() - Acquire a reference to a netpoll context bpf_netpoll_release() - Release a reference (cleanup via
queue_rcu_work since netpoll_cleanup sleeps)
bpf_netpoll_send_udp() - Send a UDP packet (any context, LSM prog
type only for now)

The commit message states bpf_netpoll_send_udp() is "LSM prog type only for now," but looking at the implementation in bpf_netpoll_kfunc_init(), the function registers netpoll_kfunc_set using BPF_PROG_TYPE_UNSPEC:

net/core/bpf_netpoll.c:bpf_netpoll_kfunc_init() {
    ...
    ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC,
                                           &netpoll_kfunc_set);
    ...
}

Per bpf_prog_type_to_kfunc_hook() in kernel/bpf/btf.c, BPF_PROG_TYPE_UNSPEC maps to BTF_KFUNC_HOOK_COMMON, which makes the kfunc available to ALL BPF program types, not just LSM.

Quoted 7 lines

The check in btf_kfunc_is_allowed() tests BTF_KFUNC_HOOK_COMMON first and returns true if the kfunc is present there, so any program type (tracing, kprobe, XDP, sched-cls, etc.) can call bpf_netpoll_send_udp().

Is the commit message stale, or should the registration be BPF_PROG_TYPE_LSM instead of BPF_PROG_TYPE_UNSPEC to match the stated design?

Ah indeed, this a stale commit message, will fix in v2. The real intention is to make it BPF_PROG_TYPE_UNSPEC. Will wait on some more reviews before submitting.

Quoted 5 lines

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/25660780250

maintainer
#4 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

In reply to Mahe Tardy
Quoted 45 lines · Mahe Tardy

From: Song Liu <[email protected]>

Add BPF kfuncs that allow BPF programs to send UDP packets via the netpoll infrastructure. This provides a mechanism for BPF programs (e.g., LSM hooks) to emit telemetry over UDP without depending on the regular networking stack.

The API consists of four kfuncs:

bpf_netpoll_create() - Allocate and set up a netpoll context
(sleepable, SYSCALL prog type only)
bpf_netpoll_acquire() - Acquire a reference to a netpoll context bpf_netpoll_release() - Release a reference (cleanup via
queue_rcu_work since netpoll_cleanup sleeps)
bpf_netpoll_send_udp() - Send a UDP packet (any context, LSM prog
type only for now)

The implementation follows the established kfunc lifecycle pattern (create/acquire/release with refcounting, kptr map storage, dtor registration). The netpoll context is wrapped in a refcounted bpf_netpoll struct. Cleanup is deferred via queue_rcu_work() because netpoll_cleanup() takes rtnl_lock.

AI was used to generate the code and each line was manually reviewed.

---
  drivers/net/Kconfig         |  11 ++
  include/linux/bpf_netpoll.h |  38 +++++++
  kernel/bpf/verifier.c       |   3 +
  net/core/Makefile           |   1 +
  net/core/bpf_netpoll.c      | 209 ++++++++++++++++++++++++++++++++++++
  5 files changed, 262 insertions(+)

create mode 100644 include/linux/bpf_netpoll.h
create mode 100644 net/core/bpf_netpoll.c

drivers/net/Kconfig +0 -0
index edaab759dc50..8d94c9d58f32 100644
@@ -364,6 +364,17 @@ config NETCONSOLE_PREPEND_RELEASE
  	  message.  See <file:Documentation/networking/netconsole.rst> for
  	  details.
 

drivers/net/Kconfig config NETCONSOLE_PREPEND_RELEASE · line 376

Quoted 10 lines
drivers/net/Kconfig +10 -0
+config BPF_NETPOLL
+	bool "BPF netpoll UDP support"
+	depends on BPF_SYSCALL && NET
+	select NETPOLL
+	help
+	  Enable BPF kfuncs for sending UDP packets via netpoll.
+	  This allows BPF programs to send UDP packets from any
+	  context using the netpoll infrastructure.
+
+	  If unsure, say N.

Do we need the extra Kconfig knob? For most other BPF functionality we don't have such convention. BPF_NETPOLL could be a hidden config enabled when both BPF_SYSCALL && NETPOLL is enabled?

Quoted 9 lines
drivers/net/Kconfig +0 -0
  config NETPOLL
  	def_bool NETCONSOLE
 
include/linux/bpf_netpoll.h new file +0 -0
new file mode 100644
index 000000000000..b738032548c7

include/linux/bpf_netpoll.h line 9

Quoted 9 lines
include/linux/bpf_netpoll.h new file +9 -0
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#ifndef _BPF_NETPOLL_H
+#define _BPF_NETPOLL_H
+
+#include <linux/types.h>
+
+#define BPF_NETPOLL_DEV_NAME_LEN 16	/* IFNAMSIZ */

Do we need this, can't we just reuse IFNAMSIZ?

Quoted 16 lines
include/linux/bpf_netpoll.h new file +16 -0
+/**
+ * struct bpf_netpoll_opts - BPF netpoll initialization parameters
+ * @dev_name:	Network device name (e.g. "eth0"), null-terminated.
+ * @local_ip:	Local IPv4 address in network byte order. 0 = auto-detect.
+ * @remote_ip:	Remote IPv4 address in network byte order.
+ * @local_port:	Local UDP port in host byte order.
+ * @remote_port: Remote UDP port in host byte order.
+ * @remote_mac:	Remote MAC address (6 bytes).
+ * @ipv6:	Set to 1 for IPv6, 0 for IPv4.
+ * @reserved:	Must be zero. Reserved for future use.
+ * @local_ip6:	Local IPv6 address (16 bytes). Used when ipv6=1.
+ *		Zero = auto-detect.
+ * @remote_ip6:	Remote IPv6 address (16 bytes). Used when ipv6=1.
+ */
+struct bpf_netpoll_opts {
+	char dev_name[BPF_NETPOLL_DEV_NAME_LEN];

include/linux/bpf_netpoll.h line 36

Quoted 10 lines
include/linux/bpf_netpoll.h new file +10 -0
+	__be32 local_ip;
+	__be32 remote_ip;
+	__u16 local_port;
+	__u16 remote_port;
+	__u8 remote_mac[6];
+	__u8 ipv6;
+	__u8 reserved;
+	__u8 local_ip6[16];
+	__u8 remote_ip6[16];
+};

Could we detangle this a bit and structure the design similar to what was done for the bpf fib lookup helpers? __u8 family and then union.

Is the remote_mac strictly needed? Feels a bit like a burden, maybe for the selftest, we should first do a bpf_fib_lookup before the bpf_netpoll_create so that the use case where the user only cares about giving remote_ip/remote_port and maybe local_port (e.g. to as indicator for different event types and/or RSS hashing) works, but everything else is derived via kernel fib lookup.

Thanks,
Daniel

author
#5 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

Quoted 10 lines · Daniel Borkmann
Quoted 9 lines · Mahe Tardy

From: Song Liu <[email protected]>

Add BPF kfuncs that allow BPF programs to send UDP packets via the netpoll infrastructure. This provides a mechanism for BPF programs (e.g., LSM hooks) to emit telemetry over UDP without depending on the regular networking stack.

[...]

Quoted 14 lines

drivers/net/Kconfig config NETCONSOLE_PREPEND_RELEASE · line 376

Quoted 10 lines
drivers/net/Kconfig +10 -0
+config BPF_NETPOLL
+	bool "BPF netpoll UDP support"
+	depends on BPF_SYSCALL && NET
+	select NETPOLL
+	help
+	  Enable BPF kfuncs for sending UDP packets via netpoll.
+	  This allows BPF programs to send UDP packets from any
+	  context using the netpoll infrastructure.
+
+	  If unsure, say N.

Do we need the extra Kconfig knob? For most other BPF functionality we don't have such convention. BPF_NETPOLL could be a hidden config enabled when both BPF_SYSCALL && NETPOLL is enabled?

Okay I changed it to:

+config BPF_NETPOLL
+ def_bool BPF_SYSCALL && NETPOLL

So now we need to explicitely enable NETCONSOLE since NETPOL is also a hidden config defined by:

config NETPOLL
def_bool NETCONSOLE

I somehow initially wanted to define NETPOL by having def_bool NETCONSOLE || BPF_NETPOLL but then we have a circular dependency...

Quoted 12 lines
Quoted 12 lines

config NETPOLL
def_bool NETCONSOLE

include/linux/bpf_netpoll.h new file +3 -0
new file mode 100644
index 000000000000..b738032548c7
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
Quoted 8 lines

include/linux/bpf_netpoll.h line 9

Quoted 6 lines
include/linux/bpf_netpoll.h new file +6 -0
+#ifndef _BPF_NETPOLL_H
+#define _BPF_NETPOLL_H
+
+#include <linux/types.h>
+
+#define BPF_NETPOLL_DEV_NAME_LEN 16	/* IFNAMSIZ */

Do we need this, can't we just reuse IFNAMSIZ?

We don't really need it, done!

Quoted 21 lines
Quoted 21 lines
include/linux/bpf_netpoll.h new file +21 -0
+/**
+ * struct bpf_netpoll_opts - BPF netpoll initialization parameters
+ * @dev_name:	Network device name (e.g. "eth0"), null-terminated.
+ * @local_ip:	Local IPv4 address in network byte order. 0 = auto-detect.
+ * @remote_ip:	Remote IPv4 address in network byte order.
+ * @local_port:	Local UDP port in host byte order.
+ * @remote_port: Remote UDP port in host byte order.
+ * @remote_mac:	Remote MAC address (6 bytes).
+ * @ipv6:	Set to 1 for IPv6, 0 for IPv4.
+ * @reserved:	Must be zero. Reserved for future use.
+ * @local_ip6:	Local IPv6 address (16 bytes). Used when ipv6=1.
+ *		Zero = auto-detect.
+ * @remote_ip6:	Remote IPv6 address (16 bytes). Used when ipv6=1.
+ */
+struct bpf_netpoll_opts {
+	char dev_name[BPF_NETPOLL_DEV_NAME_LEN];
+	__be32 local_ip;
+	__be32 remote_ip;
+	__u16 local_port;
+	__u16 remote_port;
+	__u8 remote_mac[6];
Quoted 8 lines

include/linux/bpf_netpoll.h line 36

Quoted 5 lines
include/linux/bpf_netpoll.h new file +5 -0
+	__u8 ipv6;
+	__u8 reserved;
+	__u8 local_ip6[16];
+	__u8 remote_ip6[16];
+};

Could we detangle this a bit and structure the design similar to what was done for the bpf fib lookup helpers? __u8 family and then union.

Yeah I think initially it was heavily inspired by the struct netpoll for IPv6 boolean but they have the union, let's do it, I wrote this one that is similar to the fib lookup params:

struct bpf_netpoll_opts {
	char dev_name[IFNAMSIZ];
	__u8 family;
	__u8 reserved;
	__u16 local_port;
	__u16 remote_port;
	__u8 remote_mac[ETH_ALEN];

	union {
		__be32 ipv4_local;
		__u32 ipv6_local[4];  /* in6_addr; network order */
	};

	union {
		__be32 ipv4_remote;
		__u32 ipv6_remote[4];  /* in6_addr; network order */
	};
};

Also used family instead of the bool with this in the kfunc:

switch (opts->family) {
case AF_INET:

	bnp->np.local_ip.ip = opts->ipv4_local;
	bnp->np.remote_ip.ip = opts->ipv4_remote;
	break;
case AF_INET6:
	memcpy(&bnp->np.local_ip.in6, opts->ipv6_local,
	       sizeof(struct in6_addr));
	memcpy(&bnp->np.remote_ip.in6, opts->ipv6_remote,
	       sizeof(struct in6_addr));
	bnp->np.ipv6 = true;
	break;
default:
	*err = -EAFNOSUPPORT;
	return NULL;
}
Quoted 1 line

Is the remote_mac strictly needed?

I think it's indeed needed since netpoll is pushing the MAC address in the skb at the send step. It's not reusing the network stack for lookup. I think netconsole is using the broadcast address by default, maybe the kfunc could default to it as well.

Quoted 5 lines

Feels a bit like a burden, maybe for the
selftest, we should first do a bpf_fib_lookup before the bpf_netpoll_create so that the use case where the user only cares about giving remote_ip/remote_port and maybe local_port (e.g. to as indicator for different event types and/or RSS hashing) works, but everything else is derived via kernel fib lookup.

Yeah that would be nice but then it would mean we need to call fib lookup from the SYSCALL programs without any network context so I'm not sure how it would look like.

Quoted 2 lines

Thanks,
Daniel

Thanks for the review!
Mahe

maintainer Nacked-by on v1 1/4 NAK
#6 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

In reply to Mahe Tardy
Quoted 15 lines · On Mon, 11 May 2026 08:53:41 +0000 Mahe Tardy wrote:

Add BPF kfuncs that allow BPF programs to send UDP packets via the netpoll infrastructure. This provides a mechanism for BPF programs (e.g., LSM hooks) to emit telemetry over UDP without depending on the regular networking stack.

The API consists of four kfuncs:

bpf_netpoll_create() - Allocate and set up a netpoll context
(sleepable, SYSCALL prog type only)
bpf_netpoll_acquire() - Acquire a reference to a netpoll context bpf_netpoll_release() - Release a reference (cleanup via
queue_rcu_work since netpoll_cleanup sleeps)
bpf_netpoll_send_udp() - Send a UDP packet (any context, LSM prog
type only for now)

Quoted 5 lines

The implementation follows the established kfunc lifecycle pattern (create/acquire/release with refcounting, kptr map storage, dtor registration). The netpoll context is wrapped in a refcounted bpf_netpoll struct. Cleanup is deferred via queue_rcu_work() because netpoll_cleanup() takes rtnl_lock.

We have enough bug reports as is, let's not merge unusable toys. For any use of netpoll by BPF:

#7 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

Quoted 22 lines · Jakub Kicinski
Quoted 15 lines · On Mon, 11 May 2026 08:53:41 +0000 Mahe Tardy wrote:

Add BPF kfuncs that allow BPF programs to send UDP packets via the netpoll infrastructure. This provides a mechanism for BPF programs (e.g., LSM hooks) to emit telemetry over UDP without depending on the regular networking stack.

The API consists of four kfuncs:

bpf_netpoll_create() - Allocate and set up a netpoll context
(sleepable, SYSCALL prog type only)
bpf_netpoll_acquire() - Acquire a reference to a netpoll context bpf_netpoll_release() - Release a reference (cleanup via
queue_rcu_work since netpoll_cleanup sleeps)
bpf_netpoll_send_udp() - Send a UDP packet (any context, LSM prog
type only for now)

Quoted 5 lines

The implementation follows the established kfunc lifecycle pattern (create/acquire/release with refcounting, kptr map storage, dtor registration). The netpoll context is wrapped in a refcounted bpf_netpoll struct. Cleanup is deferred via queue_rcu_work() because netpoll_cleanup() takes rtnl_lock.

Quoted 4 lines

We have enough bug reports as is, let's not merge unusable toys. For any use of netpoll by BPF:

unusable toys? What are you talking about?
netpoll is already called from everywhere.
bpf_netpoll_send_udp() won't add any more bugs.

I like this netpoll approach way more then creating and keeping proper socket within bpf and all head aches of keeping it around and doing send from good context. With netpoll all of these issues are gone.
Just bpf_netpoll_send_udp() from anywhere and it works just like it does for dmesg.

maintainer
#8 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

Quoted 1 line · On Mon, 11 May 2026 18:59:56 -0700 Alexei Starovoitov wrote:

unusable toys? What are you talking about?

Please refer to the comments on the RFC. netpoll bypasses most of the networking stack. And there either has to be some user space to negotiate crypto or this is unusable for anyone serious.

Quoted 1 line

netpoll is already called from everywhere.

You mean all contexts because it's used by console?

Quoted 1 line

bpf_netpoll_send_udp() won't add any more bugs.

Same as TLS + sockmap didn't? I spend enough time mopping up after people.

Quoted 5 lines

I like this netpoll approach way more then creating and keeping proper socket within bpf and all head aches of keeping it around and doing send from good context. With netpoll all of these issues are gone.
Just bpf_netpoll_send_udp() from anywhere and it works just like it does for dmesg.

No, netpoll is high risk / brittle and tightly integrated with NAPI.

"I don't want to run a tiny user space program" is nowhere near strong enough justification to expose this API. SMH.

#9 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

Quoted 6 lines · Jakub Kicinski
Quoted 1 line · On Mon, 11 May 2026 18:59:56 -0700 Alexei Starovoitov wrote:

unusable toys? What are you talking about?

Please refer to the comments on the RFC. netpoll bypasses most of the networking stack. And there either has to be some user space to negotiate crypto or this is unusable for anyone serious.

encryption inside UDP packet is orthogonal. netpoll as-is is usable and useful to send plain text best effort messages.

Quoted 3 lines
Quoted 1 line

netpoll is already called from everywhere.

You mean all contexts because it's used by console?

yes

Quoted 4 lines
Quoted 1 line

bpf_netpoll_send_udp() won't add any more bugs.

Same as TLS + sockmap didn't? I spend enough time mopping up after people.

huh? comparing this to sockmap is apples and oranges.

Quoted 10 lines
Quoted 5 lines

I like this netpoll approach way more then creating and keeping proper socket within bpf and all head aches of keeping it around and doing send from good context. With netpoll all of these issues are gone.
Just bpf_netpoll_send_udp() from anywhere and it works just like it does for dmesg.

No, netpoll is high risk / brittle and tightly integrated with NAPI.

"I don't want to run a tiny user space program" is nowhere near strong enough justification to expose this API. SMH.

No. Nothing to do with 'user space prog'.

What's brittle in netpoll? It works today in any context and that's the main point. All the gotchas were resolved over the years by heavy netconsole usage. Sure, it only works in mainstream drivers like mlx, brcm and likely broken in niche drivers. That's not obstacle at all.
netpoll_send_udp() is a perfect building block for best effort try_to_sendmsg. I don't see the reason to reinvent the whole thing. Because alternatives are much worse.
bpf progs would need to create UDP socket, split udp_sendmsg into who knows what. Lots of design questions, code reviews, etc. Way more headaches for everyone involved.
Doing full networking with dst,fib,nh from kernel context is complicated. Works for NFS, but it's an uncharted territory for bpf.
While netpoll_send_udp() is available, easy to use and proven to work.

maintainer
#10 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

Quoted 16 lines · On Mon, 11 May 2026 19:59:55 -0700 Alexei Starovoitov wrote:
Quoted 6 lines · Jakub Kicinski
Quoted 1 line · On Mon, 11 May 2026 18:59:56 -0700 Alexei Starovoitov wrote:

unusable toys? What are you talking about?

Please refer to the comments on the RFC. netpoll bypasses most of the networking stack. And there either has to be some user space to negotiate crypto or this is unusable for anyone serious.

encryption inside UDP packet is orthogonal. netpoll as-is is usable and useful to send plain text best effort messages.

Quoted 3 lines
Quoted 1 line

netpoll is already called from everywhere.

You mean all contexts because it's used by console?

Quoted 8 lines

yes

Quoted 4 lines
Quoted 1 line

bpf_netpoll_send_udp() won't add any more bugs.

Same as TLS + sockmap didn't? I spend enough time mopping up after people.

huh? comparing this to sockmap is apples and oranges.

There's code which has a lot of users and barely any bugs. And then there's code which has more bugs than users. IMO this will be firmly in the later group.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

I like this netpoll approach way more then creating and keeping proper socket within bpf and all head aches of keeping it around and doing send from good context. With netpoll all of these issues are gone.
Just bpf_netpoll_send_udp() from anywhere and it works just like it does for dmesg.

Quoted 6 lines
Quoted 4 lines

No, netpoll is high risk / brittle and tightly integrated with NAPI.

"I don't want to run a tiny user space program" is nowhere near strong enough justification to expose this API. SMH.

No. Nothing to do with 'user space prog'.

I'm sorry, are you saying that the motivation in the cover letter is not the motivation? It'd be great if someone could reveal the real motivation then.

Quoted 7 lines

What's brittle in netpoll? It works today in any context and that's the main point. All the gotchas were resolved over the years by heavy netconsole usage. Sure, it only works in mainstream drivers like mlx, brcm and likely broken in niche drivers. That's not obstacle at all.
netpoll_send_udp() is a perfect building block for best effort try_to_sendmsg.

It's not a generic building block for random subsystems to use. netconsole is very important so we suffer its needs.

Quoted 4 lines

I don't see the reason to reinvent the whole thing. Because alternatives are much worse.
bpf progs would need to create UDP socket, split udp_sendmsg into who knows what. Lots of design questions, code reviews, etc.

In kernel sockets are used broadly. I accept that it's more work for you but it's also the correct way to go about this if you want to send traffic from BPF.

Quoted 2 lines

Way more headaches for everyone involved.
Doing full networking with dst,fib,nh from kernel context is complicated.

Why would you have to worry about any of the low level details with sockets?

Quoted 2 lines

Works for NFS, but it's an uncharted territory for bpf. While netpoll_send_udp() is available, easy to use and proven to work.

It's not "available", the only reason it's exported is because netconsole is a module.

#11 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

Quoted 8 lines · Jakub Kicinski
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

I like this netpoll approach way more then creating and keeping proper socket within bpf and all head aches of keeping it around and doing send from good context. With netpoll all of these issues are gone.
Just bpf_netpoll_send_udp() from anywhere and it works just like it does for dmesg.

No, netpoll is high risk / brittle and tightly integrated with NAPI.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

"I don't want to run a tiny user space program" is nowhere near strong enough justification to expose this API. SMH.

No. Nothing to do with 'user space prog'.

I'm sorry, are you saying that the motivation in the cover letter is not the motivation? It'd be great if someone could reveal the real motivation then.

AI makes commit logs sound very convincing, so I don't read them anymore to avoid bias. Only looking at the code.
And what I see in this set is a facility for emergency notifications that is safe to use in any context.

If people bolt crypto on top and make a toy prototype out of it it doesn't take away from usefulness of sending UDP packets out.

I want to use it out of bpf core to notify of things like divide by zero, arena fault, etc. Currently they are sent via bpf_stream, but user space has to be there to pick up from the stream.
We cannot use printk, since it's not safe.
So netcons is the only option available today for 'bad things happen' notifications.
Theoretically con->write_atomic() can be wrapped in such helper/kfunc that both bpf core and bpf progs can call.
progs also need emergency notifications.
Like sched-ext might detect something bad and notify.
But at the end netconsole_write() is netpoll_send_udp(),
so more natural and simpler to just use that.

Quoted 3 lines

In kernel sockets are used broadly. I accept that it's more work for you but it's also the correct way to go about this if you want to send traffic from BPF.

proper sockets and full TCP/UDP back and forth is a different use case. Maybe it will work for Mahe/Liam. I don't know.
I'm arguing that emergency UDP is necessary too.
We can skip netpoll_send_udp() and let bpf core prepare skb earlier in a good context, then at div-by-0 time populate it with a message and call netpoll_send_skb().
Looks like netcons, bridge, vlan are using it, so why not let bpf core use it too?
__netpoll_send_skb() is the main value here because
it's all trylock based at every step.
The div-by-0 notification using normal sockets would have to be done async and that's the main downside.
We'd need to irq_work, then schedule_work and then send it via socket. Way too many steps to do in emergency. If sched-ext messed it up kthread may or may not wakeup.

maintainer
#12 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

Quoted 11 lines · On Tue, 12 May 2026 13:25:04 -0700 Alexei Starovoitov wrote:
Quoted 5 lines · Jakub Kicinski
Quoted 1 line

No. Nothing to do with 'user space prog'.

I'm sorry, are you saying that the motivation in the cover letter is not the motivation? It'd be great if someone could reveal the real motivation then.

AI makes commit logs sound very convincing, so I don't read them anymore to avoid bias. Only looking at the code.
And what I see in this set is a facility for emergency notifications that is safe to use in any context.

True, but I hope a real use case does exist for this given it's posted?

Quoted 7 lines

If people bolt crypto on top and make a toy prototype out of it it doesn't take away from usefulness of sending UDP packets out.

I want to use it out of bpf core to notify of things like divide by zero, arena fault, etc. Currently they are sent via bpf_stream, but user space has to be there to pick up from the stream.
We cannot use printk, since it's not safe.

printk is unsafe but netconsole is safe?

Quoted 8 lines

So netcons is the only option available today for 'bad things happen' notifications.
Theoretically con->write_atomic() can be wrapped in such helper/kfunc that both bpf core and bpf progs can call.
progs also need emergency notifications.
Like sched-ext might detect something bad and notify.
But at the end netconsole_write() is netpoll_send_udp(), so more natural and simpler to just use that.

This sounds very scary. I mean it sounds exacting and technically challenging, but I really don't want this to hinge on netpoll. netpoll is not a good solution to synchronization problems.

Quoted 4 lines
Quoted 3 lines

In kernel sockets are used broadly. I accept that it's more work for you but it's also the correct way to go about this if you want to send traffic from BPF.

Quoted 8 lines

proper sockets and full TCP/UDP back and forth is a different use case. Maybe it will work for Mahe/Liam. I don't know.
I'm arguing that emergency UDP is necessary too.
We can skip netpoll_send_udp() and let bpf core prepare skb earlier in a good context, then at div-by-0 time populate it with a message and call netpoll_send_skb().
Looks like netcons, bridge, vlan are using it, so why not let bpf core use it too?

Those drivers are not really using it, they are just trying to pass thru the skb to the lower dev. And I'd be surprised if most of that stuff even works, don't think it has any users. Most people try to pipe netcons to eth0.

Quoted 7 lines

__netpoll_send_skb() is the main value here because
it's all trylock based at every step.
The div-by-0 notification using normal sockets would have to be done async and that's the main downside.
We'd need to irq_work, then schedule_work and then send it via socket. Way too many steps to do in emergency. If sched-ext messed it up kthread may or may not wakeup.

I think you may be overly optimistic on the dependability of netpoll. Rik has added support for serial scraping at Meta because netconsole misses a lot of crucial alerts. And I can confirm there's a bunch of errors that appear in that dataset that are missed by netcons.

Then there are little nuggets like some generations of bnxt NICs having to discard all Rx frames when netcons is trying to send and Tx is full (bnxt_force_rx_discard()). netpoll is really meant for very narrow use cases, please trust me, opening it up for broader use is not going to end well.

#13 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

Quoted 9 lines · Jakub Kicinski

I think you may be overly optimistic on the dependability of netpoll. Rik has added support for serial scraping at Meta because netconsole misses a lot of crucial alerts. And I can confirm there's a bunch of errors that appear in that dataset that are missed by netcons.

Then there are little nuggets like some generations of bnxt NICs having to discard all Rx frames when netcons is trying to send and Tx is full (bnxt_force_rx_discard()).

horrors. since bnxt is doing that then indeed it's not something to rely on. I thought that over the years major vendors cleaned up the driver side well. Looks like it's still not the case. Sigh. Moving to sock_create_kern() and friends then.

reviewer
#14 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

Hi Jakub and Alexei,

Quoted 8 lines · Alexei Starovoitov
Quoted 6 lines · Jakub Kicinski

I think you may be overly optimistic on the dependability of netpoll. Rik has added support for serial scraping at Meta because netconsole misses a lot of crucial alerts. And I can confirm there's a bunch of errors that appear in that dataset that are missed by netcons.

Quoted 8 lines
Quoted 3 lines

Then there are little nuggets like some generations of bnxt NICs having to discard all Rx frames when netcons is trying to send and Tx is full (bnxt_force_rx_discard()).

horrors. since bnxt is doing that then indeed it's not something to rely on. I thought that over the years major vendors cleaned up the driver side well. Looks like it's still not the case. Sigh. Moving to sock_create_kern() and friends then.

Thanks for sharing these thoughts and background with netpoll. We will look into sock_create_kern() based solutions.

Song

#15 lore raw

Re: [PATCH v1 1/4] bpf: Add netpoll kfuncs for sending UDP packets

In reply to Jakub Kicinski
Quoted 4 lines · Jakub Kicinski

I think you may be overly optimistic on the dependability of netpoll. Rik has added support for serial scraping at Meta because netconsole misses a lot of crucial alerts. And I can confirm there's a bunch of errors that appear in that dataset that are missed by netcons.

Unrelated to the discussion above, I'm curious about the nature of these issues. Are the primary problems related to try-lock failures—where packets get queued after failing to acquire the TX queue lock, then fail again during queue_process due to the same lock contention?

author
#16 lore raw

PATCH 2/4 selftests/bpf: Add netpoll kfunc sanity test

In reply to Mahe Tardy

From: Song Liu <[email protected]>

Add a selftest that exercises the bpf_netpoll kfuncs end-to-end:

1. A SYSCALL BPF program creates a netpoll context on a dummy network device and stores it in a kptr map.
2. A fentry program is attached to dummy_xmit, the driver function that will receive the netpoll sent packet.
3. An LSM program attached to security_file_open retrieves the context from the map and calls bpf_netpoll_send_udp().

The userspace harness creates a network namespace with a dummy device, runs the setup program, attach the fentry hook then triggers the LSM hook by opening /dev/null. The dummy device accepts all packets, so send_status == 0 confirms success and the packet content is verified by reading the skb from the dummy driver xmit function.

The code was partially generated by AI and each line was manually reviewed.

---
 tools/testing/selftests/bpf/config            |   1 +
 .../selftests/bpf/prog_tests/netpoll.c        |  92 ++++++++++++++
 .../selftests/bpf/progs/netpoll_common.h      |  64 ++++++++++
 .../selftests/bpf/progs/netpoll_sanity.c      | 118 ++++++++++++++++++
 4 files changed, 275 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/netpoll.c
 create mode 100644 tools/testing/selftests/bpf/progs/netpoll_common.h
 create mode 100644 tools/testing/selftests/bpf/progs/netpoll_sanity.c
tools/testing/selftests/bpf/config +1 -0
index 24855381290d..bd6d0a4e5239 100644
@@ -7,6 +7,7 @@ CONFIG_BPF_JIT=y
 CONFIG_BPF_KPROBE_OVERRIDE=y
 CONFIG_BPF_LIRC_MODE2=y
 CONFIG_BPF_LSM=y
+CONFIG_BPF_NETPOLL=y
 CONFIG_BPF_STREAM_PARSER=y
 CONFIG_BPF_SYSCALL=y
 # CONFIG_BPF_UNPRIV_DEFAULT_OFF is not set
tools/testing/selftests/bpf/prog_tests/netpoll.c new file +92 -0

92 lines not on this page · raw message

tools/testing/selftests/bpf/progs/netpoll_common.h new file +64 -0

64 lines not on this page · raw message

tools/testing/selftests/bpf/progs/netpoll_sanity.c new file +118 -0

118 lines not on this page · raw message

--
2.34.1

author
#17 lore raw

PATCH 3/4 selftests/bpf: Add netpoll kfunc IPv6 variant test

In reply to Mahe Tardy

Add a selftest that exercises the bpf_netpoll kfuncs end-to-end with IPv6, reusing the code that was added for IPv4.

---
 .../selftests/bpf/prog_tests/netpoll.c        | 42 +++++++++++++++----
 .../selftests/bpf/progs/netpoll_sanity.c      | 33 +++++++++++----
 2 files changed, 58 insertions(+), 17 deletions(-)
tools/testing/selftests/bpf/prog_tests/netpoll.c +33 -9
index 1cfac4b13e7d..eac0378c426a 100644
@@ -9,15 +9,20 @@
 #include "netpoll_sanity.skel.h"
 
 #define NS_TEST "netpoll_sanity_ns"
+#define NS_TEST_V6 "netpoll_sanity_ns_v6"
 #define DUMMY_DEV "dummy0"
 #define DUMMY_IP "10.0.0.1"
 #define REMOTE_IP "10.0.0.2"
+#define DUMMY_IP6 "fd00::1"
+#define REMOTE_IP6 "fd00::2"
 
-void test_netpoll_sanity(void)
+static void run_netpoll_test(const char *ns_name, const char *local_ip,
+			      const char *remote_ip, bool ipv6)
 {
 	LIBBPF_OPTS(bpf_test_run_opts, opts);
 	struct nstoken *nstoken = NULL;
 	struct netpoll_sanity *skel;
+	struct in6_addr addr6;
 	int err, pfd, fd;
 
 	skel = netpoll_sanity__open_and_load();
@@ -25,18 +30,28 @@ void test_netpoll_sanity(void)
 		return;
 
 	/* Create a network namespace with a dummy device */
-	SYS(fail, "ip netns add %s", NS_TEST);
-	SYS(fail, "ip -net %s link add %s type dummy", NS_TEST, DUMMY_DEV);
-	SYS(fail, "ip -net %s addr add %s/24 dev %s", NS_TEST, DUMMY_IP, DUMMY_DEV);
-	SYS(fail, "ip -net %s link set %s up", NS_TEST, DUMMY_DEV);
-
-	nstoken = open_netns(NS_TEST);
+	SYS(fail, "ip netns add %s", ns_name);
+	SYS(fail, "ip -net %s link add %s type dummy", ns_name, DUMMY_DEV);
+	if (ipv6)
+		SYS(fail, "ip -net %s addr add %s/64 dev %s", ns_name, local_ip, DUMMY_DEV);
+	else
+		SYS(fail, "ip -net %s addr add %s/24 dev %s", ns_name, local_ip, DUMMY_DEV);
+	SYS(fail, "ip -net %s link set %s up", ns_name, DUMMY_DEV);
+
+	nstoken = open_netns(ns_name);
 	if (!ASSERT_OK_PTR(nstoken, "open_netns"))
 		goto fail;
 
 	/* Configure the BPF program globals */
 	snprintf(skel->bss->dev_name, sizeof(skel->bss->dev_name), "%s", DUMMY_DEV);
-	skel->bss->remote_ip = inet_addr(REMOTE_IP);
+	if (ipv6) {
+		if (inet_pton(AF_INET6, remote_ip, &addr6) != 1)
+			goto fail;
+		__builtin_memcpy(&skel->bss->remote_ip6, &addr6, sizeof(addr6));
+		skel->bss->ipv6 = 1;
+	} else {
+		skel->bss->remote_ip = inet_addr(remote_ip);
+	}
 	skel->bss->local_port = 5555;
 	skel->bss->remote_port = 6666;
 	skel->bss->remote_mac[0] = 0xaa;
@@ -87,6 +102,15 @@ void test_netpoll_sanity(void)
 fail:
 	if (nstoken)
 		close_netns(nstoken);
-	SYS_NOFAIL("ip netns del " NS_TEST " &> /dev/null");
+	SYS_NOFAIL("ip netns del %s &> /dev/null", ns_name);
 	netpoll_sanity__destroy(skel);
 }
+
+void test_netpoll_sanity(void)
+{
+	if (test__start_subtest("ipv4"))
+		run_netpoll_test(NS_TEST, DUMMY_IP, REMOTE_IP, false);
+
+	if (test__start_subtest("ipv6"))
+		run_netpoll_test(NS_TEST_V6, DUMMY_IP6, REMOTE_IP6, true);
+}
tools/testing/selftests/bpf/progs/netpoll_sanity.c +25 -8
index 9e1e595eff2c..26303632ebde 100644
@@ -12,9 +12,11 @@
 /* Globals for passing config from userspace */
 char dev_name[16] = {};
 __be32 remote_ip;
+struct in6_addr remote_ip6;
 __u16 local_port;
 __u16 remote_port;
 __u8 remote_mac[6] = {};
+int ipv6;
 
 /* Results */
 int status;
@@ -35,10 +37,14 @@ int netpoll_setup_test(void *ctx)
 	status = 0;
 
 	__builtin_memcpy(opts.dev_name, dev_name, 16);
-	opts.remote_ip = remote_ip;
+	if (ipv6)
+		__builtin_memcpy(&opts.remote_ip6, &remote_ip6, sizeof(remote_ip6));
+	else
+		opts.remote_ip = remote_ip;
 	opts.local_port = local_port;
 	opts.remote_port = remote_port;
 	__builtin_memcpy(opts.remote_mac, remote_mac, 6);
+	opts.ipv6 = ipv6;
 
 	bnp = bpf_netpoll_create(&opts, sizeof(opts), &err);
 	if (!bnp) {
@@ -88,7 +94,9 @@ int BPF_PROG(netpoll_dummy_xmit, struct sk_buff *skb, struct net_device *dev)
 	unsigned char *data;
 	struct ethhdr eth;
 	struct iphdr ip;
+	struct ipv6hdr ip6;
 	struct udphdr udp;
+	unsigned int offset;
 
 	if (bpf_probe_read_kernel(&data, sizeof(data), &skb->data) < 0)
 		return 0;
@@ -97,19 +105,28 @@ int BPF_PROG(netpoll_dummy_xmit, struct sk_buff *skb, struct net_device *dev)
 
 	if (bpf_probe_read_kernel(&eth, sizeof(eth), data) < 0)
 		return 0;
-	if (eth.h_proto != bpf_htons(ETH_P_IP))
-		return 0;
 
-	if (bpf_probe_read_kernel(&ip, sizeof(ip), data + sizeof(struct ethhdr)) < 0)
-		return 0;
-	if (ip.protocol != IPPROTO_UDP)
+	if (eth.h_proto == bpf_htons(ETH_P_IP)) {
+		if (bpf_probe_read_kernel(&ip, sizeof(ip), data + sizeof(struct ethhdr)) < 0)
+			return 0;
+		if (ip.protocol != IPPROTO_UDP)
+			return 0;
+		offset = sizeof(struct ethhdr) + (ip.ihl * 4);
+	} else if (eth.h_proto == bpf_htons(ETH_P_IPV6)) {
+		if (bpf_probe_read_kernel(&ip6, sizeof(ip6), data + sizeof(struct ethhdr)) < 0)
+			return 0;
+		if (ip6.nexthdr != IPPROTO_UDP)
+			return 0;
+		offset = sizeof(struct ethhdr) + sizeof(struct ipv6hdr);
+	} else {
 		return 0;
+	}
 
-	if (bpf_probe_read_kernel(&udp, sizeof(udp), data + sizeof(struct ethhdr) + (ip.ihl * 4)) < 0)
+	if (bpf_probe_read_kernel(&udp, sizeof(udp), data + offset) < 0)
 		return 0;
 	if (udp.dest != bpf_htons(remote_port))
 		return 0;
-	if (bpf_probe_read_kernel(&driver_xmit, sizeof(driver_xmit), data + sizeof(struct ethhdr) + (ip.ihl * 4) + sizeof(struct udphdr)) < 0)
+	if (bpf_probe_read_kernel(&driver_xmit, sizeof(driver_xmit), data + offset + sizeof(struct udphdr)) < 0)
 		return 0;
 
 	return 0;

--
2.34.1

author
#18 lore raw

PATCH 4/4 selftests/bpf: Add netpoll setup basic tests

In reply to Mahe Tardy

Add a pair of BPF progs, netpoll_release and netpoll_acquire to test that the respective bpf_netpoll_release and bpf_netpoll_acquire behave as expected and that the verifier has been configured to catch unreleased references.

---
 .../selftests/bpf/prog_tests/netpoll.c        | 23 +++++++++
 .../selftests/bpf/progs/netpoll_basic.c       | 47 +++++++++++++++++++
 2 files changed, 70 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/netpoll_basic.c
tools/testing/selftests/bpf/prog_tests/netpoll.c +23 -0
index eac0378c426a..daea1068a739 100644
@@ -7,15 +7,38 @@
 #include "test_progs.h"
 #include "network_helpers.h"
 #include "netpoll_sanity.skel.h"
+#include "netpoll_basic.skel.h"
 
 #define NS_TEST "netpoll_sanity_ns"
 #define NS_TEST_V6 "netpoll_sanity_ns_v6"
+#define NS_BASIC_TEST "netpoll_basic_ns"
 #define DUMMY_DEV "dummy0"
 #define DUMMY_IP "10.0.0.1"
 #define REMOTE_IP "10.0.0.2"
 #define DUMMY_IP6 "fd00::1"
 #define REMOTE_IP6 "fd00::2"
 
+void test_netpoll_basic(void)
+{
+	struct nstoken *nstoken = NULL;
+
+	SYS(fail, "ip netns add %s", NS_BASIC_TEST);
+	SYS(fail, "ip -net %s link add %s type dummy", NS_BASIC_TEST, DUMMY_DEV);
+	SYS(fail, "ip -net %s addr add %s/24 dev %s", NS_BASIC_TEST, DUMMY_IP, DUMMY_DEV);
+	SYS(fail, "ip -net %s link set %s up", NS_BASIC_TEST, DUMMY_DEV);
+
+	nstoken = open_netns(NS_BASIC_TEST);
+	if (!ASSERT_OK_PTR(nstoken, "open_netns"))
+		goto fail;
+
+	RUN_TESTS(netpoll_basic);
+
+fail:
+	if (nstoken)
+		close_netns(nstoken);
+	SYS_NOFAIL("ip netns del %s &> /dev/null", NS_BASIC_TEST);
+}
+
 static void run_netpoll_test(const char *ns_name, const char *local_ip,
 			      const char *remote_ip, bool ipv6)
 {
tools/testing/selftests/bpf/progs/netpoll_basic.c new file +47 -0

47 lines not on this page · raw message

--
2.34.1